Skip to content

Text subtitles: decode legacy-encoded tracks (GBK/Big5/Shift_JIS/EUC-KR/…) per track with chardetng (#628) - #708

Merged
GLinnik21 merged 6 commits into
mainfrom
fix-628-subtitle-charset
Oct 15, 2026
Merged

GLinnik21 merged 6 commits into
mainfrom
fix-628-subtitle-charset

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Closes #628

Embedded and sidecar text subtitles in a legacy encoding (GBK, Big5, Shift_JIS, EUC-KR, windows-125x, …) rendered as mojibake: the cue bytes went through from_utf8_lossy. Fonts were not the cause — the 0.8.0 ipk already ships appfont-cjk.ttf.

What changes

  • New media/src/player/sub_charset.rs: a per-track TrackCharset decided once and then reused for every cue. Order: BOM → valid UTF-8 (zero-copy Cow) → chardetng (locks after 160 non-ASCII bytes) → lossy. chardetng gets a TLD hint from the stream's language tag (zh-Hans/CN/SG → cn, zh-Hant/TW → tw, zh-HK/MO → hk, ja → jp, ko → kr, ru, …); an unqualified chi/zho/zh gives no hint, because a cn hint made a Big5 track decode as GBK on the TV.
  • ff_subs.rs decodes per track; SubCue::Text carries Cow<[u8]>. ff.rs gains stream_language() and shares one tag reader with stream_name() (simplify pass).
  • sidecar.rs: the bare-file fallback uses the same detection over the whole file.
  • Dependency: chardetng on top of the already-pinned encoding_rs 0.8.35; THIRD-PARTY-NOTICES + licenses/WHATWG-BSD-3-Clause.txt.
  • Regression fixture tests/fixtures/gbk-subrip.mkv (588 B) with test ff::subtitle_charset_tests, run by the Makefile check-ffmpeg target.

Verification

  • The Big5-read-as-GBK regression test was watched failing before d0858b0 and passing after.
  • Committed tree 93d3e97: make check exit 0 (every test result: ok, 0 failed); cargo check --lib --no-default-features clean. make check-ffmpeg (the fixture test) not run locally — CI.
  • TV (LG webOS 4.5, mock guest server): GBK, Big5, Shift_JIS and EUC-KR SRT tracks all render correctly.
  • ARM release binary: 16,070,496 → 16,350,700 bytes (+280 KB, +1.7 %; chardetng's tables).

make build-bench (same sitting, M4 10 cores, 3 runs; host load was above core count throughout, so treat deltas inside the spread as noise; the base checkout had no target-fast, so its incremental rows are skipped)

Base:

Scenario base 64a580a (median, min–max)
No-op host test build 0.1 s (0.1–0.2)
Edit leaf plx_base cbuf.rs, non-incr 26.2 s (26.0–33.4)
Edit leaf plx_machine, non-incr 25.1 s (24.6–29.6)
Edit leaf plx_platform, non-incr 24.8 s (24.0–28.7)
Edit leaf plx_gfx, non-incr 11.6 s (11.0–13.7)
Edit leaf plx_net, non-incr 20.6 s (20.5–29.6)
Edit leaf plx_ui, non-incr 10.7 s (10.5–19.6)
Edit leaf plx_plex, non-incr 20.5 s (20.0–23.9)
Edit leaf plx_telemetry, non-incr 17.5 s (17.3–28.3)
Edit leaf plx_data, non-incr 18.5 s (15.4–24.5)
Edit leaf plx_session, non-incr 12.3 s (9.3–20.8)
Edit leaf plx_media, non-incr 14.6 s (10.8–23.6)
Edit leaf plx_appkit, non-incr 9.6 s (8.3–18.9)
Edit leaf plx_screens, non-incr 8.8 s (7.7–18.2)
Edit leaf app coldstart.rs, non-incr 4.0 s (3.8–9.6)
Edit hub plx_ui lib.rs, non-incr 11.5 s (10.1–23.4)
Edit leaf / hub, incremental skipped (no target-fast in base checkout)
Unit suite 79.6 s (73.3–126.3), 7511 passed / 0 failed / 20 ignored

fix-628-subtitle-charset (93d3e97):

Scenario median (min–max)
No-op host test build 0.1 s (0.1–0.1), 0/182 units
Edit leaf plx_base cbuf.rs, non-incr 23.1 s (22.5–24.4)
Edit leaf plx_machine, non-incr 22.0 s (21.6–23.7)
Edit leaf plx_platform, non-incr 21.9 s (20.9–26.5)
Edit leaf plx_gfx, non-incr 9.9 s (9.7–15.3)
Edit leaf plx_net, non-incr 18.4 s (17.6–22.6)
Edit leaf plx_ui, non-incr 9.6 s (9.6–10.8)
Edit leaf plx_plex, non-incr 18.0 s (17.5–20.4)
Edit leaf plx_telemetry, non-incr 16.9 s (15.3–17.7)
Edit leaf plx_data, non-incr 15.1 s (13.2–17.4)
Edit leaf plx_session, non-incr 8.7 s (7.8–9.5)
Edit leaf plx_media, non-incr 9.8 s (9.6–12.8)
Edit leaf plx_appkit, non-incr 7.3 s (7.1–9.1)
Edit leaf plx_screens, non-incr 6.8 s (6.7–8.6)
Edit leaf app coldstart.rs, non-incr 3.2 s (3.2–4.2)
Edit hub plx_ui lib.rs, non-incr 9.2 s (8.8–12.3)
Edit leaf plx_base, incremental 9.8 s (9.3–10.4)
Edit hub plx_ui, incremental 3.4 s (3.2–3.6)
Unit suite 56.4 s (56.2–67.6), 7521 passed / 0 failed / 21 ignored

Units 180 → 182 (chardetng and its dependency); no build-time regression is visible above the noise.

🤖 Generated with Claude Code

GLinnik21 and others added 5 commits October 14, 2026 19:52
…rough a per-track charset step (#628)

Embedded subrip/SRT/webvtt/mov_text packets and the sidecar bare fallback were
decoded with String::from_utf8_lossy, so a GBK Chinese track rendered as a row of
U+FFFD. player::sub_charset::subtitle_text takes BOM, then valid UTF-8 (zero-copy),
then the legacy encoding the stream's language tag implies (encoding_rs, pure Rust),
decided once per track by its first non-UTF-8 packet. Host-FFmpeg test on a 588-byte
GBK subrip MKV fixture.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… its language (#628)

Replaces the language table with chardetng (pure Rust, on encoding_rs): any legacy
encoding is detected from the track's first non-UTF-8 packets, locked after 160
non-ASCII bytes; the sidecar fallback detects over the whole file.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… (Big5 cue was read as GBK) (#628)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
assert!(avformat_open_input(&mut fmt, path.as_ptr(), std::ptr::null_mut(), std::ptr::null_mut()) >= 0);
let mut pkt = av_packet_alloc();
assert!(!pkt.is_null());
let mut subs = SubTracks::open(fmt, (*fmt).streams);
…it opened

CodeQL rust/access-invalid-pointer flagged the (*fmt) dereference in open() and
the new test's. A null format context now yields no tracks instead of a fault.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GLinnik21
GLinnik21 enabled auto-merge (squash) October 15, 2026 00:33
@GLinnik21
GLinnik21 merged commit c33b1b1 into main Oct 15, 2026
19 checks passed
@GLinnik21
GLinnik21 deleted the fix-628-subtitle-charset branch October 15, 2026 00:37
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.

Chinese characters in embedded SRT subtitles display as garbled text / unreadable

2 participants