Mercurial > mplayer.hg
comparison libfaad2/decoder.c @ 10725:e989150f8216
libfaad2 v2.0rc1 imported
author | arpi |
---|---|
date | Sat, 30 Aug 2003 22:30:28 +0000 |
parents | |
children | 3185f64f6350 |
comparison
equal
deleted
inserted
replaced
10724:adf5697b9d83 | 10725:e989150f8216 |
---|---|
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 ** | |
25 ** $Id: decoder.c,v 1.62 2003/07/29 08:20:12 menno Exp $ | |
26 **/ | |
27 | |
28 #include "common.h" | |
29 #include "structs.h" | |
30 | |
31 #include <stdlib.h> | |
32 #include <string.h> | |
33 | |
34 #include "decoder.h" | |
35 #include "mp4.h" | |
36 #include "syntax.h" | |
37 #include "specrec.h" | |
38 #include "tns.h" | |
39 #include "pns.h" | |
40 #include "is.h" | |
41 #include "ms.h" | |
42 #include "ic_predict.h" | |
43 #include "lt_predict.h" | |
44 #include "drc.h" | |
45 #include "error.h" | |
46 #include "output.h" | |
47 #include "dither.h" | |
48 #ifdef SSR_DEC | |
49 #include "ssr.h" | |
50 #include "ssr_fb.h" | |
51 #endif | |
52 #ifdef SBR_DEC | |
53 #include "sbr_dec.h" | |
54 #endif | |
55 | |
56 #ifdef ANALYSIS | |
57 uint16_t dbg_count; | |
58 #endif | |
59 | |
60 int8_t* FAADAPI faacDecGetErrorMessage(uint8_t errcode) | |
61 { | |
62 if (errcode >= NUM_ERROR_MESSAGES) | |
63 return NULL; | |
64 return err_msg[errcode]; | |
65 } | |
66 | |
67 uint32_t FAADAPI faacDecGetCapabilities() | |
68 { | |
69 uint32_t cap = 0; | |
70 | |
71 /* can't do without it */ | |
72 cap += LC_DEC_CAP; | |
73 | |
74 #ifdef MAIN_DEC | |
75 cap += MAIN_DEC_CAP; | |
76 #endif | |
77 #ifdef LTP_DEC | |
78 cap += LTP_DEC_CAP; | |
79 #endif | |
80 #ifdef LD_DEC | |
81 cap += LD_DEC_CAP; | |
82 #endif | |
83 #ifdef ERROR_RESILIENCE | |
84 cap += ERROR_RESILIENCE_CAP; | |
85 #endif | |
86 #ifdef FIXED_POINT | |
87 cap += FIXED_POINT_CAP; | |
88 #endif | |
89 | |
90 return cap; | |
91 } | |
92 | |
93 faacDecHandle FAADAPI faacDecOpen() | |
94 { | |
95 uint8_t i; | |
96 faacDecHandle hDecoder = NULL; | |
97 | |
98 if ((hDecoder = (faacDecHandle)malloc(sizeof(faacDecStruct))) == NULL) | |
99 return NULL; | |
100 | |
101 memset(hDecoder, 0, sizeof(faacDecStruct)); | |
102 | |
103 hDecoder->config.outputFormat = FAAD_FMT_16BIT; | |
104 hDecoder->config.defObjectType = MAIN; | |
105 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */ | |
106 hDecoder->adts_header_present = 0; | |
107 hDecoder->adif_header_present = 0; | |
108 #ifdef ERROR_RESILIENCE | |
109 hDecoder->aacSectionDataResilienceFlag = 0; | |
110 hDecoder->aacScalefactorDataResilienceFlag = 0; | |
111 hDecoder->aacSpectralDataResilienceFlag = 0; | |
112 #endif | |
113 hDecoder->frameLength = 1024; | |
114 | |
115 hDecoder->frame = 0; | |
116 hDecoder->sample_buffer = NULL; | |
117 | |
118 for (i = 0; i < MAX_CHANNELS; i++) | |
119 { | |
120 hDecoder->window_shape_prev[i] = 0; | |
121 hDecoder->time_out[i] = NULL; | |
122 #ifdef SBR_DEC | |
123 hDecoder->time_out2[i] = NULL; | |
124 #endif | |
125 #ifdef SSR_DEC | |
126 hDecoder->ssr_overlap[i] = NULL; | |
127 hDecoder->prev_fmd[i] = NULL; | |
128 #endif | |
129 #ifdef MAIN_DEC | |
130 hDecoder->pred_stat[i] = NULL; | |
131 #endif | |
132 #ifdef LTP_DEC | |
133 hDecoder->ltp_lag[i] = 0; | |
134 hDecoder->lt_pred_stat[i] = NULL; | |
135 #endif | |
136 } | |
137 | |
138 #ifdef SBR_DEC | |
139 for (i = 0; i < 32; i++) | |
140 { | |
141 hDecoder->sbr[i] = NULL; | |
142 } | |
143 #endif | |
144 | |
145 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0)); | |
146 | |
147 #if POW_TABLE_SIZE | |
148 hDecoder->pow2_table = (real_t*)malloc(POW_TABLE_SIZE*sizeof(real_t)); | |
149 build_tables(hDecoder->pow2_table); | |
150 #endif | |
151 | |
152 return hDecoder; | |
153 } | |
154 | |
155 faacDecConfigurationPtr FAADAPI faacDecGetCurrentConfiguration(faacDecHandle hDecoder) | |
156 { | |
157 faacDecConfigurationPtr config = &(hDecoder->config); | |
158 | |
159 return config; | |
160 } | |
161 | |
162 uint8_t FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder, | |
163 faacDecConfigurationPtr config) | |
164 { | |
165 hDecoder->config.defObjectType = config->defObjectType; | |
166 hDecoder->config.defSampleRate = config->defSampleRate; | |
167 hDecoder->config.outputFormat = config->outputFormat; | |
168 hDecoder->config.downMatrix = config->downMatrix; | |
169 | |
170 /* OK */ | |
171 return 1; | |
172 } | |
173 | |
174 int32_t FAADAPI faacDecInit(faacDecHandle hDecoder, uint8_t *buffer, | |
175 uint32_t buffer_size, | |
176 uint32_t *samplerate, uint8_t *channels) | |
177 { | |
178 uint32_t bits = 0; | |
179 bitfile ld; | |
180 adif_header adif; | |
181 adts_header adts; | |
182 | |
183 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); | |
184 hDecoder->object_type = hDecoder->config.defObjectType; | |
185 *samplerate = sample_rates[hDecoder->sf_index]; | |
186 *channels = 1; | |
187 | |
188 if (buffer != NULL) | |
189 { | |
190 faad_initbits(&ld, buffer, buffer_size); | |
191 | |
192 /* Check if an ADIF header is present */ | |
193 if ((buffer[0] == 'A') && (buffer[1] == 'D') && | |
194 (buffer[2] == 'I') && (buffer[3] == 'F')) | |
195 { | |
196 hDecoder->adif_header_present = 1; | |
197 | |
198 get_adif_header(&adif, &ld); | |
199 faad_byte_align(&ld); | |
200 | |
201 hDecoder->sf_index = adif.pce[0].sf_index; | |
202 hDecoder->object_type = adif.pce[0].object_type; | |
203 | |
204 *samplerate = sample_rates[hDecoder->sf_index]; | |
205 *channels = adif.pce[0].channels; | |
206 | |
207 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config)); | |
208 hDecoder->pce_set = 1; | |
209 | |
210 bits = bit2byte(faad_get_processed_bits(&ld)); | |
211 | |
212 /* Check if an ADTS header is present */ | |
213 } else if (faad_showbits(&ld, 12) == 0xfff) { | |
214 hDecoder->adts_header_present = 1; | |
215 | |
216 adts_frame(&adts, &ld); | |
217 | |
218 hDecoder->sf_index = adts.sf_index; | |
219 hDecoder->object_type = adts.profile; | |
220 | |
221 *samplerate = sample_rates[hDecoder->sf_index]; | |
222 *channels = (adts.channel_configuration > 6) ? | |
223 2 : adts.channel_configuration; | |
224 } | |
225 | |
226 if (ld.error) | |
227 { | |
228 faad_endbits(&ld); | |
229 return -1; | |
230 } | |
231 faad_endbits(&ld); | |
232 } | |
233 hDecoder->channelConfiguration = *channels; | |
234 | |
235 /* must be done before frameLength is divided by 2 for LD */ | |
236 #ifdef SSR_DEC | |
237 if (hDecoder->object_type == SSR) | |
238 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS); | |
239 else | |
240 #endif | |
241 hDecoder->fb = filter_bank_init(hDecoder->frameLength); | |
242 | |
243 #ifdef LD_DEC | |
244 if (hDecoder->object_type == LD) | |
245 hDecoder->frameLength >>= 1; | |
246 #endif | |
247 | |
248 if (can_decode_ot(hDecoder->object_type) < 0) | |
249 return -1; | |
250 | |
251 #ifndef FIXED_POINT | |
252 if (hDecoder->config.outputFormat >= FAAD_FMT_DITHER_LOWEST) | |
253 Init_Dither(16, hDecoder->config.outputFormat - FAAD_FMT_DITHER_LOWEST); | |
254 #endif | |
255 | |
256 return bits; | |
257 } | |
258 | |
259 /* Init the library using a DecoderSpecificInfo */ | |
260 int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer, | |
261 uint32_t SizeOfDecoderSpecificInfo, | |
262 uint32_t *samplerate, uint8_t *channels) | |
263 { | |
264 int8_t rc; | |
265 mp4AudioSpecificConfig mp4ASC; | |
266 | |
267 hDecoder->adif_header_present = 0; | |
268 hDecoder->adts_header_present = 0; | |
269 | |
270 if((hDecoder == NULL) | |
271 || (pBuffer == NULL) | |
272 || (SizeOfDecoderSpecificInfo < 2) | |
273 || (samplerate == NULL) | |
274 || (channels == NULL)) | |
275 { | |
276 return -1; | |
277 } | |
278 | |
279 /* decode the audio specific config */ | |
280 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC, | |
281 &(hDecoder->pce)); | |
282 | |
283 /* copy the relevant info to the decoder handle */ | |
284 *samplerate = mp4ASC.samplingFrequency; | |
285 if (mp4ASC.channelsConfiguration) | |
286 { | |
287 *channels = mp4ASC.channelsConfiguration; | |
288 } else { | |
289 *channels = hDecoder->pce.channels; | |
290 hDecoder->pce_set = 1; | |
291 } | |
292 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex; | |
293 hDecoder->object_type = mp4ASC.objectTypeIndex; | |
294 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag; | |
295 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag; | |
296 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag; | |
297 #ifdef SBR_DEC | |
298 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag; | |
299 | |
300 /* AAC core decoder samplerate is 2 times as low */ | |
301 if (hDecoder->sbr_present_flag == 1) | |
302 { | |
303 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2); | |
304 } | |
305 #endif | |
306 | |
307 if (hDecoder->object_type < 5) | |
308 hDecoder->object_type--; /* For AAC differs from MPEG-4 */ | |
309 if (rc != 0) | |
310 { | |
311 return rc; | |
312 } | |
313 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration; | |
314 if (mp4ASC.frameLengthFlag) | |
315 hDecoder->frameLength = 960; | |
316 | |
317 /* must be done before frameLength is divided by 2 for LD */ | |
318 #ifdef SSR_DEC | |
319 if (hDecoder->object_type == SSR) | |
320 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS); | |
321 else | |
322 #endif | |
323 hDecoder->fb = filter_bank_init(hDecoder->frameLength); | |
324 | |
325 #ifdef LD_DEC | |
326 if (hDecoder->object_type == LD) | |
327 hDecoder->frameLength >>= 1; | |
328 #endif | |
329 | |
330 #ifndef FIXED_POINT | |
331 if (hDecoder->config.outputFormat >= FAAD_FMT_DITHER_LOWEST) | |
332 Init_Dither(16, hDecoder->config.outputFormat - FAAD_FMT_DITHER_LOWEST); | |
333 #endif | |
334 | |
335 return 0; | |
336 } | |
337 | |
338 int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, | |
339 uint8_t channels) | |
340 { | |
341 /* Special object type defined for DRM */ | |
342 hDecoder->config.defObjectType = DRM_ER_LC; | |
343 | |
344 hDecoder->config.defSampleRate = samplerate; | |
345 hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */ | |
346 hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */ | |
347 hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */ | |
348 hDecoder->frameLength = 960; | |
349 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); | |
350 hDecoder->object_type = hDecoder->config.defObjectType; | |
351 hDecoder->channelConfiguration = channels; | |
352 | |
353 /* must be done before frameLength is divided by 2 for LD */ | |
354 hDecoder->fb = filter_bank_init(hDecoder->frameLength); | |
355 | |
356 #ifndef FIXED_POINT | |
357 if (hDecoder->config.outputFormat >= FAAD_FMT_DITHER_LOWEST) | |
358 Init_Dither(16, hDecoder->config.outputFormat - FAAD_FMT_DITHER_LOWEST); | |
359 #endif | |
360 | |
361 return 0; | |
362 } | |
363 | |
364 void FAADAPI faacDecClose(faacDecHandle hDecoder) | |
365 { | |
366 uint8_t i; | |
367 | |
368 if (hDecoder == NULL) | |
369 return; | |
370 | |
371 for (i = 0; i < MAX_CHANNELS; i++) | |
372 { | |
373 if (hDecoder->time_out[i]) free(hDecoder->time_out[i]); | |
374 #ifdef SBR_DEC | |
375 if (hDecoder->time_out2[i]) free(hDecoder->time_out2[i]); | |
376 #endif | |
377 #ifdef SSR_DEC | |
378 if (hDecoder->ssr_overlap[i]) free(hDecoder->ssr_overlap[i]); | |
379 if (hDecoder->prev_fmd[i]) free(hDecoder->prev_fmd[i]); | |
380 #endif | |
381 #ifdef MAIN_DEC | |
382 if (hDecoder->pred_stat[i]) free(hDecoder->pred_stat[i]); | |
383 #endif | |
384 #ifdef LTP_DEC | |
385 if (hDecoder->lt_pred_stat[i]) free(hDecoder->lt_pred_stat[i]); | |
386 #endif | |
387 } | |
388 | |
389 #ifdef SSR_DEC | |
390 if (hDecoder->object_type == SSR) | |
391 ssr_filter_bank_end(hDecoder->fb); | |
392 else | |
393 #endif | |
394 filter_bank_end(hDecoder->fb); | |
395 | |
396 drc_end(hDecoder->drc); | |
397 | |
398 #ifndef FIXED_POINT | |
399 #if POW_TABLE_SIZE | |
400 if (hDecoder->pow2_table) free(hDecoder->pow2_table); | |
401 #endif | |
402 #endif | |
403 | |
404 if (hDecoder->sample_buffer) free(hDecoder->sample_buffer); | |
405 | |
406 #ifdef SBR_DEC | |
407 for (i = 0; i < 32; i++) | |
408 { | |
409 if (hDecoder->sbr[i]) | |
410 sbrDecodeEnd(hDecoder->sbr[i]); | |
411 } | |
412 #endif | |
413 | |
414 if (hDecoder) free(hDecoder); | |
415 } | |
416 | |
417 void FAADAPI faacDecPostSeekReset(faacDecHandle hDecoder, int32_t frame) | |
418 { | |
419 if (hDecoder) | |
420 { | |
421 hDecoder->postSeekResetFlag = 1; | |
422 | |
423 if (frame != -1) | |
424 hDecoder->frame = frame; | |
425 } | |
426 } | |
427 | |
428 void create_channel_config(faacDecHandle hDecoder, faacDecFrameInfo *hInfo) | |
429 { | |
430 hInfo->num_front_channels = 0; | |
431 hInfo->num_side_channels = 0; | |
432 hInfo->num_back_channels = 0; | |
433 hInfo->num_lfe_channels = 0; | |
434 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t)); | |
435 | |
436 if (hDecoder->downMatrix) | |
437 { | |
438 hInfo->num_front_channels = 2; | |
439 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | |
440 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | |
441 return; | |
442 } | |
443 | |
444 /* check if there is a PCE */ | |
445 if (hDecoder->pce_set) | |
446 { | |
447 uint8_t i, chpos = 0; | |
448 uint8_t chdir, back_center = 0; | |
449 | |
450 hInfo->num_front_channels = hDecoder->pce.num_front_channels; | |
451 hInfo->num_side_channels = hDecoder->pce.num_side_channels; | |
452 hInfo->num_back_channels = hDecoder->pce.num_back_channels; | |
453 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; | |
454 | |
455 chdir = hInfo->num_front_channels; | |
456 if (chdir & 1) | |
457 { | |
458 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; | |
459 chdir--; | |
460 } | |
461 for (i = 0; i < chdir; i += 2) | |
462 { | |
463 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; | |
464 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; | |
465 } | |
466 | |
467 for (i = 0; i < hInfo->num_side_channels; i += 2) | |
468 { | |
469 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT; | |
470 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT; | |
471 } | |
472 | |
473 chdir = hInfo->num_back_channels; | |
474 if (chdir & 1) | |
475 { | |
476 back_center = 1; | |
477 chdir--; | |
478 } | |
479 for (i = 0; i < chdir; i += 2) | |
480 { | |
481 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT; | |
482 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT; | |
483 } | |
484 if (back_center) | |
485 { | |
486 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER; | |
487 } | |
488 | |
489 for (i = 0; i < hInfo->num_lfe_channels; i++) | |
490 { | |
491 hInfo->channel_position[chpos++] = LFE_CHANNEL; | |
492 } | |
493 | |
494 } else { | |
495 switch (hDecoder->channelConfiguration) | |
496 { | |
497 case 1: | |
498 hInfo->num_front_channels = 1; | |
499 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
500 break; | |
501 case 2: | |
502 hInfo->num_front_channels = 2; | |
503 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | |
504 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | |
505 break; | |
506 case 3: | |
507 hInfo->num_front_channels = 3; | |
508 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
509 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
510 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
511 break; | |
512 case 4: | |
513 hInfo->num_front_channels = 3; | |
514 hInfo->num_back_channels = 1; | |
515 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
516 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
517 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
518 hInfo->channel_position[3] = BACK_CHANNEL_CENTER; | |
519 break; | |
520 case 5: | |
521 hInfo->num_front_channels = 3; | |
522 hInfo->num_back_channels = 2; | |
523 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
524 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
525 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
526 hInfo->channel_position[3] = BACK_CHANNEL_LEFT; | |
527 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; | |
528 break; | |
529 case 6: | |
530 hInfo->num_front_channels = 3; | |
531 hInfo->num_back_channels = 2; | |
532 hInfo->num_lfe_channels = 1; | |
533 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
534 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
535 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
536 hInfo->channel_position[3] = BACK_CHANNEL_LEFT; | |
537 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; | |
538 hInfo->channel_position[5] = LFE_CHANNEL; | |
539 break; | |
540 case 7: | |
541 hInfo->num_front_channels = 3; | |
542 hInfo->num_side_channels = 2; | |
543 hInfo->num_back_channels = 2; | |
544 hInfo->num_lfe_channels = 1; | |
545 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
546 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; | |
547 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; | |
548 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT; | |
549 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT; | |
550 hInfo->channel_position[5] = BACK_CHANNEL_LEFT; | |
551 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT; | |
552 hInfo->channel_position[7] = LFE_CHANNEL; | |
553 break; | |
554 default: /* channelConfiguration == 0 || channelConfiguration > 7 */ | |
555 { | |
556 uint8_t i; | |
557 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe; | |
558 if (ch & 1) /* there's either a center front or a center back channel */ | |
559 { | |
560 uint8_t ch1 = (ch-1)/2; | |
561 if (hDecoder->first_syn_ele == ID_SCE) | |
562 { | |
563 hInfo->num_front_channels = ch1 + 1; | |
564 hInfo->num_back_channels = ch1; | |
565 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
566 for (i = 1; i <= ch1; i+=2) | |
567 { | |
568 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
569 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
570 } | |
571 for (i = ch1+1; i < ch; i+=2) | |
572 { | |
573 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
574 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
575 } | |
576 } else { | |
577 hInfo->num_front_channels = ch1; | |
578 hInfo->num_back_channels = ch1 + 1; | |
579 for (i = 0; i < ch1; i+=2) | |
580 { | |
581 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
582 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
583 } | |
584 for (i = ch1; i < ch-1; i+=2) | |
585 { | |
586 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
587 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
588 } | |
589 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; | |
590 } | |
591 } else { | |
592 uint8_t ch1 = (ch)/2; | |
593 hInfo->num_front_channels = ch1; | |
594 hInfo->num_back_channels = ch1; | |
595 if (ch1 & 1) | |
596 { | |
597 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | |
598 for (i = 1; i <= ch1; i+=2) | |
599 { | |
600 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
601 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
602 } | |
603 for (i = ch1+1; i < ch-1; i+=2) | |
604 { | |
605 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
606 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
607 } | |
608 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; | |
609 } else { | |
610 for (i = 0; i < ch1; i+=2) | |
611 { | |
612 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; | |
613 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; | |
614 } | |
615 for (i = ch1; i < ch; i+=2) | |
616 { | |
617 hInfo->channel_position[i] = BACK_CHANNEL_LEFT; | |
618 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; | |
619 } | |
620 } | |
621 } | |
622 hInfo->num_lfe_channels = hDecoder->has_lfe; | |
623 for (i = ch; i < hDecoder->fr_channels; i++) | |
624 { | |
625 hInfo->channel_position[i] = LFE_CHANNEL; | |
626 } | |
627 } | |
628 break; | |
629 } | |
630 } | |
631 } | |
632 | |
633 void* FAADAPI faacDecDecode(faacDecHandle hDecoder, | |
634 faacDecFrameInfo *hInfo, | |
635 uint8_t *buffer, uint32_t buffer_size) | |
636 { | |
637 int32_t i; | |
638 uint8_t ch; | |
639 adts_header adts; | |
640 uint8_t channels = 0, ch_ele = 0; | |
641 uint8_t output_channels = 0; | |
642 bitfile *ld = (bitfile*)malloc(sizeof(bitfile)); | |
643 | |
644 /* local copys of globals */ | |
645 uint8_t sf_index = hDecoder->sf_index; | |
646 uint8_t object_type = hDecoder->object_type; | |
647 uint8_t channelConfiguration = hDecoder->channelConfiguration; | |
648 #ifdef MAIN_DEC | |
649 pred_state **pred_stat = hDecoder->pred_stat; | |
650 #endif | |
651 #ifdef LTP_DEC | |
652 real_t **lt_pred_stat = hDecoder->lt_pred_stat; | |
653 #endif | |
654 #ifndef FIXED_POINT | |
655 #if POW_TABLE_SIZE | |
656 real_t *pow2_table = hDecoder->pow2_table; | |
657 #else | |
658 real_t *pow2_table = NULL; | |
659 #endif | |
660 #endif | |
661 uint8_t *window_shape_prev = hDecoder->window_shape_prev; | |
662 real_t **time_out = hDecoder->time_out; | |
663 #ifdef SBR_DEC | |
664 real_t **time_out2 = hDecoder->time_out2; | |
665 #endif | |
666 #ifdef SSR_DEC | |
667 real_t **ssr_overlap = hDecoder->ssr_overlap; | |
668 real_t **prev_fmd = hDecoder->prev_fmd; | |
669 #endif | |
670 fb_info *fb = hDecoder->fb; | |
671 drc_info *drc = hDecoder->drc; | |
672 uint8_t outputFormat = hDecoder->config.outputFormat; | |
673 #ifdef LTP_DEC | |
674 uint16_t *ltp_lag = hDecoder->ltp_lag; | |
675 #endif | |
676 program_config *pce = &hDecoder->pce; | |
677 | |
678 element *syntax_elements[MAX_SYNTAX_ELEMENTS]; | |
679 element **elements; | |
680 int16_t *spec_data[MAX_CHANNELS]; | |
681 real_t *spec_coef[MAX_CHANNELS]; | |
682 | |
683 uint16_t frame_len = hDecoder->frameLength; | |
684 | |
685 void *sample_buffer; | |
686 | |
687 | |
688 memset(hInfo, 0, sizeof(faacDecFrameInfo)); | |
689 | |
690 /* initialize the bitstream */ | |
691 faad_initbits(ld, buffer, buffer_size); | |
692 | |
693 #ifdef DRM | |
694 if (object_type == DRM_ER_LC) | |
695 { | |
696 faad_getbits(ld, 8 | |
697 DEBUGVAR(1,1,"faacDecDecode(): skip CRC")); | |
698 } | |
699 #endif | |
700 | |
701 if (hDecoder->adts_header_present) | |
702 { | |
703 if ((hInfo->error = adts_frame(&adts, ld)) > 0) | |
704 goto error; | |
705 | |
706 /* MPEG2 does byte_alignment() here, | |
707 * but ADTS header is always multiple of 8 bits in MPEG2 | |
708 * so not needed to actually do it. | |
709 */ | |
710 } | |
711 | |
712 #ifdef ANALYSIS | |
713 dbg_count = 0; | |
714 #endif | |
715 | |
716 elements = syntax_elements; | |
717 | |
718 /* decode the complete bitstream */ | |
719 elements = raw_data_block(hDecoder, hInfo, ld, syntax_elements, | |
720 spec_data, spec_coef, pce, drc); | |
721 | |
722 ch_ele = hDecoder->fr_ch_ele; | |
723 channels = hDecoder->fr_channels; | |
724 | |
725 if (hInfo->error > 0) | |
726 goto error; | |
727 | |
728 | |
729 /* no more bit reading after this */ | |
730 hInfo->bytesconsumed = bit2byte(faad_get_processed_bits(ld)); | |
731 if (ld->error) | |
732 { | |
733 hInfo->error = 14; | |
734 goto error; | |
735 } | |
736 faad_endbits(ld); | |
737 if (ld) free(ld); | |
738 ld = NULL; | |
739 | |
740 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present) | |
741 { | |
742 if (channels != hDecoder->channelConfiguration) | |
743 hDecoder->channelConfiguration = channels; | |
744 | |
745 if (channels == 8) /* 7.1 */ | |
746 hDecoder->channelConfiguration = 7; | |
747 if (channels == 7) /* not a standard channelConfiguration */ | |
748 hDecoder->channelConfiguration = 0; | |
749 } | |
750 | |
751 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix) | |
752 { | |
753 hDecoder->downMatrix = 1; | |
754 output_channels = 2; | |
755 } else { | |
756 output_channels = channels; | |
757 } | |
758 | |
759 /* Make a channel configuration based on either a PCE or a channelConfiguration */ | |
760 create_channel_config(hDecoder, hInfo); | |
761 | |
762 /* number of samples in this frame */ | |
763 hInfo->samples = frame_len*output_channels; | |
764 /* number of channels in this frame */ | |
765 hInfo->channels = output_channels; | |
766 /* samplerate */ | |
767 hInfo->samplerate = sample_rates[hDecoder->sf_index]; | |
768 | |
769 /* check if frame has channel elements */ | |
770 if (channels == 0) | |
771 { | |
772 hDecoder->frame++; | |
773 return NULL; | |
774 } | |
775 | |
776 if (hDecoder->sample_buffer == NULL) | |
777 { | |
778 #ifdef SBR_DEC | |
779 if (hDecoder->sbr_present_flag == 1) | |
780 { | |
781 if (hDecoder->config.outputFormat == FAAD_FMT_DOUBLE) | |
782 hDecoder->sample_buffer = malloc(2*frame_len*channels*sizeof(double)); | |
783 else | |
784 hDecoder->sample_buffer = malloc(2*frame_len*channels*sizeof(real_t)); | |
785 } else { | |
786 #endif | |
787 if (hDecoder->config.outputFormat == FAAD_FMT_DOUBLE) | |
788 hDecoder->sample_buffer = malloc(frame_len*channels*sizeof(double)); | |
789 else | |
790 hDecoder->sample_buffer = malloc(frame_len*channels*sizeof(real_t)); | |
791 #ifdef SBR_DEC | |
792 } | |
793 #endif | |
794 } | |
795 | |
796 sample_buffer = hDecoder->sample_buffer; | |
797 | |
798 /* noiseless coding is done, the rest of the tools come now */ | |
799 for (ch = 0; ch < channels; ch++) | |
800 { | |
801 ic_stream *ics; | |
802 | |
803 /* find the syntax element to which this channel belongs */ | |
804 if (syntax_elements[hDecoder->channel_element[ch]]->channel == ch) | |
805 ics = &(syntax_elements[hDecoder->channel_element[ch]]->ics1); | |
806 else if (syntax_elements[hDecoder->channel_element[ch]]->paired_channel == ch) | |
807 ics = &(syntax_elements[hDecoder->channel_element[ch]]->ics2); | |
808 | |
809 /* inverse quantization */ | |
810 inverse_quantization(spec_coef[ch], spec_data[ch], frame_len); | |
811 | |
812 /* apply scalefactors */ | |
813 #ifdef FIXED_POINT | |
814 apply_scalefactors(hDecoder, ics, spec_coef[ch], frame_len); | |
815 #else | |
816 apply_scalefactors(ics, spec_coef[ch], pow2_table, frame_len); | |
817 #endif | |
818 | |
819 /* deinterleave short block grouping */ | |
820 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
821 quant_to_spec(ics, spec_coef[ch], frame_len); | |
822 } | |
823 | |
824 /* Because for ms, is and pns both channels spectral coefficients are needed | |
825 we have to restart running through all channels here. | |
826 */ | |
827 for (ch = 0; ch < channels; ch++) | |
828 { | |
829 int16_t pch = -1; | |
830 uint8_t right_channel; | |
831 ic_stream *ics, *icsr; | |
832 ltp_info *ltp; | |
833 | |
834 /* find the syntax element to which this channel belongs */ | |
835 if (syntax_elements[hDecoder->channel_element[ch]]->channel == ch) | |
836 { | |
837 ics = &(syntax_elements[hDecoder->channel_element[ch]]->ics1); | |
838 icsr = &(syntax_elements[hDecoder->channel_element[ch]]->ics2); | |
839 ltp = &(ics->ltp); | |
840 pch = syntax_elements[hDecoder->channel_element[ch]]->paired_channel; | |
841 right_channel = 0; | |
842 } else if (syntax_elements[hDecoder->channel_element[ch]]->paired_channel == ch) { | |
843 ics = &(syntax_elements[hDecoder->channel_element[ch]]->ics2); | |
844 if (syntax_elements[hDecoder->channel_element[ch]]->common_window) | |
845 ltp = &(ics->ltp2); | |
846 else | |
847 ltp = &(ics->ltp); | |
848 right_channel = 1; | |
849 } | |
850 | |
851 /* pns decoding */ | |
852 if ((!right_channel) && (pch != -1) && (ics->ms_mask_present)) | |
853 pns_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len, 1); | |
854 else if ((pch == -1) || ((pch != -1) && (!ics->ms_mask_present))) | |
855 pns_decode(ics, NULL, spec_coef[ch], NULL, frame_len, 0); | |
856 | |
857 if (!right_channel && (pch != -1)) | |
858 { | |
859 /* mid/side decoding */ | |
860 ms_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len); | |
861 | |
862 /* intensity stereo decoding */ | |
863 is_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len); | |
864 } | |
865 | |
866 #ifdef MAIN_DEC | |
867 /* MAIN object type prediction */ | |
868 if (object_type == MAIN) | |
869 { | |
870 /* allocate the state only when needed */ | |
871 if (pred_stat[ch] == NULL) | |
872 { | |
873 pred_stat[ch] = (pred_state*)malloc(frame_len * sizeof(pred_state)); | |
874 reset_all_predictors(pred_stat[ch], frame_len); | |
875 } | |
876 | |
877 /* intra channel prediction */ | |
878 ic_prediction(ics, spec_coef[ch], pred_stat[ch], frame_len); | |
879 | |
880 /* In addition, for scalefactor bands coded by perceptual | |
881 noise substitution the predictors belonging to the | |
882 corresponding spectral coefficients are reset. | |
883 */ | |
884 pns_reset_pred_state(ics, pred_stat[ch]); | |
885 } | |
886 #endif | |
887 #ifdef LTP_DEC | |
888 if ((object_type == LTP) | |
889 #ifdef ERROR_RESILIENCE | |
890 || (object_type == ER_LTP) | |
891 #endif | |
892 #ifdef LD_DEC | |
893 || (object_type == LD) | |
894 #endif | |
895 ) | |
896 { | |
897 #ifdef LD_DEC | |
898 if (object_type == LD) | |
899 { | |
900 if (ltp->data_present) | |
901 { | |
902 if (ltp->lag_update) | |
903 ltp_lag[ch] = ltp->lag; | |
904 } | |
905 ltp->lag = ltp_lag[ch]; | |
906 } | |
907 #endif | |
908 | |
909 /* allocate the state only when needed */ | |
910 if (lt_pred_stat[ch] == NULL) | |
911 { | |
912 lt_pred_stat[ch] = (real_t*)malloc(frame_len*4 * sizeof(real_t)); | |
913 memset(lt_pred_stat[ch], 0, frame_len*4 * sizeof(real_t)); | |
914 } | |
915 | |
916 /* long term prediction */ | |
917 lt_prediction(ics, ltp, spec_coef[ch], lt_pred_stat[ch], fb, | |
918 ics->window_shape, window_shape_prev[ch], | |
919 sf_index, object_type, frame_len); | |
920 } | |
921 #endif | |
922 | |
923 /* tns decoding */ | |
924 tns_decode_frame(ics, &(ics->tns), sf_index, object_type, | |
925 spec_coef[ch], frame_len); | |
926 | |
927 /* drc decoding */ | |
928 if (drc->present) | |
929 { | |
930 if (!drc->exclude_mask[ch] || !drc->excluded_chns_present) | |
931 drc_decode(drc, spec_coef[ch]); | |
932 } | |
933 | |
934 if (time_out[ch] == NULL) | |
935 { | |
936 time_out[ch] = (real_t*)malloc(frame_len*2*sizeof(real_t)); | |
937 memset(time_out[ch], 0, frame_len*2*sizeof(real_t)); | |
938 } | |
939 #ifdef SBR_DEC | |
940 if (time_out2[ch] == NULL) | |
941 { | |
942 time_out2[ch] = (real_t*)malloc(frame_len*2*sizeof(real_t)); | |
943 memset(time_out2[ch], 0, frame_len*2*sizeof(real_t)); | |
944 } | |
945 #endif | |
946 | |
947 /* filter bank */ | |
948 #ifdef SSR_DEC | |
949 if (object_type != SSR) | |
950 { | |
951 #endif | |
952 ifilter_bank(fb, ics->window_sequence, ics->window_shape, | |
953 window_shape_prev[ch], spec_coef[ch], | |
954 time_out[ch], object_type, frame_len); | |
955 #ifdef SSR_DEC | |
956 } else { | |
957 if (ssr_overlap[ch] == NULL) | |
958 { | |
959 ssr_overlap[ch] = (real_t*)malloc(2*frame_len*sizeof(real_t)); | |
960 memset(ssr_overlap[ch], 0, 2*frame_len*sizeof(real_t)); | |
961 } | |
962 if (prev_fmd[ch] == NULL) | |
963 { | |
964 uint16_t k; | |
965 prev_fmd[ch] = (real_t*)malloc(2*frame_len*sizeof(real_t)); | |
966 for (k = 0; k < 2*frame_len; k++) | |
967 prev_fmd[ch][k] = REAL_CONST(-1); | |
968 } | |
969 | |
970 ssr_decode(&(ics->ssr), fb, ics->window_sequence, ics->window_shape, | |
971 window_shape_prev[ch], spec_coef[ch], time_out[ch], | |
972 ssr_overlap[ch], hDecoder->ipqf_buffer[ch], prev_fmd[ch], frame_len); | |
973 } | |
974 #endif | |
975 /* save window shape for next frame */ | |
976 window_shape_prev[ch] = ics->window_shape; | |
977 | |
978 #ifdef LTP_DEC | |
979 if ((object_type == LTP) | |
980 #ifdef ERROR_RESILIENCE | |
981 || (object_type == ER_LTP) | |
982 #endif | |
983 #ifdef LD_DEC | |
984 || (object_type == LD) | |
985 #endif | |
986 ) | |
987 { | |
988 lt_update_state(lt_pred_stat[ch], time_out[ch], time_out[ch]+frame_len, | |
989 frame_len, object_type); | |
990 } | |
991 #endif | |
992 } | |
993 | |
994 #ifdef SBR_DEC | |
995 if (hDecoder->sbr_present_flag == 1) | |
996 { | |
997 for (i = 0; i < ch_ele; i++) | |
998 { | |
999 if (syntax_elements[i]->paired_channel != -1) | |
1000 { | |
1001 memcpy(time_out2[syntax_elements[i]->channel], | |
1002 time_out[syntax_elements[i]->channel], frame_len*sizeof(real_t)); | |
1003 memcpy(time_out2[syntax_elements[i]->paired_channel], | |
1004 time_out[syntax_elements[i]->paired_channel], frame_len*sizeof(real_t)); | |
1005 sbrDecodeFrame(hDecoder->sbr[i], | |
1006 time_out2[syntax_elements[i]->channel], | |
1007 time_out2[syntax_elements[i]->paired_channel], ID_CPE, | |
1008 hDecoder->postSeekResetFlag); | |
1009 } else { | |
1010 memcpy(time_out2[syntax_elements[i]->channel], | |
1011 time_out[syntax_elements[i]->channel], frame_len*sizeof(real_t)); | |
1012 sbrDecodeFrame(hDecoder->sbr[i], | |
1013 time_out2[syntax_elements[i]->channel], | |
1014 NULL, ID_SCE, | |
1015 hDecoder->postSeekResetFlag); | |
1016 } | |
1017 } | |
1018 frame_len *= 2; | |
1019 hInfo->samples *= 2; | |
1020 hInfo->samplerate *= 2; | |
1021 | |
1022 sample_buffer = output_to_PCM(hDecoder, time_out2, sample_buffer, | |
1023 output_channels, frame_len, outputFormat); | |
1024 } else { | |
1025 #endif | |
1026 sample_buffer = output_to_PCM(hDecoder, time_out, sample_buffer, | |
1027 output_channels, frame_len, outputFormat); | |
1028 #ifdef SBR_DEC | |
1029 } | |
1030 #endif | |
1031 | |
1032 /* gapless playback */ | |
1033 if (hDecoder->samplesLeft != 0) | |
1034 { | |
1035 hInfo->samples = hDecoder->samplesLeft*channels; | |
1036 } | |
1037 hDecoder->samplesLeft = 0; | |
1038 | |
1039 hDecoder->postSeekResetFlag = 0; | |
1040 | |
1041 hDecoder->frame++; | |
1042 #ifdef LD_DEC | |
1043 if (object_type != LD) | |
1044 { | |
1045 #endif | |
1046 if (hDecoder->frame <= 1) | |
1047 hInfo->samples = 0; | |
1048 | |
1049 #if 0 | |
1050 if (hDecoder->frame == 2 && hDecoder->sbr_present_flag == 1) | |
1051 { | |
1052 uint8_t samplesize; | |
1053 switch (outputFormat) | |
1054 { | |
1055 case FAAD_FMT_16BIT: case FAAD_FMT_16BIT_DITHER: | |
1056 case FAAD_FMT_16BIT_L_SHAPE: case FAAD_FMT_16BIT_M_SHAPE: | |
1057 case FAAD_FMT_16BIT_H_SHAPE: | |
1058 samplesize = 2; | |
1059 break; | |
1060 case FAAD_FMT_24BIT: | |
1061 case FAAD_FMT_32BIT: | |
1062 case FAAD_FMT_FLOAT: | |
1063 samplesize = 4; | |
1064 break; | |
1065 case FAAD_FMT_DOUBLE: | |
1066 samplesize = 8; | |
1067 break; | |
1068 } | |
1069 hInfo->samples = 512*channels; | |
1070 memmove(sample_buffer, (void*)((char*)sample_buffer + 1536*channels*samplesize), hInfo->samples*samplesize); | |
1071 } | |
1072 #endif | |
1073 | |
1074 #ifdef LD_DEC | |
1075 } else { | |
1076 /* LD encoders will give lower delay */ | |
1077 if (hDecoder->frame <= 0) | |
1078 hInfo->samples = 0; | |
1079 } | |
1080 #endif | |
1081 | |
1082 /* cleanup */ | |
1083 for (ch = 0; ch < channels; ch++) | |
1084 { | |
1085 if (spec_coef[ch]) free(spec_coef[ch]); | |
1086 if (spec_data[ch]) free(spec_data[ch]); | |
1087 } | |
1088 | |
1089 for (i = 0; i < ch_ele; i++) | |
1090 { | |
1091 if (syntax_elements[i]) free(syntax_elements[i]); | |
1092 } | |
1093 | |
1094 #ifdef ANALYSIS | |
1095 fflush(stdout); | |
1096 #endif | |
1097 | |
1098 return sample_buffer; | |
1099 | |
1100 error: | |
1101 /* free all memory that could have been allocated */ | |
1102 faad_endbits(ld); | |
1103 if (ld) free(ld); | |
1104 | |
1105 /* cleanup */ | |
1106 for (ch = 0; ch < channels; ch++) | |
1107 { | |
1108 if (spec_coef[ch]) free(spec_coef[ch]); | |
1109 if (spec_data[ch]) free(spec_data[ch]); | |
1110 } | |
1111 | |
1112 for (i = 0; i < ch_ele; i++) | |
1113 { | |
1114 if (syntax_elements[i]) free(syntax_elements[i]); | |
1115 } | |
1116 | |
1117 #ifdef ANALYSIS | |
1118 fflush(stdout); | |
1119 #endif | |
1120 | |
1121 return NULL; | |
1122 } |