Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Zhao Zhili via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Zhao Zhili <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avfilter/vf_libopencv: fix memleak (PR #21267)
Date: Mon, 22 Dec 2025 16:53:36 -0000
Message-ID: <176642241710.60.8050881729441821444@2cb04c0e5124> (raw)

PR #21267 opened by Zhao Zhili (quink)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21267
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21267.patch


>From 52833f19a2582ca05852f93e26d9c88d9a47fdfc Mon Sep 17 00:00:00 2001
From: Zhao Zhili <zhilizhao@tencent.com>
Date: Tue, 23 Dec 2025 00:32:44 +0800
Subject: [PATCH 1/2] avfilter/vf_libopencv: fix memleak

---
 libavfilter/vf_libopencv.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
index 8a80a7fdab..bc8fd0bad5 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -36,9 +36,9 @@
 #include "formats.h"
 #include "video.h"
 
-static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt)
+static void fill_iplimage_from_frame(IplImage **_img, const AVFrame *frame, enum AVPixelFormat pixfmt)
 {
-    IplImage *tmpimg;
+    IplImage *img;
     int depth, channels_nb;
 
     if      (pixfmt == AV_PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U;  channels_nb = 1; }
@@ -46,8 +46,7 @@ static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum A
     else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
     else return;
 
-    tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
-    *img = *tmpimg;
+    *_img = img = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
     img->imageData = img->imageDataOrigin = frame->data[0];
     img->dataOrder = IPL_DATA_ORDER_PIXEL;
     img->origin    = IPL_ORIGIN_TL;
@@ -366,7 +365,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     OCVContext *s = ctx->priv;
     AVFilterLink *outlink= inlink->dst->outputs[0];
     AVFrame *out;
-    IplImage inimg, outimg;
+    IplImage *inimg, *outimg;
 
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
@@ -377,10 +376,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
     fill_iplimage_from_frame(&inimg , in , inlink->format);
     fill_iplimage_from_frame(&outimg, out, inlink->format);
-    s->end_frame_filter(ctx, &inimg, &outimg);
-    fill_frame_from_iplimage(out, &outimg, inlink->format);
+    s->end_frame_filter(ctx, inimg, outimg);
+    fill_frame_from_iplimage(out, outimg, inlink->format);
 
     av_frame_free(&in);
+    cvReleaseImageHeader(&inimg);
+    cvReleaseImageHeader(&outimg);
 
     return ff_filter_frame(outlink, out);
 }
-- 
2.49.1


>From b73758bfd7ae344676effc353b3b5b30228dbaa1 Mon Sep 17 00:00:00 2001
From: Zhao Zhili <zhilizhao@tencent.com>
Date: Tue, 23 Dec 2025 00:36:29 +0800
Subject: [PATCH 2/2] avfilter/vf_libopencv: use av_unreachable on unreachable
 path

---
 libavfilter/vf_libopencv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
index bc8fd0bad5..06a2954bc6 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -26,6 +26,7 @@
 #include "config.h"
 #include <opencv2/core/core_c.h>
 #include <opencv2/imgproc/imgproc_c.h>
+#include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/common.h"
 #include "libavutil/file.h"
@@ -44,7 +45,7 @@ static void fill_iplimage_from_frame(IplImage **_img, const AVFrame *frame, enum
     if      (pixfmt == AV_PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U;  channels_nb = 1; }
     else if (pixfmt == AV_PIX_FMT_BGRA)  { depth = IPL_DEPTH_8U;  channels_nb = 4; }
     else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
-    else return;
+    else av_unreachable("unsupported pix fmt");
 
     *_img = img = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
     img->imageData = img->imageDataOrigin = frame->data[0];
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2025-12-22 16:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=176642241710.60.8050881729441821444@2cb04c0e5124 \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git