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 {
88 ADPCMChannelStatus status[6];
91 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
93 ADPCMDecodeContext *c = avctx->priv_data;
94 unsigned int max_channels = 2;
96 switch(avctx->codec->id) {
97 case CODEC_ID_ADPCM_EA_R1:
98 case CODEC_ID_ADPCM_EA_R2:
99 case CODEC_ID_ADPCM_EA_R3:
100 case CODEC_ID_ADPCM_EA_XAS:
104 if(avctx->channels > max_channels){
108 switch(avctx->codec->id) {
109 case CODEC_ID_ADPCM_CT:
110 c->status[0].step = c->status[1].step = 511;
112 case CODEC_ID_ADPCM_IMA_WAV:
113 if (avctx->bits_per_coded_sample != 4) {
114 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
118 case CODEC_ID_ADPCM_IMA_WS:
119 if (avctx->extradata && avctx->extradata_size == 2 * 4) {
120 c->status[0].predictor = AV_RL32(avctx->extradata);
121 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
127 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
129 avcodec_get_frame_defaults(&c->frame);
130 avctx->coded_frame = &c->frame;
135 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
139 int sign, delta, diff, step;
141 step = ff_adpcm_step_table[c->step_index];
142 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
143 if (step_index < 0) step_index = 0;
144 else if (step_index > 88) step_index = 88;
148 /* perform direct multiplication instead of series of jumps proposed by
149 * the reference ADPCM implementation since modern CPUs can do the mults
151 diff = ((2 * delta + 1) * step) >> shift;
152 predictor = c->predictor;
153 if (sign) predictor -= diff;
154 else predictor += diff;
156 c->predictor = av_clip_int16(predictor);
157 c->step_index = step_index;
159 return (short)c->predictor;
162 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
168 step = ff_adpcm_step_table[c->step_index];
169 step_index = c->step_index + ff_adpcm_index_table[nibble];
170 step_index = av_clip(step_index, 0, 88);
173 if (nibble & 4) diff += step;
174 if (nibble & 2) diff += step >> 1;
175 if (nibble & 1) diff += step >> 2;
178 predictor = c->predictor - diff;
180 predictor = c->predictor + diff;
182 c->predictor = av_clip_int16(predictor);
183 c->step_index = step_index;
188 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
192 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
193 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
195 c->sample2 = c->sample1;
196 c->sample1 = av_clip_int16(predictor);
197 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
198 if (c->idelta < 16) c->idelta = 16;
203 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
205 int sign, delta, diff;
210 /* perform direct multiplication instead of series of jumps proposed by
211 * the reference ADPCM implementation since modern CPUs can do the mults
213 diff = ((2 * delta + 1) * c->step) >> 3;
214 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
215 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
216 c->predictor = av_clip_int16(c->predictor);
217 /* calculate new step and clamp it to range 511..32767 */
218 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
219 c->step = av_clip(new_step, 511, 32767);
221 return (short)c->predictor;
224 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
226 int sign, delta, diff;
228 sign = nibble & (1<<(size-1));
229 delta = nibble & ((1<<(size-1))-1);
230 diff = delta << (7 + c->step + shift);
233 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
235 /* calculate new step */
236 if (delta >= (2*size - 3) && c->step < 3)
238 else if (delta == 0 && c->step > 0)
241 return (short) c->predictor;
244 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
251 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
252 c->predictor = av_clip_int16(c->predictor);
253 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
254 c->step = av_clip(c->step, 127, 24567);
258 static void xa_decode(short *out, const unsigned char *in,
259 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
262 int shift,filter,f0,f1;
268 shift = 12 - (in[4+i*2] & 15);
269 filter = in[4+i*2] >> 4;
270 f0 = xa_adpcm_table[filter][0];
271 f1 = xa_adpcm_table[filter][1];
279 t = (signed char)(d<<4)>>4;
280 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
282 s_1 = av_clip_int16(s);
287 if (inc==2) { /* stereo */
290 s_1 = right->sample1;
291 s_2 = right->sample2;
292 out = out + 1 - 28*2;
295 shift = 12 - (in[5+i*2] & 15);
296 filter = in[5+i*2] >> 4;
298 f0 = xa_adpcm_table[filter][0];
299 f1 = xa_adpcm_table[filter][1];
304 t = (signed char)d >> 4;
305 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
307 s_1 = av_clip_int16(s);
312 if (inc==2) { /* stereo */
313 right->sample1 = s_1;
314 right->sample2 = s_2;
324 * Get the number of samples that will be decoded from the packet.
325 * In one case, this is actually the maximum number of samples possible to
326 * decode with the given buf_size.
328 * @param[out] coded_samples set to the number of samples as coded in the
329 * packet, or 0 if the codec does not encode the
330 * number of samples in each frame.
332 static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
333 int buf_size, int *coded_samples)
335 ADPCMDecodeContext *s = avctx->priv_data;
337 int ch = avctx->channels;
338 int has_coded_samples = 0;
343 switch (avctx->codec->id) {
344 /* constant, only check buf_size */
345 case CODEC_ID_ADPCM_EA_XAS:
346 if (buf_size < 76 * ch)
350 case CODEC_ID_ADPCM_IMA_QT:
351 if (buf_size < 34 * ch)
355 /* simple 4-bit adpcm */
356 case CODEC_ID_ADPCM_CT:
357 case CODEC_ID_ADPCM_IMA_EA_SEAD:
358 case CODEC_ID_ADPCM_IMA_WS:
359 case CODEC_ID_ADPCM_YAMAHA:
360 nb_samples = buf_size * 2 / ch;
366 /* simple 4-bit adpcm, with header */
368 switch (avctx->codec->id) {
369 case CODEC_ID_ADPCM_4XM:
370 case CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
371 case CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
372 case CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
375 return (buf_size - header_size) * 2 / ch;
377 /* more complex formats */
378 switch (avctx->codec->id) {
379 case CODEC_ID_ADPCM_EA:
380 has_coded_samples = 1;
383 *coded_samples = AV_RL32(buf);
384 *coded_samples -= *coded_samples % 28;
385 nb_samples = (buf_size - 12) / 30 * 28;
387 case CODEC_ID_ADPCM_IMA_EA_EACS:
388 has_coded_samples = 1;
391 *coded_samples = AV_RL32(buf);
392 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
394 case CODEC_ID_ADPCM_EA_MAXIS_XA:
395 nb_samples = ((buf_size - ch) / (2 * ch)) * 2 * ch;
397 case CODEC_ID_ADPCM_EA_R1:
398 case CODEC_ID_ADPCM_EA_R2:
399 case CODEC_ID_ADPCM_EA_R3:
400 /* maximum number of samples */
401 /* has internal offsets and a per-frame switch to signal raw 16-bit */
402 has_coded_samples = 1;
405 switch (avctx->codec->id) {
406 case CODEC_ID_ADPCM_EA_R1:
407 header_size = 4 + 9 * ch;
408 *coded_samples = AV_RL32(buf);
410 case CODEC_ID_ADPCM_EA_R2:
411 header_size = 4 + 5 * ch;
412 *coded_samples = AV_RL32(buf);
414 case CODEC_ID_ADPCM_EA_R3:
415 header_size = 4 + 5 * ch;
416 *coded_samples = AV_RB32(buf);
419 *coded_samples -= *coded_samples % 28;
420 nb_samples = (buf_size - header_size) * 2 / ch;
421 nb_samples -= nb_samples % 28;
423 case CODEC_ID_ADPCM_IMA_DK3:
424 if (avctx->block_align > 0)
425 buf_size = FFMIN(buf_size, avctx->block_align);
426 nb_samples = ((buf_size - 16) * 8 / 3) / ch;
428 case CODEC_ID_ADPCM_IMA_DK4:
429 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
431 case CODEC_ID_ADPCM_IMA_WAV:
432 if (avctx->block_align > 0)
433 buf_size = FFMIN(buf_size, avctx->block_align);
434 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
436 case CODEC_ID_ADPCM_MS:
437 if (avctx->block_align > 0)
438 buf_size = FFMIN(buf_size, avctx->block_align);
439 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
441 case CODEC_ID_ADPCM_SBPRO_2:
442 case CODEC_ID_ADPCM_SBPRO_3:
443 case CODEC_ID_ADPCM_SBPRO_4:
445 int samples_per_byte;
446 switch (avctx->codec->id) {
447 case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
448 case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
449 case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
451 if (!s->status[0].step_index) {
455 nb_samples += buf_size * samples_per_byte / ch;
458 case CODEC_ID_ADPCM_SWF:
460 int buf_bits = buf_size * 8 - 2;
461 int nbits = (buf[0] >> 6) + 2;
462 int block_hdr_size = 22 * ch;
463 int block_size = block_hdr_size + nbits * ch * 4095;
464 int nblocks = buf_bits / block_size;
465 int bits_left = buf_bits - nblocks * block_size;
466 nb_samples = nblocks * 4096;
467 if (bits_left >= block_hdr_size)
468 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
471 case CODEC_ID_ADPCM_THP:
472 has_coded_samples = 1;
475 *coded_samples = AV_RB32(&buf[4]);
476 *coded_samples -= *coded_samples % 14;
477 nb_samples = (buf_size - 80) / (8 * ch) * 14;
479 case CODEC_ID_ADPCM_XA:
480 nb_samples = (buf_size / 128) * 224 / ch;
484 /* validate coded sample count */
485 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
486 return AVERROR_INVALIDDATA;
491 /* DK3 ADPCM support macro */
492 #define DK3_GET_NEXT_NIBBLE() \
493 if (decode_top_nibble_next) \
495 nibble = last_byte >> 4; \
496 decode_top_nibble_next = 0; \
502 last_byte = *src++; \
503 if (src >= buf + buf_size) \
505 nibble = last_byte & 0x0F; \
506 decode_top_nibble_next = 1; \
509 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
510 int *got_frame_ptr, AVPacket *avpkt)
512 const uint8_t *buf = avpkt->data;
513 int buf_size = avpkt->size;
514 ADPCMDecodeContext *c = avctx->priv_data;
515 ADPCMChannelStatus *cs;
516 int n, m, channel, i;
521 int nb_samples, coded_samples, ret;
523 nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
524 if (nb_samples <= 0) {
525 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
526 return AVERROR_INVALIDDATA;
529 /* get output buffer */
530 c->frame.nb_samples = nb_samples;
531 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
532 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
535 samples = (short *)c->frame.data[0];
537 /* use coded_samples when applicable */
538 /* it is always <= nb_samples, so the output buffer will be large enough */
540 if (coded_samples != nb_samples)
541 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
542 c->frame.nb_samples = nb_samples = coded_samples;
547 st = avctx->channels == 2 ? 1 : 0;
549 switch(avctx->codec->id) {
550 case CODEC_ID_ADPCM_IMA_QT:
551 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
552 Channel data is interleaved per-chunk. */
553 for (channel = 0; channel < avctx->channels; channel++) {
556 cs = &(c->status[channel]);
557 /* (pppppp) (piiiiiii) */
559 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
560 predictor = AV_RB16(src);
561 step_index = predictor & 0x7F;
566 if (cs->step_index == step_index) {
567 int diff = (int)predictor - cs->predictor;
574 cs->step_index = step_index;
575 cs->predictor = predictor;
578 if (cs->step_index > 88){
579 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
583 samples = (short *)c->frame.data[0] + channel;
585 for (m = 0; m < 32; m++) {
586 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
587 samples += avctx->channels;
588 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
589 samples += avctx->channels;
594 case CODEC_ID_ADPCM_IMA_WAV:
595 if (avctx->block_align != 0 && buf_size > avctx->block_align)
596 buf_size = avctx->block_align;
598 for(i=0; i<avctx->channels; i++){
599 cs = &(c->status[i]);
600 cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
602 cs->step_index = *src++;
603 if (cs->step_index > 88){
604 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
607 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
610 for (n = (nb_samples - 1) / 8; n > 0; n--) {
611 for (i = 0; i < avctx->channels; i++) {
613 for (m = 0; m < 4; m++) {
615 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
616 samples += avctx->channels;
617 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
618 samples += avctx->channels;
620 samples -= 8 * avctx->channels - 1;
622 samples += 7 * avctx->channels;
625 case CODEC_ID_ADPCM_4XM:
626 for (i = 0; i < avctx->channels; i++)
627 c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
629 for (i = 0; i < avctx->channels; i++) {
630 c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
631 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
634 for (i = 0; i < avctx->channels; i++) {
635 samples = (short *)c->frame.data[0] + i;
637 for (n = nb_samples >> 1; n > 0; n--, src++) {
639 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
640 samples += avctx->channels;
641 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
642 samples += avctx->channels;
646 case CODEC_ID_ADPCM_MS:
650 if (avctx->block_align != 0 && buf_size > avctx->block_align)
651 buf_size = avctx->block_align;
653 block_predictor = av_clip(*src++, 0, 6);
654 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
655 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
657 block_predictor = av_clip(*src++, 0, 6);
658 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
659 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
661 c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
663 c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
666 c->status[0].sample1 = bytestream_get_le16(&src);
667 if (st) c->status[1].sample1 = bytestream_get_le16(&src);
668 c->status[0].sample2 = bytestream_get_le16(&src);
669 if (st) c->status[1].sample2 = bytestream_get_le16(&src);
671 *samples++ = c->status[0].sample2;
672 if (st) *samples++ = c->status[1].sample2;
673 *samples++ = c->status[0].sample1;
674 if (st) *samples++ = c->status[1].sample1;
675 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--, src++) {
676 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
677 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
681 case CODEC_ID_ADPCM_IMA_DK4:
682 if (avctx->block_align != 0 && buf_size > avctx->block_align)
683 buf_size = avctx->block_align;
685 for (channel = 0; channel < avctx->channels; channel++) {
686 cs = &c->status[channel];
687 cs->predictor = (int16_t)bytestream_get_le16(&src);
688 cs->step_index = *src++;
690 *samples++ = cs->predictor;
692 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
694 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
695 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
698 case CODEC_ID_ADPCM_IMA_DK3:
700 unsigned char last_byte = 0;
701 unsigned char nibble;
702 int decode_top_nibble_next = 0;
703 int end_of_packet = 0;
706 if (avctx->block_align != 0 && buf_size > avctx->block_align)
707 buf_size = avctx->block_align;
709 c->status[0].predictor = (int16_t)AV_RL16(src + 10);
710 c->status[1].predictor = (int16_t)AV_RL16(src + 12);
711 c->status[0].step_index = src[14];
712 c->status[1].step_index = src[15];
713 /* sign extend the predictors */
715 diff_channel = c->status[1].predictor;
717 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
718 * the buffer is consumed */
721 /* for this algorithm, c->status[0] is the sum channel and
722 * c->status[1] is the diff channel */
724 /* process the first predictor of the sum channel */
725 DK3_GET_NEXT_NIBBLE();
726 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
728 /* process the diff channel predictor */
729 DK3_GET_NEXT_NIBBLE();
730 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
732 /* process the first pair of stereo PCM samples */
733 diff_channel = (diff_channel + c->status[1].predictor) / 2;
734 *samples++ = c->status[0].predictor + c->status[1].predictor;
735 *samples++ = c->status[0].predictor - c->status[1].predictor;
737 /* process the second predictor of the sum channel */
738 DK3_GET_NEXT_NIBBLE();
739 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
741 /* process the second pair of stereo PCM samples */
742 diff_channel = (diff_channel + c->status[1].predictor) / 2;
743 *samples++ = c->status[0].predictor + c->status[1].predictor;
744 *samples++ = c->status[0].predictor - c->status[1].predictor;
748 case CODEC_ID_ADPCM_IMA_ISS:
749 for (channel = 0; channel < avctx->channels; channel++) {
750 cs = &c->status[channel];
751 cs->predictor = (int16_t)bytestream_get_le16(&src);
752 cs->step_index = *src++;
756 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
759 /* nibbles are swapped for mono */
767 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
768 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
771 case CODEC_ID_ADPCM_IMA_WS:
772 while (src < buf + buf_size) {
774 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
775 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
778 case CODEC_ID_ADPCM_XA:
779 while (buf_size >= 128) {
780 xa_decode(samples, src, &c->status[0], &c->status[1],
787 case CODEC_ID_ADPCM_IMA_EA_EACS:
788 src += 4; // skip sample count (already read)
790 for (i=0; i<=st; i++)
791 c->status[i].step_index = bytestream_get_le32(&src);
792 for (i=0; i<=st; i++)
793 c->status[i].predictor = bytestream_get_le32(&src);
795 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
796 *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
797 *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
800 case CODEC_ID_ADPCM_IMA_EA_SEAD:
801 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
802 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
803 *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
806 case CODEC_ID_ADPCM_EA:
808 int32_t previous_left_sample, previous_right_sample;
809 int32_t current_left_sample, current_right_sample;
810 int32_t next_left_sample, next_right_sample;
811 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
812 uint8_t shift_left, shift_right;
814 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
815 each coding 28 stereo samples. */
817 src += 4; // skip sample count (already read)
819 current_left_sample = (int16_t)bytestream_get_le16(&src);
820 previous_left_sample = (int16_t)bytestream_get_le16(&src);
821 current_right_sample = (int16_t)bytestream_get_le16(&src);
822 previous_right_sample = (int16_t)bytestream_get_le16(&src);
824 for (count1 = 0; count1 < nb_samples / 28; count1++) {
825 coeff1l = ea_adpcm_table[ *src >> 4 ];
826 coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
827 coeff1r = ea_adpcm_table[*src & 0x0F];
828 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
831 shift_left = 20 - (*src >> 4);
832 shift_right = 20 - (*src & 0x0F);
835 for (count2 = 0; count2 < 28; count2++) {
836 next_left_sample = sign_extend(*src >> 4, 4) << shift_left;
837 next_right_sample = sign_extend(*src, 4) << shift_right;
840 next_left_sample = (next_left_sample +
841 (current_left_sample * coeff1l) +
842 (previous_left_sample * coeff2l) + 0x80) >> 8;
843 next_right_sample = (next_right_sample +
844 (current_right_sample * coeff1r) +
845 (previous_right_sample * coeff2r) + 0x80) >> 8;
847 previous_left_sample = current_left_sample;
848 current_left_sample = av_clip_int16(next_left_sample);
849 previous_right_sample = current_right_sample;
850 current_right_sample = av_clip_int16(next_right_sample);
851 *samples++ = (unsigned short)current_left_sample;
852 *samples++ = (unsigned short)current_right_sample;
856 if (src - buf == buf_size - 2)
857 src += 2; // Skip terminating 0x0000
861 case CODEC_ID_ADPCM_EA_MAXIS_XA:
863 int coeff[2][2], shift[2];
865 for(channel = 0; channel < avctx->channels; channel++) {
867 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
868 shift[channel] = 20 - (*src & 0x0F);
871 for (count1 = 0; count1 < nb_samples / 2; count1++) {
872 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
873 for(channel = 0; channel < avctx->channels; channel++) {
874 int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
876 c->status[channel].sample1 * coeff[channel][0] +
877 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
878 c->status[channel].sample2 = c->status[channel].sample1;
879 c->status[channel].sample1 = av_clip_int16(sample);
880 *samples++ = c->status[channel].sample1;
883 src+=avctx->channels;
885 /* consume whole packet */
886 src = buf + buf_size;
889 case CODEC_ID_ADPCM_EA_R1:
890 case CODEC_ID_ADPCM_EA_R2:
891 case CODEC_ID_ADPCM_EA_R3: {
894 4chan: 0=fl, 1=rl, 2=fr, 3=rr
895 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
896 const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
897 int32_t previous_sample, current_sample, next_sample;
898 int32_t coeff1, coeff2;
900 unsigned int channel;
903 const uint8_t *src_end = buf + buf_size;
906 src += 4; // skip sample count (already read)
908 for (channel=0; channel<avctx->channels; channel++) {
909 int32_t offset = (big_endian ? bytestream_get_be32(&src)
910 : bytestream_get_le32(&src))
911 + (avctx->channels-channel-1) * 4;
913 if ((offset < 0) || (offset >= src_end - src - 4)) break;
915 samplesC = samples + channel;
917 if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
918 current_sample = (int16_t)bytestream_get_le16(&srcC);
919 previous_sample = (int16_t)bytestream_get_le16(&srcC);
921 current_sample = c->status[channel].predictor;
922 previous_sample = c->status[channel].prev_sample;
925 for (count1 = 0; count1 < nb_samples / 28; count1++) {
926 if (*srcC == 0xEE) { /* only seen in R2 and R3 */
928 if (srcC > src_end - 30*2) break;
929 current_sample = (int16_t)bytestream_get_be16(&srcC);
930 previous_sample = (int16_t)bytestream_get_be16(&srcC);
932 for (count2=0; count2<28; count2++) {
933 *samplesC = (int16_t)bytestream_get_be16(&srcC);
934 samplesC += avctx->channels;
937 coeff1 = ea_adpcm_table[ *srcC>>4 ];
938 coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
939 shift = 20 - (*srcC++ & 0x0F);
941 if (srcC > src_end - 14) break;
942 for (count2=0; count2<28; count2++) {
944 next_sample = sign_extend(*srcC++, 4) << shift;
946 next_sample = sign_extend(*srcC >> 4, 4) << shift;
948 next_sample += (current_sample * coeff1) +
949 (previous_sample * coeff2);
950 next_sample = av_clip_int16(next_sample >> 8);
952 previous_sample = current_sample;
953 current_sample = next_sample;
954 *samplesC = current_sample;
955 samplesC += avctx->channels;
961 } else if (count != count1) {
962 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
963 count = FFMAX(count, count1);
966 if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
967 c->status[channel].predictor = current_sample;
968 c->status[channel].prev_sample = previous_sample;
972 c->frame.nb_samples = count * 28;
976 case CODEC_ID_ADPCM_EA_XAS:
977 for (channel=0; channel<avctx->channels; channel++) {
978 int coeff[2][4], shift[4];
979 short *s2, *s = &samples[channel];
980 for (n=0; n<4; n++, s+=32*avctx->channels) {
982 coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
983 shift[n] = 20 - (src[2] & 0x0F);
984 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
985 s2[0] = (src[0]&0xF0) + (src[1]<<8);
988 for (m=2; m<32; m+=2) {
989 s = &samples[m*avctx->channels + channel];
990 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
991 for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
992 int level = sign_extend(*src >> (4 - i), 4) << shift[n];
993 int pred = s2[-1*avctx->channels] * coeff[0][n]
994 + s2[-2*avctx->channels] * coeff[1][n];
995 s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
1001 case CODEC_ID_ADPCM_IMA_AMV:
1002 case CODEC_ID_ADPCM_IMA_SMJPEG:
1003 c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
1004 c->status[0].step_index = bytestream_get_le16(&src);
1006 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1009 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1014 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1015 FFSWAP(char, hi, lo);
1017 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1019 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1023 case CODEC_ID_ADPCM_CT:
1024 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1026 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1027 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1030 case CODEC_ID_ADPCM_SBPRO_4:
1031 case CODEC_ID_ADPCM_SBPRO_3:
1032 case CODEC_ID_ADPCM_SBPRO_2:
1033 if (!c->status[0].step_index) {
1034 /* the first byte is a raw sample */
1035 *samples++ = 128 * (*src++ - 0x80);
1037 *samples++ = 128 * (*src++ - 0x80);
1038 c->status[0].step_index = 1;
1041 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1042 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1043 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1045 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1046 src[0] & 0x0F, 4, 0);
1048 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1049 for (n = nb_samples / 3; n > 0; n--, src++) {
1050 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1051 src[0] >> 5 , 3, 0);
1052 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1053 (src[0] >> 2) & 0x07, 3, 0);
1054 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1055 src[0] & 0x03, 2, 0);
1058 for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
1059 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1060 src[0] >> 6 , 2, 2);
1061 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1062 (src[0] >> 4) & 0x03, 2, 2);
1063 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1064 (src[0] >> 2) & 0x03, 2, 2);
1065 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1066 src[0] & 0x03, 2, 2);
1070 case CODEC_ID_ADPCM_SWF:
1074 int k0, signmask, nb_bits, count;
1075 int size = buf_size*8;
1077 init_get_bits(&gb, buf, size);
1079 //read bits & initial values
1080 nb_bits = get_bits(&gb, 2)+2;
1081 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1082 table = swf_index_tables[nb_bits-2];
1083 k0 = 1 << (nb_bits-2);
1084 signmask = 1 << (nb_bits-1);
1086 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1087 for (i = 0; i < avctx->channels; i++) {
1088 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1089 c->status[i].step_index = get_bits(&gb, 6);
1092 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
1095 for (i = 0; i < avctx->channels; i++) {
1096 // similar to IMA adpcm
1097 int delta = get_bits(&gb, nb_bits);
1098 int step = ff_adpcm_step_table[c->status[i].step_index];
1099 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1110 if (delta & signmask)
1111 c->status[i].predictor -= vpdiff;
1113 c->status[i].predictor += vpdiff;
1115 c->status[i].step_index += table[delta & (~signmask)];
1117 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1118 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1120 *samples++ = c->status[i].predictor;
1127 case CODEC_ID_ADPCM_YAMAHA:
1128 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1130 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1131 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1134 case CODEC_ID_ADPCM_THP:
1140 src += 4; // skip channel size
1141 src += 4; // skip number of samples (already read)
1143 for (i = 0; i < 32; i++)
1144 table[0][i] = (int16_t)bytestream_get_be16(&src);
1146 /* Initialize the previous sample. */
1147 for (i = 0; i < 4; i++)
1148 prev[0][i] = (int16_t)bytestream_get_be16(&src);
1150 for (ch = 0; ch <= st; ch++) {
1151 samples = (short *)c->frame.data[0] + ch;
1153 /* Read in every sample for this channel. */
1154 for (i = 0; i < nb_samples / 14; i++) {
1155 int index = (*src >> 4) & 7;
1156 unsigned int exp = *src++ & 15;
1157 int factor1 = table[ch][index * 2];
1158 int factor2 = table[ch][index * 2 + 1];
1160 /* Decode 14 samples. */
1161 for (n = 0; n < 14; n++) {
1163 if(n&1) sampledat = sign_extend(*src++, 4);
1164 else sampledat = sign_extend(*src >> 4, 4);
1166 sampledat = ((prev[ch][0]*factor1
1167 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1168 *samples = av_clip_int16(sampledat);
1169 prev[ch][1] = prev[ch][0];
1170 prev[ch][0] = *samples++;
1172 /* In case of stereo, skip one sample, this sample
1173 is for the other channel. */
1186 *(AVFrame *)data = c->frame;
1192 #define ADPCM_DECODER(id_, name_, long_name_) \
1193 AVCodec ff_ ## name_ ## _decoder = { \
1195 .type = AVMEDIA_TYPE_AUDIO, \
1197 .priv_data_size = sizeof(ADPCMDecodeContext), \
1198 .init = adpcm_decode_init, \
1199 .decode = adpcm_decode_frame, \
1200 .capabilities = CODEC_CAP_DR1, \
1201 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1204 /* Note: Do not forget to add new entries to the Makefile as well. */
1205 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1206 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1207 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1208 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1209 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1210 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1211 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1212 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1213 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1214 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1215 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1216 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1217 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1218 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1219 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1220 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1221 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1222 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1223 ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1224 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1225 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1226 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1227 ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1228 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1229 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1230 ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");