2003-08-30 22:30:28 +00:00
|
|
|
/*
|
|
|
|
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
2004-06-02 22:59:04 +00:00
|
|
|
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
|
|
**
|
2003-08-30 22:30:28 +00:00
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation; either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
2004-06-02 22:59:04 +00:00
|
|
|
**
|
2003-08-30 22:30:28 +00:00
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
2004-06-02 22:59:04 +00:00
|
|
|
**
|
2003-08-30 22:30:28 +00:00
|
|
|
** You should have received a copy of the GNU General Public License
|
2004-06-02 22:59:04 +00:00
|
|
|
** along with this program; if not, write to the Free Software
|
2003-08-30 22:30:28 +00:00
|
|
|
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
**
|
|
|
|
** Any non-GPL usage of this software or parts of this software is strictly
|
|
|
|
** forbidden.
|
|
|
|
**
|
|
|
|
** Commercial non-GPL licensing of this software is possible.
|
|
|
|
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
|
|
**
|
2006-04-18 19:39:34 +00:00
|
|
|
** $Id: decoder.c,v 1.107 2004/09/08 09:43:11 gcp Exp $
|
2003-08-30 22:30:28 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "structs.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "decoder.h"
|
|
|
|
#include "mp4.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "output.h"
|
2004-06-02 22:59:04 +00:00
|
|
|
#include "filtbank.h"
|
|
|
|
#include "drc.h"
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef SBR_DEC
|
|
|
|
#include "sbr_dec.h"
|
2004-06-02 22:59:04 +00:00
|
|
|
#include "sbr_syntax.h"
|
|
|
|
#endif
|
|
|
|
#ifdef SSR_DEC
|
|
|
|
#include "ssr.h"
|
2003-08-30 22:30:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANALYSIS
|
|
|
|
uint16_t dbg_count;
|
|
|
|
#endif
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
/* static function declarations */
|
|
|
|
static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
|
|
|
|
uint8_t *buffer, uint32_t buffer_size,
|
|
|
|
void **sample_buffer, uint32_t sample_buffer_size);
|
|
|
|
static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
|
|
|
|
|
|
|
|
|
|
|
|
char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
if (errcode >= NUM_ERROR_MESSAGES)
|
|
|
|
return NULL;
|
|
|
|
return err_msg[errcode];
|
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
uint32_t cap = 0;
|
|
|
|
|
|
|
|
/* can't do without it */
|
|
|
|
cap += LC_DEC_CAP;
|
|
|
|
|
|
|
|
#ifdef MAIN_DEC
|
|
|
|
cap += MAIN_DEC_CAP;
|
|
|
|
#endif
|
|
|
|
#ifdef LTP_DEC
|
|
|
|
cap += LTP_DEC_CAP;
|
|
|
|
#endif
|
|
|
|
#ifdef LD_DEC
|
|
|
|
cap += LD_DEC_CAP;
|
|
|
|
#endif
|
|
|
|
#ifdef ERROR_RESILIENCE
|
|
|
|
cap += ERROR_RESILIENCE_CAP;
|
|
|
|
#endif
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
cap += FIXED_POINT_CAP;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
uint8_t i;
|
2004-09-24 17:31:36 +00:00
|
|
|
NeAACDecHandle hDecoder = NULL;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
|
2003-08-30 22:30:28 +00:00
|
|
|
return NULL;
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
memset(hDecoder, 0, sizeof(NeAACDecStruct));
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
hDecoder->config.outputFormat = FAAD_FMT_16BIT;
|
|
|
|
hDecoder->config.defObjectType = MAIN;
|
|
|
|
hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
|
2004-06-02 22:59:04 +00:00
|
|
|
hDecoder->config.downMatrix = 0;
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->adts_header_present = 0;
|
|
|
|
hDecoder->adif_header_present = 0;
|
2008-01-26 18:42:17 +00:00
|
|
|
hDecoder->latm_header_present = 0;
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef ERROR_RESILIENCE
|
|
|
|
hDecoder->aacSectionDataResilienceFlag = 0;
|
|
|
|
hDecoder->aacScalefactorDataResilienceFlag = 0;
|
|
|
|
hDecoder->aacSpectralDataResilienceFlag = 0;
|
|
|
|
#endif
|
|
|
|
hDecoder->frameLength = 1024;
|
|
|
|
|
|
|
|
hDecoder->frame = 0;
|
|
|
|
hDecoder->sample_buffer = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
hDecoder->window_shape_prev[i] = 0;
|
|
|
|
hDecoder->time_out[i] = NULL;
|
2004-06-02 22:59:04 +00:00
|
|
|
hDecoder->fb_intermed[i] = NULL;
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef SSR_DEC
|
|
|
|
hDecoder->ssr_overlap[i] = NULL;
|
|
|
|
hDecoder->prev_fmd[i] = NULL;
|
|
|
|
#endif
|
|
|
|
#ifdef MAIN_DEC
|
|
|
|
hDecoder->pred_stat[i] = NULL;
|
|
|
|
#endif
|
|
|
|
#ifdef LTP_DEC
|
|
|
|
hDecoder->ltp_lag[i] = 0;
|
|
|
|
hDecoder->lt_pred_stat[i] = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SBR_DEC
|
2004-06-02 22:59:04 +00:00
|
|
|
for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
hDecoder->sbr[i] = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
|
|
|
|
|
|
|
|
return hDecoder;
|
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2003-10-03 22:23:26 +00:00
|
|
|
if (hDecoder)
|
|
|
|
{
|
2004-09-24 17:31:36 +00:00
|
|
|
NeAACDecConfigurationPtr config = &(hDecoder->config);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
return config;
|
2003-10-03 22:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
|
|
|
|
NeAACDecConfigurationPtr config)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2003-10-03 22:23:26 +00:00
|
|
|
if (hDecoder && config)
|
|
|
|
{
|
|
|
|
/* check if we can decode this object type */
|
|
|
|
if (can_decode_ot(config->defObjectType) < 0)
|
|
|
|
return 0;
|
2004-06-02 22:59:04 +00:00
|
|
|
hDecoder->config.defObjectType = config->defObjectType;
|
2003-10-03 22:23:26 +00:00
|
|
|
|
|
|
|
/* samplerate: anything but 0 should be possible */
|
|
|
|
if (config->defSampleRate == 0)
|
|
|
|
return 0;
|
2004-06-02 22:59:04 +00:00
|
|
|
hDecoder->config.defSampleRate = config->defSampleRate;
|
2003-10-03 22:23:26 +00:00
|
|
|
|
|
|
|
/* check output format */
|
2004-09-24 17:31:36 +00:00
|
|
|
#ifdef FIXED_POINT
|
|
|
|
if ((config->outputFormat < 1) || (config->outputFormat > 4))
|
2003-10-03 22:23:26 +00:00
|
|
|
return 0;
|
2004-09-24 17:31:36 +00:00
|
|
|
#else
|
|
|
|
if ((config->outputFormat < 1) || (config->outputFormat > 5))
|
|
|
|
return 0;
|
|
|
|
#endif
|
2004-06-02 22:59:04 +00:00
|
|
|
hDecoder->config.outputFormat = config->outputFormat;
|
2003-10-03 22:23:26 +00:00
|
|
|
|
|
|
|
if (config->downMatrix > 1)
|
2004-09-24 17:31:36 +00:00
|
|
|
return 0;
|
|
|
|
hDecoder->config.downMatrix = config->downMatrix;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
/* OK */
|
|
|
|
return 1;
|
2003-10-03 22:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
|
2008-01-26 18:45:11 +00:00
|
|
|
static int latmCheck(latm_header *latm, bitfile *ld)
|
|
|
|
{
|
|
|
|
uint32_t good=0, bad=0, bits, m;
|
|
|
|
|
|
|
|
while(!ld->error && !ld->no_more_reading)
|
|
|
|
{
|
|
|
|
bits = faad_latm_frame(latm, ld);
|
|
|
|
if(bits==-1U)
|
|
|
|
bad++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
good++;
|
|
|
|
while(bits>0)
|
|
|
|
{
|
|
|
|
m = min(bits, 8);
|
|
|
|
faad_getbits(ld, m);
|
|
|
|
bits -= m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (good>0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
|
|
|
|
uint32_t buffer_size,
|
|
|
|
uint32_t *samplerate, uint8_t *channels)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
uint32_t bits = 0;
|
|
|
|
bitfile ld;
|
|
|
|
adif_header adif;
|
|
|
|
adts_header adts;
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
|
|
|
|
return -1;
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
|
|
|
|
hDecoder->object_type = hDecoder->config.defObjectType;
|
2003-10-03 22:23:26 +00:00
|
|
|
*samplerate = get_sample_rate(hDecoder->sf_index);
|
2003-08-30 22:30:28 +00:00
|
|
|
*channels = 1;
|
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
2008-01-26 18:45:11 +00:00
|
|
|
int is_latm;
|
|
|
|
latm_header *l = &hDecoder->latm_config;
|
2003-08-30 22:30:28 +00:00
|
|
|
faad_initbits(&ld, buffer, buffer_size);
|
|
|
|
|
2008-01-26 18:45:11 +00:00
|
|
|
memset(l, 0, sizeof(latm_header));
|
|
|
|
is_latm = latmCheck(l, &ld);
|
|
|
|
l->inited = 0;
|
|
|
|
l->frameLength = 0;
|
|
|
|
faad_rewindbits(&ld);
|
|
|
|
if(is_latm && l->ASCbits>0)
|
|
|
|
{
|
|
|
|
int32_t x;
|
|
|
|
hDecoder->latm_header_present = 1;
|
|
|
|
x = NeAACDecInit2(hDecoder, &l->ASC, (l->ASCbits+7)/8, samplerate, channels);
|
|
|
|
if(x!=0)
|
|
|
|
hDecoder->latm_header_present = 0;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
else
|
2003-08-30 22:30:28 +00:00
|
|
|
/* Check if an ADIF header is present */
|
|
|
|
if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
|
|
|
|
(buffer[2] == 'I') && (buffer[3] == 'F'))
|
|
|
|
{
|
|
|
|
hDecoder->adif_header_present = 1;
|
|
|
|
|
|
|
|
get_adif_header(&adif, &ld);
|
|
|
|
faad_byte_align(&ld);
|
|
|
|
|
|
|
|
hDecoder->sf_index = adif.pce[0].sf_index;
|
2003-10-03 22:23:26 +00:00
|
|
|
hDecoder->object_type = adif.pce[0].object_type + 1;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
*samplerate = get_sample_rate(hDecoder->sf_index);
|
2003-08-30 22:30:28 +00:00
|
|
|
*channels = adif.pce[0].channels;
|
|
|
|
|
|
|
|
memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
|
|
|
|
hDecoder->pce_set = 1;
|
|
|
|
|
|
|
|
bits = bit2byte(faad_get_processed_bits(&ld));
|
|
|
|
|
|
|
|
/* Check if an ADTS header is present */
|
|
|
|
} else if (faad_showbits(&ld, 12) == 0xfff) {
|
|
|
|
hDecoder->adts_header_present = 1;
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
adts.old_format = hDecoder->config.useOldADTSFormat;
|
2003-08-30 22:30:28 +00:00
|
|
|
adts_frame(&adts, &ld);
|
|
|
|
|
|
|
|
hDecoder->sf_index = adts.sf_index;
|
2003-10-03 22:23:26 +00:00
|
|
|
hDecoder->object_type = adts.profile + 1;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
*samplerate = get_sample_rate(hDecoder->sf_index);
|
2003-08-30 22:30:28 +00:00
|
|
|
*channels = (adts.channel_configuration > 6) ?
|
|
|
|
2 : adts.channel_configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ld.error)
|
|
|
|
{
|
|
|
|
faad_endbits(&ld);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
faad_endbits(&ld);
|
|
|
|
}
|
|
|
|
hDecoder->channelConfiguration = *channels;
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
#if (defined(PS_DEC) || defined(DRM_PS))
|
|
|
|
/* check if we have a mono file */
|
|
|
|
if (*channels == 1)
|
|
|
|
{
|
|
|
|
/* upMatrix to 2 channels for implicit signalling of PS */
|
|
|
|
*channels = 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
#ifdef SBR_DEC
|
|
|
|
/* implicit signalling */
|
2004-06-02 22:59:04 +00:00
|
|
|
if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
|
2003-10-03 22:23:26 +00:00
|
|
|
{
|
|
|
|
*samplerate *= 2;
|
|
|
|
hDecoder->forceUpSampling = 1;
|
2004-09-24 17:31:36 +00:00
|
|
|
} else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
|
|
|
|
hDecoder->downSampledSBR = 1;
|
2003-10-03 22:23:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
/* must be done before frameLength is divided by 2 for LD */
|
|
|
|
#ifdef SSR_DEC
|
|
|
|
if (hDecoder->object_type == SSR)
|
|
|
|
hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
hDecoder->fb = filter_bank_init(hDecoder->frameLength);
|
|
|
|
|
|
|
|
#ifdef LD_DEC
|
|
|
|
if (hDecoder->object_type == LD)
|
|
|
|
hDecoder->frameLength >>= 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (can_decode_ot(hDecoder->object_type) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Init the library using a DecoderSpecificInfo */
|
2004-09-24 17:31:36 +00:00
|
|
|
int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
|
|
|
|
uint32_t SizeOfDecoderSpecificInfo,
|
|
|
|
uint32_t *samplerate, uint8_t *channels)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
int8_t rc;
|
|
|
|
mp4AudioSpecificConfig mp4ASC;
|
|
|
|
|
|
|
|
if((hDecoder == NULL)
|
|
|
|
|| (pBuffer == NULL)
|
|
|
|
|| (SizeOfDecoderSpecificInfo < 2)
|
|
|
|
|| (samplerate == NULL)
|
|
|
|
|| (channels == NULL))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
hDecoder->adif_header_present = 0;
|
|
|
|
hDecoder->adts_header_present = 0;
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
/* decode the audio specific config */
|
|
|
|
rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
|
2008-01-26 18:42:17 +00:00
|
|
|
&(hDecoder->pce), hDecoder->latm_header_present);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
/* copy the relevant info to the decoder handle */
|
|
|
|
*samplerate = mp4ASC.samplingFrequency;
|
|
|
|
if (mp4ASC.channelsConfiguration)
|
|
|
|
{
|
|
|
|
*channels = mp4ASC.channelsConfiguration;
|
|
|
|
} else {
|
|
|
|
*channels = hDecoder->pce.channels;
|
|
|
|
hDecoder->pce_set = 1;
|
|
|
|
}
|
2004-09-24 17:31:36 +00:00
|
|
|
#if (defined(PS_DEC) || defined(DRM_PS))
|
|
|
|
/* check if we have a mono file */
|
|
|
|
if (*channels == 1)
|
|
|
|
{
|
|
|
|
/* upMatrix to 2 channels for implicit signalling of PS */
|
|
|
|
*channels = 2;
|
|
|
|
}
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
|
|
|
|
hDecoder->object_type = mp4ASC.objectTypeIndex;
|
2003-10-03 22:23:26 +00:00
|
|
|
#ifdef ERROR_RESILIENCE
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
|
|
|
|
hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
|
|
|
|
hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
|
2003-10-03 22:23:26 +00:00
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef SBR_DEC
|
|
|
|
hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
|
2004-09-24 17:31:36 +00:00
|
|
|
hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->config.dontUpSampleImplicitSBR == 0)
|
|
|
|
hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
|
|
|
|
else
|
|
|
|
hDecoder->forceUpSampling = 0;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
/* AAC core decoder samplerate is 2 times as low */
|
2004-09-24 17:31:36 +00:00
|
|
|
if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
|
|
|
|
if (mp4ASC.frameLengthFlag)
|
2004-06-02 22:59:04 +00:00
|
|
|
#ifdef ALLOW_SMALL_FRAMELENGTH
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->frameLength = 960;
|
2004-06-02 22:59:04 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
/* must be done before frameLength is divided by 2 for LD */
|
|
|
|
#ifdef SSR_DEC
|
|
|
|
if (hDecoder->object_type == SSR)
|
|
|
|
hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
hDecoder->fb = filter_bank_init(hDecoder->frameLength);
|
|
|
|
|
|
|
|
#ifdef LD_DEC
|
|
|
|
if (hDecoder->object_type == LD)
|
|
|
|
hDecoder->frameLength >>= 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
#ifdef DRM
|
2004-09-24 17:31:36 +00:00
|
|
|
int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
|
|
|
|
uint8_t channels)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder == NULL)
|
|
|
|
return 1; /* error */
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
NeAACDecClose(*hDecoder);
|
|
|
|
|
|
|
|
*hDecoder = NeAACDecOpen();
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
/* Special object type defined for DRM */
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->config.defObjectType = DRM_ER_LC;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->config.defSampleRate = samplerate;
|
2003-10-03 22:23:26 +00:00
|
|
|
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
|
|
|
|
(*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
|
|
|
|
(*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
|
2003-10-03 22:23:26 +00:00
|
|
|
#endif
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->frameLength = 960;
|
|
|
|
(*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
|
|
|
|
(*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
|
2003-10-03 22:23:26 +00:00
|
|
|
|
|
|
|
if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->channelConfiguration = 2;
|
2003-10-03 22:23:26 +00:00
|
|
|
else
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->channelConfiguration = 1;
|
2003-10-03 22:23:26 +00:00
|
|
|
|
|
|
|
#ifdef SBR_DEC
|
|
|
|
if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->sbr_present_flag = 0;
|
2003-10-03 22:23:26 +00:00
|
|
|
else
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->sbr_present_flag = 1;
|
|
|
|
#endif
|
2004-06-02 22:59:04 +00:00
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
(*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-02 22:59:04 +00:00
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
if (hDecoder == NULL)
|
|
|
|
return;
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
#ifdef PROFILE
|
|
|
|
printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
|
|
|
|
printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
|
|
|
|
printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
|
|
|
|
printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
|
|
|
|
printf("output: %I64d cycles\n", hDecoder->output_cycles);
|
|
|
|
#endif
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
for (i = 0; i < MAX_CHANNELS; i++)
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
|
|
|
|
if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef SSR_DEC
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
|
|
|
|
if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
|
2003-08-30 22:30:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef MAIN_DEC
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
|
2003-08-30 22:30:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef LTP_DEC
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
|
2003-08-30 22:30:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SSR_DEC
|
|
|
|
if (hDecoder->object_type == SSR)
|
|
|
|
ssr_filter_bank_end(hDecoder->fb);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
filter_bank_end(hDecoder->fb);
|
|
|
|
|
|
|
|
drc_end(hDecoder->drc);
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
#ifdef SBR_DEC
|
2004-06-02 22:59:04 +00:00
|
|
|
for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
if (hDecoder->sbr[i])
|
|
|
|
sbrDecodeEnd(hDecoder->sbr[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder) faad_free(hDecoder);
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
if (hDecoder)
|
|
|
|
{
|
|
|
|
hDecoder->postSeekResetFlag = 1;
|
|
|
|
|
|
|
|
if (frame != -1)
|
|
|
|
hDecoder->frame = frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
hInfo->num_front_channels = 0;
|
|
|
|
hInfo->num_side_channels = 0;
|
|
|
|
hInfo->num_back_channels = 0;
|
|
|
|
hInfo->num_lfe_channels = 0;
|
|
|
|
memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
|
|
|
|
|
|
|
|
if (hDecoder->downMatrix)
|
|
|
|
{
|
|
|
|
hInfo->num_front_channels = 2;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if there is a PCE */
|
|
|
|
if (hDecoder->pce_set)
|
|
|
|
{
|
|
|
|
uint8_t i, chpos = 0;
|
|
|
|
uint8_t chdir, back_center = 0;
|
|
|
|
|
|
|
|
hInfo->num_front_channels = hDecoder->pce.num_front_channels;
|
|
|
|
hInfo->num_side_channels = hDecoder->pce.num_side_channels;
|
|
|
|
hInfo->num_back_channels = hDecoder->pce.num_back_channels;
|
|
|
|
hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
|
|
|
|
|
|
|
|
chdir = hInfo->num_front_channels;
|
|
|
|
if (chdir & 1)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
|
|
|
|
chdir--;
|
|
|
|
}
|
|
|
|
for (i = 0; i < chdir; i += 2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < hInfo->num_side_channels; i += 2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
chdir = hInfo->num_back_channels;
|
|
|
|
if (chdir & 1)
|
|
|
|
{
|
|
|
|
back_center = 1;
|
|
|
|
chdir--;
|
|
|
|
}
|
|
|
|
for (i = 0; i < chdir; i += 2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
if (back_center)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < hInfo->num_lfe_channels; i++)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[chpos++] = LFE_CHANNEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
switch (hDecoder->channelConfiguration)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
hInfo->num_front_channels = 1;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
hInfo->num_front_channels = 2;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
hInfo->num_front_channels = 3;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
hInfo->num_front_channels = 3;
|
|
|
|
hInfo->num_back_channels = 1;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
hInfo->num_front_channels = 3;
|
|
|
|
hInfo->num_back_channels = 2;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
hInfo->num_front_channels = 3;
|
|
|
|
hInfo->num_back_channels = 2;
|
|
|
|
hInfo->num_lfe_channels = 1;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[5] = LFE_CHANNEL;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
hInfo->num_front_channels = 3;
|
|
|
|
hInfo->num_side_channels = 2;
|
|
|
|
hInfo->num_back_channels = 2;
|
|
|
|
hInfo->num_lfe_channels = 1;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
|
|
|
|
hInfo->channel_position[7] = LFE_CHANNEL;
|
|
|
|
break;
|
|
|
|
default: /* channelConfiguration == 0 || channelConfiguration > 7 */
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
|
|
|
|
if (ch & 1) /* there's either a center front or a center back channel */
|
|
|
|
{
|
|
|
|
uint8_t ch1 = (ch-1)/2;
|
|
|
|
if (hDecoder->first_syn_ele == ID_SCE)
|
|
|
|
{
|
|
|
|
hInfo->num_front_channels = ch1 + 1;
|
|
|
|
hInfo->num_back_channels = ch1;
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
for (i = 1; i <= ch1; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
for (i = ch1+1; i < ch; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hInfo->num_front_channels = ch1;
|
|
|
|
hInfo->num_back_channels = ch1 + 1;
|
|
|
|
for (i = 0; i < ch1; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
for (i = ch1; i < ch-1; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint8_t ch1 = (ch)/2;
|
|
|
|
hInfo->num_front_channels = ch1;
|
|
|
|
hInfo->num_back_channels = ch1;
|
|
|
|
if (ch1 & 1)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
|
|
|
|
for (i = 1; i <= ch1; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
for (i = ch1+1; i < ch-1; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < ch1; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
for (i = ch1; i < ch; i+=2)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
|
|
|
|
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hInfo->num_lfe_channels = hDecoder->has_lfe;
|
|
|
|
for (i = ch; i < hDecoder->fr_channels; i++)
|
|
|
|
{
|
|
|
|
hInfo->channel_position[i] = LFE_CHANNEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
|
|
|
|
NeAACDecFrameInfo *hInfo,
|
|
|
|
uint8_t *buffer, uint32_t buffer_size)
|
|
|
|
{
|
|
|
|
return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
|
|
|
|
NeAACDecFrameInfo *hInfo,
|
|
|
|
uint8_t *buffer, uint32_t buffer_size,
|
|
|
|
void **sample_buffer, uint32_t sample_buffer_size)
|
|
|
|
{
|
|
|
|
if ((sample_buffer == NULL) || (sample_buffer_size == 0))
|
|
|
|
{
|
|
|
|
hInfo->error = 27;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
|
|
|
|
sample_buffer, sample_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
|
|
|
|
uint8_t *buffer, uint32_t buffer_size,
|
|
|
|
void **sample_buffer2, uint32_t sample_buffer_size)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
uint8_t channels = 0;
|
2003-08-30 22:30:28 +00:00
|
|
|
uint8_t output_channels = 0;
|
2004-06-02 22:59:04 +00:00
|
|
|
bitfile ld;
|
2003-10-03 22:23:26 +00:00
|
|
|
uint32_t bitsconsumed;
|
|
|
|
uint16_t frame_len;
|
|
|
|
void *sample_buffer;
|
2008-01-26 18:45:11 +00:00
|
|
|
uint32_t startbit=0, endbit=0, payload_bits=0;
|
2004-06-02 22:59:04 +00:00
|
|
|
|
|
|
|
#ifdef PROFILE
|
|
|
|
int64_t count = faad_get_ts();
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
/* safety checks */
|
2004-06-02 22:59:04 +00:00
|
|
|
if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
|
2003-10-03 22:23:26 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
#if 0
|
|
|
|
printf("%d\n", buffer_size*8);
|
|
|
|
#endif
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
frame_len = hDecoder->frameLength;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
|
2003-10-03 22:23:26 +00:00
|
|
|
memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
/* initialize the bitstream */
|
2004-06-02 22:59:04 +00:00
|
|
|
faad_initbits(&ld, buffer, buffer_size);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ((buffer_size+3)>>2); i++)
|
|
|
|
{
|
|
|
|
uint8_t *buf;
|
|
|
|
uint32_t temp = 0;
|
|
|
|
buf = faad_getbitbuffer(&ld, 32);
|
|
|
|
//temp = getdword((void*)buf);
|
|
|
|
temp = *((uint32_t*)buf);
|
|
|
|
printf("0x%.8X\n", temp);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
faad_endbits(&ld);
|
|
|
|
faad_initbits(&ld, buffer, buffer_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-26 18:45:11 +00:00
|
|
|
if(hDecoder->latm_header_present)
|
|
|
|
{
|
|
|
|
payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
|
|
|
|
startbit = faad_get_processed_bits(&ld);
|
|
|
|
if(payload_bits == -1U)
|
|
|
|
{
|
|
|
|
hInfo->error = 1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef DRM
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->object_type == DRM_ER_LC)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
/* We do not support stereo right now */
|
2004-09-24 17:31:36 +00:00
|
|
|
if (0) //(hDecoder->channelConfiguration == 2)
|
2004-06-02 22:59:04 +00:00
|
|
|
{
|
|
|
|
hInfo->error = 8; // Throw CRC error
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
faad_getbits(&ld, 8
|
2004-09-24 17:31:36 +00:00
|
|
|
DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (hDecoder->adts_header_present)
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
adts_header adts;
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
adts.old_format = hDecoder->config.useOldADTSFormat;
|
2004-06-02 22:59:04 +00:00
|
|
|
if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
|
2003-08-30 22:30:28 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* MPEG2 does byte_alignment() here,
|
|
|
|
* but ADTS header is always multiple of 8 bits in MPEG2
|
|
|
|
* so not needed to actually do it.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ANALYSIS
|
|
|
|
dbg_count = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* decode the complete bitstream */
|
2004-06-02 22:59:04 +00:00
|
|
|
#ifdef SCALABLE_DEC
|
|
|
|
if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
|
|
|
|
{
|
|
|
|
aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
|
|
|
|
#ifdef SCALABLE_DEC
|
|
|
|
}
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2008-01-26 18:45:11 +00:00
|
|
|
if(hDecoder->latm_header_present)
|
|
|
|
{
|
|
|
|
endbit = faad_get_processed_bits(&ld);
|
|
|
|
if(endbit-startbit > payload_bits)
|
|
|
|
fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
|
|
|
|
endbit-startbit, payload_bits);
|
|
|
|
if(hDecoder->latm_config.otherDataLenBits > 0)
|
|
|
|
faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
|
|
|
|
faad_byte_align(&ld);
|
|
|
|
}
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
channels = hDecoder->fr_channels;
|
|
|
|
|
|
|
|
if (hInfo->error > 0)
|
|
|
|
goto error;
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
/* safety check */
|
|
|
|
if (channels == 0 || channels > MAX_CHANNELS)
|
|
|
|
{
|
|
|
|
/* invalid number of channels */
|
|
|
|
hInfo->error = 12;
|
|
|
|
goto error;
|
|
|
|
}
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
/* no more bit reading after this */
|
2004-06-02 22:59:04 +00:00
|
|
|
bitsconsumed = faad_get_processed_bits(&ld);
|
2003-10-03 22:23:26 +00:00
|
|
|
hInfo->bytesconsumed = bit2byte(bitsconsumed);
|
2004-06-02 22:59:04 +00:00
|
|
|
if (ld.error)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
hInfo->error = 14;
|
|
|
|
goto error;
|
|
|
|
}
|
2004-06-02 22:59:04 +00:00
|
|
|
faad_endbits(&ld);
|
2003-10-03 22:23:26 +00:00
|
|
|
|
|
|
|
|
2008-01-26 18:45:11 +00:00
|
|
|
if (!hDecoder->adts_header_present && !hDecoder->adif_header_present && !hDecoder->latm_header_present)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-09-24 17:31:36 +00:00
|
|
|
if (hDecoder->channelConfiguration == 0)
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->channelConfiguration = channels;
|
|
|
|
|
|
|
|
if (channels == 8) /* 7.1 */
|
|
|
|
hDecoder->channelConfiguration = 7;
|
|
|
|
if (channels == 7) /* not a standard channelConfiguration */
|
|
|
|
hDecoder->channelConfiguration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
|
|
|
|
{
|
|
|
|
hDecoder->downMatrix = 1;
|
|
|
|
output_channels = 2;
|
|
|
|
} else {
|
|
|
|
output_channels = channels;
|
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
#if (defined(PS_DEC) || defined(DRM_PS))
|
|
|
|
hDecoder->upMatrix = 0;
|
|
|
|
/* check if we have a mono file */
|
|
|
|
if (output_channels == 1)
|
|
|
|
{
|
|
|
|
/* upMatrix to 2 channels for implicit signalling of PS */
|
|
|
|
hDecoder->upMatrix = 1;
|
|
|
|
output_channels = 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
/* Make a channel configuration based on either a PCE or a channelConfiguration */
|
|
|
|
create_channel_config(hDecoder, hInfo);
|
|
|
|
|
|
|
|
/* number of samples in this frame */
|
|
|
|
hInfo->samples = frame_len*output_channels;
|
|
|
|
/* number of channels in this frame */
|
|
|
|
hInfo->channels = output_channels;
|
|
|
|
/* samplerate */
|
2003-10-03 22:23:26 +00:00
|
|
|
hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
|
|
|
|
/* object type */
|
|
|
|
hInfo->object_type = hDecoder->object_type;
|
|
|
|
/* sbr */
|
|
|
|
hInfo->sbr = NO_SBR;
|
|
|
|
/* header type */
|
|
|
|
hInfo->header_type = RAW;
|
|
|
|
if (hDecoder->adif_header_present)
|
|
|
|
hInfo->header_type = ADIF;
|
|
|
|
if (hDecoder->adts_header_present)
|
|
|
|
hInfo->header_type = ADTS;
|
2008-01-26 18:45:11 +00:00
|
|
|
if (hDecoder->latm_header_present)
|
|
|
|
hInfo->header_type = LATM;
|
2006-04-18 19:39:34 +00:00
|
|
|
#if (defined(PS_DEC) || defined(DRM_PS))
|
|
|
|
hInfo->ps = hDecoder->ps_used_global;
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
/* check if frame has channel elements */
|
|
|
|
if (channels == 0)
|
|
|
|
{
|
|
|
|
hDecoder->frame++;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
/* allocate the buffer for the final samples */
|
|
|
|
if ((hDecoder->sample_buffer == NULL) ||
|
|
|
|
(hDecoder->alloced_channels != output_channels))
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
|
|
|
|
sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
|
|
|
|
sizeof(int16_t), sizeof(int16_t), 0, 0, 0
|
|
|
|
};
|
|
|
|
uint8_t stride = str[hDecoder->config.outputFormat-1];
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef SBR_DEC
|
2004-09-24 17:31:36 +00:00
|
|
|
if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
stride = 2 * stride;
|
2004-09-24 17:31:36 +00:00
|
|
|
}
|
2003-08-30 22:30:28 +00:00
|
|
|
#endif
|
2004-09-24 17:31:36 +00:00
|
|
|
/* check if we want to use internal sample_buffer */
|
|
|
|
if (sample_buffer_size == 0)
|
|
|
|
{
|
|
|
|
if (hDecoder->sample_buffer)
|
|
|
|
faad_free(hDecoder->sample_buffer);
|
|
|
|
hDecoder->sample_buffer = NULL;
|
|
|
|
hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
|
|
|
|
} else if (sample_buffer_size < frame_len*output_channels*stride) {
|
|
|
|
/* provided sample buffer is not big enough */
|
|
|
|
hInfo->error = 27;
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-02 22:59:04 +00:00
|
|
|
hDecoder->alloced_channels = output_channels;
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
if (sample_buffer_size == 0)
|
|
|
|
{
|
|
|
|
sample_buffer = hDecoder->sample_buffer;
|
|
|
|
} else {
|
|
|
|
sample_buffer = *sample_buffer2;
|
|
|
|
}
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
#ifdef SBR_DEC
|
|
|
|
if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
uint8_t ele;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
/* this data is different when SBR is used or when the data is upsampled */
|
2004-09-24 17:31:36 +00:00
|
|
|
if (!hDecoder->downSampledSBR)
|
|
|
|
{
|
|
|
|
frame_len *= 2;
|
|
|
|
hInfo->samples *= 2;
|
|
|
|
hInfo->samplerate *= 2;
|
|
|
|
}
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
/* check if every element was provided with SBR data */
|
|
|
|
for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->sbr[ele] == NULL)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
hInfo->error = 25;
|
|
|
|
goto error;
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
/* sbr */
|
|
|
|
if (hDecoder->sbr_present_flag == 1)
|
|
|
|
{
|
|
|
|
hInfo->object_type = HE_AAC;
|
|
|
|
hInfo->sbr = SBR_UPSAMPLED;
|
|
|
|
} else {
|
|
|
|
hInfo->sbr = NO_SBR_UPSAMPLED;
|
|
|
|
}
|
2004-09-24 17:31:36 +00:00
|
|
|
if (hDecoder->downSampledSBR)
|
|
|
|
{
|
|
|
|
hInfo->sbr = SBR_DOWNSAMPLED;
|
|
|
|
}
|
2003-08-30 22:30:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
|
|
|
|
output_channels, frame_len, hDecoder->config.outputFormat);
|
|
|
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
hDecoder->postSeekResetFlag = 0;
|
|
|
|
|
|
|
|
hDecoder->frame++;
|
|
|
|
#ifdef LD_DEC
|
2004-06-02 22:59:04 +00:00
|
|
|
if (hDecoder->object_type != LD)
|
2003-08-30 22:30:28 +00:00
|
|
|
{
|
|
|
|
#endif
|
|
|
|
if (hDecoder->frame <= 1)
|
|
|
|
hInfo->samples = 0;
|
|
|
|
#ifdef LD_DEC
|
|
|
|
} else {
|
|
|
|
/* LD encoders will give lower delay */
|
|
|
|
if (hDecoder->frame <= 0)
|
|
|
|
hInfo->samples = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
#ifdef ANALYSIS
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
#ifdef PROFILE
|
|
|
|
count = faad_get_ts() - count;
|
|
|
|
hDecoder->cycles += count;
|
|
|
|
#endif
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
return sample_buffer;
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
faad_endbits(&ld);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
/* cleanup */
|
2003-08-30 22:30:28 +00:00
|
|
|
#ifdef ANALYSIS
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|