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