FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacsbr_template.c
Date: 2023-03-22 23:59:29
Exec Total Coverage
Lines: 778 933 83.4%
Functions: 41 41 100.0%
Branches: 531 712 74.6%

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