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