Skip to content

chroma444_videotoolbox: libplacebo's chroma scalers as a Metal filter, 4:2:0 to 4:4:4 on VideoToolbox frames (tested on macOS only) #1134

Backlog

Description

@classicjazz

What should the engine do?

A new FFmpeg filter, chroma444_videotoolbox: 4:2:0 chroma upsampled to 4:4:4 with libplacebo's chroma scalers and presets, as a Metal kernel over VideoToolbox frames.

format=p010,hwupload,chroma444_videotoolbox=preset=high_quality

Inputs are nv12, p010 and yuv420p; outputs nv24, p410 or p416, never shallower than the input. The FFmpeg patch, the tests and the timing scripts are attached (chroma444-videotoolbox-files.zip), written with Claude Code. As with #1102 and #1108, I'm not a software engineer and it has only run on a Mac, so please treat it as material to use, rework or replace, not as a PR.

Built on the template from #1102: the bwdif_videotoolbox wrapper at 1848189d28 (FFmpeg PR 26276). One commit on top of 26276.

FFmpeg has no chroma upsampling filter of its own: on the CPU it's scale (libswscale) or zscale (zimg), so there is no CPU filter to twin. The five filters I'm filing:

Filter Kind Issue
deblock_videotoolbox Exact twin of CPU deblock #DEBLOCK
deband_videotoolbox Exact twin of CPU deband #DEBAND
debanditer_videotoolbox libplacebo's deband #DEBANDITER
dither_videotoolbox libplacebo's dither #DITHER
chroma444_videotoolbox libplacebo's chroma scalers this one

Adding the filter to a build changes nothing for current hosts.

Motivating media or use case

#957 lists chroma upsampling among the engine's video processing stages. The frame chain upsamples chroma bilinearly inside its NV12-to-RGBA conversion (FrameChain.swift, AetherEngine 1257b90d). A better chroma scaler matters most where chroma is soft to begin with: 4:2:0 SD broadcast upscaled to a 4K panel, and saturated edges (graphics, sports tickers, red on black).

Two ways the engine could use this, neither decided:

  • As a graph filter, where a route presents 4:4:4. 10-bit 4:4:4 (444YpCbCr10BiPlanarVideoRange, the p410 layout) already reaches the display layer in the FEL composer's experimental 4:4:4 mode, measured on an A12 (docs/dolby-vision-fel.md §4.7). That step-up uses co-sited means, so it is a second place a better scaler could go. Not checked: 8-bit nv24 on the display layer, and whether the HDMI link keeps 4:4:4.
  • As the stage's math. The kernel's sampling functions (chroma_quad_ortho, chroma_quad_polar) take a loader and write nothing, so a fused engine stage could call them straight into its YCbCr-to-RGB conversion, with no 4:4:4 texture written. This filter is then the tested reference.

What it costs, per frame, one filter thread, wall time / CPU time (Testing 3). swscale does the same job on the CPU with the same scaler names but a different implementation:

Input swscale (CPU time) chroma444_videotoolbox
1080 NV12, bilinear 2.0 ms 0.35 ms / 0.35 ms
1080 NV12, lanczos (the default preset) 3.3 ms 0.7 ms / 0.3 ms
1080 NV12, ewa_lanczossharp (high_quality) (no swscale equivalent) 1.15 ms / 0.6 ms
2160 P010, lanczos 14.8 ms 0.5 to 0.6 ms / 0.5 ms
2160 P010, ewa_lanczossharp 1.6 to 1.7 ms / 0.3 to 0.4 ms

chroma444-videotoolbox-files.zip

Area

Performance

Host app / integration context

No response

Would you be willing to open a PR?

No

Activity

  1. added
    enhancement
    on Nov 25, 2026
  2. quint-aether commented on Nov 25, 2026

    @quint-aether

    This is a proposal for the opt-in video processing stage in #957, and is part of the same set of filters as #1102 and #1108.

    The table in the description still has placeholder numbers. These are the other four filter issues:

    Automated triage by Quint (Claude). A maintainer reads every issue and will follow up.

  3. classicjazz commented on Nov 25, 2026

    @classicjazz
    Author

    Everything below was measured on an Apple M1 Max, macOS 27.2.1. Nothing has run on an Apple TV, iPhone or iPad.

    What's attached

    File What it is
    chroma444-videotoolbox.patch FFmpeg patch, one commit. Applies to master with PR 26276 at 1848189d28
    tests/ The CPU-reference anchors and fate reference check, the 48-case end-to-end against a double-precision reference with its controls and fixtures script, the commands and odd-size checks, the kernel test and bench, the libplacebo comparisons, the availability compile check, results
    timing/ timing.sh (all five filters), extra.sh, results
    README.txt Layout, build and run lines, expected results
    File in the patch Lines What
    libavfilter/metal/vf_chroma444_videotoolbox.metal 294 The kernel
    libavfilter/vf_chroma444_videotoolbox.m 633 The wrapper
    libavfilter/chroma_weights.{c,h} 464, 153 libplacebo's filter definitions, exact weights per phase
    doc/filters.texi +61 New section
    tests/fate/filter-video.mak, refs +35, 5 refs Five fate-hw tests
    configure, Makefile, allfilters.c, Changelog, version.h +7 Registration; libavfilter minor 13 to 14

    What it is

    • Source: libplacebo src/filters.c, src/shaders/sampling.c, src/renderer.c, LGPL-2.1-or-later, checked against libplacebo 7.387.0. Separable filters as pl_shader_sample_ortho2, polar (EWA) filters as pl_shader_sample_polar, both with libplacebo's anti-ringing; the sharpened EWA blur constants are Nicolas Robidoux's.
    • Presets: fast (bilinear), default (lanczos), high_quality (ewa_lanczossharp); filter= takes any of twelve libplacebo scaler names.
    • Siting: the frame's, and left when the frame has none, as libplacebo does; chroma_loc overrides it.
    • One thread per input chroma sample, writing the 2x2 output block it covers, so both planes are one dispatch. No dither: the result is rounded to the output's grid, and format=p416 keeps the precision for a later dither (#DITHER).
    • Output attachments: the chroma location and chroma subsampling attachments are removed, since the output is 4:4:4.
    • No runtime commands: the options are fixed at init (Testing 4).

    The template points

    Point Here
    Availability macos(10.12), ios(10.0). The earlier version claimed macos(10.11), ios(8.0) and failed the iOS 8.0 build on setBytes and MTLFunctionConstantValues. 0 errors at iOS 8.0, tvOS 12.0, macOS 10.11 (x86_64)
    Context The template's typed context with its computed size and static_assert (the earlier version took sizeof of the annotated type outside an availability check)
    Nil checks, one command buffer, formats and texture formats in config_output Yes
    Output attachments Chroma location and subsampling removed after the propagate. Not verified: hwupload does not set these keys, so the ffmpeg CLI can't show the removal; it needs a probe that sets them on the input
    P010 unpacking Multiplies by 2^-shift, computed exactly on the CPU; no GPU reciprocal
    Copyright, const externs, every parameter read Yes

    Found by the tvOS 12 build check, fixed here: the earlier version called dispatchThreads under @available(..., tvOS 11.0, *), but the SDK declares it tvos(14.5). It also skipped the GPU check the shared helper makes (supportsFamily:MTLGPUFamilyCommon3), so on a GPU without non-uniform threadgroups it would have issued a dispatch the hardware can't run. The dispatch now goes through ff_metal_compute_encoder_dispatch like the other Metal filters. Output unchanged.

    Testing

    1. fate-hw, all five references computed on the CPU. nearest at center siting copies each chroma sample to its 2x2 block, as scale=flags=neighbor does: NV12, P010, yuv420p written at 16 bits, and NV12 at an odd luma size (159x119, against the 160x120 CPU result cropped). Bilinear differs from nearest on all 10 frames, so these are not trivial. And constant chroma through ewa_lanczossharp with anti-ringing must come back unchanged (also checked for lanczos with antiring=0.8 and for ewa_lanczos4sharpest). All identical.

    2. Against a double-precision reference of the same sampling, inside ffmpeg. 48 cases: bilinear, catmull_rom, lanczos, ewa_lanczos, ewa_lanczossharp, ewa_lanczos4sharpest; left, center and topleft siting; anti-ringing 0 and 0.5; nv12, p010 and yuv420p in; nv24, p410 and p416 out; an odd chroma size (722x482, chroma 361x241). 48 of 48 PASS: every luma sample identical, no chroma sample off by more than 1, and the off-by-1 ones at most 0.028 codes from a rounding tie (float32 against double). The quick kernel test (48,384 cases) also passes, with 0 samples off by more than 1.

    The comparison can fail. A lanczos output checked as catmull_rom, as center siting or as anti-ringing 0.5 fails on 1.5 to 6.7 million of its 8.3 million chroma samples; these three controls are at the end of run_e2e.sh.

    The reference uses the patch's own weights (chroma_weights.c). Those are checked against libplacebo 7.387.0's pl_filter_sample and pl_filter_generate by weights_vs_libplacebo.c: all 12 filters match exactly (radius, first zero, weights).

    Odd luma sizes (odd-size.sh): at 37x11, 39x9, 3x3 and 1x1, nearest/center matches CPU scale=flags=neighbor on the even frame cropped after, and all 12 filters at three sitings run, with identical output across two runs. Bilinear at 37x11 differs, as the control.

    3. Speed (timing/). Raw input, one filter thread, best of three, minus reading and hwupload (GPU) or reading (CPU), CPU time as user plus system. Two runs:

    Chain Wall ms CPU ms
    1080 NV12: swscale to yuv444p, bilinear 1.67 / 1.67 2.04 / 2.03
    chroma444_videotoolbox=preset=fast 0.35 / 0.35 0.35 / 0.34
    swscale to yuv444p, lanczos 2.90 / 2.85 3.28 / 3.23
    chroma444_videotoolbox (lanczos) 0.72 / 0.73 0.32 / 0.32
    chroma444_videotoolbox=preset=high_quality 1.14 / 1.17 0.56 / 0.59
    2160 P010: swscale to yuv444p10, lanczos 14.74 / 14.64 14.86 / 14.78
    chroma444_videotoolbox (lanczos) 0.51 / 0.60 0.53 / 0.54
    chroma444_videotoolbox=preset=high_quality 1.57 / 1.68 0.30 / 0.42

    The 1080 NV12 input is an off-air ATSC 1.0 capture; any 1080 NV12 will do, since the timing does not depend on content. The reader runs in its own thread, so wall minus baseline is a lower bound. Lanczos measures less wall time at 2160 than at 1080; I haven't checked why.

    4. Runtime commands and option bounds (commands.sh, 10 frames of testsrc2). No runtime commands: antiring, filter, preset, chroma_loc and format sent with sendcmd get Function not implemented, enable gets Not yet implemented, a following ping answers, and 0 of 10 frames change. Out-of-range antiring (-0.5, 1.5), chroma_loc (-1, 7), preset and format, and an unknown filter, are refused at init. The same options set at init change 10 of 10 frames, as the control.

    Open question

    fate-hw for the interpolating filters. Only nearest and flat chroma can be pinned to CPU references. GPU-pinned CRCs for lanczos or EWA would depend on the GPU family's float rounding. Add them, or keep the outside-FATE comparison? The same question is in #DEBANDITER.

    Open tasks

    Shared by all five filters (the same list in #DEBLOCK, #DEBAND, #DEBANDITER, #DITHER and this one):

    • The shared Metal helper (rule 4 from bwdif_videotoolbox: a Metal (GPU) bwdif whose output matches CPU bwdif exactly (tested on macOS only) #1102). Each filter repeats about 150 to 200 lines of device, library, pipeline, texture cache and output pool setup, as bwdif_videotoolbox does. You planned the helper in libavfilter/metal/ as a separate factoring patch once 26244 and 26276 have settled; these filters would move onto it afterwards, mostly mechanically. Not done here, on purpose.
    • The metallib's Metal language version. FFmpeg builds the metallib with xcrun -sdk macosx metal and no -std, so the oldest OS that can load the library comes from the SDK, not from the API_AVAILABLE annotations. A kernel that uses newer features (deblock's SIMD-group attributes need MSL 2.0, macOS 10.13 / iOS 11) fails cleanly in newLibraryWithData on an older OS, with an error. A -std per target, or annotations that follow the language version, would settle it for every Metal filter.
    • Timeline and the pass-through fix. None of these filters supports enable: a timeline bypass would hand on input frames with the input's frames context, the pre-existing problem from Fairy's review that you're fixing separately for yadif and bwdif. Timeline support could follow that fix.
    • FFmpegBuild. The patches are on master with 26276 and don't apply to n8.1 as they are; there is no FFmpegBuild diff with them.
    • Overlapping lines. All five and bwdif add a Changelog line and the same libavfilter minor bump, so the patches touch the same lines there. Stacked on the others, this one also conflicts in doc/filters.texi with deblock and deband (sections after bwdif's) and in tests/fate/filter-video.mak with debanditer and dither.
    • Engine integration. Where each filter would run (in a graph, or as the math behind a FrameChain stage under Opt-in video processing stage (HDR tone mapping, debanding, dithering, chroma upsampling, 3D LUT) on the software path #957), what option turns it on, and what the stats should show. Not decided, and not proposed here.
    • Devices. Nothing has run on an Apple TV, iPhone or iPad (see "What I can test" below).
    • Docs build. The doc/filters.texi sections are unbuilt (no makeinfo here).

    This filter:

    • GPU-pinned CRCs for the interpolating filters (the open question above).
    • Whether the display layer takes nv24, and whether HDMI keeps 4:4:4. The 10-bit layout already reaches the display layer (the FEL 4:4:4 mode); this decides whether the graph filter is usable as is, or only its math in a fused stage.
    • A probe for the removed attachments (set both keys on the input, read them back from the output), as for the field keys on 26244.
    • Re-run since the port: the full kernel test, lp_compare.c (against libplacebo's own GPU scalers, needs Vulkan/MoltenVK), and the kernel bench.
    • The GPU timing log. The wrapper logs its mean and max GPU time every 300 frames at verbose level, to choose presets on a device. It is a development aid; whether it stays is open.
  4. superuser404notfound commented on Nov 26, 2026

    @superuser404notfound
    Owner

    Thanks for the last one of the five. Same base as the others, 26276 round 4 (6efb91b844), on an M1.

    What I checked

    • The patch applies cleanly, builds without warnings, and the five fate-hw tests pass.
    • Against libplacebo's renderer, not only its samplers. That is your lp_compare item, done a different way. libplacebo 7.360.1 and MoltenVK happen to be on this Mac, so I wrote a separate harness. It hands the same 4:2:0 frame to pl_render_image as three planes in the RGB system, so the renderer runs no matrix and only scales the two subsampled planes with plane_upscaler. The siting comes from pl_frame_set_chroma_location, so the geometry is libplacebo's own and neither yours nor mine. The same frame goes through the filter with format=p416 and nv24.
    • 271 cases: all 11 scalers, all six sitings, anti-ringing 0 and 0.6, noise and hard edges at 128x72, plus 1920x1080 for lanczos, ewa_lanczossharp, bilinear and ewa_lanczos4sharpest. The largest difference is 0.34 codes (8-bit units), and luma is untouched. That much is libplacebo's own noise. Its FBOs are fp16, so even the luma plane, which it doesn't filter, comes back up to 0.125 codes off. On top of that comes its weight LUT. The controls do differ: a wrong siting, or catmull_rom checked as lanczos, gives a mean of 1.9 to 21.6 codes.
    • One exception, and it is libplacebo's. mitchell with anti-ringing differs by up to 8 codes on hard edges at the four sitings where chroma sits on a luma row (topleft, top, bottomleft, bottom). At a phase of exactly 0, the two "centre" taps depend on whether fract(pos * size - 0.5) comes out as 0 or as 1 minus a hair. libplacebo decides that row by row. Rows 2, 4, 6, 18, 22 and so on go one way, the rest the other. Your port always takes the sample and the one after it, which matches most rows. When I forced the other side, the mismatch moved to the other rows. Only mitchell shows it, because it is the only separable filter here that has negative lobes and doesn't interpolate at phase 0. For the others the clamp only ever sees the exact sample. I wouldn't change anything, maybe a sentence in the kernel comment.
    • Your attachment item. A probe sets chroma location and subsampling on every input frame, and neither arrives on any output. With your three CVBufferRemoveAttachment lines disabled as a control, both arrive.
    • The wrapper compiles with no availability errors at iOS 8.0, tvOS 12.0 and macOS 10.11.

    Fixes from the later bwdif rounds

    The same three as in #1131 to #1133, plus one the range fallback brings with it.

    1. CVBufferRemoveAllAttachments before propagating. Six NV12 frames, each output freed right away so the pool hands back the same buffer, only the first input tagged HLG. Before, all six outputs carried HLG. After, only the first.
    2. The range falls back to the link's color_range when the input frames context leaves it unspecified. On a full-range link the pool was 444v and x444, now 444f and xf44.
    3. With that fallback, format=p416 on a full-range link failed on the first frame, because CoreVideo has no full-range 16-bit 4:4:4 format (pool error -6680). Before the fallback it went quietly into a video-range sv44 buffer. It is now refused at configuration, and the docs say so.
    4. Each fate-hw test is gated on the CoreVideo formats it reads and writes. That is 8-bit 4:4:4 for the nv12 ones, 10-bit 4:2:0 and 4:4:4 for p010, and 16-bit 4:4:4 for p416.

    FATE passes again with these in place. They sit as a second commit on top of yours on a local branch.

    One thing I left for you

    depth_mul is a plain shift for every plane. That is right for limited range. On full range, white only stays white if luma is scaled by (2^out - 1) / (2^in - 1). A full-range NV12 white of 255 comes out as 1020 in p410, where it should be 1023 (and as 65280 in p416 before fix 3 refused that case). swscale turns the same input into luma 1023 and still shifts chroma (128 to 512, 255 to 1020), so neutral grey stays exactly neutral. I'd do the same. Luma gets a multiplier from the frame's range, or from the link range of fix 2 when the frame has none, and chroma stays as it is. It changes ChromaParams and your test tools, so I didn't do it.

    Your other open items

    • GPU-pinned CRCs for the interpolating filters: same answer as in debanditer_videotoolbox: libplacebo's deband as a Metal filter for VideoToolbox frames (tested on macOS only) #1131. Keep them out of FATE. The nearest and flat anchors plus the comparison above are enough.
    • nv24 on the display layer and 4:4:4 over HDMI: I haven't checked either. Today FrameChain reads chroma with a bilinear sampler on its way to half-float RGB (frameChainRGB), so your second option fits better. The sampling functions would replace that read, and this filter stays as their tested reference. That read also assumes center siting for every frame, so the swap would bring the frame's siting along for free.
    • The GPU timing log: useful for the device runs, but I'd take it out before anything goes upstream.

    What happens next

    Same as the other four. Nothing goes upstream before 26276 is merged and the shared Metal helper is in. Then the filters move onto it and go up one at a time, with you as co-author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: videoenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions