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

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.kler.cn/news/315716.html

相关文章:

  • Python在数据科学与机器学习中的应用
  • C语言 | Leetcode C语言题解之第429题N叉树的层序遍历
  • Nginx简介;Nginx安装
  • Chainlit集成LlamaIndex实现知识库高级检索(自动合并检索)
  • VUE3学习---【一】【从零开始的VUE学习】
  • Java面试篇基础部分-Synchronized关键字详解
  • python爬虫中json和xml字符串的xPath和jsonpath过滤语法区别对比
  • 零工市场小程序:推动零工市场建设
  • 【Kubernetes】常见面试题汇总(三十)
  • 【二等奖论文】2024年华为杯研赛D题成品论文(后续会更新)
  • rust GTK4 窗口创建与 wayland Subsurface (vulkan 渲染窗口初始化 (Linux) 上篇)
  • Docker实践——天池篇
  • 极度精简 Winows11 系统镜像!Tiny11 2311下载 - 支持苹果 M 芯片 Mac 安装 (ARM 精简版)!
  • get_property --Cmakelist之中
  • 关闭小广告【JavaScript】
  • 【线程】线程的同步
  • PHP转Go很丝滑开发框架设计思路-把php优秀设计借鉴到Go框架设计里面-保留php开发习惯又能提供高软件性能
  • OpenCV特征检测(8)检测图像中圆形的函数HoughCircles()的使用
  • 利用JAVA写一张纸折叠珠穆拉玛峰高度
  • 算法打卡:第十一章 图论part04
  • 情指行一体化平台建设方案和必要性-———未来之窗行业应用跨平台架构
  • 0基础学习PyTorch——最小Demo
  • AI教你学Python 第17天 :小项目联系人管理系统
  • 小程序-模板与配置
  • 乱弹篇(53)丹桂未飘香
  • Excel常用函数大全
  • DAPP智能合约系统开发
  • 【计算机网络 - 基础问题】每日 3 题(十八)
  • SecureCRT下载
  • 如何在 MySQL Workbench 中修改表数据并保存??