[FFmpeg-devel] [PATCH] vaapi_encode: explicitly free buffers after vaEndPicture

Will Kelleher wkelleher at gogoair.com
Thu Jun 9 02:18:52 CEST 2016


On 06/08, Will Kelleher wrote:
> 
> That works for me.  I can update the patch with that warning.  Thanks!
>
Updated patch attached.

-------------- next part --------------
>From 3229edf9e52dfd61580209630f20ddef3e39e78e Mon Sep 17 00:00:00 2001
From: Will Kelleher <wkelleher at gogoair.com>
Date: Tue, 7 Jun 2016 20:49:48 -0500
Subject: [PATCH] vaapi_encode: explicitly free buffers after vaEndPicture

otherwise they leak, contrary to what the documentation seems to say.
---
 libavcodec/vaapi_encode.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 329b33c..01b8c70 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -370,6 +370,29 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
         goto fail_at_end;
     }
 
+    // We free all of the parameter buffers here.
+    // This is not consistent with what VAAPI states is required; under
+    // vaDestroyBuffer, it says: "Only call this if the buffer is not
+    // going to be passed to vaRenderBuffer" [sic; vaRenderPicture].
+    // Doing this here is a pragmatic decision to preferentially support
+    // the Intel i965 back-end driver, which does not and has never
+    // freed the buffers as required by the specification - if we don't
+    // free here, memory will leak for every frame submitted when using
+    // that driver.
+    // If other drivers are supported in future, this decision may need
+    // to be revisted - if the driver really has already freed the
+    // buffer, doing so here is disaster for thread-safety because we
+    // may free buffers which have been allocated in other threads.
+    for(i = 0; i < pic->nb_param_buffers; i++) {
+        if (pic->param_buffers[i] != VA_INVALID_ID) {
+            vas = vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
+            if (vas != 0) {
+                av_log(avctx, AV_LOG_WARNING, "Failed to destroy param buffer %d\n",i);
+            }
+            pic->param_buffers[i] = VA_INVALID_ID;
+        }
+    }
+
     pic->encode_issued = 1;
 
     if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
-- 
2.8.0



More information about the ffmpeg-devel mailing list