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[10];
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 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
157 case AV_CODEC_ID_ADPCM_IMA_WS:
158 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
162 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
168 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
172 int sign, delta, diff, step;
174 step = ff_adpcm_step_table[c->step_index];
175 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
176 step_index = av_clip(step_index, 0, 88);
180 /* perform direct multiplication instead of series of jumps proposed by
181 * the reference ADPCM implementation since modern CPUs can do the mults
183 diff = ((2 * delta + 1) * step) >> shift;
184 predictor = c->predictor;
185 if (sign) predictor -= diff;
186 else predictor += diff;
188 c->predictor = av_clip_int16(predictor);
189 c->step_index = step_index;
191 return (short)c->predictor;
194 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
196 int nibble, step_index, predictor, sign, delta, diff, step, shift;
199 nibble = get_bits_le(gb, bps),
200 step = ff_adpcm_step_table[c->step_index];
201 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
202 step_index = av_clip(step_index, 0, 88);
204 sign = nibble & (1 << shift);
205 delta = av_mod_uintp2(nibble, shift);
206 diff = ((2 * delta + 1) * step) >> shift;
207 predictor = c->predictor;
208 if (sign) predictor -= diff;
209 else predictor += diff;
211 c->predictor = av_clip_int16(predictor);
212 c->step_index = step_index;
214 return (int16_t)c->predictor;
217 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
223 step = ff_adpcm_step_table[c->step_index];
224 step_index = c->step_index + ff_adpcm_index_table[nibble];
225 step_index = av_clip(step_index, 0, 88);
228 if (nibble & 4) diff += step;
229 if (nibble & 2) diff += step >> 1;
230 if (nibble & 1) diff += step >> 2;
233 predictor = c->predictor - diff;
235 predictor = c->predictor + diff;
237 c->predictor = av_clip_int16(predictor);
238 c->step_index = step_index;
243 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
247 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
248 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
250 c->sample2 = c->sample1;
251 c->sample1 = av_clip_int16(predictor);
252 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
253 if (c->idelta < 16) c->idelta = 16;
254 if (c->idelta > INT_MAX/768) {
255 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
256 c->idelta = INT_MAX/768;
262 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
264 int step_index, predictor, sign, delta, diff, step;
266 step = ff_adpcm_oki_step_table[c->step_index];
267 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
268 step_index = av_clip(step_index, 0, 48);
272 diff = ((2 * delta + 1) * step) >> 3;
273 predictor = c->predictor;
274 if (sign) predictor -= diff;
275 else predictor += diff;
277 c->predictor = av_clip_intp2(predictor, 11);
278 c->step_index = step_index;
280 return c->predictor << 4;
283 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
285 int sign, delta, diff;
290 /* perform direct multiplication instead of series of jumps proposed by
291 * the reference ADPCM implementation since modern CPUs can do the mults
293 diff = ((2 * delta + 1) * c->step) >> 3;
294 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
295 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
296 c->predictor = av_clip_int16(c->predictor);
297 /* calculate new step and clamp it to range 511..32767 */
298 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
299 c->step = av_clip(new_step, 511, 32767);
301 return (short)c->predictor;
304 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
306 int sign, delta, diff;
308 sign = nibble & (1<<(size-1));
309 delta = nibble & ((1<<(size-1))-1);
310 diff = delta << (7 + c->step + shift);
313 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
315 /* calculate new step */
316 if (delta >= (2*size - 3) && c->step < 3)
318 else if (delta == 0 && c->step > 0)
321 return (short) c->predictor;
324 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
331 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
332 c->predictor = av_clip_int16(c->predictor);
333 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
334 c->step = av_clip(c->step, 127, 24567);
338 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
339 const uint8_t *in, ADPCMChannelStatus *left,
340 ADPCMChannelStatus *right, int channels, int sample_offset)
343 int shift,filter,f0,f1;
347 out0 += sample_offset;
351 out1 += sample_offset;
354 shift = 12 - (in[4+i*2] & 15);
355 filter = in[4+i*2] >> 4;
356 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
357 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
360 f0 = xa_adpcm_table[filter][0];
361 f1 = xa_adpcm_table[filter][1];
369 t = sign_extend(d, 4);
370 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
372 s_1 = av_clip_int16(s);
379 s_1 = right->sample1;
380 s_2 = right->sample2;
383 shift = 12 - (in[5+i*2] & 15);
384 filter = in[5+i*2] >> 4;
385 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
386 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
390 f0 = xa_adpcm_table[filter][0];
391 f1 = xa_adpcm_table[filter][1];
396 t = sign_extend(d >> 4, 4);
397 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
399 s_1 = av_clip_int16(s);
404 right->sample1 = s_1;
405 right->sample2 = s_2;
411 out0 += 28 * (3 - channels);
412 out1 += 28 * (3 - channels);
418 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
420 ADPCMDecodeContext *c = avctx->priv_data;
423 int k0, signmask, nb_bits, count;
424 int size = buf_size*8;
427 init_get_bits(&gb, buf, size);
429 //read bits & initial values
430 nb_bits = get_bits(&gb, 2)+2;
431 table = swf_index_tables[nb_bits-2];
432 k0 = 1 << (nb_bits-2);
433 signmask = 1 << (nb_bits-1);
435 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
436 for (i = 0; i < avctx->channels; i++) {
437 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
438 c->status[i].step_index = get_bits(&gb, 6);
441 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
444 for (i = 0; i < avctx->channels; i++) {
445 // similar to IMA adpcm
446 int delta = get_bits(&gb, nb_bits);
447 int step = ff_adpcm_step_table[c->status[i].step_index];
448 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
459 if (delta & signmask)
460 c->status[i].predictor -= vpdiff;
462 c->status[i].predictor += vpdiff;
464 c->status[i].step_index += table[delta & (~signmask)];
466 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
467 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
469 *samples++ = c->status[i].predictor;
476 * Get the number of samples that will be decoded from the packet.
477 * In one case, this is actually the maximum number of samples possible to
478 * decode with the given buf_size.
480 * @param[out] coded_samples set to the number of samples as coded in the
481 * packet, or 0 if the codec does not encode the
482 * number of samples in each frame.
483 * @param[out] approx_nb_samples set to non-zero if the number of samples
484 * returned is an approximation.
486 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
487 int buf_size, int *coded_samples, int *approx_nb_samples)
489 ADPCMDecodeContext *s = avctx->priv_data;
491 int ch = avctx->channels;
492 int has_coded_samples = 0;
496 *approx_nb_samples = 0;
501 switch (avctx->codec->id) {
502 /* constant, only check buf_size */
503 case AV_CODEC_ID_ADPCM_EA_XAS:
504 if (buf_size < 76 * ch)
508 case AV_CODEC_ID_ADPCM_IMA_QT:
509 if (buf_size < 34 * ch)
513 /* simple 4-bit adpcm */
514 case AV_CODEC_ID_ADPCM_CT:
515 case AV_CODEC_ID_ADPCM_IMA_APC:
516 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
517 case AV_CODEC_ID_ADPCM_IMA_OKI:
518 case AV_CODEC_ID_ADPCM_IMA_WS:
519 case AV_CODEC_ID_ADPCM_YAMAHA:
520 nb_samples = buf_size * 2 / ch;
526 /* simple 4-bit adpcm, with header */
528 switch (avctx->codec->id) {
529 case AV_CODEC_ID_ADPCM_4XM:
530 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
531 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
532 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
535 return (buf_size - header_size) * 2 / ch;
537 /* more complex formats */
538 switch (avctx->codec->id) {
539 case AV_CODEC_ID_ADPCM_EA:
540 has_coded_samples = 1;
541 *coded_samples = bytestream2_get_le32(gb);
542 *coded_samples -= *coded_samples % 28;
543 nb_samples = (buf_size - 12) / 30 * 28;
545 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
546 has_coded_samples = 1;
547 *coded_samples = bytestream2_get_le32(gb);
548 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
550 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
551 nb_samples = (buf_size - ch) / ch * 2;
553 case AV_CODEC_ID_ADPCM_EA_R1:
554 case AV_CODEC_ID_ADPCM_EA_R2:
555 case AV_CODEC_ID_ADPCM_EA_R3:
556 /* maximum number of samples */
557 /* has internal offsets and a per-frame switch to signal raw 16-bit */
558 has_coded_samples = 1;
559 switch (avctx->codec->id) {
560 case AV_CODEC_ID_ADPCM_EA_R1:
561 header_size = 4 + 9 * ch;
562 *coded_samples = bytestream2_get_le32(gb);
564 case AV_CODEC_ID_ADPCM_EA_R2:
565 header_size = 4 + 5 * ch;
566 *coded_samples = bytestream2_get_le32(gb);
568 case AV_CODEC_ID_ADPCM_EA_R3:
569 header_size = 4 + 5 * ch;
570 *coded_samples = bytestream2_get_be32(gb);
573 *coded_samples -= *coded_samples % 28;
574 nb_samples = (buf_size - header_size) * 2 / ch;
575 nb_samples -= nb_samples % 28;
576 *approx_nb_samples = 1;
578 case AV_CODEC_ID_ADPCM_IMA_DK3:
579 if (avctx->block_align > 0)
580 buf_size = FFMIN(buf_size, avctx->block_align);
581 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
583 case AV_CODEC_ID_ADPCM_IMA_DK4:
584 if (avctx->block_align > 0)
585 buf_size = FFMIN(buf_size, avctx->block_align);
586 if (buf_size < 4 * ch)
587 return AVERROR_INVALIDDATA;
588 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
590 case AV_CODEC_ID_ADPCM_IMA_RAD:
591 if (avctx->block_align > 0)
592 buf_size = FFMIN(buf_size, avctx->block_align);
593 nb_samples = (buf_size - 4 * ch) * 2 / ch;
595 case AV_CODEC_ID_ADPCM_IMA_WAV:
597 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
598 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
599 if (avctx->block_align > 0)
600 buf_size = FFMIN(buf_size, avctx->block_align);
601 if (buf_size < 4 * ch)
602 return AVERROR_INVALIDDATA;
603 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
606 case AV_CODEC_ID_ADPCM_MS:
607 if (avctx->block_align > 0)
608 buf_size = FFMIN(buf_size, avctx->block_align);
609 nb_samples = (buf_size - 6 * ch) * 2 / ch;
611 case AV_CODEC_ID_ADPCM_SBPRO_2:
612 case AV_CODEC_ID_ADPCM_SBPRO_3:
613 case AV_CODEC_ID_ADPCM_SBPRO_4:
615 int samples_per_byte;
616 switch (avctx->codec->id) {
617 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
618 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
619 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
621 if (!s->status[0].step_index) {
623 return AVERROR_INVALIDDATA;
627 nb_samples += buf_size * samples_per_byte / ch;
630 case AV_CODEC_ID_ADPCM_SWF:
632 int buf_bits = buf_size * 8 - 2;
633 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
634 int block_hdr_size = 22 * ch;
635 int block_size = block_hdr_size + nbits * ch * 4095;
636 int nblocks = buf_bits / block_size;
637 int bits_left = buf_bits - nblocks * block_size;
638 nb_samples = nblocks * 4096;
639 if (bits_left >= block_hdr_size)
640 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
643 case AV_CODEC_ID_ADPCM_THP:
644 case AV_CODEC_ID_ADPCM_THP_LE:
645 if (avctx->extradata) {
646 nb_samples = buf_size * 14 / (8 * ch);
649 has_coded_samples = 1;
650 bytestream2_skip(gb, 4); // channel size
651 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
652 bytestream2_get_le32(gb) :
653 bytestream2_get_be32(gb);
654 buf_size -= 8 + 36 * ch;
656 nb_samples = buf_size / 8 * 14;
657 if (buf_size % 8 > 1)
658 nb_samples += (buf_size % 8 - 1) * 2;
659 *approx_nb_samples = 1;
661 case AV_CODEC_ID_ADPCM_AFC:
662 nb_samples = buf_size / (9 * ch) * 16;
664 case AV_CODEC_ID_ADPCM_XA:
665 nb_samples = (buf_size / 128) * 224 / ch;
667 case AV_CODEC_ID_ADPCM_DTK:
668 nb_samples = buf_size / (16 * ch) * 28;
672 /* validate coded sample count */
673 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
674 return AVERROR_INVALIDDATA;
679 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
680 int *got_frame_ptr, AVPacket *avpkt)
682 AVFrame *frame = data;
683 const uint8_t *buf = avpkt->data;
684 int buf_size = avpkt->size;
685 ADPCMDecodeContext *c = avctx->priv_data;
686 ADPCMChannelStatus *cs;
687 int n, m, channel, i;
692 int nb_samples, coded_samples, approx_nb_samples, ret;
695 bytestream2_init(&gb, buf, buf_size);
696 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
697 if (nb_samples <= 0) {
698 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
699 return AVERROR_INVALIDDATA;
702 /* get output buffer */
703 frame->nb_samples = nb_samples;
704 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
706 samples = (short *)frame->data[0];
707 samples_p = (int16_t **)frame->extended_data;
709 /* use coded_samples when applicable */
710 /* it is always <= nb_samples, so the output buffer will be large enough */
712 if (!approx_nb_samples && coded_samples != nb_samples)
713 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
714 frame->nb_samples = nb_samples = coded_samples;
717 st = avctx->channels == 2 ? 1 : 0;
719 switch(avctx->codec->id) {
720 case AV_CODEC_ID_ADPCM_IMA_QT:
721 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
722 Channel data is interleaved per-chunk. */
723 for (channel = 0; channel < avctx->channels; channel++) {
726 cs = &(c->status[channel]);
727 /* (pppppp) (piiiiiii) */
729 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
730 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
731 step_index = predictor & 0x7F;
734 if (cs->step_index == step_index) {
735 int diff = predictor - cs->predictor;
742 cs->step_index = step_index;
743 cs->predictor = predictor;
746 if (cs->step_index > 88u){
747 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
748 channel, cs->step_index);
749 return AVERROR_INVALIDDATA;
752 samples = samples_p[channel];
754 for (m = 0; m < 64; m += 2) {
755 int byte = bytestream2_get_byteu(&gb);
756 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
757 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
761 case AV_CODEC_ID_ADPCM_IMA_WAV:
762 for(i=0; i<avctx->channels; i++){
763 cs = &(c->status[i]);
764 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
766 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
767 if (cs->step_index > 88u){
768 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
770 return AVERROR_INVALIDDATA;
774 if (avctx->bits_per_coded_sample != 4) {
775 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
778 ret = init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
781 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
782 for (i = 0; i < avctx->channels; i++) {
784 samples = &samples_p[i][1 + n * samples_per_block];
785 for (m = 0; m < samples_per_block; m++) {
786 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
787 avctx->bits_per_coded_sample);
791 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
793 for (n = 0; n < (nb_samples - 1) / 8; n++) {
794 for (i = 0; i < avctx->channels; i++) {
796 samples = &samples_p[i][1 + n * 8];
797 for (m = 0; m < 8; m += 2) {
798 int v = bytestream2_get_byteu(&gb);
799 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
800 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
806 case AV_CODEC_ID_ADPCM_4XM:
807 for (i = 0; i < avctx->channels; i++)
808 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
810 for (i = 0; i < avctx->channels; i++) {
811 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
812 if (c->status[i].step_index > 88u) {
813 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
814 i, c->status[i].step_index);
815 return AVERROR_INVALIDDATA;
819 for (i = 0; i < avctx->channels; i++) {
820 samples = (int16_t *)frame->data[i];
822 for (n = nb_samples >> 1; n > 0; n--) {
823 int v = bytestream2_get_byteu(&gb);
824 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
825 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
829 case AV_CODEC_ID_ADPCM_MS:
833 block_predictor = bytestream2_get_byteu(&gb);
834 if (block_predictor > 6) {
835 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
837 return AVERROR_INVALIDDATA;
839 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
840 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
842 block_predictor = bytestream2_get_byteu(&gb);
843 if (block_predictor > 6) {
844 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
846 return AVERROR_INVALIDDATA;
848 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
849 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
851 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
853 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
856 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
857 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
858 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
859 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
861 *samples++ = c->status[0].sample2;
862 if (st) *samples++ = c->status[1].sample2;
863 *samples++ = c->status[0].sample1;
864 if (st) *samples++ = c->status[1].sample1;
865 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
866 int byte = bytestream2_get_byteu(&gb);
867 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
868 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
872 case AV_CODEC_ID_ADPCM_IMA_DK4:
873 for (channel = 0; channel < avctx->channels; channel++) {
874 cs = &c->status[channel];
875 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
876 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
877 if (cs->step_index > 88u){
878 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
879 channel, cs->step_index);
880 return AVERROR_INVALIDDATA;
883 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
884 int v = bytestream2_get_byteu(&gb);
885 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
886 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
889 case AV_CODEC_ID_ADPCM_IMA_DK3:
893 int decode_top_nibble_next = 0;
895 const int16_t *samples_end = samples + avctx->channels * nb_samples;
897 bytestream2_skipu(&gb, 10);
898 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
899 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
900 c->status[0].step_index = bytestream2_get_byteu(&gb);
901 c->status[1].step_index = bytestream2_get_byteu(&gb);
902 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
903 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
904 c->status[0].step_index, c->status[1].step_index);
905 return AVERROR_INVALIDDATA;
907 /* sign extend the predictors */
908 diff_channel = c->status[1].predictor;
910 /* DK3 ADPCM support macro */
911 #define DK3_GET_NEXT_NIBBLE() \
912 if (decode_top_nibble_next) { \
913 nibble = last_byte >> 4; \
914 decode_top_nibble_next = 0; \
916 last_byte = bytestream2_get_byteu(&gb); \
917 nibble = last_byte & 0x0F; \
918 decode_top_nibble_next = 1; \
921 while (samples < samples_end) {
923 /* for this algorithm, c->status[0] is the sum channel and
924 * c->status[1] is the diff channel */
926 /* process the first predictor of the sum channel */
927 DK3_GET_NEXT_NIBBLE();
928 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
930 /* process the diff channel predictor */
931 DK3_GET_NEXT_NIBBLE();
932 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
934 /* process the first pair of stereo PCM samples */
935 diff_channel = (diff_channel + c->status[1].predictor) / 2;
936 *samples++ = c->status[0].predictor + c->status[1].predictor;
937 *samples++ = c->status[0].predictor - c->status[1].predictor;
939 /* process the second predictor of the sum channel */
940 DK3_GET_NEXT_NIBBLE();
941 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
943 /* process the second pair of stereo PCM samples */
944 diff_channel = (diff_channel + c->status[1].predictor) / 2;
945 *samples++ = c->status[0].predictor + c->status[1].predictor;
946 *samples++ = c->status[0].predictor - c->status[1].predictor;
949 if ((bytestream2_tell(&gb) & 1))
950 bytestream2_skip(&gb, 1);
953 case AV_CODEC_ID_ADPCM_IMA_ISS:
954 for (channel = 0; channel < avctx->channels; channel++) {
955 cs = &c->status[channel];
956 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
957 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
958 if (cs->step_index > 88u){
959 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
960 channel, cs->step_index);
961 return AVERROR_INVALIDDATA;
965 for (n = nb_samples >> (1 - st); n > 0; n--) {
967 int v = bytestream2_get_byteu(&gb);
968 /* nibbles are swapped for mono */
976 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
977 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
980 case AV_CODEC_ID_ADPCM_IMA_APC:
981 while (bytestream2_get_bytes_left(&gb) > 0) {
982 int v = bytestream2_get_byteu(&gb);
983 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
984 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
987 case AV_CODEC_ID_ADPCM_IMA_OKI:
988 while (bytestream2_get_bytes_left(&gb) > 0) {
989 int v = bytestream2_get_byteu(&gb);
990 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
991 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
994 case AV_CODEC_ID_ADPCM_IMA_RAD:
995 for (channel = 0; channel < avctx->channels; channel++) {
996 cs = &c->status[channel];
997 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
998 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
999 if (cs->step_index > 88u){
1000 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1001 channel, cs->step_index);
1002 return AVERROR_INVALIDDATA;
1005 for (n = 0; n < nb_samples / 2; n++) {
1008 byte[0] = bytestream2_get_byteu(&gb);
1010 byte[1] = bytestream2_get_byteu(&gb);
1011 for(channel = 0; channel < avctx->channels; channel++) {
1012 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1014 for(channel = 0; channel < avctx->channels; channel++) {
1015 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1019 case AV_CODEC_ID_ADPCM_IMA_WS:
1020 if (c->vqa_version == 3) {
1021 for (channel = 0; channel < avctx->channels; channel++) {
1022 int16_t *smp = samples_p[channel];
1024 for (n = nb_samples / 2; n > 0; n--) {
1025 int v = bytestream2_get_byteu(&gb);
1026 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1027 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1031 for (n = nb_samples / 2; n > 0; n--) {
1032 for (channel = 0; channel < avctx->channels; channel++) {
1033 int v = bytestream2_get_byteu(&gb);
1034 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1035 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1037 samples += avctx->channels;
1040 bytestream2_seek(&gb, 0, SEEK_END);
1042 case AV_CODEC_ID_ADPCM_XA:
1044 int16_t *out0 = samples_p[0];
1045 int16_t *out1 = samples_p[1];
1046 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1047 int sample_offset = 0;
1048 while (bytestream2_get_bytes_left(&gb) >= 128) {
1049 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1050 &c->status[0], &c->status[1],
1051 avctx->channels, sample_offset)) < 0)
1053 bytestream2_skipu(&gb, 128);
1054 sample_offset += samples_per_block;
1058 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1059 for (i=0; i<=st; i++) {
1060 c->status[i].step_index = bytestream2_get_le32u(&gb);
1061 if (c->status[i].step_index > 88u) {
1062 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1063 i, c->status[i].step_index);
1064 return AVERROR_INVALIDDATA;
1067 for (i=0; i<=st; i++)
1068 c->status[i].predictor = bytestream2_get_le32u(&gb);
1070 for (n = nb_samples >> (1 - st); n > 0; n--) {
1071 int byte = bytestream2_get_byteu(&gb);
1072 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1073 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1076 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1077 for (n = nb_samples >> (1 - st); n > 0; n--) {
1078 int byte = bytestream2_get_byteu(&gb);
1079 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1080 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1083 case AV_CODEC_ID_ADPCM_EA:
1085 int previous_left_sample, previous_right_sample;
1086 int current_left_sample, current_right_sample;
1087 int next_left_sample, next_right_sample;
1088 int coeff1l, coeff2l, coeff1r, coeff2r;
1089 int shift_left, shift_right;
1091 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1092 each coding 28 stereo samples. */
1094 if(avctx->channels != 2)
1095 return AVERROR_INVALIDDATA;
1097 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1098 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1099 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1100 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1102 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1103 int byte = bytestream2_get_byteu(&gb);
1104 coeff1l = ea_adpcm_table[ byte >> 4 ];
1105 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1106 coeff1r = ea_adpcm_table[ byte & 0x0F];
1107 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1109 byte = bytestream2_get_byteu(&gb);
1110 shift_left = 20 - (byte >> 4);
1111 shift_right = 20 - (byte & 0x0F);
1113 for (count2 = 0; count2 < 28; count2++) {
1114 byte = bytestream2_get_byteu(&gb);
1115 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1116 next_right_sample = sign_extend(byte, 4) << shift_right;
1118 next_left_sample = (next_left_sample +
1119 (current_left_sample * coeff1l) +
1120 (previous_left_sample * coeff2l) + 0x80) >> 8;
1121 next_right_sample = (next_right_sample +
1122 (current_right_sample * coeff1r) +
1123 (previous_right_sample * coeff2r) + 0x80) >> 8;
1125 previous_left_sample = current_left_sample;
1126 current_left_sample = av_clip_int16(next_left_sample);
1127 previous_right_sample = current_right_sample;
1128 current_right_sample = av_clip_int16(next_right_sample);
1129 *samples++ = current_left_sample;
1130 *samples++ = current_right_sample;
1134 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1138 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1140 int coeff[2][2], shift[2];
1142 for(channel = 0; channel < avctx->channels; channel++) {
1143 int byte = bytestream2_get_byteu(&gb);
1145 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1146 shift[channel] = 20 - (byte & 0x0F);
1148 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1151 byte[0] = bytestream2_get_byteu(&gb);
1152 if (st) byte[1] = bytestream2_get_byteu(&gb);
1153 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1154 for(channel = 0; channel < avctx->channels; channel++) {
1155 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1157 c->status[channel].sample1 * coeff[channel][0] +
1158 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1159 c->status[channel].sample2 = c->status[channel].sample1;
1160 c->status[channel].sample1 = av_clip_int16(sample);
1161 *samples++ = c->status[channel].sample1;
1165 bytestream2_seek(&gb, 0, SEEK_END);
1168 case AV_CODEC_ID_ADPCM_EA_R1:
1169 case AV_CODEC_ID_ADPCM_EA_R2:
1170 case AV_CODEC_ID_ADPCM_EA_R3: {
1171 /* channel numbering
1173 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1174 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1175 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1176 int previous_sample, current_sample, next_sample;
1179 unsigned int channel;
1184 for (channel=0; channel<avctx->channels; channel++)
1185 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1186 bytestream2_get_le32(&gb)) +
1187 (avctx->channels + 1) * 4;
1189 for (channel=0; channel<avctx->channels; channel++) {
1190 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1191 samplesC = samples_p[channel];
1193 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1194 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1195 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1197 current_sample = c->status[channel].predictor;
1198 previous_sample = c->status[channel].prev_sample;
1201 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1202 int byte = bytestream2_get_byte(&gb);
1203 if (byte == 0xEE) { /* only seen in R2 and R3 */
1204 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1205 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1207 for (count2=0; count2<28; count2++)
1208 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1210 coeff1 = ea_adpcm_table[ byte >> 4 ];
1211 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1212 shift = 20 - (byte & 0x0F);
1214 for (count2=0; count2<28; count2++) {
1216 next_sample = sign_extend(byte, 4) << shift;
1218 byte = bytestream2_get_byte(&gb);
1219 next_sample = sign_extend(byte >> 4, 4) << shift;
1222 next_sample += (current_sample * coeff1) +
1223 (previous_sample * coeff2);
1224 next_sample = av_clip_int16(next_sample >> 8);
1226 previous_sample = current_sample;
1227 current_sample = next_sample;
1228 *samplesC++ = current_sample;
1234 } else if (count != count1) {
1235 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1236 count = FFMAX(count, count1);
1239 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1240 c->status[channel].predictor = current_sample;
1241 c->status[channel].prev_sample = previous_sample;
1245 frame->nb_samples = count * 28;
1246 bytestream2_seek(&gb, 0, SEEK_END);
1249 case AV_CODEC_ID_ADPCM_EA_XAS:
1250 for (channel=0; channel<avctx->channels; channel++) {
1251 int coeff[2][4], shift[4];
1252 int16_t *s = samples_p[channel];
1253 for (n = 0; n < 4; n++, s += 32) {
1254 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1256 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1259 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1260 shift[n] = 20 - (val & 0x0F);
1264 for (m=2; m<32; m+=2) {
1265 s = &samples_p[channel][m];
1266 for (n = 0; n < 4; n++, s += 32) {
1268 int byte = bytestream2_get_byteu(&gb);
1270 level = sign_extend(byte >> 4, 4) << shift[n];
1271 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1272 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1274 level = sign_extend(byte, 4) << shift[n];
1275 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1276 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1281 case AV_CODEC_ID_ADPCM_IMA_AMV:
1282 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1283 c->status[0].step_index = bytestream2_get_le16u(&gb);
1284 bytestream2_skipu(&gb, 4);
1285 if (c->status[0].step_index > 88u) {
1286 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1287 c->status[0].step_index);
1288 return AVERROR_INVALIDDATA;
1291 for (n = nb_samples >> (1 - st); n > 0; n--) {
1292 int v = bytestream2_get_byteu(&gb);
1294 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1295 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1298 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1299 for (i = 0; i < avctx->channels; i++) {
1300 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1301 c->status[i].step_index = bytestream2_get_byteu(&gb);
1302 bytestream2_skipu(&gb, 1);
1303 if (c->status[i].step_index > 88u) {
1304 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1305 c->status[i].step_index);
1306 return AVERROR_INVALIDDATA;
1310 for (n = nb_samples >> (1 - st); n > 0; n--) {
1311 int v = bytestream2_get_byteu(&gb);
1313 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1314 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1317 case AV_CODEC_ID_ADPCM_CT:
1318 for (n = nb_samples >> (1 - st); n > 0; n--) {
1319 int v = bytestream2_get_byteu(&gb);
1320 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1321 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1324 case AV_CODEC_ID_ADPCM_SBPRO_4:
1325 case AV_CODEC_ID_ADPCM_SBPRO_3:
1326 case AV_CODEC_ID_ADPCM_SBPRO_2:
1327 if (!c->status[0].step_index) {
1328 /* the first byte is a raw sample */
1329 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1331 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1332 c->status[0].step_index = 1;
1335 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1336 for (n = nb_samples >> (1 - st); n > 0; n--) {
1337 int byte = bytestream2_get_byteu(&gb);
1338 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1340 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1343 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1344 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1345 int byte = bytestream2_get_byteu(&gb);
1346 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1348 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1349 (byte >> 2) & 0x07, 3, 0);
1350 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1354 for (n = nb_samples >> (2 - st); n > 0; n--) {
1355 int byte = bytestream2_get_byteu(&gb);
1356 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1358 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1359 (byte >> 4) & 0x03, 2, 2);
1360 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1361 (byte >> 2) & 0x03, 2, 2);
1362 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1367 case AV_CODEC_ID_ADPCM_SWF:
1368 adpcm_swf_decode(avctx, buf, buf_size, samples);
1369 bytestream2_seek(&gb, 0, SEEK_END);
1371 case AV_CODEC_ID_ADPCM_YAMAHA:
1372 for (n = nb_samples >> (1 - st); n > 0; n--) {
1373 int v = bytestream2_get_byteu(&gb);
1374 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1375 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1378 case AV_CODEC_ID_ADPCM_AFC:
1380 int samples_per_block;
1383 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1384 samples_per_block = avctx->extradata[0] / 16;
1385 blocks = nb_samples / avctx->extradata[0];
1387 samples_per_block = nb_samples / 16;
1391 for (m = 0; m < blocks; m++) {
1392 for (channel = 0; channel < avctx->channels; channel++) {
1393 int prev1 = c->status[channel].sample1;
1394 int prev2 = c->status[channel].sample2;
1396 samples = samples_p[channel] + m * 16;
1397 /* Read in every sample for this channel. */
1398 for (i = 0; i < samples_per_block; i++) {
1399 int byte = bytestream2_get_byteu(&gb);
1400 int scale = 1 << (byte >> 4);
1401 int index = byte & 0xf;
1402 int factor1 = ff_adpcm_afc_coeffs[0][index];
1403 int factor2 = ff_adpcm_afc_coeffs[1][index];
1405 /* Decode 16 samples. */
1406 for (n = 0; n < 16; n++) {
1410 sampledat = sign_extend(byte, 4);
1412 byte = bytestream2_get_byteu(&gb);
1413 sampledat = sign_extend(byte >> 4, 4);
1416 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1417 ((sampledat * scale) << 11)) >> 11;
1418 *samples = av_clip_int16(sampledat);
1424 c->status[channel].sample1 = prev1;
1425 c->status[channel].sample2 = prev2;
1428 bytestream2_seek(&gb, 0, SEEK_END);
1431 case AV_CODEC_ID_ADPCM_THP:
1432 case AV_CODEC_ID_ADPCM_THP_LE:
1437 #define THP_GET16(g) \
1439 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1440 bytestream2_get_le16u(&(g)) : \
1441 bytestream2_get_be16u(&(g)), 16)
1443 if (avctx->extradata) {
1445 if (avctx->extradata_size < 32 * avctx->channels) {
1446 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1447 return AVERROR_INVALIDDATA;
1450 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1451 for (i = 0; i < avctx->channels; i++)
1452 for (n = 0; n < 16; n++)
1453 table[i][n] = THP_GET16(tb);
1455 for (i = 0; i < avctx->channels; i++)
1456 for (n = 0; n < 16; n++)
1457 table[i][n] = THP_GET16(gb);
1459 if (!c->has_status) {
1460 /* Initialize the previous sample. */
1461 for (i = 0; i < avctx->channels; i++) {
1462 c->status[i].sample1 = THP_GET16(gb);
1463 c->status[i].sample2 = THP_GET16(gb);
1467 bytestream2_skip(&gb, avctx->channels * 4);
1471 for (ch = 0; ch < avctx->channels; ch++) {
1472 samples = samples_p[ch];
1474 /* Read in every sample for this channel. */
1475 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1476 int byte = bytestream2_get_byteu(&gb);
1477 int index = (byte >> 4) & 7;
1478 unsigned int exp = byte & 0x0F;
1479 int factor1 = table[ch][index * 2];
1480 int factor2 = table[ch][index * 2 + 1];
1482 /* Decode 14 samples. */
1483 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1487 sampledat = sign_extend(byte, 4);
1489 byte = bytestream2_get_byteu(&gb);
1490 sampledat = sign_extend(byte >> 4, 4);
1493 sampledat = ((c->status[ch].sample1 * factor1
1494 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1495 *samples = av_clip_int16(sampledat);
1496 c->status[ch].sample2 = c->status[ch].sample1;
1497 c->status[ch].sample1 = *samples++;
1503 case AV_CODEC_ID_ADPCM_DTK:
1504 for (channel = 0; channel < avctx->channels; channel++) {
1505 samples = samples_p[channel];
1507 /* Read in every sample for this channel. */
1508 for (i = 0; i < nb_samples / 28; i++) {
1511 bytestream2_skipu(&gb, 1);
1512 header = bytestream2_get_byteu(&gb);
1513 bytestream2_skipu(&gb, 3 - channel);
1515 /* Decode 28 samples. */
1516 for (n = 0; n < 28; n++) {
1517 int32_t sampledat, prev;
1519 switch (header >> 4) {
1521 prev = (c->status[channel].sample1 * 0x3c);
1524 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1527 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1533 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1535 byte = bytestream2_get_byteu(&gb);
1537 sampledat = sign_extend(byte, 4);
1539 sampledat = sign_extend(byte >> 4, 4);
1541 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1542 *samples++ = av_clip_int16(sampledat >> 6);
1543 c->status[channel].sample2 = c->status[channel].sample1;
1544 c->status[channel].sample1 = sampledat;
1548 bytestream2_seek(&gb, 0, SEEK_SET);
1556 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1557 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1558 return AVERROR_INVALIDDATA;
1563 if (avpkt->size < bytestream2_tell(&gb)) {
1564 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1568 return bytestream2_tell(&gb);
1571 static void adpcm_flush(AVCodecContext *avctx)
1573 ADPCMDecodeContext *c = avctx->priv_data;
1578 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1579 AV_SAMPLE_FMT_NONE };
1580 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1581 AV_SAMPLE_FMT_NONE };
1582 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1584 AV_SAMPLE_FMT_NONE };
1586 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1587 AVCodec ff_ ## name_ ## _decoder = { \
1589 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1590 .type = AVMEDIA_TYPE_AUDIO, \
1592 .priv_data_size = sizeof(ADPCMDecodeContext), \
1593 .init = adpcm_decode_init, \
1594 .decode = adpcm_decode_frame, \
1595 .flush = adpcm_flush, \
1596 .capabilities = AV_CODEC_CAP_DR1, \
1597 .sample_fmts = sample_fmts_, \
1600 /* Note: Do not forget to add new entries to the Makefile as well. */
1601 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1602 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1603 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1604 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1605 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1606 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1607 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1608 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1609 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1610 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1611 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1612 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1613 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1614 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1615 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1616 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1617 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1618 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1619 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1620 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1621 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1622 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1623 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1624 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1625 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1626 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1627 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1628 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1629 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1630 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1631 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1632 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");