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_plugin_hw_refine_slave);
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 int err = snd_pcm_hw_params_slave(pcm, params,
307 snd_pcm_alaw_hw_refine_cchange,
308 snd_pcm_alaw_hw_refine_sprepare,
309 snd_pcm_alaw_hw_refine_schange,
310 snd_pcm_plugin_hw_params_slave);
314 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
315 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
316 alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
317 alaw->func = snd_pcm_alaw_encode;
319 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
320 alaw->func = snd_pcm_alaw_decode;
323 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
324 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
325 alaw->func = snd_pcm_alaw_decode;
327 alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
328 alaw->func = snd_pcm_alaw_encode;
334 static snd_pcm_uframes_t
335 snd_pcm_alaw_write_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(slave_areas, slave_offset,
354 static snd_pcm_uframes_t
355 snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
356 const snd_pcm_channel_area_t *areas,
357 snd_pcm_uframes_t offset,
358 snd_pcm_uframes_t size,
359 const snd_pcm_channel_area_t *slave_areas,
360 snd_pcm_uframes_t slave_offset,
361 snd_pcm_uframes_t *slave_sizep)
363 snd_pcm_alaw_t *alaw = pcm->private_data;
364 if (size > *slave_sizep)
366 alaw->func(areas, offset,
367 slave_areas, slave_offset,
374 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
376 snd_pcm_alaw_t *alaw = pcm->private_data;
377 snd_output_printf(out, "A-Law conversion PCM (%s)\n",
378 snd_pcm_format_name(alaw->sformat));
380 snd_output_printf(out, "Its setup is:\n");
381 snd_pcm_dump_setup(pcm, out);
383 snd_output_printf(out, "Slave: ");
384 snd_pcm_dump(alaw->plug.slave, out);
387 static snd_pcm_ops_t snd_pcm_alaw_ops = {
388 close: snd_pcm_plugin_close,
389 info: snd_pcm_plugin_info,
390 hw_refine: snd_pcm_alaw_hw_refine,
391 hw_params: snd_pcm_alaw_hw_params,
392 hw_free: snd_pcm_plugin_hw_free,
393 sw_params: snd_pcm_plugin_sw_params,
394 channel_info: snd_pcm_plugin_channel_info,
395 dump: snd_pcm_alaw_dump,
396 nonblock: snd_pcm_plugin_nonblock,
397 async: snd_pcm_plugin_async,
398 mmap: snd_pcm_plugin_mmap,
399 munmap: snd_pcm_plugin_munmap,
403 * \brief Creates a new A-Law conversion PCM
404 * \param pcmp Returns created PCM handle
405 * \param name Name of PCM
406 * \param sformat Slave (destination) format
407 * \param slave Slave PCM handle
408 * \param close_slave When set, the slave PCM handle is closed with copy PCM
409 * \retval zero on success otherwise a negative error code
410 * \warning Using of this function might be dangerous in the sense
411 * of compatibility reasons. The prototype might be freely
414 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
417 snd_pcm_alaw_t *alaw;
419 assert(pcmp && slave);
420 if (snd_pcm_format_linear(sformat) != 1 &&
421 sformat != SND_PCM_FORMAT_A_LAW)
423 alaw = calloc(1, sizeof(snd_pcm_alaw_t));
427 alaw->sformat = sformat;
428 alaw->plug.read = snd_pcm_alaw_read_areas;
429 alaw->plug.write = snd_pcm_alaw_write_areas;
430 alaw->plug.slave = slave;
431 alaw->plug.close_slave = close_slave;
433 err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
438 pcm->ops = &snd_pcm_alaw_ops;
439 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
440 pcm->private_data = alaw;
441 pcm->poll_fd = slave->poll_fd;
442 pcm->hw_ptr = &alaw->plug.hw_ptr;
443 pcm->appl_ptr = &alaw->plug.appl_ptr;
449 /*! \page pcm_plugins
451 \section pcm_plugins_alaw Plugin: A-Law
453 This plugin converts A-Law samples to linear or linear to A-Law samples
454 from master A-Law conversion PCM to given slave PCM. The channel count,
455 format and rate must match for both of them.
459 type alaw # A-Law conversion PCM
460 slave STR # Slave name
462 slave { # Slave definition
463 pcm STR # Slave PCM name
465 pcm { } # Slave PCM definition
470 \subsection pcm_plugins_alaw_funcref Function reference
473 <LI>snd_pcm_alaw_open()
474 <LI>_snd_pcm_alaw_open()
480 * \brief Creates a new A-Law conversion PCM
481 * \param pcmp Returns created PCM handle
482 * \param name Name of PCM
483 * \param root Root configuration node
484 * \param conf Configuration node with copy PCM description
485 * \param stream Stream type
486 * \param mode Stream mode
487 * \retval zero on success otherwise a negative error code
488 * \warning Using of this function might be dangerous in the sense
489 * of compatibility reasons. The prototype might be freely
492 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
493 snd_config_t *root, snd_config_t *conf,
494 snd_pcm_stream_t stream, int mode)
496 snd_config_iterator_t i, next;
499 snd_config_t *slave = NULL, *sconf;
500 snd_pcm_format_t sformat;
501 snd_config_for_each(i, next, conf) {
502 snd_config_t *n = snd_config_iterator_entry(i);
504 if (snd_config_get_id(n, &id) < 0)
506 if (snd_pcm_conf_generic_id(id))
508 if (strcmp(id, "slave") == 0) {
512 SNDERR("Unknown field %s", id);
516 SNDERR("slave is not defined");
519 err = snd_pcm_slave_conf(root, slave, &sconf, 1,
520 SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
523 if (snd_pcm_format_linear(sformat) != 1 &&
524 sformat != SND_PCM_FORMAT_A_LAW) {
525 snd_config_delete(sconf);
526 SNDERR("invalid slave format");
529 err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
530 snd_config_delete(sconf);
533 err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
539 SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);