Skip to content

debanditer_videotoolbox: libplacebo's deband as a Metal filter for VideoToolbox frames (tested on macOS only) #1131

Backlog

Description

@classicjazz

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

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

    @quint-aether

    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, the bwdif_videotoolbox template 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.

  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
    debanditer-videotoolbox.patch FFmpeg patch, one commit. Applies to master with PR 26276 at 1848189d28
    tests/ The threshold-0 anchors and fate reference check, deband_check.c (a C translation of pl_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, 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_debanditer_videotoolbox.metal 216 The kernel, per plane and fused (both planes of a two-plane frame)
    libavfilter/vf_debanditer_videotoolbox.m 728 The wrapper
    doc/filters.texi +70 New section, and an anchor on deband
    tests/fate/filter-video.mak, refs +28, 4 refs Four fate-hw tests
    configure, Makefile, allfilters.c, Changelog, version.h +7 Registration; libavfilter minor 13 to 14

    What it is

    • Source: libplacebo src/shaders/sampling.c and src/shaders.c, LGPL-2.1-or-later, read at commit cb1ad12c. sampling.c carries no copyright line; the credit is Niklas Haas and the libplacebo contributors, and pcg3d is from Jarzynski and Olano (JCGT 2020).
    • Options: preset (libplacebo's fast and default leave debanding off; high_quality, the default, uses PL_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, radius and grain take -1 for the preset's value; other negative values are refused at init, as libplacebo refuses them.
    • No runtime commands: iterations and grain are 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).
    • Grain on HDR: divided by the source peak relative to SDR white, as libplacebo's renderer does (mastering peak, else MaxCLL, else 10000 nits for PQ and 1000 for HLG).
    • Math: integer sample values held in floats; texels unpacked with an integer shift (the P010 point from Fairy's review). Sampling positions use fast::cos and fast::sin, as GLSL does on most GPUs.
    • Formats: nv12, p010, yuv420p, nv16, p210, nv24, p410, p216, p416; the output keeps the input's planes and subsampling.
    • Pass-through: when nothing would change (iterations 0, no grain, same format), the output link takes the input's frames context.

    The template points

    Point Here
    Availability macos(10.12), ios(10.0) for MTLFunctionConstantValues. 0 errors at iOS 8.0, tvOS 12.0, macOS 10.11 (x86_64)
    Nil checks, one command buffer, formats and plane layouts in config_output Yes
    fate-hw with CPU references Yes, for what has one (below)
    Copyright, const externs, every parameter read Yes
    P010 unpacking Integer shift; the earlier version used 1.0f / in_shift on the GPU

    Testing

    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: with threshold=0 the 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, inside ffmpeg. 24 cases: nv12, p010, yuv420p, nv16 to p210, nv24 to p410, p216, p416, a 37x11 odd size (made at 38x12 and scaled, since testsrc2 rounds 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 at threshold=6 against a reference told 3, and at radius=8 against 16, both differ. The matrix also checks that threshold=-0.5, radius=-0.3 and grain=-0.2 are refused.

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

    Chain Wall ms CPU ms
    NV12, preset=high_quality 0.51 / 0.49 0.31 / 0.29
    NV12, grain=0:format=p010 0.46 / 0.45 0.32 / 0.32
    NV12, iterations=4:grain=0 1.31 / 1.32 0.59 / 0.35
    P010, preset=high_quality 0.23 / 0.28 0.38 / 0.39

    The 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 soft testsrc2). threshold 5 and iterations 0 sent with sendcmd get Function not implemented, enable 0 gets Not yet implemented in FFmpeg, patches welcome (libavfilter checks the timeline flag first), and the output is identical to a run with no command; a ping afterwards answers. As control, threshold=5 at init changes 10 of 10 frames. Negative values other than -1 are refused at init; -1 everywhere and fuse=0 give the default output.

    Open question

    fate-hw for 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):

    • 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, and each adds its fate-hw block right after bwdif's, so the patches touch the same lines in Changelog, version.h and tests/fate/filter-video.mak. The configure lines of deband, debanditer and deblock also sit side by side.
    • 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 smoothing (the open question above).
    • Against libplacebo itself. Only compared with a C translation of pl_shader_deband; a run against libplacebo's own shader needs Vulkan.
    • Re-run since the port: fullrange-hdr.sh (full range and the HDR grain scaling), platform-compile.sh, and the kernel bench.
    • The HDR peak from metadata. PQ without metadata and HLG were tested on 11-22; the mastering display and MaxCLL paths were not, because no filter in the test build attaches that side data.
    • Which debander to use: a picture comparison against #DEBAND on real banding.
  4. superuser404notfound commented on Nov 25, 2026

    @superuser404notfound
    Owner

    Thanks for this one too. I went through it on an M1 today, on the same base as #1130.

    What I checked

    • The patch applies cleanly on 26276 round 4 (6efb91b844), and the four fate-hw tests pass.
    • I read the kernel next to libplacebo's pl_shader_deband and sh_prng: the pcg3d steps, the seed from the plane coordinates and the frame index, 2 pi as %f prints 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.
    • Your matrix against deband_check: 24 of 24 pass with real_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 with preset=high_quality straight to nv12. Both pass, at most 35 tie mismatches in 93 million samples.
    • The wrapper compiles with no availability errors at iOS 8.0, tvOS 12.0 and macOS 10.11.

    Three fixes from the later bwdif rounds

    These are the same three as in #1130.

    1. CVBufferRemoveAllAttachments before CVBufferPropagateAttachments. 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.
    2. The range falls back to the link's color_range when 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's 420v or 420f. It also sets the code scale, the grain's neutral black and the output scale. A full-range NV12 white (255) through hwupload and format=p010 came out as 1020. With the fallback it is 1023, and the pool is 420f.
    3. The two p010 fate-hw tests sit behind 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.

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