# sup2vob A small native Swift command-line tool that converts Blu-ray PGS `.sup` subtitle files into VobSub `.sub` + `.idx` pairs — the classic DVD subtitle format that ffmpeg, mkvmerge, VLC, and DirectVobSub all read. No external dependencies beyond the macOS Swift toolchain. ## Requirements - macOS with the Xcode command-line tools (`swiftc`) - `make` ## Build & install ```sh make # builds ./sup2vob (swiftc -O over all *.swift) make rebuild # clean + build make clean sudo make install # installs to /usr/local/bin (override with PREFIX=...) ``` All four `.swift` files compile together as one flat module — there is no SwiftPM manifest or Xcode project. Note that companion tooling that resolves `sup2vob` from `PATH` only sees a new build after `make install`. ## Usage ```sh sup2vob [options] ``` Example: ```sh sup2vob --pal --lang eng --langname English movie.sup # writes movie.sub + movie.idx ``` | Option | Meaning | |---|---| | `-o ` | Output `.sub` path; a matching `.idx` is written beside it. Default: the input path with `.sup` replaced by `.sub`. | | `--ntsc` / `--pal` | Force a 720x480 or 720x576 DVD frame. Default: picked automatically from the PGS frame-rate code (25/50-family rates → PAL, otherwise NTSC). | | `--full` | HD passthrough: keep the source frame size (e.g. 1920x1080) with no scaling. | | `--lang ` | Two-letter language id written to the `.idx` (default `en`). | | `--langname ` | Language name for the commented `alt:` line (default `English`). | | `--bt601` | Interpret the PGS palette as BT.601 (default BT.709). | | `--debug` | Trace per-frame conversion decisions on stderr. | The conversion summary and all errors go to **stderr**; stdout is only used for `--help`. ## How it works The pipeline is three stages, one per source file: **`PGS.swift` — parsing.** Reads the 13-byte `PG` segment envelope (the ffmpeg/mkvextract flavor) and dispatches on segment type: PCS (canvas size, frame rate, object references), PDS (palette — updates are sparse and carry absolute entry ids, so entries are written at their own id), and ODS (RLE objects, which may arrive split across continuation segments and are decoded at composition-flush time). Palette colors are converted from YCbCr to RGB in the chosen color space. The RLE and buffering grammar mirrors ffmpeg's `pgssubdec.c`, including its linear-fill quirk. **`Convert.swift` — pixels.** Turns PGS compositions into DVD subtitle frames. Display end times follow PGS "clear screen" semantics (the next composition closes the previous frame). Offscreen frames are downscaled with an area-average filter weighted by alpha coverage; since DVD has no real alpha, a destination pixel survives only if its coverage reaches 50%. Frames are then cropped to their opaque bounding box, never dropping below 2 rows (ffmpeg's dvdsub decoder requires `h > 1`). Finally everything is color- reduced to a **single shared 4-color palette for the whole file**: VobSub's `tridx: 1000` convention makes only 2-bit code 0 transparent, so every frame must use the same three opaque colors (the globally most-used three). `--full` skips the scaling and cropping and passes the source geometry through verbatim. **`VobSub.swift` — writing.** Encodes each frame as a classic DVD SPU: interlaced even/odd fields of 2-bit run-length nibble data (the exact 1–4-nibble code forms ffmpeg's `dvdsubenc` emits), plus SPU command blocks for colormap, alpha, position, field offsets, and start/end dates — the date words being durations *relative to the packet PTS*, as every consumer implements it. SPUs are packaged into the MPEG program-stream layout of `.sub` files: constant PACK header + PES packets (first fragment carries a 40-bit PTS) sliced into 0x800-byte sectors, with a padding PES closing the last sector. The matching `.idx` is plain v7 VobSub-index text listing the palette and one `timestamp:`/`filepos:` entry per frame. Output follows the byte-level conventions those tools actually accept — for example the PTS prefix dialect mkvmerge checks for, and the identity colormap form ffmpeg reads — which is why several constants look unusual; the comments in each file record why each quirk exists. ## Debugging `--debug` prints per-frame drop/scale decisions to stderr and dumps frame 0's index plane as a grayscale PGM to `/tmp/suptest/dbg_frame0.pgm` (the directory must already exist). Generated `.sub` files can be spot-checked with `ffmpeg`/`ffprobe`, or handed to `mkvmerge` to confirm the container. ## Source layout | File | Role | |---|---| | `main.swift` | CLI parsing, file I/O, orchestration | | `PGS.swift` | PGS segment/palette/RLE parsing | | `Convert.swift` | Scaling, cropping, 4-color reduction | | `VobSub.swift` | SPU encoding, `.sub` container, `.idx` writer |