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
267static const int8_t citrix_index_table[4] = {
268 -1, 1, -1, 1,
269};
270
271/* end of tables */
272
273typedef struct ADPCMDecodeContext {
275 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
276 int has_status; /**< Status flag. Reset to 0 after a flush. */
278
279static void adpcm_flush(AVCodecContext *avctx);
280
282{
284 unsigned int min_channels = 1;
285 unsigned int max_channels = 2;
286
287 adpcm_flush(avctx);
288
289 switch(avctx->codec->id) {
293 max_channels = 1;
294 break;
296 max_channels = 2;
297 break;
304 max_channels = 6;
305 break;
307 min_channels = 2;
308 max_channels = 8;
309 if (avctx->ch_layout.nb_channels & 1) {
310 avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);
312 }
313 break;
315 min_channels = 2;
316 break;
318 max_channels = 8;
319 if (avctx->ch_layout.nb_channels <= 0 ||
320 avctx->block_align % (16 * avctx->ch_layout.nb_channels))
321 return AVERROR_INVALIDDATA;
322 break;
324 max_channels = 8;
325 if (avctx->ch_layout.nb_channels <= 0 || avctx->block_align <= 0 ||
326 avctx->block_align % avctx->ch_layout.nb_channels)
327 return AVERROR_INVALIDDATA;
328 break;
332 max_channels = 14;
333 break;
334 }
335 if (avctx->ch_layout.nb_channels < min_channels ||
336 avctx->ch_layout.nb_channels > max_channels) {
337 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
338 return AVERROR(EINVAL);
339 }
340
341 switch(avctx->codec->id) {
343 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
344 return AVERROR_INVALIDDATA;
345 break;
347 if (avctx->bits_per_coded_sample != 4 ||
348 avctx->block_align != 17 * avctx->ch_layout.nb_channels)
349 return AVERROR_INVALIDDATA;
350 break;
352 if (avctx->bits_per_coded_sample < 3 || avctx->bits_per_coded_sample > 5)
353 return AVERROR_INVALIDDATA;
354 break;
356 if (avctx->bits_per_coded_sample != 4)
357 return AVERROR_INVALIDDATA;
358 break;
360 if (avctx->bits_per_coded_sample != 8)
361 return AVERROR_INVALIDDATA;
362 break;
364 if (avctx->bits_per_coded_sample != 2 ||
365 avctx->block_align < 4 * avctx->ch_layout.nb_channels ||
366 (avctx->ch_layout.nb_channels == 2 && avctx->block_align % 8))
367 return AVERROR_INVALIDDATA;
368 break;
369 default:
370 break;
371 }
372
373 switch (avctx->codec->id) {
399 break;
401 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
403 break;
405 avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
407 break;
408 default:
410 }
411 return 0;
412}
413
414static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
415{
416 int delta, pred, step, add;
417
418 pred = c->predictor;
419 delta = nibble & 7;
420 step = c->step;
421 add = (delta * 2 + 1) * step;
422 if (add < 0)
423 add = add + 7;
424
425 if ((nibble & 8) == 0)
426 pred = av_clip(pred + (add >> 3), -32767, 32767);
427 else
428 pred = av_clip(pred - (add >> 3), -32767, 32767);
429
430 switch (delta) {
431 case 7:
432 step *= 0x99;
433 break;
434 case 6:
435 c->step = av_clip(c->step * 2, 127, 24576);
436 c->predictor = pred;
437 return pred;
438 case 5:
439 step *= 0x66;
440 break;
441 case 4:
442 step *= 0x4d;
443 break;
444 default:
445 step *= 0x39;
446 break;
447 }
448
449 if (step < 0)
450 step += 0x3f;
451
452 c->step = step >> 6;
453 c->step = av_clip(c->step, 127, 24576);
454 c->predictor = pred;
455 return pred;
456}
457
458#if CONFIG_ADPCM_IMA_ESCAPE_DECODER
459static inline int16_t adpcm_ima_escape_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
460{
461 int step_index;
462 int predictor;
463 int sign, delta, diff, step;
464
465 step = ff_adpcm_step_table[c->step_index];
466 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
467 step_index = av_clip(step_index, 0, 88);
468
469 sign = nibble & 8;
470 delta = nibble & 7;
471 diff = (delta * step) >> 2;
472 predictor = c->predictor;
473 if (sign) predictor -= diff;
474 else predictor += diff;
475
476 c->predictor = av_clip_int16(predictor);
477 c->step_index = step_index;
478
479 return (int16_t)c->predictor;
480}
481#endif
482
483static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
484{
485 int step_index;
486 int predictor;
487 int sign, delta, diff, step;
488
489 step = ff_adpcm_step_table[c->step_index];
490 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
491 step_index = av_clip(step_index, 0, 88);
492
493 sign = nibble & 8;
494 delta = nibble & 7;
495 /* perform direct multiplication instead of series of jumps proposed by
496 * the reference ADPCM implementation since modern CPUs can do the mults
497 * quickly enough */
498 diff = ((2 * delta + 1) * step) >> shift;
499 predictor = c->predictor;
500 if (sign) predictor -= diff;
501 else predictor += diff;
502
503 c->predictor = av_clip_int16(predictor);
504 c->step_index = step_index;
505
506 return (int16_t)c->predictor;
507}
508
509static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
510{
511 int step_index;
512 int predictor;
513 int sign, delta, diff, step;
514
515 step = ff_adpcm_step_table[c->step_index];
516 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
517 step_index = av_clip(step_index, 0, 88);
518
519 sign = nibble & 8;
520 delta = nibble & 7;
521 diff = (delta * step) >> shift;
522 predictor = c->predictor;
523 if (sign) predictor -= diff;
524 else predictor += diff;
525
526 c->predictor = av_clip_int16(predictor);
527 c->step_index = step_index;
528
529 return (int16_t)c->predictor;
530}
531
532static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
533{
534 int step_index, step, delta, predictor;
535
536 step = ff_adpcm_step_table[c->step_index];
537
538 delta = step * (2 * nibble - 15);
539 predictor = c->predictor + delta;
540
541 step_index = c->step_index + mtf_index_table[(unsigned)nibble];
542 c->predictor = av_clip_int16(predictor >> 4);
543 c->step_index = av_clip(step_index, 0, 88);
544
545 return (int16_t)c->predictor;
546}
547
548static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
549{
550 int step_index;
551 int predictor;
552 int step;
553
554 nibble = sign_extend(nibble & 0xF, 4);
555
556 step = ima_cunning_step_table[c->step_index];
557 step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
558 step_index = av_clip(step_index, 0, 60);
559
560 predictor = c->predictor + step * nibble;
561
562 c->predictor = av_clip_int16(predictor);
563 c->step_index = step_index;
564
565 return c->predictor;
566}
567
569{
570 int nibble, step_index, predictor, sign, delta, diff, step, shift;
571
572 shift = bps - 1;
573 nibble = get_bits_le(gb, bps),
574 step = ff_adpcm_step_table[c->step_index];
575 step_index = c->step_index + adpcm_index_tables[bps - 2][nibble];
576 step_index = av_clip(step_index, 0, 88);
577
578 sign = nibble & (1 << shift);
579 delta = av_zero_extend(nibble, shift);
580 diff = step >> shift;
581 for (int i = 0; i < shift; i++)
582 diff += (step >> (shift-1-i)) * !!(delta & (1 << i));
583 predictor = c->predictor;
584 if (sign) predictor -= diff;
585 else predictor += diff;
586
587 c->predictor = av_clip_int16(predictor);
588 c->step_index = step_index;
589
590 return (int16_t)c->predictor;
591}
592
594{
595 int step_index;
596 int predictor;
597 int diff, step;
598
599 step = ff_adpcm_step_table[c->step_index];
600 step_index = c->step_index + ff_adpcm_index_table[nibble];
601 step_index = av_clip(step_index, 0, 88);
602
603 diff = step >> 3;
604 if (nibble & 4) diff += step;
605 if (nibble & 2) diff += step >> 1;
606 if (nibble & 1) diff += step >> 2;
607
608 if (nibble & 8)
609 predictor = c->predictor - diff;
610 else
611 predictor = c->predictor + diff;
612
613 c->predictor = av_clip_int16(predictor);
614 c->step_index = step_index;
615
616 return c->predictor;
617}
618
619#if CONFIG_ADPCM_IMA_HVQM2_DECODER
620static void decode_adpcm_ima_hvqm2(AVCodecContext *avctx, int16_t *outbuf,
621 int samples_to_do, int frame_format,
622 GetByteContext *gb)
623{
625 int st = avctx->ch_layout.nb_channels == 2;
626 uint8_t nibble;
627
628 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
629 unsigned tmp;
630
631 switch (frame_format) {
632 case 0: /* combined hist+index */
633 tmp = bytestream2_get_be16(gb);
634 c->status[ch].predictor = sign_extend(tmp & 0xFF80, 16);
635 c->status[ch].step_index = tmp & 0x7f;
636 *outbuf++ = c->status[ch].predictor;
637 samples_to_do--;
638 break;
639 default:
640 break;
641 }
642
643 c->status[ch].step_index = av_clip(c->status[ch].step_index, 0, 88);
644 }
645
646 for (int i = 0; i < samples_to_do; i++) {
647 if (!(i&1)) {
648 nibble = bytestream2_get_byte(gb);
649 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], nibble >> 4);
650 } else {
651 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[ 0], nibble & 0xF);
652 }
653 }
654
655 bytestream2_seek(gb, 0, SEEK_END);
656}
657#endif
658
659#if CONFIG_ADPCM_IMA_HVQM4_DECODER
660static void decode_adpcm_ima_hvqm4(AVCodecContext *avctx, int16_t *outbuf,
661 int samples_to_do, int frame_format,
662 GetByteContext *gb)
663{
665 int st = avctx->ch_layout.nb_channels == 2;
666 unsigned tmp;
667
668 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
669 switch (frame_format) {
670 case 1: /* combined hist+index */
671 tmp = bytestream2_get_be16(gb);
672 c->status[ch].predictor = sign_extend(tmp & 0xFF80, 16);
673 c->status[ch].step_index = tmp & 0x7f;
674 break;
675 case 2: /* no hist/index (continues from previous frame) */
676 default:
677 break;
678 case 3: /* separate hist+index */
679 tmp = bytestream2_get_be16(gb);
680 c->status[ch].predictor = sign_extend(tmp, 16);
681 c->status[ch].step_index = bytestream2_get_byte(gb);
682 break;
683 }
684
685 c->status[ch].step_index = av_clip(c->status[ch].step_index, 0, 88);
686 }
687
688 if (frame_format == 1 || frame_format == 3) {
689 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
690 *outbuf++ = (int16_t)c->status[st - ch].predictor;
691 samples_to_do--;
692 }
693
694 for (int i = 0; i < samples_to_do; i += 1+(!st)) {
695 uint8_t nibble = bytestream2_get_byte(gb);
696
697 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], nibble & 0xF);
698 *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[ 0], nibble >> 4);
699 }
700
701 bytestream2_seek(gb, 0, SEEK_END);
702}
703#endif
704
705static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
706{
707 int predictor;
708
709 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) >> 6;
710 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
711
712 c->sample2 = c->sample1;
713 c->sample1 = av_clip_int16(predictor);
714 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
715 if (c->idelta < 16) c->idelta = 16;
716 if (c->idelta > INT_MAX/768) {
717 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
718 c->idelta = INT_MAX/768;
719 }
720
721 return c->sample1;
722}
723
724static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
725{
726 int step_index, predictor, sign, delta, diff, step;
727
728 step = oki_step_table[c->step_index];
729 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
730 step_index = av_clip(step_index, 0, 48);
731
732 sign = nibble & 8;
733 delta = nibble & 7;
734 diff = ((2 * delta + 1) * step) >> 3;
735 predictor = c->predictor;
736 if (sign) predictor -= diff;
737 else predictor += diff;
738
739 c->predictor = av_clip_intp2(predictor, 11);
740 c->step_index = step_index;
741
742 return c->predictor * 16;
743}
744
745static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
746{
747 int sign, delta, diff;
748 int new_step;
749
750 sign = nibble & 8;
751 delta = nibble & 7;
752 /* perform direct multiplication instead of series of jumps proposed by
753 * the reference ADPCM implementation since modern CPUs can do the mults
754 * quickly enough */
755 diff = ((2 * delta + 1) * c->step) >> 3;
756 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
757 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
758 c->predictor = av_clip_int16(c->predictor);
759 /* calculate new step and clamp it to range 511..32767 */
760 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
761 c->step = av_clip(new_step, 511, 32767);
762
763 return (int16_t)c->predictor;
764}
765
766static inline int16_t adpcm_rhetorex_expand_nibble(ADPCMChannelStatus *c, uint16_t nibble)
767{
768 int delta, add, a, b;
769 int16_t sample;
770
771 delta = nibble & 7;
772 add = rhetorex_step[((c->step_index >> 8) & 0xF8) + delta];
773 if (nibble & 0x8)
774 add = -add;
775
776 sample = av_clip_int16(c->predictor + add);
777
778 a = 32768 * c->coeff1 + (add >> 1) * c->sample2;
779 c->coeff1 = av_clip_int16((a - ((a >> 7) & ~0xFF)) >> 15);
780
781 b = 32768 * c->coeff2 + (add >> 1) * c->sample1;
782 c->coeff2 = av_clip_int16((b - ((b >> 7) & ~0xFF) + 0x800000) >> 15);
783
784 c->predictor = av_clip_int16((c->coeff1 * c->sample1 + c->coeff2 * sample) >> 15);
785
786 c->sample2 = c->sample1;
787 c->sample1 = sample;
788
789 c->step_index = FFABS((int)(64512U * c->step_index + 65536U * rhetorex_index[delta])) >> 16;
790
791 return sample;
792}
793
794static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
795{
796 int sign, delta, diff;
797
798 sign = nibble & (1<<(size-1));
799 delta = nibble & ((1<<(size-1))-1);
800 diff = delta << (7 + c->step + shift);
801
802 /* clamp result */
803 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
804
805 /* calculate new step */
806 if (delta >= (2*size - 3) && c->step < 3)
807 c->step++;
808 else if (delta == 0 && c->step > 0)
809 c->step--;
810
811 return (int16_t) c->predictor;
812}
813
814static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
815{
816 if(!c->step) {
817 c->predictor = 0;
818 c->step = 127;
819 }
820
821 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
822 c->predictor = av_clip_int16(c->predictor);
823 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
824 c->step = av_clip(c->step, 127, 24576);
825 return c->predictor;
826}
827
828static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
829{
830 c->predictor += mtaf_stepsize[c->step][nibble];
831 c->predictor = av_clip_int16(c->predictor);
832 c->step += ff_adpcm_index_table[nibble];
833 c->step = av_clip_uintp2(c->step, 5);
834 return c->predictor;
835}
836
837#if CONFIG_ADPCM_CIRCUS_DECODER
838static inline int16_t adpcm_circus_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
839{
840 int32_t sample = c->predictor;
841 int32_t scale = c->step;
842 int32_t code = sign_extend(nibble, 8);
843
844 sample += code * (1 << scale);
845 if (code == 0) {
846 scale--;
847 } else if (code == 127 || code == -128) {
848 scale++;
849 }
850 scale = av_clip(scale, 0, 8);
852
853 c->predictor = sample;
854 c->step = scale;
855
856 return sample;
857}
858#endif
859
860static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
861{
862 int16_t index = c->step_index;
863 uint32_t lookup_sample = ff_adpcm_step_table[index];
864 int32_t sample = 0;
865
866 if (nibble & 0x40)
867 sample += lookup_sample;
868 if (nibble & 0x20)
869 sample += lookup_sample >> 1;
870 if (nibble & 0x10)
871 sample += lookup_sample >> 2;
872 if (nibble & 0x08)
873 sample += lookup_sample >> 3;
874 if (nibble & 0x04)
875 sample += lookup_sample >> 4;
876 if (nibble & 0x02)
877 sample += lookup_sample >> 5;
878 if (nibble & 0x01)
879 sample += lookup_sample >> 6;
880 if (nibble & 0x80)
881 sample = -sample;
882
883 sample += c->predictor;
885
886 index += zork_index_table[(nibble >> 4) & 7];
887 index = av_clip(index, 0, 88);
888
889 c->predictor = sample;
890 c->step_index = index;
891
892 return sample;
893}
894
895#if CONFIG_ADPCM_XA_DECODER
896static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
897 const uint8_t *in, ADPCMChannelStatus *left,
898 ADPCMChannelStatus *right, int channels, int sample_offset)
899{
900 int i, j;
901 int shift,filter,f0,f1;
902 int s_1,s_2;
903 int d,s,t;
904
905 out0 += sample_offset;
906 if (channels == 1)
907 out1 = out0 + 28;
908 else
909 out1 += sample_offset;
910
911 for(i=0;i<4;i++) {
912 shift = 12 - (in[4+i*2] & 15);
913 filter = in[4+i*2] >> 4;
915 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
916 filter=0;
917 }
918 if (shift < 0) {
919 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
920 shift = 0;
921 }
922 f0 = xa_adpcm_table[filter][0];
923 f1 = xa_adpcm_table[filter][1];
924
925 s_1 = left->sample1;
926 s_2 = left->sample2;
927
928 for(j=0;j<28;j++) {
929 d = in[16+i+j*4];
930
931 t = sign_extend(d, 4);
932 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
933 s_2 = s_1;
934 s_1 = av_clip_int16(s);
935 out0[j] = s_1;
936 }
937
938 if (channels == 2) {
939 left->sample1 = s_1;
940 left->sample2 = s_2;
941 s_1 = right->sample1;
942 s_2 = right->sample2;
943 }
944
945 shift = 12 - (in[5+i*2] & 15);
946 filter = in[5+i*2] >> 4;
947 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
948 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
949 filter=0;
950 }
951 if (shift < 0) {
952 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
953 shift = 0;
954 }
955
956 f0 = xa_adpcm_table[filter][0];
957 f1 = xa_adpcm_table[filter][1];
958
959 for(j=0;j<28;j++) {
960 d = in[16+i+j*4];
961
962 t = sign_extend(d >> 4, 4);
963 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
964 s_2 = s_1;
965 s_1 = av_clip_int16(s);
966 out1[j] = s_1;
967 }
968
969 if (channels == 2) {
970 right->sample1 = s_1;
971 right->sample2 = s_2;
972 } else {
973 left->sample1 = s_1;
974 left->sample2 = s_2;
975 }
976
977 out0 += 28 * (3 - channels);
978 out1 += 28 * (3 - channels);
979 }
980
981 return 0;
982}
983#endif
984
985#if CONFIG_ADPCM_SWF_DECODER
986static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
987{
989 GetBitContext gb;
990 const int8_t *table;
991 int channels = avctx->ch_layout.nb_channels;
992 int k0, signmask, nb_bits, count;
993 int size = buf_size*8;
994 int i;
995
996 init_get_bits(&gb, buf, size);
997
998 //read bits & initial values
999 nb_bits = get_bits(&gb, 2)+2;
1000 table = swf_index_tables[nb_bits-2];
1001 k0 = 1 << (nb_bits-2);
1002 signmask = 1 << (nb_bits-1);
1003
1004 while (get_bits_count(&gb) <= size - 22 * channels) {
1005 for (i = 0; i < channels; i++) {
1006 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1007 c->status[i].step_index = get_bits(&gb, 6);
1008 }
1009
1010 for (count = 0; get_bits_count(&gb) <= size - nb_bits * channels && count < 4095; count++) {
1011 int i;
1012
1013 for (i = 0; i < channels; i++) {
1014 // similar to IMA adpcm
1015 int delta = get_bits(&gb, nb_bits);
1016 int step = ff_adpcm_step_table[c->status[i].step_index];
1017 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1018 int k = k0;
1019
1020 do {
1021 if (delta & k)
1022 vpdiff += step;
1023 step >>= 1;
1024 k >>= 1;
1025 } while(k);
1026 vpdiff += step;
1027
1028 if (delta & signmask)
1029 c->status[i].predictor -= vpdiff;
1030 else
1031 c->status[i].predictor += vpdiff;
1032
1033 c->status[i].step_index += table[delta & (~signmask)];
1034
1035 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1036 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1037
1038 *samples++ = c->status[i].predictor;
1039 }
1040 }
1041 }
1042}
1043#endif
1044
1046{
1047 int sample = sign_extend(nibble, 4) * (1 << shift);
1048
1049 if (flag)
1050 sample += (8 * cs->sample1) - (4 * cs->sample2);
1051 else
1052 sample += 4 * cs->sample1;
1053
1054 sample = av_clip_int16(sample >> 2);
1055
1056 cs->sample2 = cs->sample1;
1057 cs->sample1 = sample;
1058
1059 return sample;
1060}
1061
1062#if CONFIG_ADPCM_SANYO_DECODER
1063static int adpcm_sanyo_expand3(ADPCMChannelStatus *c, int bits)
1064{
1065 int sign, delta, add;
1066
1067 sign = bits & 4;
1068 if (sign)
1069 delta = 4 - (bits & 3);
1070 else
1071 delta = bits;
1072
1073 switch (delta) {
1074 case 0:
1075 add = 0;
1076 c->step = (3 * c->step) >> 2;
1077 break;
1078 case 1:
1079 add = c->step;
1080 c->step = (4 * c->step - (c->step >> 1)) >> 2;
1081 break;
1082 case 2:
1083 add = 2 * c->step;
1084 c->step = ((c->step >> 1) + add) >> 1;
1085 break;
1086 case 3:
1087 add = 4 * c->step - (c->step >> 1);
1088 c->step = 2 * c->step;
1089 break;
1090 case 4:
1091 add = (11 * c->step) >> 1;
1092 c->step = 3 * c->step;
1093 break;
1094 default:
1095 av_unreachable("There are cases for all control paths when bits is 3-bit");
1096 }
1097
1098 if (sign)
1099 add = -add;
1100
1101 c->predictor = av_clip_int16(c->predictor + add);
1102 c->step = av_clip(c->step, 1, 7281);
1103 return c->predictor;
1104}
1105
1106static int adpcm_sanyo_expand4(ADPCMChannelStatus *c, int bits)
1107{
1108 int sign, delta, add;
1109
1110 sign = bits & 8;
1111 if (sign)
1112 delta = 8 - (bits & 7);
1113 else
1114 delta = bits;
1115
1116 switch (delta) {
1117 case 0:
1118 add = 0;
1119 c->step = (3 * c->step) >> 2;
1120 break;
1121 case 1:
1122 add = c->step;
1123 c->step = (3 * c->step) >> 2;
1124 break;
1125 case 2:
1126 add = 2 * c->step;
1127 break;
1128 case 3:
1129 add = 3 * c->step;
1130 break;
1131 case 4:
1132 add = 4 * c->step;
1133 break;
1134 case 5:
1135 add = (11 * c->step) >> 1;
1136 c->step += c->step >> 2;
1137 break;
1138 case 6:
1139 add = (15 * c->step) >> 1;
1140 c->step = 2 * c->step;
1141 break;
1142 case 7:
1143 if (sign)
1144 add = (19 * c->step) >> 1;
1145 else
1146 add = (21 * c->step) >> 1;
1147 c->step = (c->step >> 1) + 2 * c->step;
1148 break;
1149 case 8:
1150 add = (25 * c->step) >> 1;
1151 c->step = 5 * c->step;
1152 break;
1153 default:
1154 av_unreachable("There are cases for all control paths when bits is 4-bit");
1155 }
1156
1157 if (sign)
1158 add = -add;
1159
1160 c->predictor = av_clip_int16(c->predictor + add);
1161 c->step = av_clip(c->step, 1, 2621);
1162 return c->predictor;
1163}
1164
1165static int adpcm_sanyo_expand5(ADPCMChannelStatus *c, int bits)
1166{
1167 int sign, delta, add;
1168
1169 sign = bits & 0x10;
1170 if (sign)
1171 delta = 16 - (bits & 0xF);
1172 else
1173 delta = bits;
1174
1175 add = delta * c->step;
1176 switch (delta) {
1177 case 0:
1178 c->step += (c->step >> 2) - (c->step >> 1);
1179 break;
1180 case 1:
1181 case 2:
1182 case 3:
1183 c->step += (c->step >> 3) - (c->step >> 2);
1184 break;
1185 case 4:
1186 case 5:
1187 c->step += (c->step >> 4) - (c->step >> 3);
1188 break;
1189 case 6:
1190 break;
1191 case 7:
1192 c->step += c->step >> 3;
1193 break;
1194 case 8:
1195 c->step += c->step >> 2;
1196 break;
1197 case 9:
1198 c->step += c->step >> 1;
1199 break;
1200 case 10:
1201 c->step = 2 * c->step - (c->step >> 3);
1202 break;
1203 case 11:
1204 c->step = 2 * c->step + (c->step >> 3);
1205 break;
1206 case 12:
1207 c->step = 2 * c->step + (c->step >> 1) - (c->step >> 3);
1208 break;
1209 case 13:
1210 c->step = 3 * c->step - (c->step >> 2);
1211 break;
1212 case 14:
1213 c->step *= 3;
1214 break;
1215 case 15:
1216 case 16:
1217 c->step = (7 * c->step) >> 1;
1218 break;
1219 }
1220
1221 if (sign)
1222 add = -add;
1223
1224 c->predictor = av_clip_int16(c->predictor + add);
1225 c->step = av_clip(c->step, 1, 1024);
1226 return c->predictor;
1227}
1228#endif
1229
1230#if CONFIG_ADPCM_IMA_CITRIX_DECODER
1231static int adpcm_citrix_expand(ADPCMChannelStatus *c, int bits)
1232{
1233 int step = ff_adpcm_step_table[c->step_index];
1234 int add = step >> 1;
1235 if ((bits & 1))
1236 add += step;
1237 if ((bits & 2))
1238 add = -add;
1239 c->step_index = av_clip(c->step_index + citrix_index_table[bits], 0, 88);
1240 c->predictor = av_clip_int16(c->predictor + add);
1241 return c->predictor;
1242}
1243#endif
1244
1245/**
1246 * Get the number of samples (per channel) that will be decoded from the packet.
1247 * In one case, this is actually the maximum number of samples possible to
1248 * decode with the given buf_size.
1249 *
1250 * @param[out] coded_samples set to the number of samples as coded in the
1251 * packet, or 0 if the codec does not encode the
1252 * number of samples in each frame.
1253 * @param[out] approx_nb_samples set to non-zero if the number of samples
1254 * returned is an approximation.
1255 */
1257 int buf_size, int *coded_samples, int *approx_nb_samples)
1258{
1259 ADPCMDecodeContext *s = avctx->priv_data;
1260 int nb_samples = 0;
1261 int ch = avctx->ch_layout.nb_channels;
1262 int has_coded_samples = 0;
1263 int header_size;
1264
1265 *coded_samples = 0;
1266 *approx_nb_samples = 0;
1267
1268 if(ch <= 0)
1269 return 0;
1270 if (buf_size > INT_MAX / 14)
1271 return 0;
1272
1273 switch (avctx->codec->id) {
1274 /* constant, only check buf_size */
1276 if (buf_size < 76 * ch)
1277 return 0;
1278 nb_samples = 128;
1279 break;
1281 if (buf_size < 34 * ch)
1282 return 0;
1283 nb_samples = 64;
1284 break;
1286 nb_samples = (buf_size / 9) * 16;
1287 break;
1288 /* simple 4-bit adpcm */
1302 nb_samples = buf_size * 2 / ch;
1303 break;
1304 }
1305 if (nb_samples)
1306 return nb_samples;
1307
1308 /* simple 4-bit adpcm, with header */
1309 header_size = 0;
1310 switch (avctx->codec->id) {
1316 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
1317 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
1318 }
1319 if (header_size > 0)
1320 return (buf_size - header_size) * 2 / ch;
1321
1322 /* more complex formats */
1323 switch (avctx->codec->id) {
1325 bytestream2_skip(gb, 4);
1326 has_coded_samples = 1;
1327 *coded_samples = bytestream2_get_le32u(gb);
1328 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
1329 bytestream2_seek(gb, -8, SEEK_CUR);
1330 break;
1332 /* Stereo is 30 bytes per block */
1333 /* Mono is 15 bytes per block */
1334 has_coded_samples = 1;
1335 *coded_samples = bytestream2_get_le32(gb);
1336 *coded_samples -= *coded_samples % 28;
1337 nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
1338 break;
1340 nb_samples = ((bytestream2_peek_be64(gb) >> 16) & 0xFFFF);
1341 break;
1343 {
1344 int frame_format = bytestream2_get_be16(gb);
1345 int skip = 6;
1346
1347 if (frame_format == 1)
1348 skip += 2 * ch;
1349 if (frame_format == 3)
1350 skip += 3 * ch;
1351
1352 nb_samples = (buf_size - skip) * 2 / ch;
1353 bytestream2_seek(gb, 0, SEEK_SET);
1354 }
1355 break;
1357 has_coded_samples = 1;
1358 *coded_samples = bytestream2_get_le32(gb);
1359 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
1360 break;
1362 nb_samples = (buf_size - ch) / ch * 2;
1363 break;
1367 /* maximum number of samples */
1368 /* has internal offsets and a per-frame switch to signal raw 16-bit */
1369 has_coded_samples = 1;
1370 switch (avctx->codec->id) {
1372 header_size = 4 + 9 * ch;
1373 *coded_samples = bytestream2_get_le32(gb);
1374 break;
1376 header_size = 4 + 5 * ch;
1377 *coded_samples = bytestream2_get_le32(gb);
1378 break;
1380 header_size = 4 + 5 * ch;
1381 *coded_samples = bytestream2_get_be32(gb);
1382 break;
1383 }
1384 *coded_samples -= *coded_samples % 28;
1385 nb_samples = (buf_size - header_size) * 2 / ch;
1386 nb_samples -= nb_samples % 28;
1387 *approx_nb_samples = 1;
1388 break;
1390 if (avctx->block_align > 0)
1391 buf_size = FFMIN(buf_size, avctx->block_align);
1392 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
1393 break;
1395 if (avctx->block_align > 0)
1396 buf_size = FFMIN(buf_size, avctx->block_align);
1397 if (buf_size < 4 * ch)
1398 return AVERROR_INVALIDDATA;
1399 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
1400 break;
1402 if (avctx->block_align > 0)
1403 buf_size = FFMIN(buf_size, avctx->block_align);
1404 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1405 break;
1407 if (avctx->block_align > 0)
1408 buf_size = FFMIN(buf_size, avctx->block_align);
1409 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1410 break;
1412 if (avctx->block_align > 0)
1413 buf_size = FFMIN(buf_size, avctx->block_align);
1414 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1415 if (ch == 1) {
1416 avpriv_request_sample(avctx, "mono ADPCM Magix");
1417 return AVERROR_PATCHWELCOME;
1418 }
1419 break;
1420 CASE(ADPCM_IMA_WAV,
1421 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1422 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1423 if (avctx->block_align > 0)
1424 buf_size = FFMIN(buf_size, avctx->block_align);
1425 if (buf_size < 4 * ch)
1426 return AVERROR_INVALIDDATA;
1427 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
1428 ) /* End of CASE */
1429 CASE(ADPCM_IMA_XBOX,
1430 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1431 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1432 if (avctx->block_align > 0)
1433 buf_size = FFMIN(buf_size, avctx->block_align);
1434 if (buf_size < 4 * ch)
1435 return AVERROR_INVALIDDATA;
1436 nb_samples = (buf_size - 4 * ch) / (bsize * ch) * bsamples + 1;
1437 ) /* End of CASE */
1439 if (avctx->block_align > 0)
1440 buf_size = FFMIN(buf_size, avctx->block_align);
1441 nb_samples = (buf_size - 6 * ch) * 2 / ch;
1442 break;
1444 if (avctx->block_align > 0)
1445 buf_size = FFMIN(buf_size, avctx->block_align);
1446 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
1447 break;
1451 {
1452 int samples_per_byte;
1453 switch (avctx->codec->id) {
1454 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
1455 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
1456 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
1457 }
1458 if (!s->status[0].step_index) {
1459 if (buf_size < ch)
1460 return AVERROR_INVALIDDATA;
1461 nb_samples++;
1462 buf_size -= ch;
1463 }
1464 nb_samples += buf_size * samples_per_byte / ch;
1465 break;
1466 }
1468 {
1469 int buf_bits = buf_size * 8 - 2;
1470 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1471 int block_hdr_size = 22 * ch;
1472 int block_size = block_hdr_size + nbits * ch * 4095;
1473 int nblocks = buf_bits / block_size;
1474 int bits_left = buf_bits - nblocks * block_size;
1475 nb_samples = nblocks * 4096;
1476 if (bits_left >= block_hdr_size)
1477 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
1478 break;
1479 }
1482 if (avctx->extradata) {
1483 nb_samples = buf_size * 14 / (8 * ch);
1484 break;
1485 }
1486 has_coded_samples = 1;
1487 bytestream2_skip(gb, 4); // channel size
1488 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
1489 bytestream2_get_le32(gb) :
1490 bytestream2_get_be32(gb);
1491 buf_size -= 8 + 36 * ch;
1492 buf_size /= ch;
1493 nb_samples = buf_size / 8 * 14;
1494 if (buf_size % 8 > 1)
1495 nb_samples += (buf_size % 8 - 1) * 2;
1496 *approx_nb_samples = 1;
1497 break;
1499 nb_samples = buf_size / (9 * ch) * 16;
1500 break;
1502 nb_samples = (buf_size / 128) * 224 / ch;
1503 break;
1505 nb_samples = buf_size / (21 * ch) * 32;
1506 break;
1509 nb_samples = buf_size / (16 * ch) * 28;
1510 break;
1512 nb_samples = ((buf_size - 1) / ch) * 2;
1513 break;
1515 nb_samples = buf_size * 2;
1516 break;
1518 nb_samples = buf_size / avctx->block_align * 32;
1519 break;
1522 nb_samples = buf_size / ch;
1523 break;
1525 if (!avctx->extradata || avctx->extradata_size != 2)
1526 return AVERROR_INVALIDDATA;
1527 nb_samples = AV_RL16(avctx->extradata);
1528 break;
1530 if (buf_size < avctx->block_align)
1531 return 0;
1532 nb_samples = (buf_size / avctx->block_align) * (1 + ((avctx->block_align / ch) - 4) * 4);
1533 break;
1534 }
1535
1536 /* validate coded sample count */
1537 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1538 return AVERROR_INVALIDDATA;
1539
1540 return nb_samples;
1541}
1542
1544 int *got_frame_ptr, AVPacket *avpkt)
1545{
1546 const uint8_t *buf = avpkt->data;
1547 int buf_size = avpkt->size;
1549 int channels = avctx->ch_layout.nb_channels;
1550 av_unused int16_t *samples;
1551 av_unused int16_t **samples_p;
1552 av_unused int st; /* stereo */
1553 int nb_samples, coded_samples, approx_nb_samples, ret;
1554 GetByteContext gb;
1555
1556 bytestream2_init(&gb, buf, buf_size);
1557 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1558 if (nb_samples <= 0) {
1559 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
1560 return AVERROR_INVALIDDATA;
1561 }
1562
1563 /* get output buffer */
1564 frame->nb_samples = nb_samples;
1565 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1566 return ret;
1567 samples = (int16_t *)frame->data[0];
1568 samples_p = (int16_t **)frame->extended_data;
1569
1570 /* use coded_samples when applicable */
1571 /* it is always <= nb_samples, so the output buffer will be large enough */
1572 if (coded_samples) {
1573 if (!approx_nb_samples && coded_samples != nb_samples)
1574 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
1575 frame->nb_samples = nb_samples = coded_samples;
1576 }
1577
1578 st = channels == 2 ? 1 : 0;
1579
1580 switch(avctx->codec->id) {
1581 CASE(ADPCM_IMA_QT,
1582 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
1583 Channel data is interleaved per-chunk. */
1584 for (int channel = 0; channel < channels; channel++) {
1585 ADPCMChannelStatus *cs = &c->status[channel];
1586 int predictor;
1587 int step_index;
1588 /* (pppppp) (piiiiiii) */
1589
1590 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
1591 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1592 step_index = predictor & 0x7F;
1593 predictor &= ~0x7F;
1594
1595 if (cs->step_index == step_index) {
1596 int diff = predictor - cs->predictor;
1597 if (diff < 0)
1598 diff = - diff;
1599 if (diff > 0x7f)
1600 goto update;
1601 } else {
1602 update:
1603 cs->step_index = step_index;
1604 cs->predictor = predictor;
1605 }
1606
1607 if (cs->step_index > 88u){
1608 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1609 channel, cs->step_index);
1610 return AVERROR_INVALIDDATA;
1611 }
1612
1613 samples = samples_p[channel];
1614
1615 for (int m = 0; m < 64; m += 2) {
1616 int byte = bytestream2_get_byteu(&gb);
1617 samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1618 samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1619 }
1620 }
1621 ) /* End of CASE */
1622 CASE(ADPCM_IMA_WAV,
1623 for (int i = 0; i < channels; i++) {
1624 ADPCMChannelStatus *cs = &c->status[i];
1625 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1626
1627 cs->step_index = bytestream2_get_byteu(&gb);
1628 bytestream2_skipu(&gb, 1);
1629 if (cs->step_index > 88u){
1630 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1631 i, cs->step_index);
1632 return AVERROR_INVALIDDATA;
1633 }
1634 }
1635
1636 if (avctx->bits_per_coded_sample != 4) {
1637 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1638 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1639 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1641
1642 for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1643 for (int i = 0; i < channels; i++) {
1644 ADPCMChannelStatus *cs = &c->status[i];
1645 samples = &samples_p[i][1 + n * samples_per_block];
1646 for (int j = 0; j < block_size; j++) {
1647 temp[j] = buf[4 * channels + block_size * n * channels +
1648 (j % 4) + (j / 4) * (channels * 4) + i * 4];
1649 }
1650 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1651 if (ret < 0)
1652 return ret;
1653 for (int m = 0; m < samples_per_block; m++) {
1654 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1655 avctx->bits_per_coded_sample);
1656 }
1657 }
1658 }
1659 bytestream2_skip(&gb, avctx->block_align - channels * 4);
1660 } else {
1661 for (int n = 0; n < (nb_samples - 1) / 8; n++) {
1662 for (int i = 0; i < channels; i++) {
1663 ADPCMChannelStatus *cs = &c->status[i];
1664 samples = &samples_p[i][1 + n * 8];
1665 for (int m = 0; m < 8; m += 2) {
1666 int v = bytestream2_get_byteu(&gb);
1667 samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, v & 0x0F);
1668 samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, v >> 4);
1669 }
1670 }
1671 }
1672 }
1673 ) /* End of CASE */
1674 CASE(ADPCM_IMA_XBOX,
1675 for (int i = 0; i < channels; i++) {
1676 ADPCMChannelStatus *cs = &c->status[i];
1677 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1678
1679 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1680 if (cs->step_index > 88u) {
1681 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1682 i, cs->step_index);
1683 return AVERROR_INVALIDDATA;
1684 }
1685 }
1686
1687 for (int n = 0; n < (nb_samples-1) / 8; n++) {
1688 for (int i = 0; i < channels; i++) {
1689 ADPCMChannelStatus *cs = &c->status[i];
1690 samples = &samples_p[i][1 + n * 8];
1691 for (int m = 0; m < 8; m += 2) {
1692 int v = bytestream2_get_byteu(&gb);
1693 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1694 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1695 }
1696 }
1697 }
1698 frame->nb_samples--;
1699 ) /* End of CASE */
1700 CASE(ADPCM_4XM,
1701 for (int i = 0; i < channels; i++)
1702 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1703
1704 for (int i = 0; i < channels; i++) {
1705 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1706 if (c->status[i].step_index > 88u) {
1707 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1708 i, c->status[i].step_index);
1709 return AVERROR_INVALIDDATA;
1710 }
1711 }
1712
1713 for (int i = 0; i < channels; i++) {
1714 ADPCMChannelStatus *cs = &c->status[i];
1715 samples = (int16_t *)frame->data[i];
1716 for (int n = nb_samples >> 1; n > 0; n--) {
1717 int v = bytestream2_get_byteu(&gb);
1718 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1719 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1720 }
1721 }
1722 ) /* End of CASE */
1723 CASE(ADPCM_AGM,
1724 for (int i = 0; i < channels; i++)
1725 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1726 for (int i = 0; i < channels; i++)
1727 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1728
1729 for (int n = 0; n < nb_samples >> (1 - st); n++) {
1730 int v = bytestream2_get_byteu(&gb);
1731 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1732 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1733 }
1734 ) /* End of CASE */
1735 CASE(ADPCM_MS,
1736 int block_predictor;
1737
1738 if (avctx->ch_layout.nb_channels > 2) {
1739 for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
1740 samples = samples_p[channel];
1741 block_predictor = bytestream2_get_byteu(&gb);
1742 if (block_predictor > 6) {
1743 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1744 channel, block_predictor);
1745 return AVERROR_INVALIDDATA;
1746 }
1747 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1748 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1749 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1750 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1751 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1752 *samples++ = c->status[channel].sample2;
1753 *samples++ = c->status[channel].sample1;
1754 for (int n = (nb_samples - 2) >> 1; n > 0; n--) {
1755 int byte = bytestream2_get_byteu(&gb);
1756 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1757 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1758 }
1759 }
1760 } else {
1761 block_predictor = bytestream2_get_byteu(&gb);
1762 if (block_predictor > 6) {
1763 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1764 block_predictor);
1765 return AVERROR_INVALIDDATA;
1766 }
1767 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1768 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1769 if (st) {
1770 block_predictor = bytestream2_get_byteu(&gb);
1771 if (block_predictor > 6) {
1772 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1773 block_predictor);
1774 return AVERROR_INVALIDDATA;
1775 }
1776 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1777 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1778 }
1779 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1780 if (st){
1781 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1782 }
1783
1784 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1785 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1786 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1787 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1788
1789 *samples++ = c->status[0].sample2;
1790 if (st) *samples++ = c->status[1].sample2;
1791 *samples++ = c->status[0].sample1;
1792 if (st) *samples++ = c->status[1].sample1;
1793 for (int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1794 int byte = bytestream2_get_byteu(&gb);
1795 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1796 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1797 }
1798 }
1799 ) /* End of CASE */
1800 CASE(ADPCM_MTAF,
1801 for (int channel = 0; channel < channels; channel += 2) {
1802 bytestream2_skipu(&gb, 4);
1803 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1804 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1805 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1806 bytestream2_skipu(&gb, 2);
1807 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1808 bytestream2_skipu(&gb, 2);
1809 for (int n = 0; n < nb_samples; n += 2) {
1810 int v = bytestream2_get_byteu(&gb);
1811 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1812 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1813 }
1814 for (int n = 0; n < nb_samples; n += 2) {
1815 int v = bytestream2_get_byteu(&gb);
1816 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1817 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1818 }
1819 }
1820 ) /* End of CASE */
1821 CASE(ADPCM_IMA_DK4,
1822 for (int channel = 0; channel < channels; channel++) {
1823 ADPCMChannelStatus *cs = &c->status[channel];
1824 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1825 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1826 if (cs->step_index > 88u){
1827 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1828 channel, cs->step_index);
1829 return AVERROR_INVALIDDATA;
1830 }
1831 }
1832 for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1833 int v = bytestream2_get_byteu(&gb);
1834 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1835 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1836 }
1837 ) /* End of CASE */
1838
1839 /* DK3 ADPCM support macro */
1840#define DK3_GET_NEXT_NIBBLE() \
1841 if (decode_top_nibble_next) { \
1842 nibble = last_byte >> 4; \
1843 decode_top_nibble_next = 0; \
1844 } else { \
1845 last_byte = bytestream2_get_byteu(&gb); \
1846 nibble = last_byte & 0x0F; \
1847 decode_top_nibble_next = 1; \
1848 }
1849 CASE(ADPCM_IMA_DK3,
1850 int last_byte = 0;
1851 int nibble;
1852 int decode_top_nibble_next = 0;
1853 int diff_channel;
1854 const int16_t *samples_end = samples + channels * nb_samples;
1855
1856 bytestream2_skipu(&gb, 10);
1857 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1858 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1859 c->status[0].step_index = bytestream2_get_byteu(&gb);
1860 c->status[1].step_index = bytestream2_get_byteu(&gb);
1861 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1862 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1863 c->status[0].step_index, c->status[1].step_index);
1864 return AVERROR_INVALIDDATA;
1865 }
1866 /* sign extend the predictors */
1867 diff_channel = c->status[1].predictor;
1868
1869 while (samples < samples_end) {
1870
1871 /* for this algorithm, c->status[0] is the sum channel and
1872 * c->status[1] is the diff channel */
1873
1874 /* process the first predictor of the sum channel */
1876 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1877
1878 /* process the diff channel predictor */
1880 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1881
1882 /* process the first pair of stereo PCM samples */
1883 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1884 *samples++ = c->status[0].predictor + c->status[1].predictor;
1885 *samples++ = c->status[0].predictor - c->status[1].predictor;
1886
1887 /* process the second predictor of the sum channel */
1889 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1890
1891 /* process the second pair of stereo PCM samples */
1892 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1893 *samples++ = c->status[0].predictor + c->status[1].predictor;
1894 *samples++ = c->status[0].predictor - c->status[1].predictor;
1895 }
1896
1897 if ((bytestream2_tell(&gb) & 1))
1898 bytestream2_skip(&gb, 1);
1899 ) /* End of CASE */
1900 CASE(ADPCM_IMA_MAGIX,
1901 for (int channel = 0; channel < channels; channel++) {
1902 ADPCMChannelStatus *cs = &c->status[channel];
1903 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1904 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1905 if (cs->step_index > 88u){
1906 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1907 channel, cs->step_index);
1908 return AVERROR_INVALIDDATA;
1909 }
1910 }
1911
1912 for (int m = 0; m < channels*nb_samples/16; m ++) {
1913 uint32_t v0 = bytestream2_get_le32u(&gb);
1914 uint32_t v1 = bytestream2_get_le32u(&gb);
1915
1916 for (int n = 8; n > 0; n--, v0 >>= 4, v1 >>= 4, samples += 2) {
1917 samples[0] = adpcm_ima_expand_nibble(&c->status[0], v0 & 15, 3);
1918 samples[1] = adpcm_ima_expand_nibble(&c->status[1], v1 & 15, 3);
1919 }
1920 }
1921 ) /* End of CASE */
1922 CASE(ADPCM_IMA_ISS,
1923 for (int channel = 0; channel < channels; channel++) {
1924 ADPCMChannelStatus *cs = &c->status[channel];
1925 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1926 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1927 if (cs->step_index > 88u){
1928 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1929 channel, cs->step_index);
1930 return AVERROR_INVALIDDATA;
1931 }
1932 }
1933
1934 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1935 int v1, v2;
1936 int v = bytestream2_get_byteu(&gb);
1937 /* nibbles are swapped for mono */
1938 if (st) {
1939 v1 = v >> 4;
1940 v2 = v & 0x0F;
1941 } else {
1942 v2 = v >> 4;
1943 v1 = v & 0x0F;
1944 }
1945 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1946 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1947 }
1948 ) /* End of CASE */
1949 CASE(ADPCM_IMA_MOFLEX,
1950 for (int channel = 0; channel < channels; channel++) {
1951 ADPCMChannelStatus *cs = &c->status[channel];
1952 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1953 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1954 if (cs->step_index > 88u){
1955 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1956 channel, cs->step_index);
1957 return AVERROR_INVALIDDATA;
1958 }
1959 }
1960
1961 for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1962 for (int channel = 0; channel < channels; channel++) {
1963 samples = samples_p[channel] + 256 * subframe;
1964 for (int n = 0; n < 256; n += 2) {
1965 int v = bytestream2_get_byteu(&gb);
1966 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1967 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1968 }
1969 }
1970 }
1971 ) /* End of CASE */
1972 CASE(ADPCM_IMA_DAT4,
1973 for (int channel = 0; channel < channels; channel++) {
1974 ADPCMChannelStatus *cs = &c->status[channel];
1975 samples = samples_p[channel];
1976 bytestream2_skip(&gb, 4);
1977 for (int n = 0; n < nb_samples; n += 2) {
1978 int v = bytestream2_get_byteu(&gb);
1979 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1980 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1981 }
1982 }
1983 ) /* End of CASE */
1984 CASE(ADPCM_IMA_APC,
1985 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1986 int v = bytestream2_get_byteu(&gb);
1987 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1988 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1989 }
1990 ) /* End of CASE */
1991 CASE(ADPCM_IMA_HVQM2,
1992 int format = bytestream2_get_be16(&gb);
1993
1994 bytestream2_skip(&gb, 4);
1995 decode_adpcm_ima_hvqm2(avctx, samples, nb_samples, format, &gb);
1996 ) /* End of CASE */
1997 CASE(ADPCM_IMA_HVQM4,
1998 int format = bytestream2_get_be16(&gb);
1999
2000 bytestream2_skip(&gb, 4);
2001 decode_adpcm_ima_hvqm4(avctx, samples, nb_samples, format, &gb);
2002 ) /* End of CASE */
2003 CASE(ADPCM_IMA_SSI,
2004 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2005 int v = bytestream2_get_byteu(&gb);
2006 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
2007 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
2008 }
2009 ) /* End of CASE */
2010 CASE(ADPCM_IMA_APM,
2011 for (int n = nb_samples / 2; n > 0; n--) {
2012 for (int channel = 0; channel < channels; channel++) {
2013 int v = bytestream2_get_byteu(&gb);
2014 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
2015 samples[st] = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
2016 }
2017 samples += channels;
2018 }
2019 ) /* End of CASE */
2020 CASE(ADPCM_IMA_ALP,
2021 for (int n = nb_samples / 2; n > 0; n--) {
2022 for (int channel = 0; channel < channels; channel++) {
2023 int v = bytestream2_get_byteu(&gb);
2024 *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
2025 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
2026 }
2027 samples += channels;
2028 }
2029 ) /* End of CASE */
2030 CASE(ADPCM_IMA_CUNNING,
2031 for (int channel = 0; channel < channels; channel++) {
2032 int16_t *smp = samples_p[channel];
2033 for (int n = 0; n < nb_samples / 2; n++) {
2034 int v = bytestream2_get_byteu(&gb);
2035 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
2036 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
2037 }
2038 }
2039 ) /* End of CASE */
2040 CASE(ADPCM_IMA_OKI,
2041 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2042 int v = bytestream2_get_byteu(&gb);
2043 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
2044 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
2045 }
2046 ) /* End of CASE */
2047 CASE(ADPCM_IMA_RAD,
2048 for (int channel = 0; channel < channels; channel++) {
2049 ADPCMChannelStatus *cs = &c->status[channel];
2050 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
2051 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2052 if (cs->step_index > 88u){
2053 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2054 channel, cs->step_index);
2055 return AVERROR_INVALIDDATA;
2056 }
2057 }
2058 for (int n = 0; n < nb_samples / 2; n++) {
2059 int byte[2];
2060
2061 byte[0] = bytestream2_get_byteu(&gb);
2062 if (st)
2063 byte[1] = bytestream2_get_byteu(&gb);
2064 for (int channel = 0; channel < channels; channel++) {
2065 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
2066 }
2067 for (int channel = 0; channel < channels; channel++) {
2068 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
2069 }
2070 }
2071 ) /* End of CASE */
2072 CASE(ADPCM_IMA_WS,
2073 if (c->vqa_version == 3) {
2074 for (int channel = 0; channel < channels; channel++) {
2075 int16_t *smp = samples_p[channel];
2076
2077 for (int n = nb_samples / 2; n > 0; n--) {
2078 int v = bytestream2_get_byteu(&gb);
2079 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
2080 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
2081 }
2082 }
2083 } else {
2084 for (int n = nb_samples / 2; n > 0; n--) {
2085 for (int channel = 0; channel < channels; channel++) {
2086 int v = bytestream2_get_byteu(&gb);
2087 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
2088 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
2089 }
2090 samples += channels;
2091 }
2092 }
2093 bytestream2_seek(&gb, 0, SEEK_END);
2094 ) /* End of CASE */
2095 CASE(ADPCM_XMD,
2096 int bytes_remaining, block = 0;
2097 while (bytestream2_get_bytes_left(&gb) >= 21 * channels) {
2098 for (int channel = 0; channel < channels; channel++) {
2099 int16_t *out = samples_p[channel] + block * 32;
2100 int16_t history[2];
2101 uint16_t scale;
2102
2103 history[1] = sign_extend(bytestream2_get_le16(&gb), 16);
2104 history[0] = sign_extend(bytestream2_get_le16(&gb), 16);
2105 scale = bytestream2_get_le16(&gb);
2106
2107 out[0] = history[1];
2108 out[1] = history[0];
2109
2110 for (int n = 0; n < 15; n++) {
2111 unsigned byte = bytestream2_get_byte(&gb);
2112 int32_t nibble[2];
2113
2114 nibble[0] = sign_extend(byte & 15, 4);
2115 nibble[1] = sign_extend(byte >> 4, 4);
2116
2117 out[2+n*2] = nibble[0]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
2118 history[1] = history[0];
2119 history[0] = out[2+n*2];
2120
2121 out[2+n*2+1] = nibble[1]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
2122 history[1] = history[0];
2123 history[0] = out[2+n*2+1];
2124 }
2125 }
2126
2127 block++;
2128 }
2129 bytes_remaining = bytestream2_get_bytes_left(&gb);
2130 if (bytes_remaining > 0) {
2131 bytestream2_skip(&gb, bytes_remaining);
2132 }
2133 ) /* End of CASE */
2134 CASE(ADPCM_XA,
2135 int16_t *out0 = samples_p[0];
2136 int16_t *out1 = samples_p[1];
2137 int samples_per_block = 28 * (3 - channels) * 4;
2138 int sample_offset = 0;
2139 int bytes_remaining;
2140 while (bytestream2_get_bytes_left(&gb) >= 128) {
2141 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
2142 &c->status[0], &c->status[1],
2143 channels, sample_offset)) < 0)
2144 return ret;
2145 bytestream2_skipu(&gb, 128);
2146 sample_offset += samples_per_block;
2147 }
2148 /* Less than a full block of data left, e.g. when reading from
2149 * 2324 byte per sector XA; the remainder is padding */
2150 bytes_remaining = bytestream2_get_bytes_left(&gb);
2151 if (bytes_remaining > 0) {
2152 bytestream2_skip(&gb, bytes_remaining);
2153 }
2154 ) /* End of CASE */
2155 CASE(ADPCM_IMA_ESCAPE,
2156 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2157 int byte = bytestream2_get_byteu(&gb);
2158 *samples++ = adpcm_ima_escape_expand_nibble(&c->status[0], byte >> 4);
2159 *samples++ = adpcm_ima_escape_expand_nibble(&c->status[st], byte & 0xF);
2160 }
2161 ) /* End of CASE */
2162 CASE(ADPCM_IMA_EA_EACS,
2163 for (int i = 0; i <= st; i++) {
2164 c->status[i].step_index = bytestream2_get_le32u(&gb);
2165 if (c->status[i].step_index > 88u) {
2166 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2167 i, c->status[i].step_index);
2168 return AVERROR_INVALIDDATA;
2169 }
2170 }
2171 for (int i = 0; i <= st; i++) {
2172 c->status[i].predictor = bytestream2_get_le32u(&gb);
2173 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
2174 return AVERROR_INVALIDDATA;
2175 }
2176
2177 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2178 int byte = bytestream2_get_byteu(&gb);
2179 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
2180 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
2181 }
2182 ) /* End of CASE */
2183 CASE(ADPCM_IMA_EA_SEAD,
2184 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2185 int byte = bytestream2_get_byteu(&gb);
2186 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
2187 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
2188 }
2189 ) /* End of CASE */
2190 CASE(ADPCM_EA,
2191 int previous_left_sample, previous_right_sample;
2192 int current_left_sample, current_right_sample;
2193 int next_left_sample, next_right_sample;
2194 int coeff1l, coeff2l, coeff1r, coeff2r;
2195 int shift_left, shift_right;
2196
2197 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte (stereo) or 15-byte (mono) pieces,
2198 each coding 28 stereo/mono samples. */
2199
2200 if (channels != 2 && channels != 1)
2201 return AVERROR_INVALIDDATA;
2202
2203 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2204 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2205 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2206 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2207
2208 for (int count1 = 0; count1 < nb_samples / 28; count1++) {
2209 int byte = bytestream2_get_byteu(&gb);
2210 coeff1l = ea_adpcm_table[ byte >> 4 ];
2211 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
2212 coeff1r = ea_adpcm_table[ byte & 0x0F];
2213 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
2214
2215 if (channels == 2){
2216 byte = bytestream2_get_byteu(&gb);
2217 shift_left = 20 - (byte >> 4);
2218 shift_right = 20 - (byte & 0x0F);
2219 } else{
2220 /* Mono packs the shift into the coefficient byte's lower nibble instead */
2221 shift_left = 20 - (byte & 0x0F);
2222 }
2223
2224 for (int count2 = 0; count2 < (channels == 2 ? 28 : 14); count2++) {
2225 byte = bytestream2_get_byteu(&gb);
2226 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
2227
2228 next_left_sample = (next_left_sample +
2229 (current_left_sample * coeff1l) +
2230 (previous_left_sample * coeff2l) + 0x80) >> 8;
2231
2232 previous_left_sample = current_left_sample;
2233 current_left_sample = av_clip_int16(next_left_sample);
2234 *samples++ = current_left_sample;
2235
2236 if (channels == 2){
2237 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
2238
2239 next_right_sample = (next_right_sample +
2240 (current_right_sample * coeff1r) +
2241 (previous_right_sample * coeff2r) + 0x80) >> 8;
2242
2243 previous_right_sample = current_right_sample;
2244 current_right_sample = av_clip_int16(next_right_sample);
2245 *samples++ = current_right_sample;
2246 } else {
2247 next_left_sample = sign_extend(byte, 4) * (1 << shift_left);
2248
2249 next_left_sample = (next_left_sample +
2250 (current_left_sample * coeff1l) +
2251 (previous_left_sample * coeff2l) + 0x80) >> 8;
2252
2253 previous_left_sample = current_left_sample;
2254 current_left_sample = av_clip_int16(next_left_sample);
2255
2256 *samples++ = current_left_sample;
2257 }
2258 }
2259 }
2260 bytestream2_skip(&gb, channels == 2 ? 2 : 3); // Skip terminating NULs
2261 ) /* End of CASE */
2262 CASE(ADPCM_EA_MAXIS_XA,
2263 int coeff[2][2], shift[2];
2264
2265 for (int channel = 0; channel < channels; channel++) {
2266 int byte = bytestream2_get_byteu(&gb);
2267 for (int i = 0; i < 2; i++)
2268 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
2269 shift[channel] = 20 - (byte & 0x0F);
2270 }
2271 for (int count1 = 0; count1 < nb_samples / 2; count1++) {
2272 int byte[2];
2273
2274 byte[0] = bytestream2_get_byteu(&gb);
2275 if (st) byte[1] = bytestream2_get_byteu(&gb);
2276 for (int i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
2277 for (int channel = 0; channel < channels; channel++) {
2278 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
2279 sample = (sample +
2280 c->status[channel].sample1 * coeff[channel][0] +
2281 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
2282 c->status[channel].sample2 = c->status[channel].sample1;
2283 c->status[channel].sample1 = av_clip_int16(sample);
2284 *samples++ = c->status[channel].sample1;
2285 }
2286 }
2287 }
2288 bytestream2_seek(&gb, 0, SEEK_END);
2289 ) /* End of CASE */
2290#if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER
2294 /* channel numbering
2295 2chan: 0=fl, 1=fr
2296 4chan: 0=fl, 1=rl, 2=fr, 3=rr
2297 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
2298 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
2299 int previous_sample, current_sample, next_sample;
2300 int coeff1, coeff2;
2301 int shift;
2302 uint16_t *samplesC;
2303 int count = 0;
2304 int offsets[6];
2305
2306 for (unsigned channel = 0; channel < channels; channel++)
2307 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
2308 bytestream2_get_le32(&gb)) +
2309 (channels + 1) * 4;
2310
2311 for (unsigned channel = 0; channel < channels; channel++) {
2312 int count1;
2313
2314 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
2315 samplesC = samples_p[channel];
2316
2317 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
2318 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2319 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2320 } else {
2321 current_sample = c->status[channel].predictor;
2322 previous_sample = c->status[channel].prev_sample;
2323 }
2324
2325 for (count1 = 0; count1 < nb_samples / 28; count1++) {
2326 int byte = bytestream2_get_byte(&gb);
2327 if (byte == 0xEE) { /* only seen in R2 and R3 */
2328 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2329 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2330
2331 for (int count2 = 0; count2 < 28; count2++)
2332 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
2333 } else {
2334 coeff1 = ea_adpcm_table[ byte >> 4 ];
2335 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
2336 shift = 20 - (byte & 0x0F);
2337
2338 for (int count2 = 0; count2 < 28; count2++) {
2339 if (count2 & 1)
2340 next_sample = (unsigned)sign_extend(byte, 4) << shift;
2341 else {
2342 byte = bytestream2_get_byte(&gb);
2343 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
2344 }
2345
2346 next_sample += (current_sample * coeff1) +
2347 (previous_sample * coeff2);
2348 next_sample = av_clip_int16(next_sample >> 8);
2349
2350 previous_sample = current_sample;
2351 current_sample = next_sample;
2352 *samplesC++ = current_sample;
2353 }
2354 }
2355 }
2356 if (!count) {
2357 count = count1;
2358 } else if (count != count1) {
2359 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
2360 count = FFMAX(count, count1);
2361 }
2362
2363 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
2364 c->status[channel].predictor = current_sample;
2365 c->status[channel].prev_sample = previous_sample;
2366 }
2367 }
2368
2369 frame->nb_samples = count * 28;
2370 bytestream2_seek(&gb, 0, SEEK_END);
2371 break;
2372 }
2373#endif /* CONFIG_ADPCM_EA_Rx_DECODER */
2374 CASE(ADPCM_EA_XAS,
2375 for (int channel=0; channel < channels; channel++) {
2376 int coeff[2][4], shift[4];
2377 int16_t *s = samples_p[channel];
2378 for (int n = 0; n < 4; n++, s += 32) {
2379 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
2380 for (int i = 0; i < 2; i++)
2381 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
2382 s[0] = val & ~0x0F;
2383
2384 val = sign_extend(bytestream2_get_le16u(&gb), 16);
2385 shift[n] = 20 - (val & 0x0F);
2386 s[1] = val & ~0x0F;
2387 }
2388
2389 for (int m = 2; m < 32; m += 2) {
2390 s = &samples_p[channel][m];
2391 for (int n = 0; n < 4; n++, s += 32) {
2392 int level, pred;
2393 int byte = bytestream2_get_byteu(&gb);
2394
2395 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
2396 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
2397 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
2398
2399 level = sign_extend(byte, 4) * (1 << shift[n]);
2400 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
2401 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
2402 }
2403 }
2404 }
2405 ) /* End of CASE */
2406 CASE(ADPCM_IMA_ACORN,
2407 for (int channel = 0; channel < channels; channel++) {
2408 ADPCMChannelStatus *cs = &c->status[channel];
2409 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2410 cs->step_index = bytestream2_get_le16u(&gb) & 0xFF;
2411 if (cs->step_index > 88u){
2412 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2413 channel, cs->step_index);
2414 return AVERROR_INVALIDDATA;
2415 }
2416 }
2417 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2418 int byte = bytestream2_get_byteu(&gb);
2419 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3);
2420 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3);
2421 }
2422 ) /* End of CASE */
2423 CASE(ADPCM_IMA_AMV,
2424 av_assert0(channels == 1);
2425
2426 /*
2427 * Header format:
2428 * int16_t predictor;
2429 * uint8_t step_index;
2430 * uint8_t reserved;
2431 * uint32_t frame_size;
2432 *
2433 * Some implementations have step_index as 16-bits, but others
2434 * only use the lower 8 and store garbage in the upper 8.
2435 */
2436 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2437 c->status[0].step_index = bytestream2_get_byteu(&gb);
2438 bytestream2_skipu(&gb, 5);
2439 if (c->status[0].step_index > 88u) {
2440 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2441 c->status[0].step_index);
2442 return AVERROR_INVALIDDATA;
2443 }
2444
2445 for (int n = nb_samples >> 1; n > 0; n--) {
2446 int v = bytestream2_get_byteu(&gb);
2447
2448 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2449 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
2450 }
2451
2452 if (nb_samples & 1) {
2453 int v = bytestream2_get_byteu(&gb);
2454 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2455
2456 if (v & 0x0F) {
2457 /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
2458 av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
2459 av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
2460 }
2461 }
2462 ) /* End of CASE */
2463 CASE(ADPCM_IMA_PDA,
2464 for (int i = 0; i < channels; i++) {
2465 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2466 c->status[i].step_index = bytestream2_get_byteu(&gb);
2467 bytestream2_skipu(&gb, 1);
2468 if (c->status[i].step_index > 88u) {
2469 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2470 c->status[i].step_index);
2471 return AVERROR_INVALIDDATA;
2472 }
2473 }
2474
2475 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2476 int v = bytestream2_get_byteu(&gb);
2477
2478 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2479 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2480 }
2481 ) /* End of CASE */
2482 CASE(ADPCM_IMA_SMJPEG,
2483 for (int i = 0; i < channels; i++) {
2484 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
2485 c->status[i].step_index = bytestream2_get_byteu(&gb);
2486 bytestream2_skipu(&gb, 1);
2487 if (c->status[i].step_index > 88u) {
2488 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2489 c->status[i].step_index);
2490 return AVERROR_INVALIDDATA;
2491 }
2492 }
2493
2494 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2495 int v = bytestream2_get_byteu(&gb);
2496
2497 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2498 *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2499 }
2500 ) /* End of CASE */
2501 CASE(ADPCM_CT,
2502 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2503 int v = bytestream2_get_byteu(&gb);
2504 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
2505 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
2506 }
2507 ) /* End of CASE */
2508#if CONFIG_ADPCM_SBPRO_2_DECODER || CONFIG_ADPCM_SBPRO_3_DECODER || \
2509 CONFIG_ADPCM_SBPRO_4_DECODER
2513 if (!c->status[0].step_index) {
2514 /* the first byte is a raw sample */
2515 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2516 if (st)
2517 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2518 c->status[0].step_index = 1;
2519 nb_samples--;
2520 }
2521 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
2522 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2523 int byte = bytestream2_get_byteu(&gb);
2524 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2525 byte >> 4, 4, 0);
2526 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2527 byte & 0x0F, 4, 0);
2528 }
2529 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
2530 for (int n = (nb_samples<<st) / 3; n > 0; n--) {
2531 int byte = bytestream2_get_byteu(&gb);
2532 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2533 byte >> 5 , 3, 0);
2534 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2535 (byte >> 2) & 0x07, 3, 0);
2536 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2537 byte & 0x03, 2, 0);
2538 }
2539 } else {
2540 for (int n = nb_samples >> (2 - st); n > 0; n--) {
2541 int byte = bytestream2_get_byteu(&gb);
2542 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2543 byte >> 6 , 2, 2);
2544 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2545 (byte >> 4) & 0x03, 2, 2);
2546 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2547 (byte >> 2) & 0x03, 2, 2);
2548 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2549 byte & 0x03, 2, 2);
2550 }
2551 }
2552 break;
2553#endif /* CONFIG_ADPCM_SBPRO_x_DECODER */
2554 CASE(ADPCM_SWF,
2555 adpcm_swf_decode(avctx, buf, buf_size, samples);
2556 bytestream2_seek(&gb, 0, SEEK_END);
2557 ) /* End of CASE */
2558 CASE(ADPCM_YAMAHA,
2559 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2560 int v = bytestream2_get_byteu(&gb);
2561 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
2562 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
2563 }
2564 ) /* End of CASE */
2565 CASE(ADPCM_AICA,
2566 for (int channel = 0; channel < channels; channel++) {
2567 samples = samples_p[channel];
2568 for (int n = nb_samples >> 1; n > 0; n--) {
2569 int v = bytestream2_get_byteu(&gb);
2570 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
2571 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
2572 }
2573 }
2574 ) /* End of CASE */
2575 CASE(ADPCM_AFC,
2576 int samples_per_block;
2577 int blocks;
2578
2579 if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
2580 samples_per_block = avctx->extradata[0] / 16;
2581 blocks = nb_samples / avctx->extradata[0];
2582 } else {
2583 samples_per_block = nb_samples / 16;
2584 blocks = 1;
2585 }
2586
2587 for (int m = 0; m < blocks; m++) {
2588 for (int channel = 0; channel < channels; channel++) {
2589 int prev1 = c->status[channel].sample1;
2590 int prev2 = c->status[channel].sample2;
2591
2592 samples = samples_p[channel] + m * 16;
2593 /* Read in every sample for this channel. */
2594 for (int i = 0; i < samples_per_block; i++) {
2595 int byte = bytestream2_get_byteu(&gb);
2596 int scale = 1 << (byte >> 4);
2597 int index = byte & 0xf;
2598 int factor1 = afc_coeffs[0][index];
2599 int factor2 = afc_coeffs[1][index];
2600
2601 /* Decode 16 samples. */
2602 for (int n = 0; n < 16; n++) {
2603 int32_t sampledat;
2604
2605 if (n & 1) {
2606 sampledat = sign_extend(byte, 4);
2607 } else {
2608 byte = bytestream2_get_byteu(&gb);
2609 sampledat = sign_extend(byte >> 4, 4);
2610 }
2611
2612 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
2613 sampledat * scale;
2614 *samples = av_clip_int16(sampledat);
2615 prev2 = prev1;
2616 prev1 = *samples++;
2617 }
2618 }
2619
2620 c->status[channel].sample1 = prev1;
2621 c->status[channel].sample2 = prev2;
2622 }
2623 }
2624 bytestream2_seek(&gb, 0, SEEK_END);
2625 ) /* End of CASE */
2626#if CONFIG_ADPCM_THP_DECODER || CONFIG_ADPCM_THP_LE_DECODER
2629 {
2630 int table[14][16];
2631
2632#define THP_GET16(g) \
2633 sign_extend( \
2634 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
2635 bytestream2_get_le16u(&(g)) : \
2636 bytestream2_get_be16u(&(g)), 16)
2637
2638 if (avctx->extradata) {
2639 GetByteContext tb;
2640 if (avctx->extradata_size < 32 * channels) {
2641 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
2642 return AVERROR_INVALIDDATA;
2643 }
2644
2645 bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
2646 for (int i = 0; i < channels; i++)
2647 for (int n = 0; n < 16; n++)
2648 table[i][n] = THP_GET16(tb);
2649 } else {
2650 for (int i = 0; i < channels; i++)
2651 for (int n = 0; n < 16; n++)
2652 table[i][n] = THP_GET16(gb);
2653
2654 if (!c->has_status) {
2655 /* Initialize the previous sample. */
2656 for (int i = 0; i < channels; i++) {
2657 c->status[i].sample1 = THP_GET16(gb);
2658 c->status[i].sample2 = THP_GET16(gb);
2659 }
2660 c->has_status = 1;
2661 } else {
2662 bytestream2_skip(&gb, channels * 4);
2663 }
2664 }
2665
2666 for (int ch = 0; ch < channels; ch++) {
2667 samples = samples_p[ch];
2668
2669 /* Read in every sample for this channel. */
2670 for (int i = 0; i < (nb_samples + 13) / 14; i++) {
2671 int byte = bytestream2_get_byteu(&gb);
2672 int index = (byte >> 4) & 7;
2673 unsigned int exp = byte & 0x0F;
2674 int64_t factor1 = table[ch][index * 2];
2675 int64_t factor2 = table[ch][index * 2 + 1];
2676
2677 /* Decode 14 samples. */
2678 for (int n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
2679 int32_t sampledat;
2680
2681 if (n & 1) {
2682 sampledat = sign_extend(byte, 4);
2683 } else {
2684 byte = bytestream2_get_byteu(&gb);
2685 sampledat = sign_extend(byte >> 4, 4);
2686 }
2687
2688 sampledat = ((c->status[ch].sample1 * factor1
2689 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
2690 *samples = av_clip_int16(sampledat);
2691 c->status[ch].sample2 = c->status[ch].sample1;
2692 c->status[ch].sample1 = *samples++;
2693 }
2694 }
2695 }
2696 break;
2697 }
2698#endif /* CONFIG_ADPCM_THP(_LE)_DECODER */
2699 CASE(ADPCM_DTK,
2700 for (int channel = 0; channel < channels; channel++) {
2701 samples = samples_p[channel];
2702
2703 /* Read in every sample for this channel. */
2704 for (int i = 0; i < nb_samples / 28; i++) {
2705 int byte, header;
2706 if (channel)
2707 bytestream2_skipu(&gb, 1);
2708 header = bytestream2_get_byteu(&gb);
2709 bytestream2_skipu(&gb, 3 - channel);
2710
2711 /* Decode 28 samples. */
2712 for (int n = 0; n < 28; n++) {
2713 int32_t sampledat, prev;
2714
2715 switch (header >> 4) {
2716 case 1:
2717 prev = (c->status[channel].sample1 * 0x3c);
2718 break;
2719 case 2:
2720 prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
2721 break;
2722 case 3:
2723 prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
2724 break;
2725 default:
2726 prev = 0;
2727 }
2728
2729 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
2730
2731 byte = bytestream2_get_byteu(&gb);
2732 if (!channel)
2733 sampledat = sign_extend(byte, 4);
2734 else
2735 sampledat = sign_extend(byte >> 4, 4);
2736
2737 sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
2738 *samples++ = av_clip_int16(sampledat >> 6);
2739 c->status[channel].sample2 = c->status[channel].sample1;
2740 c->status[channel].sample1 = sampledat;
2741 }
2742 }
2743 if (!channel)
2744 bytestream2_seek(&gb, 0, SEEK_SET);
2745 }
2746 ) /* End of CASE */
2747 CASE(ADPCM_N64,
2748 ADPCMChannelStatus *cs = &c->status[0];
2749 int coefs[8*2*8] = { 0 };
2750
2751 if (avctx->extradata) {
2752 int version, order, entries;
2753 GetByteContext cb;
2754
2755 bytestream2_init(&cb, avctx->extradata, avctx->extradata_size);
2756
2757 version = bytestream2_get_be16(&cb);
2758 order = bytestream2_get_be16(&cb);
2759 entries = bytestream2_get_be16(&cb);
2760 if (version != 1 || order != 2 || entries > 8)
2761 return AVERROR_INVALIDDATA;
2762
2763 for (int n = 0; n < order * entries * 8; n++)
2764 coefs[n] = sign_extend(bytestream2_get_be16(&cb), 16);
2765 }
2766
2767 for (int block = 0; block < avpkt->size / 9; block++) {
2768 int scale, index, codes[16];
2769 int16_t hist[8] = { 0 };
2770 const int order = 2;
2771 int16_t out[16];
2772
2773 hist[6] = cs->sample2;
2774 hist[7] = cs->sample1;
2775
2776 samples = samples_p[0] + block * 16;
2777
2778 scale = (buf[0] >> 4) & 0xF;
2779 index = (buf[0] >> 0) & 0xF;
2780 scale = 1 << scale;
2781 index = FFMIN(index, 8);
2782
2783 for (int i = 0, j = 0; i < 16; i += 2, j++) {
2784 int n0 = (buf[j+1] >> 4) & 0xF;
2785 int n1 = (buf[j+1] >> 0) & 0xF;
2786
2787 if (n0 & 8)
2788 n0 = n0 - 16;
2789 if (n1 & 8)
2790 n1 = n1 - 16;
2791
2792 codes[i+0] = n0 * scale;
2793 codes[i+1] = n1 * scale;
2794 }
2795
2796 for (int j = 0; j < 2; j++) {
2797 int *sf_codes = &codes[j*8];
2798 int16_t *sf_out = &out[j*8];
2799
2800 for (int i = 0; i < 8; i++) {
2801 int sample;
2802 unsigned delta = 0;
2803
2804 for (int o = 0; o < order; o++)
2805 delta += coefs[o*8 + i] * hist[(8 - order) + o];
2806
2807 for (int k = i-1; k > -1; k--) {
2808 for (int o = 1; o < order; o++)
2809 delta += sf_codes[(i-1) - k] * (unsigned)coefs[(o*8) + k];
2810 }
2811
2812 sample = sf_codes[i] * 2048;
2813 sample = (int)(sample + delta) / 2048;
2815 sf_out[i] = sample;
2816 }
2817
2818 for (int i = 8 - order; i < 8; i++)
2819 hist[i] = sf_out[i];
2820 }
2821
2822 memcpy(samples, out, sizeof(out));
2823
2824 cs->sample2 = hist[6];
2825 cs->sample1 = hist[7];
2826
2827 buf += 9;
2828 }
2829 bytestream2_seek(&gb, 0, SEEK_END);
2830 ) /* End of CASE */
2831 CASE(ADPCM_PSX,
2832 for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * channels); block++) {
2833 int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * channels) / (16 * channels);
2834 for (int channel = 0; channel < channels; channel++) {
2835 samples = samples_p[channel] + block * nb_samples_per_block;
2836 av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2837
2838 /* Read in every sample for this channel. */
2839 for (int i = 0; i < nb_samples_per_block / 28; i++) {
2840 int filter, shift, flag, byte;
2841
2842 filter = bytestream2_get_byteu(&gb);
2843 shift = filter & 0xf;
2844 filter = filter >> 4;
2846 return AVERROR_INVALIDDATA;
2847 flag = bytestream2_get_byteu(&gb) & 0x7;
2848
2849 /* Decode 28 samples. */
2850 for (int n = 0; n < 28; n++) {
2851 int sample = 0, scale;
2852
2853 if (n & 1) {
2854 scale = sign_extend(byte >> 4, 4);
2855 } else {
2856 byte = bytestream2_get_byteu(&gb);
2857 scale = sign_extend(byte, 4);
2858 }
2859
2860 if (flag < 0x07) {
2861 scale = scale * (1 << 12);
2862 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2863 }
2864 *samples++ = av_clip_int16(sample);
2865 c->status[channel].sample2 = c->status[channel].sample1;
2866 c->status[channel].sample1 = sample;
2867 }
2868 }
2869 }
2870 }
2871 ) /* End of CASE */
2872 CASE(ADPCM_PSXC,
2873 for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2874 int nb_samples_per_block = ((avctx->block_align - 1) / channels) * 2;
2875 for (int channel = 0; channel < channels; channel++) {
2876 int filter, shift, byte;
2877
2878 samples = samples_p[channel] + block * nb_samples_per_block;
2879 av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2880
2881 filter = bytestream2_get_byteu(&gb);
2882 shift = filter & 0xf;
2883 filter = filter >> 4;
2885 return AVERROR_INVALIDDATA;
2886
2887 for (int n = 0; n < nb_samples_per_block; n++) {
2888 int sample = 0, scale;
2889
2890 if (n & 1) {
2891 scale = sign_extend(byte >> 4, 4);
2892 } else {
2893 byte = bytestream2_get_byteu(&gb);
2894 scale = sign_extend(byte & 0xF, 4);
2895 }
2896
2897 scale = scale * (1 << 12);
2898 sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2899 *samples++ = av_clip_int16(sample);
2900 c->status[channel].sample2 = c->status[channel].sample1;
2901 c->status[channel].sample1 = sample;
2902 }
2903 }
2904 }
2905 ) /* End of CASE */
2906 CASE(ADPCM_SANYO,
2907 int (*expand)(ADPCMChannelStatus *c, int bits);
2909
2910 switch(avctx->bits_per_coded_sample) {
2911 case 3: expand = adpcm_sanyo_expand3; break;
2912 case 4: expand = adpcm_sanyo_expand4; break;
2913 case 5: expand = adpcm_sanyo_expand5; break;
2914 }
2915
2916 for (int ch = 0; ch < channels; ch++) {
2917 c->status[ch].predictor = sign_extend(bytestream2_get_le16(&gb), 16);
2918 c->status[ch].step = sign_extend(bytestream2_get_le16(&gb), 16);
2919 }
2920
2922 for (int i = 0; i < nb_samples; i++)
2923 for (int ch = 0; ch < channels; ch++)
2924 samples_p[ch][i] = expand(&c->status[ch], get_bits_le(&g, avctx->bits_per_coded_sample));
2925
2926 align_get_bits(&g);
2927 bytestream2_skip(&gb, get_bits_count(&g) / 8);
2928 ) /* End of CASE */
2929 CASE(ADPCM_RHETOREX,
2930 for (int i = 0; i < nb_samples / 2; i++) {
2931 uint8_t byte = bytestream2_get_byteu(&gb);
2932 *samples++ = adpcm_rhetorex_expand_nibble(c->status, byte >> 4);
2933 *samples++ = adpcm_rhetorex_expand_nibble(c->status, byte);
2934 }
2935 ) /* End of CASE */
2936 CASE(ADPCM_ARGO,
2937 /*
2938 * The format of each block:
2939 * uint8_t left_control;
2940 * uint4_t left_samples[nb_samples];
2941 * ---- and if stereo ----
2942 * uint8_t right_control;
2943 * uint4_t right_samples[nb_samples];
2944 *
2945 * Format of the control byte:
2946 * MSB [SSSSRDRR] LSB
2947 * S = (Shift Amount - 2)
2948 * D = Decoder flag.
2949 * R = Reserved
2950 *
2951 * Each block relies on the previous two samples of each channel.
2952 * They should be 0 initially.
2953 */
2954 for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2955 for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
2956 ADPCMChannelStatus *cs = c->status + channel;
2957 int control, shift;
2958
2959 samples = samples_p[channel] + block * 32;
2960
2961 /* Get the control byte and decode the samples, 2 at a time. */
2962 control = bytestream2_get_byteu(&gb);
2963 shift = (control >> 4) + 2;
2964
2965 for (int n = 0; n < 16; n++) {
2966 int sample = bytestream2_get_byteu(&gb);
2967 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2968 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2969 }
2970 }
2971 }
2972 ) /* End of CASE */
2973 CASE(ADPCM_CIRCUS,
2974 for (int n = 0; n < nb_samples; n++) {
2975 for (int ch = 0; ch < channels; ch++) {
2976 int v = bytestream2_get_byteu(&gb);
2977 *samples++ = adpcm_circus_expand_nibble(&c->status[ch], v);
2978 }
2979 }
2980 ) /* End of CASE */
2981 CASE(ADPCM_ZORK,
2982 for (int n = 0; n < nb_samples * channels; n++) {
2983 int v = bytestream2_get_byteu(&gb);
2984 *samples++ = adpcm_zork_expand_nibble(&c->status[n % channels], v);
2985 }
2986 ) /* End of CASE */
2987 CASE(ADPCM_IMA_MTF,
2988 for (int n = nb_samples / 2; n > 0; n--) {
2989 for (int channel = 0; channel < channels; channel++) {
2990 int v = bytestream2_get_byteu(&gb);
2991 *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2992 samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2993 }
2994 samples += channels;
2995 }
2996 ) /* End of CASE */
2997 CASE(ADPCM_IMA_CITRIX,
2998 while (bytestream2_get_bytes_left(&gb) >= avctx->block_align) {
2999 for (int ch = 0; ch < channels; ch++) {
3000 *samples++ = c->status[ch].predictor = sign_extend(bytestream2_get_le16(&gb), 16);
3001 c->status[ch].step_index = bytestream2_get_byte(&gb);
3002 if (c->status[ch].step_index > 88)
3003 return AVERROR_INVALIDDATA;
3004 bytestream2_skip(&gb, 1);
3005 }
3006 if (channels == 1) {
3007 for (int block = 0; block < avctx->block_align - 4; block++) {
3008 uint8_t bits = bytestream2_get_byteu(&gb);
3009 samples[0] = adpcm_citrix_expand(&c->status[0], bits & 3);
3010 samples[1] = adpcm_citrix_expand(&c->status[0], (bits >> 2) & 3);
3011 samples[2] = adpcm_citrix_expand(&c->status[0], (bits >> 4) & 3);
3012 samples[3] = adpcm_citrix_expand(&c->status[0], (bits >> 6) & 3);
3013 samples += 4;
3014 }
3015 } else { // channels == 2
3016 for (int block = 0; block < (avctx->block_align - 4*channels) / (4*channels); block++) {
3017 for (int ch = 0; ch < channels; ch++) {
3018 uint32_t bits = bytestream2_get_le32(&gb);
3019 for (int j = 0; j < 16; j++)
3020 samples[j*channels + ch] = adpcm_citrix_expand(&c->status[ch], (bits >> (2*j)) & 3);
3021 }
3022 samples += 16*channels;
3023 }
3024 }
3025 }
3026 ) /* End of CASE */
3027 default:
3028 av_unreachable("There are cases for all codec ids using adpcm_decode_frame");
3029 }
3030
3031 if (avpkt->size && bytestream2_tell(&gb) == 0) {
3032 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
3033 return AVERROR_INVALIDDATA;
3034 }
3035
3036 *got_frame_ptr = 1;
3037
3038 if (avpkt->size < bytestream2_tell(&gb)) {
3039 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
3040 return avpkt->size;
3041 }
3042
3043 return bytestream2_tell(&gb);
3044}
3045
3047{
3048 ADPCMDecodeContext *c = avctx->priv_data;
3049
3050 /* Just nuke the entire state and re-init. */
3051 memset(c, 0, sizeof(ADPCMDecodeContext));
3052
3053 switch(avctx->codec_id) {
3055 c->status[0].step = c->status[1].step = 511;
3056 break;
3057
3059 if (avctx->extradata && avctx->extradata_size >= 8) {
3060 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
3061 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
3062 }
3063 break;
3064
3066 if (avctx->extradata && avctx->extradata_size >= 28) {
3067 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
3068 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
3069 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
3070 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
3071 }
3072 break;
3073
3075 if (avctx->extradata && avctx->extradata_size >= 2)
3076 c->vqa_version = AV_RL16(avctx->extradata);
3077 break;
3078 default:
3079 /* Other codecs may want to handle this during decoding. */
3080 c->has_status = 0;
3081 return;
3082 }
3083
3084 c->has_status = 1;
3085}
3086
3087
3088#define ADPCM_DECODER_0(id_, name_, long_name_)
3089#define ADPCM_DECODER_1(id_, name_, long_name_) \
3090const FFCodec ff_ ## name_ ## _decoder = { \
3091 .p.name = #name_, \
3092 CODEC_LONG_NAME(long_name_), \
3093 .p.type = AVMEDIA_TYPE_AUDIO, \
3094 .p.id = id_, \
3095 .p.capabilities = AV_CODEC_CAP_DR1, \
3096 .priv_data_size = sizeof(ADPCMDecodeContext), \
3097 .init = adpcm_decode_init, \
3098 FF_CODEC_DECODE_CB(adpcm_decode_frame), \
3099 .flush = adpcm_flush, \
3100};
3101#define ADPCM_DECODER_2(enabled, codec_id, name, long_name) \
3102 ADPCM_DECODER_ ## enabled(codec_id, name, long_name)
3103#define ADPCM_DECODER_3(config, codec_id, name, long_name) \
3104 ADPCM_DECODER_2(config, codec_id, name, long_name)
3105#define ADPCM_DECODER(codec, name, long_name) \
3106 ADPCM_DECODER_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, \
3107 name, long_name)
3108
3109/* Note: Do not forget to add new entries to the Makefile as well. */
3110ADPCM_DECODER(ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie")
3111ADPCM_DECODER(ADPCM_AFC, adpcm_afc, "ADPCM Nintendo Gamecube AFC")
3112ADPCM_DECODER(ADPCM_AGM, adpcm_agm, "ADPCM AmuseGraphics Movie")
3113ADPCM_DECODER(ADPCM_AICA, adpcm_aica, "ADPCM Yamaha AICA")
3114ADPCM_DECODER(ADPCM_ARGO, adpcm_argo, "ADPCM Argonaut Games")
3115ADPCM_DECODER(ADPCM_CIRCUS, adpcm_circus, "ADPCM Circus")
3116ADPCM_DECODER(ADPCM_CT, adpcm_ct, "ADPCM Creative Technology")
3117ADPCM_DECODER(ADPCM_DTK, adpcm_dtk, "ADPCM Nintendo Gamecube DTK")
3118ADPCM_DECODER(ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts")
3119ADPCM_DECODER(ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA")
3120ADPCM_DECODER(ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1")
3121ADPCM_DECODER(ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2")
3122ADPCM_DECODER(ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3")
3123ADPCM_DECODER(ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS")
3124ADPCM_DECODER(ADPCM_IMA_ACORN, adpcm_ima_acorn, "ADPCM IMA Acorn Replay")
3125ADPCM_DECODER(ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV")
3126ADPCM_DECODER(ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC")
3127ADPCM_DECODER(ADPCM_IMA_APM, adpcm_ima_apm, "ADPCM IMA Ubisoft APM")
3128ADPCM_DECODER(ADPCM_IMA_CITRIX, adpcm_ima_citrix, "ADPCM IMA Citrix")
3129ADPCM_DECODER(ADPCM_IMA_CUNNING, adpcm_ima_cunning, "ADPCM IMA Cunning Developments")
3130ADPCM_DECODER(ADPCM_IMA_DAT4, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4")
3131ADPCM_DECODER(ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3")
3132ADPCM_DECODER(ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4")
3133ADPCM_DECODER(ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS")
3134ADPCM_DECODER(ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD")
3135ADPCM_DECODER(ADPCM_IMA_ESCAPE, adpcm_ima_escape, "ADPCM IMA Acorn Escape")
3136ADPCM_DECODER(ADPCM_IMA_HVQM2, adpcm_ima_hvqm2, "ADPCM IMA HVQM2")
3137ADPCM_DECODER(ADPCM_IMA_HVQM4, adpcm_ima_hvqm4, "ADPCM IMA HVQM4")
3138ADPCM_DECODER(ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS")
3139ADPCM_DECODER(ADPCM_IMA_MAGIX, adpcm_ima_magix, "ADPCM IMA Magix")
3140ADPCM_DECODER(ADPCM_IMA_MOFLEX, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX")
3141ADPCM_DECODER(ADPCM_IMA_MTF, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework")
3142ADPCM_DECODER(ADPCM_IMA_OKI, adpcm_ima_oki, "ADPCM IMA Dialogic OKI")
3143ADPCM_DECODER(ADPCM_IMA_PDA, adpcm_ima_pda, "ADPCM IMA PlayDate")
3144ADPCM_DECODER(ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime")
3145ADPCM_DECODER(ADPCM_IMA_RAD, adpcm_ima_rad, "ADPCM IMA Radical")
3146ADPCM_DECODER(ADPCM_IMA_SSI, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive")
3147ADPCM_DECODER(ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG")
3148ADPCM_DECODER(ADPCM_IMA_ALP, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP")
3149ADPCM_DECODER(ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV")
3150ADPCM_DECODER(ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood")
3151ADPCM_DECODER(ADPCM_IMA_XBOX, adpcm_ima_xbox, "ADPCM IMA Xbox")
3152ADPCM_DECODER(ADPCM_MS, adpcm_ms, "ADPCM Microsoft")
3153ADPCM_DECODER(ADPCM_MTAF, adpcm_mtaf, "ADPCM MTAF")
3154ADPCM_DECODER(ADPCM_N64, adpcm_n64, "ADPCM Silicon Graphics N64")
3155ADPCM_DECODER(ADPCM_PSX, adpcm_psx, "ADPCM Playstation")
3156ADPCM_DECODER(ADPCM_PSXC, adpcm_psxc, "ADPCM Playstation C")
3157ADPCM_DECODER(ADPCM_RHETOREX, adpcm_rhetorex, "ADPCM Rhetorex")
3158ADPCM_DECODER(ADPCM_SANYO, adpcm_sanyo, "ADPCM Sanyo")
3159ADPCM_DECODER(ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit")
3160ADPCM_DECODER(ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit")
3161ADPCM_DECODER(ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit")
3162ADPCM_DECODER(ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash")
3163ADPCM_DECODER(ADPCM_THP_LE, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)")
3164ADPCM_DECODER(ADPCM_THP, adpcm_thp, "ADPCM Nintendo THP")
3165ADPCM_DECODER(ADPCM_XA, adpcm_xa, "ADPCM CDROM XA")
3166ADPCM_DECODER(ADPCM_XMD, adpcm_xmd, "ADPCM Konami XMD")
3167ADPCM_DECODER(ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha")
3168ADPCM_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:1045
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:593
static int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition adpcm.c:483
static void adpcm_flush(AVCodecContext *avctx)
Definition adpcm.c:3046
static int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition adpcm.c:828
static int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:724
static int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:532
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:568
static const int8_t citrix_index_table[4]
Definition adpcm.c:267
static const int8_t adpcm_index_table3[8]
Definition adpcm.c:135
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:794
#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:1256
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:414
static int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition adpcm.c:509
static const int16_t rhetorex_index[8]
Definition adpcm.c:263
static const int16_t ea_adpcm_table[]
Definition adpcm.c:97
static const int8_t adpcm_index_table5[32]
Definition adpcm.c:140
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:860
static int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition adpcm.c:814
static const int8_t zork_index_table[8]
Definition adpcm.c:235
static int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition adpcm.c:548
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:705
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:1543
static int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition adpcm.c:745
#define ADPCM_DECODER(codec, name, long_name)
Definition adpcm.c:3105
static av_cold int adpcm_decode_init(AVCodecContext *avctx)
Definition adpcm.c:281
static int16_t adpcm_rhetorex_expand_nibble(ADPCMChannelStatus *c, uint16_t nibble)
Definition adpcm.c:766
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:1814
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
int ret
Definition ffplay.c:391
#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:429
@ AV_CODEC_ID_ADPCM_SWF
Definition codec_id.h:387
@ AV_CODEC_ID_ADPCM_IMA_HVQM4
Definition codec_id.h:428
@ AV_CODEC_ID_ADPCM_CT
Definition codec_id.h:386
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition codec_id.h:378
@ AV_CODEC_ID_ADPCM_IMA_CITRIX
Definition codec_id.h:437
@ AV_CODEC_ID_ADPCM_EA_R1
Definition codec_id.h:394
@ AV_CODEC_ID_ADPCM_4XM
Definition codec_id.h:381
@ AV_CODEC_ID_ADPCM_IMA_OKI
Definition codec_id.h:406
@ AV_CODEC_ID_ADPCM_IMA_EA_EACS
Definition codec_id.h:398
@ AV_CODEC_ID_ADPCM_SBPRO_2
Definition codec_id.h:391
@ AV_CODEC_ID_ADPCM_DTK
Definition codec_id.h:407
@ AV_CODEC_ID_ADPCM_CIRCUS
Definition codec_id.h:434
@ AV_CODEC_ID_ADPCM_IMA_EA_SEAD
Definition codec_id.h:397
@ AV_CODEC_ID_ADPCM_IMA_HVQM2
Definition codec_id.h:431
@ AV_CODEC_ID_ADPCM_PSX
Definition codec_id.h:411
@ AV_CODEC_ID_ADPCM_XA
Definition codec_id.h:382
@ AV_CODEC_ID_ADPCM_YAMAHA
Definition codec_id.h:388
@ AV_CODEC_ID_ADPCM_SBPRO_3
Definition codec_id.h:390
@ AV_CODEC_ID_ADPCM_IMA_ESCAPE
Definition codec_id.h:435
@ AV_CODEC_ID_ADPCM_EA_R2
Definition codec_id.h:396
@ AV_CODEC_ID_ADPCM_IMA_ISS
Definition codec_id.h:401
@ AV_CODEC_ID_ADPCM_RHETOREX
Definition codec_id.h:436
@ AV_CODEC_ID_ADPCM_MS
Definition codec_id.h:380
@ AV_CODEC_ID_ADPCM_ZORK
Definition codec_id.h:418
@ AV_CODEC_ID_ADPCM_SBPRO_4
Definition codec_id.h:389
@ AV_CODEC_ID_ADPCM_EA_MAXIS_XA
Definition codec_id.h:400
@ AV_CODEC_ID_ADPCM_ARGO
Definition codec_id.h:416
@ AV_CODEC_ID_ADPCM_IMA_APC
Definition codec_id.h:403
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition codec_id.h:393
@ AV_CODEC_ID_ADPCM_EA_XAS
Definition codec_id.h:399
@ AV_CODEC_ID_ADPCM_IMA_MAGIX
Definition codec_id.h:432
@ AV_CODEC_ID_ADPCM_AGM
Definition codec_id.h:415
@ AV_CODEC_ID_ADPCM_IMA_CUNNING
Definition codec_id.h:422
@ AV_CODEC_ID_ADPCM_IMA_DK4
Definition codec_id.h:377
@ AV_CODEC_ID_ADPCM_IMA_DK3
Definition codec_id.h:376
@ AV_CODEC_ID_ADPCM_IMA_DAT4
Definition codec_id.h:413
@ AV_CODEC_ID_ADPCM_XMD
Definition codec_id.h:425
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition codec_id.h:374
@ AV_CODEC_ID_ADPCM_EA
Definition codec_id.h:384
@ AV_CODEC_ID_ADPCM_IMA_SMJPEG
Definition codec_id.h:379
@ AV_CODEC_ID_ADPCM_MTAF
Definition codec_id.h:414
@ AV_CODEC_ID_ADPCM_AICA
Definition codec_id.h:412
@ AV_CODEC_ID_ADPCM_IMA_MTF
Definition codec_id.h:421
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition codec_id.h:419
@ AV_CODEC_ID_ADPCM_THP
Definition codec_id.h:392
@ AV_CODEC_ID_ADPCM_IMA_XBOX
Definition codec_id.h:426
@ AV_CODEC_ID_ADPCM_IMA_ACORN
Definition codec_id.h:424
@ AV_CODEC_ID_ADPCM_AFC
Definition codec_id.h:405
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition codec_id.h:375
@ AV_CODEC_ID_ADPCM_THP_LE
Definition codec_id.h:410
@ AV_CODEC_ID_ADPCM_N64
Definition codec_id.h:430
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition codec_id.h:420
@ AV_CODEC_ID_ADPCM_EA_R3
Definition codec_id.h:395
@ AV_CODEC_ID_ADPCM_PSXC
Definition codec_id.h:433
@ AV_CODEC_ID_ADPCM_SANYO
Definition codec_id.h:427
@ AV_CODEC_ID_ADPCM_IMA_RAD
Definition codec_id.h:408
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition codec_id.h:417
@ AV_CODEC_ID_ADPCM_IMA_MOFLEX
Definition codec_id.h:423
#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
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
#define b
Definition input.c:43
#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_unused
Definition attributes.h:164
#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:2191
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:275
ADPCMChannelStatus status[14]
Definition adpcm.c:274
int has_status
Status flag.
Definition adpcm.c:276
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:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1574
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:1079
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:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
const uint8_t * buffer
Definition get_bits.h:110
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 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]