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