[vlc-commits] vdpau: preallocate pool of video surfaces

Rémi Denis-Courmont git at videolan.org
Sat Oct 4 15:51:17 CEST 2014


vlc/vlc-2.2 | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Oct  4 16:29:59 2014 +0300| [a9b21ce754063dc6d11dc93d6fd978f30d7346e7] | committer: Rémi Denis-Courmont

vdpau: preallocate pool of video surfaces

That should help failing safe to software decoding if the graphic card
has too little free memory. Old cards with only 256 MiB are commonly
affected by that problem.

(cherry picked from commit f6da7e32f09d9671e92d90d98b7a2c51c28290b5)

Conflicts:
	modules/hw/vdpau/avcodec.c

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.2.git/?a=commit;h=a9b21ce754063dc6d11dc93d6fd978f30d7346e7
---

 modules/hw/vdpau/avcodec.c |   76 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 71 insertions(+), 5 deletions(-)

diff --git a/modules/hw/vdpau/avcodec.c b/modules/hw/vdpau/avcodec.c
index 1fbb5fa..8515dbc 100644
--- a/modules/hw/vdpau/avcodec.c
+++ b/modules/hw/vdpau/avcodec.c
@@ -34,6 +34,7 @@
 #include <vlc_plugin.h>
 #include <vlc_fourcc.h>
 #include <vlc_picture.h>
+#include <vlc_atomic.h>
 #include <vlc_xlib.h>
 #include "vlc_vdpau.h"
 #include "../../codec/avcodec/va.h"
@@ -58,6 +59,7 @@ struct vlc_va_sys_t
     VdpDecoderProfile profile;
     uint16_t width;
     uint16_t height;
+    vlc_vdp_video_field_t **pool;
 };
 
 static vlc_vdp_video_field_t *CreateSurface(vlc_va_t *va)
@@ -81,11 +83,46 @@ static vlc_vdp_video_field_t *CreateSurface(vlc_va_t *va)
     return field;
 }
 
+static void DestroySurface(vlc_vdp_video_field_t *field)
+{
+    assert(field != NULL);
+    field->destroy(field);
+}
+
+static vlc_vdp_video_field_t *GetSurface(vlc_va_t *va)
+{
+    vlc_va_sys_t *sys = va->sys;
+    vlc_vdp_video_field_t *f;
+
+    for (unsigned i = 0; (f = sys->pool[i]) != NULL; i++)
+    {
+        uintptr_t expected = 1;
+
+        if (atomic_compare_exchange_strong(&f->frame->refs, &expected, 2))
+        {
+            vlc_vdp_video_field_t *field = vlc_vdp_video_copy(f);
+            atomic_fetch_sub(&f->frame->refs, 1);
+            return field;
+        }
+    }
+
+    /* All pictures in the pool are referenced. Try to make a new one. */
+    return CreateSurface(va);
+}
+
 static int Lock(vlc_va_t *va, void **opaque, uint8_t **data)
 {
-    vlc_vdp_video_field_t *field = CreateSurface(va);
-    if (unlikely(field == NULL))
-        return VLC_ENOMEM;
+    vlc_vdp_video_field_t *field;
+    unsigned tries = (CLOCK_FREQ + VOUT_OUTMEM_SLEEP) / VOUT_OUTMEM_SLEEP;
+
+    while ((field = GetSurface(va)) == NULL)
+    {
+        if (--tries == 0)
+            return VLC_ENOMEM;
+        /* Out of video RAM, wait for some time as in src/input/decoder.c.
+         * XXX: Both this and the core should use a semaphore or a CV. */
+        msleep(VOUT_OUTMEM_SLEEP);
+    }
 
     *opaque = field;
     *data = (void *)(uintptr_t)field->frame->surface;
@@ -96,8 +133,7 @@ static void Unlock(void *opaque, uint8_t *data)
 {
     vlc_vdp_video_field_t *field = opaque;
 
-    assert(field != NULL);
-    field->destroy(field);
+    DestroySurface(field);
     (void) data;
 }
 
@@ -137,12 +173,39 @@ static int Init(vlc_va_t *va, void **ctxp, vlc_fourcc_t *chromap,
         break;
     }
 
+    vlc_vdp_video_field_t **pool = malloc(sizeof (*pool) * (surfaces + 6));
+    if (unlikely(pool == NULL))
+        return VLC_ENOMEM;
+
+    unsigned i = 0;
+    while (i < surfaces + 5)
+    {
+        pool[i] = CreateSurface(va);
+        if (pool[i] == NULL)
+            break;
+        i++;
+    }
+    pool[i] = NULL;
+
+    if (i < surfaces + 3)
+    {
+        msg_Err(va, "not enough video RAM");
+        while (i > 0)
+            DestroySurface(pool[--i]);
+        free(pool);
+        return VLC_ENOMEM;
+    }
+    sys->pool = pool;
+
     err = vdp_decoder_create(sys->vdp, sys->device, sys->profile, width,
                              height, surfaces, &sys->context->decoder);
     if (err != VDP_STATUS_OK)
     {
         msg_Err(va, "%s creation failure: %s", "decoder",
                 vdp_get_error_string(sys->vdp, err));
+        while (i > 0)
+            DestroySurface(pool[--i]);
+        free(pool);
         sys->context->decoder = VDP_INVALID_HANDLE;
         return VLC_EGENERIC;
     }
@@ -159,6 +222,9 @@ static void Deinit(vlc_va_t *va)
 
     assert(sys->context->decoder != VDP_INVALID_HANDLE);
     vdp_decoder_destroy(sys->vdp, sys->context->decoder);
+    for (unsigned i = 0; sys->pool[i] != NULL; i++)
+        sys->pool[i]->destroy(sys->pool[i]);
+    free(sys->pool);
 }
 
 static int Setup(vlc_va_t *va, void **ctxp, vlc_fourcc_t *chromap,



More information about the vlc-commits mailing list