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_THP:
111 case AV_CODEC_ID_ADPCM_THP_LE:
115 if (avctx->channels < min_channels || avctx->channels > max_channels) {
116 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
117 return AVERROR(EINVAL);
120 switch(avctx->codec->id) {
121 case AV_CODEC_ID_ADPCM_CT:
122 c->status[0].step = c->status[1].step = 511;
124 case AV_CODEC_ID_ADPCM_IMA_WAV:
125 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
126 return AVERROR_INVALIDDATA;
128 case AV_CODEC_ID_ADPCM_IMA_APC:
129 if (avctx->extradata && avctx->extradata_size >= 8) {
130 c->status[0].predictor = AV_RL32(avctx->extradata);
131 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
134 case AV_CODEC_ID_ADPCM_IMA_WS:
135 if (avctx->extradata && avctx->extradata_size >= 2)
136 c->vqa_version = AV_RL16(avctx->extradata);
142 switch(avctx->codec->id) {
143 case AV_CODEC_ID_ADPCM_IMA_QT:
144 case AV_CODEC_ID_ADPCM_IMA_WAV:
145 case AV_CODEC_ID_ADPCM_4XM:
146 case AV_CODEC_ID_ADPCM_XA:
147 case AV_CODEC_ID_ADPCM_EA_R1:
148 case AV_CODEC_ID_ADPCM_EA_R2:
149 case AV_CODEC_ID_ADPCM_EA_R3:
150 case AV_CODEC_ID_ADPCM_EA_XAS:
151 case AV_CODEC_ID_ADPCM_THP:
152 case AV_CODEC_ID_ADPCM_THP_LE:
153 case AV_CODEC_ID_ADPCM_AFC:
154 case AV_CODEC_ID_ADPCM_DTK:
155 case AV_CODEC_ID_ADPCM_PSX:
156 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
158 case AV_CODEC_ID_ADPCM_IMA_WS:
159 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
163 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
169 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
173 int sign, delta, diff, step;
175 step = ff_adpcm_step_table[c->step_index];
176 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
177 step_index = av_clip(step_index, 0, 88);
181 /* perform direct multiplication instead of series of jumps proposed by
182 * the reference ADPCM implementation since modern CPUs can do the mults
184 diff = ((2 * delta + 1) * step) >> shift;
185 predictor = c->predictor;
186 if (sign) predictor -= diff;
187 else predictor += diff;
189 c->predictor = av_clip_int16(predictor);
190 c->step_index = step_index;
192 return (short)c->predictor;
195 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
197 int nibble, step_index, predictor, sign, delta, diff, step, shift;
200 nibble = get_bits_le(gb, bps),
201 step = ff_adpcm_step_table[c->step_index];
202 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
203 step_index = av_clip(step_index, 0, 88);
205 sign = nibble & (1 << shift);
206 delta = av_mod_uintp2(nibble, shift);
207 diff = ((2 * delta + 1) * step) >> shift;
208 predictor = c->predictor;
209 if (sign) predictor -= diff;
210 else predictor += diff;
212 c->predictor = av_clip_int16(predictor);
213 c->step_index = step_index;
215 return (int16_t)c->predictor;
218 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
224 step = ff_adpcm_step_table[c->step_index];
225 step_index = c->step_index + ff_adpcm_index_table[nibble];
226 step_index = av_clip(step_index, 0, 88);
229 if (nibble & 4) diff += step;
230 if (nibble & 2) diff += step >> 1;
231 if (nibble & 1) diff += step >> 2;
234 predictor = c->predictor - diff;
236 predictor = c->predictor + diff;
238 c->predictor = av_clip_int16(predictor);
239 c->step_index = step_index;
244 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
248 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
249 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
251 c->sample2 = c->sample1;
252 c->sample1 = av_clip_int16(predictor);
253 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
254 if (c->idelta < 16) c->idelta = 16;
255 if (c->idelta > INT_MAX/768) {
256 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
257 c->idelta = INT_MAX/768;
263 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
265 int step_index, predictor, sign, delta, diff, step;
267 step = ff_adpcm_oki_step_table[c->step_index];
268 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
269 step_index = av_clip(step_index, 0, 48);
273 diff = ((2 * delta + 1) * step) >> 3;
274 predictor = c->predictor;
275 if (sign) predictor -= diff;
276 else predictor += diff;
278 c->predictor = av_clip_intp2(predictor, 11);
279 c->step_index = step_index;
281 return c->predictor << 4;
284 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
286 int sign, delta, diff;
291 /* perform direct multiplication instead of series of jumps proposed by
292 * the reference ADPCM implementation since modern CPUs can do the mults
294 diff = ((2 * delta + 1) * c->step) >> 3;
295 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
296 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
297 c->predictor = av_clip_int16(c->predictor);
298 /* calculate new step and clamp it to range 511..32767 */
299 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
300 c->step = av_clip(new_step, 511, 32767);
302 return (short)c->predictor;
305 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
307 int sign, delta, diff;
309 sign = nibble & (1<<(size-1));
310 delta = nibble & ((1<<(size-1))-1);
311 diff = delta << (7 + c->step + shift);
314 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
316 /* calculate new step */
317 if (delta >= (2*size - 3) && c->step < 3)
319 else if (delta == 0 && c->step > 0)
322 return (short) c->predictor;
325 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
332 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
333 c->predictor = av_clip_int16(c->predictor);
334 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
335 c->step = av_clip(c->step, 127, 24567);
339 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
340 const uint8_t *in, ADPCMChannelStatus *left,
341 ADPCMChannelStatus *right, int channels, int sample_offset)
344 int shift,filter,f0,f1;
348 out0 += sample_offset;
352 out1 += sample_offset;
355 shift = 12 - (in[4+i*2] & 15);
356 filter = in[4+i*2] >> 4;
357 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
358 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
361 f0 = xa_adpcm_table[filter][0];
362 f1 = xa_adpcm_table[filter][1];
370 t = sign_extend(d, 4);
371 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
373 s_1 = av_clip_int16(s);
380 s_1 = right->sample1;
381 s_2 = right->sample2;
384 shift = 12 - (in[5+i*2] & 15);
385 filter = in[5+i*2] >> 4;
386 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
387 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
391 f0 = xa_adpcm_table[filter][0];
392 f1 = xa_adpcm_table[filter][1];
397 t = sign_extend(d >> 4, 4);
398 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
400 s_1 = av_clip_int16(s);
405 right->sample1 = s_1;
406 right->sample2 = s_2;
412 out0 += 28 * (3 - channels);
413 out1 += 28 * (3 - channels);
419 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
421 ADPCMDecodeContext *c = avctx->priv_data;
424 int k0, signmask, nb_bits, count;
425 int size = buf_size*8;
428 init_get_bits(&gb, buf, size);
430 //read bits & initial values
431 nb_bits = get_bits(&gb, 2)+2;
432 table = swf_index_tables[nb_bits-2];
433 k0 = 1 << (nb_bits-2);
434 signmask = 1 << (nb_bits-1);
436 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
437 for (i = 0; i < avctx->channels; i++) {
438 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
439 c->status[i].step_index = get_bits(&gb, 6);
442 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
445 for (i = 0; i < avctx->channels; i++) {
446 // similar to IMA adpcm
447 int delta = get_bits(&gb, nb_bits);
448 int step = ff_adpcm_step_table[c->status[i].step_index];
449 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
460 if (delta & signmask)
461 c->status[i].predictor -= vpdiff;
463 c->status[i].predictor += vpdiff;
465 c->status[i].step_index += table[delta & (~signmask)];
467 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
468 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
470 *samples++ = c->status[i].predictor;
477 * Get the number of samples that will be decoded from the packet.
478 * In one case, this is actually the maximum number of samples possible to
479 * decode with the given buf_size.
481 * @param[out] coded_samples set to the number of samples as coded in the
482 * packet, or 0 if the codec does not encode the
483 * number of samples in each frame.
484 * @param[out] approx_nb_samples set to non-zero if the number of samples
485 * returned is an approximation.
487 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
488 int buf_size, int *coded_samples, int *approx_nb_samples)
490 ADPCMDecodeContext *s = avctx->priv_data;
492 int ch = avctx->channels;
493 int has_coded_samples = 0;
497 *approx_nb_samples = 0;
502 switch (avctx->codec->id) {
503 /* constant, only check buf_size */
504 case AV_CODEC_ID_ADPCM_EA_XAS:
505 if (buf_size < 76 * ch)
509 case AV_CODEC_ID_ADPCM_IMA_QT:
510 if (buf_size < 34 * ch)
514 /* simple 4-bit adpcm */
515 case AV_CODEC_ID_ADPCM_CT:
516 case AV_CODEC_ID_ADPCM_IMA_APC:
517 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
518 case AV_CODEC_ID_ADPCM_IMA_OKI:
519 case AV_CODEC_ID_ADPCM_IMA_WS:
520 case AV_CODEC_ID_ADPCM_YAMAHA:
521 nb_samples = buf_size * 2 / ch;
527 /* simple 4-bit adpcm, with header */
529 switch (avctx->codec->id) {
530 case AV_CODEC_ID_ADPCM_4XM:
531 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
532 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
533 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
536 return (buf_size - header_size) * 2 / ch;
538 /* more complex formats */
539 switch (avctx->codec->id) {
540 case AV_CODEC_ID_ADPCM_EA:
541 has_coded_samples = 1;
542 *coded_samples = bytestream2_get_le32(gb);
543 *coded_samples -= *coded_samples % 28;
544 nb_samples = (buf_size - 12) / 30 * 28;
546 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
547 has_coded_samples = 1;
548 *coded_samples = bytestream2_get_le32(gb);
549 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
551 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
552 nb_samples = (buf_size - ch) / ch * 2;
554 case AV_CODEC_ID_ADPCM_EA_R1:
555 case AV_CODEC_ID_ADPCM_EA_R2:
556 case AV_CODEC_ID_ADPCM_EA_R3:
557 /* maximum number of samples */
558 /* has internal offsets and a per-frame switch to signal raw 16-bit */
559 has_coded_samples = 1;
560 switch (avctx->codec->id) {
561 case AV_CODEC_ID_ADPCM_EA_R1:
562 header_size = 4 + 9 * ch;
563 *coded_samples = bytestream2_get_le32(gb);
565 case AV_CODEC_ID_ADPCM_EA_R2:
566 header_size = 4 + 5 * ch;
567 *coded_samples = bytestream2_get_le32(gb);
569 case AV_CODEC_ID_ADPCM_EA_R3:
570 header_size = 4 + 5 * ch;
571 *coded_samples = bytestream2_get_be32(gb);
574 *coded_samples -= *coded_samples % 28;
575 nb_samples = (buf_size - header_size) * 2 / ch;
576 nb_samples -= nb_samples % 28;
577 *approx_nb_samples = 1;
579 case AV_CODEC_ID_ADPCM_IMA_DK3:
580 if (avctx->block_align > 0)
581 buf_size = FFMIN(buf_size, avctx->block_align);
582 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
584 case AV_CODEC_ID_ADPCM_IMA_DK4:
585 if (avctx->block_align > 0)
586 buf_size = FFMIN(buf_size, avctx->block_align);
587 if (buf_size < 4 * ch)
588 return AVERROR_INVALIDDATA;
589 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
591 case AV_CODEC_ID_ADPCM_IMA_RAD:
592 if (avctx->block_align > 0)
593 buf_size = FFMIN(buf_size, avctx->block_align);
594 nb_samples = (buf_size - 4 * ch) * 2 / ch;
596 case AV_CODEC_ID_ADPCM_IMA_WAV:
598 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
599 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
600 if (avctx->block_align > 0)
601 buf_size = FFMIN(buf_size, avctx->block_align);
602 if (buf_size < 4 * ch)
603 return AVERROR_INVALIDDATA;
604 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
607 case AV_CODEC_ID_ADPCM_MS:
608 if (avctx->block_align > 0)
609 buf_size = FFMIN(buf_size, avctx->block_align);
610 nb_samples = (buf_size - 6 * ch) * 2 / ch;
612 case AV_CODEC_ID_ADPCM_SBPRO_2:
613 case AV_CODEC_ID_ADPCM_SBPRO_3:
614 case AV_CODEC_ID_ADPCM_SBPRO_4:
616 int samples_per_byte;
617 switch (avctx->codec->id) {
618 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
619 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
620 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
622 if (!s->status[0].step_index) {
624 return AVERROR_INVALIDDATA;
628 nb_samples += buf_size * samples_per_byte / ch;
631 case AV_CODEC_ID_ADPCM_SWF:
633 int buf_bits = buf_size * 8 - 2;
634 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
635 int block_hdr_size = 22 * ch;
636 int block_size = block_hdr_size + nbits * ch * 4095;
637 int nblocks = buf_bits / block_size;
638 int bits_left = buf_bits - nblocks * block_size;
639 nb_samples = nblocks * 4096;
640 if (bits_left >= block_hdr_size)
641 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
644 case AV_CODEC_ID_ADPCM_THP:
645 case AV_CODEC_ID_ADPCM_THP_LE:
646 if (avctx->extradata) {
647 nb_samples = buf_size * 14 / (8 * ch);
650 has_coded_samples = 1;
651 bytestream2_skip(gb, 4); // channel size
652 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
653 bytestream2_get_le32(gb) :
654 bytestream2_get_be32(gb);
655 buf_size -= 8 + 36 * ch;
657 nb_samples = buf_size / 8 * 14;
658 if (buf_size % 8 > 1)
659 nb_samples += (buf_size % 8 - 1) * 2;
660 *approx_nb_samples = 1;
662 case AV_CODEC_ID_ADPCM_AFC:
663 nb_samples = buf_size / (9 * ch) * 16;
665 case AV_CODEC_ID_ADPCM_XA:
666 nb_samples = (buf_size / 128) * 224 / ch;
668 case AV_CODEC_ID_ADPCM_DTK:
669 case AV_CODEC_ID_ADPCM_PSX:
670 nb_samples = buf_size / (16 * ch) * 28;
674 /* validate coded sample count */
675 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
676 return AVERROR_INVALIDDATA;
681 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
682 int *got_frame_ptr, AVPacket *avpkt)
684 AVFrame *frame = data;
685 const uint8_t *buf = avpkt->data;
686 int buf_size = avpkt->size;
687 ADPCMDecodeContext *c = avctx->priv_data;
688 ADPCMChannelStatus *cs;
689 int n, m, channel, i;
694 int nb_samples, coded_samples, approx_nb_samples, ret;
697 bytestream2_init(&gb, buf, buf_size);
698 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
699 if (nb_samples <= 0) {
700 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
701 return AVERROR_INVALIDDATA;
704 /* get output buffer */
705 frame->nb_samples = nb_samples;
706 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
708 samples = (short *)frame->data[0];
709 samples_p = (int16_t **)frame->extended_data;
711 /* use coded_samples when applicable */
712 /* it is always <= nb_samples, so the output buffer will be large enough */
714 if (!approx_nb_samples && coded_samples != nb_samples)
715 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
716 frame->nb_samples = nb_samples = coded_samples;
719 st = avctx->channels == 2 ? 1 : 0;
721 switch(avctx->codec->id) {
722 case AV_CODEC_ID_ADPCM_IMA_QT:
723 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
724 Channel data is interleaved per-chunk. */
725 for (channel = 0; channel < avctx->channels; channel++) {
728 cs = &(c->status[channel]);
729 /* (pppppp) (piiiiiii) */
731 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
732 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
733 step_index = predictor & 0x7F;
736 if (cs->step_index == step_index) {
737 int diff = predictor - cs->predictor;
744 cs->step_index = step_index;
745 cs->predictor = predictor;
748 if (cs->step_index > 88u){
749 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
750 channel, cs->step_index);
751 return AVERROR_INVALIDDATA;
754 samples = samples_p[channel];
756 for (m = 0; m < 64; m += 2) {
757 int byte = bytestream2_get_byteu(&gb);
758 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
759 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
763 case AV_CODEC_ID_ADPCM_IMA_WAV:
764 for(i=0; i<avctx->channels; i++){
765 cs = &(c->status[i]);
766 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
768 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
769 if (cs->step_index > 88u){
770 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
772 return AVERROR_INVALIDDATA;
776 if (avctx->bits_per_coded_sample != 4) {
777 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
780 ret = init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
783 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
784 for (i = 0; i < avctx->channels; i++) {
786 samples = &samples_p[i][1 + n * samples_per_block];
787 for (m = 0; m < samples_per_block; m++) {
788 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
789 avctx->bits_per_coded_sample);
793 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
795 for (n = 0; n < (nb_samples - 1) / 8; n++) {
796 for (i = 0; i < avctx->channels; i++) {
798 samples = &samples_p[i][1 + n * 8];
799 for (m = 0; m < 8; m += 2) {
800 int v = bytestream2_get_byteu(&gb);
801 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
802 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
808 case AV_CODEC_ID_ADPCM_4XM:
809 for (i = 0; i < avctx->channels; i++)
810 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
812 for (i = 0; i < avctx->channels; i++) {
813 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
814 if (c->status[i].step_index > 88u) {
815 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
816 i, c->status[i].step_index);
817 return AVERROR_INVALIDDATA;
821 for (i = 0; i < avctx->channels; i++) {
822 samples = (int16_t *)frame->data[i];
824 for (n = nb_samples >> 1; n > 0; n--) {
825 int v = bytestream2_get_byteu(&gb);
826 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
827 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
831 case AV_CODEC_ID_ADPCM_MS:
835 block_predictor = bytestream2_get_byteu(&gb);
836 if (block_predictor > 6) {
837 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
839 return AVERROR_INVALIDDATA;
841 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
842 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
844 block_predictor = bytestream2_get_byteu(&gb);
845 if (block_predictor > 6) {
846 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
848 return AVERROR_INVALIDDATA;
850 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
851 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
853 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
855 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
858 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
859 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
860 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
861 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
863 *samples++ = c->status[0].sample2;
864 if (st) *samples++ = c->status[1].sample2;
865 *samples++ = c->status[0].sample1;
866 if (st) *samples++ = c->status[1].sample1;
867 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
868 int byte = bytestream2_get_byteu(&gb);
869 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
870 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
874 case AV_CODEC_ID_ADPCM_IMA_DK4:
875 for (channel = 0; channel < avctx->channels; channel++) {
876 cs = &c->status[channel];
877 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
878 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
879 if (cs->step_index > 88u){
880 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
881 channel, cs->step_index);
882 return AVERROR_INVALIDDATA;
885 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
886 int v = bytestream2_get_byteu(&gb);
887 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
888 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
891 case AV_CODEC_ID_ADPCM_IMA_DK3:
895 int decode_top_nibble_next = 0;
897 const int16_t *samples_end = samples + avctx->channels * nb_samples;
899 bytestream2_skipu(&gb, 10);
900 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
901 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
902 c->status[0].step_index = bytestream2_get_byteu(&gb);
903 c->status[1].step_index = bytestream2_get_byteu(&gb);
904 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
905 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
906 c->status[0].step_index, c->status[1].step_index);
907 return AVERROR_INVALIDDATA;
909 /* sign extend the predictors */
910 diff_channel = c->status[1].predictor;
912 /* DK3 ADPCM support macro */
913 #define DK3_GET_NEXT_NIBBLE() \
914 if (decode_top_nibble_next) { \
915 nibble = last_byte >> 4; \
916 decode_top_nibble_next = 0; \
918 last_byte = bytestream2_get_byteu(&gb); \
919 nibble = last_byte & 0x0F; \
920 decode_top_nibble_next = 1; \
923 while (samples < samples_end) {
925 /* for this algorithm, c->status[0] is the sum channel and
926 * c->status[1] is the diff channel */
928 /* process the first predictor of the sum channel */
929 DK3_GET_NEXT_NIBBLE();
930 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
932 /* process the diff channel predictor */
933 DK3_GET_NEXT_NIBBLE();
934 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
936 /* process the first pair of stereo PCM samples */
937 diff_channel = (diff_channel + c->status[1].predictor) / 2;
938 *samples++ = c->status[0].predictor + c->status[1].predictor;
939 *samples++ = c->status[0].predictor - c->status[1].predictor;
941 /* process the second predictor of the sum channel */
942 DK3_GET_NEXT_NIBBLE();
943 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
945 /* process the second pair of stereo PCM samples */
946 diff_channel = (diff_channel + c->status[1].predictor) / 2;
947 *samples++ = c->status[0].predictor + c->status[1].predictor;
948 *samples++ = c->status[0].predictor - c->status[1].predictor;
951 if ((bytestream2_tell(&gb) & 1))
952 bytestream2_skip(&gb, 1);
955 case AV_CODEC_ID_ADPCM_IMA_ISS:
956 for (channel = 0; channel < avctx->channels; channel++) {
957 cs = &c->status[channel];
958 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
959 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
960 if (cs->step_index > 88u){
961 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
962 channel, cs->step_index);
963 return AVERROR_INVALIDDATA;
967 for (n = nb_samples >> (1 - st); n > 0; n--) {
969 int v = bytestream2_get_byteu(&gb);
970 /* nibbles are swapped for mono */
978 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
979 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
982 case AV_CODEC_ID_ADPCM_IMA_APC:
983 while (bytestream2_get_bytes_left(&gb) > 0) {
984 int v = bytestream2_get_byteu(&gb);
985 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
986 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
989 case AV_CODEC_ID_ADPCM_IMA_OKI:
990 while (bytestream2_get_bytes_left(&gb) > 0) {
991 int v = bytestream2_get_byteu(&gb);
992 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
993 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
996 case AV_CODEC_ID_ADPCM_IMA_RAD:
997 for (channel = 0; channel < avctx->channels; channel++) {
998 cs = &c->status[channel];
999 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1000 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1001 if (cs->step_index > 88u){
1002 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1003 channel, cs->step_index);
1004 return AVERROR_INVALIDDATA;
1007 for (n = 0; n < nb_samples / 2; n++) {
1010 byte[0] = bytestream2_get_byteu(&gb);
1012 byte[1] = bytestream2_get_byteu(&gb);
1013 for(channel = 0; channel < avctx->channels; channel++) {
1014 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1016 for(channel = 0; channel < avctx->channels; channel++) {
1017 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1021 case AV_CODEC_ID_ADPCM_IMA_WS:
1022 if (c->vqa_version == 3) {
1023 for (channel = 0; channel < avctx->channels; channel++) {
1024 int16_t *smp = samples_p[channel];
1026 for (n = nb_samples / 2; n > 0; n--) {
1027 int v = bytestream2_get_byteu(&gb);
1028 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1029 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1033 for (n = nb_samples / 2; n > 0; n--) {
1034 for (channel = 0; channel < avctx->channels; channel++) {
1035 int v = bytestream2_get_byteu(&gb);
1036 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1037 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1039 samples += avctx->channels;
1042 bytestream2_seek(&gb, 0, SEEK_END);
1044 case AV_CODEC_ID_ADPCM_XA:
1046 int16_t *out0 = samples_p[0];
1047 int16_t *out1 = samples_p[1];
1048 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1049 int sample_offset = 0;
1050 while (bytestream2_get_bytes_left(&gb) >= 128) {
1051 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1052 &c->status[0], &c->status[1],
1053 avctx->channels, sample_offset)) < 0)
1055 bytestream2_skipu(&gb, 128);
1056 sample_offset += samples_per_block;
1060 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1061 for (i=0; i<=st; i++) {
1062 c->status[i].step_index = bytestream2_get_le32u(&gb);
1063 if (c->status[i].step_index > 88u) {
1064 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1065 i, c->status[i].step_index);
1066 return AVERROR_INVALIDDATA;
1069 for (i=0; i<=st; i++)
1070 c->status[i].predictor = bytestream2_get_le32u(&gb);
1072 for (n = nb_samples >> (1 - st); n > 0; n--) {
1073 int byte = bytestream2_get_byteu(&gb);
1074 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1075 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1078 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1079 for (n = nb_samples >> (1 - st); n > 0; n--) {
1080 int byte = bytestream2_get_byteu(&gb);
1081 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1082 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1085 case AV_CODEC_ID_ADPCM_EA:
1087 int previous_left_sample, previous_right_sample;
1088 int current_left_sample, current_right_sample;
1089 int next_left_sample, next_right_sample;
1090 int coeff1l, coeff2l, coeff1r, coeff2r;
1091 int shift_left, shift_right;
1093 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1094 each coding 28 stereo samples. */
1096 if(avctx->channels != 2)
1097 return AVERROR_INVALIDDATA;
1099 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1100 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1101 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1102 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1104 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1105 int byte = bytestream2_get_byteu(&gb);
1106 coeff1l = ea_adpcm_table[ byte >> 4 ];
1107 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1108 coeff1r = ea_adpcm_table[ byte & 0x0F];
1109 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1111 byte = bytestream2_get_byteu(&gb);
1112 shift_left = 20 - (byte >> 4);
1113 shift_right = 20 - (byte & 0x0F);
1115 for (count2 = 0; count2 < 28; count2++) {
1116 byte = bytestream2_get_byteu(&gb);
1117 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1118 next_right_sample = sign_extend(byte, 4) << shift_right;
1120 next_left_sample = (next_left_sample +
1121 (current_left_sample * coeff1l) +
1122 (previous_left_sample * coeff2l) + 0x80) >> 8;
1123 next_right_sample = (next_right_sample +
1124 (current_right_sample * coeff1r) +
1125 (previous_right_sample * coeff2r) + 0x80) >> 8;
1127 previous_left_sample = current_left_sample;
1128 current_left_sample = av_clip_int16(next_left_sample);
1129 previous_right_sample = current_right_sample;
1130 current_right_sample = av_clip_int16(next_right_sample);
1131 *samples++ = current_left_sample;
1132 *samples++ = current_right_sample;
1136 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1140 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1142 int coeff[2][2], shift[2];
1144 for(channel = 0; channel < avctx->channels; channel++) {
1145 int byte = bytestream2_get_byteu(&gb);
1147 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1148 shift[channel] = 20 - (byte & 0x0F);
1150 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1153 byte[0] = bytestream2_get_byteu(&gb);
1154 if (st) byte[1] = bytestream2_get_byteu(&gb);
1155 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1156 for(channel = 0; channel < avctx->channels; channel++) {
1157 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1159 c->status[channel].sample1 * coeff[channel][0] +
1160 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1161 c->status[channel].sample2 = c->status[channel].sample1;
1162 c->status[channel].sample1 = av_clip_int16(sample);
1163 *samples++ = c->status[channel].sample1;
1167 bytestream2_seek(&gb, 0, SEEK_END);
1170 case AV_CODEC_ID_ADPCM_EA_R1:
1171 case AV_CODEC_ID_ADPCM_EA_R2:
1172 case AV_CODEC_ID_ADPCM_EA_R3: {
1173 /* channel numbering
1175 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1176 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1177 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1178 int previous_sample, current_sample, next_sample;
1181 unsigned int channel;
1186 for (channel=0; channel<avctx->channels; channel++)
1187 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1188 bytestream2_get_le32(&gb)) +
1189 (avctx->channels + 1) * 4;
1191 for (channel=0; channel<avctx->channels; channel++) {
1192 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1193 samplesC = samples_p[channel];
1195 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1196 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1197 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1199 current_sample = c->status[channel].predictor;
1200 previous_sample = c->status[channel].prev_sample;
1203 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1204 int byte = bytestream2_get_byte(&gb);
1205 if (byte == 0xEE) { /* only seen in R2 and R3 */
1206 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1207 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1209 for (count2=0; count2<28; count2++)
1210 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1212 coeff1 = ea_adpcm_table[ byte >> 4 ];
1213 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1214 shift = 20 - (byte & 0x0F);
1216 for (count2=0; count2<28; count2++) {
1218 next_sample = sign_extend(byte, 4) << shift;
1220 byte = bytestream2_get_byte(&gb);
1221 next_sample = sign_extend(byte >> 4, 4) << shift;
1224 next_sample += (current_sample * coeff1) +
1225 (previous_sample * coeff2);
1226 next_sample = av_clip_int16(next_sample >> 8);
1228 previous_sample = current_sample;
1229 current_sample = next_sample;
1230 *samplesC++ = current_sample;
1236 } else if (count != count1) {
1237 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1238 count = FFMAX(count, count1);
1241 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1242 c->status[channel].predictor = current_sample;
1243 c->status[channel].prev_sample = previous_sample;
1247 frame->nb_samples = count * 28;
1248 bytestream2_seek(&gb, 0, SEEK_END);
1251 case AV_CODEC_ID_ADPCM_EA_XAS:
1252 for (channel=0; channel<avctx->channels; channel++) {
1253 int coeff[2][4], shift[4];
1254 int16_t *s = samples_p[channel];
1255 for (n = 0; n < 4; n++, s += 32) {
1256 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1258 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1261 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1262 shift[n] = 20 - (val & 0x0F);
1266 for (m=2; m<32; m+=2) {
1267 s = &samples_p[channel][m];
1268 for (n = 0; n < 4; n++, s += 32) {
1270 int byte = bytestream2_get_byteu(&gb);
1272 level = sign_extend(byte >> 4, 4) << shift[n];
1273 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1274 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1276 level = sign_extend(byte, 4) << shift[n];
1277 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1278 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1283 case AV_CODEC_ID_ADPCM_IMA_AMV:
1284 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1285 c->status[0].step_index = bytestream2_get_le16u(&gb);
1286 bytestream2_skipu(&gb, 4);
1287 if (c->status[0].step_index > 88u) {
1288 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1289 c->status[0].step_index);
1290 return AVERROR_INVALIDDATA;
1293 for (n = nb_samples >> (1 - st); n > 0; n--) {
1294 int v = bytestream2_get_byteu(&gb);
1296 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1297 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1300 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1301 for (i = 0; i < avctx->channels; i++) {
1302 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1303 c->status[i].step_index = bytestream2_get_byteu(&gb);
1304 bytestream2_skipu(&gb, 1);
1305 if (c->status[i].step_index > 88u) {
1306 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1307 c->status[i].step_index);
1308 return AVERROR_INVALIDDATA;
1312 for (n = nb_samples >> (1 - st); n > 0; n--) {
1313 int v = bytestream2_get_byteu(&gb);
1315 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1316 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1319 case AV_CODEC_ID_ADPCM_CT:
1320 for (n = nb_samples >> (1 - st); n > 0; n--) {
1321 int v = bytestream2_get_byteu(&gb);
1322 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1323 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1326 case AV_CODEC_ID_ADPCM_SBPRO_4:
1327 case AV_CODEC_ID_ADPCM_SBPRO_3:
1328 case AV_CODEC_ID_ADPCM_SBPRO_2:
1329 if (!c->status[0].step_index) {
1330 /* the first byte is a raw sample */
1331 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1333 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1334 c->status[0].step_index = 1;
1337 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1338 for (n = nb_samples >> (1 - st); n > 0; n--) {
1339 int byte = bytestream2_get_byteu(&gb);
1340 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1342 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1345 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1346 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1347 int byte = bytestream2_get_byteu(&gb);
1348 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1350 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1351 (byte >> 2) & 0x07, 3, 0);
1352 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1356 for (n = nb_samples >> (2 - st); n > 0; n--) {
1357 int byte = bytestream2_get_byteu(&gb);
1358 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1360 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1361 (byte >> 4) & 0x03, 2, 2);
1362 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1363 (byte >> 2) & 0x03, 2, 2);
1364 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1369 case AV_CODEC_ID_ADPCM_SWF:
1370 adpcm_swf_decode(avctx, buf, buf_size, samples);
1371 bytestream2_seek(&gb, 0, SEEK_END);
1373 case AV_CODEC_ID_ADPCM_YAMAHA:
1374 for (n = nb_samples >> (1 - st); n > 0; n--) {
1375 int v = bytestream2_get_byteu(&gb);
1376 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1377 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1380 case AV_CODEC_ID_ADPCM_AFC:
1382 int samples_per_block;
1385 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1386 samples_per_block = avctx->extradata[0] / 16;
1387 blocks = nb_samples / avctx->extradata[0];
1389 samples_per_block = nb_samples / 16;
1393 for (m = 0; m < blocks; m++) {
1394 for (channel = 0; channel < avctx->channels; channel++) {
1395 int prev1 = c->status[channel].sample1;
1396 int prev2 = c->status[channel].sample2;
1398 samples = samples_p[channel] + m * 16;
1399 /* Read in every sample for this channel. */
1400 for (i = 0; i < samples_per_block; i++) {
1401 int byte = bytestream2_get_byteu(&gb);
1402 int scale = 1 << (byte >> 4);
1403 int index = byte & 0xf;
1404 int factor1 = ff_adpcm_afc_coeffs[0][index];
1405 int factor2 = ff_adpcm_afc_coeffs[1][index];
1407 /* Decode 16 samples. */
1408 for (n = 0; n < 16; n++) {
1412 sampledat = sign_extend(byte, 4);
1414 byte = bytestream2_get_byteu(&gb);
1415 sampledat = sign_extend(byte >> 4, 4);
1418 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1419 ((sampledat * scale) << 11)) >> 11;
1420 *samples = av_clip_int16(sampledat);
1426 c->status[channel].sample1 = prev1;
1427 c->status[channel].sample2 = prev2;
1430 bytestream2_seek(&gb, 0, SEEK_END);
1433 case AV_CODEC_ID_ADPCM_THP:
1434 case AV_CODEC_ID_ADPCM_THP_LE:
1439 #define THP_GET16(g) \
1441 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1442 bytestream2_get_le16u(&(g)) : \
1443 bytestream2_get_be16u(&(g)), 16)
1445 if (avctx->extradata) {
1447 if (avctx->extradata_size < 32 * avctx->channels) {
1448 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1449 return AVERROR_INVALIDDATA;
1452 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1453 for (i = 0; i < avctx->channels; i++)
1454 for (n = 0; n < 16; n++)
1455 table[i][n] = THP_GET16(tb);
1457 for (i = 0; i < avctx->channels; i++)
1458 for (n = 0; n < 16; n++)
1459 table[i][n] = THP_GET16(gb);
1461 if (!c->has_status) {
1462 /* Initialize the previous sample. */
1463 for (i = 0; i < avctx->channels; i++) {
1464 c->status[i].sample1 = THP_GET16(gb);
1465 c->status[i].sample2 = THP_GET16(gb);
1469 bytestream2_skip(&gb, avctx->channels * 4);
1473 for (ch = 0; ch < avctx->channels; ch++) {
1474 samples = samples_p[ch];
1476 /* Read in every sample for this channel. */
1477 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1478 int byte = bytestream2_get_byteu(&gb);
1479 int index = (byte >> 4) & 7;
1480 unsigned int exp = byte & 0x0F;
1481 int factor1 = table[ch][index * 2];
1482 int factor2 = table[ch][index * 2 + 1];
1484 /* Decode 14 samples. */
1485 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1489 sampledat = sign_extend(byte, 4);
1491 byte = bytestream2_get_byteu(&gb);
1492 sampledat = sign_extend(byte >> 4, 4);
1495 sampledat = ((c->status[ch].sample1 * factor1
1496 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1497 *samples = av_clip_int16(sampledat);
1498 c->status[ch].sample2 = c->status[ch].sample1;
1499 c->status[ch].sample1 = *samples++;
1505 case AV_CODEC_ID_ADPCM_DTK:
1506 for (channel = 0; channel < avctx->channels; channel++) {
1507 samples = samples_p[channel];
1509 /* Read in every sample for this channel. */
1510 for (i = 0; i < nb_samples / 28; i++) {
1513 bytestream2_skipu(&gb, 1);
1514 header = bytestream2_get_byteu(&gb);
1515 bytestream2_skipu(&gb, 3 - channel);
1517 /* Decode 28 samples. */
1518 for (n = 0; n < 28; n++) {
1519 int32_t sampledat, prev;
1521 switch (header >> 4) {
1523 prev = (c->status[channel].sample1 * 0x3c);
1526 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1529 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1535 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1537 byte = bytestream2_get_byteu(&gb);
1539 sampledat = sign_extend(byte, 4);
1541 sampledat = sign_extend(byte >> 4, 4);
1543 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1544 *samples++ = av_clip_int16(sampledat >> 6);
1545 c->status[channel].sample2 = c->status[channel].sample1;
1546 c->status[channel].sample1 = sampledat;
1550 bytestream2_seek(&gb, 0, SEEK_SET);
1553 case AV_CODEC_ID_ADPCM_PSX:
1554 for (channel = 0; channel < avctx->channels; channel++) {
1555 samples = samples_p[channel];
1557 /* Read in every sample for this channel. */
1558 for (i = 0; i < nb_samples / 28; i++) {
1559 int filter, shift, flag, byte;
1561 filter = bytestream2_get_byteu(&gb);
1562 shift = filter & 0xf;
1563 filter = filter >> 4;
1564 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1565 return AVERROR_INVALIDDATA;
1566 flag = bytestream2_get_byteu(&gb);
1568 /* Decode 28 samples. */
1569 for (n = 0; n < 28; n++) {
1570 int sample = 0, scale;
1574 scale = sign_extend(byte >> 4, 4);
1576 byte = bytestream2_get_byteu(&gb);
1577 scale = sign_extend(byte, 4);
1580 scale = scale << 12;
1581 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1583 *samples++ = av_clip_int16(sample);
1584 c->status[channel].sample2 = c->status[channel].sample1;
1585 c->status[channel].sample1 = sample;
1595 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1596 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1597 return AVERROR_INVALIDDATA;
1602 if (avpkt->size < bytestream2_tell(&gb)) {
1603 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1607 return bytestream2_tell(&gb);
1610 static void adpcm_flush(AVCodecContext *avctx)
1612 ADPCMDecodeContext *c = avctx->priv_data;
1617 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1618 AV_SAMPLE_FMT_NONE };
1619 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1620 AV_SAMPLE_FMT_NONE };
1621 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1623 AV_SAMPLE_FMT_NONE };
1625 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1626 AVCodec ff_ ## name_ ## _decoder = { \
1628 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1629 .type = AVMEDIA_TYPE_AUDIO, \
1631 .priv_data_size = sizeof(ADPCMDecodeContext), \
1632 .init = adpcm_decode_init, \
1633 .decode = adpcm_decode_frame, \
1634 .flush = adpcm_flush, \
1635 .capabilities = AV_CODEC_CAP_DR1, \
1636 .sample_fmts = sample_fmts_, \
1639 /* Note: Do not forget to add new entries to the Makefile as well. */
1640 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1641 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1642 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1643 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1644 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1645 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1646 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1647 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1648 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1649 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1650 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1651 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1652 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1653 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1654 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1655 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1656 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1657 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1658 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1659 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1660 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1661 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1662 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1663 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1664 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM PSX");
1665 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1666 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1667 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1668 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1669 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1670 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1671 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1672 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");