⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aacsbrenhanced.cpp

📁 6410BSP1
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Copy the input sample into the output sample - then transform the 
// output sample 'in place'. If we have all keyframes, then we should
// not do a copy. If we have cinepak or indeo and are decompressing 
// frame N it needs frame decompressed frame N-1 available to calculate
// it, unless we are at a keyframe. So with keyframed codecs, you can't 
// get away with applying the transform to change the frames in place, 
// because you'll mess up the next frames decompression. The runtime 
// MPEG decoder does not have keyframes in the same way so it can be 
// done in place. We know if a sample is key frame as we transform 
// because the sync point property will be set on the sample
///////////////////////////////////////////////////////////////////////
HRESULT CAACSBREnhanced::Transform(IMediaSample *pIn, IMediaSample *pOut)
{
    unsigned char *pBufferIn;
    unsigned char *pBufferOut;
    SsbSipAudioAacDecoderFrameInfo_t frameInfo;
    AM_MEDIA_TYPE *mtout;
     int iBytesConsumed=0;
    unsigned int ActualOutLength=0;
    short End;
    unsigned int decoded_bytes=0;
    static int once=1;
    frameInfo.sbrEnabled=0;


    HRESULT hr;
#if 0
if (first_time == 1)
RETAILMSG(1,(L"\n[EAAC PLUS] Transform....\n"));

{
LONGLONG  tStart, tEnd;
pIn->GetMediaTime(&tStart, &tEnd);
printf("\n\t*** Time = %u, %u", (unsigned int) tStart, (unsigned int) tEnd);
}
#endif
    memset(&frameInfo,0,sizeof(SsbSipAudioAacDecoderFrameInfo_t));
    hr= pIn->GetPointer(&pBufferIn);
    if(FAILED(hr))
    {
        return E_FAIL;
    }

    hr= pOut->GetPointer(&pBufferOut);
    if(FAILED(hr))
    {
        return E_FAIL;
    }

     int srcBufLength= pIn->GetActualDataLength();

    if(param_size!=0)
    {

        if(first_time==1)
        {
            if(stopped_once==1)
            {
                Initialize_Decoder(2048,param, param_size,&bytes_left);
            }
            first_time=0;
            once=1;
        }
        memcpy(InBuf, pBufferIn, srcBufLength);
        End=AACDecode_Ittiam(AACDecoder,InBuf,OutBuf,&frameInfo);

        if(frameInfo.error)
            {
                RETAILMSG(1,(L"\nError in decoding : %d\n", frameInfo.error));
                pOut->SetActualDataLength(ActualOutLength);
                Stop();
            }
        if(frameInfo.sbrEnabled && once)
        {
            uiSampleRate *=2;
            once=0;
            CMediaType temp;

            GetMediaType(0,&temp);

            mtout=(AM_MEDIA_TYPE*)&temp;
        

            WAVEFORMATEXTENSIBLE *fmt = (WAVEFORMATEXTENSIBLE*)mtout->pbFormat;

            fmt->Format.nChannels = uiChannels;
            fmt->Format.nSamplesPerSec =uiSampleRate ;
            fmt->Format.nBlockAlign = fmt->Format.wBitsPerSample / 8 * fmt->Format.nChannels;
            fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec * fmt->Format.nBlockAlign;

             hr = m_pOutput->QueryAccept((AM_MEDIA_TYPE*)&temp);

            if(hr==S_OK)
                hr=pOut->SetMediaType(mtout);
            
        }
        iBytesConsumed =frameInfo.bytesconsumed;

        memcpy(pBufferOut, (void*)OutBuf, frameInfo.samples*2);
        ActualOutLength=frameInfo.samples*2;
        pOut->SetActualDataLength(ActualOutLength);


RETAILMSG(0,(L"\n[EAAC PLUS] Decode  (in=%d)(out=%d)....\n", srcBufLength, ActualOutLength));
    }

    else
    {

        iBytesConsumed=0;
        if(first_time==1)
        {
            hr=Initialize_Decoder(srcBufLength,pBufferIn,srcBufLength,&iBytesConsumed);
            if(hr!= S_OK)
            {
                Stop();
                return E_FAIL;
            }

            first_time=0;
            once=1;
            
        }
        else
        {
            memcpy(InBuf+bytes_left, pBufferIn, srcBufLength);
        }

        bytes_left+=srcBufLength;
        ActualOutLength=0;
        
        while(1)
            {

                if(iBytesConsumed >0)
                {

                    bytes_left-=iBytesConsumed;
                    memmove(InBuf,InBuf+iBytesConsumed,bytes_left);
                    
                    if(bytes_left<500)
                    {
                        break;
                    }
                }

                End=AACDecode_Ittiam(AACDecoder,InBuf,OutBuf,&frameInfo);

                if(frameInfo.sbrEnabled && once)
                {
                    uiSampleRate *=2;
                    once=0;
                    CMediaType temp;
            
                    GetMediaType(0,&temp);

                    mtout=(AM_MEDIA_TYPE*)&temp;
                

                    WAVEFORMATEXTENSIBLE *fmt = (WAVEFORMATEXTENSIBLE*)mtout->pbFormat;

                    fmt->Format.nChannels = uiChannels;
                    fmt->Format.nSamplesPerSec =uiSampleRate ;
                    fmt->Format.nBlockAlign = fmt->Format.wBitsPerSample / 8 * fmt->Format.nChannels;
                    fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec * fmt->Format.nBlockAlign;

                     hr = m_pOutput->QueryAccept((AM_MEDIA_TYPE*)&temp);

                    if(hr==S_OK)
                        hr=pOut->SetMediaType(mtout);
                    
                }

                if(frameInfo.error)
                {
                    RETAILMSG(1,(L"\nError in decoding : %d\n", frameInfo.error));
                    pOut->SetActualDataLength(ActualOutLength);
                    Stop();
                }



                iBytesConsumed =frameInfo.bytesconsumed;

                memcpy(pBufferOut, (void*)OutBuf, frameInfo.samples*2);
                ActualOutLength+=frameInfo.samples*2;
                pBufferOut+=frameInfo.samples*2;

            }

            pOut->SetActualDataLength(ActualOutLength);
        
        }

    return S_OK;

}



