2 * Copyright (c) 2001-2003 The FFmpeg Project
4 * first version by Francois Revol (revol@free.fr)
5 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6 * by Mike Melanson (melanson@pcisys.net)
7 * CD-ROM XA ADPCM codec by BERO
8 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
16 * This file is part of FFmpeg.
18 * FFmpeg is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
23 * FFmpeg is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with FFmpeg; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bytestream.h"
36 #include "adpcm_data.h"
42 * Features and limitations:
44 * Reference documents:
45 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48 * http://openquicktime.sourceforge.net/
49 * XAnim sources (xa_codec.c) http://xanim.polter.net/
50 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51 * SoX source code http://sox.sourceforge.net/
54 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56 * readstr http://www.geocities.co.jp/Playtown/2004/
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table
[5][2] = {
68 static const int ea_adpcm_table
[] = {
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables
[4][16] = {
79 /*3*/ { -1, -1, 2, 4 },
80 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
86 typedef struct ADPCMDecodeContext
{
87 ADPCMChannelStatus status
[6];
88 int vqa_version
; /**< VQA version. Used for ADPCM_IMA_WS */
91 static av_cold
int adpcm_decode_init(AVCodecContext
* avctx
)
93 ADPCMDecodeContext
*c
= avctx
->priv_data
;
94 unsigned int min_channels
= 1;
95 unsigned int max_channels
= 2;
97 switch(avctx
->codec
->id
) {
98 case AV_CODEC_ID_ADPCM_DTK
:
99 case AV_CODEC_ID_ADPCM_EA
:
102 case AV_CODEC_ID_ADPCM_AFC
:
103 case AV_CODEC_ID_ADPCM_EA_R1
:
104 case AV_CODEC_ID_ADPCM_EA_R2
:
105 case AV_CODEC_ID_ADPCM_EA_R3
:
106 case AV_CODEC_ID_ADPCM_EA_XAS
:
107 case AV_CODEC_ID_ADPCM_THP
:
111 if (avctx
->channels
< min_channels
|| avctx
->channels
> max_channels
) {
112 av_log(avctx
, AV_LOG_ERROR
, "Invalid number of channels\n");
113 return AVERROR(EINVAL
);
116 switch(avctx
->codec
->id
) {
117 case AV_CODEC_ID_ADPCM_CT
:
118 c
->status
[0].step
= c
->status
[1].step
= 511;
120 case AV_CODEC_ID_ADPCM_IMA_WAV
:
121 if (avctx
->bits_per_coded_sample
< 2 || avctx
->bits_per_coded_sample
> 5)
122 return AVERROR_INVALIDDATA
;
124 case AV_CODEC_ID_ADPCM_IMA_APC
:
125 if (avctx
->extradata
&& avctx
->extradata_size
>= 8) {
126 c
->status
[0].predictor
= AV_RL32(avctx
->extradata
);
127 c
->status
[1].predictor
= AV_RL32(avctx
->extradata
+ 4);
130 case AV_CODEC_ID_ADPCM_IMA_WS
:
131 if (avctx
->extradata
&& avctx
->extradata_size
>= 2)
132 c
->vqa_version
= AV_RL16(avctx
->extradata
);
138 switch(avctx
->codec
->id
) {
139 case AV_CODEC_ID_ADPCM_IMA_QT
:
140 case AV_CODEC_ID_ADPCM_IMA_WAV
:
141 case AV_CODEC_ID_ADPCM_4XM
:
142 case AV_CODEC_ID_ADPCM_XA
:
143 case AV_CODEC_ID_ADPCM_EA_R1
:
144 case AV_CODEC_ID_ADPCM_EA_R2
:
145 case AV_CODEC_ID_ADPCM_EA_R3
:
146 case AV_CODEC_ID_ADPCM_EA_XAS
:
147 case AV_CODEC_ID_ADPCM_THP
:
148 case AV_CODEC_ID_ADPCM_AFC
:
149 case AV_CODEC_ID_ADPCM_DTK
:
150 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16P
;
152 case AV_CODEC_ID_ADPCM_IMA_WS
:
153 avctx
->sample_fmt
= c
->vqa_version
== 3 ? AV_SAMPLE_FMT_S16P
:
157 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16
;
163 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus
*c
, char nibble
, int shift
)
167 int sign
, delta
, diff
, step
;
169 step
= ff_adpcm_step_table
[c
->step_index
];
170 step_index
= c
->step_index
+ ff_adpcm_index_table
[(unsigned)nibble
];
171 step_index
= av_clip(step_index
, 0, 88);
175 /* perform direct multiplication instead of series of jumps proposed by
176 * the reference ADPCM implementation since modern CPUs can do the mults
178 diff
= ((2 * delta
+ 1) * step
) >> shift
;
179 predictor
= c
->predictor
;
180 if (sign
) predictor
-= diff
;
181 else predictor
+= diff
;
183 c
->predictor
= av_clip_int16(predictor
);
184 c
->step_index
= step_index
;
186 return (short)c
->predictor
;
189 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus
*c
, GetBitContext
*gb
, int bps
)
191 int nibble
, step_index
, predictor
, sign
, delta
, diff
, step
, shift
;
194 nibble
= get_bits_le(gb
, bps
),
195 step
= ff_adpcm_step_table
[c
->step_index
];
196 step_index
= c
->step_index
+ ff_adpcm_index_tables
[bps
- 2][nibble
];
197 step_index
= av_clip(step_index
, 0, 88);
199 sign
= nibble
& (1 << shift
);
200 delta
= nibble
& ((1 << shift
) - 1);
201 diff
= ((2 * delta
+ 1) * step
) >> shift
;
202 predictor
= c
->predictor
;
203 if (sign
) predictor
-= diff
;
204 else predictor
+= diff
;
206 c
->predictor
= av_clip_int16(predictor
);
207 c
->step_index
= step_index
;
209 return (int16_t)c
->predictor
;
212 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus
*c
, int nibble
, int shift
)
218 step
= ff_adpcm_step_table
[c
->step_index
];
219 step_index
= c
->step_index
+ ff_adpcm_index_table
[nibble
];
220 step_index
= av_clip(step_index
, 0, 88);
223 if (nibble
& 4) diff
+= step
;
224 if (nibble
& 2) diff
+= step
>> 1;
225 if (nibble
& 1) diff
+= step
>> 2;
228 predictor
= c
->predictor
- diff
;
230 predictor
= c
->predictor
+ diff
;
232 c
->predictor
= av_clip_int16(predictor
);
233 c
->step_index
= step_index
;
238 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus
*c
, int nibble
)
242 predictor
= (((c
->sample1
) * (c
->coeff1
)) + ((c
->sample2
) * (c
->coeff2
))) / 64;
243 predictor
+= ((nibble
& 0x08)?(nibble
- 0x10):(nibble
)) * c
->idelta
;
245 c
->sample2
= c
->sample1
;
246 c
->sample1
= av_clip_int16(predictor
);
247 c
->idelta
= (ff_adpcm_AdaptationTable
[(int)nibble
] * c
->idelta
) >> 8;
248 if (c
->idelta
< 16) c
->idelta
= 16;
253 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus
*c
, int nibble
)
255 int step_index
, predictor
, sign
, delta
, diff
, step
;
257 step
= ff_adpcm_oki_step_table
[c
->step_index
];
258 step_index
= c
->step_index
+ ff_adpcm_index_table
[(unsigned)nibble
];
259 step_index
= av_clip(step_index
, 0, 48);
263 diff
= ((2 * delta
+ 1) * step
) >> 3;
264 predictor
= c
->predictor
;
265 if (sign
) predictor
-= diff
;
266 else predictor
+= diff
;
268 c
->predictor
= av_clip(predictor
, -2048, 2047);
269 c
->step_index
= step_index
;
271 return c
->predictor
<< 4;
274 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus
*c
, char nibble
)
276 int sign
, delta
, diff
;
281 /* perform direct multiplication instead of series of jumps proposed by
282 * the reference ADPCM implementation since modern CPUs can do the mults
284 diff
= ((2 * delta
+ 1) * c
->step
) >> 3;
285 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
286 c
->predictor
= ((c
->predictor
* 254) >> 8) + (sign
? -diff
: diff
);
287 c
->predictor
= av_clip_int16(c
->predictor
);
288 /* calculate new step and clamp it to range 511..32767 */
289 new_step
= (ff_adpcm_AdaptationTable
[nibble
& 7] * c
->step
) >> 8;
290 c
->step
= av_clip(new_step
, 511, 32767);
292 return (short)c
->predictor
;
295 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus
*c
, char nibble
, int size
, int shift
)
297 int sign
, delta
, diff
;
299 sign
= nibble
& (1<<(size
-1));
300 delta
= nibble
& ((1<<(size
-1))-1);
301 diff
= delta
<< (7 + c
->step
+ shift
);
304 c
->predictor
= av_clip(c
->predictor
+ (sign
? -diff
: diff
), -16384,16256);
306 /* calculate new step */
307 if (delta
>= (2*size
- 3) && c
->step
< 3)
309 else if (delta
== 0 && c
->step
> 0)
312 return (short) c
->predictor
;
315 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus
*c
, unsigned char nibble
)
322 c
->predictor
+= (c
->step
* ff_adpcm_yamaha_difflookup
[nibble
]) / 8;
323 c
->predictor
= av_clip_int16(c
->predictor
);
324 c
->step
= (c
->step
* ff_adpcm_yamaha_indexscale
[nibble
]) >> 8;
325 c
->step
= av_clip(c
->step
, 127, 24567);
329 static int xa_decode(AVCodecContext
*avctx
, int16_t *out0
, int16_t *out1
,
330 const uint8_t *in
, ADPCMChannelStatus
*left
,
331 ADPCMChannelStatus
*right
, int channels
, int sample_offset
)
334 int shift
,filter
,f0
,f1
;
338 out0
+= sample_offset
;
342 out1
+= sample_offset
;
345 shift
= 12 - (in
[4+i
*2] & 15);
346 filter
= in
[4+i
*2] >> 4;
347 if (filter
>= FF_ARRAY_ELEMS(xa_adpcm_table
)) {
348 avpriv_request_sample(avctx
, "unknown XA-ADPCM filter %d", filter
);
351 f0
= xa_adpcm_table
[filter
][0];
352 f1
= xa_adpcm_table
[filter
][1];
360 t
= sign_extend(d
, 4);
361 s
= ( t
<<shift
) + ((s_1
*f0
+ s_2
*f1
+32)>>6);
363 s_1
= av_clip_int16(s
);
370 s_1
= right
->sample1
;
371 s_2
= right
->sample2
;
374 shift
= 12 - (in
[5+i
*2] & 15);
375 filter
= in
[5+i
*2] >> 4;
376 if (filter
>= FF_ARRAY_ELEMS(xa_adpcm_table
)) {
377 avpriv_request_sample(avctx
, "unknown XA-ADPCM filter %d", filter
);
381 f0
= xa_adpcm_table
[filter
][0];
382 f1
= xa_adpcm_table
[filter
][1];
387 t
= sign_extend(d
>> 4, 4);
388 s
= ( t
<<shift
) + ((s_1
*f0
+ s_2
*f1
+32)>>6);
390 s_1
= av_clip_int16(s
);
395 right
->sample1
= s_1
;
396 right
->sample2
= s_2
;
402 out0
+= 28 * (3 - channels
);
403 out1
+= 28 * (3 - channels
);
409 static void adpcm_swf_decode(AVCodecContext
*avctx
, const uint8_t *buf
, int buf_size
, int16_t *samples
)
411 ADPCMDecodeContext
*c
= avctx
->priv_data
;
414 int k0
, signmask
, nb_bits
, count
;
415 int size
= buf_size
*8;
418 init_get_bits(&gb
, buf
, size
);
420 //read bits & initial values
421 nb_bits
= get_bits(&gb
, 2)+2;
422 table
= swf_index_tables
[nb_bits
-2];
423 k0
= 1 << (nb_bits
-2);
424 signmask
= 1 << (nb_bits
-1);
426 while (get_bits_count(&gb
) <= size
- 22*avctx
->channels
) {
427 for (i
= 0; i
< avctx
->channels
; i
++) {
428 *samples
++ = c
->status
[i
].predictor
= get_sbits(&gb
, 16);
429 c
->status
[i
].step_index
= get_bits(&gb
, 6);
432 for (count
= 0; get_bits_count(&gb
) <= size
- nb_bits
*avctx
->channels
&& count
< 4095; count
++) {
435 for (i
= 0; i
< avctx
->channels
; i
++) {
436 // similar to IMA adpcm
437 int delta
= get_bits(&gb
, nb_bits
);
438 int step
= ff_adpcm_step_table
[c
->status
[i
].step_index
];
439 long vpdiff
= 0; // vpdiff = (delta+0.5)*step/4
450 if (delta
& signmask
)
451 c
->status
[i
].predictor
-= vpdiff
;
453 c
->status
[i
].predictor
+= vpdiff
;
455 c
->status
[i
].step_index
+= table
[delta
& (~signmask
)];
457 c
->status
[i
].step_index
= av_clip(c
->status
[i
].step_index
, 0, 88);
458 c
->status
[i
].predictor
= av_clip_int16(c
->status
[i
].predictor
);
460 *samples
++ = c
->status
[i
].predictor
;
467 * Get the number of samples that will be decoded from the packet.
468 * In one case, this is actually the maximum number of samples possible to
469 * decode with the given buf_size.
471 * @param[out] coded_samples set to the number of samples as coded in the
472 * packet, or 0 if the codec does not encode the
473 * number of samples in each frame.
474 * @param[out] approx_nb_samples set to non-zero if the number of samples
475 * returned is an approximation.
477 static int get_nb_samples(AVCodecContext
*avctx
, GetByteContext
*gb
,
478 int buf_size
, int *coded_samples
, int *approx_nb_samples
)
480 ADPCMDecodeContext
*s
= avctx
->priv_data
;
482 int ch
= avctx
->channels
;
483 int has_coded_samples
= 0;
487 *approx_nb_samples
= 0;
492 switch (avctx
->codec
->id
) {
493 /* constant, only check buf_size */
494 case AV_CODEC_ID_ADPCM_EA_XAS
:
495 if (buf_size
< 76 * ch
)
499 case AV_CODEC_ID_ADPCM_IMA_QT
:
500 if (buf_size
< 34 * ch
)
504 /* simple 4-bit adpcm */
505 case AV_CODEC_ID_ADPCM_CT
:
506 case AV_CODEC_ID_ADPCM_IMA_APC
:
507 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD
:
508 case AV_CODEC_ID_ADPCM_IMA_OKI
:
509 case AV_CODEC_ID_ADPCM_IMA_WS
:
510 case AV_CODEC_ID_ADPCM_YAMAHA
:
511 nb_samples
= buf_size
* 2 / ch
;
517 /* simple 4-bit adpcm, with header */
519 switch (avctx
->codec
->id
) {
520 case AV_CODEC_ID_ADPCM_4XM
:
521 case AV_CODEC_ID_ADPCM_IMA_ISS
: header_size
= 4 * ch
; break;
522 case AV_CODEC_ID_ADPCM_IMA_AMV
: header_size
= 8; break;
523 case AV_CODEC_ID_ADPCM_IMA_SMJPEG
: header_size
= 4 * ch
; break;
526 return (buf_size
- header_size
) * 2 / ch
;
528 /* more complex formats */
529 switch (avctx
->codec
->id
) {
530 case AV_CODEC_ID_ADPCM_EA
:
531 has_coded_samples
= 1;
532 *coded_samples
= bytestream2_get_le32(gb
);
533 *coded_samples
-= *coded_samples
% 28;
534 nb_samples
= (buf_size
- 12) / 30 * 28;
536 case AV_CODEC_ID_ADPCM_IMA_EA_EACS
:
537 has_coded_samples
= 1;
538 *coded_samples
= bytestream2_get_le32(gb
);
539 nb_samples
= (buf_size
- (4 + 8 * ch
)) * 2 / ch
;
541 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA
:
542 nb_samples
= (buf_size
- ch
) / ch
* 2;
544 case AV_CODEC_ID_ADPCM_EA_R1
:
545 case AV_CODEC_ID_ADPCM_EA_R2
:
546 case AV_CODEC_ID_ADPCM_EA_R3
:
547 /* maximum number of samples */
548 /* has internal offsets and a per-frame switch to signal raw 16-bit */
549 has_coded_samples
= 1;
550 switch (avctx
->codec
->id
) {
551 case AV_CODEC_ID_ADPCM_EA_R1
:
552 header_size
= 4 + 9 * ch
;
553 *coded_samples
= bytestream2_get_le32(gb
);
555 case AV_CODEC_ID_ADPCM_EA_R2
:
556 header_size
= 4 + 5 * ch
;
557 *coded_samples
= bytestream2_get_le32(gb
);
559 case AV_CODEC_ID_ADPCM_EA_R3
:
560 header_size
= 4 + 5 * ch
;
561 *coded_samples
= bytestream2_get_be32(gb
);
564 *coded_samples
-= *coded_samples
% 28;
565 nb_samples
= (buf_size
- header_size
) * 2 / ch
;
566 nb_samples
-= nb_samples
% 28;
567 *approx_nb_samples
= 1;
569 case AV_CODEC_ID_ADPCM_IMA_DK3
:
570 if (avctx
->block_align
> 0)
571 buf_size
= FFMIN(buf_size
, avctx
->block_align
);
572 nb_samples
= ((buf_size
- 16) * 2 / 3 * 4) / ch
;
574 case AV_CODEC_ID_ADPCM_IMA_DK4
:
575 if (avctx
->block_align
> 0)
576 buf_size
= FFMIN(buf_size
, avctx
->block_align
);
577 nb_samples
= 1 + (buf_size
- 4 * ch
) * 2 / ch
;
579 case AV_CODEC_ID_ADPCM_IMA_RAD
:
580 if (avctx
->block_align
> 0)
581 buf_size
= FFMIN(buf_size
, avctx
->block_align
);
582 nb_samples
= (buf_size
- 4 * ch
) * 2 / ch
;
584 case AV_CODEC_ID_ADPCM_IMA_WAV
:
586 int bsize
= ff_adpcm_ima_block_sizes
[avctx
->bits_per_coded_sample
- 2];
587 int bsamples
= ff_adpcm_ima_block_samples
[avctx
->bits_per_coded_sample
- 2];
588 if (avctx
->block_align
> 0)
589 buf_size
= FFMIN(buf_size
, avctx
->block_align
);
590 nb_samples
= 1 + (buf_size
- 4 * ch
) / (bsize
* ch
) * bsamples
;
593 case AV_CODEC_ID_ADPCM_MS
:
594 if (avctx
->block_align
> 0)
595 buf_size
= FFMIN(buf_size
, avctx
->block_align
);
596 nb_samples
= 2 + (buf_size
- 7 * ch
) * 2 / ch
;
598 case AV_CODEC_ID_ADPCM_SBPRO_2
:
599 case AV_CODEC_ID_ADPCM_SBPRO_3
:
600 case AV_CODEC_ID_ADPCM_SBPRO_4
:
602 int samples_per_byte
;
603 switch (avctx
->codec
->id
) {
604 case AV_CODEC_ID_ADPCM_SBPRO_2
: samples_per_byte
= 4; break;
605 case AV_CODEC_ID_ADPCM_SBPRO_3
: samples_per_byte
= 3; break;
606 case AV_CODEC_ID_ADPCM_SBPRO_4
: samples_per_byte
= 2; break;
608 if (!s
->status
[0].step_index
) {
612 nb_samples
+= buf_size
* samples_per_byte
/ ch
;
615 case AV_CODEC_ID_ADPCM_SWF
:
617 int buf_bits
= buf_size
* 8 - 2;
618 int nbits
= (bytestream2_get_byte(gb
) >> 6) + 2;
619 int block_hdr_size
= 22 * ch
;
620 int block_size
= block_hdr_size
+ nbits
* ch
* 4095;
621 int nblocks
= buf_bits
/ block_size
;
622 int bits_left
= buf_bits
- nblocks
* block_size
;
623 nb_samples
= nblocks
* 4096;
624 if (bits_left
>= block_hdr_size
)
625 nb_samples
+= 1 + (bits_left
- block_hdr_size
) / (nbits
* ch
);
628 case AV_CODEC_ID_ADPCM_THP
:
629 if (avctx
->extradata
) {
630 nb_samples
= buf_size
/ (8 * ch
) * 14;
633 has_coded_samples
= 1;
634 bytestream2_skip(gb
, 4); // channel size
635 *coded_samples
= bytestream2_get_be32(gb
);
636 *coded_samples
-= *coded_samples
% 14;
637 nb_samples
= (buf_size
- (8 + 36 * ch
)) / (8 * ch
) * 14;
639 case AV_CODEC_ID_ADPCM_AFC
:
640 nb_samples
= buf_size
/ (9 * ch
) * 16;
642 case AV_CODEC_ID_ADPCM_XA
:
643 nb_samples
= (buf_size
/ 128) * 224 / ch
;
645 case AV_CODEC_ID_ADPCM_DTK
:
646 nb_samples
= buf_size
/ (16 * ch
) * 28;
650 /* validate coded sample count */
651 if (has_coded_samples
&& (*coded_samples
<= 0 || *coded_samples
> nb_samples
))
652 return AVERROR_INVALIDDATA
;
657 static int adpcm_decode_frame(AVCodecContext
*avctx
, void *data
,
658 int *got_frame_ptr
, AVPacket
*avpkt
)
660 AVFrame
*frame
= data
;
661 const uint8_t *buf
= avpkt
->data
;
662 int buf_size
= avpkt
->size
;
663 ADPCMDecodeContext
*c
= avctx
->priv_data
;
664 ADPCMChannelStatus
*cs
;
665 int n
, m
, channel
, i
;
670 int nb_samples
, coded_samples
, approx_nb_samples
, ret
;
673 bytestream2_init(&gb
, buf
, buf_size
);
674 nb_samples
= get_nb_samples(avctx
, &gb
, buf_size
, &coded_samples
, &approx_nb_samples
);
675 if (nb_samples
<= 0) {
676 av_log(avctx
, AV_LOG_ERROR
, "invalid number of samples in packet\n");
677 return AVERROR_INVALIDDATA
;
680 /* get output buffer */
681 frame
->nb_samples
= nb_samples
;
682 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
684 samples
= (short *)frame
->data
[0];
685 samples_p
= (int16_t **)frame
->extended_data
;
687 /* use coded_samples when applicable */
688 /* it is always <= nb_samples, so the output buffer will be large enough */
690 if (!approx_nb_samples
&& coded_samples
!= nb_samples
)
691 av_log(avctx
, AV_LOG_WARNING
, "mismatch in coded sample count\n");
692 frame
->nb_samples
= nb_samples
= coded_samples
;
695 st
= avctx
->channels
== 2 ? 1 : 0;
697 switch(avctx
->codec
->id
) {
698 case AV_CODEC_ID_ADPCM_IMA_QT
:
699 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
700 Channel data is interleaved per-chunk. */
701 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
704 cs
= &(c
->status
[channel
]);
705 /* (pppppp) (piiiiiii) */
707 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
708 predictor
= sign_extend(bytestream2_get_be16u(&gb
), 16);
709 step_index
= predictor
& 0x7F;
712 if (cs
->step_index
== step_index
) {
713 int diff
= predictor
- cs
->predictor
;
720 cs
->step_index
= step_index
;
721 cs
->predictor
= predictor
;
724 if (cs
->step_index
> 88u){
725 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
726 channel
, cs
->step_index
);
727 return AVERROR_INVALIDDATA
;
730 samples
= samples_p
[channel
];
732 for (m
= 0; m
< 64; m
+= 2) {
733 int byte
= bytestream2_get_byteu(&gb
);
734 samples
[m
] = adpcm_ima_qt_expand_nibble(cs
, byte
& 0x0F, 3);
735 samples
[m
+ 1] = adpcm_ima_qt_expand_nibble(cs
, byte
>> 4 , 3);
739 case AV_CODEC_ID_ADPCM_IMA_WAV
:
740 for(i
=0; i
<avctx
->channels
; i
++){
741 cs
= &(c
->status
[i
]);
742 cs
->predictor
= samples_p
[i
][0] = sign_extend(bytestream2_get_le16u(&gb
), 16);
744 cs
->step_index
= sign_extend(bytestream2_get_le16u(&gb
), 16);
745 if (cs
->step_index
> 88u){
746 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
748 return AVERROR_INVALIDDATA
;
752 if (avctx
->bits_per_coded_sample
!= 4) {
753 int samples_per_block
= ff_adpcm_ima_block_samples
[avctx
->bits_per_coded_sample
- 2];
756 init_get_bits8(&g
, gb
.buffer
, bytestream2_get_bytes_left(&gb
));
757 for (n
= 0; n
< (nb_samples
- 1) / samples_per_block
; n
++) {
758 for (i
= 0; i
< avctx
->channels
; i
++) {
760 samples
= &samples_p
[i
][1 + n
* samples_per_block
];
761 for (m
= 0; m
< samples_per_block
; m
++) {
762 samples
[m
] = adpcm_ima_wav_expand_nibble(cs
, &g
,
763 avctx
->bits_per_coded_sample
);
767 bytestream2_skip(&gb
, avctx
->block_align
- avctx
->channels
* 4);
769 for (n
= 0; n
< (nb_samples
- 1) / 8; n
++) {
770 for (i
= 0; i
< avctx
->channels
; i
++) {
772 samples
= &samples_p
[i
][1 + n
* 8];
773 for (m
= 0; m
< 8; m
+= 2) {
774 int v
= bytestream2_get_byteu(&gb
);
775 samples
[m
] = adpcm_ima_expand_nibble(cs
, v
& 0x0F, 3);
776 samples
[m
+ 1] = adpcm_ima_expand_nibble(cs
, v
>> 4 , 3);
782 case AV_CODEC_ID_ADPCM_4XM
:
783 for (i
= 0; i
< avctx
->channels
; i
++)
784 c
->status
[i
].predictor
= sign_extend(bytestream2_get_le16u(&gb
), 16);
786 for (i
= 0; i
< avctx
->channels
; i
++) {
787 c
->status
[i
].step_index
= sign_extend(bytestream2_get_le16u(&gb
), 16);
788 if (c
->status
[i
].step_index
> 88u) {
789 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
790 i
, c
->status
[i
].step_index
);
791 return AVERROR_INVALIDDATA
;
795 for (i
= 0; i
< avctx
->channels
; i
++) {
796 samples
= (int16_t *)frame
->data
[i
];
798 for (n
= nb_samples
>> 1; n
> 0; n
--) {
799 int v
= bytestream2_get_byteu(&gb
);
800 *samples
++ = adpcm_ima_expand_nibble(cs
, v
& 0x0F, 4);
801 *samples
++ = adpcm_ima_expand_nibble(cs
, v
>> 4 , 4);
805 case AV_CODEC_ID_ADPCM_MS
:
809 block_predictor
= bytestream2_get_byteu(&gb
);
810 if (block_predictor
> 6) {
811 av_log(avctx
, AV_LOG_ERROR
, "ERROR: block_predictor[0] = %d\n",
813 return AVERROR_INVALIDDATA
;
815 c
->status
[0].coeff1
= ff_adpcm_AdaptCoeff1
[block_predictor
];
816 c
->status
[0].coeff2
= ff_adpcm_AdaptCoeff2
[block_predictor
];
818 block_predictor
= bytestream2_get_byteu(&gb
);
819 if (block_predictor
> 6) {
820 av_log(avctx
, AV_LOG_ERROR
, "ERROR: block_predictor[1] = %d\n",
822 return AVERROR_INVALIDDATA
;
824 c
->status
[1].coeff1
= ff_adpcm_AdaptCoeff1
[block_predictor
];
825 c
->status
[1].coeff2
= ff_adpcm_AdaptCoeff2
[block_predictor
];
827 c
->status
[0].idelta
= sign_extend(bytestream2_get_le16u(&gb
), 16);
829 c
->status
[1].idelta
= sign_extend(bytestream2_get_le16u(&gb
), 16);
832 c
->status
[0].sample1
= sign_extend(bytestream2_get_le16u(&gb
), 16);
833 if (st
) c
->status
[1].sample1
= sign_extend(bytestream2_get_le16u(&gb
), 16);
834 c
->status
[0].sample2
= sign_extend(bytestream2_get_le16u(&gb
), 16);
835 if (st
) c
->status
[1].sample2
= sign_extend(bytestream2_get_le16u(&gb
), 16);
837 *samples
++ = c
->status
[0].sample2
;
838 if (st
) *samples
++ = c
->status
[1].sample2
;
839 *samples
++ = c
->status
[0].sample1
;
840 if (st
) *samples
++ = c
->status
[1].sample1
;
841 for(n
= (nb_samples
- 2) >> (1 - st
); n
> 0; n
--) {
842 int byte
= bytestream2_get_byteu(&gb
);
843 *samples
++ = adpcm_ms_expand_nibble(&c
->status
[0 ], byte
>> 4 );
844 *samples
++ = adpcm_ms_expand_nibble(&c
->status
[st
], byte
& 0x0F);
848 case AV_CODEC_ID_ADPCM_IMA_DK4
:
849 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
850 cs
= &c
->status
[channel
];
851 cs
->predictor
= *samples
++ = sign_extend(bytestream2_get_le16u(&gb
), 16);
852 cs
->step_index
= sign_extend(bytestream2_get_le16u(&gb
), 16);
853 if (cs
->step_index
> 88u){
854 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
855 channel
, cs
->step_index
);
856 return AVERROR_INVALIDDATA
;
859 for (n
= (nb_samples
- 1) >> (1 - st
); n
> 0; n
--) {
860 int v
= bytestream2_get_byteu(&gb
);
861 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0 ], v
>> 4 , 3);
862 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[st
], v
& 0x0F, 3);
865 case AV_CODEC_ID_ADPCM_IMA_DK3
:
869 int decode_top_nibble_next
= 0;
871 const int16_t *samples_end
= samples
+ avctx
->channels
* nb_samples
;
873 bytestream2_skipu(&gb
, 10);
874 c
->status
[0].predictor
= sign_extend(bytestream2_get_le16u(&gb
), 16);
875 c
->status
[1].predictor
= sign_extend(bytestream2_get_le16u(&gb
), 16);
876 c
->status
[0].step_index
= bytestream2_get_byteu(&gb
);
877 c
->status
[1].step_index
= bytestream2_get_byteu(&gb
);
878 if (c
->status
[0].step_index
> 88u || c
->status
[1].step_index
> 88u){
879 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index = %i/%i\n",
880 c
->status
[0].step_index
, c
->status
[1].step_index
);
881 return AVERROR_INVALIDDATA
;
883 /* sign extend the predictors */
884 diff_channel
= c
->status
[1].predictor
;
886 /* DK3 ADPCM support macro */
887 #define DK3_GET_NEXT_NIBBLE() \
888 if (decode_top_nibble_next) { \
889 nibble = last_byte >> 4; \
890 decode_top_nibble_next = 0; \
892 last_byte = bytestream2_get_byteu(&gb); \
893 nibble = last_byte & 0x0F; \
894 decode_top_nibble_next = 1; \
897 while (samples
< samples_end
) {
899 /* for this algorithm, c->status[0] is the sum channel and
900 * c->status[1] is the diff channel */
902 /* process the first predictor of the sum channel */
903 DK3_GET_NEXT_NIBBLE();
904 adpcm_ima_expand_nibble(&c
->status
[0], nibble
, 3);
906 /* process the diff channel predictor */
907 DK3_GET_NEXT_NIBBLE();
908 adpcm_ima_expand_nibble(&c
->status
[1], nibble
, 3);
910 /* process the first pair of stereo PCM samples */
911 diff_channel
= (diff_channel
+ c
->status
[1].predictor
) / 2;
912 *samples
++ = c
->status
[0].predictor
+ c
->status
[1].predictor
;
913 *samples
++ = c
->status
[0].predictor
- c
->status
[1].predictor
;
915 /* process the second predictor of the sum channel */
916 DK3_GET_NEXT_NIBBLE();
917 adpcm_ima_expand_nibble(&c
->status
[0], nibble
, 3);
919 /* process the second pair of stereo PCM samples */
920 diff_channel
= (diff_channel
+ c
->status
[1].predictor
) / 2;
921 *samples
++ = c
->status
[0].predictor
+ c
->status
[1].predictor
;
922 *samples
++ = c
->status
[0].predictor
- c
->status
[1].predictor
;
925 if ((bytestream2_tell(&gb
) & 1))
926 bytestream2_skip(&gb
, 1);
929 case AV_CODEC_ID_ADPCM_IMA_ISS
:
930 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
931 cs
= &c
->status
[channel
];
932 cs
->predictor
= sign_extend(bytestream2_get_le16u(&gb
), 16);
933 cs
->step_index
= sign_extend(bytestream2_get_le16u(&gb
), 16);
934 if (cs
->step_index
> 88u){
935 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
936 channel
, cs
->step_index
);
937 return AVERROR_INVALIDDATA
;
941 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
943 int v
= bytestream2_get_byteu(&gb
);
944 /* nibbles are swapped for mono */
952 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0 ], v1
, 3);
953 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[st
], v2
, 3);
956 case AV_CODEC_ID_ADPCM_IMA_APC
:
957 while (bytestream2_get_bytes_left(&gb
) > 0) {
958 int v
= bytestream2_get_byteu(&gb
);
959 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], v
>> 4 , 3);
960 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[st
], v
& 0x0F, 3);
963 case AV_CODEC_ID_ADPCM_IMA_OKI
:
964 while (bytestream2_get_bytes_left(&gb
) > 0) {
965 int v
= bytestream2_get_byteu(&gb
);
966 *samples
++ = adpcm_ima_oki_expand_nibble(&c
->status
[0], v
>> 4 );
967 *samples
++ = adpcm_ima_oki_expand_nibble(&c
->status
[st
], v
& 0x0F);
970 case AV_CODEC_ID_ADPCM_IMA_RAD
:
971 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
972 cs
= &c
->status
[channel
];
973 cs
->step_index
= sign_extend(bytestream2_get_le16u(&gb
), 16);
974 cs
->predictor
= sign_extend(bytestream2_get_le16u(&gb
), 16);
975 if (cs
->step_index
> 88u){
976 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
977 channel
, cs
->step_index
);
978 return AVERROR_INVALIDDATA
;
981 for (n
= 0; n
< nb_samples
/ 2; n
++) {
984 byte
[0] = bytestream2_get_byteu(&gb
);
986 byte
[1] = bytestream2_get_byteu(&gb
);
987 for(channel
= 0; channel
< avctx
->channels
; channel
++) {
988 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[channel
], byte
[channel
] & 0x0F, 3);
990 for(channel
= 0; channel
< avctx
->channels
; channel
++) {
991 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[channel
], byte
[channel
] >> 4 , 3);
995 case AV_CODEC_ID_ADPCM_IMA_WS
:
996 if (c
->vqa_version
== 3) {
997 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
998 int16_t *smp
= samples_p
[channel
];
1000 for (n
= nb_samples
/ 2; n
> 0; n
--) {
1001 int v
= bytestream2_get_byteu(&gb
);
1002 *smp
++ = adpcm_ima_expand_nibble(&c
->status
[channel
], v
>> 4 , 3);
1003 *smp
++ = adpcm_ima_expand_nibble(&c
->status
[channel
], v
& 0x0F, 3);
1007 for (n
= nb_samples
/ 2; n
> 0; n
--) {
1008 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
1009 int v
= bytestream2_get_byteu(&gb
);
1010 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[channel
], v
>> 4 , 3);
1011 samples
[st
] = adpcm_ima_expand_nibble(&c
->status
[channel
], v
& 0x0F, 3);
1013 samples
+= avctx
->channels
;
1016 bytestream2_seek(&gb
, 0, SEEK_END
);
1018 case AV_CODEC_ID_ADPCM_XA
:
1020 int16_t *out0
= samples_p
[0];
1021 int16_t *out1
= samples_p
[1];
1022 int samples_per_block
= 28 * (3 - avctx
->channels
) * 4;
1023 int sample_offset
= 0;
1024 while (bytestream2_get_bytes_left(&gb
) >= 128) {
1025 if ((ret
= xa_decode(avctx
, out0
, out1
, buf
+ bytestream2_tell(&gb
),
1026 &c
->status
[0], &c
->status
[1],
1027 avctx
->channels
, sample_offset
)) < 0)
1029 bytestream2_skipu(&gb
, 128);
1030 sample_offset
+= samples_per_block
;
1034 case AV_CODEC_ID_ADPCM_IMA_EA_EACS
:
1035 for (i
=0; i
<=st
; i
++) {
1036 c
->status
[i
].step_index
= bytestream2_get_le32u(&gb
);
1037 if (c
->status
[i
].step_index
> 88u) {
1038 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index[%d] = %i\n",
1039 i
, c
->status
[i
].step_index
);
1040 return AVERROR_INVALIDDATA
;
1043 for (i
=0; i
<=st
; i
++)
1044 c
->status
[i
].predictor
= bytestream2_get_le32u(&gb
);
1046 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1047 int byte
= bytestream2_get_byteu(&gb
);
1048 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], byte
>> 4, 3);
1049 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[st
], byte
& 0x0F, 3);
1052 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD
:
1053 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1054 int byte
= bytestream2_get_byteu(&gb
);
1055 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], byte
>> 4, 6);
1056 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[st
], byte
& 0x0F, 6);
1059 case AV_CODEC_ID_ADPCM_EA
:
1061 int previous_left_sample
, previous_right_sample
;
1062 int current_left_sample
, current_right_sample
;
1063 int next_left_sample
, next_right_sample
;
1064 int coeff1l
, coeff2l
, coeff1r
, coeff2r
;
1065 int shift_left
, shift_right
;
1067 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1068 each coding 28 stereo samples. */
1070 if(avctx
->channels
!= 2)
1071 return AVERROR_INVALIDDATA
;
1073 current_left_sample
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1074 previous_left_sample
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1075 current_right_sample
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1076 previous_right_sample
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1078 for (count1
= 0; count1
< nb_samples
/ 28; count1
++) {
1079 int byte
= bytestream2_get_byteu(&gb
);
1080 coeff1l
= ea_adpcm_table
[ byte
>> 4 ];
1081 coeff2l
= ea_adpcm_table
[(byte
>> 4 ) + 4];
1082 coeff1r
= ea_adpcm_table
[ byte
& 0x0F];
1083 coeff2r
= ea_adpcm_table
[(byte
& 0x0F) + 4];
1085 byte
= bytestream2_get_byteu(&gb
);
1086 shift_left
= 20 - (byte
>> 4);
1087 shift_right
= 20 - (byte
& 0x0F);
1089 for (count2
= 0; count2
< 28; count2
++) {
1090 byte
= bytestream2_get_byteu(&gb
);
1091 next_left_sample
= sign_extend(byte
>> 4, 4) << shift_left
;
1092 next_right_sample
= sign_extend(byte
, 4) << shift_right
;
1094 next_left_sample
= (next_left_sample
+
1095 (current_left_sample
* coeff1l
) +
1096 (previous_left_sample
* coeff2l
) + 0x80) >> 8;
1097 next_right_sample
= (next_right_sample
+
1098 (current_right_sample
* coeff1r
) +
1099 (previous_right_sample
* coeff2r
) + 0x80) >> 8;
1101 previous_left_sample
= current_left_sample
;
1102 current_left_sample
= av_clip_int16(next_left_sample
);
1103 previous_right_sample
= current_right_sample
;
1104 current_right_sample
= av_clip_int16(next_right_sample
);
1105 *samples
++ = current_left_sample
;
1106 *samples
++ = current_right_sample
;
1110 bytestream2_skip(&gb
, 2); // Skip terminating 0x0000
1114 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA
:
1116 int coeff
[2][2], shift
[2];
1118 for(channel
= 0; channel
< avctx
->channels
; channel
++) {
1119 int byte
= bytestream2_get_byteu(&gb
);
1121 coeff
[channel
][i
] = ea_adpcm_table
[(byte
>> 4) + 4*i
];
1122 shift
[channel
] = 20 - (byte
& 0x0F);
1124 for (count1
= 0; count1
< nb_samples
/ 2; count1
++) {
1127 byte
[0] = bytestream2_get_byteu(&gb
);
1128 if (st
) byte
[1] = bytestream2_get_byteu(&gb
);
1129 for(i
= 4; i
>= 0; i
-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1130 for(channel
= 0; channel
< avctx
->channels
; channel
++) {
1131 int sample
= sign_extend(byte
[channel
] >> i
, 4) << shift
[channel
];
1133 c
->status
[channel
].sample1
* coeff
[channel
][0] +
1134 c
->status
[channel
].sample2
* coeff
[channel
][1] + 0x80) >> 8;
1135 c
->status
[channel
].sample2
= c
->status
[channel
].sample1
;
1136 c
->status
[channel
].sample1
= av_clip_int16(sample
);
1137 *samples
++ = c
->status
[channel
].sample1
;
1141 bytestream2_seek(&gb
, 0, SEEK_END
);
1144 case AV_CODEC_ID_ADPCM_EA_R1
:
1145 case AV_CODEC_ID_ADPCM_EA_R2
:
1146 case AV_CODEC_ID_ADPCM_EA_R3
: {
1147 /* channel numbering
1149 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1150 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1151 const int big_endian
= avctx
->codec
->id
== AV_CODEC_ID_ADPCM_EA_R3
;
1152 int previous_sample
, current_sample
, next_sample
;
1155 unsigned int channel
;
1160 for (channel
=0; channel
<avctx
->channels
; channel
++)
1161 offsets
[channel
] = (big_endian
? bytestream2_get_be32(&gb
) :
1162 bytestream2_get_le32(&gb
)) +
1163 (avctx
->channels
+ 1) * 4;
1165 for (channel
=0; channel
<avctx
->channels
; channel
++) {
1166 bytestream2_seek(&gb
, offsets
[channel
], SEEK_SET
);
1167 samplesC
= samples_p
[channel
];
1169 if (avctx
->codec
->id
== AV_CODEC_ID_ADPCM_EA_R1
) {
1170 current_sample
= sign_extend(bytestream2_get_le16(&gb
), 16);
1171 previous_sample
= sign_extend(bytestream2_get_le16(&gb
), 16);
1173 current_sample
= c
->status
[channel
].predictor
;
1174 previous_sample
= c
->status
[channel
].prev_sample
;
1177 for (count1
= 0; count1
< nb_samples
/ 28; count1
++) {
1178 int byte
= bytestream2_get_byte(&gb
);
1179 if (byte
== 0xEE) { /* only seen in R2 and R3 */
1180 current_sample
= sign_extend(bytestream2_get_be16(&gb
), 16);
1181 previous_sample
= sign_extend(bytestream2_get_be16(&gb
), 16);
1183 for (count2
=0; count2
<28; count2
++)
1184 *samplesC
++ = sign_extend(bytestream2_get_be16(&gb
), 16);
1186 coeff1
= ea_adpcm_table
[ byte
>> 4 ];
1187 coeff2
= ea_adpcm_table
[(byte
>> 4) + 4];
1188 shift
= 20 - (byte
& 0x0F);
1190 for (count2
=0; count2
<28; count2
++) {
1192 next_sample
= sign_extend(byte
, 4) << shift
;
1194 byte
= bytestream2_get_byte(&gb
);
1195 next_sample
= sign_extend(byte
>> 4, 4) << shift
;
1198 next_sample
+= (current_sample
* coeff1
) +
1199 (previous_sample
* coeff2
);
1200 next_sample
= av_clip_int16(next_sample
>> 8);
1202 previous_sample
= current_sample
;
1203 current_sample
= next_sample
;
1204 *samplesC
++ = current_sample
;
1210 } else if (count
!= count1
) {
1211 av_log(avctx
, AV_LOG_WARNING
, "per-channel sample count mismatch\n");
1212 count
= FFMAX(count
, count1
);
1215 if (avctx
->codec
->id
!= AV_CODEC_ID_ADPCM_EA_R1
) {
1216 c
->status
[channel
].predictor
= current_sample
;
1217 c
->status
[channel
].prev_sample
= previous_sample
;
1221 frame
->nb_samples
= count
* 28;
1222 bytestream2_seek(&gb
, 0, SEEK_END
);
1225 case AV_CODEC_ID_ADPCM_EA_XAS
:
1226 for (channel
=0; channel
<avctx
->channels
; channel
++) {
1227 int coeff
[2][4], shift
[4];
1228 int16_t *s
= samples_p
[channel
];
1229 for (n
= 0; n
< 4; n
++, s
+= 32) {
1230 int val
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1232 coeff
[i
][n
] = ea_adpcm_table
[(val
&0x0F)+4*i
];
1235 val
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1236 shift
[n
] = 20 - (val
& 0x0F);
1240 for (m
=2; m
<32; m
+=2) {
1241 s
= &samples_p
[channel
][m
];
1242 for (n
= 0; n
< 4; n
++, s
+= 32) {
1244 int byte
= bytestream2_get_byteu(&gb
);
1246 level
= sign_extend(byte
>> 4, 4) << shift
[n
];
1247 pred
= s
[-1] * coeff
[0][n
] + s
[-2] * coeff
[1][n
];
1248 s
[0] = av_clip_int16((level
+ pred
+ 0x80) >> 8);
1250 level
= sign_extend(byte
, 4) << shift
[n
];
1251 pred
= s
[0] * coeff
[0][n
] + s
[-1] * coeff
[1][n
];
1252 s
[1] = av_clip_int16((level
+ pred
+ 0x80) >> 8);
1257 case AV_CODEC_ID_ADPCM_IMA_AMV
:
1258 c
->status
[0].predictor
= sign_extend(bytestream2_get_le16u(&gb
), 16);
1259 c
->status
[0].step_index
= bytestream2_get_le16u(&gb
);
1260 bytestream2_skipu(&gb
, 4);
1261 if (c
->status
[0].step_index
> 88u) {
1262 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index = %i\n",
1263 c
->status
[0].step_index
);
1264 return AVERROR_INVALIDDATA
;
1267 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1268 int v
= bytestream2_get_byteu(&gb
);
1270 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], v
>> 4, 3);
1271 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], v
& 0xf, 3);
1274 case AV_CODEC_ID_ADPCM_IMA_SMJPEG
:
1275 for (i
= 0; i
< avctx
->channels
; i
++) {
1276 c
->status
[i
].predictor
= sign_extend(bytestream2_get_be16u(&gb
), 16);
1277 c
->status
[i
].step_index
= bytestream2_get_byteu(&gb
);
1278 bytestream2_skipu(&gb
, 1);
1279 if (c
->status
[i
].step_index
> 88u) {
1280 av_log(avctx
, AV_LOG_ERROR
, "ERROR: step_index = %i\n",
1281 c
->status
[i
].step_index
);
1282 return AVERROR_INVALIDDATA
;
1286 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1287 int v
= bytestream2_get_byteu(&gb
);
1289 *samples
++ = adpcm_ima_qt_expand_nibble(&c
->status
[0 ], v
>> 4, 3);
1290 *samples
++ = adpcm_ima_qt_expand_nibble(&c
->status
[st
], v
& 0xf, 3);
1293 case AV_CODEC_ID_ADPCM_CT
:
1294 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1295 int v
= bytestream2_get_byteu(&gb
);
1296 *samples
++ = adpcm_ct_expand_nibble(&c
->status
[0 ], v
>> 4 );
1297 *samples
++ = adpcm_ct_expand_nibble(&c
->status
[st
], v
& 0x0F);
1300 case AV_CODEC_ID_ADPCM_SBPRO_4
:
1301 case AV_CODEC_ID_ADPCM_SBPRO_3
:
1302 case AV_CODEC_ID_ADPCM_SBPRO_2
:
1303 if (!c
->status
[0].step_index
) {
1304 /* the first byte is a raw sample */
1305 *samples
++ = 128 * (bytestream2_get_byteu(&gb
) - 0x80);
1307 *samples
++ = 128 * (bytestream2_get_byteu(&gb
) - 0x80);
1308 c
->status
[0].step_index
= 1;
1311 if (avctx
->codec
->id
== AV_CODEC_ID_ADPCM_SBPRO_4
) {
1312 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1313 int byte
= bytestream2_get_byteu(&gb
);
1314 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[0],
1316 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[st
],
1319 } else if (avctx
->codec
->id
== AV_CODEC_ID_ADPCM_SBPRO_3
) {
1320 for (n
= (nb_samples
<<st
) / 3; n
> 0; n
--) {
1321 int byte
= bytestream2_get_byteu(&gb
);
1322 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[0],
1324 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[0],
1325 (byte
>> 2) & 0x07, 3, 0);
1326 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[0],
1330 for (n
= nb_samples
>> (2 - st
); n
> 0; n
--) {
1331 int byte
= bytestream2_get_byteu(&gb
);
1332 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[0],
1334 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[st
],
1335 (byte
>> 4) & 0x03, 2, 2);
1336 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[0],
1337 (byte
>> 2) & 0x03, 2, 2);
1338 *samples
++ = adpcm_sbpro_expand_nibble(&c
->status
[st
],
1343 case AV_CODEC_ID_ADPCM_SWF
:
1344 adpcm_swf_decode(avctx
, buf
, buf_size
, samples
);
1345 bytestream2_seek(&gb
, 0, SEEK_END
);
1347 case AV_CODEC_ID_ADPCM_YAMAHA
:
1348 for (n
= nb_samples
>> (1 - st
); n
> 0; n
--) {
1349 int v
= bytestream2_get_byteu(&gb
);
1350 *samples
++ = adpcm_yamaha_expand_nibble(&c
->status
[0 ], v
& 0x0F);
1351 *samples
++ = adpcm_yamaha_expand_nibble(&c
->status
[st
], v
>> 4 );
1354 case AV_CODEC_ID_ADPCM_AFC
:
1356 int samples_per_block
;
1359 if (avctx
->extradata
&& avctx
->extradata_size
== 1 && avctx
->extradata
[0]) {
1360 samples_per_block
= avctx
->extradata
[0] / 16;
1361 blocks
= nb_samples
/ avctx
->extradata
[0];
1363 samples_per_block
= nb_samples
/ 16;
1367 for (m
= 0; m
< blocks
; m
++) {
1368 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
1369 int prev1
= c
->status
[channel
].sample1
;
1370 int prev2
= c
->status
[channel
].sample2
;
1372 samples
= samples_p
[channel
] + m
* 16;
1373 /* Read in every sample for this channel. */
1374 for (i
= 0; i
< samples_per_block
; i
++) {
1375 int byte
= bytestream2_get_byteu(&gb
);
1376 int scale
= 1 << (byte
>> 4);
1377 int index
= byte
& 0xf;
1378 int factor1
= ff_adpcm_afc_coeffs
[0][index
];
1379 int factor2
= ff_adpcm_afc_coeffs
[1][index
];
1381 /* Decode 16 samples. */
1382 for (n
= 0; n
< 16; n
++) {
1386 sampledat
= sign_extend(byte
, 4);
1388 byte
= bytestream2_get_byteu(&gb
);
1389 sampledat
= sign_extend(byte
>> 4, 4);
1392 sampledat
= ((prev1
* factor1
+ prev2
* factor2
) +
1393 ((sampledat
* scale
) << 11)) >> 11;
1394 *samples
= av_clip_int16(sampledat
);
1400 c
->status
[channel
].sample1
= prev1
;
1401 c
->status
[channel
].sample2
= prev2
;
1404 bytestream2_seek(&gb
, 0, SEEK_END
);
1407 case AV_CODEC_ID_ADPCM_THP
:
1412 if (avctx
->extradata
) {
1414 if (avctx
->extradata_size
< 32 * avctx
->channels
) {
1415 av_log(avctx
, AV_LOG_ERROR
, "Missing coeff table\n");
1416 return AVERROR_INVALIDDATA
;
1419 bytestream2_init(&tb
, avctx
->extradata
, avctx
->extradata_size
);
1420 for (i
= 0; i
< avctx
->channels
; i
++)
1421 for (n
= 0; n
< 16; n
++)
1422 table
[i
][n
] = sign_extend(bytestream2_get_be16u(&tb
), 16);
1424 for (i
= 0; i
< avctx
->channels
; i
++)
1425 for (n
= 0; n
< 16; n
++)
1426 table
[i
][n
] = sign_extend(bytestream2_get_be16u(&gb
), 16);
1428 /* Initialize the previous sample. */
1429 for (i
= 0; i
< avctx
->channels
; i
++) {
1430 c
->status
[i
].sample1
= sign_extend(bytestream2_get_be16u(&gb
), 16);
1431 c
->status
[i
].sample2
= sign_extend(bytestream2_get_be16u(&gb
), 16);
1435 for (ch
= 0; ch
< avctx
->channels
; ch
++) {
1436 samples
= samples_p
[ch
];
1438 /* Read in every sample for this channel. */
1439 for (i
= 0; i
< nb_samples
/ 14; i
++) {
1440 int byte
= bytestream2_get_byteu(&gb
);
1441 int index
= (byte
>> 4) & 7;
1442 unsigned int exp
= byte
& 0x0F;
1443 int factor1
= table
[ch
][index
* 2];
1444 int factor2
= table
[ch
][index
* 2 + 1];
1446 /* Decode 14 samples. */
1447 for (n
= 0; n
< 14; n
++) {
1451 sampledat
= sign_extend(byte
, 4);
1453 byte
= bytestream2_get_byteu(&gb
);
1454 sampledat
= sign_extend(byte
>> 4, 4);
1457 sampledat
= ((c
->status
[ch
].sample1
* factor1
1458 + c
->status
[ch
].sample2
* factor2
) >> 11) + (sampledat
<< exp
);
1459 *samples
= av_clip_int16(sampledat
);
1460 c
->status
[ch
].sample2
= c
->status
[ch
].sample1
;
1461 c
->status
[ch
].sample1
= *samples
++;
1467 case AV_CODEC_ID_ADPCM_DTK
:
1468 for (channel
= 0; channel
< avctx
->channels
; channel
++) {
1469 samples
= samples_p
[channel
];
1471 /* Read in every sample for this channel. */
1472 for (i
= 0; i
< nb_samples
/ 28; i
++) {
1475 bytestream2_skipu(&gb
, 1);
1476 header
= bytestream2_get_byteu(&gb
);
1477 bytestream2_skipu(&gb
, 3 - channel
);
1479 /* Decode 28 samples. */
1480 for (n
= 0; n
< 28; n
++) {
1481 int32_t sampledat
, prev
;
1483 switch (header
>> 4) {
1485 prev
= (c
->status
[channel
].sample1
* 0x3c);
1488 prev
= (c
->status
[channel
].sample1
* 0x73) - (c
->status
[channel
].sample2
* 0x34);
1491 prev
= (c
->status
[channel
].sample1
* 0x62) - (c
->status
[channel
].sample2
* 0x37);
1497 prev
= av_clip((prev
+ 0x20) >> 6, -0x200000, 0x1fffff);
1499 byte
= bytestream2_get_byteu(&gb
);
1501 sampledat
= sign_extend(byte
, 4);
1503 sampledat
= sign_extend(byte
>> 4, 4);
1505 sampledat
= (((sampledat
<< 12) >> (header
& 0xf)) << 6) + prev
;
1506 *samples
++ = av_clip_int16(sampledat
>> 6);
1507 c
->status
[channel
].sample2
= c
->status
[channel
].sample1
;
1508 c
->status
[channel
].sample1
= sampledat
;
1512 bytestream2_seek(&gb
, 0, SEEK_SET
);
1520 if (avpkt
->size
&& bytestream2_tell(&gb
) == 0) {
1521 av_log(avctx
, AV_LOG_ERROR
, "Nothing consumed\n");
1522 return AVERROR_INVALIDDATA
;
1527 return bytestream2_tell(&gb
);
1531 static const enum AVSampleFormat sample_fmts_s16
[] = { AV_SAMPLE_FMT_S16
,
1532 AV_SAMPLE_FMT_NONE
};
1533 static const enum AVSampleFormat sample_fmts_s16p
[] = { AV_SAMPLE_FMT_S16P
,
1534 AV_SAMPLE_FMT_NONE
};
1535 static const enum AVSampleFormat sample_fmts_both
[] = { AV_SAMPLE_FMT_S16
,
1537 AV_SAMPLE_FMT_NONE
};
1539 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1540 AVCodec ff_ ## name_ ## _decoder = { \
1542 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1543 .type = AVMEDIA_TYPE_AUDIO, \
1545 .priv_data_size = sizeof(ADPCMDecodeContext), \
1546 .init = adpcm_decode_init, \
1547 .decode = adpcm_decode_frame, \
1548 .capabilities = CODEC_CAP_DR1, \
1549 .sample_fmts = sample_fmts_, \
1552 /* Note: Do not forget to add new entries to the Makefile as well. */
1553 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM
, sample_fmts_s16p
, adpcm_4xm
, "ADPCM 4X Movie");
1554 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC
, sample_fmts_s16p
, adpcm_afc
, "ADPCM Nintendo Gamecube AFC");
1555 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT
, sample_fmts_s16
, adpcm_ct
, "ADPCM Creative Technology");
1556 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK
, sample_fmts_s16p
, adpcm_dtk
, "ADPCM Nintendo Gamecube DTK");
1557 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA
, sample_fmts_s16
, adpcm_ea
, "ADPCM Electronic Arts");
1558 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA
, sample_fmts_s16
, adpcm_ea_maxis_xa
, "ADPCM Electronic Arts Maxis CDROM XA");
1559 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1
, sample_fmts_s16p
, adpcm_ea_r1
, "ADPCM Electronic Arts R1");
1560 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2
, sample_fmts_s16p
, adpcm_ea_r2
, "ADPCM Electronic Arts R2");
1561 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3
, sample_fmts_s16p
, adpcm_ea_r3
, "ADPCM Electronic Arts R3");
1562 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS
, sample_fmts_s16p
, adpcm_ea_xas
, "ADPCM Electronic Arts XAS");
1563 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV
, sample_fmts_s16
, adpcm_ima_amv
, "ADPCM IMA AMV");
1564 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC
, sample_fmts_s16
, adpcm_ima_apc
, "ADPCM IMA CRYO APC");
1565 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3
, sample_fmts_s16
, adpcm_ima_dk3
, "ADPCM IMA Duck DK3");
1566 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4
, sample_fmts_s16
, adpcm_ima_dk4
, "ADPCM IMA Duck DK4");
1567 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS
, sample_fmts_s16
, adpcm_ima_ea_eacs
, "ADPCM IMA Electronic Arts EACS");
1568 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD
, sample_fmts_s16
, adpcm_ima_ea_sead
, "ADPCM IMA Electronic Arts SEAD");
1569 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS
, sample_fmts_s16
, adpcm_ima_iss
, "ADPCM IMA Funcom ISS");
1570 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI
, sample_fmts_s16
, adpcm_ima_oki
, "ADPCM IMA Dialogic OKI");
1571 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT
, sample_fmts_s16p
, adpcm_ima_qt
, "ADPCM IMA QuickTime");
1572 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD
, sample_fmts_s16
, adpcm_ima_rad
, "ADPCM IMA Radical");
1573 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG
, sample_fmts_s16
, adpcm_ima_smjpeg
, "ADPCM IMA Loki SDL MJPEG");
1574 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV
, sample_fmts_s16p
, adpcm_ima_wav
, "ADPCM IMA WAV");
1575 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS
, sample_fmts_both
, adpcm_ima_ws
, "ADPCM IMA Westwood");
1576 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS
, sample_fmts_s16
, adpcm_ms
, "ADPCM Microsoft");
1577 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2
, sample_fmts_s16
, adpcm_sbpro_2
, "ADPCM Sound Blaster Pro 2-bit");
1578 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3
, sample_fmts_s16
, adpcm_sbpro_3
, "ADPCM Sound Blaster Pro 2.6-bit");
1579 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4
, sample_fmts_s16
, adpcm_sbpro_4
, "ADPCM Sound Blaster Pro 4-bit");
1580 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF
, sample_fmts_s16
, adpcm_swf
, "ADPCM Shockwave Flash");
1581 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP
, sample_fmts_s16p
, adpcm_thp
, "ADPCM Nintendo Gamecube THP");
1582 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA
, sample_fmts_s16p
, adpcm_xa
, "ADPCM CDROM XA");
1583 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA
, sample_fmts_s16
, adpcm_yamaha
, "ADPCM Yamaha");