Commit 9cffbad201 for ffmpeg

commit 9cffbad201fbad29d8e8a4cc06fa98d178a325b5
Author: Zhao Zhili <quinkblack@foxmail.com>
Date:   Sat Nov 28 01:01:19 2026 +0800

    avfilter/vf_hwupload: allocate VideoToolbox frames in the input range

    CoreVideo fixes the color range of a buffer at allocation, as part of
    its pixel format, and VideoToolbox consumers take the range from that
    format. The frames context was created without a range, so for formats
    that exist in both ranges its pool allocated video range buffers. Full
    range input was uploaded into buffers labelled video range, and the
    VideoToolbox encoder signalled limited range for it.

    The range of the samples is only known where they are written into the
    buffers, so take it from the input link here, when the format exists in
    that range. A frame in another range than its link, from a filter that
    ignores the negotiated range, would then be copied into a buffer
    labelled with the wrong range, so reject it instead.

    Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>

diff --git a/libavfilter/vf_hwupload.c b/libavfilter/vf_hwupload.c
index ec3d963fd0..7a062fb027 100644
--- a/libavfilter/vf_hwupload.c
+++ b/libavfilter/vf_hwupload.c
@@ -23,6 +23,10 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"

+#if CONFIG_VIDEOTOOLBOX
+#include "libavutil/hwcontext_videotoolbox.h"
+#endif
+
 #include "avfilter.h"
 #include "filters.h"
 #include "formats.h"
@@ -157,6 +161,16 @@ static int hwupload_config_output(AVFilterLink *outlink)
     ctx->hwframes->width     = inlink->w;
     ctx->hwframes->height    = inlink->h;

+#if CONFIG_VIDEOTOOLBOX
+    /* Keep the default for a format that CoreVideo lacks in this range. */
+    if (ctx->hwframes->format == AV_PIX_FMT_VIDEOTOOLBOX &&
+        av_map_videotoolbox_format_from_pixfmt2(ctx->hwframes->sw_format,
+                                                inlink->color_range == AVCOL_RANGE_JPEG)) {
+        AVVTFramesContext *vt_frames = ctx->hwframes->hwctx;
+        vt_frames->color_range = inlink->color_range;
+    }
+#endif
+
     if (avctx->extra_hw_frames >= 0)
         ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;

@@ -188,6 +202,29 @@ static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
     if (input->format == outlink->format)
         return ff_filter_frame(outlink, input);

+#if CONFIG_VIDEOTOOLBOX
+    if (outlink->format == AV_PIX_FMT_VIDEOTOOLBOX) {
+        const AVVTFramesContext *vt_frames = ctx->hwframes->hwctx;
+        enum AVColorRange range = input->color_range;
+
+        /* The upload copies samples as they are, so they must be in the
+         * range of the buffer whenever CoreVideo has a format for theirs.
+         */
+        if (vt_frames->color_range != AVCOL_RANGE_UNSPECIFIED &&
+            range != AVCOL_RANGE_UNSPECIFIED &&
+            range != vt_frames->color_range &&
+            av_map_videotoolbox_format_from_pixfmt2(ctx->hwframes->sw_format,
+                                                    range == AVCOL_RANGE_JPEG)) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "Frame color range %s differs from the negotiated range %s.\n",
+                   av_color_range_name(range),
+                   av_color_range_name(vt_frames->color_range));
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+    }
+#endif
+
     output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!output) {
         av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");