Quantcast
Channel: C++博客-所有随笔
Viewing all articles
Browse latest Browse all 7881

用Libaad2来进行AAC解码

$
0
0
转载自: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
-&gt;m_hAac = hAac;
    adecode
-&gt;m_init = 0;

    
return adecode;
}


int ADecode_Close (ADecode* adecode)
{
    
if (adecode-&gt;m_hAac != NULL)
    
{
        NeAACDecClose(adecode
-&gt;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-&gt;m_init == 0)
    
{
        adecode
-&gt;m_init = 1;
        unsigned 
long    samplerate;
        unsigned 
char    channels;
        NeAACDecInit (adecode
-&gt;m_hAac, (unsigned char *) buf, buf_len, &amp;samplerate, &amp;channels);
    }


    
short buf1[1024 * 4= {0};
    
int buf_off = 0;
    unsigned 
char *= (unsigned char *) buf;
    
    
do 
    
{
        
void* out = NeAACDecDecode (adecode-&gt;m_hAac, &amp;hInfo, p, buf_len);
        
if ((hInfo.error == 0&amp;&amp; (hInfo.samples &gt; 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 &gt; 0);

    
return ret;
}




杨粼波 2013-02-02 17:51 发表评论

Viewing all articles
Browse latest Browse all 7881

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>