Repository navigation
Description
What should the engine do?
Two new FFmpeg filters that run the two stages of inverse telecine on VideoToolbox frames with Metal compute kernels:
fieldmatch_videotoolbox, a GPUfieldmatch.decimate_videotoolbox, a GPUdecimate.
Their output is identical to CPU fieldmatch and decimate: the same frames, timestamps, matches, combed flags and dropped frames.
This came out of the bwdif_videotoolbox work (#1102), in parallel with the engine work for #853 that landed in #1105. It is exploratory. It does not replace anything in #1105, which built the part I did not: the film check at load, the session handling, the timeline and the API. These filters would only swap two stages inside the chain #1105 builds.
The FFmpeg patch, the FFmpegBuild changes, the kernel tests and the comparison scripts are attached (ivtc-videotoolbox-files.zip), written with Claude Code. As with #1102, I'm not a software engineer and it has only run on a Mac, so please treat it as material to keep, rework or discard, not as a PR.
On the GPU engine, #1105's chain and the chain these filters would allow are:
main: fieldmatch=order=auto:combmatch=full,decimate,format=nv12,hwupload,bwdif_videotoolbox=mode=send_frame:parity=auto:deint=interlaced
proposed: format=nv12,hwupload,fieldmatch_videotoolbox=order=auto:combmatch=full,decimate_videotoolbox,bwdif_videotoolbox=mode=send_frame:parity=auto:deint=interlaced
Adding the filters to the build changes nothing for hosts. The engine keeps its CPU stages until DeinterlaceFilter is changed to use them.
What each piece brings over main
CPU time per input frame on an M1 Max (details under Testing 3):
| Piece | Replaces on main | What it brings | SD (480, 576) | 1080 |
|---|---|---|---|---|
fieldmatch_videotoolbox |
CPU fieldmatch with combmatch=full |
Takes the most expensive stage off the CPU. Its cost stops depending on the picture. | 1.6 to 6.0 ms becomes 0.1 to 0.6 ms | 9.5 to 9.7 ms becomes 0.5 to 1.8 ms |
decimate_videotoolbox |
CPU decimate |
Lets the frames stay on the GPU after fieldmatch_videotoolbox. Without it, the GPU fieldmatch would need a download and a second upload. |
No saving: about 0.2 ms either way | 1.15 ms becomes 0.3 to 0.4 ms, upload included |
| Both, in #1105's chain | The CPU stages before hwupload |
One upload at the head of the chain, then everything on the GPU | 1.8 to 6.3 ms becomes 0.5 to 0.8 ms | 10.7 to 10.9 ms becomes 1.0 to 1.8 ms |
Motivating media or use case
1. It takes CPU time back at every resolution, and the most where it matters most. #1105's chain on the GPU engine against the proposed chain. One filter thread, two runs, minus a decode-only baseline, so the upload counts in both:
| Clip | Size | main, CPU time | Proposed, CPU time | Saved | Share of one core at 29.97 fps, main → proposed |
|---|---|---|---|---|---|
| 3:2 film, clean | 720x480 | 1.78 ms | 0.48 to 0.56 ms | 1.2 to 1.3 ms | 5.3% → 1.6% |
| 3:2 film with noise | 720x480 | 6.27 to 6.28 ms | 0.64 to 0.82 ms | 5.5 to 5.6 ms | 18.8% → 2.2% |
| 3:2 film | 720x576 | 2.19 to 2.22 ms | 0.52 to 0.61 ms | 1.6 to 1.7 ms | 6.6% → 1.7% |
| 3:2 film | 1920x1080 | 10.88 to 10.94 ms | 1.09 to 1.80 ms | 9.1 to 9.8 ms | 32.7% → 4.3% |
| Off-air CBS, ATSC 1.0 (true interlace), my HDHomeRun | 1920x1080 | 10.69 to 10.78 ms | 0.97 to 1.11 ms | 9.6 to 9.8 ms | 32.2% → 3.1% |
- At SD, main fits, and feat(deinterlace): inverse telecine for hard-telecined NTSC film (#853) #1105 is right that it does. The proposed chain still saves 1.2 to 5.6 ms of CPU time per frame, depending on the picture, on a device that is also decoding the MPEG-2 in software.
- Above SD, the saving grows to about 9.5 ms a frame: a third of a core at 29.97 fps becomes 3 to 4%. The Inverse telecine (IVTC) for hard-telecined content #853 proposal gave the CPU cost at 1920x1080 on older Apple TVs as the reason to keep
.autoto SD at first. These filters remove that cost from the playback chain.
Whether HD should be sampled is a separate decision, and the film check at load would still cost more at HD (see "What the engine would need").
2. The GPU cost does not depend on the picture. CPU fieldmatch at the same 720x480 costs 1.4 ms of wall time on clean film and 5.8 ms on the same film with noise, because its comb test branches on every pixel. #1105 measured 2.9 ms on the Friends DVDs, between the two. Grainy film, the usual NTSC DVD, is on the expensive side. fieldmatch_videotoolbox measured 0.8 to 1.4 ms of wall time on both clips, and at most 0.6 ms of CPU time.
3. Nothing that #1105 measured or tested changes. The output is identical, so these describe the proposed chain as well:
- the film check at load and its thresholds;
- the 23.976 timeline, and the re-anchor on a gap;
- the tests in
InverseTelecineChainTests.
The film check at load itself is not touched. It keeps CPU fieldmatch with combmatch=none and its own block measure.
What it costs.
- The GPU chain's wall time is 1.2 to 3.0 ms a frame. Most of it is the filter thread waiting on the GPU, not working.
- It is 2,156 lines of FFmpeg patch to carry, in the style of the
yadif_videotoolboxandbwdif_videotoolboxpatches already in the build.
Anything you tried instead?
- Merging the two filters into one, to save GPU waits.
- A commit-and-wait costs about 0.23 ms on the M1 Max. With
decimate_videotoolboxwaiting once per cycle, a merged filter would save about 0.05 ms per frame. - It would also need option names that match neither CPU filter. Both have
blockx,blocky,chroma,scthreshandppsrc, with different defaults. - So they stay two filters, each a mirror of its CPU filter.
- A commit-and-wait costs about 0.23 ms on the M1 Max. With
- Keeping CPU
decimateafter a GPUfieldmatch. That needshwdownloadafterfieldmatch_videotoolboxand a secondhwuploadbeforebwdif_videotoolbox. CPUdecimateis cheap at SD, so this would cost more than it saves. - Using
fieldmatch's comb scores to tell film from video, in place ofidet. On my synthetic pair, film and true video scored the same (medians 63 and 65). feat(deinterlace): inverse telecine for hard-telecined NTSC film (#853) #1105's worst-block cadence check solves this properly.
Area
Performance
Host app / integration context
Sodalite
Would you be willing to open a PR?
No
Activity
classicjazz commentedon Nov 21, 2026
Supplemental details
Everything below was measured on an Apple M1 Max, macOS 27.2, FFmpeg n8.1.3 built locally with this patch and
bwdif-videotoolbox.patch. Nothing has run on an Apple TV, iPhone or iPad,build.shhas not been run, and the engine has not been changed to use the filters.What's attached
ivtc-videotoolbox.patchgit apply, with or withoutbwdif-videotoolbox.patchfirstffmpegbuild-changes.diffbuild.sh,FilterAvailabilityTests.swiftandREADME.mdchanges. The patch above goes inpatches/ivtc-tests/harness/vf_fieldmatch.candvf_decimate.ccompiled from n8.1.3ivtc-tests/*.shivtc-tests/*-results.txtREADME.txtFFmpeg patch
libavfilter/metal/vf_fieldmatch_videotoolbox.metallibavfilter/vf_fieldmatch_videotoolbox.mlibavfilter/metal/vf_decimate_videotoolbox.metallibavfilter/vf_decimate_videotoolbox.mlibavfilter/allfilters.cff_vf_decimateandff_vf_fieldmatch.libavfilter/Makefileyadif_videotoolboxentry.configure*_filter_deps="metal corevideo videotoolbox", the same asyadif_videotoolbox.FFmpegBuild
build.shpatch_ffmpeg_ivtc_videotoolboxstep afterpatch_ffmpeg_bwdif_videotoolbox. It skips if the filter file exists and stops the build if the patch does not apply, like the otherpatches/steps.--enable-filterfor both, next tofieldmatchanddecimate.FilterAvailabilityTests.swiftREADME.mdWhat the engine would need
Claude's reading of AetherEngine
mainat3820c494, not tried:DeinterlaceFilter.swift:339putsfieldmatchanddecimatebeforeformat,hwuploadwhen inverse telecine runs on the GPU engine, becausedecimatetakes CPU frames only (class doc, line 69). With these filters the two stages would move afterhwupload, as in theproposedline above.inverseTelecineFiltersLinked(line 144) checks forfieldmatchanddecimate. The proposed chain would also needfieldmatch_videotoolboxanddecimate_videotoolbox, and would fall back to today's chain when either is missing.fieldmatch_videotoolboxrefuses, when the graph is configured, a frame height that is not a multiple of 4 and any pixel format other than NV12 (see below). The engine would then build feat(deinterlace): inverse telecine for hard-telecined NTSC film (#853) #1105's chain instead. feat(deinterlace): inverse telecine for hard-telecined NTSC film (#853) #1105's sources, 480 and 576 lines of 8-bit MPEG-2, are never refused.decimate's timestamps (first frame plus one tick per kept frame), sobreaksInverseTelecineCadenceand the rebuild on a seek work the same.TelecineCadenceProberuns CPUfieldmatchand a Swift block measure on up to 300 frames inside a 2 s budget. These filters do not change that, and at 1080 it would cost more. That belongs to an HD follow-up, if there is one.How the filters work
The GPU measures, the CPU decides. Each filter is a copy of its CPU filter with the loops over pixels moved to Metal:
build_abs_diff_mask+build_diff_mapdiff_map: the map of where the two fields differ a little or a lotcompare_fieldsmatch_sums: the six sums (Pc, Pm, Pml, Nc, Nm, Nml), per 64 columns of a rowcalc_combed_scorecomb_mask: the[1 -3 4 -3 1]comb test on a candidate frame, read in place, never builtcomb_count: combed pixels per half-blockluma_abs_diffluma_diff, forcombmatch=sccreate_weave_frameweave: the output frame when the match is not the current framecalc_diffs(decimate)block_diff: absolute differences per half-blockWhy the sums are exact.
R8Uint, andRG8Uintfor NV12's UV), so the samples are the same integers the CPU sees.uint64_tandint64_ttotals do. Integer sums do not depend on order, so the totals are exactly the CPU's.compare_fields' ratios,checkmm, the scene change and combed checks,decimate's choice of the frame to drop, and the timestamps.Round trips. A commit and wait costs about 0.24 ms, against 0.02 ms for the GPU work itself.
fieldmatch_videotoolboxscores every candidate the mode can ask for in one submission. A frame costs one wait, plus one more when the output must be woven (2 frames in 5 on 3:2 film).decimate_videotoolboxcommits each frame's metric without waiting and waits once per cycle, becausevf_decimate.creads the metrics only when the cycle is complete.Supported, and refused when the graph is configured:
hwuploadmakes of 8-bit 4:2:0)fieldmatchtakes only 8-bit planar input)fieldmatchmodepc,pc_n(default),pc_upc_n_ub,pcn,pcn_ubfieldmatchoptionsorder,field,combmatch,mchroma,y0/y1,cthresh,combpel,scthresh, evenblockx/blockyppsrc,combdbg,chroma=1, odd block sizesdecimateoptionsppsrcppsrc#1105's options (
order=auto:combmatch=full,decimatedefaults) are all supported.Testing
1. The kernels against
vf_fieldmatch.candvf_decimate.c. The attached programs compile the two CPU filters from n8.1.3 and compare every number the decision code reads. The only change tovf_fieldmatch.cis one added line that copiescompare_fields' six sums out before they are used.compare_fieldscthresh-1, 0, 4, 9, 20, blocks 16x16, 4x4, 8x32, 32x8decimateResult: fieldmatch 15,000 cases (29,400 values), decimate 288 cases, 0 differences, the same on three runs.
The test can fail. Planted bugs were caught:
> 19to> 18: 292 differences;decimate: 156.2. The whole chain inside
ffmpeg. Each clip and option set ran through CPUfieldmatch,decimateand through the two new filters. They were compared on:framemd5of the output, timestamps included;fieldmatch's per-frame debug line (comb scores, match, combed flag);decimate's per-cycle lines (both metrics per frame, which frame is dropped).combmatchfull (#1105's), sc and none; modespc,pc_n,pc_u;mchroma=0; forcedorder=bff:field=top; an exclusion band withcthresh=4:combpel=40and 8x32 blocks;cthresh=-1; lowscthresh.decimate: default (#1105's),chroma=0, 16x16 blocks,cycle=4,mixed=1, other thresholdsResult: 80 of 80 combinations identical on all three comparisons. Between them they ran from 0 to 834 combed frames and from 0 to 208 dropped frames.
Two details were needed to compare logs:
-loglevel repeat+debug, because FFmpeg otherwise collapses repeated lines at points that depend on thread timing;decimate's metrics for the partial cycle at end of stream, as the CPU filter does.3. Speed against main (
timing-engine.sh). One filter thread, best of three per run, two runs, minus a decode-only baseline. Per input frame, wall time / CPU time in ms:fieldmatch=order=auto:combmatch=fulldecimatehwuploadalonehwupload,fieldmatch_videotoolboxhwupload,decimate_videotoolboxdecimate); in the proposed chain it handles all 5.decimateat 1.15 ms per SD frame on an M1 Pro, decode included. On my 480 and 576 clips it measured about 0.2 ms of CPU time. I have not looked into the difference. Either way, at SDdecimate_videotoolboxis there to keep the chain on the GPU, not for speed.4. Build.
git apply --checkto a clean n8.1.3 and on top ofbwdif-videotoolbox.patch.configuresets bothCONFIG_*_VIDEOTOOLBOX_FILTER. The macOS build has no warnings in the new files.xcrun metalfor the AIR target of everybuild.shslice I tried (tvOS 16, tvOS simulator, iOS 16, visionOS 1, macOS 14). The wrappers compile for the same targets with-Werror=partial-availability.Found along the way
vf_fieldmatch.c(n8.1.3),compare_fieldssrcnfat rowheight) and atbufferrow thatbuild_abs_diff_maskdid not write for this frame. This is Claude's reading of the code, not something I saw go wrong. A 720x486 frame has a 243-row chroma plane. #1105's sources (480 and 576 lines) never hit it. The GPU filter refuses heights that are not a multiple of 4, so such a source would stay on CPUfieldmatch.vf_fieldmatch.c,compare_fieldsnxtp -= nxt_linesizewith a field base of 1), and the kernel test showed its sums changing from run to run. No caller passes such a pair (it is only ever called with c against p or n, or p against n), so the filter is not affected. Noted in case someone extends the modes.bwdif_videotoolbox,yadif_videotoolbox(both already in the build)deint=interlaced, a frame that is not marked interlaced passes through by reference and keeps the upstream frames context, while the filter's output link has its own.hwdownloadafter it fails ("Input frame is not the in the configured hwframe context"). #1105's GPU chain already passes most frames through this way and plays on an Apple TV, so the engine is not affected. It only stops a test from downloading the output, which is why the full chain withbwdif_videotoolboxis compared on timing only.Not verified
build.sh. Not run. The frameworks are not rebuilt, so the newFilterAvailabilityTeststest fails until they are.ffmpegonly.DeinterlaceFilterhas not been changed to use the filters.fieldmatch_videotoolbox,decimate_videotoolboxis compared frame for frame. The chain withbwdif_videotoolboxafter it runs, but could not be compared throughhwdownload(see above).superuser404notfound commentedon Nov 22, 2026
Thanks a lot for this, and for shipping the harness and the scripts with it. They made checking it straightforward.
I've taken both filters in. Where it stands:
FFmpegBuild (
main, not released yet). The patch is in aspatches/ivtc-videotoolbox.patch, with three changes from your version:decimate_videotoolboxadvancedfidto the cycle length before it read the cycle's metrics. After a failed command buffer, the next frame (or the EOF flush) wrote one slot past the queue, and a failed submission left a frame in a slot the next one overwrote. A GPU failure now latches, and later frames are freed and refused.fieldmatch_videotoolboxnow hasfieldmatch_init's two checks (blockx/blockya power of two,combpelat mostblockx * blocky), so both filters accept the same options.AVERROR_EXTERNALinstead ofENOMEM.Your harness and
matrix.shgave the same result here on an M1: 0 differences in the kernels, and 63 of 63 combinations identical (I don't have your CBS capture). They now live inTests/ivtc-videotoolbox/, because the GPU filters copy the CPU decision code and an FFmpeg bump could change one side only.AetherEngine (branch
feat/ae1108-gpu-ivtc). On the GPU engine the chain becomes your proposed one. The CPU stages before the upload stay as the fallback in these cases:format=nv12ahead of the GPU filters would subsample 4:2:2 chroma across the two fields of a combed frame beforefieldmatchseparates them);bwdif_videotoolboxand moves only the cadence to the CPU.The warm-up also builds the GPU cadence graph, and
VideoDeinterlace.inverseTelecineIsHardwarereports which one runs. A test compares the GPU chain with the CPU-cadence chain picture for picture after the download, and they match.aetherctl playon a telecined 480i clip runs at 24 fps with no lost frame, across a seek too.One correction to the numbers. Your timing counts user time only, and Metal's submissions show up as system time. On my M1 (not Max), with raw input so the MPEG-2 decoder's threads aren't counted, user plus system time per input frame:
So the saving is real everywhere. On clean SD it's small, and the wall time goes up by about 0.8 ms. On grain and at 1080 it's large. Nothing has run on an Apple TV yet.
Next is an FFmpegBuild release, then the engine pin and an engine release. Once it's in a TestFlight build, a run of a real NTSC DVD episode on your Apple TV would be the most useful check. With the matching Sodalite update, Stats for Nerds will show whether IVTC runs on the GPU.
superuser404notfound commentedon Nov 22, 2026
Released in 7.73.0 (bbcf3b8), on FFmpegBuild 3.19.0. Sodalite and AetherPlayer are bumped to it.
A real NTSC DVD episode on your Apple TV is still the check I'd like most, once you have a build with it. Stats for Nerds will show
IVTC (GPU) + bwdif_videotoolbox (GPU)when the GPU cadence runs, andIVTC (CPU)when it fell back.