Repository navigation
Description
What should the engine do?
A new FFmpeg filter, debanditer_videotoolbox: libplacebo's pl_shader_deband() and the pcg3d PRNG it uses, as a Metal kernel over VideoToolbox frames, with libplacebo's parameters, defaults and render presets.
format=nv12,hwupload,debanditer_videotoolbox=grain=0:format=p010
GPU debanding in FFmpeg today exists only through vf_libplacebo, which needs Vulkan (MoltenVK on Apple platforms). The FFmpeg patch, the tests and the timing scripts are attached (debanditer-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.
Boundary: nothing has run on an Apple TV, iPhone or iPad, and build.sh has not been run. The patch is on master with 26276 and does not apply to n8.1, so there is no FFmpegBuild diff.
Built on the template from #1102: the bwdif_videotoolbox wrapper at 1848189d28 (FFmpeg PR 26276). One commit on top of 26276.
The name. It is not a GPU version of deband: libplacebo runs several iterations with a growing radius and draws new sample positions every frame, and its output differs. Both sample at random offsets, so the name says what differs (iterations, which is also its first option) instead of the library. Credit is in the headers and docs. The exact twin of CPU deband is #DEBAND; the two are meant as a pair. The five filters:
| Filter | Kind | Issue |
|---|---|---|
deblock_videotoolbox |
Exact twin of CPU deblock |
#DEBLOCK |
deband_videotoolbox |
Exact twin of CPU deband |
#DEBAND |
debanditer_videotoolbox |
libplacebo's deband | this one |
dither_videotoolbox |
libplacebo's dither | #DITHER |
chroma444_videotoolbox |
libplacebo's chroma scalers | #CHROMA444 |
Adding the filter to a build changes nothing for current hosts.
debanditer-videotoolbox-files.zip
Motivating media or use case
Banding on low-bitrate IPTV and FAST channels and on 8-bit SDR streams. #957's plan has debanding as a later stage of the engine's processing, with its options shaped after libplacebo's (strength, iterations, grain). That stage would be a FrameChain stage, which works on RGBA textures; this filter works on the YUV planes in a libavfilter graph, so it is not that stage. It is libplacebo's algorithm in Metal, already checked against a C translation, so it could run in a graph, or serve as the reference a FrameChain stage is checked against.
Debanding without dithering. The design keeps the smoothing: the average of four 8-bit samples is a multiple of 0.25, so with grain=0 an NV12 input written to P010 (format=p010) keeps all of it, and one dither at the end of the chain (#DITHER) reduces to the output depth once.
What it costs, per 1080 frame, one filter thread, CPU time / wall time (Testing 3):
| Settings | debanditer_videotoolbox |
|---|---|
preset=high_quality (libplacebo's defaults, grain 4) |
0.3 ms / 0.5 ms |
grain=0:format=p010 |
0.3 ms / 0.45 ms |
iterations=4:grain=0 |
0.35 to 0.6 ms / 1.3 ms |
There is no CPU version to compare: libplacebo is GPU-only, and vf_libplacebo needs Vulkan. For scale, CPU deband (a different algorithm, #DEBAND) costs 9.5 ms of CPU on the same frames.
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 maintainer to review. It is related to #1130 (
deband_videotoolbox, the exact CPU twin this issue calls #DEBAND), to #957 (the opt-in processing stage on the software path) and to #1102, thebwdif_videotoolboxtemplate it builds on.The table still has placeholders (#DEBLOCK, #DITHER, #CHROMA444). If you file those issues, adding their numbers here would keep the five filters linked.
Automated 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
debanditer-videotoolbox.patch1848189d28tests/deband_check.c(a C translation ofpl_shader_deband), the 24-case matrix (with two controls and three refusals) and fixtures,commands.sh(runtime commands and option bounds), the kernel bench, full-range/HDR and platform scripts, resultstiming/timing.sh(all five filters),extra.sh, resultsREADME.txtlibavfilter/metal/vf_debanditer_videotoolbox.metallibavfilter/vf_debanditer_videotoolbox.mdoc/filters.texidebandtests/fate/filter-video.mak, refsfate-hwtestsconfigure,Makefile,allfilters.c,Changelog,version.hWhat it is
src/shaders/sampling.candsrc/shaders.c, LGPL-2.1-or-later, read at commitcb1ad12c.sampling.ccarries no copyright line; the credit is Niklas Haas and the libplacebo contributors, and pcg3d is from Jarzynski and Olano (JCGT 2020).preset(libplacebo'sfastanddefaultleave debanding off;high_quality, the default, usesPL_DEBAND_DEFAULTS: iterations 1, threshold 3.0, radius 16, grain 4.0),iterations,threshold,radius,grain,temporal(on by default),format(a deeper output),fuse(off by default).iterations,threshold,radiusandgraintake -1 for the preset's value; other negative values are refused at init, as libplacebo refuses them.iterationsandgrainare function constants of the pipeline, and pass-through is chosen when the output link is configured. A command is refused before anything changes (Testing 4).fast::cosandfast::sin, as GLSL does on most GPUs.The template points
macos(10.12),ios(10.0)forMTLFunctionConstantValues. 0 errors at iOS 8.0, tvOS 12.0, macOS 10.11 (x86_64)config_outputfate-hwwith CPU referencesconstexterns, every parameter read1.0f / in_shifton the GPUTesting
1.
fate-hw, CPU references. A debanded frame has no exact CPU reference: the sample positions come from GPU trigonometry, and about one sample in two million lands on another texel than libm would pick. What can be pinned exactly: withthreshold=0the kernel samples, averages and compares on every iteration but keeps only an average equal to the sample, so the output must be the source. Four tests on that: NV12 to P010 with 4 iterations, the fused kernel, P010 input (the integer unpack), three-plane yuv420p. All identical to the source; at the default threshold all 10 frames of each change, so the kernel runs. These catch unpacking, depth conversion, out-of-bounds sampling and plumbing, not a wrong average or threshold rule; test 2 covers those.2. Against a C translation of
pl_shader_deband, every sample, insideffmpeg. 24 cases: nv12, p010, yuv420p, nv16 to p210, nv24 to p410, p216, p416, a 37x11 odd size (made at 38x12 and scaled, sincetestsrc2rounds odd sizes down), 1 to 4 iterations, grain on and off, temporal on and off, and 30 frames of an off-air ATSC 1.0 1080i MPEG-2 capture. 24 of 24 PASS,real_mismatches=0. The remaining differences are all sampling positions within 1e-3 of a texel edge (tie_mismatches, at most 53 samples in 93 million). Two controls show the comparison can fail: the GPU run atthreshold=6against a reference told 3, and atradius=8against 16, both differ. The matrix also checks thatthreshold=-0.5,radius=-0.3andgrain=-0.2are refused.3. Speed (
timing/). Raw input, one filter thread, best of three, minus reading andhwupload, CPU time as user plus system. Two runs, per 1080 frame:preset=high_qualitygrain=0:format=p010iterations=4:grain=0preset=high_qualityThe reader runs in its own thread, so wall minus baseline is a lower bound. Kernel-only GPU time from 11-22: about 0.12 ms per iteration for a 1080 4:2:0 frame.
4. Runtime commands and option bounds (
tests/commands.sh, 10 frames of a softtestsrc2).threshold 5anditerations 0sent withsendcmdgetFunction not implemented,enable 0getsNot yet implemented in FFmpeg, patches welcome(libavfilter checks the timeline flag first), and the output is identical to a run with no command; apingafterwards answers. As control,threshold=5at init changes 10 of 10 frames. Negative values other than -1 are refused at init;-1everywhere andfuse=0give the default output.Open question
fate-hwfor the smoothing itself. Only the threshold-0 identity can be pinned to a CPU reference. GPU-pinned CRCs for real smoothing would depend on the GPU family's trigonometry. Should they be added anyway, or is the outside-FATE comparison (test 2) enough? The same question applies to #CHROMA444's interpolating filters.Open tasks
Shared by all five filters (the same list in #DEBLOCK, #DEBAND, this one, #DITHER and #CHROMA444):
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.fate-hwblock right after bwdif's, so the patches touch the same lines in Changelog,version.handtests/fate/filter-video.mak. Theconfigurelines of deband, debanditer and deblock also sit side by side.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:
pl_shader_deband; a run against libplacebo's own shader needs Vulkan.fullrange-hdr.sh(full range and the HDR grain scaling),platform-compile.sh, and the kernel bench.superuser404notfound commentedon Nov 25, 2026
Thanks for this one too. I went through it on an M1 today, on the same base as #1130.
What I checked
6efb91b844), and the four fate-hw tests pass.pl_shader_debandandsh_prng: the pcg3d steps, the seed from the plane coordinates and the frame index, 2 pi as%fprints it, the nearest-neighbour clamp-to-edge sampling, the threshold and grain in normalized units, and the renderer's neutral values. I found nothing that differs.deband_check: 24 of 24 pass withreal_mismatches=0, both controls differ, all three negative values are refused. I had no ATSC capture, so the 1080 case ran on 30 frames of a 1080p H.264 film clip instead, once as in your matrix and once withpreset=high_qualitystraight to nv12. Both pass, at most 35 tie mismatches in 93 million samples.Three fixes from the later bwdif rounds
These are the same three as in #1130.
CVBufferRemoveAllAttachmentsbeforeCVBufferPropagateAttachments. Same probe as in deband_videotoolbox: a Metal (GPU) deband whose output matches CPU deband exactly (tested on macOS only) #1130: six NV12 frames, each output freed right away so the pool returns the same pixel 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. This one matters more here than in deband_videotoolbox: a Metal (GPU) deband whose output matches CPU deband exactly (tested on macOS only) #1130, because the range doesn't only pick the pool's420vor420f. It also sets the code scale, the grain's neutral black and the output scale. A full-range NV12 white (255) throughhwuploadandformat=p010came out as 1020. With the fallback it is 1023, and the pool is420f.HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE, like the bwdif ones.The matrix and the fate tests pass again with all three in place. They sit as a second commit on top of yours on a local branch.
Your open question
I'd leave GPU-pinned CRCs for the smoothing out of FATE. A FATE reference has to hold on every machine that runs
fate-hw, and that includes Intel and AMD Macs and later Apple GPUs whose trig may round differently. A ref that only one GPU family can match would turn into a flaky test. The threshold-0 identity in FATE plus your outside comparison against the C translation is enough. The same answer goes for the interpolating filters in #1134.What happens next
Same as #1130. 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.
On the engine side, which debander a later #957 stage uses is still open. The next step there is a picture comparison of this filter against #1130 on real banding.