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