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