Skip to content

player: image subtitles scale from the canvas they were authored against - #9

Merged
GLinnik21 merged 1 commit into
mainfrom
unit-14-image-subtitle-canvas
Jul 30, 2026
Merged

GLinnik21 merged 1 commit into
mainfrom
unit-14-image-subtitle-canvas

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Unit 14 — parity-gaps.md: "Image-subtitle bitmaps are drawn at raw stream coordinates with no scaling from the subtitle canvas — VobSub/DVD subs land as a postage stamp" (major/small), plus the polish/small follow-up "PGS/VobSub image subtitles don't lift for the HUD, and only rect 0 of a multi-rect display set is drawn".

What was wrong

A decoded PGS/VobSub rect's x/y/w/h are in the subtitle stream's own authoring canvas, not in screen pixels. We composited them 1:1 into our 1920x1080 UI and asserted in a comment that this was correct. It is correct for Blu-ray PGS and wrong for everything else: a DVD VobSub rip is authored on 720x480 and landed as a postage stamp in the upper-left quadrant; a 4K-authored PGS track drew at double size with half of it off the panel. The track menu offers those tracks and badges them VOBSUB, so the failure was reachable from the UI.

What changed

  • ff::sub_canvas reads the canvas off the decoder after each decode; it travels with the cue through the store to the renderer.
  • player_hud::sub_screen_rect stretches the display set into the video rect — the same stretch the TV applies to the picture, since a subtitle canvas is the video's own storage grid. A 1920x1080 canvas is exactly identity (1920.0/1920.0 == 1.0, x * 1.0 == x), so every PGS track in the library renders as it did before. An undeclared canvas (0) falls back to 1:1 — today's unconditional behaviour.
  • Every rect of a display set is decoded and drawn, not just rect 0. Two-line dialogue and sign-plus-dialogue are authored as separate rects of one set; the old code dropped half the line and logged that it had. The store now holds a set of rects per cue, which also makes the supersede rule correct by construction. Capped at 8 rects; the 24 MB ceiling counts a set's rects together.
  • Image cues lift clear of the transport like text cues do, by their overhang. The shift is measured over — and applied to — only the rects that overhang, so a top-of-frame sign neither moves nor clamps the dialogue's lift. It is deliberately not per-rect: two-line dialogue has two different overhangs and lifting each by its own collapses the lines onto each other. Both paths now take the same subs_lift expression from app.rs.

Scaled content is not gfx::snapped, per the crispness contract.

Did I prove the ABI against the device's own binaries? YES — and no new offset was added

No new struct-field offset is introduced by this change. The canvas is read through the library's own avcodec_parameters_from_context, which copies the decoder's dimensions into the AVCodecParameters this crate already models (width/height at +48/+52 — the same fields the video path has read on-device since the demuxer landed). The read therefore runs inside libavcodec's own code, which is the bind-tv-lib-abi skill's preferred route ("prefer the library's own name-based API — no offset, no risk").

Two things were proven against the device, not inferred:

  1. Symbol presence. tools/abi-probe.sh has libavcodec.so.57 avcodec_parameters_alloc avcodec_parameters_free avcodec_parameters_from_context → all PRESENT on the device's own libavcodec.so.57.89.100. Recorded in the extern block, because the stub .so links either way.
  2. That this build's avcodec_parameters_from_context actually handles subtitles. The device build is stripped, so a header claim would be worthless. I pulled the library and disassembled the function with the NDK's objdump:
690a04 <+0x98>:  cmp r3, #3        ; AVMEDIA_TYPE_SUBTITLE
        beq 690aa8 <+0x13c>
