FFmpeg
Loading...
Searching...
No Matches
adpcm.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2001-2003 The FFmpeg project
3 *
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)
20 * Sanyo LD-ADPCM decoder by Peter Ross (pross@xvid.org)
21 *
22 * This file is part of FFmpeg.
23 *
24 * FFmpeg is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
28 *
29 * FFmpeg is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with FFmpeg; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 */
38
39#include "config_components.h"
40
41#include "avcodec.h"
42#include "get_bits.h"
43#include "bytestream.h"
44#include "adpcm.h"
45#include "adpcm_data.h"
46#include "codec_internal.h"
47#include "decode.h"
48
50
51/**
52 * @file
53 * ADPCM decoders
54 * Features and limitations:
55 *
56 * Reference documents:
57 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
58 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
59 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
60 * http://openquicktime.sourceforge.net/
61 * XAnim sources (xa_codec.c) http://xanim.polter.net/
62 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
63 * SoX source code http://sox.sourceforge.net/
64 *
65 * CD-ROM XA:
66 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
67 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
68 * readstr http://www.geocities.co.jp/Playtown/2004/
69 */
70
71#define CASE_0(codec_id, ...)
72#define CASE_1(codec_id, ...) \
73 case codec_id: \
74 { __VA_ARGS__ } \
75 break;
76#define CASE_2(enabled, codec_id, ...) \
77 CASE_ ## enabled(codec_id, __VA_ARGS__)
78#define CASE_3(config, codec_id, ...) \
79 CASE_2(config, codec_id, __VA_ARGS__)
80#define CASE(codec, ...) \
81 CASE_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
82
83/* These are for CD-ROM XA ADPCM */
84static const int8_t xa_adpcm_table[5][2] = {
85 { 0, 0 },
86 { 60, 0 },
87 { 115, -52 },
88 { 98, -55 },
89 { 122, -60 }
90};
91
92static const int16_t afc_coeffs[2][16] = {
93 { 0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024, -1024, -1024, -2048 },
94 { 0, 0, 2048, 1024, -2048, -1536, -1024, -2560, -2248, -2300, -3072, -2048, -1024, 1024, 0, 0 }
95};
96
97static const int16_t ea_adpcm_table[] = {
98 0, 240, 460, 392,
99 0, 0, -208, -220,
100 0, 1, 3, 4,
101 7, 8, 10, 11,
102 0, -1, -3, -4
103};
104
105/*
106 * Dumped from the binaries:
107 * - FantasticJourney.exe - 0x794D2, DGROUP:0x47A4D2
108 * - BigRaceUSA.exe - 0x9B8AA, DGROUP:0x49C4AA
109 * - Timeshock!.exe - 0x8506A, DGROUP:0x485C6A
110 */
111static const int8_t ima_cunning_index_table[9] = {
112 -1, -1, -1, -1, 1, 2, 3, 4, -1
113};
114
115/*
116 * Dumped from the binaries:
117 * - FantasticJourney.exe - 0x79458, DGROUP:0x47A458
118 * - BigRaceUSA.exe - 0x9B830, DGROUP:0x49C430
119 * - Timeshock!.exe - 0x84FF0, DGROUP:0x485BF0
120 */
121static const int16_t ima_cunning_step_table[61] = {
122 1, 1, 1, 1, 2, 2, 3, 3, 4, 5,
123 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
124 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
125 192, 224, 256, 320, 384, 448, 512, 640, 768, 896,
126 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, 4096, 5120,
127 6144, 7168, 8192, 10240, 12288, 14336, 16384, 20480, 24576, 28672, 0
128};
129
130static const int8_t adpcm_index_table2[4] = {
131 -1, 2,
132 -1, 2,
133};
134
135static const int8_t adpcm_index_table3[8] = {
136 -1, -1, 1, 2,
137 -1, -1, 1, 2,
138};
139
140static const int8_t adpcm_index_table5[32] = {
141 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
142 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
143};
144
145static const int8_t * const adpcm_index_tables[4] = {
150};
151
152static const int16_t mtaf_stepsize[32][16] = {
153 { 1, 5, 9, 13, 16, 20, 24, 28,
154 -1, -5, -9, -13, -16, -20, -24, -28, },
155 { 2, 6, 11, 15, 20, 24, 29, 33,
156 -2, -6, -11, -15, -20, -24, -29, -33, },
157 { 2, 7, 13, 18, 23, 28, 34, 39,
158 -2, -7, -13, -18, -23, -28, -34, -39, },
159 { 3, 9, 15, 21, 28, 34, 40, 46,
160 -3, -9, -15, -21, -28, -34, -40, -46, },
161 { 3, 11, 18, 26, 33, 41, 48, 56,
162 -3, -11, -18, -26, -33, -41, -48, -56, },
163 { 4, 13, 22, 31, 40, 49, 58, 67,
164 -4, -13, -22, -31, -40, -49, -58, -67, },
165 { 5, 16, 26, 37, 48, 59, 69, 80,
166 -5, -16, -26, -37, -48, -59, -69, -80, },
167 { 6, 19, 31, 44, 57, 70, 82, 95,
168 -6, -19, -31, -44, -57, -70, -82, -95, },
169 { 7, 22, 38, 53, 68, 83, 99, 114,
170 -7, -22, -38, -53, -68, -83, -99, -114, },
171 { 9, 27, 45, 63, 81, 99, 117, 135,
172 -9, -27, -45, -63, -81, -99, -117, -135, },
173 { 10, 32, 53, 75, 96, 118, 139, 161,
174 -10, -32, -53, -75, -96, -118, -139, -161, },
175 { 12, 38, 64, 90, 115, 141, 167, 193,
176 -12, -38, -64, -90, -115, -141, -167, -193, },
177 { 15, 45, 76, 106, 137, 167, 198, 228,
178 -15, -45, -76, -106, -137, -167, -198, -228, },
179 { 18, 54, 91, 127, 164, 200, 237, 273,
180 -18, -54, -91, -127, -164, -200, -237, -273, },
181 { 21, 65, 108, 152, 195, 239, 282, 326,
182 -21, -65, -108, -152, -195, -239, -282, -326, },
183 { 25, 77, 129, 181, 232, 284, 336, 388,
184 -25, -77, -129, -181, -232, -284, -336, -388, },
185 { 30, 92, 153, 215, 276, 338, 399, 461,
186 -30, -92, -153, -215, -276, -338, -399, -461, },
187 { 36, 109, 183, 256, 329, 402, 476, 549,
188 -36, -109, -183, -256, -329, -402, -476, -549, },
189 { 43, 130, 218, 305, 392, 479, 567, 654,
190 -43, -130, -218, -305, -392, -479, -567, -654, },
191 { 52, 156, 260, 364, 468, 572, 676, 780,
192 -52, -156, -260, -364, -468, -572, -676, -780, },
193 { 62, 186, 310, 434, 558, 682, 806, 930,
194 -62, -186, -310, -434, -558, -682, -806, -930, },
195 { 73, 221, 368, 516, 663, 811, 958, 1106,
196 -73, -221, -368, -516, -663, -811, -958, -1106, },
197 { 87, 263, 439, 615, 790, 966, 1142, 1318,
198 -87, -263, -439, -615, -790, -966, -1142, -1318, },
199 { 104, 314, 523, 733, 942, 1152, 1361, 1571,
200 -104, -314, -523, -733, -942, -1152, -1361, -1571, },
201 { 124, 374, 623, 873, 1122, 1372, 1621, 1871,
202 -124, -374, -623, -873, -1122, -1372, -1621, -1871, },
203 { 148, 445, 743, 1040, 1337, 1634, 1932, 2229,
204 -148, -445, -743, -1040, -1337, -1634, -1932, -2229, },
205 { 177, 531, 885, 1239, 1593, 1947, 2301, 2655,
206 -177, -531, -885, -1239, -1593, -1947, -2301, -2655, },
207 { 210, 632, 1053, 1475, 1896, 2318, 2739, 3161,
208 -210, -632, -1053, -1475, -1896, -2318, -2739, -3161, },
209 { 251, 753, 1255, 1757, 2260, 2762, 3264, 3766,
210 -251, -753, -1255, -1757, -2260, -2762, -3264, -3766, },
211 { 299, 897, 1495, 2093, 2692, 3290, 3888, 4486,
212 -299, -897, -1495, -2093, -2692, -3290, -3888, -4486, },
213 { 356, 1068, 1781, 2493, 3206, 3918, 4631, 5343,
214 -356, -1068, -1781, -2493, -3206, -3918, -4631, -5343, },
215 { 424, 1273, 2121, 2970, 3819, 4668, 5516, 6365,
216 -424, -1273, -2121, -2970, -3819, -4668, -5516, -6365, },
217};
218
219static const int16_t oki_step_table[49] = {
220 16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
221 41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
222 107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
223 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
224 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
225};
226
227// padded to zero where table size is less then 16
228static const int8_t swf_index_tables[4][16] = {
229 /*2*/ { -1, 2 },
230 /*3*/ { -1, -1, 2, 4 },
231 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
232 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
233};
234
235static const int8_t zork_index_table[8] = {
236 -1, -1, -1, 1, 4, 7, 10, 12,
237};
238
239static const int8_t mtf_index_table[16] = {
240 8, 6, 4, 2, -1, -1, -1, -1,
241 -1, -1, -1, -1, 2, 4, 6, 8,
242};
243
244static const int16_t rhetorex_step[128] = {
245 40, 130, 232, 350, 493, 673, 927, 1382,
246 60, 195, 348, 526, 740, 1010, 1391, 2074,
247 85, 277, 494, 745, 1048, 1431, 1971, 2938,
248 121, 391, 697, 1052, 1480, 2021, 2782, 4148,
249 176, 570, 1017, 1535, 2158, 2947, 4058, 6050,
250 257, 831, 1482, 2236, 3145, 4295, 5913, 8815,
251 373, 1206, 2151, 3245, 4564, 6232, 8579, 12791,
252 534, 1728, 3082, 4649, 6538, 8928, 12290, 18323,
253 782, 2527, 4507, 6798, 9560, 13055, 17971, 26793,
254 1115, 3603, 6426, 9692, 13631, 18614, 25623, 32767,
255 1620, 5233, 9334, 14078, 19799, 27036, 32767, 32767,
256 2361, 7630, 13608, 20526, 28866, 32767, 32767, 32767,
257 3447, 11136, 19860, 29955, 32767, 32767, 32767, 32767,
258 4865, 15717, 28031, 32767, 32767, 32767, 32767, 32767,
259 6888, 22255, 32767, 32767, 32767, 32767, 32767, 32767,
260 -10336, 32144, 5984,-24288, 4752, 24112, 24688,-26336,
261};
262
263static const int16_t rhetorex_index[8] = {
264 -83, -55, -27, 163, 374, 677, 1864, 3408,
265};
266
267/* end of tables */
268
269typedef struct ADPCMDecodeContext {
271 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
272 int has_status; /**< Status flag. Reset to 0 after a flush. */
274
275static void adpcm_flush(AVCodecContext *avctx);
276
278{
280 unsigned int min_channels = 1;
281 unsigned int max_channels = 2;
282
283 adpcm_flush(avctx);
284
285 switch(avctx->codec->id) {
289 max_channels = 1;
290 break;
292 max_channels = 2;
293 break;
300 max_channels = 6;
301 break;
303 min_channels = 2;
304 max_channels = 8;
305 if (avctx->ch_layout.nb_channels & 1) {
306 avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);
308 }
309 break;
311 min_channels = 2;
312 break;
314 max_channels = 8;
315 if (avctx->ch_layout.nb_channels <= 0 ||
316 avctx->block_align % (16 * avctx->ch_layout.nb_channels))
317 return AVERROR_INVALIDDATA;
318 break;
320 max_channels = 8;
321 if (avctx->ch_layout.nb_channels <= 0 || avctx->block_align <= 0 ||
322 avctx->block_align % avctx->ch_layout.nb_channels)
323 return AVERROR_INVALIDDATA;
324 break;
328 max_channels = 14;
329 break;
330 }
331 if (avctx->ch_layout.nb_channels < min_channels ||
332 avctx->ch_layout.nb_channels > max_channels) {
333 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
334 return AVERROR(EINVAL);
335 }
336
337 switch(avctx->codec->id) {
339 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
340 return AVERROR_INVALIDDATA;
341 break;
343 if (avctx->bits_per_coded_sample != 4 ||
344 avctx->block_align != 17 * avctx->ch_layout.nb_channels)
345 return AVERROR_INVALIDDATA;
346 break;
348 if (avctx->bits_per_coded_sample < 3 || avctx->bits_per_coded_sample > 5)
349 return AVERROR_INVALIDDATA;
350 break;
352 if (avctx->bits_per_coded_sample != 4)
353 return AVERROR_INVALIDDATA;
354 break;
356 if (avctx->bits_per_coded_sample != 8)
357 return AVERROR_INVALIDDATA;
358 break;
359 default:
360 break;
361 }
362
363 switch (avctx->codec->id) {
389 break;
391 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
393 break;
395 avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
397 break;
398 default:
400 }
401 return 0;
402}
403
404static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
405{
406 int delta, pred, step, add;
407
408 pred = c->predictor;
409 delta = nibble & 7;
410 step = c->step;
411 add = (delta * 2 + 1) * step;
412 if (add < 0)
413 add = add + 7;
414
415 if ((nibble & 8) == 0)
416 pred = av_clip(pred + (add >> 3), -32767, 32767);
417 else
418 pred = av_clip(pred - (add >> 3), -32767, 32767);
419
420 switch (delta) {
421 case 7:
422 step *= 0x99;
423 break;
424 case 6:
425 c->step = av_clip(c->step * 2, 127, 24576);
426 c->predictor = pred;
427 return pred;
428 case 5:
429 step *= 0x66;
430 break;
431 case 4:
432 step *= 0x4d;
433 break;
434 default:
435 step *= 0x39;
436 break;
437 }
438
439 if (step < 0)
440 step += 0x3f;
441
442 c->step = step >> 6;
443 c->step = av_clip(c->step, 127, 24576);
444 c->predictor = pred;
445 return pred;
446}
447
448static inline int16_t adpcm_ima_escape_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
449{
450 int step_index;
451 int predictor;
452 int sign, delta, diff, step;
453
454 step = ff_adpcm_step_table[c->step_index];
455 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
456 step_index = av_clip(step_index, 0, 88);
457
458 sign = nibble & 8;
459 delta = nibble & 7;
460 diff = (delta * step) >> 2;
461 predictor = c->predictor;
462 if (sign) predictor -= diff;
463 else predictor += diff;
464
465 c->predictor = av_clip_int16(predictor);
466 c->step_index = step_index;
467
468 return (int16_t)c->predictor;
469}
470
471static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
472{
473 int step_index;
474 int predictor;
475 int sign, delta, diff, step;
476
477 step = ff_adpcm_step_table[c->step_index];
478 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
479 step_index = av_clip(step_index, 0, 88);
480
481 sign = nibble & 8;
482 delta = nibble & 7;
483 /* perform direct multiplication instead of series of jumps proposed by
484 * the reference ADPCM implementation since modern CPUs can do the mults
485 * quickly enough */
486 diff = ((2 * delta + 1) * step) >> shift;
487 predictor = c->predictor;
488 if (sign) predictor -= diff;
489 else predictor += diff;
490
491 c->predictor = av_clip_int16(predictor);
492 c->step_index = step_index;
493
494 return (int16_t)c->predictor;
495}
496
497static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
498{
499 int step_index;
500 int predictor;
501 int sign, delta, diff, step;
502
503 step = ff_adpcm_step_table[c->step_index];
504 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
505 step_index = av_clip(step_index, 0, 88);
506
507 sign = nibble & 8;
508 delta = nibble & 7;
509 diff = (delta * step) >> shift;
510 predictor = c->predictor;
511 if (sign) predictor -= diff;
512 else predictor += diff;
513
514 c->predictor = av_clip_int16(predictor);
515 c->step_index = step_index;
516
517 return (int16_t)c->predictor;
518}
519
520static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
521{
522 int step_index, step, delta, predictor;
523
524 step = ff_adpcm_step_table[c->step_index];
525
526 delta = step * (2 * nibble - 15);
527 predictor = c->predictor + delta;
528
529 step_index = c->step_index + mtf_index_table[(unsigned)nibble];
530 c->predictor = av_clip_int16(predictor >> 4);
531 c->step_index = av_clip(step_index, 0, 88);
532
533 return (int16_t)c->predictor;
534}
535
536static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
537{
538 int step_index;
539 int predictor;
540 int step;
541
542 nibble = sign_extend(nibble & 0xF, 4);
543
544 step = ima_cunning_step_table[c->step_index];
545 step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
546 step_index = av_clip(step_index, 0, 60);
547
548 predictor = c->predictor + step * nibble;
549
550 c->predictor = av_clip_int16(predictor);
551 c->step_index = step_index;
552
553 return c->predictor;
554}
555
557{
558 int nibble, step_index, predictor, sign, delta, diff, step, shift;
559
560 shift = bps - 1;
561 nibble = get_bits_le(gb, bps),
562 step = ff_adpcm_step_table[c->step_index];
563 step_index = c->step_index + adpcm_index_tables[bps - 2][nibble];
564 step_index = av_clip(step_index, 0, 88);
565
566 sign = nibble & (1 << shift);
567 delta = av_zero_extend(nibble, shift);
568 diff = step >> shift;
569 for (int i = 0; i < shift; i++)
570 diff += (step >> (shift-1-i)) * !!(delta & (1 << i));
571 predictor = c->predictor;
572 if (sign) predictor -= diff;
573 else predictor += diff;
574
575 c->predictor = av_clip_int16(predictor);
576 c->step_index = step_index;
577
578 return (int16_t)c->predictor;
579}
580
582{
583 int step_index;
584 int predictor;
585 int diff, step;
586
587 step = ff_adpcm_step_table[c->step_index];
588 step_index = c->step_index + ff_adpcm_index_table[nibble];
589 step_index = av_clip(step_index, 0, 88);
590
591 diff = step >> 3;
592 if (nibble & 4) diff += step;
593 if (nibble & 2) diff += step >> 1;
594 if (nibble & 1) diff += step >> 2;
595
596 if (nibble & 8)
597 predictor = c->predictor - diff;
598 else
599 predictor = c->predictor + diff;
600
601 c->predictor = av_clip_int16(predictor);
602 c->step_index = step_index;
603
604 return c->predictor;
605}
606
607static void decode_adpcm_ima_hvqm2(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do,
608 int frame_format, GetByteContext *gb)
609{
611 int st = avctx->ch_layout.nb_channels == 2;
612 uint8_t nibble;
613
614 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
615 unsigned tmp;
616
617 switch (frame_format) {
618 case 0: /* combined hist+index */
619 tmp = bytestream2_get_be16(gb);
620 c->status[ch].predictor = sign_extend(tmp & 0xFF80, 16);
621 c->status[ch].step_index = tmp & 0x7f;
622 *outbuf++ = c->status[ch].predictor;
623 samples_to_do--;
624 break;
625 default:
626 break;
627 }
628
629 c->status[ch].step_index = av_clip(c->status[ch].step_index, 0, 88);
630 }
631
632 for (int i = 0; i < samples_to_do; i++) {
633 if (!(i&1)) {
634 nibble = bytestream2_get_byte(gb);
635 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], nibble >> 4);
636 } else {
637 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[ 0], nibble & 0xF);
638 }
639 }
640
641 bytestream2_seek(gb, 0, SEEK_END);
642}
643
644static void decode_adpcm_ima_hvqm4(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do,
645 int frame_format, GetByteContext *gb)
646{
648 int st = avctx->ch_layout.nb_channels == 2;
649 unsigned tmp;
650
651 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
652 switch (frame_format) {
653 case 1: /* combined hist+index */
654 tmp = bytestream2_get_be16(gb);
655 c->status[ch].predictor = sign_extend(tmp & 0xFF80, 16);
656 c->status[ch].step_index = tmp & 0x7f;
657 break;
658 case 2: /* no hist/index (continues from previous frame) */
659 default:
660 break;
661 case 3: /* separate hist+index */
662 tmp = bytestream2_get_be16(gb);
663 c->status[ch].predictor = sign_extend(tmp, 16);
664 c->status[ch].step_index = bytestream2_get_byte(gb);
665 break;
666 }
667
668 c->status[ch].step_index = av_clip(c->status[ch].step_index, 0, 88);
669 }
670
671 if (frame_format == 1 || frame_format == 3) {
672 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
673 *outbuf++ = (int16_t)c->status[st - ch].predictor;
674 samples_to_do--;
675 }
676
677 for (int i = 0; i < samples_to_do; i += 1+(!st)) {
678 uint8_t nibble = bytestream2_get_byte(gb);
679
680 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], nibble & 0xF);
681 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[ 0], nibble >> 4);
682 }
683
684 bytestream2_seek(gb, 0, SEEK_END);
685}
686
687static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
688{
689 int predictor;
690
691 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
692 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
693
694 c->sample2 = c->sample1;
695 c->sample1 = av_clip_int16(predictor);
696 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
697 if (c->idelta < 16) c->idelta = 16;
698 if (c->idelta > INT_MAX/768) {
699 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
700 c->idelta = INT_MAX/768;
701 }
702
703 return c->sample1;
704}
705
706static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
707{
708 int step_index, predictor, sign, delta, diff, step;
709
710 step = oki_step_table[c->step_index];
711 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
712 step_index = av_clip(step_index, 0, 48);
713
714 sign = nibble & 8;
715 delta = nibble & 7;
716 diff = ((2 * delta + 1) * step) >> 3;
717 predictor = c->predictor;
718 if (sign) predictor -= diff;
719 else predictor += diff;
720
721 c->predictor = av_clip_intp2(predictor, 11);
722 c->step_index = step_index;
723
724 return c->predictor * 16;
725}
726
727static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
728{
729 int sign, delta, diff;
730 int new_step;
731
732 sign = nibble & 8;
733 delta = nibble & 7;
734 /* perform direct multiplication instead of series of jumps proposed by
735 * the reference ADPCM implementation since modern CPUs can do the mults
736 * quickly enough */
737 diff = ((2 * delta + 1) * c->step) >> 3;
738 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
739 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
740 c->predictor = av_clip_int16(c->predictor);
741 /* calculate new step and clamp it to range 511..32767 */
742 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
743 c->step = av_clip(new_step, 511, 32767);
744
745 return (int16_t)c->predictor;
746}
747
748static inline int16_t adpcm_rhetorex_expand_nibble(ADPCMChannelStatus *c, uint16_t nibble)
749{
750 int delta, add, a, b;
751 int16_t sample;
752
753 delta = nibble & 7;
754 add = rhetorex_step[((c->step_index >> 8) & 0xF8) + delta];
755 if (nibble & 0x8)
756 add = -add;
757
758 sample = av_clip_int16(c->predictor + add);
759
760 a = 32768 * c->coeff1 + (add >> 1) * c->sample2;
761 c->coeff1 = av_clip_int16((a - ((a >> 7) & ~0xFF)) >> 15);
762
763 b = 32768 * c->coeff2 + (add >> 1) * c->sample1;
764 c->coeff2 = av_clip_int16((b - ((b >> 7) & ~0xFF) + 0x800000) >> 15);
765
766 c->predictor = av_clip_int16((c->coeff1 * c->sample1 + c->coeff2 * sample) >> 15);
767
768 c->sample2 = c->sample1;
769 c->sample1 = sample;
770
771 c->step_index = FFABS(64512 * c->step_index + 65536 * rhetorex_index[delta]) >> 16;
772
773 return sample;
774}
775
776static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
777{
778 int sign, delta, diff;
779
780 sign = nibble & (1<<(size-1));
781 delta = nibble & ((1<<(size-1))-1);
782 diff = delta << (7 + c->step + shift);
783
784 /* clamp result */
785 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
786
787 /* calculate new step */
788 if (delta >= (2*size - 3) && c->step < 3)
789 c->step++;
790 else if (delta == 0 && c->step > 0)
791 c->step--;
792
793 return (int16_t) c->predictor;
794}
795
796static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
797{
798 if(!c->step) {
799 c->predictor = 0;
800 c->step = 127;
801 }
802
803 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
804 c->predictor = av_clip_int16(c->predictor);
805 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
806 c->step = av_clip(c->step, 127, 24576);
807 return c->predictor;
808}
809
810static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
811{
812 c->predictor += mtaf_stepsize[c->step][nibble];
813 c->predictor = av_clip_int16(c->predictor);
814 c->step += ff_adpcm_index_table[nibble];
815 c->step = av_clip_uintp2(c->step, 5);
816 return c->predictor;
817}
818
819static inline int16_t adpcm_circus_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
820{
821 int32_t sample = c->predictor;
822 int32_t scale = c->step;
823 int32_t code = sign_extend(nibble, 8);
824
825 sample += code * (1 << scale);
826 if (code == 0) {
827 scale--;
828 } else if (code == 127 || code == -128) {
829 scale++;
830 }
831 scale = av_clip(scale, 0, 8);
833
834 c->predictor = sample;
835 c->step = scale;
836
837 return sample;
838}
839
840static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
841{
842 int16_t index = c->step_index;
843 uint32_t lookup_sample = ff_adpcm_step_table[index];
844 int32_t sample = 0;
845
846 if (nibble & 0x40)
847 sample += lookup_sample;
848 if (nibble & 0x20)
849 sample += lookup_sample >> 1;
850 if (nibble & 0x10)
851 sample += lookup_sample >> 2;
852 if (nibble & 0x08)
853 sample += lookup_sample >> 3;
854 if (nibble & 0x04)
855 sample += lookup_sample >> 4;
856 if (nibble & 0x02)
857 sample += lookup_sample >> 5;
858 if (nibble & 0x01)
859 sample += lookup_sample >> 6;
860 if (nibble & 0x80)
861 sample = -sample;
862
863 sample += c->predictor;
865
866 index += zork_index_table[(nibble >> 4) & 7];
867 index = av_clip(index, 0, 88);
868
869 c->predictor = sample;
870 c->step_index = index;
871
872 return sample;
873}
874
875static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
876 const uint8_t *in, ADPCMChannelStatus *left,
877 ADPCMChannelStatus *right, int channels, int sample_offset)
878{
879 int i, j;
880 int shift,filter,f0,f1;
881 int s_1,s_2;
882 int d,s,t;
883
884 out0 += sample_offset;
885 if (channels == 1)
886 out1 = out0 + 28;
887 else
888 out1 += sample_offset;
889
890 for(i=0;i<4;i++) {
891 shift = 12 - (in[4+i*2] & 15);
892 filter = in[4+i*2] >> 4;
894 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
895 filter=0;
896 }
897 if (shift < 0) {
898 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
899 shift = 0;
900 }
901 f0 = xa_adpcm_table[filter][0];
902 f1 = xa_adpcm_table[filter][1];
903
904 s_1 = left->sample1;
905 s_2 = left->sample2;
906
907 for(j=0;j<28;j++) {
908 d = in[16+i+j*4];
909
910 t = sign_extend(d, 4);
911 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
912 s_2 = s_1;
913 s_1 = av_clip_int16(s);
914 out0[j] = s_1;
915 }
916
917 if (channels == 2) {
918 left->sample1 = s_1;
919 left->sample2 = s_2;
920 s_1 = right->sample1;
921 s_2 = right->sample2;
922 }
923
924 shift = 12 - (in[5+i*2] & 15);
925 filter = in[5+i*2] >> 4;
926 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
927 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
928 filter=0;
929 }
930 if (shift < 0) {
931 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
932 shift = 0;
933 }
934
935 f0 = xa_adpcm_table[filter][0];
936 f1 = xa_adpcm_table[filter][1];
937
938 for(j=0;j<28;j++) {
939 d = in[16+i+j*4];
940
941 t = sign_extend(d >> 4, 4);
942 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
943 s_2 = s_1;
944 s_1 = av_clip_int16(s);
945 out1[j] = s_1;
946 }
947
948 if (channels == 2) {
949 right->sample1 = s_1;
950 right->sample2 = s_2;
951 } else {
952 left->sample1 = s_1;
953 left->sample2 = s_2;
954 }
955
956 out0 += 28 * (3 - channels);
957 out1 += 28 * (3 - channels);
958 }
959
960 return 0;
961}
962
963static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
964{
966 GetBitContext gb;
967 const int8_t *table;
968 int channels = avctx->ch_layout.nb_channels;
969 int k0, signmask, nb_bits, count;
970 int size = buf_size*8;
971 int i;
972
973 init_get_bits(&gb, buf, size);
974
975 //read bits & initial values
976 nb_bits = get_bits(&gb, 2)+2;
977 table = swf_index_tables[nb_bits-2];
978 k0 = 1 << (nb_bits-2);
979 signmask = 1 << (nb_bits-1);
980
981 while (get_bits_count(&gb) <= size - 22 * channels) {
982 for (i = 0; i < channels; i++) {
983 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
984 c->status[i].step_index = get_bits(&gb, 6);
985 }
986
987 for (count = 0; get_bits_count(&gb) <= size - nb_bits * channels && count < 4095; count++) {
988 int i;
989
990 for (i = 0; i < channels; i++) {
991 // similar to IMA adpcm
992 int delta = get_bits(&gb, nb_bits);
993 int step = ff_adpcm_step_table[c->status[i].step_index];
994 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
995 int k = k0;
996
997 do {
998 if (delta & k)
999 vpdiff += step;
1000 step >>= 1;
1001 k >>= 1;
1002 } while(k);
1003 vpdiff += step;
1004
1005 if (delta & signmask)
1006 c->status[i].predictor -= vpdiff;
1007 else
1008 c->status[i].predictor += vpdiff;
1009
1010 c->status[i].step_index += table[delta & (~signmask)];
1011
1012 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1013 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1014
1015 *samples++ = c->status[i].predictor;
1016 }
1017 }
1018 }
1019}
1020
1022{
1023 int sample = sign_extend(nibble, 4) * (1 << shift);
1024
1025 if (flag)
1026 sample += (8 * cs->sample1) - (4 * cs->sample2);
1027 else
1028 sample += 4 * cs->sample1;
1029
1030 sample = av_clip_int16(sample >> 2);
1031
1032 cs->sample2 = cs->sample1;
1033 cs->sample1 = sample;
1034
1035 return sample;
1036}
1037
1039{
1040 int sign, delta, add;
1041
1042 sign = bits & 4;
1043 if (sign)
1044 delta = 4 - (bits & 3);
1045 else
1046 delta = bits;
1047
1048 switch (delta) {
1049 case 0:
1050 add = 0;
1051 c->step = (3 * c->step) >> 2;
1052 break;
1053 case 1:
1054 add = c->step;
1055 c->step = (4 * c->step - (c->step >> 1)) >> 2;
1056 break;
1057 case 2:
1058 add = 2 * c->step;
1059 c->step = ((c->step >> 1) + add) >> 1;
1060 break;
1061 case 3:
1062 add = 4 * c->step - (c->step >> 1);
1063 c->step = 2 * c->step;
1064 break;
1065 case 4:
1066 add = (11 * c->step) >> 1;
1067 c->step = 3 * c->step;
1068 break;
1069 default:
1070 av_unreachable("There are cases for all control paths when bits is 3-bit");
1071 }
1072
1073 if (sign)
1074 add = -add;
1075
1076 c->predictor = av_clip_int16(c->predictor + add);
1077 c->step = av_clip(c->step, 1, 7281);
1078 return c->predictor;
1079}
1080
1082{
1083 int sign, delta, add;
1084
1085 sign = bits & 8;
1086 if (sign)
1087 delta = 8 - (bits & 7);
1088 else
1089 delta = bits;
1090
1091 switch (delta) {
1092 case 0:
1093 add = 0;
1094 c->step = (3 * c->step) >> 2;
1095 break;
1096 case 1:
1097 add = c->step;
1098 c->step = (3 * c->step) >> 2;
1099 break;
1100 case 2:
1101 add = 2 * c->step;
1102 break;
1103 case 3:
1104 add = 3 * c->step;
1105 break;
1106 case 4:
1107 add = 4 * c->step;
1108 break;
1109 case 5:
1110 add = (11 * c->step) >> 1;
1111 c->step += c->step >> 2;
1112 break;
1113 case 6:
1114 add = (15 * c->step) >> 1;
1115 c->step = 2 * c->step;
1116 break;
1117 case 7:
1118 if (sign)
1119 add = (19 * c->step) >> 1;
1120 else
1121 add = (21 * c->step) >> 1;
1122 c->step = (c->step >> 1) + 2 * c->step;
1123 break;
1124 case 8:
1125 add = (25 * c->step) >> 1;
1126 c->step = 5 * c->step;
1127 break;
1128 default:
1129 av_unreachable("There are cases for all control paths when bits is 4-bit");
1130 }
1131
1132 if (sign)
1133 add = -add;
1134
1135 c->predictor = av_clip_int16(c->predictor + add);
1136 c->step = av_clip(c->step, 1, 2621);
1137 return c->predictor;
1138}
1139
1141{
1142 int sign, delta, add;
1143
1144 sign = bits & 0x10;
1145 if (sign)
1146 delta = 16 - (bits & 0xF);
1147 else
1148 delta = bits;
1149
1150 add = delta * c->step;
1151 switch (delta) {
1152 case 0:
1153 c->step += (c->step >> 2) - (c->step >> 1);
1154 break;
1155 case 1:
1156 case 2:
1157 case 3:
1158 c->step += (c->step >> 3) - (c->step >> 2);
1159 break;
1160 case 4:
1161 case 5:
1162 c->step += (c->step >> 4) - (c->step >> 3);
1163 break;
1164 case 6:
1165 break;
1166 case 7:
1167 c->step += c->step >> 3;
1168 break;
1169 case 8:
1170 c->step += c->step >> 2;
1171 break;
1172 case 9:
1173 c->step += c->step >> 1;
1174 break;
1175 case 10:
1176 c->step = 2 * c->step - (c->step >> 3);
1177 break;
1178 case 11:
1179 c->step = 2 * c->step + (c->step >> 3);
1180 break;
1181 case 12:
1182 c->step = 2 * c->step + (c->step >> 1) - (c->step >> 3);
1183 break;
1184 case 13:
1185 c->step = 3 * c->step - (c->step >> 2);
1186 break;
1187 case 14:
1188 c->step *= 3;
1189 break;
1190 case 15:
1191 case 16:
1192 c->step = (7 * c->step) >> 1;
1193 break;
1194 }
1195
1196 if (sign)
1197 add = -add;
1198
1199 c->predictor = av_clip_int16(c->predictor + add);
1200 c->step = av_clip(c->step, 1, 1024);
1201 return c->predictor;
1202}
1203
1204/**
1205 * Get the number of samples (per channel) that will be decoded from the packet.
1206 * In one case, this is actually the maximum number of samples possible to
1207 * decode with the given buf_size.
1208 *
1209 * @param[out] coded_samples set to the number of samples as coded in the
1210 * packet, or 0 if the codec does not encode the
1211 * number of samples in each frame.
1212 * @param[out] approx_nb_samples set to non-zero if the number of samples
1213 * returned is an approximation.
1214 */
1216 int buf_size, int *coded_samples, int *approx_nb_samples)
1217{
1218 ADPCMDecodeContext *s = avctx->priv_data;
1219 int nb_samples = 0;
1220 int ch = avctx->ch_layout.nb_channels;
1221 int has_coded_samples = 0;
1222 int header_size;
1223
1224 *coded_samples = 0;
1225 *approx_nb_samples = 0;
1226
1227 if(ch <= 0)
1228 return 0;
1229 if (buf_size > INT_MAX / 14)
1230 return 0;
1231
1232 switch (avctx->codec->id) {
1233 /* constant, only check buf_size */
1235 if (buf_size < 76 * ch)
1236 return 0;
1237 nb_samples = 128;
1238 break;
1240 if (buf_size < 34 * ch)
1241 return 0;
1242 nb_samples = 64;
1243 break;
1245 nb_samples = (buf_size / 9) * 16;
1246 break;
1247 /* simple 4-bit adpcm */
1261 nb_samples = buf_size * 2 / ch;
1262 break;
1263 }
1264 if (nb_samples)
1265 return nb_samples;
1266
1267 /* simple 4-bit adpcm, with header */
1268 header_size = 0;
1269 switch (avctx->codec->id) {
1275 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
1276 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
1277 }
1278 if (header_size > 0)
1279 return (buf_size - header_size) * 2 / ch;
1280
1281 /* more complex formats */
1282 switch (avctx->codec->id) {
1284 bytestream2_skip(gb, 4);
1285 has_coded_samples = 1;
1286 *coded_samples = bytestream2_get_le32u(gb);
1287 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
1288 bytestream2_seek(gb, -8, SEEK_CUR);
1289 break;
1291 /* Stereo is 30 bytes per block */
1292 /* Mono is 15 bytes per block */
1293 has_coded_samples = 1;
1294 *coded_samples = bytestream2_get_le32(gb);
1295 *coded_samples -= *coded_samples % 28;
1296 nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
1297 break;
1299 nb_samples = ((bytestream2_peek_be64(gb) >> 16) & 0xFFFF);
1300 break;
1302 {
1303 int frame_format = bytestream2_get_be16(gb);
1304 int skip = 6;
1305
1306 if (frame_format == 1)
1307 skip += 2 * ch;
1308 if (frame_format == 3)
1309 skip += 3 * ch;
1310
1311 nb_samples = (buf_size - skip) * 2 / ch;
1312 bytestream2_seek(gb, 0, SEEK_SET);
1313 }
1314 break;
1316 has_coded_samples = 1;
1317 *coded_samples = bytestream2_get_le32(gb);
1318 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
1319 break;
1321 nb_samples = (buf_size - ch) / ch * 2;
1322 break;
1326 /* maximum number of samples */
1327 /* has internal offsets and a per-frame switch to signal raw 16-bit */
1328 has_coded_samples = 1;
1329 switch (avctx->codec->id) {
1331 header_size = 4 + 9 * ch;
1332 *coded_samples = bytestream2_get_le32(gb);
1333 break;
1335 header_size = 4 + 5 * ch;
1336 *coded_samples = bytestream2_get_le32(gb);
1337 break;
1339 header_size = 4 + 5 * ch;
1340 *coded_samples = bytestream2_get_be32(gb);
1341 break;
1342 }
1343 *coded_samples -= *coded_samples % 28;
1344 nb_samples = (buf_size - header_size) * 2 / ch;
1345 nb_samples -= nb_samples % 28;
1346 *approx_nb_samples = 1;
1347 break;
1349 if (avctx->block_align > 0)
1350 buf_size = FFMIN(buf_size, avctx->block_align);
1351 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
1352 break;
1354 if (avctx->block_align > 0)
1355 buf_size = FFMIN(buf_size, avctx->block_align);
1356 if (buf_size < 4 * ch)
1357 return AVERROR_INVALIDDATA;
1358 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
1359 break;
1361 if (avctx->block_align > 0)
1362 buf_size = FFMIN(buf_size, avctx->block_align);
1363 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1364 break;
1366 if (avctx->block_align > 0)
1367 buf_size = FFMIN(buf_size, avctx->block_align);
1368 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1369 break;
1371 if (avctx->block_align > 0)
1372 buf_size = FFMIN(buf_size, avctx->block_align);
1373 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1374 if (ch == 1) {
1375 avpriv_request_sample(avctx, "mono ADPCM Magix");
1376 return AVERROR_PATCHWELCOME;
1377 }
1378 break;
1379 CASE(ADPCM_IMA_WAV,
1380 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1381 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1382 if (avctx->block_align > 0)
1383 buf_size = FFMIN(buf_size, avctx->block_align);
1384 if (buf_size < 4 * ch)
1385 return AVERROR_INVALIDDATA;
1386 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
1387 ) /* End of CASE */
1388 CASE(ADPCM_IMA_XBOX,
1389 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1390 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1391 if (avctx->block_align > 0)
1392 buf_size = FFMIN(buf_size, avctx->block_align);
1393 if (buf_size < 4 * ch)
1394 return AVERROR_INVALIDDATA;
1395 nb_samples = (buf_size - 4 * ch) / (bsize * ch) * bsamples + 1;
1396 ) /* End of CASE */
1398 if (avctx->block_align > 0)
1399 buf_size = FFMIN(buf_size, avctx->block_align);
1400 nb_samples = (buf_size - 6 * ch) * 2 / ch;
1401 break;
1403 if (avctx->block_align > 0)
1404 buf_size = FFMIN(buf_size, avctx->block_align);
1405 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
1406 break;
1410 {
1411 int samples_per_byte;
1412 switch (avctx->codec->id) {
1413 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
1414 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
1415 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
1416 }
1417 if (!s->status[0].step_index) {
1418 if (buf_size < ch)
1419 return AVERROR_INVALIDDATA;
1420 nb_samples++;
1421 buf_size -= ch;
1422 }
1423 nb_samples += buf_size * samples_per_byte / ch;
1424 break;
1425 }
1427 {
1428 int buf_bits = buf_size * 8 - 2;
1429 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1430 int block_hdr_size = 22 * ch;
1431 int block_size = block_hdr_size + nbits * ch * 4095;
1432 int nblocks = buf_bits / block_size;
1433 int bits_left = buf_bits - nblocks * block_size;
1434 nb_samples = nblocks * 4096;
1435 if (bits_left >= block_hdr_size)
1436 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
1437 break;
1438 }
1441 if (avctx->extradata) {
1442 nb_samples = buf_size * 14 / (8 * ch);
1443 break;
1444 }
1445 has_coded_samples = 1;
1446 bytestream2_skip(gb, 4); // channel size
1447 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
1448 bytestream2_get_le32(gb) :
1449 bytestream2_get_be32(gb);
1450 buf_size -= 8 + 36 * ch;
1451 buf_size /= ch;
1452 nb_samples = buf_size / 8 * 14;
1453 if (buf_size % 8 > 1)
1454 nb_samples += (buf_size % 8 - 1) * 2;
1455 *approx_nb_samples = 1;
1456 break;
1458 nb_samples = buf_size / (9 * ch) * 16;
1459 break;
1461 nb_samples = (buf_size / 128) * 224 / ch;
1462 break;
1464 nb_samples = buf_size / (21 * ch) * 32;
1465 break;
1468 nb_samples = buf_size / (16 * ch) * 28;
1469 break;
1471 nb_samples = ((buf_size - 1) / ch) * 2;
1472 break;
1474 nb_samples = buf_size * 2;
1475 break;
1477 nb_samples = buf_size / avctx->block_align * 32;
1478 break;
1481 nb_samples = buf_size / ch;
1482 break;
1484 if (!avctx->extradata || avctx->extradata_size != 2)
1485 return AVERROR_INVALIDDATA;
1486 nb_samples = AV_RL16(avctx->extradata);
1487 break;
1488 }
1489
1490 /* validate coded sample count */
1491 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1492 return AVERROR_INVALIDDATA;
1493
1494 return nb_samples;
1495}
1496
1498 int *got_frame_ptr, AVPacket *avpkt)
1499{
1500 const uint8_t *buf = avpkt->data;
1501 int buf_size = avpkt->size;
1502 ADPCMDecodeContext *c = avctx->priv_data;
1503 int channels = avctx->ch_layout.nb_channels;
1504 int16_t *samples;
1505 int16_t **samples_p;
1506 int st; /* stereo */
1507 int nb_samples, coded_samples, approx_nb_samples, ret;
1508 GetByteContext gb;
1509
1510 bytestream2_init(&gb, buf, buf_size);
1511 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1512 if (nb_samples <= 0) {
1513 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
1514 return AVERROR_INVALIDDATA;
1515 }
1516
1517 /* get output buffer */
1518 frame->nb_samples = nb_samples;
1519 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1520 return ret;
1521 samples = (int16_t *)frame->data[0];
1522 samples_p = (int16_t **)frame->extended_data;
1523
1524 /* use coded_samples when applicable */
1525 /* it is always <= nb_samples, so the output buffer will be large enough */
1526 if (coded_samples) {
1527 if (!approx_nb_samples && coded_samples != nb_samples)
1528 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
1529 frame->nb_samples = nb_samples = coded_samples;
1530 }
1531
1532 st = channels == 2 ? 1 : 0;
1533
1534 switch(avctx->codec->id) {
1535 CASE(ADPCM_IMA_QT,
1536 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
1537 Channel data is interleaved per-chunk. */
1538 for (int channel = 0; channel < channels; channel++) {
1539 ADPCMChannelStatus *cs = &c->status[channel];
1540 int predictor;
1541 int step_index;
1542 /* (pppppp) (piiiiiii) */
1543
1544 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
1545 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1546 step_index = predictor & 0x7F;
1547 predictor &= ~0x7F;
1548
1549 if (cs->step_index == step_index) {
1550 int diff = predictor - cs->predictor;
1551 if (diff < 0)
1552 diff = - diff;
1553 if (diff > 0x7f)
1554 goto update;
1555 } else {
1556 update:
1557 cs->step_index = step_index;
1558 cs->predictor = predictor;
1559 }
1560
1561 if (cs->step_index > 88u){
1562 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1563 channel, cs->step_index);
1564 return AVERROR_INVALIDDATA;
1565 }
1566
1567 samples = samples_p[channel];
1568
1569 for (int m = 0; m < 64; m += 2) {
1570 int byte = bytestream2_get_byteu(&gb);
1571 samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1572 samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1573 }
1574 }
1575 ) /* End of CASE */
1576 CASE(ADPCM_IMA_WAV,
1577 for (int i = 0; i < channels; i++) {
1578 ADPCMChannelStatus *cs = &c->status[i];
1579 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1580
1581 cs->step_index = bytestream2_get_byteu(&gb);
1582 bytestream2_skipu(&gb, 1);
1583 if (cs->step_index > 88u){
1584 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1585 i, cs->step_index);
1586 return AVERROR_INVALIDDATA;
1587 }
1588 }
1589
1590 if (avctx->bits_per_coded_sample != 4) {
1591 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1592 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1593 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1595
1596 for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1597 for (int i = 0; i < channels; i++) {
1598 ADPCMChannelStatus *cs = &c->status[i];
1599 samples = &samples_p[i][1 + n * samples_per_block];
1600 for (int j = 0; j < block_size; j++) {
1601 temp[j] = buf[4 * channels + block_size * n * channels +
1602 (j % 4) + (j / 4) * (channels * 4) + i * 4];
1603 }
1604 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1605 if (ret < 0)
1606 return ret;
1607 for (int m = 0; m < samples_per_block; m++) {
1608 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1609 avctx->bits_per_coded_sample);
1610 }
1611 }
1612 }
1613 bytestream2_skip(&gb, avctx->block_align - channels * 4);
1614 } else {
1615 for (int n = 0; n < (nb_samples - 1) / 8; n++) {
1616 for (int i = 0; i < channels; i++) {
1617 ADPCMChannelStatus *cs = &c->status[i];
1618 samples = &samples_p[i][1 + n * 8];
1619 for (int m = 0; m < 8; m += 2) {
1620 int v = bytestream2_get_byteu(&gb);
1621 samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, v & 0x0F);
1622 samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, v >> 4);
1623 }
1624 }
1625 }
1626 }
1627 ) /* End of CASE */
1628 CASE(ADPCM_IMA_XBOX,
1629 for (int i = 0; i < channels; i++) {
1630 ADPCMChannelStatus *cs = &c->status[i];
1631 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1632
1633 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1634 if (cs->step_index > 88u) {
1635 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1636 i, cs->step_index);
1637 return AVERROR_INVALIDDATA;
1638 }
1639 }
1640
1641 for (int n = 0; n < (nb_samples-1) / 8; n++) {
1642 for (int i = 0; i < channels; i++) {
1643 ADPCMChannelStatus *cs = &c->status[i];
1644 samples = &samples_p[i][1 + n * 8];
1645 for (int m = 0; m < 8; m += 2) {
1646 int v = bytestream2_get_byteu(&gb);
1647 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1648 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1649 }
1650 }
1651 }
1652 frame->nb_samples--;
1653 ) /* End of CASE */
1654 CASE(ADPCM_4XM,
1655 for (int i = 0; i < channels; i++)
1656 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1657
1658 for (int i = 0; i < channels; i++) {
1659 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1660 if (c->status[i].step_index > 88u) {
1661 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1662 i, c->status[i].step_index);
1663 return AVERROR_INVALIDDATA;
1664 }
1665 }
1666
1667 for (int i = 0; i < channels; i++) {
1668 ADPCMChannelStatus *cs = &c->status[i];
1669 samples = (int16_t *)frame->data[i];
1670 for (int n = nb_samples >> 1; n > 0; n--) {
1671 int v = bytestream2_get_byteu(&gb);
1672 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1673 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1674 }
1675 }
1676 ) /* End of CASE */
1677 CASE(ADPCM_AGM,
1678 for (int i = 0; i < channels; i++)
1679 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1680 for (int i = 0; i < channels; i++)
1681 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1682
1683 for (int n = 0; n < nb_samples >> (1 - st); n++) {
1684 int v = bytestream2_get_byteu(&gb);
1685 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1686 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1687 }
1688 ) /* End of CASE */
1689 CASE(ADPCM_MS,
1690 int block_predictor;
1691
1692 if (avctx->ch_layout.nb_channels > 2) {
1693 for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
1694 samples = samples_p[channel];
1695 block_predictor = bytestream2_get_byteu(&gb);
1696 if (block_predictor > 6) {
1697 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1698 channel, block_predictor);
1699 return AVERROR_INVALIDDATA;
1700 }
1701 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1702 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1703 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1704 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1705 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1706 *samples++ = c->status[channel].sample2;
1707 *samples++ = c->status[channel].sample1;
1708 for (int n = (nb_samples - 2) >> 1; n > 0; n--) {
1709 int byte = bytestream2_get_byteu(&gb);
1710 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1711 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1712 }
1713 }
1714 } else {
1715 block_predictor = bytestream2_get_byteu(&gb);
1716 if (block_predictor > 6) {
1717 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1718 block_predictor);
1719 return AVERROR_INVALIDDATA;
1720 }
1721 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1722 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1723 if (st) {
1724 block_predictor = bytestream2_get_byteu(&gb);
1725 if (block_predictor > 6) {
1726 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1727 block_predictor);
1728 return AVERROR_INVALIDDATA;
1729 }
1730 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1731 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1732 }
1733 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1734 if (st){
1735 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1736 }
1737
1738 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1739 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1740 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1741 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1742
1743 *samples++ = c->status[0].sample2;
1744 if (st) *samples++ = c->status[1].sample2;
1745 *samples++ = c->status[0].sample1;
1746 if (st) *samples++ = c->status[1].sample1;
1747 for (int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1748 int byte = bytestream2_get_byteu(&gb);
1749 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1750 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1751 }
1752 }
1753 ) /* End of CASE */
1754 CASE(ADPCM_MTAF,
1755 for (int channel = 0; channel < channels; channel += 2) {
1756 bytestream2_skipu(&gb, 4);
1757 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1758 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1759 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1760 bytestream2_skipu(&gb, 2);
1761 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1762 bytestream2_skipu(&gb, 2);
1763 for (int n = 0; n < nb_samples; n += 2) {
1764 int v = bytestream2_get_byteu(&gb);
1765 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1766 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1767 }
1768 for (int n = 0; n < nb_samples; n += 2) {
1769 int v = bytestream2_get_byteu(&gb);
1770 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1771 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1772 }
1773 }
1774 ) /* End of CASE */
1775 CASE(ADPCM_IMA_DK4,
1776 for (int channel = 0; channel < channels; channel++) {
1777 ADPCMChannelStatus *cs = &c->status[channel];
1778 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1779 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1780 if (cs->step_index > 88u){
1781 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1782 channel, cs->step_index);
1783 return AVERROR_INVALIDDATA;
1784 }
1785 }
1786 for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1787 int v = bytestream2_get_byteu(&gb);
1788 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1789 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1790 }
1791 ) /* End of CASE */
1792
1793 /* DK3 ADPCM support macro */
1794#define DK3_GET_NEXT_NIBBLE() \
1795 if (decode_top_nibble_next) { \
1796 nibble = last_byte >> 4; \
1797 decode_top_nibble_next = 0; \
1798 } else { \
1799 last_byte = bytestream2_get_byteu(&gb); \
1800 nibble = last_byte & 0x0F; \
1801 decode_top_nibble_next = 1; \
1802 }
1803 CASE(ADPCM_IMA_DK3,
1804 int last_byte = 0;
1805 int nibble;
1806 int decode_top_nibble_next = 0;
1807 int diff_channel;
1808 const int16_t *samples_end = samples + channels * nb_samples;
1809
1810 bytestream2_skipu(&gb, 10);
1811 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1812 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1813 c->status[0].step_index = bytestream2_get_byteu(&gb);
1814 c->status[1].step_index = bytestream2_get_byteu(&gb);
1815 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1816 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1817 c->status[0].step_index, c->status[1].step_index);
1818 return AVERROR_INVALIDDATA;
1819 }
1820 /* sign extend the predictors */
1821 diff_channel = c->status[1].predictor;
1822
1823 while (samples < samples_end) {
1824
1825 /* for this algorithm, c->status[0] is the sum channel and
1826 * c->status[1] is the diff channel */
1827
1828 /* process the first predictor of the sum channel */
1830 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1831
1832 /* process the diff channel predictor */
1834 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1835
1836 /* process the first pair of stereo PCM samples */
1837 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1838 *samples++ = c->status[0].predictor + c->status[1].predictor;
1839 *samples++ = c->status[0].predictor - c->status[1].predictor;
1840
1841 /* process the second predictor of the sum channel */
1843 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1844
1845 /* process the second pair of stereo PCM samples */
1846 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1847 *samples++ = c->status[0].predictor + c->status[1].predictor;
1848 *samples++ = c->status[0].predictor - c->status[1].predictor;
1849 }
1850
1851 if ((bytestream2_tell(&gb) & 1))
1852 bytestream2_skip(&gb, 1);
1853 ) /* End of CASE */
1854 CASE(ADPCM_IMA_MAGIX,
1855 for (int channel = 0; channel < channels; channel++) {
1856 ADPCMChannelStatus *cs = &c->status[channel];
1857 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1858 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1859 if (cs->step_index > 88u){
1860 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1861 channel, cs->step_index);
1862 return AVERROR_INVALIDDATA;
1863 }
1864 }
1865
1866 for (int m = 0; m < channels*nb_samples/16; m ++) {
1867 uint32_t v0 = bytestream2_get_le32u(&gb);
1868 uint32_t v1 = bytestream2_get_le32u(&gb);
1869
1870 for (int n = 8; n > 0; n--, v0 >>= 4, v1 >>= 4, samples += 2) {
1871 samples[0] = adpcm_ima_expand_nibble(&c->status[0], v0 & 15, 3);
1872 samples[1] = adpcm_ima_expand_nibble(&c->status[1], v1 & 15, 3);
1873 }
1874 }
1875 ) /* End of CASE */
1876 CASE(ADPCM_IMA_ISS,
1877 for (int channel = 0; channel < channels; channel++) {
1878 ADPCMChannelStatus *cs = &c->status[channel];
1879 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1880 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1881 if (cs->step_index > 88u){
1882 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1883 channel, cs->step_index);
1884 return AVERROR_INVALIDDATA;
1885 }
1886 }
1887
1888 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1889 int v1, v2;
1890 int v = bytestream2_get_byteu(&gb);
1891 /* nibbles are swapped for mono */
1892 if (st) {
1893 v1 = v >> 4;
1894 v2 = v & 0x0F;
1895 } else {
1896 v2 = v >> 4;
1897 v1 = v & 0x0F;
1898 }
1899 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1900 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1901 }
1902 ) /* End of CASE */
1903 CASE(ADPCM_IMA_MOFLEX,
1904 for (int channel = 0; channel < channels; channel++) {
1905 ADPCMChannelStatus *cs = &c->status[channel];
1906 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1907 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1908 if (cs->step_index > 88u){
1909 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1910 channel, cs->step_index);
1911 return AVERROR_INVALIDDATA;
1912 }
1913 }
1914
1915 for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1916 for (int channel = 0; channel < channels; channel++) {
1917 samples = samples_p[channel] + 256 * subframe;
1918 for (int n = 0; n < 256; n += 2) {
1919 int v = bytestream2_get_byteu(&gb);
1920 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1921 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1922 }
1923 }
1924 }
1925 ) /* End of CASE */
1926 CASE(ADPCM_IMA_DAT4,
1927 for (int channel = 0; channel < channels; channel++) {
1928 ADPCMChannelStatus *cs = &c->status[channel];
1929 samples = samples_p[channel];
1930 bytestream2_skip(&gb, 4);
1931 for (int n = 0; n < nb_samples; n += 2) {
1932 int v = bytestream2_get_byteu(&gb);
1933 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1934 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1935 }
1936 }
1937 ) /* End of CASE */
1938 CASE(ADPCM_IMA_APC,
1939 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1940 int v = bytestream2_get_byteu(&gb);
1941 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1942 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1943 }
1944 ) /* End of CASE */
1945 CASE(ADPCM_IMA_HVQM2,
1946 int format = bytestream2_get_be16(&gb);
1947
1948 bytestream2_skip(&gb, 4);
1949 decode_adpcm_ima_hvqm2(avctx, samples, nb_samples, format, &gb);
1950 ) /* End of CASE */
1951 CASE(ADPCM_IMA_HVQM4,
1952 int format = bytestream2_get_be16(&gb);
1953
1954 bytestream2_skip(&gb, 4);
1955 decode_adpcm_ima_hvqm4(avctx, samples, nb_samples, format, &gb);
1956 ) /* End of CASE */
1957 CASE(ADPCM_IMA_SSI,
1958 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1959 int v = bytestream2_get_byteu(&gb);
1960 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1961 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1962 }
1963 ) /* End of CASE */
1964 CASE(ADPCM_IMA_APM,
1965 for (int n = nb_samples / 2; n > 0; n--) {
1966 for (int channel = 0; channel < channels; channel++) {
1967 int v = bytestream2_get_byteu(&gb);
1968 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1969 samples[st] = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1970 }
1971 samples += channels;
1972 }
1973 ) /* End of CASE */
1974 CASE(ADPCM_IMA_ALP,
1975 for (int n = nb_samples / 2; n > 0; n--) {
1976 for (int channel = 0; channel < channels; channel++) {
1977 int v = bytestream2_get_byteu(&gb);
1978 *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1979 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1980 }
1981 samples += channels;
1982 }
1983 ) /* End of CASE */
1984 CASE(ADPCM_IMA_CUNNING,
1985 for (int channel = 0; channel < channels; channel++) {
1986 int16_t *smp = samples_p[channel];
1987 for (int n = 0; n < nb_samples / 2; n++) {
1988 int v = bytestream2_get_byteu(&gb);
1989 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1990 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1991 }
1992 }
1993 ) /* End of CASE */
1994 CASE(ADPCM_IMA_OKI,
1995 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1996 int v = bytestream2_get_byteu(&gb);
1997 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1998 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1999 }
2000 ) /* End of CASE */
2001 CASE(ADPCM_IMA_RAD,
2002 for (int channel = 0; channel < channels; channel++) {
2003 ADPCMChannelStatus *cs = &c->status[channel];
2004 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
2005 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2006 if (cs->step_index > 88u){
2007 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2008 channel, cs->step_index);
2009 return AVERROR_INVALIDDATA;
2010 }
2011 }
2012 for (int n = 0; n < nb_samples / 2; n++) {
2013 int byte[2];
2014
2015 byte[0] = bytestream2_get_byteu(&gb);
2016 if (st)
2017 byte[1] = bytestream2_get_byteu(&gb);
2018 for (int channel = 0; channel < channels; channel++) {
2019 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
2020 }
2021 for (int channel = 0; channel < channels; channel++) {
2022 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
2023 }
2024 }
2025 ) /* End of CASE */
2026 CASE(ADPCM_IMA_WS,
2027 if (c->vqa_version == 3) {
2028 for (int channel = 0; channel < channels; channel++) {
2029 int16_t *smp = samples_p[channel];
2030
2031 for (int n = nb_samples / 2; n > 0; n--) {
2032 int v = bytestream2_get_byteu(&gb);
2033 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
2034 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
2035 }
2036 }
2037 } else {
2038 for (int n = nb_samples / 2; n > 0; n--) {
2039 for (int channel = 0; channel < channels; channel++) {
2040 int v = bytestream2_get_byteu(&gb);
2041 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
2042 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
2043 }
2044 samples += channels;
2045 }
2046 }
2047 bytestream2_seek(&gb, 0, SEEK_END);
2048 ) /* End of CASE */
2049 CASE(ADPCM_XMD,
2050 int bytes_remaining, block = 0;
2051 while (bytestream2_get_bytes_left(&gb) >= 21 * channels) {
2052 for (int channel = 0; channel < channels; channel++) {
2053 int16_t *out = samples_p[channel] + block * 32;
2054 int16_t history[2];
2055 uint16_t scale;
2056
2057 history[1] = sign_extend(bytestream2_get_le16(&gb), 16);
2058 history[0] = sign_extend(bytestream2_get_le16(&gb), 16);
2059 scale = bytestream2_get_le16(&gb);
2060
2061 out[0] = history[1];
2062 out[1] = history[0];
2063
2064 for (int n = 0; n < 15; n++) {
2065 unsigned byte = bytestream2_get_byte(&gb);
2066 int32_t nibble[2];
2067
2068 nibble[0] = sign_extend(byte & 15, 4);
2069 nibble[1] = sign_extend(byte >> 4, 4);
2070
2071 out[2+n*2] = nibble[0]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
2072 history[1] = history[0];
2073 history[0] = out[2+n*2];
2074
2075 out[2+n*2+1] = nibble[1]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
2076 history[1] = history[0];
2077 history[0] = out[2+n*2+1];
2078 }
2079 }
2080
2081 block++;
2082 }
2083 bytes_remaining = bytestream2_get_bytes_left(&gb);
2084 if (bytes_remaining > 0) {
2085 bytestream2_skip(&gb, bytes_remaining);
2086 }
2087 ) /* End of CASE */
2088 CASE(ADPCM_XA,
2089 int16_t *out0 = samples_p[0];
2090 int16_t *out1 = samples_p[1];
2091 int samples_per_block = 28 * (3 - channels) * 4;
2092 int sample_offset = 0;
2093 int bytes_remaining;
2094 while (bytestream2_get_bytes_left(&gb) >= 128) {
2095 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
2096 &c->status[0], &c->status[1],
2097 channels, sample_offset)) < 0)
2098 return ret;
2099 bytestream2_skipu(&gb, 128);
2100 sample_offset += samples_per_block;
2101 }
2102 /* Less than a full block of data left, e.g. when reading from
2103 * 2324 byte per sector XA; the remainder is padding */
2104 bytes_remaining = bytestream2_get_bytes_left(&gb);
2105 if (bytes_remaining > 0) {
2106 bytestream2_skip(&gb, bytes_remaining);
2107 }
2108 ) /* End of CASE */
2109 CASE(ADPCM_IMA_ESCAPE,
2110 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2111 int byte = bytestream2_get_byteu(&gb);
2112 *samples++ = adpcm_ima_escape_expand_nibble(&c->status[0], byte >> 4);
2113 *samples++ = adpcm_ima_escape_expand_nibble(&c->status[st], byte & 0xF);
2114 }
2115 ) /* End of CASE */
2116 CASE(ADPCM_IMA_EA_EACS,
2117 for (int i = 0; i <= st; i++) {
2118 c->status[i].step_index = bytestream2_get_le32u(&gb);
2119 if (c->status[i].step_index > 88u) {
2120 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2121 i, c->status[i].step_index);
2122 return AVERROR_INVALIDDATA;
2123 }
2124 }
2125 for (int i = 0; i <= st; i++) {
2126 c->status[i].predictor = bytestream2_get_le32u(&gb);
2127 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
2128 return AVERROR_INVALIDDATA;
2129 }
2130
2131 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2132 int byte = bytestream2_get_byteu(&gb);
2133 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
2134 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
2135 }
2136 ) /* End of CASE */
2137 CASE(ADPCM_IMA_EA_SEAD,
2138 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2139 int byte = bytestream2_get_byteu(&gb);
2140 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
2141 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
2142 }
2143 ) /* End of CASE */
2144 CASE(ADPCM_EA,
2145 int previous_left_sample, previous_right_sample;
2146 int current_left_sample, current_right_sample;
2147 int next_left_sample, next_right_sample;
2148 int coeff1l, coeff2l, coeff1r, coeff2r;
2149 int shift_left, shift_right;
2150
2151 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte (stereo) or 15-byte (mono) pieces,
2152 each coding 28 stereo/mono samples. */
2153
2154 if (channels != 2 && channels != 1)
2155 return AVERROR_INVALIDDATA;
2156
2157 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2158 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2159 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2160 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2161
2162 for (int count1 = 0; count1 < nb_samples / 28; count1++) {
2163 int byte = bytestream2_get_byteu(&gb);
2164 coeff1l = ea_adpcm_table[ byte >> 4 ];
2165 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
2166 coeff1r = ea_adpcm_table[ byte & 0x0F];
2167 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
2168
2169 if (channels == 2){
2170 byte = bytestream2_get_byteu(&gb);
2171 shift_left = 20 - (byte >> 4);
2172 shift_right = 20 - (byte & 0x0F);
2173 } else{
2174 /* Mono packs the shift into the coefficient byte's lower nibble instead */
2175 shift_left = 20 - (byte & 0x0F);
2176 }
2177
2178 for (int count2 = 0; count2 < (channels == 2 ? 28 : 14); count2++) {
2179 byte = bytestream2_get_byteu(&gb);
2180 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
2181
2182 next_left_sample = (next_left_sample +
2183 (current_left_sample * coeff1l) +
2184 (previous_left_sample * coeff2l) + 0x80) >> 8;
2185
2186 previous_left_sample = current_left_sample;
2187 current_left_sample = av_clip_int16(next_left_sample);
2188 *samples++ = current_left_sample;
2189
2190 if (channels == 2){
2191 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
2192
2193 next_right_sample = (next_right_sample +
2194 (current_right_sample * coeff1r) +
2195 (previous_right_sample * coeff2r) + 0x80) >> 8;
2196
2197 previous_right_sample = current_right_sample;
2198 current_right_sample = av_clip_int16(next_right_sample);
2199 *samples++ = current_right_sample;
2200 } else {
2201 next_left_sample = sign_extend(byte, 4) * (1 << shift_left);
2202
2203 next_left_sample = (next_left_sample +
2204 (current_left_sample * coeff1l) +
2205 (previous_left_sample * coeff2l) + 0x80) >> 8;
2206
2207 previous_left_sample = current_left_sample;
2208 current_left_sample = av_clip_int16(next_left_sample);
2209
2210 *samples++ = current_left_sample;
2211 }
2212 }
2213 }
2214 bytestream2_skip(&gb, channels == 2 ? 2 : 3); // Skip terminating NULs
2215 ) /* End of CASE */
2216 CASE(ADPCM_EA_MAXIS_XA,
2217 int coeff[2][2], shift[2];
2218
2219 for (int channel = 0; channel < channels; channel++) {
2220 int byte = bytestream2_get_byteu(&gb);
2221 for (int i = 0; i < 2; i++)
2222 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
2223 shift[channel] = 20 - (byte & 0x0F);
2224 }
2225 for (int count1 = 0; count1 < nb_samples / 2; count1++) {
2226 int byte[2];
2227
2228 byte[0] = bytestream2_get_byteu(&gb);
2229 if (st) byte[1] = bytestream2_get_byteu(&gb);
2230 for (int i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
2231 for (int channel = 0; channel < channels; channel++) {
2232 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
2233 sample = (sample +
2234 c->status[channel].sample1 * coeff[channel][0] +
2235 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
2236 c->status[channel].sample2 = c->status[channel].sample1;
2237 c->status[channel].sample1 = av_clip_int16(sample);
2238 *samples++ = c->status[channel].sample1;
2239 }
2240 }
2241 }
2242 bytestream2_seek(&gb, 0, SEEK_END);
2243 ) /* End of CASE */
2244#if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER
2248 /* channel numbering
2249 2chan: 0=fl, 1=fr
2250 4chan: 0=fl, 1=rl, 2=fr, 3=rr
2251 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
2252 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
2253 int previous_sample, current_sample, next_sample;
2254 int coeff1, coeff2;
2255 int shift;
2256 uint16_t *samplesC;
2257 int count = 0;
2258 int offsets[6];
2259
2260 for (unsigned channel = 0; channel < channels; channel++)
2261 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
2262 bytestream2_get_le32(&gb)) +
2263 (channels + 1) * 4;
2264
2265 for (unsigned channel = 0; channel < channels; channel++) {
2266 int count1;
2267
2268 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
2269 samplesC = samples_p[channel];
2270
2271 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
2272 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2273 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2274 } else {
2275 current_sample = c->status[channel].predictor;
2276 previous_sample = c->status[channel].prev_sample;
2277 }
2278
2279 for (count1 = 0; count1 < nb_samples / 28; count1++) {
2280 int byte = bytestream2_get_byte(&gb);
2281 if (byte == 0xEE) { /* only seen in R2 and R3 */
2282 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2283 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2284
2285 for (int count2 = 0; count2 < 28; count2++)
2286 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
2287 } else {
2288 coeff1 = ea_adpcm_table[ byte >> 4 ];
2289 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
2290 shift = 20 - (byte & 0x0F);
2291
2292 for (int count2 = 0; count2 < 28; count2++) {
2293 if (count2 & 1)
2294 next_sample = (unsigned)sign_extend(byte, 4) << shift;
2295 else {
2296 byte = bytestream2_get_byte(&gb);
2297 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
2298 }
2299
2300 next_sample += (current_sample * coeff1) +
2301 (previous_sample * coeff2);
2302 next_sample = av_clip_int16(next_sample >> 8);
2303
2304 previous_sample = current_sample;
2305 current_sample = next_sample;
2306 *samplesC++ = current_sample;
2307 }
2308 }
2309 }
2310 if (!count) {
2311 count = count1;
2312 } else if (count != count1) {
2313 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
2314 count = FFMAX(count, count1);
2315 }
2316
2317 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
2318 c->status[channel].predictor = current_sample;
2319 c->status[channel].prev_sample = previous_sample;
2320 }
2321 }
2322
2323 frame->nb_samples = count * 28;
2324 bytestream2_seek(&gb, 0, SEEK_END);
2325 break;
2326 }
2327#endif /* CONFIG_ADPCM_EA_Rx_DECODER */
2328 CASE(ADPCM_EA_XAS,
2329 for (int channel=0; channel < channels; channel++) {
2330 int coeff[2][4], shift[4];
2331 int16_t *s = samples_p[channel];
2332 for (int n = 0; n < 4; n++, s += 32) {
2333 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
2334 for (int i = 0; i < 2; i++)
2335 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
2336 s[0] = val & ~0x0F;
2337
2338 val = sign_extend(bytestream2_get_le16u(&gb), 16);
2339 shift[n] = 20 - (val & 0x0F);
2340 s[1] = val & ~0x0F;
2341 }
2342
2343 for (int m = 2; m < 32; m += 2) {
2344 s = &samples_p[channel][m];
2345 for (int n = 0; n < 4; n++, s += 32) {
2346 int level, pred;
2347 int byte = bytestream2_get_byteu(&gb);
2348
2349 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
2350 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
2351 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
2352
2353 level = sign_extend(byte, 4) * (1 << shift[n]);
2354 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
2355 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
2356 }
2357 }
2358 }
2359 ) /* End of CASE */
2360 CASE(ADPCM_IMA_ACORN,
2361 for (int channel = 0; channel < channels; channel++) {
2362 ADPCMChannelStatus *cs = &c->status[channel];
2363 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2364 cs->step_index = bytestream2_get_le16u(&gb) & 0xFF;
2365 if (cs->step_index > 88u){
2366 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2367 channel, cs->step_index);
2368 return AVERROR_INVALIDDATA;
2369 }
2370 }
2371 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2372 int byte = bytestream2_get_byteu(&gb);
2373 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3);
2374 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3);
2375 }
2376 ) /* End of CASE */
2377 CASE(ADPCM_IMA_AMV,
2378 av_assert0(channels == 1);
2379
2380 /*
2381 * Header format:
2382 * int16_t predictor;
2383 * uint8_t step_index;
2384 * uint8_t reserved;
2385 * uint32_t frame_size;
2386 *
2387 * Some implementations have step_index as 16-bits, but others
2388 * only use the lower 8 and store garbage in the upper 8.
2389 */
2390 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2391 c->status[0].step_index = bytestream2_get_byteu(&gb);
2392 bytestream2_skipu(&gb, 5);
2393 if (c->status[0].step_index > 88u) {
2394 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2395 c->status[0].step_index);
2396 return AVERROR_INVALIDDATA;
2397 }
2398
2399 for (int n = nb_samples >> 1; n > 0; n--) {
2400 int v = bytestream2_get_byteu(&gb);
2401
2402 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2403 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
2404 }
2405
2406 if (nb_samples & 1) {
2407 int v = bytestream2_get_byteu(&gb);
2408 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2409
2410 if (v & 0x0F) {
2411 /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
2412 av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
2413 av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
2414 }
2415 }
2416 ) /* End of CASE */
2417 CASE(ADPCM_IMA_PDA,
2418 for (int i = 0; i < channels; i++) {
2419 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2420 c->status[i].step_index = bytestream2_get_byteu(&gb);
2421 bytestream2_skipu(&gb, 1);
2422 if (c->status[i].step_index > 88u) {
2423 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2424 c->status[i].step_index);
2425 return AVERROR_INVALIDDATA;
2426 }
2427 }
2428
2429 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2430 int v = bytestream2_get_byteu(&gb);
2431
2432 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2433 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2434 }
2435 ) /* End of CASE */
2436 CASE(ADPCM_IMA_SMJPEG,
2437 for (int i = 0; i < channels; i++) {
2438 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
2439 c->status[i].step_index = bytestream2_get_byteu(&gb);
2440 bytestream2_skipu(&gb, 1);
2441 if (c->status[i].step_index > 88u) {
2442 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2443 c->status[i].step_index);
2444 return AVERROR_INVALIDDATA;
2445 }
2446 }
2447
2448 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2449 int v = bytestream2_get_byteu(&gb);
2450
2451 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2452 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2453 }
2454 ) /* End of CASE */
2455 CASE(ADPCM_CT,
2456 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2457 int v = bytestream2_get_byteu(&gb);
2458 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
2459 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
2460 }
2461 ) /* End of CASE */
2462#if CONFIG_ADPCM_SBPRO_2_DECODER || CONFIG_ADPCM_SBPRO_3_DECODER || \
2463 CONFIG_ADPCM_SBPRO_4_DECODER
2467 if (!c->status[0].step_index) {
2468 /* the first byte is a raw sample */
2469 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2470 if (st)
2471 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2472 c->status[0].step_index = 1;
2473 nb_samples--;
2474 }
2475 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
2476 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2477 int byte = bytestream2_get_byteu(&gb);
2478 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2479 byte >> 4, 4, 0);
2480 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2481 byte & 0x0F, 4, 0);
2482 }
2483 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
2484 for (int n = (nb_samples<<st) / 3; n > 0; n--) {
2485 int byte = bytestream2_get_byteu(&gb);
2486 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2487 byte >> 5 , 3, 0);
2488 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2489 (byte >> 2) & 0x07, 3, 0);
2490 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2491 byte & 0x03, 2, 0);
2492 }
2493 } else {
2494 for (int n = nb_samples >> (2 - st); n > 0; n--) {
2495 int byte = bytestream2_get_byteu(&gb);
2496 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2497 byte >> 6 , 2, 2);
2498 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2499 (byte >> 4) & 0x03, 2, 2);
2500 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2501 (byte >> 2) & 0x03, 2, 2);
2502 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2503 byte & 0x03, 2, 2);
2504 }
2505 }
2506 break;
2507#endif /* CONFIG_ADPCM_SBPRO_x_DECODER */
2508 CASE(ADPCM_SWF,
2509 adpcm_swf_decode(avctx, buf, buf_size, samples);
2510 bytestream2_seek(&gb, 0, SEEK_END);
2511 ) /* End of CASE */
2512 CASE(ADPCM_YAMAHA,
2513 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2514 int v = bytestream2_get_byteu(&gb);
2515 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
2516 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
2517 }
2518 ) /* End of CASE */
2519 CASE(ADPCM_AICA,
2520 for (int channel = 0; channel < channels; channel++) {
2521 samples = samples_p[channel];
2522 for (int n = nb_samples >> 1; n > 0; n--) {
2523 int v = bytestream2_get_byteu(&gb);
2524 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
2525 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
2526 }
2527 }
2528 ) /* End of CASE */
2529 CASE(ADPCM_AFC,
2530 int samples_per_block;
2531 int blocks;
2532
2533 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
2534 samples_per_block = avctx->extradata[0] / 16;
2535 blocks = nb_samples / avctx->extradata[0];
2536 } else {
2537 samples_per_block = nb_samples / 16;
2538 blocks = 1;
2539 }
2540
2541 for (int m = 0; m < blocks; m++) {
2542 for (int channel = 0; channel < channels; channel++) {
2543 int prev1 = c->status[channel].sample1;
2544 int prev2 = c->status[channel].sample2;
2545
2546 samples = samples_p[channel] + m * 16;
2547 /* Read in every sample for this channel. */
2548 for (int i = 0; i < samples_per_block; i++) {
2549 int byte = bytestream2_get_byteu(&gb);
2550 int scale = 1 << (byte >> 4);
2551 int index = byte & 0xf;
2552 int factor1 = afc_coeffs[0][index];
2553 int factor2 = afc_coeffs[1][index];
2554
2555 /* Decode 16 samples. */
2556 for (int n = 0; n < 16; n++) {
2557 int32_t sampledat;
2558
2559 if (n & 1) {
2560 sampledat = sign_extend(byte, 4);
2561 } else {
2562 byte = bytestream2_get_byteu(&gb);
2563 sampledat = sign_extend(byte >> 4, 4);
2564 }
2565
2566 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
2567 sampledat * scale;
2568 *samples = av_clip_int16(sampledat);
2569 prev2 = prev1;
2570 prev1 = *samples++;
2571 }
2572 }
2573
2574 c->status[channel].sample1 = prev1;
2575 c->status[channel].sample2 = prev2;
2576 }
2577 }
2578 bytestream2_seek(&gb, 0, SEEK_END);
2579 ) /* End of CASE */
2580#if CONFIG_ADPCM_THP_DECODER || CONFIG_ADPCM_THP_LE_DECODER
2583 {
2584 int table[14][16];
2585
2586#define THP_GET16(g) \
2587 sign_extend( \
2588 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
2589 bytestream2_get_le16u(&(g)) : \
2590 bytestream2_get_be16u(&(g)), 16)
2591
2592 if (avctx->extradata) {
2593 GetByteContext tb;
2594 if (avctx->extradata_size < 32 * channels) {
2595 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
2596 return AVERROR_INVALIDDATA;
2597 }
2598
2599 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
2600 for (int i = 0; i < channels; i++)
2601 for (int n = 0; n < 16; n++)
2602 table[i][n] = THP_GET16(tb);
2603 } else {
2604 for (int i = 0; i < channels; i++)
2605 for (int n = 0; n < 16; n++)
2606 table[i][n] = THP_GET16(gb);
2607
2608 if (!c->has_status) {
2609 /* Initialize the previous sample. */
2610 for (int i = 0; i < channels; i++) {
2611 c->status[i].sample1 = THP_GET16(gb);
2612 c->status[i].sample2 = THP_GET16(gb);
2613 }
2614 c->has_status = 1;
2615 } else {
2616 bytestream2_skip(&gb, channels * 4);
2617 }
2618 }
2619
2620 for (int ch = 0; ch < channels; ch++) {
2621 samples = samples_p[ch];
2622
2623 /* Read in every sample for this channel. */
2624 for (int i = 0; i < (nb_samples + 13) / 14; i++) {
2625 int byte = bytestream2_get_byteu(&gb);
2626 int index = (byte >> 4) & 7;
2627 unsigned int exp = byte & 0x0F;
2628 int64_t factor1 = table[ch][index * 2];
2629 int64_t factor2 = table[ch][index * 2 + 1];
2630
2631 /* Decode 14 samples. */
2632 for (int n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
2633 int32_t sampledat;
2634
2635 if (n & 1) {
2636 sampledat = sign_extend(byte, 4);
2637 } else {
2638 byte = bytestream2_get_byteu(&gb);
2639 sampledat = sign_extend(byte >> 4, 4);
2640 }
2641
2642 sampledat = ((c->status[ch].sample1 * factor1
2643 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
2644 *samples = av_clip_int16(sampledat);
2645 c->status[ch].sample2 = c->status[ch].sample1;
2646 c->status[ch].sample1 = *samples++;
2647 }
2648 }
2649 }
2650 break;
2651 }
2652#endif /* CONFIG_ADPCM_THP(_LE)_DECODER */
2653 CASE(ADPCM_DTK,
2654 for (int channel = 0; channel < channels; channel++) {
2655 samples = samples_p[channel];
2656
2657 /* Read in every sample for this channel. */
2658 for (int i = 0; i < nb_samples / 28; i++) {
2659 int byte, header;
2660 if (channel)
2661 bytestream2_skipu(&gb, 1);
2662 header = bytestream2_get_byteu(&gb);
2663 bytestream2_skipu(&gb, 3 - channel);
2664
2665 /* Decode 28 samples. */
2666 for (int n = 0; n < 28; n++) {
2667 int32_t sampledat, prev;
2668
2669 switch (header >> 4) {
2670 case 1:
2671 prev = (c->status[channel].sample1 * 0x3c);
2672 break;
2673 case 2:
2674 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
2675 break;
2676 case 3:
2677 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
2678 break;
2679 default:
2680 prev = 0;
2681 }
2682
2683 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
2684
2685 byte = bytestream2_get_byteu(&gb);
2686 if (!channel)
2687 sampledat = sign_extend(byte, 4);
2688 else
2689 sampledat = sign_extend(byte >> 4, 4);
2690
2691 sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
2692 *samples++ = av_clip_int16(sampledat >> 6);
2693 c->status[channel].sample2 = c->status[channel].sample1;
2694 c->status[channel].sample1 = sampledat;
2695 }
2696 }
2697 if (!channel)
2698 bytestream2_seek(&gb, 0, SEEK_SET);
2699 }
2700 ) /* End of CASE */
2701 CASE(ADPCM_N64,
2702 ADPCMChannelStatus *cs = &c->status[0];
2703 int coefs[8*2*8] = { 0 };
2704
2705 if (avctx->extradata) {
2706 int version, order, entries;
2707 GetByteContext cb;
2708
2709 bytestream2_init(&cb, avctx->extradata, avctx->extradata_size);
2710
2711 version = bytestream2_get_be16(&cb);
2712 order = bytestream2_get_be16(&cb);
2713 entries = bytestream2_get_be16(&cb);
2714 if (version != 1 || order != 2 || entries > 8)
2715 return AVERROR_INVALIDDATA;
2716
2717 for (int n = 0; n < order * entries * 8; n++)
2718 coefs[n] = sign_extend(bytestream2_get_be16(&cb), 16);
2719 }
2720
2721 for (int block = 0; block < avpkt->size / 9; block++) {
2722 int scale, index, codes[16];
2723 int16_t hist[8] = { 0 };
2724 const int order = 2;
2725 int16_t out[16];
2726
2727 hist[6] = cs->sample2;
2728 hist[7] = cs->sample1;
2729
2730 samples = samples_p[0] + block * 16;
2731
2732 scale = (buf[0] >> 4) & 0xF;
2733 index = (buf[0] >> 0) & 0xF;
2734 scale = 1 << scale;
2735 index = FFMIN(index, 8);
2736
2737 for (int i = 0, j = 0; i < 16; i += 2, j++) {
2738 int n0 = (buf[j+1] >> 4) & 0xF;
2739 int n1 = (buf[j+1] >> 0) & 0xF;
2740
2741 if (n0 & 8)
2742 n0 = n0 - 16;
2743 if (n1 & 8)
2744 n1 = n1 - 16;
2745
2746 codes[i+0] = n0 * scale;
2747 codes[i+1] = n1 * scale;
2748 }
2749
2750 for (int j = 0; j < 2; j++) {
2751 int *sf_codes = &codes[j*8];
2752 int16_t *sf_out = &out[j*8];
2753
2754 for (int i = 0; i < 8; i++) {
2755 int sample;
2756 unsigned delta = 0;
2757
2758 for (int o = 0; o < order; o++)
2759 delta += coefs[o*8 + i] * hist[(8 - order) + o];
2760
2761 for (int k = i-1; k > -1; k--) {
2762 for (int o = 1; o < order; o++)
2763 delta += sf_codes[(i-1) - k] * (unsigned)coefs[(o*8) + k];
2764 }
2765
2766 sample = sf_codes[i] * 2048;
2767 sample = (int)(sample + delta) / 2048;
2769 sf_out[i] = sample;
2770 }
2771
2772 for (int i = 8 - order; i < 8; i++)
2773 hist[i] = sf_out[i];
2774 }
2775
2776 memcpy(samples, out, sizeof(out));
2777
2778 cs->sample2 = hist[6];
2779 cs->sample1 = hist[7];
2780
2781 buf += 9;
2782 }
2783 bytestream2_seek(&gb, 0, SEEK_END);
2784 ) /* End of CASE */
2785 CASE(ADPCM_PSX,
2786 for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * channels); block++) {
2787 int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * channels) / (16 * channels);
2788 for (int channel = 0; channel < channels; channel++) {
2789 samples = samples_p[channel] + block * nb_samples_per_block;
2790 av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2791
2792 /* Read in every sample for this channel. */
2793 for (int i = 0; i < nb_samples_per_block / 28; i++) {
2794 int filter, shift, flag, byte;
2795
2796 filter = bytestream2_get_byteu(&gb);
2797 shift = filter & 0xf;
2798 filter = filter >> 4;
2800 return AVERROR_INVALIDDATA;
2801 flag = bytestream2_get_byteu(&gb) & 0x7;
2802
2803 /* Decode 28 samples. */
2804 for (int n = 0; n < 28; n++) {
2805 int sample = 0, scale;
2806
2807 if (n & 1) {
2808 scale = sign_extend(byte >> 4, 4);
2809 } else {
2810 byte = bytestream2_get_byteu(&gb);
2811 scale = sign_extend(byte, 4);
2812 }
2813
2814 if (flag < 0x07) {
2815 scale = scale * (1 << 12);
2816 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2817 }
2818 *samples++ = av_clip_int16(sample);
2819 c->status[channel].sample2 = c->status[channel].sample1;
2820 c->status[channel].sample1 = sample;
2821 }
2822 }
2823 }
2824 }
2825 ) /* End of CASE */
2826 CASE(ADPCM_PSXC,
2827 for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2828 int nb_samples_per_block = ((avctx->block_align - 1) / channels) * 2;
2829 for (int channel = 0; channel < channels; channel++) {
2830 int filter, shift, byte;
2831
2832 samples = samples_p[channel] + block * nb_samples_per_block;
2833 av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2834
2835 filter = bytestream2_get_byteu(&gb);
2836 shift = filter & 0xf;
2837 filter = filter >> 4;
2839 return AVERROR_INVALIDDATA;
2840
2841 for (int n = 0; n < nb_samples_per_block; n++) {
2842 int sample = 0, scale;
2843
2844 if (n & 1) {
2845 scale = sign_extend(byte >> 4, 4);
2846 } else {
2847 byte = bytestream2_get_byteu(&gb);
2848 scale = sign_extend(byte & 0xF, 4);
2849 }
2850
2851 scale = scale * (1 << 12);
2852 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2853 *samples++ = av_clip_int16(sample);
2854 c->status[channel].sample2 = c->status[channel].sample1;
2855 c->status[channel].sample1 = sample;
2856 }
2857 }
2858 }
2859 ) /* End of CASE */
2860 CASE(ADPCM_SANYO,
2861 int (*expand)(ADPCMChannelStatus *c, int bits);
2863
2864 switch(avctx->bits_per_coded_sample) {
2865 case 3: expand = adpcm_sanyo_expand3; break;
2866 case 4: expand = adpcm_sanyo_expand4; break;
2867 case 5: expand = adpcm_sanyo_expand5; break;
2868 }
2869
2870 for (int ch = 0; ch < channels; ch++) {
2871 c->status[ch].predictor = sign_extend(bytestream2_get_le16(&gb), 16);
2872 c->status[ch].step = sign_extend(bytestream2_get_le16(&gb), 16);
2873 }
2874
2875 init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
2876 for (int i = 0; i < nb_samples; i++)
2877 for (int ch = 0; ch < channels; ch++)
2878 samples_p[ch][i] = expand(&c->status[ch], get_bits_le(&g, avctx->bits_per_coded_sample));
2879
2880 align_get_bits(&g);
2881 bytestream2_skip(&gb, get_bits_count(&g) / 8);
2882 ) /* End of CASE */
2883 CASE(ADPCM_RHETOREX,
2884 for (int i = 0; i < nb_samples / 2; i++) {
2885 uint8_t byte = bytestream2_get_byteu(&gb);
2886 *samples++ = adpcm_rhetorex_expand_nibble(c->status, byte >> 4);
2887 *samples++ = adpcm_rhetorex_expand_nibble(c->status, byte);
2888 }
2889 ) /* End of CASE */
2890 CASE(ADPCM_ARGO,
2891 /*
2892 * The format of each block:
2893 * uint8_t left_control;
2894 * uint4_t left_samples[nb_samples];
2895 * ---- and if stereo ----
2896 * uint8_t right_control;
2897 * uint4_t right_samples[nb_samples];
2898 *
2899 * Format of the control byte:
2900 * MSB [SSSSRDRR] LSB
2901 * S = (Shift Amount - 2)
2902 * D = Decoder flag.
2903 * R = Reserved
2904 *
2905 * Each block relies on the previous two samples of each channel.
2906 * They should be 0 initially.
2907 */
2908 for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2909 for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
2910 ADPCMChannelStatus *cs = c->status + channel;
2911 int control, shift;
2912
2913 samples = samples_p[channel] + block * 32;
2914
2915 /* Get the control byte and decode the samples, 2 at a time. */
2916 control = bytestream2_get_byteu(&gb);
2917 shift = (control >> 4) + 2;
2918
2919 for (int n = 0; n < 16; n++) {
2920 int sample = bytestream2_get_byteu(&gb);
2921 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2922 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2923 }
2924 }
2925 }
2926 ) /* End of CASE */
2927 CASE(ADPCM_CIRCUS,
2928 for (int n = 0; n < nb_samples; n++) {
2929 for (int ch = 0; ch < channels; ch++) {
2930 int v = bytestream2_get_byteu(&gb);
2931 *samples++ = adpcm_circus_expand_nibble(&c->status[ch], v);
2932 }
2933 }
2934 ) /* End of CASE */
2935 CASE(ADPCM_ZORK,
2936 for (int n = 0; n < nb_samples * channels; n++) {
2937 int v = bytestream2_get_byteu(&gb);
2938 *samples++ = adpcm_zork_expand_nibble(&c->status[n % channels], v);
2939 }
2940 ) /* End of CASE */
2941 CASE(ADPCM_IMA_MTF,
2942 for (int n = nb_samples / 2; n > 0; n--) {
2943 for (int channel = 0; channel < channels; channel++) {
2944 int v = bytestream2_get_byteu(&gb);
2945 *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2946 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2947 }
2948 samples += channels;
2949 }
2950 ) /* End of CASE */
2951 default:
2952 av_unreachable("There are cases for all codec ids using adpcm_decode_frame");
2953 }
2954
2955 if (avpkt->size && bytestream2_tell(&gb) == 0) {
2956 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2957 return AVERROR_INVALIDDATA;
2958 }
2959
2960 *got_frame_ptr = 1;
2961
2962 if (avpkt->size < bytestream2_tell(&gb)) {
2963 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2964 return avpkt->size;
2965 }
2966
2967 return bytestream2_tell(&gb);
2968}
2969
2971{
2972 ADPCMDecodeContext *c = avctx->priv_data;
2973
2974 /* Just nuke the entire state and re-init. */
2975 memset(c, 0, sizeof(ADPCMDecodeContext));
2976
2977 switch(avctx->codec_id) {
2979 c->status[0].step = c->status[1].step = 511;
2980 break;
2981
2983 if (avctx->extradata && avctx->extradata_size >= 8) {
2984 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
2985 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2986 }
2987 break;
2988
2990 if (avctx->extradata && avctx->extradata_size >= 28) {
2991 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
2992 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
2993 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2994 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
2995 }
2996 break;
2997
2999 if (avctx->extradata && avctx->extradata_size >= 2)
3000 c->vqa_version = AV_RL16(avctx->extradata);
3001 break;
3002 default:
3003 /* Other codecs may want to handle this during decoding. */
3004 c->has_status = 0;
3005 return;
3006 }
3007
3008 c->has_status = 1;
3009}
3010
3011
3012#define ADPCM_DECODER_0(id_, name_, long_name_)
3013#define ADPCM_DECODER_1(id_, name_, long_name_) \
3014const FFCodec ff_ ## name_ ## _decoder = { \
3015 .p.name = #name_, \
3016 CODEC_LONG_NAME(long_name_), \
3017 .p.type = AVMEDIA_TYPE_AUDIO, \
3018 .p.id = id_, \
3019 .p.capabilities = AV_CODEC_CAP_DR1, \
3020 .priv_data_size = sizeof(ADPCMDecodeContext), \
3021 .init = adpcm_decode_init, \
3022 FF_CODEC_DECODE_CB(adpcm_decode_frame), \
3023 .flush = adpcm_flush, \
3024};
3025#define ADPCM_DECODER_2(enabled, codec_id, name, long_name) \
3026 ADPCM_DECODER_ ## enabled(codec_id, name, long_name)
3027#define ADPCM_DECODER_3(config, codec_id, name, long_name) \
3028 ADPCM_DECODER_2(config, codec_id, name, long_name)
3029#define ADPCM_DECODER(codec, name, long_name) \
3030 ADPCM_DECODER_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, \
3031 name, long_name)
3032
3033/* Note: Do not forget to add new entries to the Makefile as well. */
3034ADPCM_DECODER(ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie")
3035ADPCM_DECODER(ADPCM_AFC, adpcm_afc, "ADPCM Nintendo Gamecube AFC")
3036ADPCM_DECODER(ADPCM_AGM, adpcm_agm, "ADPCM AmuseGraphics Movie")
3037ADPCM_DECODER(ADPCM_AICA, adpcm_aica, "ADPCM Yamaha AICA")
3038ADPCM_DECODER(ADPCM_ARGO, adpcm_argo, "ADPCM Argonaut Games")
3039ADPCM_DECODER(ADPCM_CIRCUS, adpcm_circus, "ADPCM Circus")
3040ADPCM_DECODER(ADPCM_CT, adpcm_ct, "ADPCM Creative Technology")
3041ADPCM_DECODER(ADPCM_DTK, adpcm_dtk, "ADPCM Nintendo Gamecube DTK")
3042ADPCM_DECODER(ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts")
3043ADPCM_DECODER(ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA")
3044ADPCM_DECODER(ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1")
3045ADPCM_DECODER(ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2")
3046ADPCM_DECODER(ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3")
3047ADPCM_DECODER(ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS")
3048ADPCM_DECODER(ADPCM_IMA_ACORN, adpcm_ima_acorn, "ADPCM IMA Acorn Replay")
3049ADPCM_DECODER(ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV")
3050ADPCM_DECODER(ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC")
3051ADPCM_DECODER(ADPCM_IMA_APM, adpcm_ima_apm, "ADPCM IMA Ubisoft APM")
3052ADPCM_DECODER(ADPCM_IMA_CUNNING, adpcm_ima_cunning, "ADPCM IMA Cunning Developments")
3053ADPCM_DECODER(ADPCM_IMA_DAT4, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4")
3054ADPCM_DECODER(ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3")
3055ADPCM_DECODER(ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4")
3056ADPCM_DECODER(ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS")
3057ADPCM_DECODER(ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD")
3058ADPCM_DECODER(ADPCM_IMA_ESCAPE, adpcm_ima_escape, "ADPCM IMA Acorn Escape")
3059ADPCM_DECODER(ADPCM_IMA_HVQM2, adpcm_ima_hvqm2, "ADPCM IMA HVQM2")
3060ADPCM_DECODER(ADPCM_IMA_HVQM4, adpcm_ima_hvqm4, "ADPCM IMA HVQM4")
3061ADPCM_DECODER(ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS")
3062ADPCM_DECODER(ADPCM_IMA_MAGIX, adpcm_ima_magix, "ADPCM IMA Magix")
3063ADPCM_DECODER(ADPCM_IMA_MOFLEX, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX")
3064ADPCM_DECODER(ADPCM_IMA_MTF, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework")
3065ADPCM_DECODER(ADPCM_IMA_OKI, adpcm_ima_oki, "ADPCM IMA Dialogic OKI")
3066ADPCM_DECODER(ADPCM_IMA_PDA, adpcm_ima_pda, "ADPCM IMA PlayDate")
3067ADPCM_DECODER(ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime")
3068ADPCM_DECODER(ADPCM_IMA_RAD, adpcm_ima_rad, "ADPCM IMA Radical")
3069ADPCM_DECODER(ADPCM_IMA_SSI, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive")
3070ADPCM_DECODER(ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG")
3071ADPCM_DECODER(ADPCM_IMA_ALP, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP")
3072ADPCM_DECODER(ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV")
3073ADPCM_DECODER(ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood")
3074ADPCM_DECODER(ADPCM_IMA_XBOX, adpcm_ima_xbox, "ADPCM IMA Xbox")
3075ADPCM_DECODER(ADPCM_MS, adpcm_ms, "ADPCM Microsoft")
3076ADPCM_DECODER(ADPCM_MTAF, adpcm_mtaf, "ADPCM MTAF")
3077ADPCM_DECODER(ADPCM_N64, adpcm_n64, "ADPCM Silicon Graphics N64")
3078ADPCM_DECODER(ADPCM_PSX, adpcm_psx, "ADPCM Playstation")
3079ADPCM_DECODER(ADPCM_PSXC, adpcm_psxc, "ADPCM Playstation C")
3080ADPCM_DECODER(ADPCM_RHETOREX, adpcm_rhetorex, "ADPCM Rhetorex")
3081ADPCM_DECODER(ADPCM_SANYO, adpcm_sanyo, "ADPCM Sanyo")
3082ADPCM_DECODER(ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit")
3083ADPCM_DECODER(ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit")
3084ADPCM_DECODER(ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit")
3085ADPCM_DECODER(ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash")
3086ADPCM_DECODER(ADPCM_THP_LE, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)")
3087ADPCM_DECODER(ADPCM_THP, adpcm_thp, "ADPCM Nintendo THP")
3088ADPCM_DECODER(ADPCM_XA, adpcm_xa, "ADPCM CDROM XA")
3089ADPCM_DECODER(ADPCM_XMD, adpcm_xmd, "ADPCM Konami XMD")
3090ADPCM_DECODER(ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha")
3091ADPCM_DECODER(ADPCM_ZORK, adpcm_zork, "ADPCM Zork")
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
Definition adpcm.c:1021
static const int8_t ima_cunning_index_table[9]
Definition adpcm.c:111
static const int8_t swf_index_tables[4][16]
Definition adpcm.c:228
int16_t ff_adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:581
static void decode_adpcm_ima_hvqm4(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do, int frame_format, GetByteContext *gb)
Definition adpcm.c:644
static int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition adpcm.c:471
static void adpcm_flush(AVCodecContext *avctx)
Definition adpcm.c:2970
static int adpcm_sanyo_expand5(ADPCMChannelStatus *c, int bits)
Definition adpcm.c:1140
static int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition adpcm.c:810
static int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:706
static int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:520
static const int8_t *const adpcm_index_tables[4]
Definition adpcm.c:145
static const int16_t afc_coeffs[2][16]
Definition adpcm.c:92
#define DK3_GET_NEXT_NIBBLE()
static int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
Definition adpcm.c:556
static const int8_t adpcm_index_table3[8]
Definition adpcm.c:135
static int adpcm_sanyo_expand3(ADPCMChannelStatus *c, int bits)
Definition adpcm.c:1038
static const int16_t mtaf_stepsize[32][16]
Definition adpcm.c:152
static const int16_t oki_step_table[49]
Definition adpcm.c:219
static int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
Definition adpcm.c:776
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
Definition adpcm.c:963
#define CASE(codec,...)
Definition adpcm.c:80
static const int8_t xa_adpcm_table[5][2]
Definition adpcm.c:84
static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb, int buf_size, int *coded_samples, int *approx_nb_samples)
Get the number of samples (per channel) that will be decoded from the packet.
Definition adpcm.c:1215
static const int8_t adpcm_index_table2[4]
Definition adpcm.c:130
static int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition adpcm.c:404
static int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition adpcm.c:497
static const int16_t rhetorex_index[8]
Definition adpcm.c:263
static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1, const uint8_t *in, ADPCMChannelStatus *left, ADPCMChannelStatus *right, int channels, int sample_offset)
Definition adpcm.c:875
static const int16_t ea_adpcm_table[]
Definition adpcm.c:97
static int16_t adpcm_circus_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition adpcm.c:819
static const int8_t adpcm_index_table5[32]
Definition adpcm.c:140
static int16_t adpcm_ima_escape_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition adpcm.c:448
static const int16_t rhetorex_step[128]
Definition adpcm.c:244
static int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition adpcm.c:840
static int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition adpcm.c:796
static const int8_t zork_index_table[8]
Definition adpcm.c:235
static int adpcm_sanyo_expand4(ADPCMChannelStatus *c, int bits)
Definition adpcm.c:1081
static int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition adpcm.c:536
static void decode_adpcm_ima_hvqm2(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do, int frame_format, GetByteContext *gb)
Definition adpcm.c:607
static const int16_t ima_cunning_step_table[61]
Definition adpcm.c:121
static int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:687
static const int8_t mtf_index_table[16]
Definition adpcm.c:239
static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition adpcm.c:1497
static int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition adpcm.c:727
#define ADPCM_DECODER(codec, name, long_name)
Definition adpcm.c:3029
static av_cold int adpcm_decode_init(AVCodecContext *avctx)
Definition adpcm.c:277
static int16_t adpcm_rhetorex_expand_nibble(ADPCMChannelStatus *c, uint16_t nibble)
Definition adpcm.c:748
ADPCM encoder/decoder common header.
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
Definition adpcm_data.c:65
const int8_t ff_adpcm_index_table[16]
Definition adpcm_data.c:30
const int8_t ff_adpcm_yamaha_difflookup[]
Definition adpcm_data.c:74
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition adpcm_data.c:39
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition adpcm_data.c:60
const int16_t ff_adpcm_yamaha_indexscale[]
Definition adpcm_data.c:69
const int16_t ff_adpcm_AdaptationTable[]
Definition adpcm_data.c:54
ADPCM tables.
static const uint8_t ff_adpcm_ima_block_sizes[4]
Definition adpcm_data.h:31
static const uint8_t ff_adpcm_ima_block_samples[4]
Definition adpcm_data.h:32
static double val(void *priv, double ch)
Definition aeval.c:77
static int expand(AVFilterContext *ctx, double *pz, int n, double *coefs)
Definition af_aiir.c:500
static const char *const format[]
Definition af_aiir.c:445
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
static FILE * out
channels
Definition aptx.h:31
int32_t
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define bits_left
Definition bitstream.h:116
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition bytestream.h:99
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define flag(name)
Definition cbs_h264.c:60
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define av_clip_intp2
Definition common.h:121
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
#define av_zero_extend
Definition common.h:151
#define av_clip_uintp2
Definition common.h:124
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define abs(x)
static int16_t block[64]
Definition dct.c:125
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1781
static AVFrame * frame
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
int8_t exp
Definition eval.c:76
static void predictor(uint8_t *src, ptrdiff_t size)
Definition exrenc.c:170
static const uint8_t bits[8]
Definition fastaudio.c:100
#define sample
bitstream reader API header.
static unsigned int get_bits_le(GetBitContext *s, int n)
Definition get_bits.h:358
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static const uint8_t * align_get_bits(GetBitContext *s)
Definition get_bits.h:560
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_ADPCM_IMA_PDA
Definition codec_id.h:426
@ AV_CODEC_ID_ADPCM_SWF
Definition codec_id.h:384
@ AV_CODEC_ID_ADPCM_IMA_HVQM4
Definition codec_id.h:425
@ AV_CODEC_ID_ADPCM_CT
Definition codec_id.h:383
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition codec_id.h:375
@ AV_CODEC_ID_ADPCM_EA_R1
Definition codec_id.h:391
@ AV_CODEC_ID_ADPCM_4XM
Definition codec_id.h:378
@ AV_CODEC_ID_ADPCM_IMA_OKI
Definition codec_id.h:403
@ AV_CODEC_ID_ADPCM_IMA_EA_EACS
Definition codec_id.h:395
@ AV_CODEC_ID_ADPCM_SBPRO_2
Definition codec_id.h:388
@ AV_CODEC_ID_ADPCM_DTK
Definition codec_id.h:404
@ AV_CODEC_ID_ADPCM_CIRCUS
Definition codec_id.h:431
@ AV_CODEC_ID_ADPCM_IMA_EA_SEAD
Definition codec_id.h:394
@ AV_CODEC_ID_ADPCM_IMA_HVQM2
Definition codec_id.h:428
@ AV_CODEC_ID_ADPCM_PSX
Definition codec_id.h:408
@ AV_CODEC_ID_ADPCM_XA
Definition codec_id.h:379
@ AV_CODEC_ID_ADPCM_YAMAHA
Definition codec_id.h:385
@ AV_CODEC_ID_ADPCM_SBPRO_3
Definition codec_id.h:387
@ AV_CODEC_ID_ADPCM_IMA_ESCAPE
Definition codec_id.h:432
@ AV_CODEC_ID_ADPCM_EA_R2
Definition codec_id.h:393
@ AV_CODEC_ID_ADPCM_IMA_ISS
Definition codec_id.h:398
@ AV_CODEC_ID_ADPCM_RHETOREX
Definition codec_id.h:433
@ AV_CODEC_ID_ADPCM_MS
Definition codec_id.h:377
@ AV_CODEC_ID_ADPCM_ZORK
Definition codec_id.h:415
@ AV_CODEC_ID_ADPCM_SBPRO_4
Definition codec_id.h:386
@ AV_CODEC_ID_ADPCM_EA_MAXIS_XA
Definition codec_id.h:397
@ AV_CODEC_ID_ADPCM_ARGO
Definition codec_id.h:413
@ AV_CODEC_ID_ADPCM_IMA_APC
Definition codec_id.h:400
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition codec_id.h:390
@ AV_CODEC_ID_ADPCM_EA_XAS
Definition codec_id.h:396
@ AV_CODEC_ID_ADPCM_IMA_MAGIX
Definition codec_id.h:429
@ AV_CODEC_ID_ADPCM_AGM
Definition codec_id.h:412
@ AV_CODEC_ID_ADPCM_IMA_CUNNING
Definition codec_id.h:419
@ AV_CODEC_ID_ADPCM_IMA_DK4
Definition codec_id.h:374
@ AV_CODEC_ID_ADPCM_IMA_DK3
Definition codec_id.h:373
@ AV_CODEC_ID_ADPCM_IMA_DAT4
Definition codec_id.h:410
@ AV_CODEC_ID_ADPCM_XMD
Definition codec_id.h:422
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition codec_id.h:371
@ AV_CODEC_ID_ADPCM_EA
Definition codec_id.h:381
@ AV_CODEC_ID_ADPCM_IMA_SMJPEG
Definition codec_id.h:376
@ AV_CODEC_ID_ADPCM_MTAF
Definition codec_id.h:411
@ AV_CODEC_ID_ADPCM_AICA
Definition codec_id.h:409
@ AV_CODEC_ID_ADPCM_IMA_MTF
Definition codec_id.h:418
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition codec_id.h:416
@ AV_CODEC_ID_ADPCM_THP
Definition codec_id.h:389
@ AV_CODEC_ID_ADPCM_IMA_XBOX
Definition codec_id.h:423
@ AV_CODEC_ID_ADPCM_IMA_ACORN
Definition codec_id.h:421
@ AV_CODEC_ID_ADPCM_AFC
Definition codec_id.h:402
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition codec_id.h:372
@ AV_CODEC_ID_ADPCM_THP_LE
Definition codec_id.h:407
@ AV_CODEC_ID_ADPCM_N64
Definition codec_id.h:427
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition codec_id.h:417
@ AV_CODEC_ID_ADPCM_EA_R3
Definition codec_id.h:392
@ AV_CODEC_ID_ADPCM_PSXC
Definition codec_id.h:430
@ AV_CODEC_ID_ADPCM_SANYO
Definition codec_id.h:424
@ AV_CODEC_ID_ADPCM_IMA_RAD
Definition codec_id.h:405
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition codec_id.h:414
@ AV_CODEC_ID_ADPCM_IMA_MOFLEX
Definition codec_id.h:420
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
int index
Definition gxfenc.c:90
int a
for(k=2;k<=8;++k)
if(svq3)
static const int offsets[]
Definition hevc_pel.c:34
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_RL32(p)
#define AV_RL16(p)
static int shift(int a, int b)
Definition bonk.c:261
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
unsigned bps
Definition movenc.c:2128
static const uint16_t table[]
Definition prosumer.c:203
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
static const float pred[4]
Definition siprdata.h:259
const uint8_t * code
Definition spdifenc.c:433
int16_t step_index
Definition adpcm.h:33
int vqa_version
VQA version.
Definition adpcm.c:271
ADPCMChannelStatus status[14]
Definition adpcm.c:270
int has_status
Status flag.
Definition adpcm.c:272
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1569
const struct AVCodec * codec
Definition avcodec.h:452
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition avcodec.h:1075
void * priv_data
Definition avcodec.h:470
enum AVCodecID id
Definition codec.h:189
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
uint8_t level
Definition svq3.c:208
#define avpriv_request_sample(...)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
int size
const char * g
Definition vf_curves.c:128
else temp
Definition vf_mcdeint.c:275
static const double coeff[2][5]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
float delta
static double c[64]