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