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