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