94707c9966599256849ee8d33f27a40eaae843b0
[libav.git] / libavcodec / aacsbr.c
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 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * AAC Spectral Band Replication decoding functions
26 * @author Robert Swain ( rob opendot cl )
27 */
28
29 #include "aac.h"
30 #include "sbr.h"
31 #include "aacsbr.h"
32 #include "aacsbrdata.h"
33 #include "fft.h"
34 #include "ps.h"
35
36 #include <stdint.h>
37 #include <float.h>
38
39 #define ENVELOPE_ADJUSTMENT_OFFSET 2
40 #define NOISE_FLOOR_OFFSET 6.0f
41
42 /**
43 * SBR VLC tables
44 */
45 enum {
46 T_HUFFMAN_ENV_1_5DB,
47 F_HUFFMAN_ENV_1_5DB,
48 T_HUFFMAN_ENV_BAL_1_5DB,
49 F_HUFFMAN_ENV_BAL_1_5DB,
50 T_HUFFMAN_ENV_3_0DB,
51 F_HUFFMAN_ENV_3_0DB,
52 T_HUFFMAN_ENV_BAL_3_0DB,
53 F_HUFFMAN_ENV_BAL_3_0DB,
54 T_HUFFMAN_NOISE_3_0DB,
55 T_HUFFMAN_NOISE_BAL_3_0DB,
56 };
57
58 /**
59 * bs_frame_class - frame class of current SBR frame (14496-3 sp04 p98)
60 */
61 enum {
62 FIXFIX,
63 FIXVAR,
64 VARFIX,
65 VARVAR,
66 };
67
68 enum {
69 EXTENSION_ID_PS = 2,
70 };
71
72 static VLC vlc_sbr[10];
73 static const int8_t vlc_sbr_lav[10] =
74 { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
75 static const DECLARE_ALIGNED(16, float, zero64)[64];
76
77 #define SBR_INIT_VLC_STATIC(num, size) \
78 INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
79 sbr_tmp[num].sbr_bits , 1, 1, \
80 sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
81 size)
82
83 #define SBR_VLC_ROW(name) \
84 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
85
86 av_cold void ff_aac_sbr_init(void)
87 {
88 int n;
89 static const struct {
90 const void *sbr_codes, *sbr_bits;
91 const unsigned int table_size, elem_size;
92 } sbr_tmp[] = {
93 SBR_VLC_ROW(t_huffman_env_1_5dB),
94 SBR_VLC_ROW(f_huffman_env_1_5dB),
95 SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
96 SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
97 SBR_VLC_ROW(t_huffman_env_3_0dB),
98 SBR_VLC_ROW(f_huffman_env_3_0dB),
99 SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
100 SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
101 SBR_VLC_ROW(t_huffman_noise_3_0dB),
102 SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
103 };
104
105 // SBR VLC table initialization
106 SBR_INIT_VLC_STATIC(0, 1098);
107 SBR_INIT_VLC_STATIC(1, 1092);
108 SBR_INIT_VLC_STATIC(2, 768);
109 SBR_INIT_VLC_STATIC(3, 1026);
110 SBR_INIT_VLC_STATIC(4, 1058);
111 SBR_INIT_VLC_STATIC(5, 1052);
112 SBR_INIT_VLC_STATIC(6, 544);
113 SBR_INIT_VLC_STATIC(7, 544);
114 SBR_INIT_VLC_STATIC(8, 592);
115 SBR_INIT_VLC_STATIC(9, 512);
116
117 for (n = 1; n < 320; n++)
118 sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
119 sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
120 sbr_qmf_window_us[512] = -sbr_qmf_window_us[512];
121
122 for (n = 0; n < 320; n++)
123 sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
124
125 ff_ps_init();
126 }
127
128 av_cold void ff_aac_sbr_ctx_init(SpectralBandReplication *sbr)
129 {
130 sbr->kx[0] = sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
131 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
132 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
133 sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
134 ff_mdct_init(&sbr->mdct, 7, 1, 1.0/64);
135 ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0);
136 ff_ps_ctx_init(&sbr->ps);
137 }
138
139 av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
140 {
141 ff_mdct_end(&sbr->mdct);
142 ff_mdct_end(&sbr->mdct_ana);
143 }
144
145 static int qsort_comparison_function_int16(const void *a, const void *b)
146 {
147 return *(const int16_t *)a - *(const int16_t *)b;
148 }
149
150 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
151 {
152 int i;
153 for (i = 0; i <= last_el; i++)
154 if (table[i] == needle)
155 return 1;
156 return 0;
157 }
158
159 /// Limiter Frequency Band Table (14496-3 sp04 p198)
160 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
161 {
162 int k;
163 if (sbr->bs_limiter_bands > 0) {
164 static const float bands_warped[3] = { 1.32715174233856803909f, //2^(0.49/1.2)
165 1.18509277094158210129f, //2^(0.49/2)
166 1.11987160404675912501f }; //2^(0.49/3)
167 const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
168 int16_t patch_borders[7];
169 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
170
171 patch_borders[0] = sbr->kx[1];
172 for (k = 1; k <= sbr->num_patches; k++)
173 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
174
175 memcpy(sbr->f_tablelim, sbr->f_tablelow,
176 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
177 if (sbr->num_patches > 1)
178 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
179 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
180
181 qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
182 sizeof(sbr->f_tablelim[0]),
183 qsort_comparison_function_int16);
184
185 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
186 while (out < sbr->f_tablelim + sbr->n_lim) {
187 if (*in >= *out * lim_bands_per_octave_warped) {
188 *++out = *in++;
189 } else if (*in == *out ||
190 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
191 in++;
192 sbr->n_lim--;
193 } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
194 *out = *in++;
195 sbr->n_lim--;
196 } else {
197 *++out = *in++;
198 }
199 }
200 } else {
201 sbr->f_tablelim[0] = sbr->f_tablelow[0];
202 sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
203 sbr->n_lim = 1;
204 }
205 }
206
207 static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
208 {
209 unsigned int cnt = get_bits_count(gb);
210 uint8_t bs_header_extra_1;
211 uint8_t bs_header_extra_2;
212 int old_bs_limiter_bands = sbr->bs_limiter_bands;
213 SpectrumParameters old_spectrum_params;
214
215 sbr->start = 1;
216
217 // Save last spectrum parameters variables to compare to new ones
218 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
219
220 sbr->bs_amp_res_header = get_bits1(gb);
221 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
222 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
223 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
224 skip_bits(gb, 2); // bs_reserved
225
226 bs_header_extra_1 = get_bits1(gb);
227 bs_header_extra_2 = get_bits1(gb);
228
229 if (bs_header_extra_1) {
230 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
231 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
232 sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
233 } else {
234 sbr->spectrum_params.bs_freq_scale = 2;
235 sbr->spectrum_params.bs_alter_scale = 1;
236 sbr->spectrum_params.bs_noise_bands = 2;
237 }
238
239 // Check if spectrum parameters changed
240 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
241 sbr->reset = 1;
242
243 if (bs_header_extra_2) {
244 sbr->bs_limiter_bands = get_bits(gb, 2);
245 sbr->bs_limiter_gains = get_bits(gb, 2);
246 sbr->bs_interpol_freq = get_bits1(gb);
247 sbr->bs_smoothing_mode = get_bits1(gb);
248 } else {
249 sbr->bs_limiter_bands = 2;
250 sbr->bs_limiter_gains = 2;
251 sbr->bs_interpol_freq = 1;
252 sbr->bs_smoothing_mode = 1;
253 }
254
255 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
256 sbr_make_f_tablelim(sbr);
257
258 return get_bits_count(gb) - cnt;
259 }
260
261 static int array_min_int16(const int16_t *array, int nel)
262 {
263 int i, min = array[0];
264 for (i = 1; i < nel; i++)
265 min = FFMIN(array[i], min);
266 return min;
267 }
268
269 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
270 {
271 int k, previous, present;
272 float base, prod;
273
274 base = powf((float)stop / start, 1.0f / num_bands);
275 prod = start;
276 previous = start;
277
278 for (k = 0; k < num_bands-1; k++) {
279 prod *= base;
280 present = lrintf(prod);
281 bands[k] = present - previous;
282 previous = present;
283 }
284 bands[num_bands-1] = stop - previous;
285 }
286
287 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
288 {
289 // Requirements (14496-3 sp04 p205)
290 if (n_master <= 0) {
291 av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
292 return -1;
293 }
294 if (bs_xover_band >= n_master) {
295 av_log(avctx, AV_LOG_ERROR,
296 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
297 bs_xover_band);
298 return -1;
299 }
300 return 0;
301 }
302
303 /// Master Frequency Band Table (14496-3 sp04 p194)
304 static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
305 SpectrumParameters *spectrum)
306 {
307 unsigned int temp, max_qmf_subbands;
308 unsigned int start_min, stop_min;
309 int k;
310 const int8_t *sbr_offset_ptr;
311 int16_t stop_dk[13];
312
313 if (sbr->sample_rate < 32000) {
314 temp = 3000;
315 } else if (sbr->sample_rate < 64000) {
316 temp = 4000;
317 } else
318 temp = 5000;
319
320 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
321 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
322
323 switch (sbr->sample_rate) {
324 case 16000:
325 sbr_offset_ptr = sbr_offset[0];
326 break;
327 case 22050:
328 sbr_offset_ptr = sbr_offset[1];
329 break;
330 case 24000:
331 sbr_offset_ptr = sbr_offset[2];
332 break;
333 case 32000:
334 sbr_offset_ptr = sbr_offset[3];
335 break;
336 case 44100: case 48000: case 64000:
337 sbr_offset_ptr = sbr_offset[4];
338 break;
339 case 88200: case 96000: case 128000: case 176400: case 192000:
340 sbr_offset_ptr = sbr_offset[5];
341 break;
342 default:
343 av_log(ac->avctx, AV_LOG_ERROR,
344 "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
345 return -1;
346 }
347
348 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
349
350 if (spectrum->bs_stop_freq < 14) {
351 sbr->k[2] = stop_min;
352 make_bands(stop_dk, stop_min, 64, 13);
353 qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
354 for (k = 0; k < spectrum->bs_stop_freq; k++)
355 sbr->k[2] += stop_dk[k];
356 } else if (spectrum->bs_stop_freq == 14) {
357 sbr->k[2] = 2*sbr->k[0];
358 } else if (spectrum->bs_stop_freq == 15) {
359 sbr->k[2] = 3*sbr->k[0];
360 } else {
361 av_log(ac->avctx, AV_LOG_ERROR,
362 "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
363 return -1;
364 }
365 sbr->k[2] = FFMIN(64, sbr->k[2]);
366
367 // Requirements (14496-3 sp04 p205)
368 if (sbr->sample_rate <= 32000) {
369 max_qmf_subbands = 48;
370 } else if (sbr->sample_rate == 44100) {
371 max_qmf_subbands = 35;
372 } else if (sbr->sample_rate >= 48000)
373 max_qmf_subbands = 32;
374
375 if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
376 av_log(ac->avctx, AV_LOG_ERROR,
377 "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
378 return -1;
379 }
380
381 if (!spectrum->bs_freq_scale) {
382 unsigned int dk;
383 int k2diff;
384
385 dk = spectrum->bs_alter_scale + 1;
386 sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
387 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
388 return -1;
389
390 for (k = 1; k <= sbr->n_master; k++)
391 sbr->f_master[k] = dk;
392
393 k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
394 if (k2diff < 0) {
395 sbr->f_master[1]--;
396 sbr->f_master[2]-= (k2diff < -1);
397 } else if (k2diff) {
398 sbr->f_master[sbr->n_master]++;
399 }
400
401 sbr->f_master[0] = sbr->k[0];
402 for (k = 1; k <= sbr->n_master; k++)
403 sbr->f_master[k] += sbr->f_master[k - 1];
404
405 } else {
406 int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
407 int two_regions, num_bands_0;
408 int vdk0_max, vdk1_min;
409 int16_t vk0[49];
410
411 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
412 two_regions = 1;
413 sbr->k[1] = 2 * sbr->k[0];
414 } else {
415 two_regions = 0;
416 sbr->k[1] = sbr->k[2];
417 }
418
419 num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
420
421 if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
422 av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
423 return -1;
424 }
425
426 vk0[0] = 0;
427
428 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
429
430 qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
431 vdk0_max = vk0[num_bands_0];
432
433 vk0[0] = sbr->k[0];
434 for (k = 1; k <= num_bands_0; k++) {
435 if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
436 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
437 return -1;
438 }
439 vk0[k] += vk0[k-1];
440 }
441
442 if (two_regions) {
443 int16_t vk1[49];
444 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
445 : 1.0f; // bs_alter_scale = {0,1}
446 int num_bands_1 = lrintf(half_bands * invwarp *
447 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
448
449 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
450
451 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
452
453 if (vdk1_min < vdk0_max) {
454 int change;
455 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
456 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
457 vk1[1] += change;
458 vk1[num_bands_1] -= change;
459 }
460
461 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
462
463 vk1[0] = sbr->k[1];
464 for (k = 1; k <= num_bands_1; k++) {
465 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
466 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
467 return -1;
468 }
469 vk1[k] += vk1[k-1];
470 }
471
472 sbr->n_master = num_bands_0 + num_bands_1;
473 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
474 return -1;
475 memcpy(&sbr->f_master[0], vk0,
476 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
477 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
478 num_bands_1 * sizeof(sbr->f_master[0]));
479
480 } else {
481 sbr->n_master = num_bands_0;
482 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
483 return -1;
484 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
485 }
486 }
487
488 return 0;
489 }
490
491 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
492 static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
493 {
494 int i, k, sb = 0;
495 int msb = sbr->k[0];
496 int usb = sbr->kx[1];
497 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
498
499 sbr->num_patches = 0;
500
501 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
502 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
503 } else
504 k = sbr->n_master;
505
506 do {
507 int odd = 0;
508 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
509 sb = sbr->f_master[i];
510 odd = (sb + sbr->k[0]) & 1;
511 }
512
513 // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
514 // After this check the final number of patches can still be six which is
515 // illegal however the Coding Technologies decoder check stream has a final
516 // count of 6 patches
517 if (sbr->num_patches > 5) {
518 av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
519 return -1;
520 }
521
522 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
523 sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
524
525 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
526 usb = sb;
527 msb = sb;
528 sbr->num_patches++;
529 } else
530 msb = sbr->kx[1];
531
532 if (sbr->f_master[k] - sb < 3)
533 k = sbr->n_master;
534 } while (sb != sbr->kx[1] + sbr->m[1]);
535
536 if (sbr->patch_num_subbands[sbr->num_patches-1] < 3 && sbr->num_patches > 1)
537 sbr->num_patches--;
538
539 return 0;
540 }
541
542 /// Derived Frequency Band Tables (14496-3 sp04 p197)
543 static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
544 {
545 int k, temp;
546
547 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
548 sbr->n[0] = (sbr->n[1] + 1) >> 1;
549
550 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
551 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
552 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
553 sbr->kx[1] = sbr->f_tablehigh[0];
554
555 // Requirements (14496-3 sp04 p205)
556 if (sbr->kx[1] + sbr->m[1] > 64) {
557 av_log(ac->avctx, AV_LOG_ERROR,
558 "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
559 return -1;
560 }
561 if (sbr->kx[1] > 32) {
562 av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
563 return -1;
564 }
565
566 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
567 temp = sbr->n[1] & 1;
568 for (k = 1; k <= sbr->n[0]; k++)
569 sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
570
571 sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
572 log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
573 if (sbr->n_q > 5) {
574 av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
575 return -1;
576 }
577
578 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
579 temp = 0;
580 for (k = 1; k <= sbr->n_q; k++) {
581 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
582 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
583 }
584
585 if (sbr_hf_calc_npatches(ac, sbr) < 0)
586 return -1;
587
588 sbr_make_f_tablelim(sbr);
589
590 sbr->data[0].f_indexnoise = 0;
591 sbr->data[1].f_indexnoise = 0;
592
593 return 0;
594 }
595
596 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
597 int elements)
598 {
599 int i;
600 for (i = 0; i < elements; i++) {
601 vec[i] = get_bits1(gb);
602 }
603 }
604
605 /** ceil(log2(index+1)) */
606 static const int8_t ceil_log2[] = {
607 0, 1, 2, 2, 3, 3,
608 };
609
610 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
611 GetBitContext *gb, SBRData *ch_data)
612 {
613 int i;
614 unsigned bs_pointer = 0;
615 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
616 int abs_bord_trail = 16;
617 int num_rel_lead, num_rel_trail;
618 unsigned bs_num_env_old = ch_data->bs_num_env;
619
620 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
621 ch_data->bs_amp_res = sbr->bs_amp_res_header;
622 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
623
624 switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
625 case FIXFIX:
626 ch_data->bs_num_env = 1 << get_bits(gb, 2);
627 num_rel_lead = ch_data->bs_num_env - 1;
628 if (ch_data->bs_num_env == 1)
629 ch_data->bs_amp_res = 0;
630
631 if (ch_data->bs_num_env > 4) {
632 av_log(ac->avctx, AV_LOG_ERROR,
633 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
634 ch_data->bs_num_env);
635 return -1;
636 }
637
638 ch_data->t_env[0] = 0;
639 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
640
641 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
642 ch_data->bs_num_env;
643 for (i = 0; i < num_rel_lead; i++)
644 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
645
646 ch_data->bs_freq_res[1] = get_bits1(gb);
647 for (i = 1; i < ch_data->bs_num_env; i++)
648 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
649 break;
650 case FIXVAR:
651 abs_bord_trail += get_bits(gb, 2);
652 num_rel_trail = get_bits(gb, 2);
653 ch_data->bs_num_env = num_rel_trail + 1;
654 ch_data->t_env[0] = 0;
655 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
656
657 for (i = 0; i < num_rel_trail; i++)
658 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
659 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
660
661 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
662
663 for (i = 0; i < ch_data->bs_num_env; i++)
664 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
665 break;
666 case VARFIX:
667 ch_data->t_env[0] = get_bits(gb, 2);
668 num_rel_lead = get_bits(gb, 2);
669 ch_data->bs_num_env = num_rel_lead + 1;
670 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
671
672 for (i = 0; i < num_rel_lead; i++)
673 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
674
675 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
676
677 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
678 break;
679 case VARVAR:
680 ch_data->t_env[0] = get_bits(gb, 2);
681 abs_bord_trail += get_bits(gb, 2);
682 num_rel_lead = get_bits(gb, 2);
683 num_rel_trail = get_bits(gb, 2);
684 ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
685
686 if (ch_data->bs_num_env > 5) {
687 av_log(ac->avctx, AV_LOG_ERROR,
688 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
689 ch_data->bs_num_env);
690 return -1;
691 }
692
693 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
694
695 for (i = 0; i < num_rel_lead; i++)
696 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
697 for (i = 0; i < num_rel_trail; i++)
698 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
699 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
700
701 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
702
703 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
704 break;
705 }
706
707 if (bs_pointer > ch_data->bs_num_env + 1) {
708 av_log(ac->avctx, AV_LOG_ERROR,
709 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
710 bs_pointer);
711 return -1;
712 }
713
714 for (i = 1; i <= ch_data->bs_num_env; i++) {
715 if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
716 av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
717 return -1;
718 }
719 }
720
721 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
722
723 ch_data->t_q[0] = ch_data->t_env[0];
724 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
725 if (ch_data->bs_num_noise > 1) {
726 unsigned int idx;
727 if (ch_data->bs_frame_class == FIXFIX) {
728 idx = ch_data->bs_num_env >> 1;
729 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
730 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
731 } else { // VARFIX
732 if (!bs_pointer)
733 idx = 1;
734 else if (bs_pointer == 1)
735 idx = ch_data->bs_num_env - 1;
736 else // bs_pointer > 1
737 idx = bs_pointer - 1;
738 }
739 ch_data->t_q[1] = ch_data->t_env[idx];
740 }
741
742 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
743 ch_data->e_a[1] = -1;
744 if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
745 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
746 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
747 ch_data->e_a[1] = bs_pointer - 1;
748
749 return 0;
750 }
751
752 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
753 //These variables are saved from the previous frame rather than copied
754 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
755 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
756 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
757
758 //These variables are read from the bitstream and therefore copied
759 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
760 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
761 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
762 dst->bs_num_env = src->bs_num_env;
763 dst->bs_amp_res = src->bs_amp_res;
764 dst->bs_num_noise = src->bs_num_noise;
765 dst->bs_frame_class = src->bs_frame_class;
766 dst->e_a[1] = src->e_a[1];
767 }
768
769 /// Read how the envelope and noise floor data is delta coded
770 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
771 SBRData *ch_data)
772 {
773 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
774 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
775 }
776
777 /// Read inverse filtering data
778 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
779 SBRData *ch_data)
780 {
781 int i;
782
783 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
784 for (i = 0; i < sbr->n_q; i++)
785 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
786 }
787
788 static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,
789 SBRData *ch_data, int ch)
790 {
791 int bits;
792 int i, j, k;
793 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
794 int t_lav, f_lav;
795 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
796 const int odd = sbr->n[1] & 1;
797
798 if (sbr->bs_coupling && ch) {
799 if (ch_data->bs_amp_res) {
800 bits = 5;
801 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
802 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
803 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
804 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
805 } else {
806 bits = 6;
807 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
808 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
809 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
810 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
811 }
812 } else {
813 if (ch_data->bs_amp_res) {
814 bits = 6;
815 t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
816 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
817 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
818 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
819 } else {
820 bits = 7;
821 t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
822 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
823 f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
824 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
825 }
826 }
827
828 for (i = 0; i < ch_data->bs_num_env; i++) {
829 if (ch_data->bs_df_env[i]) {
830 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
831 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
832 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
833 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
834 } else if (ch_data->bs_freq_res[i + 1]) {
835 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
836 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
837 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
838 }
839 } else {
840 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
841 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
842 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
843 }
844 }
845 } else {
846 ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
847 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
848 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
849 }
850 }
851
852 //assign 0th elements of env_facs from last elements
853 memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
854 sizeof(ch_data->env_facs[0]));
855 }
856
857 static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
858 SBRData *ch_data, int ch)
859 {
860 int i, j;
861 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
862 int t_lav, f_lav;
863 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
864
865 if (sbr->bs_coupling && ch) {
866 t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
867 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
868 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
869 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
870 } else {
871 t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
872 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
873 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
874 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
875 }
876
877 for (i = 0; i < ch_data->bs_num_noise; i++) {
878 if (ch_data->bs_df_noise[i]) {
879 for (j = 0; j < sbr->n_q; j++)
880 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
881 } else {
882 ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
883 for (j = 1; j < sbr->n_q; j++)
884 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
885 }
886 }
887
888 //assign 0th elements of noise_facs from last elements
889 memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
890 sizeof(ch_data->noise_facs[0]));
891 }
892
893 static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
894 GetBitContext *gb,
895 int bs_extension_id, int *num_bits_left)
896 {
897 switch (bs_extension_id) {
898 case EXTENSION_ID_PS:
899 if (!ac->m4ac.ps) {
900 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
901 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
902 *num_bits_left = 0;
903 } else {
904 #if 1
905 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
906 #else
907 av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
908 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
909 *num_bits_left = 0;
910 #endif
911 }
912 break;
913 default:
914 av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
915 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
916 *num_bits_left = 0;
917 break;
918 }
919 }
920
921 static int read_sbr_single_channel_element(AACContext *ac,
922 SpectralBandReplication *sbr,
923 GetBitContext *gb)
924 {
925 if (get_bits1(gb)) // bs_data_extra
926 skip_bits(gb, 4); // bs_reserved
927
928 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
929 return -1;
930 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
931 read_sbr_invf(sbr, gb, &sbr->data[0]);
932 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
933 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
934
935 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
936 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
937
938 return 0;
939 }
940
941 static int read_sbr_channel_pair_element(AACContext *ac,
942 SpectralBandReplication *sbr,
943 GetBitContext *gb)
944 {
945 if (get_bits1(gb)) // bs_data_extra
946 skip_bits(gb, 8); // bs_reserved
947
948 if ((sbr->bs_coupling = get_bits1(gb))) {
949 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
950 return -1;
951 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
952 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
953 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
954 read_sbr_invf(sbr, gb, &sbr->data[0]);
955 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
956 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
957 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
958 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
959 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
960 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
961 } else {
962 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
963 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
964 return -1;
965 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
966 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
967 read_sbr_invf(sbr, gb, &sbr->data[0]);
968 read_sbr_invf(sbr, gb, &sbr->data[1]);
969 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
970 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
971 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
972 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
973 }
974
975 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
976 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
977 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
978 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
979
980 return 0;
981 }
982
983 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
984 GetBitContext *gb, int id_aac)
985 {
986 unsigned int cnt = get_bits_count(gb);
987
988 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
989 if (read_sbr_single_channel_element(ac, sbr, gb)) {
990 sbr->start = 0;
991 return get_bits_count(gb) - cnt;
992 }
993 } else if (id_aac == TYPE_CPE) {
994 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
995 sbr->start = 0;
996 return get_bits_count(gb) - cnt;
997 }
998 } else {
999 av_log(ac->avctx, AV_LOG_ERROR,
1000 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1001 sbr->start = 0;
1002 return get_bits_count(gb) - cnt;
1003 }
1004 if (get_bits1(gb)) { // bs_extended_data
1005 int num_bits_left = get_bits(gb, 4); // bs_extension_size
1006 if (num_bits_left == 15)
1007 num_bits_left += get_bits(gb, 8); // bs_esc_count
1008
1009 num_bits_left <<= 3;
1010 while (num_bits_left > 7) {
1011 num_bits_left -= 2;
1012 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1013 }
1014 if (num_bits_left < 0) {
1015 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1016 }
1017 if (num_bits_left > 0)
1018 skip_bits(gb, num_bits_left);
1019 }
1020
1021 return get_bits_count(gb) - cnt;
1022 }
1023
1024 static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
1025 {
1026 int err;
1027 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1028 if (err >= 0)
1029 err = sbr_make_f_derived(ac, sbr);
1030 if (err < 0) {
1031 av_log(ac->avctx, AV_LOG_ERROR,
1032 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1033 sbr->start = 0;
1034 }
1035 }
1036
1037 /**
1038 * Decode Spectral Band Replication extension data; reference: table 4.55.
1039 *
1040 * @param crc flag indicating the presence of CRC checksum
1041 * @param cnt length of TYPE_FIL syntactic element in bytes
1042 *
1043 * @return Returns number of bytes consumed from the TYPE_FIL element.
1044 */
1045 int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
1046 GetBitContext *gb_host, int crc, int cnt, int id_aac)
1047 {
1048 unsigned int num_sbr_bits = 0, num_align_bits;
1049 unsigned bytes_read;
1050 GetBitContext gbc = *gb_host, *gb = &gbc;
1051 skip_bits_long(gb_host, cnt*8 - 4);
1052
1053 sbr->reset = 0;
1054
1055 if (!sbr->sample_rate)
1056 sbr->sample_rate = 2 * ac->m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1057 if (!ac->m4ac.ext_sample_rate)
1058 ac->m4ac.ext_sample_rate = 2 * ac->m4ac.sample_rate;
1059
1060 if (crc) {
1061 skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1062 num_sbr_bits += 10;
1063 }
1064
1065 //Save some state from the previous frame.
1066 sbr->kx[0] = sbr->kx[1];
1067 sbr->m[0] = sbr->m[1];
1068
1069 num_sbr_bits++;
1070 if (get_bits1(gb)) // bs_header_flag
1071 num_sbr_bits += read_sbr_header(sbr, gb);
1072
1073 if (sbr->reset)
1074 sbr_reset(ac, sbr);
1075
1076 if (sbr->start)
1077 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1078
1079 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1080 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1081
1082 if (bytes_read > cnt) {
1083 av_log(ac->avctx, AV_LOG_ERROR,
1084 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1085 }
1086 return cnt;
1087 }
1088
1089 /// Dequantization and stereo decoding (14496-3 sp04 p203)
1090 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
1091 {
1092 int k, e;
1093 int ch;
1094
1095 if (id_aac == TYPE_CPE && sbr->bs_coupling) {
1096 float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f;
1097 float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
1098 for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
1099 for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
1100 float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
1101 float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
1102 float fac = temp1 / (1.0f + temp2);
1103 sbr->data[0].env_facs[e][k] = fac;
1104 sbr->data[1].env_facs[e][k] = fac * temp2;
1105 }
1106 }
1107 for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
1108 for (k = 0; k < sbr->n_q; k++) {
1109 float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
1110 float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
1111 float fac = temp1 / (1.0f + temp2);
1112 sbr->data[0].noise_facs[e][k] = fac;
1113 sbr->data[1].noise_facs[e][k] = fac * temp2;
1114 }
1115 }
1116 } else { // SCE or one non-coupled CPE
1117 for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
1118 float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
1119 for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
1120 for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++)
1121 sbr->data[ch].env_facs[e][k] =
1122 exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
1123 for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
1124 for (k = 0; k < sbr->n_q; k++)
1125 sbr->data[ch].noise_facs[e][k] =
1126 exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
1127 }
1128 }
1129 }
1130
1131 /**
1132 * Analysis QMF Bank (14496-3 sp04 p206)
1133 *
1134 * @param x pointer to the beginning of the first sample window
1135 * @param W array of complex-valued samples split into subbands
1136 */
1137 static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
1138 float z[320], float W[2][32][32][2],
1139 float scale)
1140 {
1141 int i, k;
1142 memcpy(W[0], W[1], sizeof(W[0]));
1143 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1144 if (scale != 1.0f)
1145 dsp->vector_fmul_scalar(x+288, in, scale, 1024);
1146 else
1147 memcpy(x+288, in, 1024*sizeof(*x));
1148 for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1149 // are not supported
1150 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1151 for (k = 0; k < 64; k++) {
1152 float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
1153 z[k] = f;
1154 }
1155 //Shuffle to IMDCT
1156 z[64] = z[0];
1157 for (k = 1; k < 32; k++) {
1158 z[64+2*k-1] = z[ k];
1159 z[64+2*k ] = -z[64-k];
1160 }
1161 z[64+63] = z[32];
1162
1163 ff_imdct_half(mdct, z, z+64);
1164 for (k = 0; k < 32; k++) {
1165 W[1][i][k][0] = -z[63-k];
1166 W[1][i][k][1] = z[k];
1167 }
1168 x += 32;
1169 }
1170 }
1171
1172 /**
1173 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1174 * (14496-3 sp04 p206)
1175 */
1176 static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
1177 float *out, float X[2][38][64],
1178 float mdct_buf[2][64],
1179 float *v0, int *v_off, const unsigned int div,
1180 float bias, float scale)
1181 {
1182 int i, n;
1183 const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1184 int scale_and_bias = scale != 1.0f || bias != 0.0f;
1185 float *v;
1186 for (i = 0; i < 32; i++) {
1187 if (*v_off == 0) {
1188 int saved_samples = (1280 - 128) >> div;
1189 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float));
1190 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - (128 >> div);
1191 } else {
1192 *v_off -= 128 >> div;
1193 }
1194 v = v0 + *v_off;
1195 if (div) {
1196 for (n = 0; n < 32; n++) {
1197 X[0][i][ n] = -X[0][i][n];
1198 X[0][i][32+n] = X[1][i][31-n];
1199 }
1200 ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
1201 for (n = 0; n < 32; n++) {
1202 v[ n] = mdct_buf[0][63 - 2*n];
1203 v[63 - n] = -mdct_buf[0][62 - 2*n];
1204 }
1205 } else {
1206 for (n = 1; n < 64; n+=2) {
1207 X[1][i][n] = -X[1][i][n];
1208 }
1209 ff_imdct_half(mdct, mdct_buf[0], X[0][i]);
1210 ff_imdct_half(mdct, mdct_buf[1], X[1][i]);
1211 for (n = 0; n < 64; n++) {
1212 v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ];
1213 v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ];
1214 }
1215 }
1216 dsp->vector_fmul_add(out, v , sbr_qmf_window , zero64, 64 >> div);
1217 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1218 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1219 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1220 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1221 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1222 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1223 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1224 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1225 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1226 if (scale_and_bias)
1227 for (n = 0; n < 64 >> div; n++)
1228 out[n] = out[n] * scale + bias;
1229 out += 64 >> div;
1230 }
1231 }
1232
1233 static void autocorrelate(const float x[40][2], float phi[3][2][2], int lag)
1234 {
1235 int i;
1236 float real_sum = 0.0f;
1237 float imag_sum = 0.0f;
1238 if (lag) {
1239 for (i = 1; i < 38; i++) {
1240 real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1];
1241 imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0];
1242 }
1243 phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1];
1244 phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0];
1245 if (lag == 1) {
1246 phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1];
1247 phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0];
1248 }
1249 } else {
1250 for (i = 1; i < 38; i++) {
1251 real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1];
1252 }
1253 phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
1254 phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1];
1255 }
1256 }
1257
1258 /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
1259 * (14496-3 sp04 p214)
1260 * Warning: This routine does not seem numerically stable.
1261 */
1262 static void sbr_hf_inverse_filter(float (*alpha0)[2], float (*alpha1)[2],
1263 const float X_low[32][40][2], int k0)
1264 {
1265 int k;
1266 for (k = 0; k < k0; k++) {
1267 float phi[3][2][2], dk;
1268
1269 autocorrelate(X_low[k], phi, 0);
1270 autocorrelate(X_low[k], phi, 1);
1271 autocorrelate(X_low[k], phi, 2);
1272
1273 dk = phi[2][1][0] * phi[1][0][0] -
1274 (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
1275
1276 if (!dk) {
1277 alpha1[k][0] = 0;
1278 alpha1[k][1] = 0;
1279 } else {
1280 float temp_real, temp_im;
1281 temp_real = phi[0][0][0] * phi[1][1][0] -
1282 phi[0][0][1] * phi[1][1][1] -
1283 phi[0][1][0] * phi[1][0][0];
1284 temp_im = phi[0][0][0] * phi[1][1][1] +
1285 phi[0][0][1] * phi[1][1][0] -
1286 phi[0][1][1] * phi[1][0][0];
1287
1288 alpha1[k][0] = temp_real / dk;
1289 alpha1[k][1] = temp_im / dk;
1290 }
1291
1292 if (!phi[1][0][0]) {
1293 alpha0[k][0] = 0;
1294 alpha0[k][1] = 0;
1295 } else {
1296 float temp_real, temp_im;
1297 temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
1298 alpha1[k][1] * phi[1][1][1];
1299 temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
1300 alpha1[k][0] * phi[1][1][1];
1301
1302 alpha0[k][0] = -temp_real / phi[1][0][0];
1303 alpha0[k][1] = -temp_im / phi[1][0][0];
1304 }
1305
1306 if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
1307 alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
1308 alpha1[k][0] = 0;
1309 alpha1[k][1] = 0;
1310 alpha0[k][0] = 0;
1311 alpha0[k][1] = 0;
1312 }
1313 }
1314 }
1315
1316 /// Chirp Factors (14496-3 sp04 p214)
1317 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
1318 {
1319 int i;
1320 float new_bw;
1321 static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
1322
1323 for (i = 0; i < sbr->n_q; i++) {
1324 if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
1325 new_bw = 0.6f;
1326 } else
1327 new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
1328
1329 if (new_bw < ch_data->bw_array[i]) {
1330 new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
1331 } else
1332 new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
1333 ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
1334 }
1335 }
1336
1337 /// Generate the subband filtered lowband
1338 static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
1339 float X_low[32][40][2], const float W[2][32][32][2])
1340 {
1341 int i, k;
1342 const int t_HFGen = 8;
1343 const int i_f = 32;
1344 memset(X_low, 0, 32*sizeof(*X_low));
1345 for (k = 0; k < sbr->kx[1]; k++) {
1346 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1347 X_low[k][i][0] = W[1][i - t_HFGen][k][0];
1348 X_low[k][i][1] = W[1][i - t_HFGen][k][1];
1349 }
1350 }
1351 for (k = 0; k < sbr->kx[0]; k++) {
1352 for (i = 0; i < t_HFGen; i++) {
1353 X_low[k][i][0] = W[0][i + i_f - t_HFGen][k][0];
1354 X_low[k][i][1] = W[0][i + i_f - t_HFGen][k][1];
1355 }
1356 }
1357 return 0;
1358 }
1359
1360 /// High Frequency Generator (14496-3 sp04 p215)
1361 static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
1362 float X_high[64][40][2], const float X_low[32][40][2],
1363 const float (*alpha0)[2], const float (*alpha1)[2],
1364 const float bw_array[5], const uint8_t *t_env,
1365 int bs_num_env)
1366 {
1367 int i, j, x;
1368 int g = 0;
1369 int k = sbr->kx[1];
1370 for (j = 0; j < sbr->num_patches; j++) {
1371 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1372 float alpha[4];
1373 const int p = sbr->patch_start_subband[j] + x;
1374 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1375 g++;
1376 g--;
1377
1378 if (g < 0) {
1379 av_log(ac->avctx, AV_LOG_ERROR,
1380 "ERROR : no subband found for frequency %d\n", k);
1381 return -1;
1382 }
1383
1384 alpha[0] = alpha1[p][0] * bw_array[g] * bw_array[g];
1385 alpha[1] = alpha1[p][1] * bw_array[g] * bw_array[g];
1386 alpha[2] = alpha0[p][0] * bw_array[g];
1387 alpha[3] = alpha0[p][1] * bw_array[g];
1388
1389 for (i = 2 * t_env[0]; i < 2 * t_env[bs_num_env]; i++) {
1390 const int idx = i + ENVELOPE_ADJUSTMENT_OFFSET;
1391 X_high[k][idx][0] =
1392 X_low[p][idx - 2][0] * alpha[0] -
1393 X_low[p][idx - 2][1] * alpha[1] +
1394 X_low[p][idx - 1][0] * alpha[2] -
1395 X_low[p][idx - 1][1] * alpha[3] +
1396 X_low[p][idx][0];
1397 X_high[k][idx][1] =
1398 X_low[p][idx - 2][1] * alpha[0] +
1399 X_low[p][idx - 2][0] * alpha[1] +
1400 X_low[p][idx - 1][1] * alpha[2] +
1401 X_low[p][idx - 1][0] * alpha[3] +
1402 X_low[p][idx][1];
1403 }
1404 }
1405 }
1406 if (k < sbr->m[1] + sbr->kx[1])
1407 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1408
1409 return 0;
1410 }
1411
1412 /// Generate the subband filtered lowband
1413 static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
1414 const float X_low[32][40][2], const float Y[2][38][64][2],
1415 int ch)
1416 {
1417 int k, i;
1418 const int i_f = 32;
1419 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1420 memset(X, 0, 2*sizeof(*X));
1421 for (k = 0; k < sbr->kx[0]; k++) {
1422 for (i = 0; i < i_Temp; i++) {
1423 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1424 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1425 }
1426 }
1427 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1428 for (i = 0; i < i_Temp; i++) {
1429 X[0][i][k] = Y[0][i + i_f][k][0];
1430 X[1][i][k] = Y[0][i + i_f][k][1];
1431 }
1432 }
1433
1434 for (k = 0; k < sbr->kx[1]; k++) {
1435 for (i = i_Temp; i < 38; i++) {
1436 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1437 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1438 }
1439 }
1440 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1441 for (i = i_Temp; i < i_f; i++) {
1442 X[0][i][k] = Y[1][i][k][0];
1443 X[1][i][k] = Y[1][i][k][1];
1444 }
1445 }
1446 return 0;
1447 }
1448
1449 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1450 * (14496-3 sp04 p217)
1451 */
1452 static void sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
1453 SBRData *ch_data, int e_a[2])
1454 {
1455 int e, i, m;
1456
1457 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1458 for (e = 0; e < ch_data->bs_num_env; e++) {
1459 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1460 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1461 int k;
1462
1463 for (i = 0; i < ilim; i++)
1464 for (m = table[i]; m < table[i + 1]; m++)
1465 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1466
1467 // ch_data->bs_num_noise > 1 => 2 noise floors
1468 k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1469 for (i = 0; i < sbr->n_q; i++)
1470 for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1471 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1472
1473 for (i = 0; i < sbr->n[1]; i++) {
1474 if (ch_data->bs_add_harmonic_flag) {
1475 const unsigned int m_midpoint =
1476 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1477
1478 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1479 (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1480 }
1481 }
1482
1483 for (i = 0; i < ilim; i++) {
1484 int additional_sinusoid_present = 0;
1485 for (m = table[i]; m < table[i + 1]; m++) {
1486 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1487 additional_sinusoid_present = 1;
1488 break;
1489 }
1490 }
1491 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1492 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1493 }
1494 }
1495
1496 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1497 }
1498
1499 /// Estimation of current envelope (14496-3 sp04 p218)
1500 static void sbr_env_estimate(float (*e_curr)[48], float X_high[64][40][2],
1501 SpectralBandReplication *sbr, SBRData *ch_data)
1502 {
1503 int e, i, m;
1504
1505 if (sbr->bs_interpol_freq) {
1506 for (e = 0; e < ch_data->bs_num_env; e++) {
1507 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1508 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1509 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1510
1511 for (m = 0; m < sbr->m[1]; m++) {
1512 float sum = 0.0f;
1513
1514 for (i = ilb; i < iub; i++) {
1515 sum += X_high[m + sbr->kx[1]][i][0] * X_high[m + sbr->kx[1]][i][0] +
1516 X_high[m + sbr->kx[1]][i][1] * X_high[m + sbr->kx[1]][i][1];
1517 }
1518 e_curr[e][m] = sum * recip_env_size;
1519 }
1520 }
1521 } else {
1522 int k, p;
1523
1524 for (e = 0; e < ch_data->bs_num_env; e++) {
1525 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1526 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1527 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1528 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1529
1530 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1531 float sum = 0.0f;
1532 const int den = env_size * (table[p + 1] - table[p]);
1533
1534 for (k = table[p]; k < table[p + 1]; k++) {
1535 for (i = ilb; i < iub; i++) {
1536 sum += X_high[k][i][0] * X_high[k][i][0] +
1537 X_high[k][i][1] * X_high[k][i][1];
1538 }
1539 }
1540 sum /= den;
1541 for (k = table[p]; k < table[p + 1]; k++) {
1542 e_curr[e][k - sbr->kx[1]] = sum;
1543 }
1544 }
1545 }
1546 }
1547 }
1548
1549 /**
1550 * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
1551 * and Calculation of gain (14496-3 sp04 p219)
1552 */
1553 static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr,
1554 SBRData *ch_data, const int e_a[2])
1555 {
1556 int e, k, m;
1557 // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
1558 static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
1559
1560 for (e = 0; e < ch_data->bs_num_env; e++) {
1561 int delta = !((e == e_a[1]) || (e == e_a[0]));
1562 for (k = 0; k < sbr->n_lim; k++) {
1563 float gain_boost, gain_max;
1564 float sum[2] = { 0.0f, 0.0f };
1565 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1566 const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
1567 sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
1568 sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
1569 if (!sbr->s_mapped[e][m]) {
1570 sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
1571 ((1.0f + sbr->e_curr[e][m]) *
1572 (1.0f + sbr->q_mapped[e][m] * delta)));
1573 } else {
1574 sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
1575 ((1.0f + sbr->e_curr[e][m]) *
1576 (1.0f + sbr->q_mapped[e][m])));
1577 }
1578 }
1579 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1580 sum[0] += sbr->e_origmapped[e][m];
1581 sum[1] += sbr->e_curr[e][m];
1582 }
1583 gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1584 gain_max = FFMIN(100000, gain_max);
1585 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1586 float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
1587 sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max);
1588 sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
1589 }
1590 sum[0] = sum[1] = 0.0f;
1591 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1592 sum[0] += sbr->e_origmapped[e][m];
1593 sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
1594 + sbr->s_m[e][m] * sbr->s_m[e][m]
1595 + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
1596 }
1597 gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1598 gain_boost = FFMIN(1.584893192, gain_boost);
1599 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
1600 sbr->gain[e][m] *= gain_boost;
1601 sbr->q_m[e][m] *= gain_boost;
1602 sbr->s_m[e][m] *= gain_boost;
1603 }
1604 }
1605 }
1606 }
1607
1608 /// Assembling HF Signals (14496-3 sp04 p220)
1609 static void sbr_hf_assemble(float Y[2][38][64][2], const float X_high[64][40][2],
1610 SpectralBandReplication *sbr, SBRData *ch_data,
1611 const int e_a[2])
1612 {
1613 int e, i, j, m;
1614 const int h_SL = 4 * !sbr->bs_smoothing_mode;
1615 const int kx = sbr->kx[1];
1616 const int m_max = sbr->m[1];
1617 static const float h_smooth[5] = {
1618 0.33333333333333,
1619 0.30150283239582,
1620 0.21816949906249,
1621 0.11516383427084,
1622 0.03183050093751,
1623 };
1624 static const int8_t phi[2][4] = {
1625 { 1, 0, -1, 0}, // real
1626 { 0, 1, 0, -1}, // imaginary
1627 };
1628 float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
1629 int indexnoise = ch_data->f_indexnoise;
1630 int indexsine = ch_data->f_indexsine;
1631 memcpy(Y[0], Y[1], sizeof(Y[0]));
1632
1633 if (sbr->reset) {
1634 for (i = 0; i < h_SL; i++) {
1635 memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
1636 memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
1637 }
1638 } else if (h_SL) {
1639 memcpy(g_temp[2*ch_data->t_env[0]], g_temp[2*ch_data->t_env_num_env_old], 4*sizeof(g_temp[0]));
1640 memcpy(q_temp[2*ch_data->t_env[0]], q_temp[2*ch_data->t_env_num_env_old], 4*sizeof(q_temp[0]));
1641 }
1642
1643 for (e = 0; e < ch_data->bs_num_env; e++) {
1644 for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1645 memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
1646 memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
1647 }
1648 }
1649
1650 for (e = 0; e < ch_data->bs_num_env; e++) {
1651 for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1652 int phi_sign = (1 - 2*(kx & 1));
1653
1654 if (h_SL && e != e_a[0] && e != e_a[1]) {
1655 for (m = 0; m < m_max; m++) {
1656 const int idx1 = i + h_SL;
1657 float g_filt = 0.0f;
1658 for (j = 0; j <= h_SL; j++)
1659 g_filt += g_temp[idx1 - j][m] * h_smooth[j];
1660 Y[1][i][m + kx][0] =
1661 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt;
1662 Y[1][i][m + kx][1] =
1663 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt;
1664 }
1665 } else {
1666 for (m = 0; m < m_max; m++) {
1667 const float g_filt = g_temp[i + h_SL][m];
1668 Y[1][i][m + kx][0] =
1669 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt;
1670 Y[1][i][m + kx][1] =
1671 X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt;
1672 }
1673 }
1674
1675 if (e != e_a[0] && e != e_a[1]) {
1676 for (m = 0; m < m_max; m++) {
1677 indexnoise = (indexnoise + 1) & 0x1ff;
1678 if (sbr->s_m[e][m]) {
1679 Y[1][i][m + kx][0] +=
1680 sbr->s_m[e][m] * phi[0][indexsine];
1681 Y[1][i][m + kx][1] +=
1682 sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign);
1683 } else {
1684 float q_filt;
1685 if (h_SL) {
1686 const int idx1 = i + h_SL;
1687 q_filt = 0.0f;
1688 for (j = 0; j <= h_SL; j++)
1689 q_filt += q_temp[idx1 - j][m] * h_smooth[j];
1690 } else {
1691 q_filt = q_temp[i][m];
1692 }
1693 Y[1][i][m + kx][0] +=
1694 q_filt * sbr_noise_table[indexnoise][0];
1695 Y[1][i][m + kx][1] +=
1696 q_filt * sbr_noise_table[indexnoise][1];
1697 }
1698 phi_sign = -phi_sign;
1699 }
1700 } else {
1701 indexnoise = (indexnoise + m_max) & 0x1ff;
1702 for (m = 0; m < m_max; m++) {
1703 Y[1][i][m + kx][0] +=
1704 sbr->s_m[e][m] * phi[0][indexsine];
1705 Y[1][i][m + kx][1] +=
1706 sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign);
1707 phi_sign = -phi_sign;
1708 }
1709 }
1710 indexsine = (indexsine + 1) & 3;
1711 }
1712 }
1713 ch_data->f_indexnoise = indexnoise;
1714 ch_data->f_indexsine = indexsine;
1715 }
1716
1717 void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
1718 float* L, float* R)
1719 {
1720 int downsampled = ac->m4ac.ext_sample_rate < sbr->sample_rate;
1721 int ch;
1722 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1723
1724 if (sbr->start) {
1725 sbr_dequant(sbr, id_aac);
1726 }
1727 for (ch = 0; ch < nch; ch++) {
1728 /* decode channel */
1729 sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1730 (float*)sbr->qmf_filter_scratch,
1731 sbr->data[ch].W, 1/(-1024 * ac->sf_scale));
1732 sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
1733 if (sbr->start) {
1734 sbr_hf_inverse_filter(sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
1735 sbr_chirp(sbr, &sbr->data[ch]);
1736 sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1,
1737 sbr->data[ch].bw_array, sbr->data[ch].t_env,
1738 sbr->data[ch].bs_num_env);
1739
1740 // hf_adj
1741 sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1742 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1743 sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1744 sbr_hf_assemble(sbr->data[ch].Y, sbr->X_high, sbr, &sbr->data[ch],
1745 sbr->data[ch].e_a);
1746 }
1747
1748 /* synthesis */
1749 sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch);
1750 }
1751
1752 if (ac->m4ac.ps == 1) {
1753 if (sbr->ps.start) {
1754 ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1755 } else {
1756 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1757 }
1758 nch = 2;
1759 }
1760
1761 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch,
1762 sbr->data[0].synthesis_filterbank_samples,
1763 &sbr->data[0].synthesis_filterbank_samples_offset,
1764 downsampled,
1765 ac->add_bias, -1024 * ac->sf_scale);
1766 if (nch == 2)
1767 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, R, sbr->X[1], sbr->qmf_filter_scratch,
1768 sbr->data[1].synthesis_filterbank_samples,
1769 &sbr->data[1].synthesis_filterbank_samples_offset,
1770 downsampled,
1771 ac->add_bias, -1024 * ac->sf_scale);
1772 }