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);
562 f0 = xa_adpcm_table[filter][0];
563 f1 = xa_adpcm_table[filter][1];
571 t = sign_extend(d, 4);
572 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
574 s_1 = av_clip_int16(s);
581 s_1 = right->sample1;
582 s_2 = right->sample2;
585 shift = 12 - (in[5+i*2] & 15);
586 filter = in[5+i*2] >> 4;
587 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
588 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
592 f0 = xa_adpcm_table[filter][0];
593 f1 = xa_adpcm_table[filter][1];
598 t = sign_extend(d >> 4, 4);
599 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
601 s_1 = av_clip_int16(s);
606 right->sample1 = s_1;
607 right->sample2 = s_2;
613 out0 += 28 * (3 - channels);
614 out1 += 28 * (3 - channels);
620 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
622 ADPCMDecodeContext *c = avctx->priv_data;
625 int k0, signmask, nb_bits, count;
626 int size = buf_size*8;
629 init_get_bits(&gb, buf, size);
631 //read bits & initial values
632 nb_bits = get_bits(&gb, 2)+2;
633 table = swf_index_tables[nb_bits-2];
634 k0 = 1 << (nb_bits-2);
635 signmask = 1 << (nb_bits-1);
637 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
638 for (i = 0; i < avctx->channels; i++) {
639 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
640 c->status[i].step_index = get_bits(&gb, 6);
643 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
646 for (i = 0; i < avctx->channels; i++) {
647 // similar to IMA adpcm
648 int delta = get_bits(&gb, nb_bits);
649 int step = ff_adpcm_step_table[c->status[i].step_index];
650 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
661 if (delta & signmask)
662 c->status[i].predictor -= vpdiff;
664 c->status[i].predictor += vpdiff;
666 c->status[i].step_index += table[delta & (~signmask)];
668 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
669 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
671 *samples++ = c->status[i].predictor;
677 static inline int16_t adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int control, int shift)
679 int sample = nibble * (1 << shift);
682 sample += (8 * cs->sample1) - (4 * cs->sample2);
684 sample += 4 * cs->sample1;
686 sample = av_clip_int16(sample >> 2);
688 cs->sample2 = cs->sample1;
689 cs->sample1 = sample;
695 * Get the number of samples (per channel) that will be decoded from the packet.
696 * In one case, this is actually the maximum number of samples possible to
697 * decode with the given buf_size.
699 * @param[out] coded_samples set to the number of samples as coded in the
700 * packet, or 0 if the codec does not encode the
701 * number of samples in each frame.
702 * @param[out] approx_nb_samples set to non-zero if the number of samples
703 * returned is an approximation.
705 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
706 int buf_size, int *coded_samples, int *approx_nb_samples)
708 ADPCMDecodeContext *s = avctx->priv_data;
710 int ch = avctx->channels;
711 int has_coded_samples = 0;
715 *approx_nb_samples = 0;
720 switch (avctx->codec->id) {
721 /* constant, only check buf_size */
722 case AV_CODEC_ID_ADPCM_EA_XAS:
723 if (buf_size < 76 * ch)
727 case AV_CODEC_ID_ADPCM_IMA_QT:
728 if (buf_size < 34 * ch)
732 case AV_CODEC_ID_ADPCM_ARGO:
733 if (buf_size < 17 * ch)
737 /* simple 4-bit adpcm */
738 case AV_CODEC_ID_ADPCM_CT:
739 case AV_CODEC_ID_ADPCM_IMA_APC:
740 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
741 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
742 case AV_CODEC_ID_ADPCM_IMA_OKI:
743 case AV_CODEC_ID_ADPCM_IMA_WS:
744 case AV_CODEC_ID_ADPCM_YAMAHA:
745 case AV_CODEC_ID_ADPCM_AICA:
746 case AV_CODEC_ID_ADPCM_IMA_SSI:
747 case AV_CODEC_ID_ADPCM_IMA_APM:
748 case AV_CODEC_ID_ADPCM_IMA_ALP:
749 case AV_CODEC_ID_ADPCM_IMA_MTF:
750 nb_samples = buf_size * 2 / ch;
756 /* simple 4-bit adpcm, with header */
758 switch (avctx->codec->id) {
759 case AV_CODEC_ID_ADPCM_4XM:
760 case AV_CODEC_ID_ADPCM_AGM:
761 case AV_CODEC_ID_ADPCM_IMA_DAT4:
762 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
763 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
764 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
767 return (buf_size - header_size) * 2 / ch;
769 /* more complex formats */
770 switch (avctx->codec->id) {
771 case AV_CODEC_ID_ADPCM_EA:
772 has_coded_samples = 1;
773 *coded_samples = bytestream2_get_le32(gb);
774 *coded_samples -= *coded_samples % 28;
775 nb_samples = (buf_size - 12) / 30 * 28;
777 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
778 has_coded_samples = 1;
779 *coded_samples = bytestream2_get_le32(gb);
780 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
782 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
783 nb_samples = (buf_size - ch) / ch * 2;
785 case AV_CODEC_ID_ADPCM_EA_R1:
786 case AV_CODEC_ID_ADPCM_EA_R2:
787 case AV_CODEC_ID_ADPCM_EA_R3:
788 /* maximum number of samples */
789 /* has internal offsets and a per-frame switch to signal raw 16-bit */
790 has_coded_samples = 1;
791 switch (avctx->codec->id) {
792 case AV_CODEC_ID_ADPCM_EA_R1:
793 header_size = 4 + 9 * ch;
794 *coded_samples = bytestream2_get_le32(gb);
796 case AV_CODEC_ID_ADPCM_EA_R2:
797 header_size = 4 + 5 * ch;
798 *coded_samples = bytestream2_get_le32(gb);
800 case AV_CODEC_ID_ADPCM_EA_R3:
801 header_size = 4 + 5 * ch;
802 *coded_samples = bytestream2_get_be32(gb);
805 *coded_samples -= *coded_samples % 28;
806 nb_samples = (buf_size - header_size) * 2 / ch;
807 nb_samples -= nb_samples % 28;
808 *approx_nb_samples = 1;
810 case AV_CODEC_ID_ADPCM_IMA_DK3:
811 if (avctx->block_align > 0)
812 buf_size = FFMIN(buf_size, avctx->block_align);
813 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
815 case AV_CODEC_ID_ADPCM_IMA_DK4:
816 if (avctx->block_align > 0)
817 buf_size = FFMIN(buf_size, avctx->block_align);
818 if (buf_size < 4 * ch)
819 return AVERROR_INVALIDDATA;
820 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
822 case AV_CODEC_ID_ADPCM_IMA_RAD:
823 if (avctx->block_align > 0)
824 buf_size = FFMIN(buf_size, avctx->block_align);
825 nb_samples = (buf_size - 4 * ch) * 2 / ch;
827 case AV_CODEC_ID_ADPCM_IMA_WAV:
829 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
830 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
831 if (avctx->block_align > 0)
832 buf_size = FFMIN(buf_size, avctx->block_align);
833 if (buf_size < 4 * ch)
834 return AVERROR_INVALIDDATA;
835 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
838 case AV_CODEC_ID_ADPCM_MS:
839 if (avctx->block_align > 0)
840 buf_size = FFMIN(buf_size, avctx->block_align);
841 nb_samples = (buf_size - 6 * ch) * 2 / ch;
843 case AV_CODEC_ID_ADPCM_MTAF:
844 if (avctx->block_align > 0)
845 buf_size = FFMIN(buf_size, avctx->block_align);
846 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
848 case AV_CODEC_ID_ADPCM_SBPRO_2:
849 case AV_CODEC_ID_ADPCM_SBPRO_3:
850 case AV_CODEC_ID_ADPCM_SBPRO_4:
852 int samples_per_byte;
853 switch (avctx->codec->id) {
854 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
855 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
856 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
858 if (!s->status[0].step_index) {
860 return AVERROR_INVALIDDATA;
864 nb_samples += buf_size * samples_per_byte / ch;
867 case AV_CODEC_ID_ADPCM_SWF:
869 int buf_bits = buf_size * 8 - 2;
870 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
871 int block_hdr_size = 22 * ch;
872 int block_size = block_hdr_size + nbits * ch * 4095;
873 int nblocks = buf_bits / block_size;
874 int bits_left = buf_bits - nblocks * block_size;
875 nb_samples = nblocks * 4096;
876 if (bits_left >= block_hdr_size)
877 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
880 case AV_CODEC_ID_ADPCM_THP:
881 case AV_CODEC_ID_ADPCM_THP_LE:
882 if (avctx->extradata) {
883 nb_samples = buf_size * 14 / (8 * ch);
886 has_coded_samples = 1;
887 bytestream2_skip(gb, 4); // channel size
888 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
889 bytestream2_get_le32(gb) :
890 bytestream2_get_be32(gb);
891 buf_size -= 8 + 36 * ch;
893 nb_samples = buf_size / 8 * 14;
894 if (buf_size % 8 > 1)
895 nb_samples += (buf_size % 8 - 1) * 2;
896 *approx_nb_samples = 1;
898 case AV_CODEC_ID_ADPCM_AFC:
899 nb_samples = buf_size / (9 * ch) * 16;
901 case AV_CODEC_ID_ADPCM_XA:
902 nb_samples = (buf_size / 128) * 224 / ch;
904 case AV_CODEC_ID_ADPCM_DTK:
905 case AV_CODEC_ID_ADPCM_PSX:
906 nb_samples = buf_size / (16 * ch) * 28;
908 case AV_CODEC_ID_ADPCM_ZORK:
909 nb_samples = buf_size / ch;
913 /* validate coded sample count */
914 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
915 return AVERROR_INVALIDDATA;
920 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
921 int *got_frame_ptr, AVPacket *avpkt)
923 AVFrame *frame = data;
924 const uint8_t *buf = avpkt->data;
925 int buf_size = avpkt->size;
926 ADPCMDecodeContext *c = avctx->priv_data;
927 ADPCMChannelStatus *cs;
928 int n, m, channel, i;
933 int nb_samples, coded_samples, approx_nb_samples, ret;
936 bytestream2_init(&gb, buf, buf_size);
937 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
938 if (nb_samples <= 0) {
939 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
940 return AVERROR_INVALIDDATA;
943 /* get output buffer */
944 frame->nb_samples = nb_samples;
945 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
947 samples = (int16_t *)frame->data[0];
948 samples_p = (int16_t **)frame->extended_data;
950 /* use coded_samples when applicable */
951 /* it is always <= nb_samples, so the output buffer will be large enough */
953 if (!approx_nb_samples && coded_samples != nb_samples)
954 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
955 frame->nb_samples = nb_samples = coded_samples;
958 st = avctx->channels == 2 ? 1 : 0;
960 switch(avctx->codec->id) {
961 case AV_CODEC_ID_ADPCM_IMA_QT:
962 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
963 Channel data is interleaved per-chunk. */
964 for (channel = 0; channel < avctx->channels; channel++) {
967 cs = &(c->status[channel]);
968 /* (pppppp) (piiiiiii) */
970 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
971 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
972 step_index = predictor & 0x7F;
975 if (cs->step_index == step_index) {
976 int diff = predictor - cs->predictor;
983 cs->step_index = step_index;
984 cs->predictor = predictor;
987 if (cs->step_index > 88u){
988 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
989 channel, cs->step_index);
990 return AVERROR_INVALIDDATA;
993 samples = samples_p[channel];
995 for (m = 0; m < 64; m += 2) {
996 int byte = bytestream2_get_byteu(&gb);
997 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
998 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1002 case AV_CODEC_ID_ADPCM_IMA_WAV:
1003 for(i=0; i<avctx->channels; i++){
1004 cs = &(c->status[i]);
1005 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1007 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1008 if (cs->step_index > 88u){
1009 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1011 return AVERROR_INVALIDDATA;
1015 if (avctx->bits_per_coded_sample != 4) {
1016 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1017 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1018 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1021 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1022 for (i = 0; i < avctx->channels; i++) {
1026 samples = &samples_p[i][1 + n * samples_per_block];
1027 for (j = 0; j < block_size; j++) {
1028 temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
1029 (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
1031 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1034 for (m = 0; m < samples_per_block; m++) {
1035 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1036 avctx->bits_per_coded_sample);
1040 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1042 for (n = 0; n < (nb_samples - 1) / 8; n++) {
1043 for (i = 0; i < avctx->channels; i++) {
1045 samples = &samples_p[i][1 + n * 8];
1046 for (m = 0; m < 8; m += 2) {
1047 int v = bytestream2_get_byteu(&gb);
1048 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1049 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1055 case AV_CODEC_ID_ADPCM_4XM:
1056 for (i = 0; i < avctx->channels; i++)
1057 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1059 for (i = 0; i < avctx->channels; i++) {
1060 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1061 if (c->status[i].step_index > 88u) {
1062 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1063 i, c->status[i].step_index);
1064 return AVERROR_INVALIDDATA;
1068 for (i = 0; i < avctx->channels; i++) {
1069 samples = (int16_t *)frame->data[i];
1071 for (n = nb_samples >> 1; n > 0; n--) {
1072 int v = bytestream2_get_byteu(&gb);
1073 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1074 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1078 case AV_CODEC_ID_ADPCM_AGM:
1079 for (i = 0; i < avctx->channels; i++)
1080 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1081 for (i = 0; i < avctx->channels; i++)
1082 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1084 for (n = 0; n < nb_samples >> (1 - st); n++) {
1085 int v = bytestream2_get_byteu(&gb);
1086 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1087 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1090 case AV_CODEC_ID_ADPCM_MS:
1092 int block_predictor;
1094 if (avctx->channels > 2) {
1095 for (channel = 0; channel < avctx->channels; channel++) {
1096 samples = samples_p[channel];
1097 block_predictor = bytestream2_get_byteu(&gb);
1098 if (block_predictor > 6) {
1099 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1100 channel, block_predictor);
1101 return AVERROR_INVALIDDATA;
1103 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1104 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1105 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1106 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1107 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1108 *samples++ = c->status[channel].sample2;
1109 *samples++ = c->status[channel].sample1;
1110 for(n = (nb_samples - 2) >> 1; n > 0; n--) {
1111 int byte = bytestream2_get_byteu(&gb);
1112 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1113 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1117 block_predictor = bytestream2_get_byteu(&gb);
1118 if (block_predictor > 6) {
1119 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1121 return AVERROR_INVALIDDATA;
1123 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1124 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1126 block_predictor = bytestream2_get_byteu(&gb);
1127 if (block_predictor > 6) {
1128 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1130 return AVERROR_INVALIDDATA;
1132 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1133 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1135 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1137 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1140 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1141 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1142 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1143 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1145 *samples++ = c->status[0].sample2;
1146 if (st) *samples++ = c->status[1].sample2;
1147 *samples++ = c->status[0].sample1;
1148 if (st) *samples++ = c->status[1].sample1;
1149 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1150 int byte = bytestream2_get_byteu(&gb);
1151 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1152 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1157 case AV_CODEC_ID_ADPCM_MTAF:
1158 for (channel = 0; channel < avctx->channels; channel+=2) {
1159 bytestream2_skipu(&gb, 4);
1160 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1161 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1162 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1163 bytestream2_skipu(&gb, 2);
1164 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1165 bytestream2_skipu(&gb, 2);
1166 for (n = 0; n < nb_samples; n+=2) {
1167 int v = bytestream2_get_byteu(&gb);
1168 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1169 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1171 for (n = 0; n < nb_samples; n+=2) {
1172 int v = bytestream2_get_byteu(&gb);
1173 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1174 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1178 case AV_CODEC_ID_ADPCM_IMA_DK4:
1179 for (channel = 0; channel < avctx->channels; channel++) {
1180 cs = &c->status[channel];
1181 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1182 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1183 if (cs->step_index > 88u){
1184 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1185 channel, cs->step_index);
1186 return AVERROR_INVALIDDATA;
1189 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1190 int v = bytestream2_get_byteu(&gb);
1191 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1192 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1195 case AV_CODEC_ID_ADPCM_IMA_DK3:
1199 int decode_top_nibble_next = 0;
1201 const int16_t *samples_end = samples + avctx->channels * nb_samples;
1203 bytestream2_skipu(&gb, 10);
1204 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1205 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1206 c->status[0].step_index = bytestream2_get_byteu(&gb);
1207 c->status[1].step_index = bytestream2_get_byteu(&gb);
1208 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1209 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1210 c->status[0].step_index, c->status[1].step_index);
1211 return AVERROR_INVALIDDATA;
1213 /* sign extend the predictors */
1214 diff_channel = c->status[1].predictor;
1216 /* DK3 ADPCM support macro */
1217 #define DK3_GET_NEXT_NIBBLE() \
1218 if (decode_top_nibble_next) { \
1219 nibble = last_byte >> 4; \
1220 decode_top_nibble_next = 0; \
1222 last_byte = bytestream2_get_byteu(&gb); \
1223 nibble = last_byte & 0x0F; \
1224 decode_top_nibble_next = 1; \
1227 while (samples < samples_end) {
1229 /* for this algorithm, c->status[0] is the sum channel and
1230 * c->status[1] is the diff channel */
1232 /* process the first predictor of the sum channel */
1233 DK3_GET_NEXT_NIBBLE();
1234 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1236 /* process the diff channel predictor */
1237 DK3_GET_NEXT_NIBBLE();
1238 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1240 /* process the first pair of stereo PCM samples */
1241 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1242 *samples++ = c->status[0].predictor + c->status[1].predictor;
1243 *samples++ = c->status[0].predictor - c->status[1].predictor;
1245 /* process the second predictor of the sum channel */
1246 DK3_GET_NEXT_NIBBLE();
1247 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1249 /* process the second pair of stereo PCM samples */
1250 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1251 *samples++ = c->status[0].predictor + c->status[1].predictor;
1252 *samples++ = c->status[0].predictor - c->status[1].predictor;
1255 if ((bytestream2_tell(&gb) & 1))
1256 bytestream2_skip(&gb, 1);
1259 case AV_CODEC_ID_ADPCM_IMA_ISS:
1260 for (channel = 0; channel < avctx->channels; channel++) {
1261 cs = &c->status[channel];
1262 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1263 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1264 if (cs->step_index > 88u){
1265 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1266 channel, cs->step_index);
1267 return AVERROR_INVALIDDATA;
1271 for (n = nb_samples >> (1 - st); n > 0; n--) {
1273 int v = bytestream2_get_byteu(&gb);
1274 /* nibbles are swapped for mono */
1282 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1283 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1286 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1287 for (channel = 0; channel < avctx->channels; channel++) {
1288 cs = &c->status[channel];
1289 samples = samples_p[channel];
1290 bytestream2_skip(&gb, 4);
1291 for (n = 0; n < nb_samples; n += 2) {
1292 int v = bytestream2_get_byteu(&gb);
1293 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1294 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1298 case AV_CODEC_ID_ADPCM_IMA_APC:
1299 for (n = nb_samples >> (1 - st); n > 0; n--) {
1300 int v = bytestream2_get_byteu(&gb);
1301 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1302 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1305 case AV_CODEC_ID_ADPCM_IMA_SSI:
1306 for (n = nb_samples >> (1 - st); n > 0; n--) {
1307 int v = bytestream2_get_byteu(&gb);
1308 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1309 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1312 case AV_CODEC_ID_ADPCM_IMA_APM:
1313 for (n = nb_samples / 2; n > 0; n--) {
1314 for (channel = 0; channel < avctx->channels; channel++) {
1315 int v = bytestream2_get_byteu(&gb);
1316 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1317 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1319 samples += avctx->channels;
1322 case AV_CODEC_ID_ADPCM_IMA_ALP:
1323 for (n = nb_samples / 2; n > 0; n--) {
1324 for (channel = 0; channel < avctx->channels; channel++) {
1325 int v = bytestream2_get_byteu(&gb);
1326 *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1327 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1329 samples += avctx->channels;
1332 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1333 for (n = 0; n < nb_samples / 2; n++) {
1334 int v = bytestream2_get_byteu(&gb);
1335 *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v & 0x0F);
1336 *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v >> 4);
1339 case AV_CODEC_ID_ADPCM_IMA_OKI:
1340 for (n = nb_samples >> (1 - st); n > 0; n--) {
1341 int v = bytestream2_get_byteu(&gb);
1342 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1343 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1346 case AV_CODEC_ID_ADPCM_IMA_RAD:
1347 for (channel = 0; channel < avctx->channels; channel++) {
1348 cs = &c->status[channel];
1349 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1350 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1351 if (cs->step_index > 88u){
1352 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1353 channel, cs->step_index);
1354 return AVERROR_INVALIDDATA;
1357 for (n = 0; n < nb_samples / 2; n++) {
1360 byte[0] = bytestream2_get_byteu(&gb);
1362 byte[1] = bytestream2_get_byteu(&gb);
1363 for(channel = 0; channel < avctx->channels; channel++) {
1364 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1366 for(channel = 0; channel < avctx->channels; channel++) {
1367 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1371 case AV_CODEC_ID_ADPCM_IMA_WS:
1372 if (c->vqa_version == 3) {
1373 for (channel = 0; channel < avctx->channels; channel++) {
1374 int16_t *smp = samples_p[channel];
1376 for (n = nb_samples / 2; n > 0; n--) {
1377 int v = bytestream2_get_byteu(&gb);
1378 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1379 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1383 for (n = nb_samples / 2; n > 0; n--) {
1384 for (channel = 0; channel < avctx->channels; channel++) {
1385 int v = bytestream2_get_byteu(&gb);
1386 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1387 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1389 samples += avctx->channels;
1392 bytestream2_seek(&gb, 0, SEEK_END);
1394 case AV_CODEC_ID_ADPCM_XA:
1396 int16_t *out0 = samples_p[0];
1397 int16_t *out1 = samples_p[1];
1398 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1399 int sample_offset = 0;
1400 int bytes_remaining;
1401 while (bytestream2_get_bytes_left(&gb) >= 128) {
1402 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1403 &c->status[0], &c->status[1],
1404 avctx->channels, sample_offset)) < 0)
1406 bytestream2_skipu(&gb, 128);
1407 sample_offset += samples_per_block;
1409 /* Less than a full block of data left, e.g. when reading from
1410 * 2324 byte per sector XA; the remainder is padding */
1411 bytes_remaining = bytestream2_get_bytes_left(&gb);
1412 if (bytes_remaining > 0) {
1413 bytestream2_skip(&gb, bytes_remaining);
1417 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1418 for (i=0; i<=st; i++) {
1419 c->status[i].step_index = bytestream2_get_le32u(&gb);
1420 if (c->status[i].step_index > 88u) {
1421 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1422 i, c->status[i].step_index);
1423 return AVERROR_INVALIDDATA;
1426 for (i=0; i<=st; i++) {
1427 c->status[i].predictor = bytestream2_get_le32u(&gb);
1428 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1429 return AVERROR_INVALIDDATA;
1432 for (n = nb_samples >> (1 - st); n > 0; n--) {
1433 int byte = bytestream2_get_byteu(&gb);
1434 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1435 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1438 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1439 for (n = nb_samples >> (1 - st); n > 0; n--) {
1440 int byte = bytestream2_get_byteu(&gb);
1441 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1442 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1445 case AV_CODEC_ID_ADPCM_EA:
1447 int previous_left_sample, previous_right_sample;
1448 int current_left_sample, current_right_sample;
1449 int next_left_sample, next_right_sample;
1450 int coeff1l, coeff2l, coeff1r, coeff2r;
1451 int shift_left, shift_right;
1453 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1454 each coding 28 stereo samples. */
1456 if(avctx->channels != 2)
1457 return AVERROR_INVALIDDATA;
1459 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1460 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1461 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1462 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1464 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1465 int byte = bytestream2_get_byteu(&gb);
1466 coeff1l = ea_adpcm_table[ byte >> 4 ];
1467 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1468 coeff1r = ea_adpcm_table[ byte & 0x0F];
1469 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1471 byte = bytestream2_get_byteu(&gb);
1472 shift_left = 20 - (byte >> 4);
1473 shift_right = 20 - (byte & 0x0F);
1475 for (count2 = 0; count2 < 28; count2++) {
1476 byte = bytestream2_get_byteu(&gb);
1477 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1478 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1480 next_left_sample = (next_left_sample +
1481 (current_left_sample * coeff1l) +
1482 (previous_left_sample * coeff2l) + 0x80) >> 8;
1483 next_right_sample = (next_right_sample +
1484 (current_right_sample * coeff1r) +
1485 (previous_right_sample * coeff2r) + 0x80) >> 8;
1487 previous_left_sample = current_left_sample;
1488 current_left_sample = av_clip_int16(next_left_sample);
1489 previous_right_sample = current_right_sample;
1490 current_right_sample = av_clip_int16(next_right_sample);
1491 *samples++ = current_left_sample;
1492 *samples++ = current_right_sample;
1496 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1500 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1502 int coeff[2][2], shift[2];
1504 for(channel = 0; channel < avctx->channels; channel++) {
1505 int byte = bytestream2_get_byteu(&gb);
1507 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1508 shift[channel] = 20 - (byte & 0x0F);
1510 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1513 byte[0] = bytestream2_get_byteu(&gb);
1514 if (st) byte[1] = bytestream2_get_byteu(&gb);
1515 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1516 for(channel = 0; channel < avctx->channels; channel++) {
1517 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1519 c->status[channel].sample1 * coeff[channel][0] +
1520 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1521 c->status[channel].sample2 = c->status[channel].sample1;
1522 c->status[channel].sample1 = av_clip_int16(sample);
1523 *samples++ = c->status[channel].sample1;
1527 bytestream2_seek(&gb, 0, SEEK_END);
1530 case AV_CODEC_ID_ADPCM_EA_R1:
1531 case AV_CODEC_ID_ADPCM_EA_R2:
1532 case AV_CODEC_ID_ADPCM_EA_R3: {
1533 /* channel numbering
1535 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1536 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1537 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1538 int previous_sample, current_sample, next_sample;
1541 unsigned int channel;
1546 for (channel=0; channel<avctx->channels; channel++)
1547 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1548 bytestream2_get_le32(&gb)) +
1549 (avctx->channels + 1) * 4;
1551 for (channel=0; channel<avctx->channels; channel++) {
1552 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1553 samplesC = samples_p[channel];
1555 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1556 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1557 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1559 current_sample = c->status[channel].predictor;
1560 previous_sample = c->status[channel].prev_sample;
1563 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1564 int byte = bytestream2_get_byte(&gb);
1565 if (byte == 0xEE) { /* only seen in R2 and R3 */
1566 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1567 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1569 for (count2=0; count2<28; count2++)
1570 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1572 coeff1 = ea_adpcm_table[ byte >> 4 ];
1573 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1574 shift = 20 - (byte & 0x0F);
1576 for (count2=0; count2<28; count2++) {
1578 next_sample = (unsigned)sign_extend(byte, 4) << shift;
1580 byte = bytestream2_get_byte(&gb);
1581 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1584 next_sample += (current_sample * coeff1) +
1585 (previous_sample * coeff2);
1586 next_sample = av_clip_int16(next_sample >> 8);
1588 previous_sample = current_sample;
1589 current_sample = next_sample;
1590 *samplesC++ = current_sample;
1596 } else if (count != count1) {
1597 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1598 count = FFMAX(count, count1);
1601 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1602 c->status[channel].predictor = current_sample;
1603 c->status[channel].prev_sample = previous_sample;
1607 frame->nb_samples = count * 28;
1608 bytestream2_seek(&gb, 0, SEEK_END);
1611 case AV_CODEC_ID_ADPCM_EA_XAS:
1612 for (channel=0; channel<avctx->channels; channel++) {
1613 int coeff[2][4], shift[4];
1614 int16_t *s = samples_p[channel];
1615 for (n = 0; n < 4; n++, s += 32) {
1616 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1618 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1621 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1622 shift[n] = 20 - (val & 0x0F);
1626 for (m=2; m<32; m+=2) {
1627 s = &samples_p[channel][m];
1628 for (n = 0; n < 4; n++, s += 32) {
1630 int byte = bytestream2_get_byteu(&gb);
1632 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1633 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1634 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1636 level = sign_extend(byte, 4) * (1 << shift[n]);
1637 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1638 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1643 case AV_CODEC_ID_ADPCM_IMA_AMV:
1644 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1645 c->status[0].step_index = bytestream2_get_byteu(&gb);
1646 bytestream2_skipu(&gb, 5);
1647 if (c->status[0].step_index > 88u) {
1648 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1649 c->status[0].step_index);
1650 return AVERROR_INVALIDDATA;
1653 for (n = nb_samples >> (1 - st); n > 0; n--) {
1654 int v = bytestream2_get_byteu(&gb);
1656 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1657 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1660 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1661 for (i = 0; i < avctx->channels; i++) {
1662 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1663 c->status[i].step_index = bytestream2_get_byteu(&gb);
1664 bytestream2_skipu(&gb, 1);
1665 if (c->status[i].step_index > 88u) {
1666 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1667 c->status[i].step_index);
1668 return AVERROR_INVALIDDATA;
1672 for (n = nb_samples >> (1 - st); n > 0; n--) {
1673 int v = bytestream2_get_byteu(&gb);
1675 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1676 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1679 case AV_CODEC_ID_ADPCM_CT:
1680 for (n = nb_samples >> (1 - st); n > 0; n--) {
1681 int v = bytestream2_get_byteu(&gb);
1682 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1683 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1686 case AV_CODEC_ID_ADPCM_SBPRO_4:
1687 case AV_CODEC_ID_ADPCM_SBPRO_3:
1688 case AV_CODEC_ID_ADPCM_SBPRO_2:
1689 if (!c->status[0].step_index) {
1690 /* the first byte is a raw sample */
1691 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1693 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1694 c->status[0].step_index = 1;
1697 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1698 for (n = nb_samples >> (1 - st); n > 0; n--) {
1699 int byte = bytestream2_get_byteu(&gb);
1700 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1702 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1705 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1706 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1707 int byte = bytestream2_get_byteu(&gb);
1708 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1710 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1711 (byte >> 2) & 0x07, 3, 0);
1712 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1716 for (n = nb_samples >> (2 - st); n > 0; n--) {
1717 int byte = bytestream2_get_byteu(&gb);
1718 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1720 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1721 (byte >> 4) & 0x03, 2, 2);
1722 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1723 (byte >> 2) & 0x03, 2, 2);
1724 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1729 case AV_CODEC_ID_ADPCM_SWF:
1730 adpcm_swf_decode(avctx, buf, buf_size, samples);
1731 bytestream2_seek(&gb, 0, SEEK_END);
1733 case AV_CODEC_ID_ADPCM_YAMAHA:
1734 for (n = nb_samples >> (1 - st); n > 0; n--) {
1735 int v = bytestream2_get_byteu(&gb);
1736 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1737 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1740 case AV_CODEC_ID_ADPCM_AICA:
1741 if (!c->has_status) {
1742 for (channel = 0; channel < avctx->channels; channel++)
1743 c->status[channel].step = 0;
1746 for (channel = 0; channel < avctx->channels; channel++) {
1747 samples = samples_p[channel];
1748 for (n = nb_samples >> 1; n > 0; n--) {
1749 int v = bytestream2_get_byteu(&gb);
1750 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1751 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1755 case AV_CODEC_ID_ADPCM_AFC:
1757 int samples_per_block;
1760 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1761 samples_per_block = avctx->extradata[0] / 16;
1762 blocks = nb_samples / avctx->extradata[0];
1764 samples_per_block = nb_samples / 16;
1768 for (m = 0; m < blocks; m++) {
1769 for (channel = 0; channel < avctx->channels; channel++) {
1770 int prev1 = c->status[channel].sample1;
1771 int prev2 = c->status[channel].sample2;
1773 samples = samples_p[channel] + m * 16;
1774 /* Read in every sample for this channel. */
1775 for (i = 0; i < samples_per_block; i++) {
1776 int byte = bytestream2_get_byteu(&gb);
1777 int scale = 1 << (byte >> 4);
1778 int index = byte & 0xf;
1779 int factor1 = ff_adpcm_afc_coeffs[0][index];
1780 int factor2 = ff_adpcm_afc_coeffs[1][index];
1782 /* Decode 16 samples. */
1783 for (n = 0; n < 16; n++) {
1787 sampledat = sign_extend(byte, 4);
1789 byte = bytestream2_get_byteu(&gb);
1790 sampledat = sign_extend(byte >> 4, 4);
1793 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1795 *samples = av_clip_int16(sampledat);
1801 c->status[channel].sample1 = prev1;
1802 c->status[channel].sample2 = prev2;
1805 bytestream2_seek(&gb, 0, SEEK_END);
1808 case AV_CODEC_ID_ADPCM_THP:
1809 case AV_CODEC_ID_ADPCM_THP_LE:
1814 #define THP_GET16(g) \
1816 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1817 bytestream2_get_le16u(&(g)) : \
1818 bytestream2_get_be16u(&(g)), 16)
1820 if (avctx->extradata) {
1822 if (avctx->extradata_size < 32 * avctx->channels) {
1823 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1824 return AVERROR_INVALIDDATA;
1827 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1828 for (i = 0; i < avctx->channels; i++)
1829 for (n = 0; n < 16; n++)
1830 table[i][n] = THP_GET16(tb);
1832 for (i = 0; i < avctx->channels; i++)
1833 for (n = 0; n < 16; n++)
1834 table[i][n] = THP_GET16(gb);
1836 if (!c->has_status) {
1837 /* Initialize the previous sample. */
1838 for (i = 0; i < avctx->channels; i++) {
1839 c->status[i].sample1 = THP_GET16(gb);
1840 c->status[i].sample2 = THP_GET16(gb);
1844 bytestream2_skip(&gb, avctx->channels * 4);
1848 for (ch = 0; ch < avctx->channels; ch++) {
1849 samples = samples_p[ch];
1851 /* Read in every sample for this channel. */
1852 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1853 int byte = bytestream2_get_byteu(&gb);
1854 int index = (byte >> 4) & 7;
1855 unsigned int exp = byte & 0x0F;
1856 int factor1 = table[ch][index * 2];
1857 int factor2 = table[ch][index * 2 + 1];
1859 /* Decode 14 samples. */
1860 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1864 sampledat = sign_extend(byte, 4);
1866 byte = bytestream2_get_byteu(&gb);
1867 sampledat = sign_extend(byte >> 4, 4);
1870 sampledat = ((c->status[ch].sample1 * factor1
1871 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1872 *samples = av_clip_int16(sampledat);
1873 c->status[ch].sample2 = c->status[ch].sample1;
1874 c->status[ch].sample1 = *samples++;
1880 case AV_CODEC_ID_ADPCM_DTK:
1881 for (channel = 0; channel < avctx->channels; channel++) {
1882 samples = samples_p[channel];
1884 /* Read in every sample for this channel. */
1885 for (i = 0; i < nb_samples / 28; i++) {
1888 bytestream2_skipu(&gb, 1);
1889 header = bytestream2_get_byteu(&gb);
1890 bytestream2_skipu(&gb, 3 - channel);
1892 /* Decode 28 samples. */
1893 for (n = 0; n < 28; n++) {
1894 int32_t sampledat, prev;
1896 switch (header >> 4) {
1898 prev = (c->status[channel].sample1 * 0x3c);
1901 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1904 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1910 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1912 byte = bytestream2_get_byteu(&gb);
1914 sampledat = sign_extend(byte, 4);
1916 sampledat = sign_extend(byte >> 4, 4);
1918 sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1919 *samples++ = av_clip_int16(sampledat >> 6);
1920 c->status[channel].sample2 = c->status[channel].sample1;
1921 c->status[channel].sample1 = sampledat;
1925 bytestream2_seek(&gb, 0, SEEK_SET);
1928 case AV_CODEC_ID_ADPCM_PSX:
1929 for (channel = 0; channel < avctx->channels; channel++) {
1930 samples = samples_p[channel];
1932 /* Read in every sample for this channel. */
1933 for (i = 0; i < nb_samples / 28; i++) {
1934 int filter, shift, flag, byte;
1936 filter = bytestream2_get_byteu(&gb);
1937 shift = filter & 0xf;
1938 filter = filter >> 4;
1939 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1940 return AVERROR_INVALIDDATA;
1941 flag = bytestream2_get_byteu(&gb);
1943 /* Decode 28 samples. */
1944 for (n = 0; n < 28; n++) {
1945 int sample = 0, scale;
1949 scale = sign_extend(byte >> 4, 4);
1951 byte = bytestream2_get_byteu(&gb);
1952 scale = sign_extend(byte, 4);
1955 scale = scale * (1 << 12);
1956 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1958 *samples++ = av_clip_int16(sample);
1959 c->status[channel].sample2 = c->status[channel].sample1;
1960 c->status[channel].sample1 = sample;
1965 case AV_CODEC_ID_ADPCM_ARGO:
1967 * The format of each block:
1968 * uint8_t left_control;
1969 * uint4_t left_samples[nb_samples];
1970 * ---- and if stereo ----
1971 * uint8_t right_control;
1972 * uint4_t right_samples[nb_samples];
1974 * Format of the control byte:
1975 * MSB [SSSSDRRR] LSB
1976 * S = (Shift Amount - 2)
1980 * Each block relies on the previous two samples of each channel.
1981 * They should be 0 initially.
1983 for (channel = 0; channel < avctx->channels; channel++) {
1986 samples = samples_p[channel];
1987 cs = c->status + channel;
1989 /* Get the control byte and decode the samples, 2 at a time. */
1990 control = bytestream2_get_byteu(&gb);
1991 shift = (control >> 4) + 2;
1993 for (n = 0; n < nb_samples / 2; n++) {
1994 int sample = bytestream2_get_byteu(&gb);
1995 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 4, 4), control, shift);
1996 *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 0, 4), control, shift);
2000 case AV_CODEC_ID_ADPCM_ZORK:
2001 if (!c->has_status) {
2002 for (channel = 0; channel < avctx->channels; channel++) {
2003 c->status[channel].predictor = 0;
2004 c->status[channel].step_index = 0;
2008 for (n = 0; n < nb_samples * avctx->channels; n++) {
2009 int v = bytestream2_get_byteu(&gb);
2010 *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
2013 case AV_CODEC_ID_ADPCM_IMA_MTF:
2014 for (n = nb_samples / 2; n > 0; n--) {
2015 for (channel = 0; channel < avctx->channels; channel++) {
2016 int v = bytestream2_get_byteu(&gb);
2017 *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2018 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2020 samples += avctx->channels;
2024 av_assert0(0); // unsupported codec_id should not happen
2027 if (avpkt->size && bytestream2_tell(&gb) == 0) {
2028 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2029 return AVERROR_INVALIDDATA;
2034 if (avpkt->size < bytestream2_tell(&gb)) {
2035 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2039 return bytestream2_tell(&gb);
2042 static void adpcm_flush(AVCodecContext *avctx)
2044 ADPCMDecodeContext *c = avctx->priv_data;
2049 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
2050 AV_SAMPLE_FMT_NONE };
2051 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2052 AV_SAMPLE_FMT_NONE };
2053 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2055 AV_SAMPLE_FMT_NONE };
2057 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2058 AVCodec ff_ ## name_ ## _decoder = { \
2060 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
2061 .type = AVMEDIA_TYPE_AUDIO, \
2063 .priv_data_size = sizeof(ADPCMDecodeContext), \
2064 .init = adpcm_decode_init, \
2065 .decode = adpcm_decode_frame, \
2066 .flush = adpcm_flush, \
2067 .capabilities = AV_CODEC_CAP_DR1, \
2068 .sample_fmts = sample_fmts_, \
2071 /* Note: Do not forget to add new entries to the Makefile as well. */
2072 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
2073 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
2074 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
2075 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
2076 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO, sample_fmts_s16p, adpcm_argo, "ADPCM Argonaut Games");
2077 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
2078 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
2079 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
2080 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2081 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
2082 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
2083 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
2084 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
2085 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
2086 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
2087 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM, sample_fmts_s16, adpcm_ima_apm, "ADPCM IMA Ubisoft APM");
2088 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16, adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2089 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
2090 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
2091 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
2092 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2093 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2094 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
2095 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF, sample_fmts_s16, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework");
2096 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
2097 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
2098 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
2099 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI, sample_fmts_s16, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive");
2100 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
2101 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP, sample_fmts_s16, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP");
2102 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
2103 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
2104 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
2105 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
2106 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
2107 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
2108 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
2109 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
2110 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
2111 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
2112 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
2113 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
2114 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");
2115 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK, sample_fmts_s16, adpcm_zork, "ADPCM Zork");