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