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