本文的主要內容:使用H.264編碼對YUV視頻進行壓縮。
如果是命令行的操作,非常簡單。
ffmpeg -s 640x480 -pix_fmt yuv420p -i in.yuv -c:v libx264 out.h264
# -c:v libx264是指定使用libx264作為編碼器
接下來主要講解如何通過代碼的方式使用H.264編碼,用到了avcodec、avutil兩個庫,整體過程跟《AAC編碼實戰》類似。
類的聲明
extern "C" {
#include <libavutil/avutil.h>
}
typedef struct {
const char *filename;
int width;
int height;
AVPixelFormat pixFmt;
int fps;
} VideoEncodeSpec;
class FFmpegs {
public:
FFmpegs();
static void h264Encode(VideoEncodeSpec &in,
const char *outFilename);
};
類的使用
VideoEncodeSpec in;
in.filename = "F:/res/in.yuv";
in.width = 640;
in.height = 480;
in.fps = 30;
in.pixFmt = AV_PIX_FMT_YUV420P;
FFmpegs::h264Encode(in, "F:/res/out.h264");
宏定義
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
}
#define ERROR_BUF(ret) \
char errbuf[1024]; \
av_strerror(ret, errbuf, sizeof (errbuf));
變量定義
// 文件
QFile inFile(in.filename);
QFile outFile(outFilename);
// 一幀圖片的大小
int imgSize = av_image_get_buffer_size(in.pixFmt, in.width, in.height, 1);
// 返回結果
int ret = 0;
// 編碼器
AVCodec *codec = nullptr;
// 編碼上下文
AVCodecContext *ctx = nullptr;
// 存放編碼前的數據(yuv)
AVFrame *frame = nullptr;
// 存放編碼后的數據(h264)
AVPacket *pkt = nullptr;
初始化
// 獲取編碼器
codec = avcodec_find_encoder_by_name("libx264");
if (!codec) {
qDebug() << "encoder not found";
return;
}
// 檢查輸入數據的采樣格式
if (!check_pix_fmt(codec, in.pixFmt)) {
qDebug() << "unsupported pixel format"
<< av_get_pix_fmt_name(in.pixFmt);
return;
}
// 創建編碼上下文
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
qDebug() << "avcodec_alloc_context3 error";
return;
}
// 設置yuv參數
ctx->width = in.width;
ctx->height = in.height;
ctx->pix_fmt = in.pixFmt;
// 設置幀率(1秒鍾顯示的幀數是in.fps)
ctx->time_base = {1, in.fps};
// 打開編碼器
ret = avcodec_open2(ctx, codec, nullptr);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_open2 error" << errbuf;
goto end;
}
// 創建AVFrame
frame = av_frame_alloc();
if (!frame) {
qDebug() << "av_frame_alloc error";
goto end;
}
frame->width = ctx->width;
frame->height = ctx->height;
frame->format = ctx->pix_fmt;
frame->pts = 0;
// 利用width、height、format創建緩沖區
ret = av_image_alloc(frame->data, frame->linesize,
in.width, in.height, in.pixFmt, 1);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "av_frame_get_buffer error" << errbuf;
goto end;
}
// 創建AVPacket
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;
}
// 讀取數據到frame中
while ((ret = inFile.read((char *) frame->data[0],
imgSize)) > 0) {
// 進行編碼
if (encode(ctx, frame, pkt, outFile) < 0) {
goto end;
}
// 設置幀的序號
frame->pts++;
}
// 刷新緩沖區
encode(ctx, nullptr, pkt, outFile);
encode函數的實現如下所示:
// 返回負數:中途出現了錯誤
// 返回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) {
// 繼續讀取數據到frame,然后送到編碼器
return 0;
} else if (ret < 0) { // 其他錯誤
return ret;
}
// 成功從編碼器拿到編碼后的數據
// 將編碼后的數據寫入文件
outFile.write((char *) pkt->data, pkt->size);
// 釋放pkt內部的資源
av_packet_unref(pkt);
}
}
回收資源
end:
// 關閉文件
inFile.close();
outFile.close();
// 釋放資源
if (frame) {
av_freep(&frame->data[0]);
av_frame_free(&frame);
}
av_packet_free(&pkt);
avcodec_free_context(&ctx);