2 * Copyright (c) 2001-2003 The ffmpeg Project
4 * first version by Francois Revol (revol@free.fr)
5 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6 * by Mike Melanson (melanson@pcisys.net)
7 * CD-ROM XA ADPCM codec by BERO
8 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
16 * This file is part of FFmpeg.
18 * FFmpeg is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
23 * FFmpeg is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with FFmpeg; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bytestream.h"
36 #include "adpcm_data.h"
42 * Features and limitations:
44 * Reference documents:
45 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48 * http://openquicktime.sourceforge.net/
49 * XAnim sources (xa_codec.c) http://xanim.polter.net/
50 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51 * SoX source code http://sox.sourceforge.net/
54 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56 * readstr http://www.geocities.co.jp/Playtown/2004/
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table[5][2] = {
68 static const int ea_adpcm_table[] = {
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables[4][16] = {
79 /*3*/ { -1, -1, 2, 4 },
80 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
86 typedef struct ADPCMDecodeContext {
87 ADPCMChannelStatus status[6];
88 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
91 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
93 ADPCMDecodeContext *c = avctx->priv_data;
94 unsigned int min_channels = 1;
95 unsigned int max_channels = 2;
97 switch(avctx->codec->id) {
98 case AV_CODEC_ID_ADPCM_EA:
101 case AV_CODEC_ID_ADPCM_AFC:
102 case AV_CODEC_ID_ADPCM_EA_R1:
103 case AV_CODEC_ID_ADPCM_EA_R2:
104 case AV_CODEC_ID_ADPCM_EA_R3:
105 case AV_CODEC_ID_ADPCM_EA_XAS:
106 case AV_CODEC_ID_ADPCM_THP:
110 if (avctx->channels < min_channels || avctx->channels > max_channels) {
111 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
112 return AVERROR(EINVAL);
115 switch(avctx->codec->id) {
116 case AV_CODEC_ID_ADPCM_CT:
117 c->status[0].step = c->status[1].step = 511;
119 case AV_CODEC_ID_ADPCM_IMA_WAV:
120 if (avctx->bits_per_coded_sample != 4) {
121 av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
125 case AV_CODEC_ID_ADPCM_IMA_APC:
126 if (avctx->extradata && avctx->extradata_size >= 8) {
127 c->status[0].predictor = AV_RL32(avctx->extradata);
128 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
131 case AV_CODEC_ID_ADPCM_IMA_WS:
132 if (avctx->extradata && avctx->extradata_size >= 2)
133 c->vqa_version = AV_RL16(avctx->extradata);
139 switch(avctx->codec->id) {
140 case AV_CODEC_ID_ADPCM_IMA_QT:
141 case AV_CODEC_ID_ADPCM_IMA_WAV:
142 case AV_CODEC_ID_ADPCM_4XM:
143 case AV_CODEC_ID_ADPCM_XA:
144 case AV_CODEC_ID_ADPCM_EA_R1:
145 case AV_CODEC_ID_ADPCM_EA_R2:
146 case AV_CODEC_ID_ADPCM_EA_R3:
147 case AV_CODEC_ID_ADPCM_EA_XAS:
148 case AV_CODEC_ID_ADPCM_THP:
149 case AV_CODEC_ID_ADPCM_AFC:
150 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
152 case AV_CODEC_ID_ADPCM_IMA_WS:
153 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
157 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
163 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
167 int sign, delta, diff, step;
169 step = ff_adpcm_step_table[c->step_index];
170 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
171 step_index = av_clip(step_index, 0, 88);
175 /* perform direct multiplication instead of series of jumps proposed by
176 * the reference ADPCM implementation since modern CPUs can do the mults
178 diff = ((2 * delta + 1) * step) >> shift;
179 predictor = c->predictor;
180 if (sign) predictor -= diff;
181 else predictor += diff;
183 c->predictor = av_clip_int16(predictor);
184 c->step_index = step_index;
186 return (short)c->predictor;
189 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
195 step = ff_adpcm_step_table[c->step_index];
196 step_index = c->step_index + ff_adpcm_index_table[nibble];
197 step_index = av_clip(step_index, 0, 88);
200 if (nibble & 4) diff += step;
201 if (nibble & 2) diff += step >> 1;
202 if (nibble & 1) diff += step >> 2;
205 predictor = c->predictor - diff;
207 predictor = c->predictor + diff;
209 c->predictor = av_clip_int16(predictor);
210 c->step_index = step_index;
215 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
219 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
220 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
222 c->sample2 = c->sample1;
223 c->sample1 = av_clip_int16(predictor);
224 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
225 if (c->idelta < 16) c->idelta = 16;
230 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
232 int step_index, predictor, sign, delta, diff, step;
234 step = ff_adpcm_oki_step_table[c->step_index];
235 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
236 step_index = av_clip(step_index, 0, 48);
240 diff = ((2 * delta + 1) * step) >> 3;
241 predictor = c->predictor;
242 if (sign) predictor -= diff;
243 else predictor += diff;
245 c->predictor = av_clip(predictor, -2048, 2047);
246 c->step_index = step_index;
248 return c->predictor << 4;
251 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
253 int sign, delta, diff;
258 /* perform direct multiplication instead of series of jumps proposed by
259 * the reference ADPCM implementation since modern CPUs can do the mults
261 diff = ((2 * delta + 1) * c->step) >> 3;
262 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
263 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
264 c->predictor = av_clip_int16(c->predictor);
265 /* calculate new step and clamp it to range 511..32767 */
266 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
267 c->step = av_clip(new_step, 511, 32767);
269 return (short)c->predictor;
272 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
274 int sign, delta, diff;
276 sign = nibble & (1<<(size-1));
277 delta = nibble & ((1<<(size-1))-1);
278 diff = delta << (7 + c->step + shift);
281 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
283 /* calculate new step */
284 if (delta >= (2*size - 3) && c->step < 3)
286 else if (delta == 0 && c->step > 0)
289 return (short) c->predictor;
292 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
299 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
300 c->predictor = av_clip_int16(c->predictor);
301 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
302 c->step = av_clip(c->step, 127, 24567);
306 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
307 const uint8_t *in, ADPCMChannelStatus *left,
308 ADPCMChannelStatus *right, int channels, int sample_offset)
311 int shift,filter,f0,f1;
315 out0 += sample_offset;
319 out1 += sample_offset;
322 shift = 12 - (in[4+i*2] & 15);
323 filter = in[4+i*2] >> 4;
324 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
325 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
328 f0 = xa_adpcm_table[filter][0];
329 f1 = xa_adpcm_table[filter][1];
337 t = sign_extend(d, 4);
338 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
340 s_1 = av_clip_int16(s);
347 s_1 = right->sample1;
348 s_2 = right->sample2;
351 shift = 12 - (in[5+i*2] & 15);
352 filter = in[5+i*2] >> 4;
353 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
354 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
358 f0 = xa_adpcm_table[filter][0];
359 f1 = xa_adpcm_table[filter][1];
364 t = sign_extend(d >> 4, 4);
365 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
367 s_1 = av_clip_int16(s);
372 right->sample1 = s_1;
373 right->sample2 = s_2;
379 out0 += 28 * (3 - channels);
380 out1 += 28 * (3 - channels);
386 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
388 ADPCMDecodeContext *c = avctx->priv_data;
391 int k0, signmask, nb_bits, count;
392 int size = buf_size*8;
395 init_get_bits(&gb, buf, size);
397 //read bits & initial values
398 nb_bits = get_bits(&gb, 2)+2;
399 table = swf_index_tables[nb_bits-2];
400 k0 = 1 << (nb_bits-2);
401 signmask = 1 << (nb_bits-1);
403 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
404 for (i = 0; i < avctx->channels; i++) {
405 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
406 c->status[i].step_index = get_bits(&gb, 6);
409 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
412 for (i = 0; i < avctx->channels; i++) {
413 // similar to IMA adpcm
414 int delta = get_bits(&gb, nb_bits);
415 int step = ff_adpcm_step_table[c->status[i].step_index];
416 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
427 if (delta & signmask)
428 c->status[i].predictor -= vpdiff;
430 c->status[i].predictor += vpdiff;
432 c->status[i].step_index += table[delta & (~signmask)];
434 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
435 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
437 *samples++ = c->status[i].predictor;
444 * Get the number of samples that will be decoded from the packet.
445 * In one case, this is actually the maximum number of samples possible to
446 * decode with the given buf_size.
448 * @param[out] coded_samples set to the number of samples as coded in the
449 * packet, or 0 if the codec does not encode the
450 * number of samples in each frame.
452 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
453 int buf_size, int *coded_samples)
455 ADPCMDecodeContext *s = avctx->priv_data;
457 int ch = avctx->channels;
458 int has_coded_samples = 0;
466 switch (avctx->codec->id) {
467 /* constant, only check buf_size */
468 case AV_CODEC_ID_ADPCM_EA_XAS:
469 if (buf_size < 76 * ch)
473 case AV_CODEC_ID_ADPCM_IMA_QT:
474 if (buf_size < 34 * ch)
478 /* simple 4-bit adpcm */
479 case AV_CODEC_ID_ADPCM_CT:
480 case AV_CODEC_ID_ADPCM_IMA_APC:
481 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
482 case AV_CODEC_ID_ADPCM_IMA_OKI:
483 case AV_CODEC_ID_ADPCM_IMA_WS:
484 case AV_CODEC_ID_ADPCM_YAMAHA:
485 nb_samples = buf_size * 2 / ch;
491 /* simple 4-bit adpcm, with header */
493 switch (avctx->codec->id) {
494 case AV_CODEC_ID_ADPCM_4XM:
495 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
496 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
497 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
500 return (buf_size - header_size) * 2 / ch;
502 /* more complex formats */
503 switch (avctx->codec->id) {
504 case AV_CODEC_ID_ADPCM_EA:
505 has_coded_samples = 1;
506 *coded_samples = bytestream2_get_le32(gb);
507 *coded_samples -= *coded_samples % 28;
508 nb_samples = (buf_size - 12) / 30 * 28;
510 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
511 has_coded_samples = 1;
512 *coded_samples = bytestream2_get_le32(gb);
513 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
515 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
516 nb_samples = (buf_size - ch) / ch * 2;
518 case AV_CODEC_ID_ADPCM_EA_R1:
519 case AV_CODEC_ID_ADPCM_EA_R2:
520 case AV_CODEC_ID_ADPCM_EA_R3:
521 /* maximum number of samples */
522 /* has internal offsets and a per-frame switch to signal raw 16-bit */
523 has_coded_samples = 1;
524 switch (avctx->codec->id) {
525 case AV_CODEC_ID_ADPCM_EA_R1:
526 header_size = 4 + 9 * ch;
527 *coded_samples = bytestream2_get_le32(gb);
529 case AV_CODEC_ID_ADPCM_EA_R2:
530 header_size = 4 + 5 * ch;
531 *coded_samples = bytestream2_get_le32(gb);
533 case AV_CODEC_ID_ADPCM_EA_R3:
534 header_size = 4 + 5 * ch;
535 *coded_samples = bytestream2_get_be32(gb);
538 *coded_samples -= *coded_samples % 28;
539 nb_samples = (buf_size - header_size) * 2 / ch;
540 nb_samples -= nb_samples % 28;
542 case AV_CODEC_ID_ADPCM_IMA_DK3:
543 if (avctx->block_align > 0)
544 buf_size = FFMIN(buf_size, avctx->block_align);
545 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
547 case AV_CODEC_ID_ADPCM_IMA_DK4:
548 if (avctx->block_align > 0)
549 buf_size = FFMIN(buf_size, avctx->block_align);
550 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
552 case AV_CODEC_ID_ADPCM_IMA_WAV:
553 if (avctx->block_align > 0)
554 buf_size = FFMIN(buf_size, avctx->block_align);
555 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
557 case AV_CODEC_ID_ADPCM_MS:
558 if (avctx->block_align > 0)
559 buf_size = FFMIN(buf_size, avctx->block_align);
560 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
562 case AV_CODEC_ID_ADPCM_SBPRO_2:
563 case AV_CODEC_ID_ADPCM_SBPRO_3:
564 case AV_CODEC_ID_ADPCM_SBPRO_4:
566 int samples_per_byte;
567 switch (avctx->codec->id) {
568 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
569 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
570 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
572 if (!s->status[0].step_index) {
576 nb_samples += buf_size * samples_per_byte / ch;
579 case AV_CODEC_ID_ADPCM_SWF:
581 int buf_bits = buf_size * 8 - 2;
582 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
583 int block_hdr_size = 22 * ch;
584 int block_size = block_hdr_size + nbits * ch * 4095;
585 int nblocks = buf_bits / block_size;
586 int bits_left = buf_bits - nblocks * block_size;
587 nb_samples = nblocks * 4096;
588 if (bits_left >= block_hdr_size)
589 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
592 case AV_CODEC_ID_ADPCM_THP:
593 has_coded_samples = 1;
594 bytestream2_skip(gb, 4); // channel size
595 *coded_samples = bytestream2_get_be32(gb);
596 *coded_samples -= *coded_samples % 14;
597 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
599 case AV_CODEC_ID_ADPCM_AFC:
600 nb_samples = buf_size / (9 * ch) * 16;
602 case AV_CODEC_ID_ADPCM_XA:
603 nb_samples = (buf_size / 128) * 224 / ch;
607 /* validate coded sample count */
608 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
609 return AVERROR_INVALIDDATA;
614 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
615 int *got_frame_ptr, AVPacket *avpkt)
617 AVFrame *frame = data;
618 const uint8_t *buf = avpkt->data;
619 int buf_size = avpkt->size;
620 ADPCMDecodeContext *c = avctx->priv_data;
621 ADPCMChannelStatus *cs;
622 int n, m, channel, i;
627 int nb_samples, coded_samples, ret;
630 bytestream2_init(&gb, buf, buf_size);
631 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
632 if (nb_samples <= 0) {
633 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
634 return AVERROR_INVALIDDATA;
637 /* get output buffer */
638 frame->nb_samples = nb_samples;
639 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
641 samples = (short *)frame->data[0];
642 samples_p = (int16_t **)frame->extended_data;
644 /* use coded_samples when applicable */
645 /* it is always <= nb_samples, so the output buffer will be large enough */
647 if (coded_samples != nb_samples)
648 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
649 frame->nb_samples = nb_samples = coded_samples;
652 st = avctx->channels == 2 ? 1 : 0;
654 switch(avctx->codec->id) {
655 case AV_CODEC_ID_ADPCM_IMA_QT:
656 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
657 Channel data is interleaved per-chunk. */
658 for (channel = 0; channel < avctx->channels; channel++) {
661 cs = &(c->status[channel]);
662 /* (pppppp) (piiiiiii) */
664 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
665 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
666 step_index = predictor & 0x7F;
669 if (cs->step_index == step_index) {
670 int diff = predictor - cs->predictor;
677 cs->step_index = step_index;
678 cs->predictor = predictor;
681 if (cs->step_index > 88u){
682 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
683 channel, cs->step_index);
684 return AVERROR_INVALIDDATA;
687 samples = samples_p[channel];
689 for (m = 0; m < 64; m += 2) {
690 int byte = bytestream2_get_byteu(&gb);
691 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
692 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
696 case AV_CODEC_ID_ADPCM_IMA_WAV:
697 for(i=0; i<avctx->channels; i++){
698 cs = &(c->status[i]);
699 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
701 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
702 if (cs->step_index > 88u){
703 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
705 return AVERROR_INVALIDDATA;
709 for (n = 0; n < (nb_samples - 1) / 8; n++) {
710 for (i = 0; i < avctx->channels; i++) {
712 samples = &samples_p[i][1 + n * 8];
713 for (m = 0; m < 8; m += 2) {
714 int v = bytestream2_get_byteu(&gb);
715 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
716 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
721 case AV_CODEC_ID_ADPCM_4XM:
722 for (i = 0; i < avctx->channels; i++)
723 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
725 for (i = 0; i < avctx->channels; i++) {
726 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
727 if (c->status[i].step_index > 88u) {
728 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
729 i, c->status[i].step_index);
730 return AVERROR_INVALIDDATA;
734 for (i = 0; i < avctx->channels; i++) {
735 samples = (int16_t *)frame->data[i];
737 for (n = nb_samples >> 1; n > 0; n--) {
738 int v = bytestream2_get_byteu(&gb);
739 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
740 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
744 case AV_CODEC_ID_ADPCM_MS:
748 block_predictor = bytestream2_get_byteu(&gb);
749 if (block_predictor > 6) {
750 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
752 return AVERROR_INVALIDDATA;
754 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
755 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
757 block_predictor = bytestream2_get_byteu(&gb);
758 if (block_predictor > 6) {
759 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
761 return AVERROR_INVALIDDATA;
763 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
764 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
766 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
768 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
771 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
772 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
773 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
774 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
776 *samples++ = c->status[0].sample2;
777 if (st) *samples++ = c->status[1].sample2;
778 *samples++ = c->status[0].sample1;
779 if (st) *samples++ = c->status[1].sample1;
780 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
781 int byte = bytestream2_get_byteu(&gb);
782 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
783 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
787 case AV_CODEC_ID_ADPCM_IMA_DK4:
788 for (channel = 0; channel < avctx->channels; channel++) {
789 cs = &c->status[channel];
790 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
791 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
792 if (cs->step_index > 88u){
793 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
794 channel, cs->step_index);
795 return AVERROR_INVALIDDATA;
798 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
799 int v = bytestream2_get_byteu(&gb);
800 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
801 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
804 case AV_CODEC_ID_ADPCM_IMA_DK3:
808 int decode_top_nibble_next = 0;
810 const int16_t *samples_end = samples + avctx->channels * nb_samples;
812 bytestream2_skipu(&gb, 10);
813 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
814 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
815 c->status[0].step_index = bytestream2_get_byteu(&gb);
816 c->status[1].step_index = bytestream2_get_byteu(&gb);
817 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
818 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
819 c->status[0].step_index, c->status[1].step_index);
820 return AVERROR_INVALIDDATA;
822 /* sign extend the predictors */
823 diff_channel = c->status[1].predictor;
825 /* DK3 ADPCM support macro */
826 #define DK3_GET_NEXT_NIBBLE() \
827 if (decode_top_nibble_next) { \
828 nibble = last_byte >> 4; \
829 decode_top_nibble_next = 0; \
831 last_byte = bytestream2_get_byteu(&gb); \
832 nibble = last_byte & 0x0F; \
833 decode_top_nibble_next = 1; \
836 while (samples < samples_end) {
838 /* for this algorithm, c->status[0] is the sum channel and
839 * c->status[1] is the diff channel */
841 /* process the first predictor of the sum channel */
842 DK3_GET_NEXT_NIBBLE();
843 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
845 /* process the diff channel predictor */
846 DK3_GET_NEXT_NIBBLE();
847 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
849 /* process the first pair of stereo PCM samples */
850 diff_channel = (diff_channel + c->status[1].predictor) / 2;
851 *samples++ = c->status[0].predictor + c->status[1].predictor;
852 *samples++ = c->status[0].predictor - c->status[1].predictor;
854 /* process the second predictor of the sum channel */
855 DK3_GET_NEXT_NIBBLE();
856 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
858 /* process the second pair of stereo PCM samples */
859 diff_channel = (diff_channel + c->status[1].predictor) / 2;
860 *samples++ = c->status[0].predictor + c->status[1].predictor;
861 *samples++ = c->status[0].predictor - c->status[1].predictor;
865 case AV_CODEC_ID_ADPCM_IMA_ISS:
866 for (channel = 0; channel < avctx->channels; channel++) {
867 cs = &c->status[channel];
868 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
869 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
870 if (cs->step_index > 88u){
871 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
872 channel, cs->step_index);
873 return AVERROR_INVALIDDATA;
877 for (n = nb_samples >> (1 - st); n > 0; n--) {
879 int v = bytestream2_get_byteu(&gb);
880 /* nibbles are swapped for mono */
888 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
889 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
892 case AV_CODEC_ID_ADPCM_IMA_APC:
893 while (bytestream2_get_bytes_left(&gb) > 0) {
894 int v = bytestream2_get_byteu(&gb);
895 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
896 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
899 case AV_CODEC_ID_ADPCM_IMA_OKI:
900 while (bytestream2_get_bytes_left(&gb) > 0) {
901 int v = bytestream2_get_byteu(&gb);
902 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
903 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
906 case AV_CODEC_ID_ADPCM_IMA_WS:
907 if (c->vqa_version == 3) {
908 for (channel = 0; channel < avctx->channels; channel++) {
909 int16_t *smp = samples_p[channel];
911 for (n = nb_samples / 2; n > 0; n--) {
912 int v = bytestream2_get_byteu(&gb);
913 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
914 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
918 for (n = nb_samples / 2; n > 0; n--) {
919 for (channel = 0; channel < avctx->channels; channel++) {
920 int v = bytestream2_get_byteu(&gb);
921 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
922 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
924 samples += avctx->channels;
927 bytestream2_seek(&gb, 0, SEEK_END);
929 case AV_CODEC_ID_ADPCM_XA:
931 int16_t *out0 = samples_p[0];
932 int16_t *out1 = samples_p[1];
933 int samples_per_block = 28 * (3 - avctx->channels) * 4;
934 int sample_offset = 0;
935 while (bytestream2_get_bytes_left(&gb) >= 128) {
936 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
937 &c->status[0], &c->status[1],
938 avctx->channels, sample_offset)) < 0)
940 bytestream2_skipu(&gb, 128);
941 sample_offset += samples_per_block;
945 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
946 for (i=0; i<=st; i++) {
947 c->status[i].step_index = bytestream2_get_le32u(&gb);
948 if (c->status[i].step_index > 88u) {
949 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
950 i, c->status[i].step_index);
951 return AVERROR_INVALIDDATA;
954 for (i=0; i<=st; i++)
955 c->status[i].predictor = bytestream2_get_le32u(&gb);
957 for (n = nb_samples >> (1 - st); n > 0; n--) {
958 int byte = bytestream2_get_byteu(&gb);
959 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
960 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
963 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
964 for (n = nb_samples >> (1 - st); n > 0; n--) {
965 int byte = bytestream2_get_byteu(&gb);
966 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
967 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
970 case AV_CODEC_ID_ADPCM_EA:
972 int previous_left_sample, previous_right_sample;
973 int current_left_sample, current_right_sample;
974 int next_left_sample, next_right_sample;
975 int coeff1l, coeff2l, coeff1r, coeff2r;
976 int shift_left, shift_right;
978 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
979 each coding 28 stereo samples. */
981 if(avctx->channels != 2)
982 return AVERROR_INVALIDDATA;
984 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
985 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
986 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
987 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
989 for (count1 = 0; count1 < nb_samples / 28; count1++) {
990 int byte = bytestream2_get_byteu(&gb);
991 coeff1l = ea_adpcm_table[ byte >> 4 ];
992 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
993 coeff1r = ea_adpcm_table[ byte & 0x0F];
994 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
996 byte = bytestream2_get_byteu(&gb);
997 shift_left = 20 - (byte >> 4);
998 shift_right = 20 - (byte & 0x0F);
1000 for (count2 = 0; count2 < 28; count2++) {
1001 byte = bytestream2_get_byteu(&gb);
1002 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1003 next_right_sample = sign_extend(byte, 4) << shift_right;
1005 next_left_sample = (next_left_sample +
1006 (current_left_sample * coeff1l) +
1007 (previous_left_sample * coeff2l) + 0x80) >> 8;
1008 next_right_sample = (next_right_sample +
1009 (current_right_sample * coeff1r) +
1010 (previous_right_sample * coeff2r) + 0x80) >> 8;
1012 previous_left_sample = current_left_sample;
1013 current_left_sample = av_clip_int16(next_left_sample);
1014 previous_right_sample = current_right_sample;
1015 current_right_sample = av_clip_int16(next_right_sample);
1016 *samples++ = current_left_sample;
1017 *samples++ = current_right_sample;
1021 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1025 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1027 int coeff[2][2], shift[2];
1029 for(channel = 0; channel < avctx->channels; channel++) {
1030 int byte = bytestream2_get_byteu(&gb);
1032 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1033 shift[channel] = 20 - (byte & 0x0F);
1035 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1038 byte[0] = bytestream2_get_byteu(&gb);
1039 if (st) byte[1] = bytestream2_get_byteu(&gb);
1040 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1041 for(channel = 0; channel < avctx->channels; channel++) {
1042 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1044 c->status[channel].sample1 * coeff[channel][0] +
1045 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1046 c->status[channel].sample2 = c->status[channel].sample1;
1047 c->status[channel].sample1 = av_clip_int16(sample);
1048 *samples++ = c->status[channel].sample1;
1052 bytestream2_seek(&gb, 0, SEEK_END);
1055 case AV_CODEC_ID_ADPCM_EA_R1:
1056 case AV_CODEC_ID_ADPCM_EA_R2:
1057 case AV_CODEC_ID_ADPCM_EA_R3: {
1058 /* channel numbering
1060 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1061 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1062 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1063 int previous_sample, current_sample, next_sample;
1066 unsigned int channel;
1071 for (channel=0; channel<avctx->channels; channel++)
1072 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1073 bytestream2_get_le32(&gb)) +
1074 (avctx->channels + 1) * 4;
1076 for (channel=0; channel<avctx->channels; channel++) {
1077 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1078 samplesC = samples_p[channel];
1080 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1081 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1082 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1084 current_sample = c->status[channel].predictor;
1085 previous_sample = c->status[channel].prev_sample;
1088 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1089 int byte = bytestream2_get_byte(&gb);
1090 if (byte == 0xEE) { /* only seen in R2 and R3 */
1091 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1092 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1094 for (count2=0; count2<28; count2++)
1095 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1097 coeff1 = ea_adpcm_table[ byte >> 4 ];
1098 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1099 shift = 20 - (byte & 0x0F);
1101 for (count2=0; count2<28; count2++) {
1103 next_sample = sign_extend(byte, 4) << shift;
1105 byte = bytestream2_get_byte(&gb);
1106 next_sample = sign_extend(byte >> 4, 4) << shift;
1109 next_sample += (current_sample * coeff1) +
1110 (previous_sample * coeff2);
1111 next_sample = av_clip_int16(next_sample >> 8);
1113 previous_sample = current_sample;
1114 current_sample = next_sample;
1115 *samplesC++ = current_sample;
1121 } else if (count != count1) {
1122 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1123 count = FFMAX(count, count1);
1126 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1127 c->status[channel].predictor = current_sample;
1128 c->status[channel].prev_sample = previous_sample;
1132 frame->nb_samples = count * 28;
1133 bytestream2_seek(&gb, 0, SEEK_END);
1136 case AV_CODEC_ID_ADPCM_EA_XAS:
1137 for (channel=0; channel<avctx->channels; channel++) {
1138 int coeff[2][4], shift[4];
1139 int16_t *s = samples_p[channel];
1140 for (n = 0; n < 4; n++, s += 32) {
1141 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1143 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1146 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1147 shift[n] = 20 - (val & 0x0F);
1151 for (m=2; m<32; m+=2) {
1152 s = &samples_p[channel][m];
1153 for (n = 0; n < 4; n++, s += 32) {
1155 int byte = bytestream2_get_byteu(&gb);
1157 level = sign_extend(byte >> 4, 4) << shift[n];
1158 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1159 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1161 level = sign_extend(byte, 4) << shift[n];
1162 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1163 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1168 case AV_CODEC_ID_ADPCM_IMA_AMV:
1169 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1170 c->status[0].step_index = bytestream2_get_le16u(&gb);
1171 bytestream2_skipu(&gb, 4);
1172 if (c->status[0].step_index > 88u) {
1173 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1174 c->status[0].step_index);
1175 return AVERROR_INVALIDDATA;
1178 for (n = nb_samples >> (1 - st); n > 0; n--) {
1179 int v = bytestream2_get_byteu(&gb);
1181 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1182 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1185 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1186 for (i = 0; i < avctx->channels; i++) {
1187 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1188 c->status[i].step_index = bytestream2_get_byteu(&gb);
1189 bytestream2_skipu(&gb, 1);
1190 if (c->status[i].step_index > 88u) {
1191 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1192 c->status[i].step_index);
1193 return AVERROR_INVALIDDATA;
1197 for (n = nb_samples >> (1 - st); n > 0; n--) {
1198 int v = bytestream2_get_byteu(&gb);
1200 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1201 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1204 case AV_CODEC_ID_ADPCM_CT:
1205 for (n = nb_samples >> (1 - st); n > 0; n--) {
1206 int v = bytestream2_get_byteu(&gb);
1207 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1208 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1211 case AV_CODEC_ID_ADPCM_SBPRO_4:
1212 case AV_CODEC_ID_ADPCM_SBPRO_3:
1213 case AV_CODEC_ID_ADPCM_SBPRO_2:
1214 if (!c->status[0].step_index) {
1215 /* the first byte is a raw sample */
1216 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1218 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1219 c->status[0].step_index = 1;
1222 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1223 for (n = nb_samples >> (1 - st); n > 0; n--) {
1224 int byte = bytestream2_get_byteu(&gb);
1225 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1227 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1230 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1231 for (n = nb_samples / 3; n > 0; n--) {
1232 int byte = bytestream2_get_byteu(&gb);
1233 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1235 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1236 (byte >> 2) & 0x07, 3, 0);
1237 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1241 for (n = nb_samples >> (2 - st); n > 0; n--) {
1242 int byte = bytestream2_get_byteu(&gb);
1243 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1245 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1246 (byte >> 4) & 0x03, 2, 2);
1247 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1248 (byte >> 2) & 0x03, 2, 2);
1249 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1254 case AV_CODEC_ID_ADPCM_SWF:
1255 adpcm_swf_decode(avctx, buf, buf_size, samples);
1256 bytestream2_seek(&gb, 0, SEEK_END);
1258 case AV_CODEC_ID_ADPCM_YAMAHA:
1259 for (n = nb_samples >> (1 - st); n > 0; n--) {
1260 int v = bytestream2_get_byteu(&gb);
1261 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1262 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1265 case AV_CODEC_ID_ADPCM_AFC:
1267 int samples_per_block;
1270 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1271 samples_per_block = avctx->extradata[0] / 16;
1272 blocks = nb_samples / avctx->extradata[0];
1274 samples_per_block = nb_samples / 16;
1278 for (m = 0; m < blocks; m++) {
1279 for (channel = 0; channel < avctx->channels; channel++) {
1280 int prev1 = c->status[channel].sample1;
1281 int prev2 = c->status[channel].sample2;
1283 samples = samples_p[channel] + m * 16;
1284 /* Read in every sample for this channel. */
1285 for (i = 0; i < samples_per_block; i++) {
1286 int byte = bytestream2_get_byteu(&gb);
1287 int scale = 1 << (byte >> 4);
1288 int index = byte & 0xf;
1289 int factor1 = ff_adpcm_afc_coeffs[0][index];
1290 int factor2 = ff_adpcm_afc_coeffs[1][index];
1292 /* Decode 16 samples. */
1293 for (n = 0; n < 16; n++) {
1297 sampledat = sign_extend(byte, 4);
1299 byte = bytestream2_get_byteu(&gb);
1300 sampledat = sign_extend(byte >> 4, 4);
1303 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1304 ((sampledat * scale) << 11)) >> 11;
1305 *samples = av_clip_int16(sampledat);
1311 c->status[channel].sample1 = prev1;
1312 c->status[channel].sample2 = prev2;
1315 bytestream2_seek(&gb, 0, SEEK_END);
1318 case AV_CODEC_ID_ADPCM_THP:
1323 for (i = 0; i < avctx->channels; i++)
1324 for (n = 0; n < 16; n++)
1325 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1327 /* Initialize the previous sample. */
1328 for (i = 0; i < avctx->channels; i++) {
1329 c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1330 c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1333 for (ch = 0; ch < avctx->channels; ch++) {
1334 samples = samples_p[ch];
1336 /* Read in every sample for this channel. */
1337 for (i = 0; i < nb_samples / 14; i++) {
1338 int byte = bytestream2_get_byteu(&gb);
1339 int index = (byte >> 4) & 7;
1340 unsigned int exp = byte & 0x0F;
1341 int factor1 = table[ch][index * 2];
1342 int factor2 = table[ch][index * 2 + 1];
1344 /* Decode 14 samples. */
1345 for (n = 0; n < 14; n++) {
1349 sampledat = sign_extend(byte, 4);
1351 byte = bytestream2_get_byteu(&gb);
1352 sampledat = sign_extend(byte >> 4, 4);
1355 sampledat = ((c->status[ch].sample1 * factor1
1356 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1357 *samples = av_clip_int16(sampledat);
1358 c->status[ch].sample2 = c->status[ch].sample1;
1359 c->status[ch].sample1 = *samples++;
1370 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1371 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1372 return AVERROR_INVALIDDATA;
1377 return bytestream2_tell(&gb);
1381 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1382 AV_SAMPLE_FMT_NONE };
1383 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
1384 AV_SAMPLE_FMT_NONE };
1385 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1387 AV_SAMPLE_FMT_NONE };
1389 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1390 AVCodec ff_ ## name_ ## _decoder = { \
1392 .type = AVMEDIA_TYPE_AUDIO, \
1394 .priv_data_size = sizeof(ADPCMDecodeContext), \
1395 .init = adpcm_decode_init, \
1396 .decode = adpcm_decode_frame, \
1397 .capabilities = CODEC_CAP_DR1, \
1398 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1399 .sample_fmts = sample_fmts_, \
1402 /* Note: Do not forget to add new entries to the Makefile as well. */
1403 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1404 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1405 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1406 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1407 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1408 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1409 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1410 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1411 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1412 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1413 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1414 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1415 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1416 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1417 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1418 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1419 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1420 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1421 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1422 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1423 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1424 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1425 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1426 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1427 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1428 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1429 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1430 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1431 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");