本文的主要内容:对H.264数据进行解码(解压缩)。
如果是命令行的操作,非常简单。
| ffmpeg -c:v h264 -i in.h264 out.yuv |
| |
接下来主要讲解如何通过代码的方式解码H.264数据,用到了avcodec、avutil两个库,整体过程跟《AAC解码实战》类似。
类的声明
| 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); |
| }; |
类的使用
| VideoDecodeSpec out; |
| out.filename = "F:/res/out.yuv"; |
| |
| FFmpegs::h264Decode("F:/res/in.h264", out); |
| |
| qDebug() << out.width << out.height |
| << out.fps << av_get_pix_fmt_name(out.pixFmt); |
宏定义
| 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 |
变量定义
| |
| 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); |
decode函数的实现如下所示:
| 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; |
| } |
| |
| |
| |
| 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); |
| } |
| } |
回收资源
| end: |
| inFile.close(); |
| outFile.close(); |
| av_packet_free(&pkt); |
| av_frame_free(&frame); |
| av_parser_close(parserCtx); |
| avcodec_free_context(&ctx); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2013-05-25 【零基础学习iOS开发】【02-C语言】06-变量与内存