Handling Every Subtitle Format: Remux Extraction, PGS Clear Sets, and CJK Encodings
How Vela Player 1.1.0 (commit 88f350f) extracts and renders embedded PGS, VobSub, ASS, and text subtitles across MKV, MP4, and M2TS without abandoning hardware decoding — covering subsink.c generation tokens, PGS empty clear packets, libass storage_size scaling, cloud-drive sibling subtitles, and GB18030 detection.
Vela Player defaults to HarmonyOS’s system AVPlayer for hardware decoding and power efficiency. The trade-off is subtitle coverage: AVPlayer cannot render bitmap subtitles (PGS/VobSub), skips several container-embedded tracks, and has no ASS styling support.
Commit 88f350f in 1.1.0 unified the entire subtitle pipeline across MKV, MP4, and M2TS containers, bitmap and styled tracks, cloud-drive sibling files, and legacy CJK encodings — without forcing software decoding unless truly necessary.
Probing MKV, MP4, and 192-byte M2TS up front
Before opening a player engine, services/MediaProbe.ets inspects up to 256 KB (capped at an 8 MB scan window) in pure ArkTS:
- Matroska / WebM: parses EBML
TracksforS_TEXT/UTF8,S_TEXT/ASS,S_HDMV/PGS, andS_VOBSUB. - MP4 / MOV: walks
moov -> trak -> mdia, matchinghdlrhandlerssbtl,text, andsubp. - TS / Blu-ray M2TS: handles both 188-byte TS packets and 192-byte BDAV M2TS packets (4-byte timestamp prefix + 188-byte packet), parsing PAT -> PMT for stream type
0x90(S_HDMV/PGS) and0x06with descriptor0x59(S_DVBSUB).
When a user selects an embedded bitmap or incompatible subtitle track while AVPlayer can still hardware-decode the video, Vela routes playback through transcoder.c and extracts subtitles directly during demuxing.
MqSubSink: bound to the bridge slot, not the transcoder
Because MPEG-TS cannot carry text subtitles for AVPlayer and bitmap subtitles render directly on an ArkUI Image overlay, transcoder.c’s worker_main invokes collect_subtitle() on every subtitle packet and writes into entry/src/main/cpp/transcode/subsink.c.
Crucially, MqSubSink is owned by http_bridge.c’s MqSlot, not by an individual MqTc transcoder instance. Every time AVPlayer seeks, it closes the HTTP socket and spawns a fresh MqTc at the new byte offset. If the subtitle buffer died with MqTc, seeking would wipe every extracted cue.
Sharing one sink across restarts introduces a race: a cancelled seek probe or slow dying worker can emit stale cues after a newer worker starts. mq_subsink_claim() solves this with a 64-bit generation token owner_gen. Each new MqTc increments owner_gen and rewinds the watermark to startMs - 1000ms (trimming any queued future bitmap frames past that mark), so late writes from superseded workers fail gen != s->owner_gen and drop silently.
Because 4K Blu-ray remuxes routinely carry 40+ PGS tracks (testing Avatar: The Way of Water hit over 40), MQ_SUB_MAX_TRACKS was raised to 64 — previously 32 silently ignored Chinese tracks sitting near the end of the stream list.
Text quirks and PGS clear-screen packets
Text subtitles (ASS / SRT / MOV_TEXT / VTT) pass through ass_to_plain() to become clean SRT queues:
- Vector drawings: styled ASS scripts use
{\p1}m 0 0 l 100 0 ...{\p0}to draw shapes. Simply stripping{...}braces dumps thousands of coordinate numbers onto the screen.ass_drawing_after()tracks the active\p<n>state across override blocks and drops everything between\p1and\p0. - Bilingual same-timestamp lines: deduplicating seek-back overlaps by
startMs <= last_startdropped the second language whenever Chinese and English lines shared a timestamp. Changing the cutoff tostartMs < last_startand keeping up toMQ_SUB_MAX_SAME = 6FNV-1a text hashes (same_hash) per timestamp preserves both lines.
Bitmap subtitles (PGS / VobSub / DVB) composite multiple AVSubtitleRect regions via mq_compose_sub_bitmap() into a single bounding-box RGBA buffer and enter a ring queue (MQ_SUB_MAX_BMP = 24 frames per track, MQ_SUB_MAX_BMP_BYTES = 24 MiB total across all tracks).
Real PGS testing hit a strange bug: the first line stayed frozen on screen for half a minute. PGS decoders emit end_display_time = UINT32_MAX and rely on the next display set — often an empty packet with num_rects == 0 (“clear screen”) — to end the previous line. Previously, num_rects == 0 was discarded as an empty composition failure. Now collect_subtitle() calls mq_subsink_bitmap_close_gen(tc->sink, tc->sinkGen, pkt->stream_index, startMs) on every decoded display set before checking num_rects > 0, backed by an MQ_SUB_OPEN_CAP_MS = 15000 safety ceiling in case a packet is lost.
ass_render.c and the 4K storage_size trap
When rendering standalone ASS files via entry/src/main/cpp/subtitle/ass_render.c, HarmonyOS lacks fontconfig, so mq_ass_open() configures ASS_FONTPROVIDER_NONE with the first available system CJK font (HarmonyOS_Sans_SC_Regular.ttf, NotoSansCJK-Regular.ttc, etc.).
To avoid compositing full 3840×2160 RGBA frames per tick on 4K video, mq_ass_open() caps the rendering canvas at 1920×1080 (MQ_ASS_MAX_W / MQ_ASS_MAX_H). On the first test run, every \pos, \move, and \clip annotation flew off-screen — because I forgot ass_set_storage_size(ren, canvasW, canvasH). libass scales absolute script coordinates using the ratio between frame_size and storage_size; omitting storage_size makes it interpret 4K coordinates against a 1080p script assumption.
Cloud-drive sibling subtitles and CJK encodings
External subtitles on Quark, UC, Tianyi, China Mobile, and AList cannot store permanent URLs at scan time — signatures expire, and Quark binds download URLs to the __puus cookie snapshot active when the link was signed:
CloudSubtitles.etsstores lazyvela-cloud://references and fetches the direct link plus matching headers only when selected, caching for10 minutes(TTL_MS) and invalidating viaclearSource()on connection changes.PlayRequestFactorybounds startup directory discovery toSUB_BUDGET_MS = 1500ms so huge cloud folders never stall playback start.common/SubtitleFiles.etsorders user-imported subtitles with text tracks ahead of bitmap files (.sup/.idx), hides.subdata files paired with an.idx, and preserves exact filenames without numeric prefixes so.idx/.subpairs never break.SubtitleParser.decode()handles legacy Chinese subtitles without mojibake: it checks UTF-8 / UTF-16LE / UTF-16BE BOMs, probes null-byte patterns for BOM-less UTF-16, validates strict UTF-8 viaisValidUtf8()(rejecting overlong sequences and surrogates), and falls back cleanly toutil.TextDecoder.create('gb18030')(which covers GBK and GB2312).
Comments