690aa8 <+0x13c>: ldr r2, [r5, #124]   ; avctx->width
        ldr r3, [r5, #128]            ; avctx->height
        str r2, [r4, #48]             ; par->width
        str r3, [r4, #52]             ; par->height

The subtitle case is present in this build, and this incidentally corroborates three offsets already in ff.rs from an independent direction: OFF_CTX_WIDTH/OFF_CTX_HEIGHT (124/128) and AVCodecParameters.width/height (48/52). The prologue's memset(par, 0, 136) likewise confirms the modeled sizeof == 136. The whole derivation is in sub_canvas's doc comment so it is not lost.

A canvas outside 160..8192 is reported as unknown rather than used — scaling by a garbage ratio is worse than not scaling.

Device verification: deferred to coordinator

I did not run anything on the TV. I queued for the shared lock, never acquired it, and killed the job on the coordinator's instruction. The TV was never touched by me — no deploy, no launch, no stale binary. (I did pull libavcodec.so.57 off the device read-only for the disassembly above.)

Case to run

./tests/run.py --filter toy_story2_subtitle_pgs --no-early

Toy Story 2 (rk=1919, HEVC 4K direct-play). The harness seeds viewOffset to 600000 ms (10:00) and selects the PGS track from the track menu (tab 1, row 1) once playing, so a cue should appear within seconds of the switch. --no-early runs the full 90 s so there is a comfortable capture window; the existing assertions (directplay, codec_id=174, min_video_width=3000, timeline climb, no Playing error, video bound) are the regression gate.

What to look for in /tmp/plxnative-events.log — the new evidence is the canvas= field:

image cue [<t>ms] <W>x<H> at <x>,<y> rects=<N> canvas=<CW>x<CH>
  • canvas=1920x1080 → the canvas read works and the render is byte-identical to before (scale is exact identity). This is the expected result for a 1080p-authored PGS track and is a clean pass.
  • canvas=3840x2160 → this track is 4K-authored, and the fix is visible: the old build drew it at 2x with half off the panel; it should now sit correctly.
  • canvas=0x0 → the decoder declared nothing, so the 1:1 fallback is in play. Not a crash and not a regression, but it means the canvas read did not fire for PGS and the VobSub case would still be unfixed — worth reporting back.

rects= should normally be 1; a 2 is the multi-rect path working.

Capture — service capture only

tools/capture-screen.sh /tmp/unit-14.png DISPLAY

taken while a cue is up. The in-app capture stream (/tmp/plxnative-capture, TCP :8910) cannot see this at all — capture.rs grabs the app's own GLES frames, i.e. the UI plane only, and the subtitle composites over the hardware VIDEO overlay plane. DISPLAY is the composited panel output and is the only evidence. (VIDEO alone would show the picture without the subtitle; GRAPHIC alone shows the subtitle without the picture — that pair is a useful split if something looks wrong.)

What the frame should look like if the scaling is right

A PGS dialogue line on a 1920x1080 canvas is a wide, short strip: roughly 60-85% of the frame width (~1150-1650 px), 6-10% of its height (~70-110 px), horizontally centred, sitting in the bottom sixth of the frame (top edge around y≈900-1000 of 1080). Failure signatures:

  • a small bitmap in the upper-left third of the frame → the canvas is larger than we scaled for (the original bug shape);
  • a bitmap overflowing the right/bottom edge, roughly double the expected size → a 4K canvas whose scale was not applied.

One more check, free while you are there: with a cue on screen, press any key to raise the transport. The bitmap should rise so its bottom edge sits at y = 780 (SCR_H - 300), clear of the scrubber — matching where text subtitles lift to. It should not move if it was already above that line.

No VobSub item exists to test against. I scanned all 477 movie + episode items on the PMS via /library/metadata/{rk}: every image-subtitle track in the library is pgs. There is no vobsub/dvd_subtitle anywhere, so the 720x480 path cannot be exercised on this server and is covered by host tests only. That is the one gap in this verification, and it is a library limitation, not a skipped step.

Tests

make check — 65 passed (58 baseline + 7 new), make (ARM cross-build) clean, no new warnings.

Per the stated boundary, the new tests exercise the arithmetic, not the FFI (ff.rs's #[link] directives are cfg_attr(not(test))-gated, so a test that calls FFmpeg cannot link):

  • image_sub_1080p_canvas_is_identity — the 1920x1080 regression guard, asserted exact
  • image_sub_dvd_canvas_fills_the_panel — 720x480, the bug this unit exists for
  • image_sub_pal_and_4k_canvases — 720x576 and 3840x2160 (the latter scales down)
  • image_sub_unknown_canvas_stays_1to1 — the 0 / negative fallback
  • image_sub_lifts_only_over_the_transport and image_sub_lift_spans_only_the_rects_that_overhang — the lift, including sign-plus-dialogue and two-line dialogue
  • an_image_display_set_round_trips_whole_and_is_superseded_as_a_unit (player/mod.rs, takes the crate-wide testlock) — the multi-rect store, the supersede handover, and the eviction order below

Review pass

An independent reviewer went over the diff for memory safety, GL texture lifetime, arithmetic and cross-thread invariants. Everything it found is fixed in this commit:

Finding Fix
The store's byte ceiling evicted the cue at the playhead. v[0] is the cue being drawn once the time-retain has run, and the tail is the demuxer's 10-20 s read-ahead — so the budget blanked the subtitle being read and kept ones nobody had reached. A set costing up to 8 rects instead of 1 is what made it reachable Drop a cue the playhead has already passed first; only fall back to the far end of the read-ahead when there is none. The test was checked to fail against the old policy
hud_lift measured the whole set, so a sign at y=100 clamped the lift and left the dialogue behind the scrim Measured over and applied to only the overhanging rects
rect_to_rgba did not null-check the rect pointer — the loop now walks up to 8, not just [0] Added
w*h*4 can wrap: usize is 32-bit on this target, and w/h are unvalidated decoder output. A wrapped product sizes the buffer below what the write loop and glTexImage2D then read Bounded w/h to 8192 and checked_mul
The renderer's cache key was start_ns alone, but two image tracks of one file routinely start a set on the same pts — a switch left the outgoing track on screen Key is now (track, start_ns); both halves reset together
Symbol presence for the two new externs was proven but not written down Recorded in the extern block
tests/README.md documented the old image cue format Updated

The reviewer confirmed no texture leak, double-delete, draw-after-delete or stale draw across every early-return and both the grow and shrink directions, and that the 1080p path is bit-identical.

🤖 Generated with Claude Code

https://claude.ai/code/session_018GuSHrq9oydnX7nhKE1Hvc

A decoded PGS/VobSub rect's x/y/w/h are in the SUBTITLE stream's own authoring
canvas, not in screen pixels. We composited them 1:1 into our 1920x1080 UI and
said so in a comment — which is right for Blu-ray PGS and wrong for everything
else: a DVD VobSub rip is authored on 720x480, so it landed as a postage stamp
in the upper-left quadrant, and a 4K-authored PGS track drew at double size with
half of it off the panel. The track menu offers those tracks and badges them, so
the failure was reachable from the UI with no warning.

The canvas now comes off the decoder and travels with the cue to the renderer,
which stretches the display set into the video rect — the same stretch the TV
applies to the picture itself, since a subtitle canvas is the video's own
storage grid. A 1920x1080 canvas is exactly identity, so every PGS track in the
library renders as it did before; an undeclared canvas (0) falls back to 1:1,
which is what the whole path did unconditionally.

Reading it is FFI into the TV's own libavcodec, so it is done WITHOUT a new
struct offset: `avcodec_parameters_from_context` copies the decoder's dimensions
into the AVCodecParameters this crate already models, so the read runs inside
the library's own code. That it does so for subtitles at all is not a header
claim — the device's `libavcodec.so.57.89.100` is stripped, so the subtitle case
(`cmp r3,#3`) and the exact loads (`ldr r2,[r5,#124] / ldr r3,[r5,#128]` into
par+48/+52) were read out of its disassembly, which incidentally corroborates
`OFF_CTX_WIDTH`/`OFF_CTX_HEIGHT` and the modeled sizeof=136. A canvas outside
160..8192 is reported as unknown rather than used, because scaling by a garbage
ratio is worse than not scaling at all.

Two smaller fidelity losses on the same path, both from the same audit entry:

  * EVERY rect of a display set is decoded and drawn, not just rect 0. Two-line
    dialogue and sign-plus-dialogue are authored as separate rects of one set,
    so the old code silently dropped half the line and logged that it had. The
    store now holds a set of rects per cue, which also makes the supersede rule
    correct by construction — pushing rects individually would have had the
    second one close the first at its own start. The 24 MB ceiling counts a
    set's rects together, and a pathological set is capped at 8 rects.
  * image cues lift clear of the transport like text cues do. They lift by their
    overhang rather than by a fixed offset, so a full-height set is clamped
    instead of pushed off screen. The shift is measured over — and applied to —
    only the rects that actually overhang: measuring the whole set lets a sign at
    y=100 clamp the lift to 100, sliding the sign for no reason and leaving the
    dialogue behind the scrim anyway. It is not per-rect either, because two-line
    dialogue is two rects with different overhangs and lifting each by its own
    collapses them onto each other. Both paths now take the same `subs_lift`
    expression from app.rs (an open track menu counts — that is exactly when the
    bottom of the screen is being read) and share the baseline/ceiling constants.

The store's byte ceiling had to change ends. `v[0]` is the cue AT the playhead
once the time-retain has run, and the tail is the demuxer's 10-20s read-ahead, so
evicting index 0 blanked the subtitle being read while keeping cues nobody had
reached. A set costing up to 8 rects instead of 1 is what made that reachable.
It now drops a cue the playhead has already passed, and only falls back to the
far end of the read-ahead when there is none; the test fails against the old
policy. Two guards on the decode side went in with it: `rect_to_rgba` null-checks
the rect (the loop walks up to 8 now, not just [0]) and bounds w/h before
multiplying, since `usize` is 32 bits here and a wrapped `w*h*4` would size the
buffer below what the write loop and glTexImage2D then read.

The renderer keeps one texture per rect and re-specs the ids it can reuse, so
the steady one-rect case still never allocates twice; its cache key gains the
selected track, because two image tracks of one file routinely start a display
set on the same pts and the timestamp alone left the outgoing track on screen.

Tests: +7 host (65)

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_018GuSHrq9oydnX7nhKE1Hvc
Copilot AI review requested due to automatic review settings July 29, 2026 18:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@GLinnik21
GLinnik21 merged commit 4a862ac into main Jul 30, 2026
@GLinnik21
GLinnik21 deleted the unit-14-image-subtitle-canvas branch August 2, 2026 18:37
Goo6i added a commit to Goo6i/plx-native that referenced this pull request Oct 24, 2026
An Arabic or Hebrew paragraph (its first strong character, UAX plxnative#9 P2/P3)
is drawn right-aligned in a left-aligned TextView; a wrapped line that
opens with a Latin word keeps the paragraph's direction with an RLM.
A truncated block dissolves at its left end into a MORE on the left, and
the About card's MORE follows. Marquees glide mirrored: a right-to-left
run rests on its first words at the window's right edge and moves right;
the paused player title reveals its end to the left.

Signed-off-by: Goo6i <[email protected]>
GLinnik21 pushed a commit that referenced this pull request Oct 29, 2026
Arabic and Hebrew in titles, summaries, cast names and menus drew as boxes. They now draw shaped and in reading order, through the HarfBuzz, FriBidi and FreeType that libass-plx already bundles. No crate is added and the screen layout stays left-to-right.

- libass-plx ABI 4: `src/ass_text.c` adds five `plx_ass_text_*` calls (bidi levels and order, shaping, raster). `NATIVE_ABI` is 4 in `player/ass.rs` and in the new `gfx/src/rtl.rs`, a gated `dynlib!` table loaded by absolute path from the app directory. Without the library, or at another ABI, Arabic and Hebrew draw as boxes as before; subtitles and RTL UI text share `libass-plx.so.0`, so a library at the wrong ABI turns both off. The Makefile keeps `ass_text.c` out of the app ELF, so no FreeType/HarfBuzz/FriBidi symbol is added to `plxnative`.
- Fonts: Noto Kufi Arabic 2.110 and Noto Sans Hebrew 3.001, regular and bold, unmodified (OFL 1.1, 312 KB), fetched by `tools/fetch-noto-rtl.py`, in every font list; `ci/test_shipped_faces.py` holds the lists to the files `text.rs` opens.
- Layout: Arabic and Hebrew are links 4 and 5 of the fallback chain. A line holding right-to-left text is cut into bidi runs; shaped runs go through the facade, the rest through SDL2_ttf as before. Latin, CJK and emoji text draw as before.
- Direction follows a paragraph's first strong character (UAX #9 P2/P3). Right-to-left summaries are right-aligned with the fade and MORE on the left, marquees rest on their first words and glide right, and each wrapped line is pinned to its block's direction (RLM in a right-to-left block, LRM in a left-to-right one), since each drawn line is its own FriBidi paragraph.
- `plx_ui::label` gains `HAlign::Start`, resolved by the line's direction; `TextView` and the About card's title and genres use it instead of a screen-local rule.

Tests: 49 new, plus the LRM and `HAlign::Start` tests. The firmware load matrix grades `plxnative` and `libass-plx.so.0` OK on 4.4.2 to 11.2.0; the armv7 binary grows about 16 KB. Checked by the author on a webOS 6.5.3 set with Arabic metadata (Home, Library, Detail, About card); the subtitle track menu was not checked on a set. The Arabic was reviewed by a native speaker; no Hebrew reader has reviewed the Hebrew.

Closes #760

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.

2 participants