From f69ae619e5b10a39d261af36fe9b0cb33f28c3b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
Date: Wed, 4 Mar 2015 04:04:47 +0100
Subject: dxva2: Always destroy buffers when calling vaRenderPicture.

The vaapi documentation says the opposite, but other applications do that too.
Avoids that the vaapi implementation runs out of memory/buffers sooner or later.
---
 dlls/dxva2/vaapi-h264.c  | 19 ++++++++++---------
 dlls/dxva2/vaapi-mpeg2.c | 19 ++++++++++---------
 2 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/dlls/dxva2/vaapi-h264.c b/dlls/dxva2/vaapi-h264.c
index 7fe145c..edde27e 100644
--- a/dlls/dxva2/vaapi-h264.c
+++ b/dlls/dxva2/vaapi-h264.c
@@ -212,13 +212,13 @@ static HRESULT process_picture_parameters( WineVideoDecoderH264Impl *This, const
         return E_FAIL;
     }
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &vaPictureParam, 1);
+    pvaDestroyBuffer(va_display, vaPictureParam);
+
     if (status == VA_STATUS_SUCCESS)
         return S_OK;
 
     ERR("failed to process picture parameter buffer: %s (0x%x)\n", pvaErrorStr(status), status);
-    pvaDestroyBuffer(va_display, vaPictureParam);
     return E_FAIL;
 }
 
@@ -244,13 +244,13 @@ static HRESULT process_quantization_matrix( WineVideoDecoderH264Impl *This, cons
         return E_FAIL;
     }
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &vaIQMatrix, 1);
+    pvaDestroyBuffer(va_display, vaIQMatrix);
+
     if (status == VA_STATUS_SUCCESS)
         return S_OK;
 
     ERR("failed to process quantization matrix buffer: %s (0x%x)\n", pvaErrorStr(status), status);
-    pvaDestroyBuffer(va_display, vaIQMatrix);
     return E_FAIL;
 }
 
@@ -429,10 +429,12 @@ static HRESULT process_slice_control_buffer( WineVideoDecoderH264Impl *This, con
     if (pvaUnmapBuffer(va_display, vaSliceInfo) != VA_STATUS_SUCCESS)
         goto err;
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &vaSliceInfo, 1);
     if (status == VA_STATUS_SUCCESS)
+    {
+        pvaDestroyBuffer(va_display, vaSliceInfo);
         return S_OK;
+    }
 
     ERR("failed to process slice control buffer: %s (0x%x)\n", pvaErrorStr(status), status);
 
@@ -446,20 +448,19 @@ static HRESULT process_data_buffer( WineVideoDecoderH264Impl *This, const DXVA2_
 {
     VADisplay va_display = IWineVideoService_VADisplay(This->service);
     VAStatus status;
-    HRESULT hr = E_FAIL;
+    HRESULT hr = S_OK;
 
     if (This->vaBitstream == VA_INVALID_ID)
         return E_FAIL;
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &This->vaBitstream, 1);
     if (status != VA_STATUS_SUCCESS)
     {
         ERR("failed to process slice buffer: %s (0x%x)\n", pvaErrorStr(status), status);
-        pvaDestroyBuffer(va_display, This->vaBitstream);
+        hr = E_FAIL;
     }
-    else hr = S_OK;
 
+    pvaDestroyBuffer(va_display, This->vaBitstream);
     This->vaBitstream = VA_INVALID_ID;
     return hr;
 }
diff --git a/dlls/dxva2/vaapi-mpeg2.c b/dlls/dxva2/vaapi-mpeg2.c
index 7b7f61d..3b4b859 100644
--- a/dlls/dxva2/vaapi-mpeg2.c
+++ b/dlls/dxva2/vaapi-mpeg2.c
@@ -145,13 +145,13 @@ static HRESULT process_picture_parameters( WineVideoDecoderMPEG2Impl *This, cons
         return E_FAIL;
     }
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &vaPictureParam, 1);
+    pvaDestroyBuffer(va_display, vaPictureParam);
+
     if (status == VA_STATUS_SUCCESS)
         return S_OK;
 
     ERR("failed to process picture parameter buffer: %s (0x%x)\n", pvaErrorStr(status), status);
-    pvaDestroyBuffer(va_display, vaPictureParam);
     return E_FAIL;
 }
 
@@ -193,13 +193,13 @@ static HRESULT process_quantization_matrix( WineVideoDecoderMPEG2Impl *This, con
         return E_FAIL;
     }
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &vaIQMatrix, 1);
+    pvaDestroyBuffer(va_display, vaIQMatrix);
+
     if (status == VA_STATUS_SUCCESS)
         return S_OK;
 
     ERR("failed to process quantization matrix buffer: %s (0x%x)\n", pvaErrorStr(status), status);
-    pvaDestroyBuffer(va_display, vaIQMatrix);
     return E_FAIL;
 }
 
@@ -267,10 +267,12 @@ static HRESULT process_slice_control_buffer( WineVideoDecoderMPEG2Impl *This, co
     if (pvaUnmapBuffer(va_display, vaSliceInfo) != VA_STATUS_SUCCESS)
         goto err;
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &vaSliceInfo, 1);
     if (status == VA_STATUS_SUCCESS)
+    {
+        pvaDestroyBuffer(va_display, vaSliceInfo);
         return S_OK;
+    }
 
     ERR("failed to process slice control buffer: %s (0x%x)\n", pvaErrorStr(status), status);
 
@@ -284,20 +286,19 @@ static HRESULT process_data_buffer( WineVideoDecoderMPEG2Impl *This, const DXVA2
 {
     VADisplay va_display = IWineVideoService_VADisplay(This->service);
     VAStatus status;
-    HRESULT hr = E_FAIL;
+    HRESULT hr = S_OK;
 
     if (This->vaBitstream == VA_INVALID_ID)
         return E_FAIL;
 
-    /* vaRenderPicture is supposed to destroy the buffer */
     status = pvaRenderPicture(va_display, This->context, &This->vaBitstream, 1);
     if (status != VA_STATUS_SUCCESS)
     {
         ERR("failed to process slice buffer: %s (0x%x)\n", pvaErrorStr(status), status);
-        pvaDestroyBuffer(va_display, This->vaBitstream);
+        hr = E_FAIL;
     }
-    else hr = S_OK;
 
+    pvaDestroyBuffer(va_display, This->vaBitstream);
     This->vaBitstream = VA_INVALID_ID;
     return hr;
 }
-- 
2.3.0

