2 * Copyright (c) 2001-2003 The FFmpeg project
4 * first version by Francois Revol (revol@free.fr)
5 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6 * by Mike Melanson (melanson@pcisys.net)
7 * CD-ROM XA ADPCM codec by BERO
8 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
15 * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
17 * This file is part of FFmpeg.
19 * FFmpeg is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.1 of the License, or (at your option) any later version.
24 * FFmpeg is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with FFmpeg; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "bytestream.h"
37 #include "adpcm_data.h"
43 * Features and limitations:
45 * Reference documents:
46 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
47 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
48 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
49 * http://openquicktime.sourceforge.net/
50 * XAnim sources (xa_codec.c) http://xanim.polter.net/
51 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
52 * SoX source code http://sox.sourceforge.net/
55 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
56 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
57 * readstr http://www.geocities.co.jp/Playtown/2004/
60 /* These are for CD-ROM XA ADPCM */
61 static const int8_t xa_adpcm_table[5][2] = {
69 static const int16_t ea_adpcm_table[] = {
77 // padded to zero where table size is less then 16
78 static const int8_t swf_index_tables[4][16] = {
80 /*3*/ { -1, -1, 2, 4 },
81 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
82 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
87 typedef struct ADPCMDecodeContext {
88 ADPCMChannelStatus status[14];
89 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
93 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
95 ADPCMDecodeContext *c = avctx->priv_data;
96 unsigned int min_channels = 1;
97 unsigned int max_channels = 2;
99 switch(avctx->codec->id) {
100 case AV_CODEC_ID_ADPCM_DTK:
101 case AV_CODEC_ID_ADPCM_EA:
104 case AV_CODEC_ID_ADPCM_AFC:
105 case AV_CODEC_ID_ADPCM_EA_R1:
106 case AV_CODEC_ID_ADPCM_EA_R2:
107 case AV_CODEC_ID_ADPCM_EA_R3:
108 case AV_CODEC_ID_ADPCM_EA_XAS:
109 case AV_CODEC_ID_ADPCM_MS:
112 case AV_CODEC_ID_ADPCM_MTAF:
115 if (avctx->channels & 1) {
116 avpriv_request_sample(avctx, "channel count %d\n", avctx->channels);
117 return AVERROR_PATCHWELCOME;
120 case AV_CODEC_ID_ADPCM_PSX:
123 case AV_CODEC_ID_ADPCM_IMA_DAT4:
124 case AV_CODEC_ID_ADPCM_THP:
125 case AV_CODEC_ID_ADPCM_THP_LE:
129 if (avctx->channels < min_channels || avctx->channels > max_channels) {
130 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
131 return AVERROR(EINVAL);
134 switch(avctx->codec->id) {
135 case AV_CODEC_ID_ADPCM_CT:
136 c->status[0].step = c->status[1].step = 511;
138 case AV_CODEC_ID_ADPCM_IMA_WAV:
139 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
140 return AVERROR_INVALIDDATA;
142 case AV_CODEC_ID_ADPCM_IMA_APC:
143 if (avctx->extradata && avctx->extradata_size >= 8) {
144 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
145 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
148 case AV_CODEC_ID_ADPCM_IMA_WS:
149 if (avctx->extradata && avctx->extradata_size >= 2)
150 c->vqa_version = AV_RL16(avctx->extradata);
152 case AV_CODEC_ID_ADPCM_ARGO:
153 if (avctx->bits_per_coded_sample != 4)
154 return AVERROR_INVALIDDATA;
160 switch(avctx->codec->id) {
161 case AV_CODEC_ID_ADPCM_AICA:
162 case AV_CODEC_ID_ADPCM_IMA_DAT4:
163 case AV_CODEC_ID_ADPCM_IMA_QT:
164 case AV_CODEC_ID_ADPCM_IMA_WAV:
165 case AV_CODEC_ID_ADPCM_4XM:
166 case AV_CODEC_ID_ADPCM_XA:
167 case AV_CODEC_ID_ADPCM_EA_R1:
168 case AV_CODEC_ID_ADPCM_EA_R2:
169 case AV_CODEC_ID_ADPCM_EA_R3:
170 case AV_CODEC_ID_ADPCM_EA_XAS:
171 case AV_CODEC_ID_ADPCM_THP:
172 case AV_CODEC_ID_ADPCM_THP_LE:
173 case AV_CODEC_ID_ADPCM_AFC:
174 case AV_CODEC_ID_ADPCM_DTK:
175 case AV_CODEC_ID_ADPCM_PSX:
176 case AV_CODEC_ID_ADPCM_MTAF:
177 case AV_CODEC_ID_ADPCM_ARGO:
178 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
180 case AV_CODEC_ID_ADPCM_IMA_WS:
181 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
184 case AV_CODEC_ID_ADPCM_MS:
185 avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
189 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
195 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
197 int delta, pred, step, add;
202 add = (delta * 2 + 1) * step;
206 if ((nibble & 8) == 0)
207 pred = av_clip(pred + (add >> 3), -32767, 32767);
209 pred = av_clip(pred - (add >> 3), -32767, 32767);
216 c->step = av_clip(c->step * 2, 127, 24576);
234 c->step = av_clip(c->step, 127, 24576);
239 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
243 int sign, delta, diff, step;
245 step = ff_adpcm_step_table[c->step_index];
246 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
247 step_index = av_clip(step_index, 0, 88);
251 /* perform direct multiplication instead of series of jumps proposed by
252 * the reference ADPCM implementation since modern CPUs can do the mults
254 diff = ((2 * delta + 1) * step) >> shift;
255 predictor = c->predictor;
256 if (sign) predictor -= diff;
257 else predictor += diff;
259 c->predictor = av_clip_int16(predictor);
260 c->step_index = step_index;
262 return (int16_t)c->predictor;
265 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
267 int nibble, step_index, predictor, sign, delta, diff, step, shift;
270 nibble = get_bits_le(gb, bps),
271 step = ff_adpcm_step_table[c->step_index];
272 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
273 step_index = av_clip(step_index, 0, 88);
275 sign = nibble & (1 << shift);
276 delta = av_mod_uintp2(nibble, shift);
277 diff = ((2 * delta + 1) * step) >> shift;
278 predictor = c->predictor;
279 if (sign) predictor -= diff;
280 else predictor += diff;
282 c->predictor = av_clip_int16(predictor);
283 c->step_index = step_index;
285 return (int16_t)c->predictor;
288 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
294 step = ff_adpcm_step_table[c->step_index];
295 step_index = c->step_index + ff_adpcm_index_table[nibble];
296 step_index = av_clip(step_index, 0, 88);
299 if (nibble & 4) diff += step;
300 if (nibble & 2) diff += step >> 1;
301 if (nibble & 1) diff += step >> 2;
304 predictor = c->predictor - diff;
306 predictor = c->predictor + diff;
308 c->predictor = av_clip_int16(predictor);
309 c->step_index = step_index;
314 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
318 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
319 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
321 c->sample2 = c->sample1;
322 c->sample1 = av_clip_int16(predictor);
323 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
324 if (c->idelta < 16) c->idelta = 16;
325 if (c->idelta > INT_MAX/768) {
326 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
327 c->idelta = INT_MAX/768;
333 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
335 int step_index, predictor, sign, delta, diff, step;
337 step = ff_adpcm_oki_step_table[c->step_index];
338 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
339 step_index = av_clip(step_index, 0, 48);
343 diff = ((2 * delta + 1) * step) >> 3;
344 predictor = c->predictor;
345 if (sign) predictor -= diff;
346 else predictor += diff;
348 c->predictor = av_clip_intp2(predictor, 11);
349 c->step_index = step_index;
351 return c->predictor * 16;
354 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
356 int sign, delta, diff;
361 /* perform direct multiplication instead of series of jumps proposed by
362 * the reference ADPCM implementation since modern CPUs can do the mults
364 diff = ((2 * delta + 1) * c->step) >> 3;
365 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
366 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
367 c->predictor = av_clip_int16(c->predictor);
368 /* calculate new step and clamp it to range 511..32767 */
369 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
370 c->step = av_clip(new_step, 511, 32767);
372 return (int16_t)c->predictor;
375 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
377 int sign, delta, diff;
379 sign = nibble & (1<<(size-1));
380 delta = nibble & ((1<<(size-1))-1);
381 diff = delta << (7 + c->step + shift);
384 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
386 /* calculate new step */
387 if (delta >= (2*size - 3) && c->step < 3)
389 else if (delta == 0 && c->step > 0)
392 return (int16_t) c->predictor;
395 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
402 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
403 c->predictor = av_clip_int16(c->predictor);
404 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
405 c->step = av_clip(c->step, 127, 24576);
409 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
411 c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
412 c->predictor = av_clip_int16(c->predictor);
413 c->step += ff_adpcm_index_table[nibble];
414 c->step = av_clip_uintp2(c->step, 5);
418 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
419 const uint8_t *in, ADPCMChannelStatus *left,
420 ADPCMChannelStatus *right, int channels, int sample_offset)
423 int shift,filter,f0,f1;
427 out0 += sample_offset;
431 out1 += sample_offset;
434 shift = 12 - (in[4+i*2] & 15);
435 filter = in[4+i*2] >> 4;
436 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
437 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
440 f0 = xa_adpcm_table[filter][0];
441 f1 = xa_adpcm_table[filter][1];
449 t = sign_extend(d, 4);
450 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
452 s_1 = av_clip_int16(s);
459 s_1 = right->sample1;
460 s_2 = right->sample2;
463 shift = 12 - (in[5+i*2] & 15);
464 filter = in[5+i*2] >> 4;
465 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
466 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
470 f0 = xa_adpcm_table[filter][0];
471 f1 = xa_adpcm_table[filter][1];
476 t = sign_extend(d >> 4, 4);
477 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
479 s_1 = av_clip_int16(s);
484 right->sample1 = s_1;
485 right->sample2 = s_2;
491 out0 += 28 * (3 - channels);
492 out1 += 28 * (3 - channels);
498 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
500 ADPCMDecodeContext *c = avctx->priv_data;
503 int k0, signmask, nb_bits, count;
504 int size = buf_size*8;
507 init_get_bits(&gb, buf, size);
509 //read bits & initial values
510 nb_bits = get_bits(&gb, 2)+2;
511 table = swf_index_tables[nb_bits-2];
512 k0 = 1 << (nb_bits-2);
513 signmask = 1 << (nb_bits-1);
515 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
516 for (i = 0; i < avctx->channels; i++) {
517 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
518 c->status[i].step_index = get_bits(&gb, 6);
521 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
524 for (i = 0; i < avctx->channels; i++) {
525 // similar to IMA adpcm
526 int delta = get_bits(&gb, nb_bits);
527 int step = ff_adpcm_step_table[c->status[i].step_index];
528 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
539 if (delta & signmask)
540 c->status[i].predictor -= vpdiff;
542 c->status[i].predictor += vpdiff;
544 c->status[i].step_index += table[delta & (~signmask)];
546 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
547 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
549 *samples++ = c->status[i].predictor;
555 static inline int16_t adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int control, int shift)
557 int sample = nibble * (1 << shift);
560 sample += (8 * cs->sample1) - (4 * cs->sample2);
562 sample += 4 * cs->sample1;
564 sample = av_clip_int16(sample >> 2);
566 cs->sample2 = cs->sample1;
567 cs->sample1 = sample;
573 * Get the number of samples that will be decoded from the packet.
574 * In one case, this is actually the maximum number of samples possible to
575 * decode with the given buf_size.
577 * @param[out] coded_samples set to the number of samples as coded in the
578 * packet, or 0 if the codec does not encode the
579 * number of samples in each frame.
580 * @param[out] approx_nb_samples set to non-zero if the number of samples
581 * returned is an approximation.
583 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
584 int buf_size, int *coded_samples, int *approx_nb_samples)
586 ADPCMDecodeContext *s = avctx->priv_data;
588 int ch = avctx->channels;
589 int has_coded_samples = 0;
593 *approx_nb_samples = 0;
598 switch (avctx->codec->id) {
599 /* constant, only check buf_size */
600 case AV_CODEC_ID_ADPCM_EA_XAS:
601 if (buf_size < 76 * ch)
605 case AV_CODEC_ID_ADPCM_IMA_QT:
606 if (buf_size < 34 * ch)
610 case AV_CODEC_ID_ADPCM_ARGO:
611 if (buf_size < 17 * ch)
615 /* simple 4-bit adpcm */
616 case AV_CODEC_ID_ADPCM_CT:
617 case AV_CODEC_ID_ADPCM_IMA_APC:
618 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
619 case AV_CODEC_ID_ADPCM_IMA_OKI:
620 case AV_CODEC_ID_ADPCM_IMA_WS:
621 case AV_CODEC_ID_ADPCM_YAMAHA:
622 case AV_CODEC_ID_ADPCM_AICA:
623 nb_samples = buf_size * 2 / ch;
629 /* simple 4-bit adpcm, with header */
631 switch (avctx->codec->id) {
632 case AV_CODEC_ID_ADPCM_4XM:
633 case AV_CODEC_ID_ADPCM_AGM:
634 case AV_CODEC_ID_ADPCM_IMA_DAT4:
635 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
636 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
637 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
640 return (buf_size - header_size) * 2 / ch;
642 /* more complex formats */
643 switch (avctx->codec->id) {
644 case AV_CODEC_ID_ADPCM_EA:
645 has_coded_samples = 1;
646 *coded_samples = bytestream2_get_le32(gb);
647 *coded_samples -= *coded_samples % 28;
648 nb_samples = (buf_size - 12) / 30 * 28;
650 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
651 has_coded_samples = 1;
652 *coded_samples = bytestream2_get_le32(gb);
653 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
655 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
656 nb_samples = (buf_size - ch) / ch * 2;
658 case AV_CODEC_ID_ADPCM_EA_R1:
659 case AV_CODEC_ID_ADPCM_EA_R2:
660 case AV_CODEC_ID_ADPCM_EA_R3:
661 /* maximum number of samples */
662 /* has internal offsets and a per-frame switch to signal raw 16-bit */
663 has_coded_samples = 1;
664 switch (avctx->codec->id) {
665 case AV_CODEC_ID_ADPCM_EA_R1:
666 header_size = 4 + 9 * ch;
667 *coded_samples = bytestream2_get_le32(gb);
669 case AV_CODEC_ID_ADPCM_EA_R2:
670 header_size = 4 + 5 * ch;
671 *coded_samples = bytestream2_get_le32(gb);
673 case AV_CODEC_ID_ADPCM_EA_R3:
674 header_size = 4 + 5 * ch;
675 *coded_samples = bytestream2_get_be32(gb);
678 *coded_samples -= *coded_samples % 28;
679 nb_samples = (buf_size - header_size) * 2 / ch;
680 nb_samples -= nb_samples % 28;
681 *approx_nb_samples = 1;
683 case AV_CODEC_ID_ADPCM_IMA_DK3:
684 if (avctx->block_align > 0)
685 buf_size = FFMIN(buf_size, avctx->block_align);
686 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
688 case AV_CODEC_ID_ADPCM_IMA_DK4:
689 if (avctx->block_align > 0)
690 buf_size = FFMIN(buf_size, avctx->block_align);
691 if (buf_size < 4 * ch)
692 return AVERROR_INVALIDDATA;
693 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
695 case AV_CODEC_ID_ADPCM_IMA_RAD:
696 if (avctx->block_align > 0)
697 buf_size = FFMIN(buf_size, avctx->block_align);
698 nb_samples = (buf_size - 4 * ch) * 2 / ch;
700 case AV_CODEC_ID_ADPCM_IMA_WAV:
702 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
703 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
704 if (avctx->block_align > 0)
705 buf_size = FFMIN(buf_size, avctx->block_align);
706 if (buf_size < 4 * ch)
707 return AVERROR_INVALIDDATA;
708 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
711 case AV_CODEC_ID_ADPCM_MS:
712 if (avctx->block_align > 0)
713 buf_size = FFMIN(buf_size, avctx->block_align);
714 nb_samples = (buf_size - 6 * ch) * 2 / ch;
716 case AV_CODEC_ID_ADPCM_MTAF:
717 if (avctx->block_align > 0)
718 buf_size = FFMIN(buf_size, avctx->block_align);
719 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
721 case AV_CODEC_ID_ADPCM_SBPRO_2:
722 case AV_CODEC_ID_ADPCM_SBPRO_3:
723 case AV_CODEC_ID_ADPCM_SBPRO_4:
725 int samples_per_byte;
726 switch (avctx->codec->id) {
727 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
728 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
729 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
731 if (!s->status[0].step_index) {
733 return AVERROR_INVALIDDATA;
737 nb_samples += buf_size * samples_per_byte / ch;
740 case AV_CODEC_ID_ADPCM_SWF:
742 int buf_bits = buf_size * 8 - 2;
743 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
744 int block_hdr_size = 22 * ch;
745 int block_size = block_hdr_size + nbits * ch * 4095;
746 int nblocks = buf_bits / block_size;
747 int bits_left = buf_bits - nblocks * block_size;
748 nb_samples = nblocks * 4096;
749 if (bits_left >= block_hdr_size)
750 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
753 case AV_CODEC_ID_ADPCM_THP:
754 case AV_CODEC_ID_ADPCM_THP_LE:
755 if (avctx->extradata) {
756 nb_samples = buf_size * 14 / (8 * ch);
759 has_coded_samples = 1;
760 bytestream2_skip(gb, 4); // channel size
761 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
762 bytestream2_get_le32(gb) :
763 bytestream2_get_be32(gb);
764 buf_size -= 8 + 36 * ch;
766 nb_samples = buf_size / 8 * 14;
767 if (buf_size % 8 > 1)
768 nb_samples += (buf_size % 8 - 1) * 2;
769 *approx_nb_samples = 1;
771 case AV_CODEC_ID_ADPCM_AFC:
772 nb_samples = buf_size / (9 * ch) * 16;
774 case AV_CODEC_ID_ADPCM_XA:
775 nb_samples = (buf_size / 128) * 224 / ch;
777 case AV_CODEC_ID_ADPCM_DTK:
778 case AV_CODEC_ID_ADPCM_PSX:
779 nb_samples = buf_size / (16 * ch) * 28;
783 /* validate coded sample count */
784 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
785 return AVERROR_INVALIDDATA;
790 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
791 int *got_frame_ptr, AVPacket *avpkt)
793 AVFrame *frame = data;
794 const uint8_t *buf = avpkt->data;
795 int buf_size = avpkt->size;
796 ADPCMDecodeContext *c = avctx->priv_data;
797 ADPCMChannelStatus *cs;
798 int n, m, channel, i;
803 int nb_samples, coded_samples, approx_nb_samples, ret;
806 bytestream2_init(&gb, buf, buf_size);
807 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
808 if (nb_samples <= 0) {
809 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
810 return AVERROR_INVALIDDATA;
813 /* get output buffer */
814 frame->nb_samples = nb_samples;
815 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
817 samples = (int16_t *)frame->data[0];
818 samples_p = (int16_t **)frame->extended_data;
820 /* use coded_samples when applicable */
821 /* it is always <= nb_samples, so the output buffer will be large enough */
823 if (!approx_nb_samples && coded_samples != nb_samples)
824 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
825 frame->nb_samples = nb_samples = coded_samples;
828 st = avctx->channels == 2 ? 1 : 0;
830 switch(avctx->codec->id) {
831 case AV_CODEC_ID_ADPCM_IMA_QT:
832 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
833 Channel data is interleaved per-chunk. */
834 for (channel = 0; channel < avctx->channels; channel++) {
837 cs = &(c->status[channel]);
838 /* (pppppp) (piiiiiii) */
840 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
841 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
842 step_index = predictor & 0x7F;
845 if (cs->step_index == step_index) {
846 int diff = predictor - cs->predictor;
853 cs->step_index = step_index;
854 cs->predictor = predictor;
857 if (cs->step_index > 88u){
858 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
859 channel, cs->step_index);
860 return AVERROR_INVALIDDATA;
863 samples = samples_p[channel];
865 for (m = 0; m < 64; m += 2) {
866 int byte = bytestream2_get_byteu(&gb);
867 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
868 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
872 case AV_CODEC_ID_ADPCM_IMA_WAV:
873 for(i=0; i<avctx->channels; i++){
874 cs = &(c->status[i]);
875 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
877 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
878 if (cs->step_index > 88u){
879 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
881 return AVERROR_INVALIDDATA;
885 if (avctx->bits_per_coded_sample != 4) {
886 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
887 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
888 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
891 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
892 for (i = 0; i < avctx->channels; i++) {
896 samples = &samples_p[i][1 + n * samples_per_block];
897 for (j = 0; j < block_size; j++) {
898 temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
899 (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
901 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
904 for (m = 0; m < samples_per_block; m++) {
905 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
906 avctx->bits_per_coded_sample);
910 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
912 for (n = 0; n < (nb_samples - 1) / 8; n++) {
913 for (i = 0; i < avctx->channels; i++) {
915 samples = &samples_p[i][1 + n * 8];
916 for (m = 0; m < 8; m += 2) {
917 int v = bytestream2_get_byteu(&gb);
918 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
919 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
925 case AV_CODEC_ID_ADPCM_4XM:
926 for (i = 0; i < avctx->channels; i++)
927 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
929 for (i = 0; i < avctx->channels; i++) {
930 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
931 if (c->status[i].step_index > 88u) {
932 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
933 i, c->status[i].step_index);
934 return AVERROR_INVALIDDATA;
938 for (i = 0; i < avctx->channels; i++) {
939 samples = (int16_t *)frame->data[i];
941 for (n = nb_samples >> 1; n > 0; n--) {
942 int v = bytestream2_get_byteu(&gb);
943 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
944 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
948 case AV_CODEC_ID_ADPCM_AGM:
949 for (i = 0; i < avctx->channels; i++)
950 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
951 for (i = 0; i < avctx->channels; i++)
952 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
954 for (n = 0; n < nb_samples >> (1 - st); n++) {
955 int v = bytestream2_get_byteu(&gb);
956 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
957 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
960 case AV_CODEC_ID_ADPCM_MS:
964 if (avctx->channels > 2) {
965 for (channel = 0; channel < avctx->channels; channel++) {
966 samples = samples_p[channel];
967 block_predictor = bytestream2_get_byteu(&gb);
968 if (block_predictor > 6) {
969 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
970 channel, block_predictor);
971 return AVERROR_INVALIDDATA;
973 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
974 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
975 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
976 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
977 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
978 *samples++ = c->status[channel].sample2;
979 *samples++ = c->status[channel].sample1;
980 for(n = (nb_samples - 2) >> 1; n > 0; n--) {
981 int byte = bytestream2_get_byteu(&gb);
982 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
983 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
987 block_predictor = bytestream2_get_byteu(&gb);
988 if (block_predictor > 6) {
989 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
991 return AVERROR_INVALIDDATA;
993 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
994 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
996 block_predictor = bytestream2_get_byteu(&gb);
997 if (block_predictor > 6) {
998 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1000 return AVERROR_INVALIDDATA;
1002 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1003 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1005 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1007 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1010 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1011 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1012 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1013 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1015 *samples++ = c->status[0].sample2;
1016 if (st) *samples++ = c->status[1].sample2;
1017 *samples++ = c->status[0].sample1;
1018 if (st) *samples++ = c->status[1].sample1;
1019 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1020 int byte = bytestream2_get_byteu(&gb);
1021 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1022 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1027 case AV_CODEC_ID_ADPCM_MTAF:
1028 for (channel = 0; channel < avctx->channels; channel+=2) {
1029 bytestream2_skipu(&gb, 4);
1030 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1031 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1032 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1033 bytestream2_skipu(&gb, 2);
1034 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1035 bytestream2_skipu(&gb, 2);
1036 for (n = 0; n < nb_samples; n+=2) {
1037 int v = bytestream2_get_byteu(&gb);
1038 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1039 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1041 for (n = 0; n < nb_samples; n+=2) {
1042 int v = bytestream2_get_byteu(&gb);
1043 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1044 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1048 case AV_CODEC_ID_ADPCM_IMA_DK4:
1049 for (channel = 0; channel < avctx->channels; channel++) {
1050 cs = &c->status[channel];
1051 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1052 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1053 if (cs->step_index > 88u){
1054 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1055 channel, cs->step_index);
1056 return AVERROR_INVALIDDATA;
1059 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1060 int v = bytestream2_get_byteu(&gb);
1061 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1062 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1065 case AV_CODEC_ID_ADPCM_IMA_DK3:
1069 int decode_top_nibble_next = 0;
1071 const int16_t *samples_end = samples + avctx->channels * nb_samples;
1073 bytestream2_skipu(&gb, 10);
1074 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1075 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1076 c->status[0].step_index = bytestream2_get_byteu(&gb);
1077 c->status[1].step_index = bytestream2_get_byteu(&gb);
1078 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1079 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1080 c->status[0].step_index, c->status[1].step_index);
1081 return AVERROR_INVALIDDATA;
1083 /* sign extend the predictors */
1084 diff_channel = c->status[1].predictor;
1086 /* DK3 ADPCM support macro */
1087 #define DK3_GET_NEXT_NIBBLE() \
1088 if (decode_top_nibble_next) { \
1089 nibble = last_byte >> 4; \
1090 decode_top_nibble_next = 0; \
1092 last_byte = bytestream2_get_byteu(&gb); \
1093 nibble = last_byte & 0x0F; \
1094 decode_top_nibble_next = 1; \
1097 while (samples < samples_end) {
1099 /* for this algorithm, c->status[0] is the sum channel and
1100 * c->status[1] is the diff channel */
1102 /* process the first predictor of the sum channel */
1103 DK3_GET_NEXT_NIBBLE();
1104 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1106 /* process the diff channel predictor */
1107 DK3_GET_NEXT_NIBBLE();
1108 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1110 /* process the first pair of stereo PCM samples */
1111 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1112 *samples++ = c->status[0].predictor + c->status[1].predictor;
1113 *samples++ = c->status[0].predictor - c->status[1].predictor;
1115 /* process the second predictor of the sum channel */
1116 DK3_GET_NEXT_NIBBLE();
1117 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1119 /* process the second pair of stereo PCM samples */
1120 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1121 *samples++ = c->status[0].predictor + c->status[1].predictor;
1122 *samples++ = c->status[0].predictor - c->status[1].predictor;
1125 if ((bytestream2_tell(&gb) & 1))
1126 bytestream2_skip(&gb, 1);
1129 case AV_CODEC_ID_ADPCM_IMA_ISS:
1130 for (channel = 0; channel < avctx->channels; channel++) {
1131 cs = &c->status[channel];
1132 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1133 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1134 if (cs->step_index > 88u){
1135 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1136 channel, cs->step_index);
1137 return AVERROR_INVALIDDATA;
1141 for (n = nb_samples >> (1 - st); n > 0; n--) {
1143 int v = bytestream2_get_byteu(&gb);
1144 /* nibbles are swapped for mono */
1152 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1153 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1156 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1157 for (channel = 0; channel < avctx->channels; channel++) {
1158 cs = &c->status[channel];
1159 samples = samples_p[channel];
1160 bytestream2_skip(&gb, 4);
1161 for (n = 0; n < nb_samples; n += 2) {
1162 int v = bytestream2_get_byteu(&gb);
1163 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1164 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1168 case AV_CODEC_ID_ADPCM_IMA_APC:
1169 while (bytestream2_get_bytes_left(&gb) > 0) {
1170 int v = bytestream2_get_byteu(&gb);
1171 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1172 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1175 case AV_CODEC_ID_ADPCM_IMA_OKI:
1176 while (bytestream2_get_bytes_left(&gb) > 0) {
1177 int v = bytestream2_get_byteu(&gb);
1178 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1179 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1182 case AV_CODEC_ID_ADPCM_IMA_RAD:
1183 for (channel = 0; channel < avctx->channels; channel++) {
1184 cs = &c->status[channel];
1185 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1186 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1187 if (cs->step_index > 88u){
1188 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1189 channel, cs->step_index);
1190 return AVERROR_INVALIDDATA;
1193 for (n = 0; n < nb_samples / 2; n++) {
1196 byte[0] = bytestream2_get_byteu(&gb);
1198 byte[1] = bytestream2_get_byteu(&gb);
1199 for(channel = 0; channel < avctx->channels; channel++) {
1200 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1202 for(channel = 0; channel < avctx->channels; channel++) {
1203 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1207 case AV_CODEC_ID_ADPCM_IMA_WS:
1208 if (c->vqa_version == 3) {
1209 for (channel = 0; channel < avctx->channels; channel++) {
1210 int16_t *smp = samples_p[channel];
1212 for (n = nb_samples / 2; n > 0; n--) {
1213 int v = bytestream2_get_byteu(&gb);
1214 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1215 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1219 for (n = nb_samples / 2; n > 0; n--) {
1220 for (channel = 0; channel < avctx->channels; channel++) {
1221 int v = bytestream2_get_byteu(&gb);
1222 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1223 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1225 samples += avctx->channels;
1228 bytestream2_seek(&gb, 0, SEEK_END);
1230 case AV_CODEC_ID_ADPCM_XA:
1232 int16_t *out0 = samples_p[0];
1233 int16_t *out1 = samples_p[1];
1234 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1235 int sample_offset = 0;
1236 int bytes_remaining;
1237 while (bytestream2_get_bytes_left(&gb) >= 128) {
1238 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1239 &c->status[0], &c->status[1],
1240 avctx->channels, sample_offset)) < 0)
1242 bytestream2_skipu(&gb, 128);
1243 sample_offset += samples_per_block;
1245 /* Less than a full block of data left, e.g. when reading from
1246 * 2324 byte per sector XA; the remainder is padding */
1247 bytes_remaining = bytestream2_get_bytes_left(&gb);
1248 if (bytes_remaining > 0) {
1249 bytestream2_skip(&gb, bytes_remaining);
1253 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1254 for (i=0; i<=st; i++) {
1255 c->status[i].step_index = bytestream2_get_le32u(&gb);
1256 if (c->status[i].step_index > 88u) {
1257 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1258 i, c->status[i].step_index);
1259 return AVERROR_INVALIDDATA;
1262 for (i=0; i<=st; i++) {
1263 c->status[i].predictor = bytestream2_get_le32u(&gb);
1264 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1265 return AVERROR_INVALIDDATA;
1268 for (n = nb_samples >> (1 - st); n > 0; n--) {
1269 int byte = bytestream2_get_byteu(&gb);
1270 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1271 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1274 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1275 for (n = nb_samples >> (1 - st); n > 0; n--) {
1276 int byte = bytestream2_get_byteu(&gb);
1277 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1278 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1281 case AV_CODEC_ID_ADPCM_EA:
1283 int previous_left_sample, previous_right_sample;
1284 int current_left_sample, current_right_sample;
1285 int next_left_sample, next_right_sample;
1286 int coeff1l, coeff2l, coeff1r, coeff2r;
1287 int shift_left, shift_right;
1289 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1290 each coding 28 stereo samples. */
1292 if(avctx->channels != 2)
1293 return AVERROR_INVALIDDATA;
1295 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1296 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1297 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1298 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1300 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1301 int byte = bytestream2_get_byteu(&gb);
1302 coeff1l = ea_adpcm_table[ byte >> 4 ];
1303 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1304 coeff1r = ea_adpcm_table[ byte & 0x0F];
1305 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1307 byte = bytestream2_get_byteu(&gb);
1308 shift_left = 20 - (byte >> 4);
1309 shift_right = 20 - (byte & 0x0F);
1311 for (count2 = 0; count2 < 28; count2++) {
1312 byte = bytestream2_get_byteu(&gb);
1313 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1314 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1316 next_left_sample = (next_left_sample +
1317 (current_left_sample * coeff1l) +
1318 (previous_left_sample * coeff2l) + 0x80) >> 8;
1319 next_right_sample = (next_right_sample +
1320 (current_right_sample * coeff1r) +
1321 (previous_right_sample * coeff2r) + 0x80) >> 8;
1323 previous_left_sample = current_left_sample;
1324 current_left_sample = av_clip_int16(next_left_sample);
1325 previous_right_sample = current_right_sample;
1326 current_right_sample = av_clip_int16(next_right_sample);
1327 *samples++ = current_left_sample;
1328 *samples++ = current_right_sample;
1332 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1336 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1338 int coeff[2][2], shift[2];
1340 for(channel = 0; channel < avctx->channels; channel++) {
1341 int byte = bytestream2_get_byteu(&gb);
1343 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1344 shift[channel] = 20 - (byte & 0x0F);
1346 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1349 byte[0] = bytestream2_get_byteu(&gb);
1350 if (st) byte[1] = bytestream2_get_byteu(&gb);
1351 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1352 for(channel = 0; channel < avctx->channels; channel++) {
1353 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1355 c->status[channel].sample1 * coeff[channel][0] +
1356 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1357 c->status[channel].sample2 = c->status[channel].sample1;
1358 c->status[channel].sample1 = av_clip_int16(sample);
1359 *samples++ = c->status[channel].sample1;
1363 bytestream2_seek(&gb, 0, SEEK_END);
1366 case AV_CODEC_ID_ADPCM_EA_R1:
1367 case AV_CODEC_ID_ADPCM_EA_R2:
1368 case AV_CODEC_ID_ADPCM_EA_R3: {
1369 /* channel numbering
1371 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1372 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1373 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1374 int previous_sample, current_sample, next_sample;
1377 unsigned int channel;
1382 for (channel=0; channel<avctx->channels; channel++)
1383 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1384 bytestream2_get_le32(&gb)) +
1385 (avctx->channels + 1) * 4;
1387 for (channel=0; channel<avctx->channels; channel++) {
1388 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1389 samplesC = samples_p[channel];
1391 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1392 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1393 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1395 current_sample = c->status[channel].predictor;
1396 previous_sample = c->status[channel].prev_sample;
1399 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1400 int byte = bytestream2_get_byte(&gb);
1401 if (byte == 0xEE) { /* only seen in R2 and R3 */
1402 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1403 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1405 for (count2=0; count2<28; count2++)
1406 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1408 coeff1 = ea_adpcm_table[ byte >> 4 ];
1409 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1410 shift = 20 - (byte & 0x0F);
1412 for (count2=0; count2<28; count2++) {
1414 next_sample = (unsigned)sign_extend(byte, 4) << shift;
1416 byte = bytestream2_get_byte(&gb);
1417 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1420 next_sample += (current_sample * coeff1) +
1421 (previous_sample * coeff2);
1422 next_sample = av_clip_int16(next_sample >> 8);
1424 previous_sample = current_sample;
1425 current_sample = next_sample;
1426 *samplesC++ = current_sample;
1432 } else if (count != count1) {
1433 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1434 count = FFMAX(count, count1);
1437 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1438 c->status[channel].predictor = current_sample;
1439 c->status[channel].prev_sample = previous_sample;
1443 frame->nb_samples = count * 28;
1444 bytestream2_seek(&gb, 0, SEEK_END);
1447 case AV_CODEC_ID_ADPCM_EA_XAS:
1448 for (channel=0; channel<avctx->channels; channel++) {
1449 int coeff[2][4], shift[4];
1450 int16_t *s = samples_p[channel];
1451 for (n = 0; n < 4; n++, s += 32) {
1452 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1454 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1457 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1458 shift[n] = 20 - (val & 0x0F);
1462 for (m=2; m<32; m+=2) {
1463 s = &samples_p[channel][m];
1464 for (n = 0; n < 4; n++, s += 32) {
1466 int byte = bytestream2_get_byteu(&gb);
1468 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1469 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1470 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1472 level = sign_extend(byte, 4) * (1 << shift[n]);
1473 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1474 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1479 case AV_CODEC_ID_ADPCM_IMA_AMV:
1480 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1481 c->status[0].step_index = bytestream2_get_byteu(&gb);
1482 bytestream2_skipu(&gb, 5);
1483 if (c->status[0].step_index > 88u) {
1484 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1485 c->status[0].step_index);
1486 return AVERROR_INVALIDDATA;
1489 for (n = nb_samples >> (1 - st); n > 0; n--) {
1490 int v = bytestream2_get_byteu(&gb);
1492 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1493 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1496 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1497 for (i = 0; i < avctx->channels; i++) {
1498 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1499 c->status[i].step_index = bytestream2_get_byteu(&gb);
1500 bytestream2_skipu(&gb, 1);
1501 if (c->status[i].step_index > 88u) {
1502 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1503 c->status[i].step_index);
1504 return AVERROR_INVALIDDATA;
1508 for (n = nb_samples >> (1 - st); n > 0; n--) {
1509 int v = bytestream2_get_byteu(&gb);
1511 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1512 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1515 case AV_CODEC_ID_ADPCM_CT:
1516 for (n = nb_samples >> (1 - st); n > 0; n--) {
1517 int v = bytestream2_get_byteu(&gb);
1518 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1519 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1522 case AV_CODEC_ID_ADPCM_SBPRO_4:
1523 case AV_CODEC_ID_ADPCM_SBPRO_3:
1524 case AV_CODEC_ID_ADPCM_SBPRO_2:
1525 if (!c->status[0].step_index) {
1526 /* the first byte is a raw sample */
1527 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1529 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1530 c->status[0].step_index = 1;
1533 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1534 for (n = nb_samples >> (1 - st); n > 0; n--) {
1535 int byte = bytestream2_get_byteu(&gb);
1536 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1538 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1541 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1542 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1543 int byte = bytestream2_get_byteu(&gb);
1544 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1546 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1547 (byte >> 2) & 0x07, 3, 0);
1548 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1552 for (n = nb_samples >> (2 - st); n > 0; n--) {
1553 int byte = bytestream2_get_byteu(&gb);
1554 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1556 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1557 (byte >> 4) & 0x03, 2, 2);
1558 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1559 (byte >> 2) & 0x03, 2, 2);
1560 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1565 case AV_CODEC_ID_ADPCM_SWF:
1566 adpcm_swf_decode(avctx, buf, buf_size, samples);
1567 bytestream2_seek(&gb, 0, SEEK_END);
1569 case AV_CODEC_ID_ADPCM_YAMAHA:
1570 for (n = nb_samples >> (1 - st); n > 0; n--) {
1571 int v = bytestream2_get_byteu(&gb);
1572 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1573 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1576 case AV_CODEC_ID_ADPCM_AICA:
1577 if (!c->has_status) {
1578 for (channel = 0; channel < avctx->channels; channel++)
1579 c->status[channel].step = 0;
1582 for (channel = 0; channel < avctx->channels; channel++) {
1583 samples = samples_p[channel];
1584 for (n = nb_samples >> 1; n > 0; n--) {
1585 int v = bytestream2_get_byteu(&gb);
1586 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1587 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1591 case AV_CODEC_ID_ADPCM_AFC:
1593 int samples_per_block;
1596 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1597 samples_per_block = avctx->extradata[0] / 16;
1598 blocks = nb_samples / avctx->extradata[0];
1600 samples_per_block = nb_samples / 16;
1604 for (m = 0; m < blocks; m++) {
1605 for (channel = 0; channel < avctx->channels; channel++) {
1606 int prev1 = c->status[channel].sample1;
1607 int prev2 = c->status[channel].sample2;
1609 samples = samples_p[channel] + m * 16;
1610 /* Read in every sample for this channel. */
1611 for (i = 0; i < samples_per_block; i++) {
1612 int byte = bytestream2_get_byteu(&gb);
1613 int scale = 1 << (byte >> 4);
1614 int index = byte & 0xf;
1615 int factor1 = ff_adpcm_afc_coeffs[0][index];
1616 int factor2 = ff_adpcm_afc_coeffs[1][index];
1618 /* Decode 16 samples. */
1619 for (n = 0; n < 16; n++) {
1623 sampledat = sign_extend(byte, 4);
1625 byte = bytestream2_get_byteu(&gb);
1626 sampledat = sign_extend(byte >> 4, 4);
1629 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1631 *samples = av_clip_int16(sampledat);
1637 c->status[channel].sample1 = prev1;
1638 c->status[channel].sample2 = prev2;
1641 bytestream2_seek(&gb, 0, SEEK_END);
1644 case AV_CODEC_ID_ADPCM_THP:
1645 case AV_CODEC_ID_ADPCM_THP_LE:
1650 #define THP_GET16(g) \
1652 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1653 bytestream2_get_le16u(&(g)) : \
1654 bytestream2_get_be16u(&(g)), 16)
1656 if (avctx->extradata) {
1658 if (avctx->extradata_size < 32 * avctx->channels) {
1659 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1660 return AVERROR_INVALIDDATA;
1663 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1664 for (i = 0; i < avctx->channels; i++)
1665 for (n = 0; n < 16; n++)
1666 table[i][n] = THP_GET16(tb);
1668 for (i = 0; i < avctx->channels; i++)
1669 for (n = 0; n < 16; n++)
1670 table[i][n] = THP_GET16(gb);
1672 if (!c->has_status) {
1673 /* Initialize the previous sample. */
1674 for (i = 0; i < avctx->channels; i++) {
1675 c->status[i].sample1 = THP_GET16(gb);
1676 c->status[i].sample2 = THP_GET16(gb);
1680 bytestream2_skip(&gb, avctx->channels * 4);
1684 for (ch = 0; ch < avctx->channels; ch++) {
1685 samples = samples_p[ch];
1687 /* Read in every sample for this channel. */
1688 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1689 int byte = bytestream2_get_byteu(&gb);
1690 int index = (byte >> 4) & 7;
1691 unsigned int exp = byte & 0x0F;
1692 int factor1 = table[ch][index * 2];
1693 int factor2 = table[ch][index * 2 + 1];
1695 /* Decode 14 samples. */
1696 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1700 sampledat = sign_extend(byte, 4);
1702 byte = bytestream2_get_byteu(&gb);
1703 sampledat = sign_extend(byte >> 4, 4);
1706 sampledat = ((c->status[ch].sample1 * factor1
1707 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1708 *samples = av_clip_int16(sampledat);
1709 c->status[ch].sample2 = c->status[ch].sample1;
1710 c->status[ch].sample1 = *samples++;
1716 case AV_CODEC_ID_ADPCM_DTK:
1717 for (channel = 0; channel < avctx->channels; channel++) {
1718 samples = samples_p[channel];
1720 /* Read in every sample for this channel. */
1721 for (i = 0; i < nb_samples / 28; i++) {
1724 bytestream2_skipu(&gb, 1);
1725 header = bytestream2_get_byteu(&gb);
1726 bytestream2_skipu(&gb, 3 - channel);
1728 /* Decode 28 samples. */
1729 for (n = 0; n < 28; n++) {
1730 int32_t sampledat, prev;
1732 switch (header >> 4) {
1734 prev = (c->status[channel].sample1 * 0x3c);
1737 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1740 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1746 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1748 byte = bytestream2_get_byteu(&gb);
1750 sampledat = sign_extend(byte, 4);
1752 sampledat = sign_extend(byte >> 4, 4);
1754 sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1755 *samples++ = av_clip_int16(sampledat >> 6);
1756 c->status[channel].sample2 = c->status[channel].sample1;
1757 c->status[channel].sample1 = sampledat;
1761 bytestream2_seek(&gb, 0, SEEK_SET);
1764 case AV_CODEC_ID_ADPCM_PSX:
1765 for (channel = 0; channel < avctx->channels; channel++) {
1766 samples = samples_p[channel];
1768 /* Read in every sample for this channel. */
1769 for (i = 0; i < nb_samples / 28; i++) {
1770 int filter, shift, flag, byte;
1772 filter = bytestream2_get_byteu(&gb);
1773 shift = filter & 0xf;
1774 filter = filter >> 4;
1775 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1776 return AVERROR_INVALIDDATA;
1777 flag = bytestream2_get_byteu(&gb);
1779 /* Decode 28 samples. */
1780 for (n = 0; n < 28; n++) {
1781 int sample = 0, scale;
1785 scale = sign_extend(byte >> 4, 4);
1787 byte = bytestream2_get_byteu(&gb);
1788 scale = sign_extend(byte, 4);
1791 scale = scale << 12;
1792 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1794 *samples++ = av_clip_int16(sample);
1795 c->status[channel].sample2 = c->status[channel].sample1;
1796 c->status[channel].sample1 = sample;
1801 case AV_CODEC_ID_ADPCM_ARGO:
1803 * The format of each block:
1804 * uint8_t left_control;
1805 * uint4_t left_samples[nb_samples];
1806 * ---- and if stereo ----
1807 * uint8_t right_control;
1808 * uint4_t right_samples[nb_samples];
1810 * Format of the control byte:
1811 * MSB [SSSSDRRR] LSB
1812 * S = (Shift Amount - 2)
1816 * Each block relies on the previous two samples of each channel.
1817 * They should be 0 initially.
1819 for (channel = 0; channel < avctx->channels; channel++) {
1822 samples = samples_p[channel];
1823 cs = c->status + channel;
1825 /* Get the control byte and decode the samples, 2 at a time. */
1826 control = bytestream2_get_byteu(&gb);
1827 shift = (control >> 4) + 2;
1829 for (n = 0; n < nb_samples / 2; n++) {
1830 int sample = bytestream2_get_byteu(&gb);
1831 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 4, 4), control, shift);
1832 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 0, 4), control, shift);
1837 av_assert0(0); // unsupported codec_id should not happen
1840 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1841 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1842 return AVERROR_INVALIDDATA;
1847 if (avpkt->size < bytestream2_tell(&gb)) {
1848 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1852 return bytestream2_tell(&gb);
1855 static void adpcm_flush(AVCodecContext *avctx)
1857 ADPCMDecodeContext *c = avctx->priv_data;
1862 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1863 AV_SAMPLE_FMT_NONE };
1864 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1865 AV_SAMPLE_FMT_NONE };
1866 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1868 AV_SAMPLE_FMT_NONE };
1870 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1871 AVCodec ff_ ## name_ ## _decoder = { \
1873 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1874 .type = AVMEDIA_TYPE_AUDIO, \
1876 .priv_data_size = sizeof(ADPCMDecodeContext), \
1877 .init = adpcm_decode_init, \
1878 .decode = adpcm_decode_frame, \
1879 .flush = adpcm_flush, \
1880 .capabilities = AV_CODEC_CAP_DR1, \
1881 .sample_fmts = sample_fmts_, \
1884 /* Note: Do not forget to add new entries to the Makefile as well. */
1885 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1886 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1887 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
1888 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
1889 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO, sample_fmts_s16p, adpcm_argo, "ADPCM Argonaut Games");
1890 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1891 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1892 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1893 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1894 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1895 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1896 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1897 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1898 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1899 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1900 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
1901 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1902 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1903 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1904 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1905 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1906 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1907 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1908 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1909 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1910 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1911 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1912 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
1913 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
1914 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
1915 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1916 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1917 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1918 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1919 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1920 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1921 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1922 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");