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

FFmpeg中结构释放小函数

用于FFmpeg一些结构内存释放问题

#pragma once
#include <iostream>extern "C"
{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libavutil/avutil.h"
#include "libavutil/frame.h"
#include "libavcodec/packet.h"
}// 泛化变参模板 ///
template <typename T, typename... Args>
void CleanUp(T* para, Args&&... args)
{CleanUp(para);	CleanUp(std::forward<Args>(args)...);
}特化模板 /
template<>
inline void CleanUp<AVFormatContext*>(AVFormatContext** pp_fmt_ctx) {	if (pp_fmt_ctx && *pp_fmt_ctx) {avformat_close_input(pp_fmt_ctx);pp_fmt_ctx = nullptr;}
}template<>
inline void CleanUp<AVCodecContext*>(AVCodecContext** pp_codec_cxt) {if (pp_codec_cxt && *pp_codec_cxt) {avcodec_free_context(pp_codec_cxt);pp_codec_cxt = nullptr;}
}template<>
inline void CleanUp<AVPacket*>(AVPacket** pp_pkt) {if (pp_pkt && *pp_pkt) {av_packet_free(pp_pkt);pp_pkt = nullptr;}
}template<>
inline void CleanUp<AVFrame*>(AVFrame** pp_frame) {if (pp_frame && *pp_frame) {av_frame_free(pp_frame);pp_frame = nullptr;}
}template<>
inline void CleanUp<uint8_t*>(uint8_t** u8_buffer) {if (u8_buffer && *u8_buffer) {av_freep(*u8_buffer);u8_buffer = nullptr;}
}template<>
inline void CleanUp<FILE*>(FILE** pp_fp) {if (pp_fp && *pp_fp) {fclose(*pp_fp);pp_fp = nullptr;}
}

测试文件

// 资源释放测试
#include "CDestroyRes.h"
#include "vld.h"
#include <iostream>extern "C"
{
#include <libavutil/avutil.h>
}
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "vld.lib")int main()
{int n = 100;while (--n){AVFormatContext* fmt_ctx = avformat_alloc_context();const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);AVCodecContext* encoder = avcodec_alloc_context3(codec);AVPacket* pkt = av_packet_alloc();AVFrame* frame = av_frame_alloc();frame->width = 1280;frame->height = 640;frame->format = AV_PIX_FMT_ARGB;av_frame_get_buffer(frame, 0);CleanUp(&fmt_ctx, &encoder, &pkt, &frame);}getchar();return 0;
}
http://www.lryc.cn/news/446413.html

相关文章:

  • C语言中的一些小知识(三)
  • 编译win2k3中tools目录下i386mk.inc文件的作用
  • IPSec隧道协议学习(一)
  • 计网作业3
  • 什么是注入攻击???
  • 牛客小白月赛101(A~E)
  • MFC设置特定控件字体大小和背景颜色
  • 专题八_链表_算法专题详细总结
  • Vue3使用vue-quill富文本编辑器实现图片大小调整
  • 感知笔记1:ROS 视觉- 跟随红球
  • JAVA多线程机制
  • Element-plus安装及其基础组件使用
  • [产品管理-38]:创意、市场机会、商业可行性的区别
  • 开源标注工具
  • 数据结构讲解二叉树 【一】
  • MATLAB基础应用精讲-【数模应用】OR值
  • [vulnhub] w1r3s.v1.0
  • c#中的功能优势
  • Windows系统设置定时任务,周期性执行.bat文件
  • xQTLs 共定位分析(XQTLbiolinks包)
  • 网络工程(学习记录)
  • 全志A133 android10 适配EC20 4G模块
  • 数据分析:Python语言网络图绘制
  • 使用ChatGPT引导批判性思维,提升论文的逻辑与说服力的全过程
  • vue限定类型上传文件 最简单实践(单个可文件、可图片)
  • 【GUI设计】基于图像分割和边缘算法的GUI系统(7),matlab实现
  • 未来之窗VOS编程工具让你的工作效率翻倍———未来之窗行业应用跨平台架构
  • 分布式数据库——HBase基本操作
  • Go语言并发编程中的超时与取消机制解析
  • Unity3D UIdocument如何改变层级详解