FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacsbr_template.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 768 1038 74.0%
Functions: 41 44 93.2%
Branches: 535 788 67.9%

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 23688 static inline SpectralBandReplication *get_sbr(ChannelElement *ch)
48 {
49 23688 return &((ExtChannelElement*)ch)->sbr;
50 }
51
52 183 av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
53 {
54 183 AAC_RENAME(ff_ps_init)();
55 183 }
56
57 /** Places SBR in pure upsampling mode. */
58 376 static void sbr_turnoff(SpectralBandReplication *sbr) {
59 376 sbr->start = 0;
60 376 sbr->usac = 0;
61 376 sbr->ready_for_dequant = 0;
62 // Init defults used in pure upsampling mode
63 376 sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
64 376 sbr->m[1] = 0;
65 // Reset values for first SBR header
66 376 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
67 376 memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
68 376 }
69
70 376 av_cold int AAC_RENAME(ff_aac_sbr_ctx_alloc_init)(AACDecContext *ac,
71 ChannelElement **che, int id_aac)
72 {
73 SpectralBandReplication *sbr;
74 376 ExtChannelElement *ext = av_mallocz(sizeof(*ext));
75 int ret;
76 float scale;
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 376 times.
376 if (!ext)
79 return AVERROR(ENOMEM);
80 376 *che = &ext->ch;
81 376 sbr = &ext->sbr;
82 376 ext->ch.ch[0].AAC_RENAME(predictor_state) = ext->predictor_state[0];
83 376 ext->ch.ch[1].AAC_RENAME(predictor_state) = ext->predictor_state[1];
84
85 376 sbr->kx[0] = sbr->kx[1];
86 376 sbr->id_aac = id_aac;
87 376 sbr_turnoff(sbr);
88 376 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
89 376 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 376 scale = USE_FIXED ? 1 : 1.0 / (64 * 32768);
95 376 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 376 times.
376 if (ret < 0)
99 return ret;
100
101 376 scale = USE_FIXED ? -1.0 : -2.0 * 32768;
102 376 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 376 times.
376 if (ret < 0)
106 return ret;
107
108 376 AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
109 376 AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
110 376 aacsbr_func_ptr_init(&sbr->c);
111
112 376 return 0;
113 }
114
115 376 av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(ChannelElement *che)
116 {
117 376 SpectralBandReplication *sbr = get_sbr(che);
118 376 av_tx_uninit(&sbr->mdct);
119 376 av_tx_uninit(&sbr->mdct_ana);
120 376 }
121
122 6109 static int qsort_comparison_function_int16(const void *a, const void *b)
123 {
124 6109 return *(const int16_t *)a - *(const int16_t *)b;
125 }
126
127 381 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 1532 times.
✓ Branch 1 taken 239 times.
1771 for (i = 0; i <= last_el; i++)
131
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 1390 times.
1532 if (table[i] == needle)
132 142 return 1;
133 239 return 0;
134 }
135
136 /// Limiter Frequency Band Table (14496-3 sp04 p198)
137 65 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
138 {
139 int k;
140
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 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 65 const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
145 int16_t patch_borders[7];
146 65 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
147
148 65 patch_borders[0] = sbr->kx[1];
149
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 65 times.
251 for (k = 1; k <= sbr->num_patches; k++)
150 186 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
151
152 65 memcpy(sbr->f_tablelim, sbr->f_tablelow,
153 65 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
154
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2 times.
65 if (sbr->num_patches > 1)
155 63 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
156 63 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
157
158
41/44
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 97 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 192 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 192 times.
✓ Branch 12 taken 83 times.
✓ Branch 13 taken 128 times.
✓ Branch 14 taken 51 times.
✓ Branch 15 taken 160 times.
✓ Branch 16 taken 456 times.
✓ Branch 17 taken 16 times.
✓ Branch 19 taken 284 times.
✓ Branch 20 taken 172 times.
✓ Branch 21 taken 340 times.
✓ Branch 22 taken 132 times.
✓ Branch 24 taken 284 times.
✓ Branch 25 taken 56 times.
✓ Branch 26 taken 132 times.
✓ Branch 27 taken 56 times.
✓ Branch 28 taken 188 times.
✓ Branch 29 taken 160 times.
✓ Branch 30 taken 109 times.
✓ Branch 31 taken 51 times.
✓ Branch 32 taken 109 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 95 times.
✓ Branch 35 taken 14 times.
✓ Branch 36 taken 451 times.
✓ Branch 37 taken 22 times.
✓ Branch 39 taken 378 times.
✓ Branch 40 taken 73 times.
✓ Branch 41 taken 22 times.
✓ Branch 42 taken 73 times.
✓ Branch 43 taken 30 times.
✓ Branch 44 taken 108 times.
✓ Branch 46 taken 45 times.
✓ Branch 47 taken 52 times.
✓ Branch 48 taken 308 times.
✓ Branch 49 taken 33 times.
✓ Branch 50 taken 203 times.
✓ Branch 51 taken 65 times.
1700 AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
159 uint16_t,
160 qsort_comparison_function_int16);
161
162 65 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
163
2/2
✓ Branch 0 taken 541 times.
✓ Branch 1 taken 65 times.
606 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 199 times.
✓ Branch 1 taken 307 times.
506 if (*in >= *out * lim_bands_per_octave_warped) {
168 #endif /* USE_FIXED */
169 212 *++out = *in++;
170
4/4
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 185 times.
✓ Branch 3 taken 98 times.
612 } else if (*in == *out ||
171 283 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
172 231 in++;
173 231 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 65 }
187
188 489 static unsigned int read_sbr_header(SpectralBandReplication *sbr,
189 GetBitContext *gb, int is_usac)
190 {
191 489 unsigned int cnt = get_bits_count(gb);
192 uint8_t bs_header_extra_1;
193 uint8_t bs_header_extra_2;
194 489 int old_bs_limiter_bands = sbr->bs_limiter_bands;
195 SpectrumParameters old_spectrum_params;
196
197 489 sbr->start = 1;
198 489 sbr->ready_for_dequant = 0;
199 489 sbr->usac = is_usac;
200
201 // Save last spectrum parameters variables to compare to new ones
202 489 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
203
204
1/2
✓ Branch 0 taken 489 times.
✗ Branch 1 not taken.
489 if (!is_usac)
205 489 sbr->bs_amp_res_header = get_bits1(gb);
206
207 489 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
208 489 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
209
210
1/2
✓ Branch 0 taken 489 times.
✗ Branch 1 not taken.
489 if (!is_usac)
211 489 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
212 489 skip_bits(gb, 2); // bs_reserved
213
214 489 bs_header_extra_1 = get_bits1(gb);
215 489 bs_header_extra_2 = get_bits1(gb);
216
217
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 59 times.
489 if (bs_header_extra_1) {
218 430 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
219 430 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
220 430 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 65 times.
✓ Branch 1 taken 424 times.
489 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
229 65 sbr->reset = 1;
230
231
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 463 times.
489 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 463 sbr->bs_limiter_bands = 2;
238 463 sbr->bs_limiter_gains = 2;
239 463 sbr->bs_interpol_freq = 1;
240 463 sbr->bs_smoothing_mode = 1;
241 }
242
243
3/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 424 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
489 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
244 sbr_make_f_tablelim(sbr);
245
246 489 return get_bits_count(gb) - cnt;
247 }
248
249 12 static int array_min_int16(const int16_t *array, int nel)
250 {
251 12 int i, min = array[0];
252
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 for (i = 1; i < nel; i++)
253 16 min = FFMIN(array[i], min);
254 12 return min;
255 }
256
257 65 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 65 times.
65 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 65 times.
65 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 65 return 0;
271 }
272
273 /// Master Frequency Band Table (14496-3 sp04 p194)
274 65 static int sbr_make_f_master(AACDecContext *ac, SpectralBandReplication *sbr,
275 SpectrumParameters *spectrum)
276 {
277 65 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 45 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
65 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 45 case 44100: case 48000: case 64000:
297 45 sbr_offset_ptr = sbr_offset[4];
298 45 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 59 times.
65 if (sbr->sample_rate < 32000) {
309 6 temp = 3000;
310
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2 times.
59 } else if (sbr->sample_rate < 64000) {
311 57 temp = 4000;
312 } else
313 2 temp = 5000;
314
315 65 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
316 65 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
317
318 65 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
319
320
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (spectrum->bs_stop_freq < 14) {
321 65 sbr->k[2] = stop_min;
322 65 make_bands(stop_dk, stop_min, 64, 13);
323
43/44
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 76 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 230 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✓ Branch 9 taken 6 times.
✓ Branch 10 taken 224 times.
✓ Branch 12 taken 12 times.
✓ Branch 13 taken 224 times.
✓ Branch 14 taken 33 times.
✓ Branch 15 taken 203 times.
✓ Branch 16 taken 512 times.
✓ Branch 17 taken 14 times.
✓ Branch 19 taken 168 times.
✓ Branch 20 taken 344 times.
✓ Branch 21 taken 664 times.
✓ Branch 22 taken 43 times.
✓ Branch 24 taken 349 times.
✓ Branch 25 taken 315 times.
✓ Branch 26 taken 306 times.
✓ Branch 27 taken 52 times.
✓ Branch 28 taken 358 times.
✓ Branch 29 taken 203 times.
✓ Branch 30 taken 179 times.
✓ Branch 31 taken 24 times.
✓ Branch 32 taken 129 times.
✓ Branch 33 taken 50 times.
✓ Branch 34 taken 107 times.
✓ Branch 35 taken 22 times.
✓ Branch 36 taken 617 times.
✓ Branch 37 taken 44 times.
✓ Branch 39 taken 504 times.
✓ Branch 40 taken 113 times.
✓ Branch 41 taken 44 times.
✓ Branch 42 taken 113 times.
✓ Branch 43 taken 22 times.
✓ Branch 44 taken 137 times.
✓ Branch 46 taken 56 times.
✓ Branch 47 taken 20 times.
✓ Branch 48 taken 312 times.
✓ Branch 49 taken 71 times.
✓ Branch 50 taken 224 times.
✓ Branch 51 taken 65 times.
2030 AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
324
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 65 times.
725 for (k = 0; k < spectrum->bs_stop_freq; k++)
325 660 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 65 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 47 times.
65 if (sbr->sample_rate <= 32000) {
339 18 max_qmf_subbands = 48;
340
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 11 times.
47 } else if (sbr->sample_rate == 44100) {
341 36 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 65 times.
65 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 51 times.
65 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 51 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 12 times.
✓ Branch 1 taken 39 times.
51 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
386 12 two_regions = 1;
387 12 sbr->k[1] = 2 * sbr->k[0];
388 } else {
389 39 two_regions = 0;
390 39 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 46 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 51 times.
51 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 51 vk0[0] = 0;
413
414 51 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
415
416
43/44
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 46 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 162 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
✓ Branch 9 taken 19 times.
✓ Branch 10 taken 143 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 170 times.
✓ Branch 14 taken 39 times.
✓ Branch 15 taken 135 times.
✓ Branch 16 taken 324 times.
✓ Branch 17 taken 19 times.
✓ Branch 19 taken 107 times.
✓ Branch 20 taken 217 times.
✓ Branch 21 taken 366 times.
✓ Branch 22 taken 34 times.
✓ Branch 24 taken 164 times.
✓ Branch 25 taken 202 times.
✓ Branch 26 taken 185 times.
✓ Branch 27 taken 51 times.
✓ Branch 28 taken 236 times.
✓ Branch 29 taken 135 times.
✓ Branch 30 taken 110 times.
✓ Branch 31 taken 25 times.
✓ Branch 32 taken 71 times.
✓ Branch 33 taken 39 times.
✓ Branch 34 taken 38 times.
✓ Branch 35 taken 33 times.
✓ Branch 36 taken 271 times.
✓ Branch 37 taken 36 times.
✓ Branch 39 taken 230 times.
✓ Branch 40 taken 41 times.
✓ Branch 41 taken 36 times.
✓ Branch 42 taken 41 times.
✓ Branch 43 taken 44 times.
✓ Branch 44 taken 55 times.
✓ Branch 46 taken 5 times.
✓ Branch 47 taken 41 times.
✓ Branch 48 taken 220 times.
✓ Branch 49 taken 29 times.
✓ Branch 50 taken 150 times.
✓ Branch 51 taken 51 times.
1172 AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
417 51 vdk0_max = vk0[num_bands_0];
418
419 51 vk0[0] = sbr->k[0];
420
2/2
✓ Branch 0 taken 560 times.
✓ Branch 1 taken 51 times.
611 for (k = 1; k <= num_bands_0; k++) {
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 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 560 vk0[k] += vk0[k-1];
426 }
427
428
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 39 times.
51 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 20 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
447
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 : 1.0f; // bs_alter_scale = {0,1}
448 10 int num_bands_1 = lrintf(half_bands * invwarp *
449 10 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
450 #endif /* USE_FIXED */
451 12 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
452
453 12 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
454
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 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 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 2 times.
✗ Branch 22 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 2 times.
✓ Branch 26 taken 1 times.
✓ Branch 27 taken 1 times.
✓ Branch 28 taken 2 times.
✓ Branch 29 taken 2 times.
✓ Branch 30 taken 2 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 2 times.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✓ Branch 36 taken 6 times.
✓ Branch 37 taken 2 times.
✓ Branch 39 taken 6 times.
✗ Branch 40 not taken.
✓ Branch 41 taken 2 times.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 46 not taken.
✓ Branch 47 taken 10 times.
✓ Branch 48 taken 12 times.
✗ Branch 49 not taken.
✓ Branch 50 taken 12 times.
✓ Branch 51 taken 12 times.
34 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
464
465 12 vk1[0] = sbr->k[1];
466
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 12 times.
40 for (k = 1; k <= num_bands_1; k++) {
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 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 28 vk1[k] += vk1[k-1];
472 }
473
474 12 sbr->n_master = num_bands_0 + num_bands_1;
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
476 return -1;
477 12 memcpy(&sbr->f_master[0], vk0,
478 12 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
479 12 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
480 num_bands_1 * sizeof(sbr->f_master[0]));
481
482 } else {
483 39 sbr->n_master = num_bands_0;
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
485 return -1;
486 39 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
487 }
488 }
489
490 65 return 0;
491 }
492
493 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
494 65 static int sbr_hf_calc_npatches(AACDecContext *ac, SpectralBandReplication *sbr)
495 {
496 65 int i, k, last_k = -1, last_msb = -1, sb = 0;
497 65 int msb = sbr->k[0];
498 65 int usb = sbr->kx[1];
499 65 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
500
501 65 sbr->num_patches = 0;
502
503
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 34 times.
65 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
504
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 31 times.
334 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
505 } else
506 34 k = sbr->n_master;
507
508 do {
509 186 int odd = 0;
510
3/4
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 100 times.
186 if (k == last_k && msb == last_msb) {
511 av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
512 return AVERROR_INVALIDDATA;
513 }
514 186 last_k = k;
515 186 last_msb = msb;
516
4/4
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 874 times.
✓ Branch 2 taken 688 times.
✓ Branch 3 taken 186 times.
1060 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
517 874 sb = sbr->f_master[i];
518 874 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 186 times.
186 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 186 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
531 186 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 186 times.
✗ Branch 1 not taken.
186 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
534 186 usb = sb;
535 186 msb = sb;
536 186 sbr->num_patches++;
537 } else
538 msb = sbr->kx[1];
539
540
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 100 times.
186 if (sbr->f_master[k] - sb < 3)
541 86 k = sbr->n_master;
542
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 65 times.
186 } while (sb != sbr->kx[1] + sbr->m[1]);
543
544
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2 times.
65 if (sbr->num_patches > 1 &&
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
546 sbr->num_patches--;
547
548 65 return 0;
549 }
550
551 /// Derived Frequency Band Tables (14496-3 sp04 p197)
552 65 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 65 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
560 65 sbr->n[0] = (sbr->n[1] + 1) >> 1;
561
562 65 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
563 65 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
564 65 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
565 65 sbr->kx[1] = sbr->f_tablehigh[0];
566
567 // Requirements (14496-3 sp04 p205)
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 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 65 times.
65 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 65 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
579 65 temp = sbr->n[1] & 1;
580
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 65 times.
485 for (k = 1; k <= sbr->n[0]; k++)
581 420 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 60 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 65 times.
65 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 return -1;
603 }
604
605 65 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
606 65 temp = 0;
607
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 65 times.
266 for (k = 1; k <= sbr->n_q; k++) {
608 201 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
609 201 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
610 }
611
612
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (sbr_hf_calc_npatches(ac, sbr) < 0)
613 return -1;
614
615 65 sbr_make_f_tablelim(sbr);
616
617 65 sbr->data[0].f_indexnoise = 0;
618 65 sbr->data[1].f_indexnoise = 0;
619
620 65 return 0;
621 }
622
623 22940 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
624 int elements)
625 {
626 int i;
627
2/2
✓ Branch 0 taken 48555 times.
✓ Branch 1 taken 22940 times.
71495 for (i = 0; i < elements; i++) {
628 48555 vec[i] = get_bits1(gb);
629 }
630 22940 }
631
632 /** ceil(log2(index+1)) */
633 static const int8_t ceil_log2[] = {
634 0, 1, 2, 2, 3, 3,
635 };
636
637 7793 static int read_sbr_grid(AACDecContext *ac, SpectralBandReplication *sbr,
638 GetBitContext *gb, SBRData *ch_data)
639 {
640 int i;
641 7793 int bs_pointer = 0;
642 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
643 7793 int abs_bord_trail = 16;
644 int num_rel_lead, num_rel_trail;
645 7793 unsigned bs_num_env_old = ch_data->bs_num_env;
646 int bs_frame_class, bs_num_env;
647
648 7793 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
649 7793 ch_data->bs_amp_res = sbr->bs_amp_res_header;
650 7793 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
651
652
4/5
✓ Branch 1 taken 6395 times.
✓ Branch 2 taken 630 times.
✓ Branch 3 taken 620 times.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
7793 switch (bs_frame_class = get_bits(gb, 2)) {
653 6395 case FIXFIX:
654 6395 bs_num_env = 1 << get_bits(gb, 2);
655
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6395 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6395 times.
6395 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 6395 ch_data->bs_num_env = bs_num_env;
662 6395 num_rel_lead = ch_data->bs_num_env - 1;
663
2/2
✓ Branch 0 taken 4929 times.
✓ Branch 1 taken 1466 times.
6395 if (ch_data->bs_num_env == 1)
664 4929 ch_data->bs_amp_res = 0;
665
666
667 6395 ch_data->t_env[0] = 0;
668 6395 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
669
670 6395 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
671 6395 ch_data->bs_num_env;
672
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 6395 times.
7861 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 6395 ch_data->bs_freq_res[1] = get_bits1(gb);
676
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 6395 times.
7861 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 6395 break;
679 630 case FIXVAR:
680 630 abs_bord_trail += get_bits(gb, 2);
681 630 num_rel_trail = get_bits(gb, 2);
682 630 ch_data->bs_num_env = num_rel_trail + 1;
683 630 ch_data->t_env[0] = 0;
684 630 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
685
686
2/2
✓ Branch 0 taken 1362 times.
✓ Branch 1 taken 630 times.
1992 for (i = 0; i < num_rel_trail; i++)
687 1362 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
688 1362 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
689
690 630 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
691
692
2/2
✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 630 times.
2622 for (i = 0; i < ch_data->bs_num_env; i++)
693 1992 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
694 630 break;
695 620 case VARFIX:
696 620 ch_data->t_env[0] = get_bits(gb, 2);
697 620 num_rel_lead = get_bits(gb, 2);
698 620 ch_data->bs_num_env = num_rel_lead + 1;
699 620 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
700
701
2/2
✓ Branch 0 taken 776 times.
✓ Branch 1 taken 620 times.
1396 for (i = 0; i < num_rel_lead; i++)
702 776 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
703
704 620 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
705
706 620 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
707 620 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 7793 ch_data->bs_frame_class = bs_frame_class;
737
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7793 times.
7793 av_assert0(bs_pointer >= 0);
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7793 times.
7793 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 11760 times.
✓ Branch 1 taken 7793 times.
19553 for (i = 1; i <= ch_data->bs_num_env; i++) {
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11760 times.
11760 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 2864 times.
✓ Branch 1 taken 4929 times.
7793 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
754
755 7793 ch_data->t_q[0] = ch_data->t_env[0];
756 7793 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
757
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 4929 times.
7793 if (ch_data->bs_num_noise > 1) {
758 int idx;
759
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 1398 times.
2864 if (ch_data->bs_frame_class == FIXFIX) {
760 1466 idx = ch_data->bs_num_env >> 1;
761
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 620 times.
1398 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
762
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 175 times.
778 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
763 } else { // VARFIX
764
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 156 times.
620 if (!bs_pointer)
765 464 idx = 1;
766
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 else if (bs_pointer == 1)
767 156 idx = ch_data->bs_num_env - 1;
768 else // bs_pointer > 1
769 idx = bs_pointer - 1;
770 }
771 2864 ch_data->t_q[1] = ch_data->t_env[idx];
772 }
773
774 7793 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
775 7793 ch_data->e_a[1] = -1;
776
3/4
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 7015 times.
✓ Branch 2 taken 778 times.
✗ Branch 3 not taken.
7793 if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
777 778 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
778
3/4
✓ Branch 0 taken 620 times.
✓ Branch 1 taken 6395 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 620 times.
7015 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
779 ch_data->e_a[1] = bs_pointer - 1;
780
781 7793 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 10418 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 10418 times.
10418 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 10418 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
821 10418 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
822 }
823 10418 }
824
825 /// Read inverse filtering data
826 7793 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
827 SBRData *ch_data)
828 {
829 int i;
830
831 7793 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
832
2/2
✓ Branch 0 taken 23985 times.
✓ Branch 1 taken 7793 times.
31778 for (i = 0; i < sbr->n_q; i++)
833 23985 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
834 7793 }
835
836 10418 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 3886 times.
✓ Branch 1 taken 6532 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 1261 times.
10418 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
843 10418 const int odd = sbr->n[1] & 1;
844
845
4/4
✓ Branch 0 taken 5250 times.
✓ Branch 1 taken 5168 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 2625 times.
10418 if (sbr->bs_coupling && ch) {
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 2864 times.
✓ Branch 1 taken 4929 times.
7793 if (ch_data->bs_amp_res) {
857 2864 bits = 6;
858 2864 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_3_0DB];
859 2864 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_3_0DB];
860 } else {
861 4929 bits = 7;
862 4929 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_1_5DB];
863 4929 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_1_5DB];
864 }
865 }
866
867
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (i = 0; i < ch_data->bs_num_env; i++) {
868
2/2
✓ Branch 0 taken 4947 times.
✓ Branch 1 taken 10213 times.
15160 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 4454 times.
✓ Branch 1 taken 493 times.
4947 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
871
2/2
✓ Branch 0 taken 51282 times.
✓ Branch 1 taken 4454 times.
55736 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
872 51282 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 51282 times.
51282 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 373 times.
✓ Branch 1 taken 120 times.
493 } else if (ch_data->bs_freq_res[i + 1]) {
879
2/2
✓ Branch 0 taken 4282 times.
✓ Branch 1 taken 373 times.
4655 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
880 4282 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
881 4282 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 4282 times.
4282 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 679 times.
✓ Branch 1 taken 120 times.
799 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
889
2/2
✓ Branch 0 taken 559 times.
✓ Branch 1 taken 120 times.
679 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
890 679 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 679 times.
679 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 10213 ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
899
2/2
✓ Branch 0 taken 105460 times.
✓ Branch 1 taken 10213 times.
115673 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
900 105460 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * get_vlc2(gb, f_huff, 9, 3);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105460 times.
105460 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 15160 times.
15160 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 10418 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 10418 return 0;
921 }
922
923 10418 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 3886 times.
✓ Branch 1 taken 6532 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 1261 times.
10418 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
929
930
4/4
✓ Branch 0 taken 5250 times.
✓ Branch 1 taken 5168 times.
✓ Branch 2 taken 2625 times.
✓ Branch 3 taken 2625 times.
10418 if (sbr->bs_coupling && ch) {
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 7793 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_NOISE_3_0DB];
935 7793 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_3_0DB];
936 }
937
938
2/2
✓ Branch 0 taken 13864 times.
✓ Branch 1 taken 10418 times.
24282 for (i = 0; i < ch_data->bs_num_noise; i++) {
939
2/2
✓ Branch 0 taken 10830 times.
✓ Branch 1 taken 3034 times.
13864 if (ch_data->bs_df_noise[i]) {
940
2/2
✓ Branch 0 taken 32863 times.
✓ Branch 1 taken 10830 times.
43693 for (j = 0; j < sbr->n_q; j++) {
941 32863 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 32863 times.
32863 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 3034 ch_data->noise_facs_q[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
949
2/2
✓ Branch 0 taken 5910 times.
✓ Branch 1 taken 3034 times.
8944 for (j = 1; j < sbr->n_q; j++) {
950 5910 ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i + 1][j - 1] + delta * get_vlc2(gb, f_huff, 9, 3);
951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5910 times.
5910 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 10418 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 10418 return 0;
963 }
964
965 1616 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 1616 times.
✗ Branch 1 not taken.
1616 switch (bs_extension_id) {
970 1616 case EXTENSION_ID_PS:
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1616 times.
1616 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 1616 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps.common, *num_bits_left);
977 1616 ac->avctx->profile = AV_PROFILE_AAC_HE_V2;
978 // ensure the warning is not printed if PS extension is present
979 1616 ac->warned_he_aac_mono = 1;
980 }
981 1616 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 1616 }
991
992 2646 static int read_sbr_single_channel_element(AACDecContext *ac,
993 SpectralBandReplication *sbr,
994 GetBitContext *gb)
995 {
996 int ret;
997
998
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 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 2646 times.
2646 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1002 return -1;
1003 2646 read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1004 2646 read_sbr_invf(sbr, gb, &sbr->data[0]);
1005
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1006 return ret;
1007
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1008 return ret;
1009
1010
2/2
✓ Branch 1 taken 174 times.
✓ Branch 2 taken 2472 times.
2646 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 2646 return 0;
1014 }
1015
1016 3886 static int read_sbr_channel_pair_element(AACDecContext *ac,
1017 SpectralBandReplication *sbr,
1018 GetBitContext *gb)
1019 {
1020 int ret;
1021
1022
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3886 times.
3886 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 1261 times.
3886 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]))
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 1261 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1261 times.
2522 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
1044 1261 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1045 return -1;
1046 1261 read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1047 1261 read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
1048 1261 read_sbr_invf(sbr, gb, &sbr->data[0]);
1049 1261 read_sbr_invf(sbr, gb, &sbr->data[1]);
1050
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1051 return ret;
1052
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1053 return ret;
1054
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1055 return ret;
1056
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1057 return ret;
1058 }
1059
1060
2/2
✓ Branch 1 taken 698 times.
✓ Branch 2 taken 3188 times.
3886 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 3422 times.
3886 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 3886 return 0;
1066 }
1067
1068 6532 static unsigned int read_sbr_data(AACDecContext *ac, SpectralBandReplication *sbr,
1069 GetBitContext *gb, int id_aac)
1070 {
1071 6532 unsigned int cnt = get_bits_count(gb);
1072
1073 6532 sbr->id_aac = id_aac;
1074 6532 sbr->ready_for_dequant = 1;
1075
1076
3/4
✓ Branch 0 taken 3886 times.
✓ Branch 1 taken 2646 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3886 times.
6532 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1077
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (read_sbr_single_channel_element(ac, sbr, gb)) {
1078 sbr_turnoff(sbr);
1079 return get_bits_count(gb) - cnt;
1080 }
1081
1/2
✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
3886 } else if (id_aac == TYPE_CPE) {
1082
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3886 times.
3886 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
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 1616 times.
✓ Branch 2 taken 4916 times.
6532 if (get_bits1(gb)) { // bs_extended_data
1093 1616 int num_bits_left = get_bits(gb, 4); // bs_extension_size
1094
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 1358 times.
1616 if (num_bits_left == 15)
1095 258 num_bits_left += get_bits(gb, 8); // bs_esc_count
1096
1097 1616 num_bits_left <<= 3;
1098
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 1616 times.
3232 while (num_bits_left > 7) {
1099 1616 num_bits_left -= 2;
1100 1616 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 1616 times.
1616 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 1451 times.
✓ Branch 1 taken 165 times.
1616 if (num_bits_left > 0)
1106 1451 skip_bits(gb, num_bits_left);
1107 }
1108
1109 6532 return get_bits_count(gb) - cnt;
1110 }
1111
1112 65 static void sbr_reset(AACDecContext *ac, SpectralBandReplication *sbr)
1113 {
1114 int err;
1115 65 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1116
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (err >= 0)
1117 65 err = sbr_make_f_derived(ac, sbr);
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 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 65 }
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 10901 int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *che,
1134 GetBitContext *gb_host, int crc,
1135 int cnt, int id_aac)
1136 {
1137 10901 SpectralBandReplication *sbr = get_sbr(che);
1138 10901 unsigned int num_sbr_bits = 0, num_align_bits;
1139 unsigned bytes_read;
1140 10901 GetBitContext gbc = *gb_host, *gb = &gbc;
1141 10901 skip_bits_long(gb_host, cnt*8 - 4);
1142
1143 10901 sbr->reset = 0;
1144
1145
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 10819 times.
10901 if (!sbr->sample_rate)
1146 82 sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1147
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10883 times.
10901 if (!ac->oc[1].m4ac.ext_sample_rate)
1148 18 ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1149
1150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 if (crc) {
1151 skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1152 num_sbr_bits += 10;
1153 }
1154
1155 //Save some state from the previous frame.
1156 10901 sbr->kx[0] = sbr->kx[1];
1157 10901 sbr->m[0] = sbr->m[1];
1158 10901 sbr->kx_and_m_pushed = 1;
1159
1160 10901 num_sbr_bits++;
1161
2/2
✓ Branch 1 taken 489 times.
✓ Branch 2 taken 10412 times.
10901 if (get_bits1(gb)) // bs_header_flag
1162 489 num_sbr_bits += read_sbr_header(sbr, gb, 0);
1163
1164
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 10836 times.
10901 if (sbr->reset)
1165 65 sbr_reset(ac, sbr);
1166
1167
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 4369 times.
10901 if (sbr->start)
1168 6532 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1169
1170 10901 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1171 10901 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1172
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 if (bytes_read > cnt) {
1174 av_log(ac->avctx, AV_LOG_ERROR,
1175 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1176 sbr_turnoff(sbr);
1177 }
1178 10901 return cnt;
1179 }
1180
1181 #if !USE_FIXED
1182 static void copy_usac_default_header(SpectralBandReplication *sbr,
1183 AACUsacElemConfig *ue)
1184 {
1185 sbr->inter_tes = ue->sbr.bs_intertes;
1186
1187 sbr->spectrum_params.bs_start_freq = ue->sbr.dflt.start_freq;
1188 sbr->spectrum_params.bs_stop_freq = ue->sbr.dflt.stop_freq;
1189
1190 sbr->spectrum_params.bs_freq_scale = ue->sbr.dflt.freq_scale;
1191 sbr->spectrum_params.bs_alter_scale = ue->sbr.dflt.alter_scale;
1192 sbr->spectrum_params.bs_noise_bands = ue->sbr.dflt.noise_bands;
1193
1194 sbr->bs_limiter_bands = ue->sbr.dflt.limiter_bands;
1195 sbr->bs_limiter_gains = ue->sbr.dflt.limiter_gains;
1196 sbr->bs_interpol_freq = ue->sbr.dflt.interpol_freq;
1197 sbr->bs_smoothing_mode = ue->sbr.dflt.smoothing_mode;
1198 }
1199
1200 int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
1201 AACUsacElemConfig *ue)
1202 {
1203 SpectralBandReplication *sbr = get_sbr(che);
1204 sbr_turnoff(sbr);
1205 return 0;
1206 }
1207
1208 int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
1209 AACUsacElemConfig *ue, GetBitContext *gb,
1210 int sbr_ch, int indep_flag)
1211 {
1212 int ret;
1213 SpectralBandReplication *sbr = get_sbr(che);
1214 int info_present = 1;
1215 int header_present = 1;
1216
1217 sbr->reset = 0;
1218 sbr->usac = 1;
1219
1220 sbr->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
1221 sbr->id_aac = sbr_ch == 2 ? TYPE_CPE : TYPE_SCE;
1222
1223 if (!indep_flag) {
1224 info_present = get_bits1(gb);
1225 if (info_present)
1226 header_present = get_bits1(gb);
1227 else
1228 header_present = 0;
1229 }
1230
1231 if (info_present) {
1232 /* SbrInfo() */
1233 sbr->bs_amp_res_header = get_bits1(gb);
1234 sbr->spectrum_params.bs_xover_band = get_bits(gb, 4);
1235 sbr->bs_sbr_preprocessing = get_bits1(gb);
1236 /* if (bs_pvc) ... */
1237 }
1238
1239 if (header_present) {
1240 if (get_bits1(gb)) {
1241 int old_bs_limiter_bands = sbr->bs_limiter_bands;
1242 SpectrumParameters old_spectrum_params;
1243 memcpy(&old_spectrum_params, &sbr->spectrum_params,
1244 sizeof(SpectrumParameters));
1245
1246 copy_usac_default_header(sbr, ue);
1247 // Check if spectrum parameters changed
1248 if (memcmp(&old_spectrum_params, &sbr->spectrum_params,
1249 sizeof(SpectrumParameters)))
1250 sbr->reset = 1;
1251
1252 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
1253 sbr_make_f_tablelim(sbr);
1254 } else {
1255 read_sbr_header(sbr, gb, 1);
1256 }
1257
1258 sbr->start = 1;
1259 }
1260
1261 //Save some state from the previous frame.
1262 sbr->kx[0] = sbr->kx[1];
1263 sbr->m[0] = sbr->m[1];
1264 sbr->kx_and_m_pushed = 1;
1265
1266 if (sbr->reset)
1267 sbr_reset(ac, sbr);
1268
1269 sbr->ready_for_dequant = 1;
1270
1271 if (sbr_ch == 1) { /* sbr_single_channel_element */
1272 /* if (harmonicSBR) ... */
1273
1274 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1275 return -1;
1276
1277 read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1278 read_sbr_invf(sbr, gb, &sbr->data[0]);
1279
1280 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1281 return ret;
1282
1283 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1284 return ret;
1285
1286 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1287 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1288 } else if (get_bits1(gb)) { /* bs_coupling == 1 */
1289 /* if (harmonicSBR) ... */
1290
1291 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1292 return -1;
1293 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1294
1295 read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1296 read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
1297
1298 read_sbr_invf(sbr, gb, &sbr->data[0]);
1299 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0],
1300 sizeof(sbr->data[1].bs_invf_mode[0]));
1301 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0],
1302 sizeof(sbr->data[1].bs_invf_mode[0]));
1303
1304 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1305 return ret;
1306 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1307 return ret;
1308
1309 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1310 return ret;
1311 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1312 return ret;
1313
1314 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1315 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1316 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1317 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1318 } else { /* bs_coupling == 0 */
1319 /* if (harmonicSBR) ... */
1320 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1321 return -1;
1322 if (read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1323 return -1;
1324
1325 read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1326 read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
1327
1328 read_sbr_invf(sbr, gb, &sbr->data[0]);
1329 read_sbr_invf(sbr, gb, &sbr->data[1]);
1330
1331 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1332 return ret;
1333 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1334 return ret;
1335
1336 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1337 return ret;
1338 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1339 return ret;
1340
1341 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1342 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1343 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1344 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1345 }
1346
1347 return 0;
1348 }
1349 #endif
1350
1351 /**
1352 * Analysis QMF Bank (14496-3 sp04 p206)
1353 *
1354 * @param x pointer to the beginning of the first sample window
1355 * @param W array of complex-valued samples split into subbands
1356 */
1357 #ifndef sbr_qmf_analysis
1358 #if USE_FIXED
1359 8328 static void sbr_qmf_analysis(AVFixedDSPContext *dsp, AVTXContext *mdct,
1360 av_tx_fn mdct_fn,
1361 #else
1362 11409 static void sbr_qmf_analysis(AVFloatDSPContext *dsp, AVTXContext *mdct,
1363 av_tx_fn mdct_fn,
1364 #endif /* USE_FIXED */
1365 SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
1366 INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
1367 {
1368 int i;
1369 #if USE_FIXED
1370 int j;
1371 #endif
1372 19737 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1373 19737 memcpy(x+288, in, 1024*sizeof(x[0]));
1374
2/2
✓ Branch 0 taken 631584 times.
✓ Branch 1 taken 19737 times.
651321 for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1375 // are not supported
1376 631584 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1377 631584 sbrdsp->sum64x5(z);
1378 631584 sbrdsp->qmf_pre_shuffle(z);
1379 #if USE_FIXED
1380
2/2
✓ Branch 0 taken 17055744 times.
✓ Branch 1 taken 266496 times.
17322240 for (j = 64; j < 128; j++) {
1381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17055744 times.
17055744 if (z[j] > 1<<24) {
1382 av_log(NULL, AV_LOG_WARNING,
1383 "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
1384 z[j], 1<<24);
1385 z[j] = 1<<24;
1386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17055744 times.
17055744 } else if (z[j] < -(1<<24)) {
1387 av_log(NULL, AV_LOG_WARNING,
1388 "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
1389 z[j], -(1<<24));
1390 z[j] = -(1<<24);
1391 }
1392 }
1393 #endif
1394 631584 mdct_fn(mdct, z, z + 64, sizeof(INTFLOAT));
1395 631584 sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1396 631584 x += 32;
1397 }
1398 19737 }
1399 #endif
1400
1401 /**
1402 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1403 * (14496-3 sp04 p206)
1404 */
1405 #ifndef sbr_qmf_synthesis
1406 21736 static void sbr_qmf_synthesis(AVTXContext *mdct, av_tx_fn mdct_fn,
1407 #if USE_FIXED
1408 SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
1409 #else
1410 SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1411 #endif /* USE_FIXED */
1412 INTFLOAT *out, INTFLOAT X[2][38][64],
1413 INTFLOAT mdct_buf[2][64],
1414 INTFLOAT *v0, int *v_off, const unsigned int div)
1415 {
1416 int i, n;
1417
2/2
✓ Branch 0 taken 4596 times.
✓ Branch 1 taken 17140 times.
21736 const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1418 21736 const int step = 128 >> div;
1419 INTFLOAT *v;
1420
2/2
✓ Branch 0 taken 695552 times.
✓ Branch 1 taken 21736 times.
717288 for (i = 0; i < 32; i++) {
1421
2/2
✓ Branch 0 taken 66284 times.
✓ Branch 1 taken 629268 times.
695552 if (*v_off < step) {
1422 66284 int saved_samples = (1280 - 128) >> div;
1423 66284 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
1424 66284 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1425 } else {
1426 629268 *v_off -= step;
1427 }
1428 695552 v = v0 + *v_off;
1429
2/2
✓ Branch 0 taken 147072 times.
✓ Branch 1 taken 548480 times.
695552 if (div) {
1430
2/2
✓ Branch 0 taken 4706304 times.
✓ Branch 1 taken 147072 times.
4853376 for (n = 0; n < 32; n++) {
1431 4706304 X[0][i][ n] = -X[0][i][n];
1432 4706304 X[0][i][32+n] = X[1][i][31-n];
1433 }
1434 147072 mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1435 147072 sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1436 } else {
1437 548480 sbrdsp->neg_odd_64(X[1][i]);
1438 548480 mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1439 548480 mdct_fn(mdct, mdct_buf[1], X[1][i], sizeof(INTFLOAT));
1440 548480 sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1441 }
1442 695552 dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1443 695552 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1444 695552 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1445 695552 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1446 695552 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1447 695552 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1448 695552 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1449 695552 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1450 695552 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1451 695552 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1452 695552 out += 64 >> div;
1453 }
1454 21736 }
1455 #endif
1456
1457 /// Generate the subband filtered lowband
1458 19737 static int sbr_lf_gen(SpectralBandReplication *sbr,
1459 INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
1460 int buf_idx)
1461 {
1462 int i, k;
1463 19737 const int t_HFGen = 8;
1464 19737 const int i_f = 32;
1465 19737 memset(X_low, 0, 32*sizeof(*X_low));
1466
2/2
✓ Branch 0 taken 490630 times.
✓ Branch 1 taken 19737 times.
510367 for (k = 0; k < sbr->kx[1]; k++) {
1467
2/2
✓ Branch 0 taken 15700160 times.
✓ Branch 1 taken 490630 times.
16190790 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1468 15700160 X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1469 15700160 X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1470 }
1471 }
1472 19737 buf_idx = 1-buf_idx;
1473
2/2
✓ Branch 0 taken 491470 times.
✓ Branch 1 taken 19737 times.
511207 for (k = 0; k < sbr->kx[0]; k++) {
1474
2/2
✓ Branch 0 taken 3931760 times.
✓ Branch 1 taken 491470 times.
4423230 for (i = 0; i < t_HFGen; i++) {
1475 3931760 X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1476 3931760 X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1477 }
1478 }
1479 19737 return 0;
1480 }
1481
1482 /// High Frequency Generator (14496-3 sp04 p215)
1483 10418 static int sbr_hf_gen(AACDecContext *ac, SpectralBandReplication *sbr,
1484 INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
1485 const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
1486 const INTFLOAT bw_array[5], const uint8_t *t_env,
1487 int bs_num_env)
1488 {
1489 int j, x;
1490 10418 int g = 0;
1491 10418 int k = sbr->kx[1];
1492
2/2
✓ Branch 0 taken 26191 times.
✓ Branch 1 taken 10418 times.
36609 for (j = 0; j < sbr->num_patches; j++) {
1493
2/2
✓ Branch 0 taken 256742 times.
✓ Branch 1 taken 26191 times.
282933 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1494 256742 const int p = sbr->patch_start_subband[j] + x;
1495
3/4
✓ Branch 0 taken 533838 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 277096 times.
✓ Branch 3 taken 256742 times.
533838 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1496 277096 g++;
1497 256742 g--;
1498
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256742 times.
256742 if (g < 0) {
1500 av_log(ac->avctx, AV_LOG_ERROR,
1501 "ERROR : no subband found for frequency %d\n", k);
1502 return -1;
1503 }
1504
1505 256742 sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1506 256742 X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1507 256742 alpha0[p], alpha1[p], bw_array[g],
1508 256742 2 * t_env[0], 2 * t_env[bs_num_env]);
1509 }
1510 }
1511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 if (k < sbr->m[1] + sbr->kx[1])
1512 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1513
1514 10418 return 0;
1515 }
1516
1517 /// Generate the subband filtered lowband
1518 19737 static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
1519 const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
1520 const INTFLOAT X_low[32][40][2], int ch)
1521 {
1522 int k, i;
1523 19737 const int i_f = 32;
1524 19737 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1525 19737 memset(X, 0, 2*sizeof(*X));
1526
2/2
✓ Branch 0 taken 491470 times.
✓ Branch 1 taken 19737 times.
511207 for (k = 0; k < sbr->kx[0]; k++) {
1527
2/2
✓ Branch 0 taken 55730 times.
✓ Branch 1 taken 491470 times.
547200 for (i = 0; i < i_Temp; i++) {
1528 55730 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1529 55730 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1530 }
1531 }
1532
2/2
✓ Branch 0 taken 254146 times.
✓ Branch 1 taken 19737 times.
273883 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1533
2/2
✓ Branch 0 taken 68266 times.
✓ Branch 1 taken 254146 times.
322412 for (i = 0; i < i_Temp; i++) {
1534 68266 X[0][i][k] = Y0[i + i_f][k][0];
1535 68266 X[1][i][k] = Y0[i + i_f][k][1];
1536 }
1537 }
1538
1539
2/2
✓ Branch 0 taken 490630 times.
✓ Branch 1 taken 19737 times.
510367 for (k = 0; k < sbr->kx[1]; k++) {
1540
2/2
✓ Branch 0 taken 18588210 times.
✓ Branch 1 taken 490630 times.
19078840 for (i = i_Temp; i < 38; i++) {
1541 18588210 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1542 18588210 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1543 }
1544 }
1545
2/2
✓ Branch 0 taken 256742 times.
✓ Branch 1 taken 19737 times.
276479 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1546
2/2
✓ Branch 0 taken 8147478 times.
✓ Branch 1 taken 256742 times.
8404220 for (i = i_Temp; i < i_f; i++) {
1547 8147478 X[0][i][k] = Y1[i][k][0];
1548 8147478 X[1][i][k] = Y1[i][k][1];
1549 }
1550 }
1551 19737 return 0;
1552 }
1553
1554 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1555 * (14496-3 sp04 p217)
1556 */
1557 10418 static int sbr_mapping(AACDecContext *ac, SpectralBandReplication *sbr,
1558 SBRData *ch_data, int e_a[2])
1559 {
1560 int e, i, m;
1561
1562 10418 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1563
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (e = 0; e < ch_data->bs_num_env; e++) {
1564 15160 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1565
2/2
✓ Branch 0 taken 13386 times.
✓ Branch 1 taken 1774 times.
15160 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1566 int k;
1567
1568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15160 times.
15160 if (sbr->kx[1] != table[0]) {
1569 av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1570 "Derived frequency tables were not regenerated.\n");
1571 sbr_turnoff(sbr);
1572 return AVERROR_BUG;
1573 }
1574
2/2
✓ Branch 0 taken 171916 times.
✓ Branch 1 taken 15160 times.
187076 for (i = 0; i < ilim; i++)
1575
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 171916 times.
550938 for (m = table[i]; m < table[i + 1]; m++)
1576 379022 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1577
1578 // ch_data->bs_num_noise > 1 => 2 noise floors
1579
4/4
✓ Branch 0 taken 8188 times.
✓ Branch 1 taken 6972 times.
✓ Branch 2 taken 4354 times.
✓ Branch 3 taken 3834 times.
15160 k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1580
2/2
✓ Branch 0 taken 45584 times.
✓ Branch 1 taken 15160 times.
60744 for (i = 0; i < sbr->n_q; i++)
1581
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 45584 times.
424606 for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1582 379022 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1583
1584
2/2
✓ Branch 0 taken 182216 times.
✓ Branch 1 taken 15160 times.
197376 for (i = 0; i < sbr->n[1]; i++) {
1585
2/2
✓ Branch 0 taken 32694 times.
✓ Branch 1 taken 149522 times.
182216 if (ch_data->bs_add_harmonic_flag) {
1586 32694 const unsigned int m_midpoint =
1587 32694 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1588
1589 32694 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1590
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));
1591 }
1592 }
1593
1594
2/2
✓ Branch 0 taken 171916 times.
✓ Branch 1 taken 15160 times.
187076 for (i = 0; i < ilim; i++) {
1595 171916 int additional_sinusoid_present = 0;
1596
2/2
✓ Branch 0 taken 376296 times.
✓ Branch 1 taken 168186 times.
544482 for (m = table[i]; m < table[i + 1]; m++) {
1597
2/2
✓ Branch 0 taken 3730 times.
✓ Branch 1 taken 372566 times.
376296 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1598 3730 additional_sinusoid_present = 1;
1599 3730 break;
1600 }
1601 }
1602 171916 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1603 171916 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1604 }
1605 }
1606
1607 10418 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1608 10418 return 0;
1609 }
1610
1611 /// Estimation of current envelope (14496-3 sp04 p218)
1612 10418 static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
1613 SpectralBandReplication *sbr, SBRData *ch_data)
1614 {
1615 int e, m;
1616 10418 int kx1 = sbr->kx[1];
1617
1618
1/2
✓ Branch 0 taken 10418 times.
✗ Branch 1 not taken.
10418 if (sbr->bs_interpol_freq) {
1619
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (e = 0; e < ch_data->bs_num_env; e++) {
1620 #if USE_FIXED
1621 5588 const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
1622 #else
1623 9572 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1624 #endif /* USE_FIXED */
1625 15160 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1626 15160 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1627
1628
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 15160 times.
394182 for (m = 0; m < sbr->m[1]; m++) {
1629 379022 AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1630 #if USE_FIXED
1631 127837 e_curr[e][m] = av_mul_sf(sum, recip_env_size);
1632 #else
1633 251185 e_curr[e][m] = sum * recip_env_size;
1634 #endif /* USE_FIXED */
1635 }
1636 }
1637 } else {
1638 int k, p;
1639
1640 for (e = 0; e < ch_data->bs_num_env; e++) {
1641 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1642 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1643 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1644 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1645
1646 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1647 #if USE_FIXED
1648 SoftFloat sum = FLOAT_0;
1649 const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
1650 for (k = table[p]; k < table[p + 1]; k++) {
1651 sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
1652 }
1653 sum = av_mul_sf(sum, den);
1654 #else
1655 float sum = 0.0f;
1656 const int den = env_size * (table[p + 1] - table[p]);
1657
1658 for (k = table[p]; k < table[p + 1]; k++) {
1659 sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1660 }
1661 sum /= den;
1662 #endif /* USE_FIXED */
1663 for (k = table[p]; k < table[p + 1]; k++) {
1664 e_curr[e][k - kx1] = sum;
1665 }
1666 }
1667 }
1668 }
1669 10418 }
1670
1671 12411 void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che,
1672 int id_aac, void *L_, void *R_)
1673 {
1674 12411 INTFLOAT *L = L_, *R = R_;
1675 12411 SpectralBandReplication *sbr = get_sbr(che);
1676 12411 int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1677 int ch;
1678
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 5085 times.
12411 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1679 int err;
1680
1681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12411 times.
12411 if (id_aac != sbr->id_aac) {
1682 av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
1683 "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
1684 sbr_turnoff(sbr);
1685 }
1686
1687
3/4
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 5879 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6532 times.
12411 if (sbr->start && !sbr->ready_for_dequant) {
1688 av_log(ac->avctx, AV_LOG_ERROR,
1689 "No quantized data read for sbr_dequant.\n");
1690 sbr_turnoff(sbr);
1691 }
1692
1693
2/2
✓ Branch 0 taken 1510 times.
✓ Branch 1 taken 10901 times.
12411 if (!sbr->kx_and_m_pushed) {
1694 1510 sbr->kx[0] = sbr->kx[1];
1695 1510 sbr->m[0] = sbr->m[1];
1696 } else {
1697 10901 sbr->kx_and_m_pushed = 0;
1698 }
1699
1700
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 5879 times.
12411 if (sbr->start) {
1701 6532 sbr_dequant(sbr, id_aac);
1702 6532 sbr->ready_for_dequant = 0;
1703 }
1704
2/2
✓ Branch 0 taken 19737 times.
✓ Branch 1 taken 12411 times.
32148 for (ch = 0; ch < nch; ch++) {
1705 /* decode channel */
1706 19737 sbr_qmf_analysis(ac->fdsp, sbr->mdct_ana, sbr->mdct_ana_fn, &sbr->dsp,
1707 19737 ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1708 19737 (INTFLOAT*)sbr->qmf_filter_scratch,
1709
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 12411 times.
19737 sbr->data[ch].W, sbr->data[ch].Ypos);
1710 19737 sbr->c.sbr_lf_gen(sbr, sbr->X_low,
1711 19737 (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
1712 sbr->data[ch].Ypos);
1713 19737 sbr->data[ch].Ypos ^= 1;
1714
2/2
✓ Branch 0 taken 10418 times.
✓ Branch 1 taken 9319 times.
19737 if (sbr->start) {
1715 10418 sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1716 10418 (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
1717 10418 sbr_chirp(sbr, &sbr->data[ch]);
1718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 av_assert0(sbr->data[ch].bs_num_env > 0);
1719 10418 sbr_hf_gen(ac, sbr, sbr->X_high,
1720 10418 (const INTFLOAT (*)[40][2]) sbr->X_low,
1721 10418 (const INTFLOAT (*)[2]) sbr->alpha0,
1722 10418 (const INTFLOAT (*)[2]) sbr->alpha1,
1723 10418 sbr->data[ch].bw_array, sbr->data[ch].t_env,
1724 6631 sbr->data[ch].bs_num_env);
1725
1726 // hf_adj
1727 10418 err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1728
1/2
✓ Branch 0 taken 10418 times.
✗ Branch 1 not taken.
10418 if (!err) {
1729 10418 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1730 10418 sbr_gain_calc(sbr, &sbr->data[ch], sbr->data[ch].e_a);
1731 10418 sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1732 10418 (const INTFLOAT (*)[40][2]) sbr->X_high,
1733 sbr, &sbr->data[ch],
1734 10418 sbr->data[ch].e_a);
1735 }
1736 }
1737
1738 /* synthesis */
1739 19737 sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1740 19737 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1741 19737 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1742 19737 (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
1743 }
1744
1745
2/2
✓ Branch 0 taken 1999 times.
✓ Branch 1 taken 10412 times.
12411 if (ac->oc[1].m4ac.ps == 1) {
1746
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 383 times.
1999 if (sbr->ps.common.start) {
1747 1616 AAC_RENAME(ff_ps_apply)(&sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1748 } else {
1749 383 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1750 }
1751 1999 nch = 2;
1752 }
1753
1754 12411 sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1755 12411 L, sbr->X[0], sbr->qmf_filter_scratch,
1756 12411 sbr->data[0].synthesis_filterbank_samples,
1757 &sbr->data[0].synthesis_filterbank_samples_offset,
1758 downsampled);
1759
2/2
✓ Branch 0 taken 9325 times.
✓ Branch 1 taken 3086 times.
12411 if (nch == 2)
1760 9325 sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1761 9325 R, sbr->X[1], sbr->qmf_filter_scratch,
1762 9325 sbr->data[1].synthesis_filterbank_samples,
1763 &sbr->data[1].synthesis_filterbank_samples_offset,
1764 downsampled);
1765 12411 }
1766
1767 376 static void aacsbr_func_ptr_init(AACSBRContext *c)
1768 {
1769 376 c->sbr_lf_gen = sbr_lf_gen;
1770 376 c->sbr_hf_assemble = sbr_hf_assemble;
1771 376 c->sbr_x_gen = sbr_x_gen;
1772 376 c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
1773 376 }
1774