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"
27 /* entry for static linking */
28 const char *_snd_module_pcm_alaw = "";
31 typedef void (*alaw_f)(const snd_pcm_channel_area_t *dst_areas,
32 snd_pcm_uframes_t dst_offset,
33 const snd_pcm_channel_area_t *src_areas,
34 snd_pcm_uframes_t src_offset,
35 unsigned int channels, snd_pcm_uframes_t frames,
36 unsigned int getputidx);
39 /* This field need to be the first */
40 snd_pcm_plugin_t plug;
41 unsigned int getput_idx;
43 snd_pcm_format_t sformat;
46 static inline int val_seg(int val)
64 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
66 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
68 * Linear Input Code Compressed Code
69 * ------------------------ ---------------
70 * 0000000wxyza 000wxyz
71 * 0000001wxyza 001wxyz
72 * 000001wxyzab 010wxyz
73 * 00001wxyzabc 011wxyz
74 * 0001wxyzabcd 100wxyz
75 * 001wxyzabcde 101wxyz
76 * 01wxyzabcdef 110wxyz
77 * 1wxyzabcdefg 111wxyz
79 * For further information see John C. Bellamy's Digital Telephony, 1982,
80 * John Wiley & Sons, pps 98-111 and 472-476.
83 static unsigned char s16_to_alaw(int pcm_val)
101 /* Convert the scaled magnitude to segment number. */
102 seg = val_seg(pcm_val);
103 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
109 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
112 static int alaw_to_s16(unsigned char a_val)
122 seg = (t >> 4) & 0x07;
123 t = ((t & 0x0f) << 4) + 0x108;
126 return ((a_val & 0x80) ? t : -t);
129 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
130 snd_pcm_uframes_t dst_offset,
131 const snd_pcm_channel_area_t *src_areas,
132 snd_pcm_uframes_t src_offset,
133 unsigned int channels, snd_pcm_uframes_t frames,
137 #include "plugin_ops.h"
139 void *put = put16_labels[putidx];
140 unsigned int channel;
141 for (channel = 0; channel < channels; ++channel) {
142 const unsigned char *src;
144 int src_step, dst_step;
145 snd_pcm_uframes_t frames1;
146 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
147 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
148 src = snd_pcm_channel_area_addr(src_area, src_offset);
149 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
150 src_step = snd_pcm_channel_area_step(src_area);
151 dst_step = snd_pcm_channel_area_step(dst_area);
153 while (frames1-- > 0) {
154 int16_t sample = alaw_to_s16(*src);
156 #define PUT16_END after
157 #include "plugin_ops.h"
166 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
167 snd_pcm_uframes_t dst_offset,
168 const snd_pcm_channel_area_t *src_areas,
169 snd_pcm_uframes_t src_offset,
170 unsigned int channels, snd_pcm_uframes_t frames,
174 #include "plugin_ops.h"
176 void *get = get16_labels[getidx];
177 unsigned int channel;
179 for (channel = 0; channel < channels; ++channel) {
182 int src_step, dst_step;
183 snd_pcm_uframes_t frames1;
184 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
185 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
186 src = snd_pcm_channel_area_addr(src_area, src_offset);
187 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
188 src_step = snd_pcm_channel_area_step(src_area);
189 dst_step = snd_pcm_channel_area_step(dst_area);
191 while (frames1-- > 0) {
193 #define GET16_END after
194 #include "plugin_ops.h"
197 *dst = s16_to_alaw(sample);
204 static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
206 snd_pcm_alaw_t *alaw = pcm->private_data;
208 snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
209 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
213 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
214 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
215 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
218 err = _snd_pcm_hw_params_set_format(params,
219 SND_PCM_FORMAT_A_LAW);
223 err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
226 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
230 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
232 snd_pcm_alaw_t *alaw = pcm->private_data;
233 snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
234 _snd_pcm_hw_params_any(sparams);
235 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
237 _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
238 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
242 static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
243 snd_pcm_hw_params_t *sparams)
246 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
247 SND_PCM_HW_PARBIT_RATE |
248 SND_PCM_HW_PARBIT_PERIOD_SIZE |
249 SND_PCM_HW_PARBIT_BUFFER_SIZE |
250 SND_PCM_HW_PARBIT_PERIODS |
251 SND_PCM_HW_PARBIT_PERIOD_TIME |
252 SND_PCM_HW_PARBIT_BUFFER_TIME |
253 SND_PCM_HW_PARBIT_TICK_TIME);
254 err = _snd_pcm_hw_params_refine(sparams, links, params);
260 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
261 snd_pcm_hw_params_t *sparams)
264 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
265 SND_PCM_HW_PARBIT_RATE |
266 SND_PCM_HW_PARBIT_PERIOD_SIZE |
267 SND_PCM_HW_PARBIT_BUFFER_SIZE |
268 SND_PCM_HW_PARBIT_PERIODS |
269 SND_PCM_HW_PARBIT_PERIOD_TIME |
270 SND_PCM_HW_PARBIT_BUFFER_TIME |
271 SND_PCM_HW_PARBIT_TICK_TIME);
272 err = _snd_pcm_hw_params_refine(params, links, sparams);
278 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
280 return snd_pcm_hw_refine_slave(pcm, params,
281 snd_pcm_alaw_hw_refine_cprepare,
282 snd_pcm_alaw_hw_refine_cchange,
283 snd_pcm_alaw_hw_refine_sprepare,
284 snd_pcm_alaw_hw_refine_schange,
285 snd_pcm_plugin_hw_refine_slave);
288 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
290 snd_pcm_alaw_t *alaw = pcm->private_data;
291 int err = snd_pcm_hw_params_slave(pcm, params,
292 snd_pcm_alaw_hw_refine_cchange,
293 snd_pcm_alaw_hw_refine_sprepare,
294 snd_pcm_alaw_hw_refine_schange,
295 snd_pcm_plugin_hw_params_slave);
299 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
300 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
301 alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
302 alaw->func = snd_pcm_alaw_encode;
304 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
305 alaw->func = snd_pcm_alaw_decode;
308 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
309 alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
310 alaw->func = snd_pcm_alaw_decode;
312 alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
313 alaw->func = snd_pcm_alaw_encode;
319 static snd_pcm_uframes_t
320 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
321 const snd_pcm_channel_area_t *areas,
322 snd_pcm_uframes_t offset,
323 snd_pcm_uframes_t size,
324 const snd_pcm_channel_area_t *slave_areas,
325 snd_pcm_uframes_t slave_offset,
326 snd_pcm_uframes_t *slave_sizep)
328 snd_pcm_alaw_t *alaw = pcm->private_data;
329 if (size > *slave_sizep)
331 alaw->func(slave_areas, slave_offset,
339 static snd_pcm_uframes_t
340 snd_pcm_alaw_read_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(areas, offset,
352 slave_areas, slave_offset,
359 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
361 snd_pcm_alaw_t *alaw = pcm->private_data;
362 snd_output_printf(out, "A-Law conversion PCM (%s)\n",
363 snd_pcm_format_name(alaw->sformat));
365 snd_output_printf(out, "Its setup is:\n");
366 snd_pcm_dump_setup(pcm, out);
368 snd_output_printf(out, "Slave: ");
369 snd_pcm_dump(alaw->plug.slave, out);
372 snd_pcm_ops_t snd_pcm_alaw_ops = {
373 close: snd_pcm_plugin_close,
374 info: snd_pcm_plugin_info,
375 hw_refine: snd_pcm_alaw_hw_refine,
376 hw_params: snd_pcm_alaw_hw_params,
377 hw_free: snd_pcm_plugin_hw_free,
378 sw_params: snd_pcm_plugin_sw_params,
379 channel_info: snd_pcm_plugin_channel_info,
380 dump: snd_pcm_alaw_dump,
381 nonblock: snd_pcm_plugin_nonblock,
382 async: snd_pcm_plugin_async,
383 mmap: snd_pcm_plugin_mmap,
384 munmap: snd_pcm_plugin_munmap,
387 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
390 snd_pcm_alaw_t *alaw;
392 assert(pcmp && slave);
393 if (snd_pcm_format_linear(sformat) != 1 &&
394 sformat != SND_PCM_FORMAT_A_LAW)
396 alaw = calloc(1, sizeof(snd_pcm_alaw_t));
400 alaw->sformat = sformat;
401 alaw->plug.read = snd_pcm_alaw_read_areas;
402 alaw->plug.write = snd_pcm_alaw_write_areas;
403 alaw->plug.slave = slave;
404 alaw->plug.close_slave = close_slave;
406 err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
411 pcm->ops = &snd_pcm_alaw_ops;
412 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
413 pcm->private_data = alaw;
414 pcm->poll_fd = slave->poll_fd;
415 pcm->hw_ptr = &alaw->plug.hw_ptr;
416 pcm->appl_ptr = &alaw->plug.appl_ptr;
422 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
423 snd_config_t *root, snd_config_t *conf,
424 snd_pcm_stream_t stream, int mode)
426 snd_config_iterator_t i, next;
429 snd_config_t *slave = NULL, *sconf;
430 snd_pcm_format_t sformat;
431 snd_config_for_each(i, next, conf) {
432 snd_config_t *n = snd_config_iterator_entry(i);
433 const char *id = snd_config_get_id(n);
434 if (snd_pcm_conf_generic_id(id))
436 if (strcmp(id, "slave") == 0) {
440 SNDERR("Unknown field %s", id);
444 SNDERR("slave is not defined");
447 err = snd_pcm_slave_conf(root, slave, &sconf, 1,
448 SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
451 if (snd_pcm_format_linear(sformat) != 1 &&
452 sformat != SND_PCM_FORMAT_A_LAW) {
453 snd_config_delete(sconf);
454 SNDERR("invalid slave format");
457 err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
458 snd_config_delete(sconf);
461 err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
466 SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);