117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/*
217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** Copyright 2003-2010, VisualOn, Inc.
317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong **
417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** Licensed under the Apache License, Version 2.0 (the "License");
517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** you may not use this file except in compliance with the License.
617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** You may obtain a copy of the License at
717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong **
817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong **     http://www.apache.org/licenses/LICENSE-2.0
917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong **
1017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** Unless required by applicable law or agreed to in writing, software
1117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** distributed under the License is distributed on an "AS IS" BASIS,
1217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** See the License for the specific language governing permissions and
1417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong ** limitations under the License.
1517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong */
1617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/*******************************************************************************
1717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	File:		aacenc.c
1817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
1917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	Content:	aac encoder interface functions
2017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
2117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*******************************************************************************/
2217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
2317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#include "voAAC.h"
2417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#include "typedef.h"
2517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#include "aacenc_core.h"
2617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#include "aac_rom.h"
2717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#include "cmnMemory.h"
2817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#include "memalign.h"
2917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
3017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
3117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* Init the audio codec module and return codec handle
3217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param phCodec [OUT] Return the video codec handle
3317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param vType	[IN] The codec type if the module support multi codec.
3417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pUserData	[IN] The init param. It is memory operator or alloced memory
3517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \retval VO_ERR_NONE Succeeded.
3617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*/
3717299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_U32 VO_API voAACEncInit(VO_HANDLE * phCodec,VO_AUDIO_CODINGTYPE vType, VO_CODEC_INIT_USERDATA *pUserData)
3817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
3917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AAC_ENCODER*hAacEnc;
4017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AACENC_CONFIG config;
4117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	int error;
4217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
4317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#ifdef USE_DEAULT_MEM
4417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	VO_MEM_OPERATOR voMemoprator;
4517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#endif
4617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	VO_MEM_OPERATOR *pMemOP;
4717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	int interMem;
4817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
4917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	interMem = 0;
5017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	error = 0;
5117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
5217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* init the memory operator */
5317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(pUserData == NULL || pUserData->memflag != VO_IMF_USERMEMOPERATOR || pUserData->memData == NULL )
5417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
5517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#ifdef USE_DEAULT_MEM
5617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		voMemoprator.Alloc = cmnMemAlloc;
5717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		voMemoprator.Copy = cmnMemCopy;
5817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		voMemoprator.Free = cmnMemFree;
5917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		voMemoprator.Set = cmnMemSet;
6017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		voMemoprator.Check = cmnMemCheck;
6117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
6217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		interMem = 1;
6317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
6417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pMemOP = &voMemoprator;
6517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#else
6617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		*phCodec = NULL;
6717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_INVALID_ARG;
6817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#endif
6917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
7017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	else
7117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
7217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pMemOP = (VO_MEM_OPERATOR *)pUserData->memData;
7317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
7417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
7517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* init the aac encoder handle */
7617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc = (AAC_ENCODER*)mem_malloc(pMemOP, sizeof(AAC_ENCODER), 32, VO_INDEX_ENC_AAC);
7717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(NULL == hAacEnc)
7817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
7917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		error = 1;
8017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
8117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
8217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(!error)
8317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
8417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* init the aac encoder intra memory */
8517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->intbuf = (short *)mem_malloc(pMemOP, AACENC_BLOCKSIZE*MAX_CHANNELS*sizeof(short), 32, VO_INDEX_ENC_AAC);
8617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(NULL == hAacEnc->intbuf)
8717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
8817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			error = 1;
8917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
9017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
9117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
9217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if (!error) {
9317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* init the aac encoder psychoacoustic */
9417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		error = (PsyNew(&hAacEnc->psyKernel, MAX_CHANNELS, pMemOP) ||
9517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			PsyOutNew(&hAacEnc->psyOut, pMemOP));
9617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
9717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
9817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if (!error) {
9917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* init the aac encoder quantization elements */
10017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		error = QCOutNew(&hAacEnc->qcOut,MAX_CHANNELS, pMemOP);
10117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
10217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
10317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if (!error) {
10417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* init the aac encoder quantization state */
10517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		error = QCNew(&hAacEnc->qcKernel, pMemOP);
10617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
10717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
10817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* uninit the aac encoder if error is nozero */
10917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(error)
11017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
11117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		AacEncClose(hAacEnc, pMemOP);
11217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(hAacEnc)
11317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
11417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			mem_free(pMemOP, hAacEnc, VO_INDEX_ENC_AAC);
11517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			hAacEnc = NULL;
11617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
11717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		*phCodec = NULL;
11817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_OUTOF_MEMORY;
11917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
12017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
12117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* init the aac encoder memory operator  */
12217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#ifdef USE_DEAULT_MEM
12317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(interMem)
12417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
12517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->voMemoprator.Alloc = cmnMemAlloc;
12617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->voMemoprator.Copy = cmnMemCopy;
12717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->voMemoprator.Free = cmnMemFree;
12817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->voMemoprator.Set = cmnMemSet;
12917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->voMemoprator.Check = cmnMemCheck;
13017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
13117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pMemOP = &hAacEnc->voMemoprator;
13217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
13317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong#endif
13417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* init the aac encoder default parameter  */
13517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(hAacEnc->initOK == 0)
13617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
13717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 AACENC_CONFIG config;
13817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 config.adtsUsed = 1;
13917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 config.bitRate = 128000;
14017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 config.nChannelsIn = 2;
14117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 config.nChannelsOut = 2;
14217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 config.sampleRate = 44100;
14317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 config.bandWidth = 20000;
14417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
14517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 AacEncOpen(hAacEnc, config);
14617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
14717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
14817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc->voMemop = pMemOP;
14917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
15017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	*phCodec = hAacEnc;
15117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
15217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	return VO_ERR_NONE;
15317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}
15417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
15517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
15617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* Set input audio data.
15717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param hCodec [IN]] The Codec Handle which was created by Init function.
15817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pInput [IN] The input buffer param.
15917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pOutBuffer [OUT] The output buffer info.
16017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \retval VO_ERR_NONE Succeeded.
16117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*/
16217299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_U32 VO_API voAACEncSetInputData(VO_HANDLE hCodec, VO_CODECBUFFER * pInput)
16317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
16417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AAC_ENCODER *hAacEnc;
16517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	int  length;
16617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
16717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(NULL == hCodec || NULL == pInput || NULL == pInput->Buffer)
16817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
16917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_INVALID_ARG;
17017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
17117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
17217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc = (AAC_ENCODER *)hCodec;
17317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
17417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* init input pcm buffer and length*/
17517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc->inbuf = (short *)pInput->Buffer;
17617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc->inlen = pInput->Length / sizeof(short);
17717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc->uselength = 0;
17817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
17917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc->encbuf = hAacEnc->inbuf;
18017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	hAacEnc->enclen = hAacEnc->inlen;
18117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
18217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	/* rebuild intra pcm buffer and length*/
18317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(hAacEnc->intlen)
18417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
18517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		length = min(hAacEnc->config.nChannelsIn*AACENC_BLOCKSIZE - hAacEnc->intlen, hAacEnc->inlen);
18617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->voMemop->Copy(VO_INDEX_ENC_AAC, hAacEnc->intbuf + hAacEnc->intlen,
18717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			hAacEnc->inbuf, length*sizeof(short));
18817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
18917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->encbuf = hAacEnc->intbuf;
19017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->enclen = hAacEnc->intlen + length;
19117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
19217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->inbuf += length;
19317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->inlen -= length;
19417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
19517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
19617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	return VO_ERR_NONE;
19717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}
19817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
19917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
20017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* Get the outut audio data
20117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param hCodec [IN]] The Codec Handle which was created by Init function.
20217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pOutBuffer [OUT] The output audio data
20317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pOutInfo [OUT] The dec module filled audio format and used the input size.
20417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*						 pOutInfo->InputUsed is total used the input size.
20517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \retval  VO_ERR_NONE Succeeded.
20617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*			VO_ERR_INPUT_BUFFER_SMALL. The input was finished or the input data was not enought.
20717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*/
20817299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_U32 VO_API voAACEncGetOutputData(VO_HANDLE hCodec, VO_CODECBUFFER * pOutput, VO_AUDIO_OUTPUTINFO * pOutInfo)
20917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
21017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AAC_ENCODER* hAacEnc = (AAC_ENCODER*)hCodec;
21117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	Word16 numAncDataBytes=0;
21217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	Word32  inbuflen;
21317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	int ret, length;
21417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(NULL == hAacEnc)
21517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_INVALID_ARG;
21617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
21717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 inbuflen = AACENC_BLOCKSIZE*hAacEnc->config.nChannelsIn;
21817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
21917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 /* check the input pcm buffer and length*/
22017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 if(NULL == hAacEnc->encbuf || hAacEnc->enclen < inbuflen)
22117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 {
22217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		length = hAacEnc->enclen;
22317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(hAacEnc->intlen == 0)
22417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
22517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			hAacEnc->voMemop->Copy(VO_INDEX_ENC_AAC, hAacEnc->intbuf,
22617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				hAacEnc->encbuf, length*sizeof(short));
22717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			hAacEnc->uselength += length*sizeof(short);
22817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
22917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		else
23017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
23117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			hAacEnc->uselength += (length - hAacEnc->intlen)*sizeof(short);
23217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
23317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
23417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->intlen = length;
23517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
23617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pOutput->Length = 0;
23717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(pOutInfo)
23817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			pOutInfo->InputUsed = hAacEnc->uselength;
23917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_INPUT_BUFFER_SMALL;
24017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 }
24117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
24217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 /* check the output aac buffer and length*/
24317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 if(NULL == pOutput || NULL == pOutput->Buffer || pOutput->Length < (6144/8)*hAacEnc->config.nChannelsOut/(sizeof(Word32)))
24417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 return VO_ERR_OUTPUT_BUFFER_SMALL;
24517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
24617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 /* aac encoder core function */
24717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 AacEncEncode( hAacEnc,
24817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			(Word16*)hAacEnc->encbuf,
24917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			NULL,
25017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			&numAncDataBytes,
25117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			pOutput->Buffer,
25217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			&pOutput->Length);
25317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
25417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 /* update the input pcm buffer and length*/
25517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 if(hAacEnc->intlen)
25617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 {
25717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		length = inbuflen - hAacEnc->intlen;
25817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->encbuf = hAacEnc->inbuf;
25917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->enclen = hAacEnc->inlen;
26017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->uselength += length*sizeof(short);
26117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc->intlen = 0;
26217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 }
26317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 else
26417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 {
26517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 hAacEnc->encbuf = hAacEnc->encbuf + inbuflen;
26617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 hAacEnc->enclen = hAacEnc->enclen - inbuflen;
26717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		 hAacEnc->uselength += inbuflen*sizeof(short);
26817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 }
26917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
27017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 /* update the output aac information */
27117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(pOutInfo)
27217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
27317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pOutInfo->Format.Channels = hAacEnc->config.nChannelsOut;
27417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pOutInfo->Format.SampleRate = hAacEnc->config.sampleRate;
27517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pOutInfo->Format.SampleBits = 16;
27617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pOutInfo->InputUsed = hAacEnc->uselength;
27717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
27817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
27917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	 return VO_ERR_NONE;
28017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}
28117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
28217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
28317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* Uninit the Codec.
28417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param hCodec [IN]] The Codec Handle which was created by Init function.
28517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \retval VO_ERR_NONE Succeeded.
28617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*/
28717299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_U32 VO_API voAACEncUninit(VO_HANDLE hCodec)
28817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
28917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AAC_ENCODER* hAacEnc = (AAC_ENCODER*)hCodec;
29017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
29117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(NULL != hAacEnc)
29217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
29317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* close the aac encoder */
29417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		AacEncClose(hAacEnc, hAacEnc->voMemop);
29517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
29617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* free the aac encoder handle*/
29717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		mem_free(hAacEnc->voMemop, hAacEnc, VO_INDEX_ENC_AAC);
29817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		hAacEnc = NULL;
29917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
30017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
30117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	return VO_ERR_NONE;
30217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}
30317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
30417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
30517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* Set the param for special target.
30617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param hCodec [IN]] The Codec Handle which was created by Init function.
30717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param uParamID [IN] The param ID.
30817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pData [IN] The param value depend on the ID>
30917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \retval VO_ERR_NONE Succeeded.
31017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*/
31117299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_U32 VO_API voAACEncSetParam(VO_HANDLE hCodec, VO_S32 uParamID, VO_PTR pData)
31217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
31317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AACENC_CONFIG config;
31417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AACENC_PARAM* pAAC_param;
31517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	VO_AUDIO_FORMAT *pWAV_Format;
31617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	AAC_ENCODER* hAacEnc = (AAC_ENCODER*)hCodec;
31717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	int ret, i, bitrate, tmp;
31817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	int SampleRateIdx;
31917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
32017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(NULL == hAacEnc)
32117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_INVALID_ARG;
32217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
32317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	switch(uParamID)
32417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	{
32517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	case VO_PID_AAC_ENCPARAM:  /* init aac encoder parameter*/
32617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		AacInitDefaultConfig(&config);
32717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(pData == NULL)
32817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_INVALID_ARG;
32917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pAAC_param = (AACENC_PARAM*)pData;
33017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.adtsUsed = pAAC_param->adtsUsed;
33117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.bitRate = pAAC_param->bitRate;
33217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.nChannelsIn = pAAC_param->nChannels;
33317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.nChannelsOut = pAAC_param->nChannels;
33417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.sampleRate = pAAC_param->sampleRate;
33517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
33617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the channel */
33717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(config.nChannelsIn< 1  || config.nChannelsIn > MAX_CHANNELS  ||
33817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong             config.nChannelsOut < 1 || config.nChannelsOut > MAX_CHANNELS || config.nChannelsIn < config.nChannelsOut)
33917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			 return VO_ERR_AUDIO_UNSCHANNEL;
34017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
34117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the samplerate */
34217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		ret = -1;
34317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		for(i = 0; i < NUM_SAMPLE_RATES; i++)
34417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
34517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			if(config.sampleRate == sampRateTab[i])
34617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			{
34717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				ret = 0;
34817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				break;
34917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			}
35017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
35117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(ret < 0)
35217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_AUDIO_UNSSAMPLERATE;
35317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
35417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		SampleRateIdx = i;
35517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
35617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		tmp = 441;
35717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(config.sampleRate%8000 == 0)
35817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			tmp =480;
35917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the bitrate */
36017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(config.bitRate!=0 && (config.bitRate/config.nChannelsOut < 4000) ||
36117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong           (config.bitRate/config.nChannelsOut > 160000) ||
36217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		   (config.bitRate > config.sampleRate*6*config.nChannelsOut))
36317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
36417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			config.bitRate = 640*config.sampleRate/tmp*config.nChannelsOut;
36517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
36617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			if(config.bitRate/config.nChannelsOut < 4000)
36717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				config.bitRate = 4000 * config.nChannelsOut;
36817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			else if(config.bitRate > config.sampleRate*6*config.nChannelsOut)
36917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				config.bitRate = config.sampleRate*6*config.nChannelsOut;
37017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			else if(config.bitRate/config.nChannelsOut > 160000)
37117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				config.bitRate = config.nChannelsOut*160000;
37217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
37317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
37417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the bandwidth */
37517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		bitrate = config.bitRate / config.nChannelsOut;
37617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		bitrate = bitrate * tmp / config.sampleRate;
37717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
37817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		for (i = 0; rates[i]; i++)
37917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
38017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			if (rates[i] >= bitrate)
38117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				break;
38217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
38317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
38417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.bandWidth = BandwithCoefTab[i][SampleRateIdx];
38517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
38617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* init aac encoder core */
38717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		ret = AacEncOpen(hAacEnc, config);
38817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(ret)
38917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_AUDIO_UNSFEATURE;
39017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		break;
39117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	case VO_PID_AUDIO_FORMAT:	/* init pcm channel and samplerate*/
39217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		AacInitDefaultConfig(&config);
39317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(pData == NULL)
39417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_INVALID_ARG;
39517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		pWAV_Format = (VO_AUDIO_FORMAT*)pData;
39617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.adtsUsed = 1;
39717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.nChannelsIn = pWAV_Format->Channels;
39817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.nChannelsOut = pWAV_Format->Channels;
39917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.sampleRate = pWAV_Format->SampleRate;
40017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
40117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the channel */
40217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(config.nChannelsIn< 1  || config.nChannelsIn > MAX_CHANNELS  ||
40317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong             config.nChannelsOut < 1 || config.nChannelsOut > MAX_CHANNELS || config.nChannelsIn < config.nChannelsOut)
40417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			 return VO_ERR_AUDIO_UNSCHANNEL;
40517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
40617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the samplebits */
40717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(pWAV_Format->SampleBits != 16)
40817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
40917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_AUDIO_UNSFEATURE;
41017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
41117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
41217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the samplerate */
41317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		ret = -1;
41417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		for(i = 0; i < NUM_SAMPLE_RATES; i++)
41517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
41617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			if(config.sampleRate == sampRateTab[i])
41717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			{
41817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				ret = 0;
41917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				break;
42017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			}
42117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
42217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(ret < 0)
42317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_AUDIO_UNSSAMPLERATE;
42417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
42517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		SampleRateIdx = i;
42617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
42717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* update the bitrates */
42817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		tmp = 441;
42917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(config.sampleRate%8000 == 0)
43017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			tmp =480;
43117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
43217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.bitRate = 640*config.sampleRate/tmp*config.nChannelsOut;
43317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
43417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(config.bitRate/config.nChannelsOut < 4000)
43517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			config.bitRate = 4000 * config.nChannelsOut;
43617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		else if(config.bitRate > config.sampleRate*6*config.nChannelsOut)
43717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			config.bitRate = config.sampleRate*6*config.nChannelsOut;
43817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		else if(config.bitRate/config.nChannelsOut > 160000)
43917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			config.bitRate = config.nChannelsOut*160000;
44017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
44117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* check the bandwidth */
44217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		bitrate = config.bitRate / config.nChannelsOut;
44317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		bitrate = bitrate * tmp / config.sampleRate;
44417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
44517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		for (i = 0; rates[i]; i++)
44617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		{
44717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			if (rates[i] >= bitrate)
44817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong				break;
44917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		}
45017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
45117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		config.bandWidth = BandwithCoefTab[i][SampleRateIdx];
45217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
45317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		/* init aac encoder core */
45417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		ret = AacEncOpen(hAacEnc, config);
45517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		if(ret)
45617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong			return VO_ERR_AUDIO_UNSFEATURE;
45717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		break;
45817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	default:
45917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_WRONG_PARAM_ID;
46017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	}
46117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
46217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	return VO_ERR_NONE;
46317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}
46417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
46517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
46617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* Get the param for special target.
46717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param hCodec [IN]] The Codec Handle which was created by Init function.
46817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param uParamID [IN] The param ID.
46917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \param pData [IN] The param value depend on the ID>
47017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong* \retval VO_ERR_NONE Succeeded.
47117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong*/
47217299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_U32 VO_API voAACEncGetParam(VO_HANDLE hCodec, VO_S32 uParamID, VO_PTR pData)
47317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
47417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	return VO_ERR_NONE;
47517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}
47617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
47717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong/**
47817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong * Get audio codec API interface
47917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong * \param pEncHandle [out] Return the AAC Encoder handle.
48017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong * \retval VO_ERR_OK Succeeded.
48117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong */
48217299ab50ceb70d904e610e3b2d7fb2361a11e03James DongVO_S32 VO_API voGetAACEncAPI(VO_AUDIO_CODECAPI * pDecHandle)
48317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong{
48417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	if(pDecHandle == NULL)
48517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong		return VO_ERR_INVALID_ARG;
48617299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
48717299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	pDecHandle->Init = voAACEncInit;
48817299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	pDecHandle->SetInputData = voAACEncSetInputData;
48917299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	pDecHandle->GetOutputData = voAACEncGetOutputData;
49017299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	pDecHandle->SetParam = voAACEncSetParam;
49117299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	pDecHandle->GetParam = voAACEncGetParam;
49217299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	pDecHandle->Uninit = voAACEncUninit;
49317299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong
49417299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong	return VO_ERR_NONE;
49517299ab50ceb70d904e610e3b2d7fb2361a11e03James Dong}