Skip to content

Inverse telecine (IVTC) for hard-telecined content #853

Closed

Description

@andreiborisov

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 in configure. The GPL ones are pullup_filter_deps="gpl" and mpdecimate_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: decimate cannot 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=interlaced
    
    fieldmatch flags the frames it could not match cleanly as interlaced, so deint=interlaced touches only those (the credits) and passes matched film frames through untouched. This is the canonical fieldmatch,yadif=deint=interlaced,decimate pattern from the FFmpeg docs.
  • The deinterlacer must run send_frame in this chain, never send_field. At field rate the frame count doubles before decimate, and its 1-in-5 cycle no longer lines up with the pulldown. I hit exactly this offline: field-rate bwdif ahead of decimate produced 47.95 fps timestamps and ~135 extra frames on the episode. So DeinterlaceFieldRate.field should not apply when IVTC is active.
  • The PTS contract in the class doc still holds: output rides av_buffersink_get_time_base(sink), which decimate sets for 24000/1001. SoftwareVideoDecoder (drainDecodedFrames, ~L372–406) already emits filtered frames on deinterlacer.outputTimeBase, so the renderer should need no change.
  • decimate buffers one full cycle (5 frames) plus fieldmatch'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:

  1. 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.
  2. 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 read idet's multi-frame counts. Use idet, not fieldmatch's own interlaced flag (see the fixture table above).
  3. 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.
  4. 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 to deinterlaceMode / deinterlaceFieldRate (PlayerState.swift ~L930–935, enums at ~L346 and ~L359).
  • Keep DeinterlaceMode meaning "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 (decimate can 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 + decimate at 1920×1080 on older Apple TV models. Gating .auto to SD at first would be reasonable.
  • PAL film (2:2, 25i): fieldmatch without decimate (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

  1. added
    enhancement
    on Oct 22, 2026
  2. superuser404notfound commented on Oct 25, 2026

    @superuser404notfound
    Owner

    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:

    1. FFmpegBuild first, as its own small PR: --enable-filter=fieldmatch --enable-filter=decimate --enable-filter=idet next to the deinterlacers in build.sh. The engine side needs that release to link against.
    2. Probe budget. The decision has to land before the display criteria are applied (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 way InterlaceProbe is (frame, packet and wall-clock ceilings) and treat expiry as "inconclusive, keep today's behaviour".
    3. EOF: DeinterlaceFilter never flushes the graph at end of stream today. With yadif that loses one or two frames; with decimate it would lose up to a full cycle plus lookahead, so the chain needs a flush on EOF.
    4. Seek and preroll: after a seek the first output frame now needs about six input frames. Please include a measurement of seek-to-first-frame and scrub stills on the IVTC chain against the plain deinterlacer.
    5. Agreed on the scope you outlined: .auto limited 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 / tinterlace and committed as files, so nothing GPL is needed at build or test time.

  3. superuser404notfound commented on Oct 29, 2026

    @superuser404notfound
    Owner

    Step 1 is done on our side: FFmpegBuild 3.11.0 ships idet, fieldmatch and decimate (checked by FilterAvailabilityTests), and AetherEngine main pins it since 23c0791. An engine PR can build on main directly, no FFmpegBuild PR needed.

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions