[vlc-devel] [PATCH 7/8] nvdec: use a surface pool for the decoder to CPU

Steve Lhomme robux4 at ycbcr.xyz
Mon Jul 27 15:45:28 CEST 2020


So that we consistently use our own pool with a tighter size.
---
 modules/hw/nvdec/nvdec.c | 52 ++++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 12 deletions(-)

diff --git a/modules/hw/nvdec/nvdec.c b/modules/hw/nvdec/nvdec.c
index 54a9c849b50..7cad7d8b239 100644
--- a/modules/hw/nvdec/nvdec.c
+++ b/modules/hw/nvdec/nvdec.c
@@ -128,11 +128,15 @@ typedef struct nvdec_ctx {
 
 static void nvdec_pool_Destroy(nvdec_pool_t *pool)
 {
+    if (pool->outputDevicePtr[0])
+    {
     for (size_t i=0; i < ARRAY_SIZE(pool->outputDevicePtr); i++)
         CALL_CUDA_POOL(cuMemFree, pool->outputDevicePtr[i]);
+    }
 
     picture_pool_Release(pool->picture_pool);
-    vlc_video_context_Release(pool->vctx);
+    if (pool->vctx)
+        vlc_video_context_Release(pool->vctx);
 }
 
 static void nvdec_pool_AddRef(nvdec_pool_t *pool)
@@ -216,6 +220,26 @@ error:
     return NULL;
 }
 
+static nvdec_pool_t*nvdec_pool_CreateFromFormat(const video_format_t *fmt,
+                                                vlc_cancelable *pool_cancel)
+{
+    nvdec_pool_t *pool = calloc(1, sizeof(*pool));
+    if (!pool)
+        goto error;
+
+    pool->picture_pool = picture_pool_NewFromFormat(fmt, MAX_POOL_SIZE, pool_cancel);
+    if (!pool->picture_pool)
+        goto error;
+
+    vlc_atomic_rc_init(&pool->rc);
+    return pool;
+
+error:
+    if (pool)
+        free(pool);
+    return NULL;
+}
+
 static void nvdec_picture_CtxDestroy(struct picture_context_t *picctx)
 {
     pic_pool_context_nvdec_t *srcpic = NVDEC_PICPOOLCTX_FROM_PICCTX(picctx);
@@ -251,7 +275,8 @@ static picture_t* nvdec_pool_Wait(nvdec_pool_t *pool)
         nvdec_picture_CtxClone,
         pool->vctx,
     };
-    vlc_video_context_Hold(picctx->ctx.ctx.vctx);
+    if (picctx->ctx.ctx.vctx)
+        vlc_video_context_Hold(picctx->ctx.ctx.vctx);
 
     picctx->pool = pool;
     nvdec_pool_AddRef(picctx->pool);
@@ -432,14 +457,21 @@ static int CUDAAPI HandleVideoSequence(void *p_opaque, CUVIDEOFORMAT *p_format)
                                             &p_dec->fmt_out.video,
                                             ByteWidth,
                                             Height);
-        if (p_sys->out_pool == NULL)
-            goto cuda_error;
+    }
+    else
+    {
+        p_sys->out_pool = nvdec_pool_CreateFromFormat(
+                                            &p_dec->fmt_out.video,
+                                            p_dec->pool_cancel);
     }
 
     p_sys->decoderHeight = p_format->coded_height;
 
     CALL_CUDA_DEC(cuCtxPopCurrent, NULL);
 
+    if (p_sys->out_pool == NULL)
+        goto error;
+
     ret = decoder_UpdateVideoOutput(p_dec, p_sys->vctx_out);
     return (ret == VLC_SUCCESS);
 
@@ -470,7 +502,6 @@ static int CUDAAPI HandlePictureDisplay(void *p_opaque, CUVIDPARSERDISPINFO *p_d
 {
     decoder_t *p_dec = (decoder_t *) p_opaque;
     nvdec_ctx_t *p_sys = p_dec->p_sys;
-    picture_t *p_pic = NULL;
     CUdeviceptr frameDevicePtr = 0;
     CUVIDPROCPARAMS params = {
         .progressive_frame = p_sys->deintMode == cudaVideoDeinterlaceMode_Weave ? 1 : p_dispinfo->progressive_frame,
@@ -480,11 +511,12 @@ static int CUDAAPI HandlePictureDisplay(void *p_opaque, CUVIDPARSERDISPINFO *p_d
     };
     int result;
 
+    picture_t *p_pic = nvdec_pool_Wait(p_sys->out_pool);
+    if (unlikely(p_pic == NULL))
+        return 0;
+
     if ( is_nvdec_opaque(p_dec->fmt_out.video.i_chroma) )
     {
-        p_pic = nvdec_pool_Wait(p_sys->out_pool);
-        if (unlikely(p_pic == NULL))
-            return 0;
         pic_context_nvdec_t *picctx = NVDEC_PICCONTEXT_FROM_PICCTX(p_pic->context);
 
         result = CALL_CUDA_DEC(cuCtxPushCurrent, p_sys->devsys->cuCtx);
@@ -559,10 +591,6 @@ static int CUDAAPI HandlePictureDisplay(void *p_opaque, CUVIDPARSERDISPINFO *p_d
     }
     else
     {
-        p_pic = decoder_NewPicture(p_dec);
-        if (unlikely(p_pic == NULL))
-            return 0;
-
         result = CALL_CUDA_DEC(cuCtxPushCurrent, p_sys->devsys->cuCtx);
         if (unlikely(result != VLC_SUCCESS))
         {
-- 
2.26.2



More information about the vlc-devel mailing list