]> git.sesse.net Git - ffmpeg/blob - libavcodec/adpcm.c
avcodec/adpcm_ima_apm: support new extradata format
[ffmpeg] / libavcodec / adpcm.c
1 /*
2  * Copyright (c) 2001-2003 The FFmpeg project
3  *
4  * first version by Francois Revol (revol@free.fr)
5  * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6  *   by Mike Melanson (melanson@pcisys.net)
7  * CD-ROM XA ADPCM codec by BERO
8  * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9  * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10  * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11  * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12  * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13  * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14  * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
15  * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
16  * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
17  * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
18  * High Voltage Software ALP decoder by Zane van Iperen (zane@zanevaniperen.com)
19  * Cunning Developments decoder by Zane van Iperen (zane@zanevaniperen.com)
20  *
21  * This file is part of FFmpeg.
22  *
23  * FFmpeg is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU Lesser General Public
25  * License as published by the Free Software Foundation; either
26  * version 2.1 of the License, or (at your option) any later version.
27  *
28  * FFmpeg is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  * Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU Lesser General Public
34  * License along with FFmpeg; if not, write to the Free Software
35  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36  */
37 #include "avcodec.h"
38 #include "get_bits.h"
39 #include "bytestream.h"
40 #include "adpcm.h"
41 #include "adpcm_data.h"
42 #include "internal.h"
43
44 /**
45  * @file
46  * ADPCM decoders
47  * Features and limitations:
48  *
49  * Reference documents:
50  * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
51  * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
52  * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
53  * http://openquicktime.sourceforge.net/
54  * XAnim sources (xa_codec.c) http://xanim.polter.net/
55  * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
56  * SoX source code http://sox.sourceforge.net/
57  *
58  * CD-ROM XA:
59  * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
60  * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
61  * readstr http://www.geocities.co.jp/Playtown/2004/
62  */
63
64 /* These are for CD-ROM XA ADPCM */
65 static const int8_t xa_adpcm_table[5][2] = {
66     {   0,   0 },
67     {  60,   0 },
68     { 115, -52 },
69     {  98, -55 },
70     { 122, -60 }
71 };
72
73 static const int16_t ea_adpcm_table[] = {
74     0,  240,  460,  392,
75     0,    0, -208, -220,
76     0,    1,    3,    4,
77     7,    8,   10,   11,
78     0,   -1,   -3,   -4
79 };
80
81 // padded to zero where table size is less then 16
82 static const int8_t swf_index_tables[4][16] = {
83     /*2*/ { -1, 2 },
84     /*3*/ { -1, -1, 2, 4 },
85     /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
86     /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
87 };
88
89 static const int8_t zork_index_table[8] = {
90     -1, -1, -1, 1, 4, 7, 10, 12,
91 };
92
93 static const int8_t mtf_index_table[16] = {
94      8,  6,  4,  2, -1, -1, -1, -1,
95     -1, -1, -1, -1,  2,  4,  6,  8,
96 };
97
98 /* end of tables */
99
100 typedef struct ADPCMDecodeContext {
101     ADPCMChannelStatus status[14];
102     int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */
103     int has_status;
104 } ADPCMDecodeContext;
105
106 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
107 {
108     ADPCMDecodeContext *c = avctx->priv_data;
109     unsigned int min_channels = 1;
110     unsigned int max_channels = 2;
111
112     switch(avctx->codec->id) {
113     case AV_CODEC_ID_ADPCM_IMA_CUNNING:
114         max_channels = 1;
115         break;
116     case AV_CODEC_ID_ADPCM_DTK:
117     case AV_CODEC_ID_ADPCM_EA:
118         min_channels = 2;
119         break;
120     case AV_CODEC_ID_ADPCM_AFC:
121     case AV_CODEC_ID_ADPCM_EA_R1:
122     case AV_CODEC_ID_ADPCM_EA_R2:
123     case AV_CODEC_ID_ADPCM_EA_R3:
124     case AV_CODEC_ID_ADPCM_EA_XAS:
125     case AV_CODEC_ID_ADPCM_MS:
126         max_channels = 6;
127         break;
128     case AV_CODEC_ID_ADPCM_MTAF:
129         min_channels = 2;
130         max_channels = 8;
131         if (avctx->channels & 1) {
132             avpriv_request_sample(avctx, "channel count %d\n", avctx->channels);
133             return AVERROR_PATCHWELCOME;
134         }
135         break;
136     case AV_CODEC_ID_ADPCM_PSX:
137         max_channels = 8;
138         break;
139     case AV_CODEC_ID_ADPCM_IMA_DAT4:
140     case AV_CODEC_ID_ADPCM_THP:
141     case AV_CODEC_ID_ADPCM_THP_LE:
142         max_channels = 14;
143         break;
144     }
145     if (avctx->channels < min_channels || avctx->channels > max_channels) {
146         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
147         return AVERROR(EINVAL);
148     }
149
150     switch(avctx->codec->id) {
151     case AV_CODEC_ID_ADPCM_CT:
152         c->status[0].step = c->status[1].step = 511;
153         break;
154     case AV_CODEC_ID_ADPCM_IMA_WAV:
155         if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
156             return AVERROR_INVALIDDATA;
157         break;
158     case AV_CODEC_ID_ADPCM_IMA_APC:
159         if (avctx->extradata && avctx->extradata_size >= 8) {
160             c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata    ), 18);
161             c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
162         }
163         break;
164     case AV_CODEC_ID_ADPCM_IMA_APM:
165         if (avctx->extradata) {
166             if (avctx->extradata_size >= 28) {
167                 c->status[0].predictor  = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
168                 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
169                 c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
170                 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
171             } else if (avctx->extradata_size >= 16) {
172                 c->status[0].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  0), 18);
173                 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata +  4), 0, 88);
174                 c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  8), 18);
175                 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
176             }
177         }
178         break;
179     case AV_CODEC_ID_ADPCM_IMA_WS:
180         if (avctx->extradata && avctx->extradata_size >= 2)
181             c->vqa_version = AV_RL16(avctx->extradata);
182         break;
183     case AV_CODEC_ID_ADPCM_ARGO:
184         if (avctx->bits_per_coded_sample != 4)
185             return AVERROR_INVALIDDATA;
186         break;
187     case AV_CODEC_ID_ADPCM_ZORK:
188         if (avctx->bits_per_coded_sample != 8)
189             return AVERROR_INVALIDDATA;
190         break;
191     default:
192         break;
193     }
194
195     switch (avctx->codec->id) {
196     case AV_CODEC_ID_ADPCM_AICA:
197     case AV_CODEC_ID_ADPCM_IMA_DAT4:
198     case AV_CODEC_ID_ADPCM_IMA_QT:
199     case AV_CODEC_ID_ADPCM_IMA_WAV:
200     case AV_CODEC_ID_ADPCM_4XM:
201     case AV_CODEC_ID_ADPCM_XA:
202     case AV_CODEC_ID_ADPCM_EA_R1:
203     case AV_CODEC_ID_ADPCM_EA_R2:
204     case AV_CODEC_ID_ADPCM_EA_R3:
205     case AV_CODEC_ID_ADPCM_EA_XAS:
206     case AV_CODEC_ID_ADPCM_THP:
207     case AV_CODEC_ID_ADPCM_THP_LE:
208     case AV_CODEC_ID_ADPCM_AFC:
209     case AV_CODEC_ID_ADPCM_DTK:
210     case AV_CODEC_ID_ADPCM_PSX:
211     case AV_CODEC_ID_ADPCM_MTAF:
212     case AV_CODEC_ID_ADPCM_ARGO:
213         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
214         break;
215     case AV_CODEC_ID_ADPCM_IMA_WS:
216         avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
217                                                   AV_SAMPLE_FMT_S16;
218         break;
219     case AV_CODEC_ID_ADPCM_MS:
220         avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
221                                                   AV_SAMPLE_FMT_S16;
222         break;
223     default:
224         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
225     }
226
227     return 0;
228 }
229
230 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
231 {
232     int delta, pred, step, add;
233
234     pred = c->predictor;
235     delta = nibble & 7;
236     step = c->step;
237     add = (delta * 2 + 1) * step;
238     if (add < 0)
239         add = add + 7;
240
241     if ((nibble & 8) == 0)
242         pred = av_clip(pred + (add >> 3), -32767, 32767);
243     else
244         pred = av_clip(pred - (add >> 3), -32767, 32767);
245
246     switch (delta) {
247     case 7:
248         step *= 0x99;
249         break;
250     case 6:
251         c->step = av_clip(c->step * 2, 127, 24576);
252         c->predictor = pred;
253         return pred;
254     case 5:
255         step *= 0x66;
256         break;
257     case 4:
258         step *= 0x4d;
259         break;
260     default:
261         step *= 0x39;
262         break;
263     }
264
265     if (step < 0)
266         step += 0x3f;
267
268     c->step = step >> 6;
269     c->step = av_clip(c->step, 127, 24576);
270     c->predictor = pred;
271     return pred;
272 }
273
274 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
275 {
276     int step_index;
277     int predictor;
278     int sign, delta, diff, step;
279
280     step = ff_adpcm_step_table[c->step_index];
281     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
282     step_index = av_clip(step_index, 0, 88);
283
284     sign = nibble & 8;
285     delta = nibble & 7;
286     /* perform direct multiplication instead of series of jumps proposed by
287      * the reference ADPCM implementation since modern CPUs can do the mults
288      * quickly enough */
289     diff = ((2 * delta + 1) * step) >> shift;
290     predictor = c->predictor;
291     if (sign) predictor -= diff;
292     else predictor += diff;
293
294     c->predictor = av_clip_int16(predictor);
295     c->step_index = step_index;
296
297     return (int16_t)c->predictor;
298 }
299
300 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
301 {
302     int step_index;
303     int predictor;
304     int sign, delta, diff, step;
305
306     step = ff_adpcm_step_table[c->step_index];
307     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
308     step_index = av_clip(step_index, 0, 88);
309
310     sign = nibble & 8;
311     delta = nibble & 7;
312     diff = (delta * step) >> shift;
313     predictor = c->predictor;
314     if (sign) predictor -= diff;
315     else predictor += diff;
316
317     c->predictor = av_clip_int16(predictor);
318     c->step_index = step_index;
319
320     return (int16_t)c->predictor;
321 }
322
323 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
324 {
325     int step_index, step, delta, predictor;
326
327     step = ff_adpcm_step_table[c->step_index];
328
329     delta = step * (2 * nibble - 15);
330     predictor = c->predictor + delta;
331
332     step_index = c->step_index + mtf_index_table[(unsigned)nibble];
333     c->predictor = av_clip_int16(predictor >> 4);
334     c->step_index = av_clip(step_index, 0, 88);
335
336     return (int16_t)c->predictor;
337 }
338
339 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
340 {
341     int step_index;
342     int predictor;
343     int step;
344
345     nibble = sign_extend(nibble & 0xF, 4);
346
347     step = ff_adpcm_ima_cunning_step_table[c->step_index];
348     step_index = c->step_index + ff_adpcm_ima_cunning_index_table[abs(nibble)];
349     step_index = av_clip(step_index, 0, 60);
350
351     predictor = c->predictor + step * nibble;
352
353     c->predictor = av_clip_int16(predictor);
354     c->step_index = step_index;
355
356     return c->predictor;
357 }
358
359 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
360 {
361     int nibble, step_index, predictor, sign, delta, diff, step, shift;
362
363     shift = bps - 1;
364     nibble = get_bits_le(gb, bps),
365     step = ff_adpcm_step_table[c->step_index];
366     step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
367     step_index = av_clip(step_index, 0, 88);
368
369     sign = nibble & (1 << shift);
370     delta = av_mod_uintp2(nibble, shift);
371     diff = ((2 * delta + 1) * step) >> shift;
372     predictor = c->predictor;
373     if (sign) predictor -= diff;
374     else predictor += diff;
375
376     c->predictor = av_clip_int16(predictor);
377     c->step_index = step_index;
378
379     return (int16_t)c->predictor;
380 }
381
382 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
383 {
384     int step_index;
385     int predictor;
386     int diff, step;
387
388     step = ff_adpcm_step_table[c->step_index];
389     step_index = c->step_index + ff_adpcm_index_table[nibble];
390     step_index = av_clip(step_index, 0, 88);
391
392     diff = step >> 3;
393     if (nibble & 4) diff += step;
394     if (nibble & 2) diff += step >> 1;
395     if (nibble & 1) diff += step >> 2;
396
397     if (nibble & 8)
398         predictor = c->predictor - diff;
399     else
400         predictor = c->predictor + diff;
401
402     c->predictor = av_clip_int16(predictor);
403     c->step_index = step_index;
404
405     return c->predictor;
406 }
407
408 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
409 {
410     int predictor;
411
412     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
413     predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
414
415     c->sample2 = c->sample1;
416     c->sample1 = av_clip_int16(predictor);
417     c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
418     if (c->idelta < 16) c->idelta = 16;
419     if (c->idelta > INT_MAX/768) {
420         av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
421         c->idelta = INT_MAX/768;
422     }
423
424     return c->sample1;
425 }
426
427 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
428 {
429     int step_index, predictor, sign, delta, diff, step;
430
431     step = ff_adpcm_oki_step_table[c->step_index];
432     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
433     step_index = av_clip(step_index, 0, 48);
434
435     sign = nibble & 8;
436     delta = nibble & 7;
437     diff = ((2 * delta + 1) * step) >> 3;
438     predictor = c->predictor;
439     if (sign) predictor -= diff;
440     else predictor += diff;
441
442     c->predictor = av_clip_intp2(predictor, 11);
443     c->step_index = step_index;
444
445     return c->predictor * 16;
446 }
447
448 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
449 {
450     int sign, delta, diff;
451     int new_step;
452
453     sign = nibble & 8;
454     delta = nibble & 7;
455     /* perform direct multiplication instead of series of jumps proposed by
456      * the reference ADPCM implementation since modern CPUs can do the mults
457      * quickly enough */
458     diff = ((2 * delta + 1) * c->step) >> 3;
459     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
460     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
461     c->predictor = av_clip_int16(c->predictor);
462     /* calculate new step and clamp it to range 511..32767 */
463     new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
464     c->step = av_clip(new_step, 511, 32767);
465
466     return (int16_t)c->predictor;
467 }
468
469 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
470 {
471     int sign, delta, diff;
472
473     sign = nibble & (1<<(size-1));
474     delta = nibble & ((1<<(size-1))-1);
475     diff = delta << (7 + c->step + shift);
476
477     /* clamp result */
478     c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
479
480     /* calculate new step */
481     if (delta >= (2*size - 3) && c->step < 3)
482         c->step++;
483     else if (delta == 0 && c->step > 0)
484         c->step--;
485
486     return (int16_t) c->predictor;
487 }
488
489 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
490 {
491     if(!c->step) {
492         c->predictor = 0;
493         c->step = 127;
494     }
495
496     c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
497     c->predictor = av_clip_int16(c->predictor);
498     c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
499     c->step = av_clip(c->step, 127, 24576);
500     return c->predictor;
501 }
502
503 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
504 {
505     c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
506     c->predictor = av_clip_int16(c->predictor);
507     c->step += ff_adpcm_index_table[nibble];
508     c->step = av_clip_uintp2(c->step, 5);
509     return c->predictor;
510 }
511
512 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
513 {
514     int16_t index = c->step_index;
515     uint32_t lookup_sample = ff_adpcm_step_table[index];
516     int32_t sample = 0;
517
518     if (nibble & 0x40)
519         sample += lookup_sample;
520     if (nibble & 0x20)
521         sample += lookup_sample >> 1;
522     if (nibble & 0x10)
523         sample += lookup_sample >> 2;
524     if (nibble & 0x08)
525         sample += lookup_sample >> 3;
526     if (nibble & 0x04)
527         sample += lookup_sample >> 4;
528     if (nibble & 0x02)
529         sample += lookup_sample >> 5;
530     if (nibble & 0x01)
531         sample += lookup_sample >> 6;
532     if (nibble & 0x80)
533         sample = -sample;
534
535     sample += c->predictor;
536     sample = av_clip_int16(sample);
537
538     index += zork_index_table[(nibble >> 4) & 7];
539     index = av_clip(index, 0, 88);
540
541     c->predictor = sample;
542     c->step_index = index;
543
544     return sample;
545 }
546
547 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
548                      const uint8_t *in, ADPCMChannelStatus *left,
549                      ADPCMChannelStatus *right, int channels, int sample_offset)
550 {
551     int i, j;
552     int shift,filter,f0,f1;
553     int s_1,s_2;
554     int d,s,t;
555
556     out0 += sample_offset;
557     if (channels == 1)
558         out1 = out0 + 28;
559     else
560         out1 += sample_offset;
561
562     for(i=0;i<4;i++) {
563         shift  = 12 - (in[4+i*2] & 15);
564         filter = in[4+i*2] >> 4;
565         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
566             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
567             filter=0;
568         }
569         if (shift < 0) {
570             avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
571             shift = 0;
572         }
573         f0 = xa_adpcm_table[filter][0];
574         f1 = xa_adpcm_table[filter][1];
575
576         s_1 = left->sample1;
577         s_2 = left->sample2;
578
579         for(j=0;j<28;j++) {
580             d = in[16+i+j*4];
581
582             t = sign_extend(d, 4);
583             s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
584             s_2 = s_1;
585             s_1 = av_clip_int16(s);
586             out0[j] = s_1;
587         }
588
589         if (channels == 2) {
590             left->sample1 = s_1;
591             left->sample2 = s_2;
592             s_1 = right->sample1;
593             s_2 = right->sample2;
594         }
595
596         shift  = 12 - (in[5+i*2] & 15);
597         filter = in[5+i*2] >> 4;
598         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
599             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
600             filter=0;
601         }
602         if (shift < 0) {
603             avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
604             shift = 0;
605         }
606
607         f0 = xa_adpcm_table[filter][0];
608         f1 = xa_adpcm_table[filter][1];
609
610         for(j=0;j<28;j++) {
611             d = in[16+i+j*4];
612
613             t = sign_extend(d >> 4, 4);
614             s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
615             s_2 = s_1;
616             s_1 = av_clip_int16(s);
617             out1[j] = s_1;
618         }
619
620         if (channels == 2) {
621             right->sample1 = s_1;
622             right->sample2 = s_2;
623         } else {
624             left->sample1 = s_1;
625             left->sample2 = s_2;
626         }
627
628         out0 += 28 * (3 - channels);
629         out1 += 28 * (3 - channels);
630     }
631
632     return 0;
633 }
634
635 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
636 {
637     ADPCMDecodeContext *c = avctx->priv_data;
638     GetBitContext gb;
639     const int8_t *table;
640     int k0, signmask, nb_bits, count;
641     int size = buf_size*8;
642     int i;
643
644     init_get_bits(&gb, buf, size);
645
646     //read bits & initial values
647     nb_bits = get_bits(&gb, 2)+2;
648     table = swf_index_tables[nb_bits-2];
649     k0 = 1 << (nb_bits-2);
650     signmask = 1 << (nb_bits-1);
651
652     while (get_bits_count(&gb) <= size - 22*avctx->channels) {
653         for (i = 0; i < avctx->channels; i++) {
654             *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
655             c->status[i].step_index = get_bits(&gb, 6);
656         }
657
658         for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
659             int i;
660
661             for (i = 0; i < avctx->channels; i++) {
662                 // similar to IMA adpcm
663                 int delta = get_bits(&gb, nb_bits);
664                 int step = ff_adpcm_step_table[c->status[i].step_index];
665                 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
666                 int k = k0;
667
668                 do {
669                     if (delta & k)
670                         vpdiff += step;
671                     step >>= 1;
672                     k >>= 1;
673                 } while(k);
674                 vpdiff += step;
675
676                 if (delta & signmask)
677                     c->status[i].predictor -= vpdiff;
678                 else
679                     c->status[i].predictor += vpdiff;
680
681                 c->status[i].step_index += table[delta & (~signmask)];
682
683                 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
684                 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
685
686                 *samples++ = c->status[i].predictor;
687             }
688         }
689     }
690 }
691
692 static inline int16_t adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int control, int shift)
693 {
694     int sample = nibble * (1 << shift);
695
696     if (control & 0x04)
697         sample += (8 * cs->sample1) - (4 * cs->sample2);
698     else
699         sample += 4 * cs->sample1;
700
701     sample = av_clip_int16(sample >> 2);
702
703     cs->sample2 = cs->sample1;
704     cs->sample1 = sample;
705
706     return sample;
707 }
708
709 /**
710  * Get the number of samples (per channel) that will be decoded from the packet.
711  * In one case, this is actually the maximum number of samples possible to
712  * decode with the given buf_size.
713  *
714  * @param[out] coded_samples set to the number of samples as coded in the
715  *                           packet, or 0 if the codec does not encode the
716  *                           number of samples in each frame.
717  * @param[out] approx_nb_samples set to non-zero if the number of samples
718  *                               returned is an approximation.
719  */
720 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
721                           int buf_size, int *coded_samples, int *approx_nb_samples)
722 {
723     ADPCMDecodeContext *s = avctx->priv_data;
724     int nb_samples        = 0;
725     int ch                = avctx->channels;
726     int has_coded_samples = 0;
727     int header_size;
728
729     *coded_samples = 0;
730     *approx_nb_samples = 0;
731
732     if(ch <= 0)
733         return 0;
734
735     switch (avctx->codec->id) {
736     /* constant, only check buf_size */
737     case AV_CODEC_ID_ADPCM_EA_XAS:
738         if (buf_size < 76 * ch)
739             return 0;
740         nb_samples = 128;
741         break;
742     case AV_CODEC_ID_ADPCM_IMA_QT:
743         if (buf_size < 34 * ch)
744             return 0;
745         nb_samples = 64;
746         break;
747     case AV_CODEC_ID_ADPCM_ARGO:
748         if (buf_size < 17 * ch)
749             return 0;
750         nb_samples = 32;
751         break;
752     /* simple 4-bit adpcm */
753     case AV_CODEC_ID_ADPCM_CT:
754     case AV_CODEC_ID_ADPCM_IMA_APC:
755     case AV_CODEC_ID_ADPCM_IMA_CUNNING:
756     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
757     case AV_CODEC_ID_ADPCM_IMA_OKI:
758     case AV_CODEC_ID_ADPCM_IMA_WS:
759     case AV_CODEC_ID_ADPCM_YAMAHA:
760     case AV_CODEC_ID_ADPCM_AICA:
761     case AV_CODEC_ID_ADPCM_IMA_SSI:
762     case AV_CODEC_ID_ADPCM_IMA_APM:
763     case AV_CODEC_ID_ADPCM_IMA_ALP:
764     case AV_CODEC_ID_ADPCM_IMA_MTF:
765         nb_samples = buf_size * 2 / ch;
766         break;
767     }
768     if (nb_samples)
769         return nb_samples;
770
771     /* simple 4-bit adpcm, with header */
772     header_size = 0;
773     switch (avctx->codec->id) {
774         case AV_CODEC_ID_ADPCM_4XM:
775         case AV_CODEC_ID_ADPCM_AGM:
776         case AV_CODEC_ID_ADPCM_IMA_DAT4:
777         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
778         case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
779         case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
780     }
781     if (header_size > 0)
782         return (buf_size - header_size) * 2 / ch;
783
784     /* more complex formats */
785     switch (avctx->codec->id) {
786     case AV_CODEC_ID_ADPCM_EA:
787         has_coded_samples = 1;
788         *coded_samples  = bytestream2_get_le32(gb);
789         *coded_samples -= *coded_samples % 28;
790         nb_samples      = (buf_size - 12) / 30 * 28;
791         break;
792     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
793         has_coded_samples = 1;
794         *coded_samples = bytestream2_get_le32(gb);
795         nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
796         break;
797     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
798         nb_samples = (buf_size - ch) / ch * 2;
799         break;
800     case AV_CODEC_ID_ADPCM_EA_R1:
801     case AV_CODEC_ID_ADPCM_EA_R2:
802     case AV_CODEC_ID_ADPCM_EA_R3:
803         /* maximum number of samples */
804         /* has internal offsets and a per-frame switch to signal raw 16-bit */
805         has_coded_samples = 1;
806         switch (avctx->codec->id) {
807         case AV_CODEC_ID_ADPCM_EA_R1:
808             header_size    = 4 + 9 * ch;
809             *coded_samples = bytestream2_get_le32(gb);
810             break;
811         case AV_CODEC_ID_ADPCM_EA_R2:
812             header_size    = 4 + 5 * ch;
813             *coded_samples = bytestream2_get_le32(gb);
814             break;
815         case AV_CODEC_ID_ADPCM_EA_R3:
816             header_size    = 4 + 5 * ch;
817             *coded_samples = bytestream2_get_be32(gb);
818             break;
819         }
820         *coded_samples -= *coded_samples % 28;
821         nb_samples      = (buf_size - header_size) * 2 / ch;
822         nb_samples     -= nb_samples % 28;
823         *approx_nb_samples = 1;
824         break;
825     case AV_CODEC_ID_ADPCM_IMA_DK3:
826         if (avctx->block_align > 0)
827             buf_size = FFMIN(buf_size, avctx->block_align);
828         nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
829         break;
830     case AV_CODEC_ID_ADPCM_IMA_DK4:
831         if (avctx->block_align > 0)
832             buf_size = FFMIN(buf_size, avctx->block_align);
833         if (buf_size < 4 * ch)
834             return AVERROR_INVALIDDATA;
835         nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
836         break;
837     case AV_CODEC_ID_ADPCM_IMA_RAD:
838         if (avctx->block_align > 0)
839             buf_size = FFMIN(buf_size, avctx->block_align);
840         nb_samples = (buf_size - 4 * ch) * 2 / ch;
841         break;
842     case AV_CODEC_ID_ADPCM_IMA_WAV:
843     {
844         int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
845         int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
846         if (avctx->block_align > 0)
847             buf_size = FFMIN(buf_size, avctx->block_align);
848         if (buf_size < 4 * ch)
849             return AVERROR_INVALIDDATA;
850         nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
851         break;
852     }
853     case AV_CODEC_ID_ADPCM_MS:
854         if (avctx->block_align > 0)
855             buf_size = FFMIN(buf_size, avctx->block_align);
856         nb_samples = (buf_size - 6 * ch) * 2 / ch;
857         break;
858     case AV_CODEC_ID_ADPCM_MTAF:
859         if (avctx->block_align > 0)
860             buf_size = FFMIN(buf_size, avctx->block_align);
861         nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
862         break;
863     case AV_CODEC_ID_ADPCM_SBPRO_2:
864     case AV_CODEC_ID_ADPCM_SBPRO_3:
865     case AV_CODEC_ID_ADPCM_SBPRO_4:
866     {
867         int samples_per_byte;
868         switch (avctx->codec->id) {
869         case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
870         case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
871         case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
872         }
873         if (!s->status[0].step_index) {
874             if (buf_size < ch)
875                 return AVERROR_INVALIDDATA;
876             nb_samples++;
877             buf_size -= ch;
878         }
879         nb_samples += buf_size * samples_per_byte / ch;
880         break;
881     }
882     case AV_CODEC_ID_ADPCM_SWF:
883     {
884         int buf_bits       = buf_size * 8 - 2;
885         int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
886         int block_hdr_size = 22 * ch;
887         int block_size     = block_hdr_size + nbits * ch * 4095;
888         int nblocks        = buf_bits / block_size;
889         int bits_left      = buf_bits - nblocks * block_size;
890         nb_samples         = nblocks * 4096;
891         if (bits_left >= block_hdr_size)
892             nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
893         break;
894     }
895     case AV_CODEC_ID_ADPCM_THP:
896     case AV_CODEC_ID_ADPCM_THP_LE:
897         if (avctx->extradata) {
898             nb_samples = buf_size * 14 / (8 * ch);
899             break;
900         }
901         has_coded_samples = 1;
902         bytestream2_skip(gb, 4); // channel size
903         *coded_samples  = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
904                           bytestream2_get_le32(gb) :
905                           bytestream2_get_be32(gb);
906         buf_size       -= 8 + 36 * ch;
907         buf_size       /= ch;
908         nb_samples      = buf_size / 8 * 14;
909         if (buf_size % 8 > 1)
910             nb_samples     += (buf_size % 8 - 1) * 2;
911         *approx_nb_samples = 1;
912         break;
913     case AV_CODEC_ID_ADPCM_AFC:
914         nb_samples = buf_size / (9 * ch) * 16;
915         break;
916     case AV_CODEC_ID_ADPCM_XA:
917         nb_samples = (buf_size / 128) * 224 / ch;
918         break;
919     case AV_CODEC_ID_ADPCM_DTK:
920     case AV_CODEC_ID_ADPCM_PSX:
921         nb_samples = buf_size / (16 * ch) * 28;
922         break;
923     case AV_CODEC_ID_ADPCM_ZORK:
924         nb_samples = buf_size / ch;
925         break;
926     }
927
928     /* validate coded sample count */
929     if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
930         return AVERROR_INVALIDDATA;
931
932     return nb_samples;
933 }
934
935 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
936                               int *got_frame_ptr, AVPacket *avpkt)
937 {
938     AVFrame *frame     = data;
939     const uint8_t *buf = avpkt->data;
940     int buf_size = avpkt->size;
941     ADPCMDecodeContext *c = avctx->priv_data;
942     ADPCMChannelStatus *cs;
943     int n, m, channel, i;
944     int16_t *samples;
945     int16_t **samples_p;
946     int st; /* stereo */
947     int count1, count2;
948     int nb_samples, coded_samples, approx_nb_samples, ret;
949     GetByteContext gb;
950
951     bytestream2_init(&gb, buf, buf_size);
952     nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
953     if (nb_samples <= 0) {
954         av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
955         return AVERROR_INVALIDDATA;
956     }
957
958     /* get output buffer */
959     frame->nb_samples = nb_samples;
960     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
961         return ret;
962     samples = (int16_t *)frame->data[0];
963     samples_p = (int16_t **)frame->extended_data;
964
965     /* use coded_samples when applicable */
966     /* it is always <= nb_samples, so the output buffer will be large enough */
967     if (coded_samples) {
968         if (!approx_nb_samples && coded_samples != nb_samples)
969             av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
970         frame->nb_samples = nb_samples = coded_samples;
971     }
972
973     st = avctx->channels == 2 ? 1 : 0;
974
975     switch(avctx->codec->id) {
976     case AV_CODEC_ID_ADPCM_IMA_QT:
977         /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
978            Channel data is interleaved per-chunk. */
979         for (channel = 0; channel < avctx->channels; channel++) {
980             int predictor;
981             int step_index;
982             cs = &(c->status[channel]);
983             /* (pppppp) (piiiiiii) */
984
985             /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
986             predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
987             step_index = predictor & 0x7F;
988             predictor &= ~0x7F;
989
990             if (cs->step_index == step_index) {
991                 int diff = predictor - cs->predictor;
992                 if (diff < 0)
993                     diff = - diff;
994                 if (diff > 0x7f)
995                     goto update;
996             } else {
997             update:
998                 cs->step_index = step_index;
999                 cs->predictor = predictor;
1000             }
1001
1002             if (cs->step_index > 88u){
1003                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1004                        channel, cs->step_index);
1005                 return AVERROR_INVALIDDATA;
1006             }
1007
1008             samples = samples_p[channel];
1009
1010             for (m = 0; m < 64; m += 2) {
1011                 int byte = bytestream2_get_byteu(&gb);
1012                 samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1013                 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  );
1014             }
1015         }
1016         break;
1017     case AV_CODEC_ID_ADPCM_IMA_WAV:
1018         for(i=0; i<avctx->channels; i++){
1019             cs = &(c->status[i]);
1020             cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1021
1022             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1023             if (cs->step_index > 88u){
1024                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1025                        i, cs->step_index);
1026                 return AVERROR_INVALIDDATA;
1027             }
1028         }
1029
1030         if (avctx->bits_per_coded_sample != 4) {
1031             int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1032             int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1033             uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1034             GetBitContext g;
1035
1036             for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1037                 for (i = 0; i < avctx->channels; i++) {
1038                     int j;
1039
1040                     cs = &c->status[i];
1041                     samples = &samples_p[i][1 + n * samples_per_block];
1042                     for (j = 0; j < block_size; j++) {
1043                         temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
1044                                         (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
1045                     }
1046                     ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1047                     if (ret < 0)
1048                         return ret;
1049                     for (m = 0; m < samples_per_block; m++) {
1050                         samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1051                                           avctx->bits_per_coded_sample);
1052                     }
1053                 }
1054             }
1055             bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1056         } else {
1057         for (n = 0; n < (nb_samples - 1) / 8; n++) {
1058             for (i = 0; i < avctx->channels; i++) {
1059                 cs = &c->status[i];
1060                 samples = &samples_p[i][1 + n * 8];
1061                 for (m = 0; m < 8; m += 2) {
1062                     int v = bytestream2_get_byteu(&gb);
1063                     samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1064                     samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1065                 }
1066             }
1067         }
1068         }
1069         break;
1070     case AV_CODEC_ID_ADPCM_4XM:
1071         for (i = 0; i < avctx->channels; i++)
1072             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1073
1074         for (i = 0; i < avctx->channels; i++) {
1075             c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1076             if (c->status[i].step_index > 88u) {
1077                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1078                        i, c->status[i].step_index);
1079                 return AVERROR_INVALIDDATA;
1080             }
1081         }
1082
1083         for (i = 0; i < avctx->channels; i++) {
1084             samples = (int16_t *)frame->data[i];
1085             cs = &c->status[i];
1086             for (n = nb_samples >> 1; n > 0; n--) {
1087                 int v = bytestream2_get_byteu(&gb);
1088                 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1089                 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
1090             }
1091         }
1092         break;
1093     case AV_CODEC_ID_ADPCM_AGM:
1094         for (i = 0; i < avctx->channels; i++)
1095             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1096         for (i = 0; i < avctx->channels; i++)
1097             c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1098
1099         for (n = 0; n < nb_samples >> (1 - st); n++) {
1100             int v = bytestream2_get_byteu(&gb);
1101             *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1102             *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1103         }
1104         break;
1105     case AV_CODEC_ID_ADPCM_MS:
1106     {
1107         int block_predictor;
1108
1109         if (avctx->channels > 2) {
1110             for (channel = 0; channel < avctx->channels; channel++) {
1111                 samples = samples_p[channel];
1112                 block_predictor = bytestream2_get_byteu(&gb);
1113                 if (block_predictor > 6) {
1114                     av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1115                            channel, block_predictor);
1116                     return AVERROR_INVALIDDATA;
1117                 }
1118                 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1119                 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1120                 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1121                 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1122                 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1123                 *samples++ = c->status[channel].sample2;
1124                 *samples++ = c->status[channel].sample1;
1125                 for(n = (nb_samples - 2) >> 1; n > 0; n--) {
1126                     int byte = bytestream2_get_byteu(&gb);
1127                     *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4  );
1128                     *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1129                 }
1130             }
1131         } else {
1132             block_predictor = bytestream2_get_byteu(&gb);
1133             if (block_predictor > 6) {
1134                 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1135                        block_predictor);
1136                 return AVERROR_INVALIDDATA;
1137             }
1138             c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1139             c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1140             if (st) {
1141                 block_predictor = bytestream2_get_byteu(&gb);
1142                 if (block_predictor > 6) {
1143                     av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1144                            block_predictor);
1145                     return AVERROR_INVALIDDATA;
1146                 }
1147                 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1148                 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1149             }
1150             c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1151             if (st){
1152                 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1153             }
1154
1155             c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1156             if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1157             c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1158             if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1159
1160             *samples++ = c->status[0].sample2;
1161             if (st) *samples++ = c->status[1].sample2;
1162             *samples++ = c->status[0].sample1;
1163             if (st) *samples++ = c->status[1].sample1;
1164             for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1165                 int byte = bytestream2_get_byteu(&gb);
1166                 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
1167                 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1168             }
1169         }
1170         break;
1171     }
1172     case AV_CODEC_ID_ADPCM_MTAF:
1173         for (channel = 0; channel < avctx->channels; channel+=2) {
1174             bytestream2_skipu(&gb, 4);
1175             c->status[channel    ].step      = bytestream2_get_le16u(&gb) & 0x1f;
1176             c->status[channel + 1].step      = bytestream2_get_le16u(&gb) & 0x1f;
1177             c->status[channel    ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1178             bytestream2_skipu(&gb, 2);
1179             c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1180             bytestream2_skipu(&gb, 2);
1181             for (n = 0; n < nb_samples; n+=2) {
1182                 int v = bytestream2_get_byteu(&gb);
1183                 samples_p[channel][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1184                 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4  );
1185             }
1186             for (n = 0; n < nb_samples; n+=2) {
1187                 int v = bytestream2_get_byteu(&gb);
1188                 samples_p[channel + 1][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1189                 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4  );
1190             }
1191         }
1192         break;
1193     case AV_CODEC_ID_ADPCM_IMA_DK4:
1194         for (channel = 0; channel < avctx->channels; channel++) {
1195             cs = &c->status[channel];
1196             cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1197             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1198             if (cs->step_index > 88u){
1199                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1200                        channel, cs->step_index);
1201                 return AVERROR_INVALIDDATA;
1202             }
1203         }
1204         for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1205             int v = bytestream2_get_byteu(&gb);
1206             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
1207             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1208         }
1209         break;
1210     case AV_CODEC_ID_ADPCM_IMA_DK3:
1211     {
1212         int last_byte = 0;
1213         int nibble;
1214         int decode_top_nibble_next = 0;
1215         int diff_channel;
1216         const int16_t *samples_end = samples + avctx->channels * nb_samples;
1217
1218         bytestream2_skipu(&gb, 10);
1219         c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1220         c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1221         c->status[0].step_index = bytestream2_get_byteu(&gb);
1222         c->status[1].step_index = bytestream2_get_byteu(&gb);
1223         if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1224             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1225                    c->status[0].step_index, c->status[1].step_index);
1226             return AVERROR_INVALIDDATA;
1227         }
1228         /* sign extend the predictors */
1229         diff_channel = c->status[1].predictor;
1230
1231         /* DK3 ADPCM support macro */
1232 #define DK3_GET_NEXT_NIBBLE() \
1233     if (decode_top_nibble_next) { \
1234         nibble = last_byte >> 4; \
1235         decode_top_nibble_next = 0; \
1236     } else { \
1237         last_byte = bytestream2_get_byteu(&gb); \
1238         nibble = last_byte & 0x0F; \
1239         decode_top_nibble_next = 1; \
1240     }
1241
1242         while (samples < samples_end) {
1243
1244             /* for this algorithm, c->status[0] is the sum channel and
1245              * c->status[1] is the diff channel */
1246
1247             /* process the first predictor of the sum channel */
1248             DK3_GET_NEXT_NIBBLE();
1249             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1250
1251             /* process the diff channel predictor */
1252             DK3_GET_NEXT_NIBBLE();
1253             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1254
1255             /* process the first pair of stereo PCM samples */
1256             diff_channel = (diff_channel + c->status[1].predictor) / 2;
1257             *samples++ = c->status[0].predictor + c->status[1].predictor;
1258             *samples++ = c->status[0].predictor - c->status[1].predictor;
1259
1260             /* process the second predictor of the sum channel */
1261             DK3_GET_NEXT_NIBBLE();
1262             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1263
1264             /* process the second pair of stereo PCM samples */
1265             diff_channel = (diff_channel + c->status[1].predictor) / 2;
1266             *samples++ = c->status[0].predictor + c->status[1].predictor;
1267             *samples++ = c->status[0].predictor - c->status[1].predictor;
1268         }
1269
1270         if ((bytestream2_tell(&gb) & 1))
1271             bytestream2_skip(&gb, 1);
1272         break;
1273     }
1274     case AV_CODEC_ID_ADPCM_IMA_ISS:
1275         for (channel = 0; channel < avctx->channels; channel++) {
1276             cs = &c->status[channel];
1277             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1278             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1279             if (cs->step_index > 88u){
1280                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1281                        channel, cs->step_index);
1282                 return AVERROR_INVALIDDATA;
1283             }
1284         }
1285
1286         for (n = nb_samples >> (1 - st); n > 0; n--) {
1287             int v1, v2;
1288             int v = bytestream2_get_byteu(&gb);
1289             /* nibbles are swapped for mono */
1290             if (st) {
1291                 v1 = v >> 4;
1292                 v2 = v & 0x0F;
1293             } else {
1294                 v2 = v >> 4;
1295                 v1 = v & 0x0F;
1296             }
1297             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1298             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1299         }
1300         break;
1301     case AV_CODEC_ID_ADPCM_IMA_DAT4:
1302         for (channel = 0; channel < avctx->channels; channel++) {
1303             cs = &c->status[channel];
1304             samples = samples_p[channel];
1305             bytestream2_skip(&gb, 4);
1306             for (n = 0; n < nb_samples; n += 2) {
1307                 int v = bytestream2_get_byteu(&gb);
1308                 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1309                 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1310             }
1311         }
1312         break;
1313     case AV_CODEC_ID_ADPCM_IMA_APC:
1314         for (n = nb_samples >> (1 - st); n > 0; n--) {
1315             int v = bytestream2_get_byteu(&gb);
1316             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
1317             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1318         }
1319         break;
1320     case AV_CODEC_ID_ADPCM_IMA_SSI:
1321         for (n = nb_samples >> (1 - st); n > 0; n--) {
1322             int v = bytestream2_get_byteu(&gb);
1323             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0],  v >> 4  );
1324             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1325         }
1326         break;
1327     case AV_CODEC_ID_ADPCM_IMA_APM:
1328         for (n = nb_samples / 2; n > 0; n--) {
1329             for (channel = 0; channel < avctx->channels; channel++) {
1330                 int v = bytestream2_get_byteu(&gb);
1331                 *samples++  = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  );
1332                 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1333             }
1334             samples += avctx->channels;
1335         }
1336         break;
1337     case AV_CODEC_ID_ADPCM_IMA_ALP:
1338         for (n = nb_samples / 2; n > 0; n--) {
1339             for (channel = 0; channel < avctx->channels; channel++) {
1340                 int v = bytestream2_get_byteu(&gb);
1341                 *samples++  = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4  , 2);
1342                 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1343             }
1344             samples += avctx->channels;
1345         }
1346         break;
1347     case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1348         for (n = 0; n < nb_samples / 2; n++) {
1349             int v = bytestream2_get_byteu(&gb);
1350             *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v & 0x0F);
1351             *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v >> 4);
1352         }
1353         break;
1354     case AV_CODEC_ID_ADPCM_IMA_OKI:
1355         for (n = nb_samples >> (1 - st); n > 0; n--) {
1356             int v = bytestream2_get_byteu(&gb);
1357             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );
1358             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1359         }
1360         break;
1361     case AV_CODEC_ID_ADPCM_IMA_RAD:
1362         for (channel = 0; channel < avctx->channels; channel++) {
1363             cs = &c->status[channel];
1364             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1365             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1366             if (cs->step_index > 88u){
1367                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1368                        channel, cs->step_index);
1369                 return AVERROR_INVALIDDATA;
1370             }
1371         }
1372         for (n = 0; n < nb_samples / 2; n++) {
1373             int byte[2];
1374
1375             byte[0] = bytestream2_get_byteu(&gb);
1376             if (st)
1377                 byte[1] = bytestream2_get_byteu(&gb);
1378             for(channel = 0; channel < avctx->channels; channel++) {
1379                 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1380             }
1381             for(channel = 0; channel < avctx->channels; channel++) {
1382                 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4  , 3);
1383             }
1384         }
1385         break;
1386     case AV_CODEC_ID_ADPCM_IMA_WS:
1387         if (c->vqa_version == 3) {
1388             for (channel = 0; channel < avctx->channels; channel++) {
1389                 int16_t *smp = samples_p[channel];
1390
1391                 for (n = nb_samples / 2; n > 0; n--) {
1392                     int v = bytestream2_get_byteu(&gb);
1393                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1394                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1395                 }
1396             }
1397         } else {
1398             for (n = nb_samples / 2; n > 0; n--) {
1399                 for (channel = 0; channel < avctx->channels; channel++) {
1400                     int v = bytestream2_get_byteu(&gb);
1401                     *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1402                     samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1403                 }
1404                 samples += avctx->channels;
1405             }
1406         }
1407         bytestream2_seek(&gb, 0, SEEK_END);
1408         break;
1409     case AV_CODEC_ID_ADPCM_XA:
1410     {
1411         int16_t *out0 = samples_p[0];
1412         int16_t *out1 = samples_p[1];
1413         int samples_per_block = 28 * (3 - avctx->channels) * 4;
1414         int sample_offset = 0;
1415         int bytes_remaining;
1416         while (bytestream2_get_bytes_left(&gb) >= 128) {
1417             if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1418                                  &c->status[0], &c->status[1],
1419                                  avctx->channels, sample_offset)) < 0)
1420                 return ret;
1421             bytestream2_skipu(&gb, 128);
1422             sample_offset += samples_per_block;
1423         }
1424         /* Less than a full block of data left, e.g. when reading from
1425          * 2324 byte per sector XA; the remainder is padding */
1426         bytes_remaining = bytestream2_get_bytes_left(&gb);
1427         if (bytes_remaining > 0) {
1428             bytestream2_skip(&gb, bytes_remaining);
1429         }
1430         break;
1431     }
1432     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1433         for (i=0; i<=st; i++) {
1434             c->status[i].step_index = bytestream2_get_le32u(&gb);
1435             if (c->status[i].step_index > 88u) {
1436                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1437                        i, c->status[i].step_index);
1438                 return AVERROR_INVALIDDATA;
1439             }
1440         }
1441         for (i=0; i<=st; i++) {
1442             c->status[i].predictor  = bytestream2_get_le32u(&gb);
1443             if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1444                 return AVERROR_INVALIDDATA;
1445         }
1446
1447         for (n = nb_samples >> (1 - st); n > 0; n--) {
1448             int byte   = bytestream2_get_byteu(&gb);
1449             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
1450             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1451         }
1452         break;
1453     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1454         for (n = nb_samples >> (1 - st); n > 0; n--) {
1455             int byte = bytestream2_get_byteu(&gb);
1456             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
1457             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1458         }
1459         break;
1460     case AV_CODEC_ID_ADPCM_EA:
1461     {
1462         int previous_left_sample, previous_right_sample;
1463         int current_left_sample, current_right_sample;
1464         int next_left_sample, next_right_sample;
1465         int coeff1l, coeff2l, coeff1r, coeff2r;
1466         int shift_left, shift_right;
1467
1468         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1469            each coding 28 stereo samples. */
1470
1471         if(avctx->channels != 2)
1472             return AVERROR_INVALIDDATA;
1473
1474         current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
1475         previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1476         current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1477         previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1478
1479         for (count1 = 0; count1 < nb_samples / 28; count1++) {
1480             int byte = bytestream2_get_byteu(&gb);
1481             coeff1l = ea_adpcm_table[ byte >> 4       ];
1482             coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
1483             coeff1r = ea_adpcm_table[ byte & 0x0F];
1484             coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1485
1486             byte = bytestream2_get_byteu(&gb);
1487             shift_left  = 20 - (byte >> 4);
1488             shift_right = 20 - (byte & 0x0F);
1489
1490             for (count2 = 0; count2 < 28; count2++) {
1491                 byte = bytestream2_get_byteu(&gb);
1492                 next_left_sample  = sign_extend(byte >> 4, 4) * (1 << shift_left);
1493                 next_right_sample = sign_extend(byte,      4) * (1 << shift_right);
1494
1495                 next_left_sample = (next_left_sample +
1496                     (current_left_sample * coeff1l) +
1497                     (previous_left_sample * coeff2l) + 0x80) >> 8;
1498                 next_right_sample = (next_right_sample +
1499                     (current_right_sample * coeff1r) +
1500                     (previous_right_sample * coeff2r) + 0x80) >> 8;
1501
1502                 previous_left_sample = current_left_sample;
1503                 current_left_sample = av_clip_int16(next_left_sample);
1504                 previous_right_sample = current_right_sample;
1505                 current_right_sample = av_clip_int16(next_right_sample);
1506                 *samples++ = current_left_sample;
1507                 *samples++ = current_right_sample;
1508             }
1509         }
1510
1511         bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1512
1513         break;
1514     }
1515     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1516     {
1517         int coeff[2][2], shift[2];
1518
1519         for(channel = 0; channel < avctx->channels; channel++) {
1520             int byte = bytestream2_get_byteu(&gb);
1521             for (i=0; i<2; i++)
1522                 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1523             shift[channel] = 20 - (byte & 0x0F);
1524         }
1525         for (count1 = 0; count1 < nb_samples / 2; count1++) {
1526             int byte[2];
1527
1528             byte[0] = bytestream2_get_byteu(&gb);
1529             if (st) byte[1] = bytestream2_get_byteu(&gb);
1530             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1531                 for(channel = 0; channel < avctx->channels; channel++) {
1532                     int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1533                     sample = (sample +
1534                              c->status[channel].sample1 * coeff[channel][0] +
1535                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1536                     c->status[channel].sample2 = c->status[channel].sample1;
1537                     c->status[channel].sample1 = av_clip_int16(sample);
1538                     *samples++ = c->status[channel].sample1;
1539                 }
1540             }
1541         }
1542         bytestream2_seek(&gb, 0, SEEK_END);
1543         break;
1544     }
1545     case AV_CODEC_ID_ADPCM_EA_R1:
1546     case AV_CODEC_ID_ADPCM_EA_R2:
1547     case AV_CODEC_ID_ADPCM_EA_R3: {
1548         /* channel numbering
1549            2chan: 0=fl, 1=fr
1550            4chan: 0=fl, 1=rl, 2=fr, 3=rr
1551            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1552         const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1553         int previous_sample, current_sample, next_sample;
1554         int coeff1, coeff2;
1555         int shift;
1556         unsigned int channel;
1557         uint16_t *samplesC;
1558         int count = 0;
1559         int offsets[6];
1560
1561         for (channel=0; channel<avctx->channels; channel++)
1562             offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1563                                              bytestream2_get_le32(&gb)) +
1564                                (avctx->channels + 1) * 4;
1565
1566         for (channel=0; channel<avctx->channels; channel++) {
1567             bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1568             samplesC = samples_p[channel];
1569
1570             if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1571                 current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
1572                 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1573             } else {
1574                 current_sample  = c->status[channel].predictor;
1575                 previous_sample = c->status[channel].prev_sample;
1576             }
1577
1578             for (count1 = 0; count1 < nb_samples / 28; count1++) {
1579                 int byte = bytestream2_get_byte(&gb);
1580                 if (byte == 0xEE) {  /* only seen in R2 and R3 */
1581                     current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
1582                     previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1583
1584                     for (count2=0; count2<28; count2++)
1585                         *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1586                 } else {
1587                     coeff1 = ea_adpcm_table[ byte >> 4     ];
1588                     coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1589                     shift = 20 - (byte & 0x0F);
1590
1591                     for (count2=0; count2<28; count2++) {
1592                         if (count2 & 1)
1593                             next_sample = (unsigned)sign_extend(byte,    4) << shift;
1594                         else {
1595                             byte = bytestream2_get_byte(&gb);
1596                             next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1597                         }
1598
1599                         next_sample += (current_sample  * coeff1) +
1600                                        (previous_sample * coeff2);
1601                         next_sample = av_clip_int16(next_sample >> 8);
1602
1603                         previous_sample = current_sample;
1604                         current_sample  = next_sample;
1605                         *samplesC++ = current_sample;
1606                     }
1607                 }
1608             }
1609             if (!count) {
1610                 count = count1;
1611             } else if (count != count1) {
1612                 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1613                 count = FFMAX(count, count1);
1614             }
1615
1616             if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1617                 c->status[channel].predictor   = current_sample;
1618                 c->status[channel].prev_sample = previous_sample;
1619             }
1620         }
1621
1622         frame->nb_samples = count * 28;
1623         bytestream2_seek(&gb, 0, SEEK_END);
1624         break;
1625     }
1626     case AV_CODEC_ID_ADPCM_EA_XAS:
1627         for (channel=0; channel<avctx->channels; channel++) {
1628             int coeff[2][4], shift[4];
1629             int16_t *s = samples_p[channel];
1630             for (n = 0; n < 4; n++, s += 32) {
1631                 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1632                 for (i=0; i<2; i++)
1633                     coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1634                 s[0] = val & ~0x0F;
1635
1636                 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1637                 shift[n] = 20 - (val & 0x0F);
1638                 s[1] = val & ~0x0F;
1639             }
1640
1641             for (m=2; m<32; m+=2) {
1642                 s = &samples_p[channel][m];
1643                 for (n = 0; n < 4; n++, s += 32) {
1644                     int level, pred;
1645                     int byte = bytestream2_get_byteu(&gb);
1646
1647                     level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1648                     pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1649                     s[0]  = av_clip_int16((level + pred + 0x80) >> 8);
1650
1651                     level = sign_extend(byte, 4) * (1 << shift[n]);
1652                     pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1653                     s[1]  = av_clip_int16((level + pred + 0x80) >> 8);
1654                 }
1655             }
1656         }
1657         break;
1658     case AV_CODEC_ID_ADPCM_IMA_AMV:
1659         c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1660         c->status[0].step_index = bytestream2_get_byteu(&gb);
1661         bytestream2_skipu(&gb, 5);
1662         if (c->status[0].step_index > 88u) {
1663             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1664                    c->status[0].step_index);
1665             return AVERROR_INVALIDDATA;
1666         }
1667
1668         for (n = nb_samples >> (1 - st); n > 0; n--) {
1669             int v = bytestream2_get_byteu(&gb);
1670
1671             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1672             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1673         }
1674         break;
1675     case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1676         for (i = 0; i < avctx->channels; i++) {
1677             c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1678             c->status[i].step_index = bytestream2_get_byteu(&gb);
1679             bytestream2_skipu(&gb, 1);
1680             if (c->status[i].step_index > 88u) {
1681                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1682                        c->status[i].step_index);
1683                 return AVERROR_INVALIDDATA;
1684             }
1685         }
1686
1687         for (n = nb_samples >> (1 - st); n > 0; n--) {
1688             int v = bytestream2_get_byteu(&gb);
1689
1690             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1691             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1692         }
1693         break;
1694     case AV_CODEC_ID_ADPCM_CT:
1695         for (n = nb_samples >> (1 - st); n > 0; n--) {
1696             int v = bytestream2_get_byteu(&gb);
1697             *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
1698             *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1699         }
1700         break;
1701     case AV_CODEC_ID_ADPCM_SBPRO_4:
1702     case AV_CODEC_ID_ADPCM_SBPRO_3:
1703     case AV_CODEC_ID_ADPCM_SBPRO_2:
1704         if (!c->status[0].step_index) {
1705             /* the first byte is a raw sample */
1706             *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1707             if (st)
1708                 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1709             c->status[0].step_index = 1;
1710             nb_samples--;
1711         }
1712         if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1713             for (n = nb_samples >> (1 - st); n > 0; n--) {
1714                 int byte = bytestream2_get_byteu(&gb);
1715                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1716                                                        byte >> 4,   4, 0);
1717                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1718                                                        byte & 0x0F, 4, 0);
1719             }
1720         } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1721             for (n = (nb_samples<<st) / 3; n > 0; n--) {
1722                 int byte = bytestream2_get_byteu(&gb);
1723                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1724                                                         byte >> 5        , 3, 0);
1725                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1726                                                        (byte >> 2) & 0x07, 3, 0);
1727                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1728                                                         byte & 0x03,       2, 0);
1729             }
1730         } else {
1731             for (n = nb_samples >> (2 - st); n > 0; n--) {
1732                 int byte = bytestream2_get_byteu(&gb);
1733                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1734                                                         byte >> 6        , 2, 2);
1735                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1736                                                        (byte >> 4) & 0x03, 2, 2);
1737                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1738                                                        (byte >> 2) & 0x03, 2, 2);
1739                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1740                                                         byte & 0x03,       2, 2);
1741             }
1742         }
1743         break;
1744     case AV_CODEC_ID_ADPCM_SWF:
1745         adpcm_swf_decode(avctx, buf, buf_size, samples);
1746         bytestream2_seek(&gb, 0, SEEK_END);
1747         break;
1748     case AV_CODEC_ID_ADPCM_YAMAHA:
1749         for (n = nb_samples >> (1 - st); n > 0; n--) {
1750             int v = bytestream2_get_byteu(&gb);
1751             *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1752             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1753         }
1754         break;
1755     case AV_CODEC_ID_ADPCM_AICA:
1756         if (!c->has_status) {
1757             for (channel = 0; channel < avctx->channels; channel++)
1758                 c->status[channel].step = 0;
1759             c->has_status = 1;
1760         }
1761         for (channel = 0; channel < avctx->channels; channel++) {
1762             samples = samples_p[channel];
1763             for (n = nb_samples >> 1; n > 0; n--) {
1764                 int v = bytestream2_get_byteu(&gb);
1765                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1766                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4  );
1767             }
1768         }
1769         break;
1770     case AV_CODEC_ID_ADPCM_AFC:
1771     {
1772         int samples_per_block;
1773         int blocks;
1774
1775         if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1776             samples_per_block = avctx->extradata[0] / 16;
1777             blocks = nb_samples / avctx->extradata[0];
1778         } else {
1779             samples_per_block = nb_samples / 16;
1780             blocks = 1;
1781         }
1782
1783         for (m = 0; m < blocks; m++) {
1784         for (channel = 0; channel < avctx->channels; channel++) {
1785             int prev1 = c->status[channel].sample1;
1786             int prev2 = c->status[channel].sample2;
1787
1788             samples = samples_p[channel] + m * 16;
1789             /* Read in every sample for this channel.  */
1790             for (i = 0; i < samples_per_block; i++) {
1791                 int byte = bytestream2_get_byteu(&gb);
1792                 int scale = 1 << (byte >> 4);
1793                 int index = byte & 0xf;
1794                 int factor1 = ff_adpcm_afc_coeffs[0][index];
1795                 int factor2 = ff_adpcm_afc_coeffs[1][index];
1796
1797                 /* Decode 16 samples.  */
1798                 for (n = 0; n < 16; n++) {
1799                     int32_t sampledat;
1800
1801                     if (n & 1) {
1802                         sampledat = sign_extend(byte, 4);
1803                     } else {
1804                         byte = bytestream2_get_byteu(&gb);
1805                         sampledat = sign_extend(byte >> 4, 4);
1806                     }
1807
1808                     sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1809                                 sampledat * scale;
1810                     *samples = av_clip_int16(sampledat);
1811                     prev2 = prev1;
1812                     prev1 = *samples++;
1813                 }
1814             }
1815
1816             c->status[channel].sample1 = prev1;
1817             c->status[channel].sample2 = prev2;
1818         }
1819         }
1820         bytestream2_seek(&gb, 0, SEEK_END);
1821         break;
1822     }
1823     case AV_CODEC_ID_ADPCM_THP:
1824     case AV_CODEC_ID_ADPCM_THP_LE:
1825     {
1826         int table[14][16];
1827         int ch;
1828
1829 #define THP_GET16(g) \
1830     sign_extend( \
1831         avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1832         bytestream2_get_le16u(&(g)) : \
1833         bytestream2_get_be16u(&(g)), 16)
1834
1835         if (avctx->extradata) {
1836             GetByteContext tb;
1837             if (avctx->extradata_size < 32 * avctx->channels) {
1838                 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1839                 return AVERROR_INVALIDDATA;
1840             }
1841
1842             bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1843             for (i = 0; i < avctx->channels; i++)
1844                 for (n = 0; n < 16; n++)
1845                     table[i][n] = THP_GET16(tb);
1846         } else {
1847             for (i = 0; i < avctx->channels; i++)
1848                 for (n = 0; n < 16; n++)
1849                     table[i][n] = THP_GET16(gb);
1850
1851             if (!c->has_status) {
1852                 /* Initialize the previous sample.  */
1853                 for (i = 0; i < avctx->channels; i++) {
1854                     c->status[i].sample1 = THP_GET16(gb);
1855                     c->status[i].sample2 = THP_GET16(gb);
1856                 }
1857                 c->has_status = 1;
1858             } else {
1859                 bytestream2_skip(&gb, avctx->channels * 4);
1860             }
1861         }
1862
1863         for (ch = 0; ch < avctx->channels; ch++) {
1864             samples = samples_p[ch];
1865
1866             /* Read in every sample for this channel.  */
1867             for (i = 0; i < (nb_samples + 13) / 14; i++) {
1868                 int byte = bytestream2_get_byteu(&gb);
1869                 int index = (byte >> 4) & 7;
1870                 unsigned int exp = byte & 0x0F;
1871                 int64_t factor1 = table[ch][index * 2];
1872                 int64_t factor2 = table[ch][index * 2 + 1];
1873
1874                 /* Decode 14 samples.  */
1875                 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1876                     int32_t sampledat;
1877
1878                     if (n & 1) {
1879                         sampledat = sign_extend(byte, 4);
1880                     } else {
1881                         byte = bytestream2_get_byteu(&gb);
1882                         sampledat = sign_extend(byte >> 4, 4);
1883                     }
1884
1885                     sampledat = ((c->status[ch].sample1 * factor1
1886                                 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1887                     *samples = av_clip_int16(sampledat);
1888                     c->status[ch].sample2 = c->status[ch].sample1;
1889                     c->status[ch].sample1 = *samples++;
1890                 }
1891             }
1892         }
1893         break;
1894     }
1895     case AV_CODEC_ID_ADPCM_DTK:
1896         for (channel = 0; channel < avctx->channels; channel++) {
1897             samples = samples_p[channel];
1898
1899             /* Read in every sample for this channel.  */
1900             for (i = 0; i < nb_samples / 28; i++) {
1901                 int byte, header;
1902                 if (channel)
1903                     bytestream2_skipu(&gb, 1);
1904                 header = bytestream2_get_byteu(&gb);
1905                 bytestream2_skipu(&gb, 3 - channel);
1906
1907                 /* Decode 28 samples.  */
1908                 for (n = 0; n < 28; n++) {
1909                     int32_t sampledat, prev;
1910
1911                     switch (header >> 4) {
1912                     case 1:
1913                         prev = (c->status[channel].sample1 * 0x3c);
1914                         break;
1915                     case 2:
1916                         prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1917                         break;
1918                     case 3:
1919                         prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1920                         break;
1921                     default:
1922                         prev = 0;
1923                     }
1924
1925                     prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1926
1927                     byte = bytestream2_get_byteu(&gb);
1928                     if (!channel)
1929                         sampledat = sign_extend(byte, 4);
1930                     else
1931                         sampledat = sign_extend(byte >> 4, 4);
1932
1933                     sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1934                     *samples++ = av_clip_int16(sampledat >> 6);
1935                     c->status[channel].sample2 = c->status[channel].sample1;
1936                     c->status[channel].sample1 = sampledat;
1937                 }
1938             }
1939             if (!channel)
1940                 bytestream2_seek(&gb, 0, SEEK_SET);
1941         }
1942         break;
1943     case AV_CODEC_ID_ADPCM_PSX:
1944         for (channel = 0; channel < avctx->channels; channel++) {
1945             samples = samples_p[channel];
1946
1947             /* Read in every sample for this channel.  */
1948             for (i = 0; i < nb_samples / 28; i++) {
1949                 int filter, shift, flag, byte;
1950
1951                 filter = bytestream2_get_byteu(&gb);
1952                 shift  = filter & 0xf;
1953                 filter = filter >> 4;
1954                 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1955                     return AVERROR_INVALIDDATA;
1956                 flag   = bytestream2_get_byteu(&gb);
1957
1958                 /* Decode 28 samples.  */
1959                 for (n = 0; n < 28; n++) {
1960                     int sample = 0, scale;
1961
1962                     if (flag < 0x07) {
1963                         if (n & 1) {
1964                             scale = sign_extend(byte >> 4, 4);
1965                         } else {
1966                             byte  = bytestream2_get_byteu(&gb);
1967                             scale = sign_extend(byte, 4);
1968                         }
1969
1970                         scale  = scale * (1 << 12);
1971                         sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1972                     }
1973                     *samples++ = av_clip_int16(sample);
1974                     c->status[channel].sample2 = c->status[channel].sample1;
1975                     c->status[channel].sample1 = sample;
1976                 }
1977             }
1978         }
1979         break;
1980     case AV_CODEC_ID_ADPCM_ARGO:
1981         /*
1982          * The format of each block:
1983          *   uint8_t left_control;
1984          *   uint4_t left_samples[nb_samples];
1985          *   ---- and if stereo ----
1986          *   uint8_t right_control;
1987          *   uint4_t right_samples[nb_samples];
1988          *
1989          * Format of the control byte:
1990          * MSB [SSSSDRRR] LSB
1991          *   S = (Shift Amount - 2)
1992          *   D = Decoder flag.
1993          *   R = Reserved
1994          *
1995          * Each block relies on the previous two samples of each channel.
1996          * They should be 0 initially.
1997          */
1998         for (channel = 0; channel < avctx->channels; channel++) {
1999             int control, shift;
2000
2001             samples = samples_p[channel];
2002             cs = c->status + channel;
2003
2004             /* Get the control byte and decode the samples, 2 at a time. */
2005             control = bytestream2_get_byteu(&gb);
2006             shift = (control >> 4) + 2;
2007
2008             for (n = 0; n < nb_samples / 2; n++) {
2009                 int sample = bytestream2_get_byteu(&gb);
2010                 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 4, 4), control, shift);
2011                 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 0, 4), control, shift);
2012             }
2013         }
2014         break;
2015     case AV_CODEC_ID_ADPCM_ZORK:
2016         if (!c->has_status) {
2017             for (channel = 0; channel < avctx->channels; channel++) {
2018                 c->status[channel].predictor  = 0;
2019                 c->status[channel].step_index = 0;
2020             }
2021             c->has_status = 1;
2022         }
2023         for (n = 0; n < nb_samples * avctx->channels; n++) {
2024             int v = bytestream2_get_byteu(&gb);
2025             *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
2026         }
2027         break;
2028     case AV_CODEC_ID_ADPCM_IMA_MTF:
2029         for (n = nb_samples / 2; n > 0; n--) {
2030             for (channel = 0; channel < avctx->channels; channel++) {
2031                 int v = bytestream2_get_byteu(&gb);
2032                 *samples++  = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2033                 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2034             }
2035             samples += avctx->channels;
2036         }
2037         break;
2038     default:
2039         av_assert0(0); // unsupported codec_id should not happen
2040     }
2041
2042     if (avpkt->size && bytestream2_tell(&gb) == 0) {
2043         av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2044         return AVERROR_INVALIDDATA;
2045     }
2046
2047     *got_frame_ptr = 1;
2048
2049     if (avpkt->size < bytestream2_tell(&gb)) {
2050         av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2051         return avpkt->size;
2052     }
2053
2054     return bytestream2_tell(&gb);
2055 }
2056
2057 static void adpcm_flush(AVCodecContext *avctx)
2058 {
2059     ADPCMDecodeContext *c = avctx->priv_data;
2060     c->has_status = 0;
2061 }
2062
2063
2064 static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
2065                                                         AV_SAMPLE_FMT_NONE };
2066 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2067                                                         AV_SAMPLE_FMT_NONE };
2068 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2069                                                         AV_SAMPLE_FMT_S16P,
2070                                                         AV_SAMPLE_FMT_NONE };
2071
2072 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2073 AVCodec ff_ ## name_ ## _decoder = {                        \
2074     .name           = #name_,                               \
2075     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
2076     .type           = AVMEDIA_TYPE_AUDIO,                   \
2077     .id             = id_,                                  \
2078     .priv_data_size = sizeof(ADPCMDecodeContext),           \
2079     .init           = adpcm_decode_init,                    \
2080     .decode         = adpcm_decode_frame,                   \
2081     .flush          = adpcm_flush,                          \
2082     .capabilities   = AV_CODEC_CAP_DR1,                     \
2083     .sample_fmts    = sample_fmts_,                         \
2084 }
2085
2086 /* Note: Do not forget to add new entries to the Makefile as well. */
2087 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
2088 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
2089 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM,         sample_fmts_s16,  adpcm_agm,         "ADPCM AmuseGraphics Movie");
2090 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA,        sample_fmts_s16p, adpcm_aica,        "ADPCM Yamaha AICA");
2091 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO,        sample_fmts_s16p, adpcm_argo,        "ADPCM Argonaut Games");
2092 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
2093 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
2094 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
2095 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2096 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
2097 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
2098 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
2099 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
2100 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
2101 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
2102 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM,     sample_fmts_s16,  adpcm_ima_apm,     "ADPCM IMA Ubisoft APM");
2103 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16,  adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2104 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,  adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
2105 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
2106 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
2107 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2108 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2109 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
2110 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF,     sample_fmts_s16,  adpcm_ima_mtf,     "ADPCM IMA Capcom's MT Framework");
2111 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
2112 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
2113 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD,     sample_fmts_s16,  adpcm_ima_rad,     "ADPCM IMA Radical");
2114 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI,     sample_fmts_s16,  adpcm_ima_ssi,     "ADPCM IMA Simon & Schuster Interactive");
2115 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
2116 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP,     sample_fmts_s16,  adpcm_ima_alp,     "ADPCM IMA High Voltage Software ALP");
2117 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
2118 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
2119 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_both, adpcm_ms,          "ADPCM Microsoft");
2120 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF,        sample_fmts_s16p, adpcm_mtaf,        "ADPCM MTAF");
2121 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX,         sample_fmts_s16p, adpcm_psx,         "ADPCM Playstation");
2122 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
2123 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
2124 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
2125 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
2126 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE,      sample_fmts_s16p, adpcm_thp_le,      "ADPCM Nintendo THP (little-endian)");
2127 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo THP");
2128 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
2129 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");
2130 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK,        sample_fmts_s16,  adpcm_zork,        "ADPCM Zork");