用Libaad2来进行AAC解码

转自: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值