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 <= 0 || avctx->channels > max_channels) {
105 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
106 return AVERROR(EINVAL);
109 switch(avctx->codec->id) {
110 case CODEC_ID_ADPCM_CT:
111 c->status[0].step = c->status[1].step = 511;
113 case CODEC_ID_ADPCM_IMA_WAV:
114 if (avctx->bits_per_coded_sample != 4) {
115 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
119 case CODEC_ID_ADPCM_IMA_WS:
120 if (avctx->extradata && avctx->extradata_size == 2 * 4) {
121 c->status[0].predictor = AV_RL32(avctx->extradata);
122 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
128 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
130 avcodec_get_frame_defaults(&c->frame);
131 avctx->coded_frame = &c->frame;
136 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
140 int sign, delta, diff, step;
142 step = ff_adpcm_step_table[c->step_index];
143 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
144 if (step_index < 0) step_index = 0;
145 else if (step_index > 88) step_index = 88;
149 /* perform direct multiplication instead of series of jumps proposed by
150 * the reference ADPCM implementation since modern CPUs can do the mults
152 diff = ((2 * delta + 1) * step) >> shift;
153 predictor = c->predictor;
154 if (sign) predictor -= diff;
155 else predictor += diff;
157 c->predictor = av_clip_int16(predictor);
158 c->step_index = step_index;
160 return (short)c->predictor;
163 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
169 step = ff_adpcm_step_table[c->step_index];
170 step_index = c->step_index + ff_adpcm_index_table[nibble];
171 step_index = av_clip(step_index, 0, 88);
174 if (nibble & 4) diff += step;
175 if (nibble & 2) diff += step >> 1;
176 if (nibble & 1) diff += step >> 2;
179 predictor = c->predictor - diff;
181 predictor = c->predictor + diff;
183 c->predictor = av_clip_int16(predictor);
184 c->step_index = step_index;
189 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
193 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
194 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
196 c->sample2 = c->sample1;
197 c->sample1 = av_clip_int16(predictor);
198 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
199 if (c->idelta < 16) c->idelta = 16;
204 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
206 int sign, delta, diff;
211 /* perform direct multiplication instead of series of jumps proposed by
212 * the reference ADPCM implementation since modern CPUs can do the mults
214 diff = ((2 * delta + 1) * c->step) >> 3;
215 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
216 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
217 c->predictor = av_clip_int16(c->predictor);
218 /* calculate new step and clamp it to range 511..32767 */
219 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
220 c->step = av_clip(new_step, 511, 32767);
222 return (short)c->predictor;
225 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
227 int sign, delta, diff;
229 sign = nibble & (1<<(size-1));
230 delta = nibble & ((1<<(size-1))-1);
231 diff = delta << (7 + c->step + shift);
234 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
236 /* calculate new step */
237 if (delta >= (2*size - 3) && c->step < 3)
239 else if (delta == 0 && c->step > 0)
242 return (short) c->predictor;
245 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
252 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
253 c->predictor = av_clip_int16(c->predictor);
254 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
255 c->step = av_clip(c->step, 127, 24567);
259 static void xa_decode(short *out, const unsigned char *in,
260 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
263 int shift,filter,f0,f1;
269 shift = 12 - (in[4+i*2] & 15);
270 filter = in[4+i*2] >> 4;
271 f0 = xa_adpcm_table[filter][0];
272 f1 = xa_adpcm_table[filter][1];
280 t = (signed char)(d<<4)>>4;
281 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
283 s_1 = av_clip_int16(s);
288 if (inc==2) { /* stereo */
291 s_1 = right->sample1;
292 s_2 = right->sample2;
293 out = out + 1 - 28*2;
296 shift = 12 - (in[5+i*2] & 15);
297 filter = in[5+i*2] >> 4;
299 f0 = xa_adpcm_table[filter][0];
300 f1 = xa_adpcm_table[filter][1];
305 t = (signed char)d >> 4;
306 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
308 s_1 = av_clip_int16(s);
313 if (inc==2) { /* stereo */
314 right->sample1 = s_1;
315 right->sample2 = s_2;
325 * Get the number of samples that will be decoded from the packet.
326 * In one case, this is actually the maximum number of samples possible to
327 * decode with the given buf_size.
329 * @param[out] coded_samples set to the number of samples as coded in the
330 * packet, or 0 if the codec does not encode the
331 * number of samples in each frame.
333 static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
334 int buf_size, int *coded_samples)
336 ADPCMDecodeContext *s = avctx->priv_data;
338 int ch = avctx->channels;
339 int has_coded_samples = 0;
344 switch (avctx->codec->id) {
345 /* constant, only check buf_size */
346 case CODEC_ID_ADPCM_EA_XAS:
347 if (buf_size < 76 * ch)
351 case CODEC_ID_ADPCM_IMA_QT:
352 if (buf_size < 34 * ch)
356 /* simple 4-bit adpcm */
357 case CODEC_ID_ADPCM_CT:
358 case CODEC_ID_ADPCM_IMA_EA_SEAD:
359 case CODEC_ID_ADPCM_IMA_WS:
360 case CODEC_ID_ADPCM_YAMAHA:
361 nb_samples = buf_size * 2 / ch;
367 /* simple 4-bit adpcm, with header */
369 switch (avctx->codec->id) {
370 case CODEC_ID_ADPCM_4XM:
371 case CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
372 case CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
373 case CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
376 return (buf_size - header_size) * 2 / ch;
378 /* more complex formats */
379 switch (avctx->codec->id) {
380 case CODEC_ID_ADPCM_EA:
381 has_coded_samples = 1;
384 *coded_samples = AV_RL32(buf);
385 *coded_samples -= *coded_samples % 28;
386 nb_samples = (buf_size - 12) / 30 * 28;
388 case CODEC_ID_ADPCM_IMA_EA_EACS:
389 has_coded_samples = 1;
392 *coded_samples = AV_RL32(buf);
393 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
395 case CODEC_ID_ADPCM_EA_MAXIS_XA:
396 nb_samples = ((buf_size - ch) / (2 * ch)) * 2 * ch;
398 case CODEC_ID_ADPCM_EA_R1:
399 case CODEC_ID_ADPCM_EA_R2:
400 case CODEC_ID_ADPCM_EA_R3:
401 /* maximum number of samples */
402 /* has internal offsets and a per-frame switch to signal raw 16-bit */
403 has_coded_samples = 1;
406 switch (avctx->codec->id) {
407 case CODEC_ID_ADPCM_EA_R1:
408 header_size = 4 + 9 * ch;
409 *coded_samples = AV_RL32(buf);
411 case CODEC_ID_ADPCM_EA_R2:
412 header_size = 4 + 5 * ch;
413 *coded_samples = AV_RL32(buf);
415 case CODEC_ID_ADPCM_EA_R3:
416 header_size = 4 + 5 * ch;
417 *coded_samples = AV_RB32(buf);
420 *coded_samples -= *coded_samples % 28;
421 nb_samples = (buf_size - header_size) * 2 / ch;
422 nb_samples -= nb_samples % 28;
424 case CODEC_ID_ADPCM_IMA_DK3:
425 if (avctx->block_align > 0)
426 buf_size = FFMIN(buf_size, avctx->block_align);
427 nb_samples = ((buf_size - 16) * 8 / 3) / ch;
429 case CODEC_ID_ADPCM_IMA_DK4:
430 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
432 case CODEC_ID_ADPCM_IMA_WAV:
433 if (avctx->block_align > 0)
434 buf_size = FFMIN(buf_size, avctx->block_align);
435 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
437 case CODEC_ID_ADPCM_MS:
438 if (avctx->block_align > 0)
439 buf_size = FFMIN(buf_size, avctx->block_align);
440 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
442 case CODEC_ID_ADPCM_SBPRO_2:
443 case CODEC_ID_ADPCM_SBPRO_3:
444 case CODEC_ID_ADPCM_SBPRO_4:
446 int samples_per_byte;
447 switch (avctx->codec->id) {
448 case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
449 case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
450 case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
452 if (!s->status[0].step_index) {
456 nb_samples += buf_size * samples_per_byte / ch;
459 case CODEC_ID_ADPCM_SWF:
461 int buf_bits = buf_size * 8 - 2;
462 int nbits = (buf[0] >> 6) + 2;
463 int block_hdr_size = 22 * ch;
464 int block_size = block_hdr_size + nbits * ch * 4095;
465 int nblocks = buf_bits / block_size;
466 int bits_left = buf_bits - nblocks * block_size;
467 nb_samples = nblocks * 4096;
468 if (bits_left >= block_hdr_size)
469 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
472 case CODEC_ID_ADPCM_THP:
473 has_coded_samples = 1;
476 *coded_samples = AV_RB32(&buf[4]);
477 *coded_samples -= *coded_samples % 14;
478 nb_samples = (buf_size - 80) / (8 * ch) * 14;
480 case CODEC_ID_ADPCM_XA:
481 nb_samples = (buf_size / 128) * 224 / ch;
485 /* validate coded sample count */
486 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
487 return AVERROR_INVALIDDATA;
492 /* DK3 ADPCM support macro */
493 #define DK3_GET_NEXT_NIBBLE() \
494 if (decode_top_nibble_next) \
496 nibble = last_byte >> 4; \
497 decode_top_nibble_next = 0; \
503 last_byte = *src++; \
504 if (src >= buf + buf_size) \
506 nibble = last_byte & 0x0F; \
507 decode_top_nibble_next = 1; \
510 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
511 int *got_frame_ptr, AVPacket *avpkt)
513 const uint8_t *buf = avpkt->data;
514 int buf_size = avpkt->size;
515 ADPCMDecodeContext *c = avctx->priv_data;
516 ADPCMChannelStatus *cs;
517 int n, m, channel, i;
522 int nb_samples, coded_samples, ret;
524 nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
525 if (nb_samples <= 0) {
526 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
527 return AVERROR_INVALIDDATA;
530 /* get output buffer */
531 c->frame.nb_samples = nb_samples;
532 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
533 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
536 samples = (short *)c->frame.data[0];
538 /* use coded_samples when applicable */
539 /* it is always <= nb_samples, so the output buffer will be large enough */
541 if (coded_samples != nb_samples)
542 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
543 c->frame.nb_samples = nb_samples = coded_samples;
548 st = avctx->channels == 2 ? 1 : 0;
550 switch(avctx->codec->id) {
551 case CODEC_ID_ADPCM_IMA_QT:
552 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
553 Channel data is interleaved per-chunk. */
554 for (channel = 0; channel < avctx->channels; channel++) {
557 cs = &(c->status[channel]);
558 /* (pppppp) (piiiiiii) */
560 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
561 predictor = AV_RB16(src);
562 step_index = predictor & 0x7F;
567 if (cs->step_index == step_index) {
568 int diff = (int)predictor - cs->predictor;
575 cs->step_index = step_index;
576 cs->predictor = predictor;
579 if (cs->step_index > 88){
580 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
584 samples = (short *)c->frame.data[0] + channel;
586 for (m = 0; m < 32; m++) {
587 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
588 samples += avctx->channels;
589 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
590 samples += avctx->channels;
595 case CODEC_ID_ADPCM_IMA_WAV:
596 if (avctx->block_align != 0 && buf_size > avctx->block_align)
597 buf_size = avctx->block_align;
599 for(i=0; i<avctx->channels; i++){
600 cs = &(c->status[i]);
601 cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
603 cs->step_index = *src++;
604 if (cs->step_index > 88){
605 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
608 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
611 for (n = (nb_samples - 1) / 8; n > 0; n--) {
612 for (i = 0; i < avctx->channels; i++) {
614 for (m = 0; m < 4; m++) {
616 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
617 samples += avctx->channels;
618 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
619 samples += avctx->channels;
621 samples -= 8 * avctx->channels - 1;
623 samples += 7 * avctx->channels;
626 case CODEC_ID_ADPCM_4XM:
627 for (i = 0; i < avctx->channels; i++)
628 c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
630 for (i = 0; i < avctx->channels; i++) {
631 c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
632 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
635 for (i = 0; i < avctx->channels; i++) {
636 samples = (short *)c->frame.data[0] + i;
638 for (n = nb_samples >> 1; n > 0; n--, src++) {
640 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
641 samples += avctx->channels;
642 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
643 samples += avctx->channels;
647 case CODEC_ID_ADPCM_MS:
651 if (avctx->block_align != 0 && buf_size > avctx->block_align)
652 buf_size = avctx->block_align;
654 block_predictor = av_clip(*src++, 0, 6);
655 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
656 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
658 block_predictor = av_clip(*src++, 0, 6);
659 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
660 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
662 c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
664 c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
667 c->status[0].sample1 = bytestream_get_le16(&src);
668 if (st) c->status[1].sample1 = bytestream_get_le16(&src);
669 c->status[0].sample2 = bytestream_get_le16(&src);
670 if (st) c->status[1].sample2 = bytestream_get_le16(&src);
672 *samples++ = c->status[0].sample2;
673 if (st) *samples++ = c->status[1].sample2;
674 *samples++ = c->status[0].sample1;
675 if (st) *samples++ = c->status[1].sample1;
676 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--, src++) {
677 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
678 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
682 case CODEC_ID_ADPCM_IMA_DK4:
683 if (avctx->block_align != 0 && buf_size > avctx->block_align)
684 buf_size = avctx->block_align;
686 for (channel = 0; channel < avctx->channels; channel++) {
687 cs = &c->status[channel];
688 cs->predictor = (int16_t)bytestream_get_le16(&src);
689 cs->step_index = *src++;
691 *samples++ = cs->predictor;
693 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
695 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
696 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
699 case CODEC_ID_ADPCM_IMA_DK3:
701 unsigned char last_byte = 0;
702 unsigned char nibble;
703 int decode_top_nibble_next = 0;
704 int end_of_packet = 0;
707 if (avctx->block_align != 0 && buf_size > avctx->block_align)
708 buf_size = avctx->block_align;
710 c->status[0].predictor = (int16_t)AV_RL16(src + 10);
711 c->status[1].predictor = (int16_t)AV_RL16(src + 12);
712 c->status[0].step_index = src[14];
713 c->status[1].step_index = src[15];
714 /* sign extend the predictors */
716 diff_channel = c->status[1].predictor;
718 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
719 * the buffer is consumed */
722 /* for this algorithm, c->status[0] is the sum channel and
723 * c->status[1] is the diff channel */
725 /* process the first predictor of the sum channel */
726 DK3_GET_NEXT_NIBBLE();
727 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
729 /* process the diff channel predictor */
730 DK3_GET_NEXT_NIBBLE();
731 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
733 /* process the first pair of stereo PCM samples */
734 diff_channel = (diff_channel + c->status[1].predictor) / 2;
735 *samples++ = c->status[0].predictor + c->status[1].predictor;
736 *samples++ = c->status[0].predictor - c->status[1].predictor;
738 /* process the second predictor of the sum channel */
739 DK3_GET_NEXT_NIBBLE();
740 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
742 /* process the second pair of stereo PCM samples */
743 diff_channel = (diff_channel + c->status[1].predictor) / 2;
744 *samples++ = c->status[0].predictor + c->status[1].predictor;
745 *samples++ = c->status[0].predictor - c->status[1].predictor;
749 case CODEC_ID_ADPCM_IMA_ISS:
750 for (channel = 0; channel < avctx->channels; channel++) {
751 cs = &c->status[channel];
752 cs->predictor = (int16_t)bytestream_get_le16(&src);
753 cs->step_index = *src++;
757 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
760 /* nibbles are swapped for mono */
768 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
769 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
772 case CODEC_ID_ADPCM_IMA_WS:
773 while (src < buf + buf_size) {
775 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
776 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
779 case CODEC_ID_ADPCM_XA:
780 while (buf_size >= 128) {
781 xa_decode(samples, src, &c->status[0], &c->status[1],
788 case CODEC_ID_ADPCM_IMA_EA_EACS:
789 src += 4; // skip sample count (already read)
791 for (i=0; i<=st; i++)
792 c->status[i].step_index = bytestream_get_le32(&src);
793 for (i=0; i<=st; i++)
794 c->status[i].predictor = bytestream_get_le32(&src);
796 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
797 *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
798 *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
801 case CODEC_ID_ADPCM_IMA_EA_SEAD:
802 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
803 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
804 *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
807 case CODEC_ID_ADPCM_EA:
809 int32_t previous_left_sample, previous_right_sample;
810 int32_t current_left_sample, current_right_sample;
811 int32_t next_left_sample, next_right_sample;
812 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
813 uint8_t shift_left, shift_right;
815 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
816 each coding 28 stereo samples. */
818 src += 4; // skip sample count (already read)
820 current_left_sample = (int16_t)bytestream_get_le16(&src);
821 previous_left_sample = (int16_t)bytestream_get_le16(&src);
822 current_right_sample = (int16_t)bytestream_get_le16(&src);
823 previous_right_sample = (int16_t)bytestream_get_le16(&src);
825 for (count1 = 0; count1 < nb_samples / 28; count1++) {
826 coeff1l = ea_adpcm_table[ *src >> 4 ];
827 coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
828 coeff1r = ea_adpcm_table[*src & 0x0F];
829 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
832 shift_left = 20 - (*src >> 4);
833 shift_right = 20 - (*src & 0x0F);
836 for (count2 = 0; count2 < 28; count2++) {
837 next_left_sample = sign_extend(*src >> 4, 4) << shift_left;
838 next_right_sample = sign_extend(*src, 4) << shift_right;
841 next_left_sample = (next_left_sample +
842 (current_left_sample * coeff1l) +
843 (previous_left_sample * coeff2l) + 0x80) >> 8;
844 next_right_sample = (next_right_sample +
845 (current_right_sample * coeff1r) +
846 (previous_right_sample * coeff2r) + 0x80) >> 8;
848 previous_left_sample = current_left_sample;
849 current_left_sample = av_clip_int16(next_left_sample);
850 previous_right_sample = current_right_sample;
851 current_right_sample = av_clip_int16(next_right_sample);
852 *samples++ = (unsigned short)current_left_sample;
853 *samples++ = (unsigned short)current_right_sample;
857 if (src - buf == buf_size - 2)
858 src += 2; // Skip terminating 0x0000
862 case CODEC_ID_ADPCM_EA_MAXIS_XA:
864 int coeff[2][2], shift[2];
866 for(channel = 0; channel < avctx->channels; channel++) {
868 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
869 shift[channel] = 20 - (*src & 0x0F);
872 for (count1 = 0; count1 < nb_samples / 2; count1++) {
873 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
874 for(channel = 0; channel < avctx->channels; channel++) {
875 int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
877 c->status[channel].sample1 * coeff[channel][0] +
878 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
879 c->status[channel].sample2 = c->status[channel].sample1;
880 c->status[channel].sample1 = av_clip_int16(sample);
881 *samples++ = c->status[channel].sample1;
884 src+=avctx->channels;
886 /* consume whole packet */
887 src = buf + buf_size;
890 case CODEC_ID_ADPCM_EA_R1:
891 case CODEC_ID_ADPCM_EA_R2:
892 case CODEC_ID_ADPCM_EA_R3: {
895 4chan: 0=fl, 1=rl, 2=fr, 3=rr
896 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
897 const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
898 int32_t previous_sample, current_sample, next_sample;
899 int32_t coeff1, coeff2;
901 unsigned int channel;
904 const uint8_t *src_end = buf + buf_size;
907 src += 4; // skip sample count (already read)
909 for (channel=0; channel<avctx->channels; channel++) {
910 int32_t offset = (big_endian ? bytestream_get_be32(&src)
911 : bytestream_get_le32(&src))
912 + (avctx->channels-channel-1) * 4;
914 if ((offset < 0) || (offset >= src_end - src - 4)) break;
916 samplesC = samples + channel;
918 if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
919 current_sample = (int16_t)bytestream_get_le16(&srcC);
920 previous_sample = (int16_t)bytestream_get_le16(&srcC);
922 current_sample = c->status[channel].predictor;
923 previous_sample = c->status[channel].prev_sample;
926 for (count1 = 0; count1 < nb_samples / 28; count1++) {
927 if (*srcC == 0xEE) { /* only seen in R2 and R3 */
929 if (srcC > src_end - 30*2) break;
930 current_sample = (int16_t)bytestream_get_be16(&srcC);
931 previous_sample = (int16_t)bytestream_get_be16(&srcC);
933 for (count2=0; count2<28; count2++) {
934 *samplesC = (int16_t)bytestream_get_be16(&srcC);
935 samplesC += avctx->channels;
938 coeff1 = ea_adpcm_table[ *srcC>>4 ];
939 coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
940 shift = 20 - (*srcC++ & 0x0F);
942 if (srcC > src_end - 14) break;
943 for (count2=0; count2<28; count2++) {
945 next_sample = sign_extend(*srcC++, 4) << shift;
947 next_sample = sign_extend(*srcC >> 4, 4) << shift;
949 next_sample += (current_sample * coeff1) +
950 (previous_sample * coeff2);
951 next_sample = av_clip_int16(next_sample >> 8);
953 previous_sample = current_sample;
954 current_sample = next_sample;
955 *samplesC = current_sample;
956 samplesC += avctx->channels;
962 } else if (count != count1) {
963 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
964 count = FFMAX(count, count1);
967 if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
968 c->status[channel].predictor = current_sample;
969 c->status[channel].prev_sample = previous_sample;
973 c->frame.nb_samples = count * 28;
977 case CODEC_ID_ADPCM_EA_XAS:
978 for (channel=0; channel<avctx->channels; channel++) {
979 int coeff[2][4], shift[4];
980 short *s2, *s = &samples[channel];
981 for (n=0; n<4; n++, s+=32*avctx->channels) {
983 coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
984 shift[n] = 20 - (src[2] & 0x0F);
985 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
986 s2[0] = (src[0]&0xF0) + (src[1]<<8);
989 for (m=2; m<32; m+=2) {
990 s = &samples[m*avctx->channels + channel];
991 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
992 for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
993 int level = sign_extend(*src >> (4 - i), 4) << shift[n];
994 int pred = s2[-1*avctx->channels] * coeff[0][n]
995 + s2[-2*avctx->channels] * coeff[1][n];
996 s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
1002 case CODEC_ID_ADPCM_IMA_AMV:
1003 case CODEC_ID_ADPCM_IMA_SMJPEG:
1004 c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
1005 c->status[0].step_index = bytestream_get_le16(&src);
1007 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1010 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1015 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1016 FFSWAP(char, hi, lo);
1018 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1020 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1024 case CODEC_ID_ADPCM_CT:
1025 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1027 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1028 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1031 case CODEC_ID_ADPCM_SBPRO_4:
1032 case CODEC_ID_ADPCM_SBPRO_3:
1033 case CODEC_ID_ADPCM_SBPRO_2:
1034 if (!c->status[0].step_index) {
1035 /* the first byte is a raw sample */
1036 *samples++ = 128 * (*src++ - 0x80);
1038 *samples++ = 128 * (*src++ - 0x80);
1039 c->status[0].step_index = 1;
1042 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1043 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1044 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1046 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1047 src[0] & 0x0F, 4, 0);
1049 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1050 for (n = nb_samples / 3; n > 0; n--, src++) {
1051 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1052 src[0] >> 5 , 3, 0);
1053 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1054 (src[0] >> 2) & 0x07, 3, 0);
1055 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1056 src[0] & 0x03, 2, 0);
1059 for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
1060 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1061 src[0] >> 6 , 2, 2);
1062 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1063 (src[0] >> 4) & 0x03, 2, 2);
1064 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1065 (src[0] >> 2) & 0x03, 2, 2);
1066 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1067 src[0] & 0x03, 2, 2);
1071 case CODEC_ID_ADPCM_SWF:
1075 int k0, signmask, nb_bits, count;
1076 int size = buf_size*8;
1078 init_get_bits(&gb, buf, size);
1080 //read bits & initial values
1081 nb_bits = get_bits(&gb, 2)+2;
1082 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1083 table = swf_index_tables[nb_bits-2];
1084 k0 = 1 << (nb_bits-2);
1085 signmask = 1 << (nb_bits-1);
1087 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1088 for (i = 0; i < avctx->channels; i++) {
1089 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1090 c->status[i].step_index = get_bits(&gb, 6);
1093 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
1096 for (i = 0; i < avctx->channels; i++) {
1097 // similar to IMA adpcm
1098 int delta = get_bits(&gb, nb_bits);
1099 int step = ff_adpcm_step_table[c->status[i].step_index];
1100 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1111 if (delta & signmask)
1112 c->status[i].predictor -= vpdiff;
1114 c->status[i].predictor += vpdiff;
1116 c->status[i].step_index += table[delta & (~signmask)];
1118 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1119 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1121 *samples++ = c->status[i].predictor;
1128 case CODEC_ID_ADPCM_YAMAHA:
1129 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1131 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1132 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1135 case CODEC_ID_ADPCM_THP:
1141 src += 4; // skip channel size
1142 src += 4; // skip number of samples (already read)
1144 for (i = 0; i < 32; i++)
1145 table[0][i] = (int16_t)bytestream_get_be16(&src);
1147 /* Initialize the previous sample. */
1148 for (i = 0; i < 4; i++)
1149 prev[0][i] = (int16_t)bytestream_get_be16(&src);
1151 for (ch = 0; ch <= st; ch++) {
1152 samples = (short *)c->frame.data[0] + ch;
1154 /* Read in every sample for this channel. */
1155 for (i = 0; i < nb_samples / 14; i++) {
1156 int index = (*src >> 4) & 7;
1157 unsigned int exp = *src++ & 15;
1158 int factor1 = table[ch][index * 2];
1159 int factor2 = table[ch][index * 2 + 1];
1161 /* Decode 14 samples. */
1162 for (n = 0; n < 14; n++) {
1164 if(n&1) sampledat = sign_extend(*src++, 4);
1165 else sampledat = sign_extend(*src >> 4, 4);
1167 sampledat = ((prev[ch][0]*factor1
1168 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1169 *samples = av_clip_int16(sampledat);
1170 prev[ch][1] = prev[ch][0];
1171 prev[ch][0] = *samples++;
1173 /* In case of stereo, skip one sample, this sample
1174 is for the other channel. */
1187 *(AVFrame *)data = c->frame;
1193 #define ADPCM_DECODER(id_, name_, long_name_) \
1194 AVCodec ff_ ## name_ ## _decoder = { \
1196 .type = AVMEDIA_TYPE_AUDIO, \
1198 .priv_data_size = sizeof(ADPCMDecodeContext), \
1199 .init = adpcm_decode_init, \
1200 .decode = adpcm_decode_frame, \
1201 .capabilities = CODEC_CAP_DR1, \
1202 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1205 /* Note: Do not forget to add new entries to the Makefile as well. */
1206 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1207 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1208 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1209 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1210 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1211 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1212 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1213 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1214 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1215 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1216 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1217 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1218 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1219 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1220 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1221 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1222 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1223 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1224 ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1225 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1226 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1227 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1228 ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1229 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1230 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1231 ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");