2 * Copyright (c) 2001-2003 The ffmpeg Project
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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];
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 CODEC_ID_ADPCM_EA:
101 case CODEC_ID_ADPCM_EA_R1:
102 case CODEC_ID_ADPCM_EA_R2:
103 case CODEC_ID_ADPCM_EA_R3:
104 case CODEC_ID_ADPCM_EA_XAS:
108 if (avctx->channels < min_channels || avctx->channels > max_channels) {
109 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
110 return AVERROR(EINVAL);
113 switch(avctx->codec->id) {
114 case CODEC_ID_ADPCM_CT:
115 c->status[0].step = c->status[1].step = 511;
117 case CODEC_ID_ADPCM_IMA_WAV:
118 if (avctx->bits_per_coded_sample != 4) {
119 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
123 case CODEC_ID_ADPCM_IMA_WS:
124 if (avctx->extradata && avctx->extradata_size == 2 * 4) {
125 c->status[0].predictor = AV_RL32(avctx->extradata);
126 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
132 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
134 avcodec_get_frame_defaults(&c->frame);
135 avctx->coded_frame = &c->frame;
140 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
144 int sign, delta, diff, step;
146 step = ff_adpcm_step_table[c->step_index];
147 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
148 if (step_index < 0) step_index = 0;
149 else if (step_index > 88) step_index = 88;
153 /* perform direct multiplication instead of series of jumps proposed by
154 * the reference ADPCM implementation since modern CPUs can do the mults
156 diff = ((2 * delta + 1) * step) >> shift;
157 predictor = c->predictor;
158 if (sign) predictor -= diff;
159 else predictor += diff;
161 c->predictor = av_clip_int16(predictor);
162 c->step_index = step_index;
164 return (short)c->predictor;
167 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
173 step = ff_adpcm_step_table[c->step_index];
174 step_index = c->step_index + ff_adpcm_index_table[nibble];
175 step_index = av_clip(step_index, 0, 88);
178 if (nibble & 4) diff += step;
179 if (nibble & 2) diff += step >> 1;
180 if (nibble & 1) diff += step >> 2;
183 predictor = c->predictor - diff;
185 predictor = c->predictor + diff;
187 c->predictor = av_clip_int16(predictor);
188 c->step_index = step_index;
193 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
197 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
198 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
200 c->sample2 = c->sample1;
201 c->sample1 = av_clip_int16(predictor);
202 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
203 if (c->idelta < 16) c->idelta = 16;
208 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
210 int sign, delta, diff;
215 /* perform direct multiplication instead of series of jumps proposed by
216 * the reference ADPCM implementation since modern CPUs can do the mults
218 diff = ((2 * delta + 1) * c->step) >> 3;
219 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
220 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
221 c->predictor = av_clip_int16(c->predictor);
222 /* calculate new step and clamp it to range 511..32767 */
223 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
224 c->step = av_clip(new_step, 511, 32767);
226 return (short)c->predictor;
229 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
231 int sign, delta, diff;
233 sign = nibble & (1<<(size-1));
234 delta = nibble & ((1<<(size-1))-1);
235 diff = delta << (7 + c->step + shift);
238 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
240 /* calculate new step */
241 if (delta >= (2*size - 3) && c->step < 3)
243 else if (delta == 0 && c->step > 0)
246 return (short) c->predictor;
249 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
256 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
257 c->predictor = av_clip_int16(c->predictor);
258 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
259 c->step = av_clip(c->step, 127, 24567);
263 static void xa_decode(short *out, const unsigned char *in,
264 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
267 int shift,filter,f0,f1;
273 shift = 12 - (in[4+i*2] & 15);
274 filter = in[4+i*2] >> 4;
275 f0 = xa_adpcm_table[filter][0];
276 f1 = xa_adpcm_table[filter][1];
284 t = (signed char)(d<<4)>>4;
285 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
287 s_1 = av_clip_int16(s);
292 if (inc==2) { /* stereo */
295 s_1 = right->sample1;
296 s_2 = right->sample2;
297 out = out + 1 - 28*2;
300 shift = 12 - (in[5+i*2] & 15);
301 filter = in[5+i*2] >> 4;
303 f0 = xa_adpcm_table[filter][0];
304 f1 = xa_adpcm_table[filter][1];
309 t = (signed char)d >> 4;
310 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
312 s_1 = av_clip_int16(s);
317 if (inc==2) { /* stereo */
318 right->sample1 = s_1;
319 right->sample2 = s_2;
329 * Get the number of samples that will be decoded from the packet.
330 * In one case, this is actually the maximum number of samples possible to
331 * decode with the given buf_size.
333 * @param[out] coded_samples set to the number of samples as coded in the
334 * packet, or 0 if the codec does not encode the
335 * number of samples in each frame.
337 static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
338 int buf_size, int *coded_samples)
340 ADPCMDecodeContext *s = avctx->priv_data;
342 int ch = avctx->channels;
343 int has_coded_samples = 0;
348 switch (avctx->codec->id) {
349 /* constant, only check buf_size */
350 case CODEC_ID_ADPCM_EA_XAS:
351 if (buf_size < 76 * ch)
355 case CODEC_ID_ADPCM_IMA_QT:
356 if (buf_size < 34 * ch)
360 /* simple 4-bit adpcm */
361 case CODEC_ID_ADPCM_CT:
362 case CODEC_ID_ADPCM_IMA_EA_SEAD:
363 case CODEC_ID_ADPCM_IMA_WS:
364 case CODEC_ID_ADPCM_YAMAHA:
365 nb_samples = buf_size * 2 / ch;
371 /* simple 4-bit adpcm, with header */
373 switch (avctx->codec->id) {
374 case CODEC_ID_ADPCM_4XM:
375 case CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
376 case CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
377 case CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
380 return (buf_size - header_size) * 2 / ch;
382 /* more complex formats */
383 switch (avctx->codec->id) {
384 case CODEC_ID_ADPCM_EA:
385 has_coded_samples = 1;
388 *coded_samples = AV_RL32(buf);
389 *coded_samples -= *coded_samples % 28;
390 nb_samples = (buf_size - 12) / 30 * 28;
392 case CODEC_ID_ADPCM_IMA_EA_EACS:
393 has_coded_samples = 1;
396 *coded_samples = AV_RL32(buf);
397 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
399 case CODEC_ID_ADPCM_EA_MAXIS_XA:
400 nb_samples = ((buf_size - ch) / (2 * ch)) * 2 * ch;
402 case CODEC_ID_ADPCM_EA_R1:
403 case CODEC_ID_ADPCM_EA_R2:
404 case CODEC_ID_ADPCM_EA_R3:
405 /* maximum number of samples */
406 /* has internal offsets and a per-frame switch to signal raw 16-bit */
407 has_coded_samples = 1;
410 switch (avctx->codec->id) {
411 case CODEC_ID_ADPCM_EA_R1:
412 header_size = 4 + 9 * ch;
413 *coded_samples = AV_RL32(buf);
415 case CODEC_ID_ADPCM_EA_R2:
416 header_size = 4 + 5 * ch;
417 *coded_samples = AV_RL32(buf);
419 case CODEC_ID_ADPCM_EA_R3:
420 header_size = 4 + 5 * ch;
421 *coded_samples = AV_RB32(buf);
424 *coded_samples -= *coded_samples % 28;
425 nb_samples = (buf_size - header_size) * 2 / ch;
426 nb_samples -= nb_samples % 28;
428 case CODEC_ID_ADPCM_IMA_DK3:
429 if (avctx->block_align > 0)
430 buf_size = FFMIN(buf_size, avctx->block_align);
431 nb_samples = ((buf_size - 16) * 8 / 3) / ch;
433 case CODEC_ID_ADPCM_IMA_DK4:
434 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
436 case CODEC_ID_ADPCM_IMA_WAV:
437 if (avctx->block_align > 0)
438 buf_size = FFMIN(buf_size, avctx->block_align);
439 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
441 case CODEC_ID_ADPCM_MS:
442 if (avctx->block_align > 0)
443 buf_size = FFMIN(buf_size, avctx->block_align);
444 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
446 case CODEC_ID_ADPCM_SBPRO_2:
447 case CODEC_ID_ADPCM_SBPRO_3:
448 case CODEC_ID_ADPCM_SBPRO_4:
450 int samples_per_byte;
451 switch (avctx->codec->id) {
452 case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
453 case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
454 case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
456 if (!s->status[0].step_index) {
460 nb_samples += buf_size * samples_per_byte / ch;
463 case CODEC_ID_ADPCM_SWF:
465 int buf_bits = buf_size * 8 - 2;
466 int nbits = (buf[0] >> 6) + 2;
467 int block_hdr_size = 22 * ch;
468 int block_size = block_hdr_size + nbits * ch * 4095;
469 int nblocks = buf_bits / block_size;
470 int bits_left = buf_bits - nblocks * block_size;
471 nb_samples = nblocks * 4096;
472 if (bits_left >= block_hdr_size)
473 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
476 case CODEC_ID_ADPCM_THP:
477 has_coded_samples = 1;
480 *coded_samples = AV_RB32(&buf[4]);
481 *coded_samples -= *coded_samples % 14;
482 nb_samples = (buf_size - 80) / (8 * ch) * 14;
484 case CODEC_ID_ADPCM_XA:
485 nb_samples = (buf_size / 128) * 224 / ch;
489 /* validate coded sample count */
490 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
491 return AVERROR_INVALIDDATA;
496 /* DK3 ADPCM support macro */
497 #define DK3_GET_NEXT_NIBBLE() \
498 if (decode_top_nibble_next) \
500 nibble = last_byte >> 4; \
501 decode_top_nibble_next = 0; \
507 last_byte = *src++; \
508 if (src >= buf + buf_size) \
510 nibble = last_byte & 0x0F; \
511 decode_top_nibble_next = 1; \
514 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
515 int *got_frame_ptr, AVPacket *avpkt)
517 const uint8_t *buf = avpkt->data;
518 int buf_size = avpkt->size;
519 ADPCMDecodeContext *c = avctx->priv_data;
520 ADPCMChannelStatus *cs;
521 int n, m, channel, i;
526 int nb_samples, coded_samples, ret;
528 nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
529 if (nb_samples <= 0) {
530 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
531 return AVERROR_INVALIDDATA;
534 /* get output buffer */
535 c->frame.nb_samples = nb_samples;
536 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
537 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
540 samples = (short *)c->frame.data[0];
542 /* use coded_samples when applicable */
543 /* it is always <= nb_samples, so the output buffer will be large enough */
545 if (coded_samples != nb_samples)
546 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
547 c->frame.nb_samples = nb_samples = coded_samples;
552 st = avctx->channels == 2 ? 1 : 0;
554 switch(avctx->codec->id) {
555 case CODEC_ID_ADPCM_IMA_QT:
556 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
557 Channel data is interleaved per-chunk. */
558 for (channel = 0; channel < avctx->channels; channel++) {
561 cs = &(c->status[channel]);
562 /* (pppppp) (piiiiiii) */
564 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
565 predictor = AV_RB16(src);
566 step_index = predictor & 0x7F;
571 if (cs->step_index == step_index) {
572 int diff = (int)predictor - cs->predictor;
579 cs->step_index = step_index;
580 cs->predictor = predictor;
583 if (cs->step_index > 88){
584 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
588 samples = (short *)c->frame.data[0] + channel;
590 for (m = 0; m < 32; m++) {
591 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
592 samples += avctx->channels;
593 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
594 samples += avctx->channels;
599 case CODEC_ID_ADPCM_IMA_WAV:
600 if (avctx->block_align != 0 && buf_size > avctx->block_align)
601 buf_size = avctx->block_align;
603 for(i=0; i<avctx->channels; i++){
604 cs = &(c->status[i]);
605 cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
607 cs->step_index = *src++;
608 if (cs->step_index > 88){
609 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
612 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
615 for (n = (nb_samples - 1) / 8; n > 0; n--) {
616 for (i = 0; i < avctx->channels; i++) {
618 for (m = 0; m < 4; m++) {
620 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
621 samples += avctx->channels;
622 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
623 samples += avctx->channels;
625 samples -= 8 * avctx->channels - 1;
627 samples += 7 * avctx->channels;
630 case CODEC_ID_ADPCM_4XM:
631 for (i = 0; i < avctx->channels; i++)
632 c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
634 for (i = 0; i < avctx->channels; i++) {
635 c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
636 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
639 for (i = 0; i < avctx->channels; i++) {
640 samples = (short *)c->frame.data[0] + i;
642 for (n = nb_samples >> 1; n > 0; n--, src++) {
644 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
645 samples += avctx->channels;
646 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
647 samples += avctx->channels;
651 case CODEC_ID_ADPCM_MS:
655 if (avctx->block_align != 0 && buf_size > avctx->block_align)
656 buf_size = avctx->block_align;
658 block_predictor = av_clip(*src++, 0, 6);
659 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
660 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
662 block_predictor = av_clip(*src++, 0, 6);
663 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
664 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
666 c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
668 c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
671 c->status[0].sample1 = bytestream_get_le16(&src);
672 if (st) c->status[1].sample1 = bytestream_get_le16(&src);
673 c->status[0].sample2 = bytestream_get_le16(&src);
674 if (st) c->status[1].sample2 = bytestream_get_le16(&src);
676 *samples++ = c->status[0].sample2;
677 if (st) *samples++ = c->status[1].sample2;
678 *samples++ = c->status[0].sample1;
679 if (st) *samples++ = c->status[1].sample1;
680 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--, src++) {
681 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
682 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
686 case CODEC_ID_ADPCM_IMA_DK4:
687 if (avctx->block_align != 0 && buf_size > avctx->block_align)
688 buf_size = avctx->block_align;
690 for (channel = 0; channel < avctx->channels; channel++) {
691 cs = &c->status[channel];
692 cs->predictor = (int16_t)bytestream_get_le16(&src);
693 cs->step_index = *src++;
695 *samples++ = cs->predictor;
697 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
699 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
700 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
703 case CODEC_ID_ADPCM_IMA_DK3:
705 unsigned char last_byte = 0;
706 unsigned char nibble;
707 int decode_top_nibble_next = 0;
708 int end_of_packet = 0;
711 if (avctx->block_align != 0 && buf_size > avctx->block_align)
712 buf_size = avctx->block_align;
714 c->status[0].predictor = (int16_t)AV_RL16(src + 10);
715 c->status[1].predictor = (int16_t)AV_RL16(src + 12);
716 c->status[0].step_index = src[14];
717 c->status[1].step_index = src[15];
718 /* sign extend the predictors */
720 diff_channel = c->status[1].predictor;
722 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
723 * the buffer is consumed */
726 /* for this algorithm, c->status[0] is the sum channel and
727 * c->status[1] is the diff channel */
729 /* process the first predictor of the sum channel */
730 DK3_GET_NEXT_NIBBLE();
731 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
733 /* process the diff channel predictor */
734 DK3_GET_NEXT_NIBBLE();
735 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
737 /* process the first pair of stereo PCM samples */
738 diff_channel = (diff_channel + c->status[1].predictor) / 2;
739 *samples++ = c->status[0].predictor + c->status[1].predictor;
740 *samples++ = c->status[0].predictor - c->status[1].predictor;
742 /* process the second predictor of the sum channel */
743 DK3_GET_NEXT_NIBBLE();
744 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
746 /* process the second pair of stereo PCM samples */
747 diff_channel = (diff_channel + c->status[1].predictor) / 2;
748 *samples++ = c->status[0].predictor + c->status[1].predictor;
749 *samples++ = c->status[0].predictor - c->status[1].predictor;
753 case CODEC_ID_ADPCM_IMA_ISS:
754 for (channel = 0; channel < avctx->channels; channel++) {
755 cs = &c->status[channel];
756 cs->predictor = (int16_t)bytestream_get_le16(&src);
757 cs->step_index = *src++;
761 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
764 /* nibbles are swapped for mono */
772 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
773 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
776 case CODEC_ID_ADPCM_IMA_WS:
777 while (src < buf + buf_size) {
779 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
780 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
783 case CODEC_ID_ADPCM_XA:
784 while (buf_size >= 128) {
785 xa_decode(samples, src, &c->status[0], &c->status[1],
792 case CODEC_ID_ADPCM_IMA_EA_EACS:
793 src += 4; // skip sample count (already read)
795 for (i=0; i<=st; i++)
796 c->status[i].step_index = bytestream_get_le32(&src);
797 for (i=0; i<=st; i++)
798 c->status[i].predictor = bytestream_get_le32(&src);
800 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
801 *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
802 *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
805 case CODEC_ID_ADPCM_IMA_EA_SEAD:
806 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
807 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
808 *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
811 case CODEC_ID_ADPCM_EA:
813 int32_t previous_left_sample, previous_right_sample;
814 int32_t current_left_sample, current_right_sample;
815 int32_t next_left_sample, next_right_sample;
816 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
817 uint8_t shift_left, shift_right;
819 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
820 each coding 28 stereo samples. */
822 src += 4; // skip sample count (already read)
824 current_left_sample = (int16_t)bytestream_get_le16(&src);
825 previous_left_sample = (int16_t)bytestream_get_le16(&src);
826 current_right_sample = (int16_t)bytestream_get_le16(&src);
827 previous_right_sample = (int16_t)bytestream_get_le16(&src);
829 for (count1 = 0; count1 < nb_samples / 28; count1++) {
830 coeff1l = ea_adpcm_table[ *src >> 4 ];
831 coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
832 coeff1r = ea_adpcm_table[*src & 0x0F];
833 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
836 shift_left = 20 - (*src >> 4);
837 shift_right = 20 - (*src & 0x0F);
840 for (count2 = 0; count2 < 28; count2++) {
841 next_left_sample = sign_extend(*src >> 4, 4) << shift_left;
842 next_right_sample = sign_extend(*src, 4) << shift_right;
845 next_left_sample = (next_left_sample +
846 (current_left_sample * coeff1l) +
847 (previous_left_sample * coeff2l) + 0x80) >> 8;
848 next_right_sample = (next_right_sample +
849 (current_right_sample * coeff1r) +
850 (previous_right_sample * coeff2r) + 0x80) >> 8;
852 previous_left_sample = current_left_sample;
853 current_left_sample = av_clip_int16(next_left_sample);
854 previous_right_sample = current_right_sample;
855 current_right_sample = av_clip_int16(next_right_sample);
856 *samples++ = (unsigned short)current_left_sample;
857 *samples++ = (unsigned short)current_right_sample;
861 if (src - buf == buf_size - 2)
862 src += 2; // Skip terminating 0x0000
866 case CODEC_ID_ADPCM_EA_MAXIS_XA:
868 int coeff[2][2], shift[2];
870 for(channel = 0; channel < avctx->channels; channel++) {
872 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
873 shift[channel] = 20 - (*src & 0x0F);
876 for (count1 = 0; count1 < nb_samples / 2; count1++) {
877 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
878 for(channel = 0; channel < avctx->channels; channel++) {
879 int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
881 c->status[channel].sample1 * coeff[channel][0] +
882 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
883 c->status[channel].sample2 = c->status[channel].sample1;
884 c->status[channel].sample1 = av_clip_int16(sample);
885 *samples++ = c->status[channel].sample1;
888 src+=avctx->channels;
890 /* consume whole packet */
891 src = buf + buf_size;
894 case CODEC_ID_ADPCM_EA_R1:
895 case CODEC_ID_ADPCM_EA_R2:
896 case CODEC_ID_ADPCM_EA_R3: {
899 4chan: 0=fl, 1=rl, 2=fr, 3=rr
900 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
901 const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
902 int32_t previous_sample, current_sample, next_sample;
903 int32_t coeff1, coeff2;
905 unsigned int channel;
908 const uint8_t *src_end = buf + buf_size;
911 src += 4; // skip sample count (already read)
913 for (channel=0; channel<avctx->channels; channel++) {
914 int32_t offset = (big_endian ? bytestream_get_be32(&src)
915 : bytestream_get_le32(&src))
916 + (avctx->channels-channel-1) * 4;
918 if ((offset < 0) || (offset >= src_end - src - 4)) break;
920 samplesC = samples + channel;
922 if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
923 current_sample = (int16_t)bytestream_get_le16(&srcC);
924 previous_sample = (int16_t)bytestream_get_le16(&srcC);
926 current_sample = c->status[channel].predictor;
927 previous_sample = c->status[channel].prev_sample;
930 for (count1 = 0; count1 < nb_samples / 28; count1++) {
931 if (*srcC == 0xEE) { /* only seen in R2 and R3 */
933 if (srcC > src_end - 30*2) break;
934 current_sample = (int16_t)bytestream_get_be16(&srcC);
935 previous_sample = (int16_t)bytestream_get_be16(&srcC);
937 for (count2=0; count2<28; count2++) {
938 *samplesC = (int16_t)bytestream_get_be16(&srcC);
939 samplesC += avctx->channels;
942 coeff1 = ea_adpcm_table[ *srcC>>4 ];
943 coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
944 shift = 20 - (*srcC++ & 0x0F);
946 if (srcC > src_end - 14) break;
947 for (count2=0; count2<28; count2++) {
949 next_sample = sign_extend(*srcC++, 4) << shift;
951 next_sample = sign_extend(*srcC >> 4, 4) << shift;
953 next_sample += (current_sample * coeff1) +
954 (previous_sample * coeff2);
955 next_sample = av_clip_int16(next_sample >> 8);
957 previous_sample = current_sample;
958 current_sample = next_sample;
959 *samplesC = current_sample;
960 samplesC += avctx->channels;
966 } else if (count != count1) {
967 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
968 count = FFMAX(count, count1);
971 if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
972 c->status[channel].predictor = current_sample;
973 c->status[channel].prev_sample = previous_sample;
977 c->frame.nb_samples = count * 28;
981 case CODEC_ID_ADPCM_EA_XAS:
982 for (channel=0; channel<avctx->channels; channel++) {
983 int coeff[2][4], shift[4];
984 short *s2, *s = &samples[channel];
985 for (n=0; n<4; n++, s+=32*avctx->channels) {
987 coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
988 shift[n] = 20 - (src[2] & 0x0F);
989 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
990 s2[0] = (src[0]&0xF0) + (src[1]<<8);
993 for (m=2; m<32; m+=2) {
994 s = &samples[m*avctx->channels + channel];
995 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
996 for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
997 int level = sign_extend(*src >> (4 - i), 4) << shift[n];
998 int pred = s2[-1*avctx->channels] * coeff[0][n]
999 + s2[-2*avctx->channels] * coeff[1][n];
1000 s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
1006 case CODEC_ID_ADPCM_IMA_AMV:
1007 case CODEC_ID_ADPCM_IMA_SMJPEG:
1008 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
1009 c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16);
1010 c->status[0].step_index = bytestream_get_le16(&src);
1013 c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16);
1014 c->status[0].step_index = bytestream_get_byte(&src);
1018 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1023 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1024 FFSWAP(char, hi, lo);
1026 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1028 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1032 case CODEC_ID_ADPCM_CT:
1033 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1035 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1036 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1039 case CODEC_ID_ADPCM_SBPRO_4:
1040 case CODEC_ID_ADPCM_SBPRO_3:
1041 case CODEC_ID_ADPCM_SBPRO_2:
1042 if (!c->status[0].step_index) {
1043 /* the first byte is a raw sample */
1044 *samples++ = 128 * (*src++ - 0x80);
1046 *samples++ = 128 * (*src++ - 0x80);
1047 c->status[0].step_index = 1;
1050 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1051 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1052 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1054 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1055 src[0] & 0x0F, 4, 0);
1057 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1058 for (n = nb_samples / 3; n > 0; n--, src++) {
1059 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1060 src[0] >> 5 , 3, 0);
1061 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1062 (src[0] >> 2) & 0x07, 3, 0);
1063 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1064 src[0] & 0x03, 2, 0);
1067 for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
1068 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1069 src[0] >> 6 , 2, 2);
1070 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1071 (src[0] >> 4) & 0x03, 2, 2);
1072 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1073 (src[0] >> 2) & 0x03, 2, 2);
1074 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1075 src[0] & 0x03, 2, 2);
1079 case CODEC_ID_ADPCM_SWF:
1083 int k0, signmask, nb_bits, count;
1084 int size = buf_size*8;
1086 init_get_bits(&gb, buf, size);
1088 //read bits & initial values
1089 nb_bits = get_bits(&gb, 2)+2;
1090 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1091 table = swf_index_tables[nb_bits-2];
1092 k0 = 1 << (nb_bits-2);
1093 signmask = 1 << (nb_bits-1);
1095 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1096 for (i = 0; i < avctx->channels; i++) {
1097 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1098 c->status[i].step_index = get_bits(&gb, 6);
1101 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
1104 for (i = 0; i < avctx->channels; i++) {
1105 // similar to IMA adpcm
1106 int delta = get_bits(&gb, nb_bits);
1107 int step = ff_adpcm_step_table[c->status[i].step_index];
1108 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1119 if (delta & signmask)
1120 c->status[i].predictor -= vpdiff;
1122 c->status[i].predictor += vpdiff;
1124 c->status[i].step_index += table[delta & (~signmask)];
1126 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1127 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1129 *samples++ = c->status[i].predictor;
1136 case CODEC_ID_ADPCM_YAMAHA:
1137 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1139 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1140 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1143 case CODEC_ID_ADPCM_THP:
1149 src += 4; // skip channel size
1150 src += 4; // skip number of samples (already read)
1152 for (i = 0; i < 32; i++)
1153 table[0][i] = (int16_t)bytestream_get_be16(&src);
1155 /* Initialize the previous sample. */
1156 for (i = 0; i < 4; i++)
1157 prev[0][i] = (int16_t)bytestream_get_be16(&src);
1159 for (ch = 0; ch <= st; ch++) {
1160 samples = (short *)c->frame.data[0] + ch;
1162 /* Read in every sample for this channel. */
1163 for (i = 0; i < nb_samples / 14; i++) {
1164 int index = (*src >> 4) & 7;
1165 unsigned int exp = *src++ & 15;
1166 int factor1 = table[ch][index * 2];
1167 int factor2 = table[ch][index * 2 + 1];
1169 /* Decode 14 samples. */
1170 for (n = 0; n < 14; n++) {
1172 if(n&1) sampledat = sign_extend(*src++, 4);
1173 else sampledat = sign_extend(*src >> 4, 4);
1175 sampledat = ((prev[ch][0]*factor1
1176 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1177 *samples = av_clip_int16(sampledat);
1178 prev[ch][1] = prev[ch][0];
1179 prev[ch][0] = *samples++;
1181 /* In case of stereo, skip one sample, this sample
1182 is for the other channel. */
1195 *(AVFrame *)data = c->frame;
1201 #define ADPCM_DECODER(id_, name_, long_name_) \
1202 AVCodec ff_ ## name_ ## _decoder = { \
1204 .type = AVMEDIA_TYPE_AUDIO, \
1206 .priv_data_size = sizeof(ADPCMDecodeContext), \
1207 .init = adpcm_decode_init, \
1208 .decode = adpcm_decode_frame, \
1209 .capabilities = CODEC_CAP_DR1, \
1210 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1213 /* Note: Do not forget to add new entries to the Makefile as well. */
1214 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1215 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1216 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1217 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1218 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1219 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1220 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1221 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1222 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1223 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1224 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1225 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1226 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1227 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1228 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1229 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1230 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1231 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1232 ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1233 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1234 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1235 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1236 ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1237 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1238 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1239 ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");