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)
15 * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
16 * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
17 * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
18 * High Voltage Software ALP decoder by Zane van Iperen (zane@zanevaniperen.com)
19 * Cunning Developments decoder by Zane van Iperen (zane@zanevaniperen.com)
21 * This file is part of FFmpeg.
23 * FFmpeg is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU Lesser General Public
25 * License as published by the Free Software Foundation; either
26 * version 2.1 of the License, or (at your option) any later version.
28 * FFmpeg is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * Lesser General Public License for more details.
33 * You should have received a copy of the GNU Lesser General Public
34 * License along with FFmpeg; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
39 #include "bytestream.h"
41 #include "adpcm_data.h"
47 * Features and limitations:
49 * Reference documents:
50 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
51 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
52 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
53 * http://openquicktime.sourceforge.net/
54 * XAnim sources (xa_codec.c) http://xanim.polter.net/
55 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
56 * SoX source code http://sox.sourceforge.net/
59 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
60 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
61 * readstr http://www.geocities.co.jp/Playtown/2004/
64 /* These are for CD-ROM XA ADPCM */
65 static const int8_t xa_adpcm_table[5][2] = {
73 static const int16_t ea_adpcm_table[] = {
81 // padded to zero where table size is less then 16
82 static const int8_t swf_index_tables[4][16] = {
84 /*3*/ { -1, -1, 2, 4 },
85 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
86 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
89 static const int8_t zork_index_table[8] = {
90 -1, -1, -1, 1, 4, 7, 10, 12,
93 static const int8_t mtf_index_table[16] = {
94 8, 6, 4, 2, -1, -1, -1, -1,
95 -1, -1, -1, -1, 2, 4, 6, 8,
100 typedef struct ADPCMDecodeContext {
101 ADPCMChannelStatus status[14];
102 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
103 int has_status; /**< Status flag. Reset to 0 after a flush. */
104 } ADPCMDecodeContext;
106 static void adpcm_flush(AVCodecContext *avctx);
108 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
110 ADPCMDecodeContext *c = avctx->priv_data;
111 unsigned int min_channels = 1;
112 unsigned int max_channels = 2;
116 switch(avctx->codec->id) {
117 case AV_CODEC_ID_ADPCM_IMA_AMV:
120 case AV_CODEC_ID_ADPCM_DTK:
121 case AV_CODEC_ID_ADPCM_EA:
124 case AV_CODEC_ID_ADPCM_AFC:
125 case AV_CODEC_ID_ADPCM_EA_R1:
126 case AV_CODEC_ID_ADPCM_EA_R2:
127 case AV_CODEC_ID_ADPCM_EA_R3:
128 case AV_CODEC_ID_ADPCM_EA_XAS:
129 case AV_CODEC_ID_ADPCM_MS:
132 case AV_CODEC_ID_ADPCM_MTAF:
135 if (avctx->channels & 1) {
136 avpriv_request_sample(avctx, "channel count %d", avctx->channels);
137 return AVERROR_PATCHWELCOME;
140 case AV_CODEC_ID_ADPCM_PSX:
142 if (avctx->channels <= 0 || avctx->block_align % (16 * avctx->channels))
143 return AVERROR_INVALIDDATA;
145 case AV_CODEC_ID_ADPCM_IMA_DAT4:
146 case AV_CODEC_ID_ADPCM_THP:
147 case AV_CODEC_ID_ADPCM_THP_LE:
151 if (avctx->channels < min_channels || avctx->channels > max_channels) {
152 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
153 return AVERROR(EINVAL);
156 switch(avctx->codec->id) {
157 case AV_CODEC_ID_ADPCM_IMA_WAV:
158 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
159 return AVERROR_INVALIDDATA;
161 case AV_CODEC_ID_ADPCM_ARGO:
162 if (avctx->bits_per_coded_sample != 4 || avctx->block_align != 17 * avctx->channels)
163 return AVERROR_INVALIDDATA;
165 case AV_CODEC_ID_ADPCM_ZORK:
166 if (avctx->bits_per_coded_sample != 8)
167 return AVERROR_INVALIDDATA;
173 switch (avctx->codec->id) {
174 case AV_CODEC_ID_ADPCM_AICA:
175 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
176 case AV_CODEC_ID_ADPCM_IMA_DAT4:
177 case AV_CODEC_ID_ADPCM_IMA_QT:
178 case AV_CODEC_ID_ADPCM_IMA_WAV:
179 case AV_CODEC_ID_ADPCM_4XM:
180 case AV_CODEC_ID_ADPCM_XA:
181 case AV_CODEC_ID_ADPCM_EA_R1:
182 case AV_CODEC_ID_ADPCM_EA_R2:
183 case AV_CODEC_ID_ADPCM_EA_R3:
184 case AV_CODEC_ID_ADPCM_EA_XAS:
185 case AV_CODEC_ID_ADPCM_THP:
186 case AV_CODEC_ID_ADPCM_THP_LE:
187 case AV_CODEC_ID_ADPCM_AFC:
188 case AV_CODEC_ID_ADPCM_DTK:
189 case AV_CODEC_ID_ADPCM_PSX:
190 case AV_CODEC_ID_ADPCM_MTAF:
191 case AV_CODEC_ID_ADPCM_ARGO:
192 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
193 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
195 case AV_CODEC_ID_ADPCM_IMA_WS:
196 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
199 case AV_CODEC_ID_ADPCM_MS:
200 avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
204 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
209 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
211 int delta, pred, step, add;
216 add = (delta * 2 + 1) * step;
220 if ((nibble & 8) == 0)
221 pred = av_clip(pred + (add >> 3), -32767, 32767);
223 pred = av_clip(pred - (add >> 3), -32767, 32767);
230 c->step = av_clip(c->step * 2, 127, 24576);
248 c->step = av_clip(c->step, 127, 24576);
253 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
257 int sign, delta, diff, step;
259 step = ff_adpcm_step_table[c->step_index];
260 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
261 step_index = av_clip(step_index, 0, 88);
265 /* perform direct multiplication instead of series of jumps proposed by
266 * the reference ADPCM implementation since modern CPUs can do the mults
268 diff = ((2 * delta + 1) * step) >> shift;
269 predictor = c->predictor;
270 if (sign) predictor -= diff;
271 else predictor += diff;
273 c->predictor = av_clip_int16(predictor);
274 c->step_index = step_index;
276 return (int16_t)c->predictor;
279 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
283 int sign, delta, diff, step;
285 step = ff_adpcm_step_table[c->step_index];
286 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
287 step_index = av_clip(step_index, 0, 88);
291 diff = (delta * step) >> shift;
292 predictor = c->predictor;
293 if (sign) predictor -= diff;
294 else predictor += diff;
296 c->predictor = av_clip_int16(predictor);
297 c->step_index = step_index;
299 return (int16_t)c->predictor;
302 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
304 int step_index, step, delta, predictor;
306 step = ff_adpcm_step_table[c->step_index];
308 delta = step * (2 * nibble - 15);
309 predictor = c->predictor + delta;
311 step_index = c->step_index + mtf_index_table[(unsigned)nibble];
312 c->predictor = av_clip_int16(predictor >> 4);
313 c->step_index = av_clip(step_index, 0, 88);
315 return (int16_t)c->predictor;
318 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
324 nibble = sign_extend(nibble & 0xF, 4);
326 step = ff_adpcm_ima_cunning_step_table[c->step_index];
327 step_index = c->step_index + ff_adpcm_ima_cunning_index_table[abs(nibble)];
328 step_index = av_clip(step_index, 0, 60);
330 predictor = c->predictor + step * nibble;
332 c->predictor = av_clip_int16(predictor);
333 c->step_index = step_index;
338 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
340 int nibble, step_index, predictor, sign, delta, diff, step, shift;
343 nibble = get_bits_le(gb, bps),
344 step = ff_adpcm_step_table[c->step_index];
345 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
346 step_index = av_clip(step_index, 0, 88);
348 sign = nibble & (1 << shift);
349 delta = av_mod_uintp2(nibble, shift);
350 diff = ((2 * delta + 1) * step) >> shift;
351 predictor = c->predictor;
352 if (sign) predictor -= diff;
353 else predictor += diff;
355 c->predictor = av_clip_int16(predictor);
356 c->step_index = step_index;
358 return (int16_t)c->predictor;
361 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
367 step = ff_adpcm_step_table[c->step_index];
368 step_index = c->step_index + ff_adpcm_index_table[nibble];
369 step_index = av_clip(step_index, 0, 88);
372 if (nibble & 4) diff += step;
373 if (nibble & 2) diff += step >> 1;
374 if (nibble & 1) diff += step >> 2;
377 predictor = c->predictor - diff;
379 predictor = c->predictor + diff;
381 c->predictor = av_clip_int16(predictor);
382 c->step_index = step_index;
387 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
391 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
392 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
394 c->sample2 = c->sample1;
395 c->sample1 = av_clip_int16(predictor);
396 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
397 if (c->idelta < 16) c->idelta = 16;
398 if (c->idelta > INT_MAX/768) {
399 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
400 c->idelta = INT_MAX/768;
406 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
408 int step_index, predictor, sign, delta, diff, step;
410 step = ff_adpcm_oki_step_table[c->step_index];
411 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
412 step_index = av_clip(step_index, 0, 48);
416 diff = ((2 * delta + 1) * step) >> 3;
417 predictor = c->predictor;
418 if (sign) predictor -= diff;
419 else predictor += diff;
421 c->predictor = av_clip_intp2(predictor, 11);
422 c->step_index = step_index;
424 return c->predictor * 16;
427 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
429 int sign, delta, diff;
434 /* perform direct multiplication instead of series of jumps proposed by
435 * the reference ADPCM implementation since modern CPUs can do the mults
437 diff = ((2 * delta + 1) * c->step) >> 3;
438 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
439 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
440 c->predictor = av_clip_int16(c->predictor);
441 /* calculate new step and clamp it to range 511..32767 */
442 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
443 c->step = av_clip(new_step, 511, 32767);
445 return (int16_t)c->predictor;
448 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
450 int sign, delta, diff;
452 sign = nibble & (1<<(size-1));
453 delta = nibble & ((1<<(size-1))-1);
454 diff = delta << (7 + c->step + shift);
457 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
459 /* calculate new step */
460 if (delta >= (2*size - 3) && c->step < 3)
462 else if (delta == 0 && c->step > 0)
465 return (int16_t) c->predictor;
468 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
475 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
476 c->predictor = av_clip_int16(c->predictor);
477 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
478 c->step = av_clip(c->step, 127, 24576);
482 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
484 c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
485 c->predictor = av_clip_int16(c->predictor);
486 c->step += ff_adpcm_index_table[nibble];
487 c->step = av_clip_uintp2(c->step, 5);
491 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
493 int16_t index = c->step_index;
494 uint32_t lookup_sample = ff_adpcm_step_table[index];
498 sample += lookup_sample;
500 sample += lookup_sample >> 1;
502 sample += lookup_sample >> 2;
504 sample += lookup_sample >> 3;
506 sample += lookup_sample >> 4;
508 sample += lookup_sample >> 5;
510 sample += lookup_sample >> 6;
514 sample += c->predictor;
515 sample = av_clip_int16(sample);
517 index += zork_index_table[(nibble >> 4) & 7];
518 index = av_clip(index, 0, 88);
520 c->predictor = sample;
521 c->step_index = index;
526 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
527 const uint8_t *in, ADPCMChannelStatus *left,
528 ADPCMChannelStatus *right, int channels, int sample_offset)
531 int shift,filter,f0,f1;
535 out0 += sample_offset;
539 out1 += sample_offset;
542 shift = 12 - (in[4+i*2] & 15);
543 filter = in[4+i*2] >> 4;
544 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
545 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
549 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
552 f0 = xa_adpcm_table[filter][0];
553 f1 = xa_adpcm_table[filter][1];
561 t = sign_extend(d, 4);
562 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
564 s_1 = av_clip_int16(s);
571 s_1 = right->sample1;
572 s_2 = right->sample2;
575 shift = 12 - (in[5+i*2] & 15);
576 filter = in[5+i*2] >> 4;
577 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
578 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
582 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
586 f0 = xa_adpcm_table[filter][0];
587 f1 = xa_adpcm_table[filter][1];
592 t = sign_extend(d >> 4, 4);
593 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
595 s_1 = av_clip_int16(s);
600 right->sample1 = s_1;
601 right->sample2 = s_2;
607 out0 += 28 * (3 - channels);
608 out1 += 28 * (3 - channels);
614 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
616 ADPCMDecodeContext *c = avctx->priv_data;
619 int k0, signmask, nb_bits, count;
620 int size = buf_size*8;
623 init_get_bits(&gb, buf, size);
625 //read bits & initial values
626 nb_bits = get_bits(&gb, 2)+2;
627 table = swf_index_tables[nb_bits-2];
628 k0 = 1 << (nb_bits-2);
629 signmask = 1 << (nb_bits-1);
631 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
632 for (i = 0; i < avctx->channels; i++) {
633 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
634 c->status[i].step_index = get_bits(&gb, 6);
637 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
640 for (i = 0; i < avctx->channels; i++) {
641 // similar to IMA adpcm
642 int delta = get_bits(&gb, nb_bits);
643 int step = ff_adpcm_step_table[c->status[i].step_index];
644 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
655 if (delta & signmask)
656 c->status[i].predictor -= vpdiff;
658 c->status[i].predictor += vpdiff;
660 c->status[i].step_index += table[delta & (~signmask)];
662 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
663 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
665 *samples++ = c->status[i].predictor;
671 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
673 int sample = sign_extend(nibble, 4) * (1 << shift);
676 sample += (8 * cs->sample1) - (4 * cs->sample2);
678 sample += 4 * cs->sample1;
680 sample = av_clip_int16(sample >> 2);
682 cs->sample2 = cs->sample1;
683 cs->sample1 = sample;
689 * Get the number of samples (per channel) that will be decoded from the packet.
690 * In one case, this is actually the maximum number of samples possible to
691 * decode with the given buf_size.
693 * @param[out] coded_samples set to the number of samples as coded in the
694 * packet, or 0 if the codec does not encode the
695 * number of samples in each frame.
696 * @param[out] approx_nb_samples set to non-zero if the number of samples
697 * returned is an approximation.
699 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
700 int buf_size, int *coded_samples, int *approx_nb_samples)
702 ADPCMDecodeContext *s = avctx->priv_data;
704 int ch = avctx->channels;
705 int has_coded_samples = 0;
709 *approx_nb_samples = 0;
714 switch (avctx->codec->id) {
715 /* constant, only check buf_size */
716 case AV_CODEC_ID_ADPCM_EA_XAS:
717 if (buf_size < 76 * ch)
721 case AV_CODEC_ID_ADPCM_IMA_QT:
722 if (buf_size < 34 * ch)
726 /* simple 4-bit adpcm */
727 case AV_CODEC_ID_ADPCM_CT:
728 case AV_CODEC_ID_ADPCM_IMA_APC:
729 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
730 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
731 case AV_CODEC_ID_ADPCM_IMA_OKI:
732 case AV_CODEC_ID_ADPCM_IMA_WS:
733 case AV_CODEC_ID_ADPCM_YAMAHA:
734 case AV_CODEC_ID_ADPCM_AICA:
735 case AV_CODEC_ID_ADPCM_IMA_SSI:
736 case AV_CODEC_ID_ADPCM_IMA_APM:
737 case AV_CODEC_ID_ADPCM_IMA_ALP:
738 case AV_CODEC_ID_ADPCM_IMA_MTF:
739 nb_samples = buf_size * 2 / ch;
745 /* simple 4-bit adpcm, with header */
747 switch (avctx->codec->id) {
748 case AV_CODEC_ID_ADPCM_4XM:
749 case AV_CODEC_ID_ADPCM_AGM:
750 case AV_CODEC_ID_ADPCM_IMA_DAT4:
751 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
752 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
753 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
756 return (buf_size - header_size) * 2 / ch;
758 /* more complex formats */
759 switch (avctx->codec->id) {
760 case AV_CODEC_ID_ADPCM_IMA_AMV:
761 bytestream2_skip(gb, 4);
762 has_coded_samples = 1;
763 *coded_samples = bytestream2_get_le32u(gb);
764 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
765 bytestream2_seek(gb, -8, SEEK_CUR);
767 case AV_CODEC_ID_ADPCM_EA:
768 has_coded_samples = 1;
769 *coded_samples = bytestream2_get_le32(gb);
770 *coded_samples -= *coded_samples % 28;
771 nb_samples = (buf_size - 12) / 30 * 28;
773 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
774 has_coded_samples = 1;
775 *coded_samples = bytestream2_get_le32(gb);
776 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
778 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
779 nb_samples = (buf_size - ch) / ch * 2;
781 case AV_CODEC_ID_ADPCM_EA_R1:
782 case AV_CODEC_ID_ADPCM_EA_R2:
783 case AV_CODEC_ID_ADPCM_EA_R3:
784 /* maximum number of samples */
785 /* has internal offsets and a per-frame switch to signal raw 16-bit */
786 has_coded_samples = 1;
787 switch (avctx->codec->id) {
788 case AV_CODEC_ID_ADPCM_EA_R1:
789 header_size = 4 + 9 * ch;
790 *coded_samples = bytestream2_get_le32(gb);
792 case AV_CODEC_ID_ADPCM_EA_R2:
793 header_size = 4 + 5 * ch;
794 *coded_samples = bytestream2_get_le32(gb);
796 case AV_CODEC_ID_ADPCM_EA_R3:
797 header_size = 4 + 5 * ch;
798 *coded_samples = bytestream2_get_be32(gb);
801 *coded_samples -= *coded_samples % 28;
802 nb_samples = (buf_size - header_size) * 2 / ch;
803 nb_samples -= nb_samples % 28;
804 *approx_nb_samples = 1;
806 case AV_CODEC_ID_ADPCM_IMA_DK3:
807 if (avctx->block_align > 0)
808 buf_size = FFMIN(buf_size, avctx->block_align);
809 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
811 case AV_CODEC_ID_ADPCM_IMA_DK4:
812 if (avctx->block_align > 0)
813 buf_size = FFMIN(buf_size, avctx->block_align);
814 if (buf_size < 4 * ch)
815 return AVERROR_INVALIDDATA;
816 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
818 case AV_CODEC_ID_ADPCM_IMA_RAD:
819 if (avctx->block_align > 0)
820 buf_size = FFMIN(buf_size, avctx->block_align);
821 nb_samples = (buf_size - 4 * ch) * 2 / ch;
823 case AV_CODEC_ID_ADPCM_IMA_WAV:
825 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
826 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
827 if (avctx->block_align > 0)
828 buf_size = FFMIN(buf_size, avctx->block_align);
829 if (buf_size < 4 * ch)
830 return AVERROR_INVALIDDATA;
831 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
834 case AV_CODEC_ID_ADPCM_MS:
835 if (avctx->block_align > 0)
836 buf_size = FFMIN(buf_size, avctx->block_align);
837 nb_samples = (buf_size - 6 * ch) * 2 / ch;
839 case AV_CODEC_ID_ADPCM_MTAF:
840 if (avctx->block_align > 0)
841 buf_size = FFMIN(buf_size, avctx->block_align);
842 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
844 case AV_CODEC_ID_ADPCM_SBPRO_2:
845 case AV_CODEC_ID_ADPCM_SBPRO_3:
846 case AV_CODEC_ID_ADPCM_SBPRO_4:
848 int samples_per_byte;
849 switch (avctx->codec->id) {
850 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
851 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
852 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
854 if (!s->status[0].step_index) {
856 return AVERROR_INVALIDDATA;
860 nb_samples += buf_size * samples_per_byte / ch;
863 case AV_CODEC_ID_ADPCM_SWF:
865 int buf_bits = buf_size * 8 - 2;
866 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
867 int block_hdr_size = 22 * ch;
868 int block_size = block_hdr_size + nbits * ch * 4095;
869 int nblocks = buf_bits / block_size;
870 int bits_left = buf_bits - nblocks * block_size;
871 nb_samples = nblocks * 4096;
872 if (bits_left >= block_hdr_size)
873 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
876 case AV_CODEC_ID_ADPCM_THP:
877 case AV_CODEC_ID_ADPCM_THP_LE:
878 if (avctx->extradata) {
879 nb_samples = buf_size * 14 / (8 * ch);
882 has_coded_samples = 1;
883 bytestream2_skip(gb, 4); // channel size
884 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
885 bytestream2_get_le32(gb) :
886 bytestream2_get_be32(gb);
887 buf_size -= 8 + 36 * ch;
889 nb_samples = buf_size / 8 * 14;
890 if (buf_size % 8 > 1)
891 nb_samples += (buf_size % 8 - 1) * 2;
892 *approx_nb_samples = 1;
894 case AV_CODEC_ID_ADPCM_AFC:
895 nb_samples = buf_size / (9 * ch) * 16;
897 case AV_CODEC_ID_ADPCM_XA:
898 nb_samples = (buf_size / 128) * 224 / ch;
900 case AV_CODEC_ID_ADPCM_DTK:
901 case AV_CODEC_ID_ADPCM_PSX:
902 nb_samples = buf_size / (16 * ch) * 28;
904 case AV_CODEC_ID_ADPCM_ARGO:
905 nb_samples = buf_size / avctx->block_align * 32;
907 case AV_CODEC_ID_ADPCM_ZORK:
908 nb_samples = buf_size / ch;
912 /* validate coded sample count */
913 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
914 return AVERROR_INVALIDDATA;
919 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
920 int *got_frame_ptr, AVPacket *avpkt)
922 AVFrame *frame = data;
923 const uint8_t *buf = avpkt->data;
924 int buf_size = avpkt->size;
925 ADPCMDecodeContext *c = avctx->priv_data;
926 ADPCMChannelStatus *cs;
927 int n, m, channel, i;
932 int nb_samples, coded_samples, approx_nb_samples, ret;
935 bytestream2_init(&gb, buf, buf_size);
936 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
937 if (nb_samples <= 0) {
938 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
939 return AVERROR_INVALIDDATA;
942 /* get output buffer */
943 frame->nb_samples = nb_samples;
944 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
946 samples = (int16_t *)frame->data[0];
947 samples_p = (int16_t **)frame->extended_data;
949 /* use coded_samples when applicable */
950 /* it is always <= nb_samples, so the output buffer will be large enough */
952 if (!approx_nb_samples && coded_samples != nb_samples)
953 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
954 frame->nb_samples = nb_samples = coded_samples;
957 st = avctx->channels == 2 ? 1 : 0;
959 switch(avctx->codec->id) {
960 case AV_CODEC_ID_ADPCM_IMA_QT:
961 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
962 Channel data is interleaved per-chunk. */
963 for (channel = 0; channel < avctx->channels; channel++) {
966 cs = &(c->status[channel]);
967 /* (pppppp) (piiiiiii) */
969 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
970 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
971 step_index = predictor & 0x7F;
974 if (cs->step_index == step_index) {
975 int diff = predictor - cs->predictor;
982 cs->step_index = step_index;
983 cs->predictor = predictor;
986 if (cs->step_index > 88u){
987 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
988 channel, cs->step_index);
989 return AVERROR_INVALIDDATA;
992 samples = samples_p[channel];
994 for (m = 0; m < 64; m += 2) {
995 int byte = bytestream2_get_byteu(&gb);
996 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
997 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1001 case AV_CODEC_ID_ADPCM_IMA_WAV:
1002 for(i=0; i<avctx->channels; i++){
1003 cs = &(c->status[i]);
1004 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1006 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1007 if (cs->step_index > 88u){
1008 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1010 return AVERROR_INVALIDDATA;
1014 if (avctx->bits_per_coded_sample != 4) {
1015 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1016 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1017 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1020 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1021 for (i = 0; i < avctx->channels; i++) {
1025 samples = &samples_p[i][1 + n * samples_per_block];
1026 for (j = 0; j < block_size; j++) {
1027 temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
1028 (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
1030 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1033 for (m = 0; m < samples_per_block; m++) {
1034 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1035 avctx->bits_per_coded_sample);
1039 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1041 for (n = 0; n < (nb_samples - 1) / 8; n++) {
1042 for (i = 0; i < avctx->channels; i++) {
1044 samples = &samples_p[i][1 + n * 8];
1045 for (m = 0; m < 8; m += 2) {
1046 int v = bytestream2_get_byteu(&gb);
1047 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1048 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1054 case AV_CODEC_ID_ADPCM_4XM:
1055 for (i = 0; i < avctx->channels; i++)
1056 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1058 for (i = 0; i < avctx->channels; i++) {
1059 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1060 if (c->status[i].step_index > 88u) {
1061 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1062 i, c->status[i].step_index);
1063 return AVERROR_INVALIDDATA;
1067 for (i = 0; i < avctx->channels; i++) {
1068 samples = (int16_t *)frame->data[i];
1070 for (n = nb_samples >> 1; n > 0; n--) {
1071 int v = bytestream2_get_byteu(&gb);
1072 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1073 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1077 case AV_CODEC_ID_ADPCM_AGM:
1078 for (i = 0; i < avctx->channels; i++)
1079 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1080 for (i = 0; i < avctx->channels; i++)
1081 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1083 for (n = 0; n < nb_samples >> (1 - st); n++) {
1084 int v = bytestream2_get_byteu(&gb);
1085 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1086 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1089 case AV_CODEC_ID_ADPCM_MS:
1091 int block_predictor;
1093 if (avctx->channels > 2) {
1094 for (channel = 0; channel < avctx->channels; channel++) {
1095 samples = samples_p[channel];
1096 block_predictor = bytestream2_get_byteu(&gb);
1097 if (block_predictor > 6) {
1098 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1099 channel, block_predictor);
1100 return AVERROR_INVALIDDATA;
1102 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1103 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1104 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1105 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1106 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1107 *samples++ = c->status[channel].sample2;
1108 *samples++ = c->status[channel].sample1;
1109 for(n = (nb_samples - 2) >> 1; n > 0; n--) {
1110 int byte = bytestream2_get_byteu(&gb);
1111 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1112 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1116 block_predictor = bytestream2_get_byteu(&gb);
1117 if (block_predictor > 6) {
1118 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1120 return AVERROR_INVALIDDATA;
1122 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1123 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1125 block_predictor = bytestream2_get_byteu(&gb);
1126 if (block_predictor > 6) {
1127 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1129 return AVERROR_INVALIDDATA;
1131 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1132 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1134 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1136 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1139 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1140 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1141 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1142 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1144 *samples++ = c->status[0].sample2;
1145 if (st) *samples++ = c->status[1].sample2;
1146 *samples++ = c->status[0].sample1;
1147 if (st) *samples++ = c->status[1].sample1;
1148 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1149 int byte = bytestream2_get_byteu(&gb);
1150 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1151 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1156 case AV_CODEC_ID_ADPCM_MTAF:
1157 for (channel = 0; channel < avctx->channels; channel+=2) {
1158 bytestream2_skipu(&gb, 4);
1159 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1160 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1161 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1162 bytestream2_skipu(&gb, 2);
1163 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1164 bytestream2_skipu(&gb, 2);
1165 for (n = 0; n < nb_samples; n+=2) {
1166 int v = bytestream2_get_byteu(&gb);
1167 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1168 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1170 for (n = 0; n < nb_samples; n+=2) {
1171 int v = bytestream2_get_byteu(&gb);
1172 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1173 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1177 case AV_CODEC_ID_ADPCM_IMA_DK4:
1178 for (channel = 0; channel < avctx->channels; channel++) {
1179 cs = &c->status[channel];
1180 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1181 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1182 if (cs->step_index > 88u){
1183 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1184 channel, cs->step_index);
1185 return AVERROR_INVALIDDATA;
1188 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1189 int v = bytestream2_get_byteu(&gb);
1190 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1191 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1194 case AV_CODEC_ID_ADPCM_IMA_DK3:
1198 int decode_top_nibble_next = 0;
1200 const int16_t *samples_end = samples + avctx->channels * nb_samples;
1202 bytestream2_skipu(&gb, 10);
1203 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1204 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1205 c->status[0].step_index = bytestream2_get_byteu(&gb);
1206 c->status[1].step_index = bytestream2_get_byteu(&gb);
1207 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1208 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1209 c->status[0].step_index, c->status[1].step_index);
1210 return AVERROR_INVALIDDATA;
1212 /* sign extend the predictors */
1213 diff_channel = c->status[1].predictor;
1215 /* DK3 ADPCM support macro */
1216 #define DK3_GET_NEXT_NIBBLE() \
1217 if (decode_top_nibble_next) { \
1218 nibble = last_byte >> 4; \
1219 decode_top_nibble_next = 0; \
1221 last_byte = bytestream2_get_byteu(&gb); \
1222 nibble = last_byte & 0x0F; \
1223 decode_top_nibble_next = 1; \
1226 while (samples < samples_end) {
1228 /* for this algorithm, c->status[0] is the sum channel and
1229 * c->status[1] is the diff channel */
1231 /* process the first predictor of the sum channel */
1232 DK3_GET_NEXT_NIBBLE();
1233 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1235 /* process the diff channel predictor */
1236 DK3_GET_NEXT_NIBBLE();
1237 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1239 /* process the first pair of stereo PCM samples */
1240 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1241 *samples++ = c->status[0].predictor + c->status[1].predictor;
1242 *samples++ = c->status[0].predictor - c->status[1].predictor;
1244 /* process the second predictor of the sum channel */
1245 DK3_GET_NEXT_NIBBLE();
1246 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1248 /* process the second pair of stereo PCM samples */
1249 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1250 *samples++ = c->status[0].predictor + c->status[1].predictor;
1251 *samples++ = c->status[0].predictor - c->status[1].predictor;
1254 if ((bytestream2_tell(&gb) & 1))
1255 bytestream2_skip(&gb, 1);
1258 case AV_CODEC_ID_ADPCM_IMA_ISS:
1259 for (channel = 0; channel < avctx->channels; channel++) {
1260 cs = &c->status[channel];
1261 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1262 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1263 if (cs->step_index > 88u){
1264 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1265 channel, cs->step_index);
1266 return AVERROR_INVALIDDATA;
1270 for (n = nb_samples >> (1 - st); n > 0; n--) {
1272 int v = bytestream2_get_byteu(&gb);
1273 /* nibbles are swapped for mono */
1281 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1282 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1285 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
1286 for (channel = 0; channel < avctx->channels; channel++) {
1287 cs = &c->status[channel];
1288 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1289 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1290 if (cs->step_index > 88u){
1291 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1292 channel, cs->step_index);
1293 return AVERROR_INVALIDDATA;
1297 for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1298 for (channel = 0; channel < avctx->channels; channel++) {
1299 samples = samples_p[channel] + 256 * subframe;
1300 for (n = 0; n < 256; n += 2) {
1301 int v = bytestream2_get_byteu(&gb);
1302 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1303 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1308 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1309 for (channel = 0; channel < avctx->channels; channel++) {
1310 cs = &c->status[channel];
1311 samples = samples_p[channel];
1312 bytestream2_skip(&gb, 4);
1313 for (n = 0; n < nb_samples; n += 2) {
1314 int v = bytestream2_get_byteu(&gb);
1315 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1316 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1320 case AV_CODEC_ID_ADPCM_IMA_APC:
1321 for (n = nb_samples >> (1 - st); n > 0; n--) {
1322 int v = bytestream2_get_byteu(&gb);
1323 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1324 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1327 case AV_CODEC_ID_ADPCM_IMA_SSI:
1328 for (n = nb_samples >> (1 - st); n > 0; n--) {
1329 int v = bytestream2_get_byteu(&gb);
1330 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1331 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1334 case AV_CODEC_ID_ADPCM_IMA_APM:
1335 for (n = nb_samples / 2; n > 0; n--) {
1336 for (channel = 0; channel < avctx->channels; channel++) {
1337 int v = bytestream2_get_byteu(&gb);
1338 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1339 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1341 samples += avctx->channels;
1344 case AV_CODEC_ID_ADPCM_IMA_ALP:
1345 for (n = nb_samples / 2; n > 0; n--) {
1346 for (channel = 0; channel < avctx->channels; channel++) {
1347 int v = bytestream2_get_byteu(&gb);
1348 *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1349 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1351 samples += avctx->channels;
1354 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1355 for (channel = 0; channel < avctx->channels; channel++) {
1356 int16_t *smp = samples_p[channel];
1357 for (n = 0; n < nb_samples / 2; n++) {
1358 int v = bytestream2_get_byteu(&gb);
1359 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1360 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1364 case AV_CODEC_ID_ADPCM_IMA_OKI:
1365 for (n = nb_samples >> (1 - st); n > 0; n--) {
1366 int v = bytestream2_get_byteu(&gb);
1367 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1368 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1371 case AV_CODEC_ID_ADPCM_IMA_RAD:
1372 for (channel = 0; channel < avctx->channels; channel++) {
1373 cs = &c->status[channel];
1374 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1375 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1376 if (cs->step_index > 88u){
1377 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1378 channel, cs->step_index);
1379 return AVERROR_INVALIDDATA;
1382 for (n = 0; n < nb_samples / 2; n++) {
1385 byte[0] = bytestream2_get_byteu(&gb);
1387 byte[1] = bytestream2_get_byteu(&gb);
1388 for(channel = 0; channel < avctx->channels; channel++) {
1389 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1391 for(channel = 0; channel < avctx->channels; channel++) {
1392 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1396 case AV_CODEC_ID_ADPCM_IMA_WS:
1397 if (c->vqa_version == 3) {
1398 for (channel = 0; channel < avctx->channels; channel++) {
1399 int16_t *smp = samples_p[channel];
1401 for (n = nb_samples / 2; n > 0; n--) {
1402 int v = bytestream2_get_byteu(&gb);
1403 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1404 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1408 for (n = nb_samples / 2; n > 0; n--) {
1409 for (channel = 0; channel < avctx->channels; channel++) {
1410 int v = bytestream2_get_byteu(&gb);
1411 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1412 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1414 samples += avctx->channels;
1417 bytestream2_seek(&gb, 0, SEEK_END);
1419 case AV_CODEC_ID_ADPCM_XA:
1421 int16_t *out0 = samples_p[0];
1422 int16_t *out1 = samples_p[1];
1423 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1424 int sample_offset = 0;
1425 int bytes_remaining;
1426 while (bytestream2_get_bytes_left(&gb) >= 128) {
1427 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1428 &c->status[0], &c->status[1],
1429 avctx->channels, sample_offset)) < 0)
1431 bytestream2_skipu(&gb, 128);
1432 sample_offset += samples_per_block;
1434 /* Less than a full block of data left, e.g. when reading from
1435 * 2324 byte per sector XA; the remainder is padding */
1436 bytes_remaining = bytestream2_get_bytes_left(&gb);
1437 if (bytes_remaining > 0) {
1438 bytestream2_skip(&gb, bytes_remaining);
1442 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1443 for (i=0; i<=st; i++) {
1444 c->status[i].step_index = bytestream2_get_le32u(&gb);
1445 if (c->status[i].step_index > 88u) {
1446 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1447 i, c->status[i].step_index);
1448 return AVERROR_INVALIDDATA;
1451 for (i=0; i<=st; i++) {
1452 c->status[i].predictor = bytestream2_get_le32u(&gb);
1453 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1454 return AVERROR_INVALIDDATA;
1457 for (n = nb_samples >> (1 - st); n > 0; n--) {
1458 int byte = bytestream2_get_byteu(&gb);
1459 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1460 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1463 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1464 for (n = nb_samples >> (1 - st); n > 0; n--) {
1465 int byte = bytestream2_get_byteu(&gb);
1466 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1467 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1470 case AV_CODEC_ID_ADPCM_EA:
1472 int previous_left_sample, previous_right_sample;
1473 int current_left_sample, current_right_sample;
1474 int next_left_sample, next_right_sample;
1475 int coeff1l, coeff2l, coeff1r, coeff2r;
1476 int shift_left, shift_right;
1478 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1479 each coding 28 stereo samples. */
1481 if(avctx->channels != 2)
1482 return AVERROR_INVALIDDATA;
1484 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1485 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1486 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1487 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1489 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1490 int byte = bytestream2_get_byteu(&gb);
1491 coeff1l = ea_adpcm_table[ byte >> 4 ];
1492 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1493 coeff1r = ea_adpcm_table[ byte & 0x0F];
1494 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1496 byte = bytestream2_get_byteu(&gb);
1497 shift_left = 20 - (byte >> 4);
1498 shift_right = 20 - (byte & 0x0F);
1500 for (count2 = 0; count2 < 28; count2++) {
1501 byte = bytestream2_get_byteu(&gb);
1502 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1503 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1505 next_left_sample = (next_left_sample +
1506 (current_left_sample * coeff1l) +
1507 (previous_left_sample * coeff2l) + 0x80) >> 8;
1508 next_right_sample = (next_right_sample +
1509 (current_right_sample * coeff1r) +
1510 (previous_right_sample * coeff2r) + 0x80) >> 8;
1512 previous_left_sample = current_left_sample;
1513 current_left_sample = av_clip_int16(next_left_sample);
1514 previous_right_sample = current_right_sample;
1515 current_right_sample = av_clip_int16(next_right_sample);
1516 *samples++ = current_left_sample;
1517 *samples++ = current_right_sample;
1521 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1525 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1527 int coeff[2][2], shift[2];
1529 for(channel = 0; channel < avctx->channels; channel++) {
1530 int byte = bytestream2_get_byteu(&gb);
1532 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1533 shift[channel] = 20 - (byte & 0x0F);
1535 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1538 byte[0] = bytestream2_get_byteu(&gb);
1539 if (st) byte[1] = bytestream2_get_byteu(&gb);
1540 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1541 for(channel = 0; channel < avctx->channels; channel++) {
1542 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1544 c->status[channel].sample1 * coeff[channel][0] +
1545 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1546 c->status[channel].sample2 = c->status[channel].sample1;
1547 c->status[channel].sample1 = av_clip_int16(sample);
1548 *samples++ = c->status[channel].sample1;
1552 bytestream2_seek(&gb, 0, SEEK_END);
1555 case AV_CODEC_ID_ADPCM_EA_R1:
1556 case AV_CODEC_ID_ADPCM_EA_R2:
1557 case AV_CODEC_ID_ADPCM_EA_R3: {
1558 /* channel numbering
1560 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1561 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1562 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1563 int previous_sample, current_sample, next_sample;
1566 unsigned int channel;
1571 for (channel=0; channel<avctx->channels; channel++)
1572 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1573 bytestream2_get_le32(&gb)) +
1574 (avctx->channels + 1) * 4;
1576 for (channel=0; channel<avctx->channels; channel++) {
1577 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1578 samplesC = samples_p[channel];
1580 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1581 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1582 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1584 current_sample = c->status[channel].predictor;
1585 previous_sample = c->status[channel].prev_sample;
1588 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1589 int byte = bytestream2_get_byte(&gb);
1590 if (byte == 0xEE) { /* only seen in R2 and R3 */
1591 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1592 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1594 for (count2=0; count2<28; count2++)
1595 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1597 coeff1 = ea_adpcm_table[ byte >> 4 ];
1598 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1599 shift = 20 - (byte & 0x0F);
1601 for (count2=0; count2<28; count2++) {
1603 next_sample = (unsigned)sign_extend(byte, 4) << shift;
1605 byte = bytestream2_get_byte(&gb);
1606 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1609 next_sample += (current_sample * coeff1) +
1610 (previous_sample * coeff2);
1611 next_sample = av_clip_int16(next_sample >> 8);
1613 previous_sample = current_sample;
1614 current_sample = next_sample;
1615 *samplesC++ = current_sample;
1621 } else if (count != count1) {
1622 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1623 count = FFMAX(count, count1);
1626 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1627 c->status[channel].predictor = current_sample;
1628 c->status[channel].prev_sample = previous_sample;
1632 frame->nb_samples = count * 28;
1633 bytestream2_seek(&gb, 0, SEEK_END);
1636 case AV_CODEC_ID_ADPCM_EA_XAS:
1637 for (channel=0; channel<avctx->channels; channel++) {
1638 int coeff[2][4], shift[4];
1639 int16_t *s = samples_p[channel];
1640 for (n = 0; n < 4; n++, s += 32) {
1641 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1643 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1646 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1647 shift[n] = 20 - (val & 0x0F);
1651 for (m=2; m<32; m+=2) {
1652 s = &samples_p[channel][m];
1653 for (n = 0; n < 4; n++, s += 32) {
1655 int byte = bytestream2_get_byteu(&gb);
1657 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1658 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1659 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1661 level = sign_extend(byte, 4) * (1 << shift[n]);
1662 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1663 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1668 case AV_CODEC_ID_ADPCM_IMA_AMV:
1669 av_assert0(avctx->channels == 1);
1673 * int16_t predictor;
1674 * uint8_t step_index;
1676 * uint32_t frame_size;
1678 * Some implementations have step_index as 16-bits, but others
1679 * only use the lower 8 and store garbage in the upper 8.
1681 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1682 c->status[0].step_index = bytestream2_get_byteu(&gb);
1683 bytestream2_skipu(&gb, 5);
1684 if (c->status[0].step_index > 88u) {
1685 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1686 c->status[0].step_index);
1687 return AVERROR_INVALIDDATA;
1690 for (n = nb_samples >> 1; n > 0; n--) {
1691 int v = bytestream2_get_byteu(&gb);
1693 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1694 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1697 if (nb_samples & 1) {
1698 int v = bytestream2_get_byteu(&gb);
1699 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1702 /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
1703 av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
1704 av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
1708 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1709 for (i = 0; i < avctx->channels; i++) {
1710 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1711 c->status[i].step_index = bytestream2_get_byteu(&gb);
1712 bytestream2_skipu(&gb, 1);
1713 if (c->status[i].step_index > 88u) {
1714 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1715 c->status[i].step_index);
1716 return AVERROR_INVALIDDATA;
1720 for (n = nb_samples >> (1 - st); n > 0; n--) {
1721 int v = bytestream2_get_byteu(&gb);
1723 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1724 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1727 case AV_CODEC_ID_ADPCM_CT:
1728 for (n = nb_samples >> (1 - st); n > 0; n--) {
1729 int v = bytestream2_get_byteu(&gb);
1730 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1731 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1734 case AV_CODEC_ID_ADPCM_SBPRO_4:
1735 case AV_CODEC_ID_ADPCM_SBPRO_3:
1736 case AV_CODEC_ID_ADPCM_SBPRO_2:
1737 if (!c->status[0].step_index) {
1738 /* the first byte is a raw sample */
1739 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1741 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1742 c->status[0].step_index = 1;
1745 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1746 for (n = nb_samples >> (1 - st); n > 0; n--) {
1747 int byte = bytestream2_get_byteu(&gb);
1748 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1750 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1753 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1754 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1755 int byte = bytestream2_get_byteu(&gb);
1756 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1758 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1759 (byte >> 2) & 0x07, 3, 0);
1760 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1764 for (n = nb_samples >> (2 - st); n > 0; n--) {
1765 int byte = bytestream2_get_byteu(&gb);
1766 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1768 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1769 (byte >> 4) & 0x03, 2, 2);
1770 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1771 (byte >> 2) & 0x03, 2, 2);
1772 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1777 case AV_CODEC_ID_ADPCM_SWF:
1778 adpcm_swf_decode(avctx, buf, buf_size, samples);
1779 bytestream2_seek(&gb, 0, SEEK_END);
1781 case AV_CODEC_ID_ADPCM_YAMAHA:
1782 for (n = nb_samples >> (1 - st); n > 0; n--) {
1783 int v = bytestream2_get_byteu(&gb);
1784 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1785 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1788 case AV_CODEC_ID_ADPCM_AICA:
1789 for (channel = 0; channel < avctx->channels; channel++) {
1790 samples = samples_p[channel];
1791 for (n = nb_samples >> 1; n > 0; n--) {
1792 int v = bytestream2_get_byteu(&gb);
1793 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1794 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1798 case AV_CODEC_ID_ADPCM_AFC:
1800 int samples_per_block;
1803 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1804 samples_per_block = avctx->extradata[0] / 16;
1805 blocks = nb_samples / avctx->extradata[0];
1807 samples_per_block = nb_samples / 16;
1811 for (m = 0; m < blocks; m++) {
1812 for (channel = 0; channel < avctx->channels; channel++) {
1813 int prev1 = c->status[channel].sample1;
1814 int prev2 = c->status[channel].sample2;
1816 samples = samples_p[channel] + m * 16;
1817 /* Read in every sample for this channel. */
1818 for (i = 0; i < samples_per_block; i++) {
1819 int byte = bytestream2_get_byteu(&gb);
1820 int scale = 1 << (byte >> 4);
1821 int index = byte & 0xf;
1822 int factor1 = ff_adpcm_afc_coeffs[0][index];
1823 int factor2 = ff_adpcm_afc_coeffs[1][index];
1825 /* Decode 16 samples. */
1826 for (n = 0; n < 16; n++) {
1830 sampledat = sign_extend(byte, 4);
1832 byte = bytestream2_get_byteu(&gb);
1833 sampledat = sign_extend(byte >> 4, 4);
1836 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1838 *samples = av_clip_int16(sampledat);
1844 c->status[channel].sample1 = prev1;
1845 c->status[channel].sample2 = prev2;
1848 bytestream2_seek(&gb, 0, SEEK_END);
1851 case AV_CODEC_ID_ADPCM_THP:
1852 case AV_CODEC_ID_ADPCM_THP_LE:
1857 #define THP_GET16(g) \
1859 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1860 bytestream2_get_le16u(&(g)) : \
1861 bytestream2_get_be16u(&(g)), 16)
1863 if (avctx->extradata) {
1865 if (avctx->extradata_size < 32 * avctx->channels) {
1866 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1867 return AVERROR_INVALIDDATA;
1870 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1871 for (i = 0; i < avctx->channels; i++)
1872 for (n = 0; n < 16; n++)
1873 table[i][n] = THP_GET16(tb);
1875 for (i = 0; i < avctx->channels; i++)
1876 for (n = 0; n < 16; n++)
1877 table[i][n] = THP_GET16(gb);
1879 if (!c->has_status) {
1880 /* Initialize the previous sample. */
1881 for (i = 0; i < avctx->channels; i++) {
1882 c->status[i].sample1 = THP_GET16(gb);
1883 c->status[i].sample2 = THP_GET16(gb);
1887 bytestream2_skip(&gb, avctx->channels * 4);
1891 for (ch = 0; ch < avctx->channels; ch++) {
1892 samples = samples_p[ch];
1894 /* Read in every sample for this channel. */
1895 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1896 int byte = bytestream2_get_byteu(&gb);
1897 int index = (byte >> 4) & 7;
1898 unsigned int exp = byte & 0x0F;
1899 int64_t factor1 = table[ch][index * 2];
1900 int64_t factor2 = table[ch][index * 2 + 1];
1902 /* Decode 14 samples. */
1903 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1907 sampledat = sign_extend(byte, 4);
1909 byte = bytestream2_get_byteu(&gb);
1910 sampledat = sign_extend(byte >> 4, 4);
1913 sampledat = ((c->status[ch].sample1 * factor1
1914 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1915 *samples = av_clip_int16(sampledat);
1916 c->status[ch].sample2 = c->status[ch].sample1;
1917 c->status[ch].sample1 = *samples++;
1923 case AV_CODEC_ID_ADPCM_DTK:
1924 for (channel = 0; channel < avctx->channels; channel++) {
1925 samples = samples_p[channel];
1927 /* Read in every sample for this channel. */
1928 for (i = 0; i < nb_samples / 28; i++) {
1931 bytestream2_skipu(&gb, 1);
1932 header = bytestream2_get_byteu(&gb);
1933 bytestream2_skipu(&gb, 3 - channel);
1935 /* Decode 28 samples. */
1936 for (n = 0; n < 28; n++) {
1937 int32_t sampledat, prev;
1939 switch (header >> 4) {
1941 prev = (c->status[channel].sample1 * 0x3c);
1944 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1947 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1953 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1955 byte = bytestream2_get_byteu(&gb);
1957 sampledat = sign_extend(byte, 4);
1959 sampledat = sign_extend(byte >> 4, 4);
1961 sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1962 *samples++ = av_clip_int16(sampledat >> 6);
1963 c->status[channel].sample2 = c->status[channel].sample1;
1964 c->status[channel].sample1 = sampledat;
1968 bytestream2_seek(&gb, 0, SEEK_SET);
1971 case AV_CODEC_ID_ADPCM_PSX:
1972 for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * avctx->channels); block++) {
1973 int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * avctx->channels) / (16 * avctx->channels);
1974 for (channel = 0; channel < avctx->channels; channel++) {
1975 samples = samples_p[channel] + block * nb_samples_per_block;
1976 av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
1978 /* Read in every sample for this channel. */
1979 for (i = 0; i < nb_samples_per_block / 28; i++) {
1980 int filter, shift, flag, byte;
1982 filter = bytestream2_get_byteu(&gb);
1983 shift = filter & 0xf;
1984 filter = filter >> 4;
1985 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1986 return AVERROR_INVALIDDATA;
1987 flag = bytestream2_get_byteu(&gb);
1989 /* Decode 28 samples. */
1990 for (n = 0; n < 28; n++) {
1991 int sample = 0, scale;
1995 scale = sign_extend(byte >> 4, 4);
1997 byte = bytestream2_get_byteu(&gb);
1998 scale = sign_extend(byte, 4);
2001 scale = scale * (1 << 12);
2002 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2004 *samples++ = av_clip_int16(sample);
2005 c->status[channel].sample2 = c->status[channel].sample1;
2006 c->status[channel].sample1 = sample;
2012 case AV_CODEC_ID_ADPCM_ARGO:
2014 * The format of each block:
2015 * uint8_t left_control;
2016 * uint4_t left_samples[nb_samples];
2017 * ---- and if stereo ----
2018 * uint8_t right_control;
2019 * uint4_t right_samples[nb_samples];
2021 * Format of the control byte:
2022 * MSB [SSSSRDRR] LSB
2023 * S = (Shift Amount - 2)
2027 * Each block relies on the previous two samples of each channel.
2028 * They should be 0 initially.
2030 for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2031 for (channel = 0; channel < avctx->channels; channel++) {
2034 samples = samples_p[channel] + block * 32;
2035 cs = c->status + channel;
2037 /* Get the control byte and decode the samples, 2 at a time. */
2038 control = bytestream2_get_byteu(&gb);
2039 shift = (control >> 4) + 2;
2041 for (n = 0; n < 16; n++) {
2042 int sample = bytestream2_get_byteu(&gb);
2043 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2044 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2049 case AV_CODEC_ID_ADPCM_ZORK:
2050 for (n = 0; n < nb_samples * avctx->channels; n++) {
2051 int v = bytestream2_get_byteu(&gb);
2052 *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
2055 case AV_CODEC_ID_ADPCM_IMA_MTF:
2056 for (n = nb_samples / 2; n > 0; n--) {
2057 for (channel = 0; channel < avctx->channels; channel++) {
2058 int v = bytestream2_get_byteu(&gb);
2059 *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2060 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2062 samples += avctx->channels;
2066 av_assert0(0); // unsupported codec_id should not happen
2069 if (avpkt->size && bytestream2_tell(&gb) == 0) {
2070 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2071 return AVERROR_INVALIDDATA;
2076 if (avpkt->size < bytestream2_tell(&gb)) {
2077 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2081 return bytestream2_tell(&gb);
2084 static void adpcm_flush(AVCodecContext *avctx)
2086 ADPCMDecodeContext *c = avctx->priv_data;
2088 /* Just nuke the entire state and re-init. */
2089 memset(c, 0, sizeof(ADPCMDecodeContext));
2091 switch(avctx->codec_id) {
2092 case AV_CODEC_ID_ADPCM_CT:
2093 c->status[0].step = c->status[1].step = 511;
2096 case AV_CODEC_ID_ADPCM_IMA_APC:
2097 if (avctx->extradata && avctx->extradata_size >= 8) {
2098 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
2099 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2103 case AV_CODEC_ID_ADPCM_IMA_APM:
2104 if (avctx->extradata) {
2105 if (avctx->extradata_size >= 28) {
2106 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
2107 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
2108 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2109 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
2110 } else if (avctx->extradata_size >= 16) {
2111 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 0), 18);
2112 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 4), 0, 88);
2113 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 8), 18);
2114 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
2119 case AV_CODEC_ID_ADPCM_IMA_WS:
2120 if (avctx->extradata && avctx->extradata_size >= 2)
2121 c->vqa_version = AV_RL16(avctx->extradata);
2124 /* Other codecs may want to handle this during decoding. */
2133 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
2134 AV_SAMPLE_FMT_NONE };
2135 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2136 AV_SAMPLE_FMT_NONE };
2137 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2139 AV_SAMPLE_FMT_NONE };
2141 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2142 AVCodec ff_ ## name_ ## _decoder = { \
2144 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
2145 .type = AVMEDIA_TYPE_AUDIO, \
2147 .priv_data_size = sizeof(ADPCMDecodeContext), \
2148 .init = adpcm_decode_init, \
2149 .decode = adpcm_decode_frame, \
2150 .flush = adpcm_flush, \
2151 .capabilities = AV_CODEC_CAP_DR1, \
2152 .sample_fmts = sample_fmts_, \
2153 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
2156 /* Note: Do not forget to add new entries to the Makefile as well. */
2157 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
2158 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
2159 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
2160 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
2161 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO, sample_fmts_s16p, adpcm_argo, "ADPCM Argonaut Games");
2162 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
2163 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
2164 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
2165 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2166 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
2167 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
2168 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
2169 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
2170 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
2171 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
2172 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM, sample_fmts_s16, adpcm_ima_apm, "ADPCM IMA Ubisoft APM");
2173 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16p, adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2174 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
2175 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
2176 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
2177 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2178 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2179 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
2180 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MOFLEX, sample_fmts_s16p, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX");
2181 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF, sample_fmts_s16, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework");
2182 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
2183 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
2184 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
2185 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI, sample_fmts_s16, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive");
2186 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
2187 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP, sample_fmts_s16, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP");
2188 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
2189 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
2190 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
2191 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
2192 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
2193 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
2194 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
2195 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
2196 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
2197 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
2198 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
2199 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
2200 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");
2201 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK, sample_fmts_s16, adpcm_zork, "ADPCM Zork");