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