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>
6 * This file is part of FFmpeg.
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.
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.
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
25 * AAC Spectral Band Replication decoding functions
26 * @author Robert Swain ( rob opendot cl )
32 #include "aacsbrdata.h"
33 #include "aacsbr_tablegen.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/libm.h"
39 #include "libavutil/avassert.h"
45 #define ENVELOPE_ADJUSTMENT_OFFSET 2
46 #define NOISE_FLOOR_OFFSET 6.0f
49 #include "mips/aacsbr_mips.h"
50 #endif /* ARCH_MIPS */
58 T_HUFFMAN_ENV_BAL_1_5DB
,
59 F_HUFFMAN_ENV_BAL_1_5DB
,
62 T_HUFFMAN_ENV_BAL_3_0DB
,
63 F_HUFFMAN_ENV_BAL_3_0DB
,
64 T_HUFFMAN_NOISE_3_0DB
,
65 T_HUFFMAN_NOISE_BAL_3_0DB
,
69 * bs_frame_class - frame class of current SBR frame (14496-3 sp04 p98)
82 static VLC vlc_sbr
[10];
83 static const int8_t vlc_sbr_lav
[10] =
84 { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
86 #define SBR_INIT_VLC_STATIC(num, size) \
87 INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
88 sbr_tmp[num].sbr_bits , 1, 1, \
89 sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
92 #define SBR_VLC_ROW(name) \
93 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
95 static void aacsbr_func_ptr_init(AACSBRContext
*c
);
97 av_cold
void ff_aac_sbr_init(void)
100 const void *sbr_codes
, *sbr_bits
;
101 const unsigned int table_size
, elem_size
;
103 SBR_VLC_ROW(t_huffman_env_1_5dB
),
104 SBR_VLC_ROW(f_huffman_env_1_5dB
),
105 SBR_VLC_ROW(t_huffman_env_bal_1_5dB
),
106 SBR_VLC_ROW(f_huffman_env_bal_1_5dB
),
107 SBR_VLC_ROW(t_huffman_env_3_0dB
),
108 SBR_VLC_ROW(f_huffman_env_3_0dB
),
109 SBR_VLC_ROW(t_huffman_env_bal_3_0dB
),
110 SBR_VLC_ROW(f_huffman_env_bal_3_0dB
),
111 SBR_VLC_ROW(t_huffman_noise_3_0dB
),
112 SBR_VLC_ROW(t_huffman_noise_bal_3_0dB
),
115 // SBR VLC table initialization
116 SBR_INIT_VLC_STATIC(0, 1098);
117 SBR_INIT_VLC_STATIC(1, 1092);
118 SBR_INIT_VLC_STATIC(2, 768);
119 SBR_INIT_VLC_STATIC(3, 1026);
120 SBR_INIT_VLC_STATIC(4, 1058);
121 SBR_INIT_VLC_STATIC(5, 1052);
122 SBR_INIT_VLC_STATIC(6, 544);
123 SBR_INIT_VLC_STATIC(7, 544);
124 SBR_INIT_VLC_STATIC(8, 592);
125 SBR_INIT_VLC_STATIC(9, 512);
132 /** Places SBR in pure upsampling mode. */
133 static void sbr_turnoff(SpectralBandReplication
*sbr
) {
135 // Init defults used in pure upsampling mode
136 sbr
->kx
[1] = 32; //Typo in spec, kx' inits to 32
138 // Reset values for first SBR header
139 sbr
->data
[0].e_a
[1] = sbr
->data
[1].e_a
[1] = -1;
140 memset(&sbr
->spectrum_params
, -1, sizeof(SpectrumParameters
));
143 av_cold
void ff_aac_sbr_ctx_init(AACContext
*ac
, SpectralBandReplication
*sbr
)
145 if(sbr
->mdct
.mdct_bits
)
147 sbr
->kx
[0] = sbr
->kx
[1];
149 sbr
->data
[0].synthesis_filterbank_samples_offset
= SBR_SYNTHESIS_BUF_SIZE
- (1280 - 128);
150 sbr
->data
[1].synthesis_filterbank_samples_offset
= SBR_SYNTHESIS_BUF_SIZE
- (1280 - 128);
151 /* SBR requires samples to be scaled to +/-32768.0 to work correctly.
152 * mdct scale factors are adjusted to scale up from +/-1.0 at analysis
153 * and scale back down at synthesis. */
154 ff_mdct_init(&sbr
->mdct
, 7, 1, 1.0 / (64 * 32768.0));
155 ff_mdct_init(&sbr
->mdct_ana
, 7, 1, -2.0 * 32768.0);
156 ff_ps_ctx_init(&sbr
->ps
);
157 ff_sbrdsp_init(&sbr
->dsp
);
158 aacsbr_func_ptr_init(&sbr
->c
);
161 av_cold
void ff_aac_sbr_ctx_close(SpectralBandReplication
*sbr
)
163 ff_mdct_end(&sbr
->mdct
);
164 ff_mdct_end(&sbr
->mdct_ana
);
167 static int qsort_comparison_function_int16(const void *a
, const void *b
)
169 return *(const int16_t *)a
- *(const int16_t *)b
;
172 static inline int in_table_int16(const int16_t *table
, int last_el
, int16_t needle
)
175 for (i
= 0; i
<= last_el
; i
++)
176 if (table
[i
] == needle
)
181 /// Limiter Frequency Band Table (14496-3 sp04 p198)
182 static void sbr_make_f_tablelim(SpectralBandReplication
*sbr
)
185 if (sbr
->bs_limiter_bands
> 0) {
186 static const float bands_warped
[3] = { 1.32715174233856803909f
, //2^(0.49/1.2)
187 1.18509277094158210129f
, //2^(0.49/2)
188 1.11987160404675912501f
}; //2^(0.49/3)
189 const float lim_bands_per_octave_warped
= bands_warped
[sbr
->bs_limiter_bands
- 1];
190 int16_t patch_borders
[7];
191 uint16_t *in
= sbr
->f_tablelim
+ 1, *out
= sbr
->f_tablelim
;
193 patch_borders
[0] = sbr
->kx
[1];
194 for (k
= 1; k
<= sbr
->num_patches
; k
++)
195 patch_borders
[k
] = patch_borders
[k
-1] + sbr
->patch_num_subbands
[k
-1];
197 memcpy(sbr
->f_tablelim
, sbr
->f_tablelow
,
198 (sbr
->n
[0] + 1) * sizeof(sbr
->f_tablelow
[0]));
199 if (sbr
->num_patches
> 1)
200 memcpy(sbr
->f_tablelim
+ sbr
->n
[0] + 1, patch_borders
+ 1,
201 (sbr
->num_patches
- 1) * sizeof(patch_borders
[0]));
203 qsort(sbr
->f_tablelim
, sbr
->num_patches
+ sbr
->n
[0],
204 sizeof(sbr
->f_tablelim
[0]),
205 qsort_comparison_function_int16
);
207 sbr
->n_lim
= sbr
->n
[0] + sbr
->num_patches
- 1;
208 while (out
< sbr
->f_tablelim
+ sbr
->n_lim
) {
209 if (*in
>= *out
* lim_bands_per_octave_warped
) {
211 } else if (*in
== *out
||
212 !in_table_int16(patch_borders
, sbr
->num_patches
, *in
)) {
215 } else if (!in_table_int16(patch_borders
, sbr
->num_patches
, *out
)) {
223 sbr
->f_tablelim
[0] = sbr
->f_tablelow
[0];
224 sbr
->f_tablelim
[1] = sbr
->f_tablelow
[sbr
->n
[0]];
229 static unsigned int read_sbr_header(SpectralBandReplication
*sbr
, GetBitContext
*gb
)
231 unsigned int cnt
= get_bits_count(gb
);
232 uint8_t bs_header_extra_1
;
233 uint8_t bs_header_extra_2
;
234 int old_bs_limiter_bands
= sbr
->bs_limiter_bands
;
235 SpectrumParameters old_spectrum_params
;
239 // Save last spectrum parameters variables to compare to new ones
240 memcpy(&old_spectrum_params
, &sbr
->spectrum_params
, sizeof(SpectrumParameters
));
242 sbr
->bs_amp_res_header
= get_bits1(gb
);
243 sbr
->spectrum_params
.bs_start_freq
= get_bits(gb
, 4);
244 sbr
->spectrum_params
.bs_stop_freq
= get_bits(gb
, 4);
245 sbr
->spectrum_params
.bs_xover_band
= get_bits(gb
, 3);
246 skip_bits(gb
, 2); // bs_reserved
248 bs_header_extra_1
= get_bits1(gb
);
249 bs_header_extra_2
= get_bits1(gb
);
251 if (bs_header_extra_1
) {
252 sbr
->spectrum_params
.bs_freq_scale
= get_bits(gb
, 2);
253 sbr
->spectrum_params
.bs_alter_scale
= get_bits1(gb
);
254 sbr
->spectrum_params
.bs_noise_bands
= get_bits(gb
, 2);
256 sbr
->spectrum_params
.bs_freq_scale
= 2;
257 sbr
->spectrum_params
.bs_alter_scale
= 1;
258 sbr
->spectrum_params
.bs_noise_bands
= 2;
261 // Check if spectrum parameters changed
262 if (memcmp(&old_spectrum_params
, &sbr
->spectrum_params
, sizeof(SpectrumParameters
)))
265 if (bs_header_extra_2
) {
266 sbr
->bs_limiter_bands
= get_bits(gb
, 2);
267 sbr
->bs_limiter_gains
= get_bits(gb
, 2);
268 sbr
->bs_interpol_freq
= get_bits1(gb
);
269 sbr
->bs_smoothing_mode
= get_bits1(gb
);
271 sbr
->bs_limiter_bands
= 2;
272 sbr
->bs_limiter_gains
= 2;
273 sbr
->bs_interpol_freq
= 1;
274 sbr
->bs_smoothing_mode
= 1;
277 if (sbr
->bs_limiter_bands
!= old_bs_limiter_bands
&& !sbr
->reset
)
278 sbr_make_f_tablelim(sbr
);
280 return get_bits_count(gb
) - cnt
;
283 static int array_min_int16(const int16_t *array
, int nel
)
285 int i
, min
= array
[0];
286 for (i
= 1; i
< nel
; i
++)
287 min
= FFMIN(array
[i
], min
);
291 static void make_bands(int16_t* bands
, int start
, int stop
, int num_bands
)
293 int k
, previous
, present
;
296 base
= powf((float)stop
/ start
, 1.0f
/ num_bands
);
300 for (k
= 0; k
< num_bands
-1; k
++) {
302 present
= lrintf(prod
);
303 bands
[k
] = present
- previous
;
306 bands
[num_bands
-1] = stop
- previous
;
309 static int check_n_master(AVCodecContext
*avctx
, int n_master
, int bs_xover_band
)
311 // Requirements (14496-3 sp04 p205)
313 av_log(avctx
, AV_LOG_ERROR
, "Invalid n_master: %d\n", n_master
);
316 if (bs_xover_band
>= n_master
) {
317 av_log(avctx
, AV_LOG_ERROR
,
318 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
325 /// Master Frequency Band Table (14496-3 sp04 p194)
326 static int sbr_make_f_master(AACContext
*ac
, SpectralBandReplication
*sbr
,
327 SpectrumParameters
*spectrum
)
329 unsigned int temp
, max_qmf_subbands
= 0;
330 unsigned int start_min
, stop_min
;
332 const int8_t *sbr_offset_ptr
;
335 if (sbr
->sample_rate
< 32000) {
337 } else if (sbr
->sample_rate
< 64000) {
342 switch (sbr
->sample_rate
) {
344 sbr_offset_ptr
= sbr_offset
[0];
347 sbr_offset_ptr
= sbr_offset
[1];
350 sbr_offset_ptr
= sbr_offset
[2];
353 sbr_offset_ptr
= sbr_offset
[3];
355 case 44100: case 48000: case 64000:
356 sbr_offset_ptr
= sbr_offset
[4];
358 case 88200: case 96000: case 128000: case 176400: case 192000:
359 sbr_offset_ptr
= sbr_offset
[5];
362 av_log(ac
->avctx
, AV_LOG_ERROR
,
363 "Unsupported sample rate for SBR: %d\n", sbr
->sample_rate
);
367 start_min
= ((temp
<< 7) + (sbr
->sample_rate
>> 1)) / sbr
->sample_rate
;
368 stop_min
= ((temp
<< 8) + (sbr
->sample_rate
>> 1)) / sbr
->sample_rate
;
370 sbr
->k
[0] = start_min
+ sbr_offset_ptr
[spectrum
->bs_start_freq
];
372 if (spectrum
->bs_stop_freq
< 14) {
373 sbr
->k
[2] = stop_min
;
374 make_bands(stop_dk
, stop_min
, 64, 13);
375 qsort(stop_dk
, 13, sizeof(stop_dk
[0]), qsort_comparison_function_int16
);
376 for (k
= 0; k
< spectrum
->bs_stop_freq
; k
++)
377 sbr
->k
[2] += stop_dk
[k
];
378 } else if (spectrum
->bs_stop_freq
== 14) {
379 sbr
->k
[2] = 2*sbr
->k
[0];
380 } else if (spectrum
->bs_stop_freq
== 15) {
381 sbr
->k
[2] = 3*sbr
->k
[0];
383 av_log(ac
->avctx
, AV_LOG_ERROR
,
384 "Invalid bs_stop_freq: %d\n", spectrum
->bs_stop_freq
);
387 sbr
->k
[2] = FFMIN(64, sbr
->k
[2]);
389 // Requirements (14496-3 sp04 p205)
390 if (sbr
->sample_rate
<= 32000) {
391 max_qmf_subbands
= 48;
392 } else if (sbr
->sample_rate
== 44100) {
393 max_qmf_subbands
= 35;
394 } else if (sbr
->sample_rate
>= 48000)
395 max_qmf_subbands
= 32;
399 if (sbr
->k
[2] - sbr
->k
[0] > max_qmf_subbands
) {
400 av_log(ac
->avctx
, AV_LOG_ERROR
,
401 "Invalid bitstream, too many QMF subbands: %d\n", sbr
->k
[2] - sbr
->k
[0]);
405 if (!spectrum
->bs_freq_scale
) {
408 dk
= spectrum
->bs_alter_scale
+ 1;
409 sbr
->n_master
= ((sbr
->k
[2] - sbr
->k
[0] + (dk
&2)) >> dk
) << 1;
410 if (check_n_master(ac
->avctx
, sbr
->n_master
, sbr
->spectrum_params
.bs_xover_band
))
413 for (k
= 1; k
<= sbr
->n_master
; k
++)
414 sbr
->f_master
[k
] = dk
;
416 k2diff
= sbr
->k
[2] - sbr
->k
[0] - sbr
->n_master
* dk
;
419 sbr
->f_master
[2]-= (k2diff
< -1);
421 sbr
->f_master
[sbr
->n_master
]++;
424 sbr
->f_master
[0] = sbr
->k
[0];
425 for (k
= 1; k
<= sbr
->n_master
; k
++)
426 sbr
->f_master
[k
] += sbr
->f_master
[k
- 1];
429 int half_bands
= 7 - spectrum
->bs_freq_scale
; // bs_freq_scale = {1,2,3}
430 int two_regions
, num_bands_0
;
431 int vdk0_max
, vdk1_min
;
434 if (49 * sbr
->k
[2] > 110 * sbr
->k
[0]) {
436 sbr
->k
[1] = 2 * sbr
->k
[0];
439 sbr
->k
[1] = sbr
->k
[2];
442 num_bands_0
= lrintf(half_bands
* log2f(sbr
->k
[1] / (float)sbr
->k
[0])) * 2;
444 if (num_bands_0
<= 0) { // Requirements (14496-3 sp04 p205)
445 av_log(ac
->avctx
, AV_LOG_ERROR
, "Invalid num_bands_0: %d\n", num_bands_0
);
451 make_bands(vk0
+1, sbr
->k
[0], sbr
->k
[1], num_bands_0
);
453 qsort(vk0
+ 1, num_bands_0
, sizeof(vk0
[1]), qsort_comparison_function_int16
);
454 vdk0_max
= vk0
[num_bands_0
];
457 for (k
= 1; k
<= num_bands_0
; k
++) {
458 if (vk0
[k
] <= 0) { // Requirements (14496-3 sp04 p205)
459 av_log(ac
->avctx
, AV_LOG_ERROR
, "Invalid vDk0[%d]: %d\n", k
, vk0
[k
]);
467 float invwarp
= spectrum
->bs_alter_scale
? 0.76923076923076923077f
468 : 1.0f
; // bs_alter_scale = {0,1}
469 int num_bands_1
= lrintf(half_bands
* invwarp
*
470 log2f(sbr
->k
[2] / (float)sbr
->k
[1])) * 2;
472 make_bands(vk1
+1, sbr
->k
[1], sbr
->k
[2], num_bands_1
);
474 vdk1_min
= array_min_int16(vk1
+ 1, num_bands_1
);
476 if (vdk1_min
< vdk0_max
) {
478 qsort(vk1
+ 1, num_bands_1
, sizeof(vk1
[1]), qsort_comparison_function_int16
);
479 change
= FFMIN(vdk0_max
- vk1
[1], (vk1
[num_bands_1
] - vk1
[1]) >> 1);
481 vk1
[num_bands_1
] -= change
;
484 qsort(vk1
+ 1, num_bands_1
, sizeof(vk1
[1]), qsort_comparison_function_int16
);
487 for (k
= 1; k
<= num_bands_1
; k
++) {
488 if (vk1
[k
] <= 0) { // Requirements (14496-3 sp04 p205)
489 av_log(ac
->avctx
, AV_LOG_ERROR
, "Invalid vDk1[%d]: %d\n", k
, vk1
[k
]);
495 sbr
->n_master
= num_bands_0
+ num_bands_1
;
496 if (check_n_master(ac
->avctx
, sbr
->n_master
, sbr
->spectrum_params
.bs_xover_band
))
498 memcpy(&sbr
->f_master
[0], vk0
,
499 (num_bands_0
+ 1) * sizeof(sbr
->f_master
[0]));
500 memcpy(&sbr
->f_master
[num_bands_0
+ 1], vk1
+ 1,
501 num_bands_1
* sizeof(sbr
->f_master
[0]));
504 sbr
->n_master
= num_bands_0
;
505 if (check_n_master(ac
->avctx
, sbr
->n_master
, sbr
->spectrum_params
.bs_xover_band
))
507 memcpy(sbr
->f_master
, vk0
, (num_bands_0
+ 1) * sizeof(sbr
->f_master
[0]));
514 /// High Frequency Generation - Patch Construction (14496-3 sp04 p216 fig. 4.46)
515 static int sbr_hf_calc_npatches(AACContext
*ac
, SpectralBandReplication
*sbr
)
519 int usb
= sbr
->kx
[1];
520 int goal_sb
= ((1000 << 11) + (sbr
->sample_rate
>> 1)) / sbr
->sample_rate
;
522 sbr
->num_patches
= 0;
524 if (goal_sb
< sbr
->kx
[1] + sbr
->m
[1]) {
525 for (k
= 0; sbr
->f_master
[k
] < goal_sb
; k
++) ;
531 for (i
= k
; i
== k
|| sb
> (sbr
->k
[0] - 1 + msb
- odd
); i
--) {
532 sb
= sbr
->f_master
[i
];
533 odd
= (sb
+ sbr
->k
[0]) & 1;
536 // Requirements (14496-3 sp04 p205) sets the maximum number of patches to 5.
537 // After this check the final number of patches can still be six which is
538 // illegal however the Coding Technologies decoder check stream has a final
539 // count of 6 patches
540 if (sbr
->num_patches
> 5) {
541 av_log(ac
->avctx
, AV_LOG_ERROR
, "Too many patches: %d\n", sbr
->num_patches
);
545 sbr
->patch_num_subbands
[sbr
->num_patches
] = FFMAX(sb
- usb
, 0);
546 sbr
->patch_start_subband
[sbr
->num_patches
] = sbr
->k
[0] - odd
- sbr
->patch_num_subbands
[sbr
->num_patches
];
548 if (sbr
->patch_num_subbands
[sbr
->num_patches
] > 0) {
555 if (sbr
->f_master
[k
] - sb
< 3)
557 } while (sb
!= sbr
->kx
[1] + sbr
->m
[1]);
559 if (sbr
->num_patches
> 1 &&
560 sbr
->patch_num_subbands
[sbr
->num_patches
- 1] < 3)
566 /// Derived Frequency Band Tables (14496-3 sp04 p197)
567 static int sbr_make_f_derived(AACContext
*ac
, SpectralBandReplication
*sbr
)
571 sbr
->n
[1] = sbr
->n_master
- sbr
->spectrum_params
.bs_xover_band
;
572 sbr
->n
[0] = (sbr
->n
[1] + 1) >> 1;
574 memcpy(sbr
->f_tablehigh
, &sbr
->f_master
[sbr
->spectrum_params
.bs_xover_band
],
575 (sbr
->n
[1] + 1) * sizeof(sbr
->f_master
[0]));
576 sbr
->m
[1] = sbr
->f_tablehigh
[sbr
->n
[1]] - sbr
->f_tablehigh
[0];
577 sbr
->kx
[1] = sbr
->f_tablehigh
[0];
579 // Requirements (14496-3 sp04 p205)
580 if (sbr
->kx
[1] + sbr
->m
[1] > 64) {
581 av_log(ac
->avctx
, AV_LOG_ERROR
,
582 "Stop frequency border too high: %d\n", sbr
->kx
[1] + sbr
->m
[1]);
585 if (sbr
->kx
[1] > 32) {
586 av_log(ac
->avctx
, AV_LOG_ERROR
, "Start frequency border too high: %d\n", sbr
->kx
[1]);
590 sbr
->f_tablelow
[0] = sbr
->f_tablehigh
[0];
591 temp
= sbr
->n
[1] & 1;
592 for (k
= 1; k
<= sbr
->n
[0]; k
++)
593 sbr
->f_tablelow
[k
] = sbr
->f_tablehigh
[2 * k
- temp
];
595 sbr
->n_q
= FFMAX(1, lrintf(sbr
->spectrum_params
.bs_noise_bands
*
596 log2f(sbr
->k
[2] / (float)sbr
->kx
[1]))); // 0 <= bs_noise_bands <= 3
598 av_log(ac
->avctx
, AV_LOG_ERROR
, "Too many noise floor scale factors: %d\n", sbr
->n_q
);
602 sbr
->f_tablenoise
[0] = sbr
->f_tablelow
[0];
604 for (k
= 1; k
<= sbr
->n_q
; k
++) {
605 temp
+= (sbr
->n
[0] - temp
) / (sbr
->n_q
+ 1 - k
);
606 sbr
->f_tablenoise
[k
] = sbr
->f_tablelow
[temp
];
609 if (sbr_hf_calc_npatches(ac
, sbr
) < 0)
612 sbr_make_f_tablelim(sbr
);
614 sbr
->data
[0].f_indexnoise
= 0;
615 sbr
->data
[1].f_indexnoise
= 0;
620 static av_always_inline
void get_bits1_vector(GetBitContext
*gb
, uint8_t *vec
,
624 for (i
= 0; i
< elements
; i
++) {
625 vec
[i
] = get_bits1(gb
);
629 /** ceil(log2(index+1)) */
630 static const int8_t ceil_log2
[] = {
634 static int read_sbr_grid(AACContext
*ac
, SpectralBandReplication
*sbr
,
635 GetBitContext
*gb
, SBRData
*ch_data
)
638 unsigned bs_pointer
= 0;
639 // frameLengthFlag ? 15 : 16; 960 sample length frames unsupported; this value is numTimeSlots
640 int abs_bord_trail
= 16;
641 int num_rel_lead
, num_rel_trail
;
642 unsigned bs_num_env_old
= ch_data
->bs_num_env
;
644 ch_data
->bs_freq_res
[0] = ch_data
->bs_freq_res
[ch_data
->bs_num_env
];
645 ch_data
->bs_amp_res
= sbr
->bs_amp_res_header
;
646 ch_data
->t_env_num_env_old
= ch_data
->t_env
[bs_num_env_old
];
648 switch (ch_data
->bs_frame_class
= get_bits(gb
, 2)) {
650 ch_data
->bs_num_env
= 1 << get_bits(gb
, 2);
651 num_rel_lead
= ch_data
->bs_num_env
- 1;
652 if (ch_data
->bs_num_env
== 1)
653 ch_data
->bs_amp_res
= 0;
655 if (ch_data
->bs_num_env
> 4) {
656 av_log(ac
->avctx
, AV_LOG_ERROR
,
657 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
658 ch_data
->bs_num_env
);
662 ch_data
->t_env
[0] = 0;
663 ch_data
->t_env
[ch_data
->bs_num_env
] = abs_bord_trail
;
665 abs_bord_trail
= (abs_bord_trail
+ (ch_data
->bs_num_env
>> 1)) /
667 for (i
= 0; i
< num_rel_lead
; i
++)
668 ch_data
->t_env
[i
+ 1] = ch_data
->t_env
[i
] + abs_bord_trail
;
670 ch_data
->bs_freq_res
[1] = get_bits1(gb
);
671 for (i
= 1; i
< ch_data
->bs_num_env
; i
++)
672 ch_data
->bs_freq_res
[i
+ 1] = ch_data
->bs_freq_res
[1];
675 abs_bord_trail
+= get_bits(gb
, 2);
676 num_rel_trail
= get_bits(gb
, 2);
677 ch_data
->bs_num_env
= num_rel_trail
+ 1;
678 ch_data
->t_env
[0] = 0;
679 ch_data
->t_env
[ch_data
->bs_num_env
] = abs_bord_trail
;
681 for (i
= 0; i
< num_rel_trail
; i
++)
682 ch_data
->t_env
[ch_data
->bs_num_env
- 1 - i
] =
683 ch_data
->t_env
[ch_data
->bs_num_env
- i
] - 2 * get_bits(gb
, 2) - 2;
685 bs_pointer
= get_bits(gb
, ceil_log2
[ch_data
->bs_num_env
]);
687 for (i
= 0; i
< ch_data
->bs_num_env
; i
++)
688 ch_data
->bs_freq_res
[ch_data
->bs_num_env
- i
] = get_bits1(gb
);
691 ch_data
->t_env
[0] = get_bits(gb
, 2);
692 num_rel_lead
= get_bits(gb
, 2);
693 ch_data
->bs_num_env
= num_rel_lead
+ 1;
694 ch_data
->t_env
[ch_data
->bs_num_env
] = abs_bord_trail
;
696 for (i
= 0; i
< num_rel_lead
; i
++)
697 ch_data
->t_env
[i
+ 1] = ch_data
->t_env
[i
] + 2 * get_bits(gb
, 2) + 2;
699 bs_pointer
= get_bits(gb
, ceil_log2
[ch_data
->bs_num_env
]);
701 get_bits1_vector(gb
, ch_data
->bs_freq_res
+ 1, ch_data
->bs_num_env
);
704 ch_data
->t_env
[0] = get_bits(gb
, 2);
705 abs_bord_trail
+= get_bits(gb
, 2);
706 num_rel_lead
= get_bits(gb
, 2);
707 num_rel_trail
= get_bits(gb
, 2);
708 ch_data
->bs_num_env
= num_rel_lead
+ num_rel_trail
+ 1;
710 if (ch_data
->bs_num_env
> 5) {
711 av_log(ac
->avctx
, AV_LOG_ERROR
,
712 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
713 ch_data
->bs_num_env
);
717 ch_data
->t_env
[ch_data
->bs_num_env
] = abs_bord_trail
;
719 for (i
= 0; i
< num_rel_lead
; i
++)
720 ch_data
->t_env
[i
+ 1] = ch_data
->t_env
[i
] + 2 * get_bits(gb
, 2) + 2;
721 for (i
= 0; i
< num_rel_trail
; i
++)
722 ch_data
->t_env
[ch_data
->bs_num_env
- 1 - i
] =
723 ch_data
->t_env
[ch_data
->bs_num_env
- i
] - 2 * get_bits(gb
, 2) - 2;
725 bs_pointer
= get_bits(gb
, ceil_log2
[ch_data
->bs_num_env
]);
727 get_bits1_vector(gb
, ch_data
->bs_freq_res
+ 1, ch_data
->bs_num_env
);
731 if (bs_pointer
> ch_data
->bs_num_env
+ 1) {
732 av_log(ac
->avctx
, AV_LOG_ERROR
,
733 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
738 for (i
= 1; i
<= ch_data
->bs_num_env
; i
++) {
739 if (ch_data
->t_env
[i
-1] > ch_data
->t_env
[i
]) {
740 av_log(ac
->avctx
, AV_LOG_ERROR
, "Non monotone time borders\n");
745 ch_data
->bs_num_noise
= (ch_data
->bs_num_env
> 1) + 1;
747 ch_data
->t_q
[0] = ch_data
->t_env
[0];
748 ch_data
->t_q
[ch_data
->bs_num_noise
] = ch_data
->t_env
[ch_data
->bs_num_env
];
749 if (ch_data
->bs_num_noise
> 1) {
751 if (ch_data
->bs_frame_class
== FIXFIX
) {
752 idx
= ch_data
->bs_num_env
>> 1;
753 } else if (ch_data
->bs_frame_class
& 1) { // FIXVAR or VARVAR
754 idx
= ch_data
->bs_num_env
- FFMAX((int)bs_pointer
- 1, 1);
758 else if (bs_pointer
== 1)
759 idx
= ch_data
->bs_num_env
- 1;
760 else // bs_pointer > 1
761 idx
= bs_pointer
- 1;
763 ch_data
->t_q
[1] = ch_data
->t_env
[idx
];
766 ch_data
->e_a
[0] = -(ch_data
->e_a
[1] != bs_num_env_old
); // l_APrev
767 ch_data
->e_a
[1] = -1;
768 if ((ch_data
->bs_frame_class
& 1) && bs_pointer
) { // FIXVAR or VARVAR and bs_pointer != 0
769 ch_data
->e_a
[1] = ch_data
->bs_num_env
+ 1 - bs_pointer
;
770 } else if ((ch_data
->bs_frame_class
== 2) && (bs_pointer
> 1)) // VARFIX and bs_pointer > 1
771 ch_data
->e_a
[1] = bs_pointer
- 1;
776 static void copy_sbr_grid(SBRData
*dst
, const SBRData
*src
) {
777 //These variables are saved from the previous frame rather than copied
778 dst
->bs_freq_res
[0] = dst
->bs_freq_res
[dst
->bs_num_env
];
779 dst
->t_env_num_env_old
= dst
->t_env
[dst
->bs_num_env
];
780 dst
->e_a
[0] = -(dst
->e_a
[1] != dst
->bs_num_env
);
782 //These variables are read from the bitstream and therefore copied
783 memcpy(dst
->bs_freq_res
+1, src
->bs_freq_res
+1, sizeof(dst
->bs_freq_res
)-sizeof(*dst
->bs_freq_res
));
784 memcpy(dst
->t_env
, src
->t_env
, sizeof(dst
->t_env
));
785 memcpy(dst
->t_q
, src
->t_q
, sizeof(dst
->t_q
));
786 dst
->bs_num_env
= src
->bs_num_env
;
787 dst
->bs_amp_res
= src
->bs_amp_res
;
788 dst
->bs_num_noise
= src
->bs_num_noise
;
789 dst
->bs_frame_class
= src
->bs_frame_class
;
790 dst
->e_a
[1] = src
->e_a
[1];
793 /// Read how the envelope and noise floor data is delta coded
794 static void read_sbr_dtdf(SpectralBandReplication
*sbr
, GetBitContext
*gb
,
797 get_bits1_vector(gb
, ch_data
->bs_df_env
, ch_data
->bs_num_env
);
798 get_bits1_vector(gb
, ch_data
->bs_df_noise
, ch_data
->bs_num_noise
);
801 /// Read inverse filtering data
802 static void read_sbr_invf(SpectralBandReplication
*sbr
, GetBitContext
*gb
,
807 memcpy(ch_data
->bs_invf_mode
[1], ch_data
->bs_invf_mode
[0], 5 * sizeof(uint8_t));
808 for (i
= 0; i
< sbr
->n_q
; i
++)
809 ch_data
->bs_invf_mode
[0][i
] = get_bits(gb
, 2);
812 static void read_sbr_envelope(SpectralBandReplication
*sbr
, GetBitContext
*gb
,
813 SBRData
*ch_data
, int ch
)
817 VLC_TYPE (*t_huff
)[2], (*f_huff
)[2];
819 const int delta
= (ch
== 1 && sbr
->bs_coupling
== 1) + 1;
820 const int odd
= sbr
->n
[1] & 1;
822 if (sbr
->bs_coupling
&& ch
) {
823 if (ch_data
->bs_amp_res
) {
825 t_huff
= vlc_sbr
[T_HUFFMAN_ENV_BAL_3_0DB
].table
;
826 t_lav
= vlc_sbr_lav
[T_HUFFMAN_ENV_BAL_3_0DB
];
827 f_huff
= vlc_sbr
[F_HUFFMAN_ENV_BAL_3_0DB
].table
;
828 f_lav
= vlc_sbr_lav
[F_HUFFMAN_ENV_BAL_3_0DB
];
831 t_huff
= vlc_sbr
[T_HUFFMAN_ENV_BAL_1_5DB
].table
;
832 t_lav
= vlc_sbr_lav
[T_HUFFMAN_ENV_BAL_1_5DB
];
833 f_huff
= vlc_sbr
[F_HUFFMAN_ENV_BAL_1_5DB
].table
;
834 f_lav
= vlc_sbr_lav
[F_HUFFMAN_ENV_BAL_1_5DB
];
837 if (ch_data
->bs_amp_res
) {
839 t_huff
= vlc_sbr
[T_HUFFMAN_ENV_3_0DB
].table
;
840 t_lav
= vlc_sbr_lav
[T_HUFFMAN_ENV_3_0DB
];
841 f_huff
= vlc_sbr
[F_HUFFMAN_ENV_3_0DB
].table
;
842 f_lav
= vlc_sbr_lav
[F_HUFFMAN_ENV_3_0DB
];
845 t_huff
= vlc_sbr
[T_HUFFMAN_ENV_1_5DB
].table
;
846 t_lav
= vlc_sbr_lav
[T_HUFFMAN_ENV_1_5DB
];
847 f_huff
= vlc_sbr
[F_HUFFMAN_ENV_1_5DB
].table
;
848 f_lav
= vlc_sbr_lav
[F_HUFFMAN_ENV_1_5DB
];
852 for (i
= 0; i
< ch_data
->bs_num_env
; i
++) {
853 if (ch_data
->bs_df_env
[i
]) {
854 // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame
855 if (ch_data
->bs_freq_res
[i
+ 1] == ch_data
->bs_freq_res
[i
]) {
856 for (j
= 0; j
< sbr
->n
[ch_data
->bs_freq_res
[i
+ 1]]; j
++)
857 ch_data
->env_facs
[i
+ 1][j
] = ch_data
->env_facs
[i
][j
] + delta
* (get_vlc2(gb
, t_huff
, 9, 3) - t_lav
);
858 } else if (ch_data
->bs_freq_res
[i
+ 1]) {
859 for (j
= 0; j
< sbr
->n
[ch_data
->bs_freq_res
[i
+ 1]]; j
++) {
860 k
= (j
+ odd
) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]
861 ch_data
->env_facs
[i
+ 1][j
] = ch_data
->env_facs
[i
][k
] + delta
* (get_vlc2(gb
, t_huff
, 9, 3) - t_lav
);
864 for (j
= 0; j
< sbr
->n
[ch_data
->bs_freq_res
[i
+ 1]]; j
++) {
865 k
= j
? 2*j
- odd
: 0; // find k such that f_tablehigh[k] == f_tablelow[j]
866 ch_data
->env_facs
[i
+ 1][j
] = ch_data
->env_facs
[i
][k
] + delta
* (get_vlc2(gb
, t_huff
, 9, 3) - t_lav
);
870 ch_data
->env_facs
[i
+ 1][0] = delta
* get_bits(gb
, bits
); // bs_env_start_value_balance
871 for (j
= 1; j
< sbr
->n
[ch_data
->bs_freq_res
[i
+ 1]]; j
++)
872 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
);
876 //assign 0th elements of env_facs from last elements
877 memcpy(ch_data
->env_facs
[0], ch_data
->env_facs
[ch_data
->bs_num_env
],
878 sizeof(ch_data
->env_facs
[0]));
881 static void read_sbr_noise(SpectralBandReplication
*sbr
, GetBitContext
*gb
,
882 SBRData
*ch_data
, int ch
)
885 VLC_TYPE (*t_huff
)[2], (*f_huff
)[2];
887 int delta
= (ch
== 1 && sbr
->bs_coupling
== 1) + 1;
889 if (sbr
->bs_coupling
&& ch
) {
890 t_huff
= vlc_sbr
[T_HUFFMAN_NOISE_BAL_3_0DB
].table
;
891 t_lav
= vlc_sbr_lav
[T_HUFFMAN_NOISE_BAL_3_0DB
];
892 f_huff
= vlc_sbr
[F_HUFFMAN_ENV_BAL_3_0DB
].table
;
893 f_lav
= vlc_sbr_lav
[F_HUFFMAN_ENV_BAL_3_0DB
];
895 t_huff
= vlc_sbr
[T_HUFFMAN_NOISE_3_0DB
].table
;
896 t_lav
= vlc_sbr_lav
[T_HUFFMAN_NOISE_3_0DB
];
897 f_huff
= vlc_sbr
[F_HUFFMAN_ENV_3_0DB
].table
;
898 f_lav
= vlc_sbr_lav
[F_HUFFMAN_ENV_3_0DB
];
901 for (i
= 0; i
< ch_data
->bs_num_noise
; i
++) {
902 if (ch_data
->bs_df_noise
[i
]) {
903 for (j
= 0; j
< sbr
->n_q
; j
++)
904 ch_data
->noise_facs
[i
+ 1][j
] = ch_data
->noise_facs
[i
][j
] + delta
* (get_vlc2(gb
, t_huff
, 9, 2) - t_lav
);
906 ch_data
->noise_facs
[i
+ 1][0] = delta
* get_bits(gb
, 5); // bs_noise_start_value_balance or bs_noise_start_value_level
907 for (j
= 1; j
< sbr
->n_q
; j
++)
908 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
);
912 //assign 0th elements of noise_facs from last elements
913 memcpy(ch_data
->noise_facs
[0], ch_data
->noise_facs
[ch_data
->bs_num_noise
],
914 sizeof(ch_data
->noise_facs
[0]));
917 static void read_sbr_extension(AACContext
*ac
, SpectralBandReplication
*sbr
,
919 int bs_extension_id
, int *num_bits_left
)
921 switch (bs_extension_id
) {
922 case EXTENSION_ID_PS
:
923 if (!ac
->oc
[1].m4ac
.ps
) {
924 av_log(ac
->avctx
, AV_LOG_ERROR
, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
925 skip_bits_long(gb
, *num_bits_left
); // bs_fill_bits
929 *num_bits_left
-= ff_ps_read_data(ac
->avctx
, gb
, &sbr
->ps
, *num_bits_left
);
930 ac
->avctx
->profile
= FF_PROFILE_AAC_HE_V2
;
932 avpriv_report_missing_feature(ac
->avctx
, "Parametric Stereo");
933 skip_bits_long(gb
, *num_bits_left
); // bs_fill_bits
939 // some files contain 0-padding
940 if (bs_extension_id
|| *num_bits_left
> 16 || show_bits(gb
, *num_bits_left
))
941 avpriv_request_sample(ac
->avctx
, "Reserved SBR extensions");
942 skip_bits_long(gb
, *num_bits_left
); // bs_fill_bits
948 static int read_sbr_single_channel_element(AACContext
*ac
,
949 SpectralBandReplication
*sbr
,
952 if (get_bits1(gb
)) // bs_data_extra
953 skip_bits(gb
, 4); // bs_reserved
955 if (read_sbr_grid(ac
, sbr
, gb
, &sbr
->data
[0]))
957 read_sbr_dtdf(sbr
, gb
, &sbr
->data
[0]);
958 read_sbr_invf(sbr
, gb
, &sbr
->data
[0]);
959 read_sbr_envelope(sbr
, gb
, &sbr
->data
[0], 0);
960 read_sbr_noise(sbr
, gb
, &sbr
->data
[0], 0);
962 if ((sbr
->data
[0].bs_add_harmonic_flag
= get_bits1(gb
)))
963 get_bits1_vector(gb
, sbr
->data
[0].bs_add_harmonic
, sbr
->n
[1]);
968 static int read_sbr_channel_pair_element(AACContext
*ac
,
969 SpectralBandReplication
*sbr
,
972 if (get_bits1(gb
)) // bs_data_extra
973 skip_bits(gb
, 8); // bs_reserved
975 if ((sbr
->bs_coupling
= get_bits1(gb
))) {
976 if (read_sbr_grid(ac
, sbr
, gb
, &sbr
->data
[0]))
978 copy_sbr_grid(&sbr
->data
[1], &sbr
->data
[0]);
979 read_sbr_dtdf(sbr
, gb
, &sbr
->data
[0]);
980 read_sbr_dtdf(sbr
, gb
, &sbr
->data
[1]);
981 read_sbr_invf(sbr
, gb
, &sbr
->data
[0]);
982 memcpy(sbr
->data
[1].bs_invf_mode
[1], sbr
->data
[1].bs_invf_mode
[0], sizeof(sbr
->data
[1].bs_invf_mode
[0]));
983 memcpy(sbr
->data
[1].bs_invf_mode
[0], sbr
->data
[0].bs_invf_mode
[0], sizeof(sbr
->data
[1].bs_invf_mode
[0]));
984 read_sbr_envelope(sbr
, gb
, &sbr
->data
[0], 0);
985 read_sbr_noise(sbr
, gb
, &sbr
->data
[0], 0);
986 read_sbr_envelope(sbr
, gb
, &sbr
->data
[1], 1);
987 read_sbr_noise(sbr
, gb
, &sbr
->data
[1], 1);
989 if (read_sbr_grid(ac
, sbr
, gb
, &sbr
->data
[0]) ||
990 read_sbr_grid(ac
, sbr
, gb
, &sbr
->data
[1]))
992 read_sbr_dtdf(sbr
, gb
, &sbr
->data
[0]);
993 read_sbr_dtdf(sbr
, gb
, &sbr
->data
[1]);
994 read_sbr_invf(sbr
, gb
, &sbr
->data
[0]);
995 read_sbr_invf(sbr
, gb
, &sbr
->data
[1]);
996 read_sbr_envelope(sbr
, gb
, &sbr
->data
[0], 0);
997 read_sbr_envelope(sbr
, gb
, &sbr
->data
[1], 1);
998 read_sbr_noise(sbr
, gb
, &sbr
->data
[0], 0);
999 read_sbr_noise(sbr
, gb
, &sbr
->data
[1], 1);
1002 if ((sbr
->data
[0].bs_add_harmonic_flag
= get_bits1(gb
)))
1003 get_bits1_vector(gb
, sbr
->data
[0].bs_add_harmonic
, sbr
->n
[1]);
1004 if ((sbr
->data
[1].bs_add_harmonic_flag
= get_bits1(gb
)))
1005 get_bits1_vector(gb
, sbr
->data
[1].bs_add_harmonic
, sbr
->n
[1]);
1010 static unsigned int read_sbr_data(AACContext
*ac
, SpectralBandReplication
*sbr
,
1011 GetBitContext
*gb
, int id_aac
)
1013 unsigned int cnt
= get_bits_count(gb
);
1015 if (id_aac
== TYPE_SCE
|| id_aac
== TYPE_CCE
) {
1016 if (read_sbr_single_channel_element(ac
, sbr
, gb
)) {
1018 return get_bits_count(gb
) - cnt
;
1020 } else if (id_aac
== TYPE_CPE
) {
1021 if (read_sbr_channel_pair_element(ac
, sbr
, gb
)) {
1023 return get_bits_count(gb
) - cnt
;
1026 av_log(ac
->avctx
, AV_LOG_ERROR
,
1027 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac
);
1029 return get_bits_count(gb
) - cnt
;
1031 if (get_bits1(gb
)) { // bs_extended_data
1032 int num_bits_left
= get_bits(gb
, 4); // bs_extension_size
1033 if (num_bits_left
== 15)
1034 num_bits_left
+= get_bits(gb
, 8); // bs_esc_count
1036 num_bits_left
<<= 3;
1037 while (num_bits_left
> 7) {
1039 read_sbr_extension(ac
, sbr
, gb
, get_bits(gb
, 2), &num_bits_left
); // bs_extension_id
1041 if (num_bits_left
< 0) {
1042 av_log(ac
->avctx
, AV_LOG_ERROR
, "SBR Extension over read.\n");
1044 if (num_bits_left
> 0)
1045 skip_bits(gb
, num_bits_left
);
1048 return get_bits_count(gb
) - cnt
;
1051 static void sbr_reset(AACContext
*ac
, SpectralBandReplication
*sbr
)
1054 err
= sbr_make_f_master(ac
, sbr
, &sbr
->spectrum_params
);
1056 err
= sbr_make_f_derived(ac
, sbr
);
1058 av_log(ac
->avctx
, AV_LOG_ERROR
,
1059 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1065 * Decode Spectral Band Replication extension data; reference: table 4.55.
1067 * @param crc flag indicating the presence of CRC checksum
1068 * @param cnt length of TYPE_FIL syntactic element in bytes
1070 * @return Returns number of bytes consumed from the TYPE_FIL element.
1072 int ff_decode_sbr_extension(AACContext
*ac
, SpectralBandReplication
*sbr
,
1073 GetBitContext
*gb_host
, int crc
, int cnt
, int id_aac
)
1075 unsigned int num_sbr_bits
= 0, num_align_bits
;
1076 unsigned bytes_read
;
1077 GetBitContext gbc
= *gb_host
, *gb
= &gbc
;
1078 skip_bits_long(gb_host
, cnt
*8 - 4);
1082 if (!sbr
->sample_rate
)
1083 sbr
->sample_rate
= 2 * ac
->oc
[1].m4ac
.sample_rate
; //TODO use the nominal sample rate for arbitrary sample rate support
1084 if (!ac
->oc
[1].m4ac
.ext_sample_rate
)
1085 ac
->oc
[1].m4ac
.ext_sample_rate
= 2 * ac
->oc
[1].m4ac
.sample_rate
;
1088 skip_bits(gb
, 10); // bs_sbr_crc_bits; TODO - implement CRC check
1092 //Save some state from the previous frame.
1093 sbr
->kx
[0] = sbr
->kx
[1];
1094 sbr
->m
[0] = sbr
->m
[1];
1095 sbr
->kx_and_m_pushed
= 1;
1098 if (get_bits1(gb
)) // bs_header_flag
1099 num_sbr_bits
+= read_sbr_header(sbr
, gb
);
1105 num_sbr_bits
+= read_sbr_data(ac
, sbr
, gb
, id_aac
);
1107 num_align_bits
= ((cnt
<< 3) - 4 - num_sbr_bits
) & 7;
1108 bytes_read
= ((num_sbr_bits
+ num_align_bits
+ 4) >> 3);
1110 if (bytes_read
> cnt
) {
1111 av_log(ac
->avctx
, AV_LOG_ERROR
,
1112 "Expected to read %d SBR bytes actually read %d.\n", cnt
, bytes_read
);
1117 /// Dequantization and stereo decoding (14496-3 sp04 p203)
1118 static void sbr_dequant(SpectralBandReplication
*sbr
, int id_aac
)
1123 if (id_aac
== TYPE_CPE
&& sbr
->bs_coupling
) {
1124 float alpha
= sbr
->data
[0].bs_amp_res
? 1.0f
: 0.5f
;
1125 float pan_offset
= sbr
->data
[0].bs_amp_res
? 12.0f
: 24.0f
;
1126 for (e
= 1; e
<= sbr
->data
[0].bs_num_env
; e
++) {
1127 for (k
= 0; k
< sbr
->n
[sbr
->data
[0].bs_freq_res
[e
]]; k
++) {
1128 float temp1
= exp2f(sbr
->data
[0].env_facs
[e
][k
] * alpha
+ 7.0f
);
1129 float temp2
= exp2f((pan_offset
- sbr
->data
[1].env_facs
[e
][k
]) * alpha
);
1132 av_log(NULL
, AV_LOG_ERROR
, "envelope scalefactor overflow in dequant\n");
1135 fac
= temp1
/ (1.0f
+ temp2
);
1136 sbr
->data
[0].env_facs
[e
][k
] = fac
;
1137 sbr
->data
[1].env_facs
[e
][k
] = fac
* temp2
;
1140 for (e
= 1; e
<= sbr
->data
[0].bs_num_noise
; e
++) {
1141 for (k
= 0; k
< sbr
->n_q
; k
++) {
1142 float temp1
= exp2f(NOISE_FLOOR_OFFSET
- sbr
->data
[0].noise_facs
[e
][k
] + 1);
1143 float temp2
= exp2f(12 - sbr
->data
[1].noise_facs
[e
][k
]);
1146 av_log(NULL
, AV_LOG_ERROR
, "envelope scalefactor overflow in dequant\n");
1149 fac
= temp1
/ (1.0f
+ temp2
);
1150 sbr
->data
[0].noise_facs
[e
][k
] = fac
;
1151 sbr
->data
[1].noise_facs
[e
][k
] = fac
* temp2
;
1154 } else { // SCE or one non-coupled CPE
1155 for (ch
= 0; ch
< (id_aac
== TYPE_CPE
) + 1; ch
++) {
1156 float alpha
= sbr
->data
[ch
].bs_amp_res
? 1.0f
: 0.5f
;
1157 for (e
= 1; e
<= sbr
->data
[ch
].bs_num_env
; e
++)
1158 for (k
= 0; k
< sbr
->n
[sbr
->data
[ch
].bs_freq_res
[e
]]; k
++){
1159 sbr
->data
[ch
].env_facs
[e
][k
] =
1160 exp2f(alpha
* sbr
->data
[ch
].env_facs
[e
][k
] + 6.0f
);
1161 if (sbr
->data
[ch
].env_facs
[e
][k
] > 1E20
) {
1162 av_log(NULL
, AV_LOG_ERROR
, "envelope scalefactor overflow in dequant\n");
1163 sbr
->data
[ch
].env_facs
[e
][k
] = 1;
1167 for (e
= 1; e
<= sbr
->data
[ch
].bs_num_noise
; e
++)
1168 for (k
= 0; k
< sbr
->n_q
; k
++)
1169 sbr
->data
[ch
].noise_facs
[e
][k
] =
1170 exp2f(NOISE_FLOOR_OFFSET
- sbr
->data
[ch
].noise_facs
[e
][k
]);
1176 * Analysis QMF Bank (14496-3 sp04 p206)
1178 * @param x pointer to the beginning of the first sample window
1179 * @param W array of complex-valued samples split into subbands
1181 #ifndef sbr_qmf_analysis
1182 static void sbr_qmf_analysis(AVFloatDSPContext
*dsp
, FFTContext
*mdct
,
1183 SBRDSPContext
*sbrdsp
, const float *in
, float *x
,
1184 float z
[320], float W
[2][32][32][2], int buf_idx
)
1187 memcpy(x
, x
+1024, (320-32)*sizeof(x
[0]));
1188 memcpy(x
+288, in
, 1024*sizeof(x
[0]));
1189 for (i
= 0; i
< 32; i
++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1190 // are not supported
1191 dsp
->vector_fmul_reverse(z
, sbr_qmf_window_ds
, x
, 320);
1193 sbrdsp
->qmf_pre_shuffle(z
);
1194 mdct
->imdct_half(mdct
, z
, z
+64);
1195 sbrdsp
->qmf_post_shuffle(W
[buf_idx
][i
], z
);
1202 * Synthesis QMF Bank (14496-3 sp04 p206) and Downsampled Synthesis QMF Bank
1203 * (14496-3 sp04 p206)
1205 #ifndef sbr_qmf_synthesis
1206 static void sbr_qmf_synthesis(FFTContext
*mdct
,
1207 SBRDSPContext
*sbrdsp
, AVFloatDSPContext
*dsp
,
1208 float *out
, float X
[2][38][64],
1209 float mdct_buf
[2][64],
1210 float *v0
, int *v_off
, const unsigned int div
)
1213 const float *sbr_qmf_window
= div
? sbr_qmf_window_ds
: sbr_qmf_window_us
;
1214 const int step
= 128 >> div
;
1216 for (i
= 0; i
< 32; i
++) {
1217 if (*v_off
< step
) {
1218 int saved_samples
= (1280 - 128) >> div
;
1219 memcpy(&v0
[SBR_SYNTHESIS_BUF_SIZE
- saved_samples
], v0
, saved_samples
* sizeof(float));
1220 *v_off
= SBR_SYNTHESIS_BUF_SIZE
- saved_samples
- step
;
1226 for (n
= 0; n
< 32; n
++) {
1227 X
[0][i
][ n
] = -X
[0][i
][n
];
1228 X
[0][i
][32+n
] = X
[1][i
][31-n
];
1230 mdct
->imdct_half(mdct
, mdct_buf
[0], X
[0][i
]);
1231 sbrdsp
->qmf_deint_neg(v
, mdct_buf
[0]);
1233 sbrdsp
->neg_odd_64(X
[1][i
]);
1234 mdct
->imdct_half(mdct
, mdct_buf
[0], X
[0][i
]);
1235 mdct
->imdct_half(mdct
, mdct_buf
[1], X
[1][i
]);
1236 sbrdsp
->qmf_deint_bfly(v
, mdct_buf
[1], mdct_buf
[0]);
1238 dsp
->vector_fmul (out
, v
, sbr_qmf_window
, 64 >> div
);
1239 dsp
->vector_fmul_add(out
, v
+ ( 192 >> div
), sbr_qmf_window
+ ( 64 >> div
), out
, 64 >> div
);
1240 dsp
->vector_fmul_add(out
, v
+ ( 256 >> div
), sbr_qmf_window
+ (128 >> div
), out
, 64 >> div
);
1241 dsp
->vector_fmul_add(out
, v
+ ( 448 >> div
), sbr_qmf_window
+ (192 >> div
), out
, 64 >> div
);
1242 dsp
->vector_fmul_add(out
, v
+ ( 512 >> div
), sbr_qmf_window
+ (256 >> div
), out
, 64 >> div
);
1243 dsp
->vector_fmul_add(out
, v
+ ( 704 >> div
), sbr_qmf_window
+ (320 >> div
), out
, 64 >> div
);
1244 dsp
->vector_fmul_add(out
, v
+ ( 768 >> div
), sbr_qmf_window
+ (384 >> div
), out
, 64 >> div
);
1245 dsp
->vector_fmul_add(out
, v
+ ( 960 >> div
), sbr_qmf_window
+ (448 >> div
), out
, 64 >> div
);
1246 dsp
->vector_fmul_add(out
, v
+ (1024 >> div
), sbr_qmf_window
+ (512 >> div
), out
, 64 >> div
);
1247 dsp
->vector_fmul_add(out
, v
+ (1216 >> div
), sbr_qmf_window
+ (576 >> div
), out
, 64 >> div
);
1253 /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
1254 * (14496-3 sp04 p214)
1255 * Warning: This routine does not seem numerically stable.
1257 static void sbr_hf_inverse_filter(SBRDSPContext
*dsp
,
1258 float (*alpha0
)[2], float (*alpha1
)[2],
1259 const float X_low
[32][40][2], int k0
)
1262 for (k
= 0; k
< k0
; k
++) {
1263 LOCAL_ALIGNED_16(float, phi
, [3], [2][2]);
1266 dsp
->autocorrelate(X_low
[k
], phi
);
1268 dk
= phi
[2][1][0] * phi
[1][0][0] -
1269 (phi
[1][1][0] * phi
[1][1][0] + phi
[1][1][1] * phi
[1][1][1]) / 1.000001f
;
1275 float temp_real
, temp_im
;
1276 temp_real
= phi
[0][0][0] * phi
[1][1][0] -
1277 phi
[0][0][1] * phi
[1][1][1] -
1278 phi
[0][1][0] * phi
[1][0][0];
1279 temp_im
= phi
[0][0][0] * phi
[1][1][1] +
1280 phi
[0][0][1] * phi
[1][1][0] -
1281 phi
[0][1][1] * phi
[1][0][0];
1283 alpha1
[k
][0] = temp_real
/ dk
;
1284 alpha1
[k
][1] = temp_im
/ dk
;
1287 if (!phi
[1][0][0]) {
1291 float temp_real
, temp_im
;
1292 temp_real
= phi
[0][0][0] + alpha1
[k
][0] * phi
[1][1][0] +
1293 alpha1
[k
][1] * phi
[1][1][1];
1294 temp_im
= phi
[0][0][1] + alpha1
[k
][1] * phi
[1][1][0] -
1295 alpha1
[k
][0] * phi
[1][1][1];
1297 alpha0
[k
][0] = -temp_real
/ phi
[1][0][0];
1298 alpha0
[k
][1] = -temp_im
/ phi
[1][0][0];
1301 if (alpha1
[k
][0] * alpha1
[k
][0] + alpha1
[k
][1] * alpha1
[k
][1] >= 16.0f
||
1302 alpha0
[k
][0] * alpha0
[k
][0] + alpha0
[k
][1] * alpha0
[k
][1] >= 16.0f
) {
1311 /// Chirp Factors (14496-3 sp04 p214)
1312 static void sbr_chirp(SpectralBandReplication
*sbr
, SBRData
*ch_data
)
1316 static const float bw_tab
[] = { 0.0f
, 0.75f
, 0.9f
, 0.98f
};
1318 for (i
= 0; i
< sbr
->n_q
; i
++) {
1319 if (ch_data
->bs_invf_mode
[0][i
] + ch_data
->bs_invf_mode
[1][i
] == 1) {
1322 new_bw
= bw_tab
[ch_data
->bs_invf_mode
[0][i
]];
1324 if (new_bw
< ch_data
->bw_array
[i
]) {
1325 new_bw
= 0.75f
* new_bw
+ 0.25f
* ch_data
->bw_array
[i
];
1327 new_bw
= 0.90625f
* new_bw
+ 0.09375f
* ch_data
->bw_array
[i
];
1328 ch_data
->bw_array
[i
] = new_bw
< 0.015625f
? 0.0f
: new_bw
;
1332 /// Generate the subband filtered lowband
1333 static int sbr_lf_gen(AACContext
*ac
, SpectralBandReplication
*sbr
,
1334 float X_low
[32][40][2], const float W
[2][32][32][2],
1338 const int t_HFGen
= 8;
1340 memset(X_low
, 0, 32*sizeof(*X_low
));
1341 for (k
= 0; k
< sbr
->kx
[1]; k
++) {
1342 for (i
= t_HFGen
; i
< i_f
+ t_HFGen
; i
++) {
1343 X_low
[k
][i
][0] = W
[buf_idx
][i
- t_HFGen
][k
][0];
1344 X_low
[k
][i
][1] = W
[buf_idx
][i
- t_HFGen
][k
][1];
1347 buf_idx
= 1-buf_idx
;
1348 for (k
= 0; k
< sbr
->kx
[0]; k
++) {
1349 for (i
= 0; i
< t_HFGen
; i
++) {
1350 X_low
[k
][i
][0] = W
[buf_idx
][i
+ i_f
- t_HFGen
][k
][0];
1351 X_low
[k
][i
][1] = W
[buf_idx
][i
+ i_f
- t_HFGen
][k
][1];
1357 /// High Frequency Generator (14496-3 sp04 p215)
1358 static int sbr_hf_gen(AACContext
*ac
, SpectralBandReplication
*sbr
,
1359 float X_high
[64][40][2], const float X_low
[32][40][2],
1360 const float (*alpha0
)[2], const float (*alpha1
)[2],
1361 const float bw_array
[5], const uint8_t *t_env
,
1367 for (j
= 0; j
< sbr
->num_patches
; j
++) {
1368 for (x
= 0; x
< sbr
->patch_num_subbands
[j
]; x
++, k
++) {
1369 const int p
= sbr
->patch_start_subband
[j
] + x
;
1370 while (g
<= sbr
->n_q
&& k
>= sbr
->f_tablenoise
[g
])
1375 av_log(ac
->avctx
, AV_LOG_ERROR
,
1376 "ERROR : no subband found for frequency %d\n", k
);
1380 sbr
->dsp
.hf_gen(X_high
[k
] + ENVELOPE_ADJUSTMENT_OFFSET
,
1381 X_low
[p
] + ENVELOPE_ADJUSTMENT_OFFSET
,
1382 alpha0
[p
], alpha1
[p
], bw_array
[g
],
1383 2 * t_env
[0], 2 * t_env
[bs_num_env
]);
1386 if (k
< sbr
->m
[1] + sbr
->kx
[1])
1387 memset(X_high
+ k
, 0, (sbr
->m
[1] + sbr
->kx
[1] - k
) * sizeof(*X_high
));
1392 /// Generate the subband filtered lowband
1393 static int sbr_x_gen(SpectralBandReplication
*sbr
, float X
[2][38][64],
1394 const float Y0
[38][64][2], const float Y1
[38][64][2],
1395 const float X_low
[32][40][2], int ch
)
1399 const int i_Temp
= FFMAX(2*sbr
->data
[ch
].t_env_num_env_old
- i_f
, 0);
1400 memset(X
, 0, 2*sizeof(*X
));
1401 for (k
= 0; k
< sbr
->kx
[0]; k
++) {
1402 for (i
= 0; i
< i_Temp
; i
++) {
1403 X
[0][i
][k
] = X_low
[k
][i
+ ENVELOPE_ADJUSTMENT_OFFSET
][0];
1404 X
[1][i
][k
] = X_low
[k
][i
+ ENVELOPE_ADJUSTMENT_OFFSET
][1];
1407 for (; k
< sbr
->kx
[0] + sbr
->m
[0]; k
++) {
1408 for (i
= 0; i
< i_Temp
; i
++) {
1409 X
[0][i
][k
] = Y0
[i
+ i_f
][k
][0];
1410 X
[1][i
][k
] = Y0
[i
+ i_f
][k
][1];
1414 for (k
= 0; k
< sbr
->kx
[1]; k
++) {
1415 for (i
= i_Temp
; i
< 38; i
++) {
1416 X
[0][i
][k
] = X_low
[k
][i
+ ENVELOPE_ADJUSTMENT_OFFSET
][0];
1417 X
[1][i
][k
] = X_low
[k
][i
+ ENVELOPE_ADJUSTMENT_OFFSET
][1];
1420 for (; k
< sbr
->kx
[1] + sbr
->m
[1]; k
++) {
1421 for (i
= i_Temp
; i
< i_f
; i
++) {
1422 X
[0][i
][k
] = Y1
[i
][k
][0];
1423 X
[1][i
][k
] = Y1
[i
][k
][1];
1429 /** High Frequency Adjustment (14496-3 sp04 p217) and Mapping
1430 * (14496-3 sp04 p217)
1432 static int sbr_mapping(AACContext
*ac
, SpectralBandReplication
*sbr
,
1433 SBRData
*ch_data
, int e_a
[2])
1437 memset(ch_data
->s_indexmapped
[1], 0, 7*sizeof(ch_data
->s_indexmapped
[1]));
1438 for (e
= 0; e
< ch_data
->bs_num_env
; e
++) {
1439 const unsigned int ilim
= sbr
->n
[ch_data
->bs_freq_res
[e
+ 1]];
1440 uint16_t *table
= ch_data
->bs_freq_res
[e
+ 1] ? sbr
->f_tablehigh
: sbr
->f_tablelow
;
1443 if (sbr
->kx
[1] != table
[0]) {
1444 av_log(ac
->avctx
, AV_LOG_ERROR
, "kx != f_table{high,low}[0]. "
1445 "Derived frequency tables were not regenerated.\n");
1449 for (i
= 0; i
< ilim
; i
++)
1450 for (m
= table
[i
]; m
< table
[i
+ 1]; m
++)
1451 sbr
->e_origmapped
[e
][m
- sbr
->kx
[1]] = ch_data
->env_facs
[e
+1][i
];
1453 // ch_data->bs_num_noise > 1 => 2 noise floors
1454 k
= (ch_data
->bs_num_noise
> 1) && (ch_data
->t_env
[e
] >= ch_data
->t_q
[1]);
1455 for (i
= 0; i
< sbr
->n_q
; i
++)
1456 for (m
= sbr
->f_tablenoise
[i
]; m
< sbr
->f_tablenoise
[i
+ 1]; m
++)
1457 sbr
->q_mapped
[e
][m
- sbr
->kx
[1]] = ch_data
->noise_facs
[k
+1][i
];
1459 for (i
= 0; i
< sbr
->n
[1]; i
++) {
1460 if (ch_data
->bs_add_harmonic_flag
) {
1461 const unsigned int m_midpoint
=
1462 (sbr
->f_tablehigh
[i
] + sbr
->f_tablehigh
[i
+ 1]) >> 1;
1464 ch_data
->s_indexmapped
[e
+ 1][m_midpoint
- sbr
->kx
[1]] = ch_data
->bs_add_harmonic
[i
] *
1465 (e
>= e_a
[1] || (ch_data
->s_indexmapped
[0][m_midpoint
- sbr
->kx
[1]] == 1));
1469 for (i
= 0; i
< ilim
; i
++) {
1470 int additional_sinusoid_present
= 0;
1471 for (m
= table
[i
]; m
< table
[i
+ 1]; m
++) {
1472 if (ch_data
->s_indexmapped
[e
+ 1][m
- sbr
->kx
[1]]) {
1473 additional_sinusoid_present
= 1;
1477 memset(&sbr
->s_mapped
[e
][table
[i
] - sbr
->kx
[1]], additional_sinusoid_present
,
1478 (table
[i
+ 1] - table
[i
]) * sizeof(sbr
->s_mapped
[e
][0]));
1482 memcpy(ch_data
->s_indexmapped
[0], ch_data
->s_indexmapped
[ch_data
->bs_num_env
], sizeof(ch_data
->s_indexmapped
[0]));
1486 /// Estimation of current envelope (14496-3 sp04 p218)
1487 static void sbr_env_estimate(float (*e_curr
)[48], float X_high
[64][40][2],
1488 SpectralBandReplication
*sbr
, SBRData
*ch_data
)
1491 int kx1
= sbr
->kx
[1];
1493 if (sbr
->bs_interpol_freq
) {
1494 for (e
= 0; e
< ch_data
->bs_num_env
; e
++) {
1495 const float recip_env_size
= 0.5f
/ (ch_data
->t_env
[e
+ 1] - ch_data
->t_env
[e
]);
1496 int ilb
= ch_data
->t_env
[e
] * 2 + ENVELOPE_ADJUSTMENT_OFFSET
;
1497 int iub
= ch_data
->t_env
[e
+ 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET
;
1499 for (m
= 0; m
< sbr
->m
[1]; m
++) {
1500 float sum
= sbr
->dsp
.sum_square(X_high
[m
+kx1
] + ilb
, iub
- ilb
);
1501 e_curr
[e
][m
] = sum
* recip_env_size
;
1507 for (e
= 0; e
< ch_data
->bs_num_env
; e
++) {
1508 const int env_size
= 2 * (ch_data
->t_env
[e
+ 1] - ch_data
->t_env
[e
]);
1509 int ilb
= ch_data
->t_env
[e
] * 2 + ENVELOPE_ADJUSTMENT_OFFSET
;
1510 int iub
= ch_data
->t_env
[e
+ 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET
;
1511 const uint16_t *table
= ch_data
->bs_freq_res
[e
+ 1] ? sbr
->f_tablehigh
: sbr
->f_tablelow
;
1513 for (p
= 0; p
< sbr
->n
[ch_data
->bs_freq_res
[e
+ 1]]; p
++) {
1515 const int den
= env_size
* (table
[p
+ 1] - table
[p
]);
1517 for (k
= table
[p
]; k
< table
[p
+ 1]; k
++) {
1518 sum
+= sbr
->dsp
.sum_square(X_high
[k
] + ilb
, iub
- ilb
);
1521 for (k
= table
[p
]; k
< table
[p
+ 1]; k
++) {
1522 e_curr
[e
][k
- kx1
] = sum
;
1530 * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
1531 * and Calculation of gain (14496-3 sp04 p219)
1533 static void sbr_gain_calc(AACContext
*ac
, SpectralBandReplication
*sbr
,
1534 SBRData
*ch_data
, const int e_a
[2])
1537 // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
1538 static const float limgain
[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
1540 for (e
= 0; e
< ch_data
->bs_num_env
; e
++) {
1541 int delta
= !((e
== e_a
[1]) || (e
== e_a
[0]));
1542 for (k
= 0; k
< sbr
->n_lim
; k
++) {
1543 float gain_boost
, gain_max
;
1544 float sum
[2] = { 0.0f
, 0.0f
};
1545 for (m
= sbr
->f_tablelim
[k
] - sbr
->kx
[1]; m
< sbr
->f_tablelim
[k
+ 1] - sbr
->kx
[1]; m
++) {
1546 const float temp
= sbr
->e_origmapped
[e
][m
] / (1.0f
+ sbr
->q_mapped
[e
][m
]);
1547 sbr
->q_m
[e
][m
] = sqrtf(temp
* sbr
->q_mapped
[e
][m
]);
1548 sbr
->s_m
[e
][m
] = sqrtf(temp
* ch_data
->s_indexmapped
[e
+ 1][m
]);
1549 if (!sbr
->s_mapped
[e
][m
]) {
1550 sbr
->gain
[e
][m
] = sqrtf(sbr
->e_origmapped
[e
][m
] /
1551 ((1.0f
+ sbr
->e_curr
[e
][m
]) *
1552 (1.0f
+ sbr
->q_mapped
[e
][m
] * delta
)));
1554 sbr
->gain
[e
][m
] = sqrtf(sbr
->e_origmapped
[e
][m
] * sbr
->q_mapped
[e
][m
] /
1555 ((1.0f
+ sbr
->e_curr
[e
][m
]) *
1556 (1.0f
+ sbr
->q_mapped
[e
][m
])));
1559 for (m
= sbr
->f_tablelim
[k
] - sbr
->kx
[1]; m
< sbr
->f_tablelim
[k
+ 1] - sbr
->kx
[1]; m
++) {
1560 sum
[0] += sbr
->e_origmapped
[e
][m
];
1561 sum
[1] += sbr
->e_curr
[e
][m
];
1563 gain_max
= limgain
[sbr
->bs_limiter_gains
] * sqrtf((FLT_EPSILON
+ sum
[0]) / (FLT_EPSILON
+ sum
[1]));
1564 gain_max
= FFMIN(100000.f
, gain_max
);
1565 for (m
= sbr
->f_tablelim
[k
] - sbr
->kx
[1]; m
< sbr
->f_tablelim
[k
+ 1] - sbr
->kx
[1]; m
++) {
1566 float q_m_max
= sbr
->q_m
[e
][m
] * gain_max
/ sbr
->gain
[e
][m
];
1567 sbr
->q_m
[e
][m
] = FFMIN(sbr
->q_m
[e
][m
], q_m_max
);
1568 sbr
->gain
[e
][m
] = FFMIN(sbr
->gain
[e
][m
], gain_max
);
1570 sum
[0] = sum
[1] = 0.0f
;
1571 for (m
= sbr
->f_tablelim
[k
] - sbr
->kx
[1]; m
< sbr
->f_tablelim
[k
+ 1] - sbr
->kx
[1]; m
++) {
1572 sum
[0] += sbr
->e_origmapped
[e
][m
];
1573 sum
[1] += sbr
->e_curr
[e
][m
] * sbr
->gain
[e
][m
] * sbr
->gain
[e
][m
]
1574 + sbr
->s_m
[e
][m
] * sbr
->s_m
[e
][m
]
1575 + (delta
&& !sbr
->s_m
[e
][m
]) * sbr
->q_m
[e
][m
] * sbr
->q_m
[e
][m
];
1577 gain_boost
= sqrtf((FLT_EPSILON
+ sum
[0]) / (FLT_EPSILON
+ sum
[1]));
1578 gain_boost
= FFMIN(1.584893192f
, gain_boost
);
1579 for (m
= sbr
->f_tablelim
[k
] - sbr
->kx
[1]; m
< sbr
->f_tablelim
[k
+ 1] - sbr
->kx
[1]; m
++) {
1580 sbr
->gain
[e
][m
] *= gain_boost
;
1581 sbr
->q_m
[e
][m
] *= gain_boost
;
1582 sbr
->s_m
[e
][m
] *= gain_boost
;
1588 /// Assembling HF Signals (14496-3 sp04 p220)
1589 static void sbr_hf_assemble(float Y1
[38][64][2],
1590 const float X_high
[64][40][2],
1591 SpectralBandReplication
*sbr
, SBRData
*ch_data
,
1595 const int h_SL
= 4 * !sbr
->bs_smoothing_mode
;
1596 const int kx
= sbr
->kx
[1];
1597 const int m_max
= sbr
->m
[1];
1598 static const float h_smooth
[5] = {
1605 float (*g_temp
)[48] = ch_data
->g_temp
, (*q_temp
)[48] = ch_data
->q_temp
;
1606 int indexnoise
= ch_data
->f_indexnoise
;
1607 int indexsine
= ch_data
->f_indexsine
;
1610 for (i
= 0; i
< h_SL
; i
++) {
1611 memcpy(g_temp
[i
+ 2*ch_data
->t_env
[0]], sbr
->gain
[0], m_max
* sizeof(sbr
->gain
[0][0]));
1612 memcpy(q_temp
[i
+ 2*ch_data
->t_env
[0]], sbr
->q_m
[0], m_max
* sizeof(sbr
->q_m
[0][0]));
1615 for (i
= 0; i
< 4; i
++) {
1616 memcpy(g_temp
[i
+ 2 * ch_data
->t_env
[0]],
1617 g_temp
[i
+ 2 * ch_data
->t_env_num_env_old
],
1619 memcpy(q_temp
[i
+ 2 * ch_data
->t_env
[0]],
1620 q_temp
[i
+ 2 * ch_data
->t_env_num_env_old
],
1625 for (e
= 0; e
< ch_data
->bs_num_env
; e
++) {
1626 for (i
= 2 * ch_data
->t_env
[e
]; i
< 2 * ch_data
->t_env
[e
+ 1]; i
++) {
1627 memcpy(g_temp
[h_SL
+ i
], sbr
->gain
[e
], m_max
* sizeof(sbr
->gain
[0][0]));
1628 memcpy(q_temp
[h_SL
+ i
], sbr
->q_m
[e
], m_max
* sizeof(sbr
->q_m
[0][0]));
1632 for (e
= 0; e
< ch_data
->bs_num_env
; e
++) {
1633 for (i
= 2 * ch_data
->t_env
[e
]; i
< 2 * ch_data
->t_env
[e
+ 1]; i
++) {
1634 LOCAL_ALIGNED_16(float, g_filt_tab
, [48]);
1635 LOCAL_ALIGNED_16(float, q_filt_tab
, [48]);
1636 float *g_filt
, *q_filt
;
1638 if (h_SL
&& e
!= e_a
[0] && e
!= e_a
[1]) {
1639 g_filt
= g_filt_tab
;
1640 q_filt
= q_filt_tab
;
1641 for (m
= 0; m
< m_max
; m
++) {
1642 const int idx1
= i
+ h_SL
;
1645 for (j
= 0; j
<= h_SL
; j
++) {
1646 g_filt
[m
] += g_temp
[idx1
- j
][m
] * h_smooth
[j
];
1647 q_filt
[m
] += q_temp
[idx1
- j
][m
] * h_smooth
[j
];
1651 g_filt
= g_temp
[i
+ h_SL
];
1655 sbr
->dsp
.hf_g_filt(Y1
[i
] + kx
, X_high
+ kx
, g_filt
, m_max
,
1656 i
+ ENVELOPE_ADJUSTMENT_OFFSET
);
1658 if (e
!= e_a
[0] && e
!= e_a
[1]) {
1659 sbr
->dsp
.hf_apply_noise
[indexsine
](Y1
[i
] + kx
, sbr
->s_m
[e
],
1663 int idx
= indexsine
&1;
1664 int A
= (1-((indexsine
+(kx
& 1))&2));
1665 int B
= (A
^(-idx
)) + idx
;
1666 float *out
= &Y1
[i
][kx
][idx
];
1667 float *in
= sbr
->s_m
[e
];
1668 for (m
= 0; m
+1 < m_max
; m
+=2) {
1669 out
[2*m
] += in
[m
] * A
;
1670 out
[2*m
+2] += in
[m
+1] * B
;
1673 out
[2*m
] += in
[m
] * A
;
1675 indexnoise
= (indexnoise
+ m_max
) & 0x1ff;
1676 indexsine
= (indexsine
+ 1) & 3;
1679 ch_data
->f_indexnoise
= indexnoise
;
1680 ch_data
->f_indexsine
= indexsine
;
1683 void ff_sbr_apply(AACContext
*ac
, SpectralBandReplication
*sbr
, int id_aac
,
1686 int downsampled
= ac
->oc
[1].m4ac
.ext_sample_rate
< sbr
->sample_rate
;
1688 int nch
= (id_aac
== TYPE_CPE
) ? 2 : 1;
1691 if (!sbr
->kx_and_m_pushed
) {
1692 sbr
->kx
[0] = sbr
->kx
[1];
1693 sbr
->m
[0] = sbr
->m
[1];
1695 sbr
->kx_and_m_pushed
= 0;
1699 sbr_dequant(sbr
, id_aac
);
1701 for (ch
= 0; ch
< nch
; ch
++) {
1702 /* decode channel */
1703 sbr_qmf_analysis(ac
->fdsp
, &sbr
->mdct_ana
, &sbr
->dsp
, ch
? R
: L
, sbr
->data
[ch
].analysis_filterbank_samples
,
1704 (float*)sbr
->qmf_filter_scratch
,
1705 sbr
->data
[ch
].W
, sbr
->data
[ch
].Ypos
);
1706 sbr
->c
.sbr_lf_gen(ac
, sbr
, sbr
->X_low
,
1707 (const float (*)[32][32][2]) sbr
->data
[ch
].W
,
1708 sbr
->data
[ch
].Ypos
);
1709 sbr
->data
[ch
].Ypos
^= 1;
1711 sbr
->c
.sbr_hf_inverse_filter(&sbr
->dsp
, sbr
->alpha0
, sbr
->alpha1
,
1712 (const float (*)[40][2]) sbr
->X_low
, sbr
->k
[0]);
1713 sbr_chirp(sbr
, &sbr
->data
[ch
]);
1714 sbr_hf_gen(ac
, sbr
, sbr
->X_high
,
1715 (const float (*)[40][2]) sbr
->X_low
,
1716 (const float (*)[2]) sbr
->alpha0
,
1717 (const float (*)[2]) sbr
->alpha1
,
1718 sbr
->data
[ch
].bw_array
, sbr
->data
[ch
].t_env
,
1719 sbr
->data
[ch
].bs_num_env
);
1722 err
= sbr_mapping(ac
, sbr
, &sbr
->data
[ch
], sbr
->data
[ch
].e_a
);
1724 sbr_env_estimate(sbr
->e_curr
, sbr
->X_high
, sbr
, &sbr
->data
[ch
]);
1725 sbr_gain_calc(ac
, sbr
, &sbr
->data
[ch
], sbr
->data
[ch
].e_a
);
1726 sbr
->c
.sbr_hf_assemble(sbr
->data
[ch
].Y
[sbr
->data
[ch
].Ypos
],
1727 (const float (*)[40][2]) sbr
->X_high
,
1728 sbr
, &sbr
->data
[ch
],
1734 sbr
->c
.sbr_x_gen(sbr
, sbr
->X
[ch
],
1735 (const float (*)[64][2]) sbr
->data
[ch
].Y
[1-sbr
->data
[ch
].Ypos
],
1736 (const float (*)[64][2]) sbr
->data
[ch
].Y
[ sbr
->data
[ch
].Ypos
],
1737 (const float (*)[40][2]) sbr
->X_low
, ch
);
1740 if (ac
->oc
[1].m4ac
.ps
== 1) {
1741 if (sbr
->ps
.start
) {
1742 ff_ps_apply(ac
->avctx
, &sbr
->ps
, sbr
->X
[0], sbr
->X
[1], sbr
->kx
[1] + sbr
->m
[1]);
1744 memcpy(sbr
->X
[1], sbr
->X
[0], sizeof(sbr
->X
[0]));
1749 sbr_qmf_synthesis(&sbr
->mdct
, &sbr
->dsp
, ac
->fdsp
,
1750 L
, sbr
->X
[0], sbr
->qmf_filter_scratch
,
1751 sbr
->data
[0].synthesis_filterbank_samples
,
1752 &sbr
->data
[0].synthesis_filterbank_samples_offset
,
1755 sbr_qmf_synthesis(&sbr
->mdct
, &sbr
->dsp
, ac
->fdsp
,
1756 R
, sbr
->X
[1], sbr
->qmf_filter_scratch
,
1757 sbr
->data
[1].synthesis_filterbank_samples
,
1758 &sbr
->data
[1].synthesis_filterbank_samples_offset
,
1762 static void aacsbr_func_ptr_init(AACSBRContext
*c
)
1764 c
->sbr_lf_gen
= sbr_lf_gen
;
1765 c
->sbr_hf_assemble
= sbr_hf_assemble
;
1766 c
->sbr_x_gen
= sbr_x_gen
;
1767 c
->sbr_hf_inverse_filter
= sbr_hf_inverse_filter
;
1770 ff_aacsbr_func_ptr_init_mips(c
);