4 * \brief PCM A-Law Conversion Plugin Interface
5 * \author Abramo Bagnara <abramo@alsa-project.org>
9 * PCM - A-Law conversion
10 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
13 * This library is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as
15 * published by the Free Software Foundation; either version 2.1 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "pcm_local.h"
31 #include "pcm_plugin.h"
34 /* entry for static linking */
35 const char *_snd_module_pcm_alaw = "";
40 typedef void (*alaw_f)(const snd_pcm_channel_area_t *dst_areas,
41 snd_pcm_uframes_t dst_offset,
42 const snd_pcm_channel_area_t *src_areas,
43 snd_pcm_uframes_t src_offset,
44 unsigned int channels, snd_pcm_uframes_t frames,
45 unsigned int getputidx);
48 /* This field need to be the first */
49 snd_pcm_plugin_t plug;
50 unsigned int getput_idx;
52 snd_pcm_format_t sformat;
57 static inline int val_seg(int val)
75 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
77 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
79 * Linear Input Code Compressed Code
80 * ------------------------ ---------------
81 * 0000000wxyza 000wxyz
82 * 0000001wxyza 001wxyz
83 * 000001wxyzab 010wxyz
84 * 00001wxyzabc 011wxyz
85 * 0001wxyzabcd 100wxyz
86 * 001wxyzabcde 101wxyz
87 * 01wxyzabcdef 110wxyz
88 * 1wxyzabcdefg 111wxyz
90 * For further information see John C. Bellamy's Digital Telephony, 1982,
91 * John Wiley & Sons, pps 98-111 and 472-476.
94 static unsigned char s16_to_alaw(int pcm_val)
105 if (pcm_val > 0x7fff)
112 /* Convert the scaled magnitude to segment number. */
113 seg = val_seg(pcm_val);
114 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
120 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
123 static int alaw_to_s16(unsigned char a_val)
133 seg = (t >> 4) & 0x07;
134 t = ((t & 0x0f) << 4) + 0x108;
137 return ((a_val & 0x80) ? t : -t);
142 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
143 snd_pcm_uframes_t dst_offset,
144 const snd_pcm_channel_area_t *src_areas,
145 snd_pcm_uframes_t src_offset,
146 unsigned int channels, snd_pcm_uframes_t frames,
150 #include "plugin_ops.h"
152 void *put = put16_labels[putidx];
153 unsigned int channel;
154 for (channel = 0; channel < channels; ++channel) {
155 const unsigned char *src;
157 int src_step, dst_step;
158 snd_pcm_uframes_t frames1;
159 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
160 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
161 src = snd_pcm_channel_area_addr(src_area, src_offset);
162 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
163 src_step = snd_pcm_channel_area_step(src_area);
164 dst_step = snd_pcm_channel_area_step(dst_area);
166 while (frames1-- > 0) {
167 int16_t sample = alaw_to_s16(*src);
169 #define PUT16_END after
170 #include "plugin_ops.h"
179 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
180 snd_pcm_uframes_t dst_offset,
181 const snd_pcm_channel_area_t *src_areas,
182 snd_pcm_uframes_t src_offset,
183 unsigned int channels, snd_pcm_uframes_t frames,
187 #include "plugin_ops.h"
189 void *get = get16_labels[getidx];
190 unsigned int channel;
192 for (channel = 0; channel < channels; ++channel) {
195 int src_step, dst_step;
196 snd_pcm_uframes_t frames1;
197 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
198 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
199 src = snd_pcm_channel_area_addr(src_area, src_offset);
200 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
201 src_step = snd_pcm_channel_area_step(src_area);
202 dst_step = snd_pcm_channel_area_step(dst_area);
204 while (frames1-- > 0) {
206 #define GET16_END after
207 #include "plugin_ops.h"
210 *dst = s16_to_alaw(sample);
217 #endif /* DOC_HIDDEN */
219 static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
221 snd_pcm_alaw_t *alaw = pcm->private_data;
223 snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
224 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
228 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
229 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
230 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
233 err = _snd_pcm_hw_params_set_format(params,
234 SND_PCM_FORMAT_A_LAW);
238 err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
241 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
245 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
247 snd_pcm_alaw_t *alaw = pcm->private_data;
248 snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
249 _snd_pcm_hw_params_any(sparams);
250 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
252 _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
253 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
257 static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
258 snd_pcm_hw_params_t *sparams)
261 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
262 SND_PCM_HW_PARBIT_RATE |
263 SND_PCM_HW_PARBIT_PERIOD_SIZE |
264 SND_PCM_HW_PARBIT_BUFFER_SIZE |
265 SND_PCM_HW_PARBIT_PERIODS |
266 SND_PCM_HW_PARBIT_PERIOD_TIME |
267 SND_PCM_HW_PARBIT_BUFFER_TIME |
268 SND_PCM_HW_PARBIT_TICK_TIME);
269 err = _snd_pcm_hw_params_refine(sparams, links, params);
275 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
276 snd_pcm_hw_params_t *sparams)
279 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
280 SND_PCM_HW_PARBIT_RATE |
281 SND_PCM_HW_PARBIT_PERIOD_SIZE |
282 SND_PCM_HW_PARBIT_BUFFER_SIZE |
283 SND_PCM_HW_PARBIT_PERIODS |
284 SND_PCM_HW_PARBIT_PERIOD_TIME |
285 SND_PCM_HW_PARBIT_BUFFER_TIME |
286 SND_PCM_HW_PARBIT_TICK_TIME);
287 err = _snd_pcm_hw_params_refine(params, links, sparams);
293 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
295 return snd_pcm_hw_refine_slave(pcm, params,
296 snd_pcm_alaw_hw_refine_cprepare,
297 snd_pcm_alaw_hw_refine_cchange,
298 snd_pcm_alaw_hw_refine_sprepare,
299 snd_pcm_alaw_hw_refine_schange,
300 snd_pcm_generic_hw_refine);
303 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
305 snd_pcm_alaw_t *alaw = pcm->private_data;
306 snd_pcm_format_t format;
307 int err = snd_pcm_hw_params_slave(pcm, params,
308 snd_pcm_alaw_hw_refine_cchange,
309 snd_pcm_alaw_hw_refine_sprepare,
310 snd_pcm_alaw_hw_refine_schange,
311 snd_pcm_generic_hw_params);
315 err = INTERNAL(snd_pcm_hw_params_get_format)(params, &format);
319 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
320 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
321 alaw->getput_idx = snd_pcm_linear_get_index(format, SND_PCM_FORMAT_S16);
322 alaw->func = snd_pcm_alaw_encode;
324 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
325 alaw->func = snd_pcm_alaw_decode;
328 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
329 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, format);
330 alaw->func = snd_pcm_alaw_decode;
332 alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
333 alaw->func = snd_pcm_alaw_encode;
339 static snd_pcm_uframes_t
340 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
341 const snd_pcm_channel_area_t *areas,
342 snd_pcm_uframes_t offset,
343 snd_pcm_uframes_t size,
344 const snd_pcm_channel_area_t *slave_areas,
345 snd_pcm_uframes_t slave_offset,
346 snd_pcm_uframes_t *slave_sizep)
348 snd_pcm_alaw_t *alaw = pcm->private_data;
349 if (size > *slave_sizep)
351 alaw->func(slave_areas, slave_offset,
359 static snd_pcm_uframes_t
360 snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
361 const snd_pcm_channel_area_t *areas,
362 snd_pcm_uframes_t offset,
363 snd_pcm_uframes_t size,
364 const snd_pcm_channel_area_t *slave_areas,
365 snd_pcm_uframes_t slave_offset,
366 snd_pcm_uframes_t *slave_sizep)
368 snd_pcm_alaw_t *alaw = pcm->private_data;
369 if (size > *slave_sizep)
371 alaw->func(areas, offset,
372 slave_areas, slave_offset,
379 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
381 snd_pcm_alaw_t *alaw = pcm->private_data;
382 snd_output_printf(out, "A-Law conversion PCM (%s)\n",
383 snd_pcm_format_name(alaw->sformat));
385 snd_output_printf(out, "Its setup is:\n");
386 snd_pcm_dump_setup(pcm, out);
388 snd_output_printf(out, "Slave: ");
389 snd_pcm_dump(alaw->plug.gen.slave, out);
392 static snd_pcm_ops_t snd_pcm_alaw_ops = {
393 .close = snd_pcm_generic_close,
394 .info = snd_pcm_generic_info,
395 .hw_refine = snd_pcm_alaw_hw_refine,
396 .hw_params = snd_pcm_alaw_hw_params,
397 .hw_free = snd_pcm_generic_hw_free,
398 .sw_params = snd_pcm_generic_sw_params,
399 .channel_info = snd_pcm_generic_channel_info,
400 .dump = snd_pcm_alaw_dump,
401 .nonblock = snd_pcm_generic_nonblock,
402 .async = snd_pcm_generic_async,
403 .mmap = snd_pcm_generic_mmap,
404 .munmap = snd_pcm_generic_munmap,
408 * \brief Creates a new A-Law conversion PCM
409 * \param pcmp Returns created PCM handle
410 * \param name Name of PCM
411 * \param sformat Slave (destination) format
412 * \param slave Slave PCM handle
413 * \param close_slave When set, the slave PCM handle is closed with copy PCM
414 * \retval zero on success otherwise a negative error code
415 * \warning Using of this function might be dangerous in the sense
416 * of compatibility reasons. The prototype might be freely
419 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
422 snd_pcm_alaw_t *alaw;
424 assert(pcmp && slave);
425 if (snd_pcm_format_linear(sformat) != 1 &&
426 sformat != SND_PCM_FORMAT_A_LAW)
428 alaw = calloc(1, sizeof(snd_pcm_alaw_t));
432 snd_pcm_plugin_init(&alaw->plug);
433 alaw->sformat = sformat;
434 alaw->plug.read = snd_pcm_alaw_read_areas;
435 alaw->plug.write = snd_pcm_alaw_write_areas;
436 alaw->plug.undo_read = snd_pcm_plugin_undo_read_generic;
437 alaw->plug.undo_write = snd_pcm_plugin_undo_write_generic;
438 alaw->plug.gen.slave = slave;
439 alaw->plug.gen.close_slave = close_slave;
441 err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
446 pcm->ops = &snd_pcm_alaw_ops;
447 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
448 pcm->private_data = alaw;
449 pcm->poll_fd = slave->poll_fd;
450 pcm->poll_events = slave->poll_events;
451 snd_pcm_set_hw_ptr(pcm, &alaw->plug.hw_ptr, -1, 0);
452 snd_pcm_set_appl_ptr(pcm, &alaw->plug.appl_ptr, -1, 0);
458 /*! \page pcm_plugins
460 \section pcm_plugins_alaw Plugin: A-Law
462 This plugin converts A-Law samples to linear or linear to A-Law samples
463 from master A-Law conversion PCM to given slave PCM. The channel count,
464 format and rate must match for both of them.
468 type alaw # A-Law conversion PCM
469 slave STR # Slave name
471 slave { # Slave definition
472 pcm STR # Slave PCM name
474 pcm { } # Slave PCM definition
475 format STR # Slave format
480 \subsection pcm_plugins_alaw_funcref Function reference
483 <LI>snd_pcm_alaw_open()
484 <LI>_snd_pcm_alaw_open()
490 * \brief Creates a new A-Law conversion PCM
491 * \param pcmp Returns created PCM handle
492 * \param name Name of PCM
493 * \param root Root configuration node
494 * \param conf Configuration node with copy PCM description
495 * \param stream Stream type
496 * \param mode Stream mode
497 * \retval zero on success otherwise a negative error code
498 * \warning Using of this function might be dangerous in the sense
499 * of compatibility reasons. The prototype might be freely
502 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
503 snd_config_t *root, snd_config_t *conf,
504 snd_pcm_stream_t stream, int mode)
506 snd_config_iterator_t i, next;
509 snd_config_t *slave = NULL, *sconf;
510 snd_pcm_format_t sformat;
511 snd_config_for_each(i, next, conf) {
512 snd_config_t *n = snd_config_iterator_entry(i);
514 if (snd_config_get_id(n, &id) < 0)
516 if (snd_pcm_conf_generic_id(id))
518 if (strcmp(id, "slave") == 0) {
522 SNDERR("Unknown field %s", id);
526 SNDERR("slave is not defined");
529 err = snd_pcm_slave_conf(root, slave, &sconf, 1,
530 SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
533 if (snd_pcm_format_linear(sformat) != 1 &&
534 sformat != SND_PCM_FORMAT_A_LAW) {
535 snd_config_delete(sconf);
536 SNDERR("invalid slave format");
539 err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
540 snd_config_delete(sconf);
543 err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
549 SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);