Mercurial > mplayer.hg
annotate libfaad2/decoder.c @ 13044:e99f09e67fb7
Sync with 1.123
author | rtognimp |
---|---|
date | Wed, 18 Aug 2004 15:11:57 +0000 |
parents | d81145997036 |
children | 6d50ef45a058 |
rev | line source |
---|---|
10725 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
12527 | 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com |
4 ** | |
10725 | 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. | |
12527 | 9 ** |
10725 | 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. | |
12527 | 14 ** |
10725 | 15 ** You should have received a copy of the GNU General Public License |
12527 | 16 ** along with this program; if not, write to the Free Software |
10725 | 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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
26 ** $Id: decoder.c,v 1.3 2004/06/02 22:59:02 diego Exp $ |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10725 | 28 **/ |
29 | |
30 #include "common.h" | |
31 #include "structs.h" | |
32 | |
33 #include <stdlib.h> | |
34 #include <string.h> | |
35 | |
36 #include "decoder.h" | |
37 #include "mp4.h" | |
38 #include "syntax.h" | |
39 #include "error.h" | |
40 #include "output.h" | |
12527 | 41 #include "filtbank.h" |
42 #include "drc.h" | |
43 #ifdef SBR_DEC | |
44 #include "sbr_dec.h" | |
45 #include "sbr_syntax.h" | |
46 #endif | |
10725 | 47 #ifdef SSR_DEC |
48 #include "ssr.h" | |
49 #endif | |
50 | |
51 #ifdef ANALYSIS | |
52 uint16_t dbg_count; | |
53 #endif | |
54 | |
55 int8_t* FAADAPI faacDecGetErrorMessage(uint8_t errcode) | |
56 { | |
57 if (errcode >= NUM_ERROR_MESSAGES) | |
58 return NULL; | |
59 return err_msg[errcode]; | |
60 } | |
61 | |
12527 | 62 uint32_t FAADAPI faacDecGetCapabilities(void) |
10725 | 63 { |
64 uint32_t cap = 0; | |
65 | |
66 /* can't do without it */ | |
67 cap += LC_DEC_CAP; | |
68 | |
69 #ifdef MAIN_DEC | |
70 cap += MAIN_DEC_CAP; | |
71 #endif | |
72 #ifdef LTP_DEC | |
73 cap += LTP_DEC_CAP; | |
74 #endif | |
75 #ifdef LD_DEC | |
76 cap += LD_DEC_CAP; | |
77 #endif | |
78 #ifdef ERROR_RESILIENCE | |
79 cap += ERROR_RESILIENCE_CAP; | |
80 #endif | |
81 #ifdef FIXED_POINT | |
82 cap += FIXED_POINT_CAP; | |
83 #endif | |
84 | |
85 return cap; | |
86 } | |
87 | |
12527 | 88 faacDecHandle FAADAPI faacDecOpen(void) |
10725 | 89 { |
90 uint8_t i; | |
91 faacDecHandle hDecoder = NULL; | |
92 | |
12527 | 93 if ((hDecoder = (faacDecHandle)faad_malloc(sizeof(faacDecStruct))) == NULL) |
10725 | 94 return NULL; |
95 | |
96 memset(hDecoder, 0, sizeof(faacDecStruct)); | |
97 | |
98 hDecoder->config.outputFormat = FAAD_FMT_16BIT; | |
99 hDecoder->config.defObjectType = MAIN; | |
100 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */ | |
12527 | 101 hDecoder->config.downMatrix = 0; |
10725 | 102 hDecoder->adts_header_present = 0; |
103 hDecoder->adif_header_present = 0; | |
104 #ifdef ERROR_RESILIENCE | |
105 hDecoder->aacSectionDataResilienceFlag = 0; | |
106 hDecoder->aacScalefactorDataResilienceFlag = 0; | |
107 hDecoder->aacSpectralDataResilienceFlag = 0; | |
108 #endif | |
109 hDecoder->frameLength = 1024; | |
110 | |
111 hDecoder->frame = 0; | |
112 hDecoder->sample_buffer = NULL; | |
113 | |
114 for (i = 0; i < MAX_CHANNELS; i++) | |
115 { | |
116 hDecoder->window_shape_prev[i] = 0; | |
117 hDecoder->time_out[i] = NULL; | |
12527 | 118 hDecoder->fb_intermed[i] = NULL; |
10725 | 119 #ifdef SSR_DEC |
120 hDecoder->ssr_overlap[i] = NULL; | |
121 hDecoder->prev_fmd[i] = NULL; | |
122 #endif | |
123 #ifdef MAIN_DEC | |
124 hDecoder->pred_stat[i] = NULL; | |
125 #endif | |
126 #ifdef LTP_DEC | |
127 hDecoder->ltp_lag[i] = 0; | |
128 hDecoder->lt_pred_stat[i] = NULL; | |
129 #endif | |
130 } | |
131 | |
132 #ifdef SBR_DEC | |
12527 | 133 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) |
10725 | 134 { |
135 hDecoder->sbr[i] = NULL; | |
136 } | |
137 #endif | |
138 | |
139 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0)); | |
140 | |
12527 | 141 #ifdef USE_SSE |
142 if (cpu_has_sse()) | |
10989 | 143 { |
12527 | 144 hDecoder->apply_sf_func = apply_scalefactors_sse; |
145 } else { | |
146 hDecoder->apply_sf_func = apply_scalefactors; | |
10989 | 147 } |
10725 | 148 #endif |
149 | |
150 return hDecoder; | |
151 } | |
152 | |
153 faacDecConfigurationPtr FAADAPI faacDecGetCurrentConfiguration(faacDecHandle hDecoder) | |
154 { | |
10989 | 155 if (hDecoder) |
156 { | |
12527 | 157 faacDecConfigurationPtr config = &(hDecoder->config); |
10725 | 158 |
12527 | 159 return config; |
10989 | 160 } |
161 | |
162 return NULL; | |
10725 | 163 } |
164 | |
165 uint8_t FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder, | |
166 faacDecConfigurationPtr config) | |
167 { | |
10989 | 168 if (hDecoder && config) |
169 { | |
170 /* check if we can decode this object type */ | |
171 if (can_decode_ot(config->defObjectType) < 0) | |
172 return 0; | |
12527 | 173 hDecoder->config.defObjectType = config->defObjectType; |
10989 | 174 |
175 /* samplerate: anything but 0 should be possible */ | |
176 if (config->defSampleRate == 0) | |
177 return 0; | |
12527 | 178 hDecoder->config.defSampleRate = config->defSampleRate; |
10989 | 179 |
180 /* check output format */ | |
181 if ((config->outputFormat < 1) || (config->outputFormat > 9)) | |
182 return 0; | |
12527 | 183 hDecoder->config.outputFormat = config->outputFormat; |
10989 | 184 |
185 if (config->downMatrix > 1) | |
12527 | 186 hDecoder->config.downMatrix = config->downMatrix; |
10725 | 187 |
12527 | 188 /* OK */ |
189 return 1; | |
10989 | 190 } |
191 | |
192 return 0; | |
10725 | 193 } |
194 | |
195 int32_t FAADAPI faacDecInit(faacDecHandle hDecoder, uint8_t *buffer, | |
196 uint32_t buffer_size, | |
197 uint32_t *samplerate, uint8_t *channels) | |
198 { | |
199 uint32_t bits = 0; | |
200 bitfile ld; | |
201 adif_header adif; | |
202 adts_header adts; | |
203 | |
10989 | 204 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) |
205 return -1; | |
206 | |
10725 | 207 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); |
208 hDecoder->object_type = hDecoder->config.defObjectType; | |
10989 | 209 *samplerate = get_sample_rate(hDecoder->sf_index); |
10725 | 210 *channels = 1; |
211 | |
212 if (buffer != NULL) | |
213 { | |
214 faad_initbits(&ld, buffer, buffer_size); | |
215 | |
216 /* Check if an ADIF header is present */ | |
217 if ((buffer[0] == 'A') && (buffer[1] == 'D') && | |
218 (buffer[2] == 'I') && (buffer[3] == 'F')) | |
219 { | |
220 hDecoder->adif_header_present = 1; | |
221 | |
222 get_adif_header(&adif, &ld); | |
223 faad_byte_align(&ld); | |
224 | |
225 hDecoder->sf_index = adif.pce[0].sf_index; | |
10989 | 226 hDecoder->object_type = adif.pce[0].object_type + 1; |
10725 | 227 |
10989 | 228 *samplerate = get_sample_rate(hDecoder->sf_index); |
10725 | 229 *channels = adif.pce[0].channels; |
230 | |
231 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config)); | |
232 hDecoder->pce_set = 1; | |
233 | |
234 bits = bit2byte(faad_get_processed_bits(&ld)); | |
235 | |
236 /* Check if an ADTS header is present */ | |
237 } else if (faad_showbits(&ld, 12) == 0xfff) { | |
238 hDecoder->adts_header_present = 1; | |
239 | |
10989 | 240 adts.old_format = hDecoder->config.useOldADTSFormat; |
10725 | 241 adts_frame(&adts, &ld); |
242 | |
243 hDecoder->sf_index = adts.sf_index; | |
10989 | 244 hDecoder->object_type = adts.profile + 1; |
10725 | 245 |
10989 | 246 *samplerate = get_sample_rate(hDecoder->sf_index); |
10725 | 247 *channels = (adts.channel_configuration > 6) ? |
248 2 : adts.channel_configuration; | |
249 } | |
250 | |
251 if (ld.error) | |
252 { | |
253 faad_endbits(&ld); | |
254 return -1; | |
255 } | |
256 faad_endbits(&ld); | |
257 } | |
258 hDecoder->channelConfiguration = *channels; | |
259 | |
10989 | 260 #ifdef SBR_DEC |
261 /* implicit signalling */ | |
12527 | 262 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) |
10989 | 263 { |
264 *samplerate *= 2; | |
265 hDecoder->forceUpSampling = 1; | |
266 } | |
267 #endif | |
268 | |
10725 | 269 /* must be done before frameLength is divided by 2 for LD */ |
270 #ifdef SSR_DEC | |
271 if (hDecoder->object_type == SSR) | |
272 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS); | |
273 else | |
274 #endif | |
275 hDecoder->fb = filter_bank_init(hDecoder->frameLength); | |
276 | |
277 #ifdef LD_DEC | |
278 if (hDecoder->object_type == LD) | |
279 hDecoder->frameLength >>= 1; | |
280 #endif | |
281 | |
282 if (can_decode_ot(hDecoder->object_type) < 0) | |
283 return -1; | |
284 | |
285 return bits; | |
286 } | |
287 | |
288 /* Init the library using a DecoderSpecificInfo */ | |
289 int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer, | |
290 uint32_t SizeOfDecoderSpecificInfo, | |
291 uint32_t *samplerate, uint8_t *channels) | |
292 { | |
293 int8_t rc; | |
294 mp4AudioSpecificConfig mp4ASC; | |
295 | |
296 if((hDecoder == NULL) | |
297 || (pBuffer == NULL) | |
298 || (SizeOfDecoderSpecificInfo < 2) | |
299 || (samplerate == NULL) | |
300 || (channels == NULL)) | |
301 { | |
302 return -1; | |
303 } | |
304 | |
10989 | 305 hDecoder->adif_header_present = 0; |
306 hDecoder->adts_header_present = 0; | |
307 | |
10725 | 308 /* decode the audio specific config */ |
309 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC, | |
310 &(hDecoder->pce)); | |
311 | |
312 /* copy the relevant info to the decoder handle */ | |
313 *samplerate = mp4ASC.samplingFrequency; | |
314 if (mp4ASC.channelsConfiguration) | |
315 { | |
316 *channels = mp4ASC.channelsConfiguration; | |
317 } else { | |
318 *channels = hDecoder->pce.channels; | |
319 hDecoder->pce_set = 1; | |
320 } | |
321 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex; | |
322 hDecoder->object_type = mp4ASC.objectTypeIndex; | |
10989 | 323 #ifdef ERROR_RESILIENCE |
10725 | 324 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag; |
325 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag; | |
326 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag; | |
10989 | 327 #endif |
10725 | 328 #ifdef SBR_DEC |
329 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag; | |
12527 | 330 if (hDecoder->config.dontUpSampleImplicitSBR == 0) |
331 hDecoder->forceUpSampling = mp4ASC.forceUpSampling; | |
332 else | |
333 hDecoder->forceUpSampling = 0; | |
10725 | 334 |
335 /* AAC core decoder samplerate is 2 times as low */ | |
10989 | 336 if (hDecoder->sbr_present_flag == 1 || hDecoder->forceUpSampling == 1) |
10725 | 337 { |
338 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2); | |
339 } | |
340 #endif | |
341 | |
342 if (rc != 0) | |
343 { | |
344 return rc; | |
345 } | |
346 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration; | |
347 if (mp4ASC.frameLengthFlag) | |
12527 | 348 #ifdef ALLOW_SMALL_FRAMELENGTH |
10725 | 349 hDecoder->frameLength = 960; |
12527 | 350 #else |
351 return -1; | |
352 #endif | |
10725 | 353 |
354 /* must be done before frameLength is divided by 2 for LD */ | |
355 #ifdef SSR_DEC | |
356 if (hDecoder->object_type == SSR) | |
357 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS); | |
358 else | |
359 #endif | |
360 hDecoder->fb = filter_bank_init(hDecoder->frameLength); | |
361 | |
362 #ifdef LD_DEC | |
363 if (hDecoder->object_type == LD) | |
364 hDecoder->frameLength >>= 1; | |
365 #endif | |
366 | |
367 return 0; | |
368 } | |
369 | |
12527 | 370 #ifdef DRM |
10725 | 371 int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, |
372 uint8_t channels) | |
373 { | |
12527 | 374 uint8_t i; |
375 | |
376 if (hDecoder == NULL) | |
377 return 1; /* error */ | |
378 | |
10725 | 379 /* Special object type defined for DRM */ |
380 hDecoder->config.defObjectType = DRM_ER_LC; | |
381 | |
382 hDecoder->config.defSampleRate = samplerate; | |
10989 | 383 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM |
10725 | 384 hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */ |
385 hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */ | |
386 hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */ | |
10989 | 387 #endif |
10725 | 388 hDecoder->frameLength = 960; |
389 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); | |
390 hDecoder->object_type = hDecoder->config.defObjectType; | |
10989 | 391 |
392 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO)) | |
393 hDecoder->channelConfiguration = 2; | |
394 else | |
395 hDecoder->channelConfiguration = 1; | |
396 | |
397 #ifdef SBR_DEC | |
398 if (channels == DRMCH_SBR_LC_STEREO) | |
399 hDecoder->lcstereo_flag = 1; | |
400 else | |
401 hDecoder->lcstereo_flag = 0; | |
402 | |
403 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) | |
404 hDecoder->sbr_present_flag = 0; | |
405 else | |
406 hDecoder->sbr_present_flag = 1; | |
407 | |
408 /* Reset sbr for new initialization */ | |
409 sbrDecodeEnd(hDecoder->sbr[0]); | |
410 hDecoder->sbr[0] = NULL; | |
411 #endif | |
12527 | 412 |
413 if (hDecoder->fb) filter_bank_end(hDecoder->fb); | |
414 hDecoder->fb = NULL; | |
10725 | 415 |
12527 | 416 /* Take care of buffers */ |
417 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer); | |
418 hDecoder->sample_buffer = NULL; | |
419 hDecoder->alloced_channels = 0; | |
420 | |
421 for (i = 0; i < MAX_CHANNELS; i++) | |
422 { | |
423 hDecoder->window_shape_prev[i] = 0; | |
10725 | 424 |
12527 | 425 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]); |
426 hDecoder->time_out[i] = NULL; | |
427 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]); | |
428 hDecoder->fb_intermed[i] = NULL; | |
429 #ifdef SSR_DEC | |
430 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]); | |
431 hDecoder->ssr_overlap[i] = NULL; | |
432 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]); | |
433 hDecoder->prev_fmd[i] = NULL; | |
434 #endif | |
435 #ifdef MAIN_DEC | |
436 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]); | |
437 hDecoder->pred_stat[i] = NULL; | |
10725 | 438 #endif |
12527 | 439 #ifdef LTP_DEC |
440 hDecoder->ltp_lag[i] = 0; | |
441 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]); | |
442 hDecoder->lt_pred_stat[i] = NULL; | |
443 #endif | |
444 } | |
445 | |
446 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) | |
447 { | |
448 #ifdef SBR_DEC | |
449 if (hDecoder->sbr[i]) | |
450 sbrDecodeEnd(hDecoder->sbr[i]); | |
451 | |
452 hDecoder->sbr_alloced[i] = 0; | |
453 #endif | |
454 hDecoder->element_alloced[i] = 0; | |
455 hDecoder->element_output_channels[i] = 0; | |
456 } | |
457 | |
458 hDecoder->fb = filter_bank_init(hDecoder->frameLength); | |
10725 | 459 |
460 return 0; | |
461 } | |
12527 | 462 #endif |
10725 | 463 |
464 void FAADAPI faacDecClose(faacDecHandle hDecoder) | |
465 { | |
466 uint8_t i; | |
467 | |
468 if (hDecoder == NULL) | |
469 return; | |
470 | |
12527 | 471 #ifdef PROFILE |
472 printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles); | |
473 printf("requant: %I64d cycles\n", hDecoder->requant_cycles); | |
474 printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles); | |
475 printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles); | |
476 printf("output: %I64d cycles\n", hDecoder->output_cycles); | |
477 #endif | |
478 | |
10725 | 479 for (i = 0; i < MAX_CHANNELS; i++) |
480 { | |
12527 | 481 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]); |
482 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]); | |
10725 | 483 #ifdef SSR_DEC |
12527 | 484 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]); |
485 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]); | |
10725 | 486 #endif |
487 #ifdef MAIN_DEC | |
12527 | 488 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]); |
10725 | 489 #endif |
490 #ifdef LTP_DEC | |
12527 | 491 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]); |
10725 | 492 #endif |
493 } | |
494 | |
495 #ifdef SSR_DEC | |
496 if (hDecoder->object_type == SSR) | |
497 ssr_filter_bank_end(hDecoder->fb); | |
498 else | |
499 #endif | |
500 filter_bank_end(hDecoder->fb); | |
501 | |
502 drc_end(hDecoder->drc); | |
503 | |
12527 | 504 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer); |
10725 | 505 |
506 #ifdef SBR_DEC | |
12527 | 507 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) |
10725 | 508 { |
509 if (hDecoder->sbr[i]) | |
510 sbrDecodeEnd(hDecoder->sbr[i]); | |
511 } | |
512 #endif | |
513 | |
12527 | 514 if (hDecoder) faad_free(hDecoder); |
10725 | 515 } |
516 | |
517 void FAADAPI faacDecPostSeekReset(faacDecHandle hDecoder, int32_t frame) | |
518 { | |
519 if (hDecoder) | |
520 { | |
521 hDecoder->postSeekResetFlag = 1; | |
522 | |
523 if (frame != -1) | |
524 hDecoder->frame = frame; | |
525 } | |
526 } | |
527 | |
10989 | 528 static void create_channel_config(faacDecHandle hDecoder, faacDecFrameInfo *hInfo) |
10725 | 529 { |
530 hInfo->num_front_channels = 0; | |
531 hInfo->num_side_channels = 0; | |
532 hInfo->num_back_channels = 0; | |
533 hInfo->num_lfe_channels = 0; | |
534 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t)); | |
535 | |
536 if (hDecoder->downMatrix) | |
537 { | |
538 hInfo->num_front_channels = 2; | |
539 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | |
540 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | |
541 return; | |
542 } | |
543 | |
544 /* check if there is a PCE */ | |
545 if (hDecoder->pce_set) | |
546 { | |
547 uint8_t i, chpos = 0; | |
548 uint8_t chdir, back_center = 0; | |
549 | |
550 hInfo->num_front_channels = hDecoder->pce.num_front_channels; | |
551 hInfo->num_side_channels = hDecoder->pce.num_side_channels; | |
552 hInfo->num_back_channels = hDecoder->pce.num_back_channels; | |
553 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; | |
554 | |
555 chdir = hInfo->num_front_channels; | |
556 if (chdir & 1) | |
557 { | |
558 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; | |
559 chdir--; | |
560 } | |
561 for (i = 0; i < chdir; i += 2) | |
562 { | |
563 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; | |
564 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; | |
565 } | |
566 | |
567 for (i = 0; i < hInfo->num_side_channels; i += 2) | |
568 { | |
569 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT; | |
570 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT; | |
571 } | |
572 | |
573 chdir = hInfo->num_back_channels; | |
574 if (chdir & 1) | |
575 { | |
576 back_center = 1; | |
577 chdir--; | |
578 } | |
579 for (i = 0; i < chdir; i += 2) | |
580 { | |
581 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT; | |
582 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT; | |
583 } | |
584 if (back_center) | |
585 { | |
586 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER; | |
587 } | |
588 | |
589 for (i = 0; i < hInfo->num_lfe_channels; i++) | |
590 { | |
591 hInfo->channel_position[chpos++] = LFE_CHANNEL; | |
592 } | |
593 | |
594 } else { | |
595 switch (hDecoder->channelConfiguration) | |
596 { | |
597 case 1: | |
598 hInfo->num_front_channels = 1; | |
599 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
600 break; | |
601 case 2: | |
602 hInfo->num_front_channels = 2; | |
603 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | |
604 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | |
605 break; | |
606 case 3: | |
607 hInfo->num_front_channels = 3; | |
608 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
609 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
610 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
611 break; | |
612 case 4: | |
613 hInfo->num_front_channels = 3; | |
614 hInfo->num_back_channels = 1; | |
615 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
616 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
617 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
618 hInfo->channel_position[3] = BACK_CHANNEL_CENTER; | |
619 break; | |
620 case 5: | |
621 hInfo->num_front_channels = 3; | |
622 hInfo->num_back_channels = 2; | |
623 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
624 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
625 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
626 hInfo->channel_position[3] = BACK_CHANNEL_LEFT; | |
627 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; | |
628 break; | |
629 case 6: | |
630 hInfo->num_front_channels = 3; | |
631 hInfo->num_back_channels = 2; | |
632 hInfo->num_lfe_channels = 1; | |
633 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
634 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
635 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
636 hInfo->channel_position[3] = BACK_CHANNEL_LEFT; | |
637 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; | |
638 hInfo->channel_position[5] = LFE_CHANNEL; | |
639 break; | |
640 case 7: | |
641 hInfo->num_front_channels = 3; | |
642 hInfo->num_side_channels = 2; | |
643 hInfo->num_back_channels = 2; | |
644 hInfo->num_lfe_channels = 1; | |
645 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
646 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
647 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
648 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT; | |
649 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT; | |
650 hInfo->channel_position[5] = BACK_CHANNEL_LEFT; | |
651 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT; | |
652 hInfo->channel_position[7] = LFE_CHANNEL; | |
653 break; | |
654 default: /* channelConfiguration == 0 || channelConfiguration > 7 */ | |
655 { | |
656 uint8_t i; | |
657 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe; | |
658 if (ch & 1) /* there's either a center front or a center back channel */ | |
659 { | |
660 uint8_t ch1 = (ch-1)/2; | |
661 if (hDecoder->first_syn_ele == ID_SCE) | |
662 { | |
663 hInfo->num_front_channels = ch1 + 1; | |
664 hInfo->num_back_channels = ch1; | |
665 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
666 for (i = 1; i <= ch1; i+=2) | |
667 { | |
668 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
669 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
670 } | |
671 for (i = ch1+1; i < ch; i+=2) | |
672 { | |
673 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
674 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
675 } | |
676 } else { | |
677 hInfo->num_front_channels = ch1; | |
678 hInfo->num_back_channels = ch1 + 1; | |
679 for (i = 0; i < ch1; i+=2) | |
680 { | |
681 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
682 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
683 } | |
684 for (i = ch1; i < ch-1; i+=2) | |
685 { | |
686 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
687 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
688 } | |
689 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; | |
690 } | |
691 } else { | |
692 uint8_t ch1 = (ch)/2; | |
693 hInfo->num_front_channels = ch1; | |
694 hInfo->num_back_channels = ch1; | |
695 if (ch1 & 1) | |
696 { | |
697 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
698 for (i = 1; i <= ch1; i+=2) | |
699 { | |
700 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
701 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
702 } | |
703 for (i = ch1+1; i < ch-1; i+=2) | |
704 { | |
705 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
706 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
707 } | |
708 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; | |
709 } else { | |
710 for (i = 0; i < ch1; i+=2) | |
711 { | |
712 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
713 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
714 } | |
715 for (i = ch1; i < ch; i+=2) | |
716 { | |
717 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
718 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
719 } | |
720 } | |
721 } | |
722 hInfo->num_lfe_channels = hDecoder->has_lfe; | |
723 for (i = ch; i < hDecoder->fr_channels; i++) | |
724 { | |
725 hInfo->channel_position[i] = LFE_CHANNEL; | |
726 } | |
727 } | |
728 break; | |
729 } | |
730 } | |
731 } | |
732 | |
733 void* FAADAPI faacDecDecode(faacDecHandle hDecoder, | |
734 faacDecFrameInfo *hInfo, | |
735 uint8_t *buffer, uint32_t buffer_size) | |
736 { | |
12527 | 737 uint8_t channels = 0; |
10725 | 738 uint8_t output_channels = 0; |
12527 | 739 bitfile ld; |
10989 | 740 uint32_t bitsconsumed; |
12527 | 741 uint16_t frame_len; |
742 void *sample_buffer; | |
743 | |
744 #ifdef PROFILE | |
745 int64_t count = faad_get_ts(); | |
10989 | 746 #endif |
10725 | 747 |
10989 | 748 /* safety checks */ |
12527 | 749 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL)) |
10989 | 750 { |
751 return NULL; | |
752 } | |
10725 | 753 |
10989 | 754 frame_len = hDecoder->frameLength; |
10725 | 755 |
756 | |
757 memset(hInfo, 0, sizeof(faacDecFrameInfo)); | |
10989 | 758 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); |
10725 | 759 |
760 /* initialize the bitstream */ | |
12527 | 761 faad_initbits(&ld, buffer, buffer_size); |
10725 | 762 |
763 #ifdef DRM | |
12527 | 764 if (hDecoder->object_type == DRM_ER_LC) |
10725 | 765 { |
12527 | 766 /* We do not support stereo right now */ |
767 if (hDecoder->channelConfiguration == 2) | |
768 { | |
769 hInfo->error = 8; // Throw CRC error | |
770 goto error; | |
771 } | |
772 | |
773 faad_getbits(&ld, 8 | |
10725 | 774 DEBUGVAR(1,1,"faacDecDecode(): skip CRC")); |
775 } | |
776 #endif | |
777 | |
778 if (hDecoder->adts_header_present) | |
779 { | |
12527 | 780 adts_header adts; |
781 | |
10989 | 782 adts.old_format = hDecoder->config.useOldADTSFormat; |
12527 | 783 if ((hInfo->error = adts_frame(&adts, &ld)) > 0) |
10725 | 784 goto error; |
785 | |
786 /* MPEG2 does byte_alignment() here, | |
787 * but ADTS header is always multiple of 8 bits in MPEG2 | |
788 * so not needed to actually do it. | |
789 */ | |
790 } | |
791 | |
792 #ifdef ANALYSIS | |
793 dbg_count = 0; | |
794 #endif | |
795 | |
12527 | 796 /* decode the complete bitstream */ |
797 #ifdef SCALABLE_DEC | |
798 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC)) | |
799 { | |
800 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); | |
801 } else { | |
802 #endif | |
803 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); | |
804 #ifdef SCALABLE_DEC | |
805 } | |
806 #endif | |
10725 | 807 |
808 channels = hDecoder->fr_channels; | |
809 | |
810 if (hInfo->error > 0) | |
811 goto error; | |
812 | |
12527 | 813 /* safety check */ |
814 if (channels == 0 || channels > MAX_CHANNELS) | |
815 { | |
816 /* invalid number of channels */ | |
817 hInfo->error = 12; | |
818 goto error; | |
819 } | |
10725 | 820 |
821 /* no more bit reading after this */ | |
12527 | 822 bitsconsumed = faad_get_processed_bits(&ld); |
10989 | 823 hInfo->bytesconsumed = bit2byte(bitsconsumed); |
12527 | 824 if (ld.error) |
10725 | 825 { |
826 hInfo->error = 14; | |
827 goto error; | |
828 } | |
12527 | 829 faad_endbits(&ld); |
10989 | 830 |
831 | |
10725 | 832 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present) |
833 { | |
834 if (channels != hDecoder->channelConfiguration) | |
835 hDecoder->channelConfiguration = channels; | |
836 | |
837 if (channels == 8) /* 7.1 */ | |
838 hDecoder->channelConfiguration = 7; | |
839 if (channels == 7) /* not a standard channelConfiguration */ | |
840 hDecoder->channelConfiguration = 0; | |
841 } | |
842 | |
843 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix) | |
844 { | |
845 hDecoder->downMatrix = 1; | |
846 output_channels = 2; | |
847 } else { | |
848 output_channels = channels; | |
849 } | |
850 | |
851 /* Make a channel configuration based on either a PCE or a channelConfiguration */ | |
852 create_channel_config(hDecoder, hInfo); | |
853 | |
854 /* number of samples in this frame */ | |
855 hInfo->samples = frame_len*output_channels; | |
856 /* number of channels in this frame */ | |
857 hInfo->channels = output_channels; | |
858 /* samplerate */ | |
10989 | 859 hInfo->samplerate = get_sample_rate(hDecoder->sf_index); |
860 /* object type */ | |
861 hInfo->object_type = hDecoder->object_type; | |
862 /* sbr */ | |
863 hInfo->sbr = NO_SBR; | |
864 /* header type */ | |
865 hInfo->header_type = RAW; | |
866 if (hDecoder->adif_header_present) | |
867 hInfo->header_type = ADIF; | |
868 if (hDecoder->adts_header_present) | |
869 hInfo->header_type = ADTS; | |
10725 | 870 |
871 /* check if frame has channel elements */ | |
872 if (channels == 0) | |
873 { | |
874 hDecoder->frame++; | |
875 return NULL; | |
876 } | |
877 | |
12527 | 878 /* allocate the buffer for the final samples */ |
879 if ((hDecoder->sample_buffer == NULL) || | |
880 (hDecoder->alloced_channels != output_channels)) | |
10725 | 881 { |
12527 | 882 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t), |
883 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t), | |
884 sizeof(int16_t), sizeof(int16_t), 0, 0, 0 | |
885 }; | |
886 uint8_t stride = str[hDecoder->config.outputFormat-1]; | |
10725 | 887 #ifdef SBR_DEC |
10989 | 888 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
12527 | 889 stride = 2 * stride; |
10725 | 890 #endif |
12527 | 891 if (hDecoder->sample_buffer) |
892 faad_free(hDecoder->sample_buffer); | |
893 hDecoder->sample_buffer = NULL; | |
894 hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride); | |
895 hDecoder->alloced_channels = output_channels; | |
10725 | 896 } |
897 | |
898 sample_buffer = hDecoder->sample_buffer; | |
899 | |
900 #ifdef SBR_DEC | |
10989 | 901 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
10725 | 902 { |
12527 | 903 uint8_t ele; |
10989 | 904 |
12527 | 905 /* this data is different when SBR is used or when the data is upsampled */ |
10725 | 906 frame_len *= 2; |
907 hInfo->samples *= 2; | |
908 hInfo->samplerate *= 2; | |
12527 | 909 |
910 /* check if every element was provided with SBR data */ | |
911 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++) | |
912 { | |
913 if (hDecoder->sbr[ele] == NULL) | |
914 { | |
915 hInfo->error = 25; | |
916 goto error; | |
917 } | |
918 } | |
919 | |
10989 | 920 /* sbr */ |
921 if (hDecoder->sbr_present_flag == 1) | |
922 { | |
923 hInfo->object_type = HE_AAC; | |
924 hInfo->sbr = SBR_UPSAMPLED; | |
925 } else { | |
926 hInfo->sbr = NO_SBR_UPSAMPLED; | |
927 } | |
10725 | 928 } |
929 #endif | |
930 | |
12527 | 931 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, |
932 output_channels, frame_len, hDecoder->config.outputFormat); | |
933 | |
934 | |
10725 | 935 hDecoder->postSeekResetFlag = 0; |
936 | |
937 hDecoder->frame++; | |
938 #ifdef LD_DEC | |
12527 | 939 if (hDecoder->object_type != LD) |
10725 | 940 { |
941 #endif | |
942 if (hDecoder->frame <= 1) | |
943 hInfo->samples = 0; | |
944 #ifdef LD_DEC | |
945 } else { | |
946 /* LD encoders will give lower delay */ | |
947 if (hDecoder->frame <= 0) | |
948 hInfo->samples = 0; | |
949 } | |
950 #endif | |
951 | |
952 /* cleanup */ | |
953 #ifdef ANALYSIS | |
954 fflush(stdout); | |
955 #endif | |
956 | |
12527 | 957 #ifdef PROFILE |
958 count = faad_get_ts() - count; | |
959 hDecoder->cycles += count; | |
960 #endif | |
961 | |
10725 | 962 return sample_buffer; |
963 | |
964 error: | |
12527 | 965 |
966 faad_endbits(&ld); | |
10725 | 967 |
968 /* cleanup */ | |
969 #ifdef ANALYSIS | |
970 fflush(stdout); | |
971 #endif | |
972 | |
973 return NULL; | |
974 } |