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