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