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