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"
30 * First version by Francois Revol (revol@free.fr)
31 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
32 * by Mike Melanson (melanson@pcisys.net)
33 * CD-ROM XA ADPCM codec by BERO
34 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
35 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
36 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
37 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
38 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
39 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
40 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
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 {
88 ADPCMChannelStatus status[6];
89 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_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 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
153 case AV_CODEC_ID_ADPCM_IMA_WS:
154 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
158 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
161 avcodec_get_frame_defaults(&c->frame);
162 avctx->coded_frame = &c->frame;
167 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
171 int sign, delta, diff, step;
173 step = ff_adpcm_step_table[c->step_index];
174 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
175 step_index = av_clip(step_index, 0, 88);
179 /* perform direct multiplication instead of series of jumps proposed by
180 * the reference ADPCM implementation since modern CPUs can do the mults
182 diff = ((2 * delta + 1) * step) >> shift;
183 predictor = c->predictor;
184 if (sign) predictor -= diff;
185 else predictor += diff;
187 c->predictor = av_clip_int16(predictor);
188 c->step_index = step_index;
190 return (short)c->predictor;
193 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
199 step = ff_adpcm_step_table[c->step_index];
200 step_index = c->step_index + ff_adpcm_index_table[nibble];
201 step_index = av_clip(step_index, 0, 88);
204 if (nibble & 4) diff += step;
205 if (nibble & 2) diff += step >> 1;
206 if (nibble & 1) diff += step >> 2;
209 predictor = c->predictor - diff;
211 predictor = c->predictor + diff;
213 c->predictor = av_clip_int16(predictor);
214 c->step_index = step_index;
219 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
223 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
224 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
226 c->sample2 = c->sample1;
227 c->sample1 = av_clip_int16(predictor);
228 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
229 if (c->idelta < 16) c->idelta = 16;
234 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
236 int step_index, predictor, sign, delta, diff, step;
238 step = ff_adpcm_oki_step_table[c->step_index];
239 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
240 step_index = av_clip(step_index, 0, 48);
244 diff = ((2 * delta + 1) * step) >> 3;
245 predictor = c->predictor;
246 if (sign) predictor -= diff;
247 else predictor += diff;
249 c->predictor = av_clip(predictor, -2048, 2047);
250 c->step_index = step_index;
252 return c->predictor << 4;
255 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
257 int sign, delta, diff;
262 /* perform direct multiplication instead of series of jumps proposed by
263 * the reference ADPCM implementation since modern CPUs can do the mults
265 diff = ((2 * delta + 1) * c->step) >> 3;
266 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
267 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
268 c->predictor = av_clip_int16(c->predictor);
269 /* calculate new step and clamp it to range 511..32767 */
270 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
271 c->step = av_clip(new_step, 511, 32767);
273 return (short)c->predictor;
276 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
278 int sign, delta, diff;
280 sign = nibble & (1<<(size-1));
281 delta = nibble & ((1<<(size-1))-1);
282 diff = delta << (7 + c->step + shift);
285 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
287 /* calculate new step */
288 if (delta >= (2*size - 3) && c->step < 3)
290 else if (delta == 0 && c->step > 0)
293 return (short) c->predictor;
296 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
303 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
304 c->predictor = av_clip_int16(c->predictor);
305 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
306 c->step = av_clip(c->step, 127, 24567);
310 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
311 const uint8_t *in, ADPCMChannelStatus *left,
312 ADPCMChannelStatus *right, int channels, int sample_offset)
315 int shift,filter,f0,f1;
319 out0 += sample_offset;
323 out1 += sample_offset;
326 shift = 12 - (in[4+i*2] & 15);
327 filter = in[4+i*2] >> 4;
328 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
329 av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
332 f0 = xa_adpcm_table[filter][0];
333 f1 = xa_adpcm_table[filter][1];
341 t = sign_extend(d, 4);
342 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
344 s_1 = av_clip_int16(s);
351 s_1 = right->sample1;
352 s_2 = right->sample2;
355 shift = 12 - (in[5+i*2] & 15);
356 filter = in[5+i*2] >> 4;
357 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
358 av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
362 f0 = xa_adpcm_table[filter][0];
363 f1 = xa_adpcm_table[filter][1];
368 t = sign_extend(d >> 4, 4);
369 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
371 s_1 = av_clip_int16(s);
376 right->sample1 = s_1;
377 right->sample2 = s_2;
383 out0 += 28 * (3 - channels);
384 out1 += 28 * (3 - channels);
390 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
392 ADPCMDecodeContext *c = avctx->priv_data;
395 int k0, signmask, nb_bits, count;
396 int size = buf_size*8;
399 init_get_bits(&gb, buf, size);
401 //read bits & initial values
402 nb_bits = get_bits(&gb, 2)+2;
403 table = swf_index_tables[nb_bits-2];
404 k0 = 1 << (nb_bits-2);
405 signmask = 1 << (nb_bits-1);
407 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
408 for (i = 0; i < avctx->channels; i++) {
409 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
410 c->status[i].step_index = get_bits(&gb, 6);
413 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
416 for (i = 0; i < avctx->channels; i++) {
417 // similar to IMA adpcm
418 int delta = get_bits(&gb, nb_bits);
419 int step = ff_adpcm_step_table[c->status[i].step_index];
420 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
431 if (delta & signmask)
432 c->status[i].predictor -= vpdiff;
434 c->status[i].predictor += vpdiff;
436 c->status[i].step_index += table[delta & (~signmask)];
438 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
439 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
441 *samples++ = c->status[i].predictor;
448 * Get the number of samples that will be decoded from the packet.
449 * In one case, this is actually the maximum number of samples possible to
450 * decode with the given buf_size.
452 * @param[out] coded_samples set to the number of samples as coded in the
453 * packet, or 0 if the codec does not encode the
454 * number of samples in each frame.
456 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
457 int buf_size, int *coded_samples)
459 ADPCMDecodeContext *s = avctx->priv_data;
461 int ch = avctx->channels;
462 int has_coded_samples = 0;
470 switch (avctx->codec->id) {
471 /* constant, only check buf_size */
472 case AV_CODEC_ID_ADPCM_EA_XAS:
473 if (buf_size < 76 * ch)
477 case AV_CODEC_ID_ADPCM_IMA_QT:
478 if (buf_size < 34 * ch)
482 /* simple 4-bit adpcm */
483 case AV_CODEC_ID_ADPCM_CT:
484 case AV_CODEC_ID_ADPCM_IMA_APC:
485 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
486 case AV_CODEC_ID_ADPCM_IMA_OKI:
487 case AV_CODEC_ID_ADPCM_IMA_WS:
488 case AV_CODEC_ID_ADPCM_YAMAHA:
489 nb_samples = buf_size * 2 / ch;
495 /* simple 4-bit adpcm, with header */
497 switch (avctx->codec->id) {
498 case AV_CODEC_ID_ADPCM_4XM:
499 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
500 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
501 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
504 return (buf_size - header_size) * 2 / ch;
506 /* more complex formats */
507 switch (avctx->codec->id) {
508 case AV_CODEC_ID_ADPCM_EA:
509 has_coded_samples = 1;
510 *coded_samples = bytestream2_get_le32(gb);
511 *coded_samples -= *coded_samples % 28;
512 nb_samples = (buf_size - 12) / 30 * 28;
514 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
515 has_coded_samples = 1;
516 *coded_samples = bytestream2_get_le32(gb);
517 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
519 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
520 nb_samples = (buf_size - ch) / ch * 2;
522 case AV_CODEC_ID_ADPCM_EA_R1:
523 case AV_CODEC_ID_ADPCM_EA_R2:
524 case AV_CODEC_ID_ADPCM_EA_R3:
525 /* maximum number of samples */
526 /* has internal offsets and a per-frame switch to signal raw 16-bit */
527 has_coded_samples = 1;
528 switch (avctx->codec->id) {
529 case AV_CODEC_ID_ADPCM_EA_R1:
530 header_size = 4 + 9 * ch;
531 *coded_samples = bytestream2_get_le32(gb);
533 case AV_CODEC_ID_ADPCM_EA_R2:
534 header_size = 4 + 5 * ch;
535 *coded_samples = bytestream2_get_le32(gb);
537 case AV_CODEC_ID_ADPCM_EA_R3:
538 header_size = 4 + 5 * ch;
539 *coded_samples = bytestream2_get_be32(gb);
542 *coded_samples -= *coded_samples % 28;
543 nb_samples = (buf_size - header_size) * 2 / ch;
544 nb_samples -= nb_samples % 28;
546 case AV_CODEC_ID_ADPCM_IMA_DK3:
547 if (avctx->block_align > 0)
548 buf_size = FFMIN(buf_size, avctx->block_align);
549 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
551 case AV_CODEC_ID_ADPCM_IMA_DK4:
552 if (avctx->block_align > 0)
553 buf_size = FFMIN(buf_size, avctx->block_align);
554 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
556 case AV_CODEC_ID_ADPCM_IMA_WAV:
557 if (avctx->block_align > 0)
558 buf_size = FFMIN(buf_size, avctx->block_align);
559 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
561 case AV_CODEC_ID_ADPCM_MS:
562 if (avctx->block_align > 0)
563 buf_size = FFMIN(buf_size, avctx->block_align);
564 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
566 case AV_CODEC_ID_ADPCM_SBPRO_2:
567 case AV_CODEC_ID_ADPCM_SBPRO_3:
568 case AV_CODEC_ID_ADPCM_SBPRO_4:
570 int samples_per_byte;
571 switch (avctx->codec->id) {
572 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
573 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
574 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
576 if (!s->status[0].step_index) {
580 nb_samples += buf_size * samples_per_byte / ch;
583 case AV_CODEC_ID_ADPCM_SWF:
585 int buf_bits = buf_size * 8 - 2;
586 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
587 int block_hdr_size = 22 * ch;
588 int block_size = block_hdr_size + nbits * ch * 4095;
589 int nblocks = buf_bits / block_size;
590 int bits_left = buf_bits - nblocks * block_size;
591 nb_samples = nblocks * 4096;
592 if (bits_left >= block_hdr_size)
593 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
596 case AV_CODEC_ID_ADPCM_THP:
597 has_coded_samples = 1;
598 bytestream2_skip(gb, 4); // channel size
599 *coded_samples = bytestream2_get_be32(gb);
600 *coded_samples -= *coded_samples % 14;
601 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
603 case AV_CODEC_ID_ADPCM_AFC:
604 nb_samples = buf_size / (9 * ch) * 16;
606 case AV_CODEC_ID_ADPCM_XA:
607 nb_samples = (buf_size / 128) * 224 / ch;
611 /* validate coded sample count */
612 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
613 return AVERROR_INVALIDDATA;
618 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
619 int *got_frame_ptr, AVPacket *avpkt)
621 const uint8_t *buf = avpkt->data;
622 int buf_size = avpkt->size;
623 ADPCMDecodeContext *c = avctx->priv_data;
624 ADPCMChannelStatus *cs;
625 int n, m, channel, i;
630 int nb_samples, coded_samples, ret;
633 bytestream2_init(&gb, buf, buf_size);
634 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
635 if (nb_samples <= 0) {
636 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
637 return AVERROR_INVALIDDATA;
640 /* get output buffer */
641 c->frame.nb_samples = nb_samples;
642 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
643 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
646 samples = (short *)c->frame.data[0];
647 samples_p = (int16_t **)c->frame.extended_data;
649 /* use coded_samples when applicable */
650 /* it is always <= nb_samples, so the output buffer will be large enough */
652 if (coded_samples != nb_samples)
653 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
654 c->frame.nb_samples = nb_samples = coded_samples;
657 st = avctx->channels == 2 ? 1 : 0;
659 switch(avctx->codec->id) {
660 case AV_CODEC_ID_ADPCM_IMA_QT:
661 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
662 Channel data is interleaved per-chunk. */
663 for (channel = 0; channel < avctx->channels; channel++) {
666 cs = &(c->status[channel]);
667 /* (pppppp) (piiiiiii) */
669 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
670 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
671 step_index = predictor & 0x7F;
674 if (cs->step_index == step_index) {
675 int diff = predictor - cs->predictor;
682 cs->step_index = step_index;
683 cs->predictor = predictor;
686 if (cs->step_index > 88u){
687 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
688 channel, cs->step_index);
689 return AVERROR_INVALIDDATA;
692 samples = samples_p[channel];
694 for (m = 0; m < 64; m += 2) {
695 int byte = bytestream2_get_byteu(&gb);
696 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
697 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
701 case AV_CODEC_ID_ADPCM_IMA_WAV:
702 for(i=0; i<avctx->channels; i++){
703 cs = &(c->status[i]);
704 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
706 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
707 if (cs->step_index > 88u){
708 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
710 return AVERROR_INVALIDDATA;
714 for (n = 0; n < (nb_samples - 1) / 8; n++) {
715 for (i = 0; i < avctx->channels; i++) {
717 samples = &samples_p[i][1 + n * 8];
718 for (m = 0; m < 8; m += 2) {
719 int v = bytestream2_get_byteu(&gb);
720 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
721 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
726 case AV_CODEC_ID_ADPCM_4XM:
727 for (i = 0; i < avctx->channels; i++)
728 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
730 for (i = 0; i < avctx->channels; i++) {
731 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
732 if (c->status[i].step_index > 88u) {
733 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
734 i, c->status[i].step_index);
735 return AVERROR_INVALIDDATA;
739 for (i = 0; i < avctx->channels; i++) {
740 samples = (int16_t *)c->frame.data[i];
742 for (n = nb_samples >> 1; n > 0; n--) {
743 int v = bytestream2_get_byteu(&gb);
744 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
745 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
749 case AV_CODEC_ID_ADPCM_MS:
753 block_predictor = bytestream2_get_byteu(&gb);
754 if (block_predictor > 6) {
755 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
757 return AVERROR_INVALIDDATA;
759 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
760 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
762 block_predictor = bytestream2_get_byteu(&gb);
763 if (block_predictor > 6) {
764 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
766 return AVERROR_INVALIDDATA;
768 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
769 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
771 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
773 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
776 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
777 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
778 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
779 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
781 *samples++ = c->status[0].sample2;
782 if (st) *samples++ = c->status[1].sample2;
783 *samples++ = c->status[0].sample1;
784 if (st) *samples++ = c->status[1].sample1;
785 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
786 int byte = bytestream2_get_byteu(&gb);
787 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
788 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
792 case AV_CODEC_ID_ADPCM_IMA_DK4:
793 for (channel = 0; channel < avctx->channels; channel++) {
794 cs = &c->status[channel];
795 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
796 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
797 if (cs->step_index > 88u){
798 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
799 channel, cs->step_index);
800 return AVERROR_INVALIDDATA;
803 for (n = nb_samples >> (1 - st); n > 0; n--) {
804 int v = bytestream2_get_byteu(&gb);
805 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
806 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
809 case AV_CODEC_ID_ADPCM_IMA_DK3:
813 int decode_top_nibble_next = 0;
815 const int16_t *samples_end = samples + avctx->channels * nb_samples;
817 bytestream2_skipu(&gb, 10);
818 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
819 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
820 c->status[0].step_index = bytestream2_get_byteu(&gb);
821 c->status[1].step_index = bytestream2_get_byteu(&gb);
822 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
823 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
824 c->status[0].step_index, c->status[1].step_index);
825 return AVERROR_INVALIDDATA;
827 /* sign extend the predictors */
828 diff_channel = c->status[1].predictor;
830 /* DK3 ADPCM support macro */
831 #define DK3_GET_NEXT_NIBBLE() \
832 if (decode_top_nibble_next) { \
833 nibble = last_byte >> 4; \
834 decode_top_nibble_next = 0; \
836 last_byte = bytestream2_get_byteu(&gb); \
837 nibble = last_byte & 0x0F; \
838 decode_top_nibble_next = 1; \
841 while (samples < samples_end) {
843 /* for this algorithm, c->status[0] is the sum channel and
844 * c->status[1] is the diff channel */
846 /* process the first predictor of the sum channel */
847 DK3_GET_NEXT_NIBBLE();
848 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
850 /* process the diff channel predictor */
851 DK3_GET_NEXT_NIBBLE();
852 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
854 /* process the first pair of stereo PCM samples */
855 diff_channel = (diff_channel + c->status[1].predictor) / 2;
856 *samples++ = c->status[0].predictor + c->status[1].predictor;
857 *samples++ = c->status[0].predictor - c->status[1].predictor;
859 /* process the second predictor of the sum channel */
860 DK3_GET_NEXT_NIBBLE();
861 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
863 /* process the second 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;
870 case AV_CODEC_ID_ADPCM_IMA_ISS:
871 for (channel = 0; channel < avctx->channels; channel++) {
872 cs = &c->status[channel];
873 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
874 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
875 if (cs->step_index > 88u){
876 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
877 channel, cs->step_index);
878 return AVERROR_INVALIDDATA;
882 for (n = nb_samples >> (1 - st); n > 0; n--) {
884 int v = bytestream2_get_byteu(&gb);
885 /* nibbles are swapped for mono */
893 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
894 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
897 case AV_CODEC_ID_ADPCM_IMA_APC:
898 while (bytestream2_get_bytes_left(&gb) > 0) {
899 int v = bytestream2_get_byteu(&gb);
900 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
901 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
904 case AV_CODEC_ID_ADPCM_IMA_OKI:
905 while (bytestream2_get_bytes_left(&gb) > 0) {
906 int v = bytestream2_get_byteu(&gb);
907 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
908 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
911 case AV_CODEC_ID_ADPCM_IMA_WS:
912 if (c->vqa_version == 3) {
913 for (channel = 0; channel < avctx->channels; channel++) {
914 int16_t *smp = samples_p[channel];
916 for (n = nb_samples / 2; n > 0; n--) {
917 int v = bytestream2_get_byteu(&gb);
918 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
919 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
923 for (n = nb_samples / 2; n > 0; n--) {
924 for (channel = 0; channel < avctx->channels; channel++) {
925 int v = bytestream2_get_byteu(&gb);
926 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
927 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
929 samples += avctx->channels;
932 bytestream2_seek(&gb, 0, SEEK_END);
934 case AV_CODEC_ID_ADPCM_XA:
936 int16_t *out0 = samples_p[0];
937 int16_t *out1 = samples_p[1];
938 int samples_per_block = 28 * (3 - avctx->channels) * 4;
939 int sample_offset = 0;
940 while (bytestream2_get_bytes_left(&gb) >= 128) {
941 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
942 &c->status[0], &c->status[1],
943 avctx->channels, sample_offset)) < 0)
945 bytestream2_skipu(&gb, 128);
946 sample_offset += samples_per_block;
950 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
951 for (i=0; i<=st; i++) {
952 c->status[i].step_index = bytestream2_get_le32u(&gb);
953 if (c->status[i].step_index > 88u) {
954 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
955 i, c->status[i].step_index);
956 return AVERROR_INVALIDDATA;
959 for (i=0; i<=st; i++)
960 c->status[i].predictor = bytestream2_get_le32u(&gb);
962 for (n = nb_samples >> (1 - st); n > 0; n--) {
963 int byte = bytestream2_get_byteu(&gb);
964 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
965 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
968 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
969 for (n = nb_samples >> (1 - st); n > 0; n--) {
970 int byte = bytestream2_get_byteu(&gb);
971 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
972 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
975 case AV_CODEC_ID_ADPCM_EA:
977 int previous_left_sample, previous_right_sample;
978 int current_left_sample, current_right_sample;
979 int next_left_sample, next_right_sample;
980 int coeff1l, coeff2l, coeff1r, coeff2r;
981 int shift_left, shift_right;
983 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
984 each coding 28 stereo samples. */
986 if(avctx->channels != 2)
987 return AVERROR_INVALIDDATA;
989 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
990 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
991 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
992 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
994 for (count1 = 0; count1 < nb_samples / 28; count1++) {
995 int byte = bytestream2_get_byteu(&gb);
996 coeff1l = ea_adpcm_table[ byte >> 4 ];
997 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
998 coeff1r = ea_adpcm_table[ byte & 0x0F];
999 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1001 byte = bytestream2_get_byteu(&gb);
1002 shift_left = 20 - (byte >> 4);
1003 shift_right = 20 - (byte & 0x0F);
1005 for (count2 = 0; count2 < 28; count2++) {
1006 byte = bytestream2_get_byteu(&gb);
1007 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1008 next_right_sample = sign_extend(byte, 4) << shift_right;
1010 next_left_sample = (next_left_sample +
1011 (current_left_sample * coeff1l) +
1012 (previous_left_sample * coeff2l) + 0x80) >> 8;
1013 next_right_sample = (next_right_sample +
1014 (current_right_sample * coeff1r) +
1015 (previous_right_sample * coeff2r) + 0x80) >> 8;
1017 previous_left_sample = current_left_sample;
1018 current_left_sample = av_clip_int16(next_left_sample);
1019 previous_right_sample = current_right_sample;
1020 current_right_sample = av_clip_int16(next_right_sample);
1021 *samples++ = current_left_sample;
1022 *samples++ = current_right_sample;
1026 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1030 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1032 int coeff[2][2], shift[2];
1034 for(channel = 0; channel < avctx->channels; channel++) {
1035 int byte = bytestream2_get_byteu(&gb);
1037 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1038 shift[channel] = 20 - (byte & 0x0F);
1040 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1043 byte[0] = bytestream2_get_byteu(&gb);
1044 if (st) byte[1] = bytestream2_get_byteu(&gb);
1045 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1046 for(channel = 0; channel < avctx->channels; channel++) {
1047 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1049 c->status[channel].sample1 * coeff[channel][0] +
1050 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1051 c->status[channel].sample2 = c->status[channel].sample1;
1052 c->status[channel].sample1 = av_clip_int16(sample);
1053 *samples++ = c->status[channel].sample1;
1057 bytestream2_seek(&gb, 0, SEEK_END);
1060 case AV_CODEC_ID_ADPCM_EA_R1:
1061 case AV_CODEC_ID_ADPCM_EA_R2:
1062 case AV_CODEC_ID_ADPCM_EA_R3: {
1063 /* channel numbering
1065 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1066 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1067 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1068 int previous_sample, current_sample, next_sample;
1071 unsigned int channel;
1076 for (channel=0; channel<avctx->channels; channel++)
1077 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1078 bytestream2_get_le32(&gb)) +
1079 (avctx->channels + 1) * 4;
1081 for (channel=0; channel<avctx->channels; channel++) {
1082 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1083 samplesC = samples_p[channel];
1085 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1086 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1087 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1089 current_sample = c->status[channel].predictor;
1090 previous_sample = c->status[channel].prev_sample;
1093 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1094 int byte = bytestream2_get_byte(&gb);
1095 if (byte == 0xEE) { /* only seen in R2 and R3 */
1096 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1097 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1099 for (count2=0; count2<28; count2++)
1100 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1102 coeff1 = ea_adpcm_table[ byte >> 4 ];
1103 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1104 shift = 20 - (byte & 0x0F);
1106 for (count2=0; count2<28; count2++) {
1108 next_sample = sign_extend(byte, 4) << shift;
1110 byte = bytestream2_get_byte(&gb);
1111 next_sample = sign_extend(byte >> 4, 4) << shift;
1114 next_sample += (current_sample * coeff1) +
1115 (previous_sample * coeff2);
1116 next_sample = av_clip_int16(next_sample >> 8);
1118 previous_sample = current_sample;
1119 current_sample = next_sample;
1120 *samplesC++ = current_sample;
1126 } else if (count != count1) {
1127 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1128 count = FFMAX(count, count1);
1131 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1132 c->status[channel].predictor = current_sample;
1133 c->status[channel].prev_sample = previous_sample;
1137 c->frame.nb_samples = count * 28;
1138 bytestream2_seek(&gb, 0, SEEK_END);
1141 case AV_CODEC_ID_ADPCM_EA_XAS:
1142 for (channel=0; channel<avctx->channels; channel++) {
1143 int coeff[2][4], shift[4];
1144 int16_t *s = samples_p[channel];
1145 for (n = 0; n < 4; n++, s += 32) {
1146 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1148 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1151 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1152 shift[n] = 20 - (val & 0x0F);
1156 for (m=2; m<32; m+=2) {
1157 s = &samples_p[channel][m];
1158 for (n = 0; n < 4; n++, s += 32) {
1160 int byte = bytestream2_get_byteu(&gb);
1162 level = sign_extend(byte >> 4, 4) << shift[n];
1163 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1164 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1166 level = sign_extend(byte, 4) << shift[n];
1167 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1168 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1173 case AV_CODEC_ID_ADPCM_IMA_AMV:
1174 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1175 c->status[0].step_index = bytestream2_get_le16u(&gb);
1176 bytestream2_skipu(&gb, 4);
1177 if (c->status[0].step_index > 88u) {
1178 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1179 c->status[0].step_index);
1180 return AVERROR_INVALIDDATA;
1183 for (n = nb_samples >> (1 - st); n > 0; n--) {
1184 int v = bytestream2_get_byteu(&gb);
1186 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1187 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1190 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1191 for (i = 0; i < avctx->channels; i++) {
1192 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1193 c->status[i].step_index = bytestream2_get_byteu(&gb);
1194 bytestream2_skipu(&gb, 1);
1195 if (c->status[i].step_index > 88u) {
1196 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1197 c->status[i].step_index);
1198 return AVERROR_INVALIDDATA;
1202 for (n = nb_samples >> (1 - st); n > 0; n--) {
1203 int v = bytestream2_get_byteu(&gb);
1205 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1206 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1209 case AV_CODEC_ID_ADPCM_CT:
1210 for (n = nb_samples >> (1 - st); n > 0; n--) {
1211 int v = bytestream2_get_byteu(&gb);
1212 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1213 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1216 case AV_CODEC_ID_ADPCM_SBPRO_4:
1217 case AV_CODEC_ID_ADPCM_SBPRO_3:
1218 case AV_CODEC_ID_ADPCM_SBPRO_2:
1219 if (!c->status[0].step_index) {
1220 /* the first byte is a raw sample */
1221 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1223 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1224 c->status[0].step_index = 1;
1227 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1228 for (n = nb_samples >> (1 - st); n > 0; n--) {
1229 int byte = bytestream2_get_byteu(&gb);
1230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1232 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1235 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1236 for (n = nb_samples / 3; n > 0; n--) {
1237 int byte = bytestream2_get_byteu(&gb);
1238 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1240 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1241 (byte >> 2) & 0x07, 3, 0);
1242 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1246 for (n = nb_samples >> (2 - st); n > 0; n--) {
1247 int byte = bytestream2_get_byteu(&gb);
1248 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1250 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1251 (byte >> 4) & 0x03, 2, 2);
1252 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1253 (byte >> 2) & 0x03, 2, 2);
1254 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1259 case AV_CODEC_ID_ADPCM_SWF:
1260 adpcm_swf_decode(avctx, buf, buf_size, samples);
1261 bytestream2_seek(&gb, 0, SEEK_END);
1263 case AV_CODEC_ID_ADPCM_YAMAHA:
1264 for (n = nb_samples >> (1 - st); n > 0; n--) {
1265 int v = bytestream2_get_byteu(&gb);
1266 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1267 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1270 case AV_CODEC_ID_ADPCM_AFC:
1272 int samples_per_block;
1275 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1276 samples_per_block = avctx->extradata[0] / 16;
1277 blocks = nb_samples / avctx->extradata[0];
1279 samples_per_block = nb_samples / 16;
1283 for (m = 0; m < blocks; m++) {
1284 for (channel = 0; channel < avctx->channels; channel++) {
1285 int prev1 = c->status[channel].sample1;
1286 int prev2 = c->status[channel].sample2;
1288 samples = samples_p[channel] + m * 16;
1289 /* Read in every sample for this channel. */
1290 for (i = 0; i < samples_per_block; i++) {
1291 int byte = bytestream2_get_byteu(&gb);
1292 int scale = 1 << (byte >> 4);
1293 int index = byte & 0xf;
1294 int factor1 = ff_adpcm_afc_coeffs[0][index];
1295 int factor2 = ff_adpcm_afc_coeffs[1][index];
1297 /* Decode 16 samples. */
1298 for (n = 0; n < 16; n++) {
1302 sampledat = sign_extend(byte, 4);
1304 byte = bytestream2_get_byteu(&gb);
1305 sampledat = sign_extend(byte >> 4, 4);
1308 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1309 ((sampledat * scale) << 11)) >> 11;
1310 *samples = av_clip_int16(sampledat);
1316 c->status[channel].sample1 = prev1;
1317 c->status[channel].sample2 = prev2;
1320 bytestream2_seek(&gb, 0, SEEK_END);
1323 case AV_CODEC_ID_ADPCM_THP:
1328 for (i = 0; i < avctx->channels; i++)
1329 for (n = 0; n < 16; n++)
1330 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1332 /* Initialize the previous sample. */
1333 for (i = 0; i < avctx->channels; i++) {
1334 c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1335 c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1338 for (ch = 0; ch < avctx->channels; ch++) {
1339 samples = samples_p[ch];
1341 /* Read in every sample for this channel. */
1342 for (i = 0; i < nb_samples / 14; i++) {
1343 int byte = bytestream2_get_byteu(&gb);
1344 int index = (byte >> 4) & 7;
1345 unsigned int exp = byte & 0x0F;
1346 int factor1 = table[ch][index * 2];
1347 int factor2 = table[ch][index * 2 + 1];
1349 /* Decode 14 samples. */
1350 for (n = 0; n < 14; n++) {
1354 sampledat = sign_extend(byte, 4);
1356 byte = bytestream2_get_byteu(&gb);
1357 sampledat = sign_extend(byte >> 4, 4);
1360 sampledat = ((c->status[ch].sample1 * factor1
1361 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1362 *samples = av_clip_int16(sampledat);
1363 c->status[ch].sample2 = c->status[ch].sample1;
1364 c->status[ch].sample1 = *samples++;
1375 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1376 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1377 return AVERROR_INVALIDDATA;
1381 *(AVFrame *)data = c->frame;
1383 return bytestream2_tell(&gb);
1387 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1388 AV_SAMPLE_FMT_NONE };
1389 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
1390 AV_SAMPLE_FMT_NONE };
1391 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1393 AV_SAMPLE_FMT_NONE };
1395 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1396 AVCodec ff_ ## name_ ## _decoder = { \
1398 .type = AVMEDIA_TYPE_AUDIO, \
1400 .priv_data_size = sizeof(ADPCMDecodeContext), \
1401 .init = adpcm_decode_init, \
1402 .decode = adpcm_decode_frame, \
1403 .capabilities = CODEC_CAP_DR1, \
1404 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1405 .sample_fmts = sample_fmts_, \
1408 /* Note: Do not forget to add new entries to the Makefile as well. */
1409 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1410 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1411 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1412 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1413 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1414 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1415 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1416 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1417 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1418 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1419 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1420 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1421 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1422 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1423 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1424 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1425 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1426 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1427 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1428 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1429 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1430 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1431 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1432 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1433 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1434 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1435 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1436 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1437 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");