From 85a28e9a1e69628b6149bdb0efb50b0a157f9c48 Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux <ajanni@videolabs.io> Date: Wed, 11 Sep 2024 15:49:55 +0200 Subject: [PATCH] vpx_alpha: add reference-counting to picture context The context is read-only and was currently copied for every picture, so switch to reference counting to avoid this. It will also help when moving planes stored in p_sys into the context, to determine when the allocated plane should be released, and to copy/share the pointer automatically. --- modules/codec/vpx_alpha.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/codec/vpx_alpha.c b/modules/codec/vpx_alpha.c index d9adc51bee99..ea6b617e6487 100644 --- a/modules/codec/vpx_alpha.c +++ b/modules/codec/vpx_alpha.c @@ -4,6 +4,7 @@ // Copyright © 2023 VideoLabs, VLC authors and VideoLAN // Authors: Steve Lhomme <robux4@videolabs.io> +// Alexandre Janniaux <ajanni@videolabs.io> #ifdef HAVE_CONFIG_H # include "config.h" @@ -63,6 +64,7 @@ static vlc_decoder_device *GetDevice( decoder_t *dec ) struct cpu_alpha_context { + vlc_atomic_rc_t rc; picture_context_t ctx; picture_t *opaque; picture_t *alpha; // may be NULL if the alpha layer was missing @@ -71,6 +73,10 @@ struct cpu_alpha_context static void cpu_alpha_destroy(picture_context_t *ctx) { struct cpu_alpha_context *pctx = container_of(ctx, struct cpu_alpha_context, ctx); + + if (!vlc_atomic_rc_dec(&pctx->rc)) + return; + picture_Release(pctx->opaque); if (pctx->alpha) picture_Release(pctx->alpha); @@ -80,13 +86,9 @@ static void cpu_alpha_destroy(picture_context_t *ctx) static picture_context_t *cpu_alpha_copy(picture_context_t *src) { struct cpu_alpha_context *pctx = container_of(src, struct cpu_alpha_context, ctx); - struct cpu_alpha_context *alpha_ctx = calloc(1, sizeof(*alpha_ctx)); - if (unlikely(alpha_ctx == NULL)) - return NULL; - alpha_ctx->ctx = *src; - alpha_ctx->opaque = picture_Hold(pctx->opaque); - alpha_ctx->alpha = alpha_ctx->alpha ? picture_Hold(pctx->alpha) : NULL; - return &alpha_ctx->ctx; + + vlc_atomic_rc_inc(&pctx->rc); + return &pctx->ctx; } struct pic_alpha_plane @@ -114,6 +116,7 @@ static picture_t *CombinePicturesCPU(decoder_t *bdec, picture_t *opaque, picture picture_Release(out); return NULL; } + vlc_atomic_rc_init(&alpha_ctx->rc); alpha_ctx->ctx = (picture_context_t) { cpu_alpha_destroy, cpu_alpha_copy, NULL }; -- GitLab