FFmpeg coverage


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