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