FFmpeg coverage


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