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 < 2 || avctx->bits_per_coded_sample > 5)
122 return AVERROR_INVALIDDATA;
124 case AV_CODEC_ID_ADPCM_IMA_APC:
125 if (avctx->extradata && avctx->extradata_size >= 8) {
126 c->status[0].predictor = AV_RL32(avctx->extradata);
127 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
130 case AV_CODEC_ID_ADPCM_IMA_WS:
131 if (avctx->extradata && avctx->extradata_size >= 2)
132 c->vqa_version = AV_RL16(avctx->extradata);
138 switch(avctx->codec->id) {
139 case AV_CODEC_ID_ADPCM_IMA_QT:
140 case AV_CODEC_ID_ADPCM_IMA_WAV:
141 case AV_CODEC_ID_ADPCM_4XM:
142 case AV_CODEC_ID_ADPCM_XA:
143 case AV_CODEC_ID_ADPCM_EA_R1:
144 case AV_CODEC_ID_ADPCM_EA_R2:
145 case AV_CODEC_ID_ADPCM_EA_R3:
146 case AV_CODEC_ID_ADPCM_EA_XAS:
147 case AV_CODEC_ID_ADPCM_THP:
148 case AV_CODEC_ID_ADPCM_AFC:
149 case AV_CODEC_ID_ADPCM_DTK:
150 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
152 case AV_CODEC_ID_ADPCM_IMA_WS:
153 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
157 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
163 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
167 int sign, delta, diff, step;
169 step = ff_adpcm_step_table[c->step_index];
170 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
171 step_index = av_clip(step_index, 0, 88);
175 /* perform direct multiplication instead of series of jumps proposed by
176 * the reference ADPCM implementation since modern CPUs can do the mults
178 diff = ((2 * delta + 1) * step) >> shift;
179 predictor = c->predictor;
180 if (sign) predictor -= diff;
181 else predictor += diff;
183 c->predictor = av_clip_int16(predictor);
184 c->step_index = step_index;
186 return (short)c->predictor;
189 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
191 int nibble, step_index, predictor, sign, delta, diff, step, shift;
194 nibble = get_bits_le(gb, bps),
195 step = ff_adpcm_step_table[c->step_index];
196 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
197 step_index = av_clip(step_index, 0, 88);
199 sign = nibble & (1 << shift);
200 delta = av_mod_uintp2(nibble, shift);
201 diff = ((2 * delta + 1) * step) >> shift;
202 predictor = c->predictor;
203 if (sign) predictor -= diff;
204 else predictor += diff;
206 c->predictor = av_clip_int16(predictor);
207 c->step_index = step_index;
209 return (int16_t)c->predictor;
212 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
218 step = ff_adpcm_step_table[c->step_index];
219 step_index = c->step_index + ff_adpcm_index_table[nibble];
220 step_index = av_clip(step_index, 0, 88);
223 if (nibble & 4) diff += step;
224 if (nibble & 2) diff += step >> 1;
225 if (nibble & 1) diff += step >> 2;
228 predictor = c->predictor - diff;
230 predictor = c->predictor + diff;
232 c->predictor = av_clip_int16(predictor);
233 c->step_index = step_index;
238 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
242 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
243 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
245 c->sample2 = c->sample1;
246 c->sample1 = av_clip_int16(predictor);
247 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
248 if (c->idelta < 16) c->idelta = 16;
249 if (c->idelta > INT_MAX/768) {
250 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
251 c->idelta = INT_MAX/768;
257 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
259 int step_index, predictor, sign, delta, diff, step;
261 step = ff_adpcm_oki_step_table[c->step_index];
262 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
263 step_index = av_clip(step_index, 0, 48);
267 diff = ((2 * delta + 1) * step) >> 3;
268 predictor = c->predictor;
269 if (sign) predictor -= diff;
270 else predictor += diff;
272 c->predictor = av_clip_intp2(predictor, 11);
273 c->step_index = step_index;
275 return c->predictor << 4;
278 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
280 int sign, delta, diff;
285 /* perform direct multiplication instead of series of jumps proposed by
286 * the reference ADPCM implementation since modern CPUs can do the mults
288 diff = ((2 * delta + 1) * c->step) >> 3;
289 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
290 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
291 c->predictor = av_clip_int16(c->predictor);
292 /* calculate new step and clamp it to range 511..32767 */
293 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
294 c->step = av_clip(new_step, 511, 32767);
296 return (short)c->predictor;
299 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
301 int sign, delta, diff;
303 sign = nibble & (1<<(size-1));
304 delta = nibble & ((1<<(size-1))-1);
305 diff = delta << (7 + c->step + shift);
308 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
310 /* calculate new step */
311 if (delta >= (2*size - 3) && c->step < 3)
313 else if (delta == 0 && c->step > 0)
316 return (short) c->predictor;
319 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
326 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
327 c->predictor = av_clip_int16(c->predictor);
328 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
329 c->step = av_clip(c->step, 127, 24567);
333 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
334 const uint8_t *in, ADPCMChannelStatus *left,
335 ADPCMChannelStatus *right, int channels, int sample_offset)
338 int shift,filter,f0,f1;
342 out0 += sample_offset;
346 out1 += sample_offset;
349 shift = 12 - (in[4+i*2] & 15);
350 filter = in[4+i*2] >> 4;
351 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
352 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
355 f0 = xa_adpcm_table[filter][0];
356 f1 = xa_adpcm_table[filter][1];
364 t = sign_extend(d, 4);
365 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
367 s_1 = av_clip_int16(s);
374 s_1 = right->sample1;
375 s_2 = right->sample2;
378 shift = 12 - (in[5+i*2] & 15);
379 filter = in[5+i*2] >> 4;
380 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
381 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
385 f0 = xa_adpcm_table[filter][0];
386 f1 = xa_adpcm_table[filter][1];
391 t = sign_extend(d >> 4, 4);
392 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
394 s_1 = av_clip_int16(s);
399 right->sample1 = s_1;
400 right->sample2 = s_2;
406 out0 += 28 * (3 - channels);
407 out1 += 28 * (3 - channels);
413 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
415 ADPCMDecodeContext *c = avctx->priv_data;
418 int k0, signmask, nb_bits, count;
419 int size = buf_size*8;
422 init_get_bits(&gb, buf, size);
424 //read bits & initial values
425 nb_bits = get_bits(&gb, 2)+2;
426 table = swf_index_tables[nb_bits-2];
427 k0 = 1 << (nb_bits-2);
428 signmask = 1 << (nb_bits-1);
430 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
431 for (i = 0; i < avctx->channels; i++) {
432 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
433 c->status[i].step_index = get_bits(&gb, 6);
436 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
439 for (i = 0; i < avctx->channels; i++) {
440 // similar to IMA adpcm
441 int delta = get_bits(&gb, nb_bits);
442 int step = ff_adpcm_step_table[c->status[i].step_index];
443 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
454 if (delta & signmask)
455 c->status[i].predictor -= vpdiff;
457 c->status[i].predictor += vpdiff;
459 c->status[i].step_index += table[delta & (~signmask)];
461 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
462 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
464 *samples++ = c->status[i].predictor;
471 * Get the number of samples that will be decoded from the packet.
472 * In one case, this is actually the maximum number of samples possible to
473 * decode with the given buf_size.
475 * @param[out] coded_samples set to the number of samples as coded in the
476 * packet, or 0 if the codec does not encode the
477 * number of samples in each frame.
478 * @param[out] approx_nb_samples set to non-zero if the number of samples
479 * returned is an approximation.
481 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
482 int buf_size, int *coded_samples, int *approx_nb_samples)
484 ADPCMDecodeContext *s = avctx->priv_data;
486 int ch = avctx->channels;
487 int has_coded_samples = 0;
491 *approx_nb_samples = 0;
496 switch (avctx->codec->id) {
497 /* constant, only check buf_size */
498 case AV_CODEC_ID_ADPCM_EA_XAS:
499 if (buf_size < 76 * ch)
503 case AV_CODEC_ID_ADPCM_IMA_QT:
504 if (buf_size < 34 * ch)
508 /* simple 4-bit adpcm */
509 case AV_CODEC_ID_ADPCM_CT:
510 case AV_CODEC_ID_ADPCM_IMA_APC:
511 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
512 case AV_CODEC_ID_ADPCM_IMA_OKI:
513 case AV_CODEC_ID_ADPCM_IMA_WS:
514 case AV_CODEC_ID_ADPCM_YAMAHA:
515 nb_samples = buf_size * 2 / ch;
521 /* simple 4-bit adpcm, with header */
523 switch (avctx->codec->id) {
524 case AV_CODEC_ID_ADPCM_4XM:
525 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
526 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
527 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
530 return (buf_size - header_size) * 2 / ch;
532 /* more complex formats */
533 switch (avctx->codec->id) {
534 case AV_CODEC_ID_ADPCM_EA:
535 has_coded_samples = 1;
536 *coded_samples = bytestream2_get_le32(gb);
537 *coded_samples -= *coded_samples % 28;
538 nb_samples = (buf_size - 12) / 30 * 28;
540 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
541 has_coded_samples = 1;
542 *coded_samples = bytestream2_get_le32(gb);
543 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
545 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
546 nb_samples = (buf_size - ch) / ch * 2;
548 case AV_CODEC_ID_ADPCM_EA_R1:
549 case AV_CODEC_ID_ADPCM_EA_R2:
550 case AV_CODEC_ID_ADPCM_EA_R3:
551 /* maximum number of samples */
552 /* has internal offsets and a per-frame switch to signal raw 16-bit */
553 has_coded_samples = 1;
554 switch (avctx->codec->id) {
555 case AV_CODEC_ID_ADPCM_EA_R1:
556 header_size = 4 + 9 * ch;
557 *coded_samples = bytestream2_get_le32(gb);
559 case AV_CODEC_ID_ADPCM_EA_R2:
560 header_size = 4 + 5 * ch;
561 *coded_samples = bytestream2_get_le32(gb);
563 case AV_CODEC_ID_ADPCM_EA_R3:
564 header_size = 4 + 5 * ch;
565 *coded_samples = bytestream2_get_be32(gb);
568 *coded_samples -= *coded_samples % 28;
569 nb_samples = (buf_size - header_size) * 2 / ch;
570 nb_samples -= nb_samples % 28;
571 *approx_nb_samples = 1;
573 case AV_CODEC_ID_ADPCM_IMA_DK3:
574 if (avctx->block_align > 0)
575 buf_size = FFMIN(buf_size, avctx->block_align);
576 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
578 case AV_CODEC_ID_ADPCM_IMA_DK4:
579 if (avctx->block_align > 0)
580 buf_size = FFMIN(buf_size, avctx->block_align);
581 if (buf_size < 4 * ch)
582 return AVERROR_INVALIDDATA;
583 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
585 case AV_CODEC_ID_ADPCM_IMA_RAD:
586 if (avctx->block_align > 0)
587 buf_size = FFMIN(buf_size, avctx->block_align);
588 nb_samples = (buf_size - 4 * ch) * 2 / ch;
590 case AV_CODEC_ID_ADPCM_IMA_WAV:
592 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
593 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
594 if (avctx->block_align > 0)
595 buf_size = FFMIN(buf_size, avctx->block_align);
596 if (buf_size < 4 * ch)
597 return AVERROR_INVALIDDATA;
598 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
601 case AV_CODEC_ID_ADPCM_MS:
602 if (avctx->block_align > 0)
603 buf_size = FFMIN(buf_size, avctx->block_align);
604 nb_samples = (buf_size - 6 * ch) * 2 / ch;
606 case AV_CODEC_ID_ADPCM_SBPRO_2:
607 case AV_CODEC_ID_ADPCM_SBPRO_3:
608 case AV_CODEC_ID_ADPCM_SBPRO_4:
610 int samples_per_byte;
611 switch (avctx->codec->id) {
612 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
613 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
614 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
616 if (!s->status[0].step_index) {
618 return AVERROR_INVALIDDATA;
622 nb_samples += buf_size * samples_per_byte / ch;
625 case AV_CODEC_ID_ADPCM_SWF:
627 int buf_bits = buf_size * 8 - 2;
628 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
629 int block_hdr_size = 22 * ch;
630 int block_size = block_hdr_size + nbits * ch * 4095;
631 int nblocks = buf_bits / block_size;
632 int bits_left = buf_bits - nblocks * block_size;
633 nb_samples = nblocks * 4096;
634 if (bits_left >= block_hdr_size)
635 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
638 case AV_CODEC_ID_ADPCM_THP:
639 if (avctx->extradata) {
640 nb_samples = buf_size / (8 * ch) * 14;
643 has_coded_samples = 1;
644 bytestream2_skip(gb, 4); // channel size
645 *coded_samples = bytestream2_get_be32(gb);
646 *coded_samples -= *coded_samples % 14;
647 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
649 case AV_CODEC_ID_ADPCM_AFC:
650 nb_samples = buf_size / (9 * ch) * 16;
652 case AV_CODEC_ID_ADPCM_XA:
653 nb_samples = (buf_size / 128) * 224 / ch;
655 case AV_CODEC_ID_ADPCM_DTK:
656 nb_samples = buf_size / (16 * ch) * 28;
660 /* validate coded sample count */
661 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
662 return AVERROR_INVALIDDATA;
667 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
668 int *got_frame_ptr, AVPacket *avpkt)
670 AVFrame *frame = data;
671 const uint8_t *buf = avpkt->data;
672 int buf_size = avpkt->size;
673 ADPCMDecodeContext *c = avctx->priv_data;
674 ADPCMChannelStatus *cs;
675 int n, m, channel, i;
680 int nb_samples, coded_samples, approx_nb_samples, ret;
683 bytestream2_init(&gb, buf, buf_size);
684 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
685 if (nb_samples <= 0) {
686 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
687 return AVERROR_INVALIDDATA;
690 /* get output buffer */
691 frame->nb_samples = nb_samples;
692 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
694 samples = (short *)frame->data[0];
695 samples_p = (int16_t **)frame->extended_data;
697 /* use coded_samples when applicable */
698 /* it is always <= nb_samples, so the output buffer will be large enough */
700 if (!approx_nb_samples && coded_samples != nb_samples)
701 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
702 frame->nb_samples = nb_samples = coded_samples;
705 st = avctx->channels == 2 ? 1 : 0;
707 switch(avctx->codec->id) {
708 case AV_CODEC_ID_ADPCM_IMA_QT:
709 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
710 Channel data is interleaved per-chunk. */
711 for (channel = 0; channel < avctx->channels; channel++) {
714 cs = &(c->status[channel]);
715 /* (pppppp) (piiiiiii) */
717 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
718 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
719 step_index = predictor & 0x7F;
722 if (cs->step_index == step_index) {
723 int diff = predictor - cs->predictor;
730 cs->step_index = step_index;
731 cs->predictor = predictor;
734 if (cs->step_index > 88u){
735 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
736 channel, cs->step_index);
737 return AVERROR_INVALIDDATA;
740 samples = samples_p[channel];
742 for (m = 0; m < 64; m += 2) {
743 int byte = bytestream2_get_byteu(&gb);
744 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
745 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
749 case AV_CODEC_ID_ADPCM_IMA_WAV:
750 for(i=0; i<avctx->channels; i++){
751 cs = &(c->status[i]);
752 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
754 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
755 if (cs->step_index > 88u){
756 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
758 return AVERROR_INVALIDDATA;
762 if (avctx->bits_per_coded_sample != 4) {
763 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
766 init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
767 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
768 for (i = 0; i < avctx->channels; i++) {
770 samples = &samples_p[i][1 + n * samples_per_block];
771 for (m = 0; m < samples_per_block; m++) {
772 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
773 avctx->bits_per_coded_sample);
777 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
779 for (n = 0; n < (nb_samples - 1) / 8; n++) {
780 for (i = 0; i < avctx->channels; i++) {
782 samples = &samples_p[i][1 + n * 8];
783 for (m = 0; m < 8; m += 2) {
784 int v = bytestream2_get_byteu(&gb);
785 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
786 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
792 case AV_CODEC_ID_ADPCM_4XM:
793 for (i = 0; i < avctx->channels; i++)
794 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
796 for (i = 0; i < avctx->channels; i++) {
797 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
798 if (c->status[i].step_index > 88u) {
799 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
800 i, c->status[i].step_index);
801 return AVERROR_INVALIDDATA;
805 for (i = 0; i < avctx->channels; i++) {
806 samples = (int16_t *)frame->data[i];
808 for (n = nb_samples >> 1; n > 0; n--) {
809 int v = bytestream2_get_byteu(&gb);
810 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
811 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
815 case AV_CODEC_ID_ADPCM_MS:
819 block_predictor = bytestream2_get_byteu(&gb);
820 if (block_predictor > 6) {
821 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
823 return AVERROR_INVALIDDATA;
825 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
826 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
828 block_predictor = bytestream2_get_byteu(&gb);
829 if (block_predictor > 6) {
830 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
832 return AVERROR_INVALIDDATA;
834 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
835 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
837 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
839 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
842 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
843 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
844 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
845 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
847 *samples++ = c->status[0].sample2;
848 if (st) *samples++ = c->status[1].sample2;
849 *samples++ = c->status[0].sample1;
850 if (st) *samples++ = c->status[1].sample1;
851 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
852 int byte = bytestream2_get_byteu(&gb);
853 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
854 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
858 case AV_CODEC_ID_ADPCM_IMA_DK4:
859 for (channel = 0; channel < avctx->channels; channel++) {
860 cs = &c->status[channel];
861 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
862 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
863 if (cs->step_index > 88u){
864 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
865 channel, cs->step_index);
866 return AVERROR_INVALIDDATA;
869 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
870 int v = bytestream2_get_byteu(&gb);
871 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
872 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
875 case AV_CODEC_ID_ADPCM_IMA_DK3:
879 int decode_top_nibble_next = 0;
881 const int16_t *samples_end = samples + avctx->channels * nb_samples;
883 bytestream2_skipu(&gb, 10);
884 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
885 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
886 c->status[0].step_index = bytestream2_get_byteu(&gb);
887 c->status[1].step_index = bytestream2_get_byteu(&gb);
888 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
889 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
890 c->status[0].step_index, c->status[1].step_index);
891 return AVERROR_INVALIDDATA;
893 /* sign extend the predictors */
894 diff_channel = c->status[1].predictor;
896 /* DK3 ADPCM support macro */
897 #define DK3_GET_NEXT_NIBBLE() \
898 if (decode_top_nibble_next) { \
899 nibble = last_byte >> 4; \
900 decode_top_nibble_next = 0; \
902 last_byte = bytestream2_get_byteu(&gb); \
903 nibble = last_byte & 0x0F; \
904 decode_top_nibble_next = 1; \
907 while (samples < samples_end) {
909 /* for this algorithm, c->status[0] is the sum channel and
910 * c->status[1] is the diff channel */
912 /* process the first predictor of the sum channel */
913 DK3_GET_NEXT_NIBBLE();
914 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
916 /* process the diff channel predictor */
917 DK3_GET_NEXT_NIBBLE();
918 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
920 /* process the first pair of stereo PCM samples */
921 diff_channel = (diff_channel + c->status[1].predictor) / 2;
922 *samples++ = c->status[0].predictor + c->status[1].predictor;
923 *samples++ = c->status[0].predictor - c->status[1].predictor;
925 /* process the second predictor of the sum channel */
926 DK3_GET_NEXT_NIBBLE();
927 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
929 /* process the second pair of stereo PCM samples */
930 diff_channel = (diff_channel + c->status[1].predictor) / 2;
931 *samples++ = c->status[0].predictor + c->status[1].predictor;
932 *samples++ = c->status[0].predictor - c->status[1].predictor;
935 if ((bytestream2_tell(&gb) & 1))
936 bytestream2_skip(&gb, 1);
939 case AV_CODEC_ID_ADPCM_IMA_ISS:
940 for (channel = 0; channel < avctx->channels; channel++) {
941 cs = &c->status[channel];
942 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
943 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
944 if (cs->step_index > 88u){
945 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
946 channel, cs->step_index);
947 return AVERROR_INVALIDDATA;
951 for (n = nb_samples >> (1 - st); n > 0; n--) {
953 int v = bytestream2_get_byteu(&gb);
954 /* nibbles are swapped for mono */
962 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
963 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
966 case AV_CODEC_ID_ADPCM_IMA_APC:
967 while (bytestream2_get_bytes_left(&gb) > 0) {
968 int v = bytestream2_get_byteu(&gb);
969 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
970 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
973 case AV_CODEC_ID_ADPCM_IMA_OKI:
974 while (bytestream2_get_bytes_left(&gb) > 0) {
975 int v = bytestream2_get_byteu(&gb);
976 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
977 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
980 case AV_CODEC_ID_ADPCM_IMA_RAD:
981 for (channel = 0; channel < avctx->channels; channel++) {
982 cs = &c->status[channel];
983 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
984 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
985 if (cs->step_index > 88u){
986 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
987 channel, cs->step_index);
988 return AVERROR_INVALIDDATA;
991 for (n = 0; n < nb_samples / 2; n++) {
994 byte[0] = bytestream2_get_byteu(&gb);
996 byte[1] = bytestream2_get_byteu(&gb);
997 for(channel = 0; channel < avctx->channels; channel++) {
998 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1000 for(channel = 0; channel < avctx->channels; channel++) {
1001 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1005 case AV_CODEC_ID_ADPCM_IMA_WS:
1006 if (c->vqa_version == 3) {
1007 for (channel = 0; channel < avctx->channels; channel++) {
1008 int16_t *smp = samples_p[channel];
1010 for (n = nb_samples / 2; n > 0; n--) {
1011 int v = bytestream2_get_byteu(&gb);
1012 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1013 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1017 for (n = nb_samples / 2; n > 0; n--) {
1018 for (channel = 0; channel < avctx->channels; channel++) {
1019 int v = bytestream2_get_byteu(&gb);
1020 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1021 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1023 samples += avctx->channels;
1026 bytestream2_seek(&gb, 0, SEEK_END);
1028 case AV_CODEC_ID_ADPCM_XA:
1030 int16_t *out0 = samples_p[0];
1031 int16_t *out1 = samples_p[1];
1032 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1033 int sample_offset = 0;
1034 while (bytestream2_get_bytes_left(&gb) >= 128) {
1035 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1036 &c->status[0], &c->status[1],
1037 avctx->channels, sample_offset)) < 0)
1039 bytestream2_skipu(&gb, 128);
1040 sample_offset += samples_per_block;
1044 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1045 for (i=0; i<=st; i++) {
1046 c->status[i].step_index = bytestream2_get_le32u(&gb);
1047 if (c->status[i].step_index > 88u) {
1048 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1049 i, c->status[i].step_index);
1050 return AVERROR_INVALIDDATA;
1053 for (i=0; i<=st; i++)
1054 c->status[i].predictor = bytestream2_get_le32u(&gb);
1056 for (n = nb_samples >> (1 - st); n > 0; n--) {
1057 int byte = bytestream2_get_byteu(&gb);
1058 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1059 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1062 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1063 for (n = nb_samples >> (1 - st); n > 0; n--) {
1064 int byte = bytestream2_get_byteu(&gb);
1065 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1066 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1069 case AV_CODEC_ID_ADPCM_EA:
1071 int previous_left_sample, previous_right_sample;
1072 int current_left_sample, current_right_sample;
1073 int next_left_sample, next_right_sample;
1074 int coeff1l, coeff2l, coeff1r, coeff2r;
1075 int shift_left, shift_right;
1077 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1078 each coding 28 stereo samples. */
1080 if(avctx->channels != 2)
1081 return AVERROR_INVALIDDATA;
1083 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1084 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1085 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1086 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1088 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1089 int byte = bytestream2_get_byteu(&gb);
1090 coeff1l = ea_adpcm_table[ byte >> 4 ];
1091 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1092 coeff1r = ea_adpcm_table[ byte & 0x0F];
1093 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1095 byte = bytestream2_get_byteu(&gb);
1096 shift_left = 20 - (byte >> 4);
1097 shift_right = 20 - (byte & 0x0F);
1099 for (count2 = 0; count2 < 28; count2++) {
1100 byte = bytestream2_get_byteu(&gb);
1101 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1102 next_right_sample = sign_extend(byte, 4) << shift_right;
1104 next_left_sample = (next_left_sample +
1105 (current_left_sample * coeff1l) +
1106 (previous_left_sample * coeff2l) + 0x80) >> 8;
1107 next_right_sample = (next_right_sample +
1108 (current_right_sample * coeff1r) +
1109 (previous_right_sample * coeff2r) + 0x80) >> 8;
1111 previous_left_sample = current_left_sample;
1112 current_left_sample = av_clip_int16(next_left_sample);
1113 previous_right_sample = current_right_sample;
1114 current_right_sample = av_clip_int16(next_right_sample);
1115 *samples++ = current_left_sample;
1116 *samples++ = current_right_sample;
1120 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1124 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1126 int coeff[2][2], shift[2];
1128 for(channel = 0; channel < avctx->channels; channel++) {
1129 int byte = bytestream2_get_byteu(&gb);
1131 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1132 shift[channel] = 20 - (byte & 0x0F);
1134 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1137 byte[0] = bytestream2_get_byteu(&gb);
1138 if (st) byte[1] = bytestream2_get_byteu(&gb);
1139 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1140 for(channel = 0; channel < avctx->channels; channel++) {
1141 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1143 c->status[channel].sample1 * coeff[channel][0] +
1144 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1145 c->status[channel].sample2 = c->status[channel].sample1;
1146 c->status[channel].sample1 = av_clip_int16(sample);
1147 *samples++ = c->status[channel].sample1;
1151 bytestream2_seek(&gb, 0, SEEK_END);
1154 case AV_CODEC_ID_ADPCM_EA_R1:
1155 case AV_CODEC_ID_ADPCM_EA_R2:
1156 case AV_CODEC_ID_ADPCM_EA_R3: {
1157 /* channel numbering
1159 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1160 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1161 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1162 int previous_sample, current_sample, next_sample;
1165 unsigned int channel;
1170 for (channel=0; channel<avctx->channels; channel++)
1171 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1172 bytestream2_get_le32(&gb)) +
1173 (avctx->channels + 1) * 4;
1175 for (channel=0; channel<avctx->channels; channel++) {
1176 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1177 samplesC = samples_p[channel];
1179 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1180 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1181 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1183 current_sample = c->status[channel].predictor;
1184 previous_sample = c->status[channel].prev_sample;
1187 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1188 int byte = bytestream2_get_byte(&gb);
1189 if (byte == 0xEE) { /* only seen in R2 and R3 */
1190 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1191 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1193 for (count2=0; count2<28; count2++)
1194 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1196 coeff1 = ea_adpcm_table[ byte >> 4 ];
1197 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1198 shift = 20 - (byte & 0x0F);
1200 for (count2=0; count2<28; count2++) {
1202 next_sample = sign_extend(byte, 4) << shift;
1204 byte = bytestream2_get_byte(&gb);
1205 next_sample = sign_extend(byte >> 4, 4) << shift;
1208 next_sample += (current_sample * coeff1) +
1209 (previous_sample * coeff2);
1210 next_sample = av_clip_int16(next_sample >> 8);
1212 previous_sample = current_sample;
1213 current_sample = next_sample;
1214 *samplesC++ = current_sample;
1220 } else if (count != count1) {
1221 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1222 count = FFMAX(count, count1);
1225 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1226 c->status[channel].predictor = current_sample;
1227 c->status[channel].prev_sample = previous_sample;
1231 frame->nb_samples = count * 28;
1232 bytestream2_seek(&gb, 0, SEEK_END);
1235 case AV_CODEC_ID_ADPCM_EA_XAS:
1236 for (channel=0; channel<avctx->channels; channel++) {
1237 int coeff[2][4], shift[4];
1238 int16_t *s = samples_p[channel];
1239 for (n = 0; n < 4; n++, s += 32) {
1240 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1242 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1245 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1246 shift[n] = 20 - (val & 0x0F);
1250 for (m=2; m<32; m+=2) {
1251 s = &samples_p[channel][m];
1252 for (n = 0; n < 4; n++, s += 32) {
1254 int byte = bytestream2_get_byteu(&gb);
1256 level = sign_extend(byte >> 4, 4) << shift[n];
1257 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1258 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1260 level = sign_extend(byte, 4) << shift[n];
1261 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1262 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1267 case AV_CODEC_ID_ADPCM_IMA_AMV:
1268 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1269 c->status[0].step_index = bytestream2_get_le16u(&gb);
1270 bytestream2_skipu(&gb, 4);
1271 if (c->status[0].step_index > 88u) {
1272 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1273 c->status[0].step_index);
1274 return AVERROR_INVALIDDATA;
1277 for (n = nb_samples >> (1 - st); n > 0; n--) {
1278 int v = bytestream2_get_byteu(&gb);
1280 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1281 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1284 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1285 for (i = 0; i < avctx->channels; i++) {
1286 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1287 c->status[i].step_index = bytestream2_get_byteu(&gb);
1288 bytestream2_skipu(&gb, 1);
1289 if (c->status[i].step_index > 88u) {
1290 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1291 c->status[i].step_index);
1292 return AVERROR_INVALIDDATA;
1296 for (n = nb_samples >> (1 - st); n > 0; n--) {
1297 int v = bytestream2_get_byteu(&gb);
1299 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1300 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1303 case AV_CODEC_ID_ADPCM_CT:
1304 for (n = nb_samples >> (1 - st); n > 0; n--) {
1305 int v = bytestream2_get_byteu(&gb);
1306 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1307 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1310 case AV_CODEC_ID_ADPCM_SBPRO_4:
1311 case AV_CODEC_ID_ADPCM_SBPRO_3:
1312 case AV_CODEC_ID_ADPCM_SBPRO_2:
1313 if (!c->status[0].step_index) {
1314 /* the first byte is a raw sample */
1315 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1317 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1318 c->status[0].step_index = 1;
1321 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1322 for (n = nb_samples >> (1 - st); n > 0; n--) {
1323 int byte = bytestream2_get_byteu(&gb);
1324 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1326 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1329 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1330 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1331 int byte = bytestream2_get_byteu(&gb);
1332 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1334 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1335 (byte >> 2) & 0x07, 3, 0);
1336 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1340 for (n = nb_samples >> (2 - st); n > 0; n--) {
1341 int byte = bytestream2_get_byteu(&gb);
1342 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1344 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1345 (byte >> 4) & 0x03, 2, 2);
1346 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1347 (byte >> 2) & 0x03, 2, 2);
1348 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1353 case AV_CODEC_ID_ADPCM_SWF:
1354 adpcm_swf_decode(avctx, buf, buf_size, samples);
1355 bytestream2_seek(&gb, 0, SEEK_END);
1357 case AV_CODEC_ID_ADPCM_YAMAHA:
1358 for (n = nb_samples >> (1 - st); n > 0; n--) {
1359 int v = bytestream2_get_byteu(&gb);
1360 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1361 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1364 case AV_CODEC_ID_ADPCM_AFC:
1366 int samples_per_block;
1369 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1370 samples_per_block = avctx->extradata[0] / 16;
1371 blocks = nb_samples / avctx->extradata[0];
1373 samples_per_block = nb_samples / 16;
1377 for (m = 0; m < blocks; m++) {
1378 for (channel = 0; channel < avctx->channels; channel++) {
1379 int prev1 = c->status[channel].sample1;
1380 int prev2 = c->status[channel].sample2;
1382 samples = samples_p[channel] + m * 16;
1383 /* Read in every sample for this channel. */
1384 for (i = 0; i < samples_per_block; i++) {
1385 int byte = bytestream2_get_byteu(&gb);
1386 int scale = 1 << (byte >> 4);
1387 int index = byte & 0xf;
1388 int factor1 = ff_adpcm_afc_coeffs[0][index];
1389 int factor2 = ff_adpcm_afc_coeffs[1][index];
1391 /* Decode 16 samples. */
1392 for (n = 0; n < 16; n++) {
1396 sampledat = sign_extend(byte, 4);
1398 byte = bytestream2_get_byteu(&gb);
1399 sampledat = sign_extend(byte >> 4, 4);
1402 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1403 ((sampledat * scale) << 11)) >> 11;
1404 *samples = av_clip_int16(sampledat);
1410 c->status[channel].sample1 = prev1;
1411 c->status[channel].sample2 = prev2;
1414 bytestream2_seek(&gb, 0, SEEK_END);
1417 case AV_CODEC_ID_ADPCM_THP:
1422 if (avctx->extradata) {
1424 if (avctx->extradata_size < 32 * avctx->channels) {
1425 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1426 return AVERROR_INVALIDDATA;
1429 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1430 for (i = 0; i < avctx->channels; i++)
1431 for (n = 0; n < 16; n++)
1432 table[i][n] = sign_extend(bytestream2_get_be16u(&tb), 16);
1434 for (i = 0; i < avctx->channels; i++)
1435 for (n = 0; n < 16; n++)
1436 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1438 /* Initialize the previous sample. */
1439 for (i = 0; i < avctx->channels; i++) {
1440 c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1441 c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1445 for (ch = 0; ch < avctx->channels; ch++) {
1446 samples = samples_p[ch];
1448 /* Read in every sample for this channel. */
1449 for (i = 0; i < nb_samples / 14; i++) {
1450 int byte = bytestream2_get_byteu(&gb);
1451 int index = (byte >> 4) & 7;
1452 unsigned int exp = byte & 0x0F;
1453 int factor1 = table[ch][index * 2];
1454 int factor2 = table[ch][index * 2 + 1];
1456 /* Decode 14 samples. */
1457 for (n = 0; n < 14; n++) {
1461 sampledat = sign_extend(byte, 4);
1463 byte = bytestream2_get_byteu(&gb);
1464 sampledat = sign_extend(byte >> 4, 4);
1467 sampledat = ((c->status[ch].sample1 * factor1
1468 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1469 *samples = av_clip_int16(sampledat);
1470 c->status[ch].sample2 = c->status[ch].sample1;
1471 c->status[ch].sample1 = *samples++;
1477 case AV_CODEC_ID_ADPCM_DTK:
1478 for (channel = 0; channel < avctx->channels; channel++) {
1479 samples = samples_p[channel];
1481 /* Read in every sample for this channel. */
1482 for (i = 0; i < nb_samples / 28; i++) {
1485 bytestream2_skipu(&gb, 1);
1486 header = bytestream2_get_byteu(&gb);
1487 bytestream2_skipu(&gb, 3 - channel);
1489 /* Decode 28 samples. */
1490 for (n = 0; n < 28; n++) {
1491 int32_t sampledat, prev;
1493 switch (header >> 4) {
1495 prev = (c->status[channel].sample1 * 0x3c);
1498 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1501 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1507 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1509 byte = bytestream2_get_byteu(&gb);
1511 sampledat = sign_extend(byte, 4);
1513 sampledat = sign_extend(byte >> 4, 4);
1515 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1516 *samples++ = av_clip_int16(sampledat >> 6);
1517 c->status[channel].sample2 = c->status[channel].sample1;
1518 c->status[channel].sample1 = sampledat;
1522 bytestream2_seek(&gb, 0, SEEK_SET);
1530 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1531 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1532 return AVERROR_INVALIDDATA;
1537 if (avpkt->size < bytestream2_tell(&gb)) {
1538 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1542 return bytestream2_tell(&gb);
1546 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1547 AV_SAMPLE_FMT_NONE };
1548 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1549 AV_SAMPLE_FMT_NONE };
1550 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1552 AV_SAMPLE_FMT_NONE };
1554 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1555 AVCodec ff_ ## name_ ## _decoder = { \
1557 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1558 .type = AVMEDIA_TYPE_AUDIO, \
1560 .priv_data_size = sizeof(ADPCMDecodeContext), \
1561 .init = adpcm_decode_init, \
1562 .decode = adpcm_decode_frame, \
1563 .capabilities = CODEC_CAP_DR1, \
1564 .sample_fmts = sample_fmts_, \
1567 /* Note: Do not forget to add new entries to the Makefile as well. */
1568 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1569 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1570 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1571 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1572 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1573 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1574 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1575 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1576 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1577 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1578 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1579 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1580 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1581 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1582 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1583 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1584 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1585 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1586 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1587 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1588 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1589 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1590 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1591 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1592 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1593 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1594 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1595 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1596 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1597 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1598 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");