From 459a60927135d9aaa3d4a8bc2b46e36dbba6f7f2 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov <2536374+asmorkalov@users.noreply.github.com> Date: Thu, 25 Dec 2025 15:43:38 +0300 Subject: [PATCH] Merge pull request #28283 from asmorkalov:as/ffmpeg_picture_leak Fixed picture_sw object leak in ffmpeg backend with hardware codecs. #28283 Replacement for https://github.com/opencv/opencv/pull/28221 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/videoio/src/cap_ffmpeg_impl.hpp | 37 ++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/modules/videoio/src/cap_ffmpeg_impl.hpp b/modules/videoio/src/cap_ffmpeg_impl.hpp index 26509a1a81..afd8fd4098 100644 --- a/modules/videoio/src/cap_ffmpeg_impl.hpp +++ b/modules/videoio/src/cap_ffmpeg_impl.hpp @@ -1845,16 +1845,28 @@ bool CvCapture_FFMPEG::retrieveFrame(int flag, unsigned char** data, int* step, // if hardware frame, copy it to system memory if (picture && picture->hw_frames_ctx) { sw_picture = av_frame_alloc(); + if (!sw_picture) { + CV_LOG_ERROR(NULL, "av_frame_alloc failed"); + return false; + } //if (av_hwframe_map(sw_picture, picture, AV_HWFRAME_MAP_READ) < 0) { if (av_hwframe_transfer_data(sw_picture, picture, 0) < 0) { CV_LOG_ERROR(NULL, "Error copying data from GPU to CPU (av_hwframe_transfer_data)"); + av_frame_free(&sw_picture); return false; } } #endif if (!sw_picture || !sw_picture->data[0]) + { +#if USE_AV_HW_CODECS + if (sw_picture != picture) + av_frame_free(&sw_picture); +#endif + CV_LOG_ERROR(NULL, "Picture does not contain data"); return false; + } #if LIBAVUTIL_BUILD >= CALC_FFMPEG_VERSION(56, 72, 0) const char* color_space_name = av_color_space_name(sw_picture->colorspace); @@ -1905,7 +1917,14 @@ bool CvCapture_FFMPEG::retrieveFrame(int flag, unsigned char** data, int* step, img_convert_ctx = sws_alloc_context(); if (img_convert_ctx == NULL) - return false;//CV_Error(0, "Cannot initialize the conversion context!"); + { + CV_LOG_ERROR(NULL, "Cannot initialize the conversion context!"); +#if USE_AV_HW_CODECS + if (sw_picture != picture) + av_frame_free(&sw_picture); +#endif + return false; + } av_opt_set_int(img_convert_ctx, "sws_flags", SWS_BICUBIC, 0); av_opt_set_int(img_convert_ctx, "threads", requestedThreads, 0); @@ -1946,8 +1965,14 @@ bool CvCapture_FFMPEG::retrieveFrame(int flag, unsigned char** data, int* step, ); #endif - if (img_convert_ctx == NULL) - return false;//CV_Error(0, "Cannot initialize the conversion context!"); + if (img_convert_ctx == NULL) { + CV_LOG_ERROR(NULL, "Cannot initialize the conversion context!"); +#if USE_AV_HW_CODECS + if (sw_picture != picture) + av_frame_free(&sw_picture); +#endif + return false; + } #if USE_AV_FRAME_GET_BUFFER av_frame_unref(&rgb_picture); @@ -1956,7 +1981,11 @@ bool CvCapture_FFMPEG::retrieveFrame(int flag, unsigned char** data, int* step, rgb_picture.height = buffer_height; if (0 != av_frame_get_buffer(&rgb_picture, 32)) { - CV_WARN("OutOfMemory"); + CV_LOG_ERROR(NULL, "Out of memory issue on av_frame_get_buffer!"); +#if USE_AV_HW_CODECS + if (sw_picture != picture) + av_frame_free(&sw_picture); +#endif return false; } #else