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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 const 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 const 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 const 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 }
120 static const int yamaha_indexscale[] = {
121 230, 230, 230, 230, 307, 409, 512, 614,
122 230, 230, 230, 230, 307, 409, 512, 614
125 static const int yamaha_difflookup[] = {
126 1, 3, 5, 7, 9, 11, 13, 15,
127 -1, -3, -5, -7, -9, -11, -13, -15
132 typedef struct ADPCMChannelStatus {
134 short int step_index;
145 } ADPCMChannelStatus;
147 typedef struct ADPCMContext {
148 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
149 ADPCMChannelStatus status[2];
150 short sample_buffer[32]; /* hold left samples while waiting for right samples */
157 /* XXX: implement encoding */
159 #ifdef CONFIG_ENCODERS
160 static int adpcm_encode_init(AVCodecContext *avctx)
162 if (avctx->channels > 2)
163 return -1; /* only stereo or mono =) */
164 switch(avctx->codec->id) {
165 case CODEC_ID_ADPCM_IMA_QT:
166 av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
167 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
170 case CODEC_ID_ADPCM_IMA_WAV:
171 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
172 /* and we have 4 bytes per channel overhead */
173 avctx->block_align = BLKSIZE;
174 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
176 case CODEC_ID_ADPCM_MS:
177 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
178 /* and we have 7 bytes per channel overhead */
179 avctx->block_align = BLKSIZE;
181 case CODEC_ID_ADPCM_YAMAHA:
182 avctx->frame_size = BLKSIZE * avctx->channels;
183 avctx->block_align = BLKSIZE;
190 avctx->coded_frame= avcodec_alloc_frame();
191 avctx->coded_frame->key_frame= 1;
196 static int adpcm_encode_close(AVCodecContext *avctx)
198 av_freep(&avctx->coded_frame);
204 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
206 int delta = sample - c->prev_sample;
207 int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
208 c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
209 CLAMP_TO_SHORT(c->prev_sample);
210 c->step_index = clip(c->step_index + index_table[nibble], 0, 88);
214 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
216 int predictor, nibble, bias;
218 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
220 nibble= sample - predictor;
221 if(nibble>=0) bias= c->idelta/2;
222 else bias=-c->idelta/2;
224 nibble= (nibble + bias) / c->idelta;
225 nibble= clip(nibble, -8, 7)&0x0F;
227 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
228 CLAMP_TO_SHORT(predictor);
230 c->sample2 = c->sample1;
231 c->sample1 = predictor;
233 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
234 if (c->idelta < 16) c->idelta = 16;
239 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
248 delta = sample - c->predictor;
250 nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
252 c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
253 CLAMP_TO_SHORT(c->predictor);
254 c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
255 c->step = clip(c->step, 127, 24567);
260 static int adpcm_encode_frame(AVCodecContext *avctx,
261 unsigned char *frame, int buf_size, void *data)
266 ADPCMContext *c = avctx->priv_data;
269 samples = (short *)data;
270 st= avctx->channels == 2;
271 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
273 switch(avctx->codec->id) {
274 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
276 case CODEC_ID_ADPCM_IMA_WAV:
277 n = avctx->frame_size / 8;
278 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
279 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
280 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
281 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
282 *dst++ = (unsigned char)c->status[0].step_index;
283 *dst++ = 0; /* unknown */
285 if (avctx->channels == 2) {
286 c->status[1].prev_sample = (signed short)samples[1];
287 /* c->status[1].step_index = 0; */
288 *dst++ = (c->status[1].prev_sample) & 0xFF;
289 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
290 *dst++ = (unsigned char)c->status[1].step_index;
295 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
297 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
298 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
300 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
301 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
303 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
304 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
306 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
307 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
310 if (avctx->channels == 2) {
311 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
312 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
314 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
315 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
317 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
318 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
320 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
321 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
324 samples += 8 * avctx->channels;
327 case CODEC_ID_ADPCM_MS:
328 for(i=0; i<avctx->channels; i++){
332 c->status[i].coeff1 = AdaptCoeff1[predictor];
333 c->status[i].coeff2 = AdaptCoeff2[predictor];
335 for(i=0; i<avctx->channels; i++){
336 if (c->status[i].idelta < 16)
337 c->status[i].idelta = 16;
339 *dst++ = c->status[i].idelta & 0xFF;
340 *dst++ = c->status[i].idelta >> 8;
342 for(i=0; i<avctx->channels; i++){
343 c->status[i].sample1= *samples++;
345 *dst++ = c->status[i].sample1 & 0xFF;
346 *dst++ = c->status[i].sample1 >> 8;
348 for(i=0; i<avctx->channels; i++){
349 c->status[i].sample2= *samples++;
351 *dst++ = c->status[i].sample2 & 0xFF;
352 *dst++ = c->status[i].sample2 >> 8;
355 for(i=7*avctx->channels; i<avctx->block_align; i++) {
357 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
358 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
362 case CODEC_ID_ADPCM_YAMAHA:
363 n = avctx->frame_size / 2;
365 for(i = 0; i < avctx->channels; i++) {
367 nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
368 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
371 samples += 2 * avctx->channels;
379 #endif //CONFIG_ENCODERS
381 static int adpcm_decode_init(AVCodecContext * avctx)
383 ADPCMContext *c = avctx->priv_data;
386 c->status[0].predictor = c->status[1].predictor = 0;
387 c->status[0].step_index = c->status[1].step_index = 0;
388 c->status[0].step = c->status[1].step = 0;
390 switch(avctx->codec->id) {
391 case CODEC_ID_ADPCM_CT:
392 c->status[0].step = c->status[1].step = 511;
400 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
404 int sign, delta, diff, step;
406 step = step_table[c->step_index];
407 step_index = c->step_index + index_table[(unsigned)nibble];
408 if (step_index < 0) step_index = 0;
409 else if (step_index > 88) step_index = 88;
413 /* perform direct multiplication instead of series of jumps proposed by
414 * the reference ADPCM implementation since modern CPUs can do the mults
416 diff = ((2 * delta + 1) * step) >> shift;
417 predictor = c->predictor;
418 if (sign) predictor -= diff;
419 else predictor += diff;
421 CLAMP_TO_SHORT(predictor);
422 c->predictor = predictor;
423 c->step_index = step_index;
425 return (short)predictor;
428 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
432 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
433 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
434 CLAMP_TO_SHORT(predictor);
436 c->sample2 = c->sample1;
437 c->sample1 = predictor;
438 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
439 if (c->idelta < 16) c->idelta = 16;
441 return (short)predictor;
444 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
447 int sign, delta, diff;
452 /* perform direct multiplication instead of series of jumps proposed by
453 * the reference ADPCM implementation since modern CPUs can do the mults
455 diff = ((2 * delta + 1) * c->step) >> 3;
456 predictor = c->predictor;
457 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
459 predictor = ((predictor * 254) >> 8) - diff;
461 predictor = ((predictor * 254) >> 8) + diff;
462 /* calculate new step and clamp it to range 511..32767 */
463 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
470 CLAMP_TO_SHORT(predictor);
471 c->predictor = predictor;
472 return (short)predictor;
475 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
477 int sign, delta, diff;
479 sign = nibble & (1<<(size-1));
480 delta = nibble & ((1<<(size-1))-1);
481 diff = delta << (7 + c->step + shift);
484 c->predictor -= diff;
486 c->predictor += diff;
489 if (c->predictor > 16256)
490 c->predictor = 16256;
491 else if (c->predictor < -16384)
492 c->predictor = -16384;
494 /* calculate new step */
495 if (delta >= (2*size - 3) && c->step < 3)
497 else if (delta == 0 && c->step > 0)
500 return (short) c->predictor;
503 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
510 c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
511 CLAMP_TO_SHORT(c->predictor);
512 c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
513 c->step = clip(c->step, 127, 24567);
517 static void xa_decode(short *out, const unsigned char *in,
518 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
521 int shift,filter,f0,f1;
527 shift = 12 - (in[4+i*2] & 15);
528 filter = in[4+i*2] >> 4;
529 f0 = xa_adpcm_table[filter][0];
530 f1 = xa_adpcm_table[filter][1];
538 t = (signed char)(d<<4)>>4;
539 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
547 if (inc==2) { /* stereo */
550 s_1 = right->sample1;
551 s_2 = right->sample2;
552 out = out + 1 - 28*2;
555 shift = 12 - (in[5+i*2] & 15);
556 filter = in[5+i*2] >> 4;
558 f0 = xa_adpcm_table[filter][0];
559 f1 = xa_adpcm_table[filter][1];
564 t = (signed char)d >> 4;
565 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
573 if (inc==2) { /* stereo */
574 right->sample1 = s_1;
575 right->sample2 = s_2;
585 /* DK3 ADPCM support macro */
586 #define DK3_GET_NEXT_NIBBLE() \
587 if (decode_top_nibble_next) \
589 nibble = (last_byte >> 4) & 0x0F; \
590 decode_top_nibble_next = 0; \
594 last_byte = *src++; \
595 if (src >= buf + buf_size) break; \
596 nibble = last_byte & 0x0F; \
597 decode_top_nibble_next = 1; \
600 static int adpcm_decode_frame(AVCodecContext *avctx,
601 void *data, int *data_size,
602 uint8_t *buf, int buf_size)
604 ADPCMContext *c = avctx->priv_data;
605 ADPCMChannelStatus *cs;
606 int n, m, channel, i;
607 int block_predictor[2];
612 /* DK3 ADPCM accounting variables */
613 unsigned char last_byte = 0;
614 unsigned char nibble;
615 int decode_top_nibble_next = 0;
618 /* EA ADPCM state variables */
619 uint32_t samples_in_chunk;
620 int32_t previous_left_sample, previous_right_sample;
621 int32_t current_left_sample, current_right_sample;
622 int32_t next_left_sample, next_right_sample;
623 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
624 uint8_t shift_left, shift_right;
633 st = avctx->channels == 2 ? 1 : 0;
635 switch(avctx->codec->id) {
636 case CODEC_ID_ADPCM_IMA_QT:
637 n = (buf_size - 2);/* >> 2*avctx->channels;*/
638 channel = c->channel;
639 cs = &(c->status[channel]);
640 /* (pppppp) (piiiiiii) */
642 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
643 cs->predictor = (*src++) << 8;
644 cs->predictor |= (*src & 0x80);
645 cs->predictor &= 0xFF80;
648 if(cs->predictor & 0x8000)
649 cs->predictor -= 0x10000;
651 CLAMP_TO_SHORT(cs->predictor);
653 cs->step_index = (*src++) & 0x7F;
655 if (cs->step_index > 88){
656 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
660 cs->step = step_table[cs->step_index];
665 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
666 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
667 samples += avctx->channels;
668 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
669 samples += avctx->channels;
673 if(st) { /* handle stereo interlacing */
674 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
675 if(channel == 1) { /* wait for the other packet before outputing anything */
680 case CODEC_ID_ADPCM_IMA_WAV:
681 if (avctx->block_align != 0 && buf_size > avctx->block_align)
682 buf_size = avctx->block_align;
684 // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
686 for(i=0; i<avctx->channels; i++){
687 cs = &(c->status[i]);
688 cs->predictor = (int16_t)(src[0] + (src[1]<<8));
691 // XXX: is this correct ??: *samples++ = cs->predictor;
693 cs->step_index = *src++;
694 if (cs->step_index > 88){
695 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
698 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
701 while(src < buf + buf_size){
704 *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
706 *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
712 case CODEC_ID_ADPCM_4XM:
713 cs = &(c->status[0]);
714 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
716 c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
718 c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
720 c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
722 if (cs->step_index < 0) cs->step_index = 0;
723 if (cs->step_index > 88) cs->step_index = 88;
725 m= (buf_size - (src - buf))>>st;
727 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
729 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
730 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
732 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
738 case CODEC_ID_ADPCM_MS:
739 if (avctx->block_align != 0 && buf_size > avctx->block_align)
740 buf_size = avctx->block_align;
741 n = buf_size - 7 * avctx->channels;
744 block_predictor[0] = clip(*src++, 0, 7);
745 block_predictor[1] = 0;
747 block_predictor[1] = clip(*src++, 0, 7);
748 c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
751 c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
754 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
755 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
756 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
757 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
759 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
761 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
763 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
765 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
768 *samples++ = c->status[0].sample1;
769 if (st) *samples++ = c->status[1].sample1;
770 *samples++ = c->status[0].sample2;
771 if (st) *samples++ = c->status[1].sample2;
773 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
774 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
778 case CODEC_ID_ADPCM_IMA_DK4:
779 if (avctx->block_align != 0 && buf_size > avctx->block_align)
780 buf_size = avctx->block_align;
782 c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
783 c->status[0].step_index = src[2];
785 *samples++ = c->status[0].predictor;
787 c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
788 c->status[1].step_index = src[2];
790 *samples++ = c->status[1].predictor;
792 while (src < buf + buf_size) {
794 /* take care of the top nibble (always left or mono channel) */
795 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
796 (src[0] >> 4) & 0x0F, 3);
798 /* take care of the bottom nibble, which is right sample for
799 * stereo, or another mono sample */
801 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
804 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
810 case CODEC_ID_ADPCM_IMA_DK3:
811 if (avctx->block_align != 0 && buf_size > avctx->block_align)
812 buf_size = avctx->block_align;
814 c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
815 c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
816 c->status[0].step_index = src[14];
817 c->status[1].step_index = src[15];
818 /* sign extend the predictors */
820 diff_channel = c->status[1].predictor;
822 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
823 * the buffer is consumed */
826 /* for this algorithm, c->status[0] is the sum channel and
827 * c->status[1] is the diff channel */
829 /* process the first predictor of the sum channel */
830 DK3_GET_NEXT_NIBBLE();
831 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
833 /* process the diff channel predictor */
834 DK3_GET_NEXT_NIBBLE();
835 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
837 /* process the first pair of stereo PCM samples */
838 diff_channel = (diff_channel + c->status[1].predictor) / 2;
839 *samples++ = c->status[0].predictor + c->status[1].predictor;
840 *samples++ = c->status[0].predictor - c->status[1].predictor;
842 /* process the second predictor of the sum channel */
843 DK3_GET_NEXT_NIBBLE();
844 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
846 /* process the second pair of stereo PCM samples */
847 diff_channel = (diff_channel + c->status[1].predictor) / 2;
848 *samples++ = c->status[0].predictor + c->status[1].predictor;
849 *samples++ = c->status[0].predictor - c->status[1].predictor;
852 case CODEC_ID_ADPCM_IMA_WS:
853 /* no per-block initialization; just start decoding the data */
854 while (src < buf + buf_size) {
857 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
858 (src[0] >> 4) & 0x0F, 3);
859 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
862 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
863 (src[0] >> 4) & 0x0F, 3);
864 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
871 case CODEC_ID_ADPCM_XA:
872 c->status[0].sample1 = c->status[0].sample2 =
873 c->status[1].sample1 = c->status[1].sample2 = 0;
874 while (buf_size >= 128) {
875 xa_decode(samples, src, &c->status[0], &c->status[1],
882 case CODEC_ID_ADPCM_EA:
883 samples_in_chunk = LE_32(src);
884 if (samples_in_chunk >= ((buf_size - 12) * 2)) {
889 current_left_sample = (int16_t)LE_16(src);
891 previous_left_sample = (int16_t)LE_16(src);
893 current_right_sample = (int16_t)LE_16(src);
895 previous_right_sample = (int16_t)LE_16(src);
898 for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
899 coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
900 coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
901 coeff1r = ea_adpcm_table[*src & 0x0F];
902 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
905 shift_left = ((*src >> 4) & 0x0F) + 8;
906 shift_right = (*src & 0x0F) + 8;
909 for (count2 = 0; count2 < 28; count2++) {
910 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
911 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
914 next_left_sample = (next_left_sample +
915 (current_left_sample * coeff1l) +
916 (previous_left_sample * coeff2l) + 0x80) >> 8;
917 next_right_sample = (next_right_sample +
918 (current_right_sample * coeff1r) +
919 (previous_right_sample * coeff2r) + 0x80) >> 8;
920 CLAMP_TO_SHORT(next_left_sample);
921 CLAMP_TO_SHORT(next_right_sample);
923 previous_left_sample = current_left_sample;
924 current_left_sample = next_left_sample;
925 previous_right_sample = current_right_sample;
926 current_right_sample = next_right_sample;
927 *samples++ = (unsigned short)current_left_sample;
928 *samples++ = (unsigned short)current_right_sample;
932 case CODEC_ID_ADPCM_IMA_SMJPEG:
933 c->status[0].predictor = *src;
935 c->status[0].step_index = *src++;
936 src++; /* skip another byte before getting to the meat */
937 while (src < buf + buf_size) {
938 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
940 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
941 (*src >> 4) & 0x0F, 3);
945 case CODEC_ID_ADPCM_CT:
946 while (src < buf + buf_size) {
948 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
949 (src[0] >> 4) & 0x0F);
950 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
953 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
954 (src[0] >> 4) & 0x0F);
955 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
961 case CODEC_ID_ADPCM_SBPRO_4:
962 case CODEC_ID_ADPCM_SBPRO_3:
963 case CODEC_ID_ADPCM_SBPRO_2:
964 if (!c->status[0].step_index) {
965 /* the first byte is a raw sample */
966 *samples++ = 128 * (*src++ - 0x80);
968 *samples++ = 128 * (*src++ - 0x80);
969 c->status[0].step_index = 1;
971 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
972 while (src < buf + buf_size) {
973 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
974 (src[0] >> 4) & 0x0F, 4, 0);
975 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
976 src[0] & 0x0F, 4, 0);
979 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
980 while (src < buf + buf_size) {
981 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
982 (src[0] >> 5) & 0x07, 3, 0);
983 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
984 (src[0] >> 2) & 0x07, 3, 0);
985 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
986 src[0] & 0x03, 2, 0);
990 while (src < buf + buf_size) {
991 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
992 (src[0] >> 6) & 0x03, 2, 2);
993 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
994 (src[0] >> 4) & 0x03, 2, 2);
995 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
996 (src[0] >> 2) & 0x03, 2, 2);
997 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
998 src[0] & 0x03, 2, 2);
1003 case CODEC_ID_ADPCM_SWF:
1008 int size = buf_size*8;
1010 init_get_bits(&gb, buf, size);
1012 // first frame, read bits & inital values
1015 c->nb_bits = get_bits(&gb, 2)+2;
1016 // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
1019 table = swf_index_tables[c->nb_bits-2];
1020 k0 = 1 << (c->nb_bits-2);
1021 signmask = 1 << (c->nb_bits-1);
1023 while (get_bits_count(&gb) <= size)
1028 // wrap around at every 4096 samples...
1029 if ((c->nb_samples & 0xfff) == 1)
1031 for (i = 0; i <= st; i++)
1033 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1034 c->status[i].step_index = get_bits(&gb, 6);
1038 // similar to IMA adpcm
1039 for (i = 0; i <= st; i++)
1041 int delta = get_bits(&gb, c->nb_bits);
1042 int step = step_table[c->status[i].step_index];
1043 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1054 if (delta & signmask)
1055 c->status[i].predictor -= vpdiff;
1057 c->status[i].predictor += vpdiff;
1059 c->status[i].step_index += table[delta & (~signmask)];
1061 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1062 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1064 *samples++ = c->status[i].predictor;
1068 // src += get_bits_count(&gb)*8;
1073 case CODEC_ID_ADPCM_YAMAHA:
1074 while (src < buf + buf_size) {
1076 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1078 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1079 (src[0] >> 4) & 0x0F);
1081 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1083 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1084 (src[0] >> 4) & 0x0F);
1092 *data_size = (uint8_t *)samples - (uint8_t *)data;
1098 #ifdef CONFIG_ENCODERS
1099 #define ADPCM_ENCODER(id,name) \
1100 AVCodec name ## _encoder = { \
1104 sizeof(ADPCMContext), \
1105 adpcm_encode_init, \
1106 adpcm_encode_frame, \
1107 adpcm_encode_close, \
1111 #define ADPCM_ENCODER(id,name)
1114 #ifdef CONFIG_DECODERS
1115 #define ADPCM_DECODER(id,name) \
1116 AVCodec name ## _decoder = { \
1120 sizeof(ADPCMContext), \
1121 adpcm_decode_init, \
1124 adpcm_decode_frame, \
1127 #define ADPCM_DECODER(id,name)
1130 #define ADPCM_CODEC(id, name) \
1131 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1133 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1134 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1135 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1136 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1137 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1138 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1139 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1140 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1141 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1142 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1143 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1144 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1145 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1146 ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1147 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1148 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1149 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);