2 * PCM - A-Law conversion
3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pcm_local.h"
24 #include "pcm_plugin.h"
26 typedef void (*alaw_f)(const snd_pcm_channel_area_t *dst_areas,
27 snd_pcm_uframes_t dst_offset,
28 const snd_pcm_channel_area_t *src_areas,
29 snd_pcm_uframes_t src_offset,
30 unsigned int channels, snd_pcm_uframes_t frames,
31 unsigned int getputidx);
34 /* This field need to be the first */
35 snd_pcm_plugin_t plug;
36 unsigned int getput_idx;
38 snd_pcm_format_t sformat;
41 static inline int val_seg(int val)
59 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
61 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
63 * Linear Input Code Compressed Code
64 * ------------------------ ---------------
65 * 0000000wxyza 000wxyz
66 * 0000001wxyza 001wxyz
67 * 000001wxyzab 010wxyz
68 * 00001wxyzabc 011wxyz
69 * 0001wxyzabcd 100wxyz
70 * 001wxyzabcde 101wxyz
71 * 01wxyzabcdef 110wxyz
72 * 1wxyzabcdefg 111wxyz
74 * For further information see John C. Bellamy's Digital Telephony, 1982,
75 * John Wiley & Sons, pps 98-111 and 472-476.
78 static unsigned char s16_to_alaw(int pcm_val)
96 /* Convert the scaled magnitude to segment number. */
97 seg = val_seg(pcm_val);
98 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
104 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
107 static int alaw_to_s16(unsigned char a_val)
117 seg = (t >> 4) & 0x07;
118 t = ((t & 0x0f) << 4) + 0x108;
121 return ((a_val & 0x80) ? t : -t);
124 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
125 snd_pcm_uframes_t dst_offset,
126 const snd_pcm_channel_area_t *src_areas,
127 snd_pcm_uframes_t src_offset,
128 unsigned int channels, snd_pcm_uframes_t frames,
132 #include "plugin_ops.h"
134 void *put = put16_labels[putidx];
135 unsigned int channel;
136 for (channel = 0; channel < channels; ++channel) {
137 const unsigned char *src;
139 int src_step, dst_step;
140 snd_pcm_uframes_t frames1;
141 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
142 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
143 src = snd_pcm_channel_area_addr(src_area, src_offset);
144 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
145 src_step = snd_pcm_channel_area_step(src_area);
146 dst_step = snd_pcm_channel_area_step(dst_area);
148 while (frames1-- > 0) {
149 int16_t sample = alaw_to_s16(*src);
151 #define PUT16_END after
152 #include "plugin_ops.h"
161 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
162 snd_pcm_uframes_t dst_offset,
163 const snd_pcm_channel_area_t *src_areas,
164 snd_pcm_uframes_t src_offset,
165 unsigned int channels, snd_pcm_uframes_t frames,
169 #include "plugin_ops.h"
171 void *get = get16_labels[getidx];
172 unsigned int channel;
174 for (channel = 0; channel < channels; ++channel) {
177 int src_step, dst_step;
178 snd_pcm_uframes_t frames1;
179 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
180 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
181 src = snd_pcm_channel_area_addr(src_area, src_offset);
182 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
183 src_step = snd_pcm_channel_area_step(src_area);
184 dst_step = snd_pcm_channel_area_step(dst_area);
186 while (frames1-- > 0) {
188 #define GET16_END after
189 #include "plugin_ops.h"
192 *dst = s16_to_alaw(sample);
199 static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
201 snd_pcm_alaw_t *alaw = pcm->private_data;
203 snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
204 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
208 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
209 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
210 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
213 err = _snd_pcm_hw_params_set_format(params,
214 SND_PCM_FORMAT_A_LAW);
218 err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
221 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
225 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
227 snd_pcm_alaw_t *alaw = pcm->private_data;
228 snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
229 _snd_pcm_hw_params_any(sparams);
230 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
232 _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
233 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
237 static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
238 snd_pcm_hw_params_t *sparams)
241 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
242 SND_PCM_HW_PARBIT_RATE |
243 SND_PCM_HW_PARBIT_PERIOD_SIZE |
244 SND_PCM_HW_PARBIT_BUFFER_SIZE |
245 SND_PCM_HW_PARBIT_PERIODS |
246 SND_PCM_HW_PARBIT_PERIOD_TIME |
247 SND_PCM_HW_PARBIT_BUFFER_TIME |
248 SND_PCM_HW_PARBIT_TICK_TIME);
249 err = _snd_pcm_hw_params_refine(sparams, links, params);
255 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
256 snd_pcm_hw_params_t *sparams)
259 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
260 SND_PCM_HW_PARBIT_RATE |
261 SND_PCM_HW_PARBIT_PERIOD_SIZE |
262 SND_PCM_HW_PARBIT_BUFFER_SIZE |
263 SND_PCM_HW_PARBIT_PERIODS |
264 SND_PCM_HW_PARBIT_PERIOD_TIME |
265 SND_PCM_HW_PARBIT_BUFFER_TIME |
266 SND_PCM_HW_PARBIT_TICK_TIME);
267 err = _snd_pcm_hw_params_refine(params, links, sparams);
273 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
275 return snd_pcm_hw_refine_slave(pcm, params,
276 snd_pcm_alaw_hw_refine_cprepare,
277 snd_pcm_alaw_hw_refine_cchange,
278 snd_pcm_alaw_hw_refine_sprepare,
279 snd_pcm_alaw_hw_refine_schange,
280 snd_pcm_plugin_hw_refine_slave);
283 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
285 snd_pcm_alaw_t *alaw = pcm->private_data;
286 int err = snd_pcm_hw_params_slave(pcm, params,
287 snd_pcm_alaw_hw_refine_cchange,
288 snd_pcm_alaw_hw_refine_sprepare,
289 snd_pcm_alaw_hw_refine_schange,
290 snd_pcm_plugin_hw_params_slave);
294 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
295 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
296 alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
297 alaw->func = snd_pcm_alaw_encode;
299 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
300 alaw->func = snd_pcm_alaw_decode;
303 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
304 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
305 alaw->func = snd_pcm_alaw_decode;
307 alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
308 alaw->func = snd_pcm_alaw_encode;
314 static snd_pcm_uframes_t
315 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
316 const snd_pcm_channel_area_t *areas,
317 snd_pcm_uframes_t offset,
318 snd_pcm_uframes_t size,
319 const snd_pcm_channel_area_t *slave_areas,
320 snd_pcm_uframes_t slave_offset,
321 snd_pcm_uframes_t *slave_sizep)
323 snd_pcm_alaw_t *alaw = pcm->private_data;
324 if (size > *slave_sizep)
326 alaw->func(slave_areas, slave_offset,
334 static snd_pcm_uframes_t
335 snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
336 const snd_pcm_channel_area_t *areas,
337 snd_pcm_uframes_t offset,
338 snd_pcm_uframes_t size,
339 const snd_pcm_channel_area_t *slave_areas,
340 snd_pcm_uframes_t slave_offset,
341 snd_pcm_uframes_t *slave_sizep)
343 snd_pcm_alaw_t *alaw = pcm->private_data;
344 if (size > *slave_sizep)
346 alaw->func(areas, offset,
347 slave_areas, slave_offset,
354 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
356 snd_pcm_alaw_t *alaw = pcm->private_data;
357 snd_output_printf(out, "A-Law conversion PCM (%s)\n",
358 snd_pcm_format_name(alaw->sformat));
360 snd_output_printf(out, "Its setup is:\n");
361 snd_pcm_dump_setup(pcm, out);
363 snd_output_printf(out, "Slave: ");
364 snd_pcm_dump(alaw->plug.slave, out);
367 snd_pcm_ops_t snd_pcm_alaw_ops = {
368 close: snd_pcm_plugin_close,
369 info: snd_pcm_plugin_info,
370 hw_refine: snd_pcm_alaw_hw_refine,
371 hw_params: snd_pcm_alaw_hw_params,
372 hw_free: snd_pcm_plugin_hw_free,
373 sw_params: snd_pcm_plugin_sw_params,
374 channel_info: snd_pcm_plugin_channel_info,
375 dump: snd_pcm_alaw_dump,
376 nonblock: snd_pcm_plugin_nonblock,
377 async: snd_pcm_plugin_async,
378 mmap: snd_pcm_plugin_mmap,
379 munmap: snd_pcm_plugin_munmap,
382 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
385 snd_pcm_alaw_t *alaw;
386 assert(pcmp && slave);
387 if (snd_pcm_format_linear(sformat) != 1 &&
388 sformat != SND_PCM_FORMAT_A_LAW)
390 alaw = calloc(1, sizeof(snd_pcm_alaw_t));
394 alaw->sformat = sformat;
395 alaw->plug.read = snd_pcm_alaw_read_areas;
396 alaw->plug.write = snd_pcm_alaw_write_areas;
397 alaw->plug.slave = slave;
398 alaw->plug.close_slave = close_slave;
400 pcm = calloc(1, sizeof(snd_pcm_t));
406 pcm->name = strdup(name);
407 pcm->type = SND_PCM_TYPE_ALAW;
408 pcm->stream = slave->stream;
409 pcm->mode = slave->mode;
410 pcm->ops = &snd_pcm_alaw_ops;
412 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
413 pcm->fast_op_arg = pcm;
414 pcm->private_data = alaw;
415 pcm->poll_fd = slave->poll_fd;
416 pcm->hw_ptr = &alaw->plug.hw_ptr;
417 pcm->appl_ptr = &alaw->plug.appl_ptr;
423 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
424 snd_config_t *root, snd_config_t *conf,
425 snd_pcm_stream_t stream, int mode)
427 snd_config_iterator_t i, next;
430 snd_config_t *slave = NULL, *sconf;
431 snd_pcm_format_t sformat;
433 snd_config_for_each(i, next, conf) {
434 snd_config_t *n = snd_config_iterator_entry(i);
435 const char *id = snd_config_get_id(n);
436 if (snd_pcm_conf_generic_id(id))
438 if (strcmp(id, "slave") == 0) {
442 SNDERR("Unknown field %s", id);
446 SNDERR("slave is not defined");
449 err = snd_pcm_slave_conf(root, slave, &sconf, &args, 1,
450 SND_PCM_HW_PARAM_FORMAT, 1, &sformat);
453 if (snd_pcm_format_linear(sformat) != 1 &&
454 sformat != SND_PCM_FORMAT_A_LAW) {
455 SNDERR("invalid slave format");
458 err = snd_pcm_open_slave(&spcm, root, sconf, args, stream, mode);
461 err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);