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