From 8c8fbb7e12481ebb0fd2fcd983d794975dcdfddf Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 16 Aug 2024 07:36:05 +0800 Subject: [PATCH 1/2] avfilter/*videotoolbox: fix color range handling --- ...add-format-option-to-vt-scale-filter.patch | 7 ++- ...fixes-for-videotoolbox-from-upstream.patch | 48 +++++++++++++++++++ ...0-add-vf-overlay-videotoolbox-filter.patch | 4 +- ...reimage-based-vf-transpose-vt-filter.patch | 4 +- ...2-add-vf-tonemap-videotoolbox-filter.patch | 12 +++-- 5 files changed, 68 insertions(+), 7 deletions(-) diff --git a/debian/patches/0048-add-format-option-to-vt-scale-filter.patch b/debian/patches/0048-add-format-option-to-vt-scale-filter.patch index 6cd20502c6c..20feee2a476 100644 --- a/debian/patches/0048-add-format-option-to-vt-scale-filter.patch +++ b/debian/patches/0048-add-format-option-to-vt-scale-filter.patch @@ -37,7 +37,7 @@ Index: FFmpeg/libavfilter/vf_scale_vt.c err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink, &s->output_width, -@@ -198,11 +214,18 @@ static int scale_vt_config_output(AVFilt +@@ -198,13 +214,21 @@ static int scale_vt_config_output(AVFilt hw_frame_ctx_in = (AVHWFramesContext *)inlink->hw_frames_ctx->data; @@ -56,8 +56,11 @@ Index: FFmpeg/libavfilter/vf_scale_vt.c + hw_frame_ctx_out->sw_format = out_format; hw_frame_ctx_out->width = outlink->w; hw_frame_ctx_out->height = outlink->h; ++ ((AVVTFramesContext *)hw_frame_ctx_out->hwctx)->color_range = ((AVVTFramesContext *)hw_frame_ctx_in->hwctx)->color_range; -@@ -234,6 +257,8 @@ static const AVOption scale_vt_options[] + err = ff_filter_init_hw_frames(avctx, outlink, 1); + if (err < 0) +@@ -234,6 +258,8 @@ static const AVOption scale_vt_options[] OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS }, { "color_transfer", "Output colour transfer characteristics", OFFSET(colour_transfer_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS }, diff --git a/debian/patches/0049-backport-fixes-for-videotoolbox-from-upstream.patch b/debian/patches/0049-backport-fixes-for-videotoolbox-from-upstream.patch index da9a22d4dd1..6ea670e7c8b 100644 --- a/debian/patches/0049-backport-fixes-for-videotoolbox-from-upstream.patch +++ b/debian/patches/0049-backport-fixes-for-videotoolbox-from-upstream.patch @@ -518,3 +518,51 @@ Index: FFmpeg/libavutil/hwcontext_videotoolbox.h */ int av_vt_pixbuf_set_attachments(void *log_ctx, CVPixelBufferRef pixbuf, const struct AVFrame *src); +Index: FFmpeg/libavfilter/vf_yadif_videotoolbox.m +=================================================================== +--- FFmpeg.orig/libavfilter/vf_yadif_videotoolbox.m ++++ FFmpeg/libavfilter/vf_yadif_videotoolbox.m +@@ -24,6 +24,7 @@ + #include "yadif.h" + #include "libavutil/avassert.h" + #include "libavutil/hwcontext.h" ++#include "libavutil/hwcontext_videotoolbox.h" + #include "libavutil/objc.h" + + #include +@@ -94,9 +95,6 @@ static void call_kernel(AVFilterContext + + [buffer commit]; + [buffer waitUntilCompleted]; +- +- ff_objc_release(&encoder); +- ff_objc_release(&buffer); + } + + static void filter(AVFilterContext *ctx, AVFrame *dst, +@@ -321,8 +319,9 @@ static int config_input(AVFilterLink *in + + static int do_config_output(AVFilterLink *link) API_AVAILABLE(macos(10.11), ios(8.0)) + { +- AVHWFramesContext *output_frames; ++ AVHWFramesContext *output_frames, *input_frames; + AVFilterContext *ctx = link->src; ++ AVFilterLink *inlink = link->src->inputs[0]; + YADIFVTContext *s = ctx->priv; + YADIFContext *y = &s->yadif; + int ret = 0; +@@ -343,12 +342,14 @@ static int do_config_output(AVFilterLink + goto exit; + } + ++ input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data; + output_frames = (AVHWFramesContext*)link->hw_frames_ctx->data; + + output_frames->format = AV_PIX_FMT_VIDEOTOOLBOX; + output_frames->sw_format = s->input_frames->sw_format; + output_frames->width = ctx->inputs[0]->w; + output_frames->height = ctx->inputs[0]->h; ++ ((AVVTFramesContext *)output_frames->hwctx)->color_range = ((AVVTFramesContext *)input_frames->hwctx)->color_range; + + ret = ff_filter_init_hw_frames(ctx, link, 10); + if (ret < 0) diff --git a/debian/patches/0050-add-vf-overlay-videotoolbox-filter.patch b/debian/patches/0050-add-vf-overlay-videotoolbox-filter.patch index 2716c08c385..6b545170fb1 100644 --- a/debian/patches/0050-add-vf-overlay-videotoolbox-filter.patch +++ b/debian/patches/0050-add-vf-overlay-videotoolbox-filter.patch @@ -232,7 +232,7 @@ Index: FFmpeg/libavfilter/vf_overlay_videotoolbox.m =================================================================== --- /dev/null +++ FFmpeg/libavfilter/vf_overlay_videotoolbox.m -@@ -0,0 +1,749 @@ +@@ -0,0 +1,751 @@ +/* + * Copyright (C) 2024 Gnattu OC + * @@ -259,6 +259,7 @@ Index: FFmpeg/libavfilter/vf_overlay_videotoolbox.m +#include "metal/utils.h" +#include "framesync.h" +#include "libavutil/hwcontext.h" ++#include "libavutil/hwcontext_videotoolbox.h" +#include "libavutil/pixdesc.h" +#include "libavutil/opt.h" +#include "libavutil/objc.h" @@ -888,6 +889,7 @@ Index: FFmpeg/libavfilter/vf_overlay_videotoolbox.m + output_frames->sw_format = main_frames->sw_format; + output_frames->width = inlink_main->w; + output_frames->height = inlink_main->h; ++ ((AVVTFramesContext *)output_frames->hwctx)->color_range = ((AVVTFramesContext *)main_frames->hwctx)->color_range; + + ret = ff_filter_init_hw_frames(avctx, link, 1); + if (ret < 0) diff --git a/debian/patches/0051-add-coreimage-based-vf-transpose-vt-filter.patch b/debian/patches/0051-add-coreimage-based-vf-transpose-vt-filter.patch index 489c869cd47..6fdb4b924fb 100644 --- a/debian/patches/0051-add-coreimage-based-vf-transpose-vt-filter.patch +++ b/debian/patches/0051-add-coreimage-based-vf-transpose-vt-filter.patch @@ -305,7 +305,7 @@ Index: FFmpeg/libavfilter/vf_transpose_vt.m =================================================================== --- /dev/null +++ FFmpeg/libavfilter/vf_transpose_vt.m -@@ -0,0 +1,381 @@ +@@ -0,0 +1,383 @@ +/* + * Copyright (c) 2023 Zhao Zhili + * Copyright (c) 2024 Gnattu OC @@ -331,6 +331,7 @@ Index: FFmpeg/libavfilter/vf_transpose_vt.m +#include + +#include "libavutil/hwcontext.h" ++#include "libavutil/hwcontext_videotoolbox.h" +#include "libavutil/opt.h" +#include "libavutil/objc.h" +#include "internal.h" @@ -486,6 +487,7 @@ Index: FFmpeg/libavfilter/vf_transpose_vt.m + hw_frame_ctx_out->sw_format = hw_frame_ctx_in->sw_format; + hw_frame_ctx_out->width = outlink->w; + hw_frame_ctx_out->height = outlink->h; ++ ((AVVTFramesContext *)hw_frame_ctx_out->hwctx)->color_range = ((AVVTFramesContext *)hw_frame_ctx_in->hwctx)->color_range; + + err = ff_filter_init_hw_frames(avctx, outlink, 1); + if (err < 0) diff --git a/debian/patches/0052-add-vf-tonemap-videotoolbox-filter.patch b/debian/patches/0052-add-vf-tonemap-videotoolbox-filter.patch index 64157bb7584..6b0efa32cbc 100644 --- a/debian/patches/0052-add-vf-tonemap-videotoolbox-filter.patch +++ b/debian/patches/0052-add-vf-tonemap-videotoolbox-filter.patch @@ -936,7 +936,7 @@ Index: FFmpeg/libavfilter/vf_tonemap_videotoolbox.m =================================================================== --- /dev/null +++ FFmpeg/libavfilter/vf_tonemap_videotoolbox.m -@@ -0,0 +1,1148 @@ +@@ -0,0 +1,1154 @@ +/* + * Copyright (c) 2024 Gnattu OC + * @@ -1711,6 +1711,12 @@ Index: FFmpeg/libavfilter/vf_tonemap_videotoolbox.m + out_frames_ctx->width = outlink->w; + out_frames_ctx->height = outlink->h; + ++ if (ctx->range != -1) { ++ ((AVVTFramesContext *)out_frames_ctx->hwctx)->color_range = ctx->range; ++ } else { ++ ((AVVTFramesContext *)out_frames_ctx->hwctx)->color_range = ((AVVTFramesContext *)in_frames_ctx->hwctx)->color_range; ++ } ++ + ret = ff_filter_init_hw_frames(avctx, outlink, 1); + if (ret < 0) + return ret; @@ -2040,8 +2046,8 @@ Index: FFmpeg/libavfilter/vf_tonemap_videotoolbox.m + { "p", "Set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, { .i64 = AVCOL_PRI_BT709 }, -1, INT_MAX, FLAGS, .unit = "primaries" }, + { "bt709", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_PRI_BT709 }, 0, 0, FLAGS, .unit = "primaries" }, + { "bt2020", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_PRI_BT2020 }, 0, 0, FLAGS, .unit = "primaries" }, -+ { "range", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_MPEG }, -1, INT_MAX, FLAGS, .unit = "range" }, -+ { "r", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_MPEG }, -1, INT_MAX, FLAGS, .unit = "range" }, ++ { "range", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS, .unit = "range" }, ++ { "r", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS, .unit = "range" }, + { "tv", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, .unit = "range" }, + { "pc", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, .unit = "range" }, + { "limited", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, .unit = "range" }, From efdad3700d2bb6392d6702e46af976df73e66d9f Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 21 Aug 2024 04:41:48 +0800 Subject: [PATCH 2/2] avfilter/tonemap_*: default to use the same range as input --- debian/patches/0004-add-cuda-tonemap-impl.patch | 4 ++-- ...-add-bt2390-eetf-and-code-refactor-to-opencl-tonemap.patch | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/debian/patches/0004-add-cuda-tonemap-impl.patch b/debian/patches/0004-add-cuda-tonemap-impl.patch index 8fa99f51f4f..18451f27744 100644 --- a/debian/patches/0004-add-cuda-tonemap-impl.patch +++ b/debian/patches/0004-add-cuda-tonemap-impl.patch @@ -2836,8 +2836,8 @@ Index: FFmpeg/libavfilter/vf_tonemap_cuda.c + { "p", "Set color primaries", OFFSET(pri), AV_OPT_TYPE_INT, {.i64 = AVCOL_PRI_BT709}, -1, INT_MAX, FLAGS, .unit = "primaries" }, + { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_PRI_BT709}, 0, 0, FLAGS, .unit = "primaries" }, + { "bt2020", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_PRI_BT2020}, 0, 0, FLAGS, .unit = "primaries" }, -+ { "range", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_MPEG}, -1, INT_MAX, FLAGS, .unit = "range" }, -+ { "r", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_MPEG}, -1, INT_MAX, FLAGS, .unit = "range" }, ++ { "range", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "range" }, ++ { "r", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "range" }, + { "tv", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, .unit = "range" }, + { "pc", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, .unit = "range" }, + { "limited", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, .unit = "range" }, diff --git a/debian/patches/0007-add-bt2390-eetf-and-code-refactor-to-opencl-tonemap.patch b/debian/patches/0007-add-bt2390-eetf-and-code-refactor-to-opencl-tonemap.patch index 503349f60a4..f1f82c7d04d 100644 --- a/debian/patches/0007-add-bt2390-eetf-and-code-refactor-to-opencl-tonemap.patch +++ b/debian/patches/0007-add-bt2390-eetf-and-code-refactor-to-opencl-tonemap.patch @@ -2160,8 +2160,8 @@ Index: FFmpeg/libavfilter/vf_tonemap_opencl.c + { "p", "Set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, { .i64 = AVCOL_PRI_BT709 }, -1, INT_MAX, FLAGS, "primaries" }, + { "bt709", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_PRI_BT709 }, 0, 0, FLAGS, "primaries" }, + { "bt2020", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_PRI_BT2020 }, 0, 0, FLAGS, "primaries" }, -+ { "range", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_MPEG }, -1, INT_MAX, FLAGS, "range" }, -+ { "r", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_MPEG }, -1, INT_MAX, FLAGS, "range" }, ++ { "range", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS, "range" }, ++ { "r", "Set color range", OFFSET(range), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS, "range" }, + { "tv", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" }, + { "pc", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" }, + { "limited", 0, 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },