]> git.sesse.net Git - ffmpeg/blob - libavcodec/adpcm.c
avcodec/adpcm: XA: Check shift similar to filter
[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_clip_intp2(AV_RL32(avctx->extradata +  0), 18);
167             c->status[0].step_index = av_clip(AV_RL32(avctx->extradata +  4), 0, 88);
168             c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  8), 18);
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         if (shift < 0) {
563             avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
564             shift = 0;
565         }
566         f0 = xa_adpcm_table[filter][0];
567         f1 = xa_adpcm_table[filter][1];
568
569         s_1 = left->sample1;
570         s_2 = left->sample2;
571
572         for(j=0;j<28;j++) {
573             d = in[16+i+j*4];
574
575             t = sign_extend(d, 4);
576             s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
577             s_2 = s_1;
578             s_1 = av_clip_int16(s);
579             out0[j] = s_1;
580         }
581
582         if (channels == 2) {
583             left->sample1 = s_1;
584             left->sample2 = s_2;
585             s_1 = right->sample1;
586             s_2 = right->sample2;
587         }
588
589         shift  = 12 - (in[5+i*2] & 15);
590         filter = in[5+i*2] >> 4;
591         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
592             avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
593             filter=0;
594         }
595         if (shift < 0) {
596             avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
597             shift = 0;
598         }
599
600         f0 = xa_adpcm_table[filter][0];
601         f1 = xa_adpcm_table[filter][1];
602
603         for(j=0;j<28;j++) {
604             d = in[16+i+j*4];
605
606             t = sign_extend(d >> 4, 4);
607             s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
608             s_2 = s_1;
609             s_1 = av_clip_int16(s);
610             out1[j] = s_1;
611         }
612
613         if (channels == 2) {
614             right->sample1 = s_1;
615             right->sample2 = s_2;
616         } else {
617             left->sample1 = s_1;
618             left->sample2 = s_2;
619         }
620
621         out0 += 28 * (3 - channels);
622         out1 += 28 * (3 - channels);
623     }
624
625     return 0;
626 }
627
628 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
629 {
630     ADPCMDecodeContext *c = avctx->priv_data;
631     GetBitContext gb;
632     const int8_t *table;
633     int k0, signmask, nb_bits, count;
634     int size = buf_size*8;
635     int i;
636
637     init_get_bits(&gb, buf, size);
638
639     //read bits & initial values
640     nb_bits = get_bits(&gb, 2)+2;
641     table = swf_index_tables[nb_bits-2];
642     k0 = 1 << (nb_bits-2);
643     signmask = 1 << (nb_bits-1);
644
645     while (get_bits_count(&gb) <= size - 22*avctx->channels) {
646         for (i = 0; i < avctx->channels; i++) {
647             *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
648             c->status[i].step_index = get_bits(&gb, 6);
649         }
650
651         for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
652             int i;
653
654             for (i = 0; i < avctx->channels; i++) {
655                 // similar to IMA adpcm
656                 int delta = get_bits(&gb, nb_bits);
657                 int step = ff_adpcm_step_table[c->status[i].step_index];
658                 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
659                 int k = k0;
660
661                 do {
662                     if (delta & k)
663                         vpdiff += step;
664                     step >>= 1;
665                     k >>= 1;
666                 } while(k);
667                 vpdiff += step;
668
669                 if (delta & signmask)
670                     c->status[i].predictor -= vpdiff;
671                 else
672                     c->status[i].predictor += vpdiff;
673
674                 c->status[i].step_index += table[delta & (~signmask)];
675
676                 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
677                 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
678
679                 *samples++ = c->status[i].predictor;
680             }
681         }
682     }
683 }
684
685 static inline int16_t adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int control, int shift)
686 {
687     int sample = nibble * (1 << shift);
688
689     if (control & 0x04)
690         sample += (8 * cs->sample1) - (4 * cs->sample2);
691     else
692         sample += 4 * cs->sample1;
693
694     sample = av_clip_int16(sample >> 2);
695
696     cs->sample2 = cs->sample1;
697     cs->sample1 = sample;
698
699     return sample;
700 }
701
702 /**
703  * Get the number of samples (per channel) that will be decoded from the packet.
704  * In one case, this is actually the maximum number of samples possible to
705  * decode with the given buf_size.
706  *
707  * @param[out] coded_samples set to the number of samples as coded in the
708  *                           packet, or 0 if the codec does not encode the
709  *                           number of samples in each frame.
710  * @param[out] approx_nb_samples set to non-zero if the number of samples
711  *                               returned is an approximation.
712  */
713 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
714                           int buf_size, int *coded_samples, int *approx_nb_samples)
715 {
716     ADPCMDecodeContext *s = avctx->priv_data;
717     int nb_samples        = 0;
718     int ch                = avctx->channels;
719     int has_coded_samples = 0;
720     int header_size;
721
722     *coded_samples = 0;
723     *approx_nb_samples = 0;
724
725     if(ch <= 0)
726         return 0;
727
728     switch (avctx->codec->id) {
729     /* constant, only check buf_size */
730     case AV_CODEC_ID_ADPCM_EA_XAS:
731         if (buf_size < 76 * ch)
732             return 0;
733         nb_samples = 128;
734         break;
735     case AV_CODEC_ID_ADPCM_IMA_QT:
736         if (buf_size < 34 * ch)
737             return 0;
738         nb_samples = 64;
739         break;
740     case AV_CODEC_ID_ADPCM_ARGO:
741         if (buf_size < 17 * ch)
742             return 0;
743         nb_samples = 32;
744         break;
745     /* simple 4-bit adpcm */
746     case AV_CODEC_ID_ADPCM_CT:
747     case AV_CODEC_ID_ADPCM_IMA_APC:
748     case AV_CODEC_ID_ADPCM_IMA_CUNNING:
749     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
750     case AV_CODEC_ID_ADPCM_IMA_OKI:
751     case AV_CODEC_ID_ADPCM_IMA_WS:
752     case AV_CODEC_ID_ADPCM_YAMAHA:
753     case AV_CODEC_ID_ADPCM_AICA:
754     case AV_CODEC_ID_ADPCM_IMA_SSI:
755     case AV_CODEC_ID_ADPCM_IMA_APM:
756     case AV_CODEC_ID_ADPCM_IMA_ALP:
757     case AV_CODEC_ID_ADPCM_IMA_MTF:
758         nb_samples = buf_size * 2 / ch;
759         break;
760     }
761     if (nb_samples)
762         return nb_samples;
763
764     /* simple 4-bit adpcm, with header */
765     header_size = 0;
766     switch (avctx->codec->id) {
767         case AV_CODEC_ID_ADPCM_4XM:
768         case AV_CODEC_ID_ADPCM_AGM:
769         case AV_CODEC_ID_ADPCM_IMA_DAT4:
770         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
771         case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
772         case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
773     }
774     if (header_size > 0)
775         return (buf_size - header_size) * 2 / ch;
776
777     /* more complex formats */
778     switch (avctx->codec->id) {
779     case AV_CODEC_ID_ADPCM_EA:
780         has_coded_samples = 1;
781         *coded_samples  = bytestream2_get_le32(gb);
782         *coded_samples -= *coded_samples % 28;
783         nb_samples      = (buf_size - 12) / 30 * 28;
784         break;
785     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
786         has_coded_samples = 1;
787         *coded_samples = bytestream2_get_le32(gb);
788         nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
789         break;
790     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
791         nb_samples = (buf_size - ch) / ch * 2;
792         break;
793     case AV_CODEC_ID_ADPCM_EA_R1:
794     case AV_CODEC_ID_ADPCM_EA_R2:
795     case AV_CODEC_ID_ADPCM_EA_R3:
796         /* maximum number of samples */
797         /* has internal offsets and a per-frame switch to signal raw 16-bit */
798         has_coded_samples = 1;
799         switch (avctx->codec->id) {
800         case AV_CODEC_ID_ADPCM_EA_R1:
801             header_size    = 4 + 9 * ch;
802             *coded_samples = bytestream2_get_le32(gb);
803             break;
804         case AV_CODEC_ID_ADPCM_EA_R2:
805             header_size    = 4 + 5 * ch;
806             *coded_samples = bytestream2_get_le32(gb);
807             break;
808         case AV_CODEC_ID_ADPCM_EA_R3:
809             header_size    = 4 + 5 * ch;
810             *coded_samples = bytestream2_get_be32(gb);
811             break;
812         }
813         *coded_samples -= *coded_samples % 28;
814         nb_samples      = (buf_size - header_size) * 2 / ch;
815         nb_samples     -= nb_samples % 28;
816         *approx_nb_samples = 1;
817         break;
818     case AV_CODEC_ID_ADPCM_IMA_DK3:
819         if (avctx->block_align > 0)
820             buf_size = FFMIN(buf_size, avctx->block_align);
821         nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
822         break;
823     case AV_CODEC_ID_ADPCM_IMA_DK4:
824         if (avctx->block_align > 0)
825             buf_size = FFMIN(buf_size, avctx->block_align);
826         if (buf_size < 4 * ch)
827             return AVERROR_INVALIDDATA;
828         nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
829         break;
830     case AV_CODEC_ID_ADPCM_IMA_RAD:
831         if (avctx->block_align > 0)
832             buf_size = FFMIN(buf_size, avctx->block_align);
833         nb_samples = (buf_size - 4 * ch) * 2 / ch;
834         break;
835     case AV_CODEC_ID_ADPCM_IMA_WAV:
836     {
837         int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
838         int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
839         if (avctx->block_align > 0)
840             buf_size = FFMIN(buf_size, avctx->block_align);
841         if (buf_size < 4 * ch)
842             return AVERROR_INVALIDDATA;
843         nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
844         break;
845     }
846     case AV_CODEC_ID_ADPCM_MS:
847         if (avctx->block_align > 0)
848             buf_size = FFMIN(buf_size, avctx->block_align);
849         nb_samples = (buf_size - 6 * ch) * 2 / ch;
850         break;
851     case AV_CODEC_ID_ADPCM_MTAF:
852         if (avctx->block_align > 0)
853             buf_size = FFMIN(buf_size, avctx->block_align);
854         nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
855         break;
856     case AV_CODEC_ID_ADPCM_SBPRO_2:
857     case AV_CODEC_ID_ADPCM_SBPRO_3:
858     case AV_CODEC_ID_ADPCM_SBPRO_4:
859     {
860         int samples_per_byte;
861         switch (avctx->codec->id) {
862         case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
863         case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
864         case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
865         }
866         if (!s->status[0].step_index) {
867             if (buf_size < ch)
868                 return AVERROR_INVALIDDATA;
869             nb_samples++;
870             buf_size -= ch;
871         }
872         nb_samples += buf_size * samples_per_byte / ch;
873         break;
874     }
875     case AV_CODEC_ID_ADPCM_SWF:
876     {
877         int buf_bits       = buf_size * 8 - 2;
878         int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
879         int block_hdr_size = 22 * ch;
880         int block_size     = block_hdr_size + nbits * ch * 4095;
881         int nblocks        = buf_bits / block_size;
882         int bits_left      = buf_bits - nblocks * block_size;
883         nb_samples         = nblocks * 4096;
884         if (bits_left >= block_hdr_size)
885             nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
886         break;
887     }
888     case AV_CODEC_ID_ADPCM_THP:
889     case AV_CODEC_ID_ADPCM_THP_LE:
890         if (avctx->extradata) {
891             nb_samples = buf_size * 14 / (8 * ch);
892             break;
893         }
894         has_coded_samples = 1;
895         bytestream2_skip(gb, 4); // channel size
896         *coded_samples  = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
897                           bytestream2_get_le32(gb) :
898                           bytestream2_get_be32(gb);
899         buf_size       -= 8 + 36 * ch;
900         buf_size       /= ch;
901         nb_samples      = buf_size / 8 * 14;
902         if (buf_size % 8 > 1)
903             nb_samples     += (buf_size % 8 - 1) * 2;
904         *approx_nb_samples = 1;
905         break;
906     case AV_CODEC_ID_ADPCM_AFC:
907         nb_samples = buf_size / (9 * ch) * 16;
908         break;
909     case AV_CODEC_ID_ADPCM_XA:
910         nb_samples = (buf_size / 128) * 224 / ch;
911         break;
912     case AV_CODEC_ID_ADPCM_DTK:
913     case AV_CODEC_ID_ADPCM_PSX:
914         nb_samples = buf_size / (16 * ch) * 28;
915         break;
916     case AV_CODEC_ID_ADPCM_ZORK:
917         nb_samples = buf_size / ch;
918         break;
919     }
920
921     /* validate coded sample count */
922     if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
923         return AVERROR_INVALIDDATA;
924
925     return nb_samples;
926 }
927
928 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
929                               int *got_frame_ptr, AVPacket *avpkt)
930 {
931     AVFrame *frame     = data;
932     const uint8_t *buf = avpkt->data;
933     int buf_size = avpkt->size;
934     ADPCMDecodeContext *c = avctx->priv_data;
935     ADPCMChannelStatus *cs;
936     int n, m, channel, i;
937     int16_t *samples;
938     int16_t **samples_p;
939     int st; /* stereo */
940     int count1, count2;
941     int nb_samples, coded_samples, approx_nb_samples, ret;
942     GetByteContext gb;
943
944     bytestream2_init(&gb, buf, buf_size);
945     nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
946     if (nb_samples <= 0) {
947         av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
948         return AVERROR_INVALIDDATA;
949     }
950
951     /* get output buffer */
952     frame->nb_samples = nb_samples;
953     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
954         return ret;
955     samples = (int16_t *)frame->data[0];
956     samples_p = (int16_t **)frame->extended_data;
957
958     /* use coded_samples when applicable */
959     /* it is always <= nb_samples, so the output buffer will be large enough */
960     if (coded_samples) {
961         if (!approx_nb_samples && coded_samples != nb_samples)
962             av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
963         frame->nb_samples = nb_samples = coded_samples;
964     }
965
966     st = avctx->channels == 2 ? 1 : 0;
967
968     switch(avctx->codec->id) {
969     case AV_CODEC_ID_ADPCM_IMA_QT:
970         /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
971            Channel data is interleaved per-chunk. */
972         for (channel = 0; channel < avctx->channels; channel++) {
973             int predictor;
974             int step_index;
975             cs = &(c->status[channel]);
976             /* (pppppp) (piiiiiii) */
977
978             /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
979             predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
980             step_index = predictor & 0x7F;
981             predictor &= ~0x7F;
982
983             if (cs->step_index == step_index) {
984                 int diff = predictor - cs->predictor;
985                 if (diff < 0)
986                     diff = - diff;
987                 if (diff > 0x7f)
988                     goto update;
989             } else {
990             update:
991                 cs->step_index = step_index;
992                 cs->predictor = predictor;
993             }
994
995             if (cs->step_index > 88u){
996                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
997                        channel, cs->step_index);
998                 return AVERROR_INVALIDDATA;
999             }
1000
1001             samples = samples_p[channel];
1002
1003             for (m = 0; m < 64; m += 2) {
1004                 int byte = bytestream2_get_byteu(&gb);
1005                 samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1006                 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  );
1007             }
1008         }
1009         break;
1010     case AV_CODEC_ID_ADPCM_IMA_WAV:
1011         for(i=0; i<avctx->channels; i++){
1012             cs = &(c->status[i]);
1013             cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1014
1015             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1016             if (cs->step_index > 88u){
1017                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1018                        i, cs->step_index);
1019                 return AVERROR_INVALIDDATA;
1020             }
1021         }
1022
1023         if (avctx->bits_per_coded_sample != 4) {
1024             int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1025             int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1026             uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1027             GetBitContext g;
1028
1029             for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1030                 for (i = 0; i < avctx->channels; i++) {
1031                     int j;
1032
1033                     cs = &c->status[i];
1034                     samples = &samples_p[i][1 + n * samples_per_block];
1035                     for (j = 0; j < block_size; j++) {
1036                         temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
1037                                         (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
1038                     }
1039                     ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1040                     if (ret < 0)
1041                         return ret;
1042                     for (m = 0; m < samples_per_block; m++) {
1043                         samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1044                                           avctx->bits_per_coded_sample);
1045                     }
1046                 }
1047             }
1048             bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1049         } else {
1050         for (n = 0; n < (nb_samples - 1) / 8; n++) {
1051             for (i = 0; i < avctx->channels; i++) {
1052                 cs = &c->status[i];
1053                 samples = &samples_p[i][1 + n * 8];
1054                 for (m = 0; m < 8; m += 2) {
1055                     int v = bytestream2_get_byteu(&gb);
1056                     samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1057                     samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1058                 }
1059             }
1060         }
1061         }
1062         break;
1063     case AV_CODEC_ID_ADPCM_4XM:
1064         for (i = 0; i < avctx->channels; i++)
1065             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1066
1067         for (i = 0; i < avctx->channels; i++) {
1068             c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1069             if (c->status[i].step_index > 88u) {
1070                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1071                        i, c->status[i].step_index);
1072                 return AVERROR_INVALIDDATA;
1073             }
1074         }
1075
1076         for (i = 0; i < avctx->channels; i++) {
1077             samples = (int16_t *)frame->data[i];
1078             cs = &c->status[i];
1079             for (n = nb_samples >> 1; n > 0; n--) {
1080                 int v = bytestream2_get_byteu(&gb);
1081                 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1082                 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
1083             }
1084         }
1085         break;
1086     case AV_CODEC_ID_ADPCM_AGM:
1087         for (i = 0; i < avctx->channels; i++)
1088             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1089         for (i = 0; i < avctx->channels; i++)
1090             c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1091
1092         for (n = 0; n < nb_samples >> (1 - st); n++) {
1093             int v = bytestream2_get_byteu(&gb);
1094             *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1095             *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1096         }
1097         break;
1098     case AV_CODEC_ID_ADPCM_MS:
1099     {
1100         int block_predictor;
1101
1102         if (avctx->channels > 2) {
1103             for (channel = 0; channel < avctx->channels; channel++) {
1104                 samples = samples_p[channel];
1105                 block_predictor = bytestream2_get_byteu(&gb);
1106                 if (block_predictor > 6) {
1107                     av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1108                            channel, block_predictor);
1109                     return AVERROR_INVALIDDATA;
1110                 }
1111                 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1112                 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1113                 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1114                 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1115                 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1116                 *samples++ = c->status[channel].sample2;
1117                 *samples++ = c->status[channel].sample1;
1118                 for(n = (nb_samples - 2) >> 1; n > 0; n--) {
1119                     int byte = bytestream2_get_byteu(&gb);
1120                     *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4  );
1121                     *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1122                 }
1123             }
1124         } else {
1125             block_predictor = bytestream2_get_byteu(&gb);
1126             if (block_predictor > 6) {
1127                 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1128                        block_predictor);
1129                 return AVERROR_INVALIDDATA;
1130             }
1131             c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1132             c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1133             if (st) {
1134                 block_predictor = bytestream2_get_byteu(&gb);
1135                 if (block_predictor > 6) {
1136                     av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1137                            block_predictor);
1138                     return AVERROR_INVALIDDATA;
1139                 }
1140                 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1141                 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1142             }
1143             c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1144             if (st){
1145                 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1146             }
1147
1148             c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1149             if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1150             c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1151             if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1152
1153             *samples++ = c->status[0].sample2;
1154             if (st) *samples++ = c->status[1].sample2;
1155             *samples++ = c->status[0].sample1;
1156             if (st) *samples++ = c->status[1].sample1;
1157             for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1158                 int byte = bytestream2_get_byteu(&gb);
1159                 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
1160                 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1161             }
1162         }
1163         break;
1164     }
1165     case AV_CODEC_ID_ADPCM_MTAF:
1166         for (channel = 0; channel < avctx->channels; channel+=2) {
1167             bytestream2_skipu(&gb, 4);
1168             c->status[channel    ].step      = bytestream2_get_le16u(&gb) & 0x1f;
1169             c->status[channel + 1].step      = bytestream2_get_le16u(&gb) & 0x1f;
1170             c->status[channel    ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1171             bytestream2_skipu(&gb, 2);
1172             c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1173             bytestream2_skipu(&gb, 2);
1174             for (n = 0; n < nb_samples; n+=2) {
1175                 int v = bytestream2_get_byteu(&gb);
1176                 samples_p[channel][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1177                 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4  );
1178             }
1179             for (n = 0; n < nb_samples; n+=2) {
1180                 int v = bytestream2_get_byteu(&gb);
1181                 samples_p[channel + 1][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1182                 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4  );
1183             }
1184         }
1185         break;
1186     case AV_CODEC_ID_ADPCM_IMA_DK4:
1187         for (channel = 0; channel < avctx->channels; channel++) {
1188             cs = &c->status[channel];
1189             cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1190             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1191             if (cs->step_index > 88u){
1192                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1193                        channel, cs->step_index);
1194                 return AVERROR_INVALIDDATA;
1195             }
1196         }
1197         for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1198             int v = bytestream2_get_byteu(&gb);
1199             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
1200             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1201         }
1202         break;
1203     case AV_CODEC_ID_ADPCM_IMA_DK3:
1204     {
1205         int last_byte = 0;
1206         int nibble;
1207         int decode_top_nibble_next = 0;
1208         int diff_channel;
1209         const int16_t *samples_end = samples + avctx->channels * nb_samples;
1210
1211         bytestream2_skipu(&gb, 10);
1212         c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1213         c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1214         c->status[0].step_index = bytestream2_get_byteu(&gb);
1215         c->status[1].step_index = bytestream2_get_byteu(&gb);
1216         if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1217             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1218                    c->status[0].step_index, c->status[1].step_index);
1219             return AVERROR_INVALIDDATA;
1220         }
1221         /* sign extend the predictors */
1222         diff_channel = c->status[1].predictor;
1223
1224         /* DK3 ADPCM support macro */
1225 #define DK3_GET_NEXT_NIBBLE() \
1226     if (decode_top_nibble_next) { \
1227         nibble = last_byte >> 4; \
1228         decode_top_nibble_next = 0; \
1229     } else { \
1230         last_byte = bytestream2_get_byteu(&gb); \
1231         nibble = last_byte & 0x0F; \
1232         decode_top_nibble_next = 1; \
1233     }
1234
1235         while (samples < samples_end) {
1236
1237             /* for this algorithm, c->status[0] is the sum channel and
1238              * c->status[1] is the diff channel */
1239
1240             /* process the first predictor of the sum channel */
1241             DK3_GET_NEXT_NIBBLE();
1242             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1243
1244             /* process the diff channel predictor */
1245             DK3_GET_NEXT_NIBBLE();
1246             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1247
1248             /* process the first pair of stereo PCM samples */
1249             diff_channel = (diff_channel + c->status[1].predictor) / 2;
1250             *samples++ = c->status[0].predictor + c->status[1].predictor;
1251             *samples++ = c->status[0].predictor - c->status[1].predictor;
1252
1253             /* process the second predictor of the sum channel */
1254             DK3_GET_NEXT_NIBBLE();
1255             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1256
1257             /* process the second pair of stereo PCM samples */
1258             diff_channel = (diff_channel + c->status[1].predictor) / 2;
1259             *samples++ = c->status[0].predictor + c->status[1].predictor;
1260             *samples++ = c->status[0].predictor - c->status[1].predictor;
1261         }
1262
1263         if ((bytestream2_tell(&gb) & 1))
1264             bytestream2_skip(&gb, 1);
1265         break;
1266     }
1267     case AV_CODEC_ID_ADPCM_IMA_ISS:
1268         for (channel = 0; channel < avctx->channels; channel++) {
1269             cs = &c->status[channel];
1270             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1271             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1272             if (cs->step_index > 88u){
1273                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1274                        channel, cs->step_index);
1275                 return AVERROR_INVALIDDATA;
1276             }
1277         }
1278
1279         for (n = nb_samples >> (1 - st); n > 0; n--) {
1280             int v1, v2;
1281             int v = bytestream2_get_byteu(&gb);
1282             /* nibbles are swapped for mono */
1283             if (st) {
1284                 v1 = v >> 4;
1285                 v2 = v & 0x0F;
1286             } else {
1287                 v2 = v >> 4;
1288                 v1 = v & 0x0F;
1289             }
1290             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1291             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1292         }
1293         break;
1294     case AV_CODEC_ID_ADPCM_IMA_DAT4:
1295         for (channel = 0; channel < avctx->channels; channel++) {
1296             cs = &c->status[channel];
1297             samples = samples_p[channel];
1298             bytestream2_skip(&gb, 4);
1299             for (n = 0; n < nb_samples; n += 2) {
1300                 int v = bytestream2_get_byteu(&gb);
1301                 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1302                 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1303             }
1304         }
1305         break;
1306     case AV_CODEC_ID_ADPCM_IMA_APC:
1307         for (n = nb_samples >> (1 - st); n > 0; n--) {
1308             int v = bytestream2_get_byteu(&gb);
1309             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
1310             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1311         }
1312         break;
1313     case AV_CODEC_ID_ADPCM_IMA_SSI:
1314         for (n = nb_samples >> (1 - st); n > 0; n--) {
1315             int v = bytestream2_get_byteu(&gb);
1316             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0],  v >> 4  );
1317             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1318         }
1319         break;
1320     case AV_CODEC_ID_ADPCM_IMA_APM:
1321         for (n = nb_samples / 2; n > 0; n--) {
1322             for (channel = 0; channel < avctx->channels; channel++) {
1323                 int v = bytestream2_get_byteu(&gb);
1324                 *samples++  = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  );
1325                 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1326             }
1327             samples += avctx->channels;
1328         }
1329         break;
1330     case AV_CODEC_ID_ADPCM_IMA_ALP:
1331         for (n = nb_samples / 2; n > 0; n--) {
1332             for (channel = 0; channel < avctx->channels; channel++) {
1333                 int v = bytestream2_get_byteu(&gb);
1334                 *samples++  = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4  , 2);
1335                 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1336             }
1337             samples += avctx->channels;
1338         }
1339         break;
1340     case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1341         for (n = 0; n < nb_samples / 2; n++) {
1342             int v = bytestream2_get_byteu(&gb);
1343             *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v & 0x0F);
1344             *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v >> 4);
1345         }
1346         break;
1347     case AV_CODEC_ID_ADPCM_IMA_OKI:
1348         for (n = nb_samples >> (1 - st); n > 0; n--) {
1349             int v = bytestream2_get_byteu(&gb);
1350             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );
1351             *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1352         }
1353         break;
1354     case AV_CODEC_ID_ADPCM_IMA_RAD:
1355         for (channel = 0; channel < avctx->channels; channel++) {
1356             cs = &c->status[channel];
1357             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1358             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1359             if (cs->step_index > 88u){
1360                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1361                        channel, cs->step_index);
1362                 return AVERROR_INVALIDDATA;
1363             }
1364         }
1365         for (n = 0; n < nb_samples / 2; n++) {
1366             int byte[2];
1367
1368             byte[0] = bytestream2_get_byteu(&gb);
1369             if (st)
1370                 byte[1] = bytestream2_get_byteu(&gb);
1371             for(channel = 0; channel < avctx->channels; channel++) {
1372                 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1373             }
1374             for(channel = 0; channel < avctx->channels; channel++) {
1375                 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4  , 3);
1376             }
1377         }
1378         break;
1379     case AV_CODEC_ID_ADPCM_IMA_WS:
1380         if (c->vqa_version == 3) {
1381             for (channel = 0; channel < avctx->channels; channel++) {
1382                 int16_t *smp = samples_p[channel];
1383
1384                 for (n = nb_samples / 2; n > 0; n--) {
1385                     int v = bytestream2_get_byteu(&gb);
1386                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1387                     *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1388                 }
1389             }
1390         } else {
1391             for (n = nb_samples / 2; n > 0; n--) {
1392                 for (channel = 0; channel < avctx->channels; channel++) {
1393                     int v = bytestream2_get_byteu(&gb);
1394                     *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1395                     samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1396                 }
1397                 samples += avctx->channels;
1398             }
1399         }
1400         bytestream2_seek(&gb, 0, SEEK_END);
1401         break;
1402     case AV_CODEC_ID_ADPCM_XA:
1403     {
1404         int16_t *out0 = samples_p[0];
1405         int16_t *out1 = samples_p[1];
1406         int samples_per_block = 28 * (3 - avctx->channels) * 4;
1407         int sample_offset = 0;
1408         int bytes_remaining;
1409         while (bytestream2_get_bytes_left(&gb) >= 128) {
1410             if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1411                                  &c->status[0], &c->status[1],
1412                                  avctx->channels, sample_offset)) < 0)
1413                 return ret;
1414             bytestream2_skipu(&gb, 128);
1415             sample_offset += samples_per_block;
1416         }
1417         /* Less than a full block of data left, e.g. when reading from
1418          * 2324 byte per sector XA; the remainder is padding */
1419         bytes_remaining = bytestream2_get_bytes_left(&gb);
1420         if (bytes_remaining > 0) {
1421             bytestream2_skip(&gb, bytes_remaining);
1422         }
1423         break;
1424     }
1425     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1426         for (i=0; i<=st; i++) {
1427             c->status[i].step_index = bytestream2_get_le32u(&gb);
1428             if (c->status[i].step_index > 88u) {
1429                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1430                        i, c->status[i].step_index);
1431                 return AVERROR_INVALIDDATA;
1432             }
1433         }
1434         for (i=0; i<=st; i++) {
1435             c->status[i].predictor  = bytestream2_get_le32u(&gb);
1436             if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1437                 return AVERROR_INVALIDDATA;
1438         }
1439
1440         for (n = nb_samples >> (1 - st); n > 0; n--) {
1441             int byte   = bytestream2_get_byteu(&gb);
1442             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
1443             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1444         }
1445         break;
1446     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1447         for (n = nb_samples >> (1 - st); n > 0; n--) {
1448             int byte = bytestream2_get_byteu(&gb);
1449             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
1450             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1451         }
1452         break;
1453     case AV_CODEC_ID_ADPCM_EA:
1454     {
1455         int previous_left_sample, previous_right_sample;
1456         int current_left_sample, current_right_sample;
1457         int next_left_sample, next_right_sample;
1458         int coeff1l, coeff2l, coeff1r, coeff2r;
1459         int shift_left, shift_right;
1460
1461         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1462            each coding 28 stereo samples. */
1463
1464         if(avctx->channels != 2)
1465             return AVERROR_INVALIDDATA;
1466
1467         current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
1468         previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1469         current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1470         previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1471
1472         for (count1 = 0; count1 < nb_samples / 28; count1++) {
1473             int byte = bytestream2_get_byteu(&gb);
1474             coeff1l = ea_adpcm_table[ byte >> 4       ];
1475             coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
1476             coeff1r = ea_adpcm_table[ byte & 0x0F];
1477             coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1478
1479             byte = bytestream2_get_byteu(&gb);
1480             shift_left  = 20 - (byte >> 4);
1481             shift_right = 20 - (byte & 0x0F);
1482
1483             for (count2 = 0; count2 < 28; count2++) {
1484                 byte = bytestream2_get_byteu(&gb);
1485                 next_left_sample  = sign_extend(byte >> 4, 4) * (1 << shift_left);
1486                 next_right_sample = sign_extend(byte,      4) * (1 << shift_right);
1487
1488                 next_left_sample = (next_left_sample +
1489                     (current_left_sample * coeff1l) +
1490                     (previous_left_sample * coeff2l) + 0x80) >> 8;
1491                 next_right_sample = (next_right_sample +
1492                     (current_right_sample * coeff1r) +
1493                     (previous_right_sample * coeff2r) + 0x80) >> 8;
1494
1495                 previous_left_sample = current_left_sample;
1496                 current_left_sample = av_clip_int16(next_left_sample);
1497                 previous_right_sample = current_right_sample;
1498                 current_right_sample = av_clip_int16(next_right_sample);
1499                 *samples++ = current_left_sample;
1500                 *samples++ = current_right_sample;
1501             }
1502         }
1503
1504         bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1505
1506         break;
1507     }
1508     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1509     {
1510         int coeff[2][2], shift[2];
1511
1512         for(channel = 0; channel < avctx->channels; channel++) {
1513             int byte = bytestream2_get_byteu(&gb);
1514             for (i=0; i<2; i++)
1515                 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1516             shift[channel] = 20 - (byte & 0x0F);
1517         }
1518         for (count1 = 0; count1 < nb_samples / 2; count1++) {
1519             int byte[2];
1520
1521             byte[0] = bytestream2_get_byteu(&gb);
1522             if (st) byte[1] = bytestream2_get_byteu(&gb);
1523             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1524                 for(channel = 0; channel < avctx->channels; channel++) {
1525                     int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1526                     sample = (sample +
1527                              c->status[channel].sample1 * coeff[channel][0] +
1528                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1529                     c->status[channel].sample2 = c->status[channel].sample1;
1530                     c->status[channel].sample1 = av_clip_int16(sample);
1531                     *samples++ = c->status[channel].sample1;
1532                 }
1533             }
1534         }
1535         bytestream2_seek(&gb, 0, SEEK_END);
1536         break;
1537     }
1538     case AV_CODEC_ID_ADPCM_EA_R1:
1539     case AV_CODEC_ID_ADPCM_EA_R2:
1540     case AV_CODEC_ID_ADPCM_EA_R3: {
1541         /* channel numbering
1542            2chan: 0=fl, 1=fr
1543            4chan: 0=fl, 1=rl, 2=fr, 3=rr
1544            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1545         const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1546         int previous_sample, current_sample, next_sample;
1547         int coeff1, coeff2;
1548         int shift;
1549         unsigned int channel;
1550         uint16_t *samplesC;
1551         int count = 0;
1552         int offsets[6];
1553
1554         for (channel=0; channel<avctx->channels; channel++)
1555             offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1556                                              bytestream2_get_le32(&gb)) +
1557                                (avctx->channels + 1) * 4;
1558
1559         for (channel=0; channel<avctx->channels; channel++) {
1560             bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1561             samplesC = samples_p[channel];
1562
1563             if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1564                 current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
1565                 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1566             } else {
1567                 current_sample  = c->status[channel].predictor;
1568                 previous_sample = c->status[channel].prev_sample;
1569             }
1570
1571             for (count1 = 0; count1 < nb_samples / 28; count1++) {
1572                 int byte = bytestream2_get_byte(&gb);
1573                 if (byte == 0xEE) {  /* only seen in R2 and R3 */
1574                     current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
1575                     previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1576
1577                     for (count2=0; count2<28; count2++)
1578                         *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1579                 } else {
1580                     coeff1 = ea_adpcm_table[ byte >> 4     ];
1581                     coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1582                     shift = 20 - (byte & 0x0F);
1583
1584                     for (count2=0; count2<28; count2++) {
1585                         if (count2 & 1)
1586                             next_sample = (unsigned)sign_extend(byte,    4) << shift;
1587                         else {
1588                             byte = bytestream2_get_byte(&gb);
1589                             next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1590                         }
1591
1592                         next_sample += (current_sample  * coeff1) +
1593                                        (previous_sample * coeff2);
1594                         next_sample = av_clip_int16(next_sample >> 8);
1595
1596                         previous_sample = current_sample;
1597                         current_sample  = next_sample;
1598                         *samplesC++ = current_sample;
1599                     }
1600                 }
1601             }
1602             if (!count) {
1603                 count = count1;
1604             } else if (count != count1) {
1605                 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1606                 count = FFMAX(count, count1);
1607             }
1608
1609             if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1610                 c->status[channel].predictor   = current_sample;
1611                 c->status[channel].prev_sample = previous_sample;
1612             }
1613         }
1614
1615         frame->nb_samples = count * 28;
1616         bytestream2_seek(&gb, 0, SEEK_END);
1617         break;
1618     }
1619     case AV_CODEC_ID_ADPCM_EA_XAS:
1620         for (channel=0; channel<avctx->channels; channel++) {
1621             int coeff[2][4], shift[4];
1622             int16_t *s = samples_p[channel];
1623             for (n = 0; n < 4; n++, s += 32) {
1624                 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1625                 for (i=0; i<2; i++)
1626                     coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1627                 s[0] = val & ~0x0F;
1628
1629                 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1630                 shift[n] = 20 - (val & 0x0F);
1631                 s[1] = val & ~0x0F;
1632             }
1633
1634             for (m=2; m<32; m+=2) {
1635                 s = &samples_p[channel][m];
1636                 for (n = 0; n < 4; n++, s += 32) {
1637                     int level, pred;
1638                     int byte = bytestream2_get_byteu(&gb);
1639
1640                     level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1641                     pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1642                     s[0]  = av_clip_int16((level + pred + 0x80) >> 8);
1643
1644                     level = sign_extend(byte, 4) * (1 << shift[n]);
1645                     pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1646                     s[1]  = av_clip_int16((level + pred + 0x80) >> 8);
1647                 }
1648             }
1649         }
1650         break;
1651     case AV_CODEC_ID_ADPCM_IMA_AMV:
1652         c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1653         c->status[0].step_index = bytestream2_get_byteu(&gb);
1654         bytestream2_skipu(&gb, 5);
1655         if (c->status[0].step_index > 88u) {
1656             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1657                    c->status[0].step_index);
1658             return AVERROR_INVALIDDATA;
1659         }
1660
1661         for (n = nb_samples >> (1 - st); n > 0; n--) {
1662             int v = bytestream2_get_byteu(&gb);
1663
1664             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1665             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1666         }
1667         break;
1668     case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1669         for (i = 0; i < avctx->channels; i++) {
1670             c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1671             c->status[i].step_index = bytestream2_get_byteu(&gb);
1672             bytestream2_skipu(&gb, 1);
1673             if (c->status[i].step_index > 88u) {
1674                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1675                        c->status[i].step_index);
1676                 return AVERROR_INVALIDDATA;
1677             }
1678         }
1679
1680         for (n = nb_samples >> (1 - st); n > 0; n--) {
1681             int v = bytestream2_get_byteu(&gb);
1682
1683             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1684             *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1685         }
1686         break;
1687     case AV_CODEC_ID_ADPCM_CT:
1688         for (n = nb_samples >> (1 - st); n > 0; n--) {
1689             int v = bytestream2_get_byteu(&gb);
1690             *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
1691             *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1692         }
1693         break;
1694     case AV_CODEC_ID_ADPCM_SBPRO_4:
1695     case AV_CODEC_ID_ADPCM_SBPRO_3:
1696     case AV_CODEC_ID_ADPCM_SBPRO_2:
1697         if (!c->status[0].step_index) {
1698             /* the first byte is a raw sample */
1699             *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1700             if (st)
1701                 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1702             c->status[0].step_index = 1;
1703             nb_samples--;
1704         }
1705         if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1706             for (n = nb_samples >> (1 - st); n > 0; n--) {
1707                 int byte = bytestream2_get_byteu(&gb);
1708                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1709                                                        byte >> 4,   4, 0);
1710                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1711                                                        byte & 0x0F, 4, 0);
1712             }
1713         } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1714             for (n = (nb_samples<<st) / 3; n > 0; n--) {
1715                 int byte = bytestream2_get_byteu(&gb);
1716                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1717                                                         byte >> 5        , 3, 0);
1718                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1719                                                        (byte >> 2) & 0x07, 3, 0);
1720                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1721                                                         byte & 0x03,       2, 0);
1722             }
1723         } else {
1724             for (n = nb_samples >> (2 - st); n > 0; n--) {
1725                 int byte = bytestream2_get_byteu(&gb);
1726                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1727                                                         byte >> 6        , 2, 2);
1728                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1729                                                        (byte >> 4) & 0x03, 2, 2);
1730                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1731                                                        (byte >> 2) & 0x03, 2, 2);
1732                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1733                                                         byte & 0x03,       2, 2);
1734             }
1735         }
1736         break;
1737     case AV_CODEC_ID_ADPCM_SWF:
1738         adpcm_swf_decode(avctx, buf, buf_size, samples);
1739         bytestream2_seek(&gb, 0, SEEK_END);
1740         break;
1741     case AV_CODEC_ID_ADPCM_YAMAHA:
1742         for (n = nb_samples >> (1 - st); n > 0; n--) {
1743             int v = bytestream2_get_byteu(&gb);
1744             *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1745             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1746         }
1747         break;
1748     case AV_CODEC_ID_ADPCM_AICA:
1749         if (!c->has_status) {
1750             for (channel = 0; channel < avctx->channels; channel++)
1751                 c->status[channel].step = 0;
1752             c->has_status = 1;
1753         }
1754         for (channel = 0; channel < avctx->channels; channel++) {
1755             samples = samples_p[channel];
1756             for (n = nb_samples >> 1; n > 0; n--) {
1757                 int v = bytestream2_get_byteu(&gb);
1758                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1759                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4  );
1760             }
1761         }
1762         break;
1763     case AV_CODEC_ID_ADPCM_AFC:
1764     {
1765         int samples_per_block;
1766         int blocks;
1767
1768         if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1769             samples_per_block = avctx->extradata[0] / 16;
1770             blocks = nb_samples / avctx->extradata[0];
1771         } else {
1772             samples_per_block = nb_samples / 16;
1773             blocks = 1;
1774         }
1775
1776         for (m = 0; m < blocks; m++) {
1777         for (channel = 0; channel < avctx->channels; channel++) {
1778             int prev1 = c->status[channel].sample1;
1779             int prev2 = c->status[channel].sample2;
1780
1781             samples = samples_p[channel] + m * 16;
1782             /* Read in every sample for this channel.  */
1783             for (i = 0; i < samples_per_block; i++) {
1784                 int byte = bytestream2_get_byteu(&gb);
1785                 int scale = 1 << (byte >> 4);
1786                 int index = byte & 0xf;
1787                 int factor1 = ff_adpcm_afc_coeffs[0][index];
1788                 int factor2 = ff_adpcm_afc_coeffs[1][index];
1789
1790                 /* Decode 16 samples.  */
1791                 for (n = 0; n < 16; n++) {
1792                     int32_t sampledat;
1793
1794                     if (n & 1) {
1795                         sampledat = sign_extend(byte, 4);
1796                     } else {
1797                         byte = bytestream2_get_byteu(&gb);
1798                         sampledat = sign_extend(byte >> 4, 4);
1799                     }
1800
1801                     sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1802                                 sampledat * scale;
1803                     *samples = av_clip_int16(sampledat);
1804                     prev2 = prev1;
1805                     prev1 = *samples++;
1806                 }
1807             }
1808
1809             c->status[channel].sample1 = prev1;
1810             c->status[channel].sample2 = prev2;
1811         }
1812         }
1813         bytestream2_seek(&gb, 0, SEEK_END);
1814         break;
1815     }
1816     case AV_CODEC_ID_ADPCM_THP:
1817     case AV_CODEC_ID_ADPCM_THP_LE:
1818     {
1819         int table[14][16];
1820         int ch;
1821
1822 #define THP_GET16(g) \
1823     sign_extend( \
1824         avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1825         bytestream2_get_le16u(&(g)) : \
1826         bytestream2_get_be16u(&(g)), 16)
1827
1828         if (avctx->extradata) {
1829             GetByteContext tb;
1830             if (avctx->extradata_size < 32 * avctx->channels) {
1831                 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1832                 return AVERROR_INVALIDDATA;
1833             }
1834
1835             bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1836             for (i = 0; i < avctx->channels; i++)
1837                 for (n = 0; n < 16; n++)
1838                     table[i][n] = THP_GET16(tb);
1839         } else {
1840             for (i = 0; i < avctx->channels; i++)
1841                 for (n = 0; n < 16; n++)
1842                     table[i][n] = THP_GET16(gb);
1843
1844             if (!c->has_status) {
1845                 /* Initialize the previous sample.  */
1846                 for (i = 0; i < avctx->channels; i++) {
1847                     c->status[i].sample1 = THP_GET16(gb);
1848                     c->status[i].sample2 = THP_GET16(gb);
1849                 }
1850                 c->has_status = 1;
1851             } else {
1852                 bytestream2_skip(&gb, avctx->channels * 4);
1853             }
1854         }
1855
1856         for (ch = 0; ch < avctx->channels; ch++) {
1857             samples = samples_p[ch];
1858
1859             /* Read in every sample for this channel.  */
1860             for (i = 0; i < (nb_samples + 13) / 14; i++) {
1861                 int byte = bytestream2_get_byteu(&gb);
1862                 int index = (byte >> 4) & 7;
1863                 unsigned int exp = byte & 0x0F;
1864                 int64_t factor1 = table[ch][index * 2];
1865                 int64_t factor2 = table[ch][index * 2 + 1];
1866
1867                 /* Decode 14 samples.  */
1868                 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1869                     int32_t sampledat;
1870
1871                     if (n & 1) {
1872                         sampledat = sign_extend(byte, 4);
1873                     } else {
1874                         byte = bytestream2_get_byteu(&gb);
1875                         sampledat = sign_extend(byte >> 4, 4);
1876                     }
1877
1878                     sampledat = ((c->status[ch].sample1 * factor1
1879                                 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1880                     *samples = av_clip_int16(sampledat);
1881                     c->status[ch].sample2 = c->status[ch].sample1;
1882                     c->status[ch].sample1 = *samples++;
1883                 }
1884             }
1885         }
1886         break;
1887     }
1888     case AV_CODEC_ID_ADPCM_DTK:
1889         for (channel = 0; channel < avctx->channels; channel++) {
1890             samples = samples_p[channel];
1891
1892             /* Read in every sample for this channel.  */
1893             for (i = 0; i < nb_samples / 28; i++) {
1894                 int byte, header;
1895                 if (channel)
1896                     bytestream2_skipu(&gb, 1);
1897                 header = bytestream2_get_byteu(&gb);
1898                 bytestream2_skipu(&gb, 3 - channel);
1899
1900                 /* Decode 28 samples.  */
1901                 for (n = 0; n < 28; n++) {
1902                     int32_t sampledat, prev;
1903
1904                     switch (header >> 4) {
1905                     case 1:
1906                         prev = (c->status[channel].sample1 * 0x3c);
1907                         break;
1908                     case 2:
1909                         prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1910                         break;
1911                     case 3:
1912                         prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1913                         break;
1914                     default:
1915                         prev = 0;
1916                     }
1917
1918                     prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1919
1920                     byte = bytestream2_get_byteu(&gb);
1921                     if (!channel)
1922                         sampledat = sign_extend(byte, 4);
1923                     else
1924                         sampledat = sign_extend(byte >> 4, 4);
1925
1926                     sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1927                     *samples++ = av_clip_int16(sampledat >> 6);
1928                     c->status[channel].sample2 = c->status[channel].sample1;
1929                     c->status[channel].sample1 = sampledat;
1930                 }
1931             }
1932             if (!channel)
1933                 bytestream2_seek(&gb, 0, SEEK_SET);
1934         }
1935         break;
1936     case AV_CODEC_ID_ADPCM_PSX:
1937         for (channel = 0; channel < avctx->channels; channel++) {
1938             samples = samples_p[channel];
1939
1940             /* Read in every sample for this channel.  */
1941             for (i = 0; i < nb_samples / 28; i++) {
1942                 int filter, shift, flag, byte;
1943
1944                 filter = bytestream2_get_byteu(&gb);
1945                 shift  = filter & 0xf;
1946                 filter = filter >> 4;
1947                 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1948                     return AVERROR_INVALIDDATA;
1949                 flag   = bytestream2_get_byteu(&gb);
1950
1951                 /* Decode 28 samples.  */
1952                 for (n = 0; n < 28; n++) {
1953                     int sample = 0, scale;
1954
1955                     if (flag < 0x07) {
1956                         if (n & 1) {
1957                             scale = sign_extend(byte >> 4, 4);
1958                         } else {
1959                             byte  = bytestream2_get_byteu(&gb);
1960                             scale = sign_extend(byte, 4);
1961                         }
1962
1963                         scale  = scale * (1 << 12);
1964                         sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1965                     }
1966                     *samples++ = av_clip_int16(sample);
1967                     c->status[channel].sample2 = c->status[channel].sample1;
1968                     c->status[channel].sample1 = sample;
1969                 }
1970             }
1971         }
1972         break;
1973     case AV_CODEC_ID_ADPCM_ARGO:
1974         /*
1975          * The format of each block:
1976          *   uint8_t left_control;
1977          *   uint4_t left_samples[nb_samples];
1978          *   ---- and if stereo ----
1979          *   uint8_t right_control;
1980          *   uint4_t right_samples[nb_samples];
1981          *
1982          * Format of the control byte:
1983          * MSB [SSSSDRRR] LSB
1984          *   S = (Shift Amount - 2)
1985          *   D = Decoder flag.
1986          *   R = Reserved
1987          *
1988          * Each block relies on the previous two samples of each channel.
1989          * They should be 0 initially.
1990          */
1991         for (channel = 0; channel < avctx->channels; channel++) {
1992             int control, shift;
1993
1994             samples = samples_p[channel];
1995             cs = c->status + channel;
1996
1997             /* Get the control byte and decode the samples, 2 at a time. */
1998             control = bytestream2_get_byteu(&gb);
1999             shift = (control >> 4) + 2;
2000
2001             for (n = 0; n < nb_samples / 2; n++) {
2002                 int sample = bytestream2_get_byteu(&gb);
2003                 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 4, 4), control, shift);
2004                 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 0, 4), control, shift);
2005             }
2006         }
2007         break;
2008     case AV_CODEC_ID_ADPCM_ZORK:
2009         if (!c->has_status) {
2010             for (channel = 0; channel < avctx->channels; channel++) {
2011                 c->status[channel].predictor  = 0;
2012                 c->status[channel].step_index = 0;
2013             }
2014             c->has_status = 1;
2015         }
2016         for (n = 0; n < nb_samples * avctx->channels; n++) {
2017             int v = bytestream2_get_byteu(&gb);
2018             *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
2019         }
2020         break;
2021     case AV_CODEC_ID_ADPCM_IMA_MTF:
2022         for (n = nb_samples / 2; n > 0; n--) {
2023             for (channel = 0; channel < avctx->channels; channel++) {
2024                 int v = bytestream2_get_byteu(&gb);
2025                 *samples++  = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2026                 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2027             }
2028             samples += avctx->channels;
2029         }
2030         break;
2031     default:
2032         av_assert0(0); // unsupported codec_id should not happen
2033     }
2034
2035     if (avpkt->size && bytestream2_tell(&gb) == 0) {
2036         av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2037         return AVERROR_INVALIDDATA;
2038     }
2039
2040     *got_frame_ptr = 1;
2041
2042     if (avpkt->size < bytestream2_tell(&gb)) {
2043         av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2044         return avpkt->size;
2045     }
2046
2047     return bytestream2_tell(&gb);
2048 }
2049
2050 static void adpcm_flush(AVCodecContext *avctx)
2051 {
2052     ADPCMDecodeContext *c = avctx->priv_data;
2053     c->has_status = 0;
2054 }
2055
2056
2057 static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
2058                                                         AV_SAMPLE_FMT_NONE };
2059 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2060                                                         AV_SAMPLE_FMT_NONE };
2061 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2062                                                         AV_SAMPLE_FMT_S16P,
2063                                                         AV_SAMPLE_FMT_NONE };
2064
2065 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2066 AVCodec ff_ ## name_ ## _decoder = {                        \
2067     .name           = #name_,                               \
2068     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
2069     .type           = AVMEDIA_TYPE_AUDIO,                   \
2070     .id             = id_,                                  \
2071     .priv_data_size = sizeof(ADPCMDecodeContext),           \
2072     .init           = adpcm_decode_init,                    \
2073     .decode         = adpcm_decode_frame,                   \
2074     .flush          = adpcm_flush,                          \
2075     .capabilities   = AV_CODEC_CAP_DR1,                     \
2076     .sample_fmts    = sample_fmts_,                         \
2077 }
2078
2079 /* Note: Do not forget to add new entries to the Makefile as well. */
2080 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
2081 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
2082 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM,         sample_fmts_s16,  adpcm_agm,         "ADPCM AmuseGraphics Movie");
2083 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA,        sample_fmts_s16p, adpcm_aica,        "ADPCM Yamaha AICA");
2084 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO,        sample_fmts_s16p, adpcm_argo,        "ADPCM Argonaut Games");
2085 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
2086 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
2087 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
2088 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2089 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
2090 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
2091 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
2092 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
2093 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
2094 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
2095 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM,     sample_fmts_s16,  adpcm_ima_apm,     "ADPCM IMA Ubisoft APM");
2096 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16,  adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2097 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,  adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
2098 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
2099 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
2100 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2101 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2102 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
2103 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF,     sample_fmts_s16,  adpcm_ima_mtf,     "ADPCM IMA Capcom's MT Framework");
2104 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
2105 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
2106 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD,     sample_fmts_s16,  adpcm_ima_rad,     "ADPCM IMA Radical");
2107 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI,     sample_fmts_s16,  adpcm_ima_ssi,     "ADPCM IMA Simon & Schuster Interactive");
2108 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
2109 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP,     sample_fmts_s16,  adpcm_ima_alp,     "ADPCM IMA High Voltage Software ALP");
2110 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
2111 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
2112 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_both, adpcm_ms,          "ADPCM Microsoft");
2113 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF,        sample_fmts_s16p, adpcm_mtaf,        "ADPCM MTAF");
2114 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX,         sample_fmts_s16p, adpcm_psx,         "ADPCM Playstation");
2115 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
2116 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
2117 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
2118 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
2119 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE,      sample_fmts_s16p, adpcm_thp_le,      "ADPCM Nintendo THP (little-endian)");
2120 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo THP");
2121 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
2122 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");
2123 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK,        sample_fmts_s16,  adpcm_zork,        "ADPCM Zork");