FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/imc.c
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 422 535 78.9%
Functions: 15 20 75.0%
Branches: 279 360 77.5%

Line Branch Exec Source
1 /*
2 * IMC compatible decoder
3 * Copyright (c) 2002-2004 Maxim Poliakovski
4 * Copyright (c) 2006 Benjamin Larsson
5 * Copyright (c) 2006 Konstantin Shishkov
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * IMC - Intel Music Coder
27 * A mdct based codec using a 256 points large transform
28 * divided into 32 bands with some mix of scale factors.
29 * Only mono is supported.
30 */
31
32 #include "config_components.h"
33
34 #include <math.h>
35 #include <stddef.h>
36
37 #include "libavutil/channel_layout.h"
38 #include "libavutil/ffmath.h"
39 #include "libavutil/float_dsp.h"
40 #include "libavutil/internal.h"
41 #include "libavutil/mem.h"
42 #include "libavutil/mem_internal.h"
43 #include "libavutil/thread.h"
44 #include "libavutil/tx.h"
45
46 #include "avcodec.h"
47 #include "bswapdsp.h"
48 #include "codec_internal.h"
49 #include "decode.h"
50 #include "get_bits.h"
51 #include "sinewin.h"
52
53 #include "imcdata.h"
54
55 #define IMC_BLOCK_SIZE 64
56 #define IMC_FRAME_ID 0x21
57 #define BANDS 32
58 #define COEFFS 256
59
60 typedef struct IMCChannel {
61 float old_floor[BANDS];
62 float flcoeffs1[BANDS];
63 float flcoeffs2[BANDS];
64 float flcoeffs3[BANDS];
65 float flcoeffs4[BANDS];
66 float flcoeffs5[BANDS];
67 float flcoeffs6[BANDS];
68 DECLARE_ALIGNED(32, float, CWdecoded)[COEFFS];
69
70 int bandWidthT[BANDS]; ///< codewords per band
71 int bitsBandT[BANDS]; ///< how many bits per codeword in band
72 int CWlengthT[COEFFS]; ///< how many bits in each codeword
73 int levlCoeffBuf[BANDS];
74 int bandFlagsBuf[BANDS]; ///< flags for each band
75 int sumLenArr[BANDS]; ///< bits for all coeffs in band
76 int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
77 int skipFlagBits[BANDS]; ///< bits used to code skip flags
78 int skipFlagCount[BANDS]; ///< skipped coefficients per band
79 int skipFlags[COEFFS]; ///< skip coefficient decoding or not
80 int codewords[COEFFS]; ///< raw codewords read from bitstream
81
82 int decoder_reset;
83 DECLARE_ALIGNED(32, float, prev_win)[128];
84 } IMCChannel;
85
86 typedef struct IMCContext {
87 IMCChannel chctx[2];
88
89 /** MDCT tables */
90 DECLARE_ALIGNED(32, float, mdct_sine_window)[COEFFS];
91
92 float sqrt_tab[30];
93 GetBitContext gb;
94
95 AVFloatDSPContext *fdsp;
96 BswapDSPContext bdsp;
97 AVTXContext *mdct;
98 av_tx_fn mdct_fn;
99 float *out_samples;
100 DECLARE_ALIGNED(32, float, temp)[256];
101
102 int coef0_pos;
103
104 int8_t cyclTab[32], cyclTab2[32];
105 float weights1[31], weights2[31];
106
107 AVCodecContext *avctx;
108 } IMCContext;
109
110 static const VLCElem *huffman_vlc[4][4];
111
112 #define IMC_VLC_BITS 9
113 #define VLC_TABLES_SIZE 9512
114
115 static VLCElem vlc_tables[VLC_TABLES_SIZE];
116
117 static inline double freq2bark(double freq)
118 {
119 return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
120 }
121
122 static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
123 {
124 double freqmin[32], freqmid[32], freqmax[32];
125 double scale = sampling_rate / (256.0 * 2.0 * 2.0);
126 double nyquist_freq = sampling_rate * 0.5;
127 double freq, bark, prev_bark = 0, tf, tb;
128 int i, j;
129
130 for (i = 0; i < 32; i++) {
131 freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
132 bark = freq2bark(freq);
133
134 if (i > 0) {
135 tb = bark - prev_bark;
136 q->weights1[i - 1] = ff_exp10(-1.0 * tb);
137 q->weights2[i - 1] = ff_exp10(-2.7 * tb);
138 }
139 prev_bark = bark;
140
141 freqmid[i] = freq;
142
143 tf = freq;
144 while (tf < nyquist_freq) {
145 tf += 0.5;
146 tb = freq2bark(tf);
147 if (tb > bark + 0.5)
148 break;
149 }
150 freqmax[i] = tf;
151
152 tf = freq;
153 while (tf > 0.0) {
154 tf -= 0.5;
155 tb = freq2bark(tf);
156 if (tb <= bark - 0.5)
157 break;
158 }
159 freqmin[i] = tf;
160 }
161
162 for (i = 0; i < 32; i++) {
163 freq = freqmax[i];
164 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
165 q->cyclTab[i] = j + 1;
166
167 freq = freqmin[i];
168 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
169 q->cyclTab2[i] = j - 1;
170 }
171 }
172
173 1 static av_cold void imc_init_static(void)
174 {
175 1 VLCInitState state = VLC_INIT_STATE(vlc_tables);
176 /* initialize the VLC tables */
177
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int i = 0; i < 4 ; i++) {
178
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int j = 0; j < 4; j++) {
179 16 huffman_vlc[i][j] =
180 16 ff_vlc_init_tables_from_lengths(&state, IMC_VLC_BITS, imc_huffman_sizes[i],
181 16 imc_huffman_lens[i][j], 1,
182 16 imc_huffman_syms[i][j], 1, 1,
183 0, 0);
184 }
185 }
186 1 }
187
188 2 static av_cold int imc_decode_init(AVCodecContext *avctx)
189 {
190 int i, j, ret;
191 2 IMCContext *q = avctx->priv_data;
192 static AVOnce init_static_once = AV_ONCE_INIT;
193 2 float scale = 1.0f / (16384);
194
195
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (avctx->codec_id == AV_CODEC_ID_IAC && avctx->sample_rate > 96000) {
196 av_log(avctx, AV_LOG_ERROR,
197 "Strange sample rate of %i, file likely corrupt or "
198 "needing a new table derivation method.\n",
199 avctx->sample_rate);
200 return AVERROR_PATCHWELCOME;
201 }
202
203
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (avctx->codec_id == AV_CODEC_ID_IMC) {
204 2 av_channel_layout_uninit(&avctx->ch_layout);
205 2 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
206 }
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->ch_layout.nb_channels > 2) {
209 avpriv_request_sample(avctx, "Number of channels > 2");
210 return AVERROR_PATCHWELCOME;
211 }
212
213
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (j = 0; j < avctx->ch_layout.nb_channels; j++) {
214 2 q->chctx[j].decoder_reset = 1;
215
216
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2 times.
66 for (i = 0; i < BANDS; i++)
217 64 q->chctx[j].old_floor[i] = 1.0;
218 }
219
220 /* Build mdct window, a simple sine window normalized with sqrt(2) */
221 2 ff_sine_window_init(q->mdct_sine_window, COEFFS);
222
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < COEFFS; i++)
223 512 q->mdct_sine_window[i] *= sqrt(2.0);
224
225 /* Generate a square root table */
226
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
62 for (i = 0; i < 30; i++)
227 60 q->sqrt_tab[i] = sqrt(i);
228
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->codec_id == AV_CODEC_ID_IAC) {
230 iac_generate_tabs(q, avctx->sample_rate);
231 } else {
232 2 memcpy(q->cyclTab, cyclTab, sizeof(cyclTab));
233 2 memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
234 2 memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
235 2 memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
236 }
237
238 2 q->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!q->fdsp)
240 return AVERROR(ENOMEM);
241
242 2 ret = av_tx_init(&q->mdct, &q->mdct_fn, AV_TX_FLOAT_MDCT, 1, COEFFS, &scale, 0);
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
244 return ret;
245
246 2 ff_bswapdsp_init(&q->bdsp);
247
248 2 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
249
250 2 ff_thread_once(&init_static_once, imc_init_static);
251
252 2 return 0;
253 }
254
255 1312 static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
256 float *flcoeffs2, int *bandWidthT,
257 float *flcoeffs3, float *flcoeffs5)
258 {
259 float workT1[BANDS];
260 float workT2[BANDS];
261 float workT3[BANDS];
262 1312 float snr_limit = 1.e-30;
263 1312 float accum = 0.0;
264 int i, cnt2;
265
266
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
267 41984 flcoeffs5[i] = workT2[i] = 0.0;
268
2/2
✓ Branch 0 taken 37529 times.
✓ Branch 1 taken 4455 times.
41984 if (bandWidthT[i]) {
269 37529 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
270 37529 flcoeffs3[i] = 2.0 * flcoeffs2[i];
271 } else {
272 4455 workT1[i] = 0.0;
273 4455 flcoeffs3[i] = -30000.0;
274 }
275 41984 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
276
2/2
✓ Branch 0 taken 4455 times.
✓ Branch 1 taken 37529 times.
41984 if (workT3[i] <= snr_limit)
277 4455 workT3[i] = 0.0;
278 }
279
280
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
281
2/2
✓ Branch 0 taken 59040 times.
✓ Branch 1 taken 41984 times.
101024 for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
282 59040 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
283 41984 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
284 }
285
286
2/2
✓ Branch 0 taken 40672 times.
✓ Branch 1 taken 1312 times.
41984 for (i = 1; i < BANDS; i++) {
287 40672 accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
288 40672 flcoeffs5[i] += accum;
289 }
290
291
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++)
292 41984 workT2[i] = 0.0;
293
294
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
295
2/2
✓ Branch 0 taken 17056 times.
✓ Branch 1 taken 41984 times.
59040 for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
296 17056 flcoeffs5[cnt2] += workT3[i];
297 41984 workT2[cnt2+1] += workT3[i];
298 }
299
300 1312 accum = 0.0;
301
302
2/2
✓ Branch 0 taken 40672 times.
✓ Branch 1 taken 1312 times.
41984 for (i = BANDS-2; i >= 0; i--) {
303 40672 accum = (workT2[i+1] + accum) * q->weights2[i];
304 40672 flcoeffs5[i] += accum;
305 // there is missing code here, but it seems to never be triggered
306 }
307 1312 }
308
309
310 1312 static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
311 int *levlCoeffs)
312 {
313 int i;
314 1312 int start = 0;
315 const uint8_t *cb_sel;
316 1312 int s = stream_format_code >> 1;
317 1312 const VLCElem * const *const hufftab = huffman_vlc[s];
318
319 1312 cb_sel = imc_cb_select[s];
320
321
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1006 times.
1312 if (stream_format_code & 4)
322 306 start = 1;
323
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1006 times.
1312 if (start)
324 306 levlCoeffs[0] = get_bits(&q->gb, 7);
325
2/2
✓ Branch 0 taken 41678 times.
✓ Branch 1 taken 1312 times.
42990 for (i = start; i < BANDS; i++) {
326 41678 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]],
327 IMC_VLC_BITS, 2);
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41678 times.
41678 if (levlCoeffs[i] == 17)
329 levlCoeffs[i] += get_bits(&q->gb, 4);
330 }
331 1312 }
332
333 static void imc_read_level_coeffs_raw(IMCContext *q, int stream_format_code,
334 int *levlCoeffs)
335 {
336 int i;
337
338 q->coef0_pos = get_bits(&q->gb, 5);
339 levlCoeffs[0] = get_bits(&q->gb, 7);
340 for (i = 1; i < BANDS; i++)
341 levlCoeffs[i] = get_bits(&q->gb, 4);
342 }
343
344 306 static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
345 float *flcoeffs1, float *flcoeffs2)
346 {
347 int i, level;
348 float tmp, tmp2;
349 // maybe some frequency division thingy
350
351 306 flcoeffs1[0] = 20000.0 / exp2 (levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
352 306 flcoeffs2[0] = log2f(flcoeffs1[0]);
353 306 tmp = flcoeffs1[0];
354 306 tmp2 = flcoeffs2[0];
355
356
2/2
✓ Branch 0 taken 9486 times.
✓ Branch 1 taken 306 times.
9792 for (i = 1; i < BANDS; i++) {
357 9486 level = levlCoeffBuf[i];
358
2/2
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 8534 times.
9486 if (level == 16) {
359 952 flcoeffs1[i] = 1.0;
360 952 flcoeffs2[i] = 0.0;
361 } else {
362
1/2
✓ Branch 0 taken 8534 times.
✗ Branch 1 not taken.
8534 if (level < 17)
363 8534 level -= 7;
364 else if (level <= 24)
365 level -= 32;
366 else
367 level -= 16;
368
369 8534 tmp *= imc_exp_tab[15 + level];
370 8534 tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
371 8534 flcoeffs1[i] = tmp;
372 8534 flcoeffs2[i] = tmp2;
373 }
374 }
375 306 }
376
377
378 1006 static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
379 float *old_floor, float *flcoeffs1,
380 float *flcoeffs2)
381 {
382 int i;
383 /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
384 * and flcoeffs2 old scale factors
385 * might be incomplete due to a missing table that is in the binary code
386 */
387
2/2
✓ Branch 0 taken 32192 times.
✓ Branch 1 taken 1006 times.
33198 for (i = 0; i < BANDS; i++) {
388 32192 flcoeffs1[i] = 0;
389
2/2
✓ Branch 0 taken 28689 times.
✓ Branch 1 taken 3503 times.
32192 if (levlCoeffBuf[i] < 16) {
390 28689 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
391 28689 flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
392 } else {
393 3503 flcoeffs1[i] = old_floor[i];
394 }
395 }
396 1006 }
397
398 static void imc_decode_level_coefficients_raw(IMCContext *q, int *levlCoeffBuf,
399 float *flcoeffs1, float *flcoeffs2)
400 {
401 int i, level, pos;
402 float tmp, tmp2;
403
404 pos = q->coef0_pos;
405 flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
406 flcoeffs2[pos] = log2f(flcoeffs1[pos]);
407 tmp = flcoeffs1[pos];
408 tmp2 = flcoeffs2[pos];
409
410 levlCoeffBuf++;
411 for (i = 0; i < BANDS; i++) {
412 if (i == pos)
413 continue;
414 level = *levlCoeffBuf++;
415 flcoeffs1[i] = tmp * powf(10.0, -level * 0.4375); //todo tab
416 flcoeffs2[i] = tmp2 - 1.4533435415 * level; // 1.4533435415 = log2(10) * 0.4375
417 }
418 }
419
420 /**
421 * Perform bit allocation depending on bits available
422 */
423 1312 static int bit_allocation(IMCContext *q, IMCChannel *chctx,
424 int stream_format_code, int freebits, int flag)
425 {
426 int i, j;
427 1312 const float limit = -1.e20;
428 1312 float highest = 0.0;
429 int indx;
430 1312 int t1 = 0;
431 1312 int t2 = 1;
432 1312 float summa = 0.0;
433 1312 int iacc = 0;
434 1312 int summer = 0;
435 int rres, cwlen;
436 1312 float lowest = 1.e10;
437 1312 int low_indx = 0;
438 float workT[32];
439 int flg;
440 1312 int found_indx = 0;
441
442
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++)
443
2/2
✓ Branch 0 taken 36549 times.
✓ Branch 1 taken 5435 times.
41984 highest = FFMAX(highest, chctx->flcoeffs1[i]);
444
445
2/2
✓ Branch 0 taken 40672 times.
✓ Branch 1 taken 1312 times.
41984 for (i = 0; i < BANDS - 1; i++) {
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40672 times.
40672 if (chctx->flcoeffs5[i] <= 0) {
447 av_log(q->avctx, AV_LOG_ERROR, "flcoeffs5 %f invalid\n", chctx->flcoeffs5[i]);
448 return AVERROR_INVALIDDATA;
449 }
450 40672 chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
451 }
452 1312 chctx->flcoeffs4[BANDS - 1] = limit;
453
454 1312 highest = highest * 0.25;
455
456
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
457 41984 indx = -1;
458
2/2
✓ Branch 0 taken 37529 times.
✓ Branch 1 taken 4455 times.
41984 if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
459 37529 indx = 0;
460
461
2/2
✓ Branch 0 taken 4455 times.
✓ Branch 1 taken 37529 times.
41984 if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
462 4455 indx = 1;
463
464
2/2
✓ Branch 0 taken 4455 times.
✓ Branch 1 taken 37529 times.
41984 if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
465 4455 indx = 2;
466
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41984 times.
41984 if (indx == -1)
468 return AVERROR_INVALIDDATA;
469
470 41984 chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
471 }
472
473
2/2
✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 132 times.
1312 if (stream_format_code & 0x2) {
474 1180 chctx->flcoeffs4[0] = limit;
475 1180 chctx->flcoeffs4[1] = limit;
476 1180 chctx->flcoeffs4[2] = limit;
477 1180 chctx->flcoeffs4[3] = limit;
478 }
479
480
2/2
✓ Branch 0 taken 35952 times.
✓ Branch 1 taken 1312 times.
37264 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
481 35952 iacc += chctx->bandWidthT[i];
482 35952 summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
483 }
484
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (!iacc)
486 return AVERROR_INVALIDDATA;
487
488 1312 chctx->bandWidthT[BANDS - 1] = 0;
489 1312 summa = (summa * 0.5 - freebits) / iacc;
490
491
492
2/2
✓ Branch 0 taken 4317 times.
✓ Branch 1 taken 5 times.
4322 for (i = 0; i < BANDS / 2; i++) {
493 4317 rres = summer - freebits;
494
4/4
✓ Branch 0 taken 2056 times.
✓ Branch 1 taken 2261 times.
✓ Branch 2 taken 1307 times.
✓ Branch 3 taken 749 times.
4317 if ((rres >= -8) && (rres <= 8))
495 1307 break;
496
497 3010 summer = 0;
498 3010 iacc = 0;
499
500
2/2
✓ Branch 0 taken 85280 times.
✓ Branch 1 taken 3010 times.
88290 for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
501 85280 cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
502
503 85280 chctx->bitsBandT[j] = cwlen;
504 85280 summer += chctx->bandWidthT[j] * cwlen;
505
506
2/2
✓ Branch 0 taken 68479 times.
✓ Branch 1 taken 16801 times.
85280 if (cwlen > 0)
507 68479 iacc += chctx->bandWidthT[j];
508 }
509
510 3010 flg = t2;
511 3010 t2 = 1;
512
2/2
✓ Branch 0 taken 1369 times.
✓ Branch 1 taken 1641 times.
3010 if (freebits < summer)
513 1369 t2 = -1;
514
2/2
✓ Branch 0 taken 1312 times.
✓ Branch 1 taken 1698 times.
3010 if (i == 0)
515 1312 flg = t2;
516
2/2
✓ Branch 0 taken 1177 times.
✓ Branch 1 taken 1833 times.
3010 if (flg != t2)
517 1177 t1++;
518
519 3010 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
520 }
521
522
2/2
✓ Branch 0 taken 37264 times.
✓ Branch 1 taken 1312 times.
38576 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
523
2/2
✓ Branch 0 taken 321712 times.
✓ Branch 1 taken 37264 times.
358976 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
524 321712 chctx->CWlengthT[j] = chctx->bitsBandT[i];
525 }
526
527
2/2
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 718 times.
1312 if (freebits > summer) {
528
2/2
✓ Branch 0 taken 19008 times.
✓ Branch 1 taken 594 times.
19602 for (i = 0; i < BANDS; i++) {
529 19008 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
530
1/2
✓ Branch 0 taken 19008 times.
✗ Branch 1 not taken.
19008 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
531 }
532
533 594 highest = 0.0;
534
535 do {
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 641 times.
641 if (highest <= -1.e20)
537 break;
538
539 641 found_indx = 0;
540 641 highest = -1.e20;
541
542
2/2
✓ Branch 0 taken 20512 times.
✓ Branch 1 taken 641 times.
21153 for (i = 0; i < BANDS; i++) {
543
2/2
✓ Branch 0 taken 2365 times.
✓ Branch 1 taken 18147 times.
20512 if (workT[i] > highest) {
544 2365 highest = workT[i];
545 2365 found_indx = i;
546 }
547 }
548
549
1/2
✓ Branch 0 taken 641 times.
✗ Branch 1 not taken.
641 if (highest > -1.e20) {
550 641 workT[found_indx] -= 2.0;
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 641 times.
641 if (++chctx->bitsBandT[found_indx] == 6)
552 workT[found_indx] = -1.e20;
553
554
4/4
✓ Branch 0 taken 3012 times.
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 2456 times.
✓ Branch 3 taken 556 times.
3097 for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
555 2456 chctx->CWlengthT[j]++;
556 2456 summer++;
557 }
558 }
559
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 594 times.
641 } while (freebits > summer);
560 }
561
2/2
✓ Branch 0 taken 620 times.
✓ Branch 1 taken 692 times.
1312 if (freebits < summer) {
562
2/2
✓ Branch 0 taken 19840 times.
✓ Branch 1 taken 620 times.
20460 for (i = 0; i < BANDS; i++) {
563 19840 workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
564
2/2
✓ Branch 0 taken 16422 times.
✓ Branch 1 taken 3418 times.
19840 : 1.e20;
565 }
566
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 73 times.
620 if (stream_format_code & 0x2) {
567 547 workT[0] = 1.e20;
568 547 workT[1] = 1.e20;
569 547 workT[2] = 1.e20;
570 547 workT[3] = 1.e20;
571 }
572
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 620 times.
1292 while (freebits < summer) {
573 672 lowest = 1.e10;
574 672 low_indx = 0;
575
2/2
✓ Branch 0 taken 21504 times.
✓ Branch 1 taken 672 times.
22176 for (i = 0; i < BANDS; i++) {
576
2/2
✓ Branch 0 taken 3064 times.
✓ Branch 1 taken 18440 times.
21504 if (workT[i] < lowest) {
577 3064 lowest = workT[i];
578 3064 low_indx = i;
579 }
580 }
581 // if (lowest >= 1.e10)
582 // break;
583 672 workT[low_indx] = lowest + 2.0;
584
585
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 489 times.
672 if (!--chctx->bitsBandT[low_indx])
586 183 workT[low_indx] = 1.e20;
587
588
4/4
✓ Branch 0 taken 3208 times.
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 2628 times.
✓ Branch 3 taken 580 times.
3300 for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
589
1/2
✓ Branch 0 taken 2628 times.
✗ Branch 1 not taken.
2628 if (chctx->CWlengthT[j] > 0) {
590 2628 chctx->CWlengthT[j]--;
591 2628 summer--;
592 }
593 }
594 }
595 }
596 1312 return 0;
597 }
598
599 1312 static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
600 {
601 int i, j;
602
603 1312 memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits));
604 1312 memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
605
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
606
3/4
✓ Branch 0 taken 8850 times.
✓ Branch 1 taken 33134 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8850 times.
41984 if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
607 33134 continue;
608
609
2/2
✓ Branch 0 taken 7672 times.
✓ Branch 1 taken 1178 times.
8850 if (!chctx->skipFlagRaw[i]) {
610 7672 chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
611
612
2/2
✓ Branch 0 taken 47051 times.
✓ Branch 1 taken 7672 times.
54723 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
613 47051 chctx->skipFlags[j] = get_bits1(&q->gb);
614
2/2
✓ Branch 0 taken 24771 times.
✓ Branch 1 taken 22280 times.
47051 if (chctx->skipFlags[j])
615 24771 chctx->skipFlagCount[i]++;
616 }
617 } else {
618
2/2
✓ Branch 0 taken 5470 times.
✓ Branch 1 taken 1178 times.
6648 for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
619
2/2
✓ Branch 1 taken 3869 times.
✓ Branch 2 taken 1601 times.
5470 if (!get_bits1(&q->gb)) { // 0
620 3869 chctx->skipFlagBits[i]++;
621 3869 chctx->skipFlags[j] = 1;
622 3869 chctx->skipFlags[j + 1] = 1;
623 3869 chctx->skipFlagCount[i] += 2;
624 } else {
625
2/2
✓ Branch 1 taken 899 times.
✓ Branch 2 taken 702 times.
1601 if (get_bits1(&q->gb)) { // 11
626 899 chctx->skipFlagBits[i] += 2;
627 899 chctx->skipFlags[j] = 0;
628 899 chctx->skipFlags[j + 1] = 1;
629 899 chctx->skipFlagCount[i]++;
630 } else {
631 702 chctx->skipFlagBits[i] += 3;
632 702 chctx->skipFlags[j + 1] = 0;
633
2/2
✓ Branch 1 taken 466 times.
✓ Branch 2 taken 236 times.
702 if (!get_bits1(&q->gb)) { // 100
634 466 chctx->skipFlags[j] = 1;
635 466 chctx->skipFlagCount[i]++;
636 } else { // 101
637 236 chctx->skipFlags[j] = 0;
638 }
639 }
640 }
641 }
642
643
2/2
✓ Branch 0 taken 554 times.
✓ Branch 1 taken 624 times.
1178 if (j < band_tab[i + 1]) {
644 554 chctx->skipFlagBits[i]++;
645
2/2
✓ Branch 1 taken 357 times.
✓ Branch 2 taken 197 times.
554 if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
646 357 chctx->skipFlagCount[i]++;
647 }
648 }
649 }
650 1312 }
651
652 /**
653 * Increase highest' band coefficient sizes as some bits won't be used
654 */
655 1312 static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
656 int summer)
657 {
658 float workT[32];
659 1312 int corrected = 0;
660 int i, j;
661 1312 float highest = 0;
662 1312 int found_indx = 0;
663
664
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
665 41984 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
666
1/2
✓ Branch 0 taken 41984 times.
✗ Branch 1 not taken.
41984 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
667 }
668
669
2/2
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 1312 times.
4780 while (corrected < summer) {
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3468 times.
3468 if (highest <= -1.e20)
671 break;
672
673 3468 highest = -1.e20;
674
675
2/2
✓ Branch 0 taken 110976 times.
✓ Branch 1 taken 3468 times.
114444 for (i = 0; i < BANDS; i++) {
676
2/2
✓ Branch 0 taken 12960 times.
✓ Branch 1 taken 98016 times.
110976 if (workT[i] > highest) {
677 12960 highest = workT[i];
678 12960 found_indx = i;
679 }
680 }
681
682
1/2
✓ Branch 0 taken 3468 times.
✗ Branch 1 not taken.
3468 if (highest > -1.e20) {
683 3468 workT[found_indx] -= 2.0;
684
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3464 times.
3468 if (++(chctx->bitsBandT[found_indx]) == 6)
685 4 workT[found_indx] = -1.e20;
686
687
4/4
✓ Branch 0 taken 24189 times.
✓ Branch 1 taken 2314 times.
✓ Branch 2 taken 23035 times.
✓ Branch 3 taken 1154 times.
26503 for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
688
3/4
✓ Branch 0 taken 19375 times.
✓ Branch 1 taken 3660 times.
✓ Branch 2 taken 19375 times.
✗ Branch 3 not taken.
23035 if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
689 19375 chctx->CWlengthT[j]++;
690 19375 corrected++;
691 }
692 }
693 }
694 }
695 1312 }
696
697 1312 static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
698 int stream_format_code)
699 {
700 int i, j;
701 int middle_value, cw_len, max_size;
702 const float *quantizer;
703
704
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
705
2/2
✓ Branch 0 taken 335872 times.
✓ Branch 1 taken 41984 times.
377856 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
706 335872 chctx->CWdecoded[j] = 0;
707 335872 cw_len = chctx->CWlengthT[j];
708
709
3/4
✓ Branch 0 taken 221471 times.
✓ Branch 1 taken 114401 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 221471 times.
335872 if (cw_len <= 0 || chctx->skipFlags[j])
710 114401 continue;
711
712 221471 max_size = 1 << cw_len;
713 221471 middle_value = max_size >> 1;
714
715
2/4
✓ Branch 0 taken 221471 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 221471 times.
221471 if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
716 return AVERROR_INVALIDDATA;
717
718
2/2
✓ Branch 0 taken 26862 times.
✓ Branch 1 taken 194609 times.
221471 if (cw_len >= 4) {
719 26862 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
720
2/2
✓ Branch 0 taken 13149 times.
✓ Branch 1 taken 13713 times.
26862 if (chctx->codewords[j] >= middle_value)
721 13149 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i];
722 else
723 13713 chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
724 }else{
725 194609 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
726
2/2
✓ Branch 0 taken 97364 times.
✓ Branch 1 taken 97245 times.
194609 if (chctx->codewords[j] >= middle_value)
727 97364 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i];
728 else
729 97245 chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
730 }
731 }
732 }
733 1312 return 0;
734 }
735
736
737 1312 static void imc_get_coeffs(AVCodecContext *avctx,
738 IMCContext *q, IMCChannel *chctx)
739 {
740 int i, j, cw_len, cw;
741
742
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
743
2/2
✓ Branch 0 taken 6765 times.
✓ Branch 1 taken 35219 times.
41984 if (!chctx->sumLenArr[i])
744 6765 continue;
745
3/4
✓ Branch 0 taken 26372 times.
✓ Branch 1 taken 8847 times.
✓ Branch 2 taken 26372 times.
✗ Branch 3 not taken.
35219 if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
746
2/2
✓ Branch 0 taken 257912 times.
✓ Branch 1 taken 35219 times.
293131 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
747 257912 cw_len = chctx->CWlengthT[j];
748 257912 cw = 0;
749
750
5/6
✓ Branch 0 taken 221471 times.
✓ Branch 1 taken 36441 times.
✓ Branch 2 taken 24311 times.
✓ Branch 3 taken 197160 times.
✓ Branch 4 taken 24311 times.
✗ Branch 5 not taken.
257912 if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j])) {
751
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 221471 times.
221471 if (get_bits_count(&q->gb) + cw_len > 512) {
752 av_log(avctx, AV_LOG_WARNING,
753 "Potential problem on band %i, coefficient %i"
754 ": cw_len=%i\n", i, j, cw_len);
755 } else
756 221471 cw = get_bits(&q->gb, cw_len);
757 }
758
759 257912 chctx->codewords[j] = cw;
760 }
761 }
762 }
763 1312 }
764
765 1312 static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx)
766 {
767 int i, j;
768 int summer;
769
770
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
771 41984 chctx->sumLenArr[i] = 0;
772 41984 chctx->skipFlagRaw[i] = 0;
773
2/2
✓ Branch 0 taken 335872 times.
✓ Branch 1 taken 41984 times.
377856 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
774 335872 chctx->sumLenArr[i] += chctx->CWlengthT[j];
775
2/2
✓ Branch 0 taken 8850 times.
✓ Branch 1 taken 33134 times.
41984 if (chctx->bandFlagsBuf[i])
776
3/4
✓ Branch 0 taken 1178 times.
✓ Branch 1 taken 7672 times.
✓ Branch 2 taken 1178 times.
✗ Branch 3 not taken.
8850 if (((int)((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
777 1178 chctx->skipFlagRaw[i] = 1;
778 }
779
780 1312 imc_get_skip_coeff(q, chctx);
781
782
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
783 41984 chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
784 /* band has flag set and at least one coded coefficient */
785
3/4
✓ Branch 0 taken 8850 times.
✓ Branch 1 taken 33134 times.
✓ Branch 2 taken 8850 times.
✗ Branch 3 not taken.
41984 if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
786 8850 chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
787 8850 q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
788 }
789 }
790
791 /* calculate bits left, bits needed and adjust bit allocation */
792 1312 summer = 0;
793
794
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
795
2/2
✓ Branch 0 taken 8850 times.
✓ Branch 1 taken 33134 times.
41984 if (chctx->bandFlagsBuf[i]) {
796
2/2
✓ Branch 0 taken 58545 times.
✓ Branch 1 taken 8850 times.
67395 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
797
2/2
✓ Branch 0 taken 34231 times.
✓ Branch 1 taken 24314 times.
58545 if (chctx->skipFlags[j]) {
798 34231 summer += chctx->CWlengthT[j];
799 34231 chctx->CWlengthT[j] = 0;
800 }
801 }
802 8850 summer -= chctx->skipFlagBits[i];
803 }
804 }
805 1312 imc_adjust_bit_allocation(q, chctx, summer);
806 1312 }
807
808 1312 static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
809 {
810 int stream_format_code;
811 int imc_hdr, i, j, ret;
812 int flag;
813 int bits;
814 int bitscount;
815 1312 IMCChannel *chctx = q->chctx + ch;
816
817
818 /* Check the frame header */
819 1312 imc_hdr = get_bits(&q->gb, 9);
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (imc_hdr & 0x18) {
821 av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
822 av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
823 return AVERROR_INVALIDDATA;
824 }
825 1312 stream_format_code = get_bits(&q->gb, 3);
826
827
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1006 times.
1312 if (stream_format_code & 0x04)
828 306 chctx->decoder_reset = 1;
829
830
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1006 times.
1312 if (chctx->decoder_reset) {
831
2/2
✓ Branch 0 taken 9792 times.
✓ Branch 1 taken 306 times.
10098 for (i = 0; i < BANDS; i++)
832 9792 chctx->old_floor[i] = 1.0;
833
2/2
✓ Branch 0 taken 78336 times.
✓ Branch 1 taken 306 times.
78642 for (i = 0; i < COEFFS; i++)
834 78336 chctx->CWdecoded[i] = 0;
835 306 chctx->decoder_reset = 0;
836 }
837
838 1312 flag = get_bits1(&q->gb);
839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (stream_format_code & 0x1)
840 imc_read_level_coeffs_raw(q, stream_format_code, chctx->levlCoeffBuf);
841 else
842 1312 imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (stream_format_code & 0x1)
845 imc_decode_level_coefficients_raw(q, chctx->levlCoeffBuf,
846 chctx->flcoeffs1, chctx->flcoeffs2);
847
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1006 times.
1312 else if (stream_format_code & 0x4)
848 306 imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
849 306 chctx->flcoeffs1, chctx->flcoeffs2);
850 else
851 1006 imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
852 1006 chctx->flcoeffs1, chctx->flcoeffs2);
853
854
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for(i=0; i<BANDS; i++) {
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41984 times.
41984 if(chctx->flcoeffs1[i] > INT_MAX) {
856 av_log(avctx, AV_LOG_ERROR, "scalefactor out of range\n");
857 return AVERROR_INVALIDDATA;
858 }
859 }
860
861 1312 memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
862
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (stream_format_code & 0x1) {
864 for (i = 0; i < BANDS; i++) {
865 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
866 chctx->bandFlagsBuf[i] = 0;
867 chctx->flcoeffs3[i] = chctx->flcoeffs2[i] * 2;
868 chctx->flcoeffs5[i] = 1.0;
869 }
870 } else {
871
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
872
2/2
✓ Branch 0 taken 4455 times.
✓ Branch 1 taken 37529 times.
41984 if (chctx->levlCoeffBuf[i] == 16) {
873 4455 chctx->bandWidthT[i] = 0;
874 } else
875 37529 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
876 }
877
878 1312 memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
879
2/2
✓ Branch 0 taken 40672 times.
✓ Branch 1 taken 1312 times.
41984 for (i = 0; i < BANDS - 1; i++)
880
2/2
✓ Branch 0 taken 37510 times.
✓ Branch 1 taken 3162 times.
40672 if (chctx->bandWidthT[i])
881 37510 chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
882
883 1312 imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2,
884 1312 chctx->bandWidthT, chctx->flcoeffs3,
885 1312 chctx->flcoeffs5);
886 }
887
888 1312 bitscount = 0;
889 /* first 4 bands will be assigned 5 bits per coefficient */
890
2/2
✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 132 times.
1312 if (stream_format_code & 0x2) {
891 1180 bitscount += 15;
892
893 1180 chctx->bitsBandT[0] = 5;
894 1180 chctx->CWlengthT[0] = 5;
895 1180 chctx->CWlengthT[1] = 5;
896 1180 chctx->CWlengthT[2] = 5;
897
2/2
✓ Branch 0 taken 3540 times.
✓ Branch 1 taken 1180 times.
4720 for (i = 1; i < 4; i++) {
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3540 times.
3540 if (stream_format_code & 0x1)
899 bits = 5;
900 else
901
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 3496 times.
3540 bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
902 3540 chctx->bitsBandT[i] = bits;
903
2/2
✓ Branch 0 taken 10620 times.
✓ Branch 1 taken 3540 times.
14160 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
904 10620 chctx->CWlengthT[j] = bits;
905 10620 bitscount += bits;
906 }
907 }
908 }
909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (avctx->codec_id == AV_CODEC_ID_IAC) {
910 bitscount += !!chctx->bandWidthT[BANDS - 1];
911 if (!(stream_format_code & 0x2))
912 bitscount += 16;
913 }
914
915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1312 times.
1312 if ((ret = bit_allocation(q, chctx, stream_format_code,
916 1312 512 - bitscount - get_bits_count(&q->gb),
917 flag)) < 0) {
918 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
919 chctx->decoder_reset = 1;
920 return ret;
921 }
922
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (stream_format_code & 0x1) {
924 for (i = 0; i < BANDS; i++)
925 chctx->skipFlags[i] = 0;
926 } else {
927 1312 imc_refine_bit_allocation(q, chctx);
928 }
929
930
2/2
✓ Branch 0 taken 41984 times.
✓ Branch 1 taken 1312 times.
43296 for (i = 0; i < BANDS; i++) {
931 41984 chctx->sumLenArr[i] = 0;
932
933
2/2
✓ Branch 0 taken 335872 times.
✓ Branch 1 taken 41984 times.
377856 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
934
2/2
✓ Branch 0 taken 301641 times.
✓ Branch 1 taken 34231 times.
335872 if (!chctx->skipFlags[j])
935 301641 chctx->sumLenArr[i] += chctx->CWlengthT[j];
936 }
937
938 1312 memset(chctx->codewords, 0, sizeof(chctx->codewords));
939
940 1312 imc_get_coeffs(avctx, q, chctx);
941
942
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1312 times.
1312 if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
943 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
944 chctx->decoder_reset = 1;
945 return AVERROR_INVALIDDATA;
946 }
947
948 1312 memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
949
950 1312 q->mdct_fn(q->mdct, q->temp, chctx->CWdecoded, sizeof(float));
951 1312 q->fdsp->vector_fmul_window(q->out_samples, chctx->prev_win, q->temp,
952 1312 q->mdct_sine_window, 128);
953 1312 memcpy(chctx->prev_win, q->temp + 128, sizeof(float)*128);
954
955 1312 return 0;
956 }
957
958 1312 static int imc_decode_frame(AVCodecContext *avctx, AVFrame *frame,
959 int *got_frame_ptr, AVPacket *avpkt)
960 {
961 1312 const uint8_t *buf = avpkt->data;
962 1312 int buf_size = avpkt->size;
963 int ret, i;
964
965 1312 IMCContext *q = avctx->priv_data;
966
967 1312 LOCAL_ALIGNED_16(uint16_t, buf16, [(IMC_BLOCK_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) / 2]);
968
969 1312 q->avctx = avctx;
970
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (buf_size < IMC_BLOCK_SIZE * avctx->ch_layout.nb_channels) {
972 av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
973 return AVERROR_INVALIDDATA;
974 }
975
976 /* get output buffer */
977 1312 frame->nb_samples = COEFFS;
978
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1312 times.
1312 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
979 return ret;
980
981
2/2
✓ Branch 0 taken 1312 times.
✓ Branch 1 taken 1312 times.
2624 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
982 1312 q->out_samples = (float *)frame->extended_data[i];
983
984 1312 q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
985
986 1312 init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
987
988 1312 buf += IMC_BLOCK_SIZE;
989
990
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1312 times.
1312 if ((ret = imc_decode_block(avctx, q, i)) < 0)
991 return ret;
992 }
993
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (avctx->ch_layout.nb_channels == 2) {
995 q->fdsp->butterflies_float((float *)frame->extended_data[0],
996 (float *)frame->extended_data[1], COEFFS);
997 }
998
999 1312 *got_frame_ptr = 1;
1000
1001 1312 return IMC_BLOCK_SIZE * avctx->ch_layout.nb_channels;
1002 }
1003
1004 2 static av_cold int imc_decode_close(AVCodecContext * avctx)
1005 {
1006 2 IMCContext *q = avctx->priv_data;
1007
1008 2 av_free(q->fdsp);
1009 2 av_tx_uninit(&q->mdct);
1010
1011 2 return 0;
1012 }
1013
1014 static av_cold void flush(AVCodecContext *avctx)
1015 {
1016 IMCContext *q = avctx->priv_data;
1017
1018 q->chctx[0].decoder_reset =
1019 q->chctx[1].decoder_reset = 1;
1020 }
1021
1022 #if CONFIG_IMC_DECODER
1023 const FFCodec ff_imc_decoder = {
1024 .p.name = "imc",
1025 CODEC_LONG_NAME("IMC (Intel Music Coder)"),
1026 .p.type = AVMEDIA_TYPE_AUDIO,
1027 .p.id = AV_CODEC_ID_IMC,
1028 .priv_data_size = sizeof(IMCContext),
1029 .init = imc_decode_init,
1030 .close = imc_decode_close,
1031 FF_CODEC_DECODE_CB(imc_decode_frame),
1032 .flush = flush,
1033 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1034 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1035 AV_SAMPLE_FMT_NONE },
1036 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1037 };
1038 #endif
1039 #if CONFIG_IAC_DECODER
1040 const FFCodec ff_iac_decoder = {
1041 .p.name = "iac",
1042 CODEC_LONG_NAME("IAC (Indeo Audio Coder)"),
1043 .p.type = AVMEDIA_TYPE_AUDIO,
1044 .p.id = AV_CODEC_ID_IAC,
1045 .priv_data_size = sizeof(IMCContext),
1046 .init = imc_decode_init,
1047 .close = imc_decode_close,
1048 FF_CODEC_DECODE_CB(imc_decode_frame),
1049 .flush = flush,
1050 .p.capabilities = AV_CODEC_CAP_DR1,
1051 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1052 AV_SAMPLE_FMT_NONE },
1053 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1054 };
1055 #endif
1056