2 * Copyright (c) 2001-2003 The FFmpeg project
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)
21 * This file is part of FFmpeg.
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.
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.
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
39 #include "bytestream.h"
41 #include "adpcm_data.h"
47 * Features and limitations:
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/
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/
64 /* These are for CD-ROM XA ADPCM */
65 static const int8_t xa_adpcm_table[5][2] = {
73 static const int16_t ea_adpcm_table[] = {
81 // padded to zero where table size is less then 16
82 static const int8_t swf_index_tables[4][16] = {
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 }
89 static const int8_t zork_index_table[8] = {
90 -1, -1, -1, 1, 4, 7, 10, 12,
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,
100 typedef struct ADPCMDecodeContext {
101 ADPCMChannelStatus status[14];
102 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
104 } ADPCMDecodeContext;
106 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
108 ADPCMDecodeContext *c = avctx->priv_data;
109 unsigned int min_channels = 1;
110 unsigned int max_channels = 2;
112 switch(avctx->codec->id) {
113 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
116 case AV_CODEC_ID_ADPCM_DTK:
117 case AV_CODEC_ID_ADPCM_EA:
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:
128 case AV_CODEC_ID_ADPCM_MTAF:
131 if (avctx->channels & 1) {
132 avpriv_request_sample(avctx, "channel count %d\n", avctx->channels);
133 return AVERROR_PATCHWELCOME;
136 case AV_CODEC_ID_ADPCM_PSX:
139 case AV_CODEC_ID_ADPCM_IMA_DAT4:
140 case AV_CODEC_ID_ADPCM_THP:
141 case AV_CODEC_ID_ADPCM_THP_LE:
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);
150 switch(avctx->codec->id) {
151 case AV_CODEC_ID_ADPCM_CT:
152 c->status[0].step = c->status[1].step = 511;
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;
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);
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);
172 case AV_CODEC_ID_ADPCM_IMA_WS:
173 if (avctx->extradata && avctx->extradata_size >= 2)
174 c->vqa_version = AV_RL16(avctx->extradata);
176 case AV_CODEC_ID_ADPCM_ARGO:
177 if (avctx->bits_per_coded_sample != 4)
178 return AVERROR_INVALIDDATA;
180 case AV_CODEC_ID_ADPCM_ZORK:
181 if (avctx->bits_per_coded_sample != 8)
182 return AVERROR_INVALIDDATA;
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;
208 case AV_CODEC_ID_ADPCM_IMA_WS:
209 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
212 case AV_CODEC_ID_ADPCM_MS:
213 avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
217 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
223 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
225 int delta, pred, step, add;
230 add = (delta * 2 + 1) * step;
234 if ((nibble & 8) == 0)
235 pred = av_clip(pred + (add >> 3), -32767, 32767);
237 pred = av_clip(pred - (add >> 3), -32767, 32767);
244 c->step = av_clip(c->step * 2, 127, 24576);
262 c->step = av_clip(c->step, 127, 24576);
267 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
271 int sign, delta, diff, step;
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);
279 /* perform direct multiplication instead of series of jumps proposed by
280 * the reference ADPCM implementation since modern CPUs can do the mults
282 diff = ((2 * delta + 1) * step) >> shift;
283 predictor = c->predictor;
284 if (sign) predictor -= diff;
285 else predictor += diff;
287 c->predictor = av_clip_int16(predictor);
288 c->step_index = step_index;
290 return (int16_t)c->predictor;
293 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
297 int sign, delta, diff, step;
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);
305 diff = (delta * step) >> shift;
306 predictor = c->predictor;
307 if (sign) predictor -= diff;
308 else predictor += diff;
310 c->predictor = av_clip_int16(predictor);
311 c->step_index = step_index;
313 return (int16_t)c->predictor;
316 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
318 int step_index, step, delta, predictor;
320 step = ff_adpcm_step_table[c->step_index];
322 delta = step * (2 * nibble - 15);
323 predictor = c->predictor + delta;
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);
329 return (int16_t)c->predictor;
332 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
338 nibble = sign_extend(nibble & 0xF, 4);
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);
344 predictor = c->predictor + step * nibble;
346 c->predictor = av_clip_int16(predictor);
347 c->step_index = step_index;
352 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
354 int nibble, step_index, predictor, sign, delta, diff, step, shift;
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);
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;
369 c->predictor = av_clip_int16(predictor);
370 c->step_index = step_index;
372 return (int16_t)c->predictor;
375 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
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);
386 if (nibble & 4) diff += step;
387 if (nibble & 2) diff += step >> 1;
388 if (nibble & 1) diff += step >> 2;
391 predictor = c->predictor - diff;
393 predictor = c->predictor + diff;
395 c->predictor = av_clip_int16(predictor);
396 c->step_index = step_index;
401 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
405 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
406 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
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;
420 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
422 int step_index, predictor, sign, delta, diff, step;
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);
430 diff = ((2 * delta + 1) * step) >> 3;
431 predictor = c->predictor;
432 if (sign) predictor -= diff;
433 else predictor += diff;
435 c->predictor = av_clip_intp2(predictor, 11);
436 c->step_index = step_index;
438 return c->predictor * 16;
441 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
443 int sign, delta, diff;
448 /* perform direct multiplication instead of series of jumps proposed by
449 * the reference ADPCM implementation since modern CPUs can do the mults
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);
459 return (int16_t)c->predictor;
462 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
464 int sign, delta, diff;
466 sign = nibble & (1<<(size-1));
467 delta = nibble & ((1<<(size-1))-1);
468 diff = delta << (7 + c->step + shift);
471 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
473 /* calculate new step */
474 if (delta >= (2*size - 3) && c->step < 3)
476 else if (delta == 0 && c->step > 0)
479 return (int16_t) c->predictor;
482 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
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);
496 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
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);
505 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
507 int16_t index = c->step_index;
508 uint32_t lookup_sample = ff_adpcm_step_table[index];
512 sample += lookup_sample;
514 sample += lookup_sample >> 1;
516 sample += lookup_sample >> 2;
518 sample += lookup_sample >> 3;
520 sample += lookup_sample >> 4;
522 sample += lookup_sample >> 5;
524 sample += lookup_sample >> 6;
528 sample += c->predictor;
529 sample = av_clip_int16(sample);
531 index += zork_index_table[(nibble >> 4) & 7];
532 index = av_clip(index, 0, 88);
534 c->predictor = sample;
535 c->step_index = index;
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)
545 int shift,filter,f0,f1;
549 out0 += sample_offset;
553 out1 += sample_offset;
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);
563 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
566 f0 = xa_adpcm_table[filter][0];
567 f1 = xa_adpcm_table[filter][1];
575 t = sign_extend(d, 4);
576 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
578 s_1 = av_clip_int16(s);
585 s_1 = right->sample1;
586 s_2 = right->sample2;
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);
596 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
600 f0 = xa_adpcm_table[filter][0];
601 f1 = xa_adpcm_table[filter][1];
606 t = sign_extend(d >> 4, 4);
607 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
609 s_1 = av_clip_int16(s);
614 right->sample1 = s_1;
615 right->sample2 = s_2;
621 out0 += 28 * (3 - channels);
622 out1 += 28 * (3 - channels);
628 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
630 ADPCMDecodeContext *c = avctx->priv_data;
633 int k0, signmask, nb_bits, count;
634 int size = buf_size*8;
637 init_get_bits(&gb, buf, size);
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);
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);
651 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
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
669 if (delta & signmask)
670 c->status[i].predictor -= vpdiff;
672 c->status[i].predictor += vpdiff;
674 c->status[i].step_index += table[delta & (~signmask)];
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);
679 *samples++ = c->status[i].predictor;
685 static inline int16_t adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int control, int shift)
687 int sample = nibble * (1 << shift);
690 sample += (8 * cs->sample1) - (4 * cs->sample2);
692 sample += 4 * cs->sample1;
694 sample = av_clip_int16(sample >> 2);
696 cs->sample2 = cs->sample1;
697 cs->sample1 = sample;
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.
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.
713 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
714 int buf_size, int *coded_samples, int *approx_nb_samples)
716 ADPCMDecodeContext *s = avctx->priv_data;
718 int ch = avctx->channels;
719 int has_coded_samples = 0;
723 *approx_nb_samples = 0;
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)
735 case AV_CODEC_ID_ADPCM_IMA_QT:
736 if (buf_size < 34 * ch)
740 case AV_CODEC_ID_ADPCM_ARGO:
741 if (buf_size < 17 * ch)
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;
764 /* simple 4-bit adpcm, with header */
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;
775 return (buf_size - header_size) * 2 / ch;
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;
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;
790 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
791 nb_samples = (buf_size - ch) / ch * 2;
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);
804 case AV_CODEC_ID_ADPCM_EA_R2:
805 header_size = 4 + 5 * ch;
806 *coded_samples = bytestream2_get_le32(gb);
808 case AV_CODEC_ID_ADPCM_EA_R3:
809 header_size = 4 + 5 * ch;
810 *coded_samples = bytestream2_get_be32(gb);
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;
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;
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;
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;
835 case AV_CODEC_ID_ADPCM_IMA_WAV:
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;
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;
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;
856 case AV_CODEC_ID_ADPCM_SBPRO_2:
857 case AV_CODEC_ID_ADPCM_SBPRO_3:
858 case AV_CODEC_ID_ADPCM_SBPRO_4:
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;
866 if (!s->status[0].step_index) {
868 return AVERROR_INVALIDDATA;
872 nb_samples += buf_size * samples_per_byte / ch;
875 case AV_CODEC_ID_ADPCM_SWF:
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);
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);
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;
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;
906 case AV_CODEC_ID_ADPCM_AFC:
907 nb_samples = buf_size / (9 * ch) * 16;
909 case AV_CODEC_ID_ADPCM_XA:
910 nb_samples = (buf_size / 128) * 224 / ch;
912 case AV_CODEC_ID_ADPCM_DTK:
913 case AV_CODEC_ID_ADPCM_PSX:
914 nb_samples = buf_size / (16 * ch) * 28;
916 case AV_CODEC_ID_ADPCM_ZORK:
917 nb_samples = buf_size / ch;
921 /* validate coded sample count */
922 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
923 return AVERROR_INVALIDDATA;
928 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
929 int *got_frame_ptr, AVPacket *avpkt)
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;
941 int nb_samples, coded_samples, approx_nb_samples, ret;
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;
951 /* get output buffer */
952 frame->nb_samples = nb_samples;
953 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
955 samples = (int16_t *)frame->data[0];
956 samples_p = (int16_t **)frame->extended_data;
958 /* use coded_samples when applicable */
959 /* it is always <= nb_samples, so the output buffer will be large enough */
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;
966 st = avctx->channels == 2 ? 1 : 0;
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++) {
975 cs = &(c->status[channel]);
976 /* (pppppp) (piiiiiii) */
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;
983 if (cs->step_index == step_index) {
984 int diff = predictor - cs->predictor;
991 cs->step_index = step_index;
992 cs->predictor = predictor;
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;
1001 samples = samples_p[channel];
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 );
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);
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",
1019 return AVERROR_INVALIDDATA;
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 };
1029 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1030 for (i = 0; i < avctx->channels; 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];
1039 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
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);
1048 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1050 for (n = 0; n < (nb_samples - 1) / 8; n++) {
1051 for (i = 0; i < avctx->channels; 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);
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);
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;
1076 for (i = 0; i < avctx->channels; i++) {
1077 samples = (int16_t *)frame->data[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);
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);
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 );
1098 case AV_CODEC_ID_ADPCM_MS:
1100 int block_predictor;
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;
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);
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",
1129 return AVERROR_INVALIDDATA;
1131 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1132 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
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",
1138 return AVERROR_INVALIDDATA;
1140 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1141 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1143 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1145 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
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);
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);
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 );
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 );
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;
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);
1203 case AV_CODEC_ID_ADPCM_IMA_DK3:
1207 int decode_top_nibble_next = 0;
1209 const int16_t *samples_end = samples + avctx->channels * nb_samples;
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;
1221 /* sign extend the predictors */
1222 diff_channel = c->status[1].predictor;
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; \
1230 last_byte = bytestream2_get_byteu(&gb); \
1231 nibble = last_byte & 0x0F; \
1232 decode_top_nibble_next = 1; \
1235 while (samples < samples_end) {
1237 /* for this algorithm, c->status[0] is the sum channel and
1238 * c->status[1] is the diff channel */
1240 /* process the first predictor of the sum channel */
1241 DK3_GET_NEXT_NIBBLE();
1242 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1244 /* process the diff channel predictor */
1245 DK3_GET_NEXT_NIBBLE();
1246 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
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;
1253 /* process the second predictor of the sum channel */
1254 DK3_GET_NEXT_NIBBLE();
1255 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
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;
1263 if ((bytestream2_tell(&gb) & 1))
1264 bytestream2_skip(&gb, 1);
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;
1279 for (n = nb_samples >> (1 - st); n > 0; n--) {
1281 int v = bytestream2_get_byteu(&gb);
1282 /* nibbles are swapped for mono */
1290 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1291 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
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);
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);
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);
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);
1327 samples += avctx->channels;
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);
1337 samples += avctx->channels;
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);
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);
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;
1365 for (n = 0; n < nb_samples / 2; n++) {
1368 byte[0] = bytestream2_get_byteu(&gb);
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);
1374 for(channel = 0; channel < avctx->channels; channel++) {
1375 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
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];
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);
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);
1397 samples += avctx->channels;
1400 bytestream2_seek(&gb, 0, SEEK_END);
1402 case AV_CODEC_ID_ADPCM_XA:
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)
1414 bytestream2_skipu(&gb, 128);
1415 sample_offset += samples_per_block;
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);
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;
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;
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);
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);
1453 case AV_CODEC_ID_ADPCM_EA:
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;
1461 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1462 each coding 28 stereo samples. */
1464 if(avctx->channels != 2)
1465 return AVERROR_INVALIDDATA;
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);
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];
1479 byte = bytestream2_get_byteu(&gb);
1480 shift_left = 20 - (byte >> 4);
1481 shift_right = 20 - (byte & 0x0F);
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);
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;
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;
1504 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1508 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1510 int coeff[2][2], shift[2];
1512 for(channel = 0; channel < avctx->channels; channel++) {
1513 int byte = bytestream2_get_byteu(&gb);
1515 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1516 shift[channel] = 20 - (byte & 0x0F);
1518 for (count1 = 0; count1 < nb_samples / 2; count1++) {
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]);
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;
1535 bytestream2_seek(&gb, 0, SEEK_END);
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
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;
1549 unsigned int channel;
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;
1559 for (channel=0; channel<avctx->channels; channel++) {
1560 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1561 samplesC = samples_p[channel];
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);
1567 current_sample = c->status[channel].predictor;
1568 previous_sample = c->status[channel].prev_sample;
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);
1577 for (count2=0; count2<28; count2++)
1578 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1580 coeff1 = ea_adpcm_table[ byte >> 4 ];
1581 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1582 shift = 20 - (byte & 0x0F);
1584 for (count2=0; count2<28; count2++) {
1586 next_sample = (unsigned)sign_extend(byte, 4) << shift;
1588 byte = bytestream2_get_byte(&gb);
1589 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1592 next_sample += (current_sample * coeff1) +
1593 (previous_sample * coeff2);
1594 next_sample = av_clip_int16(next_sample >> 8);
1596 previous_sample = current_sample;
1597 current_sample = next_sample;
1598 *samplesC++ = current_sample;
1604 } else if (count != count1) {
1605 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1606 count = FFMAX(count, count1);
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;
1615 frame->nb_samples = count * 28;
1616 bytestream2_seek(&gb, 0, SEEK_END);
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);
1626 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1629 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1630 shift[n] = 20 - (val & 0x0F);
1634 for (m=2; m<32; m+=2) {
1635 s = &samples_p[channel][m];
1636 for (n = 0; n < 4; n++, s += 32) {
1638 int byte = bytestream2_get_byteu(&gb);
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);
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);
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;
1661 for (n = nb_samples >> (1 - st); n > 0; n--) {
1662 int v = bytestream2_get_byteu(&gb);
1664 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1665 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
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;
1680 for (n = nb_samples >> (1 - st); n > 0; n--) {
1681 int v = bytestream2_get_byteu(&gb);
1683 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1684 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
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);
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);
1701 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1702 c->status[0].step_index = 1;
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],
1710 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
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],
1718 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1719 (byte >> 2) & 0x07, 3, 0);
1720 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
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],
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],
1737 case AV_CODEC_ID_ADPCM_SWF:
1738 adpcm_swf_decode(avctx, buf, buf_size, samples);
1739 bytestream2_seek(&gb, 0, SEEK_END);
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 );
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;
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 );
1763 case AV_CODEC_ID_ADPCM_AFC:
1765 int samples_per_block;
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];
1772 samples_per_block = nb_samples / 16;
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;
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];
1790 /* Decode 16 samples. */
1791 for (n = 0; n < 16; n++) {
1795 sampledat = sign_extend(byte, 4);
1797 byte = bytestream2_get_byteu(&gb);
1798 sampledat = sign_extend(byte >> 4, 4);
1801 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1803 *samples = av_clip_int16(sampledat);
1809 c->status[channel].sample1 = prev1;
1810 c->status[channel].sample2 = prev2;
1813 bytestream2_seek(&gb, 0, SEEK_END);
1816 case AV_CODEC_ID_ADPCM_THP:
1817 case AV_CODEC_ID_ADPCM_THP_LE:
1822 #define THP_GET16(g) \
1824 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1825 bytestream2_get_le16u(&(g)) : \
1826 bytestream2_get_be16u(&(g)), 16)
1828 if (avctx->extradata) {
1830 if (avctx->extradata_size < 32 * avctx->channels) {
1831 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1832 return AVERROR_INVALIDDATA;
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);
1840 for (i = 0; i < avctx->channels; i++)
1841 for (n = 0; n < 16; n++)
1842 table[i][n] = THP_GET16(gb);
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);
1852 bytestream2_skip(&gb, avctx->channels * 4);
1856 for (ch = 0; ch < avctx->channels; ch++) {
1857 samples = samples_p[ch];
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];
1867 /* Decode 14 samples. */
1868 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1872 sampledat = sign_extend(byte, 4);
1874 byte = bytestream2_get_byteu(&gb);
1875 sampledat = sign_extend(byte >> 4, 4);
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++;
1888 case AV_CODEC_ID_ADPCM_DTK:
1889 for (channel = 0; channel < avctx->channels; channel++) {
1890 samples = samples_p[channel];
1892 /* Read in every sample for this channel. */
1893 for (i = 0; i < nb_samples / 28; i++) {
1896 bytestream2_skipu(&gb, 1);
1897 header = bytestream2_get_byteu(&gb);
1898 bytestream2_skipu(&gb, 3 - channel);
1900 /* Decode 28 samples. */
1901 for (n = 0; n < 28; n++) {
1902 int32_t sampledat, prev;
1904 switch (header >> 4) {
1906 prev = (c->status[channel].sample1 * 0x3c);
1909 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1912 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1918 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1920 byte = bytestream2_get_byteu(&gb);
1922 sampledat = sign_extend(byte, 4);
1924 sampledat = sign_extend(byte >> 4, 4);
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;
1933 bytestream2_seek(&gb, 0, SEEK_SET);
1936 case AV_CODEC_ID_ADPCM_PSX:
1937 for (channel = 0; channel < avctx->channels; channel++) {
1938 samples = samples_p[channel];
1940 /* Read in every sample for this channel. */
1941 for (i = 0; i < nb_samples / 28; i++) {
1942 int filter, shift, flag, byte;
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);
1951 /* Decode 28 samples. */
1952 for (n = 0; n < 28; n++) {
1953 int sample = 0, scale;
1957 scale = sign_extend(byte >> 4, 4);
1959 byte = bytestream2_get_byteu(&gb);
1960 scale = sign_extend(byte, 4);
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);
1966 *samples++ = av_clip_int16(sample);
1967 c->status[channel].sample2 = c->status[channel].sample1;
1968 c->status[channel].sample1 = sample;
1973 case AV_CODEC_ID_ADPCM_ARGO:
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];
1982 * Format of the control byte:
1983 * MSB [SSSSDRRR] LSB
1984 * S = (Shift Amount - 2)
1988 * Each block relies on the previous two samples of each channel.
1989 * They should be 0 initially.
1991 for (channel = 0; channel < avctx->channels; channel++) {
1994 samples = samples_p[channel];
1995 cs = c->status + channel;
1997 /* Get the control byte and decode the samples, 2 at a time. */
1998 control = bytestream2_get_byteu(&gb);
1999 shift = (control >> 4) + 2;
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);
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;
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);
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);
2028 samples += avctx->channels;
2032 av_assert0(0); // unsupported codec_id should not happen
2035 if (avpkt->size && bytestream2_tell(&gb) == 0) {
2036 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2037 return AVERROR_INVALIDDATA;
2042 if (avpkt->size < bytestream2_tell(&gb)) {
2043 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2047 return bytestream2_tell(&gb);
2050 static void adpcm_flush(AVCodecContext *avctx)
2052 ADPCMDecodeContext *c = avctx->priv_data;
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,
2063 AV_SAMPLE_FMT_NONE };
2065 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2066 AVCodec ff_ ## name_ ## _decoder = { \
2068 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
2069 .type = AVMEDIA_TYPE_AUDIO, \
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_, \
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");