牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

用Libaad2来进行AAC解码

转载自:http://www.oschina.net/code/snippet_196503_6933


头文件:
///////////////////////////////////////////////////////////////////////
////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 *= (unsigned char *) buf;
    
    
do 
    
{
        
void* out = 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;
}


posted on 2013-02-02 17:51 杨粼波 阅读(1987) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理