2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: sbr_syntax.c,v 1.31 2004/05/17 10:18:03 menno Exp $
33 #include "sbr_syntax.h"
37 #include "sbr_tf_grid.h"
48 /* static function declarations */
49 static void sbr_header(bitfile
*ld
, sbr_info
*sbr
);
50 static uint8_t calc_sbr_tables(sbr_info
*sbr
, uint8_t start_freq
, uint8_t stop_freq
,
51 uint8_t samplerate_mode
, uint8_t freq_scale
,
52 uint8_t alter_scale
, uint8_t xover_band
);
53 static uint8_t sbr_data(bitfile
*ld
, sbr_info
*sbr
);
54 static uint16_t sbr_extension(bitfile
*ld
, sbr_info
*sbr
,
55 uint8_t bs_extension_id
, uint16_t num_bits_left
);
56 static uint8_t sbr_single_channel_element(bitfile
*ld
, sbr_info
*sbr
);
57 static uint8_t sbr_channel_pair_element(bitfile
*ld
, sbr_info
*sbr
);
58 static uint8_t sbr_grid(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
);
59 static void sbr_dtdf(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
);
60 static void invf_mode(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
);
61 static void sinusoidal_coding(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
);
64 static void sbr_reset(sbr_info
*sbr
)
67 printf("%d\n", sbr
->bs_start_freq_prev
);
68 printf("%d\n", sbr
->bs_stop_freq_prev
);
69 printf("%d\n", sbr
->bs_freq_scale_prev
);
70 printf("%d\n", sbr
->bs_alter_scale_prev
);
71 printf("%d\n", sbr
->bs_xover_band_prev
);
72 printf("%d\n\n", sbr
->bs_noise_bands_prev
);
75 /* if these are different from the previous frame: Reset = 1 */
76 if ((sbr
->bs_start_freq
!= sbr
->bs_start_freq_prev
) ||
77 (sbr
->bs_stop_freq
!= sbr
->bs_stop_freq_prev
) ||
78 (sbr
->bs_freq_scale
!= sbr
->bs_freq_scale_prev
) ||
79 (sbr
->bs_alter_scale
!= sbr
->bs_alter_scale_prev
))
86 if ((sbr
->bs_start_freq
!= sbr
->bs_start_freq_prev
) ||
87 (sbr
->bs_stop_freq
!= sbr
->bs_stop_freq_prev
) ||
88 (sbr
->bs_freq_scale
!= sbr
->bs_freq_scale_prev
) ||
89 (sbr
->bs_alter_scale
!= sbr
->bs_alter_scale_prev
) ||
90 (sbr
->bs_xover_band
!= sbr
->bs_xover_band_prev
) ||
91 (sbr
->bs_noise_bands
!= sbr
->bs_noise_bands_prev
))
98 sbr
->bs_start_freq_prev
= sbr
->bs_start_freq
;
99 sbr
->bs_stop_freq_prev
= sbr
->bs_stop_freq
;
100 sbr
->bs_freq_scale_prev
= sbr
->bs_freq_scale
;
101 sbr
->bs_alter_scale_prev
= sbr
->bs_alter_scale
;
102 sbr
->bs_xover_band_prev
= sbr
->bs_xover_band
;
103 sbr
->bs_noise_bands_prev
= sbr
->bs_noise_bands
;
106 static uint8_t calc_sbr_tables(sbr_info
*sbr
, uint8_t start_freq
, uint8_t stop_freq
,
107 uint8_t samplerate_mode
, uint8_t freq_scale
,
108 uint8_t alter_scale
, uint8_t xover_band
)
113 /* calculate the Master Frequency Table */
114 sbr
->k0
= qmf_start_channel(start_freq
, samplerate_mode
, sbr
->sample_rate
);
115 k2
= qmf_stop_channel(stop_freq
, sbr
->sample_rate
, sbr
->k0
);
117 /* check k0 and k2 */
118 if (sbr
->sample_rate
>= 48000)
120 if ((k2
- sbr
->k0
) > 32)
122 } else if (sbr
->sample_rate
<= 32000) {
123 if ((k2
- sbr
->k0
) > 48)
125 } else { /* (sbr->sample_rate == 44100) */
126 if ((k2
- sbr
->k0
) > 45)
132 result
+= master_frequency_table_fs0(sbr
, sbr
->k0
, k2
, alter_scale
);
134 result
+= master_frequency_table(sbr
, sbr
->k0
, k2
, freq_scale
, alter_scale
);
136 result
+= derived_frequency_table(sbr
, xover_band
, k2
);
138 result
= (result
> 0) ? 1 : 0;
144 uint8_t sbr_extension_data(bitfile
*ld
, sbr_info
*sbr
, uint16_t cnt
)
147 uint16_t num_align_bits
= 0;
148 uint16_t num_sbr_bits
= (uint16_t)faad_get_processed_bits(ld
);
150 uint8_t saved_start_freq
, saved_samplerate_mode
;
151 uint8_t saved_stop_freq
, saved_freq_scale
;
152 uint8_t saved_alter_scale
, saved_xover_band
;
155 if (!sbr
->Is_DRM_SBR
)
158 uint8_t bs_extension_type
= (uint8_t)faad_getbits(ld
, 4
159 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type"));
161 if (bs_extension_type
== EXT_SBR_DATA_CRC
)
163 sbr
->bs_sbr_crc_bits
= (uint16_t)faad_getbits(ld
, 10
164 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits"));
168 /* save old header values, in case the new ones are corrupted */
169 saved_start_freq
= sbr
->bs_start_freq
;
170 saved_samplerate_mode
= sbr
->bs_samplerate_mode
;
171 saved_stop_freq
= sbr
->bs_stop_freq
;
172 saved_freq_scale
= sbr
->bs_freq_scale
;
173 saved_alter_scale
= sbr
->bs_alter_scale
;
174 saved_xover_band
= sbr
->bs_xover_band
;
176 sbr
->bs_header_flag
= faad_get1bit(ld
177 DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag"));
179 if (sbr
->bs_header_flag
)
185 /* first frame should have a header */
186 //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0))
187 if (sbr
->header_count
!= 0)
189 if (sbr
->Reset
|| (sbr
->bs_header_flag
&& sbr
->just_seeked
))
191 uint8_t rt
= calc_sbr_tables(sbr
, sbr
->bs_start_freq
, sbr
->bs_stop_freq
,
192 sbr
->bs_samplerate_mode
, sbr
->bs_freq_scale
,
193 sbr
->bs_alter_scale
, sbr
->bs_xover_band
);
195 /* if an error occured with the new header values revert to the old ones */
198 calc_sbr_tables(sbr
, saved_start_freq
, saved_stop_freq
,
199 saved_samplerate_mode
, saved_freq_scale
,
200 saved_alter_scale
, saved_xover_band
);
206 result
= sbr_data(ld
, sbr
);
208 /* sbr_data() returning an error means that there was an error in
209 envelope_time_border_vector().
210 In this case the old time border vector is saved and all the previous
211 data normally read after sbr_grid() is saved.
213 /* to be on the safe side, calculate old sbr tables in case of error */
215 (sbr
->Reset
|| (sbr
->bs_header_flag
&& sbr
->just_seeked
)))
217 calc_sbr_tables(sbr
, saved_start_freq
, saved_stop_freq
,
218 saved_samplerate_mode
, saved_freq_scale
,
219 saved_alter_scale
, saved_xover_band
);
222 /* we should be able to safely set result to 0 now */
230 if (!sbr
->Is_DRM_SBR
)
233 num_sbr_bits
= (uint16_t)faad_get_processed_bits(ld
) - num_sbr_bits
;
235 /* check if we read more bits then were available for sbr */
236 if (8*cnt
< num_sbr_bits
)
239 /* -4 does not apply, bs_extension_type is re-read in this function */
240 num_align_bits
= 8*cnt
/*- 4*/ - num_sbr_bits
;
242 while (num_align_bits
> 7)
245 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
248 faad_getbits(ld
, num_align_bits
249 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
256 static void sbr_header(bitfile
*ld
, sbr_info
*sbr
)
258 uint8_t bs_header_extra_1
, bs_header_extra_2
;
262 sbr
->bs_amp_res
= faad_get1bit(ld
263 DEBUGVAR(1,203,"sbr_header(): bs_amp_res"));
265 /* bs_start_freq and bs_stop_freq must define a fequency band that does
266 not exceed 48 channels */
267 sbr
->bs_start_freq
= (uint8_t)faad_getbits(ld
, 4
268 DEBUGVAR(1,204,"sbr_header(): bs_start_freq"));
269 sbr
->bs_stop_freq
= (uint8_t)faad_getbits(ld
, 4
270 DEBUGVAR(1,205,"sbr_header(): bs_stop_freq"));
271 sbr
->bs_xover_band
= (uint8_t)faad_getbits(ld
, 3
272 DEBUGVAR(1,206,"sbr_header(): bs_xover_band"));
274 DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr"));
275 bs_header_extra_1
= (uint8_t)faad_get1bit(ld
276 DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1"));
277 bs_header_extra_2
= (uint8_t)faad_get1bit(ld
278 DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2"));
280 if (bs_header_extra_1
)
282 sbr
->bs_freq_scale
= (uint8_t)faad_getbits(ld
, 2
283 DEBUGVAR(1,211,"sbr_header(): bs_freq_scale"));
284 sbr
->bs_alter_scale
= (uint8_t)faad_get1bit(ld
285 DEBUGVAR(1,212,"sbr_header(): bs_alter_scale"));
286 sbr
->bs_noise_bands
= (uint8_t)faad_getbits(ld
, 2
287 DEBUGVAR(1,213,"sbr_header(): bs_noise_bands"));
290 sbr
->bs_freq_scale
= 2;
291 sbr
->bs_alter_scale
= 1;
292 sbr
->bs_noise_bands
= 2;
295 if (bs_header_extra_2
)
297 sbr
->bs_limiter_bands
= (uint8_t)faad_getbits(ld
, 2
298 DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands"));
299 sbr
->bs_limiter_gains
= (uint8_t)faad_getbits(ld
, 2
300 DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains"));
301 sbr
->bs_interpol_freq
= (uint8_t)faad_get1bit(ld
302 DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq"));
303 sbr
->bs_smoothing_mode
= (uint8_t)faad_get1bit(ld
304 DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode"));
307 sbr
->bs_limiter_bands
= 2;
308 sbr
->bs_limiter_gains
= 2;
309 sbr
->bs_interpol_freq
= 1;
310 sbr
->bs_smoothing_mode
= 1;
314 /* print the header to screen */
315 printf("bs_amp_res: %d\n", sbr
->bs_amp_res
);
316 printf("bs_start_freq: %d\n", sbr
->bs_start_freq
);
317 printf("bs_stop_freq: %d\n", sbr
->bs_stop_freq
);
318 printf("bs_xover_band: %d\n", sbr
->bs_xover_band
);
319 if (bs_header_extra_1
)
321 printf("bs_freq_scale: %d\n", sbr
->bs_freq_scale
);
322 printf("bs_alter_scale: %d\n", sbr
->bs_alter_scale
);
323 printf("bs_noise_bands: %d\n", sbr
->bs_noise_bands
);
325 if (bs_header_extra_2
)
327 printf("bs_limiter_bands: %d\n", sbr
->bs_limiter_bands
);
328 printf("bs_limiter_gains: %d\n", sbr
->bs_limiter_gains
);
329 printf("bs_interpol_freq: %d\n", sbr
->bs_interpol_freq
);
330 printf("bs_smoothing_mode: %d\n", sbr
->bs_smoothing_mode
);
337 static uint8_t sbr_data(bitfile
*ld
, sbr_info
*sbr
)
341 sbr
->bs_samplerate_mode
= faad_get1bit(ld
342 DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode"));
345 sbr
->rate
= (sbr
->bs_samplerate_mode
) ? 2 : 1;
350 if ((result
= sbr_single_channel_element(ld
, sbr
)) > 0)
354 if ((result
= sbr_channel_pair_element(ld
, sbr
)) > 0)
363 static uint8_t sbr_single_channel_element(bitfile
*ld
, sbr_info
*sbr
)
368 DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra")))
371 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data"));
375 /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */
380 if ((result
= sbr_grid(ld
, sbr
, 0)) > 0)
382 sbr_dtdf(ld
, sbr
, 0);
383 invf_mode(ld
, sbr
, 0);
384 sbr_envelope(ld
, sbr
, 0);
385 sbr_noise(ld
, sbr
, 0);
388 envelope_noise_dequantisation(sbr
, 0);
391 memset(sbr
->bs_add_harmonic
[0], 0, 64*sizeof(uint8_t));
393 sbr
->bs_add_harmonic_flag
[0] = faad_get1bit(ld
394 DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]"));
395 if (sbr
->bs_add_harmonic_flag
[0])
396 sinusoidal_coding(ld
, sbr
, 0);
398 sbr
->bs_extended_data
= faad_get1bit(ld
399 DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]"));
401 if (sbr
->bs_extended_data
)
403 uint16_t nr_bits_left
;
404 uint16_t cnt
= (uint16_t)faad_getbits(ld
, 4
405 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size"));
408 cnt
+= (uint16_t)faad_getbits(ld
, 8
409 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count"));
412 nr_bits_left
= 8 * cnt
;
413 while (nr_bits_left
> 7)
415 uint16_t tmp_nr_bits
= 0;
417 sbr
->bs_extension_id
= (uint8_t)faad_getbits(ld
, 2
418 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id"));
420 tmp_nr_bits
+= sbr_extension(ld
, sbr
, sbr
->bs_extension_id
, nr_bits_left
);
422 /* check if the data read is bigger than the number of available bits */
423 if (tmp_nr_bits
> nr_bits_left
)
426 nr_bits_left
-= tmp_nr_bits
;
430 if (nr_bits_left
> 0)
432 faad_getbits(ld
, nr_bits_left
433 DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left"));
441 static uint8_t sbr_channel_pair_element(bitfile
*ld
, sbr_info
*sbr
)
446 DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra")))
449 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
451 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
454 sbr
->bs_coupling
= faad_get1bit(ld
455 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling"));
457 if (sbr
->bs_coupling
)
459 if ((result
= sbr_grid(ld
, sbr
, 0)) > 0)
462 /* need to copy some data from left to right */
463 sbr
->bs_frame_class
[1] = sbr
->bs_frame_class
[0];
464 sbr
->L_E
[1] = sbr
->L_E
[0];
465 sbr
->L_Q
[1] = sbr
->L_Q
[0];
466 sbr
->bs_pointer
[1] = sbr
->bs_pointer
[0];
468 for (n
= 0; n
<= sbr
->L_E
[0]; n
++)
470 sbr
->t_E
[1][n
] = sbr
->t_E
[0][n
];
471 sbr
->f
[1][n
] = sbr
->f
[0][n
];
473 for (n
= 0; n
<= sbr
->L_Q
[0]; n
++)
474 sbr
->t_Q
[1][n
] = sbr
->t_Q
[0][n
];
476 sbr_dtdf(ld
, sbr
, 0);
477 sbr_dtdf(ld
, sbr
, 1);
478 invf_mode(ld
, sbr
, 0);
481 for (n
= 0; n
< sbr
->N_Q
; n
++)
482 sbr
->bs_invf_mode
[1][n
] = sbr
->bs_invf_mode
[0][n
];
484 sbr_envelope(ld
, sbr
, 0);
485 sbr_noise(ld
, sbr
, 0);
486 sbr_envelope(ld
, sbr
, 1);
487 sbr_noise(ld
, sbr
, 1);
489 memset(sbr
->bs_add_harmonic
[0], 0, 64*sizeof(uint8_t));
490 memset(sbr
->bs_add_harmonic
[1], 0, 64*sizeof(uint8_t));
492 sbr
->bs_add_harmonic_flag
[0] = faad_get1bit(ld
493 DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
494 if (sbr
->bs_add_harmonic_flag
[0])
495 sinusoidal_coding(ld
, sbr
, 0);
497 sbr
->bs_add_harmonic_flag
[1] = faad_get1bit(ld
498 DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
499 if (sbr
->bs_add_harmonic_flag
[1])
500 sinusoidal_coding(ld
, sbr
, 1);
502 uint8_t saved_t_E
[6] = {0}, saved_t_Q
[3] = {0};
503 uint8_t saved_L_E
= sbr
->L_E
[0];
504 uint8_t saved_L_Q
= sbr
->L_Q
[0];
505 uint8_t saved_frame_class
= sbr
->bs_frame_class
[0];
507 for (n
= 0; n
< saved_L_E
; n
++)
508 saved_t_E
[n
] = sbr
->t_E
[0][n
];
509 for (n
= 0; n
< saved_L_Q
; n
++)
510 saved_t_Q
[n
] = sbr
->t_Q
[0][n
];
512 if ((result
= sbr_grid(ld
, sbr
, 0)) > 0)
514 if ((result
= sbr_grid(ld
, sbr
, 1)) > 0)
516 /* restore first channel data as well */
517 sbr
->bs_frame_class
[0] = saved_frame_class
;
518 sbr
->L_E
[0] = saved_L_E
;
519 sbr
->L_Q
[0] = saved_L_Q
;
520 for (n
= 0; n
< 6; n
++)
521 sbr
->t_E
[0][n
] = saved_t_E
[n
];
522 for (n
= 0; n
< 3; n
++)
523 sbr
->t_Q
[0][n
] = saved_t_Q
[n
];
527 sbr_dtdf(ld
, sbr
, 0);
528 sbr_dtdf(ld
, sbr
, 1);
529 invf_mode(ld
, sbr
, 0);
530 invf_mode(ld
, sbr
, 1);
531 sbr_envelope(ld
, sbr
, 0);
532 sbr_envelope(ld
, sbr
, 1);
533 sbr_noise(ld
, sbr
, 0);
534 sbr_noise(ld
, sbr
, 1);
536 memset(sbr
->bs_add_harmonic
[0], 0, 64*sizeof(uint8_t));
537 memset(sbr
->bs_add_harmonic
[1], 0, 64*sizeof(uint8_t));
539 sbr
->bs_add_harmonic_flag
[0] = faad_get1bit(ld
540 DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
541 if (sbr
->bs_add_harmonic_flag
[0])
542 sinusoidal_coding(ld
, sbr
, 0);
544 sbr
->bs_add_harmonic_flag
[1] = faad_get1bit(ld
545 DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
546 if (sbr
->bs_add_harmonic_flag
[1])
547 sinusoidal_coding(ld
, sbr
, 1);
550 envelope_noise_dequantisation(sbr
, 0);
551 envelope_noise_dequantisation(sbr
, 1);
553 if (sbr
->bs_coupling
)
554 unmap_envelope_noise(sbr
);
557 sbr
->bs_extended_data
= faad_get1bit(ld
558 DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]"));
559 if (sbr
->bs_extended_data
)
561 uint16_t nr_bits_left
;
562 uint16_t cnt
= (uint16_t)faad_getbits(ld
, 4
563 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size"));
566 cnt
+= (uint16_t)faad_getbits(ld
, 8
567 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count"));
570 nr_bits_left
= 8 * cnt
;
571 while (nr_bits_left
> 7)
573 uint16_t tmp_nr_bits
= 0;
575 sbr
->bs_extension_id
= (uint8_t)faad_getbits(ld
, 2
576 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id"));
578 tmp_nr_bits
+= sbr_extension(ld
, sbr
, sbr
->bs_extension_id
, nr_bits_left
);
580 /* check if the data read is bigger than the number of available bits */
581 if (tmp_nr_bits
> nr_bits_left
)
584 nr_bits_left
-= tmp_nr_bits
;
588 if (nr_bits_left
> 0)
590 faad_getbits(ld
, nr_bits_left
591 DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left"));
598 /* integer log[2](x): input range [0,10) */
599 static int8_t sbr_log2(const int8_t val
)
601 int8_t log2tab
[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 };
602 if (val
< 10 && val
>= 0)
610 static uint8_t sbr_grid(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
612 uint8_t i
, env
, rel
, result
;
613 uint8_t bs_abs_bord
, bs_abs_bord_1
;
614 uint8_t bs_num_env
= 0;
615 uint8_t saved_L_E
= sbr
->L_E
[ch
];
616 uint8_t saved_L_Q
= sbr
->L_Q
[ch
];
617 uint8_t saved_frame_class
= sbr
->bs_frame_class
[ch
];
619 sbr
->bs_frame_class
[ch
] = (uint8_t)faad_getbits(ld
, 2
620 DEBUGVAR(1,248,"sbr_grid(): bs_frame_class"));
622 switch (sbr
->bs_frame_class
[ch
])
625 i
= (uint8_t)faad_getbits(ld
, 2
626 DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw"));
628 bs_num_env
= min(1 << i
, 5);
630 i
= (uint8_t)faad_get1bit(ld
631 DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag"));
632 for (env
= 0; env
< bs_num_env
; env
++)
635 sbr
->abs_bord_lead
[ch
] = 0;
636 sbr
->abs_bord_trail
[ch
] = sbr
->numTimeSlots
;
637 sbr
->n_rel_lead
[ch
] = bs_num_env
- 1;
638 sbr
->n_rel_trail
[ch
] = 0;
642 bs_abs_bord
= (uint8_t)faad_getbits(ld
, 2
643 DEBUGVAR(1,251,"sbr_grid(): bs_abs_bord")) + sbr
->numTimeSlots
;
644 bs_num_env
= (uint8_t)faad_getbits(ld
, 2
645 DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1;
647 for (rel
= 0; rel
< bs_num_env
-1; rel
++)
649 sbr
->bs_rel_bord
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
650 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2;
652 i
= sbr_log2(bs_num_env
+ 1);
653 sbr
->bs_pointer
[ch
] = (uint8_t)faad_getbits(ld
, i
654 DEBUGVAR(1,254,"sbr_grid(): bs_pointer"));
656 for (env
= 0; env
< bs_num_env
; env
++)
658 sbr
->f
[ch
][bs_num_env
- env
- 1] = (uint8_t)faad_get1bit(ld
659 DEBUGVAR(1,255,"sbr_grid(): bs_freq_res"));
662 sbr
->abs_bord_lead
[ch
] = 0;
663 sbr
->abs_bord_trail
[ch
] = bs_abs_bord
;
664 sbr
->n_rel_lead
[ch
] = 0;
665 sbr
->n_rel_trail
[ch
] = bs_num_env
- 1;
669 bs_abs_bord
= (uint8_t)faad_getbits(ld
, 2
670 DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord"));
671 bs_num_env
= (uint8_t)faad_getbits(ld
, 2
672 DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1;
674 for (rel
= 0; rel
< bs_num_env
-1; rel
++)
676 sbr
->bs_rel_bord
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
677 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2;
679 i
= sbr_log2(bs_num_env
+ 1);
680 sbr
->bs_pointer
[ch
] = (uint8_t)faad_getbits(ld
, i
681 DEBUGVAR(1,259,"sbr_grid(): bs_pointer"));
683 for (env
= 0; env
< bs_num_env
; env
++)
685 sbr
->f
[ch
][env
] = (uint8_t)faad_get1bit(ld
686 DEBUGVAR(1,260,"sbr_grid(): bs_freq_res"));
689 sbr
->abs_bord_lead
[ch
] = bs_abs_bord
;
690 sbr
->abs_bord_trail
[ch
] = sbr
->numTimeSlots
;
691 sbr
->n_rel_lead
[ch
] = bs_num_env
- 1;
692 sbr
->n_rel_trail
[ch
] = 0;
696 bs_abs_bord
= (uint8_t)faad_getbits(ld
, 2
697 DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0"));
698 bs_abs_bord_1
= (uint8_t)faad_getbits(ld
, 2
699 DEBUGVAR(1,262,"sbr_grid(): bs_abs_bord_1")) + sbr
->numTimeSlots
;
700 sbr
->bs_num_rel_0
[ch
] = (uint8_t)faad_getbits(ld
, 2
701 DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0"));
702 sbr
->bs_num_rel_1
[ch
] = (uint8_t)faad_getbits(ld
, 2
703 DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1"));
705 bs_num_env
= min(5, sbr
->bs_num_rel_0
[ch
] + sbr
->bs_num_rel_1
[ch
] + 1);
707 for (rel
= 0; rel
< sbr
->bs_num_rel_0
[ch
]; rel
++)
709 sbr
->bs_rel_bord_0
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
710 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2;
712 for(rel
= 0; rel
< sbr
->bs_num_rel_1
[ch
]; rel
++)
714 sbr
->bs_rel_bord_1
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
715 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2;
717 i
= sbr_log2(sbr
->bs_num_rel_0
[ch
] + sbr
->bs_num_rel_1
[ch
] + 2);
718 sbr
->bs_pointer
[ch
] = (uint8_t)faad_getbits(ld
, i
719 DEBUGVAR(1,267,"sbr_grid(): bs_pointer"));
721 for (env
= 0; env
< bs_num_env
; env
++)
723 sbr
->f
[ch
][env
] = (uint8_t)faad_get1bit(ld
724 DEBUGVAR(1,268,"sbr_grid(): bs_freq_res"));
727 sbr
->abs_bord_lead
[ch
] = bs_abs_bord
;
728 sbr
->abs_bord_trail
[ch
] = bs_abs_bord_1
;
729 sbr
->n_rel_lead
[ch
] = sbr
->bs_num_rel_0
[ch
];
730 sbr
->n_rel_trail
[ch
] = sbr
->bs_num_rel_1
[ch
];
734 if (sbr
->bs_frame_class
[ch
] == VARVAR
)
735 sbr
->L_E
[ch
] = min(bs_num_env
, 5);
737 sbr
->L_E
[ch
] = min(bs_num_env
, 4);
739 if (sbr
->L_E
[ch
] <= 0)
742 if (sbr
->L_E
[ch
] > 1)
747 /* TODO: this code can probably be integrated into the code above! */
748 if ((result
= envelope_time_border_vector(sbr
, ch
)) > 0)
750 sbr
->bs_frame_class
[ch
] = saved_frame_class
;
751 sbr
->L_E
[ch
] = saved_L_E
;
752 sbr
->L_Q
[ch
] = saved_L_Q
;
755 noise_floor_time_border_vector(sbr
, ch
);
758 for (env
= 0; env
< bs_num_env
; env
++)
760 printf("freq_res[ch:%d][env:%d]: %d\n", ch
, env
, sbr
->f
[ch
][env
]);
768 static void sbr_dtdf(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
772 for (i
= 0; i
< sbr
->L_E
[ch
]; i
++)
774 sbr
->bs_df_env
[ch
][i
] = faad_get1bit(ld
775 DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env"));
778 for (i
= 0; i
< sbr
->L_Q
[ch
]; i
++)
780 sbr
->bs_df_noise
[ch
][i
] = faad_get1bit(ld
781 DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise"));
786 static void invf_mode(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
790 for (n
= 0; n
< sbr
->N_Q
; n
++)
792 sbr
->bs_invf_mode
[ch
][n
] = (uint8_t)faad_getbits(ld
, 2
793 DEBUGVAR(1,271,"invf_mode(): bs_invf_mode"));
797 static uint16_t sbr_extension(bitfile
*ld
, sbr_info
*sbr
,
798 uint8_t bs_extension_id
, uint16_t num_bits_left
)
800 switch (bs_extension_id
)
803 case EXTENSION_ID_PS
:
807 sbr
->ps
= ps_init(get_sr_index(sbr
->sample_rate
));
809 return ps_data(sbr
->ps
, ld
);
812 case DRM_PARAMETRIC_STEREO
:
816 sbr
->drm_ps
= drm_ps_init();
818 return drm_ps_data(sbr
->drm_ps
, ld
);
821 sbr
->bs_extension_data
= (uint8_t)faad_getbits(ld
, 6
822 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
828 static void sinusoidal_coding(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
832 for (n
= 0; n
< sbr
->N_high
; n
++)
834 sbr
->bs_add_harmonic
[ch
][n
] = faad_get1bit(ld
835 DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic"));