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 av_cold int adpcm_decode_init(AVCodecContext * avctx)
108 ADPCMDecodeContext *c = avctx->priv_data;
109 unsigned int min_channels = 1;
110 unsigned int max_channels = 2;
112 switch(avctx->codec->id) {
113 case AV_CODEC_ID_ADPCM_IMA_AMV:
116 case AV_CODEC_ID_ADPCM_DTK:
117 case AV_CODEC_ID_ADPCM_EA:
120 case AV_CODEC_ID_ADPCM_AFC:
121 case AV_CODEC_ID_ADPCM_EA_R1:
122 case AV_CODEC_ID_ADPCM_EA_R2:
123 case AV_CODEC_ID_ADPCM_EA_R3:
124 case AV_CODEC_ID_ADPCM_EA_XAS:
125 case AV_CODEC_ID_ADPCM_MS:
128 case AV_CODEC_ID_ADPCM_MTAF:
131 if (avctx->channels & 1) {
132 avpriv_request_sample(avctx, "channel count %d", avctx->channels);
133 return AVERROR_PATCHWELCOME;
136 case AV_CODEC_ID_ADPCM_PSX:
138 if (avctx->channels <= 0 || avctx->block_align % (16 * avctx->channels))
139 return AVERROR_INVALIDDATA;
141 case AV_CODEC_ID_ADPCM_IMA_DAT4:
142 case AV_CODEC_ID_ADPCM_THP:
143 case AV_CODEC_ID_ADPCM_THP_LE:
147 if (avctx->channels < min_channels || avctx->channels > max_channels) {
148 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
149 return AVERROR(EINVAL);
152 switch(avctx->codec->id) {
153 case AV_CODEC_ID_ADPCM_CT:
154 c->status[0].step = c->status[1].step = 511;
156 case AV_CODEC_ID_ADPCM_IMA_WAV:
157 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
158 return AVERROR_INVALIDDATA;
160 case AV_CODEC_ID_ADPCM_IMA_APC:
161 if (avctx->extradata && avctx->extradata_size >= 8) {
162 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
163 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
166 case AV_CODEC_ID_ADPCM_IMA_APM:
167 if (avctx->extradata) {
168 if (avctx->extradata_size >= 28) {
169 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
170 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
171 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
172 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
173 } else if (avctx->extradata_size >= 16) {
174 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 0), 18);
175 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 4), 0, 88);
176 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 8), 18);
177 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
181 case AV_CODEC_ID_ADPCM_IMA_WS:
182 if (avctx->extradata && avctx->extradata_size >= 2)
183 c->vqa_version = AV_RL16(avctx->extradata);
185 case AV_CODEC_ID_ADPCM_ARGO:
186 if (avctx->bits_per_coded_sample != 4 || avctx->block_align != 17 * avctx->channels)
187 return AVERROR_INVALIDDATA;
189 case AV_CODEC_ID_ADPCM_ZORK:
190 if (avctx->bits_per_coded_sample != 8)
191 return AVERROR_INVALIDDATA;
197 switch (avctx->codec->id) {
198 case AV_CODEC_ID_ADPCM_AICA:
199 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
200 case AV_CODEC_ID_ADPCM_IMA_DAT4:
201 case AV_CODEC_ID_ADPCM_IMA_QT:
202 case AV_CODEC_ID_ADPCM_IMA_WAV:
203 case AV_CODEC_ID_ADPCM_4XM:
204 case AV_CODEC_ID_ADPCM_XA:
205 case AV_CODEC_ID_ADPCM_EA_R1:
206 case AV_CODEC_ID_ADPCM_EA_R2:
207 case AV_CODEC_ID_ADPCM_EA_R3:
208 case AV_CODEC_ID_ADPCM_EA_XAS:
209 case AV_CODEC_ID_ADPCM_THP:
210 case AV_CODEC_ID_ADPCM_THP_LE:
211 case AV_CODEC_ID_ADPCM_AFC:
212 case AV_CODEC_ID_ADPCM_DTK:
213 case AV_CODEC_ID_ADPCM_PSX:
214 case AV_CODEC_ID_ADPCM_MTAF:
215 case AV_CODEC_ID_ADPCM_ARGO:
216 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
217 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
219 case AV_CODEC_ID_ADPCM_IMA_WS:
220 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
223 case AV_CODEC_ID_ADPCM_MS:
224 avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
228 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
234 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
236 int delta, pred, step, add;
241 add = (delta * 2 + 1) * step;
245 if ((nibble & 8) == 0)
246 pred = av_clip(pred + (add >> 3), -32767, 32767);
248 pred = av_clip(pred - (add >> 3), -32767, 32767);
255 c->step = av_clip(c->step * 2, 127, 24576);
273 c->step = av_clip(c->step, 127, 24576);
278 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
282 int sign, delta, diff, step;
284 step = ff_adpcm_step_table[c->step_index];
285 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
286 step_index = av_clip(step_index, 0, 88);
290 /* perform direct multiplication instead of series of jumps proposed by
291 * the reference ADPCM implementation since modern CPUs can do the mults
293 diff = ((2 * delta + 1) * step) >> shift;
294 predictor = c->predictor;
295 if (sign) predictor -= diff;
296 else predictor += diff;
298 c->predictor = av_clip_int16(predictor);
299 c->step_index = step_index;
301 return (int16_t)c->predictor;
304 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
308 int sign, delta, diff, step;
310 step = ff_adpcm_step_table[c->step_index];
311 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
312 step_index = av_clip(step_index, 0, 88);
316 diff = (delta * step) >> shift;
317 predictor = c->predictor;
318 if (sign) predictor -= diff;
319 else predictor += diff;
321 c->predictor = av_clip_int16(predictor);
322 c->step_index = step_index;
324 return (int16_t)c->predictor;
327 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
329 int step_index, step, delta, predictor;
331 step = ff_adpcm_step_table[c->step_index];
333 delta = step * (2 * nibble - 15);
334 predictor = c->predictor + delta;
336 step_index = c->step_index + mtf_index_table[(unsigned)nibble];
337 c->predictor = av_clip_int16(predictor >> 4);
338 c->step_index = av_clip(step_index, 0, 88);
340 return (int16_t)c->predictor;
343 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
349 nibble = sign_extend(nibble & 0xF, 4);
351 step = ff_adpcm_ima_cunning_step_table[c->step_index];
352 step_index = c->step_index + ff_adpcm_ima_cunning_index_table[abs(nibble)];
353 step_index = av_clip(step_index, 0, 60);
355 predictor = c->predictor + step * nibble;
357 c->predictor = av_clip_int16(predictor);
358 c->step_index = step_index;
363 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
365 int nibble, step_index, predictor, sign, delta, diff, step, shift;
368 nibble = get_bits_le(gb, bps),
369 step = ff_adpcm_step_table[c->step_index];
370 step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
371 step_index = av_clip(step_index, 0, 88);
373 sign = nibble & (1 << shift);
374 delta = av_mod_uintp2(nibble, shift);
375 diff = ((2 * delta + 1) * step) >> shift;
376 predictor = c->predictor;
377 if (sign) predictor -= diff;
378 else predictor += diff;
380 c->predictor = av_clip_int16(predictor);
381 c->step_index = step_index;
383 return (int16_t)c->predictor;
386 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
392 step = ff_adpcm_step_table[c->step_index];
393 step_index = c->step_index + ff_adpcm_index_table[nibble];
394 step_index = av_clip(step_index, 0, 88);
397 if (nibble & 4) diff += step;
398 if (nibble & 2) diff += step >> 1;
399 if (nibble & 1) diff += step >> 2;
402 predictor = c->predictor - diff;
404 predictor = c->predictor + diff;
406 c->predictor = av_clip_int16(predictor);
407 c->step_index = step_index;
412 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
416 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
417 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
419 c->sample2 = c->sample1;
420 c->sample1 = av_clip_int16(predictor);
421 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
422 if (c->idelta < 16) c->idelta = 16;
423 if (c->idelta > INT_MAX/768) {
424 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
425 c->idelta = INT_MAX/768;
431 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
433 int step_index, predictor, sign, delta, diff, step;
435 step = ff_adpcm_oki_step_table[c->step_index];
436 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
437 step_index = av_clip(step_index, 0, 48);
441 diff = ((2 * delta + 1) * step) >> 3;
442 predictor = c->predictor;
443 if (sign) predictor -= diff;
444 else predictor += diff;
446 c->predictor = av_clip_intp2(predictor, 11);
447 c->step_index = step_index;
449 return c->predictor * 16;
452 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
454 int sign, delta, diff;
459 /* perform direct multiplication instead of series of jumps proposed by
460 * the reference ADPCM implementation since modern CPUs can do the mults
462 diff = ((2 * delta + 1) * c->step) >> 3;
463 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
464 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
465 c->predictor = av_clip_int16(c->predictor);
466 /* calculate new step and clamp it to range 511..32767 */
467 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
468 c->step = av_clip(new_step, 511, 32767);
470 return (int16_t)c->predictor;
473 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
475 int sign, delta, diff;
477 sign = nibble & (1<<(size-1));
478 delta = nibble & ((1<<(size-1))-1);
479 diff = delta << (7 + c->step + shift);
482 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
484 /* calculate new step */
485 if (delta >= (2*size - 3) && c->step < 3)
487 else if (delta == 0 && c->step > 0)
490 return (int16_t) c->predictor;
493 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
500 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
501 c->predictor = av_clip_int16(c->predictor);
502 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
503 c->step = av_clip(c->step, 127, 24576);
507 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
509 c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
510 c->predictor = av_clip_int16(c->predictor);
511 c->step += ff_adpcm_index_table[nibble];
512 c->step = av_clip_uintp2(c->step, 5);
516 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
518 int16_t index = c->step_index;
519 uint32_t lookup_sample = ff_adpcm_step_table[index];
523 sample += lookup_sample;
525 sample += lookup_sample >> 1;
527 sample += lookup_sample >> 2;
529 sample += lookup_sample >> 3;
531 sample += lookup_sample >> 4;
533 sample += lookup_sample >> 5;
535 sample += lookup_sample >> 6;
539 sample += c->predictor;
540 sample = av_clip_int16(sample);
542 index += zork_index_table[(nibble >> 4) & 7];
543 index = av_clip(index, 0, 88);
545 c->predictor = sample;
546 c->step_index = index;
551 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
552 const uint8_t *in, ADPCMChannelStatus *left,
553 ADPCMChannelStatus *right, int channels, int sample_offset)
556 int shift,filter,f0,f1;
560 out0 += sample_offset;
564 out1 += sample_offset;
567 shift = 12 - (in[4+i*2] & 15);
568 filter = in[4+i*2] >> 4;
569 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
570 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
574 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
577 f0 = xa_adpcm_table[filter][0];
578 f1 = xa_adpcm_table[filter][1];
586 t = sign_extend(d, 4);
587 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
589 s_1 = av_clip_int16(s);
596 s_1 = right->sample1;
597 s_2 = right->sample2;
600 shift = 12 - (in[5+i*2] & 15);
601 filter = in[5+i*2] >> 4;
602 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
603 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
607 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
611 f0 = xa_adpcm_table[filter][0];
612 f1 = xa_adpcm_table[filter][1];
617 t = sign_extend(d >> 4, 4);
618 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
620 s_1 = av_clip_int16(s);
625 right->sample1 = s_1;
626 right->sample2 = s_2;
632 out0 += 28 * (3 - channels);
633 out1 += 28 * (3 - channels);
639 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
641 ADPCMDecodeContext *c = avctx->priv_data;
644 int k0, signmask, nb_bits, count;
645 int size = buf_size*8;
648 init_get_bits(&gb, buf, size);
650 //read bits & initial values
651 nb_bits = get_bits(&gb, 2)+2;
652 table = swf_index_tables[nb_bits-2];
653 k0 = 1 << (nb_bits-2);
654 signmask = 1 << (nb_bits-1);
656 while (get_bits_count(&gb) <= size - 22*avctx->channels) {
657 for (i = 0; i < avctx->channels; i++) {
658 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
659 c->status[i].step_index = get_bits(&gb, 6);
662 for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
665 for (i = 0; i < avctx->channels; i++) {
666 // similar to IMA adpcm
667 int delta = get_bits(&gb, nb_bits);
668 int step = ff_adpcm_step_table[c->status[i].step_index];
669 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
680 if (delta & signmask)
681 c->status[i].predictor -= vpdiff;
683 c->status[i].predictor += vpdiff;
685 c->status[i].step_index += table[delta & (~signmask)];
687 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
688 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
690 *samples++ = c->status[i].predictor;
696 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
698 int sample = sign_extend(nibble, 4) * (1 << shift);
701 sample += (8 * cs->sample1) - (4 * cs->sample2);
703 sample += 4 * cs->sample1;
705 sample = av_clip_int16(sample >> 2);
707 cs->sample2 = cs->sample1;
708 cs->sample1 = sample;
714 * Get the number of samples (per channel) that will be decoded from the packet.
715 * In one case, this is actually the maximum number of samples possible to
716 * decode with the given buf_size.
718 * @param[out] coded_samples set to the number of samples as coded in the
719 * packet, or 0 if the codec does not encode the
720 * number of samples in each frame.
721 * @param[out] approx_nb_samples set to non-zero if the number of samples
722 * returned is an approximation.
724 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
725 int buf_size, int *coded_samples, int *approx_nb_samples)
727 ADPCMDecodeContext *s = avctx->priv_data;
729 int ch = avctx->channels;
730 int has_coded_samples = 0;
734 *approx_nb_samples = 0;
739 switch (avctx->codec->id) {
740 /* constant, only check buf_size */
741 case AV_CODEC_ID_ADPCM_EA_XAS:
742 if (buf_size < 76 * ch)
746 case AV_CODEC_ID_ADPCM_IMA_QT:
747 if (buf_size < 34 * ch)
751 /* simple 4-bit adpcm */
752 case AV_CODEC_ID_ADPCM_CT:
753 case AV_CODEC_ID_ADPCM_IMA_APC:
754 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
755 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
756 case AV_CODEC_ID_ADPCM_IMA_OKI:
757 case AV_CODEC_ID_ADPCM_IMA_WS:
758 case AV_CODEC_ID_ADPCM_YAMAHA:
759 case AV_CODEC_ID_ADPCM_AICA:
760 case AV_CODEC_ID_ADPCM_IMA_SSI:
761 case AV_CODEC_ID_ADPCM_IMA_APM:
762 case AV_CODEC_ID_ADPCM_IMA_ALP:
763 case AV_CODEC_ID_ADPCM_IMA_MTF:
764 nb_samples = buf_size * 2 / ch;
770 /* simple 4-bit adpcm, with header */
772 switch (avctx->codec->id) {
773 case AV_CODEC_ID_ADPCM_4XM:
774 case AV_CODEC_ID_ADPCM_AGM:
775 case AV_CODEC_ID_ADPCM_IMA_DAT4:
776 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
777 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
778 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
781 return (buf_size - header_size) * 2 / ch;
783 /* more complex formats */
784 switch (avctx->codec->id) {
785 case AV_CODEC_ID_ADPCM_IMA_AMV:
786 bytestream2_skip(gb, 4);
787 has_coded_samples = 1;
788 *coded_samples = bytestream2_get_le32u(gb);
789 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
790 bytestream2_seek(gb, -8, SEEK_CUR);
792 case AV_CODEC_ID_ADPCM_EA:
793 has_coded_samples = 1;
794 *coded_samples = bytestream2_get_le32(gb);
795 *coded_samples -= *coded_samples % 28;
796 nb_samples = (buf_size - 12) / 30 * 28;
798 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
799 has_coded_samples = 1;
800 *coded_samples = bytestream2_get_le32(gb);
801 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
803 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
804 nb_samples = (buf_size - ch) / ch * 2;
806 case AV_CODEC_ID_ADPCM_EA_R1:
807 case AV_CODEC_ID_ADPCM_EA_R2:
808 case AV_CODEC_ID_ADPCM_EA_R3:
809 /* maximum number of samples */
810 /* has internal offsets and a per-frame switch to signal raw 16-bit */
811 has_coded_samples = 1;
812 switch (avctx->codec->id) {
813 case AV_CODEC_ID_ADPCM_EA_R1:
814 header_size = 4 + 9 * ch;
815 *coded_samples = bytestream2_get_le32(gb);
817 case AV_CODEC_ID_ADPCM_EA_R2:
818 header_size = 4 + 5 * ch;
819 *coded_samples = bytestream2_get_le32(gb);
821 case AV_CODEC_ID_ADPCM_EA_R3:
822 header_size = 4 + 5 * ch;
823 *coded_samples = bytestream2_get_be32(gb);
826 *coded_samples -= *coded_samples % 28;
827 nb_samples = (buf_size - header_size) * 2 / ch;
828 nb_samples -= nb_samples % 28;
829 *approx_nb_samples = 1;
831 case AV_CODEC_ID_ADPCM_IMA_DK3:
832 if (avctx->block_align > 0)
833 buf_size = FFMIN(buf_size, avctx->block_align);
834 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
836 case AV_CODEC_ID_ADPCM_IMA_DK4:
837 if (avctx->block_align > 0)
838 buf_size = FFMIN(buf_size, avctx->block_align);
839 if (buf_size < 4 * ch)
840 return AVERROR_INVALIDDATA;
841 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
843 case AV_CODEC_ID_ADPCM_IMA_RAD:
844 if (avctx->block_align > 0)
845 buf_size = FFMIN(buf_size, avctx->block_align);
846 nb_samples = (buf_size - 4 * ch) * 2 / ch;
848 case AV_CODEC_ID_ADPCM_IMA_WAV:
850 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
851 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
852 if (avctx->block_align > 0)
853 buf_size = FFMIN(buf_size, avctx->block_align);
854 if (buf_size < 4 * ch)
855 return AVERROR_INVALIDDATA;
856 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
859 case AV_CODEC_ID_ADPCM_MS:
860 if (avctx->block_align > 0)
861 buf_size = FFMIN(buf_size, avctx->block_align);
862 nb_samples = (buf_size - 6 * ch) * 2 / ch;
864 case AV_CODEC_ID_ADPCM_MTAF:
865 if (avctx->block_align > 0)
866 buf_size = FFMIN(buf_size, avctx->block_align);
867 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
869 case AV_CODEC_ID_ADPCM_SBPRO_2:
870 case AV_CODEC_ID_ADPCM_SBPRO_3:
871 case AV_CODEC_ID_ADPCM_SBPRO_4:
873 int samples_per_byte;
874 switch (avctx->codec->id) {
875 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
876 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
877 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
879 if (!s->status[0].step_index) {
881 return AVERROR_INVALIDDATA;
885 nb_samples += buf_size * samples_per_byte / ch;
888 case AV_CODEC_ID_ADPCM_SWF:
890 int buf_bits = buf_size * 8 - 2;
891 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
892 int block_hdr_size = 22 * ch;
893 int block_size = block_hdr_size + nbits * ch * 4095;
894 int nblocks = buf_bits / block_size;
895 int bits_left = buf_bits - nblocks * block_size;
896 nb_samples = nblocks * 4096;
897 if (bits_left >= block_hdr_size)
898 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
901 case AV_CODEC_ID_ADPCM_THP:
902 case AV_CODEC_ID_ADPCM_THP_LE:
903 if (avctx->extradata) {
904 nb_samples = buf_size * 14 / (8 * ch);
907 has_coded_samples = 1;
908 bytestream2_skip(gb, 4); // channel size
909 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
910 bytestream2_get_le32(gb) :
911 bytestream2_get_be32(gb);
912 buf_size -= 8 + 36 * ch;
914 nb_samples = buf_size / 8 * 14;
915 if (buf_size % 8 > 1)
916 nb_samples += (buf_size % 8 - 1) * 2;
917 *approx_nb_samples = 1;
919 case AV_CODEC_ID_ADPCM_AFC:
920 nb_samples = buf_size / (9 * ch) * 16;
922 case AV_CODEC_ID_ADPCM_XA:
923 nb_samples = (buf_size / 128) * 224 / ch;
925 case AV_CODEC_ID_ADPCM_DTK:
926 case AV_CODEC_ID_ADPCM_PSX:
927 nb_samples = buf_size / (16 * ch) * 28;
929 case AV_CODEC_ID_ADPCM_ARGO:
930 nb_samples = buf_size / avctx->block_align * 32;
932 case AV_CODEC_ID_ADPCM_ZORK:
933 nb_samples = buf_size / ch;
937 /* validate coded sample count */
938 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
939 return AVERROR_INVALIDDATA;
944 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
945 int *got_frame_ptr, AVPacket *avpkt)
947 AVFrame *frame = data;
948 const uint8_t *buf = avpkt->data;
949 int buf_size = avpkt->size;
950 ADPCMDecodeContext *c = avctx->priv_data;
951 ADPCMChannelStatus *cs;
952 int n, m, channel, i;
957 int nb_samples, coded_samples, approx_nb_samples, ret;
960 bytestream2_init(&gb, buf, buf_size);
961 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
962 if (nb_samples <= 0) {
963 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
964 return AVERROR_INVALIDDATA;
967 /* get output buffer */
968 frame->nb_samples = nb_samples;
969 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
971 samples = (int16_t *)frame->data[0];
972 samples_p = (int16_t **)frame->extended_data;
974 /* use coded_samples when applicable */
975 /* it is always <= nb_samples, so the output buffer will be large enough */
977 if (!approx_nb_samples && coded_samples != nb_samples)
978 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
979 frame->nb_samples = nb_samples = coded_samples;
982 st = avctx->channels == 2 ? 1 : 0;
984 switch(avctx->codec->id) {
985 case AV_CODEC_ID_ADPCM_IMA_QT:
986 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
987 Channel data is interleaved per-chunk. */
988 for (channel = 0; channel < avctx->channels; channel++) {
991 cs = &(c->status[channel]);
992 /* (pppppp) (piiiiiii) */
994 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
995 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
996 step_index = predictor & 0x7F;
999 if (cs->step_index == step_index) {
1000 int diff = predictor - cs->predictor;
1007 cs->step_index = step_index;
1008 cs->predictor = predictor;
1011 if (cs->step_index > 88u){
1012 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1013 channel, cs->step_index);
1014 return AVERROR_INVALIDDATA;
1017 samples = samples_p[channel];
1019 for (m = 0; m < 64; m += 2) {
1020 int byte = bytestream2_get_byteu(&gb);
1021 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1022 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1026 case AV_CODEC_ID_ADPCM_IMA_WAV:
1027 for(i=0; i<avctx->channels; i++){
1028 cs = &(c->status[i]);
1029 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1031 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1032 if (cs->step_index > 88u){
1033 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1035 return AVERROR_INVALIDDATA;
1039 if (avctx->bits_per_coded_sample != 4) {
1040 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1041 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1042 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1045 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1046 for (i = 0; i < avctx->channels; i++) {
1050 samples = &samples_p[i][1 + n * samples_per_block];
1051 for (j = 0; j < block_size; j++) {
1052 temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
1053 (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
1055 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1058 for (m = 0; m < samples_per_block; m++) {
1059 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1060 avctx->bits_per_coded_sample);
1064 bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1066 for (n = 0; n < (nb_samples - 1) / 8; n++) {
1067 for (i = 0; i < avctx->channels; i++) {
1069 samples = &samples_p[i][1 + n * 8];
1070 for (m = 0; m < 8; m += 2) {
1071 int v = bytestream2_get_byteu(&gb);
1072 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1073 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1079 case AV_CODEC_ID_ADPCM_4XM:
1080 for (i = 0; i < avctx->channels; i++)
1081 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1083 for (i = 0; i < avctx->channels; i++) {
1084 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1085 if (c->status[i].step_index > 88u) {
1086 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1087 i, c->status[i].step_index);
1088 return AVERROR_INVALIDDATA;
1092 for (i = 0; i < avctx->channels; i++) {
1093 samples = (int16_t *)frame->data[i];
1095 for (n = nb_samples >> 1; n > 0; n--) {
1096 int v = bytestream2_get_byteu(&gb);
1097 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1098 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1102 case AV_CODEC_ID_ADPCM_AGM:
1103 for (i = 0; i < avctx->channels; i++)
1104 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1105 for (i = 0; i < avctx->channels; i++)
1106 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1108 for (n = 0; n < nb_samples >> (1 - st); n++) {
1109 int v = bytestream2_get_byteu(&gb);
1110 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1111 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1114 case AV_CODEC_ID_ADPCM_MS:
1116 int block_predictor;
1118 if (avctx->channels > 2) {
1119 for (channel = 0; channel < avctx->channels; channel++) {
1120 samples = samples_p[channel];
1121 block_predictor = bytestream2_get_byteu(&gb);
1122 if (block_predictor > 6) {
1123 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1124 channel, block_predictor);
1125 return AVERROR_INVALIDDATA;
1127 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1128 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1129 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1130 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1131 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1132 *samples++ = c->status[channel].sample2;
1133 *samples++ = c->status[channel].sample1;
1134 for(n = (nb_samples - 2) >> 1; n > 0; n--) {
1135 int byte = bytestream2_get_byteu(&gb);
1136 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1137 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1141 block_predictor = bytestream2_get_byteu(&gb);
1142 if (block_predictor > 6) {
1143 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1145 return AVERROR_INVALIDDATA;
1147 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1148 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1150 block_predictor = bytestream2_get_byteu(&gb);
1151 if (block_predictor > 6) {
1152 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1154 return AVERROR_INVALIDDATA;
1156 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1157 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1159 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1161 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1164 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1165 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1166 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1167 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1169 *samples++ = c->status[0].sample2;
1170 if (st) *samples++ = c->status[1].sample2;
1171 *samples++ = c->status[0].sample1;
1172 if (st) *samples++ = c->status[1].sample1;
1173 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1174 int byte = bytestream2_get_byteu(&gb);
1175 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1176 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1181 case AV_CODEC_ID_ADPCM_MTAF:
1182 for (channel = 0; channel < avctx->channels; channel+=2) {
1183 bytestream2_skipu(&gb, 4);
1184 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1185 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1186 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1187 bytestream2_skipu(&gb, 2);
1188 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1189 bytestream2_skipu(&gb, 2);
1190 for (n = 0; n < nb_samples; n+=2) {
1191 int v = bytestream2_get_byteu(&gb);
1192 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1193 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1195 for (n = 0; n < nb_samples; n+=2) {
1196 int v = bytestream2_get_byteu(&gb);
1197 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1198 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1202 case AV_CODEC_ID_ADPCM_IMA_DK4:
1203 for (channel = 0; channel < avctx->channels; channel++) {
1204 cs = &c->status[channel];
1205 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1206 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1207 if (cs->step_index > 88u){
1208 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1209 channel, cs->step_index);
1210 return AVERROR_INVALIDDATA;
1213 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1214 int v = bytestream2_get_byteu(&gb);
1215 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1216 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1219 case AV_CODEC_ID_ADPCM_IMA_DK3:
1223 int decode_top_nibble_next = 0;
1225 const int16_t *samples_end = samples + avctx->channels * nb_samples;
1227 bytestream2_skipu(&gb, 10);
1228 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1229 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1230 c->status[0].step_index = bytestream2_get_byteu(&gb);
1231 c->status[1].step_index = bytestream2_get_byteu(&gb);
1232 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1233 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1234 c->status[0].step_index, c->status[1].step_index);
1235 return AVERROR_INVALIDDATA;
1237 /* sign extend the predictors */
1238 diff_channel = c->status[1].predictor;
1240 /* DK3 ADPCM support macro */
1241 #define DK3_GET_NEXT_NIBBLE() \
1242 if (decode_top_nibble_next) { \
1243 nibble = last_byte >> 4; \
1244 decode_top_nibble_next = 0; \
1246 last_byte = bytestream2_get_byteu(&gb); \
1247 nibble = last_byte & 0x0F; \
1248 decode_top_nibble_next = 1; \
1251 while (samples < samples_end) {
1253 /* for this algorithm, c->status[0] is the sum channel and
1254 * c->status[1] is the diff channel */
1256 /* process the first predictor of the sum channel */
1257 DK3_GET_NEXT_NIBBLE();
1258 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1260 /* process the diff channel predictor */
1261 DK3_GET_NEXT_NIBBLE();
1262 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1264 /* process the first pair of stereo PCM samples */
1265 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1266 *samples++ = c->status[0].predictor + c->status[1].predictor;
1267 *samples++ = c->status[0].predictor - c->status[1].predictor;
1269 /* process the second predictor of the sum channel */
1270 DK3_GET_NEXT_NIBBLE();
1271 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1273 /* process the second pair of stereo PCM samples */
1274 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1275 *samples++ = c->status[0].predictor + c->status[1].predictor;
1276 *samples++ = c->status[0].predictor - c->status[1].predictor;
1279 if ((bytestream2_tell(&gb) & 1))
1280 bytestream2_skip(&gb, 1);
1283 case AV_CODEC_ID_ADPCM_IMA_ISS:
1284 for (channel = 0; channel < avctx->channels; channel++) {
1285 cs = &c->status[channel];
1286 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1287 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1288 if (cs->step_index > 88u){
1289 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1290 channel, cs->step_index);
1291 return AVERROR_INVALIDDATA;
1295 for (n = nb_samples >> (1 - st); n > 0; n--) {
1297 int v = bytestream2_get_byteu(&gb);
1298 /* nibbles are swapped for mono */
1306 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1307 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1310 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
1311 for (channel = 0; channel < avctx->channels; channel++) {
1312 cs = &c->status[channel];
1313 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1314 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1315 if (cs->step_index > 88u){
1316 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1317 channel, cs->step_index);
1318 return AVERROR_INVALIDDATA;
1322 for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1323 for (channel = 0; channel < avctx->channels; channel++) {
1324 samples = samples_p[channel] + 256 * subframe;
1325 for (n = 0; n < 256; n += 2) {
1326 int v = bytestream2_get_byteu(&gb);
1327 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1328 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1333 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1334 for (channel = 0; channel < avctx->channels; channel++) {
1335 cs = &c->status[channel];
1336 samples = samples_p[channel];
1337 bytestream2_skip(&gb, 4);
1338 for (n = 0; n < nb_samples; n += 2) {
1339 int v = bytestream2_get_byteu(&gb);
1340 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1341 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1345 case AV_CODEC_ID_ADPCM_IMA_APC:
1346 for (n = nb_samples >> (1 - st); n > 0; n--) {
1347 int v = bytestream2_get_byteu(&gb);
1348 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1349 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1352 case AV_CODEC_ID_ADPCM_IMA_SSI:
1353 for (n = nb_samples >> (1 - st); n > 0; n--) {
1354 int v = bytestream2_get_byteu(&gb);
1355 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1356 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1359 case AV_CODEC_ID_ADPCM_IMA_APM:
1360 for (n = nb_samples / 2; n > 0; n--) {
1361 for (channel = 0; channel < avctx->channels; channel++) {
1362 int v = bytestream2_get_byteu(&gb);
1363 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1364 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1366 samples += avctx->channels;
1369 case AV_CODEC_ID_ADPCM_IMA_ALP:
1370 for (n = nb_samples / 2; n > 0; n--) {
1371 for (channel = 0; channel < avctx->channels; channel++) {
1372 int v = bytestream2_get_byteu(&gb);
1373 *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1374 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1376 samples += avctx->channels;
1379 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1380 for (channel = 0; channel < avctx->channels; channel++) {
1381 int16_t *smp = samples_p[channel];
1382 for (n = 0; n < nb_samples / 2; n++) {
1383 int v = bytestream2_get_byteu(&gb);
1384 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1385 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1389 case AV_CODEC_ID_ADPCM_IMA_OKI:
1390 for (n = nb_samples >> (1 - st); n > 0; n--) {
1391 int v = bytestream2_get_byteu(&gb);
1392 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1393 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1396 case AV_CODEC_ID_ADPCM_IMA_RAD:
1397 for (channel = 0; channel < avctx->channels; channel++) {
1398 cs = &c->status[channel];
1399 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1400 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1401 if (cs->step_index > 88u){
1402 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1403 channel, cs->step_index);
1404 return AVERROR_INVALIDDATA;
1407 for (n = 0; n < nb_samples / 2; n++) {
1410 byte[0] = bytestream2_get_byteu(&gb);
1412 byte[1] = bytestream2_get_byteu(&gb);
1413 for(channel = 0; channel < avctx->channels; channel++) {
1414 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1416 for(channel = 0; channel < avctx->channels; channel++) {
1417 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1421 case AV_CODEC_ID_ADPCM_IMA_WS:
1422 if (c->vqa_version == 3) {
1423 for (channel = 0; channel < avctx->channels; channel++) {
1424 int16_t *smp = samples_p[channel];
1426 for (n = nb_samples / 2; n > 0; n--) {
1427 int v = bytestream2_get_byteu(&gb);
1428 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1429 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1433 for (n = nb_samples / 2; n > 0; n--) {
1434 for (channel = 0; channel < avctx->channels; channel++) {
1435 int v = bytestream2_get_byteu(&gb);
1436 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1437 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1439 samples += avctx->channels;
1442 bytestream2_seek(&gb, 0, SEEK_END);
1444 case AV_CODEC_ID_ADPCM_XA:
1446 int16_t *out0 = samples_p[0];
1447 int16_t *out1 = samples_p[1];
1448 int samples_per_block = 28 * (3 - avctx->channels) * 4;
1449 int sample_offset = 0;
1450 int bytes_remaining;
1451 while (bytestream2_get_bytes_left(&gb) >= 128) {
1452 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1453 &c->status[0], &c->status[1],
1454 avctx->channels, sample_offset)) < 0)
1456 bytestream2_skipu(&gb, 128);
1457 sample_offset += samples_per_block;
1459 /* Less than a full block of data left, e.g. when reading from
1460 * 2324 byte per sector XA; the remainder is padding */
1461 bytes_remaining = bytestream2_get_bytes_left(&gb);
1462 if (bytes_remaining > 0) {
1463 bytestream2_skip(&gb, bytes_remaining);
1467 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1468 for (i=0; i<=st; i++) {
1469 c->status[i].step_index = bytestream2_get_le32u(&gb);
1470 if (c->status[i].step_index > 88u) {
1471 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1472 i, c->status[i].step_index);
1473 return AVERROR_INVALIDDATA;
1476 for (i=0; i<=st; i++) {
1477 c->status[i].predictor = bytestream2_get_le32u(&gb);
1478 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1479 return AVERROR_INVALIDDATA;
1482 for (n = nb_samples >> (1 - st); n > 0; n--) {
1483 int byte = bytestream2_get_byteu(&gb);
1484 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1485 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1488 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1489 for (n = nb_samples >> (1 - st); n > 0; n--) {
1490 int byte = bytestream2_get_byteu(&gb);
1491 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1492 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1495 case AV_CODEC_ID_ADPCM_EA:
1497 int previous_left_sample, previous_right_sample;
1498 int current_left_sample, current_right_sample;
1499 int next_left_sample, next_right_sample;
1500 int coeff1l, coeff2l, coeff1r, coeff2r;
1501 int shift_left, shift_right;
1503 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1504 each coding 28 stereo samples. */
1506 if(avctx->channels != 2)
1507 return AVERROR_INVALIDDATA;
1509 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1510 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1511 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1512 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1514 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1515 int byte = bytestream2_get_byteu(&gb);
1516 coeff1l = ea_adpcm_table[ byte >> 4 ];
1517 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1518 coeff1r = ea_adpcm_table[ byte & 0x0F];
1519 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1521 byte = bytestream2_get_byteu(&gb);
1522 shift_left = 20 - (byte >> 4);
1523 shift_right = 20 - (byte & 0x0F);
1525 for (count2 = 0; count2 < 28; count2++) {
1526 byte = bytestream2_get_byteu(&gb);
1527 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1528 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1530 next_left_sample = (next_left_sample +
1531 (current_left_sample * coeff1l) +
1532 (previous_left_sample * coeff2l) + 0x80) >> 8;
1533 next_right_sample = (next_right_sample +
1534 (current_right_sample * coeff1r) +
1535 (previous_right_sample * coeff2r) + 0x80) >> 8;
1537 previous_left_sample = current_left_sample;
1538 current_left_sample = av_clip_int16(next_left_sample);
1539 previous_right_sample = current_right_sample;
1540 current_right_sample = av_clip_int16(next_right_sample);
1541 *samples++ = current_left_sample;
1542 *samples++ = current_right_sample;
1546 bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1550 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1552 int coeff[2][2], shift[2];
1554 for(channel = 0; channel < avctx->channels; channel++) {
1555 int byte = bytestream2_get_byteu(&gb);
1557 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1558 shift[channel] = 20 - (byte & 0x0F);
1560 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1563 byte[0] = bytestream2_get_byteu(&gb);
1564 if (st) byte[1] = bytestream2_get_byteu(&gb);
1565 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1566 for(channel = 0; channel < avctx->channels; channel++) {
1567 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1569 c->status[channel].sample1 * coeff[channel][0] +
1570 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1571 c->status[channel].sample2 = c->status[channel].sample1;
1572 c->status[channel].sample1 = av_clip_int16(sample);
1573 *samples++ = c->status[channel].sample1;
1577 bytestream2_seek(&gb, 0, SEEK_END);
1580 case AV_CODEC_ID_ADPCM_EA_R1:
1581 case AV_CODEC_ID_ADPCM_EA_R2:
1582 case AV_CODEC_ID_ADPCM_EA_R3: {
1583 /* channel numbering
1585 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1586 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1587 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1588 int previous_sample, current_sample, next_sample;
1591 unsigned int channel;
1596 for (channel=0; channel<avctx->channels; channel++)
1597 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1598 bytestream2_get_le32(&gb)) +
1599 (avctx->channels + 1) * 4;
1601 for (channel=0; channel<avctx->channels; channel++) {
1602 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1603 samplesC = samples_p[channel];
1605 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1606 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1607 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1609 current_sample = c->status[channel].predictor;
1610 previous_sample = c->status[channel].prev_sample;
1613 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1614 int byte = bytestream2_get_byte(&gb);
1615 if (byte == 0xEE) { /* only seen in R2 and R3 */
1616 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1617 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1619 for (count2=0; count2<28; count2++)
1620 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1622 coeff1 = ea_adpcm_table[ byte >> 4 ];
1623 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1624 shift = 20 - (byte & 0x0F);
1626 for (count2=0; count2<28; count2++) {
1628 next_sample = (unsigned)sign_extend(byte, 4) << shift;
1630 byte = bytestream2_get_byte(&gb);
1631 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1634 next_sample += (current_sample * coeff1) +
1635 (previous_sample * coeff2);
1636 next_sample = av_clip_int16(next_sample >> 8);
1638 previous_sample = current_sample;
1639 current_sample = next_sample;
1640 *samplesC++ = current_sample;
1646 } else if (count != count1) {
1647 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1648 count = FFMAX(count, count1);
1651 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1652 c->status[channel].predictor = current_sample;
1653 c->status[channel].prev_sample = previous_sample;
1657 frame->nb_samples = count * 28;
1658 bytestream2_seek(&gb, 0, SEEK_END);
1661 case AV_CODEC_ID_ADPCM_EA_XAS:
1662 for (channel=0; channel<avctx->channels; channel++) {
1663 int coeff[2][4], shift[4];
1664 int16_t *s = samples_p[channel];
1665 for (n = 0; n < 4; n++, s += 32) {
1666 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1668 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1671 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1672 shift[n] = 20 - (val & 0x0F);
1676 for (m=2; m<32; m+=2) {
1677 s = &samples_p[channel][m];
1678 for (n = 0; n < 4; n++, s += 32) {
1680 int byte = bytestream2_get_byteu(&gb);
1682 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1683 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1684 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1686 level = sign_extend(byte, 4) * (1 << shift[n]);
1687 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1688 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1693 case AV_CODEC_ID_ADPCM_IMA_AMV:
1694 av_assert0(avctx->channels == 1);
1698 * int16_t predictor;
1699 * uint8_t step_index;
1701 * uint32_t frame_size;
1703 * Some implementations have step_index as 16-bits, but others
1704 * only use the lower 8 and store garbage in the upper 8.
1706 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1707 c->status[0].step_index = bytestream2_get_byteu(&gb);
1708 bytestream2_skipu(&gb, 5);
1709 if (c->status[0].step_index > 88u) {
1710 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1711 c->status[0].step_index);
1712 return AVERROR_INVALIDDATA;
1715 for (n = nb_samples >> 1; n > 0; n--) {
1716 int v = bytestream2_get_byteu(&gb);
1718 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1719 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1722 if (nb_samples & 1) {
1723 int v = bytestream2_get_byteu(&gb);
1724 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1727 /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
1728 av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
1729 av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
1733 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1734 for (i = 0; i < avctx->channels; i++) {
1735 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1736 c->status[i].step_index = bytestream2_get_byteu(&gb);
1737 bytestream2_skipu(&gb, 1);
1738 if (c->status[i].step_index > 88u) {
1739 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1740 c->status[i].step_index);
1741 return AVERROR_INVALIDDATA;
1745 for (n = nb_samples >> (1 - st); n > 0; n--) {
1746 int v = bytestream2_get_byteu(&gb);
1748 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1749 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1752 case AV_CODEC_ID_ADPCM_CT:
1753 for (n = nb_samples >> (1 - st); n > 0; n--) {
1754 int v = bytestream2_get_byteu(&gb);
1755 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1756 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1759 case AV_CODEC_ID_ADPCM_SBPRO_4:
1760 case AV_CODEC_ID_ADPCM_SBPRO_3:
1761 case AV_CODEC_ID_ADPCM_SBPRO_2:
1762 if (!c->status[0].step_index) {
1763 /* the first byte is a raw sample */
1764 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1766 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1767 c->status[0].step_index = 1;
1770 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1771 for (n = nb_samples >> (1 - st); n > 0; n--) {
1772 int byte = bytestream2_get_byteu(&gb);
1773 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1775 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1778 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1779 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1780 int byte = bytestream2_get_byteu(&gb);
1781 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1783 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1784 (byte >> 2) & 0x07, 3, 0);
1785 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1789 for (n = nb_samples >> (2 - st); n > 0; n--) {
1790 int byte = bytestream2_get_byteu(&gb);
1791 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1793 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1794 (byte >> 4) & 0x03, 2, 2);
1795 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1796 (byte >> 2) & 0x03, 2, 2);
1797 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1802 case AV_CODEC_ID_ADPCM_SWF:
1803 adpcm_swf_decode(avctx, buf, buf_size, samples);
1804 bytestream2_seek(&gb, 0, SEEK_END);
1806 case AV_CODEC_ID_ADPCM_YAMAHA:
1807 for (n = nb_samples >> (1 - st); n > 0; n--) {
1808 int v = bytestream2_get_byteu(&gb);
1809 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1810 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1813 case AV_CODEC_ID_ADPCM_AICA:
1814 if (!c->has_status) {
1815 for (channel = 0; channel < avctx->channels; channel++)
1816 c->status[channel].step = 0;
1819 for (channel = 0; channel < avctx->channels; channel++) {
1820 samples = samples_p[channel];
1821 for (n = nb_samples >> 1; n > 0; n--) {
1822 int v = bytestream2_get_byteu(&gb);
1823 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1824 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1828 case AV_CODEC_ID_ADPCM_AFC:
1830 int samples_per_block;
1833 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1834 samples_per_block = avctx->extradata[0] / 16;
1835 blocks = nb_samples / avctx->extradata[0];
1837 samples_per_block = nb_samples / 16;
1841 for (m = 0; m < blocks; m++) {
1842 for (channel = 0; channel < avctx->channels; channel++) {
1843 int prev1 = c->status[channel].sample1;
1844 int prev2 = c->status[channel].sample2;
1846 samples = samples_p[channel] + m * 16;
1847 /* Read in every sample for this channel. */
1848 for (i = 0; i < samples_per_block; i++) {
1849 int byte = bytestream2_get_byteu(&gb);
1850 int scale = 1 << (byte >> 4);
1851 int index = byte & 0xf;
1852 int factor1 = ff_adpcm_afc_coeffs[0][index];
1853 int factor2 = ff_adpcm_afc_coeffs[1][index];
1855 /* Decode 16 samples. */
1856 for (n = 0; n < 16; n++) {
1860 sampledat = sign_extend(byte, 4);
1862 byte = bytestream2_get_byteu(&gb);
1863 sampledat = sign_extend(byte >> 4, 4);
1866 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1868 *samples = av_clip_int16(sampledat);
1874 c->status[channel].sample1 = prev1;
1875 c->status[channel].sample2 = prev2;
1878 bytestream2_seek(&gb, 0, SEEK_END);
1881 case AV_CODEC_ID_ADPCM_THP:
1882 case AV_CODEC_ID_ADPCM_THP_LE:
1887 #define THP_GET16(g) \
1889 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1890 bytestream2_get_le16u(&(g)) : \
1891 bytestream2_get_be16u(&(g)), 16)
1893 if (avctx->extradata) {
1895 if (avctx->extradata_size < 32 * avctx->channels) {
1896 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1897 return AVERROR_INVALIDDATA;
1900 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1901 for (i = 0; i < avctx->channels; i++)
1902 for (n = 0; n < 16; n++)
1903 table[i][n] = THP_GET16(tb);
1905 for (i = 0; i < avctx->channels; i++)
1906 for (n = 0; n < 16; n++)
1907 table[i][n] = THP_GET16(gb);
1909 if (!c->has_status) {
1910 /* Initialize the previous sample. */
1911 for (i = 0; i < avctx->channels; i++) {
1912 c->status[i].sample1 = THP_GET16(gb);
1913 c->status[i].sample2 = THP_GET16(gb);
1917 bytestream2_skip(&gb, avctx->channels * 4);
1921 for (ch = 0; ch < avctx->channels; ch++) {
1922 samples = samples_p[ch];
1924 /* Read in every sample for this channel. */
1925 for (i = 0; i < (nb_samples + 13) / 14; i++) {
1926 int byte = bytestream2_get_byteu(&gb);
1927 int index = (byte >> 4) & 7;
1928 unsigned int exp = byte & 0x0F;
1929 int64_t factor1 = table[ch][index * 2];
1930 int64_t factor2 = table[ch][index * 2 + 1];
1932 /* Decode 14 samples. */
1933 for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1937 sampledat = sign_extend(byte, 4);
1939 byte = bytestream2_get_byteu(&gb);
1940 sampledat = sign_extend(byte >> 4, 4);
1943 sampledat = ((c->status[ch].sample1 * factor1
1944 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1945 *samples = av_clip_int16(sampledat);
1946 c->status[ch].sample2 = c->status[ch].sample1;
1947 c->status[ch].sample1 = *samples++;
1953 case AV_CODEC_ID_ADPCM_DTK:
1954 for (channel = 0; channel < avctx->channels; channel++) {
1955 samples = samples_p[channel];
1957 /* Read in every sample for this channel. */
1958 for (i = 0; i < nb_samples / 28; i++) {
1961 bytestream2_skipu(&gb, 1);
1962 header = bytestream2_get_byteu(&gb);
1963 bytestream2_skipu(&gb, 3 - channel);
1965 /* Decode 28 samples. */
1966 for (n = 0; n < 28; n++) {
1967 int32_t sampledat, prev;
1969 switch (header >> 4) {
1971 prev = (c->status[channel].sample1 * 0x3c);
1974 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1977 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1983 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1985 byte = bytestream2_get_byteu(&gb);
1987 sampledat = sign_extend(byte, 4);
1989 sampledat = sign_extend(byte >> 4, 4);
1991 sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1992 *samples++ = av_clip_int16(sampledat >> 6);
1993 c->status[channel].sample2 = c->status[channel].sample1;
1994 c->status[channel].sample1 = sampledat;
1998 bytestream2_seek(&gb, 0, SEEK_SET);
2001 case AV_CODEC_ID_ADPCM_PSX:
2002 for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * avctx->channels); block++) {
2003 int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * avctx->channels) / (16 * avctx->channels);
2004 for (channel = 0; channel < avctx->channels; channel++) {
2005 samples = samples_p[channel] + block * nb_samples_per_block;
2006 av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2008 /* Read in every sample for this channel. */
2009 for (i = 0; i < nb_samples_per_block / 28; i++) {
2010 int filter, shift, flag, byte;
2012 filter = bytestream2_get_byteu(&gb);
2013 shift = filter & 0xf;
2014 filter = filter >> 4;
2015 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
2016 return AVERROR_INVALIDDATA;
2017 flag = bytestream2_get_byteu(&gb);
2019 /* Decode 28 samples. */
2020 for (n = 0; n < 28; n++) {
2021 int sample = 0, scale;
2025 scale = sign_extend(byte >> 4, 4);
2027 byte = bytestream2_get_byteu(&gb);
2028 scale = sign_extend(byte, 4);
2031 scale = scale * (1 << 12);
2032 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2034 *samples++ = av_clip_int16(sample);
2035 c->status[channel].sample2 = c->status[channel].sample1;
2036 c->status[channel].sample1 = sample;
2042 case AV_CODEC_ID_ADPCM_ARGO:
2044 * The format of each block:
2045 * uint8_t left_control;
2046 * uint4_t left_samples[nb_samples];
2047 * ---- and if stereo ----
2048 * uint8_t right_control;
2049 * uint4_t right_samples[nb_samples];
2051 * Format of the control byte:
2052 * MSB [SSSSRDRR] LSB
2053 * S = (Shift Amount - 2)
2057 * Each block relies on the previous two samples of each channel.
2058 * They should be 0 initially.
2060 for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2061 for (channel = 0; channel < avctx->channels; channel++) {
2064 samples = samples_p[channel] + block * 32;
2065 cs = c->status + channel;
2067 /* Get the control byte and decode the samples, 2 at a time. */
2068 control = bytestream2_get_byteu(&gb);
2069 shift = (control >> 4) + 2;
2071 for (n = 0; n < 16; n++) {
2072 int sample = bytestream2_get_byteu(&gb);
2073 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2074 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2079 case AV_CODEC_ID_ADPCM_ZORK:
2080 for (n = 0; n < nb_samples * avctx->channels; n++) {
2081 int v = bytestream2_get_byteu(&gb);
2082 *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
2085 case AV_CODEC_ID_ADPCM_IMA_MTF:
2086 for (n = nb_samples / 2; n > 0; n--) {
2087 for (channel = 0; channel < avctx->channels; channel++) {
2088 int v = bytestream2_get_byteu(&gb);
2089 *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2090 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2092 samples += avctx->channels;
2096 av_assert0(0); // unsupported codec_id should not happen
2099 if (avpkt->size && bytestream2_tell(&gb) == 0) {
2100 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2101 return AVERROR_INVALIDDATA;
2106 if (avpkt->size < bytestream2_tell(&gb)) {
2107 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2111 return bytestream2_tell(&gb);
2114 static void adpcm_flush(AVCodecContext *avctx)
2116 ADPCMDecodeContext *c = avctx->priv_data;
2118 switch(avctx->codec_id) {
2119 case AV_CODEC_ID_ADPCM_ZORK:
2120 for (int channel = 0; channel < avctx->channels; channel++) {
2121 c->status[channel].predictor = 0;
2122 c->status[channel].step_index = 0;
2127 /* Other codecs may want to handle this during decoding. */
2136 static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
2137 AV_SAMPLE_FMT_NONE };
2138 static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2139 AV_SAMPLE_FMT_NONE };
2140 static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2142 AV_SAMPLE_FMT_NONE };
2144 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2145 AVCodec ff_ ## name_ ## _decoder = { \
2147 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
2148 .type = AVMEDIA_TYPE_AUDIO, \
2150 .priv_data_size = sizeof(ADPCMDecodeContext), \
2151 .init = adpcm_decode_init, \
2152 .decode = adpcm_decode_frame, \
2153 .flush = adpcm_flush, \
2154 .capabilities = AV_CODEC_CAP_DR1, \
2155 .sample_fmts = sample_fmts_, \
2156 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
2159 /* Note: Do not forget to add new entries to the Makefile as well. */
2160 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
2161 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
2162 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
2163 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
2164 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO, sample_fmts_s16p, adpcm_argo, "ADPCM Argonaut Games");
2165 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
2166 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
2167 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
2168 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2169 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
2170 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
2171 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
2172 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
2173 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
2174 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
2175 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM, sample_fmts_s16, adpcm_ima_apm, "ADPCM IMA Ubisoft APM");
2176 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16p, adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2177 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
2178 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
2179 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
2180 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2181 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2182 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
2183 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MOFLEX, sample_fmts_s16p, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX");
2184 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF, sample_fmts_s16, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework");
2185 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
2186 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
2187 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
2188 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI, sample_fmts_s16, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive");
2189 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
2190 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP, sample_fmts_s16, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP");
2191 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
2192 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
2193 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
2194 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
2195 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
2196 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
2197 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
2198 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
2199 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
2200 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
2201 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
2202 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
2203 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");
2204 ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK, sample_fmts_s16, adpcm_zork, "ADPCM Zork");