///////////////////////////////////////////////////////////////////////
// DllRegisterServer
///////////////////////////////////////////////////////////////////////
STDAPI DllRegisterServer()
{
    return AMovieDllRegisterServer2( TRUE );
}

///////////////////////////////////////////////////////////////////////
// DllUnregisterServer
///////////////////////////////////////////////////////////////////////
STDAPI DllUnregisterServer()
{
    return AMovieDllRegisterServer2( FALSE );
}

HRESULT CAACSBREnhanced::AACExit()
{
    if(InBuf) 
        free(InBuf);
    if(OutBuf) 
        free(OutBuf);
    if(AACDecoder)
        SAACFreeDecoder(AACDecoder);
    InBuf=NULL;
    OutBuf=NULL;
    AACDecoder=NULL;

    return S_OK;
}


///////////////////////////////////////////////////////////////////////
// Stop
///////////////////////////////////////////////////////////////////////
HRESULT CAACSBREnhanced::Stop()
{
    
    AACExit();

    first_time=1;
    stopped_once=1;
    bytes_left=0;

    CTransformFilter::Stop();

     return S_OK;
}

STDMETHODIMP CAACSBREnhanced::Initialize_Decoder(int inpBufSize, BYTE* buffer, int buffer_size, int *bytes_consumed)
{
        int hdrDecResult;
        if(InBuf==NULL)
        {
            InBuf = (unsigned char*)malloc(inpBufSize*2);
            if(!InBuf) 
            {
                RETAILMSG(err_AAC,(TEXT("memory could not be allocated for input buffer\n")));
                return E_FAIL;
            }
        }

        if(OutBuf==NULL)
        {
            OutBuf = malloc(OUTBUFSIZE * sizeof(short));
            memset(OutBuf, 0, OUTBUFSIZE*2);
            if(!OutBuf) 
            {
                AACExit();
                RETAILMSG(err_AAC,(TEXT("memory could not be allocated for output buffer\n")));
                return E_FAIL;
            }
        }

        // Intialize the Decoder 
        if(AACDecoder==NULL)
        {
            AACDecoder = (SAACDecoder*)SAACInitDecoder();
            if(!AACDecoder)
            {
                AACExit();

                RETAILMSG(err_AAC,(TEXT("decoder Initialization failed\n")));
                return 0;
            }
        }
        

        memcpy(InBuf,buffer,buffer_size);
    
        hdrDecResult = AACHeaderDecode_Ittiam(AACDecoder,InBuf, &config);
    
        *bytes_consumed = hdrDecResult;
        uiSampleRate = config.defSampleRate;
        uiChannels = config.defNumChannels;

        if(*bytes_consumed < 0)
        {
            RETAILMSG(err_AAC,(TEXT("header decode error\n")));
            if(*bytes_consumed == -5)
            {
                RETAILMSG(err_AAC,(TEXT("ADTS Sync-Word Not Found\n")));
            }

            return E_FAIL;
        }

        return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -