前段時間,在學習試用FFmpeg播放音頻的時候老是有雜音,網上的不少教程是基於以前版本的FFmpeg的,而新的FFmepg3中audio增長了平面(planar)格式,而SDL播放音頻是不支持平面格式的,因此經過FFmpeg解碼出來的數據不能直接發送到SDL進行播放,須要進行一個格式轉換。經過網上一些資料,也可以正確的播放音頻了,可是對具體的音頻轉換過程不是很瞭解,這裏就對FFmpeg的對音頻的存儲格式及格式轉換作個總結。本文主要有如下幾個方面的內容:html
音頻解碼API avcodec_decode_audio4
在新版中已廢棄,替換爲使用更爲簡單的avcodec_send_packet
和avcodec_receive_frame
。本文簡單的介紹了該API的使用。git
在FFmpeg中使用枚舉AVSampleFormat
表示音頻的採樣格式,其聲明以下:github
enum AVSampleFormat { AV_SAMPLE_FMT_NONE = -1, AV_SAMPLE_FMT_U8, ///< unsigned 8 bits AV_SAMPLE_FMT_S16, ///< signed 16 bits AV_SAMPLE_FMT_S32, ///< signed 32 bits AV_SAMPLE_FMT_FLT, ///< float AV_SAMPLE_FMT_DBL, ///< double AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar AV_SAMPLE_FMT_FLTP, ///< float, planar AV_SAMPLE_FMT_DBLP, ///< double, planar AV_SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if linking dynamically };
和圖像的像素存儲格式相似,可使用8位無符號整數、16位有符號整數、32位有符號整數以及單精度浮點數,雙精度浮點數表示一個採樣。可是,沒有使用
24位的有符號整數,這是由於這些不一樣的格式使用的是原生的C類型,而C中是沒有24位的長度的類型的。express
Sample value can be expressed by native C types,hence the lack of a signed 24-bit sample format even though
it is a common raw audio data format.ide
對於浮點格式,其值在[-1.0,1.0]之間,任何在該區間以外的值都超過了最大音量的範圍。
和YUV的圖像格式格式,音頻的採樣格式分爲平面(planar)和打包(packed)兩種類型,在枚舉值中上半部分是packed類型,後面(有P後綴的)是planar類型。
對於planar格式的,每個通道的值都有一個單獨的plane,全部的plane必須有相同的大小;對於packed類型,全部的數據在同一個數據平面中,不一樣通道的數據
交叉保存。
另外,在AVFrame
中表示音頻採樣格式的字段format
是一個int型,在使用AVSampleFormat
時候須要進行一個類型轉換,將int轉換爲AVSampleFormat
枚舉值。函數
在頭文件samplefmt.h
提供了和音頻採樣格式相關的一些函數,現列舉一些以下:學習
const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
enum AVSampleFormat av_get_sample_fmt(const char *name)
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
AV_SAMPLE_FMT_S32P
,其返回值爲 AV_SAMPLE_FMT_S32
。enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,enum AVSampleFormat sample_fmt, int align)
linesize
可設爲null,align是buff空間的對齊格式(0=default,1 = no alignment)從上面可知,sample有兩種類型的存儲方式:平面(planar)和打包(packed),在planar中每個通道獨自佔用一個存儲平面;在packed中,全部通道的sample交織存儲在同一個
平面。可是,對於planar格式不知道具體的某一通道所在的平面;對於packed格式各個通道的數據是以怎麼樣的順序交織存儲的。這就須要藉助於channel_layout。
首先來看下FFmpeg對channel_layout的定義:
channel_layout是一個64位整數,每一個值爲1的位對應一個通道。也就說,channel_layout
的位模式中值爲1的個數等於其通道數量。ui
A channel_layout is a 64-bits interget with a bit set for every channel.The number of bits set must be equal to the number of channels.編碼
在頭文件channel_layout.h
中爲將每一個通道定義了一個mask,其定義以下:code
#define AV_CH_FRONT_LEFT 0x00000001 #define AV_CH_FRONT_RIGHT 0x00000002 #define AV_CH_FRONT_CENTER 0x00000004 #define AV_CH_LOW_FREQUENCY 0x00000008 #define AV_CH_BACK_LEFT 0x00000010 #define AV_CH_BACK_RIGHT 0x00000020 #define AV_CH_FRONT_LEFT_OF_CENTER 0x00000040 #define AV_CH_FRONT_RIGHT_OF_CENTER 0x00000080 #define AV_CH_BACK_CENTER 0x00000100 #define AV_CH_SIDE_LEFT 0x00000200 #define AV_CH_SIDE_RIGHT 0x00000400 #define AV_CH_TOP_CENTER 0x00000800 #define AV_CH_TOP_FRONT_LEFT 0x00001000 #define AV_CH_TOP_FRONT_CENTER 0x00002000 #define AV_CH_TOP_FRONT_RIGHT 0x00004000 #define AV_CH_TOP_BACK_LEFT 0x00008000 #define AV_CH_TOP_BACK_CENTER 0x00010000 #define AV_CH_TOP_BACK_RIGHT 0x00020000 #define AV_CH_STEREO_LEFT 0x20000000 ///< Stereo downmix. #define AV_CH_STEREO_RIGHT 0x40000000 ///< See AV_CH_STEREO_LEFT.
這樣,一個channel_layout就是上述channel mask的組合,部分定義以下:
#define AV_CH_LAYOUT_MONO (AV_CH_FRONT_CENTER) #define AV_CH_LAYOUT_STEREO (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT) #define AV_CH_LAYOUT_2POINT1 (AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY) #define AV_CH_LAYOUT_2_1 (AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER) #define AV_CH_LAYOUT_SURROUND (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) #define AV_CH_LAYOUT_3POINT1 (AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY) #define AV_CH_LAYOUT_4POINT0 (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_CENTER) #define AV_CH_LAYOUT_4POINT1 (AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY) #define AV_CH_LAYOUT_2_2 (AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT) #define AV_CH_LAYOUT_QUAD (AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) #define AV_CH_LAYOUT_5POINT0 (AV_CH_LAYOUT_SURROUND|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT) #define AV_CH_LAYOUT_5POINT1 (AV_CH_LAYOUT_5POINT0|AV_CH_LOW_FREQUENCY) ...
AV_CH_LAYOUT_STEREO
是立體聲(2通道),其通道的存放順序爲LEFT | RIGHT
;AV_CH_LAYOUT_4POINT0
是4通道,其通道的存放順序爲
LEFT|RIGHT|FRONT-CENTER|BACK-CENTER
;其它數量的聲道與此相似。
下面列舉一些和channel_layout相關的函數
uint64_t av_get_channel_layout(const char *name)
根據傳入的字符串,返回相對應的channel_layout。傳入的參數能夠是:
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
根據通道的layout返回通道的個數int64_t av_get_default_channel_layout(int nb_channels)
根據通道的個數返回默認的layoutint av_get_channel_layout_channel_index(uint64_t channel_layout,uint64_t channel);
返回通道在layout中的index,也就是某一通道av_get_channel_layout_channel_index
的實現以下:int av_get_channel_layout_channel_index(uint64_t channel_layout, uint64_t channel) { if (!(channel_layout & channel) || av_get_channel_layout_nb_channels(channel) != 1) return AVERROR(EINVAL); channel_layout &= channel - 1; return av_get_channel_layout_nb_channels(channel_layout); }
首先判斷傳入的layout包含該通道,而且保證該傳入的通道是一個單通道。
以4通道AV_CH_LAYOUT_4POINT0
爲例,說明下計算方法。AV_CH_LAYOUT_4POINT0 = AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER
其二進制表示爲0001,0000,0111
,假如想找AV_CH_BACK_CENTER
在該layout中的index。AV_CH_BACK_CENTER
的十六進制爲0x0100
,二進制爲0001,0000,0000
,那麼
AV_CH_BACK_CENTER - 1 = 1111,1111
。 0001,0000,0111 & 0000,1111,1111 = 0111
,函數av_get_channel_layout_nb_channels
是獲取某個layout對應的通道的數量,
前面提到,layout中值爲1的位的個數和通道的數量相等,因此AV_CH_BACK_CENTER
在layoutAV_CH_LAYOUT_4POINT0
的index爲3。
在FFmpeg中進行音頻的格式轉換主要有三個步驟
SwrContext
,並設置轉換所需的參數:通道數量、channel layout、sample rate有如下兩種方式來實例SwrContext
,並設置參數:
swr_alloc
SwrContext *swr = swr_alloc(); av_opt_set_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); av_opt_set_int(swr, "in_sample_rate", 48000, 0); av_opt_set_int(swr, "out_sample_rate", 44100, 0); av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0); av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
swr_alloc_set_opts
SwrContext *swr = swr_alloc_set_opts(NULL, // we're allocating a new context AV_CH_LAYOUT_STEREO, // out_ch_layout AV_SAMPLE_FMT_S16, // out_sample_fmt 44100, // out_sample_rate AV_CH_LAYOUT_5POINT1, // in_ch_layout AV_SAMPLE_FMT_FLTP, // in_sample_fmt 48000, // in_sample_rate 0, // log_offset NULL); // log_ctx
上述兩種方法設置那個的參數是將5.1聲道,channel layout爲AV_CH_LAYOUT_5POINT1,採樣率爲48KHz轉換爲2聲道,channel_layout爲AV_SAMPLE_FMT_S16,採樣率爲44.1KHz。
int dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, AVRounding(1));
函數av_rescale_rnd
是按照指定的舍入方式計算a * b / c 。
函數swr_get_delay
獲得輸入sample和輸出sample之間的延遲,而且其返回值的根據傳入的第二個參數不一樣而不一樣。若是是輸入的採樣率,則返回值是輸入sample個數;若是輸入的是輸出採樣率,則返回值是輸出sample個數。
swr_convert
進行轉換int nb = swr_convert(swr_ctx, &audio_buf, dst_nb_samples, (const uint8_t**)frame->data, frame->nb_samples);
其返回值爲轉換的sample個數。
avcodec_send_packet
和avcodec_receive_frame
獲取解碼後的原始數據int ret = avcodec_send_packet(aCodecCtx, &pkt); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return -1; ret = avcodec_receive_frame(aCodecCtx, frame); if (ret < 0 && ret != AVERROR_EOF) return -1;
這裏再也不使用
avcodec_decode_audio4
進行音頻的解碼,在FFmpeg3中該函數已被廢棄,使用avcodec_send_packet
和avcodec_receive_frame
替代。新的解碼API使用更爲方便,
具體參見官方文檔send/receive encoding and decoding API overview。
if (frame->channels > 0 && frame->channel_layout == 0) frame->channel_layout = av_get_default_channel_layout(frame->channels); else if (frame->channels == 0 && frame->channel_layout > 0) frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
若是channel layout未知(channel_layout = 0),根據通道數量獲取其默認的channel layout;如同通道的數量未知,則根據其channel layout獲得其通道數量。
AV_SAMPLE_FMT_S16
(16位有符號整型),輸出的channel layout也dst_layout = av_get_default_channel_layout(frame->channels)
(SDL2不支持planar格式)。實例化SwrContext
swr_ctx = swr_alloc_set_opts(nullptr, dst_layout, dst_format, frame->sample_rate, frame->channel_layout, (AVSampleFormat)frame->format, frame->sample_rate, 0, nullptr); if (!swr_ctx || swr_init(swr_ctx) < 0) return -1;
在設置完參數後,必定要調用swr_init
進行初始化。
// 計算轉換後的sample個數 a * b / c int dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, AVRounding(1)); // 轉換,返回值爲轉換後的sample個數 int nb = swr_convert(swr_ctx, &audio_buf, dst_nb_samples, (const uint8_t**)frame->data, frame->nb_samples); data_size = frame->channels * nb * av_get_bytes_per_sample(dst_format);
最後data_size
中保存的是轉換的數據的字節數:通道數 * sample個數 * 每一個sample的字節數。
本文主要介紹了在FFmepg中對音頻兩個重要屬性:採樣格式和channel layout的表示方法,並簡單的實現了一個音頻的格式轉換。
本文代碼 FFmpeg-playAudio.cpp