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[6];
88 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
91 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
93 ADPCMDecodeContext *c = avctx->priv_data;
94 unsigned int min_channels = 1;
95 unsigned int max_channels = 2;
97 switch(avctx->codec->id) {
98 case AV_CODEC_ID_ADPCM_DTK:
99 case AV_CODEC_ID_ADPCM_EA:
102 case AV_CODEC_ID_ADPCM_AFC:
103 case AV_CODEC_ID_ADPCM_EA_R1:
104 case AV_CODEC_ID_ADPCM_EA_R2:
105 case AV_CODEC_ID_ADPCM_EA_R3:
106 case AV_CODEC_ID_ADPCM_EA_XAS:
107 case AV_CODEC_ID_ADPCM_THP:
111 if (avctx->channels < min_channels || avctx->channels > max_channels) {
112 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
113 return AVERROR(EINVAL);
116 switch(avctx->codec->id) {
117 case AV_CODEC_ID_ADPCM_CT:
118 c->status[0].step = c->status[1].step = 511;
120 case AV_CODEC_ID_ADPCM_IMA_WAV:
121 if (avctx->bits_per_coded_sample != 4) {
122 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
126 case AV_CODEC_ID_ADPCM_IMA_APC:
127 if (avctx->extradata && avctx->extradata_size >= 8) {
128 c->status[0].predictor = AV_RL32(avctx->extradata);
129 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
132 case AV_CODEC_ID_ADPCM_IMA_WS:
133 if (avctx->extradata && avctx->extradata_size >= 2)
134 c->vqa_version = AV_RL16(avctx->extradata);
140 switch(avctx->codec->id) {
141 case AV_CODEC_ID_ADPCM_IMA_QT:
142 case AV_CODEC_ID_ADPCM_IMA_WAV:
143 case AV_CODEC_ID_ADPCM_4XM:
144 case AV_CODEC_ID_ADPCM_XA:
145 case AV_CODEC_ID_ADPCM_EA_R1:
146 case AV_CODEC_ID_ADPCM_EA_R2:
147 case AV_CODEC_ID_ADPCM_EA_R3:
148 case AV_CODEC_ID_ADPCM_EA_XAS:
149 case AV_CODEC_ID_ADPCM_THP:
150 case AV_CODEC_ID_ADPCM_AFC:
151 case AV_CODEC_ID_ADPCM_DTK:
152 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
154 case AV_CODEC_ID_ADPCM_IMA_WS:
155 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
159 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
165 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
169 int sign, delta, diff, step;
171 step = ff_adpcm_step_table[c->step_index];
172 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
173 step_index = av_clip(step_index, 0, 88);
177 /* perform direct multiplication instead of series of jumps proposed by
178 * the reference ADPCM implementation since modern CPUs can do the mults
180 diff = ((2 * delta + 1) * step) >> shift;
181 predictor = c->predictor;
182 if (sign) predictor -= diff;
183 else predictor += diff;
185 c->predictor = av_clip_int16(predictor);
186 c->step_index = step_index;
188 return (short)c->predictor;
191 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
197 step = ff_adpcm_step_table[c->step_index];
198 step_index = c->step_index + ff_adpcm_index_table[nibble];
199 step_index = av_clip(step_index, 0, 88);
202 if (nibble & 4) diff += step;
203 if (nibble & 2) diff += step >> 1;
204 if (nibble & 1) diff += step >> 2;
207 predictor = c->predictor - diff;
209 predictor = c->predictor + diff;
211 c->predictor = av_clip_int16(predictor);
212 c->step_index = step_index;
217 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
221 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
222 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
224 c->sample2 = c->sample1;
225 c->sample1 = av_clip_int16(predictor);
226 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
227 if (c->idelta < 16) c->idelta = 16;
232 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
234 int step_index, predictor, sign, delta, diff, step;
236 step = ff_adpcm_oki_step_table[c->step_index];
237 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
238 step_index = av_clip(step_index, 0, 48);
242 diff = ((2 * delta + 1) * step) >> 3;
243 predictor = c->predictor;
244 if (sign) predictor -= diff;
245 else predictor += diff;
247 c->predictor = av_clip(predictor, -2048, 2047);
248 c->step_index = step_index;
250 return c->predictor << 4;
253 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
255 int sign, delta, diff;
260 /* perform direct multiplication instead of series of jumps proposed by
261 * the reference ADPCM implementation since modern CPUs can do the mults
263 diff = ((2 * delta + 1) * c->step) >> 3;
264 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
265 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
266 c->predictor = av_clip_int16(c->predictor);
267 /* calculate new step and clamp it to range 511..32767 */
268 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
269 c->step = av_clip(new_step, 511, 32767);
271 return (short)c->predictor;
274 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
276 int sign, delta, diff;
278 sign = nibble & (1<<(size-1));
279 delta = nibble & ((1<<(size-1))-1);
280 diff = delta << (7 + c->step + shift);
283 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
285 /* calculate new step */
286 if (delta >= (2*size - 3) && c->step < 3)
288 else if (delta == 0 && c->step > 0)
291 return (short) c->predictor;
294 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
301 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
302 c->predictor = av_clip_int16(c->predictor);
303 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
304 c->step = av_clip(c->step, 127, 24567);
308 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
309 const uint8_t *in, ADPCMChannelStatus *left,
310 ADPCMChannelStatus *right, int channels, int sample_offset)
313 int shift,filter,f0,f1;
317 out0 += sample_offset;
321 out1 += sample_offset;
324 shift = 12 - (in[4+i*2] & 15);
325 filter = in[4+i*2] >> 4;
326 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
327 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
330 f0 = xa_adpcm_table[filter][0];
331 f1 = xa_adpcm_table[filter][1];
339 t = sign_extend(d, 4);
340 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
342 s_1 = av_clip_int16(s);
349 s_1 = right->sample1;
350 s_2 = right->sample2;
353 shift = 12 - (in[5+i*2] & 15);
354 filter = in[5+i*2] >> 4;
355 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
356 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
360 f0 = xa_adpcm_table[filter][0];
361 f1 = xa_adpcm_table[filter][1];
366 t = sign_extend(d >> 4, 4);
367 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
369 s_1 = av_clip_int16(s);
374 right->sample1 = s_1;
375 right->sample2 = s_2;
381 out0 += 28 * (3 - channels);
382 out1 += 28 * (3 - channels);
388 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
390 ADPCMDecodeContext *c = avctx->priv_data;
393 int k0, signmask, nb_bits, count;
394 int size = buf_size*8;
397 init_get_bits(&gb, buf, size);
399 //read bits & initial values
400 nb_bits = get_bits(&gb, 2)+2;
401 table = swf_index_tables[nb_bits-2];
402 k0 = 1 << (nb_bits-2);
403 signmask = 1 << (nb_bits-1);
405 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
406 for (i = 0; i < avctx->channels; i++) {
407 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
408 c->status[i].step_index = get_bits(&gb, 6);
411 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
414 for (i = 0; i < avctx->channels; i++) {
415 // similar to IMA adpcm
416 int delta = get_bits(&gb, nb_bits);
417 int step = ff_adpcm_step_table[c->status[i].step_index];
418 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
429 if (delta & signmask)
430 c->status[i].predictor -= vpdiff;
432 c->status[i].predictor += vpdiff;
434 c->status[i].step_index += table[delta & (~signmask)];
436 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
437 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
439 *samples++ = c->status[i].predictor;
446 * Get the number of samples that will be decoded from the packet.
447 * In one case, this is actually the maximum number of samples possible to
448 * decode with the given buf_size.
450 * @param[out] coded_samples set to the number of samples as coded in the
451 * packet, or 0 if the codec does not encode the
452 * number of samples in each frame.
454 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
455 int buf_size, int *coded_samples)
457 ADPCMDecodeContext *s = avctx->priv_data;
459 int ch = avctx->channels;
460 int has_coded_samples = 0;
468 switch (avctx->codec->id) {
469 /* constant, only check buf_size */
470 case AV_CODEC_ID_ADPCM_EA_XAS:
471 if (buf_size < 76 * ch)
475 case AV_CODEC_ID_ADPCM_IMA_QT:
476 if (buf_size < 34 * ch)
480 /* simple 4-bit adpcm */
481 case AV_CODEC_ID_ADPCM_CT:
482 case AV_CODEC_ID_ADPCM_IMA_APC:
483 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
484 case AV_CODEC_ID_ADPCM_IMA_OKI:
485 case AV_CODEC_ID_ADPCM_IMA_WS:
486 case AV_CODEC_ID_ADPCM_YAMAHA:
487 nb_samples = buf_size * 2 / ch;
493 /* simple 4-bit adpcm, with header */
495 switch (avctx->codec->id) {
496 case AV_CODEC_ID_ADPCM_4XM:
497 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
498 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
499 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
502 return (buf_size - header_size) * 2 / ch;
504 /* more complex formats */
505 switch (avctx->codec->id) {
506 case AV_CODEC_ID_ADPCM_EA:
507 has_coded_samples = 1;
508 *coded_samples = bytestream2_get_le32(gb);
509 *coded_samples -= *coded_samples % 28;
510 nb_samples = (buf_size - 12) / 30 * 28;
512 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
513 has_coded_samples = 1;
514 *coded_samples = bytestream2_get_le32(gb);
515 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
517 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
518 nb_samples = (buf_size - ch) / ch * 2;
520 case AV_CODEC_ID_ADPCM_EA_R1:
521 case AV_CODEC_ID_ADPCM_EA_R2:
522 case AV_CODEC_ID_ADPCM_EA_R3:
523 /* maximum number of samples */
524 /* has internal offsets and a per-frame switch to signal raw 16-bit */
525 has_coded_samples = 1;
526 switch (avctx->codec->id) {
527 case AV_CODEC_ID_ADPCM_EA_R1:
528 header_size = 4 + 9 * ch;
529 *coded_samples = bytestream2_get_le32(gb);
531 case AV_CODEC_ID_ADPCM_EA_R2:
532 header_size = 4 + 5 * ch;
533 *coded_samples = bytestream2_get_le32(gb);
535 case AV_CODEC_ID_ADPCM_EA_R3:
536 header_size = 4 + 5 * ch;
537 *coded_samples = bytestream2_get_be32(gb);
540 *coded_samples -= *coded_samples % 28;
541 nb_samples = (buf_size - header_size) * 2 / ch;
542 nb_samples -= nb_samples % 28;
544 case AV_CODEC_ID_ADPCM_IMA_DK3:
545 if (avctx->block_align > 0)
546 buf_size = FFMIN(buf_size, avctx->block_align);
547 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
549 case AV_CODEC_ID_ADPCM_IMA_DK4:
550 if (avctx->block_align > 0)
551 buf_size = FFMIN(buf_size, avctx->block_align);
552 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
554 case AV_CODEC_ID_ADPCM_IMA_RAD:
555 if (avctx->block_align > 0)
556 buf_size = FFMIN(buf_size, avctx->block_align);
557 nb_samples = (buf_size - 4 * ch) * 2 / ch;
559 case AV_CODEC_ID_ADPCM_IMA_WAV:
560 if (avctx->block_align > 0)
561 buf_size = FFMIN(buf_size, avctx->block_align);
562 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
564 case AV_CODEC_ID_ADPCM_MS:
565 if (avctx->block_align > 0)
566 buf_size = FFMIN(buf_size, avctx->block_align);
567 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
569 case AV_CODEC_ID_ADPCM_SBPRO_2:
570 case AV_CODEC_ID_ADPCM_SBPRO_3:
571 case AV_CODEC_ID_ADPCM_SBPRO_4:
573 int samples_per_byte;
574 switch (avctx->codec->id) {
575 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
576 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
577 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
579 if (!s->status[0].step_index) {
583 nb_samples += buf_size * samples_per_byte / ch;
586 case AV_CODEC_ID_ADPCM_SWF:
588 int buf_bits = buf_size * 8 - 2;
589 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
590 int block_hdr_size = 22 * ch;
591 int block_size = block_hdr_size + nbits * ch * 4095;
592 int nblocks = buf_bits / block_size;
593 int bits_left = buf_bits - nblocks * block_size;
594 nb_samples = nblocks * 4096;
595 if (bits_left >= block_hdr_size)
596 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
599 case AV_CODEC_ID_ADPCM_THP:
600 if (avctx->extradata) {
601 nb_samples = buf_size / (8 * ch) * 14;
604 has_coded_samples = 1;
605 bytestream2_skip(gb, 4); // channel size
606 *coded_samples = bytestream2_get_be32(gb);
607 *coded_samples -= *coded_samples % 14;
608 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
610 case AV_CODEC_ID_ADPCM_AFC:
611 nb_samples = buf_size / (9 * ch) * 16;
613 case AV_CODEC_ID_ADPCM_XA:
614 nb_samples = (buf_size / 128) * 224 / ch;
616 case AV_CODEC_ID_ADPCM_DTK:
617 nb_samples = buf_size / (16 * ch) * 28;
621 /* validate coded sample count */
622 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
623 return AVERROR_INVALIDDATA;
628 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
629 int *got_frame_ptr, AVPacket *avpkt)
631 AVFrame *frame = data;
632 const uint8_t *buf = avpkt->data;
633 int buf_size = avpkt->size;
634 ADPCMDecodeContext *c = avctx->priv_data;
635 ADPCMChannelStatus *cs;
636 int n, m, channel, i;
641 int nb_samples, coded_samples, ret;
644 bytestream2_init(&gb, buf, buf_size);
645 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
646 if (nb_samples <= 0) {
647 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
648 return AVERROR_INVALIDDATA;
651 /* get output buffer */
652 frame->nb_samples = nb_samples;
653 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
655 samples = (short *)frame->data[0];
656 samples_p = (int16_t **)frame->extended_data;
658 /* use coded_samples when applicable */
659 /* it is always <= nb_samples, so the output buffer will be large enough */
661 if (coded_samples != nb_samples)
662 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
663 frame->nb_samples = nb_samples = coded_samples;
666 st = avctx->channels == 2 ? 1 : 0;
668 switch(avctx->codec->id) {
669 case AV_CODEC_ID_ADPCM_IMA_QT:
670 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
671 Channel data is interleaved per-chunk. */
672 for (channel = 0; channel < avctx->channels; channel++) {
675 cs = &(c->status[channel]);
676 /* (pppppp) (piiiiiii) */
678 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
679 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
680 step_index = predictor & 0x7F;
683 if (cs->step_index == step_index) {
684 int diff = predictor - cs->predictor;
691 cs->step_index = step_index;
692 cs->predictor = predictor;
695 if (cs->step_index > 88u){
696 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
697 channel, cs->step_index);
698 return AVERROR_INVALIDDATA;
701 samples = samples_p[channel];
703 for (m = 0; m < 64; m += 2) {
704 int byte = bytestream2_get_byteu(&gb);
705 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
706 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
710 case AV_CODEC_ID_ADPCM_IMA_WAV:
711 for(i=0; i<avctx->channels; i++){
712 cs = &(c->status[i]);
713 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
715 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
716 if (cs->step_index > 88u){
717 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
719 return AVERROR_INVALIDDATA;
723 for (n = 0; n < (nb_samples - 1) / 8; n++) {
724 for (i = 0; i < avctx->channels; i++) {
726 samples = &samples_p[i][1 + n * 8];
727 for (m = 0; m < 8; m += 2) {
728 int v = bytestream2_get_byteu(&gb);
729 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
730 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
735 case AV_CODEC_ID_ADPCM_4XM:
736 for (i = 0; i < avctx->channels; i++)
737 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
739 for (i = 0; i < avctx->channels; i++) {
740 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
741 if (c->status[i].step_index > 88u) {
742 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
743 i, c->status[i].step_index);
744 return AVERROR_INVALIDDATA;
748 for (i = 0; i < avctx->channels; i++) {
749 samples = (int16_t *)frame->data[i];
751 for (n = nb_samples >> 1; n > 0; n--) {
752 int v = bytestream2_get_byteu(&gb);
753 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
754 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
758 case AV_CODEC_ID_ADPCM_MS:
762 block_predictor = bytestream2_get_byteu(&gb);
763 if (block_predictor > 6) {
764 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
766 return AVERROR_INVALIDDATA;
768 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
769 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
771 block_predictor = bytestream2_get_byteu(&gb);
772 if (block_predictor > 6) {
773 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
775 return AVERROR_INVALIDDATA;
777 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
778 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
780 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
782 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
785 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
786 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
787 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
788 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
790 *samples++ = c->status[0].sample2;
791 if (st) *samples++ = c->status[1].sample2;
792 *samples++ = c->status[0].sample1;
793 if (st) *samples++ = c->status[1].sample1;
794 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
795 int byte = bytestream2_get_byteu(&gb);
796 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
797 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
801 case AV_CODEC_ID_ADPCM_IMA_DK4:
802 for (channel = 0; channel < avctx->channels; channel++) {
803 cs = &c->status[channel];
804 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
805 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
806 if (cs->step_index > 88u){
807 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
808 channel, cs->step_index);
809 return AVERROR_INVALIDDATA;
812 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
813 int v = bytestream2_get_byteu(&gb);
814 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
815 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
818 case AV_CODEC_ID_ADPCM_IMA_DK3:
822 int decode_top_nibble_next = 0;
824 const int16_t *samples_end = samples + avctx->channels * nb_samples;
826 bytestream2_skipu(&gb, 10);
827 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
828 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
829 c->status[0].step_index = bytestream2_get_byteu(&gb);
830 c->status[1].step_index = bytestream2_get_byteu(&gb);
831 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
832 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
833 c->status[0].step_index, c->status[1].step_index);
834 return AVERROR_INVALIDDATA;
836 /* sign extend the predictors */
837 diff_channel = c->status[1].predictor;
839 /* DK3 ADPCM support macro */
840 #define DK3_GET_NEXT_NIBBLE() \
841 if (decode_top_nibble_next) { \
842 nibble = last_byte >> 4; \
843 decode_top_nibble_next = 0; \
845 last_byte = bytestream2_get_byteu(&gb); \
846 nibble = last_byte & 0x0F; \
847 decode_top_nibble_next = 1; \
850 while (samples < samples_end) {
852 /* for this algorithm, c->status[0] is the sum channel and
853 * c->status[1] is the diff channel */
855 /* process the first predictor of the sum channel */
856 DK3_GET_NEXT_NIBBLE();
857 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
859 /* process the diff channel predictor */
860 DK3_GET_NEXT_NIBBLE();
861 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
863 /* process the first pair of stereo PCM samples */
864 diff_channel = (diff_channel + c->status[1].predictor) / 2;
865 *samples++ = c->status[0].predictor + c->status[1].predictor;
866 *samples++ = c->status[0].predictor - c->status[1].predictor;
868 /* process the second predictor of the sum channel */
869 DK3_GET_NEXT_NIBBLE();
870 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
872 /* process the second pair of stereo PCM samples */
873 diff_channel = (diff_channel + c->status[1].predictor) / 2;
874 *samples++ = c->status[0].predictor + c->status[1].predictor;
875 *samples++ = c->status[0].predictor - c->status[1].predictor;
879 case AV_CODEC_ID_ADPCM_IMA_ISS:
880 for (channel = 0; channel < avctx->channels; channel++) {
881 cs = &c->status[channel];
882 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
883 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
884 if (cs->step_index > 88u){
885 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
886 channel, cs->step_index);
887 return AVERROR_INVALIDDATA;
891 for (n = nb_samples >> (1 - st); n > 0; n--) {
893 int v = bytestream2_get_byteu(&gb);
894 /* nibbles are swapped for mono */
902 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
903 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
906 case AV_CODEC_ID_ADPCM_IMA_APC:
907 while (bytestream2_get_bytes_left(&gb) > 0) {
908 int v = bytestream2_get_byteu(&gb);
909 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
910 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
913 case AV_CODEC_ID_ADPCM_IMA_OKI:
914 while (bytestream2_get_bytes_left(&gb) > 0) {
915 int v = bytestream2_get_byteu(&gb);
916 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
917 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
920 case AV_CODEC_ID_ADPCM_IMA_RAD:
921 for (channel = 0; channel < avctx->channels; channel++) {
922 cs = &c->status[channel];
923 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
924 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
925 if (cs->step_index > 88u){
926 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
927 channel, cs->step_index);
928 return AVERROR_INVALIDDATA;
931 for (n = 0; n < nb_samples / 2; n++) {
934 byte[0] = bytestream2_get_byteu(&gb);
936 byte[1] = bytestream2_get_byteu(&gb);
937 for(channel = 0; channel < avctx->channels; channel++) {
938 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
940 for(channel = 0; channel < avctx->channels; channel++) {
941 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
945 case AV_CODEC_ID_ADPCM_IMA_WS:
946 if (c->vqa_version == 3) {
947 for (channel = 0; channel < avctx->channels; channel++) {
948 int16_t *smp = samples_p[channel];
950 for (n = nb_samples / 2; n > 0; n--) {
951 int v = bytestream2_get_byteu(&gb);
952 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
953 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
957 for (n = nb_samples / 2; n > 0; n--) {
958 for (channel = 0; channel < avctx->channels; channel++) {
959 int v = bytestream2_get_byteu(&gb);
960 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
961 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
963 samples += avctx->channels;
966 bytestream2_seek(&gb, 0, SEEK_END);
968 case AV_CODEC_ID_ADPCM_XA:
970 int16_t *out0 = samples_p[0];
971 int16_t *out1 = samples_p[1];
972 int samples_per_block = 28 * (3 - avctx->channels) * 4;
973 int sample_offset = 0;
974 while (bytestream2_get_bytes_left(&gb) >= 128) {
975 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
976 &c->status[0], &c->status[1],
977 avctx->channels, sample_offset)) < 0)
979 bytestream2_skipu(&gb, 128);
980 sample_offset += samples_per_block;
984 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
985 for (i=0; i<=st; i++) {
986 c->status[i].step_index = bytestream2_get_le32u(&gb);
987 if (c->status[i].step_index > 88u) {
988 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
989 i, c->status[i].step_index);
990 return AVERROR_INVALIDDATA;
993 for (i=0; i<=st; i++)
994 c->status[i].predictor = bytestream2_get_le32u(&gb);
996 for (n = nb_samples >> (1 - st); n > 0; n--) {
997 int byte = bytestream2_get_byteu(&gb);
998 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
999 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1002 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1003 for (n = nb_samples >> (1 - st); n > 0; n--) {
1004 int byte = bytestream2_get_byteu(&gb);
1005 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1006 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1009 case AV_CODEC_ID_ADPCM_EA:
1011 int previous_left_sample, previous_right_sample;
1012 int current_left_sample, current_right_sample;
1013 int next_left_sample, next_right_sample;
1014 int coeff1l, coeff2l, coeff1r, coeff2r;
1015 int shift_left, shift_right;
1017 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1018 each coding 28 stereo samples. */
1020 if(avctx->channels != 2)
1021 return AVERROR_INVALIDDATA;
1023 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1024 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1025 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1026 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1028 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1029 int byte = bytestream2_get_byteu(&gb);
1030 coeff1l = ea_adpcm_table[ byte >> 4 ];
1031 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1032 coeff1r = ea_adpcm_table[ byte & 0x0F];
1033 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1035 byte = bytestream2_get_byteu(&gb);
1036 shift_left = 20 - (byte >> 4);
1037 shift_right = 20 - (byte & 0x0F);
1039 for (count2 = 0; count2 < 28; count2++) {
1040 byte = bytestream2_get_byteu(&gb);
1041 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1042 next_right_sample = sign_extend(byte, 4) << shift_right;
1044 next_left_sample = (next_left_sample +
1045 (current_left_sample * coeff1l) +
1046 (previous_left_sample * coeff2l) + 0x80) >> 8;
1047 next_right_sample = (next_right_sample +
1048 (current_right_sample * coeff1r) +
1049 (previous_right_sample * coeff2r) + 0x80) >> 8;
1051 previous_left_sample = current_left_sample;
1052 current_left_sample = av_clip_int16(next_left_sample);
1053 previous_right_sample = current_right_sample;
1054 current_right_sample = av_clip_int16(next_right_sample);
1055 *samples++ = current_left_sample;
1056 *samples++ = current_right_sample;
1060 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1064 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1066 int coeff[2][2], shift[2];
1068 for(channel = 0; channel < avctx->channels; channel++) {
1069 int byte = bytestream2_get_byteu(&gb);
1071 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1072 shift[channel] = 20 - (byte & 0x0F);
1074 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1077 byte[0] = bytestream2_get_byteu(&gb);
1078 if (st) byte[1] = bytestream2_get_byteu(&gb);
1079 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1080 for(channel = 0; channel < avctx->channels; channel++) {
1081 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1083 c->status[channel].sample1 * coeff[channel][0] +
1084 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1085 c->status[channel].sample2 = c->status[channel].sample1;
1086 c->status[channel].sample1 = av_clip_int16(sample);
1087 *samples++ = c->status[channel].sample1;
1091 bytestream2_seek(&gb, 0, SEEK_END);
1094 case AV_CODEC_ID_ADPCM_EA_R1:
1095 case AV_CODEC_ID_ADPCM_EA_R2:
1096 case AV_CODEC_ID_ADPCM_EA_R3: {
1097 /* channel numbering
1099 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1100 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1101 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1102 int previous_sample, current_sample, next_sample;
1105 unsigned int channel;
1110 for (channel=0; channel<avctx->channels; channel++)
1111 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1112 bytestream2_get_le32(&gb)) +
1113 (avctx->channels + 1) * 4;
1115 for (channel=0; channel<avctx->channels; channel++) {
1116 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1117 samplesC = samples_p[channel];
1119 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1120 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1121 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1123 current_sample = c->status[channel].predictor;
1124 previous_sample = c->status[channel].prev_sample;
1127 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1128 int byte = bytestream2_get_byte(&gb);
1129 if (byte == 0xEE) { /* only seen in R2 and R3 */
1130 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1131 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1133 for (count2=0; count2<28; count2++)
1134 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1136 coeff1 = ea_adpcm_table[ byte >> 4 ];
1137 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1138 shift = 20 - (byte & 0x0F);
1140 for (count2=0; count2<28; count2++) {
1142 next_sample = sign_extend(byte, 4) << shift;
1144 byte = bytestream2_get_byte(&gb);
1145 next_sample = sign_extend(byte >> 4, 4) << shift;
1148 next_sample += (current_sample * coeff1) +
1149 (previous_sample * coeff2);
1150 next_sample = av_clip_int16(next_sample >> 8);
1152 previous_sample = current_sample;
1153 current_sample = next_sample;
1154 *samplesC++ = current_sample;
1160 } else if (count != count1) {
1161 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1162 count = FFMAX(count, count1);
1165 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1166 c->status[channel].predictor = current_sample;
1167 c->status[channel].prev_sample = previous_sample;
1171 frame->nb_samples = count * 28;
1172 bytestream2_seek(&gb, 0, SEEK_END);
1175 case AV_CODEC_ID_ADPCM_EA_XAS:
1176 for (channel=0; channel<avctx->channels; channel++) {
1177 int coeff[2][4], shift[4];
1178 int16_t *s = samples_p[channel];
1179 for (n = 0; n < 4; n++, s += 32) {
1180 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1182 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1185 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1186 shift[n] = 20 - (val & 0x0F);
1190 for (m=2; m<32; m+=2) {
1191 s = &samples_p[channel][m];
1192 for (n = 0; n < 4; n++, s += 32) {
1194 int byte = bytestream2_get_byteu(&gb);
1196 level = sign_extend(byte >> 4, 4) << shift[n];
1197 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1198 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1200 level = sign_extend(byte, 4) << shift[n];
1201 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1202 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1207 case AV_CODEC_ID_ADPCM_IMA_AMV:
1208 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1209 c->status[0].step_index = bytestream2_get_le16u(&gb);
1210 bytestream2_skipu(&gb, 4);
1211 if (c->status[0].step_index > 88u) {
1212 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1213 c->status[0].step_index);
1214 return AVERROR_INVALIDDATA;
1217 for (n = nb_samples >> (1 - st); n > 0; n--) {
1218 int v = bytestream2_get_byteu(&gb);
1220 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1221 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1224 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1225 for (i = 0; i < avctx->channels; i++) {
1226 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1227 c->status[i].step_index = bytestream2_get_byteu(&gb);
1228 bytestream2_skipu(&gb, 1);
1229 if (c->status[i].step_index > 88u) {
1230 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1231 c->status[i].step_index);
1232 return AVERROR_INVALIDDATA;
1236 for (n = nb_samples >> (1 - st); n > 0; n--) {
1237 int v = bytestream2_get_byteu(&gb);
1239 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1240 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1243 case AV_CODEC_ID_ADPCM_CT:
1244 for (n = nb_samples >> (1 - st); n > 0; n--) {
1245 int v = bytestream2_get_byteu(&gb);
1246 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1247 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1250 case AV_CODEC_ID_ADPCM_SBPRO_4:
1251 case AV_CODEC_ID_ADPCM_SBPRO_3:
1252 case AV_CODEC_ID_ADPCM_SBPRO_2:
1253 if (!c->status[0].step_index) {
1254 /* the first byte is a raw sample */
1255 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1257 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1258 c->status[0].step_index = 1;
1261 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1262 for (n = nb_samples >> (1 - st); n > 0; n--) {
1263 int byte = bytestream2_get_byteu(&gb);
1264 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1266 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1269 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1270 for (n = nb_samples / 3; n > 0; n--) {
1271 int byte = bytestream2_get_byteu(&gb);
1272 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1274 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1275 (byte >> 2) & 0x07, 3, 0);
1276 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1280 for (n = nb_samples >> (2 - st); n > 0; n--) {
1281 int byte = bytestream2_get_byteu(&gb);
1282 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1284 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1285 (byte >> 4) & 0x03, 2, 2);
1286 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1287 (byte >> 2) & 0x03, 2, 2);
1288 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1293 case AV_CODEC_ID_ADPCM_SWF:
1294 adpcm_swf_decode(avctx, buf, buf_size, samples);
1295 bytestream2_seek(&gb, 0, SEEK_END);
1297 case AV_CODEC_ID_ADPCM_YAMAHA:
1298 for (n = nb_samples >> (1 - st); n > 0; n--) {
1299 int v = bytestream2_get_byteu(&gb);
1300 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1301 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1304 case AV_CODEC_ID_ADPCM_AFC:
1306 int samples_per_block;
1309 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1310 samples_per_block = avctx->extradata[0] / 16;
1311 blocks = nb_samples / avctx->extradata[0];
1313 samples_per_block = nb_samples / 16;
1317 for (m = 0; m < blocks; m++) {
1318 for (channel = 0; channel < avctx->channels; channel++) {
1319 int prev1 = c->status[channel].sample1;
1320 int prev2 = c->status[channel].sample2;
1322 samples = samples_p[channel] + m * 16;
1323 /* Read in every sample for this channel. */
1324 for (i = 0; i < samples_per_block; i++) {
1325 int byte = bytestream2_get_byteu(&gb);
1326 int scale = 1 << (byte >> 4);
1327 int index = byte & 0xf;
1328 int factor1 = ff_adpcm_afc_coeffs[0][index];
1329 int factor2 = ff_adpcm_afc_coeffs[1][index];
1331 /* Decode 16 samples. */
1332 for (n = 0; n < 16; n++) {
1336 sampledat = sign_extend(byte, 4);
1338 byte = bytestream2_get_byteu(&gb);
1339 sampledat = sign_extend(byte >> 4, 4);
1342 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1343 ((sampledat * scale) << 11)) >> 11;
1344 *samples = av_clip_int16(sampledat);
1350 c->status[channel].sample1 = prev1;
1351 c->status[channel].sample2 = prev2;
1354 bytestream2_seek(&gb, 0, SEEK_END);
1357 case AV_CODEC_ID_ADPCM_THP:
1362 if (avctx->extradata) {
1364 if (avctx->extradata_size < 32 * avctx->channels) {
1365 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1366 return AVERROR_INVALIDDATA;
1369 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1370 for (i = 0; i < avctx->channels; i++)
1371 for (n = 0; n < 16; n++)
1372 table[i][n] = sign_extend(bytestream2_get_be16u(&tb), 16);
1374 for (i = 0; i < avctx->channels; i++)
1375 for (n = 0; n < 16; n++)
1376 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1378 /* Initialize the previous sample. */
1379 for (i = 0; i < avctx->channels; i++) {
1380 c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1381 c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1385 for (ch = 0; ch < avctx->channels; ch++) {
1386 samples = samples_p[ch];
1388 /* Read in every sample for this channel. */
1389 for (i = 0; i < nb_samples / 14; i++) {
1390 int byte = bytestream2_get_byteu(&gb);
1391 int index = (byte >> 4) & 7;
1392 unsigned int exp = byte & 0x0F;
1393 int factor1 = table[ch][index * 2];
1394 int factor2 = table[ch][index * 2 + 1];
1396 /* Decode 14 samples. */
1397 for (n = 0; n < 14; n++) {
1401 sampledat = sign_extend(byte, 4);
1403 byte = bytestream2_get_byteu(&gb);
1404 sampledat = sign_extend(byte >> 4, 4);
1407 sampledat = ((c->status[ch].sample1 * factor1
1408 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1409 *samples = av_clip_int16(sampledat);
1410 c->status[ch].sample2 = c->status[ch].sample1;
1411 c->status[ch].sample1 = *samples++;
1417 case AV_CODEC_ID_ADPCM_DTK:
1418 for (channel = 0; channel < avctx->channels; channel++) {
1419 samples = samples_p[channel];
1421 /* Read in every sample for this channel. */
1422 for (i = 0; i < nb_samples / 28; i++) {
1425 bytestream2_skipu(&gb, 1);
1426 header = bytestream2_get_byteu(&gb);
1427 bytestream2_skipu(&gb, 3 - channel);
1429 /* Decode 28 samples. */
1430 for (n = 0; n < 28; n++) {
1431 int32_t sampledat, prev;
1433 switch (header >> 4) {
1435 prev = (c->status[channel].sample1 * 0x3c);
1438 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1441 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1447 prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1449 byte = bytestream2_get_byteu(&gb);
1451 sampledat = sign_extend(byte, 4);
1453 sampledat = sign_extend(byte >> 4, 4);
1455 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1456 *samples++ = av_clip_int16(sampledat >> 6);
1457 c->status[channel].sample2 = c->status[channel].sample1;
1458 c->status[channel].sample1 = sampledat;
1462 bytestream2_seek(&gb, 0, SEEK_SET);
1470 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1471 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1472 return AVERROR_INVALIDDATA;
1477 return bytestream2_tell(&gb);
1481 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1482 AV_SAMPLE_FMT_NONE };
1483 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
1484 AV_SAMPLE_FMT_NONE };
1485 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1487 AV_SAMPLE_FMT_NONE };
1489 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1490 AVCodec ff_ ## name_ ## _decoder = { \
1492 .type = AVMEDIA_TYPE_AUDIO, \
1494 .priv_data_size = sizeof(ADPCMDecodeContext), \
1495 .init = adpcm_decode_init, \
1496 .decode = adpcm_decode_frame, \
1497 .capabilities = CODEC_CAP_DR1, \
1498 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1499 .sample_fmts = sample_fmts_, \
1502 /* Note: Do not forget to add new entries to the Makefile as well. */
1503 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1504 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1505 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1506 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1507 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1508 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1509 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1510 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1511 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1512 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1513 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1514 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1515 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1516 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1517 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1518 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1519 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1520 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1521 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1522 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1523 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1524 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1525 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1526 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1527 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1528 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1529 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1530 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1531 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1532 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1533 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");