10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
12527
|
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
10725
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
12527
|
25 ** $Id: sbr_syntax.c,v 1.2 2003/10/03 22:22:27 alex Exp $
|
10725
|
26 **/
|
|
27
|
|
28 #include "common.h"
|
|
29 #include "structs.h"
|
|
30
|
|
31 #ifdef SBR_DEC
|
|
32
|
|
33 #include "sbr_syntax.h"
|
|
34 #include "syntax.h"
|
|
35 #include "sbr_huff.h"
|
|
36 #include "sbr_fbt.h"
|
|
37 #include "sbr_tf_grid.h"
|
|
38 #include "sbr_e_nf.h"
|
|
39 #include "bits.h"
|
12527
|
40 #ifdef PS_DEC
|
|
41 #include "ps_dec.h"
|
|
42 #endif
|
|
43 #ifdef DRM_PS
|
|
44 #include "drm_dec.h"
|
|
45 #endif
|
10725
|
46 #include "analysis.h"
|
|
47
|
12527
|
48 /* static function declarations */
|
|
49 static void sbr_header(bitfile *ld, sbr_info *sbr);
|
|
50 static uint8_t sbr_data(bitfile *ld, sbr_info *sbr);
|
|
51 static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr,
|
|
52 uint8_t bs_extension_id, uint16_t num_bits_left);
|
|
53 static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr);
|
|
54 static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr);
|
|
55 static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch);
|
|
56 static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch);
|
|
57 static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch);
|
|
58 static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch);
|
|
59
|
|
60
|
10725
|
61 static void sbr_reset(sbr_info *sbr)
|
|
62 {
|
12527
|
63 #if 0
|
|
64 printf("%d\n", sbr->bs_start_freq_prev);
|
|
65 printf("%d\n", sbr->bs_stop_freq_prev);
|
|
66 printf("%d\n", sbr->bs_freq_scale_prev);
|
|
67 printf("%d\n", sbr->bs_alter_scale_prev);
|
|
68 printf("%d\n", sbr->bs_xover_band_prev);
|
|
69 printf("%d\n\n", sbr->bs_noise_bands_prev);
|
|
70 #endif
|
|
71
|
10725
|
72 /* if these are different from the previous frame: Reset = 1 */
|
|
73 if ((sbr->bs_start_freq != sbr->bs_start_freq_prev) ||
|
|
74 (sbr->bs_stop_freq != sbr->bs_stop_freq_prev) ||
|
|
75 (sbr->bs_freq_scale != sbr->bs_freq_scale_prev) ||
|
|
76 (sbr->bs_alter_scale != sbr->bs_alter_scale_prev))
|
|
77 {
|
|
78 sbr->Reset = 1;
|
|
79 } else {
|
|
80 sbr->Reset = 0;
|
|
81 }
|
|
82
|
|
83 if ((sbr->bs_start_freq != sbr->bs_start_freq_prev) ||
|
|
84 (sbr->bs_stop_freq != sbr->bs_stop_freq_prev) ||
|
|
85 (sbr->bs_freq_scale != sbr->bs_freq_scale_prev) ||
|
|
86 (sbr->bs_alter_scale != sbr->bs_alter_scale_prev) ||
|
|
87 (sbr->bs_xover_band != sbr->bs_xover_band_prev) ||
|
|
88 (sbr->bs_noise_bands != sbr->bs_noise_bands_prev))
|
|
89 {
|
|
90 sbr->Reset = 1;
|
|
91 } else {
|
|
92 sbr->Reset = 0;
|
|
93 }
|
|
94
|
|
95 sbr->bs_start_freq_prev = sbr->bs_start_freq;
|
|
96 sbr->bs_stop_freq_prev = sbr->bs_stop_freq;
|
|
97 sbr->bs_freq_scale_prev = sbr->bs_freq_scale;
|
|
98 sbr->bs_alter_scale_prev = sbr->bs_alter_scale;
|
|
99 sbr->bs_xover_band_prev = sbr->bs_xover_band;
|
|
100 sbr->bs_noise_bands_prev = sbr->bs_noise_bands;
|
|
101
|
|
102 if (sbr->frame == 0)
|
|
103 {
|
|
104 sbr->Reset = 1;
|
|
105 }
|
|
106 }
|
|
107
|
|
108 /* table 2 */
|
12527
|
109 uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt)
|
10725
|
110 {
|
12527
|
111 uint8_t result = 0;
|
|
112 uint16_t num_align_bits = 0;
|
|
113 uint16_t num_sbr_bits = (uint16_t)faad_get_processed_bits(ld);
|
|
114
|
10989
|
115 #ifdef DRM
|
12527
|
116 if (!sbr->Is_DRM_SBR)
|
10989
|
117 #endif
|
|
118 {
|
12527
|
119 uint8_t bs_extension_type = (uint8_t)faad_getbits(ld, 4
|
|
120 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type"));
|
10725
|
121
|
12527
|
122 if (bs_extension_type == EXT_SBR_DATA_CRC)
|
|
123 {
|
|
124 sbr->bs_sbr_crc_bits = (uint16_t)faad_getbits(ld, 10
|
|
125 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits"));
|
|
126 }
|
10989
|
127 }
|
10725
|
128
|
|
129 sbr->bs_header_flag = faad_get1bit(ld
|
|
130 DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag"));
|
10989
|
131
|
12527
|
132 if (sbr->bs_header_flag)
|
|
133 sbr_header(ld, sbr);
|
10989
|
134
|
12527
|
135 /* Reset? */
|
10725
|
136 sbr_reset(sbr);
|
|
137
|
|
138 /* first frame should have a header */
|
12527
|
139 //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0))
|
|
140 if (sbr->header_count != 0)
|
|
141 {
|
|
142 if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))
|
|
143 {
|
|
144 uint8_t k2;
|
10725
|
145
|
12527
|
146 /* calculate the Master Frequency Table */
|
|
147 sbr->k0 = qmf_start_channel(sbr->bs_start_freq, sbr->bs_samplerate_mode,
|
|
148 sbr->sample_rate);
|
|
149 k2 = qmf_stop_channel(sbr->bs_stop_freq, sbr->sample_rate, sbr->k0);
|
10725
|
150
|
12527
|
151 /* check k0 and k2 */
|
|
152 if (sbr->sample_rate >= 48000)
|
|
153 {
|
|
154 if ((k2 - sbr->k0) > 32)
|
|
155 result += 1;
|
|
156 } else if (sbr->sample_rate <= 32000) {
|
|
157 if ((k2 - sbr->k0) > 48)
|
|
158 result += 1;
|
|
159 } else { /* (sbr->sample_rate == 44100) */
|
|
160 if ((k2 - sbr->k0) > 45)
|
|
161 result += 1;
|
|
162 }
|
10725
|
163
|
12527
|
164 if (sbr->bs_freq_scale == 0)
|
|
165 {
|
|
166 result += master_frequency_table_fs0(sbr, sbr->k0, k2,
|
|
167 sbr->bs_alter_scale);
|
|
168 } else {
|
|
169 result += master_frequency_table(sbr, sbr->k0, k2, sbr->bs_freq_scale,
|
|
170 sbr->bs_alter_scale);
|
|
171 }
|
|
172 result += derived_frequency_table(sbr, sbr->bs_xover_band, k2);
|
|
173
|
|
174 result = (result > 0) ? 1 : 0;
|
10725
|
175 }
|
|
176
|
12527
|
177 if (result == 0)
|
|
178 result = sbr_data(ld, sbr);
|
|
179 } else {
|
|
180 result = 1;
|
10725
|
181 }
|
|
182
|
12527
|
183 #ifdef DRM
|
|
184 if (!sbr->Is_DRM_SBR)
|
|
185 #endif
|
|
186 {
|
|
187 num_sbr_bits = (uint16_t)faad_get_processed_bits(ld) - num_sbr_bits;
|
|
188 /* -4 does not apply, bs_extension_type is re-read in this function */
|
|
189 num_align_bits = 8*cnt /*- 4*/ - num_sbr_bits;
|
10725
|
190
|
12527
|
191 while (num_align_bits > 7)
|
|
192 {
|
|
193 faad_getbits(ld, 8
|
|
194 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
|
|
195 num_align_bits -= 8;
|
|
196 }
|
|
197 faad_getbits(ld, num_align_bits
|
|
198 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
|
|
199 }
|
|
200
|
|
201 return result;
|
10725
|
202 }
|
|
203
|
|
204 /* table 3 */
|
12527
|
205 static void sbr_header(bitfile *ld, sbr_info *sbr)
|
10725
|
206 {
|
|
207 uint8_t bs_header_extra_1, bs_header_extra_2;
|
|
208
|
|
209 sbr->header_count++;
|
|
210
|
|
211 sbr->bs_amp_res = faad_get1bit(ld
|
|
212 DEBUGVAR(1,203,"sbr_header(): bs_amp_res"));
|
|
213
|
|
214 /* bs_start_freq and bs_stop_freq must define a fequency band that does
|
|
215 not exceed 48 channels */
|
12527
|
216 sbr->bs_start_freq = (uint8_t)faad_getbits(ld, 4
|
10725
|
217 DEBUGVAR(1,204,"sbr_header(): bs_start_freq"));
|
12527
|
218 sbr->bs_stop_freq = (uint8_t)faad_getbits(ld, 4
|
10725
|
219 DEBUGVAR(1,205,"sbr_header(): bs_stop_freq"));
|
12527
|
220 sbr->bs_xover_band = (uint8_t)faad_getbits(ld, 3
|
10725
|
221 DEBUGVAR(1,206,"sbr_header(): bs_xover_band"));
|
|
222 faad_getbits(ld, 2
|
|
223 DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr"));
|
12527
|
224 bs_header_extra_1 = (uint8_t)faad_get1bit(ld
|
10725
|
225 DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1"));
|
12527
|
226 bs_header_extra_2 = (uint8_t)faad_get1bit(ld
|
10725
|
227 DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2"));
|
|
228
|
|
229 if (bs_header_extra_1)
|
|
230 {
|
12527
|
231 sbr->bs_freq_scale = (uint8_t)faad_getbits(ld, 2
|
10725
|
232 DEBUGVAR(1,211,"sbr_header(): bs_freq_scale"));
|
12527
|
233 sbr->bs_alter_scale = (uint8_t)faad_get1bit(ld
|
10725
|
234 DEBUGVAR(1,212,"sbr_header(): bs_alter_scale"));
|
12527
|
235 sbr->bs_noise_bands = (uint8_t)faad_getbits(ld, 2
|
10725
|
236 DEBUGVAR(1,213,"sbr_header(): bs_noise_bands"));
|
10989
|
237 } else {
|
|
238 /* Default values */
|
|
239 sbr->bs_freq_scale = 2;
|
|
240 sbr->bs_alter_scale = 1;
|
|
241 sbr->bs_noise_bands = 2;
|
10725
|
242 }
|
10989
|
243
|
10725
|
244 if (bs_header_extra_2)
|
|
245 {
|
12527
|
246 sbr->bs_limiter_bands = (uint8_t)faad_getbits(ld, 2
|
10725
|
247 DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands"));
|
12527
|
248 sbr->bs_limiter_gains = (uint8_t)faad_getbits(ld, 2
|
10725
|
249 DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains"));
|
12527
|
250 sbr->bs_interpol_freq = (uint8_t)faad_get1bit(ld
|
10725
|
251 DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq"));
|
12527
|
252 sbr->bs_smoothing_mode = (uint8_t)faad_get1bit(ld
|
10725
|
253 DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode"));
|
10989
|
254 } else {
|
|
255 /* Default values */
|
|
256 sbr->bs_limiter_bands = 2;
|
|
257 sbr->bs_limiter_gains = 2;
|
|
258 sbr->bs_interpol_freq = 1;
|
|
259 sbr->bs_smoothing_mode = 1;
|
10725
|
260 }
|
|
261
|
|
262 #if 0
|
|
263 /* print the header to screen */
|
|
264 printf("bs_amp_res: %d\n", sbr->bs_amp_res);
|
|
265 printf("bs_start_freq: %d\n", sbr->bs_start_freq);
|
|
266 printf("bs_stop_freq: %d\n", sbr->bs_stop_freq);
|
|
267 printf("bs_xover_band: %d\n", sbr->bs_xover_band);
|
|
268 if (bs_header_extra_1)
|
|
269 {
|
|
270 printf("bs_freq_scale: %d\n", sbr->bs_freq_scale);
|
|
271 printf("bs_alter_scale: %d\n", sbr->bs_alter_scale);
|
|
272 printf("bs_noise_bands: %d\n", sbr->bs_noise_bands);
|
|
273 }
|
|
274 if (bs_header_extra_2)
|
|
275 {
|
|
276 printf("bs_limiter_bands: %d\n", sbr->bs_limiter_bands);
|
|
277 printf("bs_limiter_gains: %d\n", sbr->bs_limiter_gains);
|
|
278 printf("bs_interpol_freq: %d\n", sbr->bs_interpol_freq);
|
|
279 printf("bs_smoothing_mode: %d\n", sbr->bs_smoothing_mode);
|
|
280 }
|
|
281 printf("\n");
|
|
282 #endif
|
|
283 }
|
|
284
|
|
285 /* table 4 */
|
12527
|
286 static uint8_t sbr_data(bitfile *ld, sbr_info *sbr)
|
10725
|
287 {
|
10989
|
288 uint8_t result;
|
10725
|
289 #if 0
|
|
290 sbr->bs_samplerate_mode = faad_get1bit(ld
|
|
291 DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode"));
|
|
292 #endif
|
|
293
|
|
294 sbr->rate = (sbr->bs_samplerate_mode) ? 2 : 1;
|
|
295
|
12527
|
296 switch (sbr->id_aac)
|
10725
|
297 {
|
|
298 case ID_SCE:
|
12527
|
299 if ((result = sbr_single_channel_element(ld, sbr)) > 0)
|
|
300 return result;
|
10725
|
301 break;
|
|
302 case ID_CPE:
|
12527
|
303 if ((result = sbr_channel_pair_element(ld, sbr)) > 0)
|
|
304 return result;
|
10725
|
305 break;
|
|
306 }
|
10989
|
307
|
12527
|
308 return 0;
|
10725
|
309 }
|
|
310
|
|
311 /* table 5 */
|
10989
|
312 static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr)
|
10725
|
313 {
|
10989
|
314 uint8_t result;
|
|
315
|
10725
|
316 if (faad_get1bit(ld
|
|
317 DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra")))
|
|
318 {
|
|
319 faad_getbits(ld, 4
|
|
320 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data"));
|
|
321 }
|
12527
|
322
|
|
323 #ifdef DRM
|
|
324 /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */
|
|
325 if (sbr->Is_DRM_SBR)
|
|
326 faad_get1bit(ld);
|
|
327 #endif
|
10725
|
328
|
10989
|
329 if ((result = sbr_grid(ld, sbr, 0)) > 0)
|
|
330 return result;
|
10725
|
331 sbr_dtdf(ld, sbr, 0);
|
|
332 invf_mode(ld, sbr, 0);
|
|
333 sbr_envelope(ld, sbr, 0);
|
|
334 sbr_noise(ld, sbr, 0);
|
|
335
|
|
336 envelope_noise_dequantisation(sbr, 0);
|
|
337
|
|
338 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
|
|
339
|
|
340 sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
|
|
341 DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]"));
|
|
342 if (sbr->bs_add_harmonic_flag[0])
|
|
343 sinusoidal_coding(ld, sbr, 0);
|
|
344
|
|
345 sbr->bs_extended_data = faad_get1bit(ld
|
|
346 DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]"));
|
|
347 if (sbr->bs_extended_data)
|
|
348 {
|
|
349 uint16_t nr_bits_left;
|
12527
|
350 uint16_t cnt = (uint16_t)faad_getbits(ld, 4
|
10725
|
351 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size"));
|
|
352 if (cnt == 15)
|
|
353 {
|
12527
|
354 cnt += (uint16_t)faad_getbits(ld, 8
|
10725
|
355 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count"));
|
|
356 }
|
|
357
|
|
358 nr_bits_left = 8 * cnt;
|
|
359 while (nr_bits_left > 7)
|
|
360 {
|
12527
|
361 sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2
|
10725
|
362 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id"));
|
|
363 nr_bits_left -= 2;
|
12527
|
364 nr_bits_left -= sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left);
|
10725
|
365 }
|
12527
|
366
|
|
367 /* Corrigendum */
|
|
368 if (nr_bits_left > 0)
|
|
369 {
|
|
370 faad_getbits(ld, nr_bits_left
|
|
371 DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left"));
|
|
372 }
|
10989
|
373 }
|
|
374
|
|
375 return 0;
|
10725
|
376 }
|
|
377
|
|
378 /* table 6 */
|
10989
|
379 static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr)
|
10725
|
380 {
|
10989
|
381 uint8_t n, result;
|
10725
|
382
|
|
383 if (faad_get1bit(ld
|
|
384 DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra")))
|
|
385 {
|
|
386 faad_getbits(ld, 4
|
|
387 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
|
|
388 faad_getbits(ld, 4
|
|
389 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
|
|
390 }
|
|
391
|
|
392 sbr->bs_coupling = faad_get1bit(ld
|
|
393 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling"));
|
|
394
|
|
395 if (sbr->bs_coupling)
|
|
396 {
|
10989
|
397 if ((result = sbr_grid(ld, sbr, 0)) > 0)
|
|
398 return result;
|
10725
|
399
|
|
400 /* need to copy some data from left to right */
|
|
401 sbr->bs_frame_class[1] = sbr->bs_frame_class[0];
|
|
402 sbr->L_E[1] = sbr->L_E[0];
|
|
403 sbr->L_Q[1] = sbr->L_Q[0];
|
|
404 sbr->bs_pointer[1] = sbr->bs_pointer[0];
|
|
405
|
|
406 for (n = 0; n <= sbr->L_E[0]; n++)
|
|
407 {
|
|
408 sbr->t_E[1][n] = sbr->t_E[0][n];
|
|
409 sbr->f[1][n] = sbr->f[0][n];
|
|
410 }
|
|
411 for (n = 0; n <= sbr->L_Q[0]; n++)
|
|
412 sbr->t_Q[1][n] = sbr->t_Q[0][n];
|
|
413
|
|
414 sbr_dtdf(ld, sbr, 0);
|
|
415 sbr_dtdf(ld, sbr, 1);
|
|
416 invf_mode(ld, sbr, 0);
|
|
417
|
|
418 /* more copying */
|
|
419 for (n = 0; n < sbr->N_Q; n++)
|
|
420 sbr->bs_invf_mode[1][n] = sbr->bs_invf_mode[0][n];
|
|
421
|
|
422 sbr_envelope(ld, sbr, 0);
|
|
423 sbr_noise(ld, sbr, 0);
|
|
424 sbr_envelope(ld, sbr, 1);
|
|
425 sbr_noise(ld, sbr, 1);
|
|
426
|
|
427 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
|
|
428 memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t));
|
|
429
|
|
430 sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
|
|
431 DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
|
|
432 if (sbr->bs_add_harmonic_flag[0])
|
|
433 sinusoidal_coding(ld, sbr, 0);
|
|
434
|
|
435 sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld
|
|
436 DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
|
|
437 if (sbr->bs_add_harmonic_flag[1])
|
|
438 sinusoidal_coding(ld, sbr, 1);
|
|
439 } else {
|
10989
|
440 if ((result = sbr_grid(ld, sbr, 0)) > 0)
|
|
441 return result;
|
|
442 if ((result = sbr_grid(ld, sbr, 1)) > 0)
|
|
443 return result;
|
10725
|
444 sbr_dtdf(ld, sbr, 0);
|
|
445 sbr_dtdf(ld, sbr, 1);
|
|
446 invf_mode(ld, sbr, 0);
|
|
447 invf_mode(ld, sbr, 1);
|
|
448 sbr_envelope(ld, sbr, 0);
|
|
449 sbr_envelope(ld, sbr, 1);
|
|
450 sbr_noise(ld, sbr, 0);
|
|
451 sbr_noise(ld, sbr, 1);
|
|
452
|
|
453 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
|
|
454 memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t));
|
|
455
|
|
456 sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
|
|
457 DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
|
|
458 if (sbr->bs_add_harmonic_flag[0])
|
|
459 sinusoidal_coding(ld, sbr, 0);
|
|
460
|
|
461 sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld
|
|
462 DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
|
|
463 if (sbr->bs_add_harmonic_flag[1])
|
|
464 sinusoidal_coding(ld, sbr, 1);
|
|
465 }
|
|
466 envelope_noise_dequantisation(sbr, 0);
|
|
467 envelope_noise_dequantisation(sbr, 1);
|
|
468
|
|
469 if (sbr->bs_coupling)
|
|
470 unmap_envelope_noise(sbr);
|
|
471
|
|
472 sbr->bs_extended_data = faad_get1bit(ld
|
|
473 DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]"));
|
|
474 if (sbr->bs_extended_data)
|
|
475 {
|
|
476 uint16_t nr_bits_left;
|
12527
|
477 uint16_t cnt = (uint16_t)faad_getbits(ld, 4
|
10725
|
478 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size"));
|
|
479 if (cnt == 15)
|
|
480 {
|
12527
|
481 cnt += (uint16_t)faad_getbits(ld, 8
|
10725
|
482 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count"));
|
|
483 }
|
|
484
|
|
485 nr_bits_left = 8 * cnt;
|
|
486 while (nr_bits_left > 7)
|
|
487 {
|
12527
|
488 sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2
|
10725
|
489 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id"));
|
|
490 nr_bits_left -= 2;
|
12527
|
491 sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left);
|
10725
|
492 }
|
12527
|
493
|
|
494 /* Corrigendum */
|
|
495 if (nr_bits_left > 0)
|
|
496 {
|
|
497 faad_getbits(ld, nr_bits_left
|
|
498 DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left"));
|
|
499 }
|
10989
|
500 }
|
|
501
|
|
502 return 0;
|
10725
|
503 }
|
|
504
|
12527
|
505 /* integer log[2](x): input range [0,10) */
|
|
506 static int8_t sbr_log2(const int8_t val)
|
|
507 {
|
|
508 int8_t log2tab[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 };
|
|
509 if (val < 10 && val >= 0)
|
|
510 return log2tab[val];
|
|
511 else
|
|
512 return 0;
|
|
513 }
|
|
514
|
|
515
|
10725
|
516 /* table 7 */
|
10989
|
517 static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
10725
|
518 {
|
10989
|
519 uint8_t i, env, rel, result;
|
10725
|
520 uint8_t bs_abs_bord, bs_abs_bord_1;
|
12527
|
521 uint8_t bs_num_env = 0;
|
10725
|
522
|
12527
|
523 sbr->bs_frame_class[ch] = (uint8_t)faad_getbits(ld, 2
|
10725
|
524 DEBUGVAR(1,248,"sbr_grid(): bs_frame_class"));
|
|
525
|
|
526 switch (sbr->bs_frame_class[ch])
|
|
527 {
|
|
528 case FIXFIX:
|
12527
|
529 i = (uint8_t)faad_getbits(ld, 2
|
10725
|
530 DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw"));
|
|
531
|
|
532 bs_num_env = min(1 << i, 5);
|
|
533
|
12527
|
534 i = (uint8_t)faad_get1bit(ld
|
10725
|
535 DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag"));
|
|
536 for (env = 0; env < bs_num_env; env++)
|
|
537 sbr->f[ch][env] = i;
|
|
538
|
|
539 sbr->abs_bord_lead[ch] = 0;
|
10989
|
540 sbr->abs_bord_trail[ch] = sbr->numTimeSlots;
|
10725
|
541 sbr->n_rel_lead[ch] = bs_num_env - 1;
|
|
542 sbr->n_rel_trail[ch] = 0;
|
|
543 break;
|
|
544
|
|
545 case FIXVAR:
|
12527
|
546 bs_abs_bord = (uint8_t)faad_getbits(ld, 2
|
|
547 DEBUGVAR(1,251,"sbr_grid(): bs_abs_bord")) + sbr->numTimeSlots;
|
|
548 bs_num_env = (uint8_t)faad_getbits(ld, 2
|
10725
|
549 DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1;
|
|
550
|
|
551 for (rel = 0; rel < bs_num_env-1; rel++)
|
|
552 {
|
12527
|
553 sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
|
10725
|
554 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2;
|
|
555 }
|
12527
|
556 i = sbr_log2(bs_num_env + 1);
|
|
557 sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i
|
10725
|
558 DEBUGVAR(1,254,"sbr_grid(): bs_pointer"));
|
|
559
|
|
560 for (env = 0; env < bs_num_env; env++)
|
|
561 {
|
12527
|
562 sbr->f[ch][bs_num_env - env - 1] = (uint8_t)faad_get1bit(ld
|
10725
|
563 DEBUGVAR(1,255,"sbr_grid(): bs_freq_res"));
|
|
564 }
|
|
565
|
|
566 sbr->abs_bord_lead[ch] = 0;
|
|
567 sbr->abs_bord_trail[ch] = bs_abs_bord;
|
|
568 sbr->n_rel_lead[ch] = 0;
|
|
569 sbr->n_rel_trail[ch] = bs_num_env - 1;
|
|
570 break;
|
|
571
|
|
572 case VARFIX:
|
12527
|
573 bs_abs_bord = (uint8_t)faad_getbits(ld, 2
|
10725
|
574 DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord"));
|
12527
|
575 bs_num_env = (uint8_t)faad_getbits(ld, 2
|
10725
|
576 DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1;
|
|
577
|
|
578 for (rel = 0; rel < bs_num_env-1; rel++)
|
|
579 {
|
12527
|
580 sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
|
10725
|
581 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2;
|
|
582 }
|
12527
|
583 i = sbr_log2(bs_num_env + 1);
|
|
584 sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i
|
10725
|
585 DEBUGVAR(1,259,"sbr_grid(): bs_pointer"));
|
|
586
|
|
587 for (env = 0; env < bs_num_env; env++)
|
|
588 {
|
12527
|
589 sbr->f[ch][env] = (uint8_t)faad_get1bit(ld
|
10725
|
590 DEBUGVAR(1,260,"sbr_grid(): bs_freq_res"));
|
|
591 }
|
|
592
|
|
593 sbr->abs_bord_lead[ch] = bs_abs_bord;
|
10989
|
594 sbr->abs_bord_trail[ch] = sbr->numTimeSlots;
|
10725
|
595 sbr->n_rel_lead[ch] = bs_num_env - 1;
|
|
596 sbr->n_rel_trail[ch] = 0;
|
|
597 break;
|
|
598
|
|
599 case VARVAR:
|
12527
|
600 bs_abs_bord = (uint8_t)faad_getbits(ld, 2
|
10725
|
601 DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0"));
|
12527
|
602 bs_abs_bord_1 = (uint8_t)faad_getbits(ld, 2
|
|
603 DEBUGVAR(1,262,"sbr_grid(): bs_abs_bord_1")) + sbr->numTimeSlots;
|
|
604 sbr->bs_num_rel_0[ch] = (uint8_t)faad_getbits(ld, 2
|
10725
|
605 DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0"));
|
12527
|
606 sbr->bs_num_rel_1[ch] = (uint8_t)faad_getbits(ld, 2
|
10725
|
607 DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1"));
|
|
608
|
|
609 bs_num_env = min(5, sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 1);
|
|
610
|
|
611 for (rel = 0; rel < sbr->bs_num_rel_0[ch]; rel++)
|
|
612 {
|
12527
|
613 sbr->bs_rel_bord_0[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
|
10725
|
614 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2;
|
|
615 }
|
|
616 for(rel = 0; rel < sbr->bs_num_rel_1[ch]; rel++)
|
|
617 {
|
12527
|
618 sbr->bs_rel_bord_1[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
|
10725
|
619 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2;
|
|
620 }
|
12527
|
621 i = sbr_log2(sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 2);
|
|
622 sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i
|
10725
|
623 DEBUGVAR(1,267,"sbr_grid(): bs_pointer"));
|
|
624
|
|
625 for (env = 0; env < bs_num_env; env++)
|
|
626 {
|
12527
|
627 sbr->f[ch][env] = (uint8_t)faad_get1bit(ld
|
10725
|
628 DEBUGVAR(1,268,"sbr_grid(): bs_freq_res"));
|
|
629 }
|
|
630
|
|
631 sbr->abs_bord_lead[ch] = bs_abs_bord;
|
|
632 sbr->abs_bord_trail[ch] = bs_abs_bord_1;
|
|
633 sbr->n_rel_lead[ch] = sbr->bs_num_rel_0[ch];
|
|
634 sbr->n_rel_trail[ch] = sbr->bs_num_rel_1[ch];
|
|
635 break;
|
|
636 }
|
|
637
|
|
638 if (sbr->bs_frame_class[ch] == VARVAR)
|
|
639 sbr->L_E[ch] = min(bs_num_env, 5);
|
|
640 else
|
|
641 sbr->L_E[ch] = min(bs_num_env, 4);
|
|
642
|
12527
|
643 if (sbr->L_E[ch] <= 0)
|
|
644 return 1;
|
|
645
|
10725
|
646 if (sbr->L_E[ch] > 1)
|
|
647 sbr->L_Q[ch] = 2;
|
|
648 else
|
|
649 sbr->L_Q[ch] = 1;
|
|
650
|
|
651 /* TODO: this code can probably be integrated into the code above! */
|
10989
|
652 if ((result = envelope_time_border_vector(sbr, ch)) > 0)
|
|
653 return result;
|
10725
|
654 noise_floor_time_border_vector(sbr, ch);
|
10989
|
655
|
|
656 return 0;
|
10725
|
657 }
|
|
658
|
|
659 /* table 8 */
|
|
660 static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
661 {
|
|
662 uint8_t i;
|
|
663
|
|
664 for (i = 0; i < sbr->L_E[ch]; i++)
|
|
665 {
|
|
666 sbr->bs_df_env[ch][i] = faad_get1bit(ld
|
|
667 DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env"));
|
|
668 }
|
|
669
|
|
670 for (i = 0; i < sbr->L_Q[ch]; i++)
|
|
671 {
|
|
672 sbr->bs_df_noise[ch][i] = faad_get1bit(ld
|
|
673 DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise"));
|
|
674 }
|
|
675 }
|
|
676
|
|
677 /* table 9 */
|
|
678 static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
679 {
|
|
680 uint8_t n;
|
|
681
|
12527
|
682 for (n = 0; n < sbr->N_Q; n++)
|
10725
|
683 {
|
12527
|
684 sbr->bs_invf_mode[ch][n] = (uint8_t)faad_getbits(ld, 2
|
10989
|
685 DEBUGVAR(1,271,"invf_mode(): bs_invf_mode"));
|
12527
|
686 }
|
|
687 }
|
|
688
|
|
689 static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr,
|
|
690 uint8_t bs_extension_id, uint16_t num_bits_left)
|
|
691 {
|
|
692 switch (bs_extension_id)
|
|
693 {
|
|
694 #ifdef PS_DEC
|
|
695 case EXTENSION_ID_PS:
|
|
696 sbr->ps_used = 1;
|
|
697 return ps_data(&(sbr->ps), ld);
|
10989
|
698 #endif
|
12527
|
699 #ifdef DRM_PS
|
|
700 case DRM_PARAMETRIC_STEREO:
|
|
701 sbr->ps_used = 1;
|
|
702 return drm_ps_data(&(sbr->drm_ps), ld);
|
|
703 #endif
|
|
704 default:
|
|
705 sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6
|
|
706 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
|
|
707 return 6;
|
10725
|
708 }
|
|
709 }
|
|
710
|
|
711 /* table 12 */
|
|
712 static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
713 {
|
|
714 uint8_t n;
|
|
715
|
|
716 for (n = 0; n < sbr->N_high; n++)
|
|
717 {
|
|
718 sbr->bs_add_harmonic[ch][n] = faad_get1bit(ld
|
|
719 DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic"));
|
|
720 }
|
|
721 }
|
|
722
|
|
723
|
12527
|
724 #endif /* SBR_DEC */
|