FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacsbr_template.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 756 911 83.0%
Functions: 41 41 100.0%
Branches: 532 712 74.7%

Line Branch Exec Source
1 /*
2 * AAC Spectral Band Replication decoding functions
3 * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
4 * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
5 *
6 * Fixed point code
7 * Copyright (c) 2013
8 * MIPS Technologies, Inc., California.
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 /**
28 * @file
29 * AAC Spectral Band Replication decoding functions
30 * @author Robert Swain ( rob opendot cl )
31 * @author Stanislav Ocovaj ( stanislav.ocovaj@imgtec.com )
32 * @author Zoran Basaric ( zoran.basaric@imgtec.com )
33 */
34
35 #include "aacdec.h"
36 #include "aacdectab.h"
37 #include "avcodec.h"
38 #include "libavutil/qsort.h"
39
40 179 static av_cold void aacsbr_tableinit(void)
41 {
42 int n;
43
44
2/2
✓ Branch 0 taken 57280 times.
✓ Branch 1 taken 179 times.
57459 for (n = 0; n < 320; n++)
45 57280 sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
46 179 }
47
48 179 av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
49 {
50 179 aacsbr_tableinit();
51
52 179 AAC_RENAME(ff_ps_init)();
53 179 }
54
55 /** Places SBR in pure upsampling mode. */
56 369 static void sbr_turnoff(SpectralBandReplication *sbr) {
57 369 sbr->start = 0;
58 369 sbr->ready_for_dequant = 0;
59 // Init defults used in pure upsampling mode
60 369 sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
61 369 sbr->m[1] = 0;
62 // Reset values for first SBR header
63 369 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
64 369 memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
65 369 }
66
67 369 av_cold int AAC_RENAME(ff_aac_sbr_ctx_init)(AACDecContext *ac, SpectralBandReplication *sbr, int id_aac)
68 {
69 int ret;
70 float scale;
71
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 if (sbr->mdct)
73 return 0;
74
75 369 sbr->kx[0] = sbr->kx[1];
76 369 sbr->id_aac = id_aac;
77 369 sbr_turnoff(sbr);
78 369 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
79 369 sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
80 /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
81 * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
82 * and scale back down at synthesis. */
83
84 369 scale = USE_FIXED ? 1 : 1.0 / (64 * 32768);
85 369 ret = av_tx_init(&sbr->mdct, &sbr->mdct_fn,
86 USE_FIXED ? AV_TX_INT32_MDCT : AV_TX_FLOAT_MDCT,
87 1, 64, &scale, 0);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 if (ret < 0)
89 return ret;
90
91 369 scale = USE_FIXED ? -1.0 : -2.0 * 32768;
92 369 ret = av_tx_init(&sbr->mdct_ana, &sbr->mdct_ana_fn,
93 USE_FIXED ? AV_TX_INT32_MDCT : AV_TX_FLOAT_MDCT,
94 1, 64, &scale, 0);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 if (ret < 0)
96 return ret;
97
98 369 AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
99 369 AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
100 369 aacsbr_func_ptr_init(&sbr->c);
101
102 369 return 0;
103 }
104
105 369 av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(SpectralBandReplication *sbr)
106 {
107 369 av_tx_uninit(&sbr->mdct);
108 369 av_tx_uninit(&sbr->mdct_ana);
109 369 }
110
111 6109 static int qsort_comparison_function_int16(const void *a, const void *b)
112 {
113 6109 return *(const int16_t *)a - *(const int16_t *)b;
114 }
115
116 381 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
117 {
118 int i;
119
2/2
✓ Branch 0 taken 1532 times.
✓ Branch 1 taken 239 times.
1771 for (i = 0; i <= last_el; i++)
120
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 1390 times.
1532 if (table[i] == needle)
121 142 return 1;
122 239 return 0;
123 }
124
125 /// Limiter Frequency Band Table (14496-3 sp04 p198)
126 65 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
127 {
128 int k;
129
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (sbr->bs_limiter_bands > 0) {
130 static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f), //2^(0.49/1.2)
131 Q23(1.18509277094158210129f), //2^(0.49/2)
132 Q23(1.11987160404675912501f) }; //2^(0.49/3)
133 65 const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
134 int16_t patch_borders[7];
135 65 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
136
137 65 patch_borders[0] = sbr->kx[1];
138
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 65 times.
251 for (k = 1; k <= sbr->num_patches; k++)
139 186 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
140
141 65 memcpy(sbr->f_tablelim, sbr->f_tablelow,
142 65 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
143
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2 times.
65 if (sbr->num_patches > 1)
144 63 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
145 63 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
146
147
41/44
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 97 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 192 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 192 times.
✓ Branch 12 taken 83 times.
✓ Branch 13 taken 128 times.
✓ Branch 14 taken 51 times.
✓ Branch 15 taken 160 times.
✓ Branch 16 taken 456 times.
✓ Branch 17 taken 16 times.
✓ Branch 19 taken 284 times.
✓ Branch 20 taken 172 times.
✓ Branch 21 taken 340 times.
✓ Branch 22 taken 132 times.
✓ Branch 24 taken 284 times.
✓ Branch 25 taken 56 times.
✓ Branch 26 taken 132 times.
✓ Branch 27 taken 56 times.
✓ Branch 28 taken 188 times.
✓ Branch 29 taken 160 times.
✓ Branch 30 taken 109 times.
✓ Branch 31 taken 51 times.
✓ Branch 32 taken 109 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 95 times.
✓ Branch 35 taken 14 times.
✓ Branch 36 taken 451 times.
✓ Branch 37 taken 22 times.
✓ Branch 39 taken 378 times.
✓ Branch 40 taken 73 times.
✓ Branch 41 taken 22 times.
✓ Branch 42 taken 73 times.
✓ Branch 43 taken 30 times.
✓ Branch 44 taken 108 times.
✓ Branch 46 taken 45 times.
✓ Branch 47 taken 52 times.
✓ Branch 48 taken 308 times.
✓ Branch 49 taken 33 times.
✓ Branch 50 taken 203 times.
✓ Branch 51 taken 65 times.
1700 AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
148 uint16_t,
149 qsort_comparison_function_int16);
150
151 65 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
152
2/2
✓ Branch 0 taken 541 times.
✓ Branch 1 taken 65 times.
606 while (out < sbr->f_tablelim + sbr->n_lim) {
153 #if USE_FIXED
154
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 22 times.
35 if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
155 #else
156
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 307 times.
506 if (*in >= *out * lim_bands_per_octave_warped) {
157 #endif /* USE_FIXED */
158 212 *++out = *in++;
159
4/4
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 185 times.
✓ Branch 3 taken 98 times.
612 } else if (*in == *out ||
160 283 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
161 231 in++;
162 231 sbr->n_lim--;
163
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 44 times.
98 } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
164 54 *out = *in++;
165 54 sbr->n_lim--;
166 } else {
167 44 *++out = *in++;
168 }
169 }
170 } else {
171 sbr->f_tablelim[0] = sbr->f_tablelow[0];
172 sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
173 sbr->n_lim = 1;
174 }
175 65 }
176
177 489 static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
178 {
179 489 unsigned int cnt = get_bits_count(gb);
180 uint8_t bs_header_extra_1;
181 uint8_t bs_header_extra_2;
182 489 int old_bs_limiter_bands = sbr->bs_limiter_bands;
183 SpectrumParameters old_spectrum_params;
184
185 489 sbr->start = 1;
186 489 sbr->ready_for_dequant = 0;
187
188 // Save last spectrum parameters variables to compare to new ones
189 489 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
190
191 489 sbr->bs_amp_res_header = get_bits1(gb);
192 489 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
193 489 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
194 489 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
195 489 skip_bits(gb, 2); // bs_reserved
196
197 489 bs_header_extra_1 = get_bits1(gb);
198 489 bs_header_extra_2 = get_bits1(gb);
199
200
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 59 times.
489 if (bs_header_extra_1) {
201 430 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
202 430 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
203 430 sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
204 } else {
205 59 sbr->spectrum_params.bs_freq_scale = 2;
206 59 sbr->spectrum_params.bs_alter_scale = 1;
207 59 sbr->spectrum_params.bs_noise_bands = 2;
208 }
209
210 // Check if spectrum parameters changed
211
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 424 times.
489 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
212 65 sbr->reset = 1;
213
214
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 463 times.
489 if (bs_header_extra_2) {
215 26 sbr->bs_limiter_bands = get_bits(gb, 2);
216 26 sbr->bs_limiter_gains = get_bits(gb, 2);
217 26 sbr->bs_interpol_freq = get_bits1(gb);
218 26 sbr->bs_smoothing_mode = get_bits1(gb);
219 } else {
220 463 sbr->bs_limiter_bands = 2;
221 463 sbr->bs_limiter_gains = 2;
222 463 sbr->bs_interpol_freq = 1;
223 463 sbr->bs_smoothing_mode = 1;
224 }
225
226
3/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 424 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
489 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
227 sbr_make_f_tablelim(sbr);
228
229 489 return get_bits_count(gb) - cnt;
230 }
231
232 12 static int array_min_int16(const int16_t *array, int nel)
233 {
234 12 int i, min = array[0];
235
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 for (i = 1; i < nel; i++)
236 16 min = FFMIN(array[i], min);
237 12 return min;
238 }
239
240 65 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
241 {
242 // Requirements (14496-3 sp04 p205)
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (n_master <= 0) {
244 av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
245 return -1;
246 }
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (bs_xover_band >= n_master) {
248 av_log(avctx, AV_LOG_ERROR,
249 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
250 bs_xover_band);
251 return -1;
252 }
253 65 return 0;
254 }
255
256 /// Master Frequency Band Table (14496-3 sp04 p194)
257 65 static int sbr_make_f_master(AACDecContext *ac, SpectralBandReplication *sbr,
258 SpectrumParameters *spectrum)
259 {
260 65 unsigned int temp, max_qmf_subbands = 0;
261 unsigned int start_min, stop_min;
262 int k;
263 const int8_t *sbr_offset_ptr;
264 int16_t stop_dk[13];
265
266
4/7
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
65 switch (sbr->sample_rate) {
267 6 case 16000:
268 6 sbr_offset_ptr = sbr_offset[0];
269 6 break;
270 case 22050:
271 sbr_offset_ptr = sbr_offset[1];
272 break;
273 case 24000:
274 sbr_offset_ptr = sbr_offset[2];
275 break;
276 12 case 32000:
277 12 sbr_offset_ptr = sbr_offset[3];
278 12 break;
279 45 case 44100: case 48000: case 64000:
280 45 sbr_offset_ptr = sbr_offset[4];
281 45 break;
282 2 case 88200: case 96000: case 128000: case 176400: case 192000:
283 2 sbr_offset_ptr = sbr_offset[5];
284 2 break;
285 default:
286 av_log(ac->avctx, AV_LOG_ERROR,
287 "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
288 return -1;
289 }
290
291
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 59 times.
65 if (sbr->sample_rate < 32000) {
292 6 temp = 3000;
293
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2 times.
59 } else if (sbr->sample_rate < 64000) {
294 57 temp = 4000;
295 } else
296 2 temp = 5000;
297
298 65 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
299 65 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
300
301 65 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
302
303
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (spectrum->bs_stop_freq < 14) {
304 65 sbr->k[2] = stop_min;
305 65 make_bands(stop_dk, stop_min, 64, 13);
306
43/44
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 76 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 230 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✓ Branch 9 taken 6 times.
✓ Branch 10 taken 224 times.
✓ Branch 12 taken 12 times.
✓ Branch 13 taken 224 times.
✓ Branch 14 taken 33 times.
✓ Branch 15 taken 203 times.
✓ Branch 16 taken 512 times.
✓ Branch 17 taken 14 times.
✓ Branch 19 taken 168 times.
✓ Branch 20 taken 344 times.
✓ Branch 21 taken 664 times.
✓ Branch 22 taken 43 times.
✓ Branch 24 taken 349 times.
✓ Branch 25 taken 315 times.
✓ Branch 26 taken 306 times.
✓ Branch 27 taken 52 times.
✓ Branch 28 taken 358 times.
✓ Branch 29 taken 203 times.
✓ Branch 30 taken 179 times.
✓ Branch 31 taken 24 times.
✓ Branch 32 taken 129 times.
✓ Branch 33 taken 50 times.
✓ Branch 34 taken 107 times.
✓ Branch 35 taken 22 times.
✓ Branch 36 taken 617 times.
✓ Branch 37 taken 44 times.
✓ Branch 39 taken 504 times.
✓ Branch 40 taken 113 times.
✓ Branch 41 taken 44 times.
✓ Branch 42 taken 113 times.
✓ Branch 43 taken 22 times.
✓ Branch 44 taken 137 times.
✓ Branch 46 taken 56 times.
✓ Branch 47 taken 20 times.
✓ Branch 48 taken 312 times.
✓ Branch 49 taken 71 times.
✓ Branch 50 taken 224 times.
✓ Branch 51 taken 65 times.
2030 AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
307
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 65 times.
725 for (k = 0; k < spectrum->bs_stop_freq; k++)
308 660 sbr->k[2] += stop_dk[k];
309 } else if (spectrum->bs_stop_freq == 14) {
310 sbr->k[2] = 2*sbr->k[0];
311 } else if (spectrum->bs_stop_freq == 15) {
312 sbr->k[2] = 3*sbr->k[0];
313 } else {
314 av_log(ac->avctx, AV_LOG_ERROR,
315 "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
316 return -1;
317 }
318 65 sbr->k[2] = FFMIN(64, sbr->k[2]);
319
320 // Requirements (14496-3 sp04 p205)
321
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 47 times.
65 if (sbr->sample_rate <= 32000) {
322 18 max_qmf_subbands = 48;
323
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 11 times.
47 } else if (sbr->sample_rate == 44100) {
324 36 max_qmf_subbands = 35;
325
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 } else if (sbr->sample_rate >= 48000)
326 11 max_qmf_subbands = 32;
327 else
328 av_assert0(0);
329
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
331 av_log(ac->avctx, AV_LOG_ERROR,
332 "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
333 return -1;
334 }
335
336
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 51 times.
65 if (!spectrum->bs_freq_scale) {
337 int dk, k2diff;
338
339 14 dk = spectrum->bs_alter_scale + 1;
340 14 sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
342 return -1;
343
344
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 14 times.
266 for (k = 1; k <= sbr->n_master; k++)
345 252 sbr->f_master[k] = dk;
346
347 14 k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
348
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (k2diff < 0) {
349 14 sbr->f_master[1]--;
350 14 sbr->f_master[2]-= (k2diff < -1);
351 } else if (k2diff) {
352 sbr->f_master[sbr->n_master]++;
353 }
354
355 14 sbr->f_master[0] = sbr->k[0];
356
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 14 times.
266 for (k = 1; k <= sbr->n_master; k++)
357 252 sbr->f_master[k] += sbr->f_master[k - 1];
358
359 } else {
360 51 int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
361 int two_regions, num_bands_0;
362 int vdk0_max, vdk1_min;
363 int16_t vk0[49];
364 #if USE_FIXED
365 5 int tmp, nz = 0;
366 #endif /* USE_FIXED */
367
368
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 39 times.
51 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
369 12 two_regions = 1;
370 12 sbr->k[1] = 2 * sbr->k[0];
371 } else {
372 39 two_regions = 0;
373 39 sbr->k[1] = sbr->k[2];
374 }
375
376 #if USE_FIXED
377 5 tmp = (sbr->k[1] << 23) / sbr->k[0];
378
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5 times.
35 while (tmp < 0x40000000) {
379 30 tmp <<= 1;
380 30 nz++;
381 }
382 5 tmp = fixed_log(tmp - 0x80000000);
383 5 tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
384 5 tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
385 5 num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
386 #else
387 46 num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
388 #endif /* USE_FIXED */
389
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
391 av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
392 return -1;
393 }
394
395 51 vk0[0] = 0;
396
397 51 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
398
399
43/44
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 46 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 162 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
✓ Branch 9 taken 19 times.
✓ Branch 10 taken 143 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 170 times.
✓ Branch 14 taken 39 times.
✓ Branch 15 taken 135 times.
✓ Branch 16 taken 324 times.
✓ Branch 17 taken 19 times.
✓ Branch 19 taken 107 times.
✓ Branch 20 taken 217 times.
✓ Branch 21 taken 366 times.
✓ Branch 22 taken 34 times.
✓ Branch 24 taken 164 times.
✓ Branch 25 taken 202 times.
✓ Branch 26 taken 185 times.
✓ Branch 27 taken 51 times.
✓ Branch 28 taken 236 times.
✓ Branch 29 taken 135 times.
✓ Branch 30 taken 110 times.
✓ Branch 31 taken 25 times.
✓ Branch 32 taken 71 times.
✓ Branch 33 taken 39 times.
✓ Branch 34 taken 38 times.
✓ Branch 35 taken 33 times.
✓ Branch 36 taken 271 times.
✓ Branch 37 taken 36 times.
✓ Branch 39 taken 230 times.
✓ Branch 40 taken 41 times.
✓ Branch 41 taken 36 times.
✓ Branch 42 taken 41 times.
✓ Branch 43 taken 44 times.
✓ Branch 44 taken 55 times.
✓ Branch 46 taken 5 times.
✓ Branch 47 taken 41 times.
✓ Branch 48 taken 220 times.
✓ Branch 49 taken 29 times.
✓ Branch 50 taken 150 times.
✓ Branch 51 taken 51 times.
1172 AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
400 51 vdk0_max = vk0[num_bands_0];
401
402 51 vk0[0] = sbr->k[0];
403
2/2
✓ Branch 0 taken 560 times.
✓ Branch 1 taken 51 times.
611 for (k = 1; k <= num_bands_0; k++) {
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
405 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
406 return -1;
407 }
408 560 vk0[k] += vk0[k-1];
409 }
410
411
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 39 times.
51 if (two_regions) {
412 int16_t vk1[49];
413 #if USE_FIXED
414 int num_bands_1;
415
416 2 tmp = (sbr->k[2] << 23) / sbr->k[1];
417 2 nz = 0;
418
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 while (tmp < 0x40000000) {
419 14 tmp <<= 1;
420 14 nz++;
421 }
422 2 tmp = fixed_log(tmp - 0x80000000);
423 2 tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
424 2 tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
425
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (spectrum->bs_alter_scale)
426 2 tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
427 2 num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
428 #else
429 20 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
430
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 : 1.0f; // bs_alter_scale = {0,1}
431 10 int num_bands_1 = lrintf(half_bands * invwarp *
432 10 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
433 #endif /* USE_FIXED */
434 12 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
435
436 12 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
437
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (vdk1_min < vdk0_max) {
439 int change;
440 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
441 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
442 vk1[1] += change;
443 vk1[num_bands_1] -= change;
444 }
445
446
24/44
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 2 times.
✗ Branch 22 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 2 times.
✓ Branch 26 taken 1 times.
✓ Branch 27 taken 1 times.
✓ Branch 28 taken 2 times.
✓ Branch 29 taken 2 times.
✓ Branch 30 taken 2 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 2 times.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✓ Branch 36 taken 6 times.
✓ Branch 37 taken 2 times.
✓ Branch 39 taken 6 times.
✗ Branch 40 not taken.
✓ Branch 41 taken 2 times.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 46 not taken.
✓ Branch 47 taken 10 times.
✓ Branch 48 taken 12 times.
✗ Branch 49 not taken.
✓ Branch 50 taken 12 times.
✓ Branch 51 taken 12 times.
34 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
447
448 12 vk1[0] = sbr->k[1];
449
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 12 times.
40 for (k = 1; k <= num_bands_1; k++) {
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
451 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
452 return -1;
453 }
454 28 vk1[k] += vk1[k-1];
455 }
456
457 12 sbr->n_master = num_bands_0 + num_bands_1;
458
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
459 return -1;
460 12 memcpy(&sbr->f_master[0], vk0,
461 12 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
462 12 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
463 num_bands_1 * sizeof(sbr->f_master[0]));
464
465 } else {
466 39 sbr->n_master = num_bands_0;
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
468 return -1;
469 39 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
470 }
471 }
472
473 65 return 0;
474 }
475
476 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
477 65 static int sbr_hf_calc_npatches(AACDecContext *ac, SpectralBandReplication *sbr)
478 {
479 65 int i, k, last_k = -1, last_msb = -1, sb = 0;
480 65 int msb = sbr->k[0];
481 65 int usb = sbr->kx[1];
482 65 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
483
484 65 sbr->num_patches = 0;
485
486
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 34 times.
65 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
487
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 31 times.
334 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
488 } else
489 34 k = sbr->n_master;
490
491 do {
492 186 int odd = 0;
493
3/4
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 100 times.
186 if (k == last_k && msb == last_msb) {
494 av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
495 return AVERROR_INVALIDDATA;
496 }
497 186 last_k = k;
498 186 last_msb = msb;
499
4/4
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 874 times.
✓ Branch 2 taken 688 times.
✓ Branch 3 taken 186 times.
1060 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
500 874 sb = sbr->f_master[i];
501 874 odd = (sb + sbr->k[0]) & 1;
502 }
503
504 // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
505 // After this check the final number of patches can still be six which is
506 // illegal however the Coding Technologies decoder check stream has a final
507 // count of 6 patches
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (sbr->num_patches > 5) {
509 av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
510 return -1;
511 }
512
513 186 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
514 186 sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
515
516
1/2
✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
186 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
517 186 usb = sb;
518 186 msb = sb;
519 186 sbr->num_patches++;
520 } else
521 msb = sbr->kx[1];
522
523
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 100 times.
186 if (sbr->f_master[k] - sb < 3)
524 86 k = sbr->n_master;
525
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 65 times.
186 } while (sb != sbr->kx[1] + sbr->m[1]);
526
527
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2 times.
65 if (sbr->num_patches > 1 &&
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
529 sbr->num_patches--;
530
531 65 return 0;
532 }
533
534 /// Derived Frequency Band Tables (14496-3 sp04 p197)
535 65 static int sbr_make_f_derived(AACDecContext *ac, SpectralBandReplication *sbr)
536 {
537 int k, temp;
538 #if USE_FIXED
539 5 int nz = 0;
540 #endif /* USE_FIXED */
541
542 65 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
543 65 sbr->n[0] = (sbr->n[1] + 1) >> 1;
544
545 65 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
546 65 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
547 65 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
548 65 sbr->kx[1] = sbr->f_tablehigh[0];
549
550 // Requirements (14496-3 sp04 p205)
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (sbr->kx[1] + sbr->m[1] > 64) {
552 av_log(ac->avctx, AV_LOG_ERROR,
553 "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
554 return -1;
555 }
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (sbr->kx[1] > 32) {
557 av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
558 return -1;
559 }
560
561 65 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
562 65 temp = sbr->n[1] & 1;
563
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 65 times.
485 for (k = 1; k <= sbr->n[0]; k++)
564 420 sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
565 #if USE_FIXED
566 5 temp = (sbr->k[2] << 23) / sbr->kx[1];
567
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5 times.
35 while (temp < 0x40000000) {
568 30 temp <<= 1;
569 30 nz++;
570 }
571 5 temp = fixed_log(temp - 0x80000000);
572 5 temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
573 5 temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
574
575 5 sbr->n_q = (temp + 0x400000) >> 23;
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (sbr->n_q < 1)
577 sbr->n_q = 1;
578 #else
579 60 sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
580 log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
581 #endif /* USE_FIXED */
582
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (sbr->n_q > 5) {
584 av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
585 return -1;
586 }
587
588 65 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
589 65 temp = 0;
590
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 65 times.
266 for (k = 1; k <= sbr->n_q; k++) {
591 201 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
592 201 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
593 }
594
595
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (sbr_hf_calc_npatches(ac, sbr) < 0)
596 return -1;
597
598 65 sbr_make_f_tablelim(sbr);
599
600 65 sbr->data[0].f_indexnoise = 0;
601 65 sbr->data[1].f_indexnoise = 0;
602
603 65 return 0;
604 }
605
606 22940 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
607 int elements)
608 {
609 int i;
610
2/2
✓ Branch 0 taken 48555 times.
✓ Branch 1 taken 22940 times.
71495 for (i = 0; i < elements; i++) {
611 48555 vec[i] = get_bits1(gb);
612 }
613 22940 }
614
615 /** ceil(log2(index+1)) */
616 static const int8_t ceil_log2[] = {
617 0, 1, 2, 2, 3, 3,
618 };
619
620 7793 static int read_sbr_grid(AACDecContext *ac, SpectralBandReplication *sbr,
621 GetBitContext *gb, SBRData *ch_data)
622 {
623 int i;
624 7793 int bs_pointer = 0;
625 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
626 7793 int abs_bord_trail = 16;
627 int num_rel_lead, num_rel_trail;
628 7793 unsigned bs_num_env_old = ch_data->bs_num_env;
629 int bs_frame_class, bs_num_env;
630
631 7793 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
632 7793 ch_data->bs_amp_res = sbr->bs_amp_res_header;
633 7793 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
634
635
4/5
✓ Branch 1 taken 6395 times.
✓ Branch 2 taken 630 times.
✓ Branch 3 taken 620 times.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
7793 switch (bs_frame_class = get_bits(gb, 2)) {
636 6395 case FIXFIX:
637 6395 bs_num_env = 1 << get_bits(gb, 2);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6395 times.
6395 if (bs_num_env > 4) {
639 av_log(ac->avctx, AV_LOG_ERROR,
640 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
641 bs_num_env);
642 return -1;
643 }
644 6395 ch_data->bs_num_env = bs_num_env;
645 6395 num_rel_lead = ch_data->bs_num_env - 1;
646
2/2
✓ Branch 0 taken 4929 times.
✓ Branch 1 taken 1466 times.
6395 if (ch_data->bs_num_env == 1)
647 4929 ch_data->bs_amp_res = 0;
648
649
650 6395 ch_data->t_env[0] = 0;
651 6395 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
652
653 6395 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
654 6395 ch_data->bs_num_env;
655
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 6395 times.
7861 for (i = 0; i < num_rel_lead; i++)
656 1466 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
657
658 6395 ch_data->bs_freq_res[1] = get_bits1(gb);
659
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 6395 times.
7861 for (i = 1; i < ch_data->bs_num_env; i++)
660 1466 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
661 6395 break;
662 630 case FIXVAR:
663 630 abs_bord_trail += get_bits(gb, 2);
664 630 num_rel_trail = get_bits(gb, 2);
665 630 ch_data->bs_num_env = num_rel_trail + 1;
666 630 ch_data->t_env[0] = 0;
667 630 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
668
669
2/2
✓ Branch 0 taken 1362 times.
✓ Branch 1 taken 630 times.
1992 for (i = 0; i < num_rel_trail; i++)
670 1362 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
671 1362 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
672
673 630 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
674
675
2/2
✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 630 times.
2622 for (i = 0; i < ch_data->bs_num_env; i++)
676 1992 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
677 630 break;
678 620 case VARFIX:
679 620 ch_data->t_env[0] = get_bits(gb, 2);
680 620 num_rel_lead = get_bits(gb, 2);
681 620 ch_data->bs_num_env = num_rel_lead + 1;
682 620 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
683
684
2/2
✓ Branch 0 taken 776 times.
✓ Branch 1 taken 620 times.
1396 for (i = 0; i < num_rel_lead; i++)
685 776 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
686
687 620 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
688
689 620 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
690 620 break;
691 148 case VARVAR:
692 148 ch_data->t_env[0] = get_bits(gb, 2);
693 148 abs_bord_trail += get_bits(gb, 2);
694 148 num_rel_lead = get_bits(gb, 2);
695 148 num_rel_trail = get_bits(gb, 2);
696 148 bs_num_env = num_rel_lead + num_rel_trail + 1;
697
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
148 if (bs_num_env > 5) {
699 av_log(ac->avctx, AV_LOG_ERROR,
700 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
701 bs_num_env);
702 return -1;
703 }
704 148 ch_data->bs_num_env = bs_num_env;
705
706 148 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
707
708
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 148 times.
220 for (i = 0; i < num_rel_lead; i++)
709 72 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
710
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 148 times.
439 for (i = 0; i < num_rel_trail; i++)
711 291 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
712 291 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
713
714 148 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
715
716 148 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
717 148 break;
718 }
719 7793 ch_data->bs_frame_class = bs_frame_class;
720
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7793 times.
7793 av_assert0(bs_pointer >= 0);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7793 times.
7793 if (bs_pointer > ch_data->bs_num_env + 1) {
723 av_log(ac->avctx, AV_LOG_ERROR,
724 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
725 bs_pointer);
726 return -1;
727 }
728
729
2/2
✓ Branch 0 taken 11760 times.
✓ Branch 1 taken 7793 times.
19553 for (i = 1; i <= ch_data->bs_num_env; i++) {
730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11760 times.
11760 if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
731 av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
732 return -1;
733 }
734 }
735
736
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 4929 times.
7793 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
737
738 7793 ch_data->t_q[0] = ch_data->t_env[0];
739 7793 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
740
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 4929 times.
7793 if (ch_data->bs_num_noise > 1) {
741 int idx;
742
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 1398 times.
2864 if (ch_data->bs_frame_class == FIXFIX) {
743 1466 idx = ch_data->bs_num_env >> 1;
744
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 620 times.
1398 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
745
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 175 times.
778 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
746 } else { // VARFIX
747
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 156 times.
620 if (!bs_pointer)
748 464 idx = 1;
749
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 else if (bs_pointer == 1)
750 156 idx = ch_data->bs_num_env - 1;
751 else // bs_pointer > 1
752 idx = bs_pointer - 1;
753 }
754 2864 ch_data->t_q[1] = ch_data->t_env[idx];
755 }
756
757 7793 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
758 7793 ch_data->e_a[1] = -1;
759
3/4
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 7015 times.
✓ Branch 2 taken 778 times.
✗ Branch 3 not taken.
7793 if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
760 778 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
761
3/4
✓ Branch 0 taken 620 times.
✓ Branch 1 taken 6395 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 620 times.
7015 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
762 ch_data->e_a[1] = bs_pointer - 1;
763
764 7793 return 0;
765 }
766
767 2625 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
768 //These variables are saved from the previous frame rather than copied
769 2625 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
770 2625 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
771 2625 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
772
773 //These variables are read from the bitstream and therefore copied
774 2625 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
775 2625 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
776 2625 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
777 2625 dst->bs_num_env = src->bs_num_env;
778 2625 dst->bs_amp_res = src->bs_amp_res;
779 2625 dst->bs_num_noise = src->bs_num_noise;
780 2625 dst->bs_frame_class = src->bs_frame_class;
781 2625 dst->e_a[1] = src->e_a[1];
782 2625 }
783
784 /// Read how the envelope and noise floor data is delta coded
785 10418 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
786 SBRData *ch_data)
787 {
788 10418 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
789 10418 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
790 10418 }
791
792 /// Read inverse filtering data
793 7793 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
794 SBRData *ch_data)
795 {
796 int i;
797
798 7793 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
799
2/2
✓ Branch 0 taken 23985 times.
✓ Branch 1 taken 7793 times.
31778 for (i = 0; i < sbr->n_q; i++)
800 23985 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
801 7793 }
802
803 10418 static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
804 SBRData *ch_data, int ch)
805 {
806 int bits;
807 int i, j, k;
808 const VLCElem *t_huff, *f_huff;
809
4/4
✓ Branch 0 taken 3886 times.
✓ Branch 1 taken 6532 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 1261 times.
10418 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
810 10418 const int odd = sbr->n[1] & 1;
811
812
4/4
✓ Branch 0 taken 5250 times.
✓ Branch 1 taken 5168 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 2625 times.
10418 if (sbr->bs_coupling && ch) {
813
2/2
✓ Branch 0 taken 582 times.
✓ Branch 1 taken 2043 times.
2625 if (ch_data->bs_amp_res) {
814 582 bits = 5;
815 582 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_BAL_3_0DB];
816 582 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_BAL_3_0DB];
817 } else {
818 2043 bits = 6;
819 2043 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_BAL_1_5DB];
820 2043 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_BAL_1_5DB];
821 }
822 } else {
823
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 4929 times.
7793 if (ch_data->bs_amp_res) {
824 2864 bits = 6;
825 2864 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_3_0DB];
826 2864 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_3_0DB];
827 } else {
828 4929 bits = 7;
829 4929 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_1_5DB];
830 4929 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_1_5DB];
831 }
832 }
833
834
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (i = 0; i < ch_data->bs_num_env; i++) {
835
2/2
✓ Branch 0 taken 4947 times.
✓ Branch 1 taken 10213 times.
15160 if (ch_data->bs_df_env[i]) {
836 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
837
2/2
✓ Branch 0 taken 4454 times.
✓ Branch 1 taken 493 times.
4947 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
838
2/2
✓ Branch 0 taken 51282 times.
✓ Branch 1 taken 4454 times.
55736 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
839 51282 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * get_vlc2(gb, t_huff, 9, 3);
840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51282 times.
51282 if (ch_data->env_facs_q[i + 1][j] > 127U) {
841 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
842 return AVERROR_INVALIDDATA;
843 }
844 }
845
2/2
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 120 times.
493 } else if (ch_data->bs_freq_res[i + 1]) {
846
2/2
✓ Branch 0 taken 4282 times.
✓ Branch 1 taken 373 times.
4655 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
847 4282 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
848 4282 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * get_vlc2(gb, t_huff, 9, 3);
849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4282 times.
4282 if (ch_data->env_facs_q[i + 1][j] > 127U) {
850 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
851 return AVERROR_INVALIDDATA;
852 }
853 }
854 } else {
855
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 120 times.
799 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
856
2/2
✓ Branch 0 taken 559 times.
✓ Branch 1 taken 120 times.
679 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
857 679 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * get_vlc2(gb, t_huff, 9, 3);
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 679 times.
679 if (ch_data->env_facs_q[i + 1][j] > 127U) {
859 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
860 return AVERROR_INVALIDDATA;
861 }
862 }
863 }
864 } else {
865 10213 ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
866
2/2
✓ Branch 0 taken 105460 times.
✓ Branch 1 taken 10213 times.
115673 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
867 105460 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * get_vlc2(gb, f_huff, 9, 3);
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105460 times.
105460 if (ch_data->env_facs_q[i + 1][j] > 127U) {
869 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
870 return AVERROR_INVALIDDATA;
871 }
872 }
873 }
874 }
875
876 //assign 0th elements of env_facs_q from last elements
877 10418 memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
878 sizeof(ch_data->env_facs_q[0]));
879
880 10418 return 0;
881 }
882
883 10418 static int read_sbr_noise(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
884 SBRData *ch_data, int ch)
885 {
886 int i, j;
887 const VLCElem *t_huff, *f_huff;
888
4/4
✓ Branch 0 taken 3886 times.
✓ Branch 1 taken 6532 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 1261 times.
10418 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
889
890
4/4
✓ Branch 0 taken 5250 times.
✓ Branch 1 taken 5168 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 2625 times.
10418 if (sbr->bs_coupling && ch) {
891 2625 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_NOISE_BAL_3_0DB];
892 2625 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_BAL_3_0DB];
893 } else {
894 7793 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_NOISE_3_0DB];
895 7793 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_3_0DB];
896 }
897
898
2/2
✓ Branch 0 taken 13864 times.
✓ Branch 1 taken 10418 times.
24282 for (i = 0; i < ch_data->bs_num_noise; i++) {
899
2/2
✓ Branch 0 taken 10830 times.
✓ Branch 1 taken 3034 times.
13864 if (ch_data->bs_df_noise[i]) {
900
2/2
✓ Branch 0 taken 32863 times.
✓ Branch 1 taken 10830 times.
43693 for (j = 0; j < sbr->n_q; j++) {
901 32863 ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i][j] + delta * get_vlc2(gb, t_huff, 9, 2);
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32863 times.
32863 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
903 av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
904 return AVERROR_INVALIDDATA;
905 }
906 }
907 } else {
908 3034 ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
909
2/2
✓ Branch 0 taken 5910 times.
✓ Branch 1 taken 3034 times.
8944 for (j = 1; j < sbr->n_q; j++) {
910 5910 ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i + 1][j - 1] + delta * get_vlc2(gb, f_huff, 9, 3);
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5910 times.
5910 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
912 av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
913 return AVERROR_INVALIDDATA;
914 }
915 }
916 }
917 }
918
919 //assign 0th elements of noise_facs_q from last elements
920 10418 memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
921 sizeof(ch_data->noise_facs_q[0]));
922 10418 return 0;
923 }
924
925 1616 static void read_sbr_extension(AACDecContext *ac, SpectralBandReplication *sbr,
926 GetBitContext *gb,
927 int bs_extension_id, int *num_bits_left)
928 {
929
1/2
✓ Branch 0 taken 1616 times.
✗ Branch 1 not taken.
1616 switch (bs_extension_id) {
930 1616 case EXTENSION_ID_PS:
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1616 times.
1616 if (!ac->oc[1].m4ac.ps) {
932 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
933 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
934 *num_bits_left = 0;
935 } else {
936 1616 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps.common, *num_bits_left);
937 1616 ac->avctx->profile = AV_PROFILE_AAC_HE_V2;
938 // ensure the warning is not printed if PS extension is present
939 1616 ac->warned_he_aac_mono = 1;
940 }
941 1616 break;
942 default:
943 // some files contain 0-padding
944 if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
945 avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
946 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
947 *num_bits_left = 0;
948 break;
949 }
950 1616 }
951
952 2646 static int read_sbr_single_channel_element(AACDecContext *ac,
953 SpectralBandReplication *sbr,
954 GetBitContext *gb)
955 {
956 int ret;
957
958
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (get_bits1(gb)) // bs_data_extra
959 skip_bits(gb, 4); // bs_reserved
960
961
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
962 return -1;
963 2646 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
964 2646 read_sbr_invf(sbr, gb, &sbr->data[0]);
965
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
966 return ret;
967
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
968 return ret;
969
970
2/2
✓ Branch 1 taken 174 times.
✓ Branch 2 taken 2472 times.
2646 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
971 174 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
972
973 2646 return 0;
974 }
975
976 3886 static int read_sbr_channel_pair_element(AACDecContext *ac,
977 SpectralBandReplication *sbr,
978 GetBitContext *gb)
979 {
980 int ret;
981
982
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3886 times.
3886 if (get_bits1(gb)) // bs_data_extra
983 skip_bits(gb, 8); // bs_reserved
984
985
2/2
✓ Branch 1 taken 2625 times.
✓ Branch 2 taken 1261 times.
3886 if ((sbr->bs_coupling = get_bits1(gb))) {
986
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
2625 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
987 return -1;
988 2625 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
989 2625 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
990 2625 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
991 2625 read_sbr_invf(sbr, gb, &sbr->data[0]);
992 2625 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
993 2625 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
994
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
2625 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
995 return ret;
996
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
2625 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
997 return ret;
998
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
2625 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
999 return ret;
1000
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
2625 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1001 return ret;
1002 } else {
1003
2/4
✓ Branch 1 taken 1261 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1261 times.
2522 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
1004 1261 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1005 return -1;
1006 1261 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
1007 1261 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
1008 1261 read_sbr_invf(sbr, gb, &sbr->data[0]);
1009 1261 read_sbr_invf(sbr, gb, &sbr->data[1]);
1010
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1011 return ret;
1012
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1013 return ret;
1014
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1015 return ret;
1016
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1017 return ret;
1018 }
1019
1020
2/2
✓ Branch 1 taken 698 times.
✓ Branch 2 taken 3188 times.
3886 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1021 698 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1022
2/2
✓ Branch 1 taken 464 times.
✓ Branch 2 taken 3422 times.
3886 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1023 464 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1024
1025 3886 return 0;
1026 }
1027
1028 6532 static unsigned int read_sbr_data(AACDecContext *ac, SpectralBandReplication *sbr,
1029 GetBitContext *gb, int id_aac)
1030 {
1031 6532 unsigned int cnt = get_bits_count(gb);
1032
1033 6532 sbr->id_aac = id_aac;
1034 6532 sbr->ready_for_dequant = 1;
1035
1036
3/4
✓ Branch 0 taken 3886 times.
✓ Branch 1 taken 2646 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3886 times.
6532 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1037
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (read_sbr_single_channel_element(ac, sbr, gb)) {
1038 sbr_turnoff(sbr);
1039 return get_bits_count(gb) - cnt;
1040 }
1041
1/2
✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
3886 } else if (id_aac == TYPE_CPE) {
1042
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3886 times.
3886 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1043 sbr_turnoff(sbr);
1044 return get_bits_count(gb) - cnt;
1045 }
1046 } else {
1047 av_log(ac->avctx, AV_LOG_ERROR,
1048 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1049 sbr_turnoff(sbr);
1050 return get_bits_count(gb) - cnt;
1051 }
1052
2/2
✓ Branch 1 taken 1616 times.
✓ Branch 2 taken 4916 times.
6532 if (get_bits1(gb)) { // bs_extended_data
1053 1616 int num_bits_left = get_bits(gb, 4); // bs_extension_size
1054
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 1358 times.
1616 if (num_bits_left == 15)
1055 258 num_bits_left += get_bits(gb, 8); // bs_esc_count
1056
1057 1616 num_bits_left <<= 3;
1058
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 1616 times.
3232 while (num_bits_left > 7) {
1059 1616 num_bits_left -= 2;
1060 1616 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1061 }
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1616 times.
1616 if (num_bits_left < 0) {
1063 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1064 }
1065
2/2
✓ Branch 0 taken 1451 times.
✓ Branch 1 taken 165 times.
1616 if (num_bits_left > 0)
1066 1451 skip_bits(gb, num_bits_left);
1067 }
1068
1069 6532 return get_bits_count(gb) - cnt;
1070 }
1071
1072 65 static void sbr_reset(AACDecContext *ac, SpectralBandReplication *sbr)
1073 {
1074 int err;
1075 65 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1076
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (err >= 0)
1077 65 err = sbr_make_f_derived(ac, sbr);
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (err < 0) {
1079 av_log(ac->avctx, AV_LOG_ERROR,
1080 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1081 sbr_turnoff(sbr);
1082 }
1083 65 }
1084
1085 /**
1086 * Decode Spectral Band Replication extension data; reference: table 4.55.
1087 *
1088 * @param crc flag indicating the presence of CRC checksum
1089 * @param cnt length of TYPE_FIL syntactic element in bytes
1090 *
1091 * @return Returns number of bytes consumed from the TYPE_FIL element.
1092 */
1093 10901 int AAC_RENAME(ff_decode_sbr_extension)(AACDecContext *ac, SpectralBandReplication *sbr,
1094 GetBitContext *gb_host, int crc, int cnt, int id_aac)
1095 {
1096 10901 unsigned int num_sbr_bits = 0, num_align_bits;
1097 unsigned bytes_read;
1098 10901 GetBitContext gbc = *gb_host, *gb = &gbc;
1099 10901 skip_bits_long(gb_host, cnt*8 - 4);
1100
1101 10901 sbr->reset = 0;
1102
1103
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 10819 times.
10901 if (!sbr->sample_rate)
1104 82 sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1105
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10883 times.
10901 if (!ac->oc[1].m4ac.ext_sample_rate)
1106 18 ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1107
1108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 if (crc) {
1109 skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1110 num_sbr_bits += 10;
1111 }
1112
1113 //Save some state from the previous frame.
1114 10901 sbr->kx[0] = sbr->kx[1];
1115 10901 sbr->m[0] = sbr->m[1];
1116 10901 sbr->kx_and_m_pushed = 1;
1117
1118 10901 num_sbr_bits++;
1119
2/2
✓ Branch 1 taken 489 times.
✓ Branch 2 taken 10412 times.
10901 if (get_bits1(gb)) // bs_header_flag
1120 489 num_sbr_bits += read_sbr_header(sbr, gb);
1121
1122
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 10836 times.
10901 if (sbr->reset)
1123 65 sbr_reset(ac, sbr);
1124
1125
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 4369 times.
10901 if (sbr->start)
1126 6532 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1127
1128 10901 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1129 10901 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1130
1131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 if (bytes_read > cnt) {
1132 av_log(ac->avctx, AV_LOG_ERROR,
1133 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1134 sbr_turnoff(sbr);
1135 }
1136 10901 return cnt;
1137 }
1138
1139 /**
1140 * Analysis QMF Bank (14496-3 sp04 p206)
1141 *
1142 * @param x pointer to the beginning of the first sample window
1143 * @param W array of complex-valued samples split into subbands
1144 */
1145 #ifndef sbr_qmf_analysis
1146 #if USE_FIXED
1147 8328 static void sbr_qmf_analysis(AVFixedDSPContext *dsp, AVTXContext *mdct,
1148 av_tx_fn mdct_fn,
1149 #else
1150 11409 static void sbr_qmf_analysis(AVFloatDSPContext *dsp, AVTXContext *mdct,
1151 av_tx_fn mdct_fn,
1152 #endif /* USE_FIXED */
1153 SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
1154 INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
1155 {
1156 int i;
1157 #if USE_FIXED
1158 int j;
1159 #endif
1160 19737 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1161 19737 memcpy(x+288, in, 1024*sizeof(x[0]));
1162
2/2
✓ Branch 0 taken 631584 times.
✓ Branch 1 taken 19737 times.
651321 for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1163 // are not supported
1164 631584 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1165 631584 sbrdsp->sum64x5(z);
1166 631584 sbrdsp->qmf_pre_shuffle(z);
1167 #if USE_FIXED
1168
2/2
✓ Branch 0 taken 17055744 times.
✓ Branch 1 taken 266496 times.
17322240 for (j = 64; j < 128; j++) {
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17055744 times.
17055744 if (z[j] > 1<<24) {
1170 av_log(NULL, AV_LOG_WARNING,
1171 "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
1172 z[j], 1<<24);
1173 z[j] = 1<<24;
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17055744 times.
17055744 } else if (z[j] < -(1<<24)) {
1175 av_log(NULL, AV_LOG_WARNING,
1176 "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
1177 z[j], -(1<<24));
1178 z[j] = -(1<<24);
1179 }
1180 }
1181 #endif
1182 631584 mdct_fn(mdct, z, z + 64, sizeof(INTFLOAT));
1183 631584 sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1184 631584 x += 32;
1185 }
1186 19737 }
1187 #endif
1188
1189 /**
1190 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1191 * (14496-3 sp04 p206)
1192 */
1193 #ifndef sbr_qmf_synthesis
1194 21736 static void sbr_qmf_synthesis(AVTXContext *mdct, av_tx_fn mdct_fn,
1195 #if USE_FIXED
1196 SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
1197 #else
1198 SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1199 #endif /* USE_FIXED */
1200 INTFLOAT *out, INTFLOAT X[2][38][64],
1201 INTFLOAT mdct_buf[2][64],
1202 INTFLOAT *v0, int *v_off, const unsigned int div)
1203 {
1204 int i, n;
1205
2/2
✓ Branch 0 taken 4596 times.
✓ Branch 1 taken 17140 times.
21736 const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1206 21736 const int step = 128 >> div;
1207 INTFLOAT *v;
1208
2/2
✓ Branch 0 taken 695552 times.
✓ Branch 1 taken 21736 times.
717288 for (i = 0; i < 32; i++) {
1209
2/2
✓ Branch 0 taken 66284 times.
✓ Branch 1 taken 629268 times.
695552 if (*v_off < step) {
1210 66284 int saved_samples = (1280 - 128) >> div;
1211 66284 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
1212 66284 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1213 } else {
1214 629268 *v_off -= step;
1215 }
1216 695552 v = v0 + *v_off;
1217
2/2
✓ Branch 0 taken 147072 times.
✓ Branch 1 taken 548480 times.
695552 if (div) {
1218
2/2
✓ Branch 0 taken 4706304 times.
✓ Branch 1 taken 147072 times.
4853376 for (n = 0; n < 32; n++) {
1219 4706304 X[0][i][ n] = -X[0][i][n];
1220 4706304 X[0][i][32+n] = X[1][i][31-n];
1221 }
1222 147072 mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1223 147072 sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1224 } else {
1225 548480 sbrdsp->neg_odd_64(X[1][i]);
1226 548480 mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1227 548480 mdct_fn(mdct, mdct_buf[1], X[1][i], sizeof(INTFLOAT));
1228 548480 sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1229 }
1230 695552 dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1231 695552 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1232 695552 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1233 695552 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1234 695552 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1235 695552 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1236 695552 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1237 695552 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1238 695552 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1239 695552 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1240 695552 out += 64 >> div;
1241 }
1242 21736 }
1243 #endif
1244
1245 /// Generate the subband filtered lowband
1246 19737 static int sbr_lf_gen(SpectralBandReplication *sbr,
1247 INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
1248 int buf_idx)
1249 {
1250 int i, k;
1251 19737 const int t_HFGen = 8;
1252 19737 const int i_f = 32;
1253 19737 memset(X_low, 0, 32*sizeof(*X_low));
1254
2/2
✓ Branch 0 taken 490630 times.
✓ Branch 1 taken 19737 times.
510367 for (k = 0; k < sbr->kx[1]; k++) {
1255
2/2
✓ Branch 0 taken 15700160 times.
✓ Branch 1 taken 490630 times.
16190790 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1256 15700160 X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1257 15700160 X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1258 }
1259 }
1260 19737 buf_idx = 1-buf_idx;
1261
2/2
✓ Branch 0 taken 491470 times.
✓ Branch 1 taken 19737 times.
511207 for (k = 0; k < sbr->kx[0]; k++) {
1262
2/2
✓ Branch 0 taken 3931760 times.
✓ Branch 1 taken 491470 times.
4423230 for (i = 0; i < t_HFGen; i++) {
1263 3931760 X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1264 3931760 X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1265 }
1266 }
1267 19737 return 0;
1268 }
1269
1270 /// High Frequency Generator (14496-3 sp04 p215)
1271 10418 static int sbr_hf_gen(AACDecContext *ac, SpectralBandReplication *sbr,
1272 INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
1273 const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
1274 const INTFLOAT bw_array[5], const uint8_t *t_env,
1275 int bs_num_env)
1276 {
1277 int j, x;
1278 10418 int g = 0;
1279 10418 int k = sbr->kx[1];
1280
2/2
✓ Branch 0 taken 26191 times.
✓ Branch 1 taken 10418 times.
36609 for (j = 0; j < sbr->num_patches; j++) {
1281
2/2
✓ Branch 0 taken 256742 times.
✓ Branch 1 taken 26191 times.
282933 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1282 256742 const int p = sbr->patch_start_subband[j] + x;
1283
3/4
✓ Branch 0 taken 533838 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 277096 times.
✓ Branch 3 taken 256742 times.
533838 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1284 277096 g++;
1285 256742 g--;
1286
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256742 times.
256742 if (g < 0) {
1288 av_log(ac->avctx, AV_LOG_ERROR,
1289 "ERROR : no subband found for frequency %d\n", k);
1290 return -1;
1291 }
1292
1293 256742 sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1294 256742 X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1295 256742 alpha0[p], alpha1[p], bw_array[g],
1296 256742 2 * t_env[0], 2 * t_env[bs_num_env]);
1297 }
1298 }
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 if (k < sbr->m[1] + sbr->kx[1])
1300 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1301
1302 10418 return 0;
1303 }
1304
1305 /// Generate the subband filtered lowband
1306 19737 static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
1307 const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
1308 const INTFLOAT X_low[32][40][2], int ch)
1309 {
1310 int k, i;
1311 19737 const int i_f = 32;
1312 19737 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1313 19737 memset(X, 0, 2*sizeof(*X));
1314
2/2
✓ Branch 0 taken 491470 times.
✓ Branch 1 taken 19737 times.
511207 for (k = 0; k < sbr->kx[0]; k++) {
1315
2/2
✓ Branch 0 taken 55730 times.
✓ Branch 1 taken 491470 times.
547200 for (i = 0; i < i_Temp; i++) {
1316 55730 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1317 55730 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1318 }
1319 }
1320
2/2
✓ Branch 0 taken 254146 times.
✓ Branch 1 taken 19737 times.
273883 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1321
2/2
✓ Branch 0 taken 68266 times.
✓ Branch 1 taken 254146 times.
322412 for (i = 0; i < i_Temp; i++) {
1322 68266 X[0][i][k] = Y0[i + i_f][k][0];
1323 68266 X[1][i][k] = Y0[i + i_f][k][1];
1324 }
1325 }
1326
1327
2/2
✓ Branch 0 taken 490630 times.
✓ Branch 1 taken 19737 times.
510367 for (k = 0; k < sbr->kx[1]; k++) {
1328
2/2
✓ Branch 0 taken 18588210 times.
✓ Branch 1 taken 490630 times.
19078840 for (i = i_Temp; i < 38; i++) {
1329 18588210 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1330 18588210 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1331 }
1332 }
1333
2/2
✓ Branch 0 taken 256742 times.
✓ Branch 1 taken 19737 times.
276479 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1334
2/2
✓ Branch 0 taken 8147478 times.
✓ Branch 1 taken 256742 times.
8404220 for (i = i_Temp; i < i_f; i++) {
1335 8147478 X[0][i][k] = Y1[i][k][0];
1336 8147478 X[1][i][k] = Y1[i][k][1];
1337 }
1338 }
1339 19737 return 0;
1340 }
1341
1342 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1343 * (14496-3 sp04 p217)
1344 */
1345 10418 static int sbr_mapping(AACDecContext *ac, SpectralBandReplication *sbr,
1346 SBRData *ch_data, int e_a[2])
1347 {
1348 int e, i, m;
1349
1350 10418 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1351
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (e = 0; e < ch_data->bs_num_env; e++) {
1352 15160 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1353
2/2
✓ Branch 0 taken 13386 times.
✓ Branch 1 taken 1774 times.
15160 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1354 int k;
1355
1356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15160 times.
15160 if (sbr->kx[1] != table[0]) {
1357 av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1358 "Derived frequency tables were not regenerated.\n");
1359 sbr_turnoff(sbr);
1360 return AVERROR_BUG;
1361 }
1362
2/2
✓ Branch 0 taken 171916 times.
✓ Branch 1 taken 15160 times.
187076 for (i = 0; i < ilim; i++)
1363
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 171916 times.
550938 for (m = table[i]; m < table[i + 1]; m++)
1364 379022 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1365
1366 // ch_data->bs_num_noise > 1 => 2 noise floors
1367
4/4
✓ Branch 0 taken 8188 times.
✓ Branch 1 taken 6972 times.
✓ Branch 2 taken 4354 times.
✓ Branch 3 taken 3834 times.
15160 k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1368
2/2
✓ Branch 0 taken 45584 times.
✓ Branch 1 taken 15160 times.
60744 for (i = 0; i < sbr->n_q; i++)
1369
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 45584 times.
424606 for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1370 379022 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1371
1372
2/2
✓ Branch 0 taken 182216 times.
✓ Branch 1 taken 15160 times.
197376 for (i = 0; i < sbr->n[1]; i++) {
1373
2/2
✓ Branch 0 taken 32694 times.
✓ Branch 1 taken 149522 times.
182216 if (ch_data->bs_add_harmonic_flag) {
1374 32694 const unsigned int m_midpoint =
1375 32694 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1376
1377 32694 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1378
4/4
✓ Branch 0 taken 4522 times.
✓ Branch 1 taken 28172 times.
✓ Branch 2 taken 500 times.
✓ Branch 3 taken 4022 times.
32694 (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1379 }
1380 }
1381
1382
2/2
✓ Branch 0 taken 171916 times.
✓ Branch 1 taken 15160 times.
187076 for (i = 0; i < ilim; i++) {
1383 171916 int additional_sinusoid_present = 0;
1384
2/2
✓ Branch 0 taken 376296 times.
✓ Branch 1 taken 168186 times.
544482 for (m = table[i]; m < table[i + 1]; m++) {
1385
2/2
✓ Branch 0 taken 3730 times.
✓ Branch 1 taken 372566 times.
376296 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1386 3730 additional_sinusoid_present = 1;
1387 3730 break;
1388 }
1389 }
1390 171916 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1391 171916 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1392 }
1393 }
1394
1395 10418 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1396 10418 return 0;
1397 }
1398
1399 /// Estimation of current envelope (14496-3 sp04 p218)
1400 10418 static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
1401 SpectralBandReplication *sbr, SBRData *ch_data)
1402 {
1403 int e, m;
1404 10418 int kx1 = sbr->kx[1];
1405
1406
1/2
✓ Branch 0 taken 10418 times.
✗ Branch 1 not taken.
10418 if (sbr->bs_interpol_freq) {
1407
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (e = 0; e < ch_data->bs_num_env; e++) {
1408 #if USE_FIXED
1409 5588 const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
1410 #else
1411 9572 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1412 #endif /* USE_FIXED */
1413 15160 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1414 15160 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1415
1416
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 15160 times.
394182 for (m = 0; m < sbr->m[1]; m++) {
1417 379022 AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1418 #if USE_FIXED
1419 127837 e_curr[e][m] = av_mul_sf(sum, recip_env_size);
1420 #else
1421 251185 e_curr[e][m] = sum * recip_env_size;
1422 #endif /* USE_FIXED */
1423 }
1424 }
1425 } else {
1426 int k, p;
1427
1428 for (e = 0; e < ch_data->bs_num_env; e++) {
1429 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1430 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1431 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1432 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1433
1434 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1435 #if USE_FIXED
1436 SoftFloat sum = FLOAT_0;
1437 const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
1438 for (k = table[p]; k < table[p + 1]; k++) {
1439 sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
1440 }
1441 sum = av_mul_sf(sum, den);
1442 #else
1443 float sum = 0.0f;
1444 const int den = env_size * (table[p + 1] - table[p]);
1445
1446 for (k = table[p]; k < table[p + 1]; k++) {
1447 sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1448 }
1449 sum /= den;
1450 #endif /* USE_FIXED */
1451 for (k = table[p]; k < table[p + 1]; k++) {
1452 e_curr[e][k - kx1] = sum;
1453 }
1454 }
1455 }
1456 }
1457 10418 }
1458
1459 12411 void AAC_RENAME(ff_sbr_apply)(AACDecContext *ac, SpectralBandReplication *sbr, int id_aac,
1460 INTFLOAT* L, INTFLOAT* R)
1461 {
1462 12411 int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1463 int ch;
1464
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 5085 times.
12411 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1465 int err;
1466
1467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12411 times.
12411 if (id_aac != sbr->id_aac) {
1468 av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
1469 "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
1470 sbr_turnoff(sbr);
1471 }
1472
1473
3/4
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 5879 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6532 times.
12411 if (sbr->start && !sbr->ready_for_dequant) {
1474 av_log(ac->avctx, AV_LOG_ERROR,
1475 "No quantized data read for sbr_dequant.\n");
1476 sbr_turnoff(sbr);
1477 }
1478
1479
2/2
✓ Branch 0 taken 1510 times.
✓ Branch 1 taken 10901 times.
12411 if (!sbr->kx_and_m_pushed) {
1480 1510 sbr->kx[0] = sbr->kx[1];
1481 1510 sbr->m[0] = sbr->m[1];
1482 } else {
1483 10901 sbr->kx_and_m_pushed = 0;
1484 }
1485
1486
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 5879 times.
12411 if (sbr->start) {
1487 6532 sbr_dequant(sbr, id_aac);
1488 6532 sbr->ready_for_dequant = 0;
1489 }
1490
2/2
✓ Branch 0 taken 19737 times.
✓ Branch 1 taken 12411 times.
32148 for (ch = 0; ch < nch; ch++) {
1491 /* decode channel */
1492 19737 sbr_qmf_analysis(ac->fdsp, sbr->mdct_ana, sbr->mdct_ana_fn, &sbr->dsp,
1493 19737 ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1494 19737 (INTFLOAT*)sbr->qmf_filter_scratch,
1495
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 12411 times.
19737 sbr->data[ch].W, sbr->data[ch].Ypos);
1496 19737 sbr->c.sbr_lf_gen(sbr, sbr->X_low,
1497 19737 (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
1498 sbr->data[ch].Ypos);
1499 19737 sbr->data[ch].Ypos ^= 1;
1500
2/2
✓ Branch 0 taken 10418 times.
✓ Branch 1 taken 9319 times.
19737 if (sbr->start) {
1501 10418 sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1502 10418 (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
1503 10418 sbr_chirp(sbr, &sbr->data[ch]);
1504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 av_assert0(sbr->data[ch].bs_num_env > 0);
1505 10418 sbr_hf_gen(ac, sbr, sbr->X_high,
1506 10418 (const INTFLOAT (*)[40][2]) sbr->X_low,
1507 10418 (const INTFLOAT (*)[2]) sbr->alpha0,
1508 10418 (const INTFLOAT (*)[2]) sbr->alpha1,
1509 10418 sbr->data[ch].bw_array, sbr->data[ch].t_env,
1510 6631 sbr->data[ch].bs_num_env);
1511
1512 // hf_adj
1513 10418 err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1514
1/2
✓ Branch 0 taken 10418 times.
✗ Branch 1 not taken.
10418 if (!err) {
1515 10418 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1516 10418 sbr_gain_calc(sbr, &sbr->data[ch], sbr->data[ch].e_a);
1517 10418 sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1518 10418 (const INTFLOAT (*)[40][2]) sbr->X_high,
1519 sbr, &sbr->data[ch],
1520 10418 sbr->data[ch].e_a);
1521 }
1522 }
1523
1524 /* synthesis */
1525 19737 sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1526 19737 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1527 19737 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1528 19737 (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
1529 }
1530
1531
2/2
✓ Branch 0 taken 1999 times.
✓ Branch 1 taken 10412 times.
12411 if (ac->oc[1].m4ac.ps == 1) {
1532
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 383 times.
1999 if (sbr->ps.common.start) {
1533 1616 AAC_RENAME(ff_ps_apply)(&sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1534 } else {
1535 383 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1536 }
1537 1999 nch = 2;
1538 }
1539
1540 12411 sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1541 12411 L, sbr->X[0], sbr->qmf_filter_scratch,
1542 12411 sbr->data[0].synthesis_filterbank_samples,
1543 &sbr->data[0].synthesis_filterbank_samples_offset,
1544 downsampled);
1545
2/2
✓ Branch 0 taken 9325 times.
✓ Branch 1 taken 3086 times.
12411 if (nch == 2)
1546 9325 sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1547 9325 R, sbr->X[1], sbr->qmf_filter_scratch,
1548 9325 sbr->data[1].synthesis_filterbank_samples,
1549 &sbr->data[1].synthesis_filterbank_samples_offset,
1550 downsampled);
1551 12411 }
1552
1553 369 static void aacsbr_func_ptr_init(AACSBRContext *c)
1554 {
1555 369 c->sbr_lf_gen = sbr_lf_gen;
1556 369 c->sbr_hf_assemble = sbr_hf_assemble;
1557 369 c->sbr_x_gen = sbr_x_gen;
1558 369 c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
1559
1560 #if !USE_FIXED
1561 #if ARCH_MIPS
1562 ff_aacsbr_func_ptr_init_mips(c);
1563 #endif
1564 #endif
1565 369 }
1566