]> git.sesse.net Git - ffmpeg/blob - libavcodec/adpcm.c
lavc/adpcm: THP: fix indentation
[ffmpeg] / libavcodec / adpcm.c
1 /*
2  * Copyright (c) 2001-2003 The FFmpeg Project
3  *
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  *
16  * This file is part of FFmpeg.
17  *
18  * FFmpeg is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * FFmpeg is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public
29  * License along with FFmpeg; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31  */
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "bytestream.h"
35 #include "adpcm.h"
36 #include "adpcm_data.h"
37 #include "internal.h"
38
39 /**
40  * @file
41  * ADPCM decoders
42  * Features and limitations:
43  *
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/
52  *
53  * CD-ROM XA:
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/
57  */
58
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table[5][2] = {
61     {   0,   0 },
62     {  60,   0 },
63     { 115, -52 },
64     {  98, -55 },
65     { 122, -60 }
66 };
67
68 static const int ea_adpcm_table[] = {
69     0,  240,  460,  392,
70     0,    0, -208, -220,
71     0,    1,    3,    4,
72     7,    8,   10,   11,
73     0,   -1,   -3,   -4
74 };
75
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables[4][16] = {
78     /*2*/ { -1, 2 },
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 }
82 };
83
84 /* end of tables */
85
86 typedef struct ADPCMDecodeContext {
87     ADPCMChannelStatus status[10];
88     int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */
89 } ADPCMDecodeContext;
90
91 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
92 {
93     ADPCMDecodeContext *c = avctx->priv_data;
94     unsigned int min_channels = 1;
95     unsigned int max_channels = 2;
96
97     switch(avctx->codec->id) {
98     case AV_CODEC_ID_ADPCM_DTK:
99     case AV_CODEC_ID_ADPCM_EA:
100         min_channels = 2;
101         break;
102     case AV_CODEC_ID_ADPCM_AFC:
103     case AV_CODEC_ID_ADPCM_EA_R1:
104     case AV_CODEC_ID_ADPCM_EA_R2:
105     case AV_CODEC_ID_ADPCM_EA_R3:
106     case AV_CODEC_ID_ADPCM_EA_XAS:
107         max_channels = 6;
108         break;
109     case AV_CODEC_ID_ADPCM_THP:
110     case AV_CODEC_ID_ADPCM_THP_LE:
111         max_channels = 10;
112         break;
113     }
114     if (avctx->channels < min_channels || avctx->channels > max_channels) {
115         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
116         return AVERROR(EINVAL);
117     }
118
119     switch(avctx->codec->id) {
120     case AV_CODEC_ID_ADPCM_CT:
121         c->status[0].step = c->status[1].step = 511;
122         break;
123     case AV_CODEC_ID_ADPCM_IMA_WAV:
124         if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
125             return AVERROR_INVALIDDATA;
126         break;
127     case AV_CODEC_ID_ADPCM_IMA_APC:
128         if (avctx->extradata && avctx->extradata_size >= 8) {
129             c->status[0].predictor = AV_RL32(avctx->extradata);
130             c->status[1].predictor = AV_RL32(avctx->extradata + 4);
131         }
132         break;
133     case AV_CODEC_ID_ADPCM_IMA_WS:
134         if (avctx->extradata && avctx->extradata_size >= 2)
135             c->vqa_version = AV_RL16(avctx->extradata);
136         break;
137     default:
138         break;
139     }
140
141     switch(avctx->codec->id) {
142         case AV_CODEC_ID_ADPCM_IMA_QT:
143         case AV_CODEC_ID_ADPCM_IMA_WAV:
144         case AV_CODEC_ID_ADPCM_4XM:
145         case AV_CODEC_ID_ADPCM_XA:
146         case AV_CODEC_ID_ADPCM_EA_R1:
147         case AV_CODEC_ID_ADPCM_EA_R2:
148         case AV_CODEC_ID_ADPCM_EA_R3:
149         case AV_CODEC_ID_ADPCM_EA_XAS:
150         case AV_CODEC_ID_ADPCM_THP:
151         case AV_CODEC_ID_ADPCM_THP_LE:
152         case AV_CODEC_ID_ADPCM_AFC:
153         case AV_CODEC_ID_ADPCM_DTK:
154             avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
155             break;
156         case AV_CODEC_ID_ADPCM_IMA_WS:
157             avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
158                                                       AV_SAMPLE_FMT_S16;
159             break;
160         default:
161             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
162     }
163
164     return 0;
165 }
166
167 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
168 {
169     int step_index;
170     int predictor;
171     int sign, delta, diff, step;
172
173     step = ff_adpcm_step_table[c->step_index];
174     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
175     step_index = av_clip(step_index, 0, 88);
176
177     sign = nibble & 8;
178     delta = nibble & 7;
179     /* perform direct multiplication instead of series of jumps proposed by
180      * the reference ADPCM implementation since modern CPUs can do the mults
181      * quickly enough */
182     diff = ((2 * delta + 1) * step) >> shift;
183     predictor = c->predictor;
184     if (sign) predictor -= diff;
185     else predictor += diff;
186
187     c->predictor = av_clip_int16(predictor);
188     c->step_index = step_index;
189
190     return (short)c->predictor;
191 }
192
193 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
194 {
195     int nibble, step_index, predictor, sign, delta, diff, step, shift;
196
197     shift = bps - 1;
198     nibble = get_bits_le(gb, bps),
199     step = ff_adpcm_step_table[c->step_index];
200     step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
201     step_index = av_clip(step_index, 0, 88);
202
203     sign = nibble & (1 << shift);
204     delta = av_mod_uintp2(nibble, shift);
205     diff = ((2 * delta + 1) * step) >> shift;
206     predictor = c->predictor;
207     if (sign) predictor -= diff;
208     else predictor += diff;
209
210     c->predictor = av_clip_int16(predictor);
211     c->step_index = step_index;
212
213     return (int16_t)c->predictor;
214 }
215
216 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
217 {
218     int step_index;
219     int predictor;
220     int diff, step;
221
222     step = ff_adpcm_step_table[c->step_index];
223     step_index = c->step_index + ff_adpcm_index_table[nibble];
224     step_index = av_clip(step_index, 0, 88);
225
226     diff = step >> 3;
227     if (nibble & 4) diff += step;
228     if (nibble & 2) diff += step >> 1;
229     if (nibble & 1) diff += step >> 2;
230
231     if (nibble & 8)
232         predictor = c->predictor - diff;
233     else
234         predictor = c->predictor + diff;
235
236     c->predictor = av_clip_int16(predictor);
237     c->step_index = step_index;
238
239     return c->predictor;
240 }
241
242 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
243 {
244     int predictor;
245
246     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
247     predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
248
249     c->sample2 = c->sample1;
250     c->sample1 = av_clip_int16(predictor);
251     c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
252     if (c->idelta < 16) c->idelta = 16;
253     if (c->idelta > INT_MAX/768) {
254         av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
255         c->idelta = INT_MAX/768;
256     }
257
258     return c->sample1;
259 }
260
261 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
262 {
263     int step_index, predictor, sign, delta, diff, step;
264
265     step = ff_adpcm_oki_step_table[c->step_index];
266     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
267     step_index = av_clip(step_index, 0, 48);
268
269     sign = nibble & 8;
270     delta = nibble & 7;
271     diff = ((2 * delta + 1) * step) >> 3;
272     predictor = c->predictor;
273     if (sign) predictor -= diff;
274     else predictor += diff;
275
276     c->predictor = av_clip_intp2(predictor, 11);
277     c->step_index = step_index;
278
279     return c->predictor << 4;
280 }
281
282 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
283 {
284     int sign, delta, diff;
285     int new_step;
286
287     sign = nibble & 8;
288     delta = nibble & 7;
289     /* perform direct multiplication instead of series of jumps proposed by
290      * the reference ADPCM implementation since modern CPUs can do the mults
291      * quickly enough */
292     diff = ((2 * delta + 1) * c->step) >> 3;
293     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
294     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
295     c->predictor = av_clip_int16(c->predictor);
296     /* calculate new step and clamp it to range 511..32767 */
297     new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
298     c->step = av_clip(new_step, 511, 32767);
299
300     return (short)c->predictor;
301 }
302
303 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
304 {
305     int sign, delta, diff;
306
307     sign = nibble & (1<<(size-1));
308     delta = nibble & ((1<<(size-1))-1);
309     diff = delta << (7 + c->step + shift);
310
311     /* clamp result */
312     c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
313
314     /* calculate new step */
315     if (delta >= (2*size - 3) && c->step < 3)
316         c->step++;
317     else if (delta == 0 && c->step > 0)
318         c->step--;
319
320     return (short) c->predictor;
321 }
322
323 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
324 {
325     if(!c->step) {
326         c->predictor = 0;
327         c->step = 127;
328     }
329
330     c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
331     c->predictor = av_clip_int16(c->predictor);
332     c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
333     c->step = av_clip(c->step, 127, 24567);
334     return c->predictor;
335 }
336
337 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
338                      const uint8_t *in, ADPCMChannelStatus *left,
339                      ADPCMChannelStatus *right, int channels, int sample_offset)
340 {
341     int i, j;
342     int shift,filter,f0,f1;
343     int s_1,s_2;
344     int d,s,t;
345
346     out0 += sample_offset;
347     if (channels == 1)
348         out1 = out0 + 28;
349     else
350         out1 += sample_offset;
351
352     for(i=0;i<4;i++) {
353         shift  = 12 - (in[4+i*2] & 15);
354         filter = in[4+i*2] >> 4;
355         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
356             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
357             filter=0;
358         }
359         f0 = xa_adpcm_table[filter][0];
360         f1 = xa_adpcm_table[filter][1];
361
362         s_1 = left->sample1;
363         s_2 = left->sample2;
364
365         for(j=0;j<28;j++) {
366             d = in[16+i+j*4];
367
368             t = sign_extend(d, 4);
369             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
370             s_2 = s_1;
371             s_1 = av_clip_int16(s);
372             out0[j] = s_1;
373         }
374
375         if (channels == 2) {
376             left->sample1 = s_1;
377             left->sample2 = s_2;
378             s_1 = right->sample1;
379             s_2 = right->sample2;
380         }
381
382         shift  = 12 - (in[5+i*2] & 15);
383         filter = in[5+i*2] >> 4;
384         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
385             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
386             filter=0;
387         }
388
389         f0 = xa_adpcm_table[filter][0];
390         f1 = xa_adpcm_table[filter][1];
391
392         for(j=0;j<28;j++) {
393             d = in[16+i+j*4];
394
395             t = sign_extend(d >> 4, 4);
396             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
397             s_2 = s_1;
398             s_1 = av_clip_int16(s);
399             out1[j] = s_1;
400         }
401
402         if (channels == 2) {
403             right->sample1 = s_1;
404             right->sample2 = s_2;
405         } else {
406             left->sample1 = s_1;
407             left->sample2 = s_2;
408         }
409
410         out0 += 28 * (3 - channels);
411         out1 += 28 * (3 - channels);
412     }
413
414     return 0;
415 }
416
417 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
418 {
419     ADPCMDecodeContext *c = avctx->priv_data;
420     GetBitContext gb;
421     const int *table;
422     int k0, signmask, nb_bits, count;
423     int size = buf_size*8;
424     int i;
425
426     init_get_bits(&gb, buf, size);
427
428     //read bits & initial values
429     nb_bits = get_bits(&gb, 2)+2;
430     table = swf_index_tables[nb_bits-2];
431     k0 = 1 << (nb_bits-2);
432     signmask = 1 << (nb_bits-1);
433
434     while (get_bits_count(&gb) <= size - 22*avctx->channels) {
435         for (i = 0; i < avctx->channels; i++) {
436             *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
437             c->status[i].step_index = get_bits(&gb, 6);
438         }
439
440         for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
441             int i;
442
443             for (i = 0; i < avctx->channels; i++) {
444                 // similar to IMA adpcm
445                 int delta = get_bits(&gb, nb_bits);
446                 int step = ff_adpcm_step_table[c->status[i].step_index];
447                 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
448                 int k = k0;
449
450                 do {
451                     if (delta & k)
452                         vpdiff += step;
453                     step >>= 1;
454                     k >>= 1;
455                 } while(k);
456                 vpdiff += step;
457
458                 if (delta & signmask)
459                     c->status[i].predictor -= vpdiff;
460                 else
461                     c->status[i].predictor += vpdiff;
462
463                 c->status[i].step_index += table[delta & (~signmask)];
464
465                 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
466                 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
467
468                 *samples++ = c->status[i].predictor;
469             }
470         }
471     }
472 }
473
474 /**
475  * Get the number of samples that will be decoded from the packet.
476  * In one case, this is actually the maximum number of samples possible to
477  * decode with the given buf_size.
478  *
479  * @param[out] coded_samples set to the number of samples as coded in the
480  *                           packet, or 0 if the codec does not encode the
481  *                           number of samples in each frame.
482  * @param[out] approx_nb_samples set to non-zero if the number of samples
483  *                               returned is an approximation.
484  */
485 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
486                           int buf_size, int *coded_samples, int *approx_nb_samples)
487 {
488     ADPCMDecodeContext *s = avctx->priv_data;
489     int nb_samples        = 0;
490     int ch                = avctx->channels;
491     int has_coded_samples = 0;
492     int header_size;
493
494     *coded_samples = 0;
495     *approx_nb_samples = 0;
496
497     if(ch <= 0)
498         return 0;
499
500     switch (avctx->codec->id) {
501     /* constant, only check buf_size */
502     case AV_CODEC_ID_ADPCM_EA_XAS:
503         if (buf_size < 76 * ch)
504             return 0;
505         nb_samples = 128;
506         break;
507     case AV_CODEC_ID_ADPCM_IMA_QT:
508         if (buf_size < 34 * ch)
509             return 0;
510         nb_samples = 64;
511         break;
512     /* simple 4-bit adpcm */
513     case AV_CODEC_ID_ADPCM_CT:
514     case AV_CODEC_ID_ADPCM_IMA_APC:
515     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
516     case AV_CODEC_ID_ADPCM_IMA_OKI:
517     case AV_CODEC_ID_ADPCM_IMA_WS:
518     case AV_CODEC_ID_ADPCM_YAMAHA:
519         nb_samples = buf_size * 2 / ch;
520         break;
521     }
522     if (nb_samples)
523         return nb_samples;
524
525     /* simple 4-bit adpcm, with header */
526     header_size = 0;
527     switch (avctx->codec->id) {
528         case AV_CODEC_ID_ADPCM_4XM:
529         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
530         case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
531         case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
532     }
533     if (header_size > 0)
534         return (buf_size - header_size) * 2 / ch;
535
536     /* more complex formats */
537     switch (avctx->codec->id) {
538     case AV_CODEC_ID_ADPCM_EA:
539         has_coded_samples = 1;
540         *coded_samples  = bytestream2_get_le32(gb);
541         *coded_samples -= *coded_samples % 28;
542         nb_samples      = (buf_size - 12) / 30 * 28;
543         break;
544     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
545         has_coded_samples = 1;
546         *coded_samples = bytestream2_get_le32(gb);
547         nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
548         break;
549     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
550         nb_samples = (buf_size - ch) / ch * 2;
551         break;
552     case AV_CODEC_ID_ADPCM_EA_R1:
553     case AV_CODEC_ID_ADPCM_EA_R2:
554     case AV_CODEC_ID_ADPCM_EA_R3:
555         /* maximum number of samples */
556         /* has internal offsets and a per-frame switch to signal raw 16-bit */
557         has_coded_samples = 1;
558         switch (avctx->codec->id) {
559         case AV_CODEC_ID_ADPCM_EA_R1:
560             header_size    = 4 + 9 * ch;
561             *coded_samples = bytestream2_get_le32(gb);
562             break;
563         case AV_CODEC_ID_ADPCM_EA_R2:
564             header_size    = 4 + 5 * ch;
565             *coded_samples = bytestream2_get_le32(gb);
566             break;
567         case AV_CODEC_ID_ADPCM_EA_R3:
568             header_size    = 4 + 5 * ch;
569             *coded_samples = bytestream2_get_be32(gb);
570             break;
571         }
572         *coded_samples -= *coded_samples % 28;
573         nb_samples      = (buf_size - header_size) * 2 / ch;
574         nb_samples     -= nb_samples % 28;
575         *approx_nb_samples = 1;
576         break;
577     case AV_CODEC_ID_ADPCM_IMA_DK3:
578         if (avctx->block_align > 0)
579             buf_size = FFMIN(buf_size, avctx->block_align);
580         nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
581         break;
582     case AV_CODEC_ID_ADPCM_IMA_DK4:
583         if (avctx->block_align > 0)
584             buf_size = FFMIN(buf_size, avctx->block_align);
585         if (buf_size < 4 * ch)
586             return AVERROR_INVALIDDATA;
587         nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
588         break;
589     case AV_CODEC_ID_ADPCM_IMA_RAD:
590         if (avctx->block_align > 0)
591             buf_size = FFMIN(buf_size, avctx->block_align);
592         nb_samples = (buf_size - 4 * ch) * 2 / ch;
593         break;
594     case AV_CODEC_ID_ADPCM_IMA_WAV:
595     {
596         int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
597         int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
598         if (avctx->block_align > 0)
599             buf_size = FFMIN(buf_size, avctx->block_align);
600         if (buf_size < 4 * ch)
601             return AVERROR_INVALIDDATA;
602         nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
603         break;
604     }
605     case AV_CODEC_ID_ADPCM_MS:
606         if (avctx->block_align > 0)
607             buf_size = FFMIN(buf_size, avctx->block_align);
608         nb_samples = (buf_size - 6 * ch) * 2 / ch;
609         break;
610     case AV_CODEC_ID_ADPCM_SBPRO_2:
611     case AV_CODEC_ID_ADPCM_SBPRO_3:
612     case AV_CODEC_ID_ADPCM_SBPRO_4:
613     {
614         int samples_per_byte;
615         switch (avctx->codec->id) {
616         case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
617         case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
618         case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
619         }
620         if (!s->status[0].step_index) {
621             if (buf_size < ch)
622                 return AVERROR_INVALIDDATA;
623             nb_samples++;
624             buf_size -= ch;
625         }
626         nb_samples += buf_size * samples_per_byte / ch;
627         break;
628     }
629     case AV_CODEC_ID_ADPCM_SWF:
630     {
631         int buf_bits       = buf_size * 8 - 2;
632         int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
633         int block_hdr_size = 22 * ch;
634         int block_size     = block_hdr_size + nbits * ch * 4095;
635         int nblocks        = buf_bits / block_size;
636         int bits_left      = buf_bits - nblocks * block_size;
637         nb_samples         = nblocks * 4096;
638         if (bits_left >= block_hdr_size)
639             nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
640         break;
641     }
642     case AV_CODEC_ID_ADPCM_THP:
643     case AV_CODEC_ID_ADPCM_THP_LE:
644         if (avctx->extradata) {
645             nb_samples = buf_size * 14 / (8 * ch);
646             break;
647         }
648         has_coded_samples = 1;
649         bytestream2_skip(gb, 4); // channel size
650         *coded_samples  = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
651                           bytestream2_get_le32(gb) :
652                           bytestream2_get_be32(gb);
653         buf_size       -= 8 + 36 * ch;
654         buf_size       /= ch;
655         nb_samples      = buf_size / 8 * 14;
656         if (buf_size % 8 > 1) {
657             nb_samples     += (buf_size % 8 - 1) * 2;
658             if (*coded_samples & 1)
659                 nb_samples -= 1;
660         }
661         break;
662     case AV_CODEC_ID_ADPCM_AFC:
663         nb_samples = buf_size / (9 * ch) * 16;
664         break;
665     case AV_CODEC_ID_ADPCM_XA:
666         nb_samples = (buf_size / 128) * 224 / ch;
667         break;
668     case AV_CODEC_ID_ADPCM_DTK:
669         nb_samples = buf_size / (16 * ch) * 28;
670         break;
671     }
672
673     /* validate coded sample count */
674     if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
675         return AVERROR_INVALIDDATA;
676
677     return nb_samples;
678 }
679
680 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
681                               int *got_frame_ptr, AVPacket *avpkt)
682 {
683     AVFrame *frame     = data;
684     const uint8_t *buf = avpkt->data;
685     int buf_size = avpkt->size;
686     ADPCMDecodeContext *c = avctx->priv_data;
687     ADPCMChannelStatus *cs;
688     int n, m, channel, i;
689     short *samples;
690     int16_t **samples_p;
691     int st; /* stereo */
692     int count1, count2;
693     int nb_samples, coded_samples, approx_nb_samples, ret;
694     GetByteContext gb;
695
696     bytestream2_init(&gb, buf, buf_size);
697     nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
698     if (nb_samples <= 0) {
699         av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
700         return AVERROR_INVALIDDATA;
701     }
702
703     /* get output buffer */
704     frame->nb_samples = nb_samples;
705     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
706         return ret;
707     samples = (short *)frame->data[0];
708     samples_p = (int16_t **)frame->extended_data;
709
710     /* use coded_samples when applicable */
711     /* it is always <= nb_samples, so the output buffer will be large enough */
712     if (coded_samples) {
713         if (!approx_nb_samples && coded_samples != nb_samples)
714             av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
715         frame->nb_samples = nb_samples = coded_samples;
716     }
717
718     st = avctx->channels == 2 ? 1 : 0;
719
720     switch(avctx->codec->id) {
721     case AV_CODEC_ID_ADPCM_IMA_QT:
722         /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
723            Channel data is interleaved per-chunk. */
724         for (channel = 0; channel < avctx->channels; channel++) {
725             int predictor;
726             int step_index;
727             cs = &(c->status[channel]);
728             /* (pppppp) (piiiiiii) */
729
730             /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
731             predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
732             step_index = predictor & 0x7F;
733             predictor &= ~0x7F;
734
735             if (cs->step_index == step_index) {
736                 int diff = predictor - cs->predictor;
737                 if (diff < 0)
738                     diff = - diff;
739                 if (diff > 0x7f)
740                     goto update;
741             } else {
742             update:
743                 cs->step_index = step_index;
744                 cs->predictor = predictor;
745             }
746
747             if (cs->step_index > 88u){
748                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
749                        channel, cs->step_index);
750                 return AVERROR_INVALIDDATA;
751             }
752
753             samples = samples_p[channel];
754
755             for (m = 0; m < 64; m += 2) {
756                 int byte = bytestream2_get_byteu(&gb);
757                 samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
758                 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  , 3);
759             }
760         }
761         break;
762     case AV_CODEC_ID_ADPCM_IMA_WAV:
763         for(i=0; i<avctx->channels; i++){
764             cs = &(c->status[i]);
765             cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
766
767             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
768             if (cs->step_index > 88u){
769                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
770                        i, cs->step_index);
771                 return AVERROR_INVALIDDATA;
772             }
773         }
774
775         if (avctx->bits_per_coded_sample != 4) {
776             int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
777             GetBitContext g;
778
779             init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
780             for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
781                 for (i = 0; i < avctx->channels; i++) {
782                     cs = &c->status[i];
783                     samples = &samples_p[i][1 + n * samples_per_block];
784                     for (m = 0; m < samples_per_block; m++) {
785                         samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
786                                           avctx->bits_per_coded_sample);
787                     }
788                 }
789             }
790             bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
791         } else {
792         for (n = 0; n < (nb_samples - 1) / 8; n++) {
793             for (i = 0; i < avctx->channels; i++) {
794                 cs = &c->status[i];
795                 samples = &samples_p[i][1 + n * 8];
796                 for (m = 0; m < 8; m += 2) {
797                     int v = bytestream2_get_byteu(&gb);
798                     samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
799                     samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
800                 }
801             }
802         }
803         }
804         break;
805     case AV_CODEC_ID_ADPCM_4XM:
806         for (i = 0; i < avctx->channels; i++)
807             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
808
809         for (i = 0; i < avctx->channels; i++) {
810             c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
811             if (c->status[i].step_index > 88u) {
812                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
813                        i, c->status[i].step_index);
814                 return AVERROR_INVALIDDATA;
815             }
816         }
817
818         for (i = 0; i < avctx->channels; i++) {
819             samples = (int16_t *)frame->data[i];
820             cs = &c->status[i];
821             for (n = nb_samples >> 1; n > 0; n--) {
822                 int v = bytestream2_get_byteu(&gb);
823                 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
824                 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
825             }
826         }
827         break;
828     case AV_CODEC_ID_ADPCM_MS:
829     {
830         int block_predictor;
831
832         block_predictor = bytestream2_get_byteu(&gb);
833         if (block_predictor > 6) {
834             av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
835                    block_predictor);
836             return AVERROR_INVALIDDATA;
837         }
838         c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
839         c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
840         if (st) {
841             block_predictor = bytestream2_get_byteu(&gb);
842             if (block_predictor > 6) {
843                 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
844                        block_predictor);
845                 return AVERROR_INVALIDDATA;
846             }
847             c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
848             c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
849         }
850         c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
851         if (st){
852             c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
853         }
854
855         c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
856         if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
857         c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
858         if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
859
860         *samples++ = c->status[0].sample2;
861         if (st) *samples++ = c->status[1].sample2;
862         *samples++ = c->status[0].sample1;
863         if (st) *samples++ = c->status[1].sample1;
864         for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
865             int byte = bytestream2_get_byteu(&gb);
866             *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
867             *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
868         }
869         break;
870     }
871     case AV_CODEC_ID_ADPCM_IMA_DK4:
872         for (channel = 0; channel < avctx->channels; channel++) {
873             cs = &c->status[channel];
874             cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
875             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
876             if (cs->step_index > 88u){
877                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
878                        channel, cs->step_index);
879                 return AVERROR_INVALIDDATA;
880             }
881         }
882         for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
883             int v = bytestream2_get_byteu(&gb);
884             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
885             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
886         }
887         break;
888     case AV_CODEC_ID_ADPCM_IMA_DK3:
889     {
890         int last_byte = 0;
891         int nibble;
892         int decode_top_nibble_next = 0;
893         int diff_channel;
894         const int16_t *samples_end = samples + avctx->channels * nb_samples;
895
896         bytestream2_skipu(&gb, 10);
897         c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
898         c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
899         c->status[0].step_index = bytestream2_get_byteu(&gb);
900         c->status[1].step_index = bytestream2_get_byteu(&gb);
901         if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
902             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
903                    c->status[0].step_index, c->status[1].step_index);
904             return AVERROR_INVALIDDATA;
905         }
906         /* sign extend the predictors */
907         diff_channel = c->status[1].predictor;
908
909         /* DK3 ADPCM support macro */
910 #define DK3_GET_NEXT_NIBBLE() \
911     if (decode_top_nibble_next) { \
912         nibble = last_byte >> 4; \
913         decode_top_nibble_next = 0; \
914     } else { \
915         last_byte = bytestream2_get_byteu(&gb); \
916         nibble = last_byte & 0x0F; \
917         decode_top_nibble_next = 1; \
918     }
919
920         while (samples < samples_end) {
921
922             /* for this algorithm, c->status[0] is the sum channel and
923              * c->status[1] is the diff channel */
924
925             /* process the first predictor of the sum channel */
926             DK3_GET_NEXT_NIBBLE();
927             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
928
929             /* process the diff channel predictor */
930             DK3_GET_NEXT_NIBBLE();
931             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
932
933             /* process the first pair of stereo PCM samples */
934             diff_channel = (diff_channel + c->status[1].predictor) / 2;
935             *samples++ = c->status[0].predictor + c->status[1].predictor;
936             *samples++ = c->status[0].predictor - c->status[1].predictor;
937
938             /* process the second predictor of the sum channel */
939             DK3_GET_NEXT_NIBBLE();
940             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
941
942             /* process the second pair of stereo PCM samples */
943             diff_channel = (diff_channel + c->status[1].predictor) / 2;
944             *samples++ = c->status[0].predictor + c->status[1].predictor;
945             *samples++ = c->status[0].predictor - c->status[1].predictor;
946         }
947
948         if ((bytestream2_tell(&gb) & 1))
949             bytestream2_skip(&gb, 1);
950         break;
951     }
952     case AV_CODEC_ID_ADPCM_IMA_ISS:
953         for (channel = 0; channel < avctx->channels; channel++) {
954             cs = &c->status[channel];
955             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
956             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
957             if (cs->step_index > 88u){
958                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
959                        channel, cs->step_index);
960                 return AVERROR_INVALIDDATA;
961             }
962         }
963
964         for (n = nb_samples >> (1 - st); n > 0; n--) {
965             int v1, v2;
966             int v = bytestream2_get_byteu(&gb);
967             /* nibbles are swapped for mono */
968             if (st) {
969                 v1 = v >> 4;
970                 v2 = v & 0x0F;
971             } else {
972                 v2 = v >> 4;
973                 v1 = v & 0x0F;
974             }
975             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
976             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
977         }
978         break;
979     case AV_CODEC_ID_ADPCM_IMA_APC:
980         while (bytestream2_get_bytes_left(&gb) > 0) {
981             int v = bytestream2_get_byteu(&gb);
982             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
983             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
984         }
985         break;
986     case AV_CODEC_ID_ADPCM_IMA_OKI:
987         while (bytestream2_get_bytes_left(&gb) > 0) {
988             int v = bytestream2_get_byteu(&gb);
989             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );
990             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
991         }
992         break;
993     case AV_CODEC_ID_ADPCM_IMA_RAD:
994         for (channel = 0; channel < avctx->channels; channel++) {
995             cs = &c->status[channel];
996             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
997             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
998             if (cs->step_index > 88u){
999                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1000                        channel, cs->step_index);
1001                 return AVERROR_INVALIDDATA;
1002             }
1003         }
1004         for (n = 0; n < nb_samples / 2; n++) {
1005             int byte[2];
1006
1007             byte[0] = bytestream2_get_byteu(&gb);
1008             if (st)
1009                 byte[1] = bytestream2_get_byteu(&gb);
1010             for(channel = 0; channel < avctx->channels; channel++) {
1011                 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1012             }
1013             for(channel = 0; channel < avctx->channels; channel++) {
1014                 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4  , 3);
1015             }
1016         }
1017         break;
1018     case AV_CODEC_ID_ADPCM_IMA_WS:
1019         if (c->vqa_version == 3) {
1020             for (channel = 0; channel < avctx->channels; channel++) {
1021                 int16_t *smp = samples_p[channel];
1022
1023                 for (n = nb_samples / 2; n > 0; n--) {
1024                     int v = bytestream2_get_byteu(&gb);
1025                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1026                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1027                 }
1028             }
1029         } else {
1030             for (n = nb_samples / 2; n > 0; n--) {
1031                 for (channel = 0; channel < avctx->channels; channel++) {
1032                     int v = bytestream2_get_byteu(&gb);
1033                     *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1034                     samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1035                 }
1036                 samples += avctx->channels;
1037             }
1038         }
1039         bytestream2_seek(&gb, 0, SEEK_END);
1040         break;
1041     case AV_CODEC_ID_ADPCM_XA:
1042     {
1043         int16_t *out0 = samples_p[0];
1044         int16_t *out1 = samples_p[1];
1045         int samples_per_block = 28 * (3 - avctx->channels) * 4;
1046         int sample_offset = 0;
1047         while (bytestream2_get_bytes_left(&gb) >= 128) {
1048             if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1049                                  &c->status[0], &c->status[1],
1050                                  avctx->channels, sample_offset)) < 0)
1051                 return ret;
1052             bytestream2_skipu(&gb, 128);
1053             sample_offset += samples_per_block;
1054         }
1055         break;
1056     }
1057     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1058         for (i=0; i<=st; i++) {
1059             c->status[i].step_index = bytestream2_get_le32u(&gb);
1060             if (c->status[i].step_index > 88u) {
1061                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1062                        i, c->status[i].step_index);
1063                 return AVERROR_INVALIDDATA;
1064             }
1065         }
1066         for (i=0; i<=st; i++)
1067             c->status[i].predictor  = bytestream2_get_le32u(&gb);
1068
1069         for (n = nb_samples >> (1 - st); n > 0; n--) {
1070             int byte   = bytestream2_get_byteu(&gb);
1071             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
1072             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1073         }
1074         break;
1075     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1076         for (n = nb_samples >> (1 - st); n > 0; n--) {
1077             int byte = bytestream2_get_byteu(&gb);
1078             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
1079             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1080         }
1081         break;
1082     case AV_CODEC_ID_ADPCM_EA:
1083     {
1084         int previous_left_sample, previous_right_sample;
1085         int current_left_sample, current_right_sample;
1086         int next_left_sample, next_right_sample;
1087         int coeff1l, coeff2l, coeff1r, coeff2r;
1088         int shift_left, shift_right;
1089
1090         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1091            each coding 28 stereo samples. */
1092
1093         if(avctx->channels != 2)
1094             return AVERROR_INVALIDDATA;
1095
1096         current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
1097         previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1098         current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1099         previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1100
1101         for (count1 = 0; count1 < nb_samples / 28; count1++) {
1102             int byte = bytestream2_get_byteu(&gb);
1103             coeff1l = ea_adpcm_table[ byte >> 4       ];
1104             coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
1105             coeff1r = ea_adpcm_table[ byte & 0x0F];
1106             coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1107
1108             byte = bytestream2_get_byteu(&gb);
1109             shift_left  = 20 - (byte >> 4);
1110             shift_right = 20 - (byte & 0x0F);
1111
1112             for (count2 = 0; count2 < 28; count2++) {
1113                 byte = bytestream2_get_byteu(&gb);
1114                 next_left_sample  = sign_extend(byte >> 4, 4) << shift_left;
1115                 next_right_sample = sign_extend(byte,      4) << shift_right;
1116
1117                 next_left_sample = (next_left_sample +
1118                     (current_left_sample * coeff1l) +
1119                     (previous_left_sample * coeff2l) + 0x80) >> 8;
1120                 next_right_sample = (next_right_sample +
1121                     (current_right_sample * coeff1r) +
1122                     (previous_right_sample * coeff2r) + 0x80) >> 8;
1123
1124                 previous_left_sample = current_left_sample;
1125                 current_left_sample = av_clip_int16(next_left_sample);
1126                 previous_right_sample = current_right_sample;
1127                 current_right_sample = av_clip_int16(next_right_sample);
1128                 *samples++ = current_left_sample;
1129                 *samples++ = current_right_sample;
1130             }
1131         }
1132
1133         bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1134
1135         break;
1136     }
1137     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1138     {
1139         int coeff[2][2], shift[2];
1140
1141         for(channel = 0; channel < avctx->channels; channel++) {
1142             int byte = bytestream2_get_byteu(&gb);
1143             for (i=0; i<2; i++)
1144                 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1145             shift[channel] = 20 - (byte & 0x0F);
1146         }
1147         for (count1 = 0; count1 < nb_samples / 2; count1++) {
1148             int byte[2];
1149
1150             byte[0] = bytestream2_get_byteu(&gb);
1151             if (st) byte[1] = bytestream2_get_byteu(&gb);
1152             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1153                 for(channel = 0; channel < avctx->channels; channel++) {
1154                     int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1155                     sample = (sample +
1156                              c->status[channel].sample1 * coeff[channel][0] +
1157                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1158                     c->status[channel].sample2 = c->status[channel].sample1;
1159                     c->status[channel].sample1 = av_clip_int16(sample);
1160                     *samples++ = c->status[channel].sample1;
1161                 }
1162             }
1163         }
1164         bytestream2_seek(&gb, 0, SEEK_END);
1165         break;
1166     }
1167     case AV_CODEC_ID_ADPCM_EA_R1:
1168     case AV_CODEC_ID_ADPCM_EA_R2:
1169     case AV_CODEC_ID_ADPCM_EA_R3: {
1170         /* channel numbering
1171            2chan: 0=fl, 1=fr
1172            4chan: 0=fl, 1=rl, 2=fr, 3=rr
1173            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1174         const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1175         int previous_sample, current_sample, next_sample;
1176         int coeff1, coeff2;
1177         int shift;
1178         unsigned int channel;
1179         uint16_t *samplesC;
1180         int count = 0;
1181         int offsets[6];
1182
1183         for (channel=0; channel<avctx->channels; channel++)
1184             offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1185                                              bytestream2_get_le32(&gb)) +
1186                                (avctx->channels + 1) * 4;
1187
1188         for (channel=0; channel<avctx->channels; channel++) {
1189             bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1190             samplesC = samples_p[channel];
1191
1192             if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1193                 current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
1194                 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1195             } else {
1196                 current_sample  = c->status[channel].predictor;
1197                 previous_sample = c->status[channel].prev_sample;
1198             }
1199
1200             for (count1 = 0; count1 < nb_samples / 28; count1++) {
1201                 int byte = bytestream2_get_byte(&gb);
1202                 if (byte == 0xEE) {  /* only seen in R2 and R3 */
1203                     current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
1204                     previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1205
1206                     for (count2=0; count2<28; count2++)
1207                         *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1208                 } else {
1209                     coeff1 = ea_adpcm_table[ byte >> 4     ];
1210                     coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1211                     shift = 20 - (byte & 0x0F);
1212
1213                     for (count2=0; count2<28; count2++) {
1214                         if (count2 & 1)
1215                             next_sample = sign_extend(byte,    4) << shift;
1216                         else {
1217                             byte = bytestream2_get_byte(&gb);
1218                             next_sample = sign_extend(byte >> 4, 4) << shift;
1219                         }
1220
1221                         next_sample += (current_sample  * coeff1) +
1222                                        (previous_sample * coeff2);
1223                         next_sample = av_clip_int16(next_sample >> 8);
1224
1225                         previous_sample = current_sample;
1226                         current_sample  = next_sample;
1227                         *samplesC++ = current_sample;
1228                     }
1229                 }
1230             }
1231             if (!count) {
1232                 count = count1;
1233             } else if (count != count1) {
1234                 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1235                 count = FFMAX(count, count1);
1236             }
1237
1238             if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1239                 c->status[channel].predictor   = current_sample;
1240                 c->status[channel].prev_sample = previous_sample;
1241             }
1242         }
1243
1244         frame->nb_samples = count * 28;
1245         bytestream2_seek(&gb, 0, SEEK_END);
1246         break;
1247     }
1248     case AV_CODEC_ID_ADPCM_EA_XAS:
1249         for (channel=0; channel<avctx->channels; channel++) {
1250             int coeff[2][4], shift[4];
1251             int16_t *s = samples_p[channel];
1252             for (n = 0; n < 4; n++, s += 32) {
1253                 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1254                 for (i=0; i<2; i++)
1255                     coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1256                 s[0] = val & ~0x0F;
1257
1258                 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1259                 shift[n] = 20 - (val & 0x0F);
1260                 s[1] = val & ~0x0F;
1261             }
1262
1263             for (m=2; m<32; m+=2) {
1264                 s = &samples_p[channel][m];
1265                 for (n = 0; n < 4; n++, s += 32) {
1266                     int level, pred;
1267                     int byte = bytestream2_get_byteu(&gb);
1268
1269                     level = sign_extend(byte >> 4, 4) << shift[n];
1270                     pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1271                     s[0]  = av_clip_int16((level + pred + 0x80) >> 8);
1272
1273                     level = sign_extend(byte, 4) << shift[n];
1274                     pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1275                     s[1]  = av_clip_int16((level + pred + 0x80) >> 8);
1276                 }
1277             }
1278         }
1279         break;
1280     case AV_CODEC_ID_ADPCM_IMA_AMV:
1281         c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1282         c->status[0].step_index = bytestream2_get_le16u(&gb);
1283         bytestream2_skipu(&gb, 4);
1284         if (c->status[0].step_index > 88u) {
1285             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1286                    c->status[0].step_index);
1287             return AVERROR_INVALIDDATA;
1288         }
1289
1290         for (n = nb_samples >> (1 - st); n > 0; n--) {
1291             int v = bytestream2_get_byteu(&gb);
1292
1293             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1294             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1295         }
1296         break;
1297     case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1298         for (i = 0; i < avctx->channels; i++) {
1299             c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1300             c->status[i].step_index = bytestream2_get_byteu(&gb);
1301             bytestream2_skipu(&gb, 1);
1302             if (c->status[i].step_index > 88u) {
1303                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1304                        c->status[i].step_index);
1305                 return AVERROR_INVALIDDATA;
1306             }
1307         }
1308
1309         for (n = nb_samples >> (1 - st); n > 0; n--) {
1310             int v = bytestream2_get_byteu(&gb);
1311
1312             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1313             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1314         }
1315         break;
1316     case AV_CODEC_ID_ADPCM_CT:
1317         for (n = nb_samples >> (1 - st); n > 0; n--) {
1318             int v = bytestream2_get_byteu(&gb);
1319             *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
1320             *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1321         }
1322         break;
1323     case AV_CODEC_ID_ADPCM_SBPRO_4:
1324     case AV_CODEC_ID_ADPCM_SBPRO_3:
1325     case AV_CODEC_ID_ADPCM_SBPRO_2:
1326         if (!c->status[0].step_index) {
1327             /* the first byte is a raw sample */
1328             *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1329             if (st)
1330                 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1331             c->status[0].step_index = 1;
1332             nb_samples--;
1333         }
1334         if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1335             for (n = nb_samples >> (1 - st); n > 0; n--) {
1336                 int byte = bytestream2_get_byteu(&gb);
1337                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1338                                                        byte >> 4,   4, 0);
1339                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1340                                                        byte & 0x0F, 4, 0);
1341             }
1342         } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1343             for (n = (nb_samples<<st) / 3; n > 0; n--) {
1344                 int byte = bytestream2_get_byteu(&gb);
1345                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1346                                                         byte >> 5        , 3, 0);
1347                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1348                                                        (byte >> 2) & 0x07, 3, 0);
1349                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1350                                                         byte & 0x03,       2, 0);
1351             }
1352         } else {
1353             for (n = nb_samples >> (2 - st); n > 0; n--) {
1354                 int byte = bytestream2_get_byteu(&gb);
1355                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1356                                                         byte >> 6        , 2, 2);
1357                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1358                                                        (byte >> 4) & 0x03, 2, 2);
1359                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1360                                                        (byte >> 2) & 0x03, 2, 2);
1361                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1362                                                         byte & 0x03,       2, 2);
1363             }
1364         }
1365         break;
1366     case AV_CODEC_ID_ADPCM_SWF:
1367         adpcm_swf_decode(avctx, buf, buf_size, samples);
1368         bytestream2_seek(&gb, 0, SEEK_END);
1369         break;
1370     case AV_CODEC_ID_ADPCM_YAMAHA:
1371         for (n = nb_samples >> (1 - st); n > 0; n--) {
1372             int v = bytestream2_get_byteu(&gb);
1373             *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1374             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1375         }
1376         break;
1377     case AV_CODEC_ID_ADPCM_AFC:
1378     {
1379         int samples_per_block;
1380         int blocks;
1381
1382         if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1383             samples_per_block = avctx->extradata[0] / 16;
1384             blocks = nb_samples / avctx->extradata[0];
1385         } else {
1386             samples_per_block = nb_samples / 16;
1387             blocks = 1;
1388         }
1389
1390         for (m = 0; m < blocks; m++) {
1391         for (channel = 0; channel < avctx->channels; channel++) {
1392             int prev1 = c->status[channel].sample1;
1393             int prev2 = c->status[channel].sample2;
1394
1395             samples = samples_p[channel] + m * 16;
1396             /* Read in every sample for this channel.  */
1397             for (i = 0; i < samples_per_block; i++) {
1398                 int byte = bytestream2_get_byteu(&gb);
1399                 int scale = 1 << (byte >> 4);
1400                 int index = byte & 0xf;
1401                 int factor1 = ff_adpcm_afc_coeffs[0][index];
1402                 int factor2 = ff_adpcm_afc_coeffs[1][index];
1403
1404                 /* Decode 16 samples.  */
1405                 for (n = 0; n < 16; n++) {
1406                     int32_t sampledat;
1407
1408                     if (n & 1) {
1409                         sampledat = sign_extend(byte, 4);
1410                     } else {
1411                         byte = bytestream2_get_byteu(&gb);
1412                         sampledat = sign_extend(byte >> 4, 4);
1413                     }
1414
1415                     sampledat = ((prev1 * factor1 + prev2 * factor2) +
1416                                  ((sampledat * scale) << 11)) >> 11;
1417                     *samples = av_clip_int16(sampledat);
1418                     prev2 = prev1;
1419                     prev1 = *samples++;
1420                 }
1421             }
1422
1423             c->status[channel].sample1 = prev1;
1424             c->status[channel].sample2 = prev2;
1425         }
1426         }
1427         bytestream2_seek(&gb, 0, SEEK_END);
1428         break;
1429     }
1430     case AV_CODEC_ID_ADPCM_THP:
1431     case AV_CODEC_ID_ADPCM_THP_LE:
1432     {
1433         int table[10][16];
1434         int ch;
1435
1436 #define THP_GET16(g) \
1437     sign_extend( \
1438         avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1439         bytestream2_get_le16u(&(g)) : \
1440         bytestream2_get_be16u(&(g)), 16)
1441
1442         if (avctx->extradata) {
1443             GetByteContext tb;
1444             if (avctx->extradata_size < 32 * avctx->channels) {
1445                 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1446                 return AVERROR_INVALIDDATA;
1447             }
1448
1449             bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1450             for (i = 0; i < avctx->channels; i++)
1451                 for (n = 0; n < 16; n++)
1452                     table[i][n] = THP_GET16(tb);
1453         } else {
1454             for (i = 0; i < avctx->channels; i++)
1455                 for (n = 0; n < 16; n++)
1456                     table[i][n] = THP_GET16(gb);
1457
1458             /* Initialize the previous sample.  */
1459             for (i = 0; i < avctx->channels; i++) {
1460                 c->status[i].sample1 = THP_GET16(gb);
1461                 c->status[i].sample2 = THP_GET16(gb);
1462             }
1463         }
1464
1465         for (ch = 0; ch < avctx->channels; ch++) {
1466             samples = samples_p[ch];
1467
1468             /* Read in every sample for this channel.  */
1469             for (i = 0; i < (nb_samples + 13) / 14; i++) {
1470                 int byte = bytestream2_get_byteu(&gb);
1471                 int index = (byte >> 4) & 7;
1472                 unsigned int exp = byte & 0x0F;
1473                 int factor1 = table[ch][index * 2];
1474                 int factor2 = table[ch][index * 2 + 1];
1475
1476                 /* Decode 14 samples.  */
1477                 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1478                     int32_t sampledat;
1479
1480                     if (n & 1) {
1481                         sampledat = sign_extend(byte, 4);
1482                     } else {
1483                         byte = bytestream2_get_byteu(&gb);
1484                         sampledat = sign_extend(byte >> 4, 4);
1485                     }
1486
1487                     sampledat = ((c->status[ch].sample1 * factor1
1488                                 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1489                     *samples = av_clip_int16(sampledat);
1490                     c->status[ch].sample2 = c->status[ch].sample1;
1491                     c->status[ch].sample1 = *samples++;
1492                 }
1493             }
1494         }
1495         break;
1496     }
1497     case AV_CODEC_ID_ADPCM_DTK:
1498         for (channel = 0; channel < avctx->channels; channel++) {
1499             samples = samples_p[channel];
1500
1501             /* Read in every sample for this channel.  */
1502             for (i = 0; i < nb_samples / 28; i++) {
1503                 int byte, header;
1504                 if (channel)
1505                     bytestream2_skipu(&gb, 1);
1506                 header = bytestream2_get_byteu(&gb);
1507                 bytestream2_skipu(&gb, 3 - channel);
1508
1509                 /* Decode 28 samples.  */
1510                 for (n = 0; n < 28; n++) {
1511                     int32_t sampledat, prev;
1512
1513                     switch (header >> 4) {
1514                     case 1:
1515                         prev = (c->status[channel].sample1 * 0x3c);
1516                         break;
1517                     case 2:
1518                         prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1519                         break;
1520                     case 3:
1521                         prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1522                         break;
1523                     default:
1524                         prev = 0;
1525                     }
1526
1527                     prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1528
1529                     byte = bytestream2_get_byteu(&gb);
1530                     if (!channel)
1531                         sampledat = sign_extend(byte, 4);
1532                     else
1533                         sampledat = sign_extend(byte >> 4, 4);
1534
1535                     sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1536                     *samples++ = av_clip_int16(sampledat >> 6);
1537                     c->status[channel].sample2 = c->status[channel].sample1;
1538                     c->status[channel].sample1 = sampledat;
1539                 }
1540             }
1541             if (!channel)
1542                 bytestream2_seek(&gb, 0, SEEK_SET);
1543         }
1544         break;
1545
1546     default:
1547         return -1;
1548     }
1549
1550     if (avpkt->size && bytestream2_tell(&gb) == 0) {
1551         av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1552         return AVERROR_INVALIDDATA;
1553     }
1554
1555     *got_frame_ptr = 1;
1556
1557     if (avpkt->size < bytestream2_tell(&gb)) {
1558         av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1559         return avpkt->size;
1560     }
1561
1562     return bytestream2_tell(&gb);
1563 }
1564
1565
1566 static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
1567                                                         AV_SAMPLE_FMT_NONE };
1568 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1569                                                         AV_SAMPLE_FMT_NONE };
1570 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1571                                                         AV_SAMPLE_FMT_S16P,
1572                                                         AV_SAMPLE_FMT_NONE };
1573
1574 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1575 AVCodec ff_ ## name_ ## _decoder = {                        \
1576     .name           = #name_,                               \
1577     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
1578     .type           = AVMEDIA_TYPE_AUDIO,                   \
1579     .id             = id_,                                  \
1580     .priv_data_size = sizeof(ADPCMDecodeContext),           \
1581     .init           = adpcm_decode_init,                    \
1582     .decode         = adpcm_decode_frame,                   \
1583     .capabilities   = CODEC_CAP_DR1,                        \
1584     .sample_fmts    = sample_fmts_,                         \
1585 }
1586
1587 /* Note: Do not forget to add new entries to the Makefile as well. */
1588 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
1589 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
1590 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
1591 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
1592 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
1593 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1594 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
1595 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
1596 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
1597 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
1598 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
1599 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
1600 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
1601 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
1602 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1603 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1604 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
1605 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
1606 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
1607 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD,     sample_fmts_s16,  adpcm_ima_rad,     "ADPCM IMA Radical");
1608 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
1609 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
1610 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
1611 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_s16,  adpcm_ms,          "ADPCM Microsoft");
1612 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
1613 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
1614 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
1615 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
1616 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE,      sample_fmts_s16p, adpcm_thp_le,      "ADPCM Nintendo Gamecube THP (little-endian)");
1617 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo Gamecube THP");
1618 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
1619 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");