1. h264的壓縮編碼文件,播放器能直接播放嗎?
- 大部分狀況下能夠,由於播放器基本都集成了 h264 解碼器。(相似 AAC 的原理)
2. 繪圖解釋 h264 解碼的過程?(必須能默出來)
3. h264 解碼的過程,若是邊框尺寸不是 16 的倍數,可能會出問題嗎?
4. h264 解碼主要源代碼以下:
#ifndef FFMPEGS_H
#define FFMPEGS_H
extern "C" {
#include <libavutil/avutil.h>
}
typedef struct {
const char *filename;
int width;
int height;
AVPixelFormat pixFmt;
int fps;
} VideoDecodeSpec;
class FFmpegs {
public:
FFmpegs();
static void h264Decode(const char *inFilename, VideoDecodeSpec &out);
};
#endif
複製代碼
#include "ffmpegs.h"
#include <QDebug>
#include <QFile>
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));
#define IN_DATA_SIZE 4096
FFmpegs::FFmpegs() {
}
static int frameIdx = 0;
static int decode(AVCodecContext *ctx, AVPacket *pkt, AVFrame *frame, QFile &outFile) {
int ret = avcodec_send_packet(ctx, pkt);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_send_packet error" << errbuf;
return ret;
}
while (true) {
ret = avcodec_receive_frame(ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
} else if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_receive_frame error" << errbuf;
return ret;
}
qDebug() << "解碼出第" << ++frameIdx << "幀";
outFile.write((char *) frame->data[0],
frame->linesize[0] * ctx->height);
outFile.write((char *) frame->data[1],
frame->linesize[1] * (ctx->height >> 1));
outFile.write((char *) frame->data[2],
frame->linesize[2] * (ctx->height >> 1));
qDebug() << "寫入Y平面" << " linesize " << frame->linesize[0] << " 總大小 " << (frame->linesize[0] * ctx->height );
qDebug() << "寫入U平面" << " linesize " << frame->linesize[1] << " 總大小 " << (frame->linesize[1] * ctx->height >> 1);
qDebug() << "寫入V平面" << " linesize " << frame->linesize[2] << " 總大小 " << (frame->linesize[2] * ctx->height >> 1);
}
}
void FFmpegs::h264Decode(const char *inFilename, VideoDecodeSpec &out) {
int ret = 0;
char inDataArray[IN_DATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
char *inData = inDataArray;
int inLen;
int inEnd = 0;
QFile inFile(inFilename);
QFile outFile(out.filename);
AVCodec *codec = nullptr;
AVCodecContext *ctx = nullptr;
AVCodecParserContext *parserCtx = nullptr;
AVPacket *pkt = nullptr;
AVFrame *frame = nullptr;
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
qDebug() << "decoder not found";
return;
}
parserCtx = av_parser_init(codec->id);
if (!parserCtx) {
qDebug() << "av_parser_init error";
return;
}
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
qDebug() << "avcodec_alloc_context3 error";
goto end;
}
pkt = av_packet_alloc();
if (!pkt) {
qDebug() << "av_packet_alloc error";
goto end;
}
frame = av_frame_alloc();
if (!frame) {
qDebug() << "av_frame_alloc error";
goto end;
}
ret = avcodec_open2(ctx, codec, nullptr);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_open2 error" << errbuf;
goto end;
}
if (!inFile.open(QFile::ReadOnly)) {
qDebug() << "file open error:" << inFilename;
goto end;
}
if (!outFile.open(QFile::WriteOnly)) {
qDebug() << "file open error:" << out.filename;
goto end;
}
do {
inLen = inFile.read(inDataArray, IN_DATA_SIZE);
inEnd = !inLen;
inData = inDataArray;
while (inLen > 0 || inEnd) {
ret = av_parser_parse2(parserCtx, ctx,
&pkt->data, &pkt->size,
(uint8_t *) inData, inLen,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "av_parser_parse2 error" << errbuf;
goto end;
}
inData += ret;
inLen -= ret;
qDebug() << inEnd << pkt->size << ret;
if (pkt->size > 0 && decode(ctx, pkt, frame, outFile) < 0) {
goto end;
}
if (inEnd) break;
}
} while (!inEnd);
decode(ctx, nullptr, frame, outFile);
out.width = ctx->width;
out.height = ctx->height;
out.pixFmt = ctx->pix_fmt;
out.fps = ctx->framerate.num;
end:
inFile.close();
outFile.close();
av_packet_free(&pkt);
av_frame_free(&frame);
av_parser_close(parserCtx);
avcodec_free_context(&ctx);
}
複製代碼