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,
59 unsigned int getputidx,
60 snd_pcm_adpcm_state_t *states);
63 /* This field need to be the first */
64 snd_pcm_plugin_t plug;
65 unsigned int getput_idx;
67 snd_pcm_format_t sformat;
68 snd_pcm_adpcm_state_t *states;
71 /* First table lookup for Ima-ADPCM quantizer */
72 static char IndexAdjust[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
74 /* Second table lookup for Ima-ADPCM quantizer */
75 static short StepSize[89] = {
76 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
77 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
78 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
79 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
80 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
81 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
82 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
83 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
84 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
87 static char adpcm_encoder(int sl, snd_pcm_adpcm_state_t * state)
89 short diff; /* Difference between sl and predicted sample */
90 short pred_diff; /* Predicted difference to next sample */
92 unsigned char sign; /* sign of diff */
93 short step; /* holds previous StepSize value */
94 unsigned char adjust_idx; /* Index to IndexAdjust lookup table */
98 /* Compute difference to previous predicted value */
99 diff = sl - state->pred_val;
100 sign = (diff < 0) ? 0x8 : 0x0;
106 * This code *approximately* computes:
107 * adjust_idx = diff * 4 / step;
108 * pred_diff = (adjust_idx + 0.5) * step / 4;
110 * But in shift step bits are dropped. The net result of this is
111 * that even if you have fast mul/div hardware you cannot put it to
112 * good use since the fixup would be too expensive.
115 step = StepSize[state->step_idx];
117 /* Divide and clamp */
118 pred_diff = step >> 3;
119 for (adjust_idx = 0, i = 0x4; i; i >>= 1, step >>= 1) {
127 /* Update and clamp previous predicted value */
128 state->pred_val += sign ? -pred_diff : pred_diff;
130 if (state->pred_val > 32767) {
131 state->pred_val = 32767;
132 } else if (state->pred_val < -32768) {
133 state->pred_val = -32768;
136 /* Update and clamp StepSize lookup table index */
137 state->step_idx += IndexAdjust[adjust_idx];
139 if (state->step_idx < 0) {
141 } else if (state->step_idx > 88) {
142 state->step_idx = 88;
144 return (sign | adjust_idx);
148 static int adpcm_decoder(unsigned char code, snd_pcm_adpcm_state_t * state)
150 short pred_diff; /* Predicted difference to next sample */
151 short step; /* holds previous StepSize value */
156 /* Separate sign and magnitude */
161 * Computes pred_diff = (code + 0.5) * step / 4,
162 * but see comment in adpcm_coder.
165 step = StepSize[state->step_idx];
167 /* Compute difference and new predicted value */
168 pred_diff = step >> 3;
169 for (i = 0x4; i; i >>= 1, step >>= 1) {
174 state->pred_val += (sign) ? -pred_diff : pred_diff;
176 /* Clamp output value */
177 if (state->pred_val > 32767) {
178 state->pred_val = 32767;
179 } else if (state->pred_val < -32768) {
180 state->pred_val = -32768;
183 /* Find new StepSize index value */
184 state->step_idx += IndexAdjust[code];
186 if (state->step_idx < 0) {
188 } else if (state->step_idx > 88) {
189 state->step_idx = 88;
191 return (state->pred_val);
194 void snd_pcm_adpcm_decode(const snd_pcm_channel_area_t *dst_areas,
195 snd_pcm_uframes_t dst_offset,
196 const snd_pcm_channel_area_t *src_areas,
197 snd_pcm_uframes_t src_offset,
198 unsigned int channels, snd_pcm_uframes_t frames,
200 snd_pcm_adpcm_state_t *states)
203 #include "plugin_ops.h"
205 void *put = put16_labels[putidx];
206 unsigned int channel;
207 for (channel = 0; channel < channels; ++channel, ++states) {
211 int src_step, srcbit_step, dst_step;
212 snd_pcm_uframes_t frames1;
213 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
214 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
215 srcbit = src_area->first + src_area->step * src_offset;
216 src = (const char *) src_area->addr + srcbit / 8;
218 src_step = src_area->step / 8;
219 srcbit_step = src_area->step % 8;
220 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
221 dst_step = snd_pcm_channel_area_step(dst_area);
223 while (frames1-- > 0) {
229 v = (*src >> 4) & 0x0f;
230 sample = adpcm_decoder(v, states);
232 #define PUT16_END after
233 #include "plugin_ops.h"
237 srcbit += srcbit_step;
247 void snd_pcm_adpcm_encode(const snd_pcm_channel_area_t *dst_areas,
248 snd_pcm_uframes_t dst_offset,
249 const snd_pcm_channel_area_t *src_areas,
250 snd_pcm_uframes_t src_offset,
251 unsigned int channels, snd_pcm_uframes_t frames,
253 snd_pcm_adpcm_state_t *states)
256 #include "plugin_ops.h"
258 void *get = get16_labels[getidx];
259 unsigned int channel;
261 for (channel = 0; channel < channels; ++channel, ++states) {
265 int src_step, dst_step, dstbit_step;
266 snd_pcm_uframes_t frames1;
267 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
268 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
269 src = snd_pcm_channel_area_addr(src_area, src_offset);
270 src_step = snd_pcm_channel_area_step(src_area);
271 dstbit = dst_area->first + dst_area->step * dst_offset;
272 dst = (char *) dst_area->addr + dstbit / 8;
274 dst_step = dst_area->step / 8;
275 dstbit_step = dst_area->step % 8;
277 while (frames1-- > 0) {
280 #define GET16_END after
281 #include "plugin_ops.h"
284 v = adpcm_encoder(sample, states);
286 *dst = (*dst & 0xf0) | v;
288 *dst = (*dst & 0x0f) | (v << 4);
291 dstbit += dstbit_step;
300 static int snd_pcm_adpcm_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
302 snd_pcm_adpcm_t *adpcm = pcm->private_data;
304 snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
305 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
309 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
310 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
311 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
314 err = _snd_pcm_hw_params_set_format(params,
315 SND_PCM_FORMAT_IMA_ADPCM);
319 err = _snd_pcm_hw_params_set_subformat(params,
320 SND_PCM_SUBFORMAT_STD);
323 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
327 static int snd_pcm_adpcm_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
329 snd_pcm_adpcm_t *adpcm = pcm->private_data;
330 snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
331 _snd_pcm_hw_params_any(sparams);
332 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
334 _snd_pcm_hw_params_set_format(sparams, adpcm->sformat);
335 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
339 static int snd_pcm_adpcm_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
340 snd_pcm_hw_params_t *sparams)
343 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
344 SND_PCM_HW_PARBIT_RATE |
345 SND_PCM_HW_PARBIT_PERIOD_SIZE |
346 SND_PCM_HW_PARBIT_BUFFER_SIZE |
347 SND_PCM_HW_PARBIT_PERIODS |
348 SND_PCM_HW_PARBIT_PERIOD_TIME |
349 SND_PCM_HW_PARBIT_BUFFER_TIME |
350 SND_PCM_HW_PARBIT_TICK_TIME);
351 err = _snd_pcm_hw_params_refine(sparams, links, params);
357 static int snd_pcm_adpcm_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
358 snd_pcm_hw_params_t *sparams)
361 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
362 SND_PCM_HW_PARBIT_RATE |
363 SND_PCM_HW_PARBIT_PERIOD_SIZE |
364 SND_PCM_HW_PARBIT_BUFFER_SIZE |
365 SND_PCM_HW_PARBIT_PERIODS |
366 SND_PCM_HW_PARBIT_PERIOD_TIME |
367 SND_PCM_HW_PARBIT_BUFFER_TIME |
368 SND_PCM_HW_PARBIT_TICK_TIME);
369 err = _snd_pcm_hw_params_refine(params, links, sparams);
375 static int snd_pcm_adpcm_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
377 return snd_pcm_hw_refine_slave(pcm, params,
378 snd_pcm_adpcm_hw_refine_cprepare,
379 snd_pcm_adpcm_hw_refine_cchange,
380 snd_pcm_adpcm_hw_refine_sprepare,
381 snd_pcm_adpcm_hw_refine_schange,
382 snd_pcm_plugin_hw_refine_slave);
385 static int snd_pcm_adpcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
387 snd_pcm_adpcm_t *adpcm = pcm->private_data;
388 int err = snd_pcm_hw_params_slave(pcm, params,
389 snd_pcm_adpcm_hw_refine_cchange,
390 snd_pcm_adpcm_hw_refine_sprepare,
391 snd_pcm_adpcm_hw_refine_schange,
392 snd_pcm_plugin_hw_params_slave);
397 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
398 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
399 adpcm->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
400 adpcm->func = snd_pcm_adpcm_encode;
402 adpcm->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, adpcm->sformat);
403 adpcm->func = snd_pcm_adpcm_decode;
406 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
407 adpcm->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
408 adpcm->func = snd_pcm_adpcm_decode;
410 adpcm->getput_idx = snd_pcm_linear_get_index(adpcm->sformat, SND_PCM_FORMAT_S16);
411 adpcm->func = snd_pcm_adpcm_encode;
414 assert(!adpcm->states);
415 adpcm->states = malloc(adpcm->plug.slave->channels * sizeof(*adpcm->states));
419 static int snd_pcm_adpcm_hw_free(snd_pcm_t *pcm)
421 snd_pcm_adpcm_t *adpcm = pcm->private_data;
426 return snd_pcm_hw_free(adpcm->plug.slave);
429 static int snd_pcm_adpcm_init(snd_pcm_t *pcm)
431 snd_pcm_adpcm_t *adpcm = pcm->private_data;
433 for (k = 0; k < pcm->channels; ++k) {
434 adpcm->states[k].pred_val = 0;
435 adpcm->states[k].step_idx = 0;
440 static snd_pcm_uframes_t
441 snd_pcm_adpcm_write_areas(snd_pcm_t *pcm,
442 const snd_pcm_channel_area_t *areas,
443 snd_pcm_uframes_t offset,
444 snd_pcm_uframes_t size,
445 const snd_pcm_channel_area_t *slave_areas,
446 snd_pcm_uframes_t slave_offset,
447 snd_pcm_uframes_t *slave_sizep)
449 snd_pcm_adpcm_t *adpcm = pcm->private_data;
450 if (size > *slave_sizep)
452 adpcm->func(slave_areas, slave_offset,
455 adpcm->getput_idx, adpcm->states);
460 static snd_pcm_uframes_t
461 snd_pcm_adpcm_read_areas(snd_pcm_t *pcm,
462 const snd_pcm_channel_area_t *areas,
463 snd_pcm_uframes_t offset,
464 snd_pcm_uframes_t size,
465 const snd_pcm_channel_area_t *slave_areas,
466 snd_pcm_uframes_t slave_offset,
467 snd_pcm_uframes_t *slave_sizep)
469 snd_pcm_adpcm_t *adpcm = pcm->private_data;
470 if (size > *slave_sizep)
472 adpcm->func(areas, offset,
473 slave_areas, slave_offset,
475 adpcm->getput_idx, adpcm->states);
480 static void snd_pcm_adpcm_dump(snd_pcm_t *pcm, snd_output_t *out)
482 snd_pcm_adpcm_t *adpcm = pcm->private_data;
483 snd_output_printf(out, "Ima-ADPCM conversion PCM (%s)\n",
484 snd_pcm_format_name(adpcm->sformat));
486 snd_output_printf(out, "Its setup is:\n");
487 snd_pcm_dump_setup(pcm, out);
489 snd_output_printf(out, "Slave: ");
490 snd_pcm_dump(adpcm->plug.slave, out);
493 snd_pcm_ops_t snd_pcm_adpcm_ops = {
494 close: snd_pcm_plugin_close,
495 info: snd_pcm_plugin_info,
496 hw_refine: snd_pcm_adpcm_hw_refine,
497 hw_params: snd_pcm_adpcm_hw_params,
498 hw_free: snd_pcm_adpcm_hw_free,
499 sw_params: snd_pcm_plugin_sw_params,
500 channel_info: snd_pcm_plugin_channel_info,
501 dump: snd_pcm_adpcm_dump,
502 nonblock: snd_pcm_plugin_nonblock,
503 async: snd_pcm_plugin_async,
504 mmap: snd_pcm_plugin_mmap,
505 munmap: snd_pcm_plugin_munmap,
508 int snd_pcm_adpcm_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
511 snd_pcm_adpcm_t *adpcm;
512 assert(pcmp && slave);
513 if (snd_pcm_format_linear(sformat) != 1 &&
514 sformat != SND_PCM_FORMAT_IMA_ADPCM)
516 adpcm = calloc(1, sizeof(snd_pcm_adpcm_t));
520 adpcm->sformat = sformat;
521 adpcm->plug.read = snd_pcm_adpcm_read_areas;
522 adpcm->plug.write = snd_pcm_adpcm_write_areas;
523 adpcm->plug.init = snd_pcm_adpcm_init;
524 adpcm->plug.slave = slave;
525 adpcm->plug.close_slave = close_slave;
527 pcm = calloc(1, sizeof(snd_pcm_t));
533 pcm->name = strdup(name);
534 pcm->type = SND_PCM_TYPE_ADPCM;
535 pcm->stream = slave->stream;
536 pcm->mode = slave->mode;
537 pcm->ops = &snd_pcm_adpcm_ops;
539 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
540 pcm->fast_op_arg = pcm;
541 pcm->private_data = adpcm;
542 pcm->poll_fd = slave->poll_fd;
543 pcm->hw_ptr = &adpcm->plug.hw_ptr;
544 pcm->appl_ptr = &adpcm->plug.appl_ptr;
550 int _snd_pcm_adpcm_open(snd_pcm_t **pcmp, const char *name,
552 snd_pcm_stream_t stream, int mode)
554 snd_config_iterator_t i, next;
555 const char *sname = NULL;
558 snd_config_t *slave = NULL;
559 snd_pcm_format_t sformat;
560 snd_config_for_each(i, next, conf) {
561 snd_config_t *n = snd_config_iterator_entry(i);
562 const char *id = snd_config_get_id(n);
563 if (strcmp(id, "comment") == 0)
565 if (strcmp(id, "type") == 0)
567 if (strcmp(id, "slave") == 0) {
571 SNDERR("Unknown field %s", id);
575 SNDERR("slave is not defined");
578 err = snd_pcm_slave_conf(slave, &sname, 1,
579 SND_PCM_HW_PARAM_FORMAT, 1, &sformat);
582 if (snd_pcm_format_linear(sformat) != 1 &&
583 sformat != SND_PCM_FORMAT_IMA_ADPCM) {
584 SNDERR("invalid slave format");
587 /* This is needed cause snd_config_update may destroy config */
588 sname = strdup(sname);
591 err = snd_pcm_open(&spcm, sname, stream, mode);
592 free((void *) sname);
595 err = snd_pcm_adpcm_open(pcmp, name, sformat, spcm, 1);