FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/adpcm.c
Date: 2024-10-04 17:46:48
Exec Total Coverage
Lines: 554 728 76.1%
Functions: 16 21 76.2%
Branches: 452 690 65.5%

Line Branch Exec Source
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 *
21 * This file is part of FFmpeg.
22 *
23 * FFmpeg is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU Lesser General Public
25 * License as published by the Free Software Foundation; either
26 * version 2.1 of the License, or (at your option) any later version.
27 *
28 * FFmpeg is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public
34 * License along with FFmpeg; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 */
37
38 #include "config_components.h"
39
40 #include "avcodec.h"
41 #include "get_bits.h"
42 #include "bytestream.h"
43 #include "adpcm.h"
44 #include "adpcm_data.h"
45 #include "codec_internal.h"
46 #include "decode.h"
47
48 /**
49 * @file
50 * ADPCM decoders
51 * Features and limitations:
52 *
53 * Reference documents:
54 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
55 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
56 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
57 * http://openquicktime.sourceforge.net/
58 * XAnim sources (xa_codec.c) http://xanim.polter.net/
59 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
60 * SoX source code http://sox.sourceforge.net/
61 *
62 * CD-ROM XA:
63 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
64 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
65 * readstr http://www.geocities.co.jp/Playtown/2004/
66 */
67
68 #define CASE_0(codec_id, ...)
69 #define CASE_1(codec_id, ...) \
70 case codec_id: \
71 { __VA_ARGS__ } \
72 break;
73 #define CASE_2(enabled, codec_id, ...) \
74 CASE_ ## enabled(codec_id, __VA_ARGS__)
75 #define CASE_3(config, codec_id, ...) \
76 CASE_2(config, codec_id, __VA_ARGS__)
77 #define CASE(codec, ...) \
78 CASE_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
79
80 /* These are for CD-ROM XA ADPCM */
81 static const int8_t xa_adpcm_table[5][2] = {
82 { 0, 0 },
83 { 60, 0 },
84 { 115, -52 },
85 { 98, -55 },
86 { 122, -60 }
87 };
88
89 static const int16_t afc_coeffs[2][16] = {
90 { 0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024, -1024, -1024, -2048 },
91 { 0, 0, 2048, 1024, -2048, -1536, -1024, -2560, -2248, -2300, -3072, -2048, -1024, 1024, 0, 0 }
92 };
93
94 static const int16_t ea_adpcm_table[] = {
95 0, 240, 460, 392,
96 0, 0, -208, -220,
97 0, 1, 3, 4,
98 7, 8, 10, 11,
99 0, -1, -3, -4
100 };
101
102 /*
103 * Dumped from the binaries:
104 * - FantasticJourney.exe - 0x794D2, DGROUP:0x47A4D2
105 * - BigRaceUSA.exe - 0x9B8AA, DGROUP:0x49C4AA
106 * - Timeshock!.exe - 0x8506A, DGROUP:0x485C6A
107 */
108 static const int8_t ima_cunning_index_table[9] = {
109 -1, -1, -1, -1, 1, 2, 3, 4, -1
110 };
111
112 /*
113 * Dumped from the binaries:
114 * - FantasticJourney.exe - 0x79458, DGROUP:0x47A458
115 * - BigRaceUSA.exe - 0x9B830, DGROUP:0x49C430
116 * - Timeshock!.exe - 0x84FF0, DGROUP:0x485BF0
117 */
118 static const int16_t ima_cunning_step_table[61] = {
119 1, 1, 1, 1, 2, 2, 3, 3, 4, 5,
120 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
121 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
122 192, 224, 256, 320, 384, 448, 512, 640, 768, 896,
123 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, 4096, 5120,
124 6144, 7168, 8192, 10240, 12288, 14336, 16384, 20480, 24576, 28672, 0
125 };
126
127 static const int8_t adpcm_index_table2[4] = {
128 -1, 2,
129 -1, 2,
130 };
131
132 static const int8_t adpcm_index_table3[8] = {
133 -1, -1, 1, 2,
134 -1, -1, 1, 2,
135 };
136
137 static const int8_t adpcm_index_table5[32] = {
138 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
139 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
140 };
141
142 static const int8_t * const adpcm_index_tables[4] = {
143 &adpcm_index_table2[0],
144 &adpcm_index_table3[0],
145 &ff_adpcm_index_table[0],
146 &adpcm_index_table5[0],
147 };
148
149 static const int16_t mtaf_stepsize[32][16] = {
150 { 1, 5, 9, 13, 16, 20, 24, 28,
151 -1, -5, -9, -13, -16, -20, -24, -28, },
152 { 2, 6, 11, 15, 20, 24, 29, 33,
153 -2, -6, -11, -15, -20, -24, -29, -33, },
154 { 2, 7, 13, 18, 23, 28, 34, 39,
155 -2, -7, -13, -18, -23, -28, -34, -39, },
156 { 3, 9, 15, 21, 28, 34, 40, 46,
157 -3, -9, -15, -21, -28, -34, -40, -46, },
158 { 3, 11, 18, 26, 33, 41, 48, 56,
159 -3, -11, -18, -26, -33, -41, -48, -56, },
160 { 4, 13, 22, 31, 40, 49, 58, 67,
161 -4, -13, -22, -31, -40, -49, -58, -67, },
162 { 5, 16, 26, 37, 48, 59, 69, 80,
163 -5, -16, -26, -37, -48, -59, -69, -80, },
164 { 6, 19, 31, 44, 57, 70, 82, 95,
165 -6, -19, -31, -44, -57, -70, -82, -95, },
166 { 7, 22, 38, 53, 68, 83, 99, 114,
167 -7, -22, -38, -53, -68, -83, -99, -114, },
168 { 9, 27, 45, 63, 81, 99, 117, 135,
169 -9, -27, -45, -63, -81, -99, -117, -135, },
170 { 10, 32, 53, 75, 96, 118, 139, 161,
171 -10, -32, -53, -75, -96, -118, -139, -161, },
172 { 12, 38, 64, 90, 115, 141, 167, 193,
173 -12, -38, -64, -90, -115, -141, -167, -193, },
174 { 15, 45, 76, 106, 137, 167, 198, 228,
175 -15, -45, -76, -106, -137, -167, -198, -228, },
176 { 18, 54, 91, 127, 164, 200, 237, 273,
177 -18, -54, -91, -127, -164, -200, -237, -273, },
178 { 21, 65, 108, 152, 195, 239, 282, 326,
179 -21, -65, -108, -152, -195, -239, -282, -326, },
180 { 25, 77, 129, 181, 232, 284, 336, 388,
181 -25, -77, -129, -181, -232, -284, -336, -388, },
182 { 30, 92, 153, 215, 276, 338, 399, 461,
183 -30, -92, -153, -215, -276, -338, -399, -461, },
184 { 36, 109, 183, 256, 329, 402, 476, 549,
185 -36, -109, -183, -256, -329, -402, -476, -549, },
186 { 43, 130, 218, 305, 392, 479, 567, 654,
187 -43, -130, -218, -305, -392, -479, -567, -654, },
188 { 52, 156, 260, 364, 468, 572, 676, 780,
189 -52, -156, -260, -364, -468, -572, -676, -780, },
190 { 62, 186, 310, 434, 558, 682, 806, 930,
191 -62, -186, -310, -434, -558, -682, -806, -930, },
192 { 73, 221, 368, 516, 663, 811, 958, 1106,
193 -73, -221, -368, -516, -663, -811, -958, -1106, },
194 { 87, 263, 439, 615, 790, 966, 1142, 1318,
195 -87, -263, -439, -615, -790, -966, -1142, -1318, },
196 { 104, 314, 523, 733, 942, 1152, 1361, 1571,
197 -104, -314, -523, -733, -942, -1152, -1361, -1571, },
198 { 124, 374, 623, 873, 1122, 1372, 1621, 1871,
199 -124, -374, -623, -873, -1122, -1372, -1621, -1871, },
200 { 148, 445, 743, 1040, 1337, 1634, 1932, 2229,
201 -148, -445, -743, -1040, -1337, -1634, -1932, -2229, },
202 { 177, 531, 885, 1239, 1593, 1947, 2301, 2655,
203 -177, -531, -885, -1239, -1593, -1947, -2301, -2655, },
204 { 210, 632, 1053, 1475, 1896, 2318, 2739, 3161,
205 -210, -632, -1053, -1475, -1896, -2318, -2739, -3161, },
206 { 251, 753, 1255, 1757, 2260, 2762, 3264, 3766,
207 -251, -753, -1255, -1757, -2260, -2762, -3264, -3766, },
208 { 299, 897, 1495, 2093, 2692, 3290, 3888, 4486,
209 -299, -897, -1495, -2093, -2692, -3290, -3888, -4486, },
210 { 356, 1068, 1781, 2493, 3206, 3918, 4631, 5343,
211 -356, -1068, -1781, -2493, -3206, -3918, -4631, -5343, },
212 { 424, 1273, 2121, 2970, 3819, 4668, 5516, 6365,
213 -424, -1273, -2121, -2970, -3819, -4668, -5516, -6365, },
214 };
215
216 static const int16_t oki_step_table[49] = {
217 16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
218 41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
219 107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
220 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
221 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
222 };
223
224 // padded to zero where table size is less then 16
225 static const int8_t swf_index_tables[4][16] = {
226 /*2*/ { -1, 2 },
227 /*3*/ { -1, -1, 2, 4 },
228 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
229 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
230 };
231
232 static const int8_t zork_index_table[8] = {
233 -1, -1, -1, 1, 4, 7, 10, 12,
234 };
235
236 static const int8_t mtf_index_table[16] = {
237 8, 6, 4, 2, -1, -1, -1, -1,
238 -1, -1, -1, -1, 2, 4, 6, 8,
239 };
240
241 /* end of tables */
242
243 typedef struct ADPCMDecodeContext {
244 ADPCMChannelStatus status[14];
245 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
246 int has_status; /**< Status flag. Reset to 0 after a flush. */
247 } ADPCMDecodeContext;
248
249 static void adpcm_flush(AVCodecContext *avctx);
250
251 187 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
252 {
253 187 ADPCMDecodeContext *c = avctx->priv_data;
254 187 unsigned int min_channels = 1;
255 187 unsigned int max_channels = 2;
256
257 187 adpcm_flush(avctx);
258
259
5/7
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 151 times.
187 switch(avctx->codec->id) {
260 3 case AV_CODEC_ID_ADPCM_IMA_AMV:
261 3 max_channels = 1;
262 3 break;
263 23 case AV_CODEC_ID_ADPCM_AFC:
264 case AV_CODEC_ID_ADPCM_EA_R1:
265 case AV_CODEC_ID_ADPCM_EA_R2:
266 case AV_CODEC_ID_ADPCM_EA_R3:
267 case AV_CODEC_ID_ADPCM_EA_XAS:
268 case AV_CODEC_ID_ADPCM_MS:
269 23 max_channels = 6;
270 23 break;
271 case AV_CODEC_ID_ADPCM_MTAF:
272 min_channels = 2;
273 max_channels = 8;
274 if (avctx->ch_layout.nb_channels & 1) {
275 avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);
276 return AVERROR_PATCHWELCOME;
277 }
278 break;
279 2 case AV_CODEC_ID_ADPCM_DTK:
280 2 min_channels = 2;
281 2 break;
282 case AV_CODEC_ID_ADPCM_PSX:
283 max_channels = 8;
284 if (avctx->ch_layout.nb_channels <= 0 ||
285 avctx->block_align % (16 * avctx->ch_layout.nb_channels))
286 return AVERROR_INVALIDDATA;
287 break;
288 8 case AV_CODEC_ID_ADPCM_IMA_DAT4:
289 case AV_CODEC_ID_ADPCM_THP:
290 case AV_CODEC_ID_ADPCM_THP_LE:
291 8 max_channels = 14;
292 8 break;
293 }
294
1/2
✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
187 if (avctx->ch_layout.nb_channels < min_channels ||
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
187 avctx->ch_layout.nb_channels > max_channels) {
296 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
297 return AVERROR(EINVAL);
298 }
299
300
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 171 times.
187 switch(avctx->codec->id) {
301 10 case AV_CODEC_ID_ADPCM_IMA_WAV:
302
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
303 return AVERROR_INVALIDDATA;
304 10 break;
305 6 case AV_CODEC_ID_ADPCM_ARGO:
306
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->bits_per_coded_sample != 4 ||
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 avctx->block_align != 17 * avctx->ch_layout.nb_channels)
308 return AVERROR_INVALIDDATA;
309 6 break;
310 case AV_CODEC_ID_ADPCM_ZORK:
311 if (avctx->bits_per_coded_sample != 8)
312 return AVERROR_INVALIDDATA;
313 break;
314 171 default:
315 171 break;
316 }
317
318
4/4
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 81 times.
187 switch (avctx->codec->id) {
319 87 case AV_CODEC_ID_ADPCM_AICA:
320 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
321 case AV_CODEC_ID_ADPCM_IMA_DAT4:
322 case AV_CODEC_ID_ADPCM_IMA_QT:
323 case AV_CODEC_ID_ADPCM_IMA_WAV:
324 case AV_CODEC_ID_ADPCM_4XM:
325 case AV_CODEC_ID_ADPCM_XA:
326 case AV_CODEC_ID_ADPCM_XMD:
327 case AV_CODEC_ID_ADPCM_EA_R1:
328 case AV_CODEC_ID_ADPCM_EA_R2:
329 case AV_CODEC_ID_ADPCM_EA_R3:
330 case AV_CODEC_ID_ADPCM_EA_XAS:
331 case AV_CODEC_ID_ADPCM_THP:
332 case AV_CODEC_ID_ADPCM_THP_LE:
333 case AV_CODEC_ID_ADPCM_AFC:
334 case AV_CODEC_ID_ADPCM_DTK:
335 case AV_CODEC_ID_ADPCM_PSX:
336 case AV_CODEC_ID_ADPCM_MTAF:
337 case AV_CODEC_ID_ADPCM_ARGO:
338 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
339 87 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
340 87 break;
341 8 case AV_CODEC_ID_ADPCM_IMA_WS:
342
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
343 AV_SAMPLE_FMT_S16;
344 8 break;
345 11 case AV_CODEC_ID_ADPCM_MS:
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
347 AV_SAMPLE_FMT_S16;
348 11 break;
349 81 default:
350 81 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
351 }
352 187 return 0;
353 }
354
355 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
356 {
357 int delta, pred, step, add;
358
359 pred = c->predictor;
360 delta = nibble & 7;
361 step = c->step;
362 add = (delta * 2 + 1) * step;
363 if (add < 0)
364 add = add + 7;
365
366 if ((nibble & 8) == 0)
367 pred = av_clip(pred + (add >> 3), -32767, 32767);
368 else
369 pred = av_clip(pred - (add >> 3), -32767, 32767);
370
371 switch (delta) {
372 case 7:
373 step *= 0x99;
374 break;
375 case 6:
376 c->step = av_clip(c->step * 2, 127, 24576);
377 c->predictor = pred;
378 return pred;
379 case 5:
380 step *= 0x66;
381 break;
382 case 4:
383 step *= 0x4d;
384 break;
385 default:
386 step *= 0x39;
387 break;
388 }
389
390 if (step < 0)
391 step += 0x3f;
392
393 c->step = step >> 6;
394 c->step = av_clip(c->step, 127, 24576);
395 c->predictor = pred;
396 return pred;
397 }
398
399 10316296 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
400 {
401 int step_index;
402 int predictor;
403 int sign, delta, diff, step;
404
405 10316296 step = ff_adpcm_step_table[c->step_index];
406 10316296 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
407 10316296 step_index = av_clip(step_index, 0, 88);
408
409 10316296 sign = nibble & 8;
410 10316296 delta = nibble & 7;
411 /* perform direct multiplication instead of series of jumps proposed by
412 * the reference ADPCM implementation since modern CPUs can do the mults
413 * quickly enough */
414 10316296 diff = ((2 * delta + 1) * step) >> shift;
415 10316296 predictor = c->predictor;
416
2/2
✓ Branch 0 taken 5110378 times.
✓ Branch 1 taken 5205918 times.
10316296 if (sign) predictor -= diff;
417 5205918 else predictor += diff;
418
419 10316296 c->predictor = av_clip_int16(predictor);
420 10316296 c->step_index = step_index;
421
422 10316296 return (int16_t)c->predictor;
423 }
424
425 679344 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
426 {
427 int step_index;
428 int predictor;
429 int sign, delta, diff, step;
430
431 679344 step = ff_adpcm_step_table[c->step_index];
432 679344 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
433 679344 step_index = av_clip(step_index, 0, 88);
434
435 679344 sign = nibble & 8;
436 679344 delta = nibble & 7;
437 679344 diff = (delta * step) >> shift;
438 679344 predictor = c->predictor;
439
2/2
✓ Branch 0 taken 339842 times.
✓ Branch 1 taken 339502 times.
679344 if (sign) predictor -= diff;
440 339502 else predictor += diff;
441
442 679344 c->predictor = av_clip_int16(predictor);
443 679344 c->step_index = step_index;
444
445 679344 return (int16_t)c->predictor;
446 }
447
448 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
449 {
450 int step_index, step, delta, predictor;
451
452 step = ff_adpcm_step_table[c->step_index];
453
454 delta = step * (2 * nibble - 15);
455 predictor = c->predictor + delta;
456
457 step_index = c->step_index + mtf_index_table[(unsigned)nibble];
458 c->predictor = av_clip_int16(predictor >> 4);
459 c->step_index = av_clip(step_index, 0, 88);
460
461 return (int16_t)c->predictor;
462 }
463
464 386028 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
465 {
466 int step_index;
467 int predictor;
468 int step;
469
470 386028 nibble = sign_extend(nibble & 0xF, 4);
471
472 386028 step = ima_cunning_step_table[c->step_index];
473 386028 step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
474 386028 step_index = av_clip(step_index, 0, 60);
475
476 386028 predictor = c->predictor + step * nibble;
477
478 386028 c->predictor = av_clip_int16(predictor);
479 386028 c->step_index = step_index;
480
481 386028 return c->predictor;
482 }
483
484 static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
485 {
486 int nibble, step_index, predictor, sign, delta, diff, step, shift;
487
488 shift = bps - 1;
489 nibble = get_bits_le(gb, bps),
490 step = ff_adpcm_step_table[c->step_index];
491 step_index = c->step_index + adpcm_index_tables[bps - 2][nibble];
492 step_index = av_clip(step_index, 0, 88);
493
494 sign = nibble & (1 << shift);
495 delta = av_zero_extend(nibble, shift);
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
507 4696090 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
508 {
509 int step_index;
510 int predictor;
511 int diff, step;
512
513 4696090 step = ff_adpcm_step_table[c->step_index];
514 4696090 step_index = c->step_index + ff_adpcm_index_table[nibble];
515 4696090 step_index = av_clip(step_index, 0, 88);
516
517 4696090 diff = step >> 3;
518
2/2
✓ Branch 0 taken 1076119 times.
✓ Branch 1 taken 3619971 times.
4696090 if (nibble & 4) diff += step;
519
2/2
✓ Branch 0 taken 1984447 times.
✓ Branch 1 taken 2711643 times.
4696090 if (nibble & 2) diff += step >> 1;
520
2/2
✓ Branch 0 taken 2159261 times.
✓ Branch 1 taken 2536829 times.
4696090 if (nibble & 1) diff += step >> 2;
521
522
2/2
✓ Branch 0 taken 2338238 times.
✓ Branch 1 taken 2357852 times.
4696090 if (nibble & 8)
523 2338238 predictor = c->predictor - diff;
524 else
525 2357852 predictor = c->predictor + diff;
526
527 4696090 c->predictor = av_clip_int16(predictor);
528 4696090 c->step_index = step_index;
529
530 4696090 return c->predictor;
531 }
532
533 2233434 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
534 {
535 int predictor;
536
537 2233434 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
538
2/2
✓ Branch 0 taken 973371 times.
✓ Branch 1 taken 1260063 times.
2233434 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
539
540 2233434 c->sample2 = c->sample1;
541 2233434 c->sample1 = av_clip_int16(predictor);
542 2233434 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
543
2/2
✓ Branch 0 taken 92703 times.
✓ Branch 1 taken 2140731 times.
2233434 if (c->idelta < 16) c->idelta = 16;
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2233434 times.
2233434 if (c->idelta > INT_MAX/768) {
545 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
546 c->idelta = INT_MAX/768;
547 }
548
549 2233434 return c->sample1;
550 }
551
552 55124 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
553 {
554 int step_index, predictor, sign, delta, diff, step;
555
556 55124 step = oki_step_table[c->step_index];
557 55124 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
558 55124 step_index = av_clip(step_index, 0, 48);
559
560 55124 sign = nibble & 8;
561 55124 delta = nibble & 7;
562 55124 diff = ((2 * delta + 1) * step) >> 3;
563 55124 predictor = c->predictor;
564
2/2
✓ Branch 0 taken 25352 times.
✓ Branch 1 taken 29772 times.
55124 if (sign) predictor -= diff;
565 29772 else predictor += diff;
566
567 55124 c->predictor = av_clip_intp2(predictor, 11);
568 55124 c->step_index = step_index;
569
570 55124 return c->predictor * 16;
571 }
572
573 524192 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
574 {
575 int sign, delta, diff;
576 int new_step;
577
578 524192 sign = nibble & 8;
579 524192 delta = nibble & 7;
580 /* perform direct multiplication instead of series of jumps proposed by
581 * the reference ADPCM implementation since modern CPUs can do the mults
582 * quickly enough */
583 524192 diff = ((2 * delta + 1) * c->step) >> 3;
584 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
585
2/2
✓ Branch 0 taken 263253 times.
✓ Branch 1 taken 260939 times.
524192 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
586 524192 c->predictor = av_clip_int16(c->predictor);
587 /* calculate new step and clamp it to range 511..32767 */
588 524192 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
589 524192 c->step = av_clip(new_step, 511, 32767);
590
591 524192 return (int16_t)c->predictor;
592 }
593
594 313380 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
595 {
596 int sign, delta, diff;
597
598 313380 sign = nibble & (1<<(size-1));
599 313380 delta = nibble & ((1<<(size-1))-1);
600 313380 diff = delta << (7 + c->step + shift);
601
602 /* clamp result */
603
2/2
✓ Branch 0 taken 129904 times.
✓ Branch 1 taken 183476 times.
313380 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
604
605 /* calculate new step */
606
4/4
✓ Branch 0 taken 97171 times.
✓ Branch 1 taken 216209 times.
✓ Branch 2 taken 75080 times.
✓ Branch 3 taken 22091 times.
313380 if (delta >= (2*size - 3) && c->step < 3)
607 75080 c->step++;
608
4/4
✓ Branch 0 taken 129907 times.
✓ Branch 1 taken 108393 times.
✓ Branch 2 taken 75080 times.
✓ Branch 3 taken 54827 times.
238300 else if (delta == 0 && c->step > 0)
609 75080 c->step--;
610
611 313380 return (int16_t) c->predictor;
612 }
613
614 1105920 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
615 {
616
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1105915 times.
1105920 if(!c->step) {
617 5 c->predictor = 0;
618 5 c->step = 127;
619 }
620
621 1105920 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
622 1105920 c->predictor = av_clip_int16(c->predictor);
623 1105920 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
624 1105920 c->step = av_clip(c->step, 127, 24576);
625 1105920 return c->predictor;
626 }
627
628 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
629 {
630 c->predictor += mtaf_stepsize[c->step][nibble];
631 c->predictor = av_clip_int16(c->predictor);
632 c->step += ff_adpcm_index_table[nibble];
633 c->step = av_clip_uintp2(c->step, 5);
634 return c->predictor;
635 }
636
637 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
638 {
639 int16_t index = c->step_index;
640 uint32_t lookup_sample = ff_adpcm_step_table[index];
641 int32_t sample = 0;
642
643 if (nibble & 0x40)
644 sample += lookup_sample;
645 if (nibble & 0x20)
646 sample += lookup_sample >> 1;
647 if (nibble & 0x10)
648 sample += lookup_sample >> 2;
649 if (nibble & 0x08)
650 sample += lookup_sample >> 3;
651 if (nibble & 0x04)
652 sample += lookup_sample >> 4;
653 if (nibble & 0x02)
654 sample += lookup_sample >> 5;
655 if (nibble & 0x01)
656 sample += lookup_sample >> 6;
657 if (nibble & 0x80)
658 sample = -sample;
659
660 sample += c->predictor;
661 sample = av_clip_int16(sample);
662
663 index += zork_index_table[(nibble >> 4) & 7];
664 index = av_clip(index, 0, 88);
665
666 c->predictor = sample;
667 c->step_index = index;
668
669 return sample;
670 }
671
672 666 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
673 const uint8_t *in, ADPCMChannelStatus *left,
674 ADPCMChannelStatus *right, int channels, int sample_offset)
675 {
676 int i, j;
677 int shift,filter,f0,f1;
678 int s_1,s_2;
679 int d,s,t;
680
681 666 out0 += sample_offset;
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (channels == 1)
683 out1 = out0 + 28;
684 else
685 666 out1 += sample_offset;
686
687
2/2
✓ Branch 0 taken 2664 times.
✓ Branch 1 taken 666 times.
3330 for(i=0;i<4;i++) {
688 2664 shift = 12 - (in[4+i*2] & 15);
689 2664 filter = in[4+i*2] >> 4;
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
691 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
692 filter=0;
693 }
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (shift < 0) {
695 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
696 shift = 0;
697 }
698 2664 f0 = xa_adpcm_table[filter][0];
699 2664 f1 = xa_adpcm_table[filter][1];
700
701 2664 s_1 = left->sample1;
702 2664 s_2 = left->sample2;
703
704
2/2
✓ Branch 0 taken 74592 times.
✓ Branch 1 taken 2664 times.
77256 for(j=0;j<28;j++) {
705 74592 d = in[16+i+j*4];
706
707 74592 t = sign_extend(d, 4);
708 74592 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
709 74592 s_2 = s_1;
710 74592 s_1 = av_clip_int16(s);
711 74592 out0[j] = s_1;
712 }
713
714
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 if (channels == 2) {
715 2664 left->sample1 = s_1;
716 2664 left->sample2 = s_2;
717 2664 s_1 = right->sample1;
718 2664 s_2 = right->sample2;
719 }
720
721 2664 shift = 12 - (in[5+i*2] & 15);
722 2664 filter = in[5+i*2] >> 4;
723
2/4
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2664 times.
2664 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
724 avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
725 filter=0;
726 }
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (shift < 0) {
728 avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
729 shift = 0;
730 }
731
732 2664 f0 = xa_adpcm_table[filter][0];
733 2664 f1 = xa_adpcm_table[filter][1];
734
735
2/2
✓ Branch 0 taken 74592 times.
✓ Branch 1 taken 2664 times.
77256 for(j=0;j<28;j++) {
736 74592 d = in[16+i+j*4];
737
738 74592 t = sign_extend(d >> 4, 4);
739 74592 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
740 74592 s_2 = s_1;
741 74592 s_1 = av_clip_int16(s);
742 74592 out1[j] = s_1;
743 }
744
745
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 if (channels == 2) {
746 2664 right->sample1 = s_1;
747 2664 right->sample2 = s_2;
748 } else {
749 left->sample1 = s_1;
750 left->sample2 = s_2;
751 }
752
753 2664 out0 += 28 * (3 - channels);
754 2664 out1 += 28 * (3 - channels);
755 }
756
757 666 return 0;
758 }
759
760 195 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
761 {
762 195 ADPCMDecodeContext *c = avctx->priv_data;
763 GetBitContext gb;
764 const int8_t *table;
765 195 int channels = avctx->ch_layout.nb_channels;
766 int k0, signmask, nb_bits, count;
767 195 int size = buf_size*8;
768 int i;
769
770 195 init_get_bits(&gb, buf, size);
771
772 //read bits & initial values
773 195 nb_bits = get_bits(&gb, 2)+2;
774 195 table = swf_index_tables[nb_bits-2];
775 195 k0 = 1 << (nb_bits-2);
776 195 signmask = 1 << (nb_bits-1);
777
778
2/2
✓ Branch 1 taken 195 times.
✓ Branch 2 taken 195 times.
390 while (get_bits_count(&gb) <= size - 22 * channels) {
779
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 195 times.
585 for (i = 0; i < channels; i++) {
780 390 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
781 390 c->status[i].step_index = get_bits(&gb, 6);
782 }
783
784
3/4
✓ Branch 1 taken 798525 times.
✓ Branch 2 taken 195 times.
✓ Branch 3 taken 798525 times.
✗ Branch 4 not taken.
798720 for (count = 0; get_bits_count(&gb) <= size - nb_bits * channels && count < 4095; count++) {
785 int i;
786
787
2/2
✓ Branch 0 taken 1597050 times.
✓ Branch 1 taken 798525 times.
2395575 for (i = 0; i < channels; i++) {
788 // similar to IMA adpcm
789 1597050 int delta = get_bits(&gb, nb_bits);
790 1597050 int step = ff_adpcm_step_table[c->status[i].step_index];
791 1597050 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
792 1597050 int k = k0;
793
794 do {
795
2/2
✓ Branch 0 taken 1979561 times.
✓ Branch 1 taken 2811589 times.
4791150 if (delta & k)
796 1979561 vpdiff += step;
797 4791150 step >>= 1;
798 4791150 k >>= 1;
799
2/2
✓ Branch 0 taken 3194100 times.
✓ Branch 1 taken 1597050 times.
4791150 } while(k);
800 1597050 vpdiff += step;
801
802
2/2
✓ Branch 0 taken 803527 times.
✓ Branch 1 taken 793523 times.
1597050 if (delta & signmask)
803 803527 c->status[i].predictor -= vpdiff;
804 else
805 793523 c->status[i].predictor += vpdiff;
806
807 1597050 c->status[i].step_index += table[delta & (~signmask)];
808
809 1597050 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
810 1597050 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
811
812 1597050 *samples++ = c->status[i].predictor;
813 }
814 }
815 }
816 195 }
817
818 18335520 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
819 {
820 18335520 int sample = sign_extend(nibble, 4) * (1 << shift);
821
822
2/2
✓ Branch 0 taken 9806208 times.
✓ Branch 1 taken 8529312 times.
18335520 if (flag)
823 9806208 sample += (8 * cs->sample1) - (4 * cs->sample2);
824 else
825 8529312 sample += 4 * cs->sample1;
826
827 18335520 sample = av_clip_int16(sample >> 2);
828
829 18335520 cs->sample2 = cs->sample1;
830 18335520 cs->sample1 = sample;
831
832 18335520 return sample;
833 }
834
835 /**
836 * Get the number of samples (per channel) that will be decoded from the packet.
837 * In one case, this is actually the maximum number of samples possible to
838 * decode with the given buf_size.
839 *
840 * @param[out] coded_samples set to the number of samples as coded in the
841 * packet, or 0 if the codec does not encode the
842 * number of samples in each frame.
843 * @param[out] approx_nb_samples set to non-zero if the number of samples
844 * returned is an approximation.
845 */
846 37697 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
847 int buf_size, int *coded_samples, int *approx_nb_samples)
848 {
849 37697 ADPCMDecodeContext *s = avctx->priv_data;
850 37697 int nb_samples = 0;
851 37697 int ch = avctx->ch_layout.nb_channels;
852 37697 int has_coded_samples = 0;
853 int header_size;
854
855 37697 *coded_samples = 0;
856 37697 *approx_nb_samples = 0;
857
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37697 times.
37697 if(ch <= 0)
859 return 0;
860
861
4/4
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 26342 times.
✓ Branch 2 taken 1195 times.
✓ Branch 3 taken 9560 times.
37697 switch (avctx->codec->id) {
862 /* constant, only check buf_size */
863 600 case AV_CODEC_ID_ADPCM_EA_XAS:
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (buf_size < 76 * ch)
865 return 0;
866 600 nb_samples = 128;
867 600 break;
868 26342 case AV_CODEC_ID_ADPCM_IMA_QT:
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26342 times.
26342 if (buf_size < 34 * ch)
870 return 0;
871 26342 nb_samples = 64;
872 26342 break;
873 /* simple 4-bit adpcm */
874 1195 case AV_CODEC_ID_ADPCM_CT:
875 case AV_CODEC_ID_ADPCM_IMA_APC:
876 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
877 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
878 case AV_CODEC_ID_ADPCM_IMA_OKI:
879 case AV_CODEC_ID_ADPCM_IMA_WS:
880 case AV_CODEC_ID_ADPCM_YAMAHA:
881 case AV_CODEC_ID_ADPCM_AICA:
882 case AV_CODEC_ID_ADPCM_IMA_SSI:
883 case AV_CODEC_ID_ADPCM_IMA_APM:
884 case AV_CODEC_ID_ADPCM_IMA_ALP:
885 case AV_CODEC_ID_ADPCM_IMA_MTF:
886 1195 nb_samples = buf_size * 2 / ch;
887 1195 break;
888 }
889
2/2
✓ Branch 0 taken 28137 times.
✓ Branch 1 taken 9560 times.
37697 if (nb_samples)
890 28137 return nb_samples;
891
892 /* simple 4-bit adpcm, with header */
893 9560 header_size = 0;
894
3/3
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 350 times.
✓ Branch 2 taken 9065 times.
9560 switch (avctx->codec->id) {
895 145 case AV_CODEC_ID_ADPCM_4XM:
896 case AV_CODEC_ID_ADPCM_AGM:
897 case AV_CODEC_ID_ADPCM_IMA_ACORN:
898 case AV_CODEC_ID_ADPCM_IMA_DAT4:
899 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
900 145 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
901 350 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
902 }
903
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 9065 times.
9560 if (header_size > 0)
904 495 return (buf_size - header_size) * 2 / ch;
905
906 /* more complex formats */
907
17/21
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 47 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 400 times.
✓ Branch 5 taken 642 times.
✓ Branch 6 taken 649 times.
✓ Branch 7 taken 1000 times.
✓ Branch 8 taken 1668 times.
✓ Branch 9 taken 2950 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 57 times.
✓ Branch 12 taken 195 times.
✓ Branch 13 taken 71 times.
✓ Branch 14 taken 12 times.
✓ Branch 15 taken 37 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 32 times.
✓ Branch 18 taken 946 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
9065 switch (avctx->codec->id) {
908 162 case AV_CODEC_ID_ADPCM_IMA_AMV:
909 162 bytestream2_skip(gb, 4);
910 162 has_coded_samples = 1;
911 162 *coded_samples = bytestream2_get_le32u(gb);
912 162 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
913 162 bytestream2_seek(gb, -8, SEEK_CUR);
914 162 break;
915 158 case AV_CODEC_ID_ADPCM_EA:
916 /* Stereo is 30 bytes per block */
917 /* Mono is 15 bytes per block */
918 158 has_coded_samples = 1;
919 158 *coded_samples = bytestream2_get_le32(gb);
920 158 *coded_samples -= *coded_samples % 28;
921
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
922 158 break;
923 47 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
924 47 has_coded_samples = 1;
925 47 *coded_samples = bytestream2_get_le32(gb);
926 47 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
927 47 break;
928 39 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
929 39 nb_samples = (buf_size - ch) / ch * 2;
930 39 break;
931 400 case AV_CODEC_ID_ADPCM_EA_R1:
932 case AV_CODEC_ID_ADPCM_EA_R2:
933 case AV_CODEC_ID_ADPCM_EA_R3:
934 /* maximum number of samples */
935 /* has internal offsets and a per-frame switch to signal raw 16-bit */
936 400 has_coded_samples = 1;
937
3/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 125 times.
✗ Branch 3 not taken.
400 switch (avctx->codec->id) {
938 95 case AV_CODEC_ID_ADPCM_EA_R1:
939 95 header_size = 4 + 9 * ch;
940 95 *coded_samples = bytestream2_get_le32(gb);
941 95 break;
942 180 case AV_CODEC_ID_ADPCM_EA_R2:
943 180 header_size = 4 + 5 * ch;
944 180 *coded_samples = bytestream2_get_le32(gb);
945 180 break;
946 125 case AV_CODEC_ID_ADPCM_EA_R3:
947 125 header_size = 4 + 5 * ch;
948 125 *coded_samples = bytestream2_get_be32(gb);
949 125 break;
950 }
951 400 *coded_samples -= *coded_samples % 28;
952 400 nb_samples = (buf_size - header_size) * 2 / ch;
953 400 nb_samples -= nb_samples % 28;
954 400 *approx_nb_samples = 1;
955 400 break;
956 642 case AV_CODEC_ID_ADPCM_IMA_DK3:
957
1/2
✓ Branch 0 taken 642 times.
✗ Branch 1 not taken.
642 if (avctx->block_align > 0)
958 642 buf_size = FFMIN(buf_size, avctx->block_align);
959 642 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
960 642 break;
961 649 case AV_CODEC_ID_ADPCM_IMA_DK4:
962
1/2
✓ Branch 0 taken 649 times.
✗ Branch 1 not taken.
649 if (avctx->block_align > 0)
963 649 buf_size = FFMIN(buf_size, avctx->block_align);
964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 649 times.
649 if (buf_size < 4 * ch)
965 return AVERROR_INVALIDDATA;
966 649 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
967 649 break;
968 1000 case AV_CODEC_ID_ADPCM_IMA_RAD:
969
1/2
✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
1000 if (avctx->block_align > 0)
970 1000 buf_size = FFMIN(buf_size, avctx->block_align);
971 1000 nb_samples = (buf_size - 4 * ch) * 2 / ch;
972 1000 break;
973
2/4
✓ Branch 0 taken 1668 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1668 times.
1668 CASE(ADPCM_IMA_WAV,
974 int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
975 int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
976 if (avctx->block_align > 0)
977 buf_size = FFMIN(buf_size, avctx->block_align);
978 if (buf_size < 4 * ch)
979 return AVERROR_INVALIDDATA;
980 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
981 ) /* End of CASE */
982 2950 case AV_CODEC_ID_ADPCM_MS:
983
1/2
✓ Branch 0 taken 2950 times.
✗ Branch 1 not taken.
2950 if (avctx->block_align > 0)
984 2950 buf_size = FFMIN(buf_size, avctx->block_align);
985 2950 nb_samples = (buf_size - 6 * ch) * 2 / ch;
986 2950 break;
987 case AV_CODEC_ID_ADPCM_MTAF:
988 if (avctx->block_align > 0)
989 buf_size = FFMIN(buf_size, avctx->block_align);
990 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
991 break;
992 57 case AV_CODEC_ID_ADPCM_SBPRO_2:
993 case AV_CODEC_ID_ADPCM_SBPRO_3:
994 case AV_CODEC_ID_ADPCM_SBPRO_4:
995 {
996 int samples_per_byte;
997
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
57 switch (avctx->codec->id) {
998 13 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
999 18 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
1000 26 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
1001 }
1002
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 54 times.
57 if (!s->status[0].step_index) {
1003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (buf_size < ch)
1004 return AVERROR_INVALIDDATA;
1005 3 nb_samples++;
1006 3 buf_size -= ch;
1007 }
1008 57 nb_samples += buf_size * samples_per_byte / ch;
1009 57 break;
1010 }
1011 195 case AV_CODEC_ID_ADPCM_SWF:
1012 {
1013 195 int buf_bits = buf_size * 8 - 2;
1014 195 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1015 195 int block_hdr_size = 22 * ch;
1016 195 int block_size = block_hdr_size + nbits * ch * 4095;
1017 195 int nblocks = buf_bits / block_size;
1018 195 int bits_left = buf_bits - nblocks * block_size;
1019 195 nb_samples = nblocks * 4096;
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (bits_left >= block_hdr_size)
1021 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
1022 195 break;
1023 }
1024 71 case AV_CODEC_ID_ADPCM_THP:
1025 case AV_CODEC_ID_ADPCM_THP_LE:
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 if (avctx->extradata) {
1027 nb_samples = buf_size * 14 / (8 * ch);
1028 break;
1029 }
1030 71 has_coded_samples = 1;
1031 71 bytestream2_skip(gb, 4); // channel size
1032 142 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
142 bytestream2_get_le32(gb) :
1034 71 bytestream2_get_be32(gb);
1035 71 buf_size -= 8 + 36 * ch;
1036 71 buf_size /= ch;
1037 71 nb_samples = buf_size / 8 * 14;
1038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 if (buf_size % 8 > 1)
1039 nb_samples += (buf_size % 8 - 1) * 2;
1040 71 *approx_nb_samples = 1;
1041 71 break;
1042 12 case AV_CODEC_ID_ADPCM_AFC:
1043 12 nb_samples = buf_size / (9 * ch) * 16;
1044 12 break;
1045 37 case AV_CODEC_ID_ADPCM_XA:
1046 37 nb_samples = (buf_size / 128) * 224 / ch;
1047 37 break;
1048 case AV_CODEC_ID_ADPCM_XMD:
1049 nb_samples = buf_size / (21 * ch) * 32;
1050 break;
1051 32 case AV_CODEC_ID_ADPCM_DTK:
1052 case AV_CODEC_ID_ADPCM_PSX:
1053 32 nb_samples = buf_size / (16 * ch) * 28;
1054 32 break;
1055 946 case AV_CODEC_ID_ADPCM_ARGO:
1056 946 nb_samples = buf_size / avctx->block_align * 32;
1057 946 break;
1058 case AV_CODEC_ID_ADPCM_ZORK:
1059 nb_samples = buf_size / ch;
1060 break;
1061 }
1062
1063 /* validate coded sample count */
1064
4/6
✓ Branch 0 taken 838 times.
✓ Branch 1 taken 8227 times.
✓ Branch 2 taken 838 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 838 times.
9065 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1065 return AVERROR_INVALIDDATA;
1066
1067 9065 return nb_samples;
1068 }
1069
1070 37697 static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1071 int *got_frame_ptr, AVPacket *avpkt)
1072 {
1073 37697 const uint8_t *buf = avpkt->data;
1074 37697 int buf_size = avpkt->size;
1075 37697 ADPCMDecodeContext *c = avctx->priv_data;
1076 37697 int channels = avctx->ch_layout.nb_channels;
1077 int16_t *samples;
1078 int16_t **samples_p;
1079 int st; /* stereo */
1080 int nb_samples, coded_samples, approx_nb_samples, ret;
1081 GetByteContext gb;
1082
1083 37697 bytestream2_init(&gb, buf, buf_size);
1084 37697 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37697 times.
37697 if (nb_samples <= 0) {
1086 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
1087 return AVERROR_INVALIDDATA;
1088 }
1089
1090 /* get output buffer */
1091 37697 frame->nb_samples = nb_samples;
1092
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37697 times.
37697 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1093 return ret;
1094 37697 samples = (int16_t *)frame->data[0];
1095 37697 samples_p = (int16_t **)frame->extended_data;
1096
1097 /* use coded_samples when applicable */
1098 /* it is always <= nb_samples, so the output buffer will be large enough */
1099
2/2
✓ Branch 0 taken 838 times.
✓ Branch 1 taken 36859 times.
37697 if (coded_samples) {
1100
3/4
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 471 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 367 times.
838 if (!approx_nb_samples && coded_samples != nb_samples)
1101 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
1102 838 frame->nb_samples = nb_samples = coded_samples;
1103 }
1104
1105 37697 st = channels == 2 ? 1 : 0;
1106
1107
32/43
✓ Branch 0 taken 26342 times.
✓ Branch 1 taken 1668 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2950 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 649 times.
✓ Branch 7 taken 642 times.
✓ Branch 8 taken 119 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 179 times.
✓ Branch 12 taken 127 times.
✓ Branch 13 taken 87 times.
✓ Branch 14 taken 85 times.
✓ Branch 15 taken 39 times.
✓ Branch 16 taken 54 times.
✓ Branch 17 taken 1000 times.
✓ Branch 18 taken 311 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 37 times.
✓ Branch 21 taken 47 times.
✓ Branch 22 taken 49 times.
✓ Branch 23 taken 158 times.
✓ Branch 24 taken 39 times.
✓ Branch 25 taken 400 times.
✓ Branch 26 taken 600 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 162 times.
✓ Branch 29 taken 350 times.
✓ Branch 30 taken 128 times.
✓ Branch 31 taken 57 times.
✓ Branch 32 taken 195 times.
✓ Branch 33 taken 136 times.
✗ Branch 34 not taken.
✓ Branch 35 taken 12 times.
✓ Branch 36 taken 71 times.
✓ Branch 37 taken 32 times.
✗ Branch 38 not taken.
✓ Branch 39 taken 946 times.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
37697 switch(avctx->codec->id) {
1108
10/12
✓ Branch 1 taken 43648 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39464 times.
✓ Branch 4 taken 4184 times.
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 43618 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 43648 times.
✓ Branch 13 taken 1396736 times.
✓ Branch 14 taken 43648 times.
✓ Branch 15 taken 43648 times.
✓ Branch 16 taken 26342 times.
1466726 CASE(ADPCM_IMA_QT,
1109 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
1110 Channel data is interleaved per-chunk. */
1111 for (int channel = 0; channel < channels; channel++) {
1112 ADPCMChannelStatus *cs = &c->status[channel];
1113 int predictor;
1114 int step_index;
1115 /* (pppppp) (piiiiiii) */
1116
1117 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
1118 predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1119 step_index = predictor & 0x7F;
1120 predictor &= ~0x7F;
1121
1122 if (cs->step_index == step_index) {
1123 int diff = predictor - cs->predictor;
1124 if (diff < 0)
1125 diff = - diff;
1126 if (diff > 0x7f)
1127 goto update;
1128 } else {
1129 update:
1130 cs->step_index = step_index;
1131 cs->predictor = predictor;
1132 }
1133
1134 if (cs->step_index > 88u){
1135 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1136 channel, cs->step_index);
1137 return AVERROR_INVALIDDATA;
1138 }
1139
1140 samples = samples_p[channel];
1141
1142 for (int m = 0; m < 64; m += 2) {
1143 int byte = bytestream2_get_byteu(&gb);
1144 samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1145 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1146 }
1147 }
1148 ) /* End of CASE */
1149
10/22
✗ Branch 2 not taken.
✓ Branch 3 taken 3336 times.
✓ Branch 5 taken 3336 times.
✓ Branch 6 taken 1668 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1668 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 25 taken 1107936 times.
✓ Branch 26 taken 276984 times.
✓ Branch 27 taken 276984 times.
✓ Branch 28 taken 138492 times.
✓ Branch 29 taken 138492 times.
✓ Branch 30 taken 1668 times.
1528416 CASE(ADPCM_IMA_WAV,
1150 for (int i = 0; i < channels; i++) {
1151 ADPCMChannelStatus *cs = &c->status[i];
1152 cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1153
1154 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1155 if (cs->step_index > 88u){
1156 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1157 i, cs->step_index);
1158 return AVERROR_INVALIDDATA;
1159 }
1160 }
1161
1162 if (avctx->bits_per_coded_sample != 4) {
1163 int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1164 int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1165 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1166 GetBitContext g;
1167
1168 for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1169 for (int i = 0; i < channels; i++) {
1170 ADPCMChannelStatus *cs = &c->status[i];
1171 samples = &samples_p[i][1 + n * samples_per_block];
1172 for (int j = 0; j < block_size; j++) {
1173 temp[j] = buf[4 * channels + block_size * n * channels +
1174 (j % 4) + (j / 4) * (channels * 4) + i * 4];
1175 }
1176 ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1177 if (ret < 0)
1178 return ret;
1179 for (int m = 0; m < samples_per_block; m++) {
1180 samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1181 avctx->bits_per_coded_sample);
1182 }
1183 }
1184 }
1185 bytestream2_skip(&gb, avctx->block_align - channels * 4);
1186 } else {
1187 for (int n = 0; n < (nb_samples - 1) / 8; n++) {
1188 for (int i = 0; i < channels; i++) {
1189 ADPCMChannelStatus *cs = &c->status[i];
1190 samples = &samples_p[i][1 + n * 8];
1191 for (int m = 0; m < 8; m += 2) {
1192 int v = bytestream2_get_byteu(&gb);
1193 samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1194 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1195 }
1196 }
1197 }
1198 }
1199 ) /* End of CASE */
1200
9/10
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 26 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 52 times.
✓ Branch 7 taken 52 times.
✓ Branch 8 taken 26 times.
✓ Branch 12 taken 38224 times.
✓ Branch 13 taken 52 times.
✓ Branch 14 taken 52 times.
✓ Branch 15 taken 26 times.
38406 CASE(ADPCM_4XM,
1201 for (int i = 0; i < channels; i++)
1202 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1203
1204 for (int i = 0; i < channels; i++) {
1205 c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1206 if (c->status[i].step_index > 88u) {
1207 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1208 i, c->status[i].step_index);
1209 return AVERROR_INVALIDDATA;
1210 }
1211 }
1212
1213 for (int i = 0; i < channels; i++) {
1214 ADPCMChannelStatus *cs = &c->status[i];
1215 samples = (int16_t *)frame->data[i];
1216 for (int n = nb_samples >> 1; n > 0; n--) {
1217 int v = bytestream2_get_byteu(&gb);
1218 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1219 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1220 }
1221 }
1222 ) /* End of CASE */
1223 CASE(ADPCM_AGM,
1224 for (int i = 0; i < channels; i++)
1225 c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1226 for (int i = 0; i < channels; i++)
1227 c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1228
1229 for (int n = 0; n < nb_samples >> (1 - st); n++) {
1230 int v = bytestream2_get_byteu(&gb);
1231 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1232 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1233 }
1234 ) /* End of CASE */
1235
17/26
✗ Branch 0 not taken.
✓ Branch 1 taken 2950 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 2950 times.
✓ Branch 20 taken 2895 times.
✓ Branch 21 taken 55 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2895 times.
✓ Branch 27 taken 2895 times.
✓ Branch 28 taken 55 times.
✓ Branch 31 taken 2895 times.
✓ Branch 32 taken 55 times.
✓ Branch 35 taken 2895 times.
✓ Branch 36 taken 55 times.
✓ Branch 38 taken 2895 times.
✓ Branch 39 taken 55 times.
✓ Branch 40 taken 2895 times.
✓ Branch 41 taken 55 times.
✓ Branch 45 taken 1116717 times.
✓ Branch 46 taken 2950 times.
1119667 CASE(ADPCM_MS,
1236 int block_predictor;
1237
1238 if (avctx->ch_layout.nb_channels > 2) {
1239 for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
1240 samples = samples_p[channel];
1241 block_predictor = bytestream2_get_byteu(&gb);
1242 if (block_predictor > 6) {
1243 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1244 channel, block_predictor);
1245 return AVERROR_INVALIDDATA;
1246 }
1247 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1248 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1249 c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1250 c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1251 c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1252 *samples++ = c->status[channel].sample2;
1253 *samples++ = c->status[channel].sample1;
1254 for (int n = (nb_samples - 2) >> 1; n > 0; n--) {
1255 int byte = bytestream2_get_byteu(&gb);
1256 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1257 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1258 }
1259 }
1260 } else {
1261 block_predictor = bytestream2_get_byteu(&gb);
1262 if (block_predictor > 6) {
1263 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1264 block_predictor);
1265 return AVERROR_INVALIDDATA;
1266 }
1267 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1268 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1269 if (st) {
1270 block_predictor = bytestream2_get_byteu(&gb);
1271 if (block_predictor > 6) {
1272 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1273 block_predictor);
1274 return AVERROR_INVALIDDATA;
1275 }
1276 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1277 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1278 }
1279 c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1280 if (st){
1281 c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1282 }
1283
1284 c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1285 if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1286 c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1287 if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1288
1289 *samples++ = c->status[0].sample2;
1290 if (st) *samples++ = c->status[1].sample2;
1291 *samples++ = c->status[0].sample1;
1292 if (st) *samples++ = c->status[1].sample1;
1293 for (int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1294 int byte = bytestream2_get_byteu(&gb);
1295 *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1296 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1297 }
1298 }
1299 ) /* End of CASE */
1300 CASE(ADPCM_MTAF,
1301 for (int channel = 0; channel < channels; channel += 2) {
1302 bytestream2_skipu(&gb, 4);
1303 c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1304 c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1305 c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1306 bytestream2_skipu(&gb, 2);
1307 c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1308 bytestream2_skipu(&gb, 2);
1309 for (int n = 0; n < nb_samples; n += 2) {
1310 int v = bytestream2_get_byteu(&gb);
1311 samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1312 samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1313 }
1314 for (int n = 0; n < nb_samples; n += 2) {
1315 int v = bytestream2_get_byteu(&gb);
1316 samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1317 samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1318 }
1319 }
1320 ) /* End of CASE */
1321
5/6
✗ Branch 2 not taken.
✓ Branch 3 taken 1298 times.
✓ Branch 5 taken 1298 times.
✓ Branch 6 taken 649 times.
✓ Branch 10 taken 1323960 times.
✓ Branch 11 taken 649 times.
1325907 CASE(ADPCM_IMA_DK4,
1322 for (int channel = 0; channel < channels; channel++) {
1323 ADPCMChannelStatus *cs = &c->status[channel];
1324 cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1325 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1326 if (cs->step_index > 88u){
1327 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1328 channel, cs->step_index);
1329 return AVERROR_INVALIDDATA;
1330 }
1331 }
1332 for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1333 int v = bytestream2_get_byteu(&gb);
1334 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1335 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1336 }
1337 ) /* End of CASE */
1338
1339 /* DK3 ADPCM support macro */
1340 #define DK3_GET_NEXT_NIBBLE() \
1341 if (decode_top_nibble_next) { \
1342 nibble = last_byte >> 4; \
1343 decode_top_nibble_next = 0; \
1344 } else { \
1345 last_byte = bytestream2_get_byteu(&gb); \
1346 nibble = last_byte & 0x0F; \
1347 decode_top_nibble_next = 1; \
1348 }
1349
11/14
✓ Branch 5 taken 642 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 642 times.
✓ Branch 10 taken 434634 times.
✓ Branch 11 taken 434634 times.
✓ Branch 14 taken 434634 times.
✓ Branch 15 taken 434634 times.
✓ Branch 18 taken 434634 times.
✓ Branch 19 taken 434634 times.
✓ Branch 22 taken 869268 times.
✓ Branch 23 taken 642 times.
✓ Branch 25 taken 642 times.
✗ Branch 26 not taken.
869910 CASE(ADPCM_IMA_DK3,
1350 int last_byte = 0;
1351 int nibble;
1352 int decode_top_nibble_next = 0;
1353 int diff_channel;
1354 const int16_t *samples_end = samples + channels * nb_samples;
1355
1356 bytestream2_skipu(&gb, 10);
1357 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1358 c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1359 c->status[0].step_index = bytestream2_get_byteu(&gb);
1360 c->status[1].step_index = bytestream2_get_byteu(&gb);
1361 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1362 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1363 c->status[0].step_index, c->status[1].step_index);
1364 return AVERROR_INVALIDDATA;
1365 }
1366 /* sign extend the predictors */
1367 diff_channel = c->status[1].predictor;
1368
1369 while (samples < samples_end) {
1370
1371 /* for this algorithm, c->status[0] is the sum channel and
1372 * c->status[1] is the diff channel */
1373
1374 /* process the first predictor of the sum channel */
1375 DK3_GET_NEXT_NIBBLE();
1376 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1377
1378 /* process the diff channel predictor */
1379 DK3_GET_NEXT_NIBBLE();
1380 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1381
1382 /* process the first pair of stereo PCM samples */
1383 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1384 *samples++ = c->status[0].predictor + c->status[1].predictor;
1385 *samples++ = c->status[0].predictor - c->status[1].predictor;
1386
1387 /* process the second predictor of the sum channel */
1388 DK3_GET_NEXT_NIBBLE();
1389 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1390
1391 /* process the second pair of stereo PCM samples */
1392 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1393 *samples++ = c->status[0].predictor + c->status[1].predictor;
1394 *samples++ = c->status[0].predictor - c->status[1].predictor;
1395 }
1396
1397 if ((bytestream2_tell(&gb) & 1))
1398 bytestream2_skip(&gb, 1);
1399 ) /* End of CASE */
1400
6/8
✗ Branch 2 not taken.
✓ Branch 3 taken 119 times.
✓ Branch 5 taken 119 times.
✓ Branch 6 taken 119 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 60452 times.
✓ Branch 12 taken 60452 times.
✓ Branch 13 taken 119 times.
60690 CASE(ADPCM_IMA_ISS,
1401 for (int channel = 0; channel < channels; channel++) {
1402 ADPCMChannelStatus *cs = &c->status[channel];
1403 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1404 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1405 if (cs->step_index > 88u){
1406 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1407 channel, cs->step_index);
1408 return AVERROR_INVALIDDATA;
1409 }
1410 }
1411
1412 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1413 int v1, v2;
1414 int v = bytestream2_get_byteu(&gb);
1415 /* nibbles are swapped for mono */
1416 if (st) {
1417 v1 = v >> 4;
1418 v2 = v & 0x0F;
1419 } else {
1420 v2 = v >> 4;
1421 v1 = v & 0x0F;
1422 }
1423 *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1424 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1425 }
1426 ) /* End of CASE */
1427 CASE(ADPCM_IMA_MOFLEX,
1428 for (int channel = 0; channel < channels; channel++) {
1429 ADPCMChannelStatus *cs = &c->status[channel];
1430 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1431 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1432 if (cs->step_index > 88u){
1433 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1434 channel, cs->step_index);
1435 return AVERROR_INVALIDDATA;
1436 }
1437 }
1438
1439 for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1440 for (int channel = 0; channel < channels; channel++) {
1441 samples = samples_p[channel] + 256 * subframe;
1442 for (int n = 0; n < 256; n += 2) {
1443 int v = bytestream2_get_byteu(&gb);
1444 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1445 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1446 }
1447 }
1448 }
1449 ) /* End of CASE */
1450 CASE(ADPCM_IMA_DAT4,
1451 for (int channel = 0; channel < channels; channel++) {
1452 ADPCMChannelStatus *cs = &c->status[channel];
1453 samples = samples_p[channel];
1454 bytestream2_skip(&gb, 4);
1455 for (int n = 0; n < nb_samples; n += 2) {
1456 int v = bytestream2_get_byteu(&gb);
1457 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1458 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1459 }
1460 }
1461 ) /* End of CASE */
1462
2/2
✓ Branch 3 taken 732060 times.
✓ Branch 4 taken 179 times.
732239 CASE(ADPCM_IMA_APC,
1463 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1464 int v = bytestream2_get_byteu(&gb);
1465 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1466 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1467 }
1468 ) /* End of CASE */
1469
2/2
✓ Branch 3 taken 514600 times.
✓ Branch 4 taken 127 times.
514727 CASE(ADPCM_IMA_SSI,
1470 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1471 int v = bytestream2_get_byteu(&gb);
1472 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1473 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1474 }
1475 ) /* End of CASE */
1476
4/4
✓ Branch 3 taken 347109 times.
✓ Branch 4 taken 181830 times.
✓ Branch 5 taken 181830 times.
✓ Branch 6 taken 87 times.
529026 CASE(ADPCM_IMA_APM,
1477 for (int n = nb_samples / 2; n > 0; n--) {
1478 for (int channel = 0; channel < channels; channel++) {
1479 int v = bytestream2_get_byteu(&gb);
1480 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1481 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1482 }
1483 samples += channels;
1484 }
1485 ) /* End of CASE */
1486
4/4
✓ Branch 3 taken 339672 times.
✓ Branch 4 taken 172372 times.
✓ Branch 5 taken 172372 times.
✓ Branch 6 taken 85 times.
512129 CASE(ADPCM_IMA_ALP,
1487 for (int n = nb_samples / 2; n > 0; n--) {
1488 for (int channel = 0; channel < channels; channel++) {
1489 int v = bytestream2_get_byteu(&gb);
1490 *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1491 samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1492 }
1493 samples += channels;
1494 }
1495 ) /* End of CASE */
1496
4/4
✓ Branch 3 taken 193014 times.
✓ Branch 4 taken 51 times.
✓ Branch 5 taken 51 times.
✓ Branch 6 taken 39 times.
193104 CASE(ADPCM_IMA_CUNNING,
1497 for (int channel = 0; channel < channels; channel++) {
1498 int16_t *smp = samples_p[channel];
1499 for (int n = 0; n < nb_samples / 2; n++) {
1500 int v = bytestream2_get_byteu(&gb);
1501 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1502 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1503 }
1504 }
1505 ) /* End of CASE */
1506
2/2
✓ Branch 3 taken 27562 times.
✓ Branch 4 taken 54 times.
27616 CASE(ADPCM_IMA_OKI,
1507 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1508 int v = bytestream2_get_byteu(&gb);
1509 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1510 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1511 }
1512 ) /* End of CASE */
1513
10/12
✗ Branch 2 not taken.
✓ Branch 3 taken 2000 times.
✓ Branch 5 taken 2000 times.
✓ Branch 6 taken 1000 times.
✓ Branch 8 taken 16000 times.
✗ Branch 9 not taken.
✓ Branch 12 taken 32000 times.
✓ Branch 13 taken 16000 times.
✓ Branch 15 taken 32000 times.
✓ Branch 16 taken 16000 times.
✓ Branch 17 taken 16000 times.
✓ Branch 18 taken 1000 times.
83000 CASE(ADPCM_IMA_RAD,
1514 for (int channel = 0; channel < channels; channel++) {
1515 ADPCMChannelStatus *cs = &c->status[channel];
1516 cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1517 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1518 if (cs->step_index > 88u){
1519 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1520 channel, cs->step_index);
1521 return AVERROR_INVALIDDATA;
1522 }
1523 }
1524 for (int n = 0; n < nb_samples / 2; n++) {
1525 int byte[2];
1526
1527 byte[0] = bytestream2_get_byteu(&gb);
1528 if (st)
1529 byte[1] = bytestream2_get_byteu(&gb);
1530 for (int channel = 0; channel < channels; channel++) {
1531 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1532 }
1533 for (int channel = 0; channel < channels; channel++) {
1534 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1535 }
1536 }
1537 ) /* End of CASE */
1538
10/10
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 299 times.
✓ Branch 5 taken 8820 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 12 times.
✓ Branch 12 taken 298777 times.
✓ Branch 13 taken 166477 times.
✓ Branch 14 taken 166477 times.
✓ Branch 15 taken 299 times.
474397 CASE(ADPCM_IMA_WS,
1539 if (c->vqa_version == 3) {
1540 for (int channel = 0; channel < channels; channel++) {
1541 int16_t *smp = samples_p[channel];
1542
1543 for (int n = nb_samples / 2; n > 0; n--) {
1544 int v = bytestream2_get_byteu(&gb);
1545 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1546 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1547 }
1548 }
1549 } else {
1550 for (int n = nb_samples / 2; n > 0; n--) {
1551 for (int channel = 0; channel < channels; channel++) {
1552 int v = bytestream2_get_byteu(&gb);
1553 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1554 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1555 }
1556 samples += channels;
1557 }
1558 }
1559 bytestream2_seek(&gb, 0, SEEK_END);
1560 ) /* End of CASE */
1561 CASE(ADPCM_XMD,
1562 int bytes_remaining, block = 0;
1563 while (bytestream2_get_bytes_left(&gb) >= 21 * channels) {
1564 for (int channel = 0; channel < channels; channel++) {
1565 int16_t *out = samples_p[channel] + block * 32;
1566 int16_t history[2];
1567 uint16_t scale;
1568
1569 history[1] = sign_extend(bytestream2_get_le16(&gb), 16);
1570 history[0] = sign_extend(bytestream2_get_le16(&gb), 16);
1571 scale = bytestream2_get_le16(&gb);
1572
1573 out[0] = history[1];
1574 out[1] = history[0];
1575
1576 for (int n = 0; n < 15; n++) {
1577 unsigned byte = bytestream2_get_byte(&gb);
1578 int32_t nibble[2];
1579
1580 nibble[0] = sign_extend(byte & 15, 4);
1581 nibble[1] = sign_extend(byte >> 4, 4);
1582
1583 out[2+n*2] = nibble[0]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
1584 history[1] = history[0];
1585 history[0] = out[2+n*2];
1586
1587 out[2+n*2+1] = nibble[1]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
1588 history[1] = history[0];
1589 history[0] = out[2+n*2+1];
1590 }
1591 }
1592
1593 block++;
1594 }
1595 bytes_remaining = bytestream2_get_bytes_left(&gb);
1596 if (bytes_remaining > 0) {
1597 bytestream2_skip(&gb, bytes_remaining);
1598 }
1599 ) /* End of CASE */
1600
4/6
✗ Branch 2 not taken.
✓ Branch 3 taken 666 times.
✓ Branch 6 taken 666 times.
✓ Branch 7 taken 37 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 37 times.
703 CASE(ADPCM_XA,
1601 int16_t *out0 = samples_p[0];
1602 int16_t *out1 = samples_p[1];
1603 int samples_per_block = 28 * (3 - channels) * 4;
1604 int sample_offset = 0;
1605 int bytes_remaining;
1606 while (bytestream2_get_bytes_left(&gb) >= 128) {
1607 if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1608 &c->status[0], &c->status[1],
1609 channels, sample_offset)) < 0)
1610 return ret;
1611 bytestream2_skipu(&gb, 128);
1612 sample_offset += samples_per_block;
1613 }
1614 /* Less than a full block of data left, e.g. when reading from
1615 * 2324 byte per sector XA; the remainder is padding */
1616 bytes_remaining = bytestream2_get_bytes_left(&gb);
1617 if (bytes_remaining > 0) {
1618 bytestream2_skip(&gb, bytes_remaining);
1619 }
1620 ) /* End of CASE */
1621
8/10
✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
✓ Branch 4 taken 94 times.
✓ Branch 5 taken 47 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 94 times.
✓ Branch 9 taken 94 times.
✓ Branch 10 taken 47 times.
✓ Branch 14 taken 68996 times.
✓ Branch 15 taken 47 times.
69231 CASE(ADPCM_IMA_EA_EACS,
1622 for (int i = 0; i <= st; i++) {
1623 c->status[i].step_index = bytestream2_get_le32u(&gb);
1624 if (c->status[i].step_index > 88u) {
1625 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1626 i, c->status[i].step_index);
1627 return AVERROR_INVALIDDATA;
1628 }
1629 }
1630 for (int i = 0; i <= st; i++) {
1631 c->status[i].predictor = bytestream2_get_le32u(&gb);
1632 if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1633 return AVERROR_INVALIDDATA;
1634 }
1635
1636 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1637 int byte = bytestream2_get_byteu(&gb);
1638 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1639 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1640 }
1641 ) /* End of CASE */
1642
2/2
✓ Branch 3 taken 71392 times.
✓ Branch 4 taken 49 times.
71441 CASE(ADPCM_IMA_EA_SEAD,
1643 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1644 int byte = bytestream2_get_byteu(&gb);
1645 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1646 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1647 }
1648 ) /* End of CASE */
1649
9/16
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 9 taken 8296 times.
✗ Branch 10 not taken.
✓ Branch 13 taken 232288 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 240584 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 232288 times.
✓ Branch 18 taken 8296 times.
✓ Branch 19 taken 8296 times.
✓ Branch 20 taken 158 times.
✓ Branch 21 taken 158 times.
✗ Branch 22 not taken.
240742 CASE(ADPCM_EA,
1650 int previous_left_sample, previous_right_sample;
1651 int current_left_sample, current_right_sample;
1652 int next_left_sample, next_right_sample;
1653 int coeff1l, coeff2l, coeff1r, coeff2r;
1654 int shift_left, shift_right;
1655
1656 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte (stereo) or 15-byte (mono) pieces,
1657 each coding 28 stereo/mono samples. */
1658
1659 if (channels != 2 && channels != 1)
1660 return AVERROR_INVALIDDATA;
1661
1662 current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1663 previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1664 current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1665 previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1666
1667 for (int count1 = 0; count1 < nb_samples / 28; count1++) {
1668 int byte = bytestream2_get_byteu(&gb);
1669 coeff1l = ea_adpcm_table[ byte >> 4 ];
1670 coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1671 coeff1r = ea_adpcm_table[ byte & 0x0F];
1672 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1673
1674 if (channels == 2){
1675 byte = bytestream2_get_byteu(&gb);
1676 shift_left = 20 - (byte >> 4);
1677 shift_right = 20 - (byte & 0x0F);
1678 } else{
1679 /* Mono packs the shift into the coefficient byte's lower nibble instead */
1680 shift_left = 20 - (byte & 0x0F);
1681 }
1682
1683 for (int count2 = 0; count2 < (channels == 2 ? 28 : 14); count2++) {
1684 byte = bytestream2_get_byteu(&gb);
1685 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1686
1687 next_left_sample = (next_left_sample +
1688 (current_left_sample * coeff1l) +
1689 (previous_left_sample * coeff2l) + 0x80) >> 8;
1690
1691 previous_left_sample = current_left_sample;
1692 current_left_sample = av_clip_int16(next_left_sample);
1693 *samples++ = current_left_sample;
1694
1695 if (channels == 2){
1696 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1697
1698 next_right_sample = (next_right_sample +
1699 (current_right_sample * coeff1r) +
1700 (previous_right_sample * coeff2r) + 0x80) >> 8;
1701
1702 previous_right_sample = current_right_sample;
1703 current_right_sample = av_clip_int16(next_right_sample);
1704 *samples++ = current_right_sample;
1705 } else {
1706 next_left_sample = sign_extend(byte, 4) * (1 << shift_left);
1707
1708 next_left_sample = (next_left_sample +
1709 (current_left_sample * coeff1l) +
1710 (previous_left_sample * coeff2l) + 0x80) >> 8;
1711
1712 previous_left_sample = current_left_sample;
1713 current_left_sample = av_clip_int16(next_left_sample);
1714
1715 *samples++ = current_left_sample;
1716 }
1717 }
1718 }
1719 bytestream2_skip(&gb, channels == 2 ? 2 : 3); // Skip terminating NULs
1720 ) /* End of CASE */
1721
11/12
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 39 times.
✓ Branch 6 taken 546 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 2184 times.
✓ Branch 10 taken 1092 times.
✓ Branch 11 taken 1092 times.
✓ Branch 12 taken 546 times.
✓ Branch 13 taken 546 times.
✓ Branch 14 taken 39 times.
4095 CASE(ADPCM_EA_MAXIS_XA,
1722 int coeff[2][2], shift[2];
1723
1724 for (int channel = 0; channel < channels; channel++) {
1725 int byte = bytestream2_get_byteu(&gb);
1726 for (int i = 0; i < 2; i++)
1727 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1728 shift[channel] = 20 - (byte & 0x0F);
1729 }
1730 for (int count1 = 0; count1 < nb_samples / 2; count1++) {
1731 int byte[2];
1732
1733 byte[0] = bytestream2_get_byteu(&gb);
1734 if (st) byte[1] = bytestream2_get_byteu(&gb);
1735 for (int i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1736 for (int channel = 0; channel < channels; channel++) {
1737 int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1738 sample = (sample +
1739 c->status[channel].sample1 * coeff[channel][0] +
1740 c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1741 c->status[channel].sample2 = c->status[channel].sample1;
1742 c->status[channel].sample1 = av_clip_int16(sample);
1743 *samples++ = c->status[channel].sample1;
1744 }
1745 }
1746 }
1747 bytestream2_seek(&gb, 0, SEEK_END);
1748 ) /* End of CASE */
1749 #if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER
1750 400 case AV_CODEC_ID_ADPCM_EA_R1:
1751 case AV_CODEC_ID_ADPCM_EA_R2:
1752 case AV_CODEC_ID_ADPCM_EA_R3: {
1753 /* channel numbering
1754 2chan: 0=fl, 1=fr
1755 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1756 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1757 400 const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1758 int previous_sample, current_sample, next_sample;
1759 int coeff1, coeff2;
1760 int shift;
1761 uint16_t *samplesC;
1762 400 int count = 0;
1763 int offsets[6];
1764
1765
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
1200 for (unsigned channel = 0; channel < channels; channel++)
1766
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 550 times.
800 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1767 550 bytestream2_get_le32(&gb)) +
1768 800 (channels + 1) * 4;
1769
1770
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
1200 for (unsigned channel = 0; channel < channels; channel++) {
1771 int count1;
1772
1773 800 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1774 800 samplesC = samples_p[channel];
1775
1776
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 610 times.
800 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1777 190 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1778 190 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1779 } else {
1780 610 current_sample = c->status[channel].predictor;
1781 610 previous_sample = c->status[channel].prev_sample;
1782 }
1783
1784
2/2
✓ Branch 0 taken 41192 times.
✓ Branch 1 taken 800 times.
41992 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1785 41192 int byte = bytestream2_get_byte(&gb);
1786
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 41180 times.
41192 if (byte == 0xEE) { /* only seen in R2 and R3 */
1787 12 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1788 12 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1789
1790
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 12 times.
348 for (int count2 = 0; count2 < 28; count2++)
1791 336 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1792 } else {
1793 41180 coeff1 = ea_adpcm_table[ byte >> 4 ];
1794 41180 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1795 41180 shift = 20 - (byte & 0x0F);
1796
1797
2/2
✓ Branch 0 taken 1153040 times.
✓ Branch 1 taken 41180 times.
1194220 for (int count2 = 0; count2 < 28; count2++) {
1798
2/2
✓ Branch 0 taken 576520 times.
✓ Branch 1 taken 576520 times.
1153040 if (count2 & 1)
1799 576520 next_sample = (unsigned)sign_extend(byte, 4) << shift;
1800 else {
1801 576520 byte = bytestream2_get_byte(&gb);
1802 576520 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1803 }
1804
1805 1153040 next_sample += (current_sample * coeff1) +
1806 1153040 (previous_sample * coeff2);
1807 1153040 next_sample = av_clip_int16(next_sample >> 8);
1808
1809 1153040 previous_sample = current_sample;
1810 1153040 current_sample = next_sample;
1811 1153040 *samplesC++ = current_sample;
1812 }
1813 }
1814 }
1815
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 400 times.
800 if (!count) {
1816 400 count = count1;
1817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 } else if (count != count1) {
1818 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1819 count = FFMAX(count, count1);
1820 }
1821
1822
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 190 times.
800 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1823 610 c->status[channel].predictor = current_sample;
1824 610 c->status[channel].prev_sample = previous_sample;
1825 }
1826 }
1827
1828 400 frame->nb_samples = count * 28;
1829 400 bytestream2_seek(&gb, 0, SEEK_END);
1830 400 break;
1831 }
1832 #endif /* CONFIG_ADPCM_EA_Rx_DECODER */
1833
10/10
✓ Branch 1 taken 4800 times.
✓ Branch 2 taken 2400 times.
✓ Branch 4 taken 2400 times.
✓ Branch 5 taken 600 times.
✓ Branch 7 taken 36000 times.
✓ Branch 8 taken 9000 times.
✓ Branch 9 taken 9000 times.
✓ Branch 10 taken 600 times.
✓ Branch 11 taken 600 times.
✓ Branch 12 taken 600 times.
53400 CASE(ADPCM_EA_XAS,
1834 for (int channel=0; channel < channels; channel++) {
1835 int coeff[2][4], shift[4];
1836 int16_t *s = samples_p[channel];
1837 for (int n = 0; n < 4; n++, s += 32) {
1838 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1839 for (int i = 0; i < 2; i++)
1840 coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1841 s[0] = val & ~0x0F;
1842
1843 val = sign_extend(bytestream2_get_le16u(&gb), 16);
1844 shift[n] = 20 - (val & 0x0F);
1845 s[1] = val & ~0x0F;
1846 }
1847
1848 for (int m = 2; m < 32; m += 2) {
1849 s = &samples_p[channel][m];
1850 for (int n = 0; n < 4; n++, s += 32) {
1851 int level, pred;
1852 int byte = bytestream2_get_byteu(&gb);
1853
1854 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1855 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1856 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1857
1858 level = sign_extend(byte, 4) * (1 << shift[n]);
1859 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1860 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1861 }
1862 }
1863 }
1864 ) /* End of CASE */
1865 CASE(ADPCM_IMA_ACORN,
1866 for (int channel = 0; channel < channels; channel++) {
1867 ADPCMChannelStatus *cs = &c->status[channel];
1868 cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1869 cs->step_index = bytestream2_get_le16u(&gb) & 0xFF;
1870 if (cs->step_index > 88u){
1871 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1872 channel, cs->step_index);
1873 return AVERROR_INVALIDDATA;
1874 }
1875 }
1876 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1877 int byte = bytestream2_get_byteu(&gb);
1878 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3);
1879 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3);
1880 }
1881 ) /* End of CASE */
1882
7/10
✗ Branch 0 not taken.
✓ Branch 1 taken 162 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 162 times.
✓ Branch 13 taken 111618 times.
✓ Branch 14 taken 162 times.
✓ Branch 15 taken 22 times.
✓ Branch 16 taken 140 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 22 times.
111780 CASE(ADPCM_IMA_AMV,
1883 av_assert0(channels == 1);
1884
1885 /*
1886 * Header format:
1887 * int16_t predictor;
1888 * uint8_t step_index;
1889 * uint8_t reserved;
1890 * uint32_t frame_size;
1891 *
1892 * Some implementations have step_index as 16-bits, but others
1893 * only use the lower 8 and store garbage in the upper 8.
1894 */
1895 c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1896 c->status[0].step_index = bytestream2_get_byteu(&gb);
1897 bytestream2_skipu(&gb, 5);
1898 if (c->status[0].step_index > 88u) {
1899 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1900 c->status[0].step_index);
1901 return AVERROR_INVALIDDATA;
1902 }
1903
1904 for (int n = nb_samples >> 1; n > 0; n--) {
1905 int v = bytestream2_get_byteu(&gb);
1906
1907 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1908 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1909 }
1910
1911 if (nb_samples & 1) {
1912 int v = bytestream2_get_byteu(&gb);
1913 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1914
1915 if (v & 0x0F) {
1916 /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
1917 av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
1918 av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
1919 }
1920 }
1921 ) /* End of CASE */
1922
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 350 times.
✓ Branch 6 taken 350 times.
✓ Branch 7 taken 350 times.
✓ Branch 11 taken 89600 times.
✓ Branch 12 taken 350 times.
90300 CASE(ADPCM_IMA_SMJPEG,
1923 for (int i = 0; i < channels; i++) {
1924 c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1925 c->status[i].step_index = bytestream2_get_byteu(&gb);
1926 bytestream2_skipu(&gb, 1);
1927 if (c->status[i].step_index > 88u) {
1928 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1929 c->status[i].step_index);
1930 return AVERROR_INVALIDDATA;
1931 }
1932 }
1933
1934 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1935 int v = bytestream2_get_byteu(&gb);
1936
1937 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1938 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1939 }
1940 ) /* End of CASE */
1941
2/2
✓ Branch 3 taken 262096 times.
✓ Branch 4 taken 128 times.
262224 CASE(ADPCM_CT,
1942 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1943 int v = bytestream2_get_byteu(&gb);
1944 *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1945 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1946 }
1947 ) /* End of CASE */
1948 #if CONFIG_ADPCM_SBPRO_2_DECODER || CONFIG_ADPCM_SBPRO_3_DECODER || \
1949 CONFIG_ADPCM_SBPRO_4_DECODER
1950 57 case AV_CODEC_ID_ADPCM_SBPRO_4:
1951 case AV_CODEC_ID_ADPCM_SBPRO_3:
1952 case AV_CODEC_ID_ADPCM_SBPRO_2:
1953
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 54 times.
57 if (!c->status[0].step_index) {
1954 /* the first byte is a raw sample */
1955 3 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st)
1957 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1958 3 c->status[0].step_index = 1;
1959 3 nb_samples--;
1960 }
1961
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 31 times.
57 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1962
2/2
✓ Branch 0 taken 52230 times.
✓ Branch 1 taken 26 times.
52256 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1963 52230 int byte = bytestream2_get_byteu(&gb);
1964 104460 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1965 52230 byte >> 4, 4, 0);
1966 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1967 52230 byte & 0x0F, 4, 0);
1968 }
1969
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1970
2/2
✓ Branch 0 taken 34820 times.
✓ Branch 1 taken 18 times.
34838 for (int n = (nb_samples<<st) / 3; n > 0; n--) {
1971 34820 int byte = bytestream2_get_byteu(&gb);
1972 69640 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1973 34820 byte >> 5 , 3, 0);
1974 69640 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1975 34820 (byte >> 2) & 0x07, 3, 0);
1976 34820 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1977 34820 byte & 0x03, 2, 0);
1978 }
1979 } else {
1980
2/2
✓ Branch 0 taken 26115 times.
✓ Branch 1 taken 13 times.
26128 for (int n = nb_samples >> (2 - st); n > 0; n--) {
1981 26115 int byte = bytestream2_get_byteu(&gb);
1982 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1983 26115 byte >> 6 , 2, 2);
1984 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1985 26115 (byte >> 4) & 0x03, 2, 2