[Libav-user] Memory doesn't decrease after freeing a derived device context of type AV_HWDEVICE_TYPE_QSV

Mark Samuelson Mark.Samuelson at sonicfoundry.com
Tue Jun 13 16:54:48 EEST 2023


I think I have found the same memory leak and it's even more basic.  The following code will leak memory when using AV_HWDEVICE_TYPE_QSV , but not when using AV_HWDEVICE_TYPE_CUDA

   while (true)
   {
      AVBufferRef* hw_device_ctx;
      av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV, NULL, NULL, 0);
      av_buffer_unref(&hw_device_ctx);
   }

Mark Samuelson

From: Libav-user <libav-user-bounces at ffmpeg.org> On Behalf Of karim abd
Sent: Saturday, June 10, 2023 7:30 AM
To: libav-user at ffmpeg.org
Subject: [Libav-user] Memory doesn't decrease after freeing a derived device context of type AV_HWDEVICE_TYPE_QSV

You don't often get email from karimabdalsalam at hotmail.com<mailto:karimabdalsalam at hotmail.com>. Learn why this is important<https://aka.ms/LearnAboutSenderIdentification>

Hi
I was able to encode ID3D11Texture2D successfully using a derived device context of type  V_HWDEVICE_TYPE_QSV and its hardware frames context but when I try to free them
the memory does not decrease. I am not very sure if I missed something to free or it is a bug. Here is a small code to reproduce the problem.
I am using windows 10, visual studio 2022 and ffmpeg 6.0.
thx in advance



#include <iostream>
#include <d3d11.h>
extern "C"
{
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext_d3d11va.h>
#include <libavutil/hwcontext_qsv.h>
#include <libavutil/hwcontext.h>
}


int main()
{
                while (true)
                {
                                AVFrame* frame = av_frame_alloc();
                                const AVCodec* codec = avcodec_find_encoder_by_name("h264_qsv");
                                AVCodecContext* encoderContext = avcodec_alloc_context3(codec);


                                encoderContext->width = 1920;
                                encoderContext->height = 1080;
                                encoderContext->time_base.den = 120;
                                encoderContext->time_base.num = 1;
                                encoderContext->framerate.den = 1;
                                encoderContext->framerate.num = 120;
                                encoderContext->max_b_frames = 0;
                                encoderContext->thread_count = 1;
                                encoderContext->gop_size = 10;
                                encoderContext->sw_pix_fmt = AV_PIX_FMT_NV12;
                                encoderContext->pix_fmt = AV_PIX_FMT_QSV;


                                av_opt_set(encoderContext->priv_data, "profile", "baseline", 0);
                                av_opt_set(encoderContext->priv_data, "preset", "veryfast", 0);
                                av_opt_set(encoderContext->priv_data, "async_depth", "1", 0);
                                av_opt_set(encoderContext, "b", "15M", 0);
                                av_opt_set(encoderContext, "maxrate", "15M", 0);
                                av_opt_set(encoderContext, "bufsize", "15M", 0);



                                AVBufferRef* hardwareDeviceContext;
                                int err = av_hwdevice_ctx_create(&(hardwareDeviceContext), AV_HWDEVICE_TYPE_D3D11VA, "hw", nullptr,
                                                0);

                                AVHWDeviceContext* deviceContext = (AVHWDeviceContext*)hardwareDeviceContext->data;
                                AVD3D11VADeviceContext* ctx = (AVD3D11VADeviceContext*)deviceContext->hwctx;
                                //ctx->device = getD3DDevice();

                                AVBufferRef* hardwareFrameContext;
                                hardwareFrameContext = av_hwframe_ctx_alloc(hardwareDeviceContext);

                                AVHWFramesContext* frames_ctx = (AVHWFramesContext*)(hardwareFrameContext->data);
                                frames_ctx->format = AV_PIX_FMT_D3D11;
                                frames_ctx->sw_format = AV_PIX_FMT_NV12;
                                frames_ctx->width = encoderContext->width;
                                frames_ctx->height = encoderContext->height;
                                frames_ctx->initial_pool_size = 1;
                                err = av_hwframe_ctx_init(hardwareFrameContext);

                                err = av_hwdevice_ctx_create_derived(&encoderContext->hw_device_ctx, AV_HWDEVICE_TYPE_QSV, hardwareDeviceContext, 0);

                                err = av_hwframe_ctx_create_derived(&encoderContext->hw_frames_ctx, AV_PIX_FMT_QSV, encoderContext->hw_device_ctx, hardwareFrameContext, 0);


                                err = avcodec_open2(encoderContext, codec, 0);


                                av_hwframe_get_buffer(encoderContext->hw_frames_ctx, frame, 0);

                                av_buffer_unref(&hardwareDeviceContext);
                                av_buffer_unref(&hardwareFrameContext);

                                hardwareDeviceContext = nullptr;
                                hardwareFrameContext = nullptr;

                                av_buffer_unref(&encoderContext->hw_device_ctx);
                                encoderContext->hw_device_ctx = nullptr;

                                av_buffer_unref(&encoderContext->hw_frames_ctx);
                                encoderContext->hw_frames_ctx = nullptr;

                                av_frame_unref(frame);
                                av_frame_free(&frame);
                                frame = nullptr;

                                avcodec_close(encoderContext);
                                avcodec_free_context(&encoderContext);

                                encoderContext = nullptr;
                }




                return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20230613/3be9b980/attachment.htm>


More information about the Libav-user mailing list