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