2 * Copyright (c) 2001-2003 The ffmpeg Project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
25 #include "adpcm_data.h"
31 * First version by Francois Revol (revol@free.fr)
32 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
33 * by Mike Melanson (melanson@pcisys.net)
34 * CD-ROM XA ADPCM codec by BERO
35 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
36 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
37 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
38 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
39 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
40 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
41 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
43 * Features and limitations:
45 * Reference documents:
46 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
47 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
48 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
49 * http://openquicktime.sourceforge.net/
50 * XAnim sources (xa_codec.c) http://xanim.polter.net/
51 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
52 * SoX source code http://sox.sourceforge.net/
55 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
56 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
57 * readstr http://www.geocities.co.jp/Playtown/2004/
60 /* These are for CD-ROM XA ADPCM */
61 static const int xa_adpcm_table[5][2] = {
69 static const int ea_adpcm_table[] = {
77 // padded to zero where table size is less then 16
78 static const int swf_index_tables[4][16] = {
80 /*3*/ { -1, -1, 2, 4 },
81 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
82 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
87 typedef struct ADPCMDecodeContext {
89 ADPCMChannelStatus status[6];
90 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
93 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
95 ADPCMDecodeContext *c = avctx->priv_data;
96 unsigned int min_channels = 1;
97 unsigned int max_channels = 2;
99 switch(avctx->codec->id) {
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:
108 case AV_CODEC_ID_ADPCM_THP:
112 if (avctx->channels < min_channels || avctx->channels > max_channels) {
113 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
114 return AVERROR(EINVAL);
117 switch(avctx->codec->id) {
118 case AV_CODEC_ID_ADPCM_CT:
119 c->status[0].step = c->status[1].step = 511;
121 case AV_CODEC_ID_ADPCM_IMA_WAV:
122 if (avctx->bits_per_coded_sample != 4) {
123 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
127 case AV_CODEC_ID_ADPCM_IMA_APC:
128 if (avctx->extradata && avctx->extradata_size >= 8) {
129 c->status[0].predictor = AV_RL32(avctx->extradata);
130 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
133 case AV_CODEC_ID_ADPCM_IMA_WS:
134 if (avctx->extradata && avctx->extradata_size >= 2)
135 c->vqa_version = AV_RL16(avctx->extradata);
141 switch(avctx->codec->id) {
142 case AV_CODEC_ID_ADPCM_IMA_QT:
143 case AV_CODEC_ID_ADPCM_IMA_WAV:
144 case AV_CODEC_ID_ADPCM_4XM:
145 case AV_CODEC_ID_ADPCM_XA:
146 case AV_CODEC_ID_ADPCM_EA_R1:
147 case AV_CODEC_ID_ADPCM_EA_R2:
148 case AV_CODEC_ID_ADPCM_EA_R3:
149 case AV_CODEC_ID_ADPCM_EA_XAS:
150 case AV_CODEC_ID_ADPCM_THP:
151 case AV_CODEC_ID_ADPCM_AFC:
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;
162 avcodec_get_frame_defaults(&c->frame);
163 avctx->coded_frame = &c->frame;
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 int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
200 step = ff_adpcm_step_table[c->step_index];
201 step_index = c->step_index + ff_adpcm_index_table[nibble];
202 step_index = av_clip(step_index, 0, 88);
205 if (nibble & 4) diff += step;
206 if (nibble & 2) diff += step >> 1;
207 if (nibble & 1) diff += step >> 2;
210 predictor = c->predictor - diff;
212 predictor = c->predictor + diff;
214 c->predictor = av_clip_int16(predictor);
215 c->step_index = step_index;
220 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
224 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
225 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
227 c->sample2 = c->sample1;
228 c->sample1 = av_clip_int16(predictor);
229 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
230 if (c->idelta < 16) c->idelta = 16;
235 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
237 int step_index, predictor, sign, delta, diff, step;
239 step = ff_adpcm_oki_step_table[c->step_index];
240 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
241 step_index = av_clip(step_index, 0, 48);
245 diff = ((2 * delta + 1) * step) >> 3;
246 predictor = c->predictor;
247 if (sign) predictor -= diff;
248 else predictor += diff;
250 c->predictor = av_clip(predictor, -2048, 2047);
251 c->step_index = step_index;
253 return c->predictor << 4;
256 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
258 int sign, delta, diff;
263 /* perform direct multiplication instead of series of jumps proposed by
264 * the reference ADPCM implementation since modern CPUs can do the mults
266 diff = ((2 * delta + 1) * c->step) >> 3;
267 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
268 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
269 c->predictor = av_clip_int16(c->predictor);
270 /* calculate new step and clamp it to range 511..32767 */
271 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
272 c->step = av_clip(new_step, 511, 32767);
274 return (short)c->predictor;
277 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
279 int sign, delta, diff;
281 sign = nibble & (1<<(size-1));
282 delta = nibble & ((1<<(size-1))-1);
283 diff = delta << (7 + c->step + shift);
286 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
288 /* calculate new step */
289 if (delta >= (2*size - 3) && c->step < 3)
291 else if (delta == 0 && c->step > 0)
294 return (short) c->predictor;
297 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
304 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
305 c->predictor = av_clip_int16(c->predictor);
306 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
307 c->step = av_clip(c->step, 127, 24567);
311 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
312 const uint8_t *in, ADPCMChannelStatus *left,
313 ADPCMChannelStatus *right, int channels, int sample_offset)
316 int shift,filter,f0,f1;
320 out0 += sample_offset;
324 out1 += sample_offset;
327 shift = 12 - (in[4+i*2] & 15);
328 filter = in[4+i*2] >> 4;
329 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
330 av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
333 f0 = xa_adpcm_table[filter][0];
334 f1 = xa_adpcm_table[filter][1];
342 t = sign_extend(d, 4);
343 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
345 s_1 = av_clip_int16(s);
352 s_1 = right->sample1;
353 s_2 = right->sample2;
356 shift = 12 - (in[5+i*2] & 15);
357 filter = in[5+i*2] >> 4;
358 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
359 av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
363 f0 = xa_adpcm_table[filter][0];
364 f1 = xa_adpcm_table[filter][1];
369 t = sign_extend(d >> 4, 4);
370 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
372 s_1 = av_clip_int16(s);
377 right->sample1 = s_1;
378 right->sample2 = s_2;
384 out0 += 28 * (3 - channels);
385 out1 += 28 * (3 - channels);
391 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
393 ADPCMDecodeContext *c = avctx->priv_data;
396 int k0, signmask, nb_bits, count;
397 int size = buf_size*8;
400 init_get_bits(&gb, buf, size);
402 //read bits & initial values
403 nb_bits = get_bits(&gb, 2)+2;
404 table = swf_index_tables[nb_bits-2];
405 k0 = 1 << (nb_bits-2);
406 signmask = 1 << (nb_bits-1);
408 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
409 for (i = 0; i < avctx->channels; i++) {
410 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
411 c->status[i].step_index = get_bits(&gb, 6);
414 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
417 for (i = 0; i < avctx->channels; i++) {
418 // similar to IMA adpcm
419 int delta = get_bits(&gb, nb_bits);
420 int step = ff_adpcm_step_table[c->status[i].step_index];
421 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
432 if (delta & signmask)
433 c->status[i].predictor -= vpdiff;
435 c->status[i].predictor += vpdiff;
437 c->status[i].step_index += table[delta & (~signmask)];
439 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
440 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
442 *samples++ = c->status[i].predictor;
449 * Get the number of samples that will be decoded from the packet.
450 * In one case, this is actually the maximum number of samples possible to
451 * decode with the given buf_size.
453 * @param[out] coded_samples set to the number of samples as coded in the
454 * packet, or 0 if the codec does not encode the
455 * number of samples in each frame.
457 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
458 int buf_size, int *coded_samples)
460 ADPCMDecodeContext *s = avctx->priv_data;
462 int ch = avctx->channels;
463 int has_coded_samples = 0;
471 switch (avctx->codec->id) {
472 /* constant, only check buf_size */
473 case AV_CODEC_ID_ADPCM_EA_XAS:
474 if (buf_size < 76 * ch)
478 case AV_CODEC_ID_ADPCM_IMA_QT:
479 if (buf_size < 34 * ch)
483 /* simple 4-bit adpcm */
484 case AV_CODEC_ID_ADPCM_CT:
485 case AV_CODEC_ID_ADPCM_IMA_APC:
486 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
487 case AV_CODEC_ID_ADPCM_IMA_OKI:
488 case AV_CODEC_ID_ADPCM_IMA_WS:
489 case AV_CODEC_ID_ADPCM_YAMAHA:
490 nb_samples = buf_size * 2 / ch;
496 /* simple 4-bit adpcm, with header */
498 switch (avctx->codec->id) {
499 case AV_CODEC_ID_ADPCM_4XM:
500 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
501 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
502 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
505 return (buf_size - header_size) * 2 / ch;
507 /* more complex formats */
508 switch (avctx->codec->id) {
509 case AV_CODEC_ID_ADPCM_EA:
510 has_coded_samples = 1;
511 *coded_samples = bytestream2_get_le32(gb);
512 *coded_samples -= *coded_samples % 28;
513 nb_samples = (buf_size - 12) / 30 * 28;
515 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
516 has_coded_samples = 1;
517 *coded_samples = bytestream2_get_le32(gb);
518 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
520 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
521 nb_samples = (buf_size - ch) / ch * 2;
523 case AV_CODEC_ID_ADPCM_EA_R1:
524 case AV_CODEC_ID_ADPCM_EA_R2:
525 case AV_CODEC_ID_ADPCM_EA_R3:
526 /* maximum number of samples */
527 /* has internal offsets and a per-frame switch to signal raw 16-bit */
528 has_coded_samples = 1;
529 switch (avctx->codec->id) {
530 case AV_CODEC_ID_ADPCM_EA_R1:
531 header_size = 4 + 9 * ch;
532 *coded_samples = bytestream2_get_le32(gb);
534 case AV_CODEC_ID_ADPCM_EA_R2:
535 header_size = 4 + 5 * ch;
536 *coded_samples = bytestream2_get_le32(gb);
538 case AV_CODEC_ID_ADPCM_EA_R3:
539 header_size = 4 + 5 * ch;
540 *coded_samples = bytestream2_get_be32(gb);
543 *coded_samples -= *coded_samples % 28;
544 nb_samples = (buf_size - header_size) * 2 / ch;
545 nb_samples -= nb_samples % 28;
547 case AV_CODEC_ID_ADPCM_IMA_DK3:
548 if (avctx->block_align > 0)
549 buf_size = FFMIN(buf_size, avctx->block_align);
550 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
552 case AV_CODEC_ID_ADPCM_IMA_DK4:
553 if (avctx->block_align > 0)
554 buf_size = FFMIN(buf_size, avctx->block_align);
555 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
557 case AV_CODEC_ID_ADPCM_IMA_WAV:
558 if (avctx->block_align > 0)
559 buf_size = FFMIN(buf_size, avctx->block_align);
560 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
562 case AV_CODEC_ID_ADPCM_MS:
563 if (avctx->block_align > 0)
564 buf_size = FFMIN(buf_size, avctx->block_align);
565 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
567 case AV_CODEC_ID_ADPCM_SBPRO_2:
568 case AV_CODEC_ID_ADPCM_SBPRO_3:
569 case AV_CODEC_ID_ADPCM_SBPRO_4:
571 int samples_per_byte;
572 switch (avctx->codec->id) {
573 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
574 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
575 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
577 if (!s->status[0].step_index) {
581 nb_samples += buf_size * samples_per_byte / ch;
584 case AV_CODEC_ID_ADPCM_SWF:
586 int buf_bits = buf_size * 8 - 2;
587 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
588 int block_hdr_size = 22 * ch;
589 int block_size = block_hdr_size + nbits * ch * 4095;
590 int nblocks = buf_bits / block_size;
591 int bits_left = buf_bits - nblocks * block_size;
592 nb_samples = nblocks * 4096;
593 if (bits_left >= block_hdr_size)
594 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
597 case AV_CODEC_ID_ADPCM_THP:
598 has_coded_samples = 1;
599 bytestream2_skip(gb, 4); // channel size
600 *coded_samples = bytestream2_get_be32(gb);
601 *coded_samples -= *coded_samples % 14;
602 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
604 case AV_CODEC_ID_ADPCM_AFC:
605 nb_samples = buf_size / (9 * ch) * 16;
607 case AV_CODEC_ID_ADPCM_XA:
608 nb_samples = (buf_size / 128) * 224 / ch;
612 /* validate coded sample count */
613 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
614 return AVERROR_INVALIDDATA;
619 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
620 int *got_frame_ptr, AVPacket *avpkt)
622 const uint8_t *buf = avpkt->data;
623 int buf_size = avpkt->size;
624 ADPCMDecodeContext *c = avctx->priv_data;
625 ADPCMChannelStatus *cs;
626 int n, m, channel, i;
631 int nb_samples, coded_samples, ret;
634 bytestream2_init(&gb, buf, buf_size);
635 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
636 if (nb_samples <= 0) {
637 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
638 return AVERROR_INVALIDDATA;
641 /* get output buffer */
642 c->frame.nb_samples = nb_samples;
643 if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
644 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
647 samples = (short *)c->frame.data[0];
648 samples_p = (int16_t **)c->frame.extended_data;
650 /* use coded_samples when applicable */
651 /* it is always <= nb_samples, so the output buffer will be large enough */
653 if (coded_samples != nb_samples)
654 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
655 c->frame.nb_samples = nb_samples = coded_samples;
658 st = avctx->channels == 2 ? 1 : 0;
660 switch(avctx->codec->id) {
661 case AV_CODEC_ID_ADPCM_IMA_QT:
662 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
663 Channel data is interleaved per-chunk. */
664 for (channel = 0; channel < avctx->channels; channel++) {
667 cs = &(c->status[channel]);
668 /* (pppppp) (piiiiiii) */
670 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
671 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
672 step_index = predictor & 0x7F;
675 if (cs->step_index == step_index) {
676 int diff = predictor - cs->predictor;
683 cs->step_index = step_index;
684 cs->predictor = predictor;
687 if (cs->step_index > 88u){
688 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
689 channel, cs->step_index);
690 return AVERROR_INVALIDDATA;
693 samples = samples_p[channel];
695 for (m = 0; m < 64; m += 2) {
696 int byte = bytestream2_get_byteu(&gb);
697 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
698 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
702 case AV_CODEC_ID_ADPCM_IMA_WAV:
703 for(i=0; i<avctx->channels; i++){
704 cs = &(c->status[i]);
705 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
707 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
708 if (cs->step_index > 88u){
709 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
711 return AVERROR_INVALIDDATA;
715 for (n = 0; n < (nb_samples - 1) / 8; n++) {
716 for (i = 0; i < avctx->channels; i++) {
718 samples = &samples_p[i][1 + n * 8];
719 for (m = 0; m < 8; m += 2) {
720 int v = bytestream2_get_byteu(&gb);
721 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
722 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
727 case AV_CODEC_ID_ADPCM_4XM:
728 for (i = 0; i < avctx->channels; i++)
729 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
731 for (i = 0; i < avctx->channels; i++) {
732 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
733 if (c->status[i].step_index > 88u) {
734 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
735 i, c->status[i].step_index);
736 return AVERROR_INVALIDDATA;
740 for (i = 0; i < avctx->channels; i++) {
741 samples = (int16_t *)c->frame.data[i];
743 for (n = nb_samples >> 1; n > 0; n--) {
744 int v = bytestream2_get_byteu(&gb);
745 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
746 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
750 case AV_CODEC_ID_ADPCM_MS:
754 block_predictor = bytestream2_get_byteu(&gb);
755 if (block_predictor > 6) {
756 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
758 return AVERROR_INVALIDDATA;
760 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
761 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
763 block_predictor = bytestream2_get_byteu(&gb);
764 if (block_predictor > 6) {
765 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
767 return AVERROR_INVALIDDATA;
769 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
770 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
772 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
774 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
777 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
778 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
779 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
780 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
782 *samples++ = c->status[0].sample2;
783 if (st) *samples++ = c->status[1].sample2;
784 *samples++ = c->status[0].sample1;
785 if (st) *samples++ = c->status[1].sample1;
786 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
787 int byte = bytestream2_get_byteu(&gb);
788 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
789 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
793 case AV_CODEC_ID_ADPCM_IMA_DK4:
794 for (channel = 0; channel < avctx->channels; channel++) {
795 cs = &c->status[channel];
796 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
797 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
798 if (cs->step_index > 88u){
799 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
800 channel, cs->step_index);
801 return AVERROR_INVALIDDATA;
804 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
805 int v = bytestream2_get_byteu(&gb);
806 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
807 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
810 case AV_CODEC_ID_ADPCM_IMA_DK3:
814 int decode_top_nibble_next = 0;
816 const int16_t *samples_end = samples + avctx->channels * nb_samples;
818 bytestream2_skipu(&gb, 10);
819 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
820 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
821 c->status[0].step_index = bytestream2_get_byteu(&gb);
822 c->status[1].step_index = bytestream2_get_byteu(&gb);
823 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
824 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
825 c->status[0].step_index, c->status[1].step_index);
826 return AVERROR_INVALIDDATA;
828 /* sign extend the predictors */
829 diff_channel = c->status[1].predictor;
831 /* DK3 ADPCM support macro */
832 #define DK3_GET_NEXT_NIBBLE() \
833 if (decode_top_nibble_next) { \
834 nibble = last_byte >> 4; \
835 decode_top_nibble_next = 0; \
837 last_byte = bytestream2_get_byteu(&gb); \
838 nibble = last_byte & 0x0F; \
839 decode_top_nibble_next = 1; \
842 while (samples < samples_end) {
844 /* for this algorithm, c->status[0] is the sum channel and
845 * c->status[1] is the diff channel */
847 /* process the first predictor of the sum channel */
848 DK3_GET_NEXT_NIBBLE();
849 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
851 /* process the diff channel predictor */
852 DK3_GET_NEXT_NIBBLE();
853 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
855 /* process the first pair of stereo PCM samples */
856 diff_channel = (diff_channel + c->status[1].predictor) / 2;
857 *samples++ = c->status[0].predictor + c->status[1].predictor;
858 *samples++ = c->status[0].predictor - c->status[1].predictor;
860 /* process the second predictor of the sum channel */
861 DK3_GET_NEXT_NIBBLE();
862 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
864 /* process the second pair of stereo PCM samples */
865 diff_channel = (diff_channel + c->status[1].predictor) / 2;
866 *samples++ = c->status[0].predictor + c->status[1].predictor;
867 *samples++ = c->status[0].predictor - c->status[1].predictor;
871 case AV_CODEC_ID_ADPCM_IMA_ISS:
872 for (channel = 0; channel < avctx->channels; channel++) {
873 cs = &c->status[channel];
874 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
875 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
876 if (cs->step_index > 88u){
877 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
878 channel, cs->step_index);
879 return AVERROR_INVALIDDATA;
883 for (n = nb_samples >> (1 - st); n > 0; n--) {
885 int v = bytestream2_get_byteu(&gb);
886 /* nibbles are swapped for mono */
894 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
895 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
898 case AV_CODEC_ID_ADPCM_IMA_APC:
899 while (bytestream2_get_bytes_left(&gb) > 0) {
900 int v = bytestream2_get_byteu(&gb);
901 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
902 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
905 case AV_CODEC_ID_ADPCM_IMA_OKI:
906 while (bytestream2_get_bytes_left(&gb) > 0) {
907 int v = bytestream2_get_byteu(&gb);
908 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
909 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
912 case AV_CODEC_ID_ADPCM_IMA_WS:
913 if (c->vqa_version == 3) {
914 for (channel = 0; channel < avctx->channels; channel++) {
915 int16_t *smp = samples_p[channel];
917 for (n = nb_samples / 2; n > 0; n--) {
918 int v = bytestream2_get_byteu(&gb);
919 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
920 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
924 for (n = nb_samples / 2; n > 0; n--) {
925 for (channel = 0; channel < avctx->channels; channel++) {
926 int v = bytestream2_get_byteu(&gb);
927 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
928 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
930 samples += avctx->channels;
933 bytestream2_seek(&gb, 0, SEEK_END);
935 case AV_CODEC_ID_ADPCM_XA:
937 int16_t *out0 = samples_p[0];
938 int16_t *out1 = samples_p[1];
939 int samples_per_block = 28 * (3 - avctx->channels) * 4;
940 int sample_offset = 0;
941 while (bytestream2_get_bytes_left(&gb) >= 128) {
942 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
943 &c->status[0], &c->status[1],
944 avctx->channels, sample_offset)) < 0)
946 bytestream2_skipu(&gb, 128);
947 sample_offset += samples_per_block;
951 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
952 for (i=0; i<=st; i++) {
953 c->status[i].step_index = bytestream2_get_le32u(&gb);
954 if (c->status[i].step_index > 88u) {
955 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
956 i, c->status[i].step_index);
957 return AVERROR_INVALIDDATA;
960 for (i=0; i<=st; i++)
961 c->status[i].predictor = bytestream2_get_le32u(&gb);
963 for (n = nb_samples >> (1 - st); n > 0; n--) {
964 int byte = bytestream2_get_byteu(&gb);
965 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
966 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
969 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
970 for (n = nb_samples >> (1 - st); n > 0; n--) {
971 int byte = bytestream2_get_byteu(&gb);
972 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
973 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
976 case AV_CODEC_ID_ADPCM_EA:
978 int previous_left_sample, previous_right_sample;
979 int current_left_sample, current_right_sample;
980 int next_left_sample, next_right_sample;
981 int coeff1l, coeff2l, coeff1r, coeff2r;
982 int shift_left, shift_right;
984 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
985 each coding 28 stereo samples. */
987 if(avctx->channels != 2)
988 return AVERROR_INVALIDDATA;
990 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
991 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
992 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
993 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
995 for (count1 = 0; count1 < nb_samples / 28; count1++) {
996 int byte = bytestream2_get_byteu(&gb);
997 coeff1l = ea_adpcm_table[ byte >> 4 ];
998 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
999 coeff1r = ea_adpcm_table[ byte & 0x0F];
1000 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1002 byte = bytestream2_get_byteu(&gb);
1003 shift_left = 20 - (byte >> 4);
1004 shift_right = 20 - (byte & 0x0F);
1006 for (count2 = 0; count2 < 28; count2++) {
1007 byte = bytestream2_get_byteu(&gb);
1008 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1009 next_right_sample = sign_extend(byte, 4) << shift_right;
1011 next_left_sample = (next_left_sample +
1012 (current_left_sample * coeff1l) +
1013 (previous_left_sample * coeff2l) + 0x80) >> 8;
1014 next_right_sample = (next_right_sample +
1015 (current_right_sample * coeff1r) +
1016 (previous_right_sample * coeff2r) + 0x80) >> 8;
1018 previous_left_sample = current_left_sample;
1019 current_left_sample = av_clip_int16(next_left_sample);
1020 previous_right_sample = current_right_sample;
1021 current_right_sample = av_clip_int16(next_right_sample);
1022 *samples++ = current_left_sample;
1023 *samples++ = current_right_sample;
1027 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1031 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1033 int coeff[2][2], shift[2];
1035 for(channel = 0; channel < avctx->channels; channel++) {
1036 int byte = bytestream2_get_byteu(&gb);
1038 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1039 shift[channel] = 20 - (byte & 0x0F);
1041 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1044 byte[0] = bytestream2_get_byteu(&gb);
1045 if (st) byte[1] = bytestream2_get_byteu(&gb);
1046 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1047 for(channel = 0; channel < avctx->channels; channel++) {
1048 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1050 c->status[channel].sample1 * coeff[channel][0] +
1051 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1052 c->status[channel].sample2 = c->status[channel].sample1;
1053 c->status[channel].sample1 = av_clip_int16(sample);
1054 *samples++ = c->status[channel].sample1;
1058 bytestream2_seek(&gb, 0, SEEK_END);
1061 case AV_CODEC_ID_ADPCM_EA_R1:
1062 case AV_CODEC_ID_ADPCM_EA_R2:
1063 case AV_CODEC_ID_ADPCM_EA_R3: {
1064 /* channel numbering
1066 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1067 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1068 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1069 int previous_sample, current_sample, next_sample;
1072 unsigned int channel;
1077 for (channel=0; channel<avctx->channels; channel++)
1078 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1079 bytestream2_get_le32(&gb)) +
1080 (avctx->channels + 1) * 4;
1082 for (channel=0; channel<avctx->channels; channel++) {
1083 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1084 samplesC = samples_p[channel];
1086 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1087 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1088 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1090 current_sample = c->status[channel].predictor;
1091 previous_sample = c->status[channel].prev_sample;
1094 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1095 int byte = bytestream2_get_byte(&gb);
1096 if (byte == 0xEE) { /* only seen in R2 and R3 */
1097 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1098 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1100 for (count2=0; count2<28; count2++)
1101 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1103 coeff1 = ea_adpcm_table[ byte >> 4 ];
1104 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1105 shift = 20 - (byte & 0x0F);
1107 for (count2=0; count2<28; count2++) {
1109 next_sample = sign_extend(byte, 4) << shift;
1111 byte = bytestream2_get_byte(&gb);
1112 next_sample = sign_extend(byte >> 4, 4) << shift;
1115 next_sample += (current_sample * coeff1) +
1116 (previous_sample * coeff2);
1117 next_sample = av_clip_int16(next_sample >> 8);
1119 previous_sample = current_sample;
1120 current_sample = next_sample;
1121 *samplesC++ = current_sample;
1127 } else if (count != count1) {
1128 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1129 count = FFMAX(count, count1);
1132 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1133 c->status[channel].predictor = current_sample;
1134 c->status[channel].prev_sample = previous_sample;
1138 c->frame.nb_samples = count * 28;
1139 bytestream2_seek(&gb, 0, SEEK_END);
1142 case AV_CODEC_ID_ADPCM_EA_XAS:
1143 for (channel=0; channel<avctx->channels; channel++) {
1144 int coeff[2][4], shift[4];
1145 int16_t *s = samples_p[channel];
1146 for (n = 0; n < 4; n++, s += 32) {
1147 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1149 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1152 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1153 shift[n] = 20 - (val & 0x0F);
1157 for (m=2; m<32; m+=2) {
1158 s = &samples_p[channel][m];
1159 for (n = 0; n < 4; n++, s += 32) {
1161 int byte = bytestream2_get_byteu(&gb);
1163 level = sign_extend(byte >> 4, 4) << shift[n];
1164 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1165 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1167 level = sign_extend(byte, 4) << shift[n];
1168 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1169 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1174 case AV_CODEC_ID_ADPCM_IMA_AMV:
1175 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1176 c->status[0].step_index = bytestream2_get_le16u(&gb);
1177 bytestream2_skipu(&gb, 4);
1178 if (c->status[0].step_index > 88u) {
1179 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1180 c->status[0].step_index);
1181 return AVERROR_INVALIDDATA;
1184 for (n = nb_samples >> (1 - st); n > 0; n--) {
1185 int v = bytestream2_get_byteu(&gb);
1187 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1188 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1191 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1192 for (i = 0; i < avctx->channels; i++) {
1193 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1194 c->status[i].step_index = bytestream2_get_byteu(&gb);
1195 bytestream2_skipu(&gb, 1);
1196 if (c->status[i].step_index > 88u) {
1197 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1198 c->status[i].step_index);
1199 return AVERROR_INVALIDDATA;
1203 for (n = nb_samples >> (1 - st); n > 0; n--) {
1204 int v = bytestream2_get_byteu(&gb);
1206 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1207 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1210 case AV_CODEC_ID_ADPCM_CT:
1211 for (n = nb_samples >> (1 - st); n > 0; n--) {
1212 int v = bytestream2_get_byteu(&gb);
1213 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1214 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1217 case AV_CODEC_ID_ADPCM_SBPRO_4:
1218 case AV_CODEC_ID_ADPCM_SBPRO_3:
1219 case AV_CODEC_ID_ADPCM_SBPRO_2:
1220 if (!c->status[0].step_index) {
1221 /* the first byte is a raw sample */
1222 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1224 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1225 c->status[0].step_index = 1;
1228 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1229 for (n = nb_samples >> (1 - st); n > 0; n--) {
1230 int byte = bytestream2_get_byteu(&gb);
1231 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1233 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1236 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1237 for (n = nb_samples / 3; n > 0; n--) {
1238 int byte = bytestream2_get_byteu(&gb);
1239 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1241 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1242 (byte >> 2) & 0x07, 3, 0);
1243 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1247 for (n = nb_samples >> (2 - st); n > 0; n--) {
1248 int byte = bytestream2_get_byteu(&gb);
1249 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1251 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1252 (byte >> 4) & 0x03, 2, 2);
1253 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1254 (byte >> 2) & 0x03, 2, 2);
1255 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1260 case AV_CODEC_ID_ADPCM_SWF:
1261 adpcm_swf_decode(avctx, buf, buf_size, samples);
1262 bytestream2_seek(&gb, 0, SEEK_END);
1264 case AV_CODEC_ID_ADPCM_YAMAHA:
1265 for (n = nb_samples >> (1 - st); n > 0; n--) {
1266 int v = bytestream2_get_byteu(&gb);
1267 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1268 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1271 case AV_CODEC_ID_ADPCM_AFC:
1273 int samples_per_block;
1276 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1277 samples_per_block = avctx->extradata[0] / 16;
1278 blocks = nb_samples / avctx->extradata[0];
1280 samples_per_block = nb_samples / 16;
1284 for (m = 0; m < blocks; m++) {
1285 for (channel = 0; channel < avctx->channels; channel++) {
1286 int prev1 = c->status[channel].sample1;
1287 int prev2 = c->status[channel].sample2;
1289 samples = samples_p[channel] + m * 16;
1290 /* Read in every sample for this channel. */
1291 for (i = 0; i < samples_per_block; i++) {
1292 int byte = bytestream2_get_byteu(&gb);
1293 int scale = 1 << (byte >> 4);
1294 int index = byte & 0xf;
1295 int factor1 = ff_adpcm_afc_coeffs[0][index];
1296 int factor2 = ff_adpcm_afc_coeffs[1][index];
1298 /* Decode 16 samples. */
1299 for (n = 0; n < 16; n++) {
1303 sampledat = sign_extend(byte, 4);
1305 byte = bytestream2_get_byteu(&gb);
1306 sampledat = sign_extend(byte >> 4, 4);
1309 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1310 ((sampledat * scale) << 11)) >> 11;
1311 *samples = av_clip_int16(sampledat);
1317 c->status[channel].sample1 = prev1;
1318 c->status[channel].sample2 = prev2;
1321 bytestream2_seek(&gb, 0, SEEK_END);
1324 case AV_CODEC_ID_ADPCM_THP:
1329 for (i = 0; i < avctx->channels; i++)
1330 for (n = 0; n < 16; n++)
1331 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1333 /* Initialize the previous sample. */
1334 for (i = 0; i < avctx->channels; i++) {
1335 c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1336 c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1339 for (ch = 0; ch < avctx->channels; ch++) {
1340 samples = samples_p[ch];
1342 /* Read in every sample for this channel. */
1343 for (i = 0; i < nb_samples / 14; i++) {
1344 int byte = bytestream2_get_byteu(&gb);
1345 int index = (byte >> 4) & 7;
1346 unsigned int exp = byte & 0x0F;
1347 int factor1 = table[ch][index * 2];
1348 int factor2 = table[ch][index * 2 + 1];
1350 /* Decode 14 samples. */
1351 for (n = 0; n < 14; n++) {
1355 sampledat = sign_extend(byte, 4);
1357 byte = bytestream2_get_byteu(&gb);
1358 sampledat = sign_extend(byte >> 4, 4);
1361 sampledat = ((c->status[ch].sample1 * factor1
1362 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1363 *samples = av_clip_int16(sampledat);
1364 c->status[ch].sample2 = c->status[ch].sample1;
1365 c->status[ch].sample1 = *samples++;
1376 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1377 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1378 return AVERROR_INVALIDDATA;
1382 *(AVFrame *)data = c->frame;
1384 return bytestream2_tell(&gb);
1388 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1389 AV_SAMPLE_FMT_NONE };
1390 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
1391 AV_SAMPLE_FMT_NONE };
1392 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1394 AV_SAMPLE_FMT_NONE };
1396 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1397 AVCodec ff_ ## name_ ## _decoder = { \
1399 .type = AVMEDIA_TYPE_AUDIO, \
1401 .priv_data_size = sizeof(ADPCMDecodeContext), \
1402 .init = adpcm_decode_init, \
1403 .decode = adpcm_decode_frame, \
1404 .capabilities = CODEC_CAP_DR1, \
1405 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1406 .sample_fmts = sample_fmts_, \
1409 /* Note: Do not forget to add new entries to the Makefile as well. */
1410 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1411 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1412 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1413 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1414 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1415 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1416 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1417 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1418 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1419 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1420 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1421 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1422 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1423 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1424 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1425 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1426 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1427 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1428 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1429 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1430 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1431 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1432 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1433 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1434 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1435 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1436 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1437 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1438 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");