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.
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
) ||
80 (sbr
->bs_xover_band
!= sbr
->bs_xover_band_prev
) ||
81 (sbr
->bs_noise_bands
!= sbr
->bs_noise_bands_prev
))
88 sbr
->bs_start_freq_prev
= sbr
->bs_start_freq
;
89 sbr
->bs_stop_freq_prev
= sbr
->bs_stop_freq
;
90 sbr
->bs_freq_scale_prev
= sbr
->bs_freq_scale
;
91 sbr
->bs_alter_scale_prev
= sbr
->bs_alter_scale
;
92 sbr
->bs_xover_band_prev
= sbr
->bs_xover_band
;
93 sbr
->bs_noise_bands_prev
= sbr
->bs_noise_bands
;
96 static uint8_t calc_sbr_tables(sbr_info
*sbr
, uint8_t start_freq
, uint8_t stop_freq
,
97 uint8_t samplerate_mode
, uint8_t freq_scale
,
98 uint8_t alter_scale
, uint8_t xover_band
)
103 /* calculate the Master Frequency Table */
104 sbr
->k0
= qmf_start_channel(start_freq
, samplerate_mode
, sbr
->sample_rate
);
105 k2
= qmf_stop_channel(stop_freq
, sbr
->sample_rate
, sbr
->k0
);
107 /* check k0 and k2 */
108 if (sbr
->sample_rate
>= 48000)
110 if ((k2
- sbr
->k0
) > 32)
112 } else if (sbr
->sample_rate
<= 32000) {
113 if ((k2
- sbr
->k0
) > 48)
115 } else { /* (sbr->sample_rate == 44100) */
116 if ((k2
- sbr
->k0
) > 45)
122 result
+= master_frequency_table_fs0(sbr
, sbr
->k0
, k2
, alter_scale
);
124 result
+= master_frequency_table(sbr
, sbr
->k0
, k2
, freq_scale
, alter_scale
);
126 result
+= derived_frequency_table(sbr
, xover_band
, k2
);
128 result
= (result
> 0) ? 1 : 0;
134 uint8_t sbr_extension_data(bitfile
*ld
, sbr_info
*sbr
, uint16_t cnt
)
137 uint16_t num_align_bits
= 0;
138 uint16_t num_sbr_bits
= (uint16_t)faad_get_processed_bits(ld
);
140 uint8_t saved_start_freq
, saved_samplerate_mode
;
141 uint8_t saved_stop_freq
, saved_freq_scale
;
142 uint8_t saved_alter_scale
, saved_xover_band
;
145 if (!sbr
->Is_DRM_SBR
)
148 uint8_t bs_extension_type
= (uint8_t)faad_getbits(ld
, 4
149 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type"));
151 if (bs_extension_type
== EXT_SBR_DATA_CRC
)
153 sbr
->bs_sbr_crc_bits
= (uint16_t)faad_getbits(ld
, 10
154 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits"));
158 /* save old header values, in case the new ones are corrupted */
159 saved_start_freq
= sbr
->bs_start_freq
;
160 saved_samplerate_mode
= sbr
->bs_samplerate_mode
;
161 saved_stop_freq
= sbr
->bs_stop_freq
;
162 saved_freq_scale
= sbr
->bs_freq_scale
;
163 saved_alter_scale
= sbr
->bs_alter_scale
;
164 saved_xover_band
= sbr
->bs_xover_band
;
166 sbr
->bs_header_flag
= faad_get1bit(ld
167 DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag"));
169 if (sbr
->bs_header_flag
)
175 /* first frame should have a header */
176 //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0))
177 if (sbr
->header_count
!= 0)
179 if (sbr
->Reset
|| (sbr
->bs_header_flag
&& sbr
->just_seeked
))
181 uint8_t rt
= calc_sbr_tables(sbr
, sbr
->bs_start_freq
, sbr
->bs_stop_freq
,
182 sbr
->bs_samplerate_mode
, sbr
->bs_freq_scale
,
183 sbr
->bs_alter_scale
, sbr
->bs_xover_band
);
185 /* if an error occured with the new header values revert to the old ones */
188 calc_sbr_tables(sbr
, saved_start_freq
, saved_stop_freq
,
189 saved_samplerate_mode
, saved_freq_scale
,
190 saved_alter_scale
, saved_xover_band
);
196 result
= sbr_data(ld
, sbr
);
198 /* sbr_data() returning an error means that there was an error in
199 envelope_time_border_vector().
200 In this case the old time border vector is saved and all the previous
201 data normally read after sbr_grid() is saved.
203 /* to be on the safe side, calculate old sbr tables in case of error */
205 (sbr
->Reset
|| (sbr
->bs_header_flag
&& sbr
->just_seeked
)))
207 calc_sbr_tables(sbr
, saved_start_freq
, saved_stop_freq
,
208 saved_samplerate_mode
, saved_freq_scale
,
209 saved_alter_scale
, saved_xover_band
);
212 /* we should be able to safely set result to 0 now */
220 if (!sbr
->Is_DRM_SBR
)
223 num_sbr_bits
= (uint16_t)faad_get_processed_bits(ld
) - num_sbr_bits
;
225 /* check if we read more bits then were available for sbr */
226 if (8*cnt
< num_sbr_bits
)
229 /* -4 does not apply, bs_extension_type is re-read in this function */
230 num_align_bits
= 8*cnt
/*- 4*/ - num_sbr_bits
;
232 while (num_align_bits
> 7)
235 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
238 faad_getbits(ld
, num_align_bits
239 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
246 static void sbr_header(bitfile
*ld
, sbr_info
*sbr
)
248 uint8_t bs_header_extra_1
, bs_header_extra_2
;
252 sbr
->bs_amp_res
= faad_get1bit(ld
253 DEBUGVAR(1,203,"sbr_header(): bs_amp_res"));
255 /* bs_start_freq and bs_stop_freq must define a fequency band that does
256 not exceed 48 channels */
257 sbr
->bs_start_freq
= (uint8_t)faad_getbits(ld
, 4
258 DEBUGVAR(1,204,"sbr_header(): bs_start_freq"));
259 sbr
->bs_stop_freq
= (uint8_t)faad_getbits(ld
, 4
260 DEBUGVAR(1,205,"sbr_header(): bs_stop_freq"));
261 sbr
->bs_xover_band
= (uint8_t)faad_getbits(ld
, 3
262 DEBUGVAR(1,206,"sbr_header(): bs_xover_band"));
264 DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr"));
265 bs_header_extra_1
= (uint8_t)faad_get1bit(ld
266 DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1"));
267 bs_header_extra_2
= (uint8_t)faad_get1bit(ld
268 DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2"));
270 if (bs_header_extra_1
)
272 sbr
->bs_freq_scale
= (uint8_t)faad_getbits(ld
, 2
273 DEBUGVAR(1,211,"sbr_header(): bs_freq_scale"));
274 sbr
->bs_alter_scale
= (uint8_t)faad_get1bit(ld
275 DEBUGVAR(1,212,"sbr_header(): bs_alter_scale"));
276 sbr
->bs_noise_bands
= (uint8_t)faad_getbits(ld
, 2
277 DEBUGVAR(1,213,"sbr_header(): bs_noise_bands"));
280 sbr
->bs_freq_scale
= 2;
281 sbr
->bs_alter_scale
= 1;
282 sbr
->bs_noise_bands
= 2;
285 if (bs_header_extra_2
)
287 sbr
->bs_limiter_bands
= (uint8_t)faad_getbits(ld
, 2
288 DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands"));
289 sbr
->bs_limiter_gains
= (uint8_t)faad_getbits(ld
, 2
290 DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains"));
291 sbr
->bs_interpol_freq
= (uint8_t)faad_get1bit(ld
292 DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq"));
293 sbr
->bs_smoothing_mode
= (uint8_t)faad_get1bit(ld
294 DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode"));
297 sbr
->bs_limiter_bands
= 2;
298 sbr
->bs_limiter_gains
= 2;
299 sbr
->bs_interpol_freq
= 1;
300 sbr
->bs_smoothing_mode
= 1;
304 /* print the header to screen */
305 printf("bs_amp_res: %d\n", sbr
->bs_amp_res
);
306 printf("bs_start_freq: %d\n", sbr
->bs_start_freq
);
307 printf("bs_stop_freq: %d\n", sbr
->bs_stop_freq
);
308 printf("bs_xover_band: %d\n", sbr
->bs_xover_band
);
309 if (bs_header_extra_1
)
311 printf("bs_freq_scale: %d\n", sbr
->bs_freq_scale
);
312 printf("bs_alter_scale: %d\n", sbr
->bs_alter_scale
);
313 printf("bs_noise_bands: %d\n", sbr
->bs_noise_bands
);
315 if (bs_header_extra_2
)
317 printf("bs_limiter_bands: %d\n", sbr
->bs_limiter_bands
);
318 printf("bs_limiter_gains: %d\n", sbr
->bs_limiter_gains
);
319 printf("bs_interpol_freq: %d\n", sbr
->bs_interpol_freq
);
320 printf("bs_smoothing_mode: %d\n", sbr
->bs_smoothing_mode
);
327 static uint8_t sbr_data(bitfile
*ld
, sbr_info
*sbr
)
331 sbr
->bs_samplerate_mode
= faad_get1bit(ld
332 DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode"));
335 sbr
->rate
= (sbr
->bs_samplerate_mode
) ? 2 : 1;
340 if ((result
= sbr_single_channel_element(ld
, sbr
)) > 0)
344 if ((result
= sbr_channel_pair_element(ld
, sbr
)) > 0)
353 static uint8_t sbr_single_channel_element(bitfile
*ld
, sbr_info
*sbr
)
358 DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra")))
361 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data"));
365 /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */
370 if ((result
= sbr_grid(ld
, sbr
, 0)) > 0)
372 sbr_dtdf(ld
, sbr
, 0);
373 invf_mode(ld
, sbr
, 0);
374 sbr_envelope(ld
, sbr
, 0);
375 sbr_noise(ld
, sbr
, 0);
378 envelope_noise_dequantisation(sbr
, 0);
381 memset(sbr
->bs_add_harmonic
[0], 0, 64*sizeof(uint8_t));
383 sbr
->bs_add_harmonic_flag
[0] = faad_get1bit(ld
384 DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]"));
385 if (sbr
->bs_add_harmonic_flag
[0])
386 sinusoidal_coding(ld
, sbr
, 0);
388 sbr
->bs_extended_data
= faad_get1bit(ld
389 DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]"));
391 if (sbr
->bs_extended_data
)
393 uint16_t nr_bits_left
;
394 #if (defined(PS_DEC) || defined(DRM_PS))
395 uint8_t ps_ext_read
= 0;
397 uint16_t cnt
= (uint16_t)faad_getbits(ld
, 4
398 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size"));
401 cnt
+= (uint16_t)faad_getbits(ld
, 8
402 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count"));
405 nr_bits_left
= 8 * cnt
;
406 while (nr_bits_left
> 7)
408 uint16_t tmp_nr_bits
= 0;
410 sbr
->bs_extension_id
= (uint8_t)faad_getbits(ld
, 2
411 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id"));
414 /* allow only 1 PS extension element per extension data */
415 #if (defined(PS_DEC) || defined(DRM_PS))
416 #if (defined(PS_DEC) && defined(DRM_PS))
417 if (sbr
->bs_extension_id
== EXTENSION_ID_PS
|| sbr
->bs_extension_id
== DRM_PARAMETRIC_STEREO
)
420 if (sbr
->bs_extension_id
== EXTENSION_ID_PS
)
423 if (sbr
->bs_extension_id
== DRM_PARAMETRIC_STEREO
)
428 if (ps_ext_read
== 0)
432 /* to be safe make it 3, will switch to "default"
433 * in sbr_extension() */
434 sbr
->bs_extension_id
= 3;
439 tmp_nr_bits
+= sbr_extension(ld
, sbr
, sbr
->bs_extension_id
, nr_bits_left
);
441 /* check if the data read is bigger than the number of available bits */
442 if (tmp_nr_bits
> nr_bits_left
)
445 nr_bits_left
-= tmp_nr_bits
;
449 if (nr_bits_left
> 0)
451 faad_getbits(ld
, nr_bits_left
452 DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left"));
460 static uint8_t sbr_channel_pair_element(bitfile
*ld
, sbr_info
*sbr
)
465 DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra")))
468 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
470 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
473 sbr
->bs_coupling
= faad_get1bit(ld
474 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling"));
476 if (sbr
->bs_coupling
)
478 if ((result
= sbr_grid(ld
, sbr
, 0)) > 0)
481 /* need to copy some data from left to right */
482 sbr
->bs_frame_class
[1] = sbr
->bs_frame_class
[0];
483 sbr
->L_E
[1] = sbr
->L_E
[0];
484 sbr
->L_Q
[1] = sbr
->L_Q
[0];
485 sbr
->bs_pointer
[1] = sbr
->bs_pointer
[0];
487 for (n
= 0; n
<= sbr
->L_E
[0]; n
++)
489 sbr
->t_E
[1][n
] = sbr
->t_E
[0][n
];
490 sbr
->f
[1][n
] = sbr
->f
[0][n
];
492 for (n
= 0; n
<= sbr
->L_Q
[0]; n
++)
493 sbr
->t_Q
[1][n
] = sbr
->t_Q
[0][n
];
495 sbr_dtdf(ld
, sbr
, 0);
496 sbr_dtdf(ld
, sbr
, 1);
497 invf_mode(ld
, sbr
, 0);
500 for (n
= 0; n
< sbr
->N_Q
; n
++)
501 sbr
->bs_invf_mode
[1][n
] = sbr
->bs_invf_mode
[0][n
];
503 sbr_envelope(ld
, sbr
, 0);
504 sbr_noise(ld
, sbr
, 0);
505 sbr_envelope(ld
, sbr
, 1);
506 sbr_noise(ld
, sbr
, 1);
508 memset(sbr
->bs_add_harmonic
[0], 0, 64*sizeof(uint8_t));
509 memset(sbr
->bs_add_harmonic
[1], 0, 64*sizeof(uint8_t));
511 sbr
->bs_add_harmonic_flag
[0] = faad_get1bit(ld
512 DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
513 if (sbr
->bs_add_harmonic_flag
[0])
514 sinusoidal_coding(ld
, sbr
, 0);
516 sbr
->bs_add_harmonic_flag
[1] = faad_get1bit(ld
517 DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
518 if (sbr
->bs_add_harmonic_flag
[1])
519 sinusoidal_coding(ld
, sbr
, 1);
521 uint8_t saved_t_E
[6] = {0}, saved_t_Q
[3] = {0};
522 uint8_t saved_L_E
= sbr
->L_E
[0];
523 uint8_t saved_L_Q
= sbr
->L_Q
[0];
524 uint8_t saved_frame_class
= sbr
->bs_frame_class
[0];
526 for (n
= 0; n
< saved_L_E
; n
++)
527 saved_t_E
[n
] = sbr
->t_E
[0][n
];
528 for (n
= 0; n
< saved_L_Q
; n
++)
529 saved_t_Q
[n
] = sbr
->t_Q
[0][n
];
531 if ((result
= sbr_grid(ld
, sbr
, 0)) > 0)
533 if ((result
= sbr_grid(ld
, sbr
, 1)) > 0)
535 /* restore first channel data as well */
536 sbr
->bs_frame_class
[0] = saved_frame_class
;
537 sbr
->L_E
[0] = saved_L_E
;
538 sbr
->L_Q
[0] = saved_L_Q
;
539 for (n
= 0; n
< 6; n
++)
540 sbr
->t_E
[0][n
] = saved_t_E
[n
];
541 for (n
= 0; n
< 3; n
++)
542 sbr
->t_Q
[0][n
] = saved_t_Q
[n
];
546 sbr_dtdf(ld
, sbr
, 0);
547 sbr_dtdf(ld
, sbr
, 1);
548 invf_mode(ld
, sbr
, 0);
549 invf_mode(ld
, sbr
, 1);
550 sbr_envelope(ld
, sbr
, 0);
551 sbr_envelope(ld
, sbr
, 1);
552 sbr_noise(ld
, sbr
, 0);
553 sbr_noise(ld
, sbr
, 1);
555 memset(sbr
->bs_add_harmonic
[0], 0, 64*sizeof(uint8_t));
556 memset(sbr
->bs_add_harmonic
[1], 0, 64*sizeof(uint8_t));
558 sbr
->bs_add_harmonic_flag
[0] = faad_get1bit(ld
559 DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
560 if (sbr
->bs_add_harmonic_flag
[0])
561 sinusoidal_coding(ld
, sbr
, 0);
563 sbr
->bs_add_harmonic_flag
[1] = faad_get1bit(ld
564 DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
565 if (sbr
->bs_add_harmonic_flag
[1])
566 sinusoidal_coding(ld
, sbr
, 1);
569 envelope_noise_dequantisation(sbr
, 0);
570 envelope_noise_dequantisation(sbr
, 1);
572 if (sbr
->bs_coupling
)
573 unmap_envelope_noise(sbr
);
576 sbr
->bs_extended_data
= faad_get1bit(ld
577 DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]"));
578 if (sbr
->bs_extended_data
)
580 uint16_t nr_bits_left
;
581 uint16_t cnt
= (uint16_t)faad_getbits(ld
, 4
582 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size"));
585 cnt
+= (uint16_t)faad_getbits(ld
, 8
586 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count"));
589 nr_bits_left
= 8 * cnt
;
590 while (nr_bits_left
> 7)
592 uint16_t tmp_nr_bits
= 0;
594 sbr
->bs_extension_id
= (uint8_t)faad_getbits(ld
, 2
595 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id"));
597 tmp_nr_bits
+= sbr_extension(ld
, sbr
, sbr
->bs_extension_id
, nr_bits_left
);
599 /* check if the data read is bigger than the number of available bits */
600 if (tmp_nr_bits
> nr_bits_left
)
603 nr_bits_left
-= tmp_nr_bits
;
607 if (nr_bits_left
> 0)
609 faad_getbits(ld
, nr_bits_left
610 DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left"));
617 /* integer log[2](x): input range [0,10) */
618 static int8_t sbr_log2(const int8_t val
)
620 int8_t log2tab
[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 };
621 if (val
< 10 && val
>= 0)
629 static uint8_t sbr_grid(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
631 uint8_t i
, env
, rel
, result
;
632 uint8_t bs_abs_bord
, bs_abs_bord_1
;
633 uint8_t bs_num_env
= 0;
634 uint8_t saved_L_E
= sbr
->L_E
[ch
];
635 uint8_t saved_L_Q
= sbr
->L_Q
[ch
];
636 uint8_t saved_frame_class
= sbr
->bs_frame_class
[ch
];
638 sbr
->bs_frame_class
[ch
] = (uint8_t)faad_getbits(ld
, 2
639 DEBUGVAR(1,248,"sbr_grid(): bs_frame_class"));
641 switch (sbr
->bs_frame_class
[ch
])
644 i
= (uint8_t)faad_getbits(ld
, 2
645 DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw"));
647 bs_num_env
= min(1 << i
, 5);
649 i
= (uint8_t)faad_get1bit(ld
650 DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag"));
651 for (env
= 0; env
< bs_num_env
; env
++)
654 sbr
->abs_bord_lead
[ch
] = 0;
655 sbr
->abs_bord_trail
[ch
] = sbr
->numTimeSlots
;
656 sbr
->n_rel_lead
[ch
] = bs_num_env
- 1;
657 sbr
->n_rel_trail
[ch
] = 0;
661 bs_abs_bord
= (uint8_t)faad_getbits(ld
, 2
662 DEBUGVAR(1,251,"sbr_grid(): bs_abs_bord")) + sbr
->numTimeSlots
;
663 bs_num_env
= (uint8_t)faad_getbits(ld
, 2
664 DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1;
666 for (rel
= 0; rel
< bs_num_env
-1; rel
++)
668 sbr
->bs_rel_bord
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
669 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2;
671 i
= sbr_log2(bs_num_env
+ 1);
672 sbr
->bs_pointer
[ch
] = (uint8_t)faad_getbits(ld
, i
673 DEBUGVAR(1,254,"sbr_grid(): bs_pointer"));
675 for (env
= 0; env
< bs_num_env
; env
++)
677 sbr
->f
[ch
][bs_num_env
- env
- 1] = (uint8_t)faad_get1bit(ld
678 DEBUGVAR(1,255,"sbr_grid(): bs_freq_res"));
681 sbr
->abs_bord_lead
[ch
] = 0;
682 sbr
->abs_bord_trail
[ch
] = bs_abs_bord
;
683 sbr
->n_rel_lead
[ch
] = 0;
684 sbr
->n_rel_trail
[ch
] = bs_num_env
- 1;
688 bs_abs_bord
= (uint8_t)faad_getbits(ld
, 2
689 DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord"));
690 bs_num_env
= (uint8_t)faad_getbits(ld
, 2
691 DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1;
693 for (rel
= 0; rel
< bs_num_env
-1; rel
++)
695 sbr
->bs_rel_bord
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
696 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2;
698 i
= sbr_log2(bs_num_env
+ 1);
699 sbr
->bs_pointer
[ch
] = (uint8_t)faad_getbits(ld
, i
700 DEBUGVAR(1,259,"sbr_grid(): bs_pointer"));
702 for (env
= 0; env
< bs_num_env
; env
++)
704 sbr
->f
[ch
][env
] = (uint8_t)faad_get1bit(ld
705 DEBUGVAR(1,260,"sbr_grid(): bs_freq_res"));
708 sbr
->abs_bord_lead
[ch
] = bs_abs_bord
;
709 sbr
->abs_bord_trail
[ch
] = sbr
->numTimeSlots
;
710 sbr
->n_rel_lead
[ch
] = bs_num_env
- 1;
711 sbr
->n_rel_trail
[ch
] = 0;
715 bs_abs_bord
= (uint8_t)faad_getbits(ld
, 2
716 DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0"));
717 bs_abs_bord_1
= (uint8_t)faad_getbits(ld
, 2
718 DEBUGVAR(1,262,"sbr_grid(): bs_abs_bord_1")) + sbr
->numTimeSlots
;
719 sbr
->bs_num_rel_0
[ch
] = (uint8_t)faad_getbits(ld
, 2
720 DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0"));
721 sbr
->bs_num_rel_1
[ch
] = (uint8_t)faad_getbits(ld
, 2
722 DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1"));
724 bs_num_env
= min(5, sbr
->bs_num_rel_0
[ch
] + sbr
->bs_num_rel_1
[ch
] + 1);
726 for (rel
= 0; rel
< sbr
->bs_num_rel_0
[ch
]; rel
++)
728 sbr
->bs_rel_bord_0
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
729 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2;
731 for(rel
= 0; rel
< sbr
->bs_num_rel_1
[ch
]; rel
++)
733 sbr
->bs_rel_bord_1
[ch
][rel
] = 2 * (uint8_t)faad_getbits(ld
, 2
734 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2;
736 i
= sbr_log2(sbr
->bs_num_rel_0
[ch
] + sbr
->bs_num_rel_1
[ch
] + 2);
737 sbr
->bs_pointer
[ch
] = (uint8_t)faad_getbits(ld
, i
738 DEBUGVAR(1,267,"sbr_grid(): bs_pointer"));
740 for (env
= 0; env
< bs_num_env
; env
++)
742 sbr
->f
[ch
][env
] = (uint8_t)faad_get1bit(ld
743 DEBUGVAR(1,268,"sbr_grid(): bs_freq_res"));
746 sbr
->abs_bord_lead
[ch
] = bs_abs_bord
;
747 sbr
->abs_bord_trail
[ch
] = bs_abs_bord_1
;
748 sbr
->n_rel_lead
[ch
] = sbr
->bs_num_rel_0
[ch
];
749 sbr
->n_rel_trail
[ch
] = sbr
->bs_num_rel_1
[ch
];
753 if (sbr
->bs_frame_class
[ch
] == VARVAR
)
754 sbr
->L_E
[ch
] = min(bs_num_env
, 5);
756 sbr
->L_E
[ch
] = min(bs_num_env
, 4);
758 if (sbr
->L_E
[ch
] <= 0)
761 if (sbr
->L_E
[ch
] > 1)
766 /* TODO: this code can probably be integrated into the code above! */
767 if ((result
= envelope_time_border_vector(sbr
, ch
)) > 0)
769 sbr
->bs_frame_class
[ch
] = saved_frame_class
;
770 sbr
->L_E
[ch
] = saved_L_E
;
771 sbr
->L_Q
[ch
] = saved_L_Q
;
774 noise_floor_time_border_vector(sbr
, ch
);
777 for (env
= 0; env
< bs_num_env
; env
++)
779 printf("freq_res[ch:%d][env:%d]: %d\n", ch
, env
, sbr
->f
[ch
][env
]);
787 static void sbr_dtdf(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
791 for (i
= 0; i
< sbr
->L_E
[ch
]; i
++)
793 sbr
->bs_df_env
[ch
][i
] = faad_get1bit(ld
794 DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env"));
797 for (i
= 0; i
< sbr
->L_Q
[ch
]; i
++)
799 sbr
->bs_df_noise
[ch
][i
] = faad_get1bit(ld
800 DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise"));
805 static void invf_mode(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
809 for (n
= 0; n
< sbr
->N_Q
; n
++)
811 sbr
->bs_invf_mode
[ch
][n
] = (uint8_t)faad_getbits(ld
, 2
812 DEBUGVAR(1,271,"invf_mode(): bs_invf_mode"));
816 static uint16_t sbr_extension(bitfile
*ld
, sbr_info
*sbr
,
817 uint8_t bs_extension_id
, uint16_t num_bits_left
)
825 switch (bs_extension_id
)
828 case EXTENSION_ID_PS
:
831 sbr
->ps
= ps_init(get_sr_index(sbr
->sample_rate
));
833 ret
= ps_data(sbr
->ps
, ld
, &header
);
835 /* enable PS if and only if: a header has been decoded */
836 if (sbr
->ps_used
== 0 && header
== 1)
844 case DRM_PARAMETRIC_STEREO
:
848 sbr
->drm_ps
= drm_ps_init();
850 return drm_ps_data(sbr
->drm_ps
, ld
);
853 sbr
->bs_extension_data
= (uint8_t)faad_getbits(ld
, 6
854 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
860 static void sinusoidal_coding(bitfile
*ld
, sbr_info
*sbr
, uint8_t ch
)
864 for (n
= 0; n
< sbr
->N_high
; n
++)
866 sbr
->bs_add_harmonic
[ch
][n
] = faad_get1bit(ld
867 DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic"));