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