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 CODEC_ID_ADPCM_EA:
102 case CODEC_ID_ADPCM_EA_R1:
103 case CODEC_ID_ADPCM_EA_R2:
104 case CODEC_ID_ADPCM_EA_R3:
105 case CODEC_ID_ADPCM_EA_XAS:
109 if (avctx->channels < min_channels || avctx->channels > max_channels) {
110 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
111 return AVERROR(EINVAL);
114 switch(avctx->codec->id) {
115 case CODEC_ID_ADPCM_CT:
116 c->status[0].step = c->status[1].step = 511;
118 case CODEC_ID_ADPCM_IMA_WAV:
119 if (avctx->bits_per_coded_sample != 4) {
120 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
124 case 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 CODEC_ID_ADPCM_IMA_WS:
131 if (avctx->extradata && avctx->extradata_size >= 42)
132 c->vqa_version = AV_RL16(avctx->extradata);
137 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
139 avcodec_get_frame_defaults(&c->frame);
140 avctx->coded_frame = &c->frame;
145 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
149 int sign, delta, diff, step;
151 step = ff_adpcm_step_table[c->step_index];
152 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
153 if (step_index < 0) step_index = 0;
154 else if (step_index > 88) step_index = 88;
158 /* perform direct multiplication instead of series of jumps proposed by
159 * the reference ADPCM implementation since modern CPUs can do the mults
161 diff = ((2 * delta + 1) * step) >> shift;
162 predictor = c->predictor;
163 if (sign) predictor -= diff;
164 else predictor += diff;
166 c->predictor = av_clip_int16(predictor);
167 c->step_index = step_index;
169 return (short)c->predictor;
172 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
178 step = ff_adpcm_step_table[c->step_index];
179 step_index = c->step_index + ff_adpcm_index_table[nibble];
180 step_index = av_clip(step_index, 0, 88);
183 if (nibble & 4) diff += step;
184 if (nibble & 2) diff += step >> 1;
185 if (nibble & 1) diff += step >> 2;
188 predictor = c->predictor - diff;
190 predictor = c->predictor + diff;
192 c->predictor = av_clip_int16(predictor);
193 c->step_index = step_index;
198 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
202 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
203 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
205 c->sample2 = c->sample1;
206 c->sample1 = av_clip_int16(predictor);
207 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
208 if (c->idelta < 16) c->idelta = 16;
213 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
215 int sign, delta, diff;
220 /* perform direct multiplication instead of series of jumps proposed by
221 * the reference ADPCM implementation since modern CPUs can do the mults
223 diff = ((2 * delta + 1) * c->step) >> 3;
224 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
225 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
226 c->predictor = av_clip_int16(c->predictor);
227 /* calculate new step and clamp it to range 511..32767 */
228 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
229 c->step = av_clip(new_step, 511, 32767);
231 return (short)c->predictor;
234 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
236 int sign, delta, diff;
238 sign = nibble & (1<<(size-1));
239 delta = nibble & ((1<<(size-1))-1);
240 diff = delta << (7 + c->step + shift);
243 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
245 /* calculate new step */
246 if (delta >= (2*size - 3) && c->step < 3)
248 else if (delta == 0 && c->step > 0)
251 return (short) c->predictor;
254 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
261 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
262 c->predictor = av_clip_int16(c->predictor);
263 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
264 c->step = av_clip(c->step, 127, 24567);
268 static void xa_decode(short *out, const unsigned char *in,
269 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
272 int shift,filter,f0,f1;
278 shift = 12 - (in[4+i*2] & 15);
279 filter = in[4+i*2] >> 4;
280 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
281 av_log_ask_for_sample(NULL, "unknown filter %d\n", filter);
284 f0 = xa_adpcm_table[filter][0];
285 f1 = xa_adpcm_table[filter][1];
293 t = (signed char)(d<<4)>>4;
294 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
296 s_1 = av_clip_int16(s);
301 if (inc==2) { /* stereo */
304 s_1 = right->sample1;
305 s_2 = right->sample2;
306 out = out + 1 - 28*2;
309 shift = 12 - (in[5+i*2] & 15);
310 filter = in[5+i*2] >> 4;
311 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
312 av_log_ask_for_sample(NULL, "unknown filter %d\n", filter);
316 f0 = xa_adpcm_table[filter][0];
317 f1 = xa_adpcm_table[filter][1];
322 t = (signed char)d >> 4;
323 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
325 s_1 = av_clip_int16(s);
330 if (inc==2) { /* stereo */
331 right->sample1 = s_1;
332 right->sample2 = s_2;
342 * Get the number of samples that will be decoded from the packet.
343 * In one case, this is actually the maximum number of samples possible to
344 * decode with the given buf_size.
346 * @param[out] coded_samples set to the number of samples as coded in the
347 * packet, or 0 if the codec does not encode the
348 * number of samples in each frame.
350 static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
351 int buf_size, int *coded_samples)
353 ADPCMDecodeContext *s = avctx->priv_data;
355 int ch = avctx->channels;
356 int has_coded_samples = 0;
364 switch (avctx->codec->id) {
365 /* constant, only check buf_size */
366 case CODEC_ID_ADPCM_EA_XAS:
367 if (buf_size < 76 * ch)
371 case CODEC_ID_ADPCM_IMA_QT:
372 if (buf_size < 34 * ch)
376 /* simple 4-bit adpcm */
377 case CODEC_ID_ADPCM_CT:
378 case CODEC_ID_ADPCM_IMA_APC:
379 case CODEC_ID_ADPCM_IMA_EA_SEAD:
380 case CODEC_ID_ADPCM_IMA_WS:
381 case CODEC_ID_ADPCM_YAMAHA:
382 nb_samples = buf_size * 2 / ch;
388 /* simple 4-bit adpcm, with header */
390 switch (avctx->codec->id) {
391 case CODEC_ID_ADPCM_4XM:
392 case CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
393 case CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
394 case CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
397 return (buf_size - header_size) * 2 / ch;
399 /* more complex formats */
400 switch (avctx->codec->id) {
401 case CODEC_ID_ADPCM_EA:
402 has_coded_samples = 1;
405 *coded_samples = AV_RL32(buf);
406 *coded_samples -= *coded_samples % 28;
407 nb_samples = (buf_size - 12) / 30 * 28;
409 case CODEC_ID_ADPCM_IMA_EA_EACS:
410 has_coded_samples = 1;
413 *coded_samples = AV_RL32(buf);
414 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
416 case CODEC_ID_ADPCM_EA_MAXIS_XA:
417 nb_samples = ((buf_size - ch) / (2 * ch)) * 2 * ch;
419 case CODEC_ID_ADPCM_EA_R1:
420 case CODEC_ID_ADPCM_EA_R2:
421 case CODEC_ID_ADPCM_EA_R3:
422 /* maximum number of samples */
423 /* has internal offsets and a per-frame switch to signal raw 16-bit */
424 has_coded_samples = 1;
427 switch (avctx->codec->id) {
428 case CODEC_ID_ADPCM_EA_R1:
429 header_size = 4 + 9 * ch;
430 *coded_samples = AV_RL32(buf);
432 case CODEC_ID_ADPCM_EA_R2:
433 header_size = 4 + 5 * ch;
434 *coded_samples = AV_RL32(buf);
436 case CODEC_ID_ADPCM_EA_R3:
437 header_size = 4 + 5 * ch;
438 *coded_samples = AV_RB32(buf);
441 *coded_samples -= *coded_samples % 28;
442 nb_samples = (buf_size - header_size) * 2 / ch;
443 nb_samples -= nb_samples % 28;
445 case CODEC_ID_ADPCM_IMA_DK3:
446 if (avctx->block_align > 0)
447 buf_size = FFMIN(buf_size, avctx->block_align);
448 nb_samples = ((buf_size - 16) * 8 / 3) / ch;
450 case CODEC_ID_ADPCM_IMA_DK4:
451 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
453 case CODEC_ID_ADPCM_IMA_WAV:
454 if (avctx->block_align > 0)
455 buf_size = FFMIN(buf_size, avctx->block_align);
456 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
458 case CODEC_ID_ADPCM_MS:
459 if (avctx->block_align > 0)
460 buf_size = FFMIN(buf_size, avctx->block_align);
461 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
463 case CODEC_ID_ADPCM_SBPRO_2:
464 case CODEC_ID_ADPCM_SBPRO_3:
465 case CODEC_ID_ADPCM_SBPRO_4:
467 int samples_per_byte;
468 switch (avctx->codec->id) {
469 case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
470 case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
471 case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
473 if (!s->status[0].step_index) {
477 nb_samples += buf_size * samples_per_byte / ch;
480 case CODEC_ID_ADPCM_SWF:
482 int buf_bits = buf_size * 8 - 2;
483 int nbits = (buf[0] >> 6) + 2;
484 int block_hdr_size = 22 * ch;
485 int block_size = block_hdr_size + nbits * ch * 4095;
486 int nblocks = buf_bits / block_size;
487 int bits_left = buf_bits - nblocks * block_size;
488 nb_samples = nblocks * 4096;
489 if (bits_left >= block_hdr_size)
490 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
493 case CODEC_ID_ADPCM_THP:
494 has_coded_samples = 1;
497 *coded_samples = AV_RB32(&buf[4]);
498 *coded_samples -= *coded_samples % 14;
499 nb_samples = (buf_size - 80) / (8 * ch) * 14;
501 case CODEC_ID_ADPCM_XA:
502 nb_samples = (buf_size / 128) * 224 / ch;
506 /* validate coded sample count */
507 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
508 return AVERROR_INVALIDDATA;
513 /* DK3 ADPCM support macro */
514 #define DK3_GET_NEXT_NIBBLE() \
515 if (decode_top_nibble_next) \
517 nibble = last_byte >> 4; \
518 decode_top_nibble_next = 0; \
524 last_byte = *src++; \
525 if (src >= buf + buf_size) \
527 nibble = last_byte & 0x0F; \
528 decode_top_nibble_next = 1; \
531 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
532 int *got_frame_ptr, AVPacket *avpkt)
534 const uint8_t *buf = avpkt->data;
535 int buf_size = avpkt->size;
536 ADPCMDecodeContext *c = avctx->priv_data;
537 ADPCMChannelStatus *cs;
538 int n, m, channel, i;
543 int nb_samples, coded_samples, ret;
545 nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
546 if (nb_samples <= 0) {
547 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
548 return AVERROR_INVALIDDATA;
551 /* get output buffer */
552 c->frame.nb_samples = nb_samples;
553 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
554 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
557 samples = (short *)c->frame.data[0];
559 /* use coded_samples when applicable */
560 /* it is always <= nb_samples, so the output buffer will be large enough */
562 if (coded_samples != nb_samples)
563 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
564 c->frame.nb_samples = nb_samples = coded_samples;
569 st = avctx->channels == 2 ? 1 : 0;
571 switch(avctx->codec->id) {
572 case CODEC_ID_ADPCM_IMA_QT:
573 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
574 Channel data is interleaved per-chunk. */
575 for (channel = 0; channel < avctx->channels; channel++) {
578 cs = &(c->status[channel]);
579 /* (pppppp) (piiiiiii) */
581 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
582 predictor = AV_RB16(src);
583 step_index = predictor & 0x7F;
588 if (cs->step_index == step_index) {
589 int diff = (int)predictor - cs->predictor;
596 cs->step_index = step_index;
597 cs->predictor = predictor;
600 if (cs->step_index > 88){
601 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
605 samples = (short *)c->frame.data[0] + channel;
607 for (m = 0; m < 32; m++) {
608 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
609 samples += avctx->channels;
610 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
611 samples += avctx->channels;
616 case CODEC_ID_ADPCM_IMA_WAV:
617 if (avctx->block_align != 0 && buf_size > avctx->block_align)
618 buf_size = avctx->block_align;
620 for(i=0; i<avctx->channels; i++){
621 cs = &(c->status[i]);
622 cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
624 cs->step_index = *src++;
625 if (cs->step_index > 88){
626 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
629 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
632 for (n = (nb_samples - 1) / 8; n > 0; n--) {
633 for (i = 0; i < avctx->channels; i++) {
635 for (m = 0; m < 4; m++) {
637 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
638 samples += avctx->channels;
639 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
640 samples += avctx->channels;
642 samples -= 8 * avctx->channels - 1;
644 samples += 7 * avctx->channels;
647 case CODEC_ID_ADPCM_4XM:
648 for (i = 0; i < avctx->channels; i++)
649 c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
651 for (i = 0; i < avctx->channels; i++) {
652 c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
653 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
656 for (i = 0; i < avctx->channels; i++) {
657 samples = (short *)c->frame.data[0] + i;
659 for (n = nb_samples >> 1; n > 0; n--, src++) {
661 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
662 samples += avctx->channels;
663 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
664 samples += avctx->channels;
668 case CODEC_ID_ADPCM_MS:
672 if (avctx->block_align != 0 && buf_size > avctx->block_align)
673 buf_size = avctx->block_align;
675 block_predictor = av_clip(*src++, 0, 6);
676 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
677 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
679 block_predictor = av_clip(*src++, 0, 6);
680 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
681 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
683 c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
685 c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
688 c->status[0].sample1 = bytestream_get_le16(&src);
689 if (st) c->status[1].sample1 = bytestream_get_le16(&src);
690 c->status[0].sample2 = bytestream_get_le16(&src);
691 if (st) c->status[1].sample2 = bytestream_get_le16(&src);
693 *samples++ = c->status[0].sample2;
694 if (st) *samples++ = c->status[1].sample2;
695 *samples++ = c->status[0].sample1;
696 if (st) *samples++ = c->status[1].sample1;
697 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--, src++) {
698 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
699 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
703 case CODEC_ID_ADPCM_IMA_DK4:
704 if (avctx->block_align != 0 && buf_size > avctx->block_align)
705 buf_size = avctx->block_align;
707 for (channel = 0; channel < avctx->channels; channel++) {
708 cs = &c->status[channel];
709 cs->predictor = (int16_t)bytestream_get_le16(&src);
710 cs->step_index = av_clip(*src++, 0, 88);
712 *samples++ = cs->predictor;
714 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
716 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
717 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
720 case CODEC_ID_ADPCM_IMA_DK3:
722 unsigned char last_byte = 0;
723 unsigned char nibble;
724 int decode_top_nibble_next = 0;
725 int end_of_packet = 0;
728 if (avctx->block_align != 0 && buf_size > avctx->block_align)
729 buf_size = avctx->block_align;
731 c->status[0].predictor = (int16_t)AV_RL16(src + 10);
732 c->status[1].predictor = (int16_t)AV_RL16(src + 12);
733 c->status[0].step_index = av_clip(src[14], 0, 88);
734 c->status[1].step_index = av_clip(src[15], 0, 88);
735 /* sign extend the predictors */
737 diff_channel = c->status[1].predictor;
739 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
740 * the buffer is consumed */
743 /* for this algorithm, c->status[0] is the sum channel and
744 * c->status[1] is the diff channel */
746 /* process the first predictor of the sum channel */
747 DK3_GET_NEXT_NIBBLE();
748 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
750 /* process the diff channel predictor */
751 DK3_GET_NEXT_NIBBLE();
752 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
754 /* process the first pair of stereo PCM samples */
755 diff_channel = (diff_channel + c->status[1].predictor) / 2;
756 *samples++ = c->status[0].predictor + c->status[1].predictor;
757 *samples++ = c->status[0].predictor - c->status[1].predictor;
759 /* process the second predictor of the sum channel */
760 DK3_GET_NEXT_NIBBLE();
761 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
763 /* process the second pair of stereo PCM samples */
764 diff_channel = (diff_channel + c->status[1].predictor) / 2;
765 *samples++ = c->status[0].predictor + c->status[1].predictor;
766 *samples++ = c->status[0].predictor - c->status[1].predictor;
770 case CODEC_ID_ADPCM_IMA_ISS:
771 for (channel = 0; channel < avctx->channels; channel++) {
772 cs = &c->status[channel];
773 cs->predictor = (int16_t)bytestream_get_le16(&src);
774 cs->step_index = av_clip(*src++, 0, 88);
778 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
781 /* nibbles are swapped for mono */
789 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
790 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
793 case CODEC_ID_ADPCM_IMA_APC:
794 while (src < buf + buf_size) {
796 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
797 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
800 case CODEC_ID_ADPCM_IMA_WS:
801 for (channel = 0; channel < avctx->channels; channel++) {
804 int16_t *smp = samples + channel;
806 if (c->vqa_version == 3) {
807 src0 = src + channel * buf_size / 2;
810 src0 = src + channel;
811 src_stride = avctx->channels;
813 for (n = nb_samples / 2; n > 0; n--) {
816 *smp = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
817 smp += avctx->channels;
818 *smp = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
819 smp += avctx->channels;
822 src = buf + buf_size;
824 case CODEC_ID_ADPCM_XA:
825 while (buf_size >= 128) {
826 xa_decode(samples, src, &c->status[0], &c->status[1],
833 case CODEC_ID_ADPCM_IMA_EA_EACS:
834 src += 4; // skip sample count (already read)
836 for (i=0; i<=st; i++)
837 c->status[i].step_index = av_clip(bytestream_get_le32(&src), 0, 88);
838 for (i=0; i<=st; i++)
839 c->status[i].predictor = bytestream_get_le32(&src);
841 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
842 *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
843 *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
846 case CODEC_ID_ADPCM_IMA_EA_SEAD:
847 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
848 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
849 *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
852 case CODEC_ID_ADPCM_EA:
854 int32_t previous_left_sample, previous_right_sample;
855 int32_t current_left_sample, current_right_sample;
856 int32_t next_left_sample, next_right_sample;
857 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
858 uint8_t shift_left, shift_right;
860 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
861 each coding 28 stereo samples. */
863 if(avctx->channels != 2)
864 return AVERROR_INVALIDDATA;
866 src += 4; // skip sample count (already read)
868 current_left_sample = (int16_t)bytestream_get_le16(&src);
869 previous_left_sample = (int16_t)bytestream_get_le16(&src);
870 current_right_sample = (int16_t)bytestream_get_le16(&src);
871 previous_right_sample = (int16_t)bytestream_get_le16(&src);
873 for (count1 = 0; count1 < nb_samples / 28; count1++) {
874 coeff1l = ea_adpcm_table[ *src >> 4 ];
875 coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
876 coeff1r = ea_adpcm_table[*src & 0x0F];
877 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
880 shift_left = 20 - (*src >> 4);
881 shift_right = 20 - (*src & 0x0F);
884 for (count2 = 0; count2 < 28; count2++) {
885 next_left_sample = sign_extend(*src >> 4, 4) << shift_left;
886 next_right_sample = sign_extend(*src, 4) << shift_right;
889 next_left_sample = (next_left_sample +
890 (current_left_sample * coeff1l) +
891 (previous_left_sample * coeff2l) + 0x80) >> 8;
892 next_right_sample = (next_right_sample +
893 (current_right_sample * coeff1r) +
894 (previous_right_sample * coeff2r) + 0x80) >> 8;
896 previous_left_sample = current_left_sample;
897 current_left_sample = av_clip_int16(next_left_sample);
898 previous_right_sample = current_right_sample;
899 current_right_sample = av_clip_int16(next_right_sample);
900 *samples++ = (unsigned short)current_left_sample;
901 *samples++ = (unsigned short)current_right_sample;
905 if (src - buf == buf_size - 2)
906 src += 2; // Skip terminating 0x0000
910 case CODEC_ID_ADPCM_EA_MAXIS_XA:
912 int coeff[2][2], shift[2];
914 for(channel = 0; channel < avctx->channels; channel++) {
916 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
917 shift[channel] = 20 - (*src & 0x0F);
920 for (count1 = 0; count1 < nb_samples / 2; count1++) {
921 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
922 for(channel = 0; channel < avctx->channels; channel++) {
923 int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
925 c->status[channel].sample1 * coeff[channel][0] +
926 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
927 c->status[channel].sample2 = c->status[channel].sample1;
928 c->status[channel].sample1 = av_clip_int16(sample);
929 *samples++ = c->status[channel].sample1;
932 src+=avctx->channels;
934 /* consume whole packet */
935 src = buf + buf_size;
938 case CODEC_ID_ADPCM_EA_R1:
939 case CODEC_ID_ADPCM_EA_R2:
940 case CODEC_ID_ADPCM_EA_R3: {
943 4chan: 0=fl, 1=rl, 2=fr, 3=rr
944 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
945 const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
946 int32_t previous_sample, current_sample, next_sample;
947 int32_t coeff1, coeff2;
949 unsigned int channel;
952 const uint8_t *src_end = buf + buf_size;
955 src += 4; // skip sample count (already read)
957 for (channel=0; channel<avctx->channels; channel++) {
958 int32_t offset = (big_endian ? bytestream_get_be32(&src)
959 : bytestream_get_le32(&src))
960 + (avctx->channels-channel-1) * 4;
962 if ((offset < 0) || (offset >= src_end - src - 4)) break;
964 samplesC = samples + channel;
966 if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
967 current_sample = (int16_t)bytestream_get_le16(&srcC);
968 previous_sample = (int16_t)bytestream_get_le16(&srcC);
970 current_sample = c->status[channel].predictor;
971 previous_sample = c->status[channel].prev_sample;
974 for (count1 = 0; count1 < nb_samples / 28; count1++) {
975 if (*srcC == 0xEE) { /* only seen in R2 and R3 */
977 if (srcC > src_end - 30*2) break;
978 current_sample = (int16_t)bytestream_get_be16(&srcC);
979 previous_sample = (int16_t)bytestream_get_be16(&srcC);
981 for (count2=0; count2<28; count2++) {
982 *samplesC = (int16_t)bytestream_get_be16(&srcC);
983 samplesC += avctx->channels;
986 coeff1 = ea_adpcm_table[ *srcC>>4 ];
987 coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
988 shift = 20 - (*srcC++ & 0x0F);
990 if (srcC > src_end - 14) break;
991 for (count2=0; count2<28; count2++) {
993 next_sample = sign_extend(*srcC++, 4) << shift;
995 next_sample = sign_extend(*srcC >> 4, 4) << shift;
997 next_sample += (current_sample * coeff1) +
998 (previous_sample * coeff2);
999 next_sample = av_clip_int16(next_sample >> 8);
1001 previous_sample = current_sample;
1002 current_sample = next_sample;
1003 *samplesC = current_sample;
1004 samplesC += avctx->channels;
1010 } else if (count != count1) {
1011 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1012 count = FFMAX(count, count1);
1015 if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
1016 c->status[channel].predictor = current_sample;
1017 c->status[channel].prev_sample = previous_sample;
1021 c->frame.nb_samples = count * 28;
1025 case CODEC_ID_ADPCM_EA_XAS:
1026 for (channel=0; channel<avctx->channels; channel++) {
1027 int coeff[2][4], shift[4];
1028 short *s2, *s = &samples[channel];
1029 for (n=0; n<4; n++, s+=32*avctx->channels) {
1031 coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
1032 shift[n] = 20 - (src[2] & 0x0F);
1033 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
1034 s2[0] = (src[0]&0xF0) + (src[1]<<8);
1037 for (m=2; m<32; m+=2) {
1038 s = &samples[m*avctx->channels + channel];
1039 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
1040 for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
1041 int level = sign_extend(*src >> (4 - i), 4) << shift[n];
1042 int pred = s2[-1*avctx->channels] * coeff[0][n]
1043 + s2[-2*avctx->channels] * coeff[1][n];
1044 s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
1050 case CODEC_ID_ADPCM_IMA_AMV:
1051 case CODEC_ID_ADPCM_IMA_SMJPEG:
1052 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
1053 c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16);
1054 c->status[0].step_index = av_clip(bytestream_get_le16(&src), 0, 88);
1057 c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16);
1058 c->status[0].step_index = av_clip(bytestream_get_byte(&src), 0, 88);
1062 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1067 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1068 FFSWAP(char, hi, lo);
1070 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1072 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1076 case CODEC_ID_ADPCM_CT:
1077 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1079 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1080 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1083 case CODEC_ID_ADPCM_SBPRO_4:
1084 case CODEC_ID_ADPCM_SBPRO_3:
1085 case CODEC_ID_ADPCM_SBPRO_2:
1086 if (!c->status[0].step_index) {
1087 /* the first byte is a raw sample */
1088 *samples++ = 128 * (*src++ - 0x80);
1090 *samples++ = 128 * (*src++ - 0x80);
1091 c->status[0].step_index = 1;
1094 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1095 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1096 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1098 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1099 src[0] & 0x0F, 4, 0);
1101 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1102 for (n = nb_samples / 3; n > 0; n--, src++) {
1103 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1104 src[0] >> 5 , 3, 0);
1105 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1106 (src[0] >> 2) & 0x07, 3, 0);
1107 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1108 src[0] & 0x03, 2, 0);
1111 for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
1112 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1113 src[0] >> 6 , 2, 2);
1114 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1115 (src[0] >> 4) & 0x03, 2, 2);
1116 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1117 (src[0] >> 2) & 0x03, 2, 2);
1118 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1119 src[0] & 0x03, 2, 2);
1123 case CODEC_ID_ADPCM_SWF:
1127 int k0, signmask, nb_bits, count;
1128 int size = buf_size*8;
1130 init_get_bits(&gb, buf, size);
1132 //read bits & initial values
1133 nb_bits = get_bits(&gb, 2)+2;
1134 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1135 table = swf_index_tables[nb_bits-2];
1136 k0 = 1 << (nb_bits-2);
1137 signmask = 1 << (nb_bits-1);
1139 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1140 for (i = 0; i < avctx->channels; i++) {
1141 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1142 c->status[i].step_index = get_bits(&gb, 6);
1145 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
1148 for (i = 0; i < avctx->channels; i++) {
1149 // similar to IMA adpcm
1150 int delta = get_bits(&gb, nb_bits);
1151 int step = ff_adpcm_step_table[c->status[i].step_index];
1152 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1163 if (delta & signmask)
1164 c->status[i].predictor -= vpdiff;
1166 c->status[i].predictor += vpdiff;
1168 c->status[i].step_index += table[delta & (~signmask)];
1170 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1171 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1173 *samples++ = c->status[i].predictor;
1180 case CODEC_ID_ADPCM_YAMAHA:
1181 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1183 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1184 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1187 case CODEC_ID_ADPCM_THP:
1193 src += 4; // skip channel size
1194 src += 4; // skip number of samples (already read)
1196 for (i = 0; i < 32; i++)
1197 table[0][i] = (int16_t)bytestream_get_be16(&src);
1199 /* Initialize the previous sample. */
1200 for (i = 0; i < 4; i++)
1201 prev[i>>1][i&1] = (int16_t)bytestream_get_be16(&src);
1203 for (ch = 0; ch <= st; ch++) {
1204 samples = (short *)c->frame.data[0] + ch;
1206 /* Read in every sample for this channel. */
1207 for (i = 0; i < nb_samples / 14; i++) {
1208 int index = (*src >> 4) & 7;
1209 unsigned int exp = *src++ & 15;
1210 int factor1 = table[ch][index * 2];
1211 int factor2 = table[ch][index * 2 + 1];
1213 /* Decode 14 samples. */
1214 for (n = 0; n < 14; n++) {
1216 if(n&1) sampledat = sign_extend(*src++, 4);
1217 else sampledat = sign_extend(*src >> 4, 4);
1219 sampledat = ((prev[ch][0]*factor1
1220 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1221 *samples = av_clip_int16(sampledat);
1222 prev[ch][1] = prev[ch][0];
1223 prev[ch][0] = *samples++;
1225 /* In case of stereo, skip one sample, this sample
1226 is for the other channel. */
1239 *(AVFrame *)data = c->frame;
1245 #define ADPCM_DECODER(id_, name_, long_name_) \
1246 AVCodec ff_ ## name_ ## _decoder = { \
1248 .type = AVMEDIA_TYPE_AUDIO, \
1250 .priv_data_size = sizeof(ADPCMDecodeContext), \
1251 .init = adpcm_decode_init, \
1252 .decode = adpcm_decode_frame, \
1253 .capabilities = CODEC_CAP_DR1, \
1254 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1257 /* Note: Do not forget to add new entries to the Makefile as well. */
1258 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1259 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1260 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1261 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1262 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1263 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1264 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1265 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1266 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1267 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1268 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1269 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1270 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1271 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1272 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1273 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1274 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1275 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1276 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1277 ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1278 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1279 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1280 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1281 ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1282 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1283 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1284 ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");