2 * Copyright (c) 2001-2003 The ffmpeg Project
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
25 #include "adpcm_data.h"
30 * First version by Francois Revol (revol@free.fr)
31 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
32 * by Mike Melanson (melanson@pcisys.net)
33 * CD-ROM XA ADPCM codec by BERO
34 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
35 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
36 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
37 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
38 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
39 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
40 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
42 * Features and limitations:
44 * Reference documents:
45 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48 * http://openquicktime.sourceforge.net/
49 * XAnim sources (xa_codec.c) http://xanim.polter.net/
50 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51 * SoX source code http://sox.sourceforge.net/
54 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56 * readstr http://www.geocities.co.jp/Playtown/2004/
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table[5][2] = {
68 static const int ea_adpcm_table[] = {
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables[4][16] = {
79 /*3*/ { -1, -1, 2, 4 },
80 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
86 typedef struct ADPCMDecodeContext {
87 ADPCMChannelStatus status[6];
90 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
92 ADPCMDecodeContext *c = avctx->priv_data;
93 unsigned int max_channels = 2;
95 switch(avctx->codec->id) {
96 case CODEC_ID_ADPCM_EA_R1:
97 case CODEC_ID_ADPCM_EA_R2:
98 case CODEC_ID_ADPCM_EA_R3:
99 case CODEC_ID_ADPCM_EA_XAS:
103 if(avctx->channels > max_channels){
107 switch(avctx->codec->id) {
108 case CODEC_ID_ADPCM_CT:
109 c->status[0].step = c->status[1].step = 511;
111 case CODEC_ID_ADPCM_IMA_WAV:
112 if (avctx->bits_per_coded_sample != 4) {
113 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
117 case CODEC_ID_ADPCM_IMA_WS:
118 if (avctx->extradata && avctx->extradata_size == 2 * 4) {
119 c->status[0].predictor = AV_RL32(avctx->extradata);
120 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
126 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
130 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
134 int sign, delta, diff, step;
136 step = ff_adpcm_step_table[c->step_index];
137 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
138 if (step_index < 0) step_index = 0;
139 else if (step_index > 88) step_index = 88;
143 /* perform direct multiplication instead of series of jumps proposed by
144 * the reference ADPCM implementation since modern CPUs can do the mults
146 diff = ((2 * delta + 1) * step) >> shift;
147 predictor = c->predictor;
148 if (sign) predictor -= diff;
149 else predictor += diff;
151 c->predictor = av_clip_int16(predictor);
152 c->step_index = step_index;
154 return (short)c->predictor;
157 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
163 step = ff_adpcm_step_table[c->step_index];
164 step_index = c->step_index + ff_adpcm_index_table[nibble];
165 step_index = av_clip(step_index, 0, 88);
168 if (nibble & 4) diff += step;
169 if (nibble & 2) diff += step >> 1;
170 if (nibble & 1) diff += step >> 2;
173 predictor = c->predictor - diff;
175 predictor = c->predictor + diff;
177 c->predictor = av_clip_int16(predictor);
178 c->step_index = step_index;
183 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
187 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
188 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
190 c->sample2 = c->sample1;
191 c->sample1 = av_clip_int16(predictor);
192 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
193 if (c->idelta < 16) c->idelta = 16;
198 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
200 int sign, delta, diff;
205 /* perform direct multiplication instead of series of jumps proposed by
206 * the reference ADPCM implementation since modern CPUs can do the mults
208 diff = ((2 * delta + 1) * c->step) >> 3;
209 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
210 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
211 c->predictor = av_clip_int16(c->predictor);
212 /* calculate new step and clamp it to range 511..32767 */
213 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
214 c->step = av_clip(new_step, 511, 32767);
216 return (short)c->predictor;
219 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
221 int sign, delta, diff;
223 sign = nibble & (1<<(size-1));
224 delta = nibble & ((1<<(size-1))-1);
225 diff = delta << (7 + c->step + shift);
228 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
230 /* calculate new step */
231 if (delta >= (2*size - 3) && c->step < 3)
233 else if (delta == 0 && c->step > 0)
236 return (short) c->predictor;
239 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
246 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
247 c->predictor = av_clip_int16(c->predictor);
248 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
249 c->step = av_clip(c->step, 127, 24567);
253 static void xa_decode(short *out, const unsigned char *in,
254 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
257 int shift,filter,f0,f1;
263 shift = 12 - (in[4+i*2] & 15);
264 filter = in[4+i*2] >> 4;
265 f0 = xa_adpcm_table[filter][0];
266 f1 = xa_adpcm_table[filter][1];
274 t = (signed char)(d<<4)>>4;
275 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
277 s_1 = av_clip_int16(s);
282 if (inc==2) { /* stereo */
285 s_1 = right->sample1;
286 s_2 = right->sample2;
287 out = out + 1 - 28*2;
290 shift = 12 - (in[5+i*2] & 15);
291 filter = in[5+i*2] >> 4;
293 f0 = xa_adpcm_table[filter][0];
294 f1 = xa_adpcm_table[filter][1];
299 t = (signed char)d >> 4;
300 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
302 s_1 = av_clip_int16(s);
307 if (inc==2) { /* stereo */
308 right->sample1 = s_1;
309 right->sample2 = s_2;
319 /* DK3 ADPCM support macro */
320 #define DK3_GET_NEXT_NIBBLE() \
321 if (decode_top_nibble_next) \
323 nibble = last_byte >> 4; \
324 decode_top_nibble_next = 0; \
328 last_byte = *src++; \
329 if (src >= buf + buf_size) break; \
330 nibble = last_byte & 0x0F; \
331 decode_top_nibble_next = 1; \
334 static int adpcm_decode_frame(AVCodecContext *avctx,
335 void *data, int *data_size,
338 const uint8_t *buf = avpkt->data;
339 int buf_size = avpkt->size;
340 ADPCMDecodeContext *c = avctx->priv_data;
341 ADPCMChannelStatus *cs;
342 int n, m, channel, i;
348 /* DK3 ADPCM accounting variables */
349 unsigned char last_byte = 0;
350 unsigned char nibble;
351 int decode_top_nibble_next = 0;
354 /* EA ADPCM state variables */
355 uint32_t samples_in_chunk;
356 int32_t previous_left_sample, previous_right_sample;
357 int32_t current_left_sample, current_right_sample;
358 int32_t next_left_sample, next_right_sample;
359 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
360 uint8_t shift_left, shift_right;
362 int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
367 //should protect all 4bit ADPCM variants
368 //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
370 if(*data_size/4 < buf_size + 8)
374 samples_end= samples + *data_size/2;
378 st = avctx->channels == 2 ? 1 : 0;
380 switch(avctx->codec->id) {
381 case CODEC_ID_ADPCM_IMA_QT:
382 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
383 Channel data is interleaved per-chunk. */
384 if (buf_size / 34 < avctx->channels) {
385 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
386 return AVERROR(EINVAL);
388 for (channel = 0; channel < avctx->channels; channel++) {
391 cs = &(c->status[channel]);
392 /* (pppppp) (piiiiiii) */
394 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
395 predictor = AV_RB16(src);
396 step_index = predictor & 0x7F;
401 if (cs->step_index == step_index) {
402 int diff = (int)predictor - cs->predictor;
409 cs->step_index = step_index;
410 cs->predictor = predictor;
413 if (cs->step_index > 88){
414 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
418 samples = (short*)data + channel;
420 for (m = 0; m < 32; m++) {
421 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
422 samples += avctx->channels;
423 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
424 samples += avctx->channels;
431 case CODEC_ID_ADPCM_IMA_WAV:
432 if (avctx->block_align != 0 && buf_size > avctx->block_align)
433 buf_size = avctx->block_align;
435 // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
437 for(i=0; i<avctx->channels; i++){
438 cs = &(c->status[i]);
439 cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
441 cs->step_index = *src++;
442 if (cs->step_index > 88){
443 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
446 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
449 while(src < buf + buf_size){
450 for (i = 0; i < avctx->channels; i++) {
452 for (m = 0; m < 4; m++) {
454 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
455 samples += avctx->channels;
456 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
457 samples += avctx->channels;
459 samples -= 8 * avctx->channels - 1;
461 samples += 7 * avctx->channels;
464 case CODEC_ID_ADPCM_4XM:
465 for (i = 0; i < avctx->channels; i++)
466 c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
468 for (i = 0; i < avctx->channels; i++) {
469 c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
470 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
473 m= (buf_size - (src - buf))>>st;
475 for (i = 0; i < avctx->channels; i++) {
476 samples = (short*)data + i;
478 for (n = 0; n < m; n++) {
480 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
481 samples += avctx->channels;
482 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
483 samples += avctx->channels;
486 samples -= (avctx->channels - 1);
488 case CODEC_ID_ADPCM_MS:
492 if (avctx->block_align != 0 && buf_size > avctx->block_align)
493 buf_size = avctx->block_align;
494 n = buf_size - 7 * avctx->channels;
498 block_predictor = av_clip(*src++, 0, 6);
499 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
500 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
502 block_predictor = av_clip(*src++, 0, 6);
503 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
504 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
506 c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
508 c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
511 c->status[0].sample1 = bytestream_get_le16(&src);
512 if (st) c->status[1].sample1 = bytestream_get_le16(&src);
513 c->status[0].sample2 = bytestream_get_le16(&src);
514 if (st) c->status[1].sample2 = bytestream_get_le16(&src);
516 *samples++ = c->status[0].sample2;
517 if (st) *samples++ = c->status[1].sample2;
518 *samples++ = c->status[0].sample1;
519 if (st) *samples++ = c->status[1].sample1;
521 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
522 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
527 case CODEC_ID_ADPCM_IMA_DK4:
528 if (avctx->block_align != 0 && buf_size > avctx->block_align)
529 buf_size = avctx->block_align;
531 n = buf_size - 4 * avctx->channels;
533 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
534 return AVERROR(EINVAL);
537 for (channel = 0; channel < avctx->channels; channel++) {
538 cs = &c->status[channel];
539 cs->predictor = (int16_t)bytestream_get_le16(&src);
540 cs->step_index = *src++;
542 *samples++ = cs->predictor;
546 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
547 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
550 case CODEC_ID_ADPCM_IMA_DK3:
551 if (avctx->block_align != 0 && buf_size > avctx->block_align)
552 buf_size = avctx->block_align;
554 if(buf_size + 16 > (samples_end - samples)*3/8)
557 c->status[0].predictor = (int16_t)AV_RL16(src + 10);
558 c->status[1].predictor = (int16_t)AV_RL16(src + 12);
559 c->status[0].step_index = src[14];
560 c->status[1].step_index = src[15];
561 /* sign extend the predictors */
563 diff_channel = c->status[1].predictor;
565 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
566 * the buffer is consumed */
569 /* for this algorithm, c->status[0] is the sum channel and
570 * c->status[1] is the diff channel */
572 /* process the first predictor of the sum channel */
573 DK3_GET_NEXT_NIBBLE();
574 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
576 /* process the diff channel predictor */
577 DK3_GET_NEXT_NIBBLE();
578 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
580 /* process the first pair of stereo PCM samples */
581 diff_channel = (diff_channel + c->status[1].predictor) / 2;
582 *samples++ = c->status[0].predictor + c->status[1].predictor;
583 *samples++ = c->status[0].predictor - c->status[1].predictor;
585 /* process the second predictor of the sum channel */
586 DK3_GET_NEXT_NIBBLE();
587 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
589 /* process the second pair of stereo PCM samples */
590 diff_channel = (diff_channel + c->status[1].predictor) / 2;
591 *samples++ = c->status[0].predictor + c->status[1].predictor;
592 *samples++ = c->status[0].predictor - c->status[1].predictor;
595 case CODEC_ID_ADPCM_IMA_ISS:
596 n = buf_size - 4 * avctx->channels;
598 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
599 return AVERROR(EINVAL);
602 for (channel = 0; channel < avctx->channels; channel++) {
603 cs = &c->status[channel];
604 cs->predictor = (int16_t)bytestream_get_le16(&src);
605 cs->step_index = *src++;
612 /* nibbles are swapped for mono */
620 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
621 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
624 case CODEC_ID_ADPCM_IMA_WS:
625 while (src < buf + buf_size) {
627 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
628 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
631 case CODEC_ID_ADPCM_XA:
632 while (buf_size >= 128) {
633 xa_decode(samples, src, &c->status[0], &c->status[1],
640 case CODEC_ID_ADPCM_IMA_EA_EACS:
641 samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
643 if (samples_in_chunk > buf_size-4-(8<<st)) {
648 for (i=0; i<=st; i++)
649 c->status[i].step_index = bytestream_get_le32(&src);
650 for (i=0; i<=st; i++)
651 c->status[i].predictor = bytestream_get_le32(&src);
653 for (; samples_in_chunk; samples_in_chunk--, src++) {
654 *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
655 *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
658 case CODEC_ID_ADPCM_IMA_EA_SEAD:
659 for (; src < buf+buf_size; src++) {
660 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
661 *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
664 case CODEC_ID_ADPCM_EA:
665 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
666 each coding 28 stereo samples. */
668 av_log(avctx, AV_LOG_ERROR, "frame too small\n");
669 return AVERROR(EINVAL);
671 samples_in_chunk = AV_RL32(src);
672 if (samples_in_chunk / 28 > (buf_size - 12) / 30) {
673 av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
674 return AVERROR(EINVAL);
677 current_left_sample = (int16_t)bytestream_get_le16(&src);
678 previous_left_sample = (int16_t)bytestream_get_le16(&src);
679 current_right_sample = (int16_t)bytestream_get_le16(&src);
680 previous_right_sample = (int16_t)bytestream_get_le16(&src);
682 for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
683 coeff1l = ea_adpcm_table[ *src >> 4 ];
684 coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
685 coeff1r = ea_adpcm_table[*src & 0x0F];
686 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
689 shift_left = (*src >> 4 ) + 8;
690 shift_right = (*src & 0x0F) + 8;
693 for (count2 = 0; count2 < 28; count2++) {
694 next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
695 next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
698 next_left_sample = (next_left_sample +
699 (current_left_sample * coeff1l) +
700 (previous_left_sample * coeff2l) + 0x80) >> 8;
701 next_right_sample = (next_right_sample +
702 (current_right_sample * coeff1r) +
703 (previous_right_sample * coeff2r) + 0x80) >> 8;
705 previous_left_sample = current_left_sample;
706 current_left_sample = av_clip_int16(next_left_sample);
707 previous_right_sample = current_right_sample;
708 current_right_sample = av_clip_int16(next_right_sample);
709 *samples++ = (unsigned short)current_left_sample;
710 *samples++ = (unsigned short)current_right_sample;
714 if (src - buf == buf_size - 2)
715 src += 2; // Skip terminating 0x0000
718 case CODEC_ID_ADPCM_EA_MAXIS_XA:
719 for(channel = 0; channel < avctx->channels; channel++) {
721 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
722 shift[channel] = (*src & 0x0F) + 8;
725 for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
726 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
727 for(channel = 0; channel < avctx->channels; channel++) {
728 int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
730 c->status[channel].sample1 * coeff[channel][0] +
731 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
732 c->status[channel].sample2 = c->status[channel].sample1;
733 c->status[channel].sample1 = av_clip_int16(sample);
734 *samples++ = c->status[channel].sample1;
737 src+=avctx->channels;
740 case CODEC_ID_ADPCM_EA_R1:
741 case CODEC_ID_ADPCM_EA_R2:
742 case CODEC_ID_ADPCM_EA_R3: {
745 4chan: 0=fl, 1=rl, 2=fr, 3=rr
746 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
747 const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
748 int32_t previous_sample, current_sample, next_sample;
749 int32_t coeff1, coeff2;
751 unsigned int channel;
754 const uint8_t *src_end = buf + buf_size;
756 samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
757 : bytestream_get_le32(&src)) / 28;
758 if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
759 28*samples_in_chunk*avctx->channels > samples_end-samples) {
764 for (channel=0; channel<avctx->channels; channel++) {
765 int32_t offset = (big_endian ? bytestream_get_be32(&src)
766 : bytestream_get_le32(&src))
767 + (avctx->channels-channel-1) * 4;
769 if ((offset < 0) || (offset >= src_end - src - 4)) break;
771 samplesC = samples + channel;
773 if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
774 current_sample = (int16_t)bytestream_get_le16(&srcC);
775 previous_sample = (int16_t)bytestream_get_le16(&srcC);
777 current_sample = c->status[channel].predictor;
778 previous_sample = c->status[channel].prev_sample;
781 for (count1=0; count1<samples_in_chunk; count1++) {
782 if (*srcC == 0xEE) { /* only seen in R2 and R3 */
784 if (srcC > src_end - 30*2) break;
785 current_sample = (int16_t)bytestream_get_be16(&srcC);
786 previous_sample = (int16_t)bytestream_get_be16(&srcC);
788 for (count2=0; count2<28; count2++) {
789 *samplesC = (int16_t)bytestream_get_be16(&srcC);
790 samplesC += avctx->channels;
793 coeff1 = ea_adpcm_table[ *srcC>>4 ];
794 coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
795 shift = (*srcC++ & 0x0F) + 8;
797 if (srcC > src_end - 14) break;
798 for (count2=0; count2<28; count2++) {
800 next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
802 next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
804 next_sample += (current_sample * coeff1) +
805 (previous_sample * coeff2);
806 next_sample = av_clip_int16(next_sample >> 8);
808 previous_sample = current_sample;
809 current_sample = next_sample;
810 *samplesC = current_sample;
811 samplesC += avctx->channels;
816 if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
817 c->status[channel].predictor = current_sample;
818 c->status[channel].prev_sample = previous_sample;
822 src = src + buf_size - (4 + 4*avctx->channels);
823 samples += 28 * samples_in_chunk * avctx->channels;
826 case CODEC_ID_ADPCM_EA_XAS:
827 if (samples_end-samples < 32*4*avctx->channels
828 || buf_size < (4+15)*4*avctx->channels) {
832 for (channel=0; channel<avctx->channels; channel++) {
833 int coeff[2][4], shift[4];
834 short *s2, *s = &samples[channel];
835 for (n=0; n<4; n++, s+=32*avctx->channels) {
837 coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
838 shift[n] = (src[2]&0x0F) + 8;
839 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
840 s2[0] = (src[0]&0xF0) + (src[1]<<8);
843 for (m=2; m<32; m+=2) {
844 s = &samples[m*avctx->channels + channel];
845 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
846 for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
847 int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
848 int pred = s2[-1*avctx->channels] * coeff[0][n]
849 + s2[-2*avctx->channels] * coeff[1][n];
850 s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
855 samples += 32*4*avctx->channels;
857 case CODEC_ID_ADPCM_IMA_AMV:
858 case CODEC_ID_ADPCM_IMA_SMJPEG:
859 c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
860 c->status[0].step_index = bytestream_get_le16(&src);
862 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
865 while (src < buf + buf_size) {
870 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
871 FFSWAP(char, hi, lo);
873 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
875 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
880 case CODEC_ID_ADPCM_CT:
881 while (src < buf + buf_size) {
883 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
884 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
887 case CODEC_ID_ADPCM_SBPRO_4:
888 case CODEC_ID_ADPCM_SBPRO_3:
889 case CODEC_ID_ADPCM_SBPRO_2:
890 if (!c->status[0].step_index) {
891 /* the first byte is a raw sample */
892 *samples++ = 128 * (*src++ - 0x80);
894 *samples++ = 128 * (*src++ - 0x80);
895 c->status[0].step_index = 1;
897 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
898 while (src < buf + buf_size) {
899 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
901 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
902 src[0] & 0x0F, 4, 0);
905 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
906 while (src < buf + buf_size && samples + 2 < samples_end) {
907 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
909 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
910 (src[0] >> 2) & 0x07, 3, 0);
911 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
912 src[0] & 0x03, 2, 0);
916 while (src < buf + buf_size && samples + 3 < samples_end) {
917 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
919 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
920 (src[0] >> 4) & 0x03, 2, 2);
921 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
922 (src[0] >> 2) & 0x03, 2, 2);
923 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
924 src[0] & 0x03, 2, 2);
929 case CODEC_ID_ADPCM_SWF:
933 int k0, signmask, nb_bits, count;
934 int size = buf_size*8;
936 init_get_bits(&gb, buf, size);
938 //read bits & initial values
939 nb_bits = get_bits(&gb, 2)+2;
940 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
941 table = swf_index_tables[nb_bits-2];
942 k0 = 1 << (nb_bits-2);
943 signmask = 1 << (nb_bits-1);
945 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
946 for (i = 0; i < avctx->channels; i++) {
947 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
948 c->status[i].step_index = get_bits(&gb, 6);
951 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
954 for (i = 0; i < avctx->channels; i++) {
955 // similar to IMA adpcm
956 int delta = get_bits(&gb, nb_bits);
957 int step = ff_adpcm_step_table[c->status[i].step_index];
958 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
969 if (delta & signmask)
970 c->status[i].predictor -= vpdiff;
972 c->status[i].predictor += vpdiff;
974 c->status[i].step_index += table[delta & (~signmask)];
976 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
977 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
979 *samples++ = c->status[i].predictor;
980 if (samples >= samples_end) {
981 av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
990 case CODEC_ID_ADPCM_YAMAHA:
991 while (src < buf + buf_size) {
993 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
994 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
997 case CODEC_ID_ADPCM_THP:
1000 unsigned int samplecnt;
1004 if (buf_size < 80) {
1005 av_log(avctx, AV_LOG_ERROR, "frame too small\n");
1010 samplecnt = bytestream_get_be32(&src);
1012 for (i = 0; i < 32; i++)
1013 table[0][i] = (int16_t)bytestream_get_be16(&src);
1015 /* Initialize the previous sample. */
1016 for (i = 0; i < 4; i++)
1017 prev[0][i] = (int16_t)bytestream_get_be16(&src);
1019 if (samplecnt >= (samples_end - samples) / (st + 1)) {
1020 av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
1024 for (ch = 0; ch <= st; ch++) {
1025 samples = (unsigned short *) data + ch;
1027 /* Read in every sample for this channel. */
1028 for (i = 0; i < samplecnt / 14; i++) {
1029 int index = (*src >> 4) & 7;
1030 unsigned int exp = 28 - (*src++ & 15);
1031 int factor1 = table[ch][index * 2];
1032 int factor2 = table[ch][index * 2 + 1];
1034 /* Decode 14 samples. */
1035 for (n = 0; n < 14; n++) {
1037 if(n&1) sampledat= *src++ <<28;
1038 else sampledat= (*src&0xF0)<<24;
1040 sampledat = ((prev[ch][0]*factor1
1041 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
1042 *samples = av_clip_int16(sampledat);
1043 prev[ch][1] = prev[ch][0];
1044 prev[ch][0] = *samples++;
1046 /* In case of stereo, skip one sample, this sample
1047 is for the other channel. */
1053 /* In the previous loop, in case stereo is used, samples is
1054 increased exactly one time too often. */
1062 *data_size = (uint8_t *)samples - (uint8_t *)data;
1067 #define ADPCM_DECODER(id_, name_, long_name_) \
1068 AVCodec ff_ ## name_ ## _decoder = { \
1070 .type = AVMEDIA_TYPE_AUDIO, \
1072 .priv_data_size = sizeof(ADPCMDecodeContext), \
1073 .init = adpcm_decode_init, \
1074 .decode = adpcm_decode_frame, \
1075 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1078 /* Note: Do not forget to add new entries to the Makefile as well. */
1079 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1080 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1081 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1082 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1083 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1084 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1085 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1086 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1087 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1088 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1089 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1090 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1091 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1092 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1093 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1094 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1095 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1096 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1097 ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1098 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1099 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1100 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1101 ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1102 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1103 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1104 ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");