FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/imc.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 455 565 80.5%
Branches: 289 370 78.1%

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