FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/adpcm.c
Date: 2026-10-19 08:11:58
Exec Total Coverage
Lines: 740 1046 70.7%
Functions: 21 30 70.0%
Branches: 550 942 58.4%

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 * 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
49 #include "libavutil/attributes.h"
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 */
84 static 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
92 static 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
97 static 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 */
111 static 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 */
121 static 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
130 static const int8_t adpcm_index_table2[4] = {
131 -1, 2,
132 -1, 2,
133 };
134
135 static const int8_t adpcm_index_table3[8] = {
136 -1, -1, 1, 2,
137 -1, -1, 1, 2,
138 };
139
140 static 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
145 static const int8_t * const adpcm_index_tables[4] = {
146 &adpcm_index_table2[0],
147 &adpcm_index_table3[0],
148 &ff_adpcm_index_table[0],
149 &adpcm_index_table5[0],
150 };
151
152 static 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
219 static 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
228 static 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
235 static const int8_t zork_index_table[8] = {
236 -1, -1, -1, 1, 4, 7, 10, 12,
237 };
238
239 static 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
244 static 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
263 static const int16_t rhetorex_index[8] = {
264 -83, -55, -27, 163, 374, 677, 1864, 3408,
265 };
266
267 static const int8_t citrix_index_table[4] = {
268 -1, 1, -1, 1,
269 };
270
271 /* end of tables */
272
273 typedef struct ADPCMDecodeContext {
274 ADPCMChannelStatus status[14];
275 int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
276 int has_status; /**< Status flag. Reset to 0 after a flush. */
277 } ADPCMDecodeContext;
278
279 static void adpcm_flush(AVCodecContext *avctx);
280
281 208 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
282 {
283 208 ADPCMDecodeContext *c = avctx->priv_data;
284 208 unsigned int min_channels = 1;
285 208 unsigned int max_channels = 2;
286
287 208 adpcm_flush(avctx);
288
289
7/9
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 161 times.
208 switch(avctx->codec->id) {
290 5 case AV_CODEC_ID_ADPCM_IMA_AMV:
291 case AV_CODEC_ID_ADPCM_N64:
292 case AV_CODEC_ID_ADPCM_RHETOREX:
293 5 max_channels = 1;
294 5 break;
295 6 case AV_CODEC_ID_ADPCM_SANYO:
296 6 max_channels = 2;
297 6 break;
298 25 case AV_CODEC_ID_ADPCM_AFC:
299 case AV_CODEC_ID_ADPCM_EA_R1:
300 case AV_CODEC_ID_ADPCM_EA_R2:
301 case AV_CODEC_ID_ADPCM_EA_R3:
302 case AV_CODEC_ID_ADPCM_EA_XAS:
303 case AV_CODEC_ID_ADPCM_MS:
304 25 max_channels = 6;
305 25 break;
306 ✗ case AV_CODEC_ID_ADPCM_MTAF:
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);
311 ✗ return AVERROR_PATCHWELCOME;
312 }
313 ✗ break;
314 2 case AV_CODEC_ID_ADPCM_DTK:
315 2 min_channels = 2;
316 2 break;
317 1 case AV_CODEC_ID_ADPCM_PSX:
318 1 max_channels = 8;
319
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (avctx->ch_layout.nb_channels <= 0 ||
320
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 avctx->block_align % (16 * avctx->ch_layout.nb_channels))
321 1 return AVERROR_INVALIDDATA;
322 ✗ break;
323 ✗ case AV_CODEC_ID_ADPCM_PSXC:
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;
329 8 case AV_CODEC_ID_ADPCM_IMA_DAT4:
330 case AV_CODEC_ID_ADPCM_THP:
331 case AV_CODEC_ID_ADPCM_THP_LE:
332 8 max_channels = 14;
333 8 break;
334 }
335
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 if (avctx->ch_layout.nb_channels < min_channels ||
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
207 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
6/7
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 179 times.
207 switch(avctx->codec->id) {
342 11 case AV_CODEC_ID_ADPCM_IMA_WAV:
343
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
344 ✗ return AVERROR_INVALIDDATA;
345 11 break;
346 6 case AV_CODEC_ID_ADPCM_ARGO:
347
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->bits_per_coded_sample != 4 ||
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 avctx->block_align != 17 * avctx->ch_layout.nb_channels)
349 ✗ return AVERROR_INVALIDDATA;
350 6 break;
351 6 case AV_CODEC_ID_ADPCM_SANYO:
352
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (avctx->bits_per_coded_sample < 3 || avctx->bits_per_coded_sample > 5)
353 ✗ return AVERROR_INVALIDDATA;
354 6 break;
355 1 case AV_CODEC_ID_ADPCM_IMA_XBOX:
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->bits_per_coded_sample != 4)
357 ✗ return AVERROR_INVALIDDATA;
358 1 break;
359 ✗ case AV_CODEC_ID_ADPCM_ZORK:
360 ✗ if (avctx->bits_per_coded_sample != 8)
361 ✗ return AVERROR_INVALIDDATA;
362 ✗ break;
363 4 case AV_CODEC_ID_ADPCM_IMA_CITRIX:
364
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (avctx->bits_per_coded_sample != 2 ||
365
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->block_align < 4 * avctx->ch_layout.nb_channels ||
366
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 (avctx->ch_layout.nb_channels == 2 && avctx->block_align % 8))
367 ✗ return AVERROR_INVALIDDATA;
368 4 break;
369 179 default:
370 179 break;
371 }
372
373
4/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 91 times.
207 switch (avctx->codec->id) {
374 95 case AV_CODEC_ID_ADPCM_AICA:
375 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
376 case AV_CODEC_ID_ADPCM_IMA_DAT4:
377 case AV_CODEC_ID_ADPCM_IMA_QT:
378 case AV_CODEC_ID_ADPCM_IMA_WAV:
379 case AV_CODEC_ID_ADPCM_IMA_XBOX:
380 case AV_CODEC_ID_ADPCM_4XM:
381 case AV_CODEC_ID_ADPCM_XA:
382 case AV_CODEC_ID_ADPCM_XMD:
383 case AV_CODEC_ID_ADPCM_EA_R1:
384 case AV_CODEC_ID_ADPCM_EA_R2:
385 case AV_CODEC_ID_ADPCM_EA_R3:
386 case AV_CODEC_ID_ADPCM_EA_XAS:
387 case AV_CODEC_ID_ADPCM_THP:
388 case AV_CODEC_ID_ADPCM_THP_LE:
389 case AV_CODEC_ID_ADPCM_AFC:
390 case AV_CODEC_ID_ADPCM_DTK:
391 case AV_CODEC_ID_ADPCM_PSX:
392 case AV_CODEC_ID_ADPCM_PSXC:
393 case AV_CODEC_ID_ADPCM_SANYO:
394 case AV_CODEC_ID_ADPCM_MTAF:
395 case AV_CODEC_ID_ADPCM_ARGO:
396 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
397 case AV_CODEC_ID_ADPCM_N64:
398 95 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
399 95 break;
400 8 case AV_CODEC_ID_ADPCM_IMA_WS:
401
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
402 AV_SAMPLE_FMT_S16;
403 8 break;
404 13 case AV_CODEC_ID_ADPCM_MS:
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
406 AV_SAMPLE_FMT_S16;
407 13 break;
408 91 default:
409 91 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
410 }
411 207 return 0;
412 }
413
414 ✗ static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
415 {
416 int delta, pred, step, add;
417
418 ✗ pred = c->predictor;
419 ✗ delta = nibble & 7;
420 ✗ step = c->step;
421 ✗ add = (delta * 2 + 1) * step;
422 ✗ if (add < 0)
423 ✗ add = add + 7;
424
425 ✗ if ((nibble & 8) == 0)
426 ✗ pred = av_clip(pred + (add >> 3), -32767, 32767);
427 else
428 ✗ pred = av_clip(pred - (add >> 3), -32767, 32767);
429
430 ✗ switch (delta) {
431 ✗ case 7:
432 ✗ step *= 0x99;
433 ✗ break;
434 ✗ case 6:
435 ✗ c->step = av_clip(c->step * 2, 127, 24576);
436 ✗ c->predictor = pred;
437 ✗ return pred;
438 ✗ case 5:
439 ✗ step *= 0x66;
440 ✗ break;
441 ✗ case 4:
442 ✗ step *= 0x4d;
443 ✗ break;
444 ✗ default:
445 ✗ step *= 0x39;
446 ✗ break;
447 }
448
449 ✗ if (step < 0)
450 ✗ step += 0x3f;
451
452 ✗ c->step = step >> 6;
453 ✗ c->step = av_clip(c->step, 127, 24576);
454 ✗ c->predictor = pred;
455 ✗ return pred;
456 }
457
458 ✗ static 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
481 8100424 static 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 8100424 step = ff_adpcm_step_table[c->step_index];
488 8100424 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
489 8100424 step_index = av_clip(step_index, 0, 88);
490
491 8100424 sign = nibble & 8;
492 8100424 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 8100424 diff = ((2 * delta + 1) * step) >> shift;
497 8100424 predictor = c->predictor;
498
2/2
✓ Branch 0 taken 4004685 times.
✓ Branch 1 taken 4095739 times.
8100424 if (sign) predictor -= diff;
499 4095739 else predictor += diff;
500
501 8100424 c->predictor = av_clip_int16(predictor);
502 8100424 c->step_index = step_index;
503
504 8100424 return (int16_t)c->predictor;
505 }
506
507 679344 static 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 679344 step = ff_adpcm_step_table[c->step_index];
514 679344 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
515 679344 step_index = av_clip(step_index, 0, 88);
516
517 679344 sign = nibble & 8;
518 679344 delta = nibble & 7;
519 679344 diff = (delta * step) >> shift;
520 679344 predictor = c->predictor;
521
2/2
✓ Branch 0 taken 339842 times.
✓ Branch 1 taken 339502 times.
679344 if (sign) predictor -= diff;
522 339502 else predictor += diff;
523
524 679344 c->predictor = av_clip_int16(predictor);
525 679344 c->step_index = step_index;
526
527 679344 return (int16_t)c->predictor;
528 }
529
530 ✗ static 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
546 386028 static 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 386028 nibble = sign_extend(nibble & 0xF, 4);
553
554 386028 step = ima_cunning_step_table[c->step_index];
555 386028 step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
556 386028 step_index = av_clip(step_index, 0, 60);
557
558 386028 predictor = c->predictor + step * nibble;
559
560 386028 c->predictor = av_clip_int16(predictor);
561 386028 c->step_index = step_index;
562
563 386028 return c->predictor;
564 }
565
566 ✗ static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
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
591 6911962 int16_t ff_adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
592 {
593 int step_index;
594 int predictor;
595 int diff, step;
596
597 6911962 step = ff_adpcm_step_table[c->step_index];
598 6911962 step_index = c->step_index + ff_adpcm_index_table[nibble];
599 6911962 step_index = av_clip(step_index, 0, 88);
600
601 6911962 diff = step >> 3;
602
2/2
✓ Branch 0 taken 1590213 times.
✓ Branch 1 taken 5321749 times.
6911962 if (nibble & 4) diff += step;
603
2/2
✓ Branch 0 taken 2944916 times.
✓ Branch 1 taken 3967046 times.
6911962 if (nibble & 2) diff += step >> 1;
604
2/2
✓ Branch 0 taken 3194806 times.
✓ Branch 1 taken 3717156 times.
6911962 if (nibble & 1) diff += step >> 2;
605
606
2/2
✓ Branch 0 taken 3443931 times.
✓ Branch 1 taken 3468031 times.
6911962 if (nibble & 8)
607 3443931 predictor = c->predictor - diff;
608 else
609 3468031 predictor = c->predictor + diff;
610
611 6911962 c->predictor = av_clip_int16(predictor);
612 6911962 c->step_index = step_index;
613
614 6911962 return c->predictor;
615 }
616
617 ✗ static void decode_adpcm_ima_hvqm2(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do,
618 int frame_format, GetByteContext *gb)
619 {
620 ✗ ADPCMDecodeContext *c = avctx->priv_data;
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
654 ✗ static void decode_adpcm_ima_hvqm4(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do,
655 int frame_format, GetByteContext *gb)
656 {
657 ✗ ADPCMDecodeContext *c = avctx->priv_data;
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
697 2230446 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
698 {
699 int predictor;
700
701 2230446 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
702
2/2
✓ Branch 0 taken 972236 times.
✓ Branch 1 taken 1258210 times.
2230446 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
703
704 2230446 c->sample2 = c->sample1;
705 2230446 c->sample1 = av_clip_int16(predictor);
706 2230446 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
707
2/2
✓ Branch 0 taken 92703 times.
✓ Branch 1 taken 2137743 times.
2230446 if (c->idelta < 16) c->idelta = 16;
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2230446 times.
2230446 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 2230446 return c->sample1;
714 }
715
716 55124 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
717 {
718 int step_index, predictor, sign, delta, diff, step;
719
720 55124 step = oki_step_table[c->step_index];
721 55124 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
722 55124 step_index = av_clip(step_index, 0, 48);
723
724 55124 sign = nibble & 8;
725 55124 delta = nibble & 7;
726 55124 diff = ((2 * delta + 1) * step) >> 3;
727 55124 predictor = c->predictor;
728
2/2
✓ Branch 0 taken 25352 times.
✓ Branch 1 taken 29772 times.
55124 if (sign) predictor -= diff;
729 29772 else predictor += diff;
730
731 55124 c->predictor = av_clip_intp2(predictor, 11);
732 55124 c->step_index = step_index;
733
734 55124 return c->predictor * 16;
735 }
736
737 524192 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
738 {
739 int sign, delta, diff;
740 int new_step;
741
742 524192 sign = nibble & 8;
743 524192 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 524192 diff = ((2 * delta + 1) * c->step) >> 3;
748 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
749
2/2
✓ Branch 0 taken 263253 times.
✓ Branch 1 taken 260939 times.
524192 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
750 524192 c->predictor = av_clip_int16(c->predictor);
751 /* calculate new step and clamp it to range 511..32767 */
752 524192 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
753 524192 c->step = av_clip(new_step, 511, 32767);
754
755 524192 return (int16_t)c->predictor;
756 }
757
758 20480 static 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 20480 delta = nibble & 7;
764 20480 add = rhetorex_step[((c->step_index >> 8) & 0xF8) + delta];
765
2/2
✓ Branch 0 taken 10650 times.
✓ Branch 1 taken 9830 times.
20480 if (nibble & 0x8)
766 10650 add = -add;
767
768 20480 sample = av_clip_int16(c->predictor + add);
769
770 20480 a = 32768 * c->coeff1 + (add >> 1) * c->sample2;
771 20480 c->coeff1 = av_clip_int16((a - ((a >> 7) & ~0xFF)) >> 15);
772
773 20480 b = 32768 * c->coeff2 + (add >> 1) * c->sample1;
774 20480 c->coeff2 = av_clip_int16((b - ((b >> 7) & ~0xFF) + 0x800000) >> 15);
775
776 20480 c->predictor = av_clip_int16((c->coeff1 * c->sample1 + c->coeff2 * sample) >> 15);
777
778 20480 c->sample2 = c->sample1;
779 20480 c->sample1 = sample;
780
781 20480 c->step_index = FFABS((int)(64512U * c->step_index + 65536U * rhetorex_index[delta])) >> 16;
782
783 20480 return sample;
784 }
785
786 313380 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
787 {
788 int sign, delta, diff;
789
790 313380 sign = nibble & (1<<(size-1));
791 313380 delta = nibble & ((1<<(size-1))-1);
792 313380 diff = delta << (7 + c->step + shift);
793
794 /* clamp result */
795
2/2
✓ Branch 0 taken 129904 times.
✓ Branch 1 taken 183476 times.
313380 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
796
797 /* calculate new step */
798
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)
799 75080 c->step++;
800
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)
801 75080 c->step--;
802
803 313380 return (int16_t) c->predictor;
804 }
805
806 1105920 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
807 {
808
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1105915 times.
1105920 if(!c->step) {
809 5 c->predictor = 0;
810 5 c->step = 127;
811 }
812
813 1105920 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
814 1105920 c->predictor = av_clip_int16(c->predictor);
815 1105920 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
816 1105920 c->step = av_clip(c->step, 127, 24576);
817 1105920 return c->predictor;
818 }
819
820 ✗ static 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
829 ✗ static 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);
842 ✗ sample = av_clip_int16(sample);
843
844 ✗ c->predictor = sample;
845 ✗ c->step = scale;
846
847 ✗ return sample;
848 }
849
850 ✗ static 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;
874 ✗ sample = av_clip_int16(sample);
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
885 666 static 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 666 out0 += sample_offset;
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (channels == 1)
896 ✗ out1 = out0 + 28;
897 else
898 666 out1 += sample_offset;
899
900
2/2
✓ Branch 0 taken 2664 times.
✓ Branch 1 taken 666 times.
3330 for(i=0;i<4;i++) {
901 2664 shift = 12 - (in[4+i*2] & 15);
902 2664 filter = in[4+i*2] >> 4;
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
904 ✗ avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
905 ✗ filter=0;
906 }
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (shift < 0) {
908 ✗ avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
909 ✗ shift = 0;
910 }
911 2664 f0 = xa_adpcm_table[filter][0];
912 2664 f1 = xa_adpcm_table[filter][1];
913
914 2664 s_1 = left->sample1;
915 2664 s_2 = left->sample2;
916
917
2/2
✓ Branch 0 taken 74592 times.
✓ Branch 1 taken 2664 times.
77256 for(j=0;j<28;j++) {
918 74592 d = in[16+i+j*4];
919
920 74592 t = sign_extend(d, 4);
921 74592 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
922 74592 s_2 = s_1;
923 74592 s_1 = av_clip_int16(s);
924 74592 out0[j] = s_1;
925 }
926
927
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 if (channels == 2) {
928 2664 left->sample1 = s_1;
929 2664 left->sample2 = s_2;
930 2664 s_1 = right->sample1;
931 2664 s_2 = right->sample2;
932 }
933
934 2664 shift = 12 - (in[5+i*2] & 15);
935 2664 filter = in[5+i*2] >> 4;
936
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) {
937 ✗ avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
938 ✗ filter=0;
939 }
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 if (shift < 0) {
941 ✗ avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
942 ✗ shift = 0;
943 }
944
945 2664 f0 = xa_adpcm_table[filter][0];
946 2664 f1 = xa_adpcm_table[filter][1];
947
948
2/2
✓ Branch 0 taken 74592 times.
✓ Branch 1 taken 2664 times.
77256 for(j=0;j<28;j++) {
949 74592 d = in[16+i+j*4];
950
951 74592 t = sign_extend(d >> 4, 4);
952 74592 s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
953 74592 s_2 = s_1;
954 74592 s_1 = av_clip_int16(s);
955 74592 out1[j] = s_1;
956 }
957
958
1/2
✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
2664 if (channels == 2) {
959 2664 right->sample1 = s_1;
960 2664 right->sample2 = s_2;
961 } else {
962 ✗ left->sample1 = s_1;
963 ✗ left->sample2 = s_2;
964 }
965
966 2664 out0 += 28 * (3 - channels);
967 2664 out1 += 28 * (3 - channels);
968 }
969
970 666 return 0;
971 }
972
973 195 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
974 {
975 195 ADPCMDecodeContext *c = avctx->priv_data;
976 GetBitContext gb;
977 const int8_t *table;
978 195 int channels = avctx->ch_layout.nb_channels;
979 int k0, signmask, nb_bits, count;
980 195 int size = buf_size*8;
981 int i;
982
983 195 init_get_bits(&gb, buf, size);
984
985 //read bits & initial values
986 195 nb_bits = get_bits(&gb, 2)+2;
987 195 table = swf_index_tables[nb_bits-2];
988 195 k0 = 1 << (nb_bits-2);
989 195 signmask = 1 << (nb_bits-1);
990
991
2/2
✓ Branch 1 taken 195 times.
✓ Branch 2 taken 195 times.
390 while (get_bits_count(&gb) <= size - 22 * channels) {
992
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 195 times.
585 for (i = 0; i < channels; i++) {
993 390 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
994 390 c->status[i].step_index = get_bits(&gb, 6);
995 }
996
997
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++) {
998 int i;
999
1000
2/2
✓ Branch 0 taken 1597050 times.
✓ Branch 1 taken 798525 times.
2395575 for (i = 0; i < channels; i++) {
1001 // similar to IMA adpcm
1002 1597050 int delta = get_bits(&gb, nb_bits);
1003 1597050 int step = ff_adpcm_step_table[c->status[i].step_index];
1004 1597050 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1005 1597050 int k = k0;
1006
1007 do {
1008
2/2
✓ Branch 0 taken 1979561 times.
✓ Branch 1 taken 2811589 times.
4791150 if (delta & k)
1009 1979561 vpdiff += step;
1010 4791150 step >>= 1;
1011 4791150 k >>= 1;
1012
2/2
✓ Branch 0 taken 3194100 times.
✓ Branch 1 taken 1597050 times.
4791150 } while(k);
1013 1597050 vpdiff += step;
1014
1015
2/2
✓ Branch 0 taken 803527 times.
✓ Branch 1 taken 793523 times.
1597050 if (delta & signmask)
1016 803527 c->status[i].predictor -= vpdiff;
1017 else
1018 793523 c->status[i].predictor += vpdiff;
1019
1020 1597050 c->status[i].step_index += table[delta & (~signmask)];
1021
1022 1597050 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1023 1597050 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1024
1025 1597050 *samples++ = c->status[i].predictor;
1026 }
1027 }
1028 }
1029 195 }
1030
1031 18335520 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
1032 {
1033 18335520 int sample = sign_extend(nibble, 4) * (1 << shift);
1034
1035
2/2
✓ Branch 0 taken 9806208 times.
✓ Branch 1 taken 8529312 times.
18335520 if (flag)
1036 9806208 sample += (8 * cs->sample1) - (4 * cs->sample2);
1037 else
1038 8529312 sample += 4 * cs->sample1;
1039
1040 18335520 sample = av_clip_int16(sample >> 2);
1041
1042 18335520 cs->sample2 = cs->sample1;
1043 18335520 cs->sample1 = sample;
1044
1045 18335520 return sample;
1046 }
1047
1048 20832 static int adpcm_sanyo_expand3(ADPCMChannelStatus *c, int bits)
1049 {
1050 int sign, delta, add;
1051
1052 20832 sign = bits & 4;
1053
2/2
✓ Branch 0 taken 7806 times.
✓ Branch 1 taken 13026 times.
20832 if (sign)
1054 7806 delta = 4 - (bits & 3);
1055 else
1056 13026 delta = bits;
1057
1058
5/6
✓ Branch 0 taken 5975 times.
✓ Branch 1 taken 8612 times.
✓ Branch 2 taken 3893 times.
✓ Branch 3 taken 2199 times.
✓ Branch 4 taken 153 times.
✗ Branch 5 not taken.
20832 switch (delta) {
1059 5975 case 0:
1060 5975 add = 0;
1061 5975 c->step = (3 * c->step) >> 2;
1062 5975 break;
1063 8612 case 1:
1064 8612 add = c->step;
1065 8612 c->step = (4 * c->step - (c->step >> 1)) >> 2;
1066 8612 break;
1067 3893 case 2:
1068 3893 add = 2 * c->step;
1069 3893 c->step = ((c->step >> 1) + add) >> 1;
1070 3893 break;
1071 2199 case 3:
1072 2199 add = 4 * c->step - (c->step >> 1);
1073 2199 c->step = 2 * c->step;
1074 2199 break;
1075 153 case 4:
1076 153 add = (11 * c->step) >> 1;
1077 153 c->step = 3 * c->step;
1078 153 break;
1079 ✗ default:
1080 ✗ av_unreachable("There are cases for all control paths when bits is 3-bit");
1081 }
1082
1083
2/2
✓ Branch 0 taken 7806 times.
✓ Branch 1 taken 13026 times.
20832 if (sign)
1084 7806 add = -add;
1085
1086 20832 c->predictor = av_clip_int16(c->predictor + add);
1087 20832 c->step = av_clip(c->step, 1, 7281);
1088 20832 return c->predictor;
1089 }
1090
1091 20664 static int adpcm_sanyo_expand4(ADPCMChannelStatus *c, int bits)
1092 {
1093 int sign, delta, add;
1094
1095 20664 sign = bits & 8;
1096
2/2
✓ Branch 0 taken 9081 times.
✓ Branch 1 taken 11583 times.
20664 if (sign)
1097 9081 delta = 8 - (bits & 7);
1098 else
1099 11583 delta = bits;
1100
1101
9/10
✓ Branch 0 taken 3407 times.
✓ Branch 1 taken 4750 times.
✓ Branch 2 taken 3536 times.
✓ Branch 3 taken 2837 times.
✓ Branch 4 taken 2269 times.
✓ Branch 5 taken 2264 times.
✓ Branch 6 taken 879 times.
✓ Branch 7 taken 605 times.
✓ Branch 8 taken 117 times.
✗ Branch 9 not taken.
20664 switch (delta) {
1102 3407 case 0:
1103 3407 add = 0;
1104 3407 c->step = (3 * c->step) >> 2;
1105 3407 break;
1106 4750 case 1:
1107 4750 add = c->step;
1108 4750 c->step = (3 * c->step) >> 2;
1109 4750 break;
1110 3536 case 2:
1111 3536 add = 2 * c->step;
1112 3536 break;
1113 2837 case 3:
1114 2837 add = 3 * c->step;
1115 2837 break;
1116 2269 case 4:
1117 2269 add = 4 * c->step;
1118 2269 break;
1119 2264 case 5:
1120 2264 add = (11 * c->step) >> 1;
1121 2264 c->step += c->step >> 2;
1122 2264 break;
1123 879 case 6:
1124 879 add = (15 * c->step) >> 1;
1125 879 c->step = 2 * c->step;
1126 879 break;
1127 605 case 7:
1128
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 436 times.
605 if (sign)
1129 169 add = (19 * c->step) >> 1;
1130 else
1131 436 add = (21 * c->step) >> 1;
1132 605 c->step = (c->step >> 1) + 2 * c->step;
1133 605 break;
1134 117 case 8:
1135 117 add = (25 * c->step) >> 1;
1136 117 c->step = 5 * c->step;
1137 117 break;
1138 ✗ default:
1139 ✗ av_unreachable("There are cases for all control paths when bits is 4-bit");
1140 }
1141
1142
2/2
✓ Branch 0 taken 9081 times.
✓ Branch 1 taken 11583 times.
20664 if (sign)
1143 9081 add = -add;
1144
1145 20664 c->predictor = av_clip_int16(c->predictor + add);
1146 20664 c->step = av_clip(c->step, 1, 2621);
1147 20664 return c->predictor;
1148 }
1149
1150 20956 static int adpcm_sanyo_expand5(ADPCMChannelStatus *c, int bits)
1151 {
1152 int sign, delta, add;
1153
1154 20956 sign = bits & 0x10;
1155
2/2
✓ Branch 0 taken 9481 times.
✓ Branch 1 taken 11475 times.
20956 if (sign)
1156 9481 delta = 16 - (bits & 0xF);
1157 else
1158 11475 delta = bits;
1159
1160 20956 add = delta * c->step;
1161
13/14
✓ Branch 0 taken 2894 times.
✓ Branch 1 taken 8615 times.
✓ Branch 2 taken 3930 times.
✓ Branch 3 taken 1463 times.
✓ Branch 4 taken 1105 times.
✓ Branch 5 taken 747 times.
✓ Branch 6 taken 516 times.
✓ Branch 7 taken 397 times.
✓ Branch 8 taken 309 times.
✓ Branch 9 taken 215 times.
✓ Branch 10 taken 175 times.
✓ Branch 11 taken 125 times.
✓ Branch 12 taken 465 times.
✗ Branch 13 not taken.
20956 switch (delta) {
1162 2894 case 0:
1163 2894 c->step += (c->step >> 2) - (c->step >> 1);
1164 2894 break;
1165 8615 case 1:
1166 case 2:
1167 case 3:
1168 8615 c->step += (c->step >> 3) - (c->step >> 2);
1169 8615 break;
1170 3930 case 4:
1171 case 5:
1172 3930 c->step += (c->step >> 4) - (c->step >> 3);
1173 3930 break;
1174 1463 case 6:
1175 1463 break;
1176 1105 case 7:
1177 1105 c->step += c->step >> 3;
1178 1105 break;
1179 747 case 8:
1180 747 c->step += c->step >> 2;
1181 747 break;
1182 516 case 9:
1183 516 c->step += c->step >> 1;
1184 516 break;
1185 397 case 10:
1186 397 c->step = 2 * c->step - (c->step >> 3);
1187 397 break;
1188 309 case 11:
1189 309 c->step = 2 * c->step + (c->step >> 3);
1190 309 break;
1191 215 case 12:
1192 215 c->step = 2 * c->step + (c->step >> 1) - (c->step >> 3);
1193 215 break;
1194 175 case 13:
1195 175 c->step = 3 * c->step - (c->step >> 2);
1196 175 break;
1197 125 case 14:
1198 125 c->step *= 3;
1199 125 break;
1200 465 case 15:
1201 case 16:
1202 465 c->step = (7 * c->step) >> 1;
1203 465 break;
1204 }
1205
1206
2/2
✓ Branch 0 taken 9481 times.
✓ Branch 1 taken 11475 times.
20956 if (sign)
1207 9481 add = -add;
1208
1209 20956 c->predictor = av_clip_int16(c->predictor + add);
1210 20956 c->step = av_clip(c->step, 1, 1024);
1211 20956 return c->predictor;
1212 }
1213
1214 424576 static int adpcm_citrix_expand(ADPCMChannelStatus *c, int bits)
1215 {
1216 424576 int step = ff_adpcm_step_table[c->step_index];
1217 424576 int add = step >> 1;
1218
2/2
✓ Branch 0 taken 124950 times.
✓ Branch 1 taken 299626 times.
424576 if ((bits & 1))
1219 124950 add += step;
1220
2/2
✓ Branch 0 taken 212006 times.
✓ Branch 1 taken 212570 times.
424576 if ((bits & 2))
1221 212006 add = -add;
1222 424576 c->step_index = av_clip(c->step_index + citrix_index_table[bits], 0, 88);
1223 424576 c->predictor = av_clip_int16(c->predictor + add);
1224 424576 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 */
1238 38299 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
1239 int buf_size, int *coded_samples, int *approx_nb_samples)
1240 {
1241 38299 ADPCMDecodeContext *s = avctx->priv_data;
1242 38299 int nb_samples = 0;
1243 38299 int ch = avctx->ch_layout.nb_channels;
1244 38299 int has_coded_samples = 0;
1245 int header_size;
1246
1247 38299 *coded_samples = 0;
1248 38299 *approx_nb_samples = 0;
1249
1250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38299 times.
38299 if(ch <= 0)
1251 ✗ return 0;
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38299 times.
38299 if (buf_size > INT_MAX / 14)
1253 ✗ return 0;
1254
1255
4/5
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 26342 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1195 times.
✓ Branch 4 taken 10162 times.
38299 switch (avctx->codec->id) {
1256 /* constant, only check buf_size */
1257 600 case AV_CODEC_ID_ADPCM_EA_XAS:
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (buf_size < 76 * ch)
1259 ✗ return 0;
1260 600 nb_samples = 128;
1261 600 break;
1262 26342 case AV_CODEC_ID_ADPCM_IMA_QT:
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26342 times.
26342 if (buf_size < 34 * ch)
1264 ✗ return 0;
1265 26342 nb_samples = 64;
1266 26342 break;
1267 ✗ case AV_CODEC_ID_ADPCM_N64:
1268 ✗ nb_samples = (buf_size / 9) * 16;
1269 ✗ break;
1270 /* simple 4-bit adpcm */
1271 1195 case AV_CODEC_ID_ADPCM_CT:
1272 case AV_CODEC_ID_ADPCM_IMA_APC:
1273 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1274 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1275 case AV_CODEC_ID_ADPCM_IMA_ESCAPE:
1276 case AV_CODEC_ID_ADPCM_IMA_OKI:
1277 case AV_CODEC_ID_ADPCM_IMA_WS:
1278 case AV_CODEC_ID_ADPCM_YAMAHA:
1279 case AV_CODEC_ID_ADPCM_AICA:
1280 case AV_CODEC_ID_ADPCM_IMA_SSI:
1281 case AV_CODEC_ID_ADPCM_IMA_APM:
1282 case AV_CODEC_ID_ADPCM_IMA_ALP:
1283 case AV_CODEC_ID_ADPCM_IMA_MTF:
1284 1195 nb_samples = buf_size * 2 / ch;
1285 1195 break;
1286 }
1287
2/2
✓ Branch 0 taken 28137 times.
✓ Branch 1 taken 10162 times.
38299 if (nb_samples)
1288 28137 return nb_samples;
1289
1290 /* simple 4-bit adpcm, with header */
1291 10162 header_size = 0;
1292
3/3
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 350 times.
✓ Branch 2 taken 9667 times.
10162 switch (avctx->codec->id) {
1293 145 case AV_CODEC_ID_ADPCM_4XM:
1294 case AV_CODEC_ID_ADPCM_AGM:
1295 case AV_CODEC_ID_ADPCM_IMA_ACORN:
1296 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1297 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
1298 145 case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
1299 350 case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
1300 }
1301
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 9667 times.
10162 if (header_size > 0)
1302 495 return (buf_size - header_size) * 2 / ch;
1303
1304 /* more complex formats */
1305
20/30
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 47 times.
✓ Branch 5 taken 34 times.
✓ Branch 6 taken 400 times.
✓ Branch 7 taken 642 times.
✓ Branch 8 taken 649 times.
✓ Branch 9 taken 1000 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 1668 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2944 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 57 times.
✓ Branch 17 taken 195 times.
✓ Branch 18 taken 71 times.
✓ Branch 19 taken 12 times.
✓ Branch 20 taken 37 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 32 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 40 times.
✓ Branch 25 taken 946 times.
✗ Branch 26 not taken.
✓ Branch 27 taken 124 times.
✓ Branch 28 taken 449 times.
✗ Branch 29 not taken.
9667 switch (avctx->codec->id) {
1306 162 case AV_CODEC_ID_ADPCM_IMA_AMV:
1307 162 bytestream2_skip(gb, 4);
1308 162 has_coded_samples = 1;
1309 162 *coded_samples = bytestream2_get_le32u(gb);
1310 162 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
1311 162 bytestream2_seek(gb, -8, SEEK_CUR);
1312 162 break;
1313 158 case AV_CODEC_ID_ADPCM_EA:
1314 /* Stereo is 30 bytes per block */
1315 /* Mono is 15 bytes per block */
1316 158 has_coded_samples = 1;
1317 158 *coded_samples = bytestream2_get_le32(gb);
1318 158 *coded_samples -= *coded_samples % 28;
1319
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
1320 158 break;
1321 ✗ case AV_CODEC_ID_ADPCM_IMA_HVQM2:
1322 ✗ nb_samples = ((bytestream2_peek_be64(gb) >> 16) & 0xFFFF);
1323 ✗ break;
1324 ✗ case AV_CODEC_ID_ADPCM_IMA_HVQM4:
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;
1338 47 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1339 47 has_coded_samples = 1;
1340 47 *coded_samples = bytestream2_get_le32(gb);
1341 47 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
1342 47 break;
1343 34 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1344 34 nb_samples = (buf_size - ch) / ch * 2;
1345 34 break;
1346 400 case AV_CODEC_ID_ADPCM_EA_R1:
1347 case AV_CODEC_ID_ADPCM_EA_R2:
1348 case AV_CODEC_ID_ADPCM_EA_R3:
1349 /* maximum number of samples */
1350 /* has internal offsets and a per-frame switch to signal raw 16-bit */
1351 400 has_coded_samples = 1;
1352
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) {
1353 95 case AV_CODEC_ID_ADPCM_EA_R1:
1354 95 header_size = 4 + 9 * ch;
1355 95 *coded_samples = bytestream2_get_le32(gb);
1356 95 break;
1357 180 case AV_CODEC_ID_ADPCM_EA_R2:
1358 180 header_size = 4 + 5 * ch;
1359 180 *coded_samples = bytestream2_get_le32(gb);
1360 180 break;
1361 125 case AV_CODEC_ID_ADPCM_EA_R3:
1362 125 header_size = 4 + 5 * ch;
1363 125 *coded_samples = bytestream2_get_be32(gb);
1364 125 break;
1365 }
1366 400 *coded_samples -= *coded_samples % 28;
1367 400 nb_samples = (buf_size - header_size) * 2 / ch;
1368 400 nb_samples -= nb_samples % 28;
1369 400 *approx_nb_samples = 1;
1370 400 break;
1371 642 case AV_CODEC_ID_ADPCM_IMA_DK3:
1372
1/2
✓ Branch 0 taken 642 times.
✗ Branch 1 not taken.
642 if (avctx->block_align > 0)
1373 642 buf_size = FFMIN(buf_size, avctx->block_align);
1374 642 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
1375 642 break;
1376 649 case AV_CODEC_ID_ADPCM_IMA_DK4:
1377
1/2
✓ Branch 0 taken 649 times.
✗ Branch 1 not taken.
649 if (avctx->block_align > 0)
1378 649 buf_size = FFMIN(buf_size, avctx->block_align);
1379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 649 times.
649 if (buf_size < 4 * ch)
1380 ✗ return AVERROR_INVALIDDATA;
1381 649 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
1382 649 break;
1383 1000 case AV_CODEC_ID_ADPCM_IMA_RAD:
1384
1/2
✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
1000 if (avctx->block_align > 0)
1385 1000 buf_size = FFMIN(buf_size, avctx->block_align);
1386 1000 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1387 1000 break;
1388 ✗ case AV_CODEC_ID_ADPCM_IMA_PDA:
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;
1393 ✗ case AV_CODEC_ID_ADPCM_IMA_MAGIX:
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
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,
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 */
1420 2944 case AV_CODEC_ID_ADPCM_MS:
1421
1/2
✓ Branch 0 taken 2944 times.
✗ Branch 1 not taken.
2944 if (avctx->block_align > 0)
1422 2944 buf_size = FFMIN(buf_size, avctx->block_align);
1423 2944 nb_samples = (buf_size - 6 * ch) * 2 / ch;
1424 2944 break;
1425 ✗ case AV_CODEC_ID_ADPCM_MTAF:
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;
1430 57 case AV_CODEC_ID_ADPCM_SBPRO_2:
1431 case AV_CODEC_ID_ADPCM_SBPRO_3:
1432 case AV_CODEC_ID_ADPCM_SBPRO_4:
1433 {
1434 int samples_per_byte;
1435
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) {
1436 13 case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
1437 18 case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
1438 26 case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
1439 }
1440
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 54 times.
57 if (!s->status[0].step_index) {
1441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (buf_size < ch)
1442 ✗ return AVERROR_INVALIDDATA;
1443 3 nb_samples++;
1444 3 buf_size -= ch;
1445 }
1446 57 nb_samples += buf_size * samples_per_byte / ch;
1447 57 break;
1448 }
1449 195 case AV_CODEC_ID_ADPCM_SWF:
1450 {
1451 195 int buf_bits = buf_size * 8 - 2;
1452 195 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1453 195 int block_hdr_size = 22 * ch;
1454 195 int block_size = block_hdr_size + nbits * ch * 4095;
1455 195 int nblocks = buf_bits / block_size;
1456 195 int bits_left = buf_bits - nblocks * block_size;
1457 195 nb_samples = nblocks * 4096;
1458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (bits_left >= block_hdr_size)
1459 ✗ nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
1460 195 break;
1461 }
1462 71 case AV_CODEC_ID_ADPCM_THP:
1463 case AV_CODEC_ID_ADPCM_THP_LE:
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 if (avctx->extradata) {
1465 ✗ nb_samples = buf_size * 14 / (8 * ch);
1466 ✗ break;
1467 }
1468 71 has_coded_samples = 1;
1469 71 bytestream2_skip(gb, 4); // channel size
1470 142 *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
1471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
142 bytestream2_get_le32(gb) :
1472 71 bytestream2_get_be32(gb);
1473 71 buf_size -= 8 + 36 * ch;
1474 71 buf_size /= ch;
1475 71 nb_samples = buf_size / 8 * 14;
1476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 if (buf_size % 8 > 1)
1477 ✗ nb_samples += (buf_size % 8 - 1) * 2;
1478 71 *approx_nb_samples = 1;
1479 71 break;
1480 12 case AV_CODEC_ID_ADPCM_AFC:
1481 12 nb_samples = buf_size / (9 * ch) * 16;
1482 12 break;
1483 37 case AV_CODEC_ID_ADPCM_XA:
1484 37 nb_samples = (buf_size / 128) * 224 / ch;
1485 37 break;
1486 ✗ case AV_CODEC_ID_ADPCM_XMD:
1487 ✗ nb_samples = buf_size / (21 * ch) * 32;
1488 ✗ break;
1489 32 case AV_CODEC_ID_ADPCM_DTK:
1490 case AV_CODEC_ID_ADPCM_PSX:
1491 32 nb_samples = buf_size / (16 * ch) * 28;
1492 32 break;
1493 ✗ case AV_CODEC_ID_ADPCM_PSXC:
1494 ✗ nb_samples = ((buf_size - 1) / ch) * 2;
1495 ✗ break;
1496 40 case AV_CODEC_ID_ADPCM_RHETOREX:
1497 40 nb_samples = buf_size * 2;
1498 40 break;
1499 946 case AV_CODEC_ID_ADPCM_ARGO:
1500 946 nb_samples = buf_size / avctx->block_align * 32;
1501 946 break;
1502 ✗ case AV_CODEC_ID_ADPCM_CIRCUS:
1503 case AV_CODEC_ID_ADPCM_ZORK:
1504 ✗ nb_samples = buf_size / ch;
1505 ✗ break;
1506 124 case AV_CODEC_ID_ADPCM_SANYO:
1507
2/4
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 124 times.
124 if (!avctx->extradata || avctx->extradata_size != 2)
1508 ✗ return AVERROR_INVALIDDATA;
1509 124 nb_samples = AV_RL16(avctx->extradata);
1510 124 break;
1511 449 case AV_CODEC_ID_ADPCM_IMA_CITRIX:
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449 times.
449 if (buf_size < avctx->block_align)
1513 ✗ return 0;
1514 449 nb_samples = (buf_size / avctx->block_align) * (1 + ((avctx->block_align / ch) - 4) * 4);
1515 449 break;
1516 }
1517
1518 /* validate coded sample count */
1519
4/6
✓ Branch 0 taken 838 times.
✓ Branch 1 taken 8829 times.
✓ Branch 2 taken 838 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 838 times.
9667 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1520 ✗ return AVERROR_INVALIDDATA;
1521
1522 9667 return nb_samples;
1523 }
1524
1525 38299 static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1526 int *got_frame_ptr, AVPacket *avpkt)
1527 {
1528 38299 const uint8_t *buf = avpkt->data;
1529 38299 int buf_size = avpkt->size;
1530 38299 ADPCMDecodeContext *c = avctx->priv_data;
1531 38299 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 38299 bytestream2_init(&gb, buf, buf_size);
1539 38299 nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38299 times.
38299 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 38299 frame->nb_samples = nb_samples;
1547
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38299 times.
38299 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1548 ✗ return ret;
1549 38299 samples = (int16_t *)frame->data[0];
1550 38299 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
2/2
✓ Branch 0 taken 838 times.
✓ Branch 1 taken 37461 times.
38299 if (coded_samples) {
1555
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)
1556 ✗ av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
1557 838 frame->nb_samples = nb_samples = coded_samples;
1558 }
1559
1560 38299 st = channels == 2 ? 1 : 0;
1561
1562
35/55
✓ Branch 0 taken 26342 times.
✓ Branch 1 taken 1668 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2944 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 649 times.
✓ Branch 8 taken 642 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 119 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 179 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 127 times.
✓ Branch 17 taken 87 times.
✓ Branch 18 taken 85 times.
✓ Branch 19 taken 39 times.
✓ Branch 20 taken 54 times.
✓ Branch 21 taken 1000 times.
✓ Branch 22 taken 311 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 37 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 47 times.
✓ Branch 27 taken 49 times.
✓ Branch 28 taken 158 times.
✓ Branch 29 taken 34 times.
✓ Branch 30 taken 400 times.
✓ Branch 31 taken 600 times.
✗ Branch 32 not taken.
✓ Branch 33 taken 162 times.
✗ Branch 34 not taken.
✓ Branch 35 taken 350 times.
✓ Branch 36 taken 128 times.
✓ Branch 37 taken 57 times.
✓ Branch 38 taken 195 times.
✓ Branch 39 taken 136 times.
✗ Branch 40 not taken.
✓ Branch 41 taken 12 times.
✓ Branch 42 taken 71 times.
✓ Branch 43 taken 32 times.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✓ Branch 47 taken 124 times.
✓ Branch 48 taken 40 times.
✓ Branch 49 taken 946 times.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✓ Branch 53 taken 449 times.
✗ Branch 54 not taken.
38299 switch(avctx->codec->id) {
1563
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,
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
10/22
✗ Branch 3 not taken.
✓ Branch 4 taken 3336 times.
✓ Branch 6 taken 3336 times.
✓ Branch 7 taken 1668 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1668 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 26 taken 1107936 times.
✓ Branch 27 taken 276984 times.
✓ Branch 28 taken 276984 times.
✓ Branch 29 taken 138492 times.
✓ Branch 30 taken 138492 times.
✓ Branch 31 taken 1668 times.
1528416 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 };
1622 GetBitContext g;
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
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,
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
17/26
✗ Branch 0 not taken.
✓ Branch 1 taken 2944 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 2944 times.
✓ Branch 20 taken 2895 times.
✓ Branch 21 taken 49 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2895 times.
✓ Branch 27 taken 2895 times.
✓ Branch 28 taken 49 times.
✓ Branch 31 taken 2895 times.
✓ Branch 32 taken 49 times.
✓ Branch 35 taken 2895 times.
✓ Branch 36 taken 49 times.
✓ Branch 38 taken 2895 times.
✓ Branch 39 taken 49 times.
✓ Branch 40 taken 2895 times.
✓ Branch 41 taken 49 times.
✓ Branch 45 taken 1115223 times.
✓ Branch 46 taken 2944 times.
1118167 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
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,
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
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,
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 */
1857 DK3_GET_NEXT_NIBBLE();
1858 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1859
1860 /* process the diff channel predictor */
1861 DK3_GET_NEXT_NIBBLE();
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 */
1870 DK3_GET_NEXT_NIBBLE();
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
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,
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
2/2
✓ Branch 3 taken 732060 times.
✓ Branch 4 taken 179 times.
732239 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
2/2
✓ Branch 3 taken 514600 times.
✓ Branch 4 taken 127 times.
514727 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
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,
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
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,
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
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,
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
2/2
✓ Branch 3 taken 27562 times.
✓ Branch 4 taken 54 times.
27616 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
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,
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
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,
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
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,
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
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,
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
2/2
✓ Branch 3 taken 71392 times.
✓ Branch 4 taken 49 times.
71441 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
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,
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
11/12
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 34 times.
✓ Branch 6 taken 476 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1904 times.
✓ Branch 10 taken 952 times.
✓ Branch 11 taken 952 times.
✓ Branch 12 taken 476 times.
✓ Branch 13 taken 476 times.
✓ Branch 14 taken 34 times.
3570 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
2273 400 case AV_CODEC_ID_ADPCM_EA_R1:
2274 case AV_CODEC_ID_ADPCM_EA_R2:
2275 case AV_CODEC_ID_ADPCM_EA_R3: {
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 400 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 400 int count = 0;
2286 int offsets[6];
2287
2288
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
1200 for (unsigned channel = 0; channel < channels; channel++)
2289
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 550 times.
800 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
2290 550 bytestream2_get_le32(&gb)) +
2291 800 (channels + 1) * 4;
2292
2293
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
1200 for (unsigned channel = 0; channel < channels; channel++) {
2294 int count1;
2295
2296 800 bytestream2_seek(&gb, offsets[channel], SEEK_SET);
2297 800 samplesC = samples_p[channel];
2298
2299
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 610 times.
800 if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
2300 190 current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2301 190 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2302 } else {
2303 610 current_sample = c->status[channel].predictor;
2304 610 previous_sample = c->status[channel].prev_sample;
2305 }
2306
2307
2/2
✓ Branch 0 taken 41192 times.
✓ Branch 1 taken 800 times.
41992 for (count1 = 0; count1 < nb_samples / 28; count1++) {
2308 41192 int byte = bytestream2_get_byte(&gb);
2309
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 41180 times.
41192 if (byte == 0xEE) { /* only seen in R2 and R3 */
2310 12 current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2311 12 previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2312
2313
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 12 times.
348 for (int count2 = 0; count2 < 28; count2++)
2314 336 *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
2315 } else {
2316 41180 coeff1 = ea_adpcm_table[ byte >> 4 ];
2317 41180 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
2318 41180 shift = 20 - (byte & 0x0F);
2319
2320
2/2
✓ Branch 0 taken 1153040 times.
✓ Branch 1 taken 41180 times.
1194220 for (int count2 = 0; count2 < 28; count2++) {
2321
2/2
✓ Branch 0 taken 576520 times.
✓ Branch 1 taken 576520 times.
1153040 if (count2 & 1)
2322 576520 next_sample = (unsigned)sign_extend(byte, 4) << shift;
2323 else {
2324 576520 byte = bytestream2_get_byte(&gb);
2325 576520 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
2326 }
2327
2328 1153040 next_sample += (current_sample * coeff1) +
2329 1153040 (previous_sample * coeff2);
2330 1153040 next_sample = av_clip_int16(next_sample >> 8);
2331
2332 1153040 previous_sample = current_sample;
2333 1153040 current_sample = next_sample;
2334 1153040 *samplesC++ = current_sample;
2335 }
2336 }
2337 }
2338
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 400 times.
800 if (!count) {
2339 400 count = count1;
2340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 } 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
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 190 times.
800 if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
2346 610 c->status[channel].predictor = current_sample;
2347 610 c->status[channel].prev_sample = previous_sample;
2348 }
2349 }
2350
2351 400 frame->nb_samples = count * 28;
2352 400 bytestream2_seek(&gb, 0, SEEK_END);
2353 400 break;
2354 }
2355 #endif /* CONFIG_ADPCM_EA_Rx_DECODER */
2356
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,
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
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,
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
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,
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
2/2
✓ Branch 3 taken 262096 times.
✓ Branch 4 taken 128 times.
262224 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
2492 57 case AV_CODEC_ID_ADPCM_SBPRO_4:
2493 case AV_CODEC_ID_ADPCM_SBPRO_3:
2494 case AV_CODEC_ID_ADPCM_SBPRO_2:
2495
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 54 times.
57 if (!c->status[0].step_index) {
2496 /* the first byte is a raw sample */
2497 3 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st)
2499 ✗ *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2500 3 c->status[0].step_index = 1;
2501 3 nb_samples--;
2502 }
2503
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 31 times.
57 if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
2504
2/2
✓ Branch 0 taken 52230 times.
✓ Branch 1 taken 26 times.
52256 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2505 52230 int byte = bytestream2_get_byteu(&gb);
2506 104460 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2507 52230 byte >> 4, 4, 0);
2508 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2509 52230 byte & 0x0F, 4, 0);
2510 }
2511
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
2512
2/2
✓ Branch 0 taken 34820 times.
✓ Branch 1 taken 18 times.
34838 for (int n = (nb_samples<<st) / 3; n > 0; n--) {
2513 34820 int byte = bytestream2_get_byteu(&gb);
2514 69640 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2515 34820 byte >> 5 , 3, 0);
2516 69640 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2517 34820 (byte >> 2) & 0x07, 3, 0);
2518 34820 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2519 34820 byte & 0x03, 2, 0);
2520 }
2521 } else {
2522
2/2
✓ Branch 0 taken 26115 times.
✓ Branch 1 taken 13 times.
26128 for (int n = nb_samples >> (2 - st); n > 0; n--) {
2523 26115 int byte = bytestream2_get_byteu(&gb);
2524 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2525 26115 byte >> 6 , 2, 2);
2526 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2527 26115 (byte >> 4) & 0x03, 2, 2);
2528 52230 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2529 26115 (byte >> 2) & 0x03, 2, 2);
2530 26115 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2531 26115 byte & 0x03, 2, 2);
2532 }
2533 }
2534 57 break;
2535 #endif /* CONFIG_ADPCM_SBPRO_x_DECODER */
2536 195 CASE(ADPCM_SWF,
2537 adpcm_swf_decode(avctx, buf, buf_size, samples);
2538 bytestream2_seek(&gb, 0, SEEK_END);
2539 ) /* End of CASE */
2540
2/2
✓ Branch 3 taken 552960 times.
✓ Branch 4 taken 136 times.
553096 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
11/16
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 7 taken 215040 times.
✓ Branch 8 taken 215040 times.
✓ Branch 10 taken 430080 times.
✓ Branch 11 taken 26880 times.
✓ Branch 12 taken 26880 times.
✓ Branch 13 taken 24 times.
✓ Branch 14 taken 24 times.
✓ Branch 15 taken 12 times.
✓ Branch 16 taken 12 times.
✓ Branch 17 taken 12 times.
457008 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
2609 71 case AV_CODEC_ID_ADPCM_THP:
2610 case AV_CODEC_ID_ADPCM_THP_LE:
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 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
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
213 for (int i = 0; i < channels; i++)
2633
2/2
✓ Branch 0 taken 2272 times.
✓ Branch 1 taken 142 times.
2414 for (int n = 0; n < 16; n++)
2634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2272 times.
2272 table[i][n] = THP_GET16(gb);
2635
2636
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 70 times.
71 if (!c->has_status) {
2637 /* Initialize the previous sample. */
2638
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < channels; i++) {
2639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 c->status[i].sample1 = THP_GET16(gb);
2640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 c->status[i].sample2 = THP_GET16(gb);
2641 }
2642 1 c->has_status = 1;
2643 } else {
2644 70 bytestream2_skip(&gb, channels * 4);
2645 }
2646 }
2647
2648
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
213 for (int ch = 0; ch < channels; ch++) {
2649 142 samples = samples_p[ch];
2650
2651 /* Read in every sample for this channel. */
2652
2/2
✓ Branch 0 taken 10840 times.
✓ Branch 1 taken 142 times.
10982 for (int i = 0; i < (nb_samples + 13) / 14; i++) {
2653 10840 int byte = bytestream2_get_byteu(&gb);
2654 10840 int index = (byte >> 4) & 7;
2655 10840 unsigned int exp = byte & 0x0F;
2656 10840 int64_t factor1 = table[ch][index * 2];
2657 10840 int64_t factor2 = table[ch][index * 2 + 1];
2658
2659 /* Decode 14 samples. */
2660
3/4
✓ Branch 0 taken 151760 times.
✓ Branch 1 taken 10840 times.
✓ Branch 2 taken 151760 times.
✗ Branch 3 not taken.
162600 for (int n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
2661 int32_t sampledat;
2662
2663
2/2
✓ Branch 0 taken 75880 times.
✓ Branch 1 taken 75880 times.
151760 if (n & 1) {
2664 75880 sampledat = sign_extend(byte, 4);
2665 } else {
2666 75880 byte = bytestream2_get_byteu(&gb);
2667 75880 sampledat = sign_extend(byte >> 4, 4);
2668 }
2669
2670 151760 sampledat = ((c->status[ch].sample1 * factor1
2671 151760 + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
2672 151760 *samples = av_clip_int16(sampledat);
2673 151760 c->status[ch].sample2 = c->status[ch].sample1;
2674 151760 c->status[ch].sample1 = *samples++;
2675 }
2676 }
2677 }
2678 71 break;
2679 }
2680 #endif /* CONFIG_ADPCM_THP(_LE)_DECODER */
2681
16/16
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 1024 times.
✓ Branch 5 taken 3388 times.
✓ Branch 6 taken 50876 times.
✓ Branch 7 taken 2744 times.
✓ Branch 8 taken 336 times.
✓ Branch 10 taken 28672 times.
✓ Branch 11 taken 28672 times.
✓ Branch 12 taken 57344 times.
✓ Branch 13 taken 2048 times.
✓ Branch 14 taken 2048 times.
✓ Branch 15 taken 64 times.
✓ Branch 16 taken 32 times.
✓ Branch 17 taken 32 times.
✓ Branch 19 taken 64 times.
✓ Branch 20 taken 32 times.
59488 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;
2796 sample = av_clip_int16(sample);
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;
2827 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
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;
2866 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
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
9/10
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 124 times.
✓ Branch 7 taken 124 times.
✓ Branch 12 taken 62452 times.
✓ Branch 13 taken 62452 times.
✓ Branch 14 taken 62452 times.
✓ Branch 15 taken 124 times.
125152 CASE(ADPCM_SANYO,
2889 int (*expand)(ADPCMChannelStatus *c, int bits);
2890 GetBitContext g;
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
2/2
✓ Branch 3 taken 10240 times.
✓ Branch 4 taken 40 times.
10280 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
6/6
✓ Branch 4 taken 747296 times.
✓ Branch 5 taken 46706 times.
✓ Branch 6 taken 46706 times.
✓ Branch 7 taken 30245 times.
✓ Branch 8 taken 30245 times.
✓ Branch 9 taken 946 times.
825193 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
15/16
✗ Branch 2 not taken.
✓ Branch 3 taken 856 times.
✓ Branch 5 taken 856 times.
✓ Branch 6 taken 449 times.
✓ Branch 7 taken 42 times.
✓ Branch 8 taken 407 times.
✓ Branch 14 taken 5208 times.
✓ Branch 15 taken 42 times.
✓ Branch 18 taken 403744 times.
✓ Branch 19 taken 25234 times.
✓ Branch 20 taken 25234 times.
✓ Branch 21 taken 12617 times.
✓ Branch 22 taken 12617 times.
✓ Branch 23 taken 407 times.
✓ Branch 25 taken 449 times.
✓ Branch 26 taken 449 times.
448557 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
2/4
✓ Branch 0 taken 38299 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 38299 times.
38299 if (avpkt->size && bytestream2_tell(&gb) == 0) {
3014 ✗ av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
3015 ✗ return AVERROR_INVALIDDATA;
3016 }
3017
3018 38299 *got_frame_ptr = 1;
3019
3020
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38299 times.
38299 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 38299 return bytestream2_tell(&gb);
3026 }
3027
3028 208 static av_cold void adpcm_flush(AVCodecContext *avctx)
3029 {
3030 208 ADPCMDecodeContext *c = avctx->priv_data;
3031
3032 /* Just nuke the entire state and re-init. */
3033 208 memset(c, 0, sizeof(ADPCMDecodeContext));
3034
3035
5/5
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 189 times.
208 switch(avctx->codec_id) {
3036 2 case AV_CODEC_ID_ADPCM_CT:
3037 2 c->status[0].step = c->status[1].step = 511;
3038 2 break;
3039
3040 3 case AV_CODEC_ID_ADPCM_IMA_APC:
3041
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
3 if (avctx->extradata && avctx->extradata_size >= 8) {
3042 2 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
3043 2 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
3044 }
3045 3 break;
3046
3047 6 case AV_CODEC_ID_ADPCM_IMA_APM:
3048
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 if (avctx->extradata && avctx->extradata_size >= 28) {
3049 6 c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
3050 6 c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
3051 6 c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
3052 6 c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
3053 }
3054 6 break;
3055
3056 8 case AV_CODEC_ID_ADPCM_IMA_WS:
3057
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
8 if (avctx->extradata && avctx->extradata_size >= 2)
3058 5 c->vqa_version = AV_RL16(avctx->extradata);
3059 8 break;
3060 189 default:
3061 /* Other codecs may want to handle this during decoding. */
3062 189 c->has_status = 0;
3063 189 return;
3064 }
3065
3066 19 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_) \
3072 const 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. */
3092 ADPCM_DECODER(ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie")
3093 ADPCM_DECODER(ADPCM_AFC, adpcm_afc, "ADPCM Nintendo Gamecube AFC")
3094 ADPCM_DECODER(ADPCM_AGM, adpcm_agm, "ADPCM AmuseGraphics Movie")
3095 ADPCM_DECODER(ADPCM_AICA, adpcm_aica, "ADPCM Yamaha AICA")
3096 ADPCM_DECODER(ADPCM_ARGO, adpcm_argo, "ADPCM Argonaut Games")
3097 ADPCM_DECODER(ADPCM_CIRCUS, adpcm_circus, "ADPCM Circus")
3098 ADPCM_DECODER(ADPCM_CT, adpcm_ct, "ADPCM Creative Technology")
3099 ADPCM_DECODER(ADPCM_DTK, adpcm_dtk, "ADPCM Nintendo Gamecube DTK")
3100 ADPCM_DECODER(ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts")
3101 ADPCM_DECODER(ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA")
3102 ADPCM_DECODER(ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1")
3103 ADPCM_DECODER(ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2")
3104 ADPCM_DECODER(ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3")
3105 ADPCM_DECODER(ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS")
3106 ADPCM_DECODER(ADPCM_IMA_ACORN, adpcm_ima_acorn, "ADPCM IMA Acorn Replay")
3107 ADPCM_DECODER(ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV")
3108 ADPCM_DECODER(ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC")
3109 ADPCM_DECODER(ADPCM_IMA_APM, adpcm_ima_apm, "ADPCM IMA Ubisoft APM")
3110 ADPCM_DECODER(ADPCM_IMA_CITRIX, adpcm_ima_citrix, "ADPCM IMA Citrix")
3111 ADPCM_DECODER(ADPCM_IMA_CUNNING, adpcm_ima_cunning, "ADPCM IMA Cunning Developments")
3112 ADPCM_DECODER(ADPCM_IMA_DAT4, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4")
3113 ADPCM_DECODER(ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3")
3114 ADPCM_DECODER(ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4")
3115 ADPCM_DECODER(ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS")
3116 ADPCM_DECODER(ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD")
3117 ADPCM_DECODER(ADPCM_IMA_ESCAPE, adpcm_ima_escape, "ADPCM IMA Acorn Escape")
3118 ADPCM_DECODER(ADPCM_IMA_HVQM2, adpcm_ima_hvqm2, "ADPCM IMA HVQM2")
3119 ADPCM_DECODER(ADPCM_IMA_HVQM4, adpcm_ima_hvqm4, "ADPCM IMA HVQM4")
3120 ADPCM_DECODER(ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS")
3121 ADPCM_DECODER(ADPCM_IMA_MAGIX, adpcm_ima_magix, "ADPCM IMA Magix")
3122 ADPCM_DECODER(ADPCM_IMA_MOFLEX, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX")
3123 ADPCM_DECODER(ADPCM_IMA_MTF, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework")
3124 ADPCM_DECODER(ADPCM_IMA_OKI, adpcm_ima_oki, "ADPCM IMA Dialogic OKI")
3125 ADPCM_DECODER(ADPCM_IMA_PDA, adpcm_ima_pda, "ADPCM IMA PlayDate")
3126 ADPCM_DECODER(ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime")
3127 ADPCM_DECODER(ADPCM_IMA_RAD, adpcm_ima_rad, "ADPCM IMA Radical")
3128 ADPCM_DECODER(ADPCM_IMA_SSI, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive")
3129 ADPCM_DECODER(ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG")
3130 ADPCM_DECODER(ADPCM_IMA_ALP, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP")
3131 ADPCM_DECODER(ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV")
3132 ADPCM_DECODER(ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood")
3133 ADPCM_DECODER(ADPCM_IMA_XBOX, adpcm_ima_xbox, "ADPCM IMA Xbox")
3134 ADPCM_DECODER(ADPCM_MS, adpcm_ms, "ADPCM Microsoft")
3135 ADPCM_DECODER(ADPCM_MTAF, adpcm_mtaf, "ADPCM MTAF")
3136 ADPCM_DECODER(ADPCM_N64, adpcm_n64, "ADPCM Silicon Graphics N64")
3137 ADPCM_DECODER(ADPCM_PSX, adpcm_psx, "ADPCM Playstation")
3138 ADPCM_DECODER(ADPCM_PSXC, adpcm_psxc, "ADPCM Playstation C")
3139 ADPCM_DECODER(ADPCM_RHETOREX, adpcm_rhetorex, "ADPCM Rhetorex")
3140 ADPCM_DECODER(ADPCM_SANYO, adpcm_sanyo, "ADPCM Sanyo")
3141 ADPCM_DECODER(ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit")
3142 ADPCM_DECODER(ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit")
3143 ADPCM_DECODER(ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit")
3144 ADPCM_DECODER(ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash")
3145 ADPCM_DECODER(ADPCM_THP_LE, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)")
3146 ADPCM_DECODER(ADPCM_THP, adpcm_thp, "ADPCM Nintendo THP")
3147 ADPCM_DECODER(ADPCM_XA, adpcm_xa, "ADPCM CDROM XA")
3148 ADPCM_DECODER(ADPCM_XMD, adpcm_xmd, "ADPCM Konami XMD")
3149 ADPCM_DECODER(ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha")
3150 ADPCM_DECODER(ADPCM_ZORK, adpcm_zork, "ADPCM Zork")
3151