1. 如何查看 ffmpeg 中支持的 AAC 編解碼器?
admindeMac-mini:$ ffmpeg -codecs | grep aac
DEAIL. aac AAC (Advanced Audio Coding) (decoders: aac aac_fixed aac_at libfdk_aac ) (encoders: aac aac_at libfdk_aac ) D.AIL. aac_latm AAC LATM (Advanced Audio Coding LATM syntax) 複製代碼
2. AVCodec
是一個重要的結構體,主要存儲什麼信息?
- AVCodec是存儲
編解碼器
信息的結構體
- 主要變量以下:
const char *name:編解碼器的名字,比較短
const char *long_name:編解碼器的名字,全稱,比較長
enum AVMediaType type:指明瞭類型,是視頻,音頻,仍是字幕 enum AVCodecID id:ID,不重複 const AVRational *supported_framerates:支持的幀率(僅視頻) const enum AVPixelFormat *pix_fmts:支持的像素格式(僅視頻) const int *supported_samplerates:支持的採樣率(僅音頻) const enum AVSampleFormat *sample_fmts:支持的採樣格式(僅音頻) const uint64_t *channel_layouts:支持的聲道數(僅音頻) int priv_data_size:私有數據的大小 複製代碼
3. AVFrame
是一個重要的結構體,主要存儲什麼信息?
AVFrame
用於存儲編碼前的數據(好比 PCM 等原始數據)
- 主要變量以下:
uint8_t *data[AV_NUM_DATA_POINTERS]:解碼後原始數據(對視頻來講是YUV,RGB,對音頻來講是PCM)
int linesize[AV_NUM_DATA_POINTERS]:data中「一行」數據的大小。注意:未必等於圖像的寬,通常大於圖像的寬。
int width, height:視頻幀寬和高(1920x1080,1280x720...)
int nb_samples:音頻的一個AVFrame中可能包含多個音頻幀,在此標記包含了幾個
int format:解碼後原始數據類型(YUV420,YUV422,RGB24...)
int key_frame:是不是關鍵幀
enum AVPictureType pict_type:幀類型(I,B,P...) AVRational sample_aspect_ratio:寬高比(16:9,4:3...)
int64_t pts:顯示時間戳
int coded_picture_number:編碼幀序號
int display_picture_number:顯示幀序號
int8_t *qscale_table:QP表
uint8_t *mbskip_table:跳過宏塊表
int16_t (*motion_val[2])[2]:運動矢量表
uint32_t *mb_type:宏塊類型表
short *dct_coeff:DCT係數,這個沒有提取過
int8_t *ref_index[2]:運動估計參考幀列表(貌似H.264這種比較新的標準纔會涉及到多參考幀)
int interlaced_frame:是不是隔行掃描
uint8_t motion_subsample_log2:一個宏塊中的運動矢量採樣個數,取log的
複製代碼
4. AVPacket
是一個重要的結構體,主要存儲什麼信息?
AVPacket
是存儲編碼後的數據(好比 aac 等壓縮後的數據)
- 重要變量有以下幾個
uint8_t *data:壓縮編碼的數據。
例如對於H.264來講。1個AVPacket的data一般對應一個NAL。
注意:在這裏只是對應,而不是如出一轍。他們之間有微小的差異:使用FFMPEG類庫分離出多媒體文件中的H.264碼流
所以在使用FFMPEG進行視音頻處理的時候,經常能夠將獲得的AVPacket的data數據直接寫成文件,從而獲得視音頻的碼流文件。
int size:data的大小
int64_t pts:顯示時間戳
int64_t dts:解碼時間戳
int stream_index:標識該AVPacket所屬的視頻/音頻流。
複製代碼
5. AVCodecContext 是一個重要的結構體,主要存儲什麼信息?
- AVCodecContext 是包含變量較多的結構體,須要注意 AVCodecContext 中不少的參數是編碼的時候使用的,而不是解碼的時候使用的。
- 重要變量有以下幾個
enum AVMediaType codec_type:編解碼器的類型(視頻,音頻...) struct AVCodec *codec:採用的解碼器AVCodec(H.264,MPEG2...) int bit_rate:平均比特率 uint8_t *extradata; int extradata_size:針對特定編碼器包含的附加信息(例如對於H.264解碼器來講,存儲SPS,PPS等)
AVRational time_base:根據該參數,能夠把PTS轉化爲實際的時間(單位爲秒s)
int width, height:若是是視頻的話,表明寬和高
int refs:運動估計參考幀的個數(H.264的話會有多幀,MPEG2這類的通常就沒有了)
int sample_rate:採樣率(音頻)
int channels:聲道數(音頻)
enum AVSampleFormat sample_fmt:採樣格式 int profile:型(H.264裏面就有,其餘編碼標準應該也有) int level:級(和profile差不太多) 複製代碼
6. 使用命令行將 PCM 編碼成指定格式的 AAC,命令以下?而後用 ffproble out.acc 能夠查看相關參數
ffmpeg -ar 44100 -ac 2 -f s16le -i 44100_s16le_2.pcm -c:a libfdk_aac -profile:a aac_he_v2 -b:a 32k out_terminal.aac
複製代碼
admindeMac-mini:$ ffprobe out_terminal.aac
Input #0, aac, from 'out_terminal.aac':
Duration: 00:00:10.16, bitrate: 32 kb/s
Stream #0:0: Audio: aac (HE-AACv2), 44100 Hz, stereo, fltp, 32 kb/s
複製代碼
7. 請簡述用代碼實現 PCM 轉 AAC 的關鍵步驟(5 個元素吧,重要)?
![image.png](http://static.javashuo.com/static/loading.gif)
8. 使用代碼實現和上面命令行同樣的效果,主要代碼在 ffmpegs.cpp
中,經過對比命令行和代碼生成的 aac 文件大小,能夠側面印證代碼的正確性。
#include "ffmpegs.h"
#include <QDebug>
#include <QFile>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
}
#define ERROR_BUF(ret) \ char errbuf[1024]; \ av_strerror(ret, errbuf, sizeof (errbuf));
FFmpegs::FFmpegs()
{
}
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt) {
const enum AVSampleFormat *p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
if (*p == sample_fmt) {
return 1;
}
p++;
}
return 0;
}
static int encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, QFile &outFile) {
int ret = avcodec_send_frame(ctx, frame);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_send_frame error" << errbuf;
return ret;
}
while (true) {
ret = avcodec_receive_packet(ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
} else if (ret < 0) {
return ret;
}
outFile.write((char *)pkt->data, pkt->size);
av_packet_unref(pkt);
}
}
void FFmpegs::accEncode(AudioEncodeSpec &in, const char *outFilename) {
QFile inFile(in.filename);
QFile outFile(outFilename);
int ret = 0;
AVCodec *codec = nullptr;
AVCodecContext *ctx = nullptr;
AVFrame *frame = nullptr;
AVPacket *pkt = nullptr;
codec = avcodec_find_encoder_by_name("libfdk_aac");
if (!codec) {
qDebug() << "encoder not found";
return;
}
if (!check_sample_fmt(codec, in.sampleFmt)) {
qDebug() << "unsupported sample format"
<< av_get_sample_fmt_name(in.sampleFmt);
return;
}
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
qDebug() << "avcodec_alloc_context3 error" ;
return;
}
ctx->sample_rate = in.sampleRate;
ctx->sample_fmt = in.sampleFmt;
ctx->channel_layout = in.chLayout;
ctx->bit_rate = 32000;
ctx->profile = FF_PROFILE_AAC_HE_V2;
ret = avcodec_open2(ctx, codec, nullptr);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_open2 error" << errbuf;
goto end;
}
frame = av_frame_alloc();
if (!frame) {
qDebug() << "av_frame_alloc error";
goto end;
}
frame->nb_samples = ctx->frame_size;
frame->format = ctx->sample_fmt;
frame->channel_layout = ctx->channel_layout;
ret = av_frame_get_buffer(frame, 0);
if (ret) {
ERROR_BUF(ret);
qDebug() << "av_frame_get_buffer error" << errbuf;
goto end;
}
pkt = av_packet_alloc();
if (!pkt) {
qDebug() << "av_packet_alloc error";
goto end;
}
if (!inFile.open(QFile::ReadOnly)) {
qDebug() << "file open error" << in.filename;
goto end;
}
if (!outFile.open(QFile::WriteOnly)) {
qDebug() << "file open error" << outFilename;
goto end;
}
while ((ret = inFile.read((char *)frame->data[0],
frame->linesize[0])) > 0) {
if (ret < frame->linesize[0]) {
int bytes = av_get_bytes_per_sample((AVSampleFormat)frame->format);
int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
frame->nb_samples = ret/(bytes * ch);
}
if (encode(ctx, frame, pkt, outFile)) {
goto end;
}
}
encode(ctx, nullptr, pkt, outFile);
end:
inFile.close();
outFile.close();
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&ctx);
qDebug() << "線程正常結束";
}
複製代碼