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