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 int xa_decode(AVCodecContext *avctx,
269 short *out, const unsigned char *in,
270 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
273 int shift,filter,f0,f1;
279 shift = 12 - (in[4+i*2] & 15);
280 filter = in[4+i*2] >> 4;
281 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
282 av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
285 f0 = xa_adpcm_table[filter][0];
286 f1 = xa_adpcm_table[filter][1];
294 t = (signed char)(d<<4)>>4;
295 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
297 s_1 = av_clip_int16(s);
302 if (inc==2) { /* stereo */
305 s_1 = right->sample1;
306 s_2 = right->sample2;
307 out = out + 1 - 28*2;
310 shift = 12 - (in[5+i*2] & 15);
311 filter = in[5+i*2] >> 4;
312 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
313 av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
317 f0 = xa_adpcm_table[filter][0];
318 f1 = xa_adpcm_table[filter][1];
323 t = (signed char)d >> 4;
324 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
326 s_1 = av_clip_int16(s);
331 if (inc==2) { /* stereo */
332 right->sample1 = s_1;
333 right->sample2 = s_2;
345 * Get the number of samples that will be decoded from the packet.
346 * In one case, this is actually the maximum number of samples possible to
347 * decode with the given buf_size.
349 * @param[out] coded_samples set to the number of samples as coded in the
350 * packet, or 0 if the codec does not encode the
351 * number of samples in each frame.
353 static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
354 int buf_size, int *coded_samples)
356 ADPCMDecodeContext *s = avctx->priv_data;
358 int ch = avctx->channels;
359 int has_coded_samples = 0;
367 switch (avctx->codec->id) {
368 /* constant, only check buf_size */
369 case CODEC_ID_ADPCM_EA_XAS:
370 if (buf_size < 76 * ch)
374 case CODEC_ID_ADPCM_IMA_QT:
375 if (buf_size < 34 * ch)
379 /* simple 4-bit adpcm */
380 case CODEC_ID_ADPCM_CT:
381 case CODEC_ID_ADPCM_IMA_APC:
382 case CODEC_ID_ADPCM_IMA_EA_SEAD:
383 case CODEC_ID_ADPCM_IMA_WS:
384 case CODEC_ID_ADPCM_YAMAHA:
385 nb_samples = buf_size * 2 / ch;
391 /* simple 4-bit adpcm, with header */
393 switch (avctx->codec->id) {
394 case CODEC_ID_ADPCM_4XM:
395 case CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
396 case CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
397 case CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
400 return (buf_size - header_size) * 2 / ch;
402 /* more complex formats */
403 switch (avctx->codec->id) {
404 case CODEC_ID_ADPCM_EA:
405 has_coded_samples = 1;
408 *coded_samples = AV_RL32(buf);
409 *coded_samples -= *coded_samples % 28;
410 nb_samples = (buf_size - 12) / 30 * 28;
412 case CODEC_ID_ADPCM_IMA_EA_EACS:
413 has_coded_samples = 1;
416 *coded_samples = AV_RL32(buf);
417 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
419 case CODEC_ID_ADPCM_EA_MAXIS_XA:
420 nb_samples = ((buf_size - ch) / (2 * ch)) * 2 * ch;
422 case CODEC_ID_ADPCM_EA_R1:
423 case CODEC_ID_ADPCM_EA_R2:
424 case CODEC_ID_ADPCM_EA_R3:
425 /* maximum number of samples */
426 /* has internal offsets and a per-frame switch to signal raw 16-bit */
427 has_coded_samples = 1;
430 switch (avctx->codec->id) {
431 case CODEC_ID_ADPCM_EA_R1:
432 header_size = 4 + 9 * ch;
433 *coded_samples = AV_RL32(buf);
435 case CODEC_ID_ADPCM_EA_R2:
436 header_size = 4 + 5 * ch;
437 *coded_samples = AV_RL32(buf);
439 case CODEC_ID_ADPCM_EA_R3:
440 header_size = 4 + 5 * ch;
441 *coded_samples = AV_RB32(buf);
444 *coded_samples -= *coded_samples % 28;
445 nb_samples = (buf_size - header_size) * 2 / ch;
446 nb_samples -= nb_samples % 28;
448 case CODEC_ID_ADPCM_IMA_DK3:
449 if (avctx->block_align > 0)
450 buf_size = FFMIN(buf_size, avctx->block_align);
451 nb_samples = ((buf_size - 16) * 8 / 3) / ch;
453 case CODEC_ID_ADPCM_IMA_DK4:
454 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
456 case CODEC_ID_ADPCM_IMA_WAV:
457 if (avctx->block_align > 0)
458 buf_size = FFMIN(buf_size, avctx->block_align);
459 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
461 case CODEC_ID_ADPCM_MS:
462 if (avctx->block_align > 0)
463 buf_size = FFMIN(buf_size, avctx->block_align);
464 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
466 case CODEC_ID_ADPCM_SBPRO_2:
467 case CODEC_ID_ADPCM_SBPRO_3:
468 case CODEC_ID_ADPCM_SBPRO_4:
470 int samples_per_byte;
471 switch (avctx->codec->id) {
472 case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
473 case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
474 case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
476 if (!s->status[0].step_index) {
480 nb_samples += buf_size * samples_per_byte / ch;
483 case CODEC_ID_ADPCM_SWF:
485 int buf_bits = buf_size * 8 - 2;
486 int nbits = (buf[0] >> 6) + 2;
487 int block_hdr_size = 22 * ch;
488 int block_size = block_hdr_size + nbits * ch * 4095;
489 int nblocks = buf_bits / block_size;
490 int bits_left = buf_bits - nblocks * block_size;
491 nb_samples = nblocks * 4096;
492 if (bits_left >= block_hdr_size)
493 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
496 case CODEC_ID_ADPCM_THP:
497 has_coded_samples = 1;
500 *coded_samples = AV_RB32(&buf[4]);
501 *coded_samples -= *coded_samples % 14;
502 nb_samples = (buf_size - 80) / (8 * ch) * 14;
504 case CODEC_ID_ADPCM_XA:
505 nb_samples = (buf_size / 128) * 224 / ch;
509 /* validate coded sample count */
510 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
511 return AVERROR_INVALIDDATA;
516 /* DK3 ADPCM support macro */
517 #define DK3_GET_NEXT_NIBBLE() \
518 if (decode_top_nibble_next) \
520 nibble = last_byte >> 4; \
521 decode_top_nibble_next = 0; \
527 last_byte = *src++; \
528 if (src >= buf + buf_size) \
530 nibble = last_byte & 0x0F; \
531 decode_top_nibble_next = 1; \
534 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
535 int *got_frame_ptr, AVPacket *avpkt)
537 const uint8_t *buf = avpkt->data;
538 int buf_size = avpkt->size;
539 ADPCMDecodeContext *c = avctx->priv_data;
540 ADPCMChannelStatus *cs;
541 int n, m, channel, i;
546 int nb_samples, coded_samples, ret;
548 nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
549 if (nb_samples <= 0) {
550 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
551 return AVERROR_INVALIDDATA;
554 /* get output buffer */
555 c->frame.nb_samples = nb_samples;
556 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
557 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
560 samples = (short *)c->frame.data[0];
562 /* use coded_samples when applicable */
563 /* it is always <= nb_samples, so the output buffer will be large enough */
565 if (coded_samples != nb_samples)
566 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
567 c->frame.nb_samples = nb_samples = coded_samples;
572 st = avctx->channels == 2 ? 1 : 0;
574 switch(avctx->codec->id) {
575 case CODEC_ID_ADPCM_IMA_QT:
576 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
577 Channel data is interleaved per-chunk. */
578 for (channel = 0; channel < avctx->channels; channel++) {
581 cs = &(c->status[channel]);
582 /* (pppppp) (piiiiiii) */
584 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
585 predictor = AV_RB16(src);
586 step_index = predictor & 0x7F;
591 if (cs->step_index == step_index) {
592 int diff = (int)predictor - cs->predictor;
599 cs->step_index = step_index;
600 cs->predictor = predictor;
603 if (cs->step_index > 88){
604 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
608 samples = (short *)c->frame.data[0] + channel;
610 for (m = 0; m < 32; m++) {
611 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
612 samples += avctx->channels;
613 *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
614 samples += avctx->channels;
619 case CODEC_ID_ADPCM_IMA_WAV:
620 if (avctx->block_align != 0 && buf_size > avctx->block_align)
621 buf_size = avctx->block_align;
623 for(i=0; i<avctx->channels; i++){
624 cs = &(c->status[i]);
625 cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
627 cs->step_index = *src++;
628 if (cs->step_index > 88){
629 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
632 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
635 for (n = (nb_samples - 1) / 8; n > 0; n--) {
636 for (i = 0; i < avctx->channels; i++) {
638 for (m = 0; m < 4; m++) {
640 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
641 samples += avctx->channels;
642 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
643 samples += avctx->channels;
645 samples -= 8 * avctx->channels - 1;
647 samples += 7 * avctx->channels;
650 case CODEC_ID_ADPCM_4XM:
651 for (i = 0; i < avctx->channels; i++)
652 c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
654 for (i = 0; i < avctx->channels; i++) {
655 c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
656 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
659 for (i = 0; i < avctx->channels; i++) {
660 samples = (short *)c->frame.data[0] + i;
662 for (n = nb_samples >> 1; n > 0; n--, src++) {
664 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
665 samples += avctx->channels;
666 *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
667 samples += avctx->channels;
671 case CODEC_ID_ADPCM_MS:
675 if (avctx->block_align != 0 && buf_size > avctx->block_align)
676 buf_size = avctx->block_align;
678 block_predictor = av_clip(*src++, 0, 6);
679 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
680 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
682 block_predictor = av_clip(*src++, 0, 6);
683 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
684 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
686 c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
688 c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
691 c->status[0].sample1 = bytestream_get_le16(&src);
692 if (st) c->status[1].sample1 = bytestream_get_le16(&src);
693 c->status[0].sample2 = bytestream_get_le16(&src);
694 if (st) c->status[1].sample2 = bytestream_get_le16(&src);
696 *samples++ = c->status[0].sample2;
697 if (st) *samples++ = c->status[1].sample2;
698 *samples++ = c->status[0].sample1;
699 if (st) *samples++ = c->status[1].sample1;
700 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--, src++) {
701 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
702 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
706 case CODEC_ID_ADPCM_IMA_DK4:
707 if (avctx->block_align != 0 && buf_size > avctx->block_align)
708 buf_size = avctx->block_align;
710 for (channel = 0; channel < avctx->channels; channel++) {
711 cs = &c->status[channel];
712 cs->predictor = (int16_t)bytestream_get_le16(&src);
713 cs->step_index = av_clip(*src++, 0, 88);
715 *samples++ = cs->predictor;
717 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
719 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
720 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
723 case CODEC_ID_ADPCM_IMA_DK3:
725 unsigned char last_byte = 0;
726 unsigned char nibble;
727 int decode_top_nibble_next = 0;
728 int end_of_packet = 0;
731 if (avctx->block_align != 0 && buf_size > avctx->block_align)
732 buf_size = avctx->block_align;
734 c->status[0].predictor = (int16_t)AV_RL16(src + 10);
735 c->status[1].predictor = (int16_t)AV_RL16(src + 12);
736 c->status[0].step_index = av_clip(src[14], 0, 88);
737 c->status[1].step_index = av_clip(src[15], 0, 88);
738 /* sign extend the predictors */
740 diff_channel = c->status[1].predictor;
742 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
743 * the buffer is consumed */
746 /* for this algorithm, c->status[0] is the sum channel and
747 * c->status[1] is the diff channel */
749 /* process the first predictor of the sum channel */
750 DK3_GET_NEXT_NIBBLE();
751 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
753 /* process the diff channel predictor */
754 DK3_GET_NEXT_NIBBLE();
755 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
757 /* process the first pair of stereo PCM samples */
758 diff_channel = (diff_channel + c->status[1].predictor) / 2;
759 *samples++ = c->status[0].predictor + c->status[1].predictor;
760 *samples++ = c->status[0].predictor - c->status[1].predictor;
762 /* process the second predictor of the sum channel */
763 DK3_GET_NEXT_NIBBLE();
764 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
766 /* process the second pair of stereo PCM samples */
767 diff_channel = (diff_channel + c->status[1].predictor) / 2;
768 *samples++ = c->status[0].predictor + c->status[1].predictor;
769 *samples++ = c->status[0].predictor - c->status[1].predictor;
773 case CODEC_ID_ADPCM_IMA_ISS:
774 for (channel = 0; channel < avctx->channels; channel++) {
775 cs = &c->status[channel];
776 cs->predictor = (int16_t)bytestream_get_le16(&src);
777 cs->step_index = av_clip(*src++, 0, 88);
781 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
784 /* nibbles are swapped for mono */
792 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
793 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
796 case CODEC_ID_ADPCM_IMA_APC:
797 while (src < buf + buf_size) {
799 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
800 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
803 case CODEC_ID_ADPCM_IMA_WS:
804 for (channel = 0; channel < avctx->channels; channel++) {
807 int16_t *smp = samples + channel;
809 if (c->vqa_version == 3) {
810 src0 = src + channel * buf_size / 2;
813 src0 = src + channel;
814 src_stride = avctx->channels;
816 for (n = nb_samples / 2; n > 0; n--) {
819 *smp = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
820 smp += avctx->channels;
821 *smp = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
822 smp += avctx->channels;
825 src = buf + buf_size;
827 case CODEC_ID_ADPCM_XA:
828 while (buf_size >= 128) {
829 if ((ret = xa_decode(avctx, samples, src, &c->status[0],
830 &c->status[1], avctx->channels)) < 0)
837 case CODEC_ID_ADPCM_IMA_EA_EACS:
838 src += 4; // skip sample count (already read)
840 for (i=0; i<=st; i++)
841 c->status[i].step_index = av_clip(bytestream_get_le32(&src), 0, 88);
842 for (i=0; i<=st; i++)
843 c->status[i].predictor = bytestream_get_le32(&src);
845 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
846 *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
847 *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
850 case CODEC_ID_ADPCM_IMA_EA_SEAD:
851 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
852 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
853 *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
856 case CODEC_ID_ADPCM_EA:
858 int32_t previous_left_sample, previous_right_sample;
859 int32_t current_left_sample, current_right_sample;
860 int32_t next_left_sample, next_right_sample;
861 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
862 uint8_t shift_left, shift_right;
864 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
865 each coding 28 stereo samples. */
867 if(avctx->channels != 2)
868 return AVERROR_INVALIDDATA;
870 src += 4; // skip sample count (already read)
872 current_left_sample = (int16_t)bytestream_get_le16(&src);
873 previous_left_sample = (int16_t)bytestream_get_le16(&src);
874 current_right_sample = (int16_t)bytestream_get_le16(&src);
875 previous_right_sample = (int16_t)bytestream_get_le16(&src);
877 for (count1 = 0; count1 < nb_samples / 28; count1++) {
878 coeff1l = ea_adpcm_table[ *src >> 4 ];
879 coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
880 coeff1r = ea_adpcm_table[*src & 0x0F];
881 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
884 shift_left = 20 - (*src >> 4);
885 shift_right = 20 - (*src & 0x0F);
888 for (count2 = 0; count2 < 28; count2++) {
889 next_left_sample = sign_extend(*src >> 4, 4) << shift_left;
890 next_right_sample = sign_extend(*src, 4) << shift_right;
893 next_left_sample = (next_left_sample +
894 (current_left_sample * coeff1l) +
895 (previous_left_sample * coeff2l) + 0x80) >> 8;
896 next_right_sample = (next_right_sample +
897 (current_right_sample * coeff1r) +
898 (previous_right_sample * coeff2r) + 0x80) >> 8;
900 previous_left_sample = current_left_sample;
901 current_left_sample = av_clip_int16(next_left_sample);
902 previous_right_sample = current_right_sample;
903 current_right_sample = av_clip_int16(next_right_sample);
904 *samples++ = (unsigned short)current_left_sample;
905 *samples++ = (unsigned short)current_right_sample;
909 if (src - buf == buf_size - 2)
910 src += 2; // Skip terminating 0x0000
914 case CODEC_ID_ADPCM_EA_MAXIS_XA:
916 int coeff[2][2], shift[2];
918 for(channel = 0; channel < avctx->channels; channel++) {
920 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
921 shift[channel] = 20 - (*src & 0x0F);
924 for (count1 = 0; count1 < nb_samples / 2; count1++) {
925 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
926 for(channel = 0; channel < avctx->channels; channel++) {
927 int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
929 c->status[channel].sample1 * coeff[channel][0] +
930 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
931 c->status[channel].sample2 = c->status[channel].sample1;
932 c->status[channel].sample1 = av_clip_int16(sample);
933 *samples++ = c->status[channel].sample1;
936 src+=avctx->channels;
938 /* consume whole packet */
939 src = buf + buf_size;
942 case CODEC_ID_ADPCM_EA_R1:
943 case CODEC_ID_ADPCM_EA_R2:
944 case CODEC_ID_ADPCM_EA_R3: {
947 4chan: 0=fl, 1=rl, 2=fr, 3=rr
948 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
949 const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
950 int32_t previous_sample, current_sample, next_sample;
951 int32_t coeff1, coeff2;
953 unsigned int channel;
956 const uint8_t *src_end = buf + buf_size;
959 src += 4; // skip sample count (already read)
961 for (channel=0; channel<avctx->channels; channel++) {
962 int32_t offset = (big_endian ? bytestream_get_be32(&src)
963 : bytestream_get_le32(&src))
964 + (avctx->channels-channel-1) * 4;
966 if ((offset < 0) || (offset >= src_end - src - 4)) break;
968 samplesC = samples + channel;
970 if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
971 current_sample = (int16_t)bytestream_get_le16(&srcC);
972 previous_sample = (int16_t)bytestream_get_le16(&srcC);
974 current_sample = c->status[channel].predictor;
975 previous_sample = c->status[channel].prev_sample;
978 for (count1 = 0; count1 < nb_samples / 28; count1++) {
979 if (*srcC == 0xEE) { /* only seen in R2 and R3 */
981 if (srcC > src_end - 30*2) break;
982 current_sample = (int16_t)bytestream_get_be16(&srcC);
983 previous_sample = (int16_t)bytestream_get_be16(&srcC);
985 for (count2=0; count2<28; count2++) {
986 *samplesC = (int16_t)bytestream_get_be16(&srcC);
987 samplesC += avctx->channels;
990 coeff1 = ea_adpcm_table[ *srcC>>4 ];
991 coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
992 shift = 20 - (*srcC++ & 0x0F);
994 if (srcC > src_end - 14) break;
995 for (count2=0; count2<28; count2++) {
997 next_sample = sign_extend(*srcC++, 4) << shift;
999 next_sample = sign_extend(*srcC >> 4, 4) << shift;
1001 next_sample += (current_sample * coeff1) +
1002 (previous_sample * coeff2);
1003 next_sample = av_clip_int16(next_sample >> 8);
1005 previous_sample = current_sample;
1006 current_sample = next_sample;
1007 *samplesC = current_sample;
1008 samplesC += avctx->channels;
1014 } else if (count != count1) {
1015 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1016 count = FFMAX(count, count1);
1019 if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
1020 c->status[channel].predictor = current_sample;
1021 c->status[channel].prev_sample = previous_sample;
1025 c->frame.nb_samples = count * 28;
1029 case CODEC_ID_ADPCM_EA_XAS:
1030 for (channel=0; channel<avctx->channels; channel++) {
1031 int coeff[2][4], shift[4];
1032 short *s2, *s = &samples[channel];
1033 for (n=0; n<4; n++, s+=32*avctx->channels) {
1035 coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
1036 shift[n] = 20 - (src[2] & 0x0F);
1037 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
1038 s2[0] = (src[0]&0xF0) + (src[1]<<8);
1041 for (m=2; m<32; m+=2) {
1042 s = &samples[m*avctx->channels + channel];
1043 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
1044 for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
1045 int level = sign_extend(*src >> (4 - i), 4) << shift[n];
1046 int pred = s2[-1*avctx->channels] * coeff[0][n]
1047 + s2[-2*avctx->channels] * coeff[1][n];
1048 s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
1054 case CODEC_ID_ADPCM_IMA_AMV:
1055 case CODEC_ID_ADPCM_IMA_SMJPEG:
1056 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
1057 c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16);
1058 c->status[0].step_index = av_clip(bytestream_get_le16(&src), 0, 88);
1061 c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16);
1062 c->status[0].step_index = av_clip(bytestream_get_byte(&src), 0, 88);
1066 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1071 if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1072 FFSWAP(char, hi, lo);
1074 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1076 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1080 case CODEC_ID_ADPCM_CT:
1081 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1083 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1084 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1087 case CODEC_ID_ADPCM_SBPRO_4:
1088 case CODEC_ID_ADPCM_SBPRO_3:
1089 case CODEC_ID_ADPCM_SBPRO_2:
1090 if (!c->status[0].step_index) {
1091 /* the first byte is a raw sample */
1092 *samples++ = 128 * (*src++ - 0x80);
1094 *samples++ = 128 * (*src++ - 0x80);
1095 c->status[0].step_index = 1;
1098 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1099 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1100 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1102 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1103 src[0] & 0x0F, 4, 0);
1105 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1106 for (n = nb_samples / 3; n > 0; n--, src++) {
1107 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1108 src[0] >> 5 , 3, 0);
1109 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1110 (src[0] >> 2) & 0x07, 3, 0);
1111 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1112 src[0] & 0x03, 2, 0);
1115 for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
1116 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1117 src[0] >> 6 , 2, 2);
1118 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1119 (src[0] >> 4) & 0x03, 2, 2);
1120 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1121 (src[0] >> 2) & 0x03, 2, 2);
1122 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1123 src[0] & 0x03, 2, 2);
1127 case CODEC_ID_ADPCM_SWF:
1131 int k0, signmask, nb_bits, count;
1132 int size = buf_size*8;
1134 init_get_bits(&gb, buf, size);
1136 //read bits & initial values
1137 nb_bits = get_bits(&gb, 2)+2;
1138 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1139 table = swf_index_tables[nb_bits-2];
1140 k0 = 1 << (nb_bits-2);
1141 signmask = 1 << (nb_bits-1);
1143 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1144 for (i = 0; i < avctx->channels; i++) {
1145 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1146 c->status[i].step_index = get_bits(&gb, 6);
1149 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
1152 for (i = 0; i < avctx->channels; i++) {
1153 // similar to IMA adpcm
1154 int delta = get_bits(&gb, nb_bits);
1155 int step = ff_adpcm_step_table[c->status[i].step_index];
1156 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1167 if (delta & signmask)
1168 c->status[i].predictor -= vpdiff;
1170 c->status[i].predictor += vpdiff;
1172 c->status[i].step_index += table[delta & (~signmask)];
1174 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1175 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1177 *samples++ = c->status[i].predictor;
1184 case CODEC_ID_ADPCM_YAMAHA:
1185 for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1187 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1188 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1191 case CODEC_ID_ADPCM_THP:
1197 src += 4; // skip channel size
1198 src += 4; // skip number of samples (already read)
1200 for (i = 0; i < 32; i++)
1201 table[0][i] = (int16_t)bytestream_get_be16(&src);
1203 /* Initialize the previous sample. */
1204 for (i = 0; i < 4; i++)
1205 prev[i>>1][i&1] = (int16_t)bytestream_get_be16(&src);
1207 for (ch = 0; ch <= st; ch++) {
1208 samples = (short *)c->frame.data[0] + ch;
1210 /* Read in every sample for this channel. */
1211 for (i = 0; i < nb_samples / 14; i++) {
1212 int index = (*src >> 4) & 7;
1213 unsigned int exp = *src++ & 15;
1214 int factor1 = table[ch][index * 2];
1215 int factor2 = table[ch][index * 2 + 1];
1217 /* Decode 14 samples. */
1218 for (n = 0; n < 14; n++) {
1220 if(n&1) sampledat = sign_extend(*src++, 4);
1221 else sampledat = sign_extend(*src >> 4, 4);
1223 sampledat = ((prev[ch][0]*factor1
1224 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1225 *samples = av_clip_int16(sampledat);
1226 prev[ch][1] = prev[ch][0];
1227 prev[ch][0] = *samples++;
1229 /* In case of stereo, skip one sample, this sample
1230 is for the other channel. */
1243 *(AVFrame *)data = c->frame;
1249 #define ADPCM_DECODER(id_, name_, long_name_) \
1250 AVCodec ff_ ## name_ ## _decoder = { \
1252 .type = AVMEDIA_TYPE_AUDIO, \
1254 .priv_data_size = sizeof(ADPCMDecodeContext), \
1255 .init = adpcm_decode_init, \
1256 .decode = adpcm_decode_frame, \
1257 .capabilities = CODEC_CAP_DR1, \
1258 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1261 /* Note: Do not forget to add new entries to the Makefile as well. */
1262 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1263 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1264 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1265 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1266 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1267 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1268 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1269 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1270 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1271 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1272 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1273 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1274 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1275 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1276 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1277 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1278 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1279 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1280 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1281 ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1282 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1283 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1284 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1285 ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1286 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1287 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1288 ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");