diff mbox series

[FFmpeg-devel,2/3] avfilter/yadif_common: factorize some part of the config_output and the uninit functions

Message ID 20240128030136.5585-2-cus@passwd.hu
State Accepted
Commit 268062fa15e1c05310f9c3977e0b95ac07eca274
Headers show
Series [FFmpeg-devel,1/3] avutil/rational: increase av_d2q precision | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Marton Balint Jan. 28, 2024, 3:01 a.m. UTC
This unifies slightly diverged code and ensures that cc_fifo is always initialized.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavfilter/vf_bwdif.c              | 30 +++-------------------
 libavfilter/vf_bwdif_cuda.c         | 15 +++--------
 libavfilter/vf_bwdif_vulkan.c       | 12 ++++-----
 libavfilter/vf_yadif.c              | 33 +++---------------------
 libavfilter/vf_yadif_cuda.c         | 27 +++----------------
 libavfilter/vf_yadif_videotoolbox.m | 19 +++-----------
 libavfilter/yadif.h                 |  4 +++
 libavfilter/yadif_common.c          | 40 +++++++++++++++++++++++++++++
 8 files changed, 66 insertions(+), 114 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/vf_bwdif.c b/libavfilter/vf_bwdif.c
index 353cd0b61a..9042db8d7f 100644
--- a/libavfilter/vf_bwdif.c
+++ b/libavfilter/vf_bwdif.c
@@ -137,17 +137,6 @@  static void filter(AVFilterContext *ctx, AVFrame *dstpic,
     }
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    BWDIFContext *bwdif = ctx->priv;
-    YADIFContext *yadif = &bwdif->yadif;
-
-    av_frame_free(&yadif->prev);
-    av_frame_free(&yadif->cur );
-    av_frame_free(&yadif->next);
-    ff_ccfifo_uninit(&yadif->cc_fifo);
-}
-
 static const enum AVPixelFormat pix_fmts[] = {
     AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV420P,
     AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
@@ -176,20 +165,9 @@  static int config_props(AVFilterLink *link)
     YADIFContext *yadif = &s->yadif;
     int ret;
 
-    link->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
-    link->w         = link->src->inputs[0]->w;
-    link->h         = link->src->inputs[0]->h;
-
-    if(yadif->mode&1)
-        link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
-    else
-        link->frame_rate = ctx->inputs[0]->frame_rate;
-
-    ret = ff_ccfifo_init(&yadif->cc_fifo, link->frame_rate, ctx);
-    if (ret < 0 ) {
-        av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
-        return ret;
-    }
+    ret = ff_yadif_config_output_common(link);
+    if (ret < 0)
+        return AVERROR(EINVAL);
 
     yadif->csp = av_pix_fmt_desc_get(link->format);
     yadif->filter = filter;
@@ -251,7 +229,7 @@  const AVFilter ff_vf_bwdif = {
     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
     .priv_size     = sizeof(BWDIFContext),
     .priv_class    = &bwdif_class,
-    .uninit        = uninit,
+    .uninit        = ff_yadif_uninit,
     FILTER_INPUTS(avfilter_vf_bwdif_inputs),
     FILTER_OUTPUTS(avfilter_vf_bwdif_outputs),
     FILTER_PIXFMTS_ARRAY(pix_fmts),
diff --git a/libavfilter/vf_bwdif_cuda.c b/libavfilter/vf_bwdif_cuda.c
index 418f15f989..7585d1fe25 100644
--- a/libavfilter/vf_bwdif_cuda.c
+++ b/libavfilter/vf_bwdif_cuda.c
@@ -208,9 +208,7 @@  static av_cold void deint_cuda_uninit(AVFilterContext *ctx)
         CHECK_CU(cu->cuCtxPopCurrent(&dummy));
     }
 
-    av_frame_free(&y->prev);
-    av_frame_free(&y->cur);
-    av_frame_free(&y->next);
+    ff_yadif_uninit(ctx);
 
     av_buffer_unref(&s->device_ref);
     s->hwctx = NULL;
@@ -288,14 +286,9 @@  static int config_output(AVFilterLink *link)
         goto exit;
     }
 
-    link->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
-    link->w         = ctx->inputs[0]->w;
-    link->h         = ctx->inputs[0]->h;
-
-    if(y->mode & 1)
-        link->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
-                                    (AVRational){2, 1});
-
+    ret = ff_yadif_config_output_common(link);
+    if (ret < 0)
+        goto exit;
 
     y->csp = av_pix_fmt_desc_get(output_frames->sw_format);
     y->filter = filter;
