当前位置: 首页 > news >正文

【FFmpeg】调用ffmpeg进行H264软解

调用FFmpeg库实现264软件解码

  • 1. FFmpeg的编译
  • 2. 调用FFmpeg实现H264软解
    • 2.1 基本框架
    • 2.2 代码实现
    • 2.3 测试结果
  • 3. 分析工具
    • 3.1 码流分析
    • 3.2 YUV分析

====== 示例工程 ======
【FFmpeg】调用FFmpeg库实现264软编

1. FFmpeg的编译

FFmpeg在Windows下的编译参考:http://t.csdnimg.cn/BtAW5
本文使用的FFmpeg版本为7.0

2. 调用FFmpeg实现H264软解

参考FFmpeg当中的/doc/examples当中的video_decode.c文件,进行调用过程的理解和实现。

2.1 基本框架

根据/doc/examples当中的实现进行修改,包括:
1.增加pragma去除fopen的warning
2.增加extern调用ffmpeg库文件
3.增加error code的打印,可以查阅其对应的错误信息
4.修改一些变量的定义
5.文件存储的方式直接修改为yuv,不使用pgm

在解码过程当中,使用了如下的函数

函数名作用
avcodec_find_decoder根据ID查找编码器,输入为AVCodecID,返回为AVCodec,记录了解码器信息
avcodec_alloc_context3创建codec的上下文信息,输入为AVCodec,返回为AVCodecContext,其记录了编码过程上下文的流信息
av_packet_alloc创建数据包packet,并且初始化为默认值,返回为AVPacket;该结构存储压缩之后的数据。通常由解码器导出,然后作为输入传递给解码器,或者作为编码器的输出接收,然后传递给解码器
avcodec_open2打开编码器,输入为AVCodec,返回为ret。该函数初始化了codec线程和配置
av_parser_init创建解析器的上下文,用于解析码流,输入为AVCodecID,返回为AVCodecParserContext
av_frame_alloc创建frame,返回为AVFrame。这个结构描述待编码的原始的音频或视频数据
av_parser_parse2解析码流文件,从输入的一串码流当中解析出一帧数据,存储到AVPacket当中,返回值ret即为一帧数据结束的索引号
avcodec_send_packet将packet送入到解码器当中进行解码,输入为AVCodecContext和AVPacket,输出为ret
avcodec_receive_frame接收解码器已解码信息,输入为AVCodecContext和AVFrame,输出为ret
av_parser_close释放解析器
avcodec_free_context释放解码器的上下文信息
av_frame_free释放frame的缓冲区以及结构体本身
av_packet_freeav_packet_free以及结构体本身

从上面这一系列的函数调用来看,大致操作流程和数据走向大约是
1.解码器的创建和初始化(avcodec_find_decoder)
2.解码器上下文的创建和初始化(avcodec_alloc_context3)
3.创建解码器输入信息,使用AVPacket进行存储(av_packet_alloc)
4.创建解析器,用于解析解码器输入信息(av_parser_init)
5.创建解码器输出信息,使用AVFrame进行存储(av_frame_alloc)
6.打开解码器(avcodec_open2)
7.进入do循环,使用fread读取数据,存储变量名为inbuf
8.对输入的数据进行解析,因为解码器是一帧一帧解码的,所以需要将数据存储到AVPacket当中。同时必须知道码流中每一帧结束的索引,用以确定下一帧的起始位置(av_parser_parse2)
9.将当前帧信息送入到解码器当中去解码,输入载体是AVPacket(avcodec_send_packet)
10.将已经解码的数据取出,输出载体是AVFrame(avcodec_receive_frame)
11.将已解码的数据存储为yuv格式【可选操作】
12.解析下一帧数据
13.当所有帧解码完毕之后,释放解析器、上下文信息、AVFrame以及AVPacket等结构体

这里使用了avcodec_send_packet和avcodec_receive_frame两个函数,这里的send和receive可以假想为使用线上网络传输软件进行数据流的传输,send将码流文件送出,receive将已经解码的yuv文件接收。

2.2 代码实现

