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