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