#pragma warning(disable : 4996)#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "video_decode.h"#ifdef _WIN32
//Windows
extern "C" // 在C++文件中调用C文件需要使用,ffmpeg是使用C实现的
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
#ifdef __cplusplus
};
#endif
#endif#define IMG_WIDTH 1920
#define IMG_HEIGHT 1200
#define INBUF_SIZE IMG_WIDTH * IMG_HEIGHTstatic void pgm_save(unsigned char* buf, int wrap, int xsize, int ysize, const char* filename)
{FILE* f;f = fopen(filename, "wb");fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);for (int i = 0; i < ysize; i++) {fwrite(buf + i * wrap, 1, xsize, f);}fclose(f);
}void decode_internal(AVCodecContext* av_codec_ctx, AVFrame* av_frm, AVPacket* av_pkt, FILE* fp_out)
{static char buf[1024];int ret;// 将当前帧送入到解码器当中去解码ret = avcodec_send_packet(av_codec_ctx, av_pkt);if (ret < 0) {fprintf(stderr, "Error sending a packet for decoding, error code:%d\n", ret);exit(1);}while (ret >= 0) {// 获取已经解码的数据ret = avcodec_receive_frame(av_codec_ctx, av_frm);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {return;}else if (ret < 0) {fprintf(stderr, "Error during decoding, error code:%d\n", ret);exit(1);}fprintf(stderr, "saving frame %3" PRId64"\n", av_codec_ctx->frame_num);fflush(stdout);//snprintf(buf, sizeof(buf), "%s-%s" PRId64, out_filename, av_codec_ctx->frame_num);//pgm_save(av_frm->data[0], av_frm->linesize[0], av_frm->width, av_frm->height, out_filename);// 将已经解码的数据存储到文件中int size = av_frm->width * av_frm->height;fwrite(av_frm->data[0], 1, size, fp_out);//Yfwrite(av_frm->data[1], 1, size / 4, fp_out);//Ufwrite(av_frm->data[2], 1, size / 4, fp_out);//V}
}int decode(const char* in_file, const char* out_file)
{const AVCodec* av_codec;AVCodecParserContext* av_parser;AVCodecContext* av_codec_ctx = NULL;AVFrame* av_frame;uint8_t* data;size_t data_size;int ret;int eof;AVPacket* av_pkt;// malloc input bufferuint8_t* inbuf = (uint8_t*)malloc((INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) * sizeof(uint8_t));if (!inbuf) {fprintf(stderr, "Error! alloc inbuf failed");exit(1);}memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);// create h264 decoderav_codec = avcodec_find_decoder(AV_CODEC_ID_H264);if (!av_codec) {fprintf(stderr, "Codec not found\n");exit(1);}// create ctxav_codec_ctx = avcodec_alloc_context3(av_codec);if (!av_codec_ctx) {fprintf(stderr, "Could not allocate video codec context\n");exit(1);}// creat pktav_pkt = av_packet_alloc();if (!av_pkt) {fprintf(stderr, "Error! alloc pkt failed");exit(1);}// parse codec infoav_parser = av_parser_init(av_codec->id);if (!av_parser) {fprintf(stderr, "parser not found\n");exit(1);}// create frameav_frame = av_frame_alloc();if (!av_frame) {fprintf(stderr, "Could not allocate video frame\n");exit(1);}// open fileFILE* fp_in = fopen(in_file, "rb");if (!fp_in) {fprintf(stderr, "Could not open %s\n", in_file);exit(1);}FILE* fp_out = fopen(out_file, "wb");if (!fp_out) {fprintf(stderr, "Could not open %s\n", out_file);exit(1);}// open dec codecif (avcodec_open2(av_codec_ctx, av_codec, NULL) < 0) {fprintf(stderr, "Could not open codec\n");exit(1);}do {/* read raw data from the input file */data_size = fread(inbuf, 1, INBUF_SIZE, fp_in);if (ferror(fp_in)) {break;}eof = !data_size;/* use the parser to split the data into frames */data = inbuf;while (data_size > 0 || eof) {// 从输入码流当中解析出一帧数据,送入到解码器当中解码// 如果是第1帧(IDR)的话,ret表示的索引还包括头信息(SPS+PPS+SEI)ret = av_parser_parse2(av_parser, av_codec_ctx, &av_pkt->data, &av_pkt->size,data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);if (ret < 0) {fprintf(stderr, "Error! can not parse iput data");exit(1);}// 更新后面帧的起始地址data += ret;data_size -= ret;if (av_pkt->size) {// decodedecode_internal(av_codec_ctx, av_frame, av_pkt, fp_out);}else if (eof) {break;}}} while (!eof);/* flush the decoder */decode_internal(av_codec_ctx, av_frame, NULL, fp_out);fclose(fp_in);free(inbuf);av_parser_close(av_parser);avcodec_free_context(&av_codec_ctx);av_frame_free(&av_frame);av_packet_free(&av_pkt);return 0;
}

2.3 测试结果

saving frame   1
saving frame   2
saving frame   3
saving frame   4
saving frame   5
saving frame   6
saving frame   7
saving frame   8
saving frame   9
saving frame  10

3. 分析工具

3.1 码流分析

264/265码流分析工具(有release文件):https://gitcode.com/latelee/H264BSAnalyzer

3.2 YUV分析

YUV分析工具:https://gitcode.com/IENT/YUView/overview

Github: https://github.com/DoFulangChen/video_implementation.git

http://www.lryc.cn/news/347836.html

相关文章:

  • 网络安全防护:抵御DDoS和CC攻击
  • 初次查询大数据信用报告,需要注意哪些问题?
  • 最短路径[floyd算法]-----视频讲解+代码实现
  • 图像/视频恢复和增强CodeFormer
  • WPF中ObservableCollection
  • 如何用鼠标点击在picturebox的图像上做标记
  • k8s介绍
  • K-means聚类模型:深入解析与应用指南
  • CTF-密码学基础
  • 代码随想录算法训练营day22 | 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
  • 企业信息防泄漏软件分析:盘点常用企业信息防泄漏软件
  • Rancher-Kubewarden-保姆级教学-含Demo测试
  • Lumerical Script ------ array 数组类型 和 matrix 矩阵类型
  • Springboot自动装配源码分析
  • Visual Transformer (ViT)模型详解 动图讲解
  • C++:完美转发(一)(std::forward)
  • 西部首个全域直播基地,打造西部直播基地领军形象
  • 钟表——蓝桥杯十三届2022国赛大学B组真题
  • CSS 之 圆形波浪进度条效果
  • 按下鼠标进行拖拽,让元素跟随鼠标进行移动,鼠标抬起,元素停止移;js鼠标拖拽 (鼠标按下事件:onmousedown、鼠标移动事件:onmousemove、鼠标抬起事件:onmouseup)
  • 第十二章 项目采购管理
  • PSFR-GAN复现
  • 函数和数组
  • docker安装时报错:Error: Nothing to do
  • 白盒测试:覆盖测试及测试用例设计
  • Java高级开发2024高频面试提问题目
  • Kamailio openssl 3.0.x 需要注意的事项
  • SpringAMQP Work Queue 工作队列
  • 一分钟带你了解什么是等保测评
  • 宝塔面板怎么解决nginx跨域问题