3 * Copyright (c) 2001-2003 The ffmpeg Project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "bitstream.h"
25 * First version by Francois Revol (revol@free.fr)
26 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
27 * by Mike Melanson (melanson@pcisys.net)
28 * CD-ROM XA ADPCM codec by BERO
29 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
31 * Features and limitations:
33 * Reference documents:
34 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
35 * http://www.geocities.com/SiliconValley/8682/aud3.txt
36 * http://openquicktime.sourceforge.net/plugins.htm
37 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
38 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
39 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
42 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
43 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
44 * readstr http://www.geocities.co.jp/Playtown/2004/
49 #define CLAMP_TO_SHORT(value) \
52 else if (value < -32768) \
55 /* step_table[] and index_table[] are from the ADPCM reference source */
56 /* This is the index table: */
57 static const int index_table[16] = {
58 -1, -1, -1, -1, 2, 4, 6, 8,
59 -1, -1, -1, -1, 2, 4, 6, 8,
63 * This is the step table. Note that many programs use slight deviations from
64 * this table, but such deviations are negligible:
66 static const int step_table[89] = {
67 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
68 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
69 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
70 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
71 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
72 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
73 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
74 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
75 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
78 /* These are for MS-ADPCM */
79 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
80 static const int AdaptationTable[] = {
81 230, 230, 230, 230, 307, 409, 512, 614,
82 768, 614, 512, 409, 307, 230, 230, 230
85 static const int AdaptCoeff1[] = {
86 256, 512, 0, 192, 240, 460, 392
89 static const int AdaptCoeff2[] = {
90 0, -256, 0, 64, 0, -208, -232
93 /* These are for CD-ROM XA ADPCM */
94 static const int xa_adpcm_table[5][2] = {
102 static int ea_adpcm_table[] = {
103 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
104 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
107 static int ct_adpcm_table[8] = {
108 0x00E6, 0x00E6, 0x00E6, 0x00E6,
109 0x0133, 0x0199, 0x0200, 0x0266
112 // padded to zero where table size is less then 16
113 static int swf_index_tables[4][16] = {
115 /*3*/ { -1, -1, 2, 4 },
116 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
117 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
122 typedef struct ADPCMChannelStatus {
124 short int step_index;
135 } ADPCMChannelStatus;
137 typedef struct ADPCMContext {
138 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
139 ADPCMChannelStatus status[2];
140 short sample_buffer[32]; /* hold left samples while waiting for right samples */
147 /* XXX: implement encoding */
149 #ifdef CONFIG_ENCODERS
150 static int adpcm_encode_init(AVCodecContext *avctx)
152 if (avctx->channels > 2)
153 return -1; /* only stereo or mono =) */
154 switch(avctx->codec->id) {
155 case CODEC_ID_ADPCM_IMA_QT:
156 av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
157 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
160 case CODEC_ID_ADPCM_IMA_WAV:
161 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
162 /* and we have 4 bytes per channel overhead */
163 avctx->block_align = BLKSIZE;
164 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
166 case CODEC_ID_ADPCM_MS:
167 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
168 /* and we have 7 bytes per channel overhead */
169 avctx->block_align = BLKSIZE;
176 avctx->coded_frame= avcodec_alloc_frame();
177 avctx->coded_frame->key_frame= 1;
182 static int adpcm_encode_close(AVCodecContext *avctx)
184 av_freep(&avctx->coded_frame);
190 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
193 unsigned char nibble;
195 int sign = 0; /* sign bit of the nibble (MSB) */
196 int delta, predicted_delta;
198 delta = sample - c->prev_sample;
205 step_index = c->step_index;
207 /* nibble = 4 * delta / step_table[step_index]; */
208 nibble = (delta << 2) / step_table[step_index];
213 step_index += index_table[nibble];
219 /* what the decoder will find */
220 predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
223 c->prev_sample -= predicted_delta;
225 c->prev_sample += predicted_delta;
227 CLAMP_TO_SHORT(c->prev_sample);
230 nibble += sign << 3; /* sign * 8 */
233 c->step_index = step_index;
238 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
240 int predictor, nibble, bias;
242 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
244 nibble= sample - predictor;
245 if(nibble>=0) bias= c->idelta/2;
246 else bias=-c->idelta/2;
248 nibble= (nibble + bias) / c->idelta;
249 nibble= clip(nibble, -8, 7)&0x0F;
251 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
252 CLAMP_TO_SHORT(predictor);
254 c->sample2 = c->sample1;
255 c->sample1 = predictor;
257 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
258 if (c->idelta < 16) c->idelta = 16;
263 static int adpcm_encode_frame(AVCodecContext *avctx,
264 unsigned char *frame, int buf_size, void *data)
269 ADPCMContext *c = avctx->priv_data;
272 samples = (short *)data;
273 st= avctx->channels == 2;
274 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
276 switch(avctx->codec->id) {
277 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
279 case CODEC_ID_ADPCM_IMA_WAV:
280 n = avctx->frame_size / 8;
281 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
282 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
283 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
284 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
285 *dst++ = (unsigned char)c->status[0].step_index;
286 *dst++ = 0; /* unknown */
288 if (avctx->channels == 2) {
289 c->status[1].prev_sample = (signed short)samples[1];
290 /* c->status[1].step_index = 0; */
291 *dst++ = (c->status[1].prev_sample) & 0xFF;
292 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
293 *dst++ = (unsigned char)c->status[1].step_index;
298 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
300 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
301 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
303 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
304 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
306 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
307 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
309 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
310 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
313 if (avctx->channels == 2) {
314 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
315 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
317 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
318 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
320 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
321 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
323 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
324 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
327 samples += 8 * avctx->channels;
330 case CODEC_ID_ADPCM_MS:
331 for(i=0; i<avctx->channels; i++){
335 c->status[i].coeff1 = AdaptCoeff1[predictor];
336 c->status[i].coeff2 = AdaptCoeff2[predictor];
338 for(i=0; i<avctx->channels; i++){
339 if (c->status[i].idelta < 16)
340 c->status[i].idelta = 16;
342 *dst++ = c->status[i].idelta & 0xFF;
343 *dst++ = c->status[i].idelta >> 8;
345 for(i=0; i<avctx->channels; i++){
346 c->status[i].sample1= *samples++;
348 *dst++ = c->status[i].sample1 & 0xFF;
349 *dst++ = c->status[i].sample1 >> 8;
351 for(i=0; i<avctx->channels; i++){
352 c->status[i].sample2= *samples++;
354 *dst++ = c->status[i].sample2 & 0xFF;
355 *dst++ = c->status[i].sample2 >> 8;
358 for(i=7*avctx->channels; i<avctx->block_align; i++) {
360 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
361 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
370 #endif //CONFIG_ENCODERS
372 static int adpcm_decode_init(AVCodecContext * avctx)
374 ADPCMContext *c = avctx->priv_data;
377 c->status[0].predictor = c->status[1].predictor = 0;
378 c->status[0].step_index = c->status[1].step_index = 0;
379 c->status[0].step = c->status[1].step = 0;
381 switch(avctx->codec->id) {
382 case CODEC_ID_ADPCM_CT:
383 c->status[0].step = c->status[1].step = 511;
391 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
395 int sign, delta, diff, step;
397 step = step_table[c->step_index];
398 step_index = c->step_index + index_table[(unsigned)nibble];
399 if (step_index < 0) step_index = 0;
400 else if (step_index > 88) step_index = 88;
404 /* perform direct multiplication instead of series of jumps proposed by
405 * the reference ADPCM implementation since modern CPUs can do the mults
407 diff = ((2 * delta + 1) * step) >> shift;
408 predictor = c->predictor;
409 if (sign) predictor -= diff;
410 else predictor += diff;
412 CLAMP_TO_SHORT(predictor);
413 c->predictor = predictor;
414 c->step_index = step_index;
416 return (short)predictor;
419 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
423 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
424 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
425 CLAMP_TO_SHORT(predictor);
427 c->sample2 = c->sample1;
428 c->sample1 = predictor;
429 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
430 if (c->idelta < 16) c->idelta = 16;
432 return (short)predictor;
435 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
438 int sign, delta, diff;
443 /* perform direct multiplication instead of series of jumps proposed by
444 * the reference ADPCM implementation since modern CPUs can do the mults
446 diff = ((2 * delta + 1) * c->step) >> 3;
447 predictor = c->predictor;
448 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
450 predictor = ((predictor * 254) >> 8) - diff;
452 predictor = ((predictor * 254) >> 8) + diff;
453 /* calculate new step and clamp it to range 511..32767 */
454 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
461 CLAMP_TO_SHORT(predictor);
462 c->predictor = predictor;
463 return (short)predictor;
466 static void xa_decode(short *out, const unsigned char *in,
467 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
470 int shift,filter,f0,f1;
476 shift = 12 - (in[4+i*2] & 15);
477 filter = in[4+i*2] >> 4;
478 f0 = xa_adpcm_table[filter][0];
479 f1 = xa_adpcm_table[filter][1];
487 t = (signed char)(d<<4)>>4;
488 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
496 if (inc==2) { /* stereo */
499 s_1 = right->sample1;
500 s_2 = right->sample2;
501 out = out + 1 - 28*2;
504 shift = 12 - (in[5+i*2] & 15);
505 filter = in[5+i*2] >> 4;
507 f0 = xa_adpcm_table[filter][0];
508 f1 = xa_adpcm_table[filter][1];
513 t = (signed char)d >> 4;
514 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
522 if (inc==2) { /* stereo */
523 right->sample1 = s_1;
524 right->sample2 = s_2;
534 /* DK3 ADPCM support macro */
535 #define DK3_GET_NEXT_NIBBLE() \
536 if (decode_top_nibble_next) \
538 nibble = (last_byte >> 4) & 0x0F; \
539 decode_top_nibble_next = 0; \
543 last_byte = *src++; \
544 if (src >= buf + buf_size) break; \
545 nibble = last_byte & 0x0F; \
546 decode_top_nibble_next = 1; \
549 static int adpcm_decode_frame(AVCodecContext *avctx,
550 void *data, int *data_size,
551 uint8_t *buf, int buf_size)
553 ADPCMContext *c = avctx->priv_data;
554 ADPCMChannelStatus *cs;
555 int n, m, channel, i;
556 int block_predictor[2];
561 /* DK3 ADPCM accounting variables */
562 unsigned char last_byte = 0;
563 unsigned char nibble;
564 int decode_top_nibble_next = 0;
567 /* EA ADPCM state variables */
568 uint32_t samples_in_chunk;
569 int32_t previous_left_sample, previous_right_sample;
570 int32_t current_left_sample, current_right_sample;
571 int32_t next_left_sample, next_right_sample;
572 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
573 uint8_t shift_left, shift_right;
582 st = avctx->channels == 2;
584 switch(avctx->codec->id) {
585 case CODEC_ID_ADPCM_IMA_QT:
586 n = (buf_size - 2);/* >> 2*avctx->channels;*/
587 channel = c->channel;
588 cs = &(c->status[channel]);
589 /* (pppppp) (piiiiiii) */
591 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
592 cs->predictor = (*src++) << 8;
593 cs->predictor |= (*src & 0x80);
594 cs->predictor &= 0xFF80;
597 if(cs->predictor & 0x8000)
598 cs->predictor -= 0x10000;
600 CLAMP_TO_SHORT(cs->predictor);
602 cs->step_index = (*src++) & 0x7F;
604 if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
605 if (cs->step_index > 88) cs->step_index = 88;
607 cs->step = step_table[cs->step_index];
612 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
613 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
614 samples += avctx->channels;
615 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
616 samples += avctx->channels;
620 if(st) { /* handle stereo interlacing */
621 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
622 if(channel == 1) { /* wait for the other packet before outputing anything */
627 case CODEC_ID_ADPCM_IMA_WAV:
628 if (avctx->block_align != 0 && buf_size > avctx->block_align)
629 buf_size = avctx->block_align;
631 for(i=0; i<avctx->channels; i++){
632 cs = &(c->status[i]);
633 cs->predictor = *src++;
634 cs->predictor |= (*src++) << 8;
635 if(cs->predictor & 0x8000)
636 cs->predictor -= 0x10000;
637 CLAMP_TO_SHORT(cs->predictor);
639 // XXX: is this correct ??: *samples++ = cs->predictor;
641 cs->step_index = *src++;
642 if (cs->step_index < 0) cs->step_index = 0;
643 if (cs->step_index > 88) cs->step_index = 88;
644 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
647 for(m=4; src < (buf + buf_size);) {
648 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
650 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
651 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
653 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
662 case CODEC_ID_ADPCM_4XM:
663 cs = &(c->status[0]);
664 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
666 c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
668 c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
670 c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
672 if (cs->step_index < 0) cs->step_index = 0;
673 if (cs->step_index > 88) cs->step_index = 88;
675 m= (buf_size - (src - buf))>>st;
677 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
679 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
680 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
682 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
688 case CODEC_ID_ADPCM_MS:
689 if (avctx->block_align != 0 && buf_size > avctx->block_align)
690 buf_size = avctx->block_align;
691 n = buf_size - 7 * avctx->channels;
694 block_predictor[0] = clip(*src++, 0, 7);
695 block_predictor[1] = 0;
697 block_predictor[1] = clip(*src++, 0, 7);
698 c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
701 c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
704 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
705 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
706 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
707 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
709 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
711 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
713 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
715 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
718 *samples++ = c->status[0].sample1;
719 if (st) *samples++ = c->status[1].sample1;
720 *samples++ = c->status[0].sample2;
721 if (st) *samples++ = c->status[1].sample2;
723 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
724 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
728 case CODEC_ID_ADPCM_IMA_DK4:
729 if (avctx->block_align != 0 && buf_size > avctx->block_align)
730 buf_size = avctx->block_align;
732 c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
733 c->status[0].step_index = src[2];
735 *samples++ = c->status[0].predictor;
737 c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
738 c->status[1].step_index = src[2];
740 *samples++ = c->status[1].predictor;
742 while (src < buf + buf_size) {
744 /* take care of the top nibble (always left or mono channel) */
745 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
746 (src[0] >> 4) & 0x0F, 3);
748 /* take care of the bottom nibble, which is right sample for
749 * stereo, or another mono sample */
751 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
754 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
760 case CODEC_ID_ADPCM_IMA_DK3:
761 if (avctx->block_align != 0 && buf_size > avctx->block_align)
762 buf_size = avctx->block_align;
764 c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
765 c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
766 c->status[0].step_index = src[14];
767 c->status[1].step_index = src[15];
768 /* sign extend the predictors */
770 diff_channel = c->status[1].predictor;
772 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
773 * the buffer is consumed */
776 /* for this algorithm, c->status[0] is the sum channel and
777 * c->status[1] is the diff channel */
779 /* process the first predictor of the sum channel */
780 DK3_GET_NEXT_NIBBLE();
781 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
783 /* process the diff channel predictor */
784 DK3_GET_NEXT_NIBBLE();
785 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
787 /* process the first pair of stereo PCM samples */
788 diff_channel = (diff_channel + c->status[1].predictor) / 2;
789 *samples++ = c->status[0].predictor + c->status[1].predictor;
790 *samples++ = c->status[0].predictor - c->status[1].predictor;
792 /* process the second predictor of the sum channel */
793 DK3_GET_NEXT_NIBBLE();
794 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
796 /* process the second pair of stereo PCM samples */
797 diff_channel = (diff_channel + c->status[1].predictor) / 2;
798 *samples++ = c->status[0].predictor + c->status[1].predictor;
799 *samples++ = c->status[0].predictor - c->status[1].predictor;
802 case CODEC_ID_ADPCM_IMA_WS:
803 /* no per-block initialization; just start decoding the data */
804 while (src < buf + buf_size) {
807 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
808 (src[0] >> 4) & 0x0F, 3);
809 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
812 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
813 (src[0] >> 4) & 0x0F, 3);
814 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
821 case CODEC_ID_ADPCM_XA:
822 c->status[0].sample1 = c->status[0].sample2 =
823 c->status[1].sample1 = c->status[1].sample2 = 0;
824 while (buf_size >= 128) {
825 xa_decode(samples, src, &c->status[0], &c->status[1],
832 case CODEC_ID_ADPCM_EA:
833 samples_in_chunk = LE_32(src);
834 if (samples_in_chunk >= ((buf_size - 12) * 2)) {
839 current_left_sample = (int16_t)LE_16(src);
841 previous_left_sample = (int16_t)LE_16(src);
843 current_right_sample = (int16_t)LE_16(src);
845 previous_right_sample = (int16_t)LE_16(src);
848 for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
849 coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
850 coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
851 coeff1r = ea_adpcm_table[*src & 0x0F];
852 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
855 shift_left = ((*src >> 4) & 0x0F) + 8;
856 shift_right = (*src & 0x0F) + 8;
859 for (count2 = 0; count2 < 28; count2++) {
860 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
861 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
864 next_left_sample = (next_left_sample +
865 (current_left_sample * coeff1l) +
866 (previous_left_sample * coeff2l) + 0x80) >> 8;
867 next_right_sample = (next_right_sample +
868 (current_right_sample * coeff1r) +
869 (previous_right_sample * coeff2r) + 0x80) >> 8;
870 CLAMP_TO_SHORT(next_left_sample);
871 CLAMP_TO_SHORT(next_right_sample);
873 previous_left_sample = current_left_sample;
874 current_left_sample = next_left_sample;
875 previous_right_sample = current_right_sample;
876 current_right_sample = next_right_sample;
877 *samples++ = (unsigned short)current_left_sample;
878 *samples++ = (unsigned short)current_right_sample;
882 case CODEC_ID_ADPCM_IMA_SMJPEG:
883 c->status[0].predictor = *src;
885 c->status[0].step_index = *src++;
886 src++; /* skip another byte before getting to the meat */
887 while (src < buf + buf_size) {
888 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
890 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
891 (*src >> 4) & 0x0F, 3);
895 case CODEC_ID_ADPCM_CT:
896 while (src < buf + buf_size) {
898 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
899 (src[0] >> 4) & 0x0F);
900 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
903 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
904 (src[0] >> 4) & 0x0F);
905 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
911 case CODEC_ID_ADPCM_SWF:
916 int size = buf_size*8;
918 init_get_bits(&gb, buf, size);
920 // first frame, read bits & inital values
923 c->nb_bits = get_bits(&gb, 2)+2;
924 // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
927 table = swf_index_tables[c->nb_bits-2];
928 k0 = 1 << (c->nb_bits-2);
929 signmask = 1 << (c->nb_bits-1);
931 while (get_bits_count(&gb) <= size)
936 // wrap around at every 4096 samples...
937 if ((c->nb_samples & 0xfff) == 1)
939 for (i = 0; i <= st; i++)
941 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
942 c->status[i].step_index = get_bits(&gb, 6);
946 // similar to IMA adpcm
947 for (i = 0; i <= st; i++)
949 int delta = get_bits(&gb, c->nb_bits);
950 int step = step_table[c->status[i].step_index];
951 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
962 if (delta & signmask)
963 c->status[i].predictor -= vpdiff;
965 c->status[i].predictor += vpdiff;
967 c->status[i].step_index += table[delta & (~signmask)];
969 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
970 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
972 *samples++ = c->status[i].predictor;
976 // src += get_bits_count(&gb)*8;
984 *data_size = (uint8_t *)samples - (uint8_t *)data;
990 #ifdef CONFIG_ENCODERS
991 #define ADPCM_ENCODER(id,name) \
992 AVCodec name ## _encoder = { \
996 sizeof(ADPCMContext), \
998 adpcm_encode_frame, \
999 adpcm_encode_close, \
1003 #define ADPCM_ENCODER(id,name)
1006 #ifdef CONFIG_DECODERS
1007 #define ADPCM_DECODER(id,name) \
1008 AVCodec name ## _decoder = { \
1012 sizeof(ADPCMContext), \
1013 adpcm_decode_init, \
1016 adpcm_decode_frame, \
1019 #define ADPCM_DECODER(id,name)
1022 #define ADPCM_CODEC(id, name) \
1023 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1025 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1026 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1027 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1028 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1029 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1030 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1031 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1032 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1033 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1034 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1035 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1036 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1037 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);