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)
16 * This file is part of FFmpeg.
18 * FFmpeg is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
23 * FFmpeg is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with FFmpeg; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bytestream.h"
36 #include "adpcm_data.h"
42 * Features and limitations:
44 * Reference documents:
45 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48 * http://openquicktime.sourceforge.net/
49 * XAnim sources (xa_codec.c) http://xanim.polter.net/
50 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51 * SoX source code http://sox.sourceforge.net/
54 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56 * readstr http://www.geocities.co.jp/Playtown/2004/
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table[5][2] = {
68 static const int ea_adpcm_table[] = {
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables[4][16] = {
79 /*3*/ { -1, -1, 2, 4 },
80 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
86 typedef struct ADPCMDecodeContext {
87 ADPCMChannelStatus status[14];
88 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
92 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
94 ADPCMDecodeContext *c = avctx->priv_data;
95 unsigned int min_channels = 1;
96 unsigned int max_channels = 2;
98 switch(avctx->codec->id) {
99 case AV_CODEC_ID_ADPCM_DTK:
100 case AV_CODEC_ID_ADPCM_EA:
103 case AV_CODEC_ID_ADPCM_AFC:
104 case AV_CODEC_ID_ADPCM_EA_R1:
105 case AV_CODEC_ID_ADPCM_EA_R2:
106 case AV_CODEC_ID_ADPCM_EA_R3:
107 case AV_CODEC_ID_ADPCM_EA_XAS:
110 case AV_CODEC_ID_ADPCM_PSX:
113 case AV_CODEC_ID_ADPCM_IMA_DAT4:
114 case AV_CODEC_ID_ADPCM_THP:
115 case AV_CODEC_ID_ADPCM_THP_LE:
119 if (avctx->channels < min_channels || avctx->channels > max_channels) {
120 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
121 return AVERROR(EINVAL);
124 switch(avctx->codec->id) {
125 case AV_CODEC_ID_ADPCM_CT:
126 c->status[0].step = c->status[1].step = 511;
128 case AV_CODEC_ID_ADPCM_IMA_WAV:
129 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
130 return AVERROR_INVALIDDATA;
132 case AV_CODEC_ID_ADPCM_IMA_APC:
133 if (avctx->extradata && avctx->extradata_size >= 8) {
134 c->status[0].predictor = AV_RL32(avctx->extradata);
135 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
138 case AV_CODEC_ID_ADPCM_IMA_WS:
139 if (avctx->extradata && avctx->extradata_size >= 2)
140 c->vqa_version = AV_RL16(avctx->extradata);
146 switch(avctx->codec->id) {
147 case AV_CODEC_ID_ADPCM_AICA:
148 case AV_CODEC_ID_ADPCM_IMA_DAT4:
149 case AV_CODEC_ID_ADPCM_IMA_QT:
150 case AV_CODEC_ID_ADPCM_IMA_WAV:
151 case AV_CODEC_ID_ADPCM_4XM:
152 case AV_CODEC_ID_ADPCM_XA:
153 case AV_CODEC_ID_ADPCM_EA_R1:
154 case AV_CODEC_ID_ADPCM_EA_R2:
155 case AV_CODEC_ID_ADPCM_EA_R3:
156 case AV_CODEC_ID_ADPCM_EA_XAS:
157 case AV_CODEC_ID_ADPCM_THP:
158 case AV_CODEC_ID_ADPCM_THP_LE:
159 case AV_CODEC_ID_ADPCM_AFC:
160 case AV_CODEC_ID_ADPCM_DTK:
161 case AV_CODEC_ID_ADPCM_PSX:
162 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
164 case AV_CODEC_ID_ADPCM_IMA_WS:
165 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
169 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
175 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
179 int sign, delta, diff, step;
181 step = ff_adpcm_step_table[c->step_index];
182 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
183 step_index = av_clip(step_index, 0, 88);
187 /* perform direct multiplication instead of series of jumps proposed by
188 * the reference ADPCM implementation since modern CPUs can do the mults
190 diff = ((2 * delta + 1) * step) >> shift;
191 predictor = c->predictor;
192 if (sign) predictor -= diff;
193 else predictor += diff;
195 c->predictor = av_clip_int16(predictor);
196 c->step_index = step_index;
198 return (int16_t)c->predictor;
201 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
203 int nibble, step_index, predictor, sign, delta, diff, step, shift;
206 nibble = get_bits_le(gb, bps),
207 step = ff_adpcm_step_table[c->step_index];
208 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
209 step_index = av_clip(step_index, 0, 88);
211 sign = nibble & (1 << shift);
212 delta = av_mod_uintp2(nibble, shift);
213 diff = ((2 * delta + 1) * step) >> shift;
214 predictor = c->predictor;
215 if (sign) predictor -= diff;
216 else predictor += diff;
218 c->predictor = av_clip_int16(predictor);
219 c->step_index = step_index;
221 return (int16_t)c->predictor;
224 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
230 step = ff_adpcm_step_table[c->step_index];
231 step_index = c->step_index + ff_adpcm_index_table[nibble];
232 step_index = av_clip(step_index, 0, 88);
235 if (nibble & 4) diff += step;
236 if (nibble & 2) diff += step >> 1;
237 if (nibble & 1) diff += step >> 2;
240 predictor = c->predictor - diff;
242 predictor = c->predictor + diff;
244 c->predictor = av_clip_int16(predictor);
245 c->step_index = step_index;
250 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
254 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
255 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
257 c->sample2 = c->sample1;
258 c->sample1 = av_clip_int16(predictor);
259 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
260 if (c->idelta < 16) c->idelta = 16;
261 if (c->idelta > INT_MAX/768) {
262 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
263 c->idelta = INT_MAX/768;
269 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
271 int step_index, predictor, sign, delta, diff, step;
273 step = ff_adpcm_oki_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, 48);
279 diff = ((2 * delta + 1) * step) >> 3;
280 predictor = c->predictor;
281 if (sign) predictor -= diff;
282 else predictor += diff;
284 c->predictor = av_clip_intp2(predictor, 11);
285 c->step_index = step_index;
287 return c->predictor << 4;
290 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
292 int sign, delta, diff;
297 /* perform direct multiplication instead of series of jumps proposed by
298 * the reference ADPCM implementation since modern CPUs can do the mults
300 diff = ((2 * delta + 1) * c->step) >> 3;
301 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
302 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
303 c->predictor = av_clip_int16(c->predictor);
304 /* calculate new step and clamp it to range 511..32767 */
305 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
306 c->step = av_clip(new_step, 511, 32767);
308 return (int16_t)c->predictor;
311 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
313 int sign, delta, diff;
315 sign = nibble & (1<<(size-1));
316 delta = nibble & ((1<<(size-1))-1);
317 diff = delta << (7 + c->step + shift);
320 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
322 /* calculate new step */
323 if (delta >= (2*size - 3) && c->step < 3)
325 else if (delta == 0 && c->step > 0)
328 return (int16_t) c->predictor;
331 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
338 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
339 c->predictor = av_clip_int16(c->predictor);
340 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
341 c->step = av_clip(c->step, 127, 24567);
345 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
346 const uint8_t *in, ADPCMChannelStatus *left,
347 ADPCMChannelStatus *right, int channels, int sample_offset)
350 int shift,filter,f0,f1;
354 out0 += sample_offset;
358 out1 += sample_offset;
361 shift = 12 - (in[4+i*2] & 15);
362 filter = in[4+i*2] >> 4;
363 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
364 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
367 f0 = xa_adpcm_table[filter][0];
368 f1 = xa_adpcm_table[filter][1];
376 t = sign_extend(d, 4);
377 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
379 s_1 = av_clip_int16(s);
386 s_1 = right->sample1;
387 s_2 = right->sample2;
390 shift = 12 - (in[5+i*2] & 15);
391 filter = in[5+i*2] >> 4;
392 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
393 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
397 f0 = xa_adpcm_table[filter][0];
398 f1 = xa_adpcm_table[filter][1];
403 t = sign_extend(d >> 4, 4);
404 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
406 s_1 = av_clip_int16(s);
411 right->sample1 = s_1;
412 right->sample2 = s_2;
418 out0 += 28 * (3 - channels);
419 out1 += 28 * (3 - channels);
425 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
427 ADPCMDecodeContext *c = avctx->priv_data;
430 int k0, signmask, nb_bits, count;
431 int size = buf_size*8;
434 init_get_bits(&gb, buf, size);
436 //read bits & initial values
437 nb_bits = get_bits(&gb, 2)+2;
438 table = swf_index_tables[nb_bits-2];
439 k0 = 1 << (nb_bits-2);
440 signmask = 1 << (nb_bits-1);
442 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
443 for (i = 0; i < avctx->channels; i++) {
444 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
445 c->status[i].step_index = get_bits(&gb, 6);
448 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
451 for (i = 0; i < avctx->channels; i++) {
452 // similar to IMA adpcm
453 int delta = get_bits(&gb, nb_bits);
454 int step = ff_adpcm_step_table[c->status[i].step_index];
455 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
466 if (delta & signmask)
467 c->status[i].predictor -= vpdiff;
469 c->status[i].predictor += vpdiff;
471 c->status[i].step_index += table[delta & (~signmask)];
473 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
474 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
476 *samples++ = c->status[i].predictor;
483 * Get the number of samples that will be decoded from the packet.
484 * In one case, this is actually the maximum number of samples possible to
485 * decode with the given buf_size.
487 * @param[out] coded_samples set to the number of samples as coded in the
488 * packet, or 0 if the codec does not encode the
489 * number of samples in each frame.
490 * @param[out] approx_nb_samples set to non-zero if the number of samples
491 * returned is an approximation.
493 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
494 int buf_size, int *coded_samples, int *approx_nb_samples)
496 ADPCMDecodeContext *s = avctx->priv_data;
498 int ch = avctx->channels;
499 int has_coded_samples = 0;
503 *approx_nb_samples = 0;
508 switch (avctx->codec->id) {
509 /* constant, only check buf_size */
510 case AV_CODEC_ID_ADPCM_EA_XAS:
511 if (buf_size < 76 * ch)
515 case AV_CODEC_ID_ADPCM_IMA_QT:
516 if (buf_size < 34 * ch)
520 /* simple 4-bit adpcm */
521 case AV_CODEC_ID_ADPCM_CT:
522 case AV_CODEC_ID_ADPCM_IMA_APC:
523 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
524 case AV_CODEC_ID_ADPCM_IMA_OKI:
525 case AV_CODEC_ID_ADPCM_IMA_WS:
526 case AV_CODEC_ID_ADPCM_YAMAHA:
527 case AV_CODEC_ID_ADPCM_AICA:
528 nb_samples = buf_size * 2 / ch;
534 /* simple 4-bit adpcm, with header */
536 switch (avctx->codec->id) {
537 case AV_CODEC_ID_ADPCM_4XM:
538 case AV_CODEC_ID_ADPCM_IMA_DAT4:
539 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
540 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
541 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
544 return (buf_size - header_size) * 2 / ch;
546 /* more complex formats */
547 switch (avctx->codec->id) {
548 case AV_CODEC_ID_ADPCM_EA:
549 has_coded_samples = 1;
550 *coded_samples = bytestream2_get_le32(gb);
551 *coded_samples -= *coded_samples % 28;
552 nb_samples = (buf_size - 12) / 30 * 28;
554 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
555 has_coded_samples = 1;
556 *coded_samples = bytestream2_get_le32(gb);
557 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
559 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
560 nb_samples = (buf_size - ch) / ch * 2;
562 case AV_CODEC_ID_ADPCM_EA_R1:
563 case AV_CODEC_ID_ADPCM_EA_R2:
564 case AV_CODEC_ID_ADPCM_EA_R3:
565 /* maximum number of samples */
566 /* has internal offsets and a per-frame switch to signal raw 16-bit */
567 has_coded_samples = 1;
568 switch (avctx->codec->id) {
569 case AV_CODEC_ID_ADPCM_EA_R1:
570 header_size = 4 + 9 * ch;
571 *coded_samples = bytestream2_get_le32(gb);
573 case AV_CODEC_ID_ADPCM_EA_R2:
574 header_size = 4 + 5 * ch;
575 *coded_samples = bytestream2_get_le32(gb);
577 case AV_CODEC_ID_ADPCM_EA_R3:
578 header_size = 4 + 5 * ch;
579 *coded_samples = bytestream2_get_be32(gb);
582 *coded_samples -= *coded_samples % 28;
583 nb_samples = (buf_size - header_size) * 2 / ch;
584 nb_samples -= nb_samples % 28;
585 *approx_nb_samples = 1;
587 case AV_CODEC_ID_ADPCM_IMA_DK3:
588 if (avctx->block_align > 0)
589 buf_size = FFMIN(buf_size, avctx->block_align);
590 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
592 case AV_CODEC_ID_ADPCM_IMA_DK4:
593 if (avctx->block_align > 0)
594 buf_size = FFMIN(buf_size, avctx->block_align);
595 if (buf_size < 4 * ch)
596 return AVERROR_INVALIDDATA;
597 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
599 case AV_CODEC_ID_ADPCM_IMA_RAD:
600 if (avctx->block_align > 0)
601 buf_size = FFMIN(buf_size, avctx->block_align);
602 nb_samples = (buf_size - 4 * ch) * 2 / ch;
604 case AV_CODEC_ID_ADPCM_IMA_WAV:
606 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
607 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
608 if (avctx->block_align > 0)
609 buf_size = FFMIN(buf_size, avctx->block_align);
610 if (buf_size < 4 * ch)
611 return AVERROR_INVALIDDATA;
612 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
615 case AV_CODEC_ID_ADPCM_MS:
616 if (avctx->block_align > 0)
617 buf_size = FFMIN(buf_size, avctx->block_align);
618 nb_samples = (buf_size - 6 * ch) * 2 / ch;
620 case AV_CODEC_ID_ADPCM_SBPRO_2:
621 case AV_CODEC_ID_ADPCM_SBPRO_3:
622 case AV_CODEC_ID_ADPCM_SBPRO_4:
624 int samples_per_byte;
625 switch (avctx->codec->id) {
626 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
627 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
628 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
630 if (!s->status[0].step_index) {
632 return AVERROR_INVALIDDATA;
636 nb_samples += buf_size * samples_per_byte / ch;
639 case AV_CODEC_ID_ADPCM_SWF:
641 int buf_bits = buf_size * 8 - 2;
642 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
643 int block_hdr_size = 22 * ch;
644 int block_size = block_hdr_size + nbits * ch * 4095;
645 int nblocks = buf_bits / block_size;
646 int bits_left = buf_bits - nblocks * block_size;
647 nb_samples = nblocks * 4096;
648 if (bits_left >= block_hdr_size)
649 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
652 case AV_CODEC_ID_ADPCM_THP:
653 case AV_CODEC_ID_ADPCM_THP_LE:
654 if (avctx->extradata) {
655 nb_samples = buf_size * 14 / (8 * ch);
658 has_coded_samples = 1;
659 bytestream2_skip(gb, 4); // channel size
660 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
661 bytestream2_get_le32(gb) :
662 bytestream2_get_be32(gb);
663 buf_size -= 8 + 36 * ch;
665 nb_samples = buf_size / 8 * 14;
666 if (buf_size % 8 > 1)
667 nb_samples += (buf_size % 8 - 1) * 2;
668 *approx_nb_samples = 1;
670 case AV_CODEC_ID_ADPCM_AFC:
671 nb_samples = buf_size / (9 * ch) * 16;
673 case AV_CODEC_ID_ADPCM_XA:
674 nb_samples = (buf_size / 128) * 224 / ch;
676 case AV_CODEC_ID_ADPCM_DTK:
677 case AV_CODEC_ID_ADPCM_PSX:
678 nb_samples = buf_size / (16 * ch) * 28;
682 /* validate coded sample count */
683 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
684 return AVERROR_INVALIDDATA;
689 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
690 int *got_frame_ptr, AVPacket *avpkt)
692 AVFrame *frame = data;
693 const uint8_t *buf = avpkt->data;
694 int buf_size = avpkt->size;
695 ADPCMDecodeContext *c = avctx->priv_data;
696 ADPCMChannelStatus *cs;
697 int n, m, channel, i;
702 int nb_samples, coded_samples, approx_nb_samples, ret;
705 bytestream2_init(&gb, buf, buf_size);
706 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
707 if (nb_samples <= 0) {
708 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
709 return AVERROR_INVALIDDATA;
712 /* get output buffer */
713 frame->nb_samples = nb_samples;
714 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
716 samples = (int16_t *)frame->data[0];
717 samples_p = (int16_t **)frame->extended_data;
719 /* use coded_samples when applicable */
720 /* it is always <= nb_samples, so the output buffer will be large enough */
722 if (!approx_nb_samples && coded_samples != nb_samples)
723 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
724 frame->nb_samples = nb_samples = coded_samples;
727 st = avctx->channels == 2 ? 1 : 0;
729 switch(avctx->codec->id) {
730 case AV_CODEC_ID_ADPCM_IMA_QT:
731 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
732 Channel data is interleaved per-chunk. */
733 for (channel = 0; channel < avctx->channels; channel++) {
736 cs = &(c->status[channel]);
737 /* (pppppp) (piiiiiii) */
739 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
740 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
741 step_index = predictor & 0x7F;
744 if (cs->step_index == step_index) {
745 int diff = predictor - cs->predictor;
752 cs->step_index = step_index;
753 cs->predictor = predictor;
756 if (cs->step_index > 88u){
757 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
758 channel, cs->step_index);
759 return AVERROR_INVALIDDATA;
762 samples = samples_p[channel];
764 for (m = 0; m < 64; m += 2) {
765 int byte = bytestream2_get_byteu(&gb);
766 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
767 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
771 case AV_CODEC_ID_ADPCM_IMA_WAV:
772 for(i=0; i<avctx->channels; i++){
773 cs = &(c->status[i]);
774 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
776 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
777 if (cs->step_index > 88u){
778 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
780 return AVERROR_INVALIDDATA;
784 if (avctx->bits_per_coded_sample != 4) {
785 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
786 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
787 uint8_t temp[20] = { 0 };
790 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
791 for (i = 0; i < avctx->channels; i++) {
795 samples = &samples_p[i][1 + n * samples_per_block];
796 for (j = 0; j < block_size; j++) {
797 temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
798 (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
800 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
803 for (m = 0; m < samples_per_block; m++) {
804 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
805 avctx->bits_per_coded_sample);
809 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
811 for (n = 0; n < (nb_samples - 1) / 8; n++) {
812 for (i = 0; i < avctx->channels; i++) {
814 samples = &samples_p[i][1 + n * 8];
815 for (m = 0; m < 8; m += 2) {
816 int v = bytestream2_get_byteu(&gb);
817 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
818 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
824 case AV_CODEC_ID_ADPCM_4XM:
825 for (i = 0; i < avctx->channels; i++)
826 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
828 for (i = 0; i < avctx->channels; i++) {
829 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
830 if (c->status[i].step_index > 88u) {
831 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
832 i, c->status[i].step_index);
833 return AVERROR_INVALIDDATA;
837 for (i = 0; i < avctx->channels; i++) {
838 samples = (int16_t *)frame->data[i];
840 for (n = nb_samples >> 1; n > 0; n--) {
841 int v = bytestream2_get_byteu(&gb);
842 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
843 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
847 case AV_CODEC_ID_ADPCM_MS:
851 block_predictor = bytestream2_get_byteu(&gb);
852 if (block_predictor > 6) {
853 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
855 return AVERROR_INVALIDDATA;
857 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
858 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
860 block_predictor = bytestream2_get_byteu(&gb);
861 if (block_predictor > 6) {
862 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
864 return AVERROR_INVALIDDATA;
866 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
867 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
869 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
871 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
874 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
875 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
876 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
877 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
879 *samples++ = c->status[0].sample2;
880 if (st) *samples++ = c->status[1].sample2;
881 *samples++ = c->status[0].sample1;
882 if (st) *samples++ = c->status[1].sample1;
883 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
884 int byte = bytestream2_get_byteu(&gb);
885 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
886 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
890 case AV_CODEC_ID_ADPCM_IMA_DK4:
891 for (channel = 0; channel < avctx->channels; channel++) {
892 cs = &c->status[channel];
893 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
894 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
895 if (cs->step_index > 88u){
896 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
897 channel, cs->step_index);
898 return AVERROR_INVALIDDATA;
901 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
902 int v = bytestream2_get_byteu(&gb);
903 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
904 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
907 case AV_CODEC_ID_ADPCM_IMA_DK3:
911 int decode_top_nibble_next = 0;
913 const int16_t *samples_end = samples + avctx->channels * nb_samples;
915 bytestream2_skipu(&gb, 10);
916 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
917 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
918 c->status[0].step_index = bytestream2_get_byteu(&gb);
919 c->status[1].step_index = bytestream2_get_byteu(&gb);
920 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
921 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
922 c->status[0].step_index, c->status[1].step_index);
923 return AVERROR_INVALIDDATA;
925 /* sign extend the predictors */
926 diff_channel = c->status[1].predictor;
928 /* DK3 ADPCM support macro */
929 #define DK3_GET_NEXT_NIBBLE() \
930 if (decode_top_nibble_next) { \
931 nibble = last_byte >> 4; \
932 decode_top_nibble_next = 0; \
934 last_byte = bytestream2_get_byteu(&gb); \
935 nibble = last_byte & 0x0F; \
936 decode_top_nibble_next = 1; \
939 while (samples < samples_end) {
941 /* for this algorithm, c->status[0] is the sum channel and
942 * c->status[1] is the diff channel */
944 /* process the first predictor of the sum channel */
945 DK3_GET_NEXT_NIBBLE();
946 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
948 /* process the diff channel predictor */
949 DK3_GET_NEXT_NIBBLE();
950 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
952 /* process the first pair of stereo PCM samples */
953 diff_channel = (diff_channel + c->status[1].predictor) / 2;
954 *samples++ = c->status[0].predictor + c->status[1].predictor;
955 *samples++ = c->status[0].predictor - c->status[1].predictor;
957 /* process the second predictor of the sum channel */
958 DK3_GET_NEXT_NIBBLE();
959 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
961 /* process the second pair of stereo PCM samples */
962 diff_channel = (diff_channel + c->status[1].predictor) / 2;
963 *samples++ = c->status[0].predictor + c->status[1].predictor;
964 *samples++ = c->status[0].predictor - c->status[1].predictor;
967 if ((bytestream2_tell(&gb) & 1))
968 bytestream2_skip(&gb, 1);
971 case AV_CODEC_ID_ADPCM_IMA_ISS:
972 for (channel = 0; channel < avctx->channels; channel++) {
973 cs = &c->status[channel];
974 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
975 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
976 if (cs->step_index > 88u){
977 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
978 channel, cs->step_index);
979 return AVERROR_INVALIDDATA;
983 for (n = nb_samples >> (1 - st); n > 0; n--) {
985 int v = bytestream2_get_byteu(&gb);
986 /* nibbles are swapped for mono */
994 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
995 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
998 case AV_CODEC_ID_ADPCM_IMA_DAT4:
999 for (channel = 0; channel < avctx->channels; channel++) {
1000 cs = &c->status[channel];
1001 samples = samples_p[channel];
1002 bytestream2_skip(&gb, 4);
1003 for (n = 0; n < nb_samples; n += 2) {
1004 int v = bytestream2_get_byteu(&gb);
1005 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1006 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1010 case AV_CODEC_ID_ADPCM_IMA_APC:
1011 while (bytestream2_get_bytes_left(&gb) > 0) {
1012 int v = bytestream2_get_byteu(&gb);
1013 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1014 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1017 case AV_CODEC_ID_ADPCM_IMA_OKI:
1018 while (bytestream2_get_bytes_left(&gb) > 0) {
1019 int v = bytestream2_get_byteu(&gb);
1020 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1021 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1024 case AV_CODEC_ID_ADPCM_IMA_RAD:
1025 for (channel = 0; channel < avctx->channels; channel++) {
1026 cs = &c->status[channel];
1027 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1028 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1029 if (cs->step_index > 88u){
1030 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1031 channel, cs->step_index);
1032 return AVERROR_INVALIDDATA;
1035 for (n = 0; n < nb_samples / 2; n++) {
1038 byte[0] = bytestream2_get_byteu(&gb);
1040 byte[1] = bytestream2_get_byteu(&gb);
1041 for(channel = 0; channel < avctx->channels; channel++) {
1042 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1044 for(channel = 0; channel < avctx->channels; channel++) {
1045 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1049 case AV_CODEC_ID_ADPCM_IMA_WS:
1050 if (c->vqa_version == 3) {
1051 for (channel = 0; channel < avctx->channels; channel++) {
1052 int16_t *smp = samples_p[channel];
1054 for (n = nb_samples / 2; n > 0; n--) {
1055 int v = bytestream2_get_byteu(&gb);
1056 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1057 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1061 for (n = nb_samples / 2; n > 0; n--) {
1062 for (channel = 0; channel < avctx->channels; channel++) {
1063 int v = bytestream2_get_byteu(&gb);
1064 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1065 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1067 samples += avctx->channels;
1070 bytestream2_seek(&gb, 0, SEEK_END);
1072 case AV_CODEC_ID_ADPCM_XA:
1074 int16_t *out0 = samples_p[0];
1075 int16_t *out1 = samples_p[1];
1076 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1077 int sample_offset = 0;
1078 while (bytestream2_get_bytes_left(&gb) >= 128) {
1079 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1080 &c->status[0], &c->status[1],
1081 avctx->channels, sample_offset)) < 0)
1083 bytestream2_skipu(&gb, 128);
1084 sample_offset += samples_per_block;
1088 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1089 for (i=0; i<=st; i++) {
1090 c->status[i].step_index = bytestream2_get_le32u(&gb);
1091 if (c->status[i].step_index > 88u) {
1092 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1093 i, c->status[i].step_index);
1094 return AVERROR_INVALIDDATA;
1097 for (i=0; i<=st; i++)
1098 c->status[i].predictor = bytestream2_get_le32u(&gb);
1100 for (n = nb_samples >> (1 - st); n > 0; n--) {
1101 int byte = bytestream2_get_byteu(&gb);
1102 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1103 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1106 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1107 for (n = nb_samples >> (1 - st); n > 0; n--) {
1108 int byte = bytestream2_get_byteu(&gb);
1109 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1110 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1113 case AV_CODEC_ID_ADPCM_EA:
1115 int previous_left_sample, previous_right_sample;
1116 int current_left_sample, current_right_sample;
1117 int next_left_sample, next_right_sample;
1118 int coeff1l, coeff2l, coeff1r, coeff2r;
1119 int shift_left, shift_right;
1121 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1122 each coding 28 stereo samples. */
1124 if(avctx->channels != 2)
1125 return AVERROR_INVALIDDATA;
1127 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1128 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1129 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1130 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1132 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1133 int byte = bytestream2_get_byteu(&gb);
1134 coeff1l = ea_adpcm_table[ byte >> 4 ];
1135 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1136 coeff1r = ea_adpcm_table[ byte & 0x0F];
1137 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1139 byte = bytestream2_get_byteu(&gb);
1140 shift_left = 20 - (byte >> 4);
1141 shift_right = 20 - (byte & 0x0F);
1143 for (count2 = 0; count2 < 28; count2++) {
1144 byte = bytestream2_get_byteu(&gb);
1145 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1146 next_right_sample = sign_extend(byte, 4) << shift_right;
1148 next_left_sample = (next_left_sample +
1149 (current_left_sample * coeff1l) +
1150 (previous_left_sample * coeff2l) + 0x80) >> 8;
1151 next_right_sample = (next_right_sample +
1152 (current_right_sample * coeff1r) +
1153 (previous_right_sample * coeff2r) + 0x80) >> 8;
1155 previous_left_sample = current_left_sample;
1156 current_left_sample = av_clip_int16(next_left_sample);
1157 previous_right_sample = current_right_sample;
1158 current_right_sample = av_clip_int16(next_right_sample);
1159 *samples++ = current_left_sample;
1160 *samples++ = current_right_sample;
1164 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1168 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1170 int coeff[2][2], shift[2];
1172 for(channel = 0; channel < avctx->channels; channel++) {
1173 int byte = bytestream2_get_byteu(&gb);
1175 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1176 shift[channel] = 20 - (byte & 0x0F);
1178 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1181 byte[0] = bytestream2_get_byteu(&gb);
1182 if (st) byte[1] = bytestream2_get_byteu(&gb);
1183 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1184 for(channel = 0; channel < avctx->channels; channel++) {
1185 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1187 c->status[channel].sample1 * coeff[channel][0] +
1188 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1189 c->status[channel].sample2 = c->status[channel].sample1;
1190 c->status[channel].sample1 = av_clip_int16(sample);
1191 *samples++ = c->status[channel].sample1;
1195 bytestream2_seek(&gb, 0, SEEK_END);
1198 case AV_CODEC_ID_ADPCM_EA_R1:
1199 case AV_CODEC_ID_ADPCM_EA_R2:
1200 case AV_CODEC_ID_ADPCM_EA_R3: {
1201 /* channel numbering
1203 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1204 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1205 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1206 int previous_sample, current_sample, next_sample;
1209 unsigned int channel;
1214 for (channel=0; channel<avctx->channels; channel++)
1215 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1216 bytestream2_get_le32(&gb)) +
1217 (avctx->channels + 1) * 4;
1219 for (channel=0; channel<avctx->channels; channel++) {
1220 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1221 samplesC = samples_p[channel];
1223 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1224 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1225 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1227 current_sample = c->status[channel].predictor;
1228 previous_sample = c->status[channel].prev_sample;
1231 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1232 int byte = bytestream2_get_byte(&gb);
1233 if (byte == 0xEE) { /* only seen in R2 and R3 */
1234 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1235 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1237 for (count2=0; count2<28; count2++)
1238 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1240 coeff1 = ea_adpcm_table[ byte >> 4 ];
1241 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1242 shift = 20 - (byte & 0x0F);
1244 for (count2=0; count2<28; count2++) {
1246 next_sample = sign_extend(byte, 4) << shift;
1248 byte = bytestream2_get_byte(&gb);
1249 next_sample = sign_extend(byte >> 4, 4) << shift;
1252 next_sample += (current_sample * coeff1) +
1253 (previous_sample * coeff2);
1254 next_sample = av_clip_int16(next_sample >> 8);
1256 previous_sample = current_sample;
1257 current_sample = next_sample;
1258 *samplesC++ = current_sample;
1264 } else if (count != count1) {
1265 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1266 count = FFMAX(count, count1);
1269 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1270 c->status[channel].predictor = current_sample;
1271 c->status[channel].prev_sample = previous_sample;
1275 frame->nb_samples = count * 28;
1276 bytestream2_seek(&gb, 0, SEEK_END);
1279 case AV_CODEC_ID_ADPCM_EA_XAS:
1280 for (channel=0; channel<avctx->channels; channel++) {
1281 int coeff[2][4], shift[4];
1282 int16_t *s = samples_p[channel];
1283 for (n = 0; n < 4; n++, s += 32) {
1284 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1286 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1289 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1290 shift[n] = 20 - (val & 0x0F);
1294 for (m=2; m<32; m+=2) {
1295 s = &samples_p[channel][m];
1296 for (n = 0; n < 4; n++, s += 32) {
1298 int byte = bytestream2_get_byteu(&gb);
1300 level = sign_extend(byte >> 4, 4) << shift[n];
1301 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1302 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1304 level = sign_extend(byte, 4) << shift[n];
1305 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1306 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1311 case AV_CODEC_ID_ADPCM_IMA_AMV:
1312 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1313 c->status[0].step_index = bytestream2_get_le16u(&gb);
1314 bytestream2_skipu(&gb, 4);
1315 if (c->status[0].step_index > 88u) {
1316 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1317 c->status[0].step_index);
1318 return AVERROR_INVALIDDATA;
1321 for (n = nb_samples >> (1 - st); n > 0; n--) {
1322 int v = bytestream2_get_byteu(&gb);
1324 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1325 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1328 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1329 for (i = 0; i < avctx->channels; i++) {
1330 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1331 c->status[i].step_index = bytestream2_get_byteu(&gb);
1332 bytestream2_skipu(&gb, 1);
1333 if (c->status[i].step_index > 88u) {
1334 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1335 c->status[i].step_index);
1336 return AVERROR_INVALIDDATA;
1340 for (n = nb_samples >> (1 - st); n > 0; n--) {
1341 int v = bytestream2_get_byteu(&gb);
1343 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1344 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1347 case AV_CODEC_ID_ADPCM_CT:
1348 for (n = nb_samples >> (1 - st); n > 0; n--) {
1349 int v = bytestream2_get_byteu(&gb);
1350 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1351 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1354 case AV_CODEC_ID_ADPCM_SBPRO_4:
1355 case AV_CODEC_ID_ADPCM_SBPRO_3:
1356 case AV_CODEC_ID_ADPCM_SBPRO_2:
1357 if (!c->status[0].step_index) {
1358 /* the first byte is a raw sample */
1359 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1361 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1362 c->status[0].step_index = 1;
1365 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1366 for (n = nb_samples >> (1 - st); n > 0; n--) {
1367 int byte = bytestream2_get_byteu(&gb);
1368 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1370 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1373 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1374 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1375 int byte = bytestream2_get_byteu(&gb);
1376 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1378 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1379 (byte >> 2) & 0x07, 3, 0);
1380 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1384 for (n = nb_samples >> (2 - st); n > 0; n--) {
1385 int byte = bytestream2_get_byteu(&gb);
1386 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1388 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1389 (byte >> 4) & 0x03, 2, 2);
1390 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1391 (byte >> 2) & 0x03, 2, 2);
1392 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1397 case AV_CODEC_ID_ADPCM_SWF:
1398 adpcm_swf_decode(avctx, buf, buf_size, samples);
1399 bytestream2_seek(&gb, 0, SEEK_END);
1401 case AV_CODEC_ID_ADPCM_YAMAHA:
1402 for (n = nb_samples >> (1 - st); n > 0; n--) {
1403 int v = bytestream2_get_byteu(&gb);
1404 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1405 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1408 case AV_CODEC_ID_ADPCM_AICA:
1409 if (!c->has_status) {
1410 for (channel = 0; channel < avctx->channels; channel++)
1411 c->status[channel].step = 0;
1414 for (channel = 0; channel < avctx->channels; channel++) {
1415 samples = samples_p[channel];
1416 for (n = nb_samples >> 1; n > 0; n--) {
1417 int v = bytestream2_get_byteu(&gb);
1418 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1419 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1423 case AV_CODEC_ID_ADPCM_AFC:
1425 int samples_per_block;
1428 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1429 samples_per_block = avctx->extradata[0] / 16;
1430 blocks = nb_samples / avctx->extradata[0];
1432 samples_per_block = nb_samples / 16;
1436 for (m = 0; m < blocks; m++) {
1437 for (channel = 0; channel < avctx->channels; channel++) {
1438 int prev1 = c->status[channel].sample1;
1439 int prev2 = c->status[channel].sample2;
1441 samples = samples_p[channel] + m * 16;
1442 /* Read in every sample for this channel. */
1443 for (i = 0; i < samples_per_block; i++) {
1444 int byte = bytestream2_get_byteu(&gb);
1445 int scale = 1 << (byte >> 4);
1446 int index = byte & 0xf;
1447 int factor1 = ff_adpcm_afc_coeffs[0][index];
1448 int factor2 = ff_adpcm_afc_coeffs[1][index];
1450 /* Decode 16 samples. */
1451 for (n = 0; n < 16; n++) {
1455 sampledat = sign_extend(byte, 4);
1457 byte = bytestream2_get_byteu(&gb);
1458 sampledat = sign_extend(byte >> 4, 4);
1461 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1462 ((sampledat * scale) << 11)) >> 11;
1463 *samples = av_clip_int16(sampledat);
1469 c->status[channel].sample1 = prev1;
1470 c->status[channel].sample2 = prev2;
1473 bytestream2_seek(&gb, 0, SEEK_END);
1476 case AV_CODEC_ID_ADPCM_THP:
1477 case AV_CODEC_ID_ADPCM_THP_LE:
1482 #define THP_GET16(g) \
1484 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1485 bytestream2_get_le16u(&(g)) : \
1486 bytestream2_get_be16u(&(g)), 16)
1488 if (avctx->extradata) {
1490 if (avctx->extradata_size < 32 * avctx->channels) {
1491 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1492 return AVERROR_INVALIDDATA;
1495 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1496 for (i = 0; i < avctx->channels; i++)
1497 for (n = 0; n < 16; n++)
1498 table[i][n] = THP_GET16(tb);
1500 for (i = 0; i < avctx->channels; i++)
1501 for (n = 0; n < 16; n++)
1502 table[i][n] = THP_GET16(gb);
1504 if (!c->has_status) {
1505 /* Initialize the previous sample. */
1506 for (i = 0; i < avctx->channels; i++) {
1507 c->status[i].sample1 = THP_GET16(gb);
1508 c->status[i].sample2 = THP_GET16(gb);
1512 bytestream2_skip(&gb, avctx->channels * 4);
1516 for (ch = 0; ch < avctx->channels; ch++) {
1517 samples = samples_p[ch];
1519 /* Read in every sample for this channel. */
1520 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1521 int byte = bytestream2_get_byteu(&gb);
1522 int index = (byte >> 4) & 7;
1523 unsigned int exp = byte & 0x0F;
1524 int factor1 = table[ch][index * 2];
1525 int factor2 = table[ch][index * 2 + 1];
1527 /* Decode 14 samples. */
1528 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1532 sampledat = sign_extend(byte, 4);
1534 byte = bytestream2_get_byteu(&gb);
1535 sampledat = sign_extend(byte >> 4, 4);
1538 sampledat = ((c->status[ch].sample1 * factor1
1539 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1540 *samples = av_clip_int16(sampledat);
1541 c->status[ch].sample2 = c->status[ch].sample1;
1542 c->status[ch].sample1 = *samples++;
1548 case AV_CODEC_ID_ADPCM_DTK:
1549 for (channel = 0; channel < avctx->channels; channel++) {
1550 samples = samples_p[channel];
1552 /* Read in every sample for this channel. */
1553 for (i = 0; i < nb_samples / 28; i++) {
1556 bytestream2_skipu(&gb, 1);
1557 header = bytestream2_get_byteu(&gb);
1558 bytestream2_skipu(&gb, 3 - channel);
1560 /* Decode 28 samples. */
1561 for (n = 0; n < 28; n++) {
1562 int32_t sampledat, prev;
1564 switch (header >> 4) {
1566 prev = (c->status[channel].sample1 * 0x3c);
1569 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1572 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1578 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1580 byte = bytestream2_get_byteu(&gb);
1582 sampledat = sign_extend(byte, 4);
1584 sampledat = sign_extend(byte >> 4, 4);
1586 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1587 *samples++ = av_clip_int16(sampledat >> 6);
1588 c->status[channel].sample2 = c->status[channel].sample1;
1589 c->status[channel].sample1 = sampledat;
1593 bytestream2_seek(&gb, 0, SEEK_SET);
1596 case AV_CODEC_ID_ADPCM_PSX:
1597 for (channel = 0; channel < avctx->channels; channel++) {
1598 samples = samples_p[channel];
1600 /* Read in every sample for this channel. */
1601 for (i = 0; i < nb_samples / 28; i++) {
1602 int filter, shift, flag, byte;
1604 filter = bytestream2_get_byteu(&gb);
1605 shift = filter & 0xf;
1606 filter = filter >> 4;
1607 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1608 return AVERROR_INVALIDDATA;
1609 flag = bytestream2_get_byteu(&gb);
1611 /* Decode 28 samples. */
1612 for (n = 0; n < 28; n++) {
1613 int sample = 0, scale;
1617 scale = sign_extend(byte >> 4, 4);
1619 byte = bytestream2_get_byteu(&gb);
1620 scale = sign_extend(byte, 4);
1623 scale = scale << 12;
1624 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1626 *samples++ = av_clip_int16(sample);
1627 c->status[channel].sample2 = c->status[channel].sample1;
1628 c->status[channel].sample1 = sample;
1638 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1639 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1640 return AVERROR_INVALIDDATA;
1645 if (avpkt->size < bytestream2_tell(&gb)) {
1646 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1650 return bytestream2_tell(&gb);
1653 static void adpcm_flush(AVCodecContext *avctx)
1655 ADPCMDecodeContext *c = avctx->priv_data;
1660 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1661 AV_SAMPLE_FMT_NONE };
1662 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1663 AV_SAMPLE_FMT_NONE };
1664 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1666 AV_SAMPLE_FMT_NONE };
1668 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1669 AVCodec ff_ ## name_ ## _decoder = { \
1671 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1672 .type = AVMEDIA_TYPE_AUDIO, \
1674 .priv_data_size = sizeof(ADPCMDecodeContext), \
1675 .init = adpcm_decode_init, \
1676 .decode = adpcm_decode_frame, \
1677 .flush = adpcm_flush, \
1678 .capabilities = AV_CODEC_CAP_DR1, \
1679 .sample_fmts = sample_fmts_, \
1682 /* Note: Do not forget to add new entries to the Makefile as well. */
1683 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1684 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1685 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
1686 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1687 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1688 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1689 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1690 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1691 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1692 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1693 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1694 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1695 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1696 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
1697 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1698 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1699 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1700 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1701 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1702 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1703 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1704 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1705 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1706 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1707 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1708 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1709 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
1710 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1711 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1712 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1713 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1714 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1715 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1716 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1717 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");