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