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, int getputidx);
33 /* This field need to be the first */
34 snd_pcm_plugin_t plug;
37 snd_pcm_format_t sformat;
40 static inline int val_seg(int val)
58 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
60 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
62 * Linear Input Code Compressed Code
63 * ------------------------ ---------------
64 * 0000000wxyza 000wxyz
65 * 0000001wxyza 001wxyz
66 * 000001wxyzab 010wxyz
67 * 00001wxyzabc 011wxyz
68 * 0001wxyzabcd 100wxyz
69 * 001wxyzabcde 101wxyz
70 * 01wxyzabcdef 110wxyz
71 * 1wxyzabcdefg 111wxyz
73 * For further information see John C. Bellamy's Digital Telephony, 1982,
74 * John Wiley & Sons, pps 98-111 and 472-476.
77 static unsigned char s16_to_alaw(int pcm_val)
95 /* Convert the scaled magnitude to segment number. */
96 seg = val_seg(pcm_val);
97 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
103 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
106 static int alaw_to_s16(unsigned char a_val)
116 seg = (t >> 4) & 0x07;
117 t = ((t & 0x0f) << 4) + 0x108;
120 return ((a_val & 0x80) ? t : -t);
123 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
124 snd_pcm_uframes_t dst_offset,
125 const snd_pcm_channel_area_t *src_areas,
126 snd_pcm_uframes_t src_offset,
127 unsigned int channels, snd_pcm_uframes_t frames, int putidx)
130 #include "plugin_ops.h"
132 void *put = put16_labels[putidx];
133 unsigned int channel;
134 for (channel = 0; channel < channels; ++channel) {
137 int src_step, dst_step;
138 snd_pcm_uframes_t frames1;
139 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
140 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
142 if (!src_area->enabled) {
143 if (dst_area->wanted)
144 snd_pcm_area_silence(&dst_areas[channel], dst_offset, frames, dst_sfmt);
145 dst_area->enabled = 0;
148 dst_area->enabled = 1;
150 src = snd_pcm_channel_area_addr(src_area, src_offset);
151 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
152 src_step = snd_pcm_channel_area_step(src_area);
153 dst_step = snd_pcm_channel_area_step(dst_area);
155 while (frames1-- > 0) {
156 int16_t sample = alaw_to_s16(*src);
158 #define PUT16_END after
159 #include "plugin_ops.h"
168 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
169 snd_pcm_uframes_t dst_offset,
170 const snd_pcm_channel_area_t *src_areas,
171 snd_pcm_uframes_t src_offset,
172 unsigned int channels, snd_pcm_uframes_t frames, int getidx)
175 #include "plugin_ops.h"
177 void *get = get16_labels[getidx];
178 unsigned int channel;
180 for (channel = 0; channel < channels; ++channel) {
183 int src_step, dst_step;
184 snd_pcm_uframes_t frames1;
185 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
186 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
188 if (!src_area->enabled) {
189 if (dst_area->wanted)
190 snd_pcm_area_silence(&dst_area->area, 0, frames, dst_sfmt);
191 dst_area->enabled = 0;
194 dst_area->enabled = 1;
196 src = snd_pcm_channel_area_addr(src_area, src_offset);
197 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
198 src_step = snd_pcm_channel_area_step(src_area);
199 dst_step = snd_pcm_channel_area_step(dst_area);
201 while (frames1-- > 0) {
203 #define GET16_END after
204 #include "plugin_ops.h"
207 *dst = s16_to_alaw(sample);
214 static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
216 snd_pcm_alaw_t *alaw = pcm->private;
218 snd_pcm_access_mask_t *access_mask = alloca(snd_pcm_access_mask_sizeof());
219 snd_mask_load(access_mask, SND_PCM_ACCBIT_PLUGIN);
220 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
224 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
225 snd_pcm_format_mask_t *format_mask = alloca(snd_pcm_format_mask_sizeof());
226 snd_mask_load(format_mask, SND_PCM_FMTBIT_LINEAR);
227 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
230 err = _snd_pcm_hw_params_set_format(params,
231 SND_PCM_FORMAT_A_LAW);
235 err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
238 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
242 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
244 snd_pcm_alaw_t *alaw = pcm->private;
245 snd_pcm_access_mask_t *saccess_mask = alloca(snd_pcm_access_mask_sizeof());
246 snd_mask_load(saccess_mask, SND_PCM_ACCBIT_MMAP);
247 _snd_pcm_hw_params_any(sparams);
248 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
250 _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
251 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
255 static int snd_pcm_alaw_hw_refine_schange(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(sparams, links, params);
273 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
274 snd_pcm_hw_params_t *sparams)
277 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
278 SND_PCM_HW_PARBIT_RATE |
279 SND_PCM_HW_PARBIT_PERIOD_SIZE |
280 SND_PCM_HW_PARBIT_BUFFER_SIZE |
281 SND_PCM_HW_PARBIT_PERIODS |
282 SND_PCM_HW_PARBIT_PERIOD_TIME |
283 SND_PCM_HW_PARBIT_BUFFER_TIME |
284 SND_PCM_HW_PARBIT_TICK_TIME);
285 err = _snd_pcm_hw_params_refine(params, links, sparams);
291 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
293 return snd_pcm_hw_refine_slave(pcm, params,
294 snd_pcm_alaw_hw_refine_cprepare,
295 snd_pcm_alaw_hw_refine_cchange,
296 snd_pcm_alaw_hw_refine_sprepare,
297 snd_pcm_alaw_hw_refine_schange,
298 snd_pcm_plugin_hw_refine_slave);
301 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
303 snd_pcm_alaw_t *alaw = pcm->private;
304 int err = snd_pcm_hw_params_slave(pcm, params,
305 snd_pcm_alaw_hw_refine_cchange,
306 snd_pcm_alaw_hw_refine_sprepare,
307 snd_pcm_alaw_hw_refine_schange,
308 snd_pcm_plugin_hw_params_slave);
312 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
313 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
314 alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
315 alaw->func = snd_pcm_alaw_encode;
317 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
318 alaw->func = snd_pcm_alaw_decode;
321 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
322 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
323 alaw->func = snd_pcm_alaw_decode;
325 alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
326 alaw->func = snd_pcm_alaw_encode;
332 static snd_pcm_sframes_t snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
333 const snd_pcm_channel_area_t *areas,
334 snd_pcm_uframes_t offset,
335 snd_pcm_uframes_t size,
336 snd_pcm_uframes_t *slave_sizep)
338 snd_pcm_alaw_t *alaw = pcm->private;
339 snd_pcm_t *slave = alaw->plug.slave;
340 snd_pcm_uframes_t xfer = 0;
341 snd_pcm_sframes_t err = 0;
342 if (slave_sizep && *slave_sizep < size)
345 while (xfer < size) {
346 snd_pcm_uframes_t frames = snd_pcm_mmap_playback_xfer(slave, size - xfer);
347 alaw->func(snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
349 pcm->channels, frames,
351 err = snd_pcm_mmap_forward(slave, frames);
354 assert((snd_pcm_uframes_t)err == frames);
357 snd_pcm_mmap_hw_forward(pcm, err);
367 static snd_pcm_sframes_t snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
368 const snd_pcm_channel_area_t *areas,
369 snd_pcm_uframes_t offset,
370 snd_pcm_uframes_t size,
371 snd_pcm_uframes_t *slave_sizep)
373 snd_pcm_alaw_t *alaw = pcm->private;
374 snd_pcm_t *slave = alaw->plug.slave;
375 snd_pcm_uframes_t xfer = 0;
376 snd_pcm_sframes_t err = 0;
377 if (slave_sizep && *slave_sizep < size)
380 while (xfer < size) {
381 snd_pcm_uframes_t frames = snd_pcm_mmap_capture_xfer(slave, size - xfer);
382 alaw->func(areas, offset,
383 snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
384 pcm->channels, frames,
386 err = snd_pcm_mmap_forward(slave, frames);
389 assert((snd_pcm_uframes_t)err == frames);
392 snd_pcm_mmap_hw_forward(pcm, err);
402 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
404 snd_pcm_alaw_t *alaw = pcm->private;
405 snd_output_printf(out, "A-Law conversion PCM (%s)\n",
406 snd_pcm_format_name(alaw->sformat));
408 snd_output_printf(out, "Its setup is:\n");
409 snd_pcm_dump_setup(pcm, out);
411 snd_output_printf(out, "Slave: ");
412 snd_pcm_dump(alaw->plug.slave, out);
415 snd_pcm_ops_t snd_pcm_alaw_ops = {
416 close: snd_pcm_plugin_close,
417 info: snd_pcm_plugin_info,
418 hw_refine: snd_pcm_alaw_hw_refine,
419 hw_params: snd_pcm_alaw_hw_params,
420 hw_free: snd_pcm_plugin_hw_free,
421 sw_params: snd_pcm_plugin_sw_params,
422 channel_info: snd_pcm_plugin_channel_info,
423 dump: snd_pcm_alaw_dump,
424 nonblock: snd_pcm_plugin_nonblock,
425 async: snd_pcm_plugin_async,
426 mmap: snd_pcm_plugin_mmap,
427 munmap: snd_pcm_plugin_munmap,
430 int snd_pcm_alaw_open(snd_pcm_t **pcmp, char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
433 snd_pcm_alaw_t *alaw;
434 assert(pcmp && slave);
435 if (snd_pcm_format_linear(sformat) != 1 &&
436 sformat != SND_PCM_FORMAT_A_LAW)
438 alaw = calloc(1, sizeof(snd_pcm_alaw_t));
442 alaw->sformat = sformat;
443 alaw->plug.read = snd_pcm_alaw_read_areas;
444 alaw->plug.write = snd_pcm_alaw_write_areas;
445 alaw->plug.slave = slave;
446 alaw->plug.close_slave = close_slave;
448 pcm = calloc(1, sizeof(snd_pcm_t));
454 pcm->name = strdup(name);
455 pcm->type = SND_PCM_TYPE_ALAW;
456 pcm->stream = slave->stream;
457 pcm->mode = slave->mode;
458 pcm->ops = &snd_pcm_alaw_ops;
460 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
461 pcm->fast_op_arg = pcm;
463 pcm->poll_fd = slave->poll_fd;
464 pcm->hw_ptr = &alaw->plug.hw_ptr;
465 pcm->appl_ptr = &alaw->plug.appl_ptr;
471 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, char *name,
473 snd_pcm_stream_t stream, int mode)
475 snd_config_iterator_t i;
479 snd_pcm_format_t sformat = SND_PCM_FORMAT_NONE;
480 snd_config_foreach(i, conf) {
481 snd_config_t *n = snd_config_entry(i);
482 if (strcmp(n->id, "comment") == 0)
484 if (strcmp(n->id, "type") == 0)
486 if (strcmp(n->id, "stream") == 0)
488 if (strcmp(n->id, "sname") == 0) {
489 err = snd_config_string_get(n, &sname);
491 ERR("Invalid type for %s", n->id);
496 if (strcmp(n->id, "sformat") == 0) {
498 err = snd_config_string_get(n, &f);
500 ERR("Invalid type for %s", n->id);
503 sformat = snd_pcm_format_value(f);
504 if (sformat == SND_PCM_FORMAT_NONE) {
505 ERR("Unknown sformat");
508 if (snd_pcm_format_linear(sformat) != 1 &&
509 sformat != SND_PCM_FORMAT_A_LAW) {
510 ERR("Invalid sformat");
515 ERR("Unknown field %s", n->id);
519 ERR("sname is not defined");
522 if (sformat == SND_PCM_FORMAT_NONE) {
523 ERR("sformat is not defined");
526 /* This is needed cause snd_config_update may destroy config */
527 sname = strdup(sname);
530 err = snd_pcm_open(&spcm, sname, stream, mode);
534 err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);