Repository navigation
Description
What should the engine do?
Detect hard-telecined film (24p film stored as 29.97i with 3:2 pulldown baked into the picture) and reverse the pulldown instead of deinterlacing it. Do this by field-matching to rebuild the original progressive frames, dropping the repeated frame of each 5-frame cycle, and requesting a 23.976 Hz display. Genuinely interlaced video keeps today's deinterlacer.
Measurements (FFmpeg 9.0.2, Friends S01E05, 23:39)
| Result | |
|---|---|
| Decoded source frames | 42,534 (29.97i, TFF) |
idet on the raw stream (2-min sample, multi-frame) |
TFF 3597 / progressive 0 |
After fieldmatch=order=tff:combmatch=full, whole episode, idet multi-frame |
progressive 33,851 / 34,028 (99.5%), TFF 106, BFF 48 |
After decimate |
34,028 frames, exactly 4/5 of the source (42,534 × 4/5 = 34,027.2), constant 24000/1001 |
| Where the residual combed frames sit | Clustered around 1:40 (opening titles) and 22:50 (end credits): video-rate overlays, handled by the deinterlacer that follows fieldmatch |
The IVTC output is the decoded DVD's own film frames, with nothing interpolated for 99.5% of the episode.
Both clips are 300 frames of MPEG-2 at 29.97i and look identical to every flag:
| Fixture | Progressive after fieldmatch (idet) |
Flagged interlaced by fieldmatch itself |
Output of the full IVTC chain |
|---|---|---|---|
| Hard-telecined 23.976 → 29.97i | 293 / 300 (97.7%) | 3 / 300 | 240 frames (correct) |
| True 59.94 → 29.97i video | 13 / 300 (4.3%) | 8 / 300 | 240 frames (wrong: drops 1/5 of real motion) |
codec_name=mpeg2video profile=Main 720x480 SAR 8:9 (DAR 4:3) yuv420p
field_order=tt r_frame_rate=avg_frame_rate=30000/1001
per-frame: interlaced_frame=1, top_field_first=1, repeat_pict=0 (every frame; no RFF / soft pulldown)
So the chain must be gated on detection. It must never run unconditionally. The detector also can't be fieldmatch's own combed flag: with its default cthresh / combpel it marks only 8 of the 287 frames that idet still sees combed on true video. That flag is fine for routing film's few residual frames to the deinterlacer, but it is far too conservative to classify a stream.
# hard-telecined film fixture
ffmpeg -f lavfi -i "testsrc2=size=720x480:rate=24000/1001:duration=10" \
-vf "telecine=first_field=top:pattern=23,setfield=tff" \
-c:v mpeg2video -flags +ilme+ildct -b:v 6M -pix_fmt yuv420p telecined.mkv
# true interlaced video fixture
ffmpeg -f lavfi -i "testsrc2=size=720x480:rate=60000/1001:duration=10" \
-vf "tinterlace=mode=interleave_top,setfield=tff" \
-c:v mpeg2video -flags +ilme+ildct -b:v 6M -pix_fmt yuv420p true60i.mkv
# detection signal
ffmpeg -i <fixture> -vf "fieldmatch=order=tff:combmatch=full,idet" -f null -(The fixture commands use telecine / tinterlace, which only matters for generating test media. Nothing GPL needs to ship.)
Rough proposal
1. FFmpegBuild: three more filters (all LGPL)
docs/architecture.md:625 describes FFmpegBuild's trimmed avfilter set (bwdif, yadif, yadif_videotoolbox, hwupload, zscale, tonemap, colorspace). IVTC needs fieldmatch and decimate. The detection probe below can use idet, or the engine can compute an equivalent comb metric itself.
Licensing check against FFmpeg release/8.1:
libavfilter/vf_fieldmatch.c: GNU Lesser GPL (Fredrik Mellbin 2012, Clément Bœsch 2013).libavfilter/vf_decimate.c: GNU Lesser GPL (same authors).libavfilter/vf_idet.c: GNU Lesser GPL.- None has a
*_filter_deps="gpl"line inconfigure. The GPL ones arepullup_filter_deps="gpl"andmpdecimate_filter_deps="gpl", and neither is needed here.
So the "dynamic frameworks (LGPL), no GPL components" property of FFmpegBuild is preserved.
2. DeinterlaceFilter: an IVTC chain
Sources/AetherEngine/Decoder/DeinterlaceFilter.swift, buildGraph (chains at ~L203–282).
- Software engine:
fieldmatch=order=auto:combmatch=full,bwdif=mode=send_frame:parity=auto:deint=interlaced,decimate - Hardware engine:
decimatecannot take VideoToolbox frames, so cadence work stays on the CPU before upload:fieldmatch=order=auto:combmatch=full,decimate,format=nv12,hwupload,yadif_videotoolbox=mode=send_frame:parity=auto:deint=interlacedfieldmatchflags the frames it could not match cleanly as interlaced, sodeint=interlacedtouches only those (the credits) and passes matched film frames through untouched. This is the canonicalfieldmatch,yadif=deint=interlaced,decimatepattern from the FFmpeg docs. - The deinterlacer must run
send_framein this chain, neversend_field. At field rate the frame count doubles beforedecimate, and its 1-in-5 cycle no longer lines up with the pulldown. I hit exactly this offline: field-rate bwdif ahead ofdecimateproduced 47.95 fps timestamps and ~135 extra frames on the episode. SoDeinterlaceFieldRate.fieldshould not apply when IVTC is active. - The PTS contract in the class doc still holds: output rides
av_buffersink_get_time_base(sink), whichdecimatesets for 24000/1001.SoftwareVideoDecoder(drainDecodedFrames, ~L372–406) already emits filtered frames ondeinterlacer.outputTimeBase, so the renderer should need no change. decimatebuffers one full cycle (5 frames) plusfieldmatch's neighbours, so latency rises by a few frames. Teardown on seek (already done) resets the cycle phase. The cadence re-locks within one cycle.
3. Detection: a load-time cadence probe (VOD)
The display rate is fixed at load: AetherEngine.swift:4786, let snappedRate = FrameRateSnap.snap(detectedRate ?? 0), which feeds displayCriteria.apply(...) (~L4896, then DisplayCriteriaController.apply at Display/DisplayCriteriaController.swift:749). So the IVTC decision has to be made before that point, or at least before the criteria are applied.
There is precedent: Decoder/InterlaceProbe.swift (#232) already decodes a sample at load to check a declared field order against the decoder's AV_FRAME_FLAG_INTERLACED. It is H.264-only today. A sibling probe for the software-path codecs (MPEG-2 first) could:
- Run only when the decoder actually emits interlaced frames (
AV_FRAME_FLAG_INTERLACED,repeat_pict == 0). Soft-telecined streams (RFF flags) already decode to progressive frames and need nothing. - Decode a few short windows (for example 2–3 × ~250 frames, away from the head so studio logos and titles don't dominate) through
fieldmatch=combmatch=full,idet, and readidet's multi-frame counts. Useidet, notfieldmatch's own interlaced flag (see the fixture table above). - Decide IVTC when about ≥ 90% are progressive after matching (film was 97.7–99.5% above, true video 4%). Optionally confirm the 1-in-5 duplicate via
decimate's metrics. - Request 24000/1001 instead of the container's 30000/1001.
4. API surface
Suggested shape:
LoadOptions.inverseTelecine: InverseTelecineMode = .auto, with.auto(probe decides),.off(today's behaviour) and.force. It would live next todeinterlaceMode/deinterlaceFieldRate(PlayerState.swift~L930–935, enums at ~L346 and ~L359).- Keep
DeinterlaceModemeaning "which engine deinterlaces", since IVTC still uses one for the residual frames. - A log line in the style of the existing
[Deinterlace] engaged …(decision, progressive ratio, sample size) would make field reports easy.
5. Edge cases
- Hybrid content: video-rate titles and credits inside film (covered by
fieldmatch+ deinterlacer, at the cost of 1/5 of the frames in those sections), and cadence breaks at edits (decimatecan occasionally drop a unique frame. That is inherent to IVTC and identical to offline tools). - Seek mid-cycle: graph teardown and rebuild, then the first output frames after the seek.
- Live / broadcast: probably excluded from
.auto(no rewind for a probe, and mixed-cadence channels). - 1080i film sources (HD broadcast, some Blu-ray extras): CPU cost of
fieldmatch+decimateat 1920×1080 on older Apple TV models. Gating.autoto SD at first would be reasonable. - PAL film (2:2, 25i):
fieldmatchwithoutdecimate(no repeated frames). This could be a follow-up.
Motivating media or use case
NTSC DVD remuxes of film-sourced TV series and movies. A lot of US sitcoms and dramas from the 1990s and 2000s were shot on film and released on DVD as hard-telecined MPEG-2. My personal motivation is watching Friends extended version in the best quality possible, the extended version were never rereleased on a diffirent format and any re-encode would be lossy.
There are no pulldown flags to honour: every picture is coded interlaced, so the 3:2 cadence can only be found by looking at the content.
As far as I can tell no tvOS player does IVTC today. I checked Infuse (yadif), VLC for tvOS (blend only) and Kodi (IVTC only on Linux hardware-decoding paths). AetherEngine already owns an FFmpeg filter graph on exactly this path, so it seems like the natural place for it.
Area
Other
Host app / integration context
Any client would benefit
Would you be willing to open a PR?
Yes
Activity
superuser404notfound commentedon Oct 25, 2026
Thanks, this is a well-measured proposal and the direction fits: the software path already owns the filter graph, so IVTC belongs there. Licensing checks out against current upstream (fieldmatch, decimate and idet are LGPL; only pullup and mpdecimate carry
_deps="gpl"). Please go ahead with a PR. A few points for it:--enable-filter=fieldmatch --enable-filter=decimate --enable-filter=idetnext to the deinterlacers inbuild.sh. The engine side needs that release to link against.AetherEngine.swift~L4889 now), so the probe sits on time-to-first-frame. Seeking to several windows away from the head over HTTP is expensive there. Given the separation you measured (97.7% vs 4.3%), one window of ~120 frames (24 cycles) at the start position should be enough. Please bound it the wayInterlaceProbeis (frame, packet and wall-clock ceilings) and treat expiry as "inconclusive, keep today's behaviour".DeinterlaceFilternever flushes the graph at end of stream today. With yadif that loses one or two frames; withdecimateit would lose up to a full cycle plus lookahead, so the chain needs a flush on EOF..autolimited to SD at first, live excluded, no field-rate output while IVTC is active, PAL 2:2 as a follow-up. A[Deinterlace]-style decision line with the progressive ratio and sample size would be great.Test fixtures can be generated offline with
telecine/tinterlaceand committed as files, so nothing GPL is needed at build or test time.chore(deps): FFmpegBuild 3.11.0, the audio shaping and inverse teleci…
superuser404notfound commentedon Oct 29, 2026
Step 1 is done on our side: FFmpegBuild 3.11.0 ships
idet,fieldmatchanddecimate(checked byFilterAvailabilityTests), and AetherEngine main pins it since 23c0791. An engine PR can build on main directly, no FFmpegBuild PR needed.feat(deinterlace): inverse telecine for hard-telecined NTSC film (#853)
Merge pull request #1105 from andreiborisov/feat/inverse-telecine-853