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