]> git.sesse.net Git - ffmpeg/blob - libavcodec/adpcm.c
lavc: remove unused put_bits.h headers
[ffmpeg] / libavcodec / adpcm.c
1 /*
2  * Copyright (c) 2001-2003 The ffmpeg Project
3  *
4  * first version by Francois Revol (revol@free.fr)
5  * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6  *   by Mike Melanson (melanson@pcisys.net)
7  * CD-ROM XA ADPCM codec by BERO
8  * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9  * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10  * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11  * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12  * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13  * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14  * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
15  *
16  * This file is part of FFmpeg.
17  *
18  * FFmpeg is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * FFmpeg is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public
29  * License along with FFmpeg; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31  */
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "bytestream.h"
35 #include "adpcm.h"
36 #include "adpcm_data.h"
37 #include "internal.h"
38
39 /**
40  * @file
41  * ADPCM decoders
42  * Features and limitations:
43  *
44  * Reference documents:
45  * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46  * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47  * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48  * http://openquicktime.sourceforge.net/
49  * XAnim sources (xa_codec.c) http://xanim.polter.net/
50  * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51  * SoX source code http://sox.sourceforge.net/
52  *
53  * CD-ROM XA:
54  * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55  * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56  * readstr http://www.geocities.co.jp/Playtown/2004/
57  */
58
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table[5][2] = {
61     {   0,   0 },
62     {  60,   0 },
63     { 115, -52 },
64     {  98, -55 },
65     { 122, -60 }
66 };
67
68 static const int ea_adpcm_table[] = {
69     0,  240,  460,  392,
70     0,    0, -208, -220,
71     0,    1,    3,    4,
72     7,    8,   10,   11,
73     0,   -1,   -3,   -4
74 };
75
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables[4][16] = {
78     /*2*/ { -1, 2 },
79     /*3*/ { -1, -1, 2, 4 },
80     /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81     /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
82 };
83
84 /* end of tables */
85
86 typedef struct ADPCMDecodeContext {
87     ADPCMChannelStatus status[6];
88     int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */
89 } ADPCMDecodeContext;
90
91 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
92 {
93     ADPCMDecodeContext *c = avctx->priv_data;
94     unsigned int min_channels = 1;
95     unsigned int max_channels = 2;
96
97     switch(avctx->codec->id) {
98     case AV_CODEC_ID_ADPCM_EA:
99         min_channels = 2;
100         break;
101     case AV_CODEC_ID_ADPCM_AFC:
102     case AV_CODEC_ID_ADPCM_EA_R1:
103     case AV_CODEC_ID_ADPCM_EA_R2:
104     case AV_CODEC_ID_ADPCM_EA_R3:
105     case AV_CODEC_ID_ADPCM_EA_XAS:
106     case AV_CODEC_ID_ADPCM_THP:
107         max_channels = 6;
108         break;
109     }
110     if (avctx->channels < min_channels || avctx->channels > max_channels) {
111         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
112         return AVERROR(EINVAL);
113     }
114
115     switch(avctx->codec->id) {
116     case AV_CODEC_ID_ADPCM_CT:
117         c->status[0].step = c->status[1].step = 511;
118         break;
119     case AV_CODEC_ID_ADPCM_IMA_WAV:
120         if (avctx->bits_per_coded_sample != 4) {
121             av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
122             return -1;
123         }
124         break;
125     case AV_CODEC_ID_ADPCM_IMA_APC:
126         if (avctx->extradata && avctx->extradata_size >= 8) {
127             c->status[0].predictor = AV_RL32(avctx->extradata);
128             c->status[1].predictor = AV_RL32(avctx->extradata + 4);
129         }
130         break;
131     case AV_CODEC_ID_ADPCM_IMA_WS:
132         if (avctx->extradata && avctx->extradata_size >= 2)
133             c->vqa_version = AV_RL16(avctx->extradata);
134         break;
135     default:
136         break;
137     }
138
139     switch(avctx->codec->id) {
140         case AV_CODEC_ID_ADPCM_IMA_QT:
141         case AV_CODEC_ID_ADPCM_IMA_WAV:
142         case AV_CODEC_ID_ADPCM_4XM:
143         case AV_CODEC_ID_ADPCM_XA:
144         case AV_CODEC_ID_ADPCM_EA_R1:
145         case AV_CODEC_ID_ADPCM_EA_R2:
146         case AV_CODEC_ID_ADPCM_EA_R3:
147         case AV_CODEC_ID_ADPCM_EA_XAS:
148         case AV_CODEC_ID_ADPCM_THP:
149         case AV_CODEC_ID_ADPCM_AFC:
150             avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
151             break;
152         case AV_CODEC_ID_ADPCM_IMA_WS:
153             avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
154                                                       AV_SAMPLE_FMT_S16;
155             break;
156         default:
157             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
158     }
159
160     return 0;
161 }
162
163 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
164 {
165     int step_index;
166     int predictor;
167     int sign, delta, diff, step;
168
169     step = ff_adpcm_step_table[c->step_index];
170     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
171     step_index = av_clip(step_index, 0, 88);
172
173     sign = nibble & 8;
174     delta = nibble & 7;
175     /* perform direct multiplication instead of series of jumps proposed by
176      * the reference ADPCM implementation since modern CPUs can do the mults
177      * quickly enough */
178     diff = ((2 * delta + 1) * step) >> shift;
179     predictor = c->predictor;
180     if (sign) predictor -= diff;
181     else predictor += diff;
182
183     c->predictor = av_clip_int16(predictor);
184     c->step_index = step_index;
185
186     return (short)c->predictor;
187 }
188
189 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
190 {
191     int step_index;
192     int predictor;
193     int diff, step;
194
195     step = ff_adpcm_step_table[c->step_index];
196     step_index = c->step_index + ff_adpcm_index_table[nibble];
197     step_index = av_clip(step_index, 0, 88);
198
199     diff = step >> 3;
200     if (nibble & 4) diff += step;
201     if (nibble & 2) diff += step >> 1;
202     if (nibble & 1) diff += step >> 2;
203
204     if (nibble & 8)
205         predictor = c->predictor - diff;
206     else
207         predictor = c->predictor + diff;
208
209     c->predictor = av_clip_int16(predictor);
210     c->step_index = step_index;
211
212     return c->predictor;
213 }
214
215 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
216 {
217     int predictor;
218
219     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
220     predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
221
222     c->sample2 = c->sample1;
223     c->sample1 = av_clip_int16(predictor);
224     c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
225     if (c->idelta < 16) c->idelta = 16;
226
227     return c->sample1;
228 }
229
230 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
231 {
232     int step_index, predictor, sign, delta, diff, step;
233
234     step = ff_adpcm_oki_step_table[c->step_index];
235     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
236     step_index = av_clip(step_index, 0, 48);
237
238     sign = nibble & 8;
239     delta = nibble & 7;
240     diff = ((2 * delta + 1) * step) >> 3;
241     predictor = c->predictor;
242     if (sign) predictor -= diff;
243     else predictor += diff;
244
245     c->predictor = av_clip(predictor, -2048, 2047);
246     c->step_index = step_index;
247
248     return c->predictor << 4;
249 }
250
251 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
252 {
253     int sign, delta, diff;
254     int new_step;
255
256     sign = nibble & 8;
257     delta = nibble & 7;
258     /* perform direct multiplication instead of series of jumps proposed by
259      * the reference ADPCM implementation since modern CPUs can do the mults
260      * quickly enough */
261     diff = ((2 * delta + 1) * c->step) >> 3;
262     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
263     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
264     c->predictor = av_clip_int16(c->predictor);
265     /* calculate new step and clamp it to range 511..32767 */
266     new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
267     c->step = av_clip(new_step, 511, 32767);
268
269     return (short)c->predictor;
270 }
271
272 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
273 {
274     int sign, delta, diff;
275
276     sign = nibble & (1<<(size-1));
277     delta = nibble & ((1<<(size-1))-1);
278     diff = delta << (7 + c->step + shift);
279
280     /* clamp result */
281     c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
282
283     /* calculate new step */
284     if (delta >= (2*size - 3) && c->step < 3)
285         c->step++;
286     else if (delta == 0 && c->step > 0)
287         c->step--;
288
289     return (short) c->predictor;
290 }
291
292 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
293 {
294     if(!c->step) {
295         c->predictor = 0;
296         c->step = 127;
297     }
298
299     c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
300     c->predictor = av_clip_int16(c->predictor);
301     c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
302     c->step = av_clip(c->step, 127, 24567);
303     return c->predictor;
304 }
305
306 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
307                      const uint8_t *in, ADPCMChannelStatus *left,
308                      ADPCMChannelStatus *right, int channels, int sample_offset)
309 {
310     int i, j;
311     int shift,filter,f0,f1;
312     int s_1,s_2;
313     int d,s,t;
314
315     out0 += sample_offset;
316     if (channels == 1)
317         out1 = out0 + 28;
318     else
319         out1 += sample_offset;
320
321     for(i=0;i<4;i++) {
322         shift  = 12 - (in[4+i*2] & 15);
323         filter = in[4+i*2] >> 4;
324         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
325             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
326             filter=0;
327         }
328         f0 = xa_adpcm_table[filter][0];
329         f1 = xa_adpcm_table[filter][1];
330
331         s_1 = left->sample1;
332         s_2 = left->sample2;
333
334         for(j=0;j<28;j++) {
335             d = in[16+i+j*4];
336
337             t = sign_extend(d, 4);
338             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
339             s_2 = s_1;
340             s_1 = av_clip_int16(s);
341             out0[j] = s_1;
342         }
343
344         if (channels == 2) {
345             left->sample1 = s_1;
346             left->sample2 = s_2;
347             s_1 = right->sample1;
348             s_2 = right->sample2;
349         }
350
351         shift  = 12 - (in[5+i*2] & 15);
352         filter = in[5+i*2] >> 4;
353         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
354             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
355             filter=0;
356         }
357
358         f0 = xa_adpcm_table[filter][0];
359         f1 = xa_adpcm_table[filter][1];
360
361         for(j=0;j<28;j++) {
362             d = in[16+i+j*4];
363
364             t = sign_extend(d >> 4, 4);
365             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
366             s_2 = s_1;
367             s_1 = av_clip_int16(s);
368             out1[j] = s_1;
369         }
370
371         if (channels == 2) {
372             right->sample1 = s_1;
373             right->sample2 = s_2;
374         } else {
375             left->sample1 = s_1;
376             left->sample2 = s_2;
377         }
378
379         out0 += 28 * (3 - channels);
380         out1 += 28 * (3 - channels);
381     }
382
383     return 0;
384 }
385
386 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
387 {
388     ADPCMDecodeContext *c = avctx->priv_data;
389     GetBitContext gb;
390     const int *table;
391     int k0, signmask, nb_bits, count;
392     int size = buf_size*8;
393     int i;
394
395     init_get_bits(&gb, buf, size);
396
397     //read bits & initial values
398     nb_bits = get_bits(&gb, 2)+2;
399     table = swf_index_tables[nb_bits-2];
400     k0 = 1 << (nb_bits-2);
401     signmask = 1 << (nb_bits-1);
402
403     while (get_bits_count(&gb) <= size - 22*avctx->channels) {
404         for (i = 0; i < avctx->channels; i++) {
405             *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
406             c->status[i].step_index = get_bits(&gb, 6);
407         }
408
409         for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
410             int i;
411
412             for (i = 0; i < avctx->channels; i++) {
413                 // similar to IMA adpcm
414                 int delta = get_bits(&gb, nb_bits);
415                 int step = ff_adpcm_step_table[c->status[i].step_index];
416                 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
417                 int k = k0;
418
419                 do {
420                     if (delta & k)
421                         vpdiff += step;
422                     step >>= 1;
423                     k >>= 1;
424                 } while(k);
425                 vpdiff += step;
426
427                 if (delta & signmask)
428                     c->status[i].predictor -= vpdiff;
429                 else
430                     c->status[i].predictor += vpdiff;
431
432                 c->status[i].step_index += table[delta & (~signmask)];
433
434                 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
435                 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
436
437                 *samples++ = c->status[i].predictor;
438             }
439         }
440     }
441 }
442
443 /**
444  * Get the number of samples that will be decoded from the packet.
445  * In one case, this is actually the maximum number of samples possible to
446  * decode with the given buf_size.
447  *
448  * @param[out] coded_samples set to the number of samples as coded in the
449  *                           packet, or 0 if the codec does not encode the
450  *                           number of samples in each frame.
451  */
452 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
453                           int buf_size, int *coded_samples)
454 {
455     ADPCMDecodeContext *s = avctx->priv_data;
456     int nb_samples        = 0;
457     int ch                = avctx->channels;
458     int has_coded_samples = 0;
459     int header_size;
460
461     *coded_samples = 0;
462
463     if(ch <= 0)
464         return 0;
465
466     switch (avctx->codec->id) {
467     /* constant, only check buf_size */
468     case AV_CODEC_ID_ADPCM_EA_XAS:
469         if (buf_size < 76 * ch)
470             return 0;
471         nb_samples = 128;
472         break;
473     case AV_CODEC_ID_ADPCM_IMA_QT:
474         if (buf_size < 34 * ch)
475             return 0;
476         nb_samples = 64;
477         break;
478     /* simple 4-bit adpcm */
479     case AV_CODEC_ID_ADPCM_CT:
480     case AV_CODEC_ID_ADPCM_IMA_APC:
481     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
482     case AV_CODEC_ID_ADPCM_IMA_OKI:
483     case AV_CODEC_ID_ADPCM_IMA_WS:
484     case AV_CODEC_ID_ADPCM_YAMAHA:
485         nb_samples = buf_size * 2 / ch;
486         break;
487     }
488     if (nb_samples)
489         return nb_samples;
490
491     /* simple 4-bit adpcm, with header */
492     header_size = 0;
493     switch (avctx->codec->id) {
494         case AV_CODEC_ID_ADPCM_4XM:
495         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
496         case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
497         case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
498     }
499     if (header_size > 0)
500         return (buf_size - header_size) * 2 / ch;
501
502     /* more complex formats */
503     switch (avctx->codec->id) {
504     case AV_CODEC_ID_ADPCM_EA:
505         has_coded_samples = 1;
506         *coded_samples  = bytestream2_get_le32(gb);
507         *coded_samples -= *coded_samples % 28;
508         nb_samples      = (buf_size - 12) / 30 * 28;
509         break;
510     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
511         has_coded_samples = 1;
512         *coded_samples = bytestream2_get_le32(gb);
513         nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
514         break;
515     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
516         nb_samples = (buf_size - ch) / ch * 2;
517         break;
518     case AV_CODEC_ID_ADPCM_EA_R1:
519     case AV_CODEC_ID_ADPCM_EA_R2:
520     case AV_CODEC_ID_ADPCM_EA_R3:
521         /* maximum number of samples */
522         /* has internal offsets and a per-frame switch to signal raw 16-bit */
523         has_coded_samples = 1;
524         switch (avctx->codec->id) {
525         case AV_CODEC_ID_ADPCM_EA_R1:
526             header_size    = 4 + 9 * ch;
527             *coded_samples = bytestream2_get_le32(gb);
528             break;
529         case AV_CODEC_ID_ADPCM_EA_R2:
530             header_size    = 4 + 5 * ch;
531             *coded_samples = bytestream2_get_le32(gb);
532             break;
533         case AV_CODEC_ID_ADPCM_EA_R3:
534             header_size    = 4 + 5 * ch;
535             *coded_samples = bytestream2_get_be32(gb);
536             break;
537         }
538         *coded_samples -= *coded_samples % 28;
539         nb_samples      = (buf_size - header_size) * 2 / ch;
540         nb_samples     -= nb_samples % 28;
541         break;
542     case AV_CODEC_ID_ADPCM_IMA_DK3:
543         if (avctx->block_align > 0)
544             buf_size = FFMIN(buf_size, avctx->block_align);
545         nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
546         break;
547     case AV_CODEC_ID_ADPCM_IMA_DK4:
548         if (avctx->block_align > 0)
549             buf_size = FFMIN(buf_size, avctx->block_align);
550         nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
551         break;
552     case AV_CODEC_ID_ADPCM_IMA_WAV:
553         if (avctx->block_align > 0)
554             buf_size = FFMIN(buf_size, avctx->block_align);
555         nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
556         break;
557     case AV_CODEC_ID_ADPCM_MS:
558         if (avctx->block_align > 0)
559             buf_size = FFMIN(buf_size, avctx->block_align);
560         nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
561         break;
562     case AV_CODEC_ID_ADPCM_SBPRO_2:
563     case AV_CODEC_ID_ADPCM_SBPRO_3:
564     case AV_CODEC_ID_ADPCM_SBPRO_4:
565     {
566         int samples_per_byte;
567         switch (avctx->codec->id) {
568         case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
569         case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
570         case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
571         }
572         if (!s->status[0].step_index) {
573             nb_samples++;
574             buf_size -= ch;
575         }
576         nb_samples += buf_size * samples_per_byte / ch;
577         break;
578     }
579     case AV_CODEC_ID_ADPCM_SWF:
580     {
581         int buf_bits       = buf_size * 8 - 2;
582         int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
583         int block_hdr_size = 22 * ch;
584         int block_size     = block_hdr_size + nbits * ch * 4095;
585         int nblocks        = buf_bits / block_size;
586         int bits_left      = buf_bits - nblocks * block_size;
587         nb_samples         = nblocks * 4096;
588         if (bits_left >= block_hdr_size)
589             nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
590         break;
591     }
592     case AV_CODEC_ID_ADPCM_THP:
593         has_coded_samples = 1;
594         bytestream2_skip(gb, 4); // channel size
595         *coded_samples  = bytestream2_get_be32(gb);
596         *coded_samples -= *coded_samples % 14;
597         nb_samples      = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
598         break;
599     case AV_CODEC_ID_ADPCM_AFC:
600         nb_samples = buf_size / (9 * ch) * 16;
601         break;
602     case AV_CODEC_ID_ADPCM_XA:
603         nb_samples = (buf_size / 128) * 224 / ch;
604         break;
605     }
606
607     /* validate coded sample count */
608     if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
609         return AVERROR_INVALIDDATA;
610
611     return nb_samples;
612 }
613
614 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
615                               int *got_frame_ptr, AVPacket *avpkt)
616 {
617     AVFrame *frame     = data;
618     const uint8_t *buf = avpkt->data;
619     int buf_size = avpkt->size;
620     ADPCMDecodeContext *c = avctx->priv_data;
621     ADPCMChannelStatus *cs;
622     int n, m, channel, i;
623     short *samples;
624     int16_t **samples_p;
625     int st; /* stereo */
626     int count1, count2;
627     int nb_samples, coded_samples, ret;
628     GetByteContext gb;
629
630     bytestream2_init(&gb, buf, buf_size);
631     nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
632     if (nb_samples <= 0) {
633         av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
634         return AVERROR_INVALIDDATA;
635     }
636
637     /* get output buffer */
638     frame->nb_samples = nb_samples;
639     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
640         return ret;
641     samples = (short *)frame->data[0];
642     samples_p = (int16_t **)frame->extended_data;
643
644     /* use coded_samples when applicable */
645     /* it is always <= nb_samples, so the output buffer will be large enough */
646     if (coded_samples) {
647         if (coded_samples != nb_samples)
648             av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
649         frame->nb_samples = nb_samples = coded_samples;
650     }
651
652     st = avctx->channels == 2 ? 1 : 0;
653
654     switch(avctx->codec->id) {
655     case AV_CODEC_ID_ADPCM_IMA_QT:
656         /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
657            Channel data is interleaved per-chunk. */
658         for (channel = 0; channel < avctx->channels; channel++) {
659             int predictor;
660             int step_index;
661             cs = &(c->status[channel]);
662             /* (pppppp) (piiiiiii) */
663
664             /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
665             predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
666             step_index = predictor & 0x7F;
667             predictor &= ~0x7F;
668
669             if (cs->step_index == step_index) {
670                 int diff = predictor - cs->predictor;
671                 if (diff < 0)
672                     diff = - diff;
673                 if (diff > 0x7f)
674                     goto update;
675             } else {
676             update:
677                 cs->step_index = step_index;
678                 cs->predictor = predictor;
679             }
680
681             if (cs->step_index > 88u){
682                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
683                        channel, cs->step_index);
684                 return AVERROR_INVALIDDATA;
685             }
686
687             samples = samples_p[channel];
688
689             for (m = 0; m < 64; m += 2) {
690                 int byte = bytestream2_get_byteu(&gb);
691                 samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
692                 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  , 3);
693             }
694         }
695         break;
696     case AV_CODEC_ID_ADPCM_IMA_WAV:
697         for(i=0; i<avctx->channels; i++){
698             cs = &(c->status[i]);
699             cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
700
701             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
702             if (cs->step_index > 88u){
703                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
704                        i, cs->step_index);
705                 return AVERROR_INVALIDDATA;
706             }
707         }
708
709         for (n = 0; n < (nb_samples - 1) / 8; n++) {
710             for (i = 0; i < avctx->channels; i++) {
711                 cs = &c->status[i];
712                 samples = &samples_p[i][1 + n * 8];
713                 for (m = 0; m < 8; m += 2) {
714                     int v = bytestream2_get_byteu(&gb);
715                     samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
716                     samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
717                 }
718             }
719         }
720         break;
721     case AV_CODEC_ID_ADPCM_4XM:
722         for (i = 0; i < avctx->channels; i++)
723             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
724
725         for (i = 0; i < avctx->channels; i++) {
726             c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
727             if (c->status[i].step_index > 88u) {
728                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
729                        i, c->status[i].step_index);
730                 return AVERROR_INVALIDDATA;
731             }
732         }
733
734         for (i = 0; i < avctx->channels; i++) {
735             samples = (int16_t *)frame->data[i];
736             cs = &c->status[i];
737             for (n = nb_samples >> 1; n > 0; n--) {
738                 int v = bytestream2_get_byteu(&gb);
739                 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
740                 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
741             }
742         }
743         break;
744     case AV_CODEC_ID_ADPCM_MS:
745     {
746         int block_predictor;
747
748         block_predictor = bytestream2_get_byteu(&gb);
749         if (block_predictor > 6) {
750             av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
751                    block_predictor);
752             return AVERROR_INVALIDDATA;
753         }
754         c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
755         c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
756         if (st) {
757             block_predictor = bytestream2_get_byteu(&gb);
758             if (block_predictor > 6) {
759                 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
760                        block_predictor);
761                 return AVERROR_INVALIDDATA;
762             }
763             c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
764             c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
765         }
766         c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
767         if (st){
768             c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
769         }
770
771         c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
772         if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
773         c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
774         if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
775
776         *samples++ = c->status[0].sample2;
777         if (st) *samples++ = c->status[1].sample2;
778         *samples++ = c->status[0].sample1;
779         if (st) *samples++ = c->status[1].sample1;
780         for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
781             int byte = bytestream2_get_byteu(&gb);
782             *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
783             *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
784         }
785         break;
786     }
787     case AV_CODEC_ID_ADPCM_IMA_DK4:
788         for (channel = 0; channel < avctx->channels; channel++) {
789             cs = &c->status[channel];
790             cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
791             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
792             if (cs->step_index > 88u){
793                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
794                        channel, cs->step_index);
795                 return AVERROR_INVALIDDATA;
796             }
797         }
798         for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
799             int v = bytestream2_get_byteu(&gb);
800             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
801             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
802         }
803         break;
804     case AV_CODEC_ID_ADPCM_IMA_DK3:
805     {
806         int last_byte = 0;
807         int nibble;
808         int decode_top_nibble_next = 0;
809         int diff_channel;
810         const int16_t *samples_end = samples + avctx->channels * nb_samples;
811
812         bytestream2_skipu(&gb, 10);
813         c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
814         c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
815         c->status[0].step_index = bytestream2_get_byteu(&gb);
816         c->status[1].step_index = bytestream2_get_byteu(&gb);
817         if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
818             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
819                    c->status[0].step_index, c->status[1].step_index);
820             return AVERROR_INVALIDDATA;
821         }
822         /* sign extend the predictors */
823         diff_channel = c->status[1].predictor;
824
825         /* DK3 ADPCM support macro */
826 #define DK3_GET_NEXT_NIBBLE() \
827     if (decode_top_nibble_next) { \
828         nibble = last_byte >> 4; \
829         decode_top_nibble_next = 0; \
830     } else { \
831         last_byte = bytestream2_get_byteu(&gb); \
832         nibble = last_byte & 0x0F; \
833         decode_top_nibble_next = 1; \
834     }
835
836         while (samples < samples_end) {
837
838             /* for this algorithm, c->status[0] is the sum channel and
839              * c->status[1] is the diff channel */
840
841             /* process the first predictor of the sum channel */
842             DK3_GET_NEXT_NIBBLE();
843             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
844
845             /* process the diff channel predictor */
846             DK3_GET_NEXT_NIBBLE();
847             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
848
849             /* process the first pair of stereo PCM samples */
850             diff_channel = (diff_channel + c->status[1].predictor) / 2;
851             *samples++ = c->status[0].predictor + c->status[1].predictor;
852             *samples++ = c->status[0].predictor - c->status[1].predictor;
853
854             /* process the second predictor of the sum channel */
855             DK3_GET_NEXT_NIBBLE();
856             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
857
858             /* process the second pair of stereo PCM samples */
859             diff_channel = (diff_channel + c->status[1].predictor) / 2;
860             *samples++ = c->status[0].predictor + c->status[1].predictor;
861             *samples++ = c->status[0].predictor - c->status[1].predictor;
862         }
863         break;
864     }
865     case AV_CODEC_ID_ADPCM_IMA_ISS:
866         for (channel = 0; channel < avctx->channels; channel++) {
867             cs = &c->status[channel];
868             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
869             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
870             if (cs->step_index > 88u){
871                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
872                        channel, cs->step_index);
873                 return AVERROR_INVALIDDATA;
874             }
875         }
876
877         for (n = nb_samples >> (1 - st); n > 0; n--) {
878             int v1, v2;
879             int v = bytestream2_get_byteu(&gb);
880             /* nibbles are swapped for mono */
881             if (st) {
882                 v1 = v >> 4;
883                 v2 = v & 0x0F;
884             } else {
885                 v2 = v >> 4;
886                 v1 = v & 0x0F;
887             }
888             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
889             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
890         }
891         break;
892     case AV_CODEC_ID_ADPCM_IMA_APC:
893         while (bytestream2_get_bytes_left(&gb) > 0) {
894             int v = bytestream2_get_byteu(&gb);
895             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
896             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
897         }
898         break;
899     case AV_CODEC_ID_ADPCM_IMA_OKI:
900         while (bytestream2_get_bytes_left(&gb) > 0) {
901             int v = bytestream2_get_byteu(&gb);
902             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );
903             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
904         }
905         break;
906     case AV_CODEC_ID_ADPCM_IMA_WS:
907         if (c->vqa_version == 3) {
908             for (channel = 0; channel < avctx->channels; channel++) {
909                 int16_t *smp = samples_p[channel];
910
911                 for (n = nb_samples / 2; n > 0; n--) {
912                     int v = bytestream2_get_byteu(&gb);
913                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
914                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
915                 }
916             }
917         } else {
918             for (n = nb_samples / 2; n > 0; n--) {
919                 for (channel = 0; channel < avctx->channels; channel++) {
920                     int v = bytestream2_get_byteu(&gb);
921                     *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
922                     samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
923                 }
924                 samples += avctx->channels;
925             }
926         }
927         bytestream2_seek(&gb, 0, SEEK_END);
928         break;
929     case AV_CODEC_ID_ADPCM_XA:
930     {
931         int16_t *out0 = samples_p[0];
932         int16_t *out1 = samples_p[1];
933         int samples_per_block = 28 * (3 - avctx->channels) * 4;
934         int sample_offset = 0;
935         while (bytestream2_get_bytes_left(&gb) >= 128) {
936             if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
937                                  &c->status[0], &c->status[1],
938                                  avctx->channels, sample_offset)) < 0)
939                 return ret;
940             bytestream2_skipu(&gb, 128);
941             sample_offset += samples_per_block;
942         }
943         break;
944     }
945     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
946         for (i=0; i<=st; i++) {
947             c->status[i].step_index = bytestream2_get_le32u(&gb);
948             if (c->status[i].step_index > 88u) {
949                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
950                        i, c->status[i].step_index);
951                 return AVERROR_INVALIDDATA;
952             }
953         }
954         for (i=0; i<=st; i++)
955             c->status[i].predictor  = bytestream2_get_le32u(&gb);
956
957         for (n = nb_samples >> (1 - st); n > 0; n--) {
958             int byte   = bytestream2_get_byteu(&gb);
959             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
960             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
961         }
962         break;
963     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
964         for (n = nb_samples >> (1 - st); n > 0; n--) {
965             int byte = bytestream2_get_byteu(&gb);
966             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
967             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
968         }
969         break;
970     case AV_CODEC_ID_ADPCM_EA:
971     {
972         int previous_left_sample, previous_right_sample;
973         int current_left_sample, current_right_sample;
974         int next_left_sample, next_right_sample;
975         int coeff1l, coeff2l, coeff1r, coeff2r;
976         int shift_left, shift_right;
977
978         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
979            each coding 28 stereo samples. */
980
981         if(avctx->channels != 2)
982             return AVERROR_INVALIDDATA;
983
984         current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
985         previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
986         current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
987         previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
988
989         for (count1 = 0; count1 < nb_samples / 28; count1++) {
990             int byte = bytestream2_get_byteu(&gb);
991             coeff1l = ea_adpcm_table[ byte >> 4       ];
992             coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
993             coeff1r = ea_adpcm_table[ byte & 0x0F];
994             coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
995
996             byte = bytestream2_get_byteu(&gb);
997             shift_left  = 20 - (byte >> 4);
998             shift_right = 20 - (byte & 0x0F);
999
1000             for (count2 = 0; count2 < 28; count2++) {
1001                 byte = bytestream2_get_byteu(&gb);
1002                 next_left_sample  = sign_extend(byte >> 4, 4) << shift_left;
1003                 next_right_sample = sign_extend(byte,      4) << shift_right;
1004
1005                 next_left_sample = (next_left_sample +
1006                     (current_left_sample * coeff1l) +
1007                     (previous_left_sample * coeff2l) + 0x80) >> 8;
1008                 next_right_sample = (next_right_sample +
1009                     (current_right_sample * coeff1r) +
1010                     (previous_right_sample * coeff2r) + 0x80) >> 8;
1011
1012                 previous_left_sample = current_left_sample;
1013                 current_left_sample = av_clip_int16(next_left_sample);
1014                 previous_right_sample = current_right_sample;
1015                 current_right_sample = av_clip_int16(next_right_sample);
1016                 *samples++ = current_left_sample;
1017                 *samples++ = current_right_sample;
1018             }
1019         }
1020
1021         bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1022
1023         break;
1024     }
1025     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1026     {
1027         int coeff[2][2], shift[2];
1028
1029         for(channel = 0; channel < avctx->channels; channel++) {
1030             int byte = bytestream2_get_byteu(&gb);
1031             for (i=0; i<2; i++)
1032                 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1033             shift[channel] = 20 - (byte & 0x0F);
1034         }
1035         for (count1 = 0; count1 < nb_samples / 2; count1++) {
1036             int byte[2];
1037
1038             byte[0] = bytestream2_get_byteu(&gb);
1039             if (st) byte[1] = bytestream2_get_byteu(&gb);
1040             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1041                 for(channel = 0; channel < avctx->channels; channel++) {
1042                     int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1043                     sample = (sample +
1044                              c->status[channel].sample1 * coeff[channel][0] +
1045                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1046                     c->status[channel].sample2 = c->status[channel].sample1;
1047                     c->status[channel].sample1 = av_clip_int16(sample);
1048                     *samples++ = c->status[channel].sample1;
1049                 }
1050             }
1051         }
1052         bytestream2_seek(&gb, 0, SEEK_END);
1053         break;
1054     }
1055     case AV_CODEC_ID_ADPCM_EA_R1:
1056     case AV_CODEC_ID_ADPCM_EA_R2:
1057     case AV_CODEC_ID_ADPCM_EA_R3: {
1058         /* channel numbering
1059            2chan: 0=fl, 1=fr
1060            4chan: 0=fl, 1=rl, 2=fr, 3=rr
1061            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1062         const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1063         int previous_sample, current_sample, next_sample;
1064         int coeff1, coeff2;
1065         int shift;
1066         unsigned int channel;
1067         uint16_t *samplesC;
1068         int count = 0;
1069         int offsets[6];
1070
1071         for (channel=0; channel<avctx->channels; channel++)
1072             offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1073                                              bytestream2_get_le32(&gb)) +
1074                                (avctx->channels + 1) * 4;
1075
1076         for (channel=0; channel<avctx->channels; channel++) {
1077             bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1078             samplesC = samples_p[channel];
1079
1080             if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1081                 current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
1082                 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1083             } else {
1084                 current_sample  = c->status[channel].predictor;
1085                 previous_sample = c->status[channel].prev_sample;
1086             }
1087
1088             for (count1 = 0; count1 < nb_samples / 28; count1++) {
1089                 int byte = bytestream2_get_byte(&gb);
1090                 if (byte == 0xEE) {  /* only seen in R2 and R3 */
1091                     current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
1092                     previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1093
1094                     for (count2=0; count2<28; count2++)
1095                         *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1096                 } else {
1097                     coeff1 = ea_adpcm_table[ byte >> 4     ];
1098                     coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1099                     shift = 20 - (byte & 0x0F);
1100
1101                     for (count2=0; count2<28; count2++) {
1102                         if (count2 & 1)
1103                             next_sample = sign_extend(byte,    4) << shift;
1104                         else {
1105                             byte = bytestream2_get_byte(&gb);
1106                             next_sample = sign_extend(byte >> 4, 4) << shift;
1107                         }
1108
1109                         next_sample += (current_sample  * coeff1) +
1110                                        (previous_sample * coeff2);
1111                         next_sample = av_clip_int16(next_sample >> 8);
1112
1113                         previous_sample = current_sample;
1114                         current_sample  = next_sample;
1115                         *samplesC++ = current_sample;
1116                     }
1117                 }
1118             }
1119             if (!count) {
1120                 count = count1;
1121             } else if (count != count1) {
1122                 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1123                 count = FFMAX(count, count1);
1124             }
1125
1126             if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1127                 c->status[channel].predictor   = current_sample;
1128                 c->status[channel].prev_sample = previous_sample;
1129             }
1130         }
1131
1132         frame->nb_samples = count * 28;
1133         bytestream2_seek(&gb, 0, SEEK_END);
1134         break;
1135     }
1136     case AV_CODEC_ID_ADPCM_EA_XAS:
1137         for (channel=0; channel<avctx->channels; channel++) {
1138             int coeff[2][4], shift[4];
1139             int16_t *s = samples_p[channel];
1140             for (n = 0; n < 4; n++, s += 32) {
1141                 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1142                 for (i=0; i<2; i++)
1143                     coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1144                 s[0] = val & ~0x0F;
1145
1146                 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1147                 shift[n] = 20 - (val & 0x0F);
1148                 s[1] = val & ~0x0F;
1149             }
1150
1151             for (m=2; m<32; m+=2) {
1152                 s = &samples_p[channel][m];
1153                 for (n = 0; n < 4; n++, s += 32) {
1154                     int level, pred;
1155                     int byte = bytestream2_get_byteu(&gb);
1156
1157                     level = sign_extend(byte >> 4, 4) << shift[n];
1158                     pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1159                     s[0]  = av_clip_int16((level + pred + 0x80) >> 8);
1160
1161                     level = sign_extend(byte, 4) << shift[n];
1162                     pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1163                     s[1]  = av_clip_int16((level + pred + 0x80) >> 8);
1164                 }
1165             }
1166         }
1167         break;
1168     case AV_CODEC_ID_ADPCM_IMA_AMV:
1169         c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1170         c->status[0].step_index = bytestream2_get_le16u(&gb);
1171         bytestream2_skipu(&gb, 4);
1172         if (c->status[0].step_index > 88u) {
1173             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1174                    c->status[0].step_index);
1175             return AVERROR_INVALIDDATA;
1176         }
1177
1178         for (n = nb_samples >> (1 - st); n > 0; n--) {
1179             int v = bytestream2_get_byteu(&gb);
1180
1181             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1182             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1183         }
1184         break;
1185     case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1186         for (i = 0; i < avctx->channels; i++) {
1187             c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1188             c->status[i].step_index = bytestream2_get_byteu(&gb);
1189             bytestream2_skipu(&gb, 1);
1190             if (c->status[i].step_index > 88u) {
1191                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1192                        c->status[i].step_index);
1193                 return AVERROR_INVALIDDATA;
1194             }
1195         }
1196
1197         for (n = nb_samples >> (1 - st); n > 0; n--) {
1198             int v = bytestream2_get_byteu(&gb);
1199
1200             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1201             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1202         }
1203         break;
1204     case AV_CODEC_ID_ADPCM_CT:
1205         for (n = nb_samples >> (1 - st); n > 0; n--) {
1206             int v = bytestream2_get_byteu(&gb);
1207             *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
1208             *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1209         }
1210         break;
1211     case AV_CODEC_ID_ADPCM_SBPRO_4:
1212     case AV_CODEC_ID_ADPCM_SBPRO_3:
1213     case AV_CODEC_ID_ADPCM_SBPRO_2:
1214         if (!c->status[0].step_index) {
1215             /* the first byte is a raw sample */
1216             *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1217             if (st)
1218                 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1219             c->status[0].step_index = 1;
1220             nb_samples--;
1221         }
1222         if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1223             for (n = nb_samples >> (1 - st); n > 0; n--) {
1224                 int byte = bytestream2_get_byteu(&gb);
1225                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1226                                                        byte >> 4,   4, 0);
1227                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1228                                                        byte & 0x0F, 4, 0);
1229             }
1230         } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1231             for (n = nb_samples / 3; n > 0; n--) {
1232                 int byte = bytestream2_get_byteu(&gb);
1233                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1234                                                         byte >> 5        , 3, 0);
1235                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1236                                                        (byte >> 2) & 0x07, 3, 0);
1237                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1238                                                         byte & 0x03,       2, 0);
1239             }
1240         } else {
1241             for (n = nb_samples >> (2 - st); n > 0; n--) {
1242                 int byte = bytestream2_get_byteu(&gb);
1243                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1244                                                         byte >> 6        , 2, 2);
1245                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1246                                                        (byte >> 4) & 0x03, 2, 2);
1247                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1248                                                        (byte >> 2) & 0x03, 2, 2);
1249                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1250                                                         byte & 0x03,       2, 2);
1251             }
1252         }
1253         break;
1254     case AV_CODEC_ID_ADPCM_SWF:
1255         adpcm_swf_decode(avctx, buf, buf_size, samples);
1256         bytestream2_seek(&gb, 0, SEEK_END);
1257         break;
1258     case AV_CODEC_ID_ADPCM_YAMAHA:
1259         for (n = nb_samples >> (1 - st); n > 0; n--) {
1260             int v = bytestream2_get_byteu(&gb);
1261             *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1262             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1263         }
1264         break;
1265     case AV_CODEC_ID_ADPCM_AFC:
1266     {
1267         int samples_per_block;
1268         int blocks;
1269
1270         if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1271             samples_per_block = avctx->extradata[0] / 16;
1272             blocks = nb_samples / avctx->extradata[0];
1273         } else {
1274             samples_per_block = nb_samples / 16;
1275             blocks = 1;
1276         }
1277
1278         for (m = 0; m < blocks; m++) {
1279         for (channel = 0; channel < avctx->channels; channel++) {
1280             int prev1 = c->status[channel].sample1;
1281             int prev2 = c->status[channel].sample2;
1282
1283             samples = samples_p[channel] + m * 16;
1284             /* Read in every sample for this channel.  */
1285             for (i = 0; i < samples_per_block; i++) {
1286                 int byte = bytestream2_get_byteu(&gb);
1287                 int scale = 1 << (byte >> 4);
1288                 int index = byte & 0xf;
1289                 int factor1 = ff_adpcm_afc_coeffs[0][index];
1290                 int factor2 = ff_adpcm_afc_coeffs[1][index];
1291
1292                 /* Decode 16 samples.  */
1293                 for (n = 0; n < 16; n++) {
1294                     int32_t sampledat;
1295
1296                     if (n & 1) {
1297                         sampledat = sign_extend(byte, 4);
1298                     } else {
1299                         byte = bytestream2_get_byteu(&gb);
1300                         sampledat = sign_extend(byte >> 4, 4);
1301                     }
1302
1303                     sampledat = ((prev1 * factor1 + prev2 * factor2) +
1304                                  ((sampledat * scale) << 11)) >> 11;
1305                     *samples = av_clip_int16(sampledat);
1306                     prev2 = prev1;
1307                     prev1 = *samples++;
1308                 }
1309             }
1310
1311             c->status[channel].sample1 = prev1;
1312             c->status[channel].sample2 = prev2;
1313         }
1314         }
1315         bytestream2_seek(&gb, 0, SEEK_END);
1316         break;
1317     }
1318     case AV_CODEC_ID_ADPCM_THP:
1319     {
1320         int table[6][16];
1321         int ch;
1322
1323         for (i = 0; i < avctx->channels; i++)
1324             for (n = 0; n < 16; n++)
1325                 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1326
1327         /* Initialize the previous sample.  */
1328         for (i = 0; i < avctx->channels; i++) {
1329             c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1330             c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1331         }
1332
1333         for (ch = 0; ch < avctx->channels; ch++) {
1334             samples = samples_p[ch];
1335
1336             /* Read in every sample for this channel.  */
1337             for (i = 0; i < nb_samples / 14; i++) {
1338                 int byte = bytestream2_get_byteu(&gb);
1339                 int index = (byte >> 4) & 7;
1340                 unsigned int exp = byte & 0x0F;
1341                 int factor1 = table[ch][index * 2];
1342                 int factor2 = table[ch][index * 2 + 1];
1343
1344                 /* Decode 14 samples.  */
1345                 for (n = 0; n < 14; n++) {
1346                     int32_t sampledat;
1347
1348                     if (n & 1) {
1349                         sampledat = sign_extend(byte, 4);
1350                     } else {
1351                         byte = bytestream2_get_byteu(&gb);
1352                         sampledat = sign_extend(byte >> 4, 4);
1353                     }
1354
1355                     sampledat = ((c->status[ch].sample1 * factor1
1356                                 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1357                     *samples = av_clip_int16(sampledat);
1358                     c->status[ch].sample2 = c->status[ch].sample1;
1359                     c->status[ch].sample1 = *samples++;
1360                 }
1361             }
1362         }
1363         break;
1364     }
1365
1366     default:
1367         return -1;
1368     }
1369
1370     if (avpkt->size && bytestream2_tell(&gb) == 0) {
1371         av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1372         return AVERROR_INVALIDDATA;
1373     }
1374
1375     *got_frame_ptr = 1;
1376
1377     return bytestream2_tell(&gb);
1378 }
1379
1380
1381 static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
1382                                                         AV_SAMPLE_FMT_NONE };
1383 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
1384                                                         AV_SAMPLE_FMT_NONE };
1385 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1386                                                         AV_SAMPLE_FMT_S16P,
1387                                                         AV_SAMPLE_FMT_NONE };
1388
1389 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1390 AVCodec ff_ ## name_ ## _decoder = {                        \
1391     .name           = #name_,                               \
1392     .type           = AVMEDIA_TYPE_AUDIO,                   \
1393     .id             = id_,                                  \
1394     .priv_data_size = sizeof(ADPCMDecodeContext),           \
1395     .init           = adpcm_decode_init,                    \
1396     .decode         = adpcm_decode_frame,                   \
1397     .capabilities   = CODEC_CAP_DR1,                        \
1398     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
1399     .sample_fmts    = sample_fmts_,                         \
1400 }
1401
1402 /* Note: Do not forget to add new entries to the Makefile as well. */
1403 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
1404 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
1405 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
1406 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
1407 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1408 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
1409 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
1410 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
1411 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
1412 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
1413 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
1414 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
1415 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
1416 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1417 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1418 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
1419 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
1420 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
1421 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
1422 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
1423 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
1424 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_s16,  adpcm_ms,          "ADPCM Microsoft");
1425 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
1426 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
1427 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
1428 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
1429 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo Gamecube THP");
1430 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
1431 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");