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