10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
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 **
|
|
25 ** $Id: sbr_syntax.c,v 1.7 2003/07/29 08:20:13 menno Exp $
|
|
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"
|
|
40 #include "analysis.h"
|
|
41
|
|
42 static void sbr_reset(sbr_info *sbr)
|
|
43 {
|
|
44 /* if these are different from the previous frame: Reset = 1 */
|
|
45 if ((sbr->bs_start_freq != sbr->bs_start_freq_prev) ||
|
|
46 (sbr->bs_stop_freq != sbr->bs_stop_freq_prev) ||
|
|
47 (sbr->bs_freq_scale != sbr->bs_freq_scale_prev) ||
|
|
48 (sbr->bs_alter_scale != sbr->bs_alter_scale_prev))
|
|
49 {
|
|
50 sbr->Reset = 1;
|
|
51 } else {
|
|
52 sbr->Reset = 0;
|
|
53 }
|
|
54
|
|
55 if ((sbr->bs_start_freq != sbr->bs_start_freq_prev) ||
|
|
56 (sbr->bs_stop_freq != sbr->bs_stop_freq_prev) ||
|
|
57 (sbr->bs_freq_scale != sbr->bs_freq_scale_prev) ||
|
|
58 (sbr->bs_alter_scale != sbr->bs_alter_scale_prev) ||
|
|
59 (sbr->bs_xover_band != sbr->bs_xover_band_prev) ||
|
|
60 (sbr->bs_noise_bands != sbr->bs_noise_bands_prev))
|
|
61 {
|
|
62 sbr->Reset = 1;
|
|
63 } else {
|
|
64 sbr->Reset = 0;
|
|
65 }
|
|
66
|
|
67 sbr->bs_start_freq_prev = sbr->bs_start_freq;
|
|
68 sbr->bs_stop_freq_prev = sbr->bs_stop_freq;
|
|
69 sbr->bs_freq_scale_prev = sbr->bs_freq_scale;
|
|
70 sbr->bs_alter_scale_prev = sbr->bs_alter_scale;
|
|
71 sbr->bs_xover_band_prev = sbr->bs_xover_band;
|
|
72 sbr->bs_noise_bands_prev = sbr->bs_noise_bands;
|
|
73
|
|
74 if (sbr->frame == 0)
|
|
75 {
|
|
76 sbr->Reset = 1;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 /* table 2 */
|
|
81 uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint8_t id_aac)
|
|
82 {
|
|
83 uint8_t bs_extension_type = (uint8_t)faad_getbits(ld, 4
|
|
84 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type"));
|
|
85
|
|
86 if (bs_extension_type == EXT_SBR_DATA_CRC)
|
|
87 {
|
|
88 sbr->bs_sbr_crc_bits = (uint16_t)faad_getbits(ld, 10
|
|
89 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits"));
|
|
90 }
|
|
91
|
|
92 sbr->bs_header_flag = faad_get1bit(ld
|
|
93 DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag"));
|
|
94 if (sbr->bs_header_flag)
|
|
95 sbr_header(ld, sbr, id_aac);
|
|
96
|
|
97 /* TODO: Reset? */
|
|
98 sbr_reset(sbr);
|
|
99
|
|
100 /* first frame should have a header */
|
|
101 if (sbr->frame == 0 && sbr->bs_header_flag == 0)
|
|
102 return 1;
|
|
103
|
|
104
|
|
105 if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))
|
|
106 {
|
|
107 uint16_t k2;
|
|
108
|
|
109 /* calculate the Master Frequency Table */
|
|
110 sbr->k0 = qmf_start_channel(sbr->bs_start_freq, sbr->bs_samplerate_mode,
|
|
111 sbr->sample_rate);
|
|
112 k2 = qmf_stop_channel(sbr->bs_stop_freq, sbr->sample_rate, sbr->k0);
|
|
113
|
|
114 /* check k0 and k2 */
|
|
115 if (sbr->sample_rate >= 48000)
|
|
116 {
|
|
117 if ((k2 - sbr->k0) > 32)
|
|
118 return 1;
|
|
119 } else if (sbr->sample_rate <= 32000) {
|
|
120 if ((k2 - sbr->k0) > 48)
|
|
121 return 1;
|
|
122 } else { /* (sbr->sample_rate == 44100) */
|
|
123 if ((k2 - sbr->k0) > 45)
|
|
124 return 1;
|
|
125 }
|
|
126
|
|
127 if (sbr->bs_freq_scale == 0)
|
|
128 {
|
|
129 master_frequency_table_fs0(sbr, sbr->k0, k2, sbr->bs_alter_scale);
|
|
130 } else {
|
|
131 master_frequency_table(sbr, sbr->k0, k2, sbr->bs_freq_scale,
|
|
132 sbr->bs_alter_scale);
|
|
133 }
|
|
134 derived_frequency_table(sbr, sbr->bs_xover_band, k2);
|
|
135 }
|
|
136
|
|
137 sbr_data(ld, sbr, id_aac);
|
|
138
|
|
139 /* no error */
|
|
140 return 0;
|
|
141 }
|
|
142
|
|
143 /* table 3 */
|
|
144 static void sbr_header(bitfile *ld, sbr_info *sbr, uint8_t id_aac)
|
|
145 {
|
|
146 uint8_t bs_header_extra_1, bs_header_extra_2;
|
|
147
|
|
148 sbr->header_count++;
|
|
149
|
|
150 sbr->bs_amp_res = faad_get1bit(ld
|
|
151 DEBUGVAR(1,203,"sbr_header(): bs_amp_res"));
|
|
152
|
|
153 /* bs_start_freq and bs_stop_freq must define a fequency band that does
|
|
154 not exceed 48 channels */
|
|
155 sbr->bs_start_freq = faad_getbits(ld, 4
|
|
156 DEBUGVAR(1,204,"sbr_header(): bs_start_freq"));
|
|
157 sbr->bs_stop_freq = faad_getbits(ld, 4
|
|
158 DEBUGVAR(1,205,"sbr_header(): bs_stop_freq"));
|
|
159 sbr->bs_xover_band = faad_getbits(ld, 3
|
|
160 DEBUGVAR(1,206,"sbr_header(): bs_xover_band"));
|
|
161 faad_getbits(ld, 2
|
|
162 DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr"));
|
|
163 bs_header_extra_1 = faad_get1bit(ld
|
|
164 DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1"));
|
|
165 bs_header_extra_2 = faad_get1bit(ld
|
|
166 DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2"));
|
|
167
|
|
168 if (bs_header_extra_1)
|
|
169 {
|
|
170 sbr->bs_freq_scale = faad_getbits(ld, 2
|
|
171 DEBUGVAR(1,211,"sbr_header(): bs_freq_scale"));
|
|
172 sbr->bs_alter_scale = faad_get1bit(ld
|
|
173 DEBUGVAR(1,212,"sbr_header(): bs_alter_scale"));
|
|
174 sbr->bs_noise_bands = faad_getbits(ld, 2
|
|
175 DEBUGVAR(1,213,"sbr_header(): bs_noise_bands"));
|
|
176 }
|
|
177 if (bs_header_extra_2)
|
|
178 {
|
|
179 sbr->bs_limiter_bands = faad_getbits(ld, 2
|
|
180 DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands"));
|
|
181 sbr->bs_limiter_gains = faad_getbits(ld, 2
|
|
182 DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains"));
|
|
183 sbr->bs_interpol_freq = faad_get1bit(ld
|
|
184 DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq"));
|
|
185 sbr->bs_smoothing_mode = faad_get1bit(ld
|
|
186 DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode"));
|
|
187 }
|
|
188
|
|
189 #if 0
|
|
190 /* print the header to screen */
|
|
191 printf("bs_amp_res: %d\n", sbr->bs_amp_res);
|
|
192 printf("bs_start_freq: %d\n", sbr->bs_start_freq);
|
|
193 printf("bs_stop_freq: %d\n", sbr->bs_stop_freq);
|
|
194 printf("bs_xover_band: %d\n", sbr->bs_xover_band);
|
|
195 if (bs_header_extra_1)
|
|
196 {
|
|
197 printf("bs_freq_scale: %d\n", sbr->bs_freq_scale);
|
|
198 printf("bs_alter_scale: %d\n", sbr->bs_alter_scale);
|
|
199 printf("bs_noise_bands: %d\n", sbr->bs_noise_bands);
|
|
200 }
|
|
201 if (bs_header_extra_2)
|
|
202 {
|
|
203 printf("bs_limiter_bands: %d\n", sbr->bs_limiter_bands);
|
|
204 printf("bs_limiter_gains: %d\n", sbr->bs_limiter_gains);
|
|
205 printf("bs_interpol_freq: %d\n", sbr->bs_interpol_freq);
|
|
206 printf("bs_smoothing_mode: %d\n", sbr->bs_smoothing_mode);
|
|
207 }
|
|
208 printf("\n");
|
|
209 #endif
|
|
210 }
|
|
211
|
|
212 /* table 4 */
|
|
213 static void sbr_data(bitfile *ld, sbr_info *sbr, uint8_t id_aac)
|
|
214 {
|
|
215 #if 0
|
|
216 sbr->bs_samplerate_mode = faad_get1bit(ld
|
|
217 DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode"));
|
|
218 #endif
|
|
219
|
|
220 sbr->rate = (sbr->bs_samplerate_mode) ? 2 : 1;
|
|
221
|
|
222 switch (id_aac)
|
|
223 {
|
|
224 case ID_SCE:
|
|
225 sbr_single_channel_element(ld, sbr);
|
|
226 break;
|
|
227 case ID_CPE:
|
|
228 sbr_channel_pair_element(ld, sbr);
|
|
229 break;
|
|
230 }
|
|
231 }
|
|
232
|
|
233 /* table 5 */
|
|
234 static void sbr_single_channel_element(bitfile *ld, sbr_info *sbr)
|
|
235 {
|
|
236 if (faad_get1bit(ld
|
|
237 DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra")))
|
|
238 {
|
|
239 faad_getbits(ld, 4
|
|
240 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data"));
|
|
241 }
|
|
242
|
|
243 sbr_grid(ld, sbr, 0);
|
|
244 sbr_dtdf(ld, sbr, 0);
|
|
245 invf_mode(ld, sbr, 0);
|
|
246 sbr_envelope(ld, sbr, 0);
|
|
247 sbr_noise(ld, sbr, 0);
|
|
248
|
|
249 envelope_noise_dequantisation(sbr, 0);
|
|
250
|
|
251 #if 0
|
|
252 // TEMP
|
|
253 if (sbr->frame == 21)
|
|
254 {
|
|
255 int l, k;
|
|
256
|
|
257 printf("\n");
|
|
258 for (l = 0; l < sbr->L_E[0]; l++)
|
|
259 {
|
|
260 for (k = 0; k < sbr->n[sbr->f[0][l]]; k++)
|
|
261 {
|
|
262 //printf("%f\n", sbr->E_orig[0][k][l]);
|
|
263 printf("%f\n", sbr->E_orig[0][k][l] * 1024. / (float)(1 << REAL_BITS));
|
|
264 }
|
|
265 }
|
|
266 }
|
|
267 // end TEMP
|
|
268 #endif
|
|
269
|
|
270 #if 0
|
|
271 // TEMP
|
|
272 {
|
|
273 int l, k;
|
|
274
|
|
275 printf("\n");
|
|
276 for (l = 0; l < sbr->L_Q[0]; l++)
|
|
277 {
|
|
278 for (k = 0; k < sbr->N_Q; k++)
|
|
279 {
|
|
280 printf("%f\n", sbr->Q_orig[0][k][l]);
|
|
281 }
|
|
282 }
|
|
283 }
|
|
284 // end TEMP
|
|
285 #endif
|
|
286
|
|
287 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
|
|
288
|
|
289 sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
|
|
290 DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]"));
|
|
291 if (sbr->bs_add_harmonic_flag[0])
|
|
292 sinusoidal_coding(ld, sbr, 0);
|
|
293
|
|
294 sbr->bs_extended_data = faad_get1bit(ld
|
|
295 DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]"));
|
|
296 if (sbr->bs_extended_data)
|
|
297 {
|
|
298 uint16_t nr_bits_left;
|
|
299 uint16_t cnt = faad_getbits(ld, 4
|
|
300 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size"));
|
|
301 if (cnt == 15)
|
|
302 {
|
|
303 cnt += faad_getbits(ld, 8
|
|
304 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count"));
|
|
305 }
|
|
306
|
|
307 nr_bits_left = 8 * cnt;
|
|
308 while (nr_bits_left > 7)
|
|
309 {
|
|
310 sbr->bs_extension_id = faad_getbits(ld, 2
|
|
311 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id"));
|
|
312 nr_bits_left -= 2;
|
|
313 /* sbr_extension(ld, sbr, 0, nr_bits_left); */
|
|
314 sbr->bs_extension_data = faad_getbits(ld, 6
|
|
315 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
|
|
316 }
|
|
317 }
|
|
318 }
|
|
319
|
|
320 /* table 6 */
|
|
321 static void sbr_channel_pair_element(bitfile *ld, sbr_info *sbr)
|
|
322 {
|
|
323 uint8_t n;
|
|
324
|
|
325 if (faad_get1bit(ld
|
|
326 DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra")))
|
|
327 {
|
|
328 faad_getbits(ld, 4
|
|
329 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
|
|
330 faad_getbits(ld, 4
|
|
331 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
|
|
332 }
|
|
333
|
|
334 sbr->bs_coupling = faad_get1bit(ld
|
|
335 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling"));
|
|
336
|
|
337 if (sbr->bs_coupling)
|
|
338 {
|
|
339 sbr_grid(ld, sbr, 0);
|
|
340
|
|
341 /* need to copy some data from left to right */
|
|
342 sbr->bs_frame_class[1] = sbr->bs_frame_class[0];
|
|
343 sbr->L_E[1] = sbr->L_E[0];
|
|
344 sbr->L_Q[1] = sbr->L_Q[0];
|
|
345 sbr->bs_pointer[1] = sbr->bs_pointer[0];
|
|
346
|
|
347 for (n = 0; n <= sbr->L_E[0]; n++)
|
|
348 {
|
|
349 sbr->t_E[1][n] = sbr->t_E[0][n];
|
|
350 sbr->f[1][n] = sbr->f[0][n];
|
|
351 }
|
|
352 for (n = 0; n <= sbr->L_Q[0]; n++)
|
|
353 sbr->t_Q[1][n] = sbr->t_Q[0][n];
|
|
354
|
|
355 sbr_dtdf(ld, sbr, 0);
|
|
356 sbr_dtdf(ld, sbr, 1);
|
|
357 invf_mode(ld, sbr, 0);
|
|
358
|
|
359 /* more copying */
|
|
360 for (n = 0; n < sbr->N_Q; n++)
|
|
361 sbr->bs_invf_mode[1][n] = sbr->bs_invf_mode[0][n];
|
|
362
|
|
363 sbr_envelope(ld, sbr, 0);
|
|
364 sbr_noise(ld, sbr, 0);
|
|
365 sbr_envelope(ld, sbr, 1);
|
|
366 sbr_noise(ld, sbr, 1);
|
|
367
|
|
368 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
|
|
369 memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t));
|
|
370
|
|
371 sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
|
|
372 DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
|
|
373 if (sbr->bs_add_harmonic_flag[0])
|
|
374 sinusoidal_coding(ld, sbr, 0);
|
|
375
|
|
376 sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld
|
|
377 DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
|
|
378 if (sbr->bs_add_harmonic_flag[1])
|
|
379 sinusoidal_coding(ld, sbr, 1);
|
|
380 } else {
|
|
381 sbr_grid(ld, sbr, 0);
|
|
382 sbr_grid(ld, sbr, 1);
|
|
383 sbr_dtdf(ld, sbr, 0);
|
|
384 sbr_dtdf(ld, sbr, 1);
|
|
385 invf_mode(ld, sbr, 0);
|
|
386 invf_mode(ld, sbr, 1);
|
|
387 sbr_envelope(ld, sbr, 0);
|
|
388 sbr_envelope(ld, sbr, 1);
|
|
389 sbr_noise(ld, sbr, 0);
|
|
390 sbr_noise(ld, sbr, 1);
|
|
391
|
|
392 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
|
|
393 memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t));
|
|
394
|
|
395 sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
|
|
396 DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
|
|
397 if (sbr->bs_add_harmonic_flag[0])
|
|
398 sinusoidal_coding(ld, sbr, 0);
|
|
399
|
|
400 sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld
|
|
401 DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
|
|
402 if (sbr->bs_add_harmonic_flag[1])
|
|
403 sinusoidal_coding(ld, sbr, 1);
|
|
404 }
|
|
405 envelope_noise_dequantisation(sbr, 0);
|
|
406 envelope_noise_dequantisation(sbr, 1);
|
|
407
|
|
408 #if 0
|
|
409 // TEMP
|
|
410 if (sbr->frame == 21)
|
|
411 {
|
|
412 int l, k;
|
|
413
|
|
414 printf("\n");
|
|
415 for (l = 0; l < sbr->L_E[0]; l++)
|
|
416 {
|
|
417 for (k = 0; k < sbr->n[sbr->f[0][l]]; k++)
|
|
418 {
|
|
419 printf("%f\n", sbr->E_orig[0][k][l]);
|
|
420 //printf("%f\n", sbr->E_orig[0][k][l] * 1024. / (float)(1 << REAL_BITS));
|
|
421 }
|
|
422 }
|
|
423 }
|
|
424 // end TEMP
|
|
425 #endif
|
|
426
|
|
427 if (sbr->bs_coupling)
|
|
428 unmap_envelope_noise(sbr);
|
|
429
|
|
430 #if 0
|
|
431 // TEMP
|
|
432 if (sbr->bs_coupling)
|
|
433 {
|
|
434 int l, k;
|
|
435
|
|
436 printf("\n");
|
|
437 for (l = 0; l < sbr->L_Q[0]; l++)
|
|
438 {
|
|
439 for (k = 0; k < sbr->N_Q; k++)
|
|
440 {
|
|
441 printf("%f\n", sbr->Q_orig[0][k][l]);
|
|
442 }
|
|
443 }
|
|
444 }
|
|
445 // end TEMP
|
|
446 #endif
|
|
447
|
|
448 sbr->bs_extended_data = faad_get1bit(ld
|
|
449 DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]"));
|
|
450 if (sbr->bs_extended_data)
|
|
451 {
|
|
452 uint16_t nr_bits_left;
|
|
453 uint16_t cnt = faad_getbits(ld, 4
|
|
454 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size"));
|
|
455 if (cnt == 15)
|
|
456 {
|
|
457 cnt += faad_getbits(ld, 8
|
|
458 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count"));
|
|
459 }
|
|
460
|
|
461 nr_bits_left = 8 * cnt;
|
|
462 while (nr_bits_left > 7)
|
|
463 {
|
|
464 sbr->bs_extension_id = faad_getbits(ld, 2
|
|
465 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id"));
|
|
466 nr_bits_left -= 2;
|
|
467 /* sbr_extension(ld, sbr, 0, nr_bits_left); */
|
|
468 sbr->bs_extension_data = faad_getbits(ld, 6
|
|
469 DEBUGVAR(1,280,"sbr_single_channel_element(): bs_extension_data"));
|
|
470 }
|
|
471 }
|
|
472 }
|
|
473
|
|
474 /* table 7 */
|
|
475 static void sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
476 {
|
|
477 uint8_t i, env, rel;
|
|
478 uint8_t bs_abs_bord, bs_abs_bord_1;
|
|
479 uint16_t bs_num_env;
|
|
480
|
|
481 sbr->bs_frame_class[ch] = faad_getbits(ld, 2
|
|
482 DEBUGVAR(1,248,"sbr_grid(): bs_frame_class"));
|
|
483
|
|
484 #if 0
|
|
485 if (sbr->bs_frame_class[ch] != FIXFIX)
|
|
486 printf("%d", sbr->bs_frame_class[ch]);
|
|
487 #endif
|
|
488
|
|
489 switch (sbr->bs_frame_class[ch])
|
|
490 {
|
|
491 case FIXFIX:
|
|
492 i = faad_getbits(ld, 2
|
|
493 DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw"));
|
|
494
|
|
495 bs_num_env = min(1 << i, 5);
|
|
496
|
|
497 i = faad_get1bit(ld
|
|
498 DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag"));
|
|
499 for (env = 0; env < bs_num_env; env++)
|
|
500 sbr->f[ch][env] = i;
|
|
501
|
|
502 sbr->abs_bord_lead[ch] = 0;
|
|
503 sbr->abs_bord_trail[ch] = NO_TIME_SLOTS;
|
|
504 sbr->n_rel_lead[ch] = bs_num_env - 1;
|
|
505 sbr->n_rel_trail[ch] = 0;
|
|
506 break;
|
|
507
|
|
508 case FIXVAR:
|
|
509 bs_abs_bord = faad_getbits(ld, 2
|
|
510 DEBUGVAR(1,251,"sbr_grid(): bs_abs_bord")) + NO_TIME_SLOTS;
|
|
511 bs_num_env = faad_getbits(ld, 2
|
|
512 DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1;
|
|
513
|
|
514 for (rel = 0; rel < bs_num_env-1; rel++)
|
|
515 {
|
|
516 sbr->bs_rel_bord[ch][rel] = 2 * faad_getbits(ld, 2
|
|
517 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2;
|
|
518 }
|
|
519 i = int_log2((int32_t)(bs_num_env + 1));
|
|
520 sbr->bs_pointer[ch] = faad_getbits(ld, i
|
|
521 DEBUGVAR(1,254,"sbr_grid(): bs_pointer"));
|
|
522
|
|
523 for (env = 0; env < bs_num_env; env++)
|
|
524 {
|
|
525 sbr->f[ch][bs_num_env - env - 1] = faad_get1bit(ld
|
|
526 DEBUGVAR(1,255,"sbr_grid(): bs_freq_res"));
|
|
527 }
|
|
528
|
|
529 sbr->abs_bord_lead[ch] = 0;
|
|
530 sbr->abs_bord_trail[ch] = bs_abs_bord;
|
|
531 sbr->n_rel_lead[ch] = 0;
|
|
532 sbr->n_rel_trail[ch] = bs_num_env - 1;
|
|
533 break;
|
|
534
|
|
535 case VARFIX:
|
|
536 bs_abs_bord = faad_getbits(ld, 2
|
|
537 DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord"));
|
|
538 bs_num_env = faad_getbits(ld, 2
|
|
539 DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1;
|
|
540
|
|
541 for (rel = 0; rel < bs_num_env-1; rel++)
|
|
542 {
|
|
543 sbr->bs_rel_bord[ch][rel] = 2 * faad_getbits(ld, 2
|
|
544 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2;
|
|
545 }
|
|
546 i = int_log2((int32_t)(bs_num_env + 1));
|
|
547 sbr->bs_pointer[ch] = faad_getbits(ld, i
|
|
548 DEBUGVAR(1,259,"sbr_grid(): bs_pointer"));
|
|
549
|
|
550 for (env = 0; env < bs_num_env; env++)
|
|
551 {
|
|
552 sbr->f[ch][env] = faad_get1bit(ld
|
|
553 DEBUGVAR(1,260,"sbr_grid(): bs_freq_res"));
|
|
554 }
|
|
555
|
|
556 sbr->abs_bord_lead[ch] = bs_abs_bord;
|
|
557 sbr->abs_bord_trail[ch] = NO_TIME_SLOTS;
|
|
558 sbr->n_rel_lead[ch] = bs_num_env - 1;
|
|
559 sbr->n_rel_trail[ch] = 0;
|
|
560 break;
|
|
561
|
|
562 case VARVAR:
|
|
563 bs_abs_bord = faad_getbits(ld, 2
|
|
564 DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0"));
|
|
565 bs_abs_bord_1 = faad_getbits(ld, 2
|
|
566 DEBUGVAR(1,262,"sbr_grid(): bs_abs_bord_1")) + NO_TIME_SLOTS;
|
|
567 sbr->bs_num_rel_0[ch] = faad_getbits(ld, 2
|
|
568 DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0"));
|
|
569 sbr->bs_num_rel_1[ch] = faad_getbits(ld, 2
|
|
570 DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1"));
|
|
571
|
|
572 bs_num_env = min(5, sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 1);
|
|
573
|
|
574 for (rel = 0; rel < sbr->bs_num_rel_0[ch]; rel++)
|
|
575 {
|
|
576 sbr->bs_rel_bord_0[ch][rel] = 2 * faad_getbits(ld, 2
|
|
577 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2;
|
|
578 }
|
|
579 for(rel = 0; rel < sbr->bs_num_rel_1[ch]; rel++)
|
|
580 {
|
|
581 sbr->bs_rel_bord_1[ch][rel] = 2 * faad_getbits(ld, 2
|
|
582 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2;
|
|
583 }
|
|
584 i = int_log2((int32_t)(sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 2));
|
|
585 sbr->bs_pointer[ch] = faad_getbits(ld, i
|
|
586 DEBUGVAR(1,267,"sbr_grid(): bs_pointer"));
|
|
587
|
|
588 for (env = 0; env < bs_num_env; env++)
|
|
589 {
|
|
590 sbr->f[ch][env] = faad_get1bit(ld
|
|
591 DEBUGVAR(1,268,"sbr_grid(): bs_freq_res"));
|
|
592 }
|
|
593
|
|
594 sbr->abs_bord_lead[ch] = bs_abs_bord;
|
|
595 sbr->abs_bord_trail[ch] = bs_abs_bord_1;
|
|
596 sbr->n_rel_lead[ch] = sbr->bs_num_rel_0[ch];
|
|
597 sbr->n_rel_trail[ch] = sbr->bs_num_rel_1[ch];
|
|
598 break;
|
|
599 }
|
|
600
|
|
601 if (sbr->bs_frame_class[ch] == VARVAR)
|
|
602 sbr->L_E[ch] = min(bs_num_env, 5);
|
|
603 else
|
|
604 sbr->L_E[ch] = min(bs_num_env, 4);
|
|
605
|
|
606 if (sbr->L_E[ch] > 1)
|
|
607 sbr->L_Q[ch] = 2;
|
|
608 else
|
|
609 sbr->L_Q[ch] = 1;
|
|
610
|
|
611 /* TODO: this code can probably be integrated into the code above! */
|
|
612 envelope_time_border_vector(sbr, ch);
|
|
613 noise_floor_time_border_vector(sbr, ch);
|
|
614 }
|
|
615
|
|
616 /* table 8 */
|
|
617 static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
618 {
|
|
619 uint8_t i;
|
|
620
|
|
621 for (i = 0; i < sbr->L_E[ch]; i++)
|
|
622 {
|
|
623 sbr->bs_df_env[ch][i] = faad_get1bit(ld
|
|
624 DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env"));
|
|
625 }
|
|
626
|
|
627 for (i = 0; i < sbr->L_Q[ch]; i++)
|
|
628 {
|
|
629 sbr->bs_df_noise[ch][i] = faad_get1bit(ld
|
|
630 DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise"));
|
|
631 }
|
|
632 }
|
|
633
|
|
634 /* table 9 */
|
|
635 static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
636 {
|
|
637 uint8_t n;
|
|
638
|
|
639 for (n = 0; n < sbr->N_Q; n++)
|
|
640 {
|
|
641 sbr->bs_invf_mode[ch][n] = faad_getbits(ld, 2
|
|
642 DEBUGVAR(1,271,"invf_mode(): bs_invf_mode"));
|
|
643 }
|
|
644 }
|
|
645
|
|
646 /* table 10 */
|
|
647 static void sbr_envelope(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
648 {
|
|
649 uint8_t env, band;
|
|
650 int8_t delta = 0;
|
|
651 sbr_huff_tab t_huff, f_huff;
|
|
652
|
|
653 if ((sbr->L_E[ch] == 1) && (sbr->bs_frame_class[ch] == FIXFIX))
|
|
654 sbr->amp_res[ch] = 0;
|
|
655 else
|
|
656 sbr->amp_res[ch] = sbr->bs_amp_res;
|
|
657
|
|
658 if ((sbr->bs_coupling) && (ch == 1))
|
|
659 {
|
|
660 delta = 1;
|
|
661 if (sbr->amp_res[ch])
|
|
662 {
|
|
663 t_huff = t_huffman_env_bal_3_0dB;
|
|
664 f_huff = f_huffman_env_bal_3_0dB;
|
|
665 } else {
|
|
666 t_huff = t_huffman_env_bal_1_5dB;
|
|
667 f_huff = f_huffman_env_bal_1_5dB;
|
|
668 }
|
|
669 } else {
|
|
670 delta = 0;
|
|
671 if (sbr->amp_res[ch])
|
|
672 {
|
|
673 t_huff = t_huffman_env_3_0dB;
|
|
674 f_huff = f_huffman_env_3_0dB;
|
|
675 } else {
|
|
676 t_huff = t_huffman_env_1_5dB;
|
|
677 f_huff = f_huffman_env_1_5dB;
|
|
678 }
|
|
679 }
|
|
680
|
|
681 for (env = 0; env < sbr->L_E[ch]; env++)
|
|
682 {
|
|
683 if (sbr->bs_df_env[ch][env] == 0)
|
|
684 {
|
|
685 if ((sbr->bs_coupling == 1) && (ch == 1))
|
|
686 {
|
|
687 if (sbr->amp_res[ch])
|
|
688 {
|
|
689 sbr->E[ch][0][env] = (faad_getbits(ld, 5
|
|
690 DEBUGVAR(1,272,"sbr_envelope(): bs_data_env")) << delta);
|
|
691 } else {
|
|
692 sbr->E[ch][0][env] = (faad_getbits(ld, 6
|
|
693 DEBUGVAR(1,273,"sbr_envelope(): bs_data_env")) << delta);
|
|
694 }
|
|
695 } else {
|
|
696 if (sbr->amp_res[ch])
|
|
697 {
|
|
698 sbr->E[ch][0][env] = (faad_getbits(ld, 6
|
|
699 DEBUGVAR(1,274,"sbr_envelope(): bs_data_env")) << delta);
|
|
700 } else {
|
|
701 sbr->E[ch][0][env] = (faad_getbits(ld, 7
|
|
702 DEBUGVAR(1,275,"sbr_envelope(): bs_data_env")) << delta);
|
|
703 }
|
|
704 }
|
|
705
|
|
706 for (band = 1; band < sbr->n[sbr->f[ch][env]]; band++)
|
|
707 {
|
|
708 sbr->E[ch][band][env] = (sbr_huff_dec(ld, f_huff) << delta);
|
|
709 }
|
|
710
|
|
711 } else {
|
|
712 for (band = 0; band < sbr->n[sbr->f[ch][env]]; band++)
|
|
713 {
|
|
714 sbr->E[ch][band][env] = (sbr_huff_dec(ld, t_huff) << delta);
|
|
715 }
|
|
716 }
|
|
717 }
|
|
718
|
|
719 #if 0
|
|
720 // TEMP
|
|
721 if (sbr->frame == 19)
|
|
722 {
|
|
723 int l, k;
|
|
724
|
|
725 printf("\n");
|
|
726 for (l = 0; l < sbr->L_E[ch]; l++)
|
|
727 {
|
|
728 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
729 {
|
|
730 printf("l:%d k:%d E:%d\n",l, k, sbr->E[ch][k][l]);
|
|
731 }
|
|
732 }
|
|
733 }
|
|
734 // end TEMP
|
|
735 #endif
|
|
736
|
|
737 extract_envelope_data(sbr, ch);
|
|
738
|
|
739 #if 0
|
|
740 // TEMP
|
|
741 if (sbr->frame == 21)
|
|
742 {
|
|
743 int l, k;
|
|
744
|
|
745 printf("\n");
|
|
746 for (l = 0; l < sbr->L_E[ch]; l++)
|
|
747 {
|
|
748 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
749 {
|
|
750 //printf("l:%d k:%d E:%d\n",l,k, sbr->E[ch][k][l]);
|
|
751 printf("%d\n", sbr->E[ch][k][l]);
|
|
752 }
|
|
753 }
|
|
754 }
|
|
755 // end TEMP
|
|
756 #endif
|
|
757 }
|
|
758
|
|
759 /* table 11 */
|
|
760 static void sbr_noise(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
761 {
|
|
762 uint8_t noise, band;
|
|
763 int8_t delta = 0;
|
|
764 sbr_huff_tab t_huff, f_huff;
|
|
765
|
|
766 if ((sbr->bs_coupling == 1) && (ch == 1))
|
|
767 {
|
|
768 delta = 1;
|
|
769 t_huff = t_huffman_noise_bal_3_0dB;
|
|
770 f_huff = f_huffman_env_bal_3_0dB;
|
|
771 } else {
|
|
772 delta = 0;
|
|
773 t_huff = t_huffman_noise_3_0dB;
|
|
774 f_huff = f_huffman_env_3_0dB;
|
|
775 }
|
|
776
|
|
777 for (noise = 0; noise < sbr->L_Q[ch]; noise++)
|
|
778 {
|
|
779 if(sbr->bs_df_noise[ch][noise] == 0)
|
|
780 {
|
|
781 if ((sbr->bs_coupling == 1) && (ch == 1))
|
|
782 {
|
|
783 sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
|
|
784 DEBUGVAR(1,276,"sbr_noise(): bs_data_noise")) << delta);
|
|
785 } else {
|
|
786 sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
|
|
787 DEBUGVAR(1,277,"sbr_noise(): bs_data_noise")) << delta);
|
|
788 }
|
|
789 for (band = 1; band < sbr->N_Q; band++)
|
|
790 {
|
|
791 sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, f_huff) << delta);
|
|
792 }
|
|
793 } else {
|
|
794 for (band = 0; band < sbr->N_Q; band++)
|
|
795 {
|
|
796 sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, t_huff) << delta);
|
|
797 }
|
|
798 }
|
|
799 }
|
|
800
|
|
801 extract_noise_floor_data(sbr, ch);
|
|
802 }
|
|
803
|
|
804 /* table 12 */
|
|
805 static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch)
|
|
806 {
|
|
807 uint8_t n;
|
|
808
|
|
809 for (n = 0; n < sbr->N_high; n++)
|
|
810 {
|
|
811 sbr->bs_add_harmonic[ch][n] = faad_get1bit(ld
|
|
812 DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic"));
|
|
813 }
|
|
814 }
|
|
815
|
|
816
|
|
817 #endif /* SBR_DEC */ |