一直在做一个语音项目,到了测试阶段,近来不是很忙,想把之前做的内容整理一下。

关于AAC音频格式基本情况,可参考维基百科http://en.wikipedia.org/wiki/Advanced_Audio_Coding

AAC音频格式分析

AAC音频格式有ADIF和ADTS:

ADIF:Audio Data Interchange Format 音频数据交换格式。这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即它的解码必须在明确定义的开始处进行。故这种格式常用在磁盘文件中。

ADTS:Audio Data Transport Stream 音频数据传输流。这种格式的特征是它是一个有同步字的比特流,解码可以在这个流中任何位置开始。它的特征类似于mp3数据流格式。

简单说,ADTS可以在任意帧解码,也就是说它每一帧都有头信息。ADIF只有一个统一的头,所以必须得到所有的数据后解码。且这两种的header的格式也是不同的,目前一般编码后的和抽取出的都是ADTS格式的音频流。

语音系统对实时性要求较高,基本是这样一个流程,采集音频数据,本地编码,数据上传,服务器处理,数据下发,本地解码

ADTS是帧序列,本身具备流特征,在音频流的传输与处理方面更加合适。

ADTS帧结构:

header

body

ADTS帧首部结构:

序号 长度(bits) 说明
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

AAC解码

在解码方面,使用了开源的FAAD,http://www.audiocoding.com/faad2.html

sdk解压缩后,docs目录有详细的api说明文档,主要用到的有以下几个:

NeAACDecHandle NEAACAPI NeAACDecOpen(void);
创建解码环境并返回一个句柄
void NEAACAPI NeAACDecClose(NeAACDecHandle hDecoder);
关闭解码环境
NeAACDecConfigurationPtr NEAACAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder);
获取当前解码器库的配置
unsigned char NEAACAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder, NeAACDecConfigurationPtr config);
为解码器库设置一个配置结构
long NEAACAPI NeAACDecInit(NeAACDecHandle hDecoder, unsigned char *buffer, unsigned long buffer_size, unsigned long *samplerate, unsigned char *channels);
初始化解码器库
void* NEAACAPI NeAACDecDecode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, unsigned char *buffer, unsigned long buffer_size);
解码AAC数据

对以上api做了简单封装,写了一个解码类,涵盖了FAAD库的基本用法,感兴趣的朋友可以看看

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;
    }
}
From http://www.cnblogs.com/caosiyang/

AAC 格式分析的更多相关文章

  1. AAC ADTS AAC LATM 格式分析

    http://blog.csdn.net/tx3344/article/details/7414543# 目录(?)[-] ADTS是个啥 ADTS内容及结构 将AAC打包成ADTS格式 1.ADTS ...

  2. 【转】AAC ADTS格式分析

    1.ADTS是个啥 ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽 ...

  3. (转)AAC ADTS格式分析

    1,ADTS是个啥ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式记得第一做demux的时候,把AAC音频的ES流从FLV封装格式中抽出来送给 ...

  4. 【多媒体封装格式详解】--- AAC ADTS格式分析

    ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽出来送给硬件解码器时 ...

  5. (转)【多媒体封装格式详解】--- AAC ADTS格式分析

     出自:http://blog.csdn.net/tx3344/article/details/7414543 http://www.it6655.com/2012/08/aac-adts-html ...

  6. aac格式解析

    AAC格式有以下两种: ADIF:Audio Data Interchange Format 音频数据交换格式.这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即 ...

  7. 视音频编解码学习工程:AAC格式分析器

    =====================================================视音频编解码学习工程系列文章列表: 视音频编解码学习工程:H.264分析器 视音频编解码学习工 ...

  8. HTTP POST请求报文格式分析与Java实现文件上传

    时间 2014-12-11 12:41:43  CSDN博客 原文  http://blog.csdn.net/bboyfeiyu/article/details/41863951 主题 HTTPHt ...

  9. Google的Protocol Buffer格式分析

    [转]转自:序列化笔记之一:Google的Protocol Buffer格式分析 从公开介绍来看,ProtocolBuffer(PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.作 ...

随机推荐

  1. 理解maven

    1.理解“仓库” 首次运行完mvn -version后,会在用户目录下创建一个.m2的目录(比如:C:\Users\当前用户名\.m2\),这个目录是maven的“本地仓库”,仓库是maven中一个很 ...

  2. Ado.Net小练习03(省市联动)

    前台界面:          后台代码: namespace _04省市联动 {     public partial class Form1 : Form     {         public ...

  3. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  4. pancake sort的几个问题

    1. 每次找剩下序列中的最大值,可以进行pancake sort,时间复杂度是O(n^2): 2. 求最少交换次数进行pancake sort的问题是个NP问题,搜索的时候,upper bound是2 ...

  5. oracle command - creata database dbca & create network netca Demo

    #Creata database command: dbca [root@redhat4 ~]# su - oracle[oracle@redhat4 ~]$ dbca               # ...

  6. [HDOJ1811]Rank of Tetris(并查集、拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811 求一堆数据的拓扑序. 处理:x>y就是x到y一条边,x<y就是y到x一条边.关键问题 ...

  7. Git使用简介

    git创建分支并直接切换到分支:git checkout -b name git提交分支到远程服务器: git push origin name/git push origin name:name   ...

  8. Junit单元测试的实例

    进行单元测试的代码 package JunitTest; import org.junit.Test; public class Calculator { private static int res ...

  9. ZJ2008树的统计(树链剖分)

    type node1=record go,next:longint;end; node2=record l,r,mx,sum:longint;end; var i,x,y,n,q,tmp,cnt,sz ...

  10. php通过curl调用jpush接口实现消息的推送

    public function actionNotifyto() { //$regid = $_REQUEST['regid']; $url = 'https://api.jpush.cn/v3/pu ...