FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacsbr_template.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 768 1039 73.9%
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 23720 static inline SpectralBandReplication *get_sbr(ChannelElement *ch)
48 {
49 23720 return &((ExtChannelElement*)ch)->sbr;
50 }
51
52 186 av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
53 {
54 186 AAC_RENAME(ff_ps_init)();
55 186 }
56
57 /** Places SBR in pure upsampling mode. */
58 408 static void sbr_turnoff(SpectralBandReplication *sbr) {
59 408 sbr->start = 0;
60 408 sbr->usac = 0;
61 408 sbr->ready_for_dequant = 0;
62 // Init defults used in pure upsampling mode
63 408 sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
64 408 sbr->m[1] = 0;
65 // Reset values for first SBR header
66 408 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
67 408 memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
68 408 }
69
70 408 av_cold int AAC_RENAME(ff_aac_sbr_ctx_alloc_init)(AACDecContext *ac,
71 ChannelElement **che, int id_aac)
72 {
73 SpectralBandReplication *sbr;
74 408 ExtChannelElement *ext = av_mallocz(sizeof(*ext));
75 int ret;
76 float scale;
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 408 times.
408 if (!ext)
79 return AVERROR(ENOMEM);
80 408 *che = &ext->ch;
81 408 sbr = &ext->sbr;
82 408 ext->ch.ch[0].AAC_RENAME(predictor_state) = ext->predictor_state[0];
83 408 ext->ch.ch[1].AAC_RENAME(predictor_state) = ext->predictor_state[1];
84
85 408 sbr->kx[0] = sbr->kx[1];
86 408 sbr->id_aac = id_aac;
87 408 sbr_turnoff(sbr);
88 408 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
89 408 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 408 scale = USE_FIXED ? 1 : 1.0 / (64 * 32768);
95 408 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 408 times.
408 if (ret < 0)
99 return ret;
100
101 408 scale = USE_FIXED ? -1.0 : -2.0 * 32768;
102 408 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 408 times.
408 if (ret < 0)
106 return ret;
107
108 408 AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
109 408 AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
110 408 aacsbr_func_ptr_init(&sbr->c);
111
112 408 return 0;
113 }
114
115 408 av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(ChannelElement *che)
116 {
117 408 SpectralBandReplication *sbr = get_sbr(che);
118 408 av_tx_uninit(&sbr->mdct);
119 408 av_tx_uninit(&sbr->mdct_ana);
120 408 }
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 sbr->n_q = 1;
603 return -1;
604 }
605
606 65 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
607 65 temp = 0;
608
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 65 times.
266 for (k = 1; k <= sbr->n_q; k++) {
609 201 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
610 201 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
611 }
612
613
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (sbr_hf_calc_npatches(ac, sbr) < 0)
614 return -1;
615
616 65 sbr_make_f_tablelim(sbr);
617
618 65 sbr->data[0].f_indexnoise = 0;
619 65 sbr->data[1].f_indexnoise = 0;
620
621 65 return 0;
622 }
623
624 22940 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 48555 times.
✓ Branch 1 taken 22940 times.
71495 for (i = 0; i < elements; i++) {
629 48555 vec[i] = get_bits1(gb);
630 }
631 22940 }
632
633 /** ceil(log2(index+1)) */
634 static const int8_t ceil_log2[] = {
635 0, 1, 2, 2, 3, 3,
636 };
637
638 7793 static int read_sbr_grid(AACDecContext *ac, SpectralBandReplication *sbr,
639 GetBitContext *gb, SBRData *ch_data)
640 {
641 int i;
642 7793 int bs_pointer = 0;
643 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
644 7793 int abs_bord_trail = 16;
645 int num_rel_lead, num_rel_trail;
646 7793 unsigned bs_num_env_old = ch_data->bs_num_env;
647 int bs_frame_class, bs_num_env;
648
649 7793 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
650 7793 ch_data->bs_amp_res = sbr->bs_amp_res_header;
651 7793 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
652
653
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)) {
654 6395 case FIXFIX:
655 6395 bs_num_env = 1 << get_bits(gb, 2);
656
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)) {
657 av_log(ac->avctx, AV_LOG_ERROR,
658 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
659 bs_num_env);
660 return -1;
661 }
662 6395 ch_data->bs_num_env = bs_num_env;
663 6395 num_rel_lead = ch_data->bs_num_env - 1;
664
2/2
✓ Branch 0 taken 4929 times.
✓ Branch 1 taken 1466 times.
6395 if (ch_data->bs_num_env == 1)
665 4929 ch_data->bs_amp_res = 0;
666
667
668 6395 ch_data->t_env[0] = 0;
669 6395 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
670
671 6395 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
672 6395 ch_data->bs_num_env;
673
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 6395 times.
7861 for (i = 0; i < num_rel_lead; i++)
674 1466 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
675
676 6395 ch_data->bs_freq_res[1] = get_bits1(gb);
677
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 6395 times.
7861 for (i = 1; i < ch_data->bs_num_env; i++)
678 1466 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
679 6395 break;
680 630 case FIXVAR:
681 630 abs_bord_trail += get_bits(gb, 2);
682 630 num_rel_trail = get_bits(gb, 2);
683 630 ch_data->bs_num_env = num_rel_trail + 1;
684 630 ch_data->t_env[0] = 0;
685 630 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
686
687
2/2
✓ Branch 0 taken 1362 times.
✓ Branch 1 taken 630 times.
1992 for (i = 0; i < num_rel_trail; i++)
688 1362 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
689 1362 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
690
691 630 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
692
693
2/2
✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 630 times.
2622 for (i = 0; i < ch_data->bs_num_env; i++)
694 1992 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
695 630 break;
696 620 case VARFIX:
697 620 ch_data->t_env[0] = get_bits(gb, 2);
698 620 num_rel_lead = get_bits(gb, 2);
699 620 ch_data->bs_num_env = num_rel_lead + 1;
700 620 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
701
702
2/2
✓ Branch 0 taken 776 times.
✓ Branch 1 taken 620 times.
1396 for (i = 0; i < num_rel_lead; i++)
703 776 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
704
705 620 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
706
707 620 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
708 620 break;
709 148 case VARVAR:
710 148 ch_data->t_env[0] = get_bits(gb, 2);
711 148 abs_bord_trail += get_bits(gb, 2);
712 148 num_rel_lead = get_bits(gb, 2);
713 148 num_rel_trail = get_bits(gb, 2);
714 148 bs_num_env = num_rel_lead + num_rel_trail + 1;
715
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
148 if (bs_num_env > 5) {
717 av_log(ac->avctx, AV_LOG_ERROR,
718 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
719 bs_num_env);
720 return -1;
721 }
722 148 ch_data->bs_num_env = bs_num_env;
723
724 148 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
725
726
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 148 times.
220 for (i = 0; i < num_rel_lead; i++)
727 72 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
728
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 148 times.
439 for (i = 0; i < num_rel_trail; i++)
729 291 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
730 291 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
731
732 148 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
733
734 148 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
735 148 break;
736 }
737 7793 ch_data->bs_frame_class = bs_frame_class;
738
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7793 times.
7793 av_assert0(bs_pointer >= 0);
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7793 times.
7793 if (bs_pointer > ch_data->bs_num_env + 1) {
741 av_log(ac->avctx, AV_LOG_ERROR,
742 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
743 bs_pointer);
744 return -1;
745 }
746
747
2/2
✓ Branch 0 taken 11760 times.
✓ Branch 1 taken 7793 times.
19553 for (i = 1; i <= ch_data->bs_num_env; i++) {
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11760 times.
11760 if (ch_data->t_env[i-1] >= ch_data->t_env[i]) {
749 av_log(ac->avctx, AV_LOG_ERROR, "Not strictly monotone time borders\n");
750 return -1;
751 }
752 }
753
754
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;
755
756 7793 ch_data->t_q[0] = ch_data->t_env[0];
757 7793 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
758
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 4929 times.
7793 if (ch_data->bs_num_noise > 1) {
759 int idx;
760
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 1398 times.
2864 if (ch_data->bs_frame_class == FIXFIX) {
761 1466 idx = ch_data->bs_num_env >> 1;
762
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 620 times.
1398 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
763
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 175 times.
778 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
764 } else { // VARFIX
765
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 156 times.
620 if (!bs_pointer)
766 464 idx = 1;
767
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 else if (bs_pointer == 1)
768 156 idx = ch_data->bs_num_env - 1;
769 else // bs_pointer > 1
770 idx = bs_pointer - 1;
771 }
772 2864 ch_data->t_q[1] = ch_data->t_env[idx];
773 }
774
775 7793 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
776 7793 ch_data->e_a[1] = -1;
777
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
778 778 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
779
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
780 ch_data->e_a[1] = bs_pointer - 1;
781
782 7793 return 0;
783 }
784
785 2625 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
786 //These variables are saved from the previous frame rather than copied
787 2625 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
788 2625 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
789 2625 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
790
791 //These variables are read from the bitstream and therefore copied
792 2625 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
793 2625 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
794 2625 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
795 2625 dst->bs_num_env = src->bs_num_env;
796 2625 dst->bs_amp_res = src->bs_amp_res;
797 2625 dst->bs_num_noise = src->bs_num_noise;
798 2625 dst->bs_frame_class = src->bs_frame_class;
799 2625 dst->e_a[1] = src->e_a[1];
800 2625 }
801
802 /// Read how the envelope and noise floor data is delta coded
803 10418 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
804 SBRData *ch_data, int indep_flag)
805 {
806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 if (sbr->usac) {
807 if (indep_flag) {
808 ch_data->bs_df_env[0] = 0;
809 get_bits1_vector(gb, &ch_data->bs_df_env[1], ch_data->bs_num_env - 1);
810 } else {
811 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
812 }
813
814 if (indep_flag) {
815 ch_data->bs_df_noise[0] = 0;
816 get_bits1_vector(gb, &ch_data->bs_df_noise[1], ch_data->bs_num_noise - 1);
817 } else {
818 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
819 }
820 } else {
821 10418 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
822 10418 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
823 }
824 10418 }
825
826 /// Read inverse filtering data
827 7793 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
828 SBRData *ch_data)
829 {
830 int i;
831
832 7793 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
833
2/2
✓ Branch 0 taken 23985 times.
✓ Branch 1 taken 7793 times.
31778 for (i = 0; i < sbr->n_q; i++)
834 23985 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
835 7793 }
836
837 10418 static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
838 SBRData *ch_data, int ch)
839 {
840 int bits;
841 int i, j, k;
842 const VLCElem *t_huff, *f_huff;
843
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;
844 10418 const int odd = sbr->n[1] & 1;
845
846
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) {
847
2/2
✓ Branch 0 taken 582 times.
✓ Branch 1 taken 2043 times.
2625 if (ch_data->bs_amp_res) {
848 582 bits = 5;
849 582 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_BAL_3_0DB];
850 582 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_BAL_3_0DB];
851 } else {
852 2043 bits = 6;
853 2043 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_BAL_1_5DB];
854 2043 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_BAL_1_5DB];
855 }
856 } else {
857
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 4929 times.
7793 if (ch_data->bs_amp_res) {
858 2864 bits = 6;
859 2864 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_3_0DB];
860 2864 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_3_0DB];
861 } else {
862 4929 bits = 7;
863 4929 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_ENV_1_5DB];
864 4929 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_1_5DB];
865 }
866 }
867
868
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (i = 0; i < ch_data->bs_num_env; i++) {
869
2/2
✓ Branch 0 taken 4947 times.
✓ Branch 1 taken 10213 times.
15160 if (ch_data->bs_df_env[i]) {
870 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
871
2/2
✓ Branch 0 taken 4454 times.
✓ Branch 1 taken 493 times.
4947 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
872
2/2
✓ Branch 0 taken 51282 times.
✓ Branch 1 taken 4454 times.
55736 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
873 51282 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * get_vlc2(gb, t_huff, 9, 3);
874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51282 times.
51282 if (ch_data->env_facs_q[i + 1][j] > 127U) {
875 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
876 return AVERROR_INVALIDDATA;
877 }
878 }
879
2/2
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 120 times.
493 } else if (ch_data->bs_freq_res[i + 1]) {
880
2/2
✓ Branch 0 taken 4282 times.
✓ Branch 1 taken 373 times.
4655 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
881 4282 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
882 4282 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * get_vlc2(gb, t_huff, 9, 3);
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4282 times.
4282 if (ch_data->env_facs_q[i + 1][j] > 127U) {
884 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
885 return AVERROR_INVALIDDATA;
886 }
887 }
888 } else {
889
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 120 times.
799 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
890
2/2
✓ Branch 0 taken 559 times.
✓ Branch 1 taken 120 times.
679 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
891 679 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * get_vlc2(gb, t_huff, 9, 3);
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 679 times.
679 if (ch_data->env_facs_q[i + 1][j] > 127U) {
893 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
894 return AVERROR_INVALIDDATA;
895 }
896 }
897 }
898 } else {
899 10213 ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
900
2/2
✓ Branch 0 taken 105460 times.
✓ Branch 1 taken 10213 times.
115673 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
901 105460 ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * get_vlc2(gb, f_huff, 9, 3);
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105460 times.
105460 if (ch_data->env_facs_q[i + 1][j] > 127U) {
903 av_log(ac->avctx, AV_LOG_ERROR, "env_facs_q %d is invalid\n", ch_data->env_facs_q[i + 1][j]);
904 return AVERROR_INVALIDDATA;
905 }
906 }
907 }
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15160 times.
15160 if (sbr->usac) {
909 if (sbr->inter_tes) {
910 ch_data->temp_shape[i] = get_bits(gb, 1);
911 if (ch_data->temp_shape[i])
912 ch_data->temp_shape_mode[i] = get_bits(gb, 2);
913 }
914 }
915 }
916
917 //assign 0th elements of env_facs_q from last elements
918 10418 memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],
919 sizeof(ch_data->env_facs_q[0]));
920
921 10418 return 0;
922 }
923
924 10418 static int read_sbr_noise(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
925 SBRData *ch_data, int ch)
926 {
927 int i, j;
928 const VLCElem *t_huff, *f_huff;
929
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;
930
931
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) {
932 2625 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_NOISE_BAL_3_0DB];
933 2625 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_BAL_3_0DB];
934 } else {
935 7793 t_huff = ff_aac_sbr_vlc[T_HUFFMAN_NOISE_3_0DB];
936 7793 f_huff = ff_aac_sbr_vlc[F_HUFFMAN_ENV_3_0DB];
937 }
938
939
2/2
✓ Branch 0 taken 13864 times.
✓ Branch 1 taken 10418 times.
24282 for (i = 0; i < ch_data->bs_num_noise; i++) {
940
2/2
✓ Branch 0 taken 10830 times.
✓ Branch 1 taken 3034 times.
13864 if (ch_data->bs_df_noise[i]) {
941
2/2
✓ Branch 0 taken 32863 times.
✓ Branch 1 taken 10830 times.
43693 for (j = 0; j < sbr->n_q; j++) {
942 32863 ch_data->noise_facs_q[i + 1][j] = ch_data->noise_facs_q[i][j] + delta * get_vlc2(gb, t_huff, 9, 2);
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32863 times.
32863 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
944 av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
945 return AVERROR_INVALIDDATA;
946 }
947 }
948 } else {
949 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
950
2/2
✓ Branch 0 taken 5910 times.
✓ Branch 1 taken 3034 times.
8944 for (j = 1; j < sbr->n_q; j++) {
951 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);
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5910 times.
5910 if (ch_data->noise_facs_q[i + 1][j] > 30U) {
953 av_log(ac->avctx, AV_LOG_ERROR, "noise_facs_q %d is invalid\n", ch_data->noise_facs_q[i + 1][j]);
954 return AVERROR_INVALIDDATA;
955 }
956 }
957 }
958 }
959
960 //assign 0th elements of noise_facs_q from last elements
961 10418 memcpy(ch_data->noise_facs_q[0], ch_data->noise_facs_q[ch_data->bs_num_noise],
962 sizeof(ch_data->noise_facs_q[0]));
963 10418 return 0;
964 }
965
966 1616 static void read_sbr_extension(AACDecContext *ac, SpectralBandReplication *sbr,
967 GetBitContext *gb,
968 int bs_extension_id, int *num_bits_left)
969 {
970
1/2
✓ Branch 0 taken 1616 times.
✗ Branch 1 not taken.
1616 switch (bs_extension_id) {
971 1616 case EXTENSION_ID_PS:
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1616 times.
1616 if (!ac->oc[1].m4ac.ps) {
973 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
974 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
975 *num_bits_left = 0;
976 } else {
977 1616 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps.common, *num_bits_left);
978 1616 ac->avctx->profile = AV_PROFILE_AAC_HE_V2;
979 // ensure the warning is not printed if PS extension is present
980 1616 ac->warned_he_aac_mono = 1;
981 }
982 1616 break;
983 default:
984 // some files contain 0-padding
985 if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
986 avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
987 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
988 *num_bits_left = 0;
989 break;
990 }
991 1616 }
992
993 2646 static int read_sbr_single_channel_element(AACDecContext *ac,
994 SpectralBandReplication *sbr,
995 GetBitContext *gb)
996 {
997 int ret;
998
999
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (get_bits1(gb)) // bs_data_extra
1000 skip_bits(gb, 4); // bs_reserved
1001
1002
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1003 return -1;
1004 2646 read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1005 2646 read_sbr_invf(sbr, gb, &sbr->data[0]);
1006
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)
1007 return ret;
1008
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)
1009 return ret;
1010
1011
2/2
✓ Branch 1 taken 174 times.
✓ Branch 2 taken 2472 times.
2646 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1012 174 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1013
1014 2646 return 0;
1015 }
1016
1017 3886 static int read_sbr_channel_pair_element(AACDecContext *ac,
1018 SpectralBandReplication *sbr,
1019 GetBitContext *gb)
1020 {
1021 int ret;
1022
1023
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3886 times.
3886 if (get_bits1(gb)) // bs_data_extra
1024 skip_bits(gb, 8); // bs_reserved
1025
1026
2/2
✓ Branch 1 taken 2625 times.
✓ Branch 2 taken 1261 times.
3886 if ((sbr->bs_coupling = get_bits1(gb))) {
1027
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2625 times.
2625 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1028 return -1;
1029 2625 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1030 2625 read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1031 2625 read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
1032 2625 read_sbr_invf(sbr, gb, &sbr->data[0]);
1033 2625 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1034 2625 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1035
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)
1036 return ret;
1037
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)
1038 return ret;
1039
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)
1040 return ret;
1041
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)
1042 return ret;
1043 } else {
1044
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]) ||
1045 1261 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1046 return -1;
1047 1261 read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
1048 1261 read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
1049 1261 read_sbr_invf(sbr, gb, &sbr->data[0]);
1050 1261 read_sbr_invf(sbr, gb, &sbr->data[1]);
1051
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)
1052 return ret;
1053
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)
1054 return ret;
1055
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1261 times.
1261 if((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1056 return ret;
1057
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)
1058 return ret;
1059 }
1060
1061
2/2
✓ Branch 1 taken 698 times.
✓ Branch 2 taken 3188 times.
3886 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1062 698 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1063
2/2
✓ Branch 1 taken 464 times.
✓ Branch 2 taken 3422 times.
3886 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1064 464 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1065
1066 3886 return 0;
1067 }
1068
1069 6532 static unsigned int read_sbr_data(AACDecContext *ac, SpectralBandReplication *sbr,
1070 GetBitContext *gb, int id_aac)
1071 {
1072 6532 unsigned int cnt = get_bits_count(gb);
1073
1074 6532 sbr->id_aac = id_aac;
1075 6532 sbr->ready_for_dequant = 1;
1076
1077
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) {
1078
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2646 times.
2646 if (read_sbr_single_channel_element(ac, sbr, gb)) {
1079 sbr_turnoff(sbr);
1080 return get_bits_count(gb) - cnt;
1081 }
1082
1/2
✓ Branch 0 taken 3886 times.
✗ Branch 1 not taken.
3886 } else if (id_aac == TYPE_CPE) {
1083
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3886 times.
3886 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1084 sbr_turnoff(sbr);
1085 return get_bits_count(gb) - cnt;
1086 }
1087 } else {
1088 av_log(ac->avctx, AV_LOG_ERROR,
1089 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1090 sbr_turnoff(sbr);
1091 return get_bits_count(gb) - cnt;
1092 }
1093
2/2
✓ Branch 1 taken 1616 times.
✓ Branch 2 taken 4916 times.
6532 if (get_bits1(gb)) { // bs_extended_data
1094 1616 int num_bits_left = get_bits(gb, 4); // bs_extension_size
1095
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 1358 times.
1616 if (num_bits_left == 15)
1096 258 num_bits_left += get_bits(gb, 8); // bs_esc_count
1097
1098 1616 num_bits_left <<= 3;
1099
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 1616 times.
3232 while (num_bits_left > 7) {
1100 1616 num_bits_left -= 2;
1101 1616 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1102 }
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1616 times.
1616 if (num_bits_left < 0) {
1104 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1105 }
1106
2/2
✓ Branch 0 taken 1451 times.
✓ Branch 1 taken 165 times.
1616 if (num_bits_left > 0)
1107 1451 skip_bits(gb, num_bits_left);
1108 }
1109
1110 6532 return get_bits_count(gb) - cnt;
1111 }
1112
1113 65 static void sbr_reset(AACDecContext *ac, SpectralBandReplication *sbr)
1114 {
1115 int err;
1116 65 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1117
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (err >= 0)
1118 65 err = sbr_make_f_derived(ac, sbr);
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (err < 0) {
1120 av_log(ac->avctx, AV_LOG_ERROR,
1121 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1122 sbr_turnoff(sbr);
1123 }
1124 65 }
1125
1126 /**
1127 * Decode Spectral Band Replication extension data; reference: table 4.55.
1128 *
1129 * @param crc flag indicating the presence of CRC checksum
1130 * @param cnt length of TYPE_FIL syntactic element in bytes
1131 *
1132 * @return Returns number of bytes consumed from the TYPE_FIL element.
1133 */
1134 10901 int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *che,
1135 GetBitContext *gb_host, int crc,
1136 int cnt, int id_aac)
1137 {
1138 10901 SpectralBandReplication *sbr = get_sbr(che);
1139 10901 unsigned int num_sbr_bits = 0, num_align_bits;
1140 unsigned bytes_read;
1141 10901 GetBitContext gbc = *gb_host, *gb = &gbc;
1142 10901 skip_bits_long(gb_host, cnt*8 - 4);
1143
1144 10901 sbr->reset = 0;
1145
1146
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 10819 times.
10901 if (!sbr->sample_rate)
1147 82 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 10883 times.
10901 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 10901 times.
10901 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 10901 sbr->kx[0] = sbr->kx[1];
1158 10901 sbr->m[0] = sbr->m[1];
1159 10901 sbr->kx_and_m_pushed = 1;
1160
1161 10901 num_sbr_bits++;
1162
2/2
✓ Branch 1 taken 489 times.
✓ Branch 2 taken 10412 times.
10901 if (get_bits1(gb)) // bs_header_flag
1163 489 num_sbr_bits += read_sbr_header(sbr, gb, 0);
1164
1165
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 10836 times.
10901 if (sbr->reset)
1166 65 sbr_reset(ac, sbr);
1167
1168
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 4369 times.
10901 if (sbr->start)
1169 6532 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1170
1171 10901 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1172 10901 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1173
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 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 10901 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]))
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 /* if (harmonicSBR) ... */
1291
1292 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1293 return -1;
1294 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1295
1296 read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1297 read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
1298
1299 read_sbr_invf(sbr, gb, &sbr->data[0]);
1300 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0],
1301 sizeof(sbr->data[1].bs_invf_mode[0]));
1302 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0],
1303 sizeof(sbr->data[1].bs_invf_mode[0]));
1304
1305 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1306 return ret;
1307 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1308 return ret;
1309
1310 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1311 return ret;
1312 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1313 return ret;
1314
1315 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1316 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1317 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1318 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1319 } else { /* bs_coupling == 0 */
1320 /* if (harmonicSBR) ... */
1321 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
1322 return -1;
1323 if (read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1324 return -1;
1325
1326 read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
1327 read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
1328
1329 read_sbr_invf(sbr, gb, &sbr->data[0]);
1330 read_sbr_invf(sbr, gb, &sbr->data[1]);
1331
1332 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1333 return ret;
1334 if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1335 return ret;
1336
1337 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
1338 return ret;
1339 if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
1340 return ret;
1341
1342 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1343 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1344 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1345 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1346 }
1347
1348 return 0;
1349 }
1350 #endif
1351
1352 /**
1353 * Analysis QMF Bank (14496-3 sp04 p206)
1354 *
1355 * @param x pointer to the beginning of the first sample window
1356 * @param W array of complex-valued samples split into subbands
1357 */
1358 #ifndef sbr_qmf_analysis
1359 #if USE_FIXED
1360 8328 static void sbr_qmf_analysis(AVFixedDSPContext *dsp, AVTXContext *mdct,
1361 av_tx_fn mdct_fn,
1362 #else
1363 11409 static void sbr_qmf_analysis(AVFloatDSPContext *dsp, AVTXContext *mdct,
1364 av_tx_fn mdct_fn,
1365 #endif /* USE_FIXED */
1366 SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
1367 INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
1368 {
1369 int i;
1370 #if USE_FIXED
1371 int j;
1372 #endif
1373 19737 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1374 19737 memcpy(x+288, in, 1024*sizeof(x[0]));
1375
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
1376 // are not supported
1377 631584 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1378 631584 sbrdsp->sum64x5(z);
1379 631584 sbrdsp->qmf_pre_shuffle(z);
1380 #if USE_FIXED
1381
2/2
✓ Branch 0 taken 17055744 times.
✓ Branch 1 taken 266496 times.
17322240 for (j = 64; j < 128; j++) {
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17055744 times.
17055744 if (z[j] > 1<<24) {
1383 av_log(NULL, AV_LOG_WARNING,
1384 "sbr_qmf_analysis: value %09d too large, setting to %09d\n",
1385 z[j], 1<<24);
1386 z[j] = 1<<24;
1387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17055744 times.
17055744 } else if (z[j] < -(1<<24)) {
1388 av_log(NULL, AV_LOG_WARNING,
1389 "sbr_qmf_analysis: value %09d too small, setting to %09d\n",
1390 z[j], -(1<<24));
1391 z[j] = -(1<<24);
1392 }
1393 }
1394 #endif
1395 631584 mdct_fn(mdct, z, z + 64, sizeof(INTFLOAT));
1396 631584 sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1397 631584 x += 32;
1398 }
1399 19737 }
1400 #endif
1401
1402 /**
1403 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1404 * (14496-3 sp04 p206)
1405 */
1406 #ifndef sbr_qmf_synthesis
1407 21736 static void sbr_qmf_synthesis(AVTXContext *mdct, av_tx_fn mdct_fn,
1408 #if USE_FIXED
1409 SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
1410 #else
1411 SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1412 #endif /* USE_FIXED */
1413 INTFLOAT *out, INTFLOAT X[2][38][64],
1414 INTFLOAT mdct_buf[2][64],
1415 INTFLOAT *v0, int *v_off, const unsigned int div)
1416 {
1417 int i, n;
1418
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;
1419 21736 const int step = 128 >> div;
1420 INTFLOAT *v;
1421
2/2
✓ Branch 0 taken 695552 times.
✓ Branch 1 taken 21736 times.
717288 for (i = 0; i < 32; i++) {
1422
2/2
✓ Branch 0 taken 66284 times.
✓ Branch 1 taken 629268 times.
695552 if (*v_off < step) {
1423 66284 int saved_samples = (1280 - 128) >> div;
1424 66284 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
1425 66284 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1426 } else {
1427 629268 *v_off -= step;
1428 }
1429 695552 v = v0 + *v_off;
1430
2/2
✓ Branch 0 taken 147072 times.
✓ Branch 1 taken 548480 times.
695552 if (div) {
1431
2/2
✓ Branch 0 taken 4706304 times.
✓ Branch 1 taken 147072 times.
4853376 for (n = 0; n < 32; n++) {
1432 4706304 X[0][i][ n] = -X[0][i][n];
1433 4706304 X[0][i][32+n] = X[1][i][31-n];
1434 }
1435 147072 mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1436 147072 sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1437 } else {
1438 548480 sbrdsp->neg_odd_64(X[1][i]);
1439 548480 mdct_fn(mdct, mdct_buf[0], X[0][i], sizeof(INTFLOAT));
1440 548480 mdct_fn(mdct, mdct_buf[1], X[1][i], sizeof(INTFLOAT));
1441 548480 sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1442 }
1443 695552 dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1444 695552 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1445 695552 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1446 695552 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1447 695552 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1448 695552 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1449 695552 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1450 695552 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1451 695552 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1452 695552 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1453 695552 out += 64 >> div;
1454 }
1455 21736 }
1456 #endif
1457
1458 /// Generate the subband filtered lowband
1459 19737 static int sbr_lf_gen(SpectralBandReplication *sbr,
1460 INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
1461 int buf_idx)
1462 {
1463 int i, k;
1464 19737 const int t_HFGen = 8;
1465 19737 const int i_f = 32;
1466 19737 memset(X_low, 0, 32*sizeof(*X_low));
1467
2/2
✓ Branch 0 taken 490630 times.
✓ Branch 1 taken 19737 times.
510367 for (k = 0; k < sbr->kx[1]; k++) {
1468
2/2
✓ Branch 0 taken 15700160 times.
✓ Branch 1 taken 490630 times.
16190790 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1469 15700160 X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1470 15700160 X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1471 }
1472 }
1473 19737 buf_idx = 1-buf_idx;
1474
2/2
✓ Branch 0 taken 491470 times.
✓ Branch 1 taken 19737 times.
511207 for (k = 0; k < sbr->kx[0]; k++) {
1475
2/2
✓ Branch 0 taken 3931760 times.
✓ Branch 1 taken 491470 times.
4423230 for (i = 0; i < t_HFGen; i++) {
1476 3931760 X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1477 3931760 X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1478 }
1479 }
1480 19737 return 0;
1481 }
1482
1483 /// High Frequency Generator (14496-3 sp04 p215)
1484 10418 static int sbr_hf_gen(AACDecContext *ac, SpectralBandReplication *sbr,
1485 INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
1486 const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
1487 const INTFLOAT bw_array[5], const uint8_t *t_env,
1488 int bs_num_env)
1489 {
1490 int j, x;
1491 10418 int g = 0;
1492 10418 int k = sbr->kx[1];
1493
2/2
✓ Branch 0 taken 26191 times.
✓ Branch 1 taken 10418 times.
36609 for (j = 0; j < sbr->num_patches; j++) {
1494
2/2
✓ Branch 0 taken 256742 times.
✓ Branch 1 taken 26191 times.
282933 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1495 256742 const int p = sbr->patch_start_subband[j] + x;
1496
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])
1497 277096 g++;
1498 256742 g--;
1499
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256742 times.
256742 if (g < 0) {
1501 av_log(ac->avctx, AV_LOG_ERROR,
1502 "ERROR : no subband found for frequency %d\n", k);
1503 return -1;
1504 }
1505
1506 256742 sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1507 256742 X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1508 256742 alpha0[p], alpha1[p], bw_array[g],
1509 256742 2 * t_env[0], 2 * t_env[bs_num_env]);
1510 }
1511 }
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 if (k < sbr->m[1] + sbr->kx[1])
1513 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1514
1515 10418 return 0;
1516 }
1517
1518 /// Generate the subband filtered lowband
1519 19737 static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
1520 const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
1521 const INTFLOAT X_low[32][40][2], int ch)
1522 {
1523 int k, i;
1524 19737 const int i_f = 32;
1525 19737 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1526 19737 memset(X, 0, 2*sizeof(*X));
1527
2/2
✓ Branch 0 taken 491470 times.
✓ Branch 1 taken 19737 times.
511207 for (k = 0; k < sbr->kx[0]; k++) {
1528
2/2
✓ Branch 0 taken 55730 times.
✓ Branch 1 taken 491470 times.
547200 for (i = 0; i < i_Temp; i++) {
1529 55730 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1530 55730 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1531 }
1532 }
1533
2/2
✓ Branch 0 taken 254146 times.
✓ Branch 1 taken 19737 times.
273883 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1534
2/2
✓ Branch 0 taken 68266 times.
✓ Branch 1 taken 254146 times.
322412 for (i = 0; i < i_Temp; i++) {
1535 68266 X[0][i][k] = Y0[i + i_f][k][0];
1536 68266 X[1][i][k] = Y0[i + i_f][k][1];
1537 }
1538 }
1539
1540
2/2
✓ Branch 0 taken 490630 times.
✓ Branch 1 taken 19737 times.
510367 for (k = 0; k < sbr->kx[1]; k++) {
1541
2/2
✓ Branch 0 taken 18588210 times.
✓ Branch 1 taken 490630 times.
19078840 for (i = i_Temp; i < 38; i++) {
1542 18588210 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1543 18588210 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1544 }
1545 }
1546
2/2
✓ Branch 0 taken 256742 times.
✓ Branch 1 taken 19737 times.
276479 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1547
2/2
✓ Branch 0 taken 8147478 times.
✓ Branch 1 taken 256742 times.
8404220 for (i = i_Temp; i < i_f; i++) {
1548 8147478 X[0][i][k] = Y1[i][k][0];
1549 8147478 X[1][i][k] = Y1[i][k][1];
1550 }
1551 }
1552 19737 return 0;
1553 }
1554
1555 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1556 * (14496-3 sp04 p217)
1557 */
1558 10418 static int sbr_mapping(AACDecContext *ac, SpectralBandReplication *sbr,
1559 SBRData *ch_data, int e_a[2])
1560 {
1561 int e, i, m;
1562
1563 10418 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1564
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (e = 0; e < ch_data->bs_num_env; e++) {
1565 15160 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1566
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;
1567 int k;
1568
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15160 times.
15160 if (sbr->kx[1] != table[0]) {
1570 av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1571 "Derived frequency tables were not regenerated.\n");
1572 sbr_turnoff(sbr);
1573 return AVERROR_BUG;
1574 }
1575
2/2
✓ Branch 0 taken 171916 times.
✓ Branch 1 taken 15160 times.
187076 for (i = 0; i < ilim; i++)
1576
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 171916 times.
550938 for (m = table[i]; m < table[i + 1]; m++)
1577 379022 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1578
1579 // ch_data->bs_num_noise > 1 => 2 noise floors
1580
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]);
1581
2/2
✓ Branch 0 taken 45584 times.
✓ Branch 1 taken 15160 times.
60744 for (i = 0; i < sbr->n_q; i++)
1582
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++)
1583 379022 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1584
1585
2/2
✓ Branch 0 taken 182216 times.
✓ Branch 1 taken 15160 times.
197376 for (i = 0; i < sbr->n[1]; i++) {
1586
2/2
✓ Branch 0 taken 32694 times.
✓ Branch 1 taken 149522 times.
182216 if (ch_data->bs_add_harmonic_flag) {
1587 32694 const unsigned int m_midpoint =
1588 32694 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1589
1590 32694 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1591
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));
1592 }
1593 }
1594
1595
2/2
✓ Branch 0 taken 171916 times.
✓ Branch 1 taken 15160 times.
187076 for (i = 0; i < ilim; i++) {
1596 171916 int additional_sinusoid_present = 0;
1597
2/2
✓ Branch 0 taken 376296 times.
✓ Branch 1 taken 168186 times.
544482 for (m = table[i]; m < table[i + 1]; m++) {
1598
2/2
✓ Branch 0 taken 3730 times.
✓ Branch 1 taken 372566 times.
376296 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1599 3730 additional_sinusoid_present = 1;
1600 3730 break;
1601 }
1602 }
1603 171916 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1604 171916 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1605 }
1606 }
1607
1608 10418 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1609 10418 return 0;
1610 }
1611
1612 /// Estimation of current envelope (14496-3 sp04 p218)
1613 10418 static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
1614 SpectralBandReplication *sbr, SBRData *ch_data)
1615 {
1616 int e, m;
1617 10418 int kx1 = sbr->kx[1];
1618
1619
1/2
✓ Branch 0 taken 10418 times.
✗ Branch 1 not taken.
10418 if (sbr->bs_interpol_freq) {
1620
2/2
✓ Branch 0 taken 15160 times.
✓ Branch 1 taken 10418 times.
25578 for (e = 0; e < ch_data->bs_num_env; e++) {
1621 #if USE_FIXED
1622 5588 const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
1623 #else
1624 9572 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1625 #endif /* USE_FIXED */
1626 15160 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1627 15160 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1628
1629
2/2
✓ Branch 0 taken 379022 times.
✓ Branch 1 taken 15160 times.
394182 for (m = 0; m < sbr->m[1]; m++) {
1630 379022 AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1631 #if USE_FIXED
1632 127837 e_curr[e][m] = av_mul_sf(sum, recip_env_size);
1633 #else
1634 251185 e_curr[e][m] = sum * recip_env_size;
1635 #endif /* USE_FIXED */
1636 }
1637 }
1638 } else {
1639 int k, p;
1640
1641 for (e = 0; e < ch_data->bs_num_env; e++) {
1642 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1643 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1644 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1645 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1646
1647 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1648 #if USE_FIXED
1649 SoftFloat sum = FLOAT_0;
1650 const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
1651 for (k = table[p]; k < table[p + 1]; k++) {
1652 sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
1653 }
1654 sum = av_mul_sf(sum, den);
1655 #else
1656 float sum = 0.0f;
1657 const int den = env_size * (table[p + 1] - table[p]);
1658
1659 for (k = table[p]; k < table[p + 1]; k++) {
1660 sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1661 }
1662 sum /= den;
1663 #endif /* USE_FIXED */
1664 for (k = table[p]; k < table[p + 1]; k++) {
1665 e_curr[e][k - kx1] = sum;
1666 }
1667 }
1668 }
1669 }
1670 10418 }
1671
1672 12411 void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che,
1673 int id_aac, void *L_, void *R_)
1674 {
1675 12411 INTFLOAT *L = L_, *R = R_;
1676 12411 SpectralBandReplication *sbr = get_sbr(che);
1677 12411 int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1678 int ch;
1679
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 5085 times.
12411 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1680 int err;
1681
1682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12411 times.
12411 if (id_aac != sbr->id_aac) {
1683 av_log(ac->avctx, id_aac == TYPE_LFE ? AV_LOG_VERBOSE : AV_LOG_WARNING,
1684 "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
1685 sbr_turnoff(sbr);
1686 }
1687
1688
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) {
1689 av_log(ac->avctx, AV_LOG_ERROR,
1690 "No quantized data read for sbr_dequant.\n");
1691 sbr_turnoff(sbr);
1692 }
1693
1694
2/2
✓ Branch 0 taken 1510 times.
✓ Branch 1 taken 10901 times.
12411 if (!sbr->kx_and_m_pushed) {
1695 1510 sbr->kx[0] = sbr->kx[1];
1696 1510 sbr->m[0] = sbr->m[1];
1697 } else {
1698 10901 sbr->kx_and_m_pushed = 0;
1699 }
1700
1701
2/2
✓ Branch 0 taken 6532 times.
✓ Branch 1 taken 5879 times.
12411 if (sbr->start) {
1702 6532 sbr_dequant(sbr, id_aac);
1703 6532 sbr->ready_for_dequant = 0;
1704 }
1705
2/2
✓ Branch 0 taken 19737 times.
✓ Branch 1 taken 12411 times.
32148 for (ch = 0; ch < nch; ch++) {
1706 /* decode channel */
1707 19737 sbr_qmf_analysis(ac->fdsp, sbr->mdct_ana, sbr->mdct_ana_fn, &sbr->dsp,
1708 19737 ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1709 19737 (INTFLOAT*)sbr->qmf_filter_scratch,
1710
2/2
✓ Branch 0 taken 7326 times.
✓ Branch 1 taken 12411 times.
19737 sbr->data[ch].W, sbr->data[ch].Ypos);
1711 19737 sbr->c.sbr_lf_gen(sbr, sbr->X_low,
1712 19737 (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
1713 sbr->data[ch].Ypos);
1714 19737 sbr->data[ch].Ypos ^= 1;
1715
2/2
✓ Branch 0 taken 10418 times.
✓ Branch 1 taken 9319 times.
19737 if (sbr->start) {
1716 10418 sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1717 10418 (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
1718 10418 sbr_chirp(sbr, &sbr->data[ch]);
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10418 times.
10418 av_assert0(sbr->data[ch].bs_num_env > 0);
1720 10418 sbr_hf_gen(ac, sbr, sbr->X_high,
1721 10418 (const INTFLOAT (*)[40][2]) sbr->X_low,
1722 10418 (const INTFLOAT (*)[2]) sbr->alpha0,
1723 10418 (const INTFLOAT (*)[2]) sbr->alpha1,
1724 10418 sbr->data[ch].bw_array, sbr->data[ch].t_env,
1725 6631 sbr->data[ch].bs_num_env);
1726
1727 // hf_adj
1728 10418 err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1729
1/2
✓ Branch 0 taken 10418 times.
✗ Branch 1 not taken.
10418 if (!err) {
1730 10418 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1731 10418 sbr_gain_calc(sbr, &sbr->data[ch], sbr->data[ch].e_a);
1732 10418 sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1733 10418 (const INTFLOAT (*)[40][2]) sbr->X_high,
1734 sbr, &sbr->data[ch],
1735 10418 sbr->data[ch].e_a);
1736 }
1737 }
1738
1739 /* synthesis */
1740 19737 sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1741 19737 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1742 19737 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1743 19737 (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
1744 }
1745
1746
2/2
✓ Branch 0 taken 1999 times.
✓ Branch 1 taken 10412 times.
12411 if (ac->oc[1].m4ac.ps == 1) {
1747
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 383 times.
1999 if (sbr->ps.common.start) {
1748 1616 AAC_RENAME(ff_ps_apply)(&sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1749 } else {
1750 383 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1751 }
1752 1999 nch = 2;
1753 }
1754
1755 12411 sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1756 12411 L, sbr->X[0], sbr->qmf_filter_scratch,
1757 12411 sbr->data[0].synthesis_filterbank_samples,
1758 &sbr->data[0].synthesis_filterbank_samples_offset,
1759 downsampled);
1760
2/2
✓ Branch 0 taken 9325 times.
✓ Branch 1 taken 3086 times.
12411 if (nch == 2)
1761 9325 sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
1762 9325 R, sbr->X[1], sbr->qmf_filter_scratch,
1763 9325 sbr->data[1].synthesis_filterbank_samples,
1764 &sbr->data[1].synthesis_filterbank_samples_offset,
1765 downsampled);
1766 12411 }
1767
1768 408 static void aacsbr_func_ptr_init(AACSBRContext *c)
1769 {
1770 408 c->sbr_lf_gen = sbr_lf_gen;
1771 408 c->sbr_hf_assemble = sbr_hf_assemble;
1772 408 c->sbr_x_gen = sbr_x_gen;
1773 408 c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
1774 408 }
1775