diff --git a/libavfilter/vf_bwdif_vulkan.c b/libavfilter/vf_bwdif_vulkan.c
index c51df9aa26..57711fb672 100644
--- a/libavfilter/vf_bwdif_vulkan.c
+++ b/libavfilter/vf_bwdif_vulkan.c
@@ -296,6 +296,8 @@  static void bwdif_vulkan_uninit(AVFilterContext *avctx)
 
     ff_vk_uninit(&s->vkctx);
 
+    ff_yadif_uninit(avctx);
+
     s->initialized = 0;
 }
 
@@ -354,13 +356,9 @@  static int bwdif_vulkan_config_output(AVFilterLink *outlink)
     if (!outlink->hw_frames_ctx)
         return AVERROR(ENOMEM);
 
-    outlink->time_base = av_mul_q(avctx->inputs[0]->time_base, (AVRational){1, 2});
-    outlink->w         = vkctx->output_width;
-    outlink->h         = vkctx->output_height;
-
-    if (y->mode & 1)
-        outlink->frame_rate = av_mul_q(avctx->inputs[0]->frame_rate,
-                                       (AVRational){2, 1});
+    err = ff_yadif_config_output_common(outlink);
+    if (err < 0)
+        return err;
 
     y->csp = av_pix_fmt_desc_get(vkctx->frames->sw_format);
     y->filter = bwdif_vulkan_filter_frame;
diff --git a/libavfilter/vf_yadif.c b/libavfilter/vf_yadif.c
index a5a856bf5f..aa5ca4a889 100644
--- a/libavfilter/vf_yadif.c
+++ b/libavfilter/vf_yadif.c
@@ -251,16 +251,6 @@  static void filter(AVFilterContext *ctx, AVFrame *dstpic,
     }
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    YADIFContext *yadif = ctx->priv;
-
-    av_frame_free(&yadif->prev);
-    av_frame_free(&yadif->cur );
-    av_frame_free(&yadif->next);
-    ff_ccfifo_uninit(&yadif->cc_fifo);
-}
-
 static const enum AVPixelFormat pix_fmts[] = {
     AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV444P,
     AV_PIX_FMT_YUV410P,   AV_PIX_FMT_YUV411P,   AV_PIX_FMT_YUV440P,
@@ -285,26 +275,9 @@  static int config_output(AVFilterLink *outlink)
     YADIFContext *s = ctx->priv;
     int ret;
 
-    outlink->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
-    outlink->w             = ctx->inputs[0]->w;
-    outlink->h             = ctx->inputs[0]->h;
-
-    if(s->mode & 1)
-        outlink->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
-                                    (AVRational){2, 1});
-    else
-        outlink->frame_rate = ctx->inputs[0]->frame_rate;
-
-    ret = ff_ccfifo_init(&s->cc_fifo, outlink->frame_rate, ctx);
-    if (ret < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
+    ret = ff_yadif_config_output_common(outlink);
+    if (ret < 0)
         return ret;
-    }
-
-    if (outlink->w < 3 || outlink->h < 3) {
-        av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
-        return AVERROR(EINVAL);
-    }
 
     s->csp = av_pix_fmt_desc_get(outlink->format);
     s->filter = filter;
@@ -354,7 +327,7 @@  const AVFilter ff_vf_yadif = {
     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
     .priv_size     = sizeof(YADIFContext),
     .priv_class    = &yadif_class,
-    .uninit        = uninit,
+    .uninit        = ff_yadif_uninit,
     FILTER_INPUTS(avfilter_vf_yadif_inputs),
     FILTER_OUTPUTS(avfilter_vf_yadif_outputs),
     FILTER_PIXFMTS_ARRAY(pix_fmts),
diff --git a/libavfilter/vf_yadif_cuda.c b/libavfilter/vf_yadif_cuda.c
index d777757e65..17389f092f 100644
--- a/libavfilter/vf_yadif_cuda.c
+++ b/libavfilter/vf_yadif_cuda.c
@@ -200,10 +200,7 @@  static av_cold void deint_cuda_uninit(AVFilterContext *ctx)
         CHECK_CU(cu->cuCtxPopCurrent(&dummy));
     }
 
-    av_frame_free(&y->prev);
-    av_frame_free(&y->cur);
-    av_frame_free(&y->next);
-    ff_ccfifo_uninit(&y->cc_fifo);
+    ff_yadif_uninit(ctx);
 
     av_buffer_unref(&s->device_ref);
     s->hwctx = NULL;
@@ -281,27 +278,9 @@  static int config_output(AVFilterLink *link)
         goto exit;
     }
 
