用Libaad2来进行AAC解码

来源:互联网 发布:锚点跳转源码 编辑:程序博客网 时间:2024/05/07 08:02

转自:http://www.cppblog.com/tx7do/archive/2013/02/02/197682.html


头文件:

///////////////////////////////////////////////////////////////////////
////audio_decode.h        2010-11-15 by lishen

#ifndef _AUDIODECODE_H_
#define _AUDIODECODE_H_
#include "../lib/neaacdec.h"

typedef struct _ADecode
{
    NeAACDecHandle  m_hAac;    // audio decode handle 
    int                m_init;
}
ADecode;
typedef int (*audio_decode_event)(DWORD arg1, const char *buf, int len);

ADecode* ADecode_Open ();
int ADecode_Close (ADecode* adecode);
int ADecode_Decode (ADecode* adecode, 
                    const char *buf, 
                    int buf_len, 
                    audio_decode_event fnt, 
                    DWORD arg);


#endif


实现文件:
///////////////////////////////////////////////////////////////////////
////audio_decode.cpp        2010-11-15 by lishen

#include "../common/common.h"
#include <windows.h>
#include <stdio.h>
#include "audio_decode.h"
#pragma comment(lib, "libfaad2.lib")

ADecode* ADecode_Open ()
{
    NeAACDecHandle hAac = NeAACDecOpen();
    
    NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(hAac);
    NeAACDecSetConfiguration(hAac, conf);

    ADecode* adecode = new ADecode ();
    adecode->m_hAac = hAac;
    adecode->m_init = 0;

    return adecode;
}


int ADecode_Close (ADecode* adecode)
{
    if (adecode->m_hAac != NULL)
    {
        NeAACDecClose(adecode->m_hAac);
    }

    delete adecode;

    return 0;
}


int ADecode_Decode (ADecode* adecode, const char *buf, int buf_len, 
                        audio_decode_event fnt, DWORD arg)
{
    int ret = 0;
    NeAACDecFrameInfo hInfo;

    if (adecode->m_init == 0)
    {
        adecode->m_init = 1;
        unsigned long    samplerate;
        unsigned char    channels;
        NeAACDecInit (adecode->m_hAac, (unsigned char *) buf, buf_len, &samplerate, &channels);
    }


    short buf1[1024 * 4] = {0};
    int buf_off = 0;
    unsigned char *p = (unsigned char *) buf;
    
    do 
    {
        voidout = NeAACDecDecode (adecode->m_hAac, &hInfo, p, buf_len);
        if ((hInfo.error == 0) && (hInfo.samples > 0))
        {
            p += hInfo.bytesconsumed; 
            buf_len -= hInfo.bytesconsumed;

            // distill wave
            short *p1 = buf1, *p2 = (short*) out;
            for (int k = (hInfo.samples / hInfo.channels); k; k --){*p1 ++ = *p2; p2 += 2;}
            
            //trace0 (PROG_DEBUG, "%s-%d ADecode_decode %d.", __FILE__, __LINE__, len);
            
// put out wave
            if (fnt != 0){ret = fnt (arg, (char*) buf1, hInfo.samples);}
        }

        else if (hInfo.error != 0)
        {
            ret = -1;
            break;
        }

    }
while (buf_len > 0);

    return ret;
}

0 0
原创粉丝点击