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[14];
88 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 AV_CODEC_ID_ADPCM_DTK:
100 case AV_CODEC_ID_ADPCM_EA:
103 case AV_CODEC_ID_ADPCM_AFC:
104 case AV_CODEC_ID_ADPCM_EA_R1:
105 case AV_CODEC_ID_ADPCM_EA_R2:
106 case AV_CODEC_ID_ADPCM_EA_R3:
107 case AV_CODEC_ID_ADPCM_EA_XAS:
110 case AV_CODEC_ID_ADPCM_PSX:
113 case AV_CODEC_ID_ADPCM_THP:
114 case AV_CODEC_ID_ADPCM_THP_LE:
118 if (avctx->channels < min_channels || avctx->channels > max_channels) {
119 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
120 return AVERROR(EINVAL);
123 switch(avctx->codec->id) {
124 case AV_CODEC_ID_ADPCM_CT:
125 c->status[0].step = c->status[1].step = 511;
127 case AV_CODEC_ID_ADPCM_IMA_WAV:
128 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
129 return AVERROR_INVALIDDATA;
131 case AV_CODEC_ID_ADPCM_IMA_APC:
132 if (avctx->extradata && avctx->extradata_size >= 8) {
133 c->status[0].predictor = AV_RL32(avctx->extradata);
134 c->status[1].predictor = AV_RL32(avctx->extradata + 4);
137 case AV_CODEC_ID_ADPCM_IMA_WS:
138 if (avctx->extradata && avctx->extradata_size >= 2)
139 c->vqa_version = AV_RL16(avctx->extradata);
145 switch(avctx->codec->id) {
146 case AV_CODEC_ID_ADPCM_IMA_QT:
147 case AV_CODEC_ID_ADPCM_IMA_WAV:
148 case AV_CODEC_ID_ADPCM_4XM:
149 case AV_CODEC_ID_ADPCM_XA:
150 case AV_CODEC_ID_ADPCM_EA_R1:
151 case AV_CODEC_ID_ADPCM_EA_R2:
152 case AV_CODEC_ID_ADPCM_EA_R3:
153 case AV_CODEC_ID_ADPCM_EA_XAS:
154 case AV_CODEC_ID_ADPCM_THP:
155 case AV_CODEC_ID_ADPCM_THP_LE:
156 case AV_CODEC_ID_ADPCM_AFC:
157 case AV_CODEC_ID_ADPCM_DTK:
158 case AV_CODEC_ID_ADPCM_PSX:
159 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
161 case AV_CODEC_ID_ADPCM_IMA_WS:
162 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
166 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
172 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
176 int sign, delta, diff, step;
178 step = ff_adpcm_step_table[c->step_index];
179 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
180 step_index = av_clip(step_index, 0, 88);
184 /* perform direct multiplication instead of series of jumps proposed by
185 * the reference ADPCM implementation since modern CPUs can do the mults
187 diff = ((2 * delta + 1) * step) >> shift;
188 predictor = c->predictor;
189 if (sign) predictor -= diff;
190 else predictor += diff;
192 c->predictor = av_clip_int16(predictor);
193 c->step_index = step_index;
195 return (short)c->predictor;
198 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
200 int nibble, step_index, predictor, sign, delta, diff, step, shift;
203 nibble = get_bits_le(gb, bps),
204 step = ff_adpcm_step_table[c->step_index];
205 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
206 step_index = av_clip(step_index, 0, 88);
208 sign = nibble & (1 << shift);
209 delta = av_mod_uintp2(nibble, shift);
210 diff = ((2 * delta + 1) * step) >> shift;
211 predictor = c->predictor;
212 if (sign) predictor -= diff;
213 else predictor += diff;
215 c->predictor = av_clip_int16(predictor);
216 c->step_index = step_index;
218 return (int16_t)c->predictor;
221 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
227 step = ff_adpcm_step_table[c->step_index];
228 step_index = c->step_index + ff_adpcm_index_table[nibble];
229 step_index = av_clip(step_index, 0, 88);
232 if (nibble & 4) diff += step;
233 if (nibble & 2) diff += step >> 1;
234 if (nibble & 1) diff += step >> 2;
237 predictor = c->predictor - diff;
239 predictor = c->predictor + diff;
241 c->predictor = av_clip_int16(predictor);
242 c->step_index = step_index;
247 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
251 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
252 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
254 c->sample2 = c->sample1;
255 c->sample1 = av_clip_int16(predictor);
256 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
257 if (c->idelta < 16) c->idelta = 16;
258 if (c->idelta > INT_MAX/768) {
259 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
260 c->idelta = INT_MAX/768;
266 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
268 int step_index, predictor, sign, delta, diff, step;
270 step = ff_adpcm_oki_step_table[c->step_index];
271 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
272 step_index = av_clip(step_index, 0, 48);
276 diff = ((2 * delta + 1) * step) >> 3;
277 predictor = c->predictor;
278 if (sign) predictor -= diff;
279 else predictor += diff;
281 c->predictor = av_clip_intp2(predictor, 11);
282 c->step_index = step_index;
284 return c->predictor << 4;
287 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
289 int sign, delta, diff;
294 /* perform direct multiplication instead of series of jumps proposed by
295 * the reference ADPCM implementation since modern CPUs can do the mults
297 diff = ((2 * delta + 1) * c->step) >> 3;
298 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
299 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
300 c->predictor = av_clip_int16(c->predictor);
301 /* calculate new step and clamp it to range 511..32767 */
302 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
303 c->step = av_clip(new_step, 511, 32767);
305 return (short)c->predictor;
308 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
310 int sign, delta, diff;
312 sign = nibble & (1<<(size-1));
313 delta = nibble & ((1<<(size-1))-1);
314 diff = delta << (7 + c->step + shift);
317 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
319 /* calculate new step */
320 if (delta >= (2*size - 3) && c->step < 3)
322 else if (delta == 0 && c->step > 0)
325 return (short) c->predictor;
328 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
335 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
336 c->predictor = av_clip_int16(c->predictor);
337 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
338 c->step = av_clip(c->step, 127, 24567);
342 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
343 const uint8_t *in, ADPCMChannelStatus *left,
344 ADPCMChannelStatus *right, int channels, int sample_offset)
347 int shift,filter,f0,f1;
351 out0 += sample_offset;
355 out1 += sample_offset;
358 shift = 12 - (in[4+i*2] & 15);
359 filter = in[4+i*2] >> 4;
360 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
361 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
364 f0 = xa_adpcm_table[filter][0];
365 f1 = xa_adpcm_table[filter][1];
373 t = sign_extend(d, 4);
374 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
376 s_1 = av_clip_int16(s);
383 s_1 = right->sample1;
384 s_2 = right->sample2;
387 shift = 12 - (in[5+i*2] & 15);
388 filter = in[5+i*2] >> 4;
389 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
390 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
394 f0 = xa_adpcm_table[filter][0];
395 f1 = xa_adpcm_table[filter][1];
400 t = sign_extend(d >> 4, 4);
401 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
403 s_1 = av_clip_int16(s);
408 right->sample1 = s_1;
409 right->sample2 = s_2;
415 out0 += 28 * (3 - channels);
416 out1 += 28 * (3 - channels);
422 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
424 ADPCMDecodeContext *c = avctx->priv_data;
427 int k0, signmask, nb_bits, count;
428 int size = buf_size*8;
431 init_get_bits(&gb, buf, size);
433 //read bits & initial values
434 nb_bits = get_bits(&gb, 2)+2;
435 table = swf_index_tables[nb_bits-2];
436 k0 = 1 << (nb_bits-2);
437 signmask = 1 << (nb_bits-1);
439 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
440 for (i = 0; i < avctx->channels; i++) {
441 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
442 c->status[i].step_index = get_bits(&gb, 6);
445 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
448 for (i = 0; i < avctx->channels; i++) {
449 // similar to IMA adpcm
450 int delta = get_bits(&gb, nb_bits);
451 int step = ff_adpcm_step_table[c->status[i].step_index];
452 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
463 if (delta & signmask)
464 c->status[i].predictor -= vpdiff;
466 c->status[i].predictor += vpdiff;
468 c->status[i].step_index += table[delta & (~signmask)];
470 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
471 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
473 *samples++ = c->status[i].predictor;
480 * Get the number of samples that will be decoded from the packet.
481 * In one case, this is actually the maximum number of samples possible to
482 * decode with the given buf_size.
484 * @param[out] coded_samples set to the number of samples as coded in the
485 * packet, or 0 if the codec does not encode the
486 * number of samples in each frame.
487 * @param[out] approx_nb_samples set to non-zero if the number of samples
488 * returned is an approximation.
490 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
491 int buf_size, int *coded_samples, int *approx_nb_samples)
493 ADPCMDecodeContext *s = avctx->priv_data;
495 int ch = avctx->channels;
496 int has_coded_samples = 0;
500 *approx_nb_samples = 0;
505 switch (avctx->codec->id) {
506 /* constant, only check buf_size */
507 case AV_CODEC_ID_ADPCM_EA_XAS:
508 if (buf_size < 76 * ch)
512 case AV_CODEC_ID_ADPCM_IMA_QT:
513 if (buf_size < 34 * ch)
517 /* simple 4-bit adpcm */
518 case AV_CODEC_ID_ADPCM_CT:
519 case AV_CODEC_ID_ADPCM_IMA_APC:
520 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
521 case AV_CODEC_ID_ADPCM_IMA_OKI:
522 case AV_CODEC_ID_ADPCM_IMA_WS:
523 case AV_CODEC_ID_ADPCM_YAMAHA:
524 nb_samples = buf_size * 2 / ch;
530 /* simple 4-bit adpcm, with header */
532 switch (avctx->codec->id) {
533 case AV_CODEC_ID_ADPCM_4XM:
534 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
535 case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
536 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
539 return (buf_size - header_size) * 2 / ch;
541 /* more complex formats */
542 switch (avctx->codec->id) {
543 case AV_CODEC_ID_ADPCM_EA:
544 has_coded_samples = 1;
545 *coded_samples = bytestream2_get_le32(gb);
546 *coded_samples -= *coded_samples % 28;
547 nb_samples = (buf_size - 12) / 30 * 28;
549 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
550 has_coded_samples = 1;
551 *coded_samples = bytestream2_get_le32(gb);
552 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
554 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
555 nb_samples = (buf_size - ch) / ch * 2;
557 case AV_CODEC_ID_ADPCM_EA_R1:
558 case AV_CODEC_ID_ADPCM_EA_R2:
559 case AV_CODEC_ID_ADPCM_EA_R3:
560 /* maximum number of samples */
561 /* has internal offsets and a per-frame switch to signal raw 16-bit */
562 has_coded_samples = 1;
563 switch (avctx->codec->id) {
564 case AV_CODEC_ID_ADPCM_EA_R1:
565 header_size = 4 + 9 * ch;
566 *coded_samples = bytestream2_get_le32(gb);
568 case AV_CODEC_ID_ADPCM_EA_R2:
569 header_size = 4 + 5 * ch;
570 *coded_samples = bytestream2_get_le32(gb);
572 case AV_CODEC_ID_ADPCM_EA_R3:
573 header_size = 4 + 5 * ch;
574 *coded_samples = bytestream2_get_be32(gb);
577 *coded_samples -= *coded_samples % 28;
578 nb_samples = (buf_size - header_size) * 2 / ch;
579 nb_samples -= nb_samples % 28;
580 *approx_nb_samples = 1;
582 case AV_CODEC_ID_ADPCM_IMA_DK3:
583 if (avctx->block_align > 0)
584 buf_size = FFMIN(buf_size, avctx->block_align);
585 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
587 case AV_CODEC_ID_ADPCM_IMA_DK4:
588 if (avctx->block_align > 0)
589 buf_size = FFMIN(buf_size, avctx->block_align);
590 if (buf_size < 4 * ch)
591 return AVERROR_INVALIDDATA;
592 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
594 case AV_CODEC_ID_ADPCM_IMA_RAD:
595 if (avctx->block_align > 0)
596 buf_size = FFMIN(buf_size, avctx->block_align);
597 nb_samples = (buf_size - 4 * ch) * 2 / ch;
599 case AV_CODEC_ID_ADPCM_IMA_WAV:
601 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
602 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
603 if (avctx->block_align > 0)
604 buf_size = FFMIN(buf_size, avctx->block_align);
605 if (buf_size < 4 * ch)
606 return AVERROR_INVALIDDATA;
607 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
610 case AV_CODEC_ID_ADPCM_MS:
611 if (avctx->block_align > 0)
612 buf_size = FFMIN(buf_size, avctx->block_align);
613 nb_samples = (buf_size - 6 * ch) * 2 / ch;
615 case AV_CODEC_ID_ADPCM_SBPRO_2:
616 case AV_CODEC_ID_ADPCM_SBPRO_3:
617 case AV_CODEC_ID_ADPCM_SBPRO_4:
619 int samples_per_byte;
620 switch (avctx->codec->id) {
621 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
622 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
623 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
625 if (!s->status[0].step_index) {
627 return AVERROR_INVALIDDATA;
631 nb_samples += buf_size * samples_per_byte / ch;
634 case AV_CODEC_ID_ADPCM_SWF:
636 int buf_bits = buf_size * 8 - 2;
637 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
638 int block_hdr_size = 22 * ch;
639 int block_size = block_hdr_size + nbits * ch * 4095;
640 int nblocks = buf_bits / block_size;
641 int bits_left = buf_bits - nblocks * block_size;
642 nb_samples = nblocks * 4096;
643 if (bits_left >= block_hdr_size)
644 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
647 case AV_CODEC_ID_ADPCM_THP:
648 case AV_CODEC_ID_ADPCM_THP_LE:
649 if (avctx->extradata) {
650 nb_samples = buf_size * 14 / (8 * ch);
653 has_coded_samples = 1;
654 bytestream2_skip(gb, 4); // channel size
655 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
656 bytestream2_get_le32(gb) :
657 bytestream2_get_be32(gb);
658 buf_size -= 8 + 36 * ch;
660 nb_samples = buf_size / 8 * 14;
661 if (buf_size % 8 > 1)
662 nb_samples += (buf_size % 8 - 1) * 2;
663 *approx_nb_samples = 1;
665 case AV_CODEC_ID_ADPCM_AFC:
666 nb_samples = buf_size / (9 * ch) * 16;
668 case AV_CODEC_ID_ADPCM_XA:
669 nb_samples = (buf_size / 128) * 224 / ch;
671 case AV_CODEC_ID_ADPCM_DTK:
672 case AV_CODEC_ID_ADPCM_PSX:
673 nb_samples = buf_size / (16 * ch) * 28;
677 /* validate coded sample count */
678 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
679 return AVERROR_INVALIDDATA;
684 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
685 int *got_frame_ptr, AVPacket *avpkt)
687 AVFrame *frame = data;
688 const uint8_t *buf = avpkt->data;
689 int buf_size = avpkt->size;
690 ADPCMDecodeContext *c = avctx->priv_data;
691 ADPCMChannelStatus *cs;
692 int n, m, channel, i;
697 int nb_samples, coded_samples, approx_nb_samples, ret;
700 bytestream2_init(&gb, buf, buf_size);
701 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
702 if (nb_samples <= 0) {
703 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
704 return AVERROR_INVALIDDATA;
707 /* get output buffer */
708 frame->nb_samples = nb_samples;
709 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
711 samples = (short *)frame->data[0];
712 samples_p = (int16_t **)frame->extended_data;
714 /* use coded_samples when applicable */
715 /* it is always <= nb_samples, so the output buffer will be large enough */
717 if (!approx_nb_samples && coded_samples != nb_samples)
718 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
719 frame->nb_samples = nb_samples = coded_samples;
722 st = avctx->channels == 2 ? 1 : 0;
724 switch(avctx->codec->id) {
725 case AV_CODEC_ID_ADPCM_IMA_QT:
726 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
727 Channel data is interleaved per-chunk. */
728 for (channel = 0; channel < avctx->channels; channel++) {
731 cs = &(c->status[channel]);
732 /* (pppppp) (piiiiiii) */
734 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
735 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
736 step_index = predictor & 0x7F;
739 if (cs->step_index == step_index) {
740 int diff = predictor - cs->predictor;
747 cs->step_index = step_index;
748 cs->predictor = predictor;
751 if (cs->step_index > 88u){
752 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
753 channel, cs->step_index);
754 return AVERROR_INVALIDDATA;
757 samples = samples_p[channel];
759 for (m = 0; m < 64; m += 2) {
760 int byte = bytestream2_get_byteu(&gb);
761 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
762 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
766 case AV_CODEC_ID_ADPCM_IMA_WAV:
767 for(i=0; i<avctx->channels; i++){
768 cs = &(c->status[i]);
769 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
771 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
772 if (cs->step_index > 88u){
773 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
775 return AVERROR_INVALIDDATA;
779 if (avctx->bits_per_coded_sample != 4) {
780 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
783 ret = init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
786 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
787 for (i = 0; i < avctx->channels; i++) {
789 samples = &samples_p[i][1 + n * samples_per_block];
790 for (m = 0; m < samples_per_block; m++) {
791 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
792 avctx->bits_per_coded_sample);
796 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
798 for (n = 0; n < (nb_samples - 1) / 8; n++) {
799 for (i = 0; i < avctx->channels; i++) {
801 samples = &samples_p[i][1 + n * 8];
802 for (m = 0; m < 8; m += 2) {
803 int v = bytestream2_get_byteu(&gb);
804 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
805 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
811 case AV_CODEC_ID_ADPCM_4XM:
812 for (i = 0; i < avctx->channels; i++)
813 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
815 for (i = 0; i < avctx->channels; i++) {
816 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
817 if (c->status[i].step_index > 88u) {
818 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
819 i, c->status[i].step_index);
820 return AVERROR_INVALIDDATA;
824 for (i = 0; i < avctx->channels; i++) {
825 samples = (int16_t *)frame->data[i];
827 for (n = nb_samples >> 1; n > 0; n--) {
828 int v = bytestream2_get_byteu(&gb);
829 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
830 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
834 case AV_CODEC_ID_ADPCM_MS:
838 block_predictor = bytestream2_get_byteu(&gb);
839 if (block_predictor > 6) {
840 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
842 return AVERROR_INVALIDDATA;
844 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
845 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
847 block_predictor = bytestream2_get_byteu(&gb);
848 if (block_predictor > 6) {
849 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
851 return AVERROR_INVALIDDATA;
853 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
854 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
856 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
858 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
861 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
862 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
863 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
864 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
866 *samples++ = c->status[0].sample2;
867 if (st) *samples++ = c->status[1].sample2;
868 *samples++ = c->status[0].sample1;
869 if (st) *samples++ = c->status[1].sample1;
870 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
871 int byte = bytestream2_get_byteu(&gb);
872 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
873 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
877 case AV_CODEC_ID_ADPCM_IMA_DK4:
878 for (channel = 0; channel < avctx->channels; channel++) {
879 cs = &c->status[channel];
880 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
881 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
882 if (cs->step_index > 88u){
883 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
884 channel, cs->step_index);
885 return AVERROR_INVALIDDATA;
888 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
889 int v = bytestream2_get_byteu(&gb);
890 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
891 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
894 case AV_CODEC_ID_ADPCM_IMA_DK3:
898 int decode_top_nibble_next = 0;
900 const int16_t *samples_end = samples + avctx->channels * nb_samples;
902 bytestream2_skipu(&gb, 10);
903 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
904 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
905 c->status[0].step_index = bytestream2_get_byteu(&gb);
906 c->status[1].step_index = bytestream2_get_byteu(&gb);
907 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
908 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
909 c->status[0].step_index, c->status[1].step_index);
910 return AVERROR_INVALIDDATA;
912 /* sign extend the predictors */
913 diff_channel = c->status[1].predictor;
915 /* DK3 ADPCM support macro */
916 #define DK3_GET_NEXT_NIBBLE() \
917 if (decode_top_nibble_next) { \
918 nibble = last_byte >> 4; \
919 decode_top_nibble_next = 0; \
921 last_byte = bytestream2_get_byteu(&gb); \
922 nibble = last_byte & 0x0F; \
923 decode_top_nibble_next = 1; \
926 while (samples < samples_end) {
928 /* for this algorithm, c->status[0] is the sum channel and
929 * c->status[1] is the diff channel */
931 /* process the first predictor of the sum channel */
932 DK3_GET_NEXT_NIBBLE();
933 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
935 /* process the diff channel predictor */
936 DK3_GET_NEXT_NIBBLE();
937 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
939 /* process the first pair of stereo PCM samples */
940 diff_channel = (diff_channel + c->status[1].predictor) / 2;
941 *samples++ = c->status[0].predictor + c->status[1].predictor;
942 *samples++ = c->status[0].predictor - c->status[1].predictor;
944 /* process the second predictor of the sum channel */
945 DK3_GET_NEXT_NIBBLE();
946 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
948 /* process the second pair of stereo PCM samples */
949 diff_channel = (diff_channel + c->status[1].predictor) / 2;
950 *samples++ = c->status[0].predictor + c->status[1].predictor;
951 *samples++ = c->status[0].predictor - c->status[1].predictor;
954 if ((bytestream2_tell(&gb) & 1))
955 bytestream2_skip(&gb, 1);
958 case AV_CODEC_ID_ADPCM_IMA_ISS:
959 for (channel = 0; channel < avctx->channels; channel++) {
960 cs = &c->status[channel];
961 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
962 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
963 if (cs->step_index > 88u){
964 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
965 channel, cs->step_index);
966 return AVERROR_INVALIDDATA;
970 for (n = nb_samples >> (1 - st); n > 0; n--) {
972 int v = bytestream2_get_byteu(&gb);
973 /* nibbles are swapped for mono */
981 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
982 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
985 case AV_CODEC_ID_ADPCM_IMA_APC:
986 while (bytestream2_get_bytes_left(&gb) > 0) {
987 int v = bytestream2_get_byteu(&gb);
988 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
989 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
992 case AV_CODEC_ID_ADPCM_IMA_OKI:
993 while (bytestream2_get_bytes_left(&gb) > 0) {
994 int v = bytestream2_get_byteu(&gb);
995 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
996 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
999 case AV_CODEC_ID_ADPCM_IMA_RAD:
1000 for (channel = 0; channel < avctx->channels; channel++) {
1001 cs = &c->status[channel];
1002 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1003 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1004 if (cs->step_index > 88u){
1005 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1006 channel, cs->step_index);
1007 return AVERROR_INVALIDDATA;
1010 for (n = 0; n < nb_samples / 2; n++) {
1013 byte[0] = bytestream2_get_byteu(&gb);
1015 byte[1] = bytestream2_get_byteu(&gb);
1016 for(channel = 0; channel < avctx->channels; channel++) {
1017 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1019 for(channel = 0; channel < avctx->channels; channel++) {
1020 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1024 case AV_CODEC_ID_ADPCM_IMA_WS:
1025 if (c->vqa_version == 3) {
1026 for (channel = 0; channel < avctx->channels; channel++) {
1027 int16_t *smp = samples_p[channel];
1029 for (n = nb_samples / 2; n > 0; n--) {
1030 int v = bytestream2_get_byteu(&gb);
1031 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1032 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1036 for (n = nb_samples / 2; n > 0; n--) {
1037 for (channel = 0; channel < avctx->channels; channel++) {
1038 int v = bytestream2_get_byteu(&gb);
1039 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1040 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1042 samples += avctx->channels;
1045 bytestream2_seek(&gb, 0, SEEK_END);
1047 case AV_CODEC_ID_ADPCM_XA:
1049 int16_t *out0 = samples_p[0];
1050 int16_t *out1 = samples_p[1];
1051 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1052 int sample_offset = 0;
1053 while (bytestream2_get_bytes_left(&gb) >= 128) {
1054 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1055 &c->status[0], &c->status[1],
1056 avctx->channels, sample_offset)) < 0)
1058 bytestream2_skipu(&gb, 128);
1059 sample_offset += samples_per_block;
1063 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1064 for (i=0; i<=st; i++) {
1065 c->status[i].step_index = bytestream2_get_le32u(&gb);
1066 if (c->status[i].step_index > 88u) {
1067 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1068 i, c->status[i].step_index);
1069 return AVERROR_INVALIDDATA;
1072 for (i=0; i<=st; i++)
1073 c->status[i].predictor = bytestream2_get_le32u(&gb);
1075 for (n = nb_samples >> (1 - st); n > 0; n--) {
1076 int byte = bytestream2_get_byteu(&gb);
1077 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1078 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1081 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1082 for (n = nb_samples >> (1 - st); n > 0; n--) {
1083 int byte = bytestream2_get_byteu(&gb);
1084 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1085 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1088 case AV_CODEC_ID_ADPCM_EA:
1090 int previous_left_sample, previous_right_sample;
1091 int current_left_sample, current_right_sample;
1092 int next_left_sample, next_right_sample;
1093 int coeff1l, coeff2l, coeff1r, coeff2r;
1094 int shift_left, shift_right;
1096 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1097 each coding 28 stereo samples. */
1099 if(avctx->channels != 2)
1100 return AVERROR_INVALIDDATA;
1102 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1103 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1104 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1105 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1107 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1108 int byte = bytestream2_get_byteu(&gb);
1109 coeff1l = ea_adpcm_table[ byte >> 4 ];
1110 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1111 coeff1r = ea_adpcm_table[ byte & 0x0F];
1112 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1114 byte = bytestream2_get_byteu(&gb);
1115 shift_left = 20 - (byte >> 4);
1116 shift_right = 20 - (byte & 0x0F);
1118 for (count2 = 0; count2 < 28; count2++) {
1119 byte = bytestream2_get_byteu(&gb);
1120 next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1121 next_right_sample = sign_extend(byte, 4) << shift_right;
1123 next_left_sample = (next_left_sample +
1124 (current_left_sample * coeff1l) +
1125 (previous_left_sample * coeff2l) + 0x80) >> 8;
1126 next_right_sample = (next_right_sample +
1127 (current_right_sample * coeff1r) +
1128 (previous_right_sample * coeff2r) + 0x80) >> 8;
1130 previous_left_sample = current_left_sample;
1131 current_left_sample = av_clip_int16(next_left_sample);
1132 previous_right_sample = current_right_sample;
1133 current_right_sample = av_clip_int16(next_right_sample);
1134 *samples++ = current_left_sample;
1135 *samples++ = current_right_sample;
1139 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1143 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1145 int coeff[2][2], shift[2];
1147 for(channel = 0; channel < avctx->channels; channel++) {
1148 int byte = bytestream2_get_byteu(&gb);
1150 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1151 shift[channel] = 20 - (byte & 0x0F);
1153 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1156 byte[0] = bytestream2_get_byteu(&gb);
1157 if (st) byte[1] = bytestream2_get_byteu(&gb);
1158 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1159 for(channel = 0; channel < avctx->channels; channel++) {
1160 int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1162 c->status[channel].sample1 * coeff[channel][0] +
1163 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1164 c->status[channel].sample2 = c->status[channel].sample1;
1165 c->status[channel].sample1 = av_clip_int16(sample);
1166 *samples++ = c->status[channel].sample1;
1170 bytestream2_seek(&gb, 0, SEEK_END);
1173 case AV_CODEC_ID_ADPCM_EA_R1:
1174 case AV_CODEC_ID_ADPCM_EA_R2:
1175 case AV_CODEC_ID_ADPCM_EA_R3: {
1176 /* channel numbering
1178 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1179 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1180 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1181 int previous_sample, current_sample, next_sample;
1184 unsigned int channel;
1189 for (channel=0; channel<avctx->channels; channel++)
1190 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1191 bytestream2_get_le32(&gb)) +
1192 (avctx->channels + 1) * 4;
1194 for (channel=0; channel<avctx->channels; channel++) {
1195 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1196 samplesC = samples_p[channel];
1198 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1199 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1200 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1202 current_sample = c->status[channel].predictor;
1203 previous_sample = c->status[channel].prev_sample;
1206 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1207 int byte = bytestream2_get_byte(&gb);
1208 if (byte == 0xEE) { /* only seen in R2 and R3 */
1209 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1210 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1212 for (count2=0; count2<28; count2++)
1213 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1215 coeff1 = ea_adpcm_table[ byte >> 4 ];
1216 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1217 shift = 20 - (byte & 0x0F);
1219 for (count2=0; count2<28; count2++) {
1221 next_sample = sign_extend(byte, 4) << shift;
1223 byte = bytestream2_get_byte(&gb);
1224 next_sample = sign_extend(byte >> 4, 4) << shift;
1227 next_sample += (current_sample * coeff1) +
1228 (previous_sample * coeff2);
1229 next_sample = av_clip_int16(next_sample >> 8);
1231 previous_sample = current_sample;
1232 current_sample = next_sample;
1233 *samplesC++ = current_sample;
1239 } else if (count != count1) {
1240 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1241 count = FFMAX(count, count1);
1244 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1245 c->status[channel].predictor = current_sample;
1246 c->status[channel].prev_sample = previous_sample;
1250 frame->nb_samples = count * 28;
1251 bytestream2_seek(&gb, 0, SEEK_END);
1254 case AV_CODEC_ID_ADPCM_EA_XAS:
1255 for (channel=0; channel<avctx->channels; channel++) {
1256 int coeff[2][4], shift[4];
1257 int16_t *s = samples_p[channel];
1258 for (n = 0; n < 4; n++, s += 32) {
1259 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1261 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1264 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1265 shift[n] = 20 - (val & 0x0F);
1269 for (m=2; m<32; m+=2) {
1270 s = &samples_p[channel][m];
1271 for (n = 0; n < 4; n++, s += 32) {
1273 int byte = bytestream2_get_byteu(&gb);
1275 level = sign_extend(byte >> 4, 4) << shift[n];
1276 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1277 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1279 level = sign_extend(byte, 4) << shift[n];
1280 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1281 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1286 case AV_CODEC_ID_ADPCM_IMA_AMV:
1287 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1288 c->status[0].step_index = bytestream2_get_le16u(&gb);
1289 bytestream2_skipu(&gb, 4);
1290 if (c->status[0].step_index > 88u) {
1291 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1292 c->status[0].step_index);
1293 return AVERROR_INVALIDDATA;
1296 for (n = nb_samples >> (1 - st); n > 0; n--) {
1297 int v = bytestream2_get_byteu(&gb);
1299 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1300 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1303 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1304 for (i = 0; i < avctx->channels; i++) {
1305 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1306 c->status[i].step_index = bytestream2_get_byteu(&gb);
1307 bytestream2_skipu(&gb, 1);
1308 if (c->status[i].step_index > 88u) {
1309 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1310 c->status[i].step_index);
1311 return AVERROR_INVALIDDATA;
1315 for (n = nb_samples >> (1 - st); n > 0; n--) {
1316 int v = bytestream2_get_byteu(&gb);
1318 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1319 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1322 case AV_CODEC_ID_ADPCM_CT:
1323 for (n = nb_samples >> (1 - st); n > 0; n--) {
1324 int v = bytestream2_get_byteu(&gb);
1325 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1326 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1329 case AV_CODEC_ID_ADPCM_SBPRO_4:
1330 case AV_CODEC_ID_ADPCM_SBPRO_3:
1331 case AV_CODEC_ID_ADPCM_SBPRO_2:
1332 if (!c->status[0].step_index) {
1333 /* the first byte is a raw sample */
1334 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1336 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1337 c->status[0].step_index = 1;
1340 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1341 for (n = nb_samples >> (1 - st); n > 0; n--) {
1342 int byte = bytestream2_get_byteu(&gb);
1343 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1345 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1348 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1349 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1350 int byte = bytestream2_get_byteu(&gb);
1351 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1353 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1354 (byte >> 2) & 0x07, 3, 0);
1355 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1359 for (n = nb_samples >> (2 - st); n > 0; n--) {
1360 int byte = bytestream2_get_byteu(&gb);
1361 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1363 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1364 (byte >> 4) & 0x03, 2, 2);
1365 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1366 (byte >> 2) & 0x03, 2, 2);
1367 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1372 case AV_CODEC_ID_ADPCM_SWF:
1373 adpcm_swf_decode(avctx, buf, buf_size, samples);
1374 bytestream2_seek(&gb, 0, SEEK_END);
1376 case AV_CODEC_ID_ADPCM_YAMAHA:
1377 for (n = nb_samples >> (1 - st); n > 0; n--) {
1378 int v = bytestream2_get_byteu(&gb);
1379 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1380 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1383 case AV_CODEC_ID_ADPCM_AFC:
1385 int samples_per_block;
1388 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1389 samples_per_block = avctx->extradata[0] / 16;
1390 blocks = nb_samples / avctx->extradata[0];
1392 samples_per_block = nb_samples / 16;
1396 for (m = 0; m < blocks; m++) {
1397 for (channel = 0; channel < avctx->channels; channel++) {
1398 int prev1 = c->status[channel].sample1;
1399 int prev2 = c->status[channel].sample2;
1401 samples = samples_p[channel] + m * 16;
1402 /* Read in every sample for this channel. */
1403 for (i = 0; i < samples_per_block; i++) {
1404 int byte = bytestream2_get_byteu(&gb);
1405 int scale = 1 << (byte >> 4);
1406 int index = byte & 0xf;
1407 int factor1 = ff_adpcm_afc_coeffs[0][index];
1408 int factor2 = ff_adpcm_afc_coeffs[1][index];
1410 /* Decode 16 samples. */
1411 for (n = 0; n < 16; n++) {
1415 sampledat = sign_extend(byte, 4);
1417 byte = bytestream2_get_byteu(&gb);
1418 sampledat = sign_extend(byte >> 4, 4);
1421 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1422 ((sampledat * scale) << 11)) >> 11;
1423 *samples = av_clip_int16(sampledat);
1429 c->status[channel].sample1 = prev1;
1430 c->status[channel].sample2 = prev2;
1433 bytestream2_seek(&gb, 0, SEEK_END);
1436 case AV_CODEC_ID_ADPCM_THP:
1437 case AV_CODEC_ID_ADPCM_THP_LE:
1442 #define THP_GET16(g) \
1444 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1445 bytestream2_get_le16u(&(g)) : \
1446 bytestream2_get_be16u(&(g)), 16)
1448 if (avctx->extradata) {
1450 if (avctx->extradata_size < 32 * avctx->channels) {
1451 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1452 return AVERROR_INVALIDDATA;
1455 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1456 for (i = 0; i < avctx->channels; i++)
1457 for (n = 0; n < 16; n++)
1458 table[i][n] = THP_GET16(tb);
1460 for (i = 0; i < avctx->channels; i++)
1461 for (n = 0; n < 16; n++)
1462 table[i][n] = THP_GET16(gb);
1464 if (!c->has_status) {
1465 /* Initialize the previous sample. */
1466 for (i = 0; i < avctx->channels; i++) {
1467 c->status[i].sample1 = THP_GET16(gb);
1468 c->status[i].sample2 = THP_GET16(gb);
1472 bytestream2_skip(&gb, avctx->channels * 4);
1476 for (ch = 0; ch < avctx->channels; ch++) {
1477 samples = samples_p[ch];
1479 /* Read in every sample for this channel. */
1480 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1481 int byte = bytestream2_get_byteu(&gb);
1482 int index = (byte >> 4) & 7;
1483 unsigned int exp = byte & 0x0F;
1484 int factor1 = table[ch][index * 2];
1485 int factor2 = table[ch][index * 2 + 1];
1487 /* Decode 14 samples. */
1488 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1492 sampledat = sign_extend(byte, 4);
1494 byte = bytestream2_get_byteu(&gb);
1495 sampledat = sign_extend(byte >> 4, 4);
1498 sampledat = ((c->status[ch].sample1 * factor1
1499 + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1500 *samples = av_clip_int16(sampledat);
1501 c->status[ch].sample2 = c->status[ch].sample1;
1502 c->status[ch].sample1 = *samples++;
1508 case AV_CODEC_ID_ADPCM_DTK:
1509 for (channel = 0; channel < avctx->channels; channel++) {
1510 samples = samples_p[channel];
1512 /* Read in every sample for this channel. */
1513 for (i = 0; i < nb_samples / 28; i++) {
1516 bytestream2_skipu(&gb, 1);
1517 header = bytestream2_get_byteu(&gb);
1518 bytestream2_skipu(&gb, 3 - channel);
1520 /* Decode 28 samples. */
1521 for (n = 0; n < 28; n++) {
1522 int32_t sampledat, prev;
1524 switch (header >> 4) {
1526 prev = (c->status[channel].sample1 * 0x3c);
1529 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1532 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1538 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1540 byte = bytestream2_get_byteu(&gb);
1542 sampledat = sign_extend(byte, 4);
1544 sampledat = sign_extend(byte >> 4, 4);
1546 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1547 *samples++ = av_clip_int16(sampledat >> 6);
1548 c->status[channel].sample2 = c->status[channel].sample1;
1549 c->status[channel].sample1 = sampledat;
1553 bytestream2_seek(&gb, 0, SEEK_SET);
1556 case AV_CODEC_ID_ADPCM_PSX:
1557 for (channel = 0; channel < avctx->channels; channel++) {
1558 samples = samples_p[channel];
1560 /* Read in every sample for this channel. */
1561 for (i = 0; i < nb_samples / 28; i++) {
1562 int filter, shift, flag, byte;
1564 filter = bytestream2_get_byteu(&gb);
1565 shift = filter & 0xf;
1566 filter = filter >> 4;
1567 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1568 return AVERROR_INVALIDDATA;
1569 flag = bytestream2_get_byteu(&gb);
1571 /* Decode 28 samples. */
1572 for (n = 0; n < 28; n++) {
1573 int sample = 0, scale;
1577 scale = sign_extend(byte >> 4, 4);
1579 byte = bytestream2_get_byteu(&gb);
1580 scale = sign_extend(byte, 4);
1583 scale = scale << 12;
1584 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1586 *samples++ = av_clip_int16(sample);
1587 c->status[channel].sample2 = c->status[channel].sample1;
1588 c->status[channel].sample1 = sample;
1598 if (avpkt->size && bytestream2_tell(&gb) == 0) {
1599 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1600 return AVERROR_INVALIDDATA;
1605 if (avpkt->size < bytestream2_tell(&gb)) {
1606 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1610 return bytestream2_tell(&gb);
1613 static void adpcm_flush(AVCodecContext *avctx)
1615 ADPCMDecodeContext *c = avctx->priv_data;
1620 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
1621 AV_SAMPLE_FMT_NONE };
1622 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1623 AV_SAMPLE_FMT_NONE };
1624 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1626 AV_SAMPLE_FMT_NONE };
1628 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1629 AVCodec ff_ ## name_ ## _decoder = { \
1631 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1632 .type = AVMEDIA_TYPE_AUDIO, \
1634 .priv_data_size = sizeof(ADPCMDecodeContext), \
1635 .init = adpcm_decode_init, \
1636 .decode = adpcm_decode_frame, \
1637 .flush = adpcm_flush, \
1638 .capabilities = AV_CODEC_CAP_DR1, \
1639 .sample_fmts = sample_fmts_, \
1642 /* Note: Do not forget to add new entries to the Makefile as well. */
1643 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1644 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1645 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1646 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1647 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1648 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1649 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1650 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1651 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1652 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1653 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1654 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1655 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1656 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1657 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1658 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1659 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1660 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1661 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1662 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1663 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1664 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1665 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1666 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1667 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
1668 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1669 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1670 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1671 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1672 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1673 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1674 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1675 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");