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