// 檢查像素格式
static int check_pix_fmt(const AVCodec *codec,
enum AVPixelFormat pixFmt) {
const enum AVPixelFormat *p = codec->pix_fmts;
while (*p != AV_PIX_FMT_NONE) {
if (*p == pixFmt) return 1;
p++;
}
return 0;
}
typedef struct {
const char *filename;
int width;
int height;
AVPixelFormat pixFmt;
int fps;
} VideoEncodeSpec;
// 返回負數(shù):中途出現(xiàn)了錯誤
// 返回0:編碼操作正常完成
static int encode(AVCodecContext *ctx,
AVFrame *frame,
AVPacket *pkt,
QFile &outFile) {
// 發(fā)送數(shù)據(jù)到編碼器
int ret = avcodec_send_frame(ctx, frame);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_send_frame error" << errbuf;
return ret;
}
// 不斷從編碼器中取出編碼后的數(shù)據(jù)
while (true) {
ret = avcodec_receive_packet(ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
// 繼續(xù)讀取數(shù)據(jù)到frame,然后送到編碼器
return 0;
} else if (ret < 0) { // 其他錯誤
return ret;
}
// 成功從編碼器拿到編碼后的數(shù)據(jù)
// 將編碼后的數(shù)據(jù)寫入文件
outFile.write((char *) pkt->data, pkt->size);
// 釋放pkt內(nèi)部的資源
av_packet_unref(pkt);
}
}
void FFmpegs::h264Encode(VideoEncodeSpec &in,
const char *outFilename) {
// 文件
QFile inFile(in.filename);
QFile outFile(outFilename);
// 一幀圖片的大小
int imgSize = av_image_get_buffer_size(in.pixFmt, in.width, in.height, 1);
// 返回結(jié)果
int ret = 0;
// 編碼器
AVCodec *codec = nullptr;
// 編碼上下文
AVCodecContext *ctx = nullptr;
// 存放編碼前的數(shù)據(jù)(yuv)
AVFrame *frame = nullptr;
// 存放編碼后的數(shù)據(jù)(h264)
AVPacket *pkt = nullptr;
// 獲取編碼器
codec = avcodec_find_encoder_by_name("libx264");
if (!codec) {
qDebug() << "encoder not found";
return;
}
// 檢查輸入數(shù)據(jù)的采樣格式
if (!check_pix_fmt(codec, in.pixFmt)) {
qDebug() << "unsupported pixel format"
<< av_get_pix_fmt_name(in.pixFmt);
return;
}
// 創(chuàng)建編碼上下文
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
qDebug() << "avcodec_alloc_context3 error";
return;
}
// 設(shè)置yuv參數(shù)
ctx->width = in.width;
ctx->height = in.height;
ctx->pix_fmt = in.pixFmt;
// 設(shè)置幀率(1秒鐘顯示的幀數(shù)是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;
}
// 創(chuàng)建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創(chuàng)建緩沖區(qū)
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;
}
// 創(chuàng)建輸入緩沖區(qū)(方法2)
// buf = (uint8_t *) av_malloc(imgSize);
// ret = av_image_fill_arrays(frame->data, frame->linesize,
// buf,
// in.pixFmt, in.width, in.height, 1);
// if (ret < 0) {
// ERROR_BUF(ret);
// qDebug() << "av_image_fill_arrays error" << errbuf;
// goto end;
// }
// qDebug() << buf << frame->data[0];
// 創(chuàng)建輸入緩沖區(qū)(方法3)
// ret = av_frame_get_buffer(frame, 0);
// if (ret < 0) {
// ERROR_BUF(ret);
// qDebug() << "av_frame_get_buffer error" << errbuf;
// goto end;
// }
// 創(chuàng)建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;
}
// 讀取數(shù)據(jù)到frame中
while ((ret = inFile.read((char *) frame->data[0],
imgSize)) > 0) {
// 進行編碼
if (encode(ctx, frame, pkt, outFile) < 0) {
goto end;
}
// 設(shè)置幀的序號
frame->pts++;
}
// 刷新緩沖區(qū)
encode(ctx, nullptr, pkt, outFile);
end:
// 關(guān)閉文件
inFile.close();
outFile.close();
// 釋放資源
if (frame) {
av_freep(&frame->data[0]);
av_frame_free(&frame);
}
av_packet_free(&pkt);
avcodec_free_context(&ctx);
qDebug() << "線程正常結(jié)束";
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。