轉(zhuǎn)載自:http://www.haogongju.net/art/1546339
一直在做一個(gè)語(yǔ)音項(xiàng)目,到了測(cè)試階段,近來(lái)不是很忙,想把之前做的內(nèi)容整理一下。
關(guān)于AAC音頻格式基本情況,可參考維基百科http://en.wikipedia.org/wiki/Advanced_Audio_Coding
AAC音頻格式分析
AAC音頻格式有ADIF和ADTS:
ADIF:Audio Data Interchange Format 音頻數(shù)據(jù)交換格式。這種格式的特征是可以確定的找到這個(gè)音頻數(shù)據(jù)的開(kāi)始,不需進(jìn)行在音頻數(shù)據(jù)流中間開(kāi)始的解碼,即它的解碼必須在明確定義的開(kāi)始處進(jìn)行。故這種格式常用在磁盤文件中。
ADTS:Audio Data Transport Stream 音頻數(shù)據(jù)傳輸流。這種格式的特征是它是一個(gè)有同步字的比特流,解碼可以在這個(gè)流中任何位置開(kāi)始。它的特征類似于mp3數(shù)據(jù)流格式。
簡(jiǎn)單說(shuō),ADTS可以在任意幀解碼,也就是說(shuō)它每一幀都有頭信息。ADIF只有一個(gè)統(tǒng)一的頭,所以必須得到所有的數(shù)據(jù)后解碼。且這兩種的header的格式也是不同的,目前一般編碼后的和抽取出的都是ADTS格式的音頻流。
語(yǔ)音系統(tǒng)對(duì)實(shí)時(shí)性要求較高,基本是這樣一個(gè)流程,采集音頻數(shù)據(jù),本地編碼,數(shù)據(jù)上傳,服務(wù)器處理,數(shù)據(jù)下發(fā),本地解碼
ADTS是幀序列,本身具備流特征,在音頻流的傳輸與處理方面更加合適。
ADTS幀結(jié)構(gòu):
ADTS幀首部結(jié)構(gòu):
序號(hào) |
域 |
長(zhǎng)度(bits) |
說(shuō)明 |
1 |
Syncword |
12 |
all bits must be 1 |
2 |
MPEG version |
1 |
0 for MPEG-4, 1 for MPEG-2 |
3 |
Layer |
2 |
always 0 |
4 |
Protection Absent |
1 |
et to 1 if there is no CRC and 0 if there is CRC |
5 |
Profile |
2 |
the MPEG-4 Audio Object Type minus 1 |
6 |
MPEG-4 Sampling Frequency Index |
4 |
MPEG-4 Sampling Frequency Index (15 is forbidden) |
7 |
Private Stream |
1 |
set to 0 when encoding, ignore when decoding |
8 |
MPEG-4 Channel Configuration |
3 |
MPEG-4 Channel Configuration (in the case of 0, the channel configuration is sent via an inband PCE) |
9 |
Originality |
1 |
set to 0 when encoding, ignore when decoding |
10 |
Home |
1 |
set to 0 when encoding, ignore when decoding |
11 |
Copyrighted Stream |
1 |
set to 0 when encoding, ignore when decoding |
12 |
Copyrighted Start |
1 |
set to 0 when encoding, ignore when decoding |
13 |
Frame Length |
13 |
this value must include 7 or 9 bytes of header length: FrameLength = (ProtectionAbsent == 1 ? 7 : 9) + size(AACFrame) |
14 |
Buffer Fullness |
11 |
buffer fullness |
15 |
Number of AAC Frames |
2 |
number of AAC frames (RDBs) in ADTS frame minus 1, for maximum compatibility always use 1 AAC frame per ADTS frame |
16 |
CRC |
16 |
CRC if protection absent is 0 |
A
AC解碼
在解碼方面,使用了開(kāi)源的FAAD,http://www.audiocoding.com/faad2.html
sdk解壓縮后,docs目錄有詳細(xì)的api說(shuō)明文檔,主要用到的有以下幾個(gè):
NeAACDecHandle NEAACAPI NeAACDecOpen(void);
創(chuàng)建解碼環(huán)境并返回一個(gè)句柄
void NEAACAPI NeAACDecClose(NeAACDecHandle hDecoder);
關(guān)閉解碼環(huán)境
NeAACDecConfigurationPtr NEAACAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder);
獲取當(dāng)前解碼器庫(kù)的配置
unsigned char NEAACAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder, NeAACDecConfigurationPtr config);
為解碼器庫(kù)設(shè)置一個(gè)配置結(jié)構(gòu)
long NEAACAPI NeAACDecInit(NeAACDecHandle hDecoder, unsigned char *buffer, unsigned long buffer_size, unsigned long *samplerate, unsigned char *channels);
初始化解碼器庫(kù)
void* NEAACAPI NeAACDecDecode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, unsigned char *buffer, unsigned long buffer_size);
解碼AAC數(shù)據(jù)
對(duì)以上api做了簡(jiǎn)單封裝,寫了一個(gè)解碼類,涵蓋了FAAD庫(kù)的基本用法,感興趣的朋友可以看看
MyAACDecoder.h:
/**
*
* filename: MyAACDecoder.h
* summary: convert aac to wave
* author: caosiyang
* email: csy3228@gmail.com
*
*/
#ifndef __MYAACDECODER_H__
#define __MYAACDECODER_H__
#include "Buffer.h"
#include "mytools.h"
#include "WaveFormat.h"
#include "faad.h"
#include <iostream>
using namespace std;
class MyAACDecoder {
public:
MyAACDecoder();
~MyAACDecoder();
int32_t Decode(char *aacbuf, uint32_t aacbuflen);
const char* WavBodyData() const {
return _mybuffer.Data();
}
uint32_t WavBodyLength() const {
return _mybuffer.Length();
}
const char* WavHeaderData() const {
return _wave_format.getHeaderData();
}
uint32_t WavHeaderLength() const {
return _wave_format.getHeaderLength();
}
private:
MyAACDecoder(const MyAACDecoder &dec);
MyAACDecoder& operator=(const MyAACDecoder &rhs);
//init AAC decoder
int32_t _init_aac_decoder(char *aacbuf, int32_t aacbuflen);
//destroy aac decoder
void _destroy_aac_decoder();
//parse AAC ADTS header, get frame length
uint32_t _get_frame_length(const char *aac_header) const;
//AAC decoder properties
NeAACDecHandle _handle;
unsigned long _samplerate;
unsigned char _channel;
Buffer _mybuffer;
WaveFormat _wave_format;
};
#endif /*__MYAACDECODER_H__*/
MyAACDecoder.cpp:
#include "MyAACDecoder.h"
MyAACDecoder::MyAACDecoder(): _handle(NULL), _samplerate(44100), _channel(2), _mybuffer(4096, 4096) {
}
MyAACDecoder::~MyAACDecoder() {
_destroy_aac_decoder();
}
int32_t MyAACDecoder::Decode(char *aacbuf, uint32_t aacbuflen) {
int32_t res = 0;
if (!_handle) {
if (_init_aac_decoder(aacbuf, aacbuflen) != 0) {
ERR1(":::: init aac decoder failed ::::");
return -1;
}
}
//clean _mybuffer
_mybuffer.Clean();
uint32_t donelen = 0;
uint32_t wav_data_len = 0;
while (donelen < aacbuflen) {
uint32_t framelen = _get_frame_length(aacbuf + donelen);
if (donelen + framelen > aacbuflen) {
break;
}
//decode
NeAACDecFrameInfo info;
void *buf = NeAACDecDecode(_handle, &info, (unsigned char*)aacbuf + donelen, framelen);
if (buf && info.error == 0) {
if (info.samplerate == 44100) {
//44100Hz
//src: 2048 samples, 4096 bytes
//dst: 2048 samples, 4096 bytes
uint32_t tmplen = info.samples * 16 / 8;
_mybuffer.Fill((const char*)buf, tmplen);
wav_data_len += tmplen;
} else if (info.samplerate == 22050) {
//22050Hz
//src: 1024 samples, 2048 bytes
//dst: 2048 samples, 4096 bytes
short *ori = (short*)buf;
short tmpbuf[info.samples * 2];
uint32_t tmplen = info.samples * 16 / 8 * 2;
for (int32_t i = 0, j = 0; i < info.samples; i += 2) {
tmpbuf[j++] = ori[i];
tmpbuf[j++] = ori[i + 1];
tmpbuf[j++] = ori[i];
tmpbuf[j++] = ori[i + 1];
}
_mybuffer.Fill((const char*)tmpbuf, tmplen);
wav_data_len += tmplen;
}
} else {
ERR1("NeAACDecDecode() failed");
}
donelen += framelen;
}
//generate Wave header
_wave_format.setSampleRate(_samplerate);
_wave_format.setChannel(_channel);
_wave_format.setSampleBit(16);
_wave_format.setBandWidth(_samplerate * 16 * _channel / 8);
_wave_format.setDataLength(wav_data_len);
_wave_format.setTotalLength(wav_data_len + 44);
_wave_format.GenerateHeader();
return 0;
}
uint32_t MyAACDecoder::_get_frame_length(const char *aac_header) const {
uint32_t len = *(uint32_t *)(aac_header + 3);
len = ntohl(len); //Little Endian
len = len << 6;
len = len >> 19;
return len;
}
int32_t MyAACDecoder::_init_aac_decoder(char* aacbuf, int32_t aacbuflen) {
unsigned long cap = NeAACDecGetCapabilities();
_handle = NeAACDecOpen();
if (!_handle) {
ERR1("NeAACDecOpen() failed");
_destroy_aac_decoder();
return -1;
}
NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(_handle);
if (!conf) {
ERR1("NeAACDecGetCurrentConfiguration() failed");
_destroy_aac_decoder();
return -1;
}
NeAACDecSetConfiguration(_handle, conf);
long res = NeAACDecInit(_handle, (unsigned char *)aacbuf, aacbuflen, &_samplerate, &_channel);
if (res < 0) {
ERR1("NeAACDecInit() failed");
_destroy_aac_decoder();
return -1;
}
//fprintf(stdout, "SampleRate = %d\n", _samplerate);
//fprintf(stdout, "Channel = %d\n", _channel);
//fprintf(stdout, ":::: init aac decoder done ::::\n");
return 0;
}
void MyAACDecoder::_destroy_aac_decoder() {
if (_handle) {
NeAACDecClose(_handle);
_handle = NULL;
}
}