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>
8 * MIPS Technologies, Inc., California.
10 * This file is part of FFmpeg.
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 * AAC Spectral Band Replication decoding functions
30 * @author Robert Swain ( rob opendot cl )
31 * @author Stanislav Ocovaj ( stanislav.ocovaj@imgtec.com )
32 * @author Zoran Basaric ( zoran.basaric@imgtec.com )
35 #include "libavutil/qsort.h"
37 av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
40 const void *sbr_codes, *sbr_bits;
41 const unsigned int table_size, elem_size;
43 SBR_VLC_ROW(t_huffman_env_1_5dB),
44 SBR_VLC_ROW(f_huffman_env_1_5dB),
45 SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
46 SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
47 SBR_VLC_ROW(t_huffman_env_3_0dB),
48 SBR_VLC_ROW(f_huffman_env_3_0dB),
49 SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
50 SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
51 SBR_VLC_ROW(t_huffman_noise_3_0dB),
52 SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
55 // SBR VLC table initialization
56 SBR_INIT_VLC_STATIC(0, 1098);
57 SBR_INIT_VLC_STATIC(1, 1092);
58 SBR_INIT_VLC_STATIC(2, 768);
59 SBR_INIT_VLC_STATIC(3, 1026);
60 SBR_INIT_VLC_STATIC(4, 1058);
61 SBR_INIT_VLC_STATIC(5, 1052);
62 SBR_INIT_VLC_STATIC(6, 544);
63 SBR_INIT_VLC_STATIC(7, 544);
64 SBR_INIT_VLC_STATIC(8, 592);
65 SBR_INIT_VLC_STATIC(9, 512);
69 AAC_RENAME(ff_ps_init)();
72 /** Places SBR in pure upsampling mode. */
73 static void sbr_turnoff(SpectralBandReplication *sbr) {
75 // Init defults used in pure upsampling mode
76 sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
78 // Reset values for first SBR header
79 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
80 memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
83 av_cold void AAC_RENAME(ff_aac_sbr_ctx_init)(AACContext *ac, SpectralBandReplication *sbr)
85 if(sbr->mdct.mdct_bits)
87 sbr->kx[0] = sbr->kx[1];
89 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
90 sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
91 /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
92 * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
93 * and scale back down at synthesis. */
94 AAC_RENAME_32(ff_mdct_init)(&sbr->mdct, 7, 1, 1.0 / (64 * 32768.0));
95 AAC_RENAME_32(ff_mdct_init)(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
96 AAC_RENAME(ff_ps_ctx_init)(&sbr->ps);
97 AAC_RENAME(ff_sbrdsp_init)(&sbr->dsp);
98 aacsbr_func_ptr_init(&sbr->c);
101 av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(SpectralBandReplication *sbr)
103 AAC_RENAME_32(ff_mdct_end)(&sbr->mdct);
104 AAC_RENAME_32(ff_mdct_end)(&sbr->mdct_ana);
107 static int qsort_comparison_function_int16(const void *a, const void *b)
109 return *(const int16_t *)a - *(const int16_t *)b;
112 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
115 for (i = 0; i <= last_el; i++)
116 if (table[i] == needle)
121 /// Limiter Frequency Band Table (14496-3 sp04 p198)
122 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
125 if (sbr->bs_limiter_bands > 0) {
126 static const INTFLOAT bands_warped[3] = { Q23(1.32715174233856803909f), //2^(0.49/1.2)
127 Q23(1.18509277094158210129f), //2^(0.49/2)
128 Q23(1.11987160404675912501f) }; //2^(0.49/3)
129 const INTFLOAT lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
130 int16_t patch_borders[7];
131 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
133 patch_borders[0] = sbr->kx[1];
134 for (k = 1; k <= sbr->num_patches; k++)
135 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
137 memcpy(sbr->f_tablelim, sbr->f_tablelow,
138 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
139 if (sbr->num_patches > 1)
140 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
141 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
143 AV_QSORT(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
145 qsort_comparison_function_int16);
147 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
148 while (out < sbr->f_tablelim + sbr->n_lim) {
150 if ((*in << 23) >= *out * lim_bands_per_octave_warped) {
152 if (*in >= *out * lim_bands_per_octave_warped) {
153 #endif /* USE_FIXED */
155 } else if (*in == *out ||
156 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
159 } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
167 sbr->f_tablelim[0] = sbr->f_tablelow[0];
168 sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
173 static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
175 unsigned int cnt = get_bits_count(gb);
176 uint8_t bs_header_extra_1;
177 uint8_t bs_header_extra_2;
178 int old_bs_limiter_bands = sbr->bs_limiter_bands;
179 SpectrumParameters old_spectrum_params;
183 // Save last spectrum parameters variables to compare to new ones
184 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
186 sbr->bs_amp_res_header = get_bits1(gb);
187 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
188 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
189 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
190 skip_bits(gb, 2); // bs_reserved
192 bs_header_extra_1 = get_bits1(gb);
193 bs_header_extra_2 = get_bits1(gb);
195 if (bs_header_extra_1) {
196 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
197 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
198 sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
200 sbr->spectrum_params.bs_freq_scale = 2;
201 sbr->spectrum_params.bs_alter_scale = 1;
202 sbr->spectrum_params.bs_noise_bands = 2;
205 // Check if spectrum parameters changed
206 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
209 if (bs_header_extra_2) {
210 sbr->bs_limiter_bands = get_bits(gb, 2);
211 sbr->bs_limiter_gains = get_bits(gb, 2);
212 sbr->bs_interpol_freq = get_bits1(gb);
213 sbr->bs_smoothing_mode = get_bits1(gb);
215 sbr->bs_limiter_bands = 2;
216 sbr->bs_limiter_gains = 2;
217 sbr->bs_interpol_freq = 1;
218 sbr->bs_smoothing_mode = 1;
221 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
222 sbr_make_f_tablelim(sbr);
224 return get_bits_count(gb) - cnt;
227 static int array_min_int16(const int16_t *array, int nel)
229 int i, min = array[0];
230 for (i = 1; i < nel; i++)
231 min = FFMIN(array[i], min);
235 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
237 // Requirements (14496-3 sp04 p205)
239 av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
242 if (bs_xover_band >= n_master) {
243 av_log(avctx, AV_LOG_ERROR,
244 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
251 /// Master Frequency Band Table (14496-3 sp04 p194)
252 static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
253 SpectrumParameters *spectrum)
255 unsigned int temp, max_qmf_subbands = 0;
256 unsigned int start_min, stop_min;
258 const int8_t *sbr_offset_ptr;
261 if (sbr->sample_rate < 32000) {
263 } else if (sbr->sample_rate < 64000) {
268 switch (sbr->sample_rate) {
270 sbr_offset_ptr = sbr_offset[0];
273 sbr_offset_ptr = sbr_offset[1];
276 sbr_offset_ptr = sbr_offset[2];
279 sbr_offset_ptr = sbr_offset[3];
281 case 44100: case 48000: case 64000:
282 sbr_offset_ptr = sbr_offset[4];
284 case 88200: case 96000: case 128000: case 176400: case 192000:
285 sbr_offset_ptr = sbr_offset[5];
288 av_log(ac->avctx, AV_LOG_ERROR,
289 "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
293 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
294 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
296 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
298 if (spectrum->bs_stop_freq < 14) {
299 sbr->k[2] = stop_min;
300 make_bands(stop_dk, stop_min, 64, 13);
301 AV_QSORT(stop_dk, 13, int16_t, qsort_comparison_function_int16);
302 for (k = 0; k < spectrum->bs_stop_freq; k++)
303 sbr->k[2] += stop_dk[k];
304 } else if (spectrum->bs_stop_freq == 14) {
305 sbr->k[2] = 2*sbr->k[0];
306 } else if (spectrum->bs_stop_freq == 15) {
307 sbr->k[2] = 3*sbr->k[0];
309 av_log(ac->avctx, AV_LOG_ERROR,
310 "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
313 sbr->k[2] = FFMIN(64, sbr->k[2]);
315 // Requirements (14496-3 sp04 p205)
316 if (sbr->sample_rate <= 32000) {
317 max_qmf_subbands = 48;
318 } else if (sbr->sample_rate == 44100) {
319 max_qmf_subbands = 35;
320 } else if (sbr->sample_rate >= 48000)
321 max_qmf_subbands = 32;
325 if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
326 av_log(ac->avctx, AV_LOG_ERROR,
327 "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
331 if (!spectrum->bs_freq_scale) {
334 dk = spectrum->bs_alter_scale + 1;
335 sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
336 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
339 for (k = 1; k <= sbr->n_master; k++)
340 sbr->f_master[k] = dk;
342 k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
345 sbr->f_master[2]-= (k2diff < -1);
347 sbr->f_master[sbr->n_master]++;
350 sbr->f_master[0] = sbr->k[0];
351 for (k = 1; k <= sbr->n_master; k++)
352 sbr->f_master[k] += sbr->f_master[k - 1];
355 int half_bands = 7 - spectrum->bs_freq_scale; // bs_freq_scale = {1,2,3}
356 int two_regions, num_bands_0;
357 int vdk0_max, vdk1_min;
361 #endif /* USE_FIXED */
363 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
365 sbr->k[1] = 2 * sbr->k[0];
368 sbr->k[1] = sbr->k[2];
372 tmp = (sbr->k[1] << 23) / sbr->k[0];
373 while (tmp < 0x40000000) {
377 tmp = fixed_log(tmp - 0x80000000);
378 tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
379 tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
380 num_bands_0 = ((tmp + 0x400000) >> 23) * 2;
382 num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
383 #endif /* USE_FIXED */
385 if (num_bands_0 <= 0) { // Requirements (14496-3 sp04 p205)
386 av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
392 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
394 AV_QSORT(vk0 + 1, num_bands_0, int16_t, qsort_comparison_function_int16);
395 vdk0_max = vk0[num_bands_0];
398 for (k = 1; k <= num_bands_0; k++) {
399 if (vk0[k] <= 0) { // Requirements (14496-3 sp04 p205)
400 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
411 tmp = (sbr->k[2] << 23) / sbr->k[1];
413 while (tmp < 0x40000000) {
417 tmp = fixed_log(tmp - 0x80000000);
418 tmp = (int)(((int64_t)tmp * CONST_RECIP_LN2 + 0x20000000) >> 30);
419 tmp = (((tmp + 0x80) >> 8) + ((8 - nz) << 23)) * half_bands;
420 if (spectrum->bs_alter_scale)
421 tmp = (int)(((int64_t)tmp * CONST_076923 + 0x40000000) >> 31);
422 num_bands_1 = ((tmp + 0x400000) >> 23) * 2;
424 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
425 : 1.0f; // bs_alter_scale = {0,1}
426 int num_bands_1 = lrintf(half_bands * invwarp *
427 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
428 #endif /* USE_FIXED */
429 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
431 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
433 if (vdk1_min < vdk0_max) {
435 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
436 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
438 vk1[num_bands_1] -= change;
441 AV_QSORT(vk1 + 1, num_bands_1, int16_t, qsort_comparison_function_int16);
444 for (k = 1; k <= num_bands_1; k++) {
445 if (vk1[k] <= 0) { // Requirements (14496-3 sp04 p205)
446 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
452 sbr->n_master = num_bands_0 + num_bands_1;
453 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
455 memcpy(&sbr->f_master[0], vk0,
456 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
457 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
458 num_bands_1 * sizeof(sbr->f_master[0]));
461 sbr->n_master = num_bands_0;
462 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
464 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
471 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
472 static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
474 int i, k, last_k = -1, last_msb = -1, sb = 0;
476 int usb = sbr->kx[1];
477 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
479 sbr->num_patches = 0;
481 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
482 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
488 if (k == last_k && msb == last_msb) {
489 av_log(ac->avctx, AV_LOG_ERROR, "patch construction failed\n");
490 return AVERROR_INVALIDDATA;
494 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
495 sb = sbr->f_master[i];
496 odd = (sb + sbr->k[0]) & 1;
499 // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
500 // After this check the final number of patches can still be six which is
501 // illegal however the Coding Technologies decoder check stream has a final
502 // count of 6 patches
503 if (sbr->num_patches > 5) {
504 av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
508 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
509 sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
511 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
518 if (sbr->f_master[k] - sb < 3)
520 } while (sb != sbr->kx[1] + sbr->m[1]);
522 if (sbr->num_patches > 1 &&
523 sbr->patch_num_subbands[sbr->num_patches - 1] < 3)
529 /// Derived Frequency Band Tables (14496-3 sp04 p197)
530 static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
535 #endif /* USE_FIXED */
537 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
538 sbr->n[0] = (sbr->n[1] + 1) >> 1;
540 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
541 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
542 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
543 sbr->kx[1] = sbr->f_tablehigh[0];
545 // Requirements (14496-3 sp04 p205)
546 if (sbr->kx[1] + sbr->m[1] > 64) {
547 av_log(ac->avctx, AV_LOG_ERROR,
548 "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
551 if (sbr->kx[1] > 32) {
552 av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
556 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
557 temp = sbr->n[1] & 1;
558 for (k = 1; k <= sbr->n[0]; k++)
559 sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
561 temp = (sbr->k[2] << 23) / sbr->kx[1];
562 while (temp < 0x40000000) {
566 temp = fixed_log(temp - 0x80000000);
567 temp = (int)(((int64_t)temp * CONST_RECIP_LN2 + 0x20000000) >> 30);
568 temp = (((temp + 0x80) >> 8) + ((8 - nz) << 23)) * sbr->spectrum_params.bs_noise_bands;
570 sbr->n_q = (temp + 0x400000) >> 23;
574 sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
575 log2f(sbr->k[2] / (float)sbr->kx[1]))); // 0 <= bs_noise_bands <= 3
576 #endif /* USE_FIXED */
579 av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
583 sbr->f_tablenoise[0] = sbr->f_tablelow[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];
590 if (sbr_hf_calc_npatches(ac, sbr) < 0)
593 sbr_make_f_tablelim(sbr);
595 sbr->data[0].f_indexnoise = 0;
596 sbr->data[1].f_indexnoise = 0;
601 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
605 for (i = 0; i < elements; i++) {
606 vec[i] = get_bits1(gb);
610 /** ceil(log2(index+1)) */
611 static const int8_t ceil_log2[] = {
615 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
616 GetBitContext *gb, SBRData *ch_data)
620 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
621 int abs_bord_trail = 16;
622 int num_rel_lead, num_rel_trail;
623 unsigned bs_num_env_old = ch_data->bs_num_env;
625 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
626 ch_data->bs_amp_res = sbr->bs_amp_res_header;
627 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
629 switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
631 ch_data->bs_num_env = 1 << get_bits(gb, 2);
632 num_rel_lead = ch_data->bs_num_env - 1;
633 if (ch_data->bs_num_env == 1)
634 ch_data->bs_amp_res = 0;
636 if (ch_data->bs_num_env > 4) {
637 av_log(ac->avctx, AV_LOG_ERROR,
638 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
639 ch_data->bs_num_env);
643 ch_data->t_env[0] = 0;
644 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
646 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
648 for (i = 0; i < num_rel_lead; i++)
649 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
651 ch_data->bs_freq_res[1] = get_bits1(gb);
652 for (i = 1; i < ch_data->bs_num_env; i++)
653 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
656 abs_bord_trail += get_bits(gb, 2);
657 num_rel_trail = get_bits(gb, 2);
658 ch_data->bs_num_env = num_rel_trail + 1;
659 ch_data->t_env[0] = 0;
660 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
662 for (i = 0; i < num_rel_trail; i++)
663 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
664 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
666 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
668 for (i = 0; i < ch_data->bs_num_env; i++)
669 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
672 ch_data->t_env[0] = get_bits(gb, 2);
673 num_rel_lead = get_bits(gb, 2);
674 ch_data->bs_num_env = num_rel_lead + 1;
675 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
677 for (i = 0; i < num_rel_lead; i++)
678 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
680 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
682 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
685 ch_data->t_env[0] = get_bits(gb, 2);
686 abs_bord_trail += get_bits(gb, 2);
687 num_rel_lead = get_bits(gb, 2);
688 num_rel_trail = get_bits(gb, 2);
689 ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
691 if (ch_data->bs_num_env > 5) {
692 av_log(ac->avctx, AV_LOG_ERROR,
693 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
694 ch_data->bs_num_env);
698 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
700 for (i = 0; i < num_rel_lead; i++)
701 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
702 for (i = 0; i < num_rel_trail; i++)
703 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
704 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
706 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
708 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
712 av_assert0(bs_pointer >= 0);
713 if (bs_pointer > ch_data->bs_num_env + 1) {
714 av_log(ac->avctx, AV_LOG_ERROR,
715 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
720 for (i = 1; i <= ch_data->bs_num_env; i++) {
721 if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
722 av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
727 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
729 ch_data->t_q[0] = ch_data->t_env[0];
730 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
731 if (ch_data->bs_num_noise > 1) {
733 if (ch_data->bs_frame_class == FIXFIX) {
734 idx = ch_data->bs_num_env >> 1;
735 } else if (ch_data->bs_frame_class & 1) { // FIXVAR or VARVAR
736 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
740 else if (bs_pointer == 1)
741 idx = ch_data->bs_num_env - 1;
742 else // bs_pointer > 1
743 idx = bs_pointer - 1;
745 ch_data->t_q[1] = ch_data->t_env[idx];
748 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old); // l_APrev
749 ch_data->e_a[1] = -1;
750 if ((ch_data->bs_frame_class & 1) && bs_pointer) { // FIXVAR or VARVAR and bs_pointer != 0
751 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
752 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1)) // VARFIX and bs_pointer > 1
753 ch_data->e_a[1] = bs_pointer - 1;
758 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
759 //These variables are saved from the previous frame rather than copied
760 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
761 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
762 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
764 //These variables are read from the bitstream and therefore copied
765 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
766 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
767 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
768 dst->bs_num_env = src->bs_num_env;
769 dst->bs_amp_res = src->bs_amp_res;
770 dst->bs_num_noise = src->bs_num_noise;
771 dst->bs_frame_class = src->bs_frame_class;
772 dst->e_a[1] = src->e_a[1];
775 /// Read how the envelope and noise floor data is delta coded
776 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
779 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
780 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
783 /// Read inverse filtering data
784 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
789 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
790 for (i = 0; i < sbr->n_q; i++)
791 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
794 static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,
795 SBRData *ch_data, int ch)
799 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
801 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
802 const int odd = sbr->n[1] & 1;
804 if (sbr->bs_coupling && ch) {
805 if (ch_data->bs_amp_res) {
807 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
808 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
809 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
810 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
813 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
814 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
815 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
816 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
819 if (ch_data->bs_amp_res) {
821 t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
822 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
823 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
824 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
827 t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
828 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
829 f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
830 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
835 for (i = 0; i < ch_data->bs_num_env; i++) {
836 if (ch_data->bs_df_env[i]) {
837 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
838 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
839 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
840 ch_data->env_facs[i + 1][j].mant = ch_data->env_facs[i][j].mant + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
841 } else if (ch_data->bs_freq_res[i + 1]) {
842 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
843 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
844 ch_data->env_facs[i + 1][j].mant = ch_data->env_facs[i][k].mant + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
847 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
848 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
849 ch_data->env_facs[i + 1][j].mant = ch_data->env_facs[i][k].mant + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
853 ch_data->env_facs[i + 1][0].mant = delta * get_bits(gb, bits); // bs_env_start_value_balance
854 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
855 ch_data->env_facs[i + 1][j].mant = ch_data->env_facs[i + 1][j - 1].mant + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
859 for (i = 0; i < ch_data->bs_num_env; i++) {
860 if (ch_data->bs_df_env[i]) {
861 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
862 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
863 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
864 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
865 } else if (ch_data->bs_freq_res[i + 1]) {
866 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
867 k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
868 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
871 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
872 k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]
873 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
877 ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance
878 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
879 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);
882 #endif /* USE_FIXED */
884 //assign 0th elements of env_facs from last elements
885 memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
886 sizeof(ch_data->env_facs[0]));
889 static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
890 SBRData *ch_data, int ch)
893 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
895 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
897 if (sbr->bs_coupling && ch) {
898 t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
899 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
900 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
901 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
903 t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
904 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
905 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
906 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
910 for (i = 0; i < ch_data->bs_num_noise; i++) {
911 if (ch_data->bs_df_noise[i]) {
912 for (j = 0; j < sbr->n_q; j++)
913 ch_data->noise_facs[i + 1][j].mant = ch_data->noise_facs[i][j].mant + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
915 ch_data->noise_facs[i + 1][0].mant = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
916 for (j = 1; j < sbr->n_q; j++)
917 ch_data->noise_facs[i + 1][j].mant = ch_data->noise_facs[i + 1][j - 1].mant + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
921 for (i = 0; i < ch_data->bs_num_noise; i++) {
922 if (ch_data->bs_df_noise[i]) {
923 for (j = 0; j < sbr->n_q; j++)
924 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
926 ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
927 for (j = 1; j < sbr->n_q; j++)
928 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);
931 #endif /* USE_FIXED */
933 //assign 0th elements of noise_facs from last elements
934 memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
935 sizeof(ch_data->noise_facs[0]));
938 static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
940 int bs_extension_id, int *num_bits_left)
942 switch (bs_extension_id) {
943 case EXTENSION_ID_PS:
944 if (!ac->oc[1].m4ac.ps) {
945 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
946 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
950 *num_bits_left -= AAC_RENAME(ff_ps_read_data)(ac->avctx, gb, &sbr->ps, *num_bits_left);
951 ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
953 avpriv_report_missing_feature(ac->avctx, "Parametric Stereo");
954 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
960 // some files contain 0-padding
961 if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
962 avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
963 skip_bits_long(gb, *num_bits_left); // bs_fill_bits
969 static int read_sbr_single_channel_element(AACContext *ac,
970 SpectralBandReplication *sbr,
973 if (get_bits1(gb)) // bs_data_extra
974 skip_bits(gb, 4); // bs_reserved
976 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
978 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
979 read_sbr_invf(sbr, gb, &sbr->data[0]);
980 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
981 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
983 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
984 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
989 static int read_sbr_channel_pair_element(AACContext *ac,
990 SpectralBandReplication *sbr,
993 if (get_bits1(gb)) // bs_data_extra
994 skip_bits(gb, 8); // bs_reserved
996 if ((sbr->bs_coupling = get_bits1(gb))) {
997 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
999 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
1000 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
1001 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
1002 read_sbr_invf(sbr, gb, &sbr->data[0]);
1003 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1004 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
1005 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
1006 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
1007 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
1008 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
1010 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
1011 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
1013 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
1014 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
1015 read_sbr_invf(sbr, gb, &sbr->data[0]);
1016 read_sbr_invf(sbr, gb, &sbr->data[1]);
1017 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
1018 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
1019 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
1020 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
1023 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
1024 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
1025 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
1026 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
1031 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
1032 GetBitContext *gb, int id_aac)
1034 unsigned int cnt = get_bits_count(gb);
1036 sbr->id_aac = id_aac;
1038 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
1039 if (read_sbr_single_channel_element(ac, sbr, gb)) {
1041 return get_bits_count(gb) - cnt;
1043 } else if (id_aac == TYPE_CPE) {
1044 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1046 return get_bits_count(gb) - cnt;
1049 av_log(ac->avctx, AV_LOG_ERROR,
1050 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1052 return get_bits_count(gb) - cnt;
1054 if (get_bits1(gb)) { // bs_extended_data
1055 int num_bits_left = get_bits(gb, 4); // bs_extension_size
1056 if (num_bits_left == 15)
1057 num_bits_left += get_bits(gb, 8); // bs_esc_count
1059 num_bits_left <<= 3;
1060 while (num_bits_left > 7) {
1062 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
1064 if (num_bits_left < 0) {
1065 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
1067 if (num_bits_left > 0)
1068 skip_bits(gb, num_bits_left);
1071 return get_bits_count(gb) - cnt;
1074 static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
1077 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
1079 err = sbr_make_f_derived(ac, sbr);
1081 av_log(ac->avctx, AV_LOG_ERROR,
1082 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1088 * Decode Spectral Band Replication extension data; reference: table 4.55.
1090 * @param crc flag indicating the presence of CRC checksum
1091 * @param cnt length of TYPE_FIL syntactic element in bytes
1093 * @return Returns number of bytes consumed from the TYPE_FIL element.
1095 int AAC_RENAME(ff_decode_sbr_extension)(AACContext *ac, SpectralBandReplication *sbr,
1096 GetBitContext *gb_host, int crc, int cnt, int id_aac)
1098 unsigned int num_sbr_bits = 0, num_align_bits;
1099 unsigned bytes_read;
1100 GetBitContext gbc = *gb_host, *gb = &gbc;
1101 skip_bits_long(gb_host, cnt*8 - 4);
1105 if (!sbr->sample_rate)
1106 sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
1107 if (!ac->oc[1].m4ac.ext_sample_rate)
1108 ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
1111 skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1115 //Save some state from the previous frame.
1116 sbr->kx[0] = sbr->kx[1];
1117 sbr->m[0] = sbr->m[1];
1118 sbr->kx_and_m_pushed = 1;
1121 if (get_bits1(gb)) // bs_header_flag
1122 num_sbr_bits += read_sbr_header(sbr, gb);
1128 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
1130 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1131 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1133 if (bytes_read > cnt) {
1134 av_log(ac->avctx, AV_LOG_ERROR,
1135 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1141 * Analysis QMF Bank (14496-3 sp04 p206)
1143 * @param x pointer to the beginning of the first sample window
1144 * @param W array of complex-valued samples split into subbands
1146 #ifndef sbr_qmf_analysis
1148 static void sbr_qmf_analysis(AVFixedDSPContext *dsp, FFTContext *mdct,
1150 static void sbr_qmf_analysis(AVFloatDSPContext *dsp, FFTContext *mdct,
1151 #endif /* USE_FIXED */
1152 SBRDSPContext *sbrdsp, const INTFLOAT *in, INTFLOAT *x,
1153 INTFLOAT z[320], INTFLOAT W[2][32][32][2], int buf_idx)
1156 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1157 memcpy(x+288, in, 1024*sizeof(x[0]));
1158 for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1159 // are not supported
1160 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1162 sbrdsp->qmf_pre_shuffle(z);
1163 mdct->imdct_half(mdct, z, z+64);
1164 sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1171 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1172 * (14496-3 sp04 p206)
1174 #ifndef sbr_qmf_synthesis
1175 static void sbr_qmf_synthesis(FFTContext *mdct,
1177 SBRDSPContext *sbrdsp, AVFixedDSPContext *dsp,
1179 SBRDSPContext *sbrdsp, AVFloatDSPContext *dsp,
1180 #endif /* USE_FIXED */
1181 INTFLOAT *out, INTFLOAT X[2][38][64],
1182 INTFLOAT mdct_buf[2][64],
1183 INTFLOAT *v0, int *v_off, const unsigned int div)
1186 const INTFLOAT *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
1187 const int step = 128 >> div;
1189 for (i = 0; i < 32; i++) {
1190 if (*v_off < step) {
1191 int saved_samples = (1280 - 128) >> div;
1192 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(INTFLOAT));
1193 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
1199 for (n = 0; n < 32; n++) {
1200 X[0][i][ n] = -X[0][i][n];
1201 X[0][i][32+n] = X[1][i][31-n];
1203 mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1204 sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1206 sbrdsp->neg_odd_64(X[1][i]);
1207 mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1208 mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
1209 sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1211 dsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1212 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1213 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1214 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1215 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1216 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1217 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1218 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1219 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1220 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1226 /// Generate the subband filtered lowband
1227 static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
1228 INTFLOAT X_low[32][40][2], const INTFLOAT W[2][32][32][2],
1232 const int t_HFGen = 8;
1234 memset(X_low, 0, 32*sizeof(*X_low));
1235 for (k = 0; k < sbr->kx[1]; k++) {
1236 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1237 X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1238 X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1241 buf_idx = 1-buf_idx;
1242 for (k = 0; k < sbr->kx[0]; k++) {
1243 for (i = 0; i < t_HFGen; i++) {
1244 X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1245 X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1251 /// High Frequency Generator (14496-3 sp04 p215)
1252 static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
1253 INTFLOAT X_high[64][40][2], const INTFLOAT X_low[32][40][2],
1254 const INTFLOAT (*alpha0)[2], const INTFLOAT (*alpha1)[2],
1255 const INTFLOAT bw_array[5], const uint8_t *t_env,
1261 for (j = 0; j < sbr->num_patches; j++) {
1262 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
1263 const int p = sbr->patch_start_subband[j] + x;
1264 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
1269 av_log(ac->avctx, AV_LOG_ERROR,
1270 "ERROR : no subband found for frequency %d\n", k);
1274 sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1275 X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1276 alpha0[p], alpha1[p], bw_array[g],
1277 2 * t_env[0], 2 * t_env[bs_num_env]);
1280 if (k < sbr->m[1] + sbr->kx[1])
1281 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
1286 /// Generate the subband filtered lowband
1287 static int sbr_x_gen(SpectralBandReplication *sbr, INTFLOAT X[2][38][64],
1288 const INTFLOAT Y0[38][64][2], const INTFLOAT Y1[38][64][2],
1289 const INTFLOAT X_low[32][40][2], int ch)
1293 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
1294 memset(X, 0, 2*sizeof(*X));
1295 for (k = 0; k < sbr->kx[0]; k++) {
1296 for (i = 0; i < i_Temp; i++) {
1297 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1298 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1301 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
1302 for (i = 0; i < i_Temp; i++) {
1303 X[0][i][k] = Y0[i + i_f][k][0];
1304 X[1][i][k] = Y0[i + i_f][k][1];
1308 for (k = 0; k < sbr->kx[1]; k++) {
1309 for (i = i_Temp; i < 38; i++) {
1310 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
1311 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
1314 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
1315 for (i = i_Temp; i < i_f; i++) {
1316 X[0][i][k] = Y1[i][k][0];
1317 X[1][i][k] = Y1[i][k][1];
1323 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1324 * (14496-3 sp04 p217)
1326 static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
1327 SBRData *ch_data, int e_a[2])
1331 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
1332 for (e = 0; e < ch_data->bs_num_env; e++) {
1333 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
1334 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1337 if (sbr->kx[1] != table[0]) {
1338 av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
1339 "Derived frequency tables were not regenerated.\n");
1343 for (i = 0; i < ilim; i++)
1344 for (m = table[i]; m < table[i + 1]; m++)
1345 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
1347 // ch_data->bs_num_noise > 1 => 2 noise floors
1348 k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
1349 for (i = 0; i < sbr->n_q; i++)
1350 for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1351 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
1353 for (i = 0; i < sbr->n[1]; i++) {
1354 if (ch_data->bs_add_harmonic_flag) {
1355 const unsigned int m_midpoint =
1356 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
1358 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
1359 (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
1363 for (i = 0; i < ilim; i++) {
1364 int additional_sinusoid_present = 0;
1365 for (m = table[i]; m < table[i + 1]; m++) {
1366 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
1367 additional_sinusoid_present = 1;
1371 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
1372 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
1376 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
1380 /// Estimation of current envelope (14496-3 sp04 p218)
1381 static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2],
1382 SpectralBandReplication *sbr, SBRData *ch_data)
1385 int kx1 = sbr->kx[1];
1387 if (sbr->bs_interpol_freq) {
1388 for (e = 0; e < ch_data->bs_num_env; e++) {
1390 const SoftFloat recip_env_size = av_int2sf(0x20000000 / (ch_data->t_env[e + 1] - ch_data->t_env[e]), 30);
1392 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1393 #endif /* USE_FIXED */
1394 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1395 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1397 for (m = 0; m < sbr->m[1]; m++) {
1398 AAC_FLOAT sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
1400 e_curr[e][m] = av_mul_sf(sum, recip_env_size);
1402 e_curr[e][m] = sum * recip_env_size;
1403 #endif /* USE_FIXED */
1409 for (e = 0; e < ch_data->bs_num_env; e++) {
1410 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
1411 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1412 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
1413 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
1415 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
1417 SoftFloat sum = { 0, 0 };
1418 const SoftFloat den = av_int2sf(0x20000000 / (env_size * (table[p + 1] - table[p])), 29);
1419 for (k = table[p]; k < table[p + 1]; k++) {
1420 sum = av_add_sf(sum, sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb));
1422 sum = av_mul_sf(sum, den);
1425 const int den = env_size * (table[p + 1] - table[p]);
1427 for (k = table[p]; k < table[p + 1]; k++) {
1428 sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
1431 #endif /* USE_FIXED */
1432 for (k = table[p]; k < table[p + 1]; k++) {
1433 e_curr[e][k - kx1] = sum;
1440 void AAC_RENAME(ff_sbr_apply)(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
1441 INTFLOAT* L, INTFLOAT* R)
1443 int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1445 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1448 if (id_aac != sbr->id_aac) {
1449 av_log(ac->avctx, AV_LOG_ERROR,
1450 "element type mismatch %d != %d\n", id_aac, sbr->id_aac);
1454 if (!sbr->kx_and_m_pushed) {
1455 sbr->kx[0] = sbr->kx[1];
1456 sbr->m[0] = sbr->m[1];
1458 sbr->kx_and_m_pushed = 0;
1462 sbr_dequant(sbr, id_aac);
1464 for (ch = 0; ch < nch; ch++) {
1465 /* decode channel */
1466 sbr_qmf_analysis(ac->fdsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1467 (INTFLOAT*)sbr->qmf_filter_scratch,
1468 sbr->data[ch].W, sbr->data[ch].Ypos);
1469 sbr->c.sbr_lf_gen(ac, sbr, sbr->X_low,
1470 (const INTFLOAT (*)[32][32][2]) sbr->data[ch].W,
1471 sbr->data[ch].Ypos);
1472 sbr->data[ch].Ypos ^= 1;
1474 sbr->c.sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1,
1475 (const INTFLOAT (*)[40][2]) sbr->X_low, sbr->k[0]);
1476 sbr_chirp(sbr, &sbr->data[ch]);
1477 av_assert0(sbr->data[ch].bs_num_env > 0);
1478 sbr_hf_gen(ac, sbr, sbr->X_high,
1479 (const INTFLOAT (*)[40][2]) sbr->X_low,
1480 (const INTFLOAT (*)[2]) sbr->alpha0,
1481 (const INTFLOAT (*)[2]) sbr->alpha1,
1482 sbr->data[ch].bw_array, sbr->data[ch].t_env,
1483 sbr->data[ch].bs_num_env);
1486 err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1488 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1489 sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1490 sbr->c.sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1491 (const INTFLOAT (*)[40][2]) sbr->X_high,
1492 sbr, &sbr->data[ch],
1498 sbr->c.sbr_x_gen(sbr, sbr->X[ch],
1499 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1500 (const INTFLOAT (*)[64][2]) sbr->data[ch].Y[ sbr->data[ch].Ypos],
1501 (const INTFLOAT (*)[40][2]) sbr->X_low, ch);
1504 if (ac->oc[1].m4ac.ps == 1) {
1505 if (sbr->ps.start) {
1506 AAC_RENAME(ff_ps_apply)(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1508 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
1513 sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
1514 L, sbr->X[0], sbr->qmf_filter_scratch,
1515 sbr->data[0].synthesis_filterbank_samples,
1516 &sbr->data[0].synthesis_filterbank_samples_offset,
1519 sbr_qmf_synthesis(&sbr->mdct, &sbr->dsp, ac->fdsp,
1520 R, sbr->X[1], sbr->qmf_filter_scratch,
1521 sbr->data[1].synthesis_filterbank_samples,
1522 &sbr->data[1].synthesis_filterbank_samples_offset,
1526 static void aacsbr_func_ptr_init(AACSBRContext *c)
1528 c->sbr_lf_gen = sbr_lf_gen;
1529 c->sbr_hf_assemble = sbr_hf_assemble;
1530 c->sbr_x_gen = sbr_x_gen;
1531 c->sbr_hf_inverse_filter = sbr_hf_inverse_filter;
1535 ff_aacsbr_func_ptr_init_mips(c);