Mercurial > mplayer.hg
annotate libfaad2/sbr_syntax.c @ 24734:09f9d0de17f1
Add support for AC-3/DTS passthrough.
patch by Ulion, ulion2002 gmail com
author | nplourde |
---|---|
date | Thu, 11 Oct 2007 02:00:05 +0000 |
parents | 59b6fa5b4201 |
children | e83eef58b30a |
rev | line source |
---|---|
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 ** | |
18141 | 25 ** $Id: sbr_syntax.c,v 1.34 2004/09/04 14:56:28 menno 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); | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
50 static uint8_t calc_sbr_tables(sbr_info *sbr, uint8_t start_freq, uint8_t stop_freq, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
51 uint8_t samplerate_mode, uint8_t freq_scale, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
52 uint8_t alter_scale, uint8_t xover_band); |
12527 | 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); | |
62 | |
63 | |
10725 | 64 static void sbr_reset(sbr_info *sbr) |
65 { | |
12527 | 66 #if 0 |
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); | |
73 #endif | |
74 | |
10725 | 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)) | |
82 { | |
83 sbr->Reset = 1; | |
84 } else { | |
85 sbr->Reset = 0; | |
86 } | |
87 | |
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; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
94 } |
10725 | 95 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
96 static uint8_t calc_sbr_tables(sbr_info *sbr, uint8_t start_freq, uint8_t stop_freq, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
97 uint8_t samplerate_mode, uint8_t freq_scale, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
98 uint8_t alter_scale, uint8_t xover_band) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
99 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
100 uint8_t result = 0; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
101 uint8_t k2; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
102 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
103 /* calculate the Master Frequency Table */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
104 sbr->k0 = qmf_start_channel(start_freq, samplerate_mode, sbr->sample_rate); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
105 k2 = qmf_stop_channel(stop_freq, sbr->sample_rate, sbr->k0); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
106 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
107 /* check k0 and k2 */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
108 if (sbr->sample_rate >= 48000) |
10725 | 109 { |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
110 if ((k2 - sbr->k0) > 32) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
111 result += 1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
112 } else if (sbr->sample_rate <= 32000) { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
113 if ((k2 - sbr->k0) > 48) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
114 result += 1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
115 } else { /* (sbr->sample_rate == 44100) */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
116 if ((k2 - sbr->k0) > 45) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
117 result += 1; |
10725 | 118 } |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
119 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
120 if (freq_scale == 0) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
121 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
122 result += master_frequency_table_fs0(sbr, sbr->k0, k2, alter_scale); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
123 } else { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
124 result += master_frequency_table(sbr, sbr->k0, k2, freq_scale, alter_scale); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
125 } |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
126 result += derived_frequency_table(sbr, xover_band, k2); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
127 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
128 result = (result > 0) ? 1 : 0; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
129 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
130 return result; |
10725 | 131 } |
132 | |
133 /* table 2 */ | |
12527 | 134 uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt) |
10725 | 135 { |
12527 | 136 uint8_t result = 0; |
137 uint16_t num_align_bits = 0; | |
138 uint16_t num_sbr_bits = (uint16_t)faad_get_processed_bits(ld); | |
139 | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
140 uint8_t saved_start_freq, saved_samplerate_mode; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
141 uint8_t saved_stop_freq, saved_freq_scale; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
142 uint8_t saved_alter_scale, saved_xover_band; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
143 |
10989 | 144 #ifdef DRM |
12527 | 145 if (!sbr->Is_DRM_SBR) |
10989 | 146 #endif |
147 { | |
12527 | 148 uint8_t bs_extension_type = (uint8_t)faad_getbits(ld, 4 |
149 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type")); | |
10725 | 150 |
12527 | 151 if (bs_extension_type == EXT_SBR_DATA_CRC) |
152 { | |
153 sbr->bs_sbr_crc_bits = (uint16_t)faad_getbits(ld, 10 | |
154 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits")); | |
155 } | |
10989 | 156 } |
10725 | 157 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
158 /* save old header values, in case the new ones are corrupted */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
159 saved_start_freq = sbr->bs_start_freq; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
160 saved_samplerate_mode = sbr->bs_samplerate_mode; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
161 saved_stop_freq = sbr->bs_stop_freq; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
162 saved_freq_scale = sbr->bs_freq_scale; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
163 saved_alter_scale = sbr->bs_alter_scale; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
164 saved_xover_band = sbr->bs_xover_band; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
165 |
10725 | 166 sbr->bs_header_flag = faad_get1bit(ld |
167 DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag")); | |
10989 | 168 |
12527 | 169 if (sbr->bs_header_flag) |
170 sbr_header(ld, sbr); | |
10989 | 171 |
12527 | 172 /* Reset? */ |
10725 | 173 sbr_reset(sbr); |
174 | |
175 /* first frame should have a header */ | |
12527 | 176 //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0)) |
177 if (sbr->header_count != 0) | |
178 { | |
179 if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked)) | |
180 { | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
181 uint8_t rt = calc_sbr_tables(sbr, sbr->bs_start_freq, sbr->bs_stop_freq, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
182 sbr->bs_samplerate_mode, sbr->bs_freq_scale, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
183 sbr->bs_alter_scale, sbr->bs_xover_band); |
10725 | 184 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
185 /* if an error occured with the new header values revert to the old ones */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
186 if (rt > 0) |
12527 | 187 { |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
188 calc_sbr_tables(sbr, saved_start_freq, saved_stop_freq, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
189 saved_samplerate_mode, saved_freq_scale, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
190 saved_alter_scale, saved_xover_band); |
12527 | 191 } |
10725 | 192 } |
193 | |
12527 | 194 if (result == 0) |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
195 { |
12527 | 196 result = sbr_data(ld, sbr); |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
197 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
198 /* sbr_data() returning an error means that there was an error in |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
199 envelope_time_border_vector(). |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
200 In this case the old time border vector is saved and all the previous |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
201 data normally read after sbr_grid() is saved. |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
202 */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
203 /* to be on the safe side, calculate old sbr tables in case of error */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
204 if ((result > 0) && |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
205 (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
206 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
207 calc_sbr_tables(sbr, saved_start_freq, saved_stop_freq, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
208 saved_samplerate_mode, saved_freq_scale, |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
209 saved_alter_scale, saved_xover_band); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
210 } |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
211 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
212 /* we should be able to safely set result to 0 now */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
213 result = 0; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
214 } |
12527 | 215 } else { |
216 result = 1; | |
10725 | 217 } |
218 | |
12527 | 219 #ifdef DRM |
220 if (!sbr->Is_DRM_SBR) | |
221 #endif | |
222 { | |
223 num_sbr_bits = (uint16_t)faad_get_processed_bits(ld) - num_sbr_bits; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
224 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
225 /* check if we read more bits then were available for sbr */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
226 if (8*cnt < num_sbr_bits) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
227 return 1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
228 |
12527 | 229 /* -4 does not apply, bs_extension_type is re-read in this function */ |
230 num_align_bits = 8*cnt /*- 4*/ - num_sbr_bits; | |
10725 | 231 |
12527 | 232 while (num_align_bits > 7) |
233 { | |
234 faad_getbits(ld, 8 | |
235 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits")); | |
236 num_align_bits -= 8; | |
237 } | |
238 faad_getbits(ld, num_align_bits | |
239 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits")); | |
240 } | |
241 | |
242 return result; | |
10725 | 243 } |
244 | |
245 /* table 3 */ | |
12527 | 246 static void sbr_header(bitfile *ld, sbr_info *sbr) |
10725 | 247 { |
248 uint8_t bs_header_extra_1, bs_header_extra_2; | |
249 | |
250 sbr->header_count++; | |
251 | |
252 sbr->bs_amp_res = faad_get1bit(ld | |
253 DEBUGVAR(1,203,"sbr_header(): bs_amp_res")); | |
254 | |
255 /* bs_start_freq and bs_stop_freq must define a fequency band that does | |
256 not exceed 48 channels */ | |
12527 | 257 sbr->bs_start_freq = (uint8_t)faad_getbits(ld, 4 |
10725 | 258 DEBUGVAR(1,204,"sbr_header(): bs_start_freq")); |
12527 | 259 sbr->bs_stop_freq = (uint8_t)faad_getbits(ld, 4 |
10725 | 260 DEBUGVAR(1,205,"sbr_header(): bs_stop_freq")); |
12527 | 261 sbr->bs_xover_band = (uint8_t)faad_getbits(ld, 3 |
10725 | 262 DEBUGVAR(1,206,"sbr_header(): bs_xover_band")); |
263 faad_getbits(ld, 2 | |
264 DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr")); | |
12527 | 265 bs_header_extra_1 = (uint8_t)faad_get1bit(ld |
10725 | 266 DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1")); |
12527 | 267 bs_header_extra_2 = (uint8_t)faad_get1bit(ld |
10725 | 268 DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2")); |
269 | |
270 if (bs_header_extra_1) | |
271 { | |
12527 | 272 sbr->bs_freq_scale = (uint8_t)faad_getbits(ld, 2 |
10725 | 273 DEBUGVAR(1,211,"sbr_header(): bs_freq_scale")); |
12527 | 274 sbr->bs_alter_scale = (uint8_t)faad_get1bit(ld |
10725 | 275 DEBUGVAR(1,212,"sbr_header(): bs_alter_scale")); |
12527 | 276 sbr->bs_noise_bands = (uint8_t)faad_getbits(ld, 2 |
10725 | 277 DEBUGVAR(1,213,"sbr_header(): bs_noise_bands")); |
10989 | 278 } else { |
279 /* Default values */ | |
280 sbr->bs_freq_scale = 2; | |
281 sbr->bs_alter_scale = 1; | |
282 sbr->bs_noise_bands = 2; | |
10725 | 283 } |
10989 | 284 |
10725 | 285 if (bs_header_extra_2) |
286 { | |
12527 | 287 sbr->bs_limiter_bands = (uint8_t)faad_getbits(ld, 2 |
10725 | 288 DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands")); |
12527 | 289 sbr->bs_limiter_gains = (uint8_t)faad_getbits(ld, 2 |
10725 | 290 DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains")); |
12527 | 291 sbr->bs_interpol_freq = (uint8_t)faad_get1bit(ld |
10725 | 292 DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq")); |
12527 | 293 sbr->bs_smoothing_mode = (uint8_t)faad_get1bit(ld |
10725 | 294 DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode")); |
10989 | 295 } else { |
296 /* Default values */ | |
297 sbr->bs_limiter_bands = 2; | |
298 sbr->bs_limiter_gains = 2; | |
299 sbr->bs_interpol_freq = 1; | |
300 sbr->bs_smoothing_mode = 1; | |
10725 | 301 } |
302 | |
303 #if 0 | |
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) | |
310 { | |
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); | |
314 } | |
315 if (bs_header_extra_2) | |
316 { | |
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); | |
321 } | |
322 printf("\n"); | |
323 #endif | |
324 } | |
325 | |
326 /* table 4 */ | |
12527 | 327 static uint8_t sbr_data(bitfile *ld, sbr_info *sbr) |
10725 | 328 { |
10989 | 329 uint8_t result; |
10725 | 330 #if 0 |
331 sbr->bs_samplerate_mode = faad_get1bit(ld | |
332 DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode")); | |
333 #endif | |
334 | |
335 sbr->rate = (sbr->bs_samplerate_mode) ? 2 : 1; | |
336 | |
12527 | 337 switch (sbr->id_aac) |
10725 | 338 { |
339 case ID_SCE: | |
12527 | 340 if ((result = sbr_single_channel_element(ld, sbr)) > 0) |
341 return result; | |
10725 | 342 break; |
343 case ID_CPE: | |
12527 | 344 if ((result = sbr_channel_pair_element(ld, sbr)) > 0) |
345 return result; | |
10725 | 346 break; |
347 } | |
10989 | 348 |
12527 | 349 return 0; |
10725 | 350 } |
351 | |
352 /* table 5 */ | |
10989 | 353 static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr) |
10725 | 354 { |
10989 | 355 uint8_t result; |
356 | |
10725 | 357 if (faad_get1bit(ld |
358 DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra"))) | |
359 { | |
360 faad_getbits(ld, 4 | |
361 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data")); | |
362 } | |
12527 | 363 |
364 #ifdef DRM | |
365 /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */ | |
366 if (sbr->Is_DRM_SBR) | |
367 faad_get1bit(ld); | |
368 #endif | |
10725 | 369 |
10989 | 370 if ((result = sbr_grid(ld, sbr, 0)) > 0) |
371 return result; | |
10725 | 372 sbr_dtdf(ld, sbr, 0); |
373 invf_mode(ld, sbr, 0); | |
374 sbr_envelope(ld, sbr, 0); | |
375 sbr_noise(ld, sbr, 0); | |
376 | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
377 #ifndef FIXED_POINT |
10725 | 378 envelope_noise_dequantisation(sbr, 0); |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
379 #endif |
10725 | 380 |
381 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); | |
382 | |
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); | |
387 | |
388 sbr->bs_extended_data = faad_get1bit(ld | |
389 DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]")); | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
390 |
10725 | 391 if (sbr->bs_extended_data) |
392 { | |
393 uint16_t nr_bits_left; | |
18141 | 394 #if (defined(PS_DEC) || defined(DRM_PS)) |
395 uint8_t ps_ext_read = 0; | |
396 #endif | |
12527 | 397 uint16_t cnt = (uint16_t)faad_getbits(ld, 4 |
10725 | 398 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size")); |
399 if (cnt == 15) | |
400 { | |
12527 | 401 cnt += (uint16_t)faad_getbits(ld, 8 |
10725 | 402 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count")); |
403 } | |
404 | |
405 nr_bits_left = 8 * cnt; | |
406 while (nr_bits_left > 7) | |
407 { | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
408 uint16_t tmp_nr_bits = 0; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
409 |
12527 | 410 sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2 |
10725 | 411 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id")); |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
412 tmp_nr_bits += 2; |
18141 | 413 |
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) | |
418 #else | |
419 #ifdef PS_DEC | |
420 if (sbr->bs_extension_id == EXTENSION_ID_PS) | |
421 #else | |
422 #ifdef DRM_PS | |
423 if (sbr->bs_extension_id == DRM_PARAMETRIC_STEREO) | |
424 #endif | |
425 #endif | |
426 #endif | |
427 { | |
428 if (ps_ext_read == 0) | |
429 { | |
430 ps_ext_read = 1; | |
431 } else { | |
432 /* to be safe make it 3, will switch to "default" | |
433 * in sbr_extension() */ | |
434 sbr->bs_extension_id = 3; | |
435 } | |
436 } | |
437 #endif | |
438 | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
439 tmp_nr_bits += sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
440 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
441 /* check if the data read is bigger than the number of available bits */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
442 if (tmp_nr_bits > nr_bits_left) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
443 return 1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
444 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
445 nr_bits_left -= tmp_nr_bits; |
10725 | 446 } |
12527 | 447 |
448 /* Corrigendum */ | |
449 if (nr_bits_left > 0) | |
450 { | |
451 faad_getbits(ld, nr_bits_left | |
452 DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left")); | |
453 } | |
10989 | 454 } |
455 | |
456 return 0; | |
10725 | 457 } |
458 | |
459 /* table 6 */ | |
10989 | 460 static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr) |
10725 | 461 { |
10989 | 462 uint8_t n, result; |
10725 | 463 |
464 if (faad_get1bit(ld | |
465 DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra"))) | |
466 { | |
467 faad_getbits(ld, 4 | |
468 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data")); | |
469 faad_getbits(ld, 4 | |
470 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data")); | |
471 } | |
472 | |
473 sbr->bs_coupling = faad_get1bit(ld | |
474 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling")); | |
475 | |
476 if (sbr->bs_coupling) | |
477 { | |
10989 | 478 if ((result = sbr_grid(ld, sbr, 0)) > 0) |
479 return result; | |
10725 | 480 |
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]; | |
486 | |
487 for (n = 0; n <= sbr->L_E[0]; n++) | |
488 { | |
489 sbr->t_E[1][n] = sbr->t_E[0][n]; | |
490 sbr->f[1][n] = sbr->f[0][n]; | |
491 } | |
492 for (n = 0; n <= sbr->L_Q[0]; n++) | |
493 sbr->t_Q[1][n] = sbr->t_Q[0][n]; | |
494 | |
495 sbr_dtdf(ld, sbr, 0); | |
496 sbr_dtdf(ld, sbr, 1); | |
497 invf_mode(ld, sbr, 0); | |
498 | |
499 /* more copying */ | |
500 for (n = 0; n < sbr->N_Q; n++) | |
501 sbr->bs_invf_mode[1][n] = sbr->bs_invf_mode[0][n]; | |
502 | |
503 sbr_envelope(ld, sbr, 0); | |
504 sbr_noise(ld, sbr, 0); | |
505 sbr_envelope(ld, sbr, 1); | |
506 sbr_noise(ld, sbr, 1); | |
507 | |
508 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); | |
509 memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t)); | |
510 | |
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); | |
515 | |
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); | |
520 } else { | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
521 uint8_t saved_t_E[6] = {0}, saved_t_Q[3] = {0}; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
522 uint8_t saved_L_E = sbr->L_E[0]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
523 uint8_t saved_L_Q = sbr->L_Q[0]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
524 uint8_t saved_frame_class = sbr->bs_frame_class[0]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
525 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
526 for (n = 0; n < saved_L_E; n++) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
527 saved_t_E[n] = sbr->t_E[0][n]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
528 for (n = 0; n < saved_L_Q; n++) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
529 saved_t_Q[n] = sbr->t_Q[0][n]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
530 |
10989 | 531 if ((result = sbr_grid(ld, sbr, 0)) > 0) |
532 return result; | |
533 if ((result = sbr_grid(ld, sbr, 1)) > 0) | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
534 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
535 /* restore first channel data as well */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
536 sbr->bs_frame_class[0] = saved_frame_class; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
537 sbr->L_E[0] = saved_L_E; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
538 sbr->L_Q[0] = saved_L_Q; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
539 for (n = 0; n < 6; n++) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
540 sbr->t_E[0][n] = saved_t_E[n]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
541 for (n = 0; n < 3; n++) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
542 sbr->t_Q[0][n] = saved_t_Q[n]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
543 |
10989 | 544 return result; |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
545 } |
10725 | 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); | |
554 | |
555 memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); | |
556 memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t)); | |
557 | |
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); | |
562 | |
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); | |
567 } | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
568 #ifndef FIXED_POINT |
10725 | 569 envelope_noise_dequantisation(sbr, 0); |
570 envelope_noise_dequantisation(sbr, 1); | |
571 | |
572 if (sbr->bs_coupling) | |
573 unmap_envelope_noise(sbr); | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
574 #endif |
10725 | 575 |
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) | |
579 { | |
580 uint16_t nr_bits_left; | |
12527 | 581 uint16_t cnt = (uint16_t)faad_getbits(ld, 4 |
10725 | 582 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size")); |
583 if (cnt == 15) | |
584 { | |
12527 | 585 cnt += (uint16_t)faad_getbits(ld, 8 |
10725 | 586 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count")); |
587 } | |
588 | |
589 nr_bits_left = 8 * cnt; | |
590 while (nr_bits_left > 7) | |
591 { | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
592 uint16_t tmp_nr_bits = 0; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
593 |
12527 | 594 sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2 |
10725 | 595 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id")); |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
596 tmp_nr_bits += 2; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
597 tmp_nr_bits += sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
598 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
599 /* check if the data read is bigger than the number of available bits */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
600 if (tmp_nr_bits > nr_bits_left) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
601 return 1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
602 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
603 nr_bits_left -= tmp_nr_bits; |
10725 | 604 } |
12527 | 605 |
606 /* Corrigendum */ | |
607 if (nr_bits_left > 0) | |
608 { | |
609 faad_getbits(ld, nr_bits_left | |
610 DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left")); | |
611 } | |
10989 | 612 } |
613 | |
614 return 0; | |
10725 | 615 } |
616 | |
12527 | 617 /* integer log[2](x): input range [0,10) */ |
618 static int8_t sbr_log2(const int8_t val) | |
619 { | |
620 int8_t log2tab[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 }; | |
621 if (val < 10 && val >= 0) | |
622 return log2tab[val]; | |
623 else | |
624 return 0; | |
625 } | |
626 | |
627 | |
10725 | 628 /* table 7 */ |
10989 | 629 static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch) |
10725 | 630 { |
10989 | 631 uint8_t i, env, rel, result; |
10725 | 632 uint8_t bs_abs_bord, bs_abs_bord_1; |
12527 | 633 uint8_t bs_num_env = 0; |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
634 uint8_t saved_L_E = sbr->L_E[ch]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
635 uint8_t saved_L_Q = sbr->L_Q[ch]; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
636 uint8_t saved_frame_class = sbr->bs_frame_class[ch]; |
10725 | 637 |
12527 | 638 sbr->bs_frame_class[ch] = (uint8_t)faad_getbits(ld, 2 |
10725 | 639 DEBUGVAR(1,248,"sbr_grid(): bs_frame_class")); |
640 | |
641 switch (sbr->bs_frame_class[ch]) | |
642 { | |
643 case FIXFIX: | |
12527 | 644 i = (uint8_t)faad_getbits(ld, 2 |
10725 | 645 DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw")); |
646 | |
647 bs_num_env = min(1 << i, 5); | |
648 | |
12527 | 649 i = (uint8_t)faad_get1bit(ld |
10725 | 650 DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag")); |
651 for (env = 0; env < bs_num_env; env++) | |
652 sbr->f[ch][env] = i; | |
653 | |
654 sbr->abs_bord_lead[ch] = 0; | |
10989 | 655 sbr->abs_bord_trail[ch] = sbr->numTimeSlots; |
10725 | 656 sbr->n_rel_lead[ch] = bs_num_env - 1; |
657 sbr->n_rel_trail[ch] = 0; | |
658 break; | |
659 | |
660 case FIXVAR: | |
12527 | 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 | |
10725 | 664 DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1; |
665 | |
666 for (rel = 0; rel < bs_num_env-1; rel++) | |
667 { | |
12527 | 668 sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 |
10725 | 669 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2; |
670 } | |
12527 | 671 i = sbr_log2(bs_num_env + 1); |
672 sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i | |
10725 | 673 DEBUGVAR(1,254,"sbr_grid(): bs_pointer")); |
674 | |
675 for (env = 0; env < bs_num_env; env++) | |
676 { | |
12527 | 677 sbr->f[ch][bs_num_env - env - 1] = (uint8_t)faad_get1bit(ld |
10725 | 678 DEBUGVAR(1,255,"sbr_grid(): bs_freq_res")); |
679 } | |
680 | |
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; | |
685 break; | |
686 | |
687 case VARFIX: | |
12527 | 688 bs_abs_bord = (uint8_t)faad_getbits(ld, 2 |
10725 | 689 DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord")); |
12527 | 690 bs_num_env = (uint8_t)faad_getbits(ld, 2 |
10725 | 691 DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1; |
692 | |
693 for (rel = 0; rel < bs_num_env-1; rel++) | |
694 { | |
12527 | 695 sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 |
10725 | 696 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2; |
697 } | |
12527 | 698 i = sbr_log2(bs_num_env + 1); |
699 sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i | |
10725 | 700 DEBUGVAR(1,259,"sbr_grid(): bs_pointer")); |
701 | |
702 for (env = 0; env < bs_num_env; env++) | |
703 { | |
12527 | 704 sbr->f[ch][env] = (uint8_t)faad_get1bit(ld |
10725 | 705 DEBUGVAR(1,260,"sbr_grid(): bs_freq_res")); |
706 } | |
707 | |
708 sbr->abs_bord_lead[ch] = bs_abs_bord; | |
10989 | 709 sbr->abs_bord_trail[ch] = sbr->numTimeSlots; |
10725 | 710 sbr->n_rel_lead[ch] = bs_num_env - 1; |
711 sbr->n_rel_trail[ch] = 0; | |
712 break; | |
713 | |
714 case VARVAR: | |
12527 | 715 bs_abs_bord = (uint8_t)faad_getbits(ld, 2 |
10725 | 716 DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0")); |
12527 | 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 | |
10725 | 720 DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0")); |
12527 | 721 sbr->bs_num_rel_1[ch] = (uint8_t)faad_getbits(ld, 2 |
10725 | 722 DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1")); |
723 | |
724 bs_num_env = min(5, sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 1); | |
725 | |
726 for (rel = 0; rel < sbr->bs_num_rel_0[ch]; rel++) | |
727 { | |
12527 | 728 sbr->bs_rel_bord_0[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 |
10725 | 729 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2; |
730 } | |
731 for(rel = 0; rel < sbr->bs_num_rel_1[ch]; rel++) | |
732 { | |
12527 | 733 sbr->bs_rel_bord_1[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 |
10725 | 734 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2; |
735 } | |
12527 | 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 | |
10725 | 738 DEBUGVAR(1,267,"sbr_grid(): bs_pointer")); |
739 | |
740 for (env = 0; env < bs_num_env; env++) | |
741 { | |
12527 | 742 sbr->f[ch][env] = (uint8_t)faad_get1bit(ld |
10725 | 743 DEBUGVAR(1,268,"sbr_grid(): bs_freq_res")); |
744 } | |
745 | |
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]; | |
750 break; | |
751 } | |
752 | |
753 if (sbr->bs_frame_class[ch] == VARVAR) | |
754 sbr->L_E[ch] = min(bs_num_env, 5); | |
755 else | |
756 sbr->L_E[ch] = min(bs_num_env, 4); | |
757 | |
12527 | 758 if (sbr->L_E[ch] <= 0) |
759 return 1; | |
760 | |
10725 | 761 if (sbr->L_E[ch] > 1) |
762 sbr->L_Q[ch] = 2; | |
763 else | |
764 sbr->L_Q[ch] = 1; | |
765 | |
766 /* TODO: this code can probably be integrated into the code above! */ | |
10989 | 767 if ((result = envelope_time_border_vector(sbr, ch)) > 0) |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
768 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
769 sbr->bs_frame_class[ch] = saved_frame_class; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
770 sbr->L_E[ch] = saved_L_E; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
771 sbr->L_Q[ch] = saved_L_Q; |
10989 | 772 return result; |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
773 } |
10725 | 774 noise_floor_time_border_vector(sbr, ch); |
10989 | 775 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
776 #if 0 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
777 for (env = 0; env < bs_num_env; env++) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
778 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
779 printf("freq_res[ch:%d][env:%d]: %d\n", ch, env, sbr->f[ch][env]); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
780 } |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
781 #endif |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
782 |
10989 | 783 return 0; |
10725 | 784 } |
785 | |
786 /* table 8 */ | |
787 static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch) | |
788 { | |
789 uint8_t i; | |
790 | |
791 for (i = 0; i < sbr->L_E[ch]; i++) | |
792 { | |
793 sbr->bs_df_env[ch][i] = faad_get1bit(ld | |
794 DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env")); | |
795 } | |
796 | |
797 for (i = 0; i < sbr->L_Q[ch]; i++) | |
798 { | |
799 sbr->bs_df_noise[ch][i] = faad_get1bit(ld | |
800 DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise")); | |
801 } | |
802 } | |
803 | |
804 /* table 9 */ | |
805 static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch) | |
806 { | |
807 uint8_t n; | |
808 | |
12527 | 809 for (n = 0; n < sbr->N_Q; n++) |
10725 | 810 { |
12527 | 811 sbr->bs_invf_mode[ch][n] = (uint8_t)faad_getbits(ld, 2 |
10989 | 812 DEBUGVAR(1,271,"invf_mode(): bs_invf_mode")); |
12527 | 813 } |
814 } | |
815 | |
816 static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr, | |
817 uint8_t bs_extension_id, uint16_t num_bits_left) | |
818 { | |
18141 | 819 #ifdef PS_DEC |
820 uint8_t header; | |
821 uint16_t ret; | |
822 #endif | |
823 | |
12527 | 824 switch (bs_extension_id) |
825 { | |
826 #ifdef PS_DEC | |
827 case EXTENSION_ID_PS: | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
828 if (!sbr->ps) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
829 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
830 sbr->ps = ps_init(get_sr_index(sbr->sample_rate)); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
831 } |
18141 | 832 ret = ps_data(sbr->ps, ld, &header); |
833 | |
834 /* enable PS if and only if: a header has been decoded */ | |
835 if (sbr->ps_used == 0 && header == 1) | |
836 { | |
837 sbr->ps_used = 1; | |
838 } | |
839 | |
840 return ret; | |
10989 | 841 #endif |
12527 | 842 #ifdef DRM_PS |
843 case DRM_PARAMETRIC_STEREO: | |
844 sbr->ps_used = 1; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
845 if (!sbr->drm_ps) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
846 { |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
847 sbr->drm_ps = drm_ps_init(); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
848 } |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
849 return drm_ps_data(sbr->drm_ps, ld); |
12527 | 850 #endif |
851 default: | |
852 sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6 | |
853 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data")); | |
854 return 6; | |
10725 | 855 } |
856 } | |
857 | |
858 /* table 12 */ | |
859 static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch) | |
860 { | |
861 uint8_t n; | |
862 | |
863 for (n = 0; n < sbr->N_high; n++) | |
864 { | |
865 sbr->bs_add_harmonic[ch][n] = faad_get1bit(ld | |
866 DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic")); | |
867 } | |
868 } | |
869 | |
870 | |
12527 | 871 #endif /* SBR_DEC */ |