h264格式轉yuv詳細步驟
- 初始化FFmpeg庫:通過
av_register_all()
來初始化必要的組件。 - 打開輸入文件并查找解碼器:使用
avformat_open_input
和avcodec_find_decoder
打開H.264文件,并查找視頻流。 - 分配并配置解碼上下文:使用
avcodec_alloc_context3
分配解碼上下文,并設置必要的參數。 - 使用
av_parser_init
初始化解析器上下文 - 打開解碼器:通過
avcodec_open2
打開解碼器。 - 讀取和解碼幀:使用
av_parser_parse2
數據經過解析器解析,并使用avcodec_send_packet
和avcodec_receive_frame
解碼幀。 - 保存YUV幀到文件:將解碼后的YUV幀寫入輸出文件。
- 清理資源:釋放所有分配的資源,確保沒有內存泄漏。
詳細代碼如下:
#include "ffmpeg.h"
#include <QFile>
#include <QDebug>
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 4096ffmpegs::ffmpegs()
{}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;}// 將解碼后的數據寫入文件//寫入y平面outFile.write((char *) frame->data[0], frame->linesize[0] * ctx->height);//寫入u平面outFile.write((char *) frame->data[1], frame->linesize[1] * ctx->height >> 1);//寫入v平面outFile.write((char *) frame->data[2], frame->linesize[2] * ctx->height >> 2);}
}void ffmpegs::h264Decode(const char *inFileName, videodecodeSpec &out)
{//返回結果int ret = 0;//用來存放讀取的文件數據(h264)//加上AV_INPUT_BUFFER_PADDING_SIZE是為了防止某些優化過的reader一次性讀取過多導致越界char inDataArray[IN_DATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];char *inData = inDataArray;//每次從輸入文件中讀取的長度(h264)//輸入緩沖區中,剩下等待進行解碼的有效數據長度int inLen = 0;int inEnd = 0;//文件QFile inFile(inFileName);QFile outFile(out.filename);//解碼器AVCodec *codec = nullptr;//上下文AVCodecContext *ctx = nullptr;//編解碼器上下文AVCodecParserContext *parserCtx = nullptr;//存放編碼前的數據(h264)AVPacket *pkt = nullptr;//存放編碼后的數據(yuv)AVFrame *frame = nullptr;//獲取編碼器//codec = avcodec_find_decoder_by_name("h264");codec = avcodec_find_decoder(AV_CODEC_ID_H264);if(!codec){qDebug() << "decodec not found";return;}//初始化解析器上下文// parserCtx = av_parser_init(codec->id);//根據編碼器ID進行初始化parserCtx = av_parser_init(AV_CODEC_ID_H264);//根據編碼器ID進行初始化if(!parserCtx){qDebug() << "av_parser_init error";return;}//創建上下文ctx = avcodec_alloc_context3(codec);if(!ctx){qDebug() << "avcodec_alloc_context3 error";goto end;}//創建AVPacketpkt = av_packet_alloc();if(!pkt){qDebug() << "av_packet_alloc error";goto end;}//創建AVFrameframe = 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指向數組的首元素inData = inDataArray;//只要輸入緩沖區中還有等待進行解碼的數據while(inLen > 0 || inEnd){//經過解析器解析//內部調用的核心是:ff_aac_ac3_parse//到了文件尾部(雖然沒有讀取任何數據,但也要調用av_parser_parse2)//經過解析器解析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){if(decode(ctx,pkt,frame,outFile) < 0){goto end;}}//如果到了文件末尾if(inEnd) break;}}while(!inEnd);//刷新緩沖區// pkt->data = nullptr;// pkt->size = 0;// decode(ctx,pkt,frame,outFile);//刷新緩沖區decode(ctx,nullptr,frame,outFile);// 設置輸出參數out.width = ctx->width;out.height = ctx->height;out.pixFmt = ctx->pix_fmt;out.fps = ctx->time_base.num;end:inFile.close();outFile.close();av_packet_free(&pkt);av_frame_free(&frame);av_parser_close(parserCtx);avcodec_free_context(&ctx);
}