Mercurial > mplayer.hg
annotate libfaad2/syntax.c @ 12904:678af17ca11e
typos
author | diego |
---|---|
date | Wed, 28 Jul 2004 00:02:45 +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: syntax.c,v 1.3 2004/06/02 22:59:04 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 /* | |
31 Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio) | |
32 */ | |
33 | |
34 #include "common.h" | |
35 #include "structs.h" | |
36 | |
37 #include <stdlib.h> | |
38 #include <string.h> | |
39 | |
40 #include "decoder.h" | |
41 #include "syntax.h" | |
42 #include "specrec.h" | |
43 #include "huffman.h" | |
44 #include "bits.h" | |
45 #include "pulse.h" | |
46 #include "analysis.h" | |
47 #include "drc.h" | |
48 #ifdef ERROR_RESILIENCE | |
49 #include "rvlc.h" | |
50 #endif | |
51 #ifdef SBR_DEC | |
52 #include "sbr_syntax.h" | |
53 #endif | |
54 | |
55 | |
12527 | 56 /* static function declarations */ |
57 static void decode_sce_lfe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld, | |
58 uint8_t id_syn_ele); | |
59 static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld, | |
60 uint8_t id_syn_ele); | |
61 static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld, | |
62 uint8_t channel, uint8_t *tag); | |
63 static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld, | |
64 uint8_t channel, uint8_t *tag); | |
65 #ifdef COUPLING_DEC | |
66 static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld); | |
67 #endif | |
68 static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld); | |
69 static uint8_t program_config_element(program_config *pce, bitfile *ld); | |
70 static uint8_t fill_element(faacDecHandle hDecoder, bitfile *ld, drc_info *drc | |
71 #ifdef SBR_DEC | |
72 ,uint8_t sbr_ele | |
73 #endif | |
74 ); | |
75 static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele, | |
76 bitfile *ld, ic_stream *ics, uint8_t scal_flag, | |
77 int16_t *spec_data); | |
78 static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, | |
79 uint8_t common_window); | |
80 static uint8_t section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld); | |
81 static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld); | |
82 #ifdef SSR_DEC | |
83 static void gain_control_data(bitfile *ld, ic_stream *ics); | |
84 #endif | |
85 static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, | |
86 int16_t *spectral_data); | |
87 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count); | |
88 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld); | |
89 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld); | |
90 static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld); | |
91 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld); | |
92 static void adts_variable_header(adts_header *adts, bitfile *ld); | |
93 static void adts_error_check(adts_header *adts, bitfile *ld); | |
94 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc); | |
95 static uint8_t excluded_channels(bitfile *ld, drc_info *drc); | |
96 #ifdef SCALABLE_DEC | |
97 static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2, | |
98 bitfile *ld, uint8_t this_layer_stereo); | |
99 #endif | |
100 | |
101 | |
10725 | 102 /* Table 4.4.1 */ |
103 int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC, | |
104 program_config *pce_out) | |
105 { | |
106 program_config pce; | |
107 | |
108 /* 1024 or 960 */ | |
109 mp4ASC->frameLengthFlag = faad_get1bit(ld | |
110 DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag")); | |
12527 | 111 #ifndef ALLOW_SMALL_FRAMELENGTH |
112 if (mp4ASC->frameLengthFlag == 1) | |
113 return -3; | |
114 #endif | |
10725 | 115 |
116 mp4ASC->dependsOnCoreCoder = faad_get1bit(ld | |
117 DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder")); | |
118 if (mp4ASC->dependsOnCoreCoder == 1) | |
119 { | |
120 mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14 | |
121 DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay")); | |
122 } | |
123 | |
124 mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag")); | |
125 if (mp4ASC->channelsConfiguration == 0) | |
126 { | |
12527 | 127 if (program_config_element(&pce, ld)) |
128 return -3; | |
10725 | 129 //mp4ASC->channelsConfiguration = pce.channels; |
130 | |
131 if (pce_out != NULL) | |
132 memcpy(pce_out, &pce, sizeof(program_config)); | |
133 | |
134 /* | |
135 if (pce.num_valid_cc_elements) | |
136 return -3; | |
137 */ | |
138 } | |
139 | |
140 #ifdef ERROR_RESILIENCE | |
141 if (mp4ASC->extensionFlag == 1) | |
142 { | |
143 /* Error resilience not supported yet */ | |
144 if (mp4ASC->objectTypeIndex >= ER_OBJECT_START) | |
145 { | |
146 mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld | |
147 DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag")); | |
148 mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld | |
149 DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag")); | |
150 mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld | |
151 DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag")); | |
152 | |
153 /* 1 bit: extensionFlag3 */ | |
154 } | |
155 } | |
156 #endif | |
157 | |
158 return 0; | |
159 } | |
160 | |
161 /* Table 4.4.2 */ | |
162 /* An MPEG-4 Audio decoder is only required to follow the Program | |
163 Configuration Element in GASpecificConfig(). The decoder shall ignore | |
164 any Program Configuration Elements that may occur in raw data blocks. | |
165 PCEs transmitted in raw data blocks cannot be used to convey decoder | |
166 configuration information. | |
167 */ | |
12527 | 168 static uint8_t program_config_element(program_config *pce, bitfile *ld) |
10725 | 169 { |
170 uint8_t i; | |
171 | |
172 memset(pce, 0, sizeof(program_config)); | |
173 | |
174 pce->channels = 0; | |
175 | |
176 pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4 | |
177 DEBUGVAR(1,10,"program_config_element(): element_instance_tag")); | |
178 | |
179 pce->object_type = (uint8_t)faad_getbits(ld, 2 | |
180 DEBUGVAR(1,11,"program_config_element(): object_type")); | |
181 pce->sf_index = (uint8_t)faad_getbits(ld, 4 | |
182 DEBUGVAR(1,12,"program_config_element(): sf_index")); | |
183 pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4 | |
184 DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements")); | |
185 pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4 | |
186 DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements")); | |
187 pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4 | |
188 DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements")); | |
189 pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2 | |
190 DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements")); | |
191 pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3 | |
192 DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements")); | |
193 pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4 | |
194 DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements")); | |
195 | |
196 pce->mono_mixdown_present = faad_get1bit(ld | |
197 DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present")); | |
198 if (pce->mono_mixdown_present == 1) | |
199 { | |
200 pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4 | |
201 DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number")); | |
202 } | |
203 | |
204 pce->stereo_mixdown_present = faad_get1bit(ld | |
205 DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present")); | |
206 if (pce->stereo_mixdown_present == 1) | |
207 { | |
208 pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4 | |
209 DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number")); | |
210 } | |
211 | |
212 pce->matrix_mixdown_idx_present = faad_get1bit(ld | |
213 DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present")); | |
214 if (pce->matrix_mixdown_idx_present == 1) | |
215 { | |
216 pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2 | |
217 DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx")); | |
218 pce->pseudo_surround_enable = faad_get1bit(ld | |
219 DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable")); | |
220 } | |
221 | |
222 for (i = 0; i < pce->num_front_channel_elements; i++) | |
223 { | |
224 pce->front_element_is_cpe[i] = faad_get1bit(ld | |
225 DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe")); | |
226 pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4 | |
227 DEBUGVAR(1,27,"program_config_element(): front_element_tag_select")); | |
228 | |
229 if (pce->front_element_is_cpe[i] & 1) | |
230 { | |
231 pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels; | |
232 pce->num_front_channels += 2; | |
233 pce->channels += 2; | |
234 } else { | |
235 pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels; | |
236 pce->num_front_channels++; | |
237 pce->channels++; | |
238 } | |
239 } | |
240 | |
241 for (i = 0; i < pce->num_side_channel_elements; i++) | |
242 { | |
243 pce->side_element_is_cpe[i] = faad_get1bit(ld | |
244 DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe")); | |
245 pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4 | |
246 DEBUGVAR(1,29,"program_config_element(): side_element_tag_select")); | |
247 | |
248 if (pce->side_element_is_cpe[i] & 1) | |
249 { | |
250 pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels; | |
251 pce->num_side_channels += 2; | |
252 pce->channels += 2; | |
253 } else { | |
254 pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels; | |
255 pce->num_side_channels++; | |
256 pce->channels++; | |
257 } | |
258 } | |
259 | |
260 for (i = 0; i < pce->num_back_channel_elements; i++) | |
261 { | |
262 pce->back_element_is_cpe[i] = faad_get1bit(ld | |
263 DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe")); | |
264 pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4 | |
265 DEBUGVAR(1,31,"program_config_element(): back_element_tag_select")); | |
266 | |
267 if (pce->back_element_is_cpe[i] & 1) | |
268 { | |
269 pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels; | |
270 pce->channels += 2; | |
271 pce->num_back_channels += 2; | |
272 } else { | |
273 pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels; | |
274 pce->num_back_channels++; | |
275 pce->channels++; | |
276 } | |
277 } | |
278 | |
279 for (i = 0; i < pce->num_lfe_channel_elements; i++) | |
280 { | |
281 pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4 | |
282 DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select")); | |
283 | |
284 pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels; | |
285 pce->num_lfe_channels++; | |
286 pce->channels++; | |
287 } | |
288 | |
289 for (i = 0; i < pce->num_assoc_data_elements; i++) | |
290 pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4 | |
291 DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select")); | |
292 | |
293 for (i = 0; i < pce->num_valid_cc_elements; i++) | |
294 { | |
295 pce->cc_element_is_ind_sw[i] = faad_get1bit(ld | |
296 DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw")); | |
297 pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4 | |
298 DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select")); | |
299 } | |
300 | |
301 faad_byte_align(ld); | |
302 | |
303 pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8 | |
304 DEBUGVAR(1,36,"program_config_element(): comment_field_bytes")); | |
305 | |
306 for (i = 0; i < pce->comment_field_bytes; i++) | |
307 { | |
308 pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8 | |
309 DEBUGVAR(1,37,"program_config_element(): comment_field_data")); | |
310 } | |
311 pce->comment_field_data[i] = 0; | |
312 | |
12527 | 313 if (pce->channels > MAX_CHANNELS) |
314 return 22; | |
315 | |
10725 | 316 return 0; |
317 } | |
318 | |
12527 | 319 static void decode_sce_lfe(faacDecHandle hDecoder, |
320 faacDecFrameInfo *hInfo, bitfile *ld, | |
321 uint8_t id_syn_ele) | |
10725 | 322 { |
323 uint8_t channels = hDecoder->fr_channels; | |
12527 | 324 uint8_t tag = 0; |
10725 | 325 |
326 if (channels+1 > MAX_CHANNELS) | |
327 { | |
328 hInfo->error = 12; | |
12527 | 329 return; |
10725 | 330 } |
331 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS) | |
332 { | |
333 hInfo->error = 13; | |
12527 | 334 return; |
10725 | 335 } |
336 | |
12527 | 337 /* for SCE hDecoder->element_output_channels[] is not set here because this |
338 can become 2 when some form of Parametric Stereo coding is used | |
339 */ | |
10725 | 340 |
12527 | 341 /* save the syntax element id */ |
342 hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele; | |
10725 | 343 |
12527 | 344 /* decode the element */ |
345 hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag); | |
10725 | 346 |
12527 | 347 /* map output channels position to internal data channels */ |
348 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2) | |
349 { | |
350 /* this might be faulty when pce_set is true */ | |
10725 | 351 hDecoder->internal_channel[channels] = channels; |
12527 | 352 hDecoder->internal_channel[channels+1] = channels+1; |
353 } else { | |
354 if (hDecoder->pce_set) | |
355 hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels; | |
356 else | |
357 hDecoder->internal_channel[channels] = channels; | |
358 } | |
10725 | 359 |
12527 | 360 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele]; |
10725 | 361 hDecoder->fr_ch_ele++; |
362 } | |
363 | |
12527 | 364 static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld, |
365 uint8_t id_syn_ele) | |
10725 | 366 { |
367 uint8_t channels = hDecoder->fr_channels; | |
12527 | 368 uint8_t tag = 0; |
10725 | 369 |
370 if (channels+2 > MAX_CHANNELS) | |
371 { | |
372 hInfo->error = 12; | |
12527 | 373 return; |
10725 | 374 } |
375 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS) | |
376 { | |
377 hInfo->error = 13; | |
12527 | 378 return; |
10725 | 379 } |
380 | |
12527 | 381 /* for CPE the number of output channels is always 2 */ |
382 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0) | |
383 { | |
384 /* element_output_channels not set yet */ | |
385 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2; | |
386 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) { | |
387 /* element inconsistency */ | |
388 hInfo->error = 21; | |
389 return; | |
390 } | |
10725 | 391 |
12527 | 392 /* save the syntax element id */ |
393 hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele; | |
10725 | 394 |
12527 | 395 /* decode the element */ |
396 hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag); | |
10725 | 397 |
12527 | 398 /* map output channel position to internal data channels */ |
10725 | 399 if (hDecoder->pce_set) |
400 { | |
12527 | 401 hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels; |
402 hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1; | |
10725 | 403 } else { |
404 hDecoder->internal_channel[channels] = channels; | |
405 hDecoder->internal_channel[channels+1] = channels+1; | |
406 } | |
407 | |
408 hDecoder->fr_channels += 2; | |
409 hDecoder->fr_ch_ele++; | |
410 } | |
411 | |
12527 | 412 void raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, |
413 bitfile *ld, program_config *pce, drc_info *drc) | |
10725 | 414 { |
415 uint8_t id_syn_ele; | |
416 | |
417 hDecoder->fr_channels = 0; | |
418 hDecoder->fr_ch_ele = 0; | |
419 hDecoder->first_syn_ele = 25; | |
420 hDecoder->has_lfe = 0; | |
421 | |
422 #ifdef ERROR_RESILIENCE | |
423 if (hDecoder->object_type < ER_OBJECT_START) | |
424 { | |
425 #endif | |
426 /* Table 4.4.3: raw_data_block() */ | |
427 while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID | |
428 DEBUGVAR(1,4,"faacDecDecode(): id_syn_ele"))) != ID_END) | |
429 { | |
430 switch (id_syn_ele) { | |
431 case ID_SCE: | |
432 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele; | |
12527 | 433 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele); |
10725 | 434 if (hInfo->error > 0) |
12527 | 435 return; |
10725 | 436 break; |
437 case ID_CPE: | |
438 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele; | |
12527 | 439 decode_cpe(hDecoder, hInfo, ld, id_syn_ele); |
10725 | 440 if (hInfo->error > 0) |
12527 | 441 return; |
10725 | 442 break; |
443 case ID_LFE: | |
444 hDecoder->has_lfe++; | |
12527 | 445 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele); |
10725 | 446 if (hInfo->error > 0) |
12527 | 447 return; |
10725 | 448 break; |
449 case ID_CCE: /* not implemented yet, but skip the bits */ | |
12527 | 450 #ifdef COUPLING_DEC |
10725 | 451 hInfo->error = coupling_channel_element(hDecoder, ld); |
12527 | 452 #else |
453 hInfo->error = 6; | |
454 #endif | |
10725 | 455 if (hInfo->error > 0) |
12527 | 456 return; |
10725 | 457 break; |
458 case ID_DSE: | |
459 data_stream_element(hDecoder, ld); | |
460 break; | |
461 case ID_PCE: | |
12527 | 462 /* 14496-4: 5.6.4.1.2.1.3: */ |
463 /* program_configuration_element()'s in access units shall be ignored */ | |
464 program_config_element(pce, ld); | |
465 //if ((hInfo->error = program_config_element(pce, ld)) > 0) | |
466 // return; | |
467 //hDecoder->pce_set = 1; | |
10725 | 468 break; |
469 case ID_FIL: | |
470 /* one sbr_info describes a channel_element not a channel! */ | |
12527 | 471 /* if we encounter SBR data here: error */ |
472 /* SBR data will be read directly in the SCE/LFE/CPE element */ | |
10725 | 473 if ((hInfo->error = fill_element(hDecoder, ld, drc |
474 #ifdef SBR_DEC | |
12527 | 475 , INVALID_SBR_ELEMENT |
10725 | 476 #endif |
477 )) > 0) | |
12527 | 478 return; |
10725 | 479 break; |
480 } | |
481 } | |
482 #ifdef ERROR_RESILIENCE | |
483 } else { | |
484 /* Table 262: er_raw_data_block() */ | |
485 switch (hDecoder->channelConfiguration) | |
486 { | |
487 case 1: | |
12527 | 488 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); |
10725 | 489 if (hInfo->error > 0) |
12527 | 490 return; |
10725 | 491 break; |
492 case 2: | |
12527 | 493 decode_cpe(hDecoder, hInfo, ld, ID_CPE); |
10725 | 494 if (hInfo->error > 0) |
12527 | 495 return; |
10725 | 496 break; |
497 case 3: | |
12527 | 498 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); |
499 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
10725 | 500 if (hInfo->error > 0) |
12527 | 501 return; |
10725 | 502 break; |
503 case 4: | |
12527 | 504 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); |
505 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
506 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); | |
10725 | 507 if (hInfo->error > 0) |
12527 | 508 return; |
10725 | 509 break; |
510 case 5: | |
12527 | 511 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); |
512 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
513 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
10725 | 514 if (hInfo->error > 0) |
12527 | 515 return; |
10725 | 516 break; |
517 case 6: | |
12527 | 518 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); |
519 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
520 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
521 decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE); | |
10725 | 522 if (hInfo->error > 0) |
12527 | 523 return; |
10725 | 524 break; |
12527 | 525 case 7: /* 8 channels */ |
526 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE); | |
527 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
528 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
529 decode_cpe(hDecoder, hInfo, ld, ID_CPE); | |
530 decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE); | |
10725 | 531 if (hInfo->error > 0) |
12527 | 532 return; |
10725 | 533 break; |
534 default: | |
535 hInfo->error = 7; | |
12527 | 536 return; |
10725 | 537 } |
538 #if 0 | |
539 cnt = bits_to_decode() / 8; | |
540 while (cnt >= 1) | |
541 { | |
542 cnt -= extension_payload(cnt); | |
543 } | |
544 #endif | |
545 } | |
546 #endif | |
547 | |
548 /* new in corrigendum 14496-3:2002 */ | |
10989 | 549 #ifdef DRM |
550 if (hDecoder->object_type != DRM_ER_LC) | |
551 #endif | |
552 { | |
553 faad_byte_align(ld); | |
554 } | |
10725 | 555 |
12527 | 556 return; |
10725 | 557 } |
558 | |
559 /* Table 4.4.4 and */ | |
560 /* Table 4.4.9 */ | |
12527 | 561 static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld, |
562 uint8_t channel, uint8_t *tag) | |
10725 | 563 { |
10989 | 564 uint8_t retval = 0; |
12527 | 565 element sce = {0}; |
566 ic_stream *ics = &(sce.ics1); | |
567 ALIGN int16_t spec_data[1024] = {0}; | |
10725 | 568 |
12527 | 569 sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG |
10725 | 570 DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag")); |
571 | |
12527 | 572 *tag = sce.element_instance_tag; |
573 sce.channel = channel; | |
574 sce.paired_channel = -1; | |
10725 | 575 |
12527 | 576 retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data); |
577 if (retval > 0) | |
578 return retval; | |
10725 | 579 |
12527 | 580 #ifdef SBR_DEC |
581 /* check if next bitstream element is a fill element */ | |
582 /* if so, read it now so SBR decoding can be done in case of a file with SBR */ | |
583 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL) | |
584 { | |
585 faad_flushbits(ld, LEN_SE_ID); | |
586 | |
587 /* one sbr_info describes a channel_element not a channel! */ | |
588 if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0) | |
10725 | 589 { |
12527 | 590 return retval; |
10725 | 591 } |
12527 | 592 } |
10725 | 593 #endif |
10989 | 594 |
595 /* noiseless coding is done, spectral reconstruction is done now */ | |
12527 | 596 retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data); |
597 if (retval > 0) | |
598 return retval; | |
10989 | 599 |
600 return 0; | |
10725 | 601 } |
602 | |
603 /* Table 4.4.5 */ | |
12527 | 604 static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld, |
605 uint8_t channels, uint8_t *tag) | |
10725 | 606 { |
12527 | 607 ALIGN int16_t spec_data1[1024] = {0}; |
608 ALIGN int16_t spec_data2[1024] = {0}; | |
609 element cpe = {0}; | |
610 ic_stream *ics1 = &(cpe.ics1); | |
611 ic_stream *ics2 = &(cpe.ics2); | |
10725 | 612 uint8_t result; |
12527 | 613 |
614 cpe.channel = channels; | |
615 cpe.paired_channel = channels+1; | |
10725 | 616 |
12527 | 617 cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG |
10725 | 618 DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag")); |
12527 | 619 *tag = cpe.element_instance_tag; |
10725 | 620 |
12527 | 621 if ((cpe.common_window = faad_get1bit(ld |
10725 | 622 DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1) |
623 { | |
624 /* both channels have common ics information */ | |
12527 | 625 if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0) |
10725 | 626 return result; |
627 | |
628 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2 | |
629 DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present")); | |
630 if (ics1->ms_mask_present == 1) | |
631 { | |
632 uint8_t g, sfb; | |
633 for (g = 0; g < ics1->num_window_groups; g++) | |
634 { | |
635 for (sfb = 0; sfb < ics1->max_sfb; sfb++) | |
636 { | |
637 ics1->ms_used[g][sfb] = faad_get1bit(ld | |
638 DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit")); | |
639 } | |
640 } | |
641 } | |
642 | |
643 #ifdef ERROR_RESILIENCE | |
644 if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present)) | |
645 { | |
646 if ((ics1->ltp.data_present = faad_get1bit(ld | |
647 DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1) | |
648 { | |
12527 | 649 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0) |
650 { | |
651 return result; | |
652 } | |
10725 | 653 } |
654 } | |
655 #endif | |
656 | |
657 memcpy(ics2, ics1, sizeof(ic_stream)); | |
658 } else { | |
659 ics1->ms_mask_present = 0; | |
660 } | |
661 | |
12527 | 662 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1, |
10725 | 663 0, spec_data1)) > 0) |
664 { | |
665 return result; | |
666 } | |
667 | |
668 #ifdef ERROR_RESILIENCE | |
12527 | 669 if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) && |
10725 | 670 (ics1->predictor_data_present)) |
671 { | |
672 if ((ics1->ltp2.data_present = faad_get1bit(ld | |
673 DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1) | |
674 { | |
12527 | 675 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0) |
676 { | |
677 return result; | |
678 } | |
10725 | 679 } |
680 } | |
681 #endif | |
682 | |
12527 | 683 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2, |
10725 | 684 0, spec_data2)) > 0) |
685 { | |
686 return result; | |
687 } | |
688 | |
12527 | 689 #ifdef SBR_DEC |
690 /* check if next bitstream element is a fill element */ | |
691 /* if so, read it now so SBR decoding can be done in case of a file with SBR */ | |
692 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL) | |
10725 | 693 { |
12527 | 694 faad_flushbits(ld, LEN_SE_ID); |
10725 | 695 |
12527 | 696 /* one sbr_info describes a channel_element not a channel! */ |
697 if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0) | |
698 { | |
10725 | 699 return result; |
700 } | |
10989 | 701 } |
10725 | 702 #endif |
703 | |
10989 | 704 /* noiseless coding is done, spectral reconstruction is done now */ |
12527 | 705 if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, |
706 spec_data1, spec_data2)) > 0) | |
707 { | |
708 return result; | |
709 } | |
10989 | 710 |
10725 | 711 return 0; |
712 } | |
713 | |
714 /* Table 4.4.6 */ | |
715 static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, | |
716 uint8_t common_window) | |
717 { | |
718 uint8_t retval = 0; | |
719 | |
720 /* ics->ics_reserved_bit = */ faad_get1bit(ld | |
721 DEBUGVAR(1,43,"ics_info(): ics_reserved_bit")); | |
722 ics->window_sequence = (uint8_t)faad_getbits(ld, 2 | |
723 DEBUGVAR(1,44,"ics_info(): window_sequence")); | |
724 ics->window_shape = faad_get1bit(ld | |
725 DEBUGVAR(1,45,"ics_info(): window_shape")); | |
726 | |
727 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
728 { | |
729 ics->max_sfb = (uint8_t)faad_getbits(ld, 4 | |
730 DEBUGVAR(1,46,"ics_info(): max_sfb (short)")); | |
731 ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7 | |
732 DEBUGVAR(1,47,"ics_info(): scale_factor_grouping")); | |
733 } else { | |
734 ics->max_sfb = (uint8_t)faad_getbits(ld, 6 | |
735 DEBUGVAR(1,48,"ics_info(): max_sfb (long)")); | |
736 } | |
737 | |
738 /* get the grouping information */ | |
739 if ((retval = window_grouping_info(hDecoder, ics)) > 0) | |
740 return retval; | |
741 | |
742 /* should be an error */ | |
743 /* check the range of max_sfb */ | |
744 if (ics->max_sfb > ics->num_swb) | |
745 return 16; | |
746 | |
747 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE) | |
748 { | |
749 if ((ics->predictor_data_present = faad_get1bit(ld | |
750 DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1) | |
751 { | |
752 if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */ | |
753 { | |
754 uint8_t sfb; | |
755 | |
12527 | 756 ics->pred.limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index)); |
10725 | 757 |
758 if ((ics->pred.predictor_reset = faad_get1bit(ld | |
759 DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1) | |
760 { | |
761 ics->pred.predictor_reset_group_number = (uint8_t)faad_getbits(ld, 5 | |
762 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number")); | |
763 } | |
764 | |
765 for (sfb = 0; sfb < ics->pred.limit; sfb++) | |
766 { | |
767 ics->pred.prediction_used[sfb] = faad_get1bit(ld | |
768 DEBUGVAR(1,55,"ics_info(): pred.prediction_used")); | |
769 } | |
770 } | |
771 #ifdef LTP_DEC | |
772 else { /* Long Term Prediction */ | |
773 if (hDecoder->object_type < ER_OBJECT_START) | |
774 { | |
775 if ((ics->ltp.data_present = faad_get1bit(ld | |
776 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1) | |
777 { | |
12527 | 778 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0) |
779 { | |
780 return retval; | |
781 } | |
10725 | 782 } |
783 if (common_window) | |
784 { | |
785 if ((ics->ltp2.data_present = faad_get1bit(ld | |
786 DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1) | |
787 { | |
12527 | 788 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0) |
789 { | |
790 return retval; | |
791 } | |
10725 | 792 } |
793 } | |
794 } | |
795 #ifdef ERROR_RESILIENCE | |
796 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START)) | |
797 { | |
798 if ((ics->ltp.data_present = faad_get1bit(ld | |
799 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1) | |
800 { | |
801 ltp_data(hDecoder, ics, &(ics->ltp), ld); | |
802 } | |
803 } | |
804 #endif | |
805 } | |
806 #endif | |
807 } | |
808 } | |
809 | |
810 return retval; | |
811 } | |
812 | |
813 /* Table 4.4.7 */ | |
814 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld) | |
815 { | |
816 uint8_t i; | |
817 | |
818 pul->number_pulse = (uint8_t)faad_getbits(ld, 2 | |
819 DEBUGVAR(1,56,"pulse_data(): number_pulse")); | |
820 pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6 | |
821 DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb")); | |
822 | |
823 /* check the range of pulse_start_sfb */ | |
824 if (pul->pulse_start_sfb > ics->num_swb) | |
825 return 16; | |
826 | |
827 for (i = 0; i < pul->number_pulse+1; i++) | |
828 { | |
829 pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5 | |
830 DEBUGVAR(1,58,"pulse_data(): pulse_offset")); | |
831 pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4 | |
832 DEBUGVAR(1,59,"pulse_data(): pulse_amp")); | |
833 } | |
834 | |
835 return 0; | |
836 } | |
837 | |
12527 | 838 #ifdef COUPLING_DEC |
10725 | 839 /* Table 4.4.8: Currently just for skipping the bits... */ |
840 static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld) | |
841 { | |
842 uint8_t c, result = 0; | |
843 uint8_t ind_sw_cce_flag = 0; | |
844 uint8_t num_gain_element_lists = 0; | |
845 uint8_t num_coupled_elements = 0; | |
846 | |
12527 | 847 element el_empty = {0}; |
848 ic_stream ics_empty = {0}; | |
10725 | 849 int16_t sh_data[1024]; |
850 | |
851 c = faad_getbits(ld, LEN_TAG | |
852 DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag")); | |
853 | |
854 ind_sw_cce_flag = faad_get1bit(ld | |
855 DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag")); | |
856 num_coupled_elements = faad_getbits(ld, 3 | |
857 DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements")); | |
858 | |
859 for (c = 0; c < num_coupled_elements + 1; c++) | |
860 { | |
861 uint8_t cc_target_is_cpe, cc_target_tag_select; | |
862 | |
863 num_gain_element_lists++; | |
864 | |
865 cc_target_is_cpe = faad_get1bit(ld | |
866 DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe")); | |
867 cc_target_tag_select = faad_getbits(ld, 4 | |
868 DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select")); | |
869 | |
870 if (cc_target_is_cpe) | |
871 { | |
872 uint8_t cc_l = faad_get1bit(ld | |
873 DEBUGVAR(1,905,"coupling_channel_element(): cc_l")); | |
874 uint8_t cc_r = faad_get1bit(ld | |
875 DEBUGVAR(1,906,"coupling_channel_element(): cc_r")); | |
876 | |
877 if (cc_l && cc_r) | |
878 num_gain_element_lists++; | |
879 } | |
880 } | |
881 | |
882 faad_get1bit(ld | |
883 DEBUGVAR(1,907,"coupling_channel_element(): cc_domain")); | |
884 faad_get1bit(ld | |
885 DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign")); | |
886 faad_getbits(ld, 2 | |
887 DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale")); | |
888 | |
889 if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty, | |
890 0, sh_data)) > 0) | |
891 { | |
892 return result; | |
893 } | |
894 | |
895 for (c = 1; c < num_gain_element_lists; c++) | |
896 { | |
897 uint8_t cge; | |
898 | |
899 if (ind_sw_cce_flag) | |
900 { | |
901 cge = 1; | |
902 } else { | |
903 cge = faad_get1bit(ld | |
904 DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present")); | |
905 } | |
906 | |
907 if (cge) | |
908 { | |
909 huffman_scale_factor(ld); | |
910 } else { | |
911 uint8_t g, sfb; | |
912 | |
913 for (g = 0; g < ics_empty.num_window_groups; g++) | |
914 { | |
915 for (sfb = 0; sfb < ics_empty.max_sfb; sfb++) | |
916 { | |
917 if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB) | |
918 huffman_scale_factor(ld); | |
919 } | |
920 } | |
921 } | |
922 } | |
923 | |
924 return 0; | |
925 } | |
12527 | 926 #endif |
10725 | 927 |
928 /* Table 4.4.10 */ | |
929 static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld) | |
930 { | |
931 uint8_t byte_aligned; | |
932 uint16_t i, count; | |
933 | |
934 /* element_instance_tag = */ faad_getbits(ld, LEN_TAG | |
935 DEBUGVAR(1,60,"data_stream_element(): element_instance_tag")); | |
936 byte_aligned = faad_get1bit(ld | |
937 DEBUGVAR(1,61,"data_stream_element(): byte_aligned")); | |
938 count = (uint16_t)faad_getbits(ld, 8 | |
939 DEBUGVAR(1,62,"data_stream_element(): count")); | |
940 if (count == 255) | |
941 { | |
942 count += (uint16_t)faad_getbits(ld, 8 | |
943 DEBUGVAR(1,63,"data_stream_element(): extra count")); | |
944 } | |
945 if (byte_aligned) | |
946 faad_byte_align(ld); | |
947 | |
948 for (i = 0; i < count; i++) | |
949 { | |
12527 | 950 faad_getbits(ld, LEN_BYTE |
10725 | 951 DEBUGVAR(1,64,"data_stream_element(): data_stream_byte")); |
952 } | |
953 | |
954 return count; | |
955 } | |
956 | |
957 /* Table 4.4.11 */ | |
958 static uint8_t fill_element(faacDecHandle hDecoder, bitfile *ld, drc_info *drc | |
959 #ifdef SBR_DEC | |
960 ,uint8_t sbr_ele | |
961 #endif | |
962 ) | |
963 { | |
964 uint16_t count; | |
965 #ifdef SBR_DEC | |
966 uint8_t bs_extension_type; | |
967 #endif | |
968 | |
969 count = (uint16_t)faad_getbits(ld, 4 | |
970 DEBUGVAR(1,65,"fill_element(): count")); | |
971 if (count == 15) | |
972 { | |
973 count += (uint16_t)faad_getbits(ld, 8 | |
974 DEBUGVAR(1,66,"fill_element(): extra count")) - 1; | |
975 } | |
976 | |
977 if (count > 0) | |
978 { | |
979 #ifdef SBR_DEC | |
980 bs_extension_type = (uint8_t)faad_showbits(ld, 4); | |
981 | |
982 if ((bs_extension_type == EXT_SBR_DATA) || | |
983 (bs_extension_type == EXT_SBR_DATA_CRC)) | |
984 { | |
12527 | 985 if (sbr_ele == INVALID_SBR_ELEMENT) |
986 return 24; | |
10725 | 987 |
988 if (!hDecoder->sbr[sbr_ele]) | |
10989 | 989 { |
12527 | 990 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength, |
991 hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index) | |
10989 | 992 #ifdef DRM |
993 , 0 | |
994 #endif | |
995 ); | |
996 } | |
10725 | 997 |
12527 | 998 hDecoder->sbr_present_flag = 1; |
999 | |
1000 /* parse the SBR data */ | |
1001 hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count); | |
1002 #if (defined(PS_DEC) || defined(DRM_PS)) | |
1003 if (hDecoder->sbr[sbr_ele]->ps_used) | |
1004 { | |
1005 hDecoder->ps_used[sbr_ele] = 1; | |
1006 } | |
1007 #endif | |
10725 | 1008 } else { |
1009 #endif | |
1010 while (count > 0) | |
1011 { | |
1012 count -= extension_payload(ld, drc, count); | |
1013 } | |
1014 #ifdef SBR_DEC | |
1015 } | |
1016 #endif | |
1017 } | |
1018 | |
1019 return 0; | |
1020 } | |
1021 | |
1022 /* Table 4.4.12 */ | |
1023 #ifdef SSR_DEC | |
1024 static void gain_control_data(bitfile *ld, ic_stream *ics) | |
1025 { | |
1026 uint8_t bd, wd, ad; | |
1027 ssr_info *ssr = &(ics->ssr); | |
1028 | |
1029 ssr->max_band = (uint8_t)faad_getbits(ld, 2 | |
1030 DEBUGVAR(1,1000,"gain_control_data(): max_band")); | |
1031 | |
1032 if (ics->window_sequence == ONLY_LONG_SEQUENCE) | |
1033 { | |
1034 for (bd = 1; bd <= ssr->max_band; bd++) | |
1035 { | |
1036 for (wd = 0; wd < 1; wd++) | |
1037 { | |
1038 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 | |
1039 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); | |
1040 | |
1041 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) | |
1042 { | |
1043 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 | |
1044 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); | |
1045 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5 | |
1046 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); | |
1047 } | |
1048 } | |
1049 } | |
1050 } else if (ics->window_sequence == LONG_START_SEQUENCE) { | |
1051 for (bd = 1; bd <= ssr->max_band; bd++) | |
1052 { | |
1053 for (wd = 0; wd < 2; wd++) | |
1054 { | |
1055 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 | |
1056 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); | |
1057 | |
1058 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) | |
1059 { | |
1060 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 | |
1061 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); | |
1062 if (wd == 0) | |
1063 { | |
1064 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 | |
1065 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); | |
1066 } else { | |
1067 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2 | |
1068 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); | |
1069 } | |
1070 } | |
1071 } | |
1072 } | |
1073 } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) { | |
1074 for (bd = 1; bd <= ssr->max_band; bd++) | |
1075 { | |
1076 for (wd = 0; wd < 8; wd++) | |
1077 { | |
1078 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 | |
1079 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); | |
1080 | |
1081 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) | |
1082 { | |
1083 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 | |
1084 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); | |
1085 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2 | |
1086 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); | |
1087 } | |
1088 } | |
1089 } | |
1090 } else if (ics->window_sequence == LONG_STOP_SEQUENCE) { | |
1091 for (bd = 1; bd <= ssr->max_band; bd++) | |
1092 { | |
1093 for (wd = 0; wd < 2; wd++) | |
1094 { | |
1095 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3 | |
1096 DEBUGVAR(1,1001,"gain_control_data(): adjust_num")); | |
1097 | |
1098 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++) | |
1099 { | |
1100 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 | |
1101 DEBUGVAR(1,1002,"gain_control_data(): alevcode")); | |
1102 | |
1103 if (wd == 0) | |
1104 { | |
1105 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4 | |
1106 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); | |
1107 } else { | |
1108 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5 | |
1109 DEBUGVAR(1,1003,"gain_control_data(): aloccode")); | |
1110 } | |
1111 } | |
1112 } | |
1113 } | |
1114 } | |
1115 } | |
1116 #endif | |
1117 | |
12527 | 1118 #ifdef SCALABLE_DEC |
1119 /* Table 4.4.13 ASME */ | |
1120 void aac_scalable_main_element(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, | |
1121 bitfile *ld, program_config *pce, drc_info *drc) | |
1122 { | |
1123 uint8_t retval = 0; | |
1124 uint8_t channels = hDecoder->fr_channels = 0; | |
1125 uint8_t ch; | |
1126 uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0; | |
1127 element cpe = {0}; | |
1128 ic_stream *ics1 = &(cpe.ics1); | |
1129 ic_stream *ics2 = &(cpe.ics2); | |
1130 int16_t *spec_data; | |
1131 ALIGN int16_t spec_data1[1024] = {0}; | |
1132 ALIGN int16_t spec_data2[1024] = {0}; | |
1133 | |
1134 hDecoder->fr_ch_ele = 0; | |
1135 | |
1136 hInfo->error = aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo); | |
1137 if (hInfo->error > 0) | |
1138 return; | |
1139 | |
1140 cpe.common_window = 1; | |
1141 if (this_layer_stereo) | |
1142 cpe.ele_id = ID_CPE; | |
1143 else | |
1144 cpe.ele_id = ID_SCE; | |
1145 | |
1146 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = (this_layer_stereo ? 2 : 0); | |
1147 | |
1148 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++) | |
1149 { | |
1150 ic_stream *ics; | |
1151 if (ch == 0) | |
1152 { | |
1153 ics = ics1; | |
1154 spec_data = spec_data1; | |
1155 } else { | |
1156 ics = ics2; | |
1157 spec_data = spec_data2; | |
1158 } | |
1159 | |
1160 hInfo->error = individual_channel_stream(hDecoder, &cpe, ld, ics, 1, spec_data); | |
1161 if (hInfo->error > 0) | |
1162 return; | |
1163 } | |
1164 | |
1165 #ifdef DRM | |
1166 #ifdef SBR_DEC | |
1167 /* In case of DRM we need to read the SBR info before channel reconstruction */ | |
1168 if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC)) | |
1169 { | |
1170 bitfile ld_sbr = {0}; | |
1171 uint32_t i; | |
1172 uint16_t count = 0; | |
1173 uint8_t *revbuffer; | |
1174 uint8_t *prevbufstart; | |
1175 uint8_t *pbufend; | |
1176 | |
1177 /* all forward bitreading should be finished at this point */ | |
1178 uint32_t bitsconsumed = faad_get_processed_bits(ld); | |
1179 uint32_t buffer_size = faad_origbitbuffer_size(ld); | |
1180 uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld); | |
1181 | |
1182 if (bitsconsumed + 8 > buffer_size*8) | |
1183 { | |
1184 hInfo->error = 14; | |
1185 return; | |
1186 } | |
1187 | |
1188 if (!hDecoder->sbr[0]) | |
1189 { | |
1190 hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, cpe.ele_id, | |
1191 2*get_sample_rate(hDecoder->sf_index), 1); | |
1192 } | |
1193 | |
1194 /* Reverse bit reading of SBR data in DRM audio frame */ | |
1195 revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t)); | |
1196 prevbufstart = revbuffer; | |
1197 pbufend = &buffer[buffer_size - 1]; | |
1198 for (i = 0; i < buffer_size; i++) | |
1199 *prevbufstart++ = tabFlipbits[*pbufend--]; | |
1200 | |
1201 /* Set SBR data */ | |
1202 /* consider 8 bits from AAC-CRC */ | |
1203 count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed); | |
1204 faad_initbits(&ld_sbr, revbuffer, count); | |
1205 | |
1206 hDecoder->sbr[0]->lcstereo_flag = hDecoder->lcstereo_flag; | |
1207 | |
1208 hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index); | |
1209 hDecoder->sbr[0]->sample_rate *= 2; | |
1210 | |
1211 faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */ | |
1212 | |
1213 hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count); | |
1214 #if (defined(PS_DEC) || defined(DRM_PS)) | |
1215 if (hDecoder->sbr[0]->ps_used) | |
1216 { | |
1217 hDecoder->ps_used[0] = 1; | |
1218 } | |
1219 #endif | |
1220 | |
1221 /* check CRC */ | |
1222 /* no need to check it if there was already an error */ | |
1223 if (hDecoder->sbr[0]->ret == 0) | |
1224 hDecoder->sbr[0]->ret = faad_check_CRC(&ld_sbr, faad_get_processed_bits(&ld_sbr) - 8); | |
1225 | |
1226 faad_endbits(&ld_sbr); | |
1227 | |
1228 if (revbuffer) | |
1229 faad_free(revbuffer); | |
1230 } | |
1231 #endif | |
1232 #endif | |
1233 | |
1234 if (this_layer_stereo) | |
1235 { | |
1236 hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2); | |
1237 if (hInfo->error > 0) | |
1238 return; | |
1239 } else { | |
1240 hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1); | |
1241 if (hInfo->error > 0) | |
1242 return; | |
1243 } | |
1244 | |
1245 /* map output channels position to internal data channels */ | |
1246 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2) | |
1247 { | |
1248 /* this might be faulty when pce_set is true */ | |
1249 hDecoder->internal_channel[channels] = channels; | |
1250 hDecoder->internal_channel[channels+1] = channels+1; | |
1251 } else { | |
1252 hDecoder->internal_channel[channels] = channels; | |
1253 } | |
1254 | |
1255 hDecoder->element_id[hDecoder->fr_ch_ele] = cpe.ele_id; | |
1256 | |
1257 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele]; | |
1258 hDecoder->fr_ch_ele++; | |
1259 | |
1260 return; | |
1261 } | |
1262 | |
1263 /* Table 4.4.15 */ | |
1264 static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2, | |
1265 bitfile *ld, uint8_t this_layer_stereo) | |
1266 { | |
1267 uint8_t retval = 0; | |
1268 uint8_t ch; | |
1269 ic_stream *ics; | |
1270 | |
1271 /* ics1->ics_reserved_bit = */ faad_get1bit(ld | |
1272 DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits")); | |
1273 ics1->window_sequence = (uint8_t)faad_getbits(ld, 2 | |
1274 DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence")); | |
1275 ics1->window_shape = faad_get1bit(ld | |
1276 DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape")); | |
1277 | |
1278 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE) | |
1279 { | |
1280 ics1->max_sfb = (uint8_t)faad_getbits(ld, 4 | |
1281 DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)")); | |
1282 ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7 | |
1283 DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping")); | |
1284 } else { | |
1285 ics1->max_sfb = (uint8_t)faad_getbits(ld, 6 | |
1286 DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)")); | |
1287 } | |
1288 | |
1289 /* get the grouping information */ | |
1290 if ((retval = window_grouping_info(hDecoder, ics1)) > 0) | |
1291 return retval; | |
1292 | |
1293 /* should be an error */ | |
1294 /* check the range of max_sfb */ | |
1295 if (ics1->max_sfb > ics1->num_swb) | |
1296 return 16; | |
1297 | |
1298 if (this_layer_stereo) | |
1299 { | |
1300 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2 | |
1301 DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present")); | |
1302 if (ics1->ms_mask_present == 1) | |
1303 { | |
1304 uint8_t g, sfb; | |
1305 for (g = 0; g < ics1->num_window_groups; g++) | |
1306 { | |
1307 for (sfb = 0; sfb < ics1->max_sfb; sfb++) | |
1308 { | |
1309 ics1->ms_used[g][sfb] = faad_get1bit(ld | |
1310 DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit")); | |
1311 } | |
1312 } | |
1313 } | |
1314 | |
1315 memcpy(ics2, ics1, sizeof(ic_stream)); | |
1316 } else { | |
1317 ics1->ms_mask_present = 0; | |
1318 } | |
1319 | |
1320 if (0) | |
1321 { | |
1322 faad_get1bit(ld | |
1323 DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer")); | |
1324 } | |
1325 | |
1326 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++) | |
1327 { | |
1328 if (ch == 0) | |
1329 ics = ics1; | |
1330 else | |
1331 ics = ics2; | |
1332 | |
1333 if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/) | |
1334 { | |
1335 if ((ics->tns_data_present = faad_get1bit(ld | |
1336 DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1) | |
1337 { | |
1338 #ifdef DRM | |
1339 /* different order of data units in DRM */ | |
1340 if (hDecoder->object_type != DRM_ER_LC) | |
1341 #endif | |
1342 { | |
1343 tns_data(ics, &(ics->tns), ld); | |
1344 } | |
1345 } | |
1346 } | |
1347 #if 0 | |
1348 if (0 /*core_flag || tvq_layer_pesent*/) | |
1349 { | |
1350 if ((ch==0) || ((ch==1) && (core_stereo || tvq_stereo)) | |
1351 diff_control_data(); | |
1352 if (mono_stereo_flag) | |
1353 diff_control_data_lr(); | |
1354 } else { | |
1355 #endif | |
1356 if ((ics->ltp.data_present = faad_get1bit(ld | |
1357 DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1) | |
1358 { | |
1359 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0) | |
1360 { | |
1361 return retval; | |
1362 } | |
1363 } | |
1364 #if 0 | |
1365 } | |
1366 #endif | |
1367 } | |
1368 | |
1369 return 0; | |
1370 } | |
1371 #endif | |
1372 | |
10725 | 1373 /* Table 4.4.24 */ |
1374 static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele, | |
1375 bitfile *ld, ic_stream *ics, uint8_t scal_flag, | |
1376 int16_t *spec_data) | |
1377 { | |
1378 uint8_t result; | |
1379 | |
1380 ics->global_gain = (uint8_t)faad_getbits(ld, 8 | |
1381 DEBUGVAR(1,67,"individual_channel_stream(): global_gain")); | |
1382 | |
1383 if (!ele->common_window && !scal_flag) | |
1384 { | |
1385 if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0) | |
1386 return result; | |
1387 } | |
1388 | |
1389 if ((result = section_data(hDecoder, ics, ld)) > 0) | |
1390 return result; | |
1391 | |
1392 if ((result = scale_factor_data(hDecoder, ics, ld)) > 0) | |
1393 return result; | |
1394 | |
1395 if (!scal_flag) | |
1396 { | |
1397 /** | |
1398 ** NOTE: It could be that pulse data is available in scalable AAC too, | |
1399 ** as said in Amendment 1, this could be only the case for ER AAC, | |
1400 ** though. (have to check this out later) | |
1401 **/ | |
1402 /* get pulse data */ | |
1403 if ((ics->pulse_data_present = faad_get1bit(ld | |
1404 DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1) | |
1405 { | |
1406 if ((result = pulse_data(ics, &(ics->pul), ld)) > 0) | |
1407 return result; | |
1408 } | |
1409 | |
1410 /* get tns data */ | |
1411 if ((ics->tns_data_present = faad_get1bit(ld | |
1412 DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1) | |
1413 { | |
1414 #ifdef ERROR_RESILIENCE | |
1415 if (hDecoder->object_type < ER_OBJECT_START) | |
1416 #endif | |
1417 tns_data(ics, &(ics->tns), ld); | |
1418 } | |
1419 | |
1420 /* get gain control data */ | |
1421 if ((ics->gain_control_data_present = faad_get1bit(ld | |
1422 DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1) | |
1423 { | |
1424 #ifdef SSR_DEC | |
1425 if (hDecoder->object_type != SSR) | |
1426 return 1; | |
1427 else | |
1428 gain_control_data(ld, ics); | |
1429 #else | |
1430 return 1; | |
1431 #endif | |
1432 } | |
1433 } | |
1434 | |
1435 #ifdef ERROR_RESILIENCE | |
1436 if (hDecoder->aacSpectralDataResilienceFlag) | |
1437 { | |
1438 ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14 | |
1439 DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data")); | |
1440 | |
1441 if (hDecoder->channelConfiguration == 2) | |
1442 { | |
1443 if (ics->length_of_reordered_spectral_data > 6144) | |
1444 ics->length_of_reordered_spectral_data = 6144; | |
1445 } else { | |
1446 if (ics->length_of_reordered_spectral_data > 12288) | |
1447 ics->length_of_reordered_spectral_data = 12288; | |
1448 } | |
1449 | |
1450 ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6 | |
1451 DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword")); | |
1452 if (ics->length_of_longest_codeword >= 49) | |
1453 ics->length_of_longest_codeword = 49; | |
1454 } | |
1455 | |
1456 /* RVLC spectral data is put here */ | |
1457 if (hDecoder->aacScalefactorDataResilienceFlag) | |
1458 { | |
1459 if ((result = rvlc_decode_scale_factors(ics, ld)) > 0) | |
1460 return result; | |
1461 } | |
1462 | |
1463 if (hDecoder->object_type >= ER_OBJECT_START) | |
1464 { | |
1465 if (ics->tns_data_present) | |
1466 tns_data(ics, &(ics->tns), ld); | |
1467 } | |
1468 | |
12527 | 1469 #ifdef DRM |
1470 /* CRC check */ | |
1471 if (hDecoder->object_type == DRM_ER_LC) | |
1472 if ((result = faad_check_CRC(ld, faad_get_processed_bits(ld) - 8)) > 0) | |
1473 return result; | |
1474 #endif | |
1475 | |
10725 | 1476 if (hDecoder->aacSpectralDataResilienceFlag) |
1477 { | |
1478 /* error resilient spectral data decoding */ | |
1479 if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0) | |
1480 { | |
1481 return result; | |
1482 } | |
1483 } else { | |
1484 #endif | |
1485 /* decode the spectral data */ | |
1486 if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0) | |
1487 { | |
1488 return result; | |
1489 } | |
1490 #ifdef ERROR_RESILIENCE | |
1491 } | |
1492 #endif | |
1493 | |
1494 /* pulse coding reconstruction */ | |
1495 if (ics->pulse_data_present) | |
1496 { | |
1497 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE) | |
1498 { | |
1499 if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0) | |
1500 return result; | |
1501 } else { | |
1502 return 2; /* pulse coding not allowed for short blocks */ | |
1503 } | |
1504 } | |
1505 | |
1506 return 0; | |
1507 } | |
1508 | |
1509 /* Table 4.4.25 */ | |
1510 static uint8_t section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld) | |
1511 { | |
1512 uint8_t g; | |
1513 uint8_t sect_esc_val, sect_bits; | |
1514 | |
1515 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
1516 sect_bits = 3; | |
1517 else | |
1518 sect_bits = 5; | |
1519 sect_esc_val = (1<<sect_bits) - 1; | |
1520 | |
1521 #if 0 | |
1522 printf("\ntotal sfb %d\n", ics->max_sfb); | |
1523 printf(" sect top cb\n"); | |
1524 #endif | |
1525 | |
1526 for (g = 0; g < ics->num_window_groups; g++) | |
1527 { | |
1528 uint8_t k = 0; | |
1529 uint8_t i = 0; | |
1530 | |
1531 while (k < ics->max_sfb) | |
1532 { | |
1533 #ifdef ERROR_RESILIENCE | |
1534 uint8_t vcb11 = 0; | |
1535 #endif | |
1536 uint8_t sfb; | |
1537 uint8_t sect_len_incr; | |
1538 uint16_t sect_len = 0; | |
1539 uint8_t sect_cb_bits = 4; | |
1540 | |
1541 /* if "faad_getbits" detects error and returns "0", "k" is never | |
1542 incremented and we cannot leave the while loop */ | |
1543 if ((ld->error != 0) || (ld->no_more_reading)) | |
1544 return 14; | |
1545 | |
1546 #ifdef ERROR_RESILIENCE | |
1547 if (hDecoder->aacSectionDataResilienceFlag) | |
1548 sect_cb_bits = 5; | |
1549 #endif | |
1550 | |
1551 ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits | |
1552 DEBUGVAR(1,71,"section_data(): sect_cb")); | |
1553 | |
1554 if (ics->sect_cb[g][i] == NOISE_HCB) | |
1555 ics->noise_used = 1; | |
1556 | |
1557 #ifdef ERROR_RESILIENCE | |
1558 if (hDecoder->aacSectionDataResilienceFlag) | |
1559 { | |
1560 if ((ics->sect_cb[g][i] == 11) || | |
1561 ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32))) | |
1562 { | |
1563 vcb11 = 1; | |
1564 } | |
1565 } | |
1566 if (vcb11) | |
1567 { | |
1568 sect_len_incr = 1; | |
1569 } else { | |
1570 #endif | |
1571 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits | |
1572 DEBUGVAR(1,72,"section_data(): sect_len_incr")); | |
1573 #ifdef ERROR_RESILIENCE | |
1574 } | |
1575 #endif | |
1576 while ((sect_len_incr == sect_esc_val) /* && | |
1577 (k+sect_len < ics->max_sfb)*/) | |
1578 { | |
1579 sect_len += sect_len_incr; | |
1580 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits | |
1581 DEBUGVAR(1,72,"section_data(): sect_len_incr")); | |
1582 } | |
1583 | |
1584 sect_len += sect_len_incr; | |
1585 | |
1586 ics->sect_start[g][i] = k; | |
1587 ics->sect_end[g][i] = k + sect_len; | |
1588 | |
1589 if (k + sect_len >= 8*15) | |
1590 return 15; | |
1591 if (i >= 8*15) | |
1592 return 15; | |
1593 | |
1594 for (sfb = k; sfb < k + sect_len; sfb++) | |
1595 ics->sfb_cb[g][sfb] = ics->sect_cb[g][i]; | |
1596 | |
1597 #if 0 | |
1598 printf(" %6d %6d %6d\n", | |
1599 i, | |
1600 ics->sect_end[g][i], | |
1601 ics->sect_cb[g][i]); | |
1602 #endif | |
1603 | |
1604 k += sect_len; | |
1605 i++; | |
1606 } | |
1607 ics->num_sec[g] = i; | |
1608 } | |
1609 | |
1610 #if 0 | |
1611 printf("\n"); | |
1612 #endif | |
1613 | |
1614 return 0; | |
1615 } | |
1616 | |
1617 /* | |
1618 * decode_scale_factors() | |
1619 * decodes the scalefactors from the bitstream | |
1620 */ | |
1621 /* | |
1622 * All scalefactors (and also the stereo positions and pns energies) are | |
1623 * transmitted using Huffman coded DPCM relative to the previous active | |
1624 * scalefactor (respectively previous stereo position or previous pns energy, | |
1625 * see subclause 4.6.2 and 4.6.3). The first active scalefactor is | |
1626 * differentially coded relative to the global gain. | |
1627 */ | |
1628 static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld) | |
1629 { | |
1630 uint8_t g, sfb; | |
1631 int16_t t; | |
1632 int8_t noise_pcm_flag = 1; | |
1633 | |
1634 int16_t scale_factor = ics->global_gain; | |
1635 int16_t is_position = 0; | |
1636 int16_t noise_energy = ics->global_gain - 90; | |
1637 | |
1638 for (g = 0; g < ics->num_window_groups; g++) | |
1639 { | |
1640 for (sfb = 0; sfb < ics->max_sfb; sfb++) | |
1641 { | |
1642 switch (ics->sfb_cb[g][sfb]) | |
1643 { | |
1644 case ZERO_HCB: /* zero book */ | |
1645 ics->scale_factors[g][sfb] = 0; | |
1646 break; | |
1647 case INTENSITY_HCB: /* intensity books */ | |
1648 case INTENSITY_HCB2: | |
1649 | |
1650 /* decode intensity position */ | |
1651 t = huffman_scale_factor(ld); | |
1652 is_position += (t - 60); | |
1653 ics->scale_factors[g][sfb] = is_position; | |
1654 | |
1655 break; | |
1656 case NOISE_HCB: /* noise books */ | |
1657 | |
1658 /* decode noise energy */ | |
1659 if (noise_pcm_flag) | |
1660 { | |
1661 noise_pcm_flag = 0; | |
1662 t = (int16_t)faad_getbits(ld, 9 | |
1663 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256; | |
1664 } else { | |
1665 t = huffman_scale_factor(ld); | |
1666 t -= 60; | |
1667 } | |
1668 noise_energy += t; | |
1669 ics->scale_factors[g][sfb] = noise_energy; | |
1670 | |
1671 break; | |
1672 default: /* spectral books */ | |
1673 | |
12527 | 1674 /* ics->scale_factors[g][sfb] must be between 0 and 255 */ |
1675 | |
1676 ics->scale_factors[g][sfb] = 0; | |
1677 | |
10725 | 1678 /* decode scale factor */ |
1679 t = huffman_scale_factor(ld); | |
1680 scale_factor += (t - 60); | |
12527 | 1681 if (scale_factor < 0 || scale_factor > 255) |
10725 | 1682 return 4; |
1683 ics->scale_factors[g][sfb] = scale_factor; | |
1684 | |
1685 break; | |
1686 } | |
1687 } | |
1688 } | |
1689 | |
1690 return 0; | |
1691 } | |
1692 | |
1693 /* Table 4.4.26 */ | |
1694 static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld) | |
1695 { | |
12527 | 1696 uint8_t ret = 0; |
1697 #ifdef PROFILE | |
1698 int64_t count = faad_get_ts(); | |
1699 #endif | |
1700 | |
10725 | 1701 #ifdef ERROR_RESILIENCE |
1702 if (!hDecoder->aacScalefactorDataResilienceFlag) | |
1703 { | |
1704 #endif | |
12527 | 1705 ret = decode_scale_factors(ics, ld); |
10725 | 1706 #ifdef ERROR_RESILIENCE |
1707 } else { | |
1708 /* In ER AAC the parameters for RVLC are seperated from the actual | |
1709 data that holds the scale_factors. | |
1710 Strangely enough, 2 parameters for HCR are put inbetween them. | |
1711 */ | |
12527 | 1712 ret = rvlc_scale_factor_data(ics, ld); |
10725 | 1713 } |
1714 #endif | |
12527 | 1715 |
1716 #ifdef PROFILE | |
1717 count = faad_get_ts() - count; | |
1718 hDecoder->scalefac_cycles += count; | |
1719 #endif | |
1720 | |
1721 return ret; | |
10725 | 1722 } |
1723 | |
1724 /* Table 4.4.27 */ | |
1725 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld) | |
1726 { | |
1727 uint8_t w, filt, i, start_coef_bits, coef_bits; | |
1728 uint8_t n_filt_bits = 2; | |
1729 uint8_t length_bits = 6; | |
1730 uint8_t order_bits = 5; | |
1731 | |
1732 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
1733 { | |
1734 n_filt_bits = 1; | |
1735 length_bits = 4; | |
1736 order_bits = 3; | |
1737 } | |
1738 | |
1739 for (w = 0; w < ics->num_windows; w++) | |
1740 { | |
1741 tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits | |
1742 DEBUGVAR(1,74,"tns_data(): n_filt")); | |
1743 | |
1744 if (tns->n_filt[w]) | |
1745 { | |
1746 if ((tns->coef_res[w] = faad_get1bit(ld | |
1747 DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1) | |
1748 { | |
1749 start_coef_bits = 4; | |
1750 } else { | |
1751 start_coef_bits = 3; | |
1752 } | |
1753 } | |
1754 | |
1755 for (filt = 0; filt < tns->n_filt[w]; filt++) | |
1756 { | |
1757 tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits | |
1758 DEBUGVAR(1,76,"tns_data(): length")); | |
1759 tns->order[w][filt] = (uint8_t)faad_getbits(ld, order_bits | |
1760 DEBUGVAR(1,77,"tns_data(): order")); | |
1761 if (tns->order[w][filt]) | |
1762 { | |
1763 tns->direction[w][filt] = faad_get1bit(ld | |
1764 DEBUGVAR(1,78,"tns_data(): direction")); | |
1765 tns->coef_compress[w][filt] = faad_get1bit(ld | |
1766 DEBUGVAR(1,79,"tns_data(): coef_compress")); | |
1767 | |
1768 coef_bits = start_coef_bits - tns->coef_compress[w][filt]; | |
1769 for (i = 0; i < tns->order[w][filt]; i++) | |
1770 { | |
1771 tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits | |
1772 DEBUGVAR(1,80,"tns_data(): coef")); | |
1773 } | |
1774 } | |
1775 } | |
1776 } | |
1777 } | |
1778 | |
1779 #ifdef LTP_DEC | |
1780 /* Table 4.4.28 */ | |
12527 | 1781 static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld) |
10725 | 1782 { |
1783 uint8_t sfb, w; | |
1784 | |
12527 | 1785 ltp->lag = 0; |
1786 | |
10725 | 1787 #ifdef LD_DEC |
1788 if (hDecoder->object_type == LD) | |
1789 { | |
1790 ltp->lag_update = (uint8_t)faad_getbits(ld, 1 | |
1791 DEBUGVAR(1,142,"ltp_data(): lag_update")); | |
1792 | |
1793 if (ltp->lag_update) | |
1794 { | |
1795 ltp->lag = (uint16_t)faad_getbits(ld, 10 | |
1796 DEBUGVAR(1,81,"ltp_data(): lag")); | |
1797 } | |
1798 } else { | |
1799 #endif | |
1800 ltp->lag = (uint16_t)faad_getbits(ld, 11 | |
1801 DEBUGVAR(1,81,"ltp_data(): lag")); | |
1802 #ifdef LD_DEC | |
1803 } | |
1804 #endif | |
12527 | 1805 |
1806 /* Check length of lag */ | |
1807 if (ltp->lag > (hDecoder->frameLength << 1)) | |
1808 return 18; | |
1809 | |
10725 | 1810 ltp->coef = (uint8_t)faad_getbits(ld, 3 |
1811 DEBUGVAR(1,82,"ltp_data(): coef")); | |
1812 | |
1813 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
1814 { | |
1815 for (w = 0; w < ics->num_windows; w++) | |
1816 { | |
1817 if ((ltp->short_used[w] = faad_get1bit(ld | |
1818 DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1) | |
1819 { | |
1820 ltp->short_lag_present[w] = faad_get1bit(ld | |
1821 DEBUGVAR(1,84,"ltp_data(): short_lag_present")); | |
1822 if (ltp->short_lag_present[w]) | |
1823 { | |
1824 ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4 | |
1825 DEBUGVAR(1,85,"ltp_data(): short_lag")); | |
1826 } | |
1827 } | |
1828 } | |
1829 } else { | |
1830 ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB); | |
1831 | |
1832 for (sfb = 0; sfb < ltp->last_band; sfb++) | |
1833 { | |
1834 ltp->long_used[sfb] = faad_get1bit(ld | |
1835 DEBUGVAR(1,86,"ltp_data(): long_used")); | |
1836 } | |
1837 } | |
12527 | 1838 |
1839 return 0; | |
10725 | 1840 } |
1841 #endif | |
1842 | |
1843 /* Table 4.4.29 */ | |
1844 static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, | |
1845 int16_t *spectral_data) | |
1846 { | |
1847 int8_t i; | |
1848 uint8_t g; | |
12527 | 1849 uint16_t inc, k, p = 0; |
10725 | 1850 uint8_t groups = 0; |
1851 uint8_t sect_cb; | |
1852 uint8_t result; | |
1853 uint16_t nshort = hDecoder->frameLength/8; | |
1854 | |
12527 | 1855 #ifdef PROFILE |
1856 int64_t count = faad_get_ts(); | |
1857 #endif | |
10725 | 1858 |
1859 for(g = 0; g < ics->num_window_groups; g++) | |
1860 { | |
1861 p = groups*nshort; | |
1862 | |
1863 for (i = 0; i < ics->num_sec[g]; i++) | |
1864 { | |
1865 sect_cb = ics->sect_cb[g][i]; | |
1866 | |
12527 | 1867 inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4; |
1868 | |
10725 | 1869 switch (sect_cb) |
1870 { | |
1871 case ZERO_HCB: | |
1872 case NOISE_HCB: | |
1873 case INTENSITY_HCB: | |
1874 case INTENSITY_HCB2: | |
1875 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] - | |
1876 ics->sect_sfb_offset[g][ics->sect_start[g][i]]); | |
1877 break; | |
1878 default: | |
1879 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; | |
12527 | 1880 k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc) |
10725 | 1881 { |
12527 | 1882 if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0) |
1883 return result; | |
1884 p += inc; | |
10725 | 1885 } |
1886 break; | |
1887 } | |
1888 } | |
1889 groups += ics->window_group_length[g]; | |
1890 } | |
1891 | |
12527 | 1892 #ifdef PROFILE |
1893 count = faad_get_ts() - count; | |
1894 hDecoder->spectral_cycles += count; | |
1895 #endif | |
1896 | |
10725 | 1897 return 0; |
1898 } | |
1899 | |
1900 /* Table 4.4.30 */ | |
1901 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count) | |
1902 { | |
1903 uint16_t i, n, dataElementLength; | |
1904 uint8_t dataElementLengthPart; | |
1905 uint8_t align = 4, data_element_version, loopCounter; | |
1906 | |
1907 uint8_t extension_type = (uint8_t)faad_getbits(ld, 4 | |
1908 DEBUGVAR(1,87,"extension_payload(): extension_type")); | |
1909 | |
1910 switch (extension_type) | |
1911 { | |
1912 case EXT_DYNAMIC_RANGE: | |
1913 drc->present = 1; | |
1914 n = dynamic_range_info(ld, drc); | |
1915 return n; | |
1916 case EXT_FILL_DATA: | |
1917 /* fill_nibble = */ faad_getbits(ld, 4 | |
1918 DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be ‘0000’ */ | |
1919 for (i = 0; i < count-1; i++) | |
1920 { | |
1921 /* fill_byte[i] = */ faad_getbits(ld, 8 | |
1922 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ‘10100101’ */ | |
1923 } | |
1924 return count; | |
1925 case EXT_DATA_ELEMENT: | |
12527 | 1926 data_element_version = (uint8_t)faad_getbits(ld, 4 |
10725 | 1927 DEBUGVAR(1,400,"extension_payload(): data_element_version")); |
1928 switch (data_element_version) | |
1929 { | |
1930 case ANC_DATA: | |
1931 loopCounter = 0; | |
1932 dataElementLength = 0; | |
1933 do { | |
12527 | 1934 dataElementLengthPart = (uint8_t)faad_getbits(ld, 8 |
10725 | 1935 DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart")); |
1936 dataElementLength += dataElementLengthPart; | |
1937 loopCounter++; | |
1938 } while (dataElementLengthPart == 255); | |
1939 | |
1940 for (i = 0; i < dataElementLength; i++) | |
1941 { | |
1942 /* data_element_byte[i] = */ faad_getbits(ld, 8 | |
1943 DEBUGVAR(1,402,"extension_payload(): data_element_byte")); | |
1944 return (dataElementLength+loopCounter+1); | |
1945 } | |
1946 default: | |
1947 align = 0; | |
1948 } | |
1949 case EXT_FIL: | |
1950 default: | |
1951 faad_getbits(ld, align | |
1952 DEBUGVAR(1,88,"extension_payload(): fill_nibble")); | |
1953 for (i = 0; i < count-1; i++) | |
1954 { | |
1955 /* other_bits[i] = */ faad_getbits(ld, 8 | |
1956 DEBUGVAR(1,89,"extension_payload(): fill_bit")); | |
1957 } | |
1958 return count; | |
1959 } | |
1960 } | |
1961 | |
1962 /* Table 4.4.31 */ | |
1963 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc) | |
1964 { | |
1965 uint8_t i, n = 1; | |
1966 uint8_t band_incr; | |
1967 | |
1968 drc->num_bands = 1; | |
1969 | |
1970 if (faad_get1bit(ld | |
1971 DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1) | |
1972 { | |
1973 drc->pce_instance_tag = (uint8_t)faad_getbits(ld, 4 | |
1974 DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag")); | |
1975 /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4 | |
1976 DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits")); | |
1977 n++; | |
1978 } | |
1979 | |
1980 drc->excluded_chns_present = faad_get1bit(ld | |
1981 DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present")); | |
1982 if (drc->excluded_chns_present == 1) | |
1983 { | |
1984 n += excluded_channels(ld, drc); | |
1985 } | |
1986 | |
1987 if (faad_get1bit(ld | |
1988 DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1) | |
1989 { | |
1990 band_incr = (uint8_t)faad_getbits(ld, 4 | |
1991 DEBUGVAR(1,95,"dynamic_range_info(): band_incr")); | |
1992 /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4 | |
1993 DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits")); | |
1994 n++; | |
1995 drc->num_bands += band_incr; | |
1996 | |
1997 for (i = 0; i < drc->num_bands; i++); | |
1998 { | |
1999 drc->band_top[i] = (uint8_t)faad_getbits(ld, 8 | |
2000 DEBUGVAR(1,97,"dynamic_range_info(): band_top")); | |
2001 n++; | |
2002 } | |
2003 } | |
2004 | |
2005 if (faad_get1bit(ld | |
2006 DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1) | |
2007 { | |
2008 drc->prog_ref_level = (uint8_t)faad_getbits(ld, 7 | |
2009 DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level")); | |
2010 /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld | |
2011 DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits")); | |
2012 n++; | |
2013 } | |
2014 | |
2015 for (i = 0; i < drc->num_bands; i++) | |
2016 { | |
2017 drc->dyn_rng_sgn[i] = faad_get1bit(ld | |
2018 DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn")); | |
2019 drc->dyn_rng_ctl[i] = (uint8_t)faad_getbits(ld, 7 | |
2020 DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl")); | |
2021 n++; | |
2022 } | |
2023 | |
2024 return n; | |
2025 } | |
2026 | |
2027 /* Table 4.4.32 */ | |
2028 static uint8_t excluded_channels(bitfile *ld, drc_info *drc) | |
2029 { | |
2030 uint8_t i, n = 0; | |
2031 uint8_t num_excl_chan = 7; | |
2032 | |
2033 for (i = 0; i < 7; i++) | |
2034 { | |
2035 drc->exclude_mask[i] = faad_get1bit(ld | |
2036 DEBUGVAR(1,103,"excluded_channels(): exclude_mask")); | |
2037 } | |
2038 n++; | |
2039 | |
2040 while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld | |
2041 DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1) | |
2042 { | |
2043 for (i = num_excl_chan; i < num_excl_chan+7; i++) | |
2044 { | |
2045 drc->exclude_mask[i] = faad_get1bit(ld | |
2046 DEBUGVAR(1,105,"excluded_channels(): exclude_mask")); | |
2047 } | |
2048 n++; | |
2049 num_excl_chan += 7; | |
2050 } | |
2051 | |
2052 return n; | |
2053 } | |
2054 | |
2055 /* Annex A: Audio Interchange Formats */ | |
2056 | |
2057 /* Table 1.A.2 */ | |
2058 void get_adif_header(adif_header *adif, bitfile *ld) | |
2059 { | |
2060 uint8_t i; | |
2061 | |
2062 /* adif_id[0] = */ faad_getbits(ld, 8 | |
2063 DEBUGVAR(1,106,"get_adif_header(): adif_id[0]")); | |
2064 /* adif_id[1] = */ faad_getbits(ld, 8 | |
2065 DEBUGVAR(1,107,"get_adif_header(): adif_id[1]")); | |
2066 /* adif_id[2] = */ faad_getbits(ld, 8 | |
2067 DEBUGVAR(1,108,"get_adif_header(): adif_id[2]")); | |
2068 /* adif_id[3] = */ faad_getbits(ld, 8 | |
2069 DEBUGVAR(1,109,"get_adif_header(): adif_id[3]")); | |
2070 adif->copyright_id_present = faad_get1bit(ld | |
2071 DEBUGVAR(1,110,"get_adif_header(): copyright_id_present")); | |
2072 if(adif->copyright_id_present) | |
2073 { | |
2074 for (i = 0; i < 72/8; i++) | |
2075 { | |
2076 adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8 | |
2077 DEBUGVAR(1,111,"get_adif_header(): copyright_id")); | |
2078 } | |
2079 adif->copyright_id[i] = 0; | |
2080 } | |
2081 adif->original_copy = faad_get1bit(ld | |
2082 DEBUGVAR(1,112,"get_adif_header(): original_copy")); | |
2083 adif->home = faad_get1bit(ld | |
2084 DEBUGVAR(1,113,"get_adif_header(): home")); | |
2085 adif->bitstream_type = faad_get1bit(ld | |
2086 DEBUGVAR(1,114,"get_adif_header(): bitstream_type")); | |
2087 adif->bitrate = faad_getbits(ld, 23 | |
2088 DEBUGVAR(1,115,"get_adif_header(): bitrate")); | |
2089 adif->num_program_config_elements = (uint8_t)faad_getbits(ld, 4 | |
2090 DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements")); | |
2091 | |
2092 for (i = 0; i < adif->num_program_config_elements + 1; i++) | |
2093 { | |
2094 if(adif->bitstream_type == 0) | |
2095 { | |
2096 adif->adif_buffer_fullness = faad_getbits(ld, 20 | |
2097 DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness")); | |
2098 } else { | |
2099 adif->adif_buffer_fullness = 0; | |
2100 } | |
2101 | |
2102 program_config_element(&adif->pce[i], ld); | |
2103 } | |
2104 } | |
2105 | |
2106 /* Table 1.A.5 */ | |
2107 uint8_t adts_frame(adts_header *adts, bitfile *ld) | |
2108 { | |
2109 /* faad_byte_align(ld); */ | |
2110 if (adts_fixed_header(adts, ld)) | |
2111 return 5; | |
2112 adts_variable_header(adts, ld); | |
2113 adts_error_check(adts, ld); | |
2114 | |
2115 return 0; | |
2116 } | |
2117 | |
2118 /* Table 1.A.6 */ | |
2119 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld) | |
2120 { | |
2121 uint16_t i; | |
2122 uint8_t sync_err = 1; | |
2123 | |
2124 /* try to recover from sync errors */ | |
2125 for (i = 0; i < 768; i++) | |
2126 { | |
2127 adts->syncword = (uint16_t)faad_showbits(ld, 12); | |
2128 if (adts->syncword != 0xFFF) | |
2129 { | |
2130 faad_getbits(ld, 8 | |
2131 DEBUGVAR(0,0,"")); | |
2132 } else { | |
2133 sync_err = 0; | |
2134 faad_getbits(ld, 12 | |
2135 DEBUGVAR(1,118,"adts_fixed_header(): syncword")); | |
2136 break; | |
2137 } | |
2138 } | |
2139 if (sync_err) | |
2140 return 5; | |
2141 | |
2142 adts->id = faad_get1bit(ld | |
2143 DEBUGVAR(1,119,"adts_fixed_header(): id")); | |
2144 adts->layer = (uint8_t)faad_getbits(ld, 2 | |
2145 DEBUGVAR(1,120,"adts_fixed_header(): layer")); | |
2146 adts->protection_absent = faad_get1bit(ld | |
2147 DEBUGVAR(1,121,"adts_fixed_header(): protection_absent")); | |
2148 adts->profile = (uint8_t)faad_getbits(ld, 2 | |
2149 DEBUGVAR(1,122,"adts_fixed_header(): profile")); | |
2150 adts->sf_index = (uint8_t)faad_getbits(ld, 4 | |
2151 DEBUGVAR(1,123,"adts_fixed_header(): sf_index")); | |
2152 adts->private_bit = faad_get1bit(ld | |
2153 DEBUGVAR(1,124,"adts_fixed_header(): private_bit")); | |
2154 adts->channel_configuration = (uint8_t)faad_getbits(ld, 3 | |
2155 DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration")); | |
2156 adts->original = faad_get1bit(ld | |
2157 DEBUGVAR(1,126,"adts_fixed_header(): original")); | |
2158 adts->home = faad_get1bit(ld | |
2159 DEBUGVAR(1,127,"adts_fixed_header(): home")); | |
10989 | 2160 |
2161 if (adts->old_format == 1) | |
2162 { | |
2163 /* Removed in corrigendum 14496-3:2002 */ | |
12527 | 2164 if (adts->id == 0) |
2165 { | |
2166 adts->emphasis = (uint8_t)faad_getbits(ld, 2 | |
2167 DEBUGVAR(1,128,"adts_fixed_header(): emphasis")); | |
2168 } | |
10989 | 2169 } |
10725 | 2170 |
2171 return 0; | |
2172 } | |
2173 | |
2174 /* Table 1.A.7 */ | |
2175 static void adts_variable_header(adts_header *adts, bitfile *ld) | |
2176 { | |
2177 adts->copyright_identification_bit = faad_get1bit(ld | |
2178 DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit")); | |
2179 adts->copyright_identification_start = faad_get1bit(ld | |
2180 DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start")); | |
2181 adts->aac_frame_length = (uint16_t)faad_getbits(ld, 13 | |
2182 DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length")); | |
2183 adts->adts_buffer_fullness = (uint16_t)faad_getbits(ld, 11 | |
2184 DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness")); | |
2185 adts->no_raw_data_blocks_in_frame = (uint8_t)faad_getbits(ld, 2 | |
2186 DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame")); | |
2187 } | |
2188 | |
2189 /* Table 1.A.8 */ | |
2190 static void adts_error_check(adts_header *adts, bitfile *ld) | |
2191 { | |
2192 if (adts->protection_absent == 0) | |
2193 { | |
2194 adts->crc_check = (uint16_t)faad_getbits(ld, 16 | |
2195 DEBUGVAR(1,134,"adts_error_check(): crc_check")); | |
2196 } | |
2197 } |