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