-    link->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
-    link->w         = ctx->inputs[0]->w;
-    link->h         = ctx->inputs[0]->h;
-
-    if(y->mode & 1)
-        link->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
-                                    (AVRational){2, 1});
-    else
-        link->frame_rate = ctx->inputs[0]->frame_rate;
-
-    ret = ff_ccfifo_init(&y->cc_fifo, link->frame_rate, ctx);
-    if (ret < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
-        goto exit;
-    }
-
-    if (link->w < 3 || link->h < 3) {
-        av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
-        ret = AVERROR(EINVAL);
+    ret = ff_yadif_config_output_common(link);
+    if (ret < 0)
         goto exit;
-    }
 
     y->csp = av_pix_fmt_desc_get(output_frames->sw_format);
     y->filter = filter;
diff --git a/libavfilter/vf_yadif_videotoolbox.m b/libavfilter/vf_yadif_videotoolbox.m
index 69186c2254..28d836b782 100644
--- a/libavfilter/vf_yadif_videotoolbox.m
+++ b/libavfilter/vf_yadif_videotoolbox.m
@@ -174,9 +174,7 @@  static av_cold void do_uninit(AVFilterContext *ctx) API_AVAILABLE(macos(10.11),
     YADIFVTContext *s = ctx->priv;
     YADIFContext *y = &s->yadif;
 
-    av_frame_free(&y->prev);
-    av_frame_free(&y->cur);
-    av_frame_free(&y->next);
+    ff_yadif_uninit(ctx);
 
     av_buffer_unref(&s->device_ref);
     av_buffer_unref(&s->input_frames_ref);
@@ -363,20 +361,9 @@  static int do_config_output(AVFilterLink *link) API_AVAILABLE(macos(10.11), ios(
         goto exit;
     }
 
-    link->time_base.num = ctx->inputs[0]->time_base.num;
-    link->time_base.den = ctx->inputs[0]->time_base.den * 2;
-    link->w             = ctx->inputs[0]->w;
-    link->h             = ctx->inputs[0]->h;
-
-    if(y->mode & 1)
-        link->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
-                                    (AVRational){2, 1});
-
-    if (link->w < 3 || link->h < 3) {
-        av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
-        ret = AVERROR(EINVAL);
+    ret = ff_yadif_config_output_common(link);
+    if (ret < 0)
         goto exit;
-    }
 
     y->csp = av_pix_fmt_desc_get(output_frames->sw_format);
     y->filter = filter;
diff --git a/libavfilter/yadif.h b/libavfilter/yadif.h
index fbb99fd46e..2c4fed62d2 100644
--- a/libavfilter/yadif.h
+++ b/libavfilter/yadif.h
@@ -94,6 +94,10 @@  int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame);
 
 int ff_yadif_request_frame(AVFilterLink *link);
 
+int ff_yadif_config_output_common(AVFilterLink *outlink);
+
+void ff_yadif_uninit(AVFilterContext *ctx);
+
 extern const AVOption ff_yadif_options[];
 
 #endif /* AVFILTER_YADIF_H */
diff --git a/libavfilter/yadif_common.c b/libavfilter/yadif_common.c
index 21097011f5..933372529e 100644
--- a/libavfilter/yadif_common.c
+++ b/libavfilter/yadif_common.c
@@ -209,6 +209,46 @@  int ff_yadif_request_frame(AVFilterLink *link)
     return 0;
 }
 
+int ff_yadif_config_output_common(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    YADIFContext *yadif = ctx->priv;
+    int ret;
+
+    outlink->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
+    outlink->w             = ctx->inputs[0]->w;
+    outlink->h             = ctx->inputs[0]->h;
+
+    if (outlink->w < 3 || outlink->h < 3) {
+        av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
+        return AVERROR(EINVAL);
+    }
+
+    if(yadif->mode & 1)
+        outlink->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
+                                    (AVRational){2, 1});
+    else
+        outlink->frame_rate = ctx->inputs[0]->frame_rate;
+
+    ret = ff_ccfifo_init(&yadif->cc_fifo, outlink->frame_rate, ctx);
+    if (ret < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
+        return ret;
+    }
+
+    return 0;
+}
+
+void ff_yadif_uninit(AVFilterContext *ctx)
+{
+    YADIFContext *yadif = ctx->priv;
+
+    av_frame_free(&yadif->prev);
+    av_frame_free(&yadif->cur );
+    av_frame_free(&yadif->next);
+    ff_ccfifo_uninit(&yadif->cc_fifo);
+}
+
 #define OFFSET(x) offsetof(YADIFContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM