Repository navigation
Description
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, thep410layout) 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-bitnv24on 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
quint-aether commentedon Nov 25, 2026
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:
deblock_videotoolbox: deblock_videotoolbox: a Metal (GPU) deblock whose output matches CPU deblock exactly (tested on macOS only) #1132deband_videotoolbox: deband_videotoolbox: a Metal (GPU) deband whose output matches CPU deband exactly (tested on macOS only) #1130debanditer_videotoolbox: debanditer_videotoolbox: libplacebo's deband as a Metal filter for VideoToolbox frames (tested on macOS only) #1131dither_videotoolbox: dither_videotoolbox: libplacebo's dither as a Metal filter for VideoToolbox frames, exact integer output (tested on macOS only) #1133Automated triage by Quint (Claude). A maintainer reads every issue and will follow up.
classicjazz commentedon Nov 25, 2026
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
chroma444-videotoolbox.patch1848189d28tests/timing/timing.sh(all five filters),extra.sh, resultsREADME.txtlibavfilter/metal/vf_chroma444_videotoolbox.metallibavfilter/vf_chroma444_videotoolbox.mlibavfilter/chroma_weights.{c,h}doc/filters.texitests/fate/filter-video.mak, refsfate-hwtestsconfigure,Makefile,allfilters.c,Changelog,version.hWhat it is
src/filters.c,src/shaders/sampling.c,src/renderer.c, LGPL-2.1-or-later, checked against libplacebo 7.387.0. Separable filters aspl_shader_sample_ortho2, polar (EWA) filters aspl_shader_sample_polar, both with libplacebo's anti-ringing; the sharpened EWA blur constants are Nicolas Robidoux's.fast(bilinear),default(lanczos),high_quality(ewa_lanczossharp);filter=takes any of twelve libplacebo scaler names.chroma_locoverrides it.format=p416keeps the precision for a later dither (#DITHER).The template points
macos(10.12),ios(10.0). The earlier version claimedmacos(10.11),ios(8.0)and failed the iOS 8.0 build onsetBytesandMTLFunctionConstantValues. 0 errors at iOS 8.0, tvOS 12.0, macOS 10.11 (x86_64)static_assert(the earlier version tooksizeofof the annotated type outside an availability check)config_outputhwuploaddoes not set these keys, so the ffmpeg CLI can't show the removal; it needs a probe that sets them on the input2^-shift, computed exactly on the CPU; no GPU reciprocalconstexterns, every parameter readFound by the tvOS 12 build check, fixed here: the earlier version called
dispatchThreadsunder@available(..., tvOS 11.0, *), but the SDK declares ittvos(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 throughff_metal_compute_encoder_dispatchlike the other Metal filters. Output unchanged.Testing
1.
fate-hw, all five references computed on the CPU.nearestat center siting copies each chroma sample to its 2x2 block, asscale=flags=neighbordoes: 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 throughewa_lanczossharpwith anti-ringing must come back unchanged (also checked for lanczos withantiring=0.8and forewa_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'spl_filter_sampleandpl_filter_generatebyweights_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 CPUscale=flags=neighboron 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 andhwupload(GPU) or reading (CPU), CPU time as user plus system. Two runs:chroma444_videotoolbox=preset=fastchroma444_videotoolbox(lanczos)chroma444_videotoolbox=preset=high_qualitychroma444_videotoolbox(lanczos)chroma444_videotoolbox=preset=high_qualityThe 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 oftestsrc2). No runtime commands:antiring,filter,preset,chroma_locandformatsent withsendcmdgetFunction not implemented,enablegetsNot yet implemented, a followingpinganswers, and 0 of 10 frames change. Out-of-rangeantiring(-0.5, 1.5),chroma_loc(-1, 7),presetandformat, and an unknownfilter, are refused at init. The same options set at init change 10 of 10 frames, as the control.Open question
fate-hwfor 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):
bwdif_videotoolboxdoes. You planned the helper inlibavfilter/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.xcrun -sdk macosx metaland no-std, so the oldest OS that can load the library comes from the SDK, not from theAPI_AVAILABLEannotations. A kernel that uses newer features (deblock's SIMD-group attributes need MSL 2.0, macOS 10.13 / iOS 11) fails cleanly innewLibraryWithDataon an older OS, with an error. A-stdper target, or annotations that follow the language version, would settle it for every Metal filter.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.doc/filters.texiwith deblock and deband (sections after bwdif's) and intests/fate/filter-video.makwith debanditer and dither.FrameChainstage 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.doc/filters.texisections are unbuilt (nomakeinfohere).This filter:
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.lp_compare.c(against libplacebo's own GPU scalers, needs Vulkan/MoltenVK), and the kernel bench.superuser404notfound commentedon Nov 26, 2026
Thanks for the last one of the five. Same base as the others, 26276 round 4 (
6efb91b844), on an M1.What I checked
lp_compareitem, 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 topl_render_imageas three planes in the RGB system, so the renderer runs no matrix and only scales the two subsampled planes withplane_upscaler. The siting comes frompl_frame_set_chroma_location, so the geometry is libplacebo's own and neither yours nor mine. The same frame goes through the filter withformat=p416andnv24.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.CVBufferRemoveAttachmentlines disabled as a control, both arrive.Fixes from the later bwdif rounds
The same three as in #1131 to #1133, plus one the range fallback brings with it.
CVBufferRemoveAllAttachmentsbefore 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.color_rangewhen the input frames context leaves it unspecified. On a full-range link the pool was444vandx444, now444fandxf44.format=p416on 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-rangesv44buffer. It is now refused at configuration, and the docs say so.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_mulis 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 inp410, where it should be 1023 (and as 65280 inp416before 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 changesChromaParamsand your test tools, so I didn't do it.Your other open items
nv24on the display layer and 4:4:4 over HDMI: I haven't checked either. TodayFrameChainreads 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.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.