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