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_MTAF:
114 case AV_CODEC_ID_ADPCM_PSX:
117 case AV_CODEC_ID_ADPCM_IMA_DAT4:
118 case AV_CODEC_ID_ADPCM_THP:
119 case AV_CODEC_ID_ADPCM_THP_LE:
123 if (avctx->channels < min_channels || avctx->channels > max_channels) {
124 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
125 return AVERROR(EINVAL);
128 switch(avctx->codec->id) {
129 case AV_CODEC_ID_ADPCM_CT:
130 c->status[0].step = c->status[1].step = 511;
132 case AV_CODEC_ID_ADPCM_IMA_WAV:
133 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
134 return AVERROR_INVALIDDATA;
136 case AV_CODEC_ID_ADPCM_IMA_APC:
137 if (avctx->extradata && avctx->extradata_size >= 8) {
138 c->status[0].predictor = AV_RL32(avctx->extradata);
139 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
142 case AV_CODEC_ID_ADPCM_IMA_WS:
143 if (avctx->extradata && avctx->extradata_size >= 2)
144 c->vqa_version = AV_RL16(avctx->extradata);
150 switch(avctx->codec->id) {
151 case AV_CODEC_ID_ADPCM_AICA:
152 case AV_CODEC_ID_ADPCM_IMA_DAT4:
153 case AV_CODEC_ID_ADPCM_IMA_QT:
154 case AV_CODEC_ID_ADPCM_IMA_WAV:
155 case AV_CODEC_ID_ADPCM_4XM:
156 case AV_CODEC_ID_ADPCM_XA:
157 case AV_CODEC_ID_ADPCM_EA_R1:
158 case AV_CODEC_ID_ADPCM_EA_R2:
159 case AV_CODEC_ID_ADPCM_EA_R3:
160 case AV_CODEC_ID_ADPCM_EA_XAS:
161 case AV_CODEC_ID_ADPCM_THP:
162 case AV_CODEC_ID_ADPCM_THP_LE:
163 case AV_CODEC_ID_ADPCM_AFC:
164 case AV_CODEC_ID_ADPCM_DTK:
165 case AV_CODEC_ID_ADPCM_PSX:
166 case AV_CODEC_ID_ADPCM_MTAF:
167 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
169 case AV_CODEC_ID_ADPCM_IMA_WS:
170 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
174 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
180 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
182 int delta, pred, step, add;
187 add = (delta * 2 + 1) * step;
191 if ((nibble & 8) == 0)
192 pred = av_clip(pred + (add >> 3), -32767, 32767);
194 pred = av_clip(pred - (add >> 3), -32767, 32767);
201 c->step = av_clip(c->step * 2, 127, 24576);
219 c->step = av_clip(c->step, 127, 24576);
224 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
228 int sign, delta, diff, step;
230 step = ff_adpcm_step_table[c->step_index];
231 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
232 step_index = av_clip(step_index, 0, 88);
236 /* perform direct multiplication instead of series of jumps proposed by
237 * the reference ADPCM implementation since modern CPUs can do the mults
239 diff = ((2 * delta + 1) * step) >> shift;
240 predictor = c->predictor;
241 if (sign) predictor -= diff;
242 else predictor += diff;
244 c->predictor = av_clip_int16(predictor);
245 c->step_index = step_index;
247 return (int16_t)c->predictor;
250 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
252 int nibble, step_index, predictor, sign, delta, diff, step, shift;
255 nibble = get_bits_le(gb, bps),
256 step = ff_adpcm_step_table[c->step_index];
257 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
258 step_index = av_clip(step_index, 0, 88);
260 sign = nibble & (1 << shift);
261 delta = av_mod_uintp2(nibble, shift);
262 diff = ((2 * delta + 1) * step) >> shift;
263 predictor = c->predictor;
264 if (sign) predictor -= diff;
265 else predictor += diff;
267 c->predictor = av_clip_int16(predictor);
268 c->step_index = step_index;
270 return (int16_t)c->predictor;
273 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
279 step = ff_adpcm_step_table[c->step_index];
280 step_index = c->step_index + ff_adpcm_index_table[nibble];
281 step_index = av_clip(step_index, 0, 88);
284 if (nibble & 4) diff += step;
285 if (nibble & 2) diff += step >> 1;
286 if (nibble & 1) diff += step >> 2;
289 predictor = c->predictor - diff;
291 predictor = c->predictor + diff;
293 c->predictor = av_clip_int16(predictor);
294 c->step_index = step_index;
299 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
303 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
304 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
306 c->sample2 = c->sample1;
307 c->sample1 = av_clip_int16(predictor);
308 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
309 if (c->idelta < 16) c->idelta = 16;
310 if (c->idelta > INT_MAX/768) {
311 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
312 c->idelta = INT_MAX/768;
318 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
320 int step_index, predictor, sign, delta, diff, step;
322 step = ff_adpcm_oki_step_table[c->step_index];
323 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
324 step_index = av_clip(step_index, 0, 48);
328 diff = ((2 * delta + 1) * step) >> 3;
329 predictor = c->predictor;
330 if (sign) predictor -= diff;
331 else predictor += diff;
333 c->predictor = av_clip_intp2(predictor, 11);
334 c->step_index = step_index;
336 return c->predictor << 4;
339 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
341 int sign, delta, diff;
346 /* perform direct multiplication instead of series of jumps proposed by
347 * the reference ADPCM implementation since modern CPUs can do the mults
349 diff = ((2 * delta + 1) * c->step) >> 3;
350 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
351 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
352 c->predictor = av_clip_int16(c->predictor);
353 /* calculate new step and clamp it to range 511..32767 */
354 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
355 c->step = av_clip(new_step, 511, 32767);
357 return (int16_t)c->predictor;
360 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
362 int sign, delta, diff;
364 sign = nibble & (1<<(size-1));
365 delta = nibble & ((1<<(size-1))-1);
366 diff = delta << (7 + c->step + shift);
369 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
371 /* calculate new step */
372 if (delta >= (2*size - 3) && c->step < 3)
374 else if (delta == 0 && c->step > 0)
377 return (int16_t) c->predictor;
380 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
387 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
388 c->predictor = av_clip_int16(c->predictor);
389 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
390 c->step = av_clip(c->step, 127, 24576);
394 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
396 c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
397 c->predictor = av_clip_int16(c->predictor);
398 c->step += ff_adpcm_index_table[nibble];
399 c->step = av_clip_uintp2(c->step, 5);
403 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
404 const uint8_t *in, ADPCMChannelStatus *left,
405 ADPCMChannelStatus *right, int channels, int sample_offset)
408 int shift,filter,f0,f1;
412 out0 += sample_offset;
416 out1 += sample_offset;
419 shift = 12 - (in[4+i*2] & 15);
420 filter = in[4+i*2] >> 4;
421 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
422 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
425 f0 = xa_adpcm_table[filter][0];
426 f1 = xa_adpcm_table[filter][1];
434 t = sign_extend(d, 4);
435 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
437 s_1 = av_clip_int16(s);
444 s_1 = right->sample1;
445 s_2 = right->sample2;
448 shift = 12 - (in[5+i*2] & 15);
449 filter = in[5+i*2] >> 4;
450 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
451 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
455 f0 = xa_adpcm_table[filter][0];
456 f1 = xa_adpcm_table[filter][1];
461 t = sign_extend(d >> 4, 4);
462 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
464 s_1 = av_clip_int16(s);
469 right->sample1 = s_1;
470 right->sample2 = s_2;
476 out0 += 28 * (3 - channels);
477 out1 += 28 * (3 - channels);
483 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
485 ADPCMDecodeContext *c = avctx->priv_data;
488 int k0, signmask, nb_bits, count;
489 int size = buf_size*8;
492 init_get_bits(&gb, buf, size);
494 //read bits & initial values
495 nb_bits = get_bits(&gb, 2)+2;
496 table = swf_index_tables[nb_bits-2];
497 k0 = 1 << (nb_bits-2);
498 signmask = 1 << (nb_bits-1);
500 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
501 for (i = 0; i < avctx->channels; i++) {
502 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
503 c->status[i].step_index = get_bits(&gb, 6);
506 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
509 for (i = 0; i < avctx->channels; i++) {
510 // similar to IMA adpcm
511 int delta = get_bits(&gb, nb_bits);
512 int step = ff_adpcm_step_table[c->status[i].step_index];
513 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
524 if (delta & signmask)
525 c->status[i].predictor -= vpdiff;
527 c->status[i].predictor += vpdiff;
529 c->status[i].step_index += table[delta & (~signmask)];
531 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
532 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
534 *samples++ = c->status[i].predictor;
541 * Get the number of samples that will be decoded from the packet.
542 * In one case, this is actually the maximum number of samples possible to
543 * decode with the given buf_size.
545 * @param[out] coded_samples set to the number of samples as coded in the
546 * packet, or 0 if the codec does not encode the
547 * number of samples in each frame.
548 * @param[out] approx_nb_samples set to non-zero if the number of samples
549 * returned is an approximation.
551 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
552 int buf_size, int *coded_samples, int *approx_nb_samples)
554 ADPCMDecodeContext *s = avctx->priv_data;
556 int ch = avctx->channels;
557 int has_coded_samples = 0;
561 *approx_nb_samples = 0;
566 switch (avctx->codec->id) {
567 /* constant, only check buf_size */
568 case AV_CODEC_ID_ADPCM_EA_XAS:
569 if (buf_size < 76 * ch)
573 case AV_CODEC_ID_ADPCM_IMA_QT:
574 if (buf_size < 34 * ch)
578 /* simple 4-bit adpcm */
579 case AV_CODEC_ID_ADPCM_CT:
580 case AV_CODEC_ID_ADPCM_IMA_APC:
581 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
582 case AV_CODEC_ID_ADPCM_IMA_OKI:
583 case AV_CODEC_ID_ADPCM_IMA_WS:
584 case AV_CODEC_ID_ADPCM_YAMAHA:
585 case AV_CODEC_ID_ADPCM_AICA:
586 nb_samples = buf_size * 2 / ch;
592 /* simple 4-bit adpcm, with header */
594 switch (avctx->codec->id) {
595 case AV_CODEC_ID_ADPCM_4XM:
596 case AV_CODEC_ID_ADPCM_AGM:
597 case AV_CODEC_ID_ADPCM_IMA_DAT4:
598 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
599 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
600 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
603 return (buf_size - header_size) * 2 / ch;
605 /* more complex formats */
606 switch (avctx->codec->id) {
607 case AV_CODEC_ID_ADPCM_EA:
608 has_coded_samples = 1;
609 *coded_samples = bytestream2_get_le32(gb);
610 *coded_samples -= *coded_samples % 28;
611 nb_samples = (buf_size - 12) / 30 * 28;
613 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
614 has_coded_samples = 1;
615 *coded_samples = bytestream2_get_le32(gb);
616 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
618 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
619 nb_samples = (buf_size - ch) / ch * 2;
621 case AV_CODEC_ID_ADPCM_EA_R1:
622 case AV_CODEC_ID_ADPCM_EA_R2:
623 case AV_CODEC_ID_ADPCM_EA_R3:
624 /* maximum number of samples */
625 /* has internal offsets and a per-frame switch to signal raw 16-bit */
626 has_coded_samples = 1;
627 switch (avctx->codec->id) {
628 case AV_CODEC_ID_ADPCM_EA_R1:
629 header_size = 4 + 9 * ch;
630 *coded_samples = bytestream2_get_le32(gb);
632 case AV_CODEC_ID_ADPCM_EA_R2:
633 header_size = 4 + 5 * ch;
634 *coded_samples = bytestream2_get_le32(gb);
636 case AV_CODEC_ID_ADPCM_EA_R3:
637 header_size = 4 + 5 * ch;
638 *coded_samples = bytestream2_get_be32(gb);
641 *coded_samples -= *coded_samples % 28;
642 nb_samples = (buf_size - header_size) * 2 / ch;
643 nb_samples -= nb_samples % 28;
644 *approx_nb_samples = 1;
646 case AV_CODEC_ID_ADPCM_IMA_DK3:
647 if (avctx->block_align > 0)
648 buf_size = FFMIN(buf_size, avctx->block_align);
649 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
651 case AV_CODEC_ID_ADPCM_IMA_DK4:
652 if (avctx->block_align > 0)
653 buf_size = FFMIN(buf_size, avctx->block_align);
654 if (buf_size < 4 * ch)
655 return AVERROR_INVALIDDATA;
656 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
658 case AV_CODEC_ID_ADPCM_IMA_RAD:
659 if (avctx->block_align > 0)
660 buf_size = FFMIN(buf_size, avctx->block_align);
661 nb_samples = (buf_size - 4 * ch) * 2 / ch;
663 case AV_CODEC_ID_ADPCM_IMA_WAV:
665 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
666 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
667 if (avctx->block_align > 0)
668 buf_size = FFMIN(buf_size, avctx->block_align);
669 if (buf_size < 4 * ch)
670 return AVERROR_INVALIDDATA;
671 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
674 case AV_CODEC_ID_ADPCM_MS:
675 if (avctx->block_align > 0)
676 buf_size = FFMIN(buf_size, avctx->block_align);
677 nb_samples = (buf_size - 6 * ch) * 2 / ch;
679 case AV_CODEC_ID_ADPCM_MTAF:
680 if (avctx->block_align > 0)
681 buf_size = FFMIN(buf_size, avctx->block_align);
682 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
684 case AV_CODEC_ID_ADPCM_SBPRO_2:
685 case AV_CODEC_ID_ADPCM_SBPRO_3:
686 case AV_CODEC_ID_ADPCM_SBPRO_4:
688 int samples_per_byte;
689 switch (avctx->codec->id) {
690 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
691 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
692 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
694 if (!s->status[0].step_index) {
696 return AVERROR_INVALIDDATA;
700 nb_samples += buf_size * samples_per_byte / ch;
703 case AV_CODEC_ID_ADPCM_SWF:
705 int buf_bits = buf_size * 8 - 2;
706 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
707 int block_hdr_size = 22 * ch;
708 int block_size = block_hdr_size + nbits * ch * 4095;
709 int nblocks = buf_bits / block_size;
710 int bits_left = buf_bits - nblocks * block_size;
711 nb_samples = nblocks * 4096;
712 if (bits_left >= block_hdr_size)
713 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
716 case AV_CODEC_ID_ADPCM_THP:
717 case AV_CODEC_ID_ADPCM_THP_LE:
718 if (avctx->extradata) {
719 nb_samples = buf_size * 14 / (8 * ch);
722 has_coded_samples = 1;
723 bytestream2_skip(gb, 4); // channel size
724 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
725 bytestream2_get_le32(gb) :
726 bytestream2_get_be32(gb);
727 buf_size -= 8 + 36 * ch;
729 nb_samples = buf_size / 8 * 14;
730 if (buf_size % 8 > 1)
731 nb_samples += (buf_size % 8 - 1) * 2;
732 *approx_nb_samples = 1;
734 case AV_CODEC_ID_ADPCM_AFC:
735 nb_samples = buf_size / (9 * ch) * 16;
737 case AV_CODEC_ID_ADPCM_XA:
738 nb_samples = (buf_size / 128) * 224 / ch;
740 case AV_CODEC_ID_ADPCM_DTK:
741 case AV_CODEC_ID_ADPCM_PSX:
742 nb_samples = buf_size / (16 * ch) * 28;
746 /* validate coded sample count */
747 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
748 return AVERROR_INVALIDDATA;
753 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
754 int *got_frame_ptr, AVPacket *avpkt)
756 AVFrame *frame = data;
757 const uint8_t *buf = avpkt->data;
758 int buf_size = avpkt->size;
759 ADPCMDecodeContext *c = avctx->priv_data;
760 ADPCMChannelStatus *cs;
761 int n, m, channel, i;
766 int nb_samples, coded_samples, approx_nb_samples, ret;
769 bytestream2_init(&gb, buf, buf_size);
770 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
771 if (nb_samples <= 0) {
772 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
773 return AVERROR_INVALIDDATA;
776 /* get output buffer */
777 frame->nb_samples = nb_samples;
778 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
780 samples = (int16_t *)frame->data[0];
781 samples_p = (int16_t **)frame->extended_data;
783 /* use coded_samples when applicable */
784 /* it is always <= nb_samples, so the output buffer will be large enough */
786 if (!approx_nb_samples && coded_samples != nb_samples)
787 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
788 frame->nb_samples = nb_samples = coded_samples;
791 st = avctx->channels == 2 ? 1 : 0;
793 switch(avctx->codec->id) {
794 case AV_CODEC_ID_ADPCM_IMA_QT:
795 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
796 Channel data is interleaved per-chunk. */
797 for (channel = 0; channel < avctx->channels; channel++) {
800 cs = &(c->status[channel]);
801 /* (pppppp) (piiiiiii) */
803 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
804 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
805 step_index = predictor & 0x7F;
808 if (cs->step_index == step_index) {
809 int diff = predictor - cs->predictor;
816 cs->step_index = step_index;
817 cs->predictor = predictor;
820 if (cs->step_index > 88u){
821 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
822 channel, cs->step_index);
823 return AVERROR_INVALIDDATA;
826 samples = samples_p[channel];
828 for (m = 0; m < 64; m += 2) {
829 int byte = bytestream2_get_byteu(&gb);
830 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
831 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
835 case AV_CODEC_ID_ADPCM_IMA_WAV:
836 for(i=0; i<avctx->channels; i++){
837 cs = &(c->status[i]);
838 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
840 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
841 if (cs->step_index > 88u){
842 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
844 return AVERROR_INVALIDDATA;
848 if (avctx->bits_per_coded_sample != 4) {
849 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
850 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
851 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
854 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
855 for (i = 0; i < avctx->channels; i++) {
859 samples = &samples_p[i][1 + n * samples_per_block];
860 for (j = 0; j < block_size; j++) {
861 temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
862 (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
864 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
867 for (m = 0; m < samples_per_block; m++) {
868 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
869 avctx->bits_per_coded_sample);
873 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
875 for (n = 0; n < (nb_samples - 1) / 8; n++) {
876 for (i = 0; i < avctx->channels; i++) {
878 samples = &samples_p[i][1 + n * 8];
879 for (m = 0; m < 8; m += 2) {
880 int v = bytestream2_get_byteu(&gb);
881 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
882 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
888 case AV_CODEC_ID_ADPCM_4XM:
889 for (i = 0; i < avctx->channels; i++)
890 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
892 for (i = 0; i < avctx->channels; i++) {
893 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
894 if (c->status[i].step_index > 88u) {
895 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
896 i, c->status[i].step_index);
897 return AVERROR_INVALIDDATA;
901 for (i = 0; i < avctx->channels; i++) {
902 samples = (int16_t *)frame->data[i];
904 for (n = nb_samples >> 1; n > 0; n--) {
905 int v = bytestream2_get_byteu(&gb);
906 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
907 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
911 case AV_CODEC_ID_ADPCM_AGM:
912 for (i = 0; i < avctx->channels; i++)
913 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
914 for (i = 0; i < avctx->channels; i++)
915 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
917 for (n = 0; n < nb_samples >> (1 - st); n++) {
918 int v = bytestream2_get_byteu(&gb);
919 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
920 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
923 case AV_CODEC_ID_ADPCM_MS:
927 block_predictor = bytestream2_get_byteu(&gb);
928 if (block_predictor > 6) {
929 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
931 return AVERROR_INVALIDDATA;
933 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
934 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
936 block_predictor = bytestream2_get_byteu(&gb);
937 if (block_predictor > 6) {
938 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
940 return AVERROR_INVALIDDATA;
942 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
943 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
945 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
947 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
950 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
951 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
952 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
953 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
955 *samples++ = c->status[0].sample2;
956 if (st) *samples++ = c->status[1].sample2;
957 *samples++ = c->status[0].sample1;
958 if (st) *samples++ = c->status[1].sample1;
959 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
960 int byte = bytestream2_get_byteu(&gb);
961 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
962 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
966 case AV_CODEC_ID_ADPCM_MTAF:
967 for (channel = 0; channel < avctx->channels; channel+=2) {
968 bytestream2_skipu(&gb, 4);
969 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
970 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
971 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
972 bytestream2_skipu(&gb, 2);
973 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
974 bytestream2_skipu(&gb, 2);
975 for (n = 0; n < nb_samples; n+=2) {
976 int v = bytestream2_get_byteu(&gb);
977 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
978 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
980 for (n = 0; n < nb_samples; n+=2) {
981 int v = bytestream2_get_byteu(&gb);
982 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
983 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
987 case AV_CODEC_ID_ADPCM_IMA_DK4:
988 for (channel = 0; channel < avctx->channels; channel++) {
989 cs = &c->status[channel];
990 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
991 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
992 if (cs->step_index > 88u){
993 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
994 channel, cs->step_index);
995 return AVERROR_INVALIDDATA;
998 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
999 int v = bytestream2_get_byteu(&gb);
1000 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1001 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1004 case AV_CODEC_ID_ADPCM_IMA_DK3:
1008 int decode_top_nibble_next = 0;
1010 const int16_t *samples_end = samples + avctx->channels * nb_samples;
1012 bytestream2_skipu(&gb, 10);
1013 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1014 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1015 c->status[0].step_index = bytestream2_get_byteu(&gb);
1016 c->status[1].step_index = bytestream2_get_byteu(&gb);
1017 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1018 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1019 c->status[0].step_index, c->status[1].step_index);
1020 return AVERROR_INVALIDDATA;
1022 /* sign extend the predictors */
1023 diff_channel = c->status[1].predictor;
1025 /* DK3 ADPCM support macro */
1026 #define DK3_GET_NEXT_NIBBLE() \
1027 if (decode_top_nibble_next) { \
1028 nibble = last_byte >> 4; \
1029 decode_top_nibble_next = 0; \
1031 last_byte = bytestream2_get_byteu(&gb); \
1032 nibble = last_byte & 0x0F; \
1033 decode_top_nibble_next = 1; \
1036 while (samples < samples_end) {
1038 /* for this algorithm, c->status[0] is the sum channel and
1039 * c->status[1] is the diff channel */
1041 /* process the first predictor of the sum channel */
1042 DK3_GET_NEXT_NIBBLE();
1043 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1045 /* process the diff channel predictor */
1046 DK3_GET_NEXT_NIBBLE();
1047 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1049 /* process the first pair of stereo PCM samples */
1050 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1051 *samples++ = c->status[0].predictor + c->status[1].predictor;
1052 *samples++ = c->status[0].predictor - c->status[1].predictor;
1054 /* process the second predictor of the sum channel */
1055 DK3_GET_NEXT_NIBBLE();
1056 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1058 /* process the second pair of stereo PCM samples */
1059 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1060 *samples++ = c->status[0].predictor + c->status[1].predictor;
1061 *samples++ = c->status[0].predictor - c->status[1].predictor;
1064 if ((bytestream2_tell(&gb) & 1))
1065 bytestream2_skip(&gb, 1);
1068 case AV_CODEC_ID_ADPCM_IMA_ISS:
1069 for (channel = 0; channel < avctx->channels; channel++) {
1070 cs = &c->status[channel];
1071 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1072 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1073 if (cs->step_index > 88u){
1074 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1075 channel, cs->step_index);
1076 return AVERROR_INVALIDDATA;
1080 for (n = nb_samples >> (1 - st); n > 0; n--) {
1082 int v = bytestream2_get_byteu(&gb);
1083 /* nibbles are swapped for mono */
1091 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1092 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1095 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1096 for (channel = 0; channel < avctx->channels; channel++) {
1097 cs = &c->status[channel];
1098 samples = samples_p[channel];
1099 bytestream2_skip(&gb, 4);
1100 for (n = 0; n < nb_samples; n += 2) {
1101 int v = bytestream2_get_byteu(&gb);
1102 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1103 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1107 case AV_CODEC_ID_ADPCM_IMA_APC:
1108 while (bytestream2_get_bytes_left(&gb) > 0) {
1109 int v = bytestream2_get_byteu(&gb);
1110 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1111 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1114 case AV_CODEC_ID_ADPCM_IMA_OKI:
1115 while (bytestream2_get_bytes_left(&gb) > 0) {
1116 int v = bytestream2_get_byteu(&gb);
1117 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1118 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1121 case AV_CODEC_ID_ADPCM_IMA_RAD:
1122 for (channel = 0; channel < avctx->channels; channel++) {
1123 cs = &c->status[channel];
1124 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1125 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1126 if (cs->step_index > 88u){
1127 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1128 channel, cs->step_index);
1129 return AVERROR_INVALIDDATA;
1132 for (n = 0; n < nb_samples / 2; n++) {
1135 byte[0] = bytestream2_get_byteu(&gb);
1137 byte[1] = bytestream2_get_byteu(&gb);
1138 for(channel = 0; channel < avctx->channels; channel++) {
1139 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1141 for(channel = 0; channel < avctx->channels; channel++) {
1142 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1146 case AV_CODEC_ID_ADPCM_IMA_WS:
1147 if (c->vqa_version == 3) {
1148 for (channel = 0; channel < avctx->channels; channel++) {
1149 int16_t *smp = samples_p[channel];
1151 for (n = nb_samples / 2; n > 0; n--) {
1152 int v = bytestream2_get_byteu(&gb);
1153 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1154 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1158 for (n = nb_samples / 2; n > 0; n--) {
1159 for (channel = 0; channel < avctx->channels; channel++) {
1160 int v = bytestream2_get_byteu(&gb);
1161 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1162 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1164 samples += avctx->channels;
1167 bytestream2_seek(&gb, 0, SEEK_END);
1169 case AV_CODEC_ID_ADPCM_XA:
1171 int16_t *out0 = samples_p[0];
1172 int16_t *out1 = samples_p[1];
1173 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1174 int sample_offset = 0;
1175 int bytes_remaining;
1176 while (bytestream2_get_bytes_left(&gb) >= 128) {
1177 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1178 &c->status[0], &c->status[1],
1179 avctx->channels, sample_offset)) < 0)
1181 bytestream2_skipu(&gb, 128);
1182 sample_offset += samples_per_block;
1184 /* Less than a full block of data left, e.g. when reading from
1185 * 2324 byte per sector XA; the remainder is padding */
1186 bytes_remaining = bytestream2_get_bytes_left(&gb);
1187 if (bytes_remaining > 0) {
1188 bytestream2_skip(&gb, bytes_remaining);
1192 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1193 for (i=0; i<=st; i++) {
1194 c->status[i].step_index = bytestream2_get_le32u(&gb);
1195 if (c->status[i].step_index > 88u) {
1196 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1197 i, c->status[i].step_index);
1198 return AVERROR_INVALIDDATA;
1201 for (i=0; i<=st; i++)
1202 c->status[i].predictor = bytestream2_get_le32u(&gb);
1204 for (n = nb_samples >> (1 - st); n > 0; n--) {
1205 int byte = bytestream2_get_byteu(&gb);
1206 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1207 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1210 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1211 for (n = nb_samples >> (1 - st); n > 0; n--) {
1212 int byte = bytestream2_get_byteu(&gb);
1213 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1214 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1217 case AV_CODEC_ID_ADPCM_EA:
1219 int previous_left_sample, previous_right_sample;
1220 int current_left_sample, current_right_sample;
1221 int next_left_sample, next_right_sample;
1222 int coeff1l, coeff2l, coeff1r, coeff2r;
1223 int shift_left, shift_right;
1225 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1226 each coding 28 stereo samples. */
1228 if(avctx->channels != 2)
1229 return AVERROR_INVALIDDATA;
1231 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1232 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1233 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1234 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1236 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1237 int byte = bytestream2_get_byteu(&gb);
1238 coeff1l = ea_adpcm_table[ byte >> 4 ];
1239 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1240 coeff1r = ea_adpcm_table[ byte & 0x0F];
1241 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1243 byte = bytestream2_get_byteu(&gb);
1244 shift_left = 20 - (byte >> 4);
1245 shift_right = 20 - (byte & 0x0F);
1247 for (count2 = 0; count2 < 28; count2++) {
1248 byte = bytestream2_get_byteu(&gb);
1249 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1250 next_right_sample = sign_extend(byte, 4) << shift_right;
1252 next_left_sample = (next_left_sample +
1253 (current_left_sample * coeff1l) +
1254 (previous_left_sample * coeff2l) + 0x80) >> 8;
1255 next_right_sample = (next_right_sample +
1256 (current_right_sample * coeff1r) +
1257 (previous_right_sample * coeff2r) + 0x80) >> 8;
1259 previous_left_sample = current_left_sample;
1260 current_left_sample = av_clip_int16(next_left_sample);
1261 previous_right_sample = current_right_sample;
1262 current_right_sample = av_clip_int16(next_right_sample);
1263 *samples++ = current_left_sample;
1264 *samples++ = current_right_sample;
1268 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1272 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1274 int coeff[2][2], shift[2];
1276 for(channel = 0; channel < avctx->channels; channel++) {
1277 int byte = bytestream2_get_byteu(&gb);
1279 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1280 shift[channel] = 20 - (byte & 0x0F);
1282 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1285 byte[0] = bytestream2_get_byteu(&gb);
1286 if (st) byte[1] = bytestream2_get_byteu(&gb);
1287 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1288 for(channel = 0; channel < avctx->channels; channel++) {
1289 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1291 c->status[channel].sample1 * coeff[channel][0] +
1292 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1293 c->status[channel].sample2 = c->status[channel].sample1;
1294 c->status[channel].sample1 = av_clip_int16(sample);
1295 *samples++ = c->status[channel].sample1;
1299 bytestream2_seek(&gb, 0, SEEK_END);
1302 case AV_CODEC_ID_ADPCM_EA_R1:
1303 case AV_CODEC_ID_ADPCM_EA_R2:
1304 case AV_CODEC_ID_ADPCM_EA_R3: {
1305 /* channel numbering
1307 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1308 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1309 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1310 int previous_sample, current_sample, next_sample;
1313 unsigned int channel;
1318 for (channel=0; channel<avctx->channels; channel++)
1319 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1320 bytestream2_get_le32(&gb)) +
1321 (avctx->channels + 1) * 4;
1323 for (channel=0; channel<avctx->channels; channel++) {
1324 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1325 samplesC = samples_p[channel];
1327 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1328 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1329 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1331 current_sample = c->status[channel].predictor;
1332 previous_sample = c->status[channel].prev_sample;
1335 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1336 int byte = bytestream2_get_byte(&gb);
1337 if (byte == 0xEE) { /* only seen in R2 and R3 */
1338 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1339 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1341 for (count2=0; count2<28; count2++)
1342 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1344 coeff1 = ea_adpcm_table[ byte >> 4 ];
1345 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1346 shift = 20 - (byte & 0x0F);
1348 for (count2=0; count2<28; count2++) {
1350 next_sample = sign_extend(byte, 4) << shift;
1352 byte = bytestream2_get_byte(&gb);
1353 next_sample = sign_extend(byte >> 4, 4) << shift;
1356 next_sample += (current_sample * coeff1) +
1357 (previous_sample * coeff2);
1358 next_sample = av_clip_int16(next_sample >> 8);
1360 previous_sample = current_sample;
1361 current_sample = next_sample;
1362 *samplesC++ = current_sample;
1368 } else if (count != count1) {
1369 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1370 count = FFMAX(count, count1);
1373 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1374 c->status[channel].predictor = current_sample;
1375 c->status[channel].prev_sample = previous_sample;
1379 frame->nb_samples = count * 28;
1380 bytestream2_seek(&gb, 0, SEEK_END);
1383 case AV_CODEC_ID_ADPCM_EA_XAS:
1384 for (channel=0; channel<avctx->channels; channel++) {
1385 int coeff[2][4], shift[4];
1386 int16_t *s = samples_p[channel];
1387 for (n = 0; n < 4; n++, s += 32) {
1388 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1390 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1393 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1394 shift[n] = 20 - (val & 0x0F);
1398 for (m=2; m<32; m+=2) {
1399 s = &samples_p[channel][m];
1400 for (n = 0; n < 4; n++, s += 32) {
1402 int byte = bytestream2_get_byteu(&gb);
1404 level = sign_extend(byte >> 4, 4) << shift[n];
1405 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1406 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1408 level = sign_extend(byte, 4) << shift[n];
1409 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1410 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1415 case AV_CODEC_ID_ADPCM_IMA_AMV:
1416 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1417 c->status[0].step_index = bytestream2_get_byteu(&gb);
1418 bytestream2_skipu(&gb, 5);
1419 if (c->status[0].step_index > 88u) {
1420 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1421 c->status[0].step_index);
1422 return AVERROR_INVALIDDATA;
1425 for (n = nb_samples >> (1 - st); n > 0; n--) {
1426 int v = bytestream2_get_byteu(&gb);
1428 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1429 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1432 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1433 for (i = 0; i < avctx->channels; i++) {
1434 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1435 c->status[i].step_index = bytestream2_get_byteu(&gb);
1436 bytestream2_skipu(&gb, 1);
1437 if (c->status[i].step_index > 88u) {
1438 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1439 c->status[i].step_index);
1440 return AVERROR_INVALIDDATA;
1444 for (n = nb_samples >> (1 - st); n > 0; n--) {
1445 int v = bytestream2_get_byteu(&gb);
1447 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1448 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1451 case AV_CODEC_ID_ADPCM_CT:
1452 for (n = nb_samples >> (1 - st); n > 0; n--) {
1453 int v = bytestream2_get_byteu(&gb);
1454 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1455 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1458 case AV_CODEC_ID_ADPCM_SBPRO_4:
1459 case AV_CODEC_ID_ADPCM_SBPRO_3:
1460 case AV_CODEC_ID_ADPCM_SBPRO_2:
1461 if (!c->status[0].step_index) {
1462 /* the first byte is a raw sample */
1463 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1465 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1466 c->status[0].step_index = 1;
1469 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1470 for (n = nb_samples >> (1 - st); n > 0; n--) {
1471 int byte = bytestream2_get_byteu(&gb);
1472 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1474 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1477 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1478 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1479 int byte = bytestream2_get_byteu(&gb);
1480 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1482 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1483 (byte >> 2) & 0x07, 3, 0);
1484 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1488 for (n = nb_samples >> (2 - st); n > 0; n--) {
1489 int byte = bytestream2_get_byteu(&gb);
1490 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1492 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1493 (byte >> 4) & 0x03, 2, 2);
1494 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1495 (byte >> 2) & 0x03, 2, 2);
1496 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1501 case AV_CODEC_ID_ADPCM_SWF:
1502 adpcm_swf_decode(avctx, buf, buf_size, samples);
1503 bytestream2_seek(&gb, 0, SEEK_END);
1505 case AV_CODEC_ID_ADPCM_YAMAHA:
1506 for (n = nb_samples >> (1 - st); n > 0; n--) {
1507 int v = bytestream2_get_byteu(&gb);
1508 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1509 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1512 case AV_CODEC_ID_ADPCM_AICA:
1513 if (!c->has_status) {
1514 for (channel = 0; channel < avctx->channels; channel++)
1515 c->status[channel].step = 0;
1518 for (channel = 0; channel < avctx->channels; channel++) {
1519 samples = samples_p[channel];
1520 for (n = nb_samples >> 1; n > 0; n--) {
1521 int v = bytestream2_get_byteu(&gb);
1522 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1523 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1527 case AV_CODEC_ID_ADPCM_AFC:
1529 int samples_per_block;
1532 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1533 samples_per_block = avctx->extradata[0] / 16;
1534 blocks = nb_samples / avctx->extradata[0];
1536 samples_per_block = nb_samples / 16;
1540 for (m = 0; m < blocks; m++) {
1541 for (channel = 0; channel < avctx->channels; channel++) {
1542 int prev1 = c->status[channel].sample1;
1543 int prev2 = c->status[channel].sample2;
1545 samples = samples_p[channel] + m * 16;
1546 /* Read in every sample for this channel. */
1547 for (i = 0; i < samples_per_block; i++) {
1548 int byte = bytestream2_get_byteu(&gb);
1549 int scale = 1 << (byte >> 4);
1550 int index = byte & 0xf;
1551 int factor1 = ff_adpcm_afc_coeffs[0][index];
1552 int factor2 = ff_adpcm_afc_coeffs[1][index];
1554 /* Decode 16 samples. */
1555 for (n = 0; n < 16; n++) {
1559 sampledat = sign_extend(byte, 4);
1561 byte = bytestream2_get_byteu(&gb);
1562 sampledat = sign_extend(byte >> 4, 4);
1565 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1566 ((sampledat * scale) << 11)) >> 11;
1567 *samples = av_clip_int16(sampledat);
1573 c->status[channel].sample1 = prev1;
1574 c->status[channel].sample2 = prev2;
1577 bytestream2_seek(&gb, 0, SEEK_END);
1580 case AV_CODEC_ID_ADPCM_THP:
1581 case AV_CODEC_ID_ADPCM_THP_LE:
1586 #define THP_GET16(g) \
1588 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1589 bytestream2_get_le16u(&(g)) : \
1590 bytestream2_get_be16u(&(g)), 16)
1592 if (avctx->extradata) {
1594 if (avctx->extradata_size < 32 * avctx->channels) {
1595 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1596 return AVERROR_INVALIDDATA;
1599 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1600 for (i = 0; i < avctx->channels; i++)
1601 for (n = 0; n < 16; n++)
1602 table[i][n] = THP_GET16(tb);
1604 for (i = 0; i < avctx->channels; i++)
1605 for (n = 0; n < 16; n++)
1606 table[i][n] = THP_GET16(gb);
1608 if (!c->has_status) {
1609 /* Initialize the previous sample. */
1610 for (i = 0; i < avctx->channels; i++) {
1611 c->status[i].sample1 = THP_GET16(gb);
1612 c->status[i].sample2 = THP_GET16(gb);
1616 bytestream2_skip(&gb, avctx->channels * 4);
1620 for (ch = 0; ch < avctx->channels; ch++) {
1621 samples = samples_p[ch];
1623 /* Read in every sample for this channel. */
1624 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1625 int byte = bytestream2_get_byteu(&gb);
1626 int index = (byte >> 4) & 7;
1627 unsigned int exp = byte & 0x0F;
1628 int factor1 = table[ch][index * 2];
1629 int factor2 = table[ch][index * 2 + 1];
1631 /* Decode 14 samples. */
1632 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1636 sampledat = sign_extend(byte, 4);
1638 byte = bytestream2_get_byteu(&gb);
1639 sampledat = sign_extend(byte >> 4, 4);
1642 sampledat = ((c->status[ch].sample1 * factor1
1643 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1644 *samples = av_clip_int16(sampledat);
1645 c->status[ch].sample2 = c->status[ch].sample1;
1646 c->status[ch].sample1 = *samples++;
1652 case AV_CODEC_ID_ADPCM_DTK:
1653 for (channel = 0; channel < avctx->channels; channel++) {
1654 samples = samples_p[channel];
1656 /* Read in every sample for this channel. */
1657 for (i = 0; i < nb_samples / 28; i++) {
1660 bytestream2_skipu(&gb, 1);
1661 header = bytestream2_get_byteu(&gb);
1662 bytestream2_skipu(&gb, 3 - channel);
1664 /* Decode 28 samples. */
1665 for (n = 0; n < 28; n++) {
1666 int32_t sampledat, prev;
1668 switch (header >> 4) {
1670 prev = (c->status[channel].sample1 * 0x3c);
1673 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1676 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1682 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1684 byte = bytestream2_get_byteu(&gb);
1686 sampledat = sign_extend(byte, 4);
1688 sampledat = sign_extend(byte >> 4, 4);
1690 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1691 *samples++ = av_clip_int16(sampledat >> 6);
1692 c->status[channel].sample2 = c->status[channel].sample1;
1693 c->status[channel].sample1 = sampledat;
1697 bytestream2_seek(&gb, 0, SEEK_SET);
1700 case AV_CODEC_ID_ADPCM_PSX:
1701 for (channel = 0; channel < avctx->channels; channel++) {
1702 samples = samples_p[channel];
1704 /* Read in every sample for this channel. */
1705 for (i = 0; i < nb_samples / 28; i++) {
1706 int filter, shift, flag, byte;
1708 filter = bytestream2_get_byteu(&gb);
1709 shift = filter & 0xf;
1710 filter = filter >> 4;
1711 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1712 return AVERROR_INVALIDDATA;
1713 flag = bytestream2_get_byteu(&gb);
1715 /* Decode 28 samples. */
1716 for (n = 0; n < 28; n++) {
1717 int sample = 0, scale;
1721 scale = sign_extend(byte >> 4, 4);
1723 byte = bytestream2_get_byteu(&gb);
1724 scale = sign_extend(byte, 4);
1727 scale = scale << 12;
1728 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1730 *samples++ = av_clip_int16(sample);
1731 c->status[channel].sample2 = c->status[channel].sample1;
1732 c->status[channel].sample1 = sample;
1739 av_assert0(0); // unsupported codec_id should not happen
1742 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1743 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1744 return AVERROR_INVALIDDATA;
1749 if (avpkt->size < bytestream2_tell(&gb)) {
1750 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1754 return bytestream2_tell(&gb);
1757 static void adpcm_flush(AVCodecContext *avctx)
1759 ADPCMDecodeContext *c = avctx->priv_data;
1764 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1765 AV_SAMPLE_FMT_NONE };
1766 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1767 AV_SAMPLE_FMT_NONE };
1768 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1770 AV_SAMPLE_FMT_NONE };
1772 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1773 AVCodec ff_ ## name_ ## _decoder = { \
1775 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1776 .type = AVMEDIA_TYPE_AUDIO, \
1778 .priv_data_size = sizeof(ADPCMDecodeContext), \
1779 .init = adpcm_decode_init, \
1780 .decode = adpcm_decode_frame, \
1781 .flush = adpcm_flush, \
1782 .capabilities = AV_CODEC_CAP_DR1, \
1783 .sample_fmts = sample_fmts_, \
1786 /* Note: Do not forget to add new entries to the Makefile as well. */
1787 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1788 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1789 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
1790 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
1791 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1792 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1793 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1794 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1795 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1796 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1797 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1798 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1799 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1800 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1801 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
1802 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1803 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1804 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1805 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1806 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1807 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1808 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1809 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1810 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1811 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1812 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1813 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1814 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
1815 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
1816 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1817 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1818 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1819 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1820 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1821 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1822 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1823 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");