Skip to content

fix(subtitles): decode bitmap subtitles off the MainActor (AE#628) - #635

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/ae628-pgs-main-thread
Sep 25, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/ae628-pgs-main-thread

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Refs #628.

imageForSubtitleRect was the top Main Thread frame with a PGS track enabled. Two causes, both fixed:

The overlay drain decoded on the MainActor. Each display set was expanded there. A selection or seek backfilled the whole 75 s window in one tick, and the Phase D OCR worker (PiP/AirPlay rendition) decoded the same packets a second time on it.

  • subtitleDrainTick is split into prepareSubtitleDrainTick (MainActor), a detached decode, and finishSubtitleDrainTick (MainActor). The drain loop goes through requestSubtitleDrainTick. At most one tick is in flight; a tick asked for meanwhile is queued, because the decoders are not reentrant.
  • finish skips a channel whose target or decoder changed while its batch decoded (re-selection, teletext rebuild, clear). The cursor does not move, so the next tick redoes it. stopSubtitleDrainer drops the in-flight batch by serial.
  • The synchronous subtitleDrainTick stays for the teletext page change and for tests, and queues itself if a tick is in flight.
  • The OCR worker plans and resolves on the MainActor and decodes in its own detached loop.

The blit was per pixel, twice. It now resolves the palette once into a premultiplied lookup table and finds the crop by row scans. Output is byte-identical to the old blit (a reference test pins it). Full-canvas 1080p set with one text band, M1:

build before after
release 5.08 ms 1.31 ms
debug 215 ms 52 ms

The debug figure matters: a host running a Debug build of the package paid about 200 ms of main thread per bitmap.

Verification

  • swift test: 3504 swift-testing tests (5 new in Issue628PGSOffMainDecodeTests) and XCTest all green.
  • aetherctl play --subs pgssub on a generated Blu-ray-shaped track (full 1920x1080 ODS per set), with one seek: identical cue windows on main and on this branch.

🤖 Generated with Claude Code

https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f

Enabling a PGS track on a Blu-ray remux put imageForSubtitleRect at the top
of the Main Thread profile under subtitleDrainTick. Every display set was
expanded pixel by pixel on the MainActor, twice per set (alpha bounding box,
then RGBA), and a selection or seek backfilled a whole 75 s window in one
tick. The Phase D OCR worker decoded the same packets a second time there.

- The drain tick is split into prepare (MainActor), decode (detached) and
  finish (MainActor). One tick at most is in flight; a tick asked for
  meanwhile is queued. A channel re-selected, cleared or rebuilt while its
  batch decoded is skipped with its cursor unmoved, and stopping the drainer
  drops the batch by serial.
- The OCR worker plans and resolves on the MainActor and decodes in its own
  detached loop.
- imageForSubtitleRect resolves the palette once into a premultiplied lookup
  table and scans rows for the crop, byte-identical output. Full 1080p set on
  an M1: 5.08 -> 1.31 ms release, 215 -> 52 ms debug.

Verified with aetherctl play on a generated full-canvas 1920x1080 PGS track:
identical cue windows on main and on this branch, across a seek.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f
@superuser404notfound
superuser404notfound merged commit 875acef into main Sep 25, 2026
7 checks passed
@superuser404notfound
superuser404notfound deleted the fix/ae628-pgs-main-thread branch September 25, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant