FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/imc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 460 570 80.7%
Branches: 290 372 78.0%

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