2 * PCM - Ima-ADPC conversion
3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4 * Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
5 * Jaroslav Kysela <perex@suse.cz>
7 * Based on Version 1.2, 18-Dec-92 implementation of Intel/DVI ADPCM code
8 * by Jack Jansen, CWI, Amsterdam <Jack.Jansen@cwi.nl>, Copyright 1992
9 * by Stichting Mathematisch Centrum, Amsterdam, The Netherlands.
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Library General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 These routines convert 16 bit linear PCM samples to 4 bit ADPCM code
29 and vice versa. The ADPCM code used is the Intel/DVI ADPCM code which
30 is being recommended by the IMA Digital Audio Technical Working Group.
32 The algorithm for this coder was taken from:
33 Proposal for Standardized Audio Interstreamge Formats,
34 IMA compatability project proceedings, Vol 2, Issue 2, May 1992.
36 - No, this is *not* a G.721 coder/decoder. The algorithm used by G.721
37 is very complicated, requiring oodles of floating-point ops per
38 sample (resulting in very poor performance). I have not done any
39 tests myself but various people have assured my that 721 quality is
40 actually lower than DVI quality.
42 - No, it probably isn't a RIFF ADPCM decoder either. Trying to decode
43 RIFF ADPCM with these routines seems to result in something
44 recognizable but very distorted.
46 - No, it is not a CDROM-XA coder either, as far as I know. I haven't
47 come across a good description of XA yet.
51 #include "pcm_local.h"
52 #include "pcm_plugin.h"
54 typedef void (*adpcm_f)(const snd_pcm_channel_area_t *dst_areas,
55 snd_pcm_uframes_t dst_offset,
56 const snd_pcm_channel_area_t *src_areas,
57 snd_pcm_uframes_t src_offset,
58 unsigned int channels, snd_pcm_uframes_t frames, int getputidx,
59 snd_pcm_adpcm_state_t *states);
62 /* This field need to be the first */
63 snd_pcm_plugin_t plug;
66 snd_pcm_format_t sformat;
67 snd_pcm_adpcm_state_t *states;
70 /* First table lookup for Ima-ADPCM quantizer */
71 static char IndexAdjust[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
73 /* Second table lookup for Ima-ADPCM quantizer */
74 static short StepSize[89] = {
75 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
76 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
77 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
78 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
79 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
80 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
81 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
82 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
83 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
86 static char adpcm_encoder(int sl, snd_pcm_adpcm_state_t * state)
88 short diff; /* Difference between sl and predicted sample */
89 short pred_diff; /* Predicted difference to next sample */
91 unsigned char sign; /* sign of diff */
92 short step; /* holds previous StepSize value */
93 unsigned char adjust_idx; /* Index to IndexAdjust lookup table */
97 /* Compute difference to previous predicted value */
98 diff = sl - state->pred_val;
99 sign = (diff < 0) ? 0x8 : 0x0;
105 * This code *approximately* computes:
106 * adjust_idx = diff * 4 / step;
107 * pred_diff = (adjust_idx + 0.5) * step / 4;
109 * But in shift step bits are dropped. The net result of this is
110 * that even if you have fast mul/div hardware you cannot put it to
111 * good use since the fixup would be too expensive.
114 step = StepSize[state->step_idx];
116 /* Divide and clamp */
117 pred_diff = step >> 3;
118 for (adjust_idx = 0, i = 0x4; i; i >>= 1, step >>= 1) {
126 /* Update and clamp previous predicted value */
127 state->pred_val += sign ? -pred_diff : pred_diff;
129 if (state->pred_val > 32767) {
130 state->pred_val = 32767;
131 } else if (state->pred_val < -32768) {
132 state->pred_val = -32768;
135 /* Update and clamp StepSize lookup table index */
136 state->step_idx += IndexAdjust[adjust_idx];
138 if (state->step_idx < 0) {
140 } else if (state->step_idx > 88) {
141 state->step_idx = 88;
143 return (sign | adjust_idx);
147 static int adpcm_decoder(unsigned char code, snd_pcm_adpcm_state_t * state)
149 short pred_diff; /* Predicted difference to next sample */
150 short step; /* holds previous StepSize value */
155 /* Separate sign and magnitude */
160 * Computes pred_diff = (code + 0.5) * step / 4,
161 * but see comment in adpcm_coder.
164 step = StepSize[state->step_idx];
166 /* Compute difference and new predicted value */
167 pred_diff = step >> 3;
168 for (i = 0x4; i; i >>= 1, step >>= 1) {
173 state->pred_val += (sign) ? -pred_diff : pred_diff;
175 /* Clamp output value */
176 if (state->pred_val > 32767) {
177 state->pred_val = 32767;
178 } else if (state->pred_val < -32768) {
179 state->pred_val = -32768;
182 /* Find new StepSize index value */
183 state->step_idx += IndexAdjust[code];
185 if (state->step_idx < 0) {
187 } else if (state->step_idx > 88) {
188 state->step_idx = 88;
190 return (state->pred_val);
193 void snd_pcm_adpcm_decode(const snd_pcm_channel_area_t *dst_areas,
194 snd_pcm_uframes_t dst_offset,
195 const snd_pcm_channel_area_t *src_areas,
196 snd_pcm_uframes_t src_offset,
197 unsigned int channels, snd_pcm_uframes_t frames, int putidx,
198 snd_pcm_adpcm_state_t *states)
201 #include "plugin_ops.h"
203 void *put = put16_labels[putidx];
204 unsigned int channel;
205 for (channel = 0; channel < channels; ++channel, ++states) {
209 int src_step, srcbit_step, dst_step;
210 snd_pcm_uframes_t frames1;
211 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
212 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
213 srcbit = src_area->first + src_area->step * src_offset;
214 src = src_area->addr + srcbit / 8;
216 src_step = src_area->step / 8;
217 srcbit_step = src_area->step % 8;
218 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
219 dst_step = snd_pcm_channel_area_step(dst_area);
221 while (frames1-- > 0) {
227 v = (*src >> 4) & 0x0f;
228 sample = adpcm_decoder(v, states);
230 #define PUT16_END after
231 #include "plugin_ops.h"
235 srcbit += srcbit_step;
245 void snd_pcm_adpcm_encode(const snd_pcm_channel_area_t *dst_areas,
246 snd_pcm_uframes_t dst_offset,
247 const snd_pcm_channel_area_t *src_areas,
248 snd_pcm_uframes_t src_offset,
249 unsigned int channels, snd_pcm_uframes_t frames, int getidx,
250 snd_pcm_adpcm_state_t *states)
253 #include "plugin_ops.h"
255 void *get = get16_labels[getidx];
256 unsigned int channel;
258 for (channel = 0; channel < channels; ++channel, ++states) {
262 int src_step, dst_step, dstbit_step;
263 snd_pcm_uframes_t frames1;
264 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
265 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
266 src = snd_pcm_channel_area_addr(src_area, src_offset);
267 src_step = snd_pcm_channel_area_step(src_area);
268 dstbit = dst_area->first + dst_area->step * dst_offset;
269 dst = dst_area->addr + dstbit / 8;
271 dst_step = dst_area->step / 8;
272 dstbit_step = dst_area->step % 8;
274 while (frames1-- > 0) {
277 #define GET16_END after
278 #include "plugin_ops.h"
281 v = adpcm_encoder(sample, states);
283 *dst = (*dst & 0xf0) | v;
285 *dst = (*dst & 0x0f) | (v << 4);
288 dstbit += dstbit_step;
297 static int snd_pcm_adpcm_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
299 snd_pcm_adpcm_t *adpcm = pcm->private_data;
301 snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
302 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
306 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
307 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
308 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
311 err = _snd_pcm_hw_params_set_format(params,
312 SND_PCM_FORMAT_IMA_ADPCM);
316 err = _snd_pcm_hw_params_set_subformat(params,
317 SND_PCM_SUBFORMAT_STD);
320 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
324 static int snd_pcm_adpcm_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
326 snd_pcm_adpcm_t *adpcm = pcm->private_data;
327 snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
328 _snd_pcm_hw_params_any(sparams);
329 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
331 _snd_pcm_hw_params_set_format(sparams, adpcm->sformat);
332 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
336 static int snd_pcm_adpcm_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
337 snd_pcm_hw_params_t *sparams)
340 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
341 SND_PCM_HW_PARBIT_RATE |
342 SND_PCM_HW_PARBIT_PERIOD_SIZE |
343 SND_PCM_HW_PARBIT_BUFFER_SIZE |
344 SND_PCM_HW_PARBIT_PERIODS |
345 SND_PCM_HW_PARBIT_PERIOD_TIME |
346 SND_PCM_HW_PARBIT_BUFFER_TIME |
347 SND_PCM_HW_PARBIT_TICK_TIME);
348 err = _snd_pcm_hw_params_refine(sparams, links, params);
354 static int snd_pcm_adpcm_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
355 snd_pcm_hw_params_t *sparams)
358 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
359 SND_PCM_HW_PARBIT_RATE |
360 SND_PCM_HW_PARBIT_PERIOD_SIZE |
361 SND_PCM_HW_PARBIT_BUFFER_SIZE |
362 SND_PCM_HW_PARBIT_PERIODS |
363 SND_PCM_HW_PARBIT_PERIOD_TIME |
364 SND_PCM_HW_PARBIT_BUFFER_TIME |
365 SND_PCM_HW_PARBIT_TICK_TIME);
366 err = _snd_pcm_hw_params_refine(params, links, sparams);
372 static int snd_pcm_adpcm_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
374 return snd_pcm_hw_refine_slave(pcm, params,
375 snd_pcm_adpcm_hw_refine_cprepare,
376 snd_pcm_adpcm_hw_refine_cchange,
377 snd_pcm_adpcm_hw_refine_sprepare,
378 snd_pcm_adpcm_hw_refine_schange,
379 snd_pcm_plugin_hw_refine_slave);
382 static int snd_pcm_adpcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
384 snd_pcm_adpcm_t *adpcm = pcm->private_data;
385 int err = snd_pcm_hw_params_slave(pcm, params,
386 snd_pcm_adpcm_hw_refine_cchange,
387 snd_pcm_adpcm_hw_refine_sprepare,
388 snd_pcm_adpcm_hw_refine_schange,
389 snd_pcm_plugin_hw_params_slave);
394 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
395 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
396 adpcm->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
397 adpcm->func = snd_pcm_adpcm_encode;
399 adpcm->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, adpcm->sformat);
400 adpcm->func = snd_pcm_adpcm_decode;
403 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
404 adpcm->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
405 adpcm->func = snd_pcm_adpcm_decode;
407 adpcm->getput_idx = snd_pcm_linear_get_index(adpcm->sformat, SND_PCM_FORMAT_S16);
408 adpcm->func = snd_pcm_adpcm_encode;
411 assert(!adpcm->states);
412 adpcm->states = malloc(snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, 0) * sizeof(*adpcm->states));
416 static int snd_pcm_adpcm_hw_free(snd_pcm_t *pcm)
418 snd_pcm_adpcm_t *adpcm = pcm->private_data;
423 return snd_pcm_hw_free(adpcm->plug.slave);
426 static int snd_pcm_adpcm_init(snd_pcm_t *pcm)
428 snd_pcm_adpcm_t *adpcm = pcm->private_data;
430 for (k = 0; k < pcm->channels; ++k) {
431 adpcm->states[k].pred_val = 0;
432 adpcm->states[k].step_idx = 0;
437 static snd_pcm_uframes_t
438 snd_pcm_adpcm_write_areas(snd_pcm_t *pcm,
439 const snd_pcm_channel_area_t *areas,
440 snd_pcm_uframes_t offset,
441 snd_pcm_uframes_t size,
442 const snd_pcm_channel_area_t *slave_areas,
443 snd_pcm_uframes_t slave_offset,
444 snd_pcm_uframes_t *slave_sizep)
446 snd_pcm_adpcm_t *adpcm = pcm->private_data;
447 if (size > *slave_sizep)
449 adpcm->func(slave_areas, slave_offset,
452 adpcm->getput_idx, adpcm->states);
457 static snd_pcm_uframes_t
458 snd_pcm_adpcm_read_areas(snd_pcm_t *pcm,
459 const snd_pcm_channel_area_t *areas,
460 snd_pcm_uframes_t offset,
461 snd_pcm_uframes_t size,
462 const snd_pcm_channel_area_t *slave_areas,
463 snd_pcm_uframes_t slave_offset,
464 snd_pcm_uframes_t *slave_sizep)
466 snd_pcm_adpcm_t *adpcm = pcm->private_data;
467 if (size > *slave_sizep)
469 adpcm->func(areas, offset,
470 slave_areas, slave_offset,
472 adpcm->getput_idx, adpcm->states);
477 static void snd_pcm_adpcm_dump(snd_pcm_t *pcm, snd_output_t *out)
479 snd_pcm_adpcm_t *adpcm = pcm->private_data;
480 snd_output_printf(out, "Ima-ADPCM conversion PCM (%s)\n",
481 snd_pcm_format_name(adpcm->sformat));
483 snd_output_printf(out, "Its setup is:\n");
484 snd_pcm_dump_setup(pcm, out);
486 snd_output_printf(out, "Slave: ");
487 snd_pcm_dump(adpcm->plug.slave, out);
490 snd_pcm_ops_t snd_pcm_adpcm_ops = {
491 close: snd_pcm_plugin_close,
492 info: snd_pcm_plugin_info,
493 hw_refine: snd_pcm_adpcm_hw_refine,
494 hw_params: snd_pcm_adpcm_hw_params,
495 hw_free: snd_pcm_adpcm_hw_free,
496 sw_params: snd_pcm_plugin_sw_params,
497 channel_info: snd_pcm_plugin_channel_info,
498 dump: snd_pcm_adpcm_dump,
499 nonblock: snd_pcm_plugin_nonblock,
500 async: snd_pcm_plugin_async,
501 mmap: snd_pcm_plugin_mmap,
502 munmap: snd_pcm_plugin_munmap,
505 int snd_pcm_adpcm_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
508 snd_pcm_adpcm_t *adpcm;
509 assert(pcmp && slave);
510 if (snd_pcm_format_linear(sformat) != 1 &&
511 sformat != SND_PCM_FORMAT_IMA_ADPCM)
513 adpcm = calloc(1, sizeof(snd_pcm_adpcm_t));
517 adpcm->sformat = sformat;
518 adpcm->plug.read = snd_pcm_adpcm_read_areas;
519 adpcm->plug.write = snd_pcm_adpcm_write_areas;
520 adpcm->plug.init = snd_pcm_adpcm_init;
521 adpcm->plug.slave = slave;
522 adpcm->plug.close_slave = close_slave;
524 pcm = calloc(1, sizeof(snd_pcm_t));
530 pcm->name = strdup(name);
531 pcm->type = SND_PCM_TYPE_ADPCM;
532 pcm->stream = slave->stream;
533 pcm->mode = slave->mode;
534 pcm->ops = &snd_pcm_adpcm_ops;
536 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
537 pcm->fast_op_arg = pcm;
538 pcm->private_data = adpcm;
539 pcm->poll_fd = slave->poll_fd;
540 pcm->hw_ptr = &adpcm->plug.hw_ptr;
541 pcm->appl_ptr = &adpcm->plug.appl_ptr;
547 int _snd_pcm_adpcm_open(snd_pcm_t **pcmp, const char *name,
549 snd_pcm_stream_t stream, int mode)
551 snd_config_iterator_t i, next;
552 const char *sname = NULL;
555 snd_config_t *slave = NULL;
556 snd_pcm_format_t sformat;
557 snd_config_for_each(i, next, conf) {
558 snd_config_t *n = snd_config_iterator_entry(i);
559 const char *id = snd_config_get_id(n);
560 if (strcmp(id, "comment") == 0)
562 if (strcmp(id, "type") == 0)
564 if (strcmp(id, "slave") == 0) {
568 SNDERR("Unknown field %s", id);
572 SNDERR("slave is not defined");
575 err = snd_pcm_slave_conf(slave, &sname, 1,
576 SND_PCM_HW_PARAM_FORMAT, 1, &sformat);
579 if (snd_pcm_format_linear(sformat) != 1 &&
580 sformat != SND_PCM_FORMAT_IMA_ADPCM) {
581 SNDERR("invalid slave format");
584 /* This is needed cause snd_config_update may destroy config */
585 sname = strdup(sname);
588 err = snd_pcm_open(&spcm, sname, stream, mode);
589 free((void *) sname);
592 err = snd_pcm_adpcm_open(pcmp, name, sformat, spcm, 1);