[FFmpeg-devel] [PATCH 04/10] lavfi/vf_*_cuda: do not access hw contexts before checking they exist

Anton Khirnov anton at khirnov.net
Sun Aug 11 17:42:05 EEST 2024


The checks are performed in init_processing_chain().
---
 libavfilter/vf_bilateral_cuda.c  | 13 ++++++++-----
 libavfilter/vf_chromakey_cuda.c  | 13 ++++++++-----
 libavfilter/vf_colorspace_cuda.c | 14 ++++++++------
 libavfilter/vf_scale_cuda.c      | 13 ++++++++-----
 4 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/libavfilter/vf_bilateral_cuda.c b/libavfilter/vf_bilateral_cuda.c
index ba008b517a..037227af93 100644
--- a/libavfilter/vf_bilateral_cuda.c
+++ b/libavfilter/vf_bilateral_cuda.c
@@ -249,17 +249,20 @@ static av_cold int cuda_bilateral_config_props(AVFilterLink *outlink)
     AVFilterContext *ctx = outlink->src;
     AVFilterLink *inlink = outlink->src->inputs[0];
     CUDABilateralContext *s  = ctx->priv;
-    AVHWFramesContext     *frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
-    AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
+    AVHWFramesContext     *frames_ctx;
+    AVCUDADeviceContext *device_hwctx;
     int ret;
 
-    s->hwctx = device_hwctx;
-    s->cu_stream = s->hwctx->stream;
-
     ret = init_processing_chain(ctx, inlink->w, inlink->h);
     if (ret < 0)
         return ret;
 
+    frames_ctx   = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
+    device_hwctx = frames_ctx->device_ctx->hwctx;
+
+    s->hwctx = device_hwctx;
+    s->cu_stream = s->hwctx->stream;
+
     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
 
     // the window_size makes more sense when it is odd, so add 1 if even
diff --git a/libavfilter/vf_chromakey_cuda.c b/libavfilter/vf_chromakey_cuda.c
index ac644caea7..e38521be0d 100644
--- a/libavfilter/vf_chromakey_cuda.c
+++ b/libavfilter/vf_chromakey_cuda.c
@@ -259,13 +259,10 @@ static av_cold int cudachromakey_config_props(AVFilterLink *outlink)
     AVFilterContext *ctx = outlink->src;
     AVFilterLink *inlink = outlink->src->inputs[0];
     ChromakeyCUDAContext *s = ctx->priv;
-    AVHWFramesContext *frames_ctx = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
-    AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
+    AVHWFramesContext *frames_ctx;
+    AVCUDADeviceContext *device_hwctx;
     int ret;
 
-    s->hwctx = device_hwctx;
-    s->cu_stream = s->hwctx->stream;
-
     if (s->is_yuv) {
         s->chromakey_uv[0] = s->chromakey_rgba[1];
         s->chromakey_uv[1] = s->chromakey_rgba[2];
@@ -278,6 +275,12 @@ static av_cold int cudachromakey_config_props(AVFilterLink *outlink)
     if (ret < 0)
         return ret;
 
+    frames_ctx = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
+    device_hwctx = frames_ctx->device_ctx->hwctx;
+
+    s->hwctx = device_hwctx;
+    s->cu_stream = s->hwctx->stream;
+
     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
 
     ret = cudachromakey_load_functions(ctx);
diff --git a/libavfilter/vf_colorspace_cuda.c b/libavfilter/vf_colorspace_cuda.c
index 5ad81e959c..3949cb7ed8 100644
--- a/libavfilter/vf_colorspace_cuda.c
+++ b/libavfilter/vf_colorspace_cuda.c
@@ -226,14 +226,10 @@ static av_cold int cudacolorspace_config_props(AVFilterLink* outlink)
     AVFilterContext* ctx = outlink->src;
     AVFilterLink* inlink = outlink->src->inputs[0];
     CUDAColorspaceContext* s = ctx->priv;
-    AVHWFramesContext* frames_ctx =
-        (AVHWFramesContext*)inlink->hw_frames_ctx->data;
-    AVCUDADeviceContext* device_hwctx = frames_ctx->device_ctx->hwctx;
+    AVHWFramesContext* frames_ctx;
+    AVCUDADeviceContext* device_hwctx;
     int ret;
 
-    s->hwctx = device_hwctx;
-    s->cu_stream = s->hwctx->stream;
-
     outlink->w = inlink->w;
     outlink->h = inlink->h;
 
@@ -241,6 +237,12 @@ static av_cold int cudacolorspace_config_props(AVFilterLink* outlink)
     if (ret < 0)
         return ret;
 
+    frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
+    device_hwctx = frames_ctx->device_ctx->hwctx;
+
+    s->hwctx = device_hwctx;
+    s->cu_stream = s->hwctx->stream;
+
     if (inlink->sample_aspect_ratio.num) {
         outlink->sample_aspect_ratio = av_mul_q(
             (AVRational){outlink->h * inlink->w, outlink->w * inlink->h},
diff --git a/libavfilter/vf_scale_cuda.c b/libavfilter/vf_scale_cuda.c
index 5571a52b1e..68d17072e3 100644
--- a/libavfilter/vf_scale_cuda.c
+++ b/libavfilter/vf_scale_cuda.c
@@ -349,14 +349,11 @@ static av_cold int cudascale_config_props(AVFilterLink *outlink)
     AVFilterContext *ctx = outlink->src;
     AVFilterLink *inlink = outlink->src->inputs[0];
     CUDAScaleContext *s  = ctx->priv;
-    AVHWFramesContext     *frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
-    AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
+    AVHWFramesContext     *frames_ctx;
+    AVCUDADeviceContext *device_hwctx;
     int w, h;
     int ret;
 
-    s->hwctx = device_hwctx;
-    s->cu_stream = s->hwctx->stream;
-
     if ((ret = ff_scale_eval_dimensions(s,
                                         s->w_expr, s->h_expr,
                                         inlink, outlink,
@@ -377,6 +374,12 @@ static av_cold int cudascale_config_props(AVFilterLink *outlink)
     if (ret < 0)
         return ret;
 
+    frames_ctx   = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
+    device_hwctx = frames_ctx->device_ctx->hwctx;
+
+    s->hwctx = device_hwctx;
+    s->cu_stream = s->hwctx->stream;
+
     if (inlink->sample_aspect_ratio.num) {
         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
                                                              outlink->w*inlink->h},
-- 
2.43.0



More information about the ffmpeg-devel mailing list