FFmpeg解码内存泄漏,sws_getContext函数释放失败原因分析?

2026-05-25 04:181阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计762个文字,预计阅读时间需要4分钟。

FFmpeg解码内存泄漏,sws_getContext函数释放失败原因分析?

使用FFmpeg库的项目,在调试项目时若持续解码视频,可能会出现明显内存增加问题。连续工作10分钟后,项目可能会被Linux内核kill掉。通过逐步注解代码和查阅网上资料,最终发现内存泄漏问题。

  使用FFmpeg库做的项目,调试项目的时候发现,连续解视频进行播放,会有明显内存增加问题。连续工作10个小时后就会

被linux内核kill掉。

通过逐步注掉代码和网上查阅资料。最后发现内存泄漏有一些几个地方:

一、av_read_frame的问题

从网上查阅大神们的经验,主要是av_read_frame有内存泄漏风险。

av_read_frame每次循环后必须执行av_packet_unref(pkt)进行释放。

如循环内退出需要释放,不成立while外也需要释放。

最后需要执行av_packet_free,av_free再进行释放packet的外壳。

while (av_read_frame(pa->fmt_ctx, pkt) >= 0) { int64_t dts; if(pkt->stream_index != pa->videoStream) { av_packet_unref(pkt); continue; } av_packet_unref(pkt); } av_packet_unref(pkt);

if(pa->packet != NULL) { av_packet_unref(pa->packet); } av_packet_free(&pa->packet); av_free(pa->packet);

二、AVFrame*结构体的释放

AVFrame结构体需要

FFmpeg解码内存泄漏,sws_getContext函数释放失败原因分析?

1、先执行av_frame_unref()释放内部

2、调用av_frame_free(&pa->srcFrame);释放外壳

三、注意释放顺序

  内存释放要按顺序进行释放。以防释放掉外壳后,内部就不能进行释放了。

如下面是释放上下文的顺序。

sws_freeContext(pSwsContext); av_frame_free(&pAVFrame); avcodec_close(pAVCodecContext); avformat_close_input(&pAVFormatContext);

四、sws_getContext函数无法释放问题

  经过反复查找,发现sws_getContext函数调用后存在内存泄漏问题。非常奇怪的是

程序中已调用了sws_freeContext(pa->sws_ctx)进行释放,内存依然泄漏。

后发现有类型函数sws_getCachedContext(),替换后发现内存不再泄漏!!!!

sws_getCachedContext

/** * Check if context can be reused, otherwise reallocate a new one. * * If context is NULL, just calls sws_getContext() to get a new * context. Otherwise, checks if the parameters are the ones already * saved in context. If that is the case, returns the current * context. Otherwise, frees context and gets a new context with * the new parameters. * * Be warned that srcFilter and dstFilter are not checked, they * are assumed to remain the same. */ struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param); /*参数说明*/ int srcW, /* 输入图像的宽度 */ int srcH, /* 输入图像的宽度 */ enum AVPixelFormat srcFormat, /* 输入图像的像素格式 */ int dstW, /* 输出图像的宽度 */ int dstH, /* 输出图像的高度 */ enum AVPixelFormat dstFormat, /* 输出图像的像素格式 */ int flags,/* 选择缩放算法(只有当输入输出图像大小不同时有效),一般选择SWS_FAST_BILINEAR */ SwsFilter *srcFilter, /* 输入图像的滤波器信息, 若不需要传NULL */ SwsFilter *dstFilter, /* 输出图像的滤波器信息, 若不需要传NULL */ const double *param /* 特定缩放算法需要的参数(?),默认为NULL */

参考资料:
blog.csdn.net/xiuxiuxiua/article/details/120370302

FFmpeg视频转换sws_getCachedContext|入门笔记 (rumenz.com)

本文共计762个文字,预计阅读时间需要4分钟。

FFmpeg解码内存泄漏,sws_getContext函数释放失败原因分析?

使用FFmpeg库的项目,在调试项目时若持续解码视频,可能会出现明显内存增加问题。连续工作10分钟后,项目可能会被Linux内核kill掉。通过逐步注解代码和查阅网上资料,最终发现内存泄漏问题。

  使用FFmpeg库做的项目,调试项目的时候发现,连续解视频进行播放,会有明显内存增加问题。连续工作10个小时后就会

被linux内核kill掉。

通过逐步注掉代码和网上查阅资料。最后发现内存泄漏有一些几个地方:

一、av_read_frame的问题

从网上查阅大神们的经验,主要是av_read_frame有内存泄漏风险。

av_read_frame每次循环后必须执行av_packet_unref(pkt)进行释放。

如循环内退出需要释放,不成立while外也需要释放。

最后需要执行av_packet_free,av_free再进行释放packet的外壳。

while (av_read_frame(pa->fmt_ctx, pkt) >= 0) { int64_t dts; if(pkt->stream_index != pa->videoStream) { av_packet_unref(pkt); continue; } av_packet_unref(pkt); } av_packet_unref(pkt);

if(pa->packet != NULL) { av_packet_unref(pa->packet); } av_packet_free(&pa->packet); av_free(pa->packet);

二、AVFrame*结构体的释放

AVFrame结构体需要

FFmpeg解码内存泄漏,sws_getContext函数释放失败原因分析?

1、先执行av_frame_unref()释放内部

2、调用av_frame_free(&pa->srcFrame);释放外壳

三、注意释放顺序

  内存释放要按顺序进行释放。以防释放掉外壳后,内部就不能进行释放了。

如下面是释放上下文的顺序。

sws_freeContext(pSwsContext); av_frame_free(&pAVFrame); avcodec_close(pAVCodecContext); avformat_close_input(&pAVFormatContext);

四、sws_getContext函数无法释放问题

  经过反复查找,发现sws_getContext函数调用后存在内存泄漏问题。非常奇怪的是

程序中已调用了sws_freeContext(pa->sws_ctx)进行释放,内存依然泄漏。

后发现有类型函数sws_getCachedContext(),替换后发现内存不再泄漏!!!!

sws_getCachedContext

/** * Check if context can be reused, otherwise reallocate a new one. * * If context is NULL, just calls sws_getContext() to get a new * context. Otherwise, checks if the parameters are the ones already * saved in context. If that is the case, returns the current * context. Otherwise, frees context and gets a new context with * the new parameters. * * Be warned that srcFilter and dstFilter are not checked, they * are assumed to remain the same. */ struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param); /*参数说明*/ int srcW, /* 输入图像的宽度 */ int srcH, /* 输入图像的宽度 */ enum AVPixelFormat srcFormat, /* 输入图像的像素格式 */ int dstW, /* 输出图像的宽度 */ int dstH, /* 输出图像的高度 */ enum AVPixelFormat dstFormat, /* 输出图像的像素格式 */ int flags,/* 选择缩放算法(只有当输入输出图像大小不同时有效),一般选择SWS_FAST_BILINEAR */ SwsFilter *srcFilter, /* 输入图像的滤波器信息, 若不需要传NULL */ SwsFilter *dstFilter, /* 输出图像的滤波器信息, 若不需要传NULL */ const double *param /* 特定缩放算法需要的参数(?),默认为NULL */

参考资料:
blog.csdn.net/xiuxiuxiua/article/details/120370302

FFmpeg视频转换sws_getCachedContext|入门笔记 (rumenz.com)