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