Compare commits

..
2 Commits
Author SHA1 Message Date
Scott GarrettandClaude Code 72e879e316 Add a slim sup2vob CLI built from the vendored Sup2Vob sources
The extractsubs helper shipped in the Subler app bundle needs sup2vob
on PATH, so the converter gets a CLI build beside the framework target.
Slim by design: this directory holds only upstream's main.swift and a
Makefile that compiles it against ../MP42/Sup2Vob/ — the exact files the
framework builds, so an upstream merge updates app and CLI in one pass
and the two can never silently disagree. The CLI's --full output is the
byte-identical oracle for the framework converter (verified: same
sub/idx bytes for a full movie track).

main.swift is upstream verbatim except for the documented globals seam:
the CLI's debugFrames/fullFramePassthrough declarations belong to the
vendored Convert.swift, so main.swift drops its re-declaration and only
assigns them (--debug/--full). The framework facade
(MP42SupConverter.swift) is deliberately not part of the CLI build.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-28 23:06:27 -05:00
Scott GarrettandClaude Code 837aa1c098 Keep the PGS source resolution through the sup2vob conversion
VobSub conversions of PGS material were downscaled to the 720x480/576
DVD frame — a 1080p source came out of the mux at one quarter of its
authored size. The framework's sup2vob façade now always runs the
upstream CLI's `--full` mode: fullFramePassthrough keeps every frame's
object geometry verbatim, and the idx size line (which the VobSub
importer carries into track.width/height and the muxer writes into the
subpic track) is the PGS's own PCS canvas, e.g. 1920x1080. The
frame-rate-derived DVD frame stays as fallback for streams that define
no usable canvas. The conversion output is byte-identical to running
`sup2vob --full` on the same .sup.

The vendored upstream sources stay untouched apart from one comment —
the passthrough is enabled in the adaptation layer, same as the CLI
does with its flag. This applies to both PGS paths, which share the
conversion: .sup drag-import and PGS-from-MKV via
MP42PgsToVobSubConverter.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-28 21:13:57 -05:00
7 changed files with 214 additions and 15 deletions

No files matched your search

+5 -4
View File
@@ -148,10 +148,11 @@ MP42_OBJC_DIRECT_MEMBERS
track.converter = pgsConverter;
format = track.conversionSettings.format;
// The converted subtitles live in the DVD frame (720x480/576),
// not the PGS source video's frame — and an MKV-sourced track
// carries no size at all — so steer the subpic branch's
// trackWidth/trackHeight preference with it.
// The converted subtitles keep the PGS's own canvas size
// (sup2vob runs in full-frame passthrough, e.g. 1920x1080)
// — and an MKV-sourced track carries no size at all — so
// steer the subpic branch's trackWidth/trackHeight
// preference with the converter's frame size.
MP42SubtitleTrack *subTrack = (MP42SubtitleTrack *)track;
subTrack.trackWidth = pgsConverter.frameWidth;
subTrack.trackHeight = pgsConverter.frameHeight;
+6 -5
View File
@@ -164,9 +164,9 @@ MP42_OBJC_DIRECT_MEMBERS
// Phase 2 — the vendored sup2vob conversion rides the VobSub importer
// wholesale: its .sup support already converts to a temp idx/sub pair
// (DVD resolution, global 4-color palette) and its demux emits the
// raw-SPU subpic samples with gap-clearing that the subp mux path
// wants. Re-inventing any of it here would just drift.
// (the PGS canvas resolution, global 4-color palette) and its demux
// emits the raw-SPU subpic samples with gap-clearing that the subp
// mux path wants. Re-inventing any of it here would just drift.
_tempDirectory = [[NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]
URLByAppendingPathComponent:[NSString stringWithFormat:@"pgsvob-%@", [NSUUID UUID].UUIDString]];
[[NSFileManager defaultManager] createDirectoryAtURL:_tempDirectory withIntermediateDirectories:YES attributes:nil error:NULL];
@@ -219,8 +219,9 @@ MP42_OBJC_DIRECT_MEMBERS
return nil;
}
// The idx `size:` line carries the DVD frame sup2vob rendered into
// (MP42VobSubImporter stores it as the track's width/height).
// The idx `size:` line carries the frame sup2vob rendered into —
// the PGS's own canvas (MP42VobSubImporter stores it as the
// track's width/height).
_frameWidth = (uint32_t)vobTrack.width;
_frameHeight = (uint32_t)vobTrack.height;
+3 -2
View File
@@ -17,8 +17,9 @@ import Foundation
nonisolated(unsafe) var debugFrames = false
/// Set by the CLI's `--full`: keep the source frame size with no scaling.
/// Upstream declares this in main.swift; inside the framework it stays false —
/// imports always downscale to the DVD frame the muxer writes.
/// Upstream declares this in main.swift; inside the framework
/// MP42SupConverter turns it on for every import — the muxed subpic track
/// keeps the PGS's own canvas size instead of the DVD frame.
nonisolated(unsafe) var fullFramePassthrough = false
// MARK: - DVD subtitle frame (post scale + color reduction)
+21 -4
View File
@@ -20,9 +20,12 @@ import Foundation
/// lines — the importer overwrites the track language from the source
/// filename, the way the .srt importer does.
///
/// Mirrors sup2vob's CLI orchestration: PAL/NTSC picked automatically from
/// the PGS frame-rate code, BT.709 palette interpretation, frames
/// downscaled to the DVD frame the muxer writes.
/// Mirrors sup2vob's CLI orchestration with `--full`: BT.709 palette
/// interpretation, and the source geometry is kept — frames stay at the
/// resolution the PGS itself defines (its PCS canvas, e.g. 1920x1080),
/// so the muxed subpic track matches the subtitles' authored size
/// instead of being downscaled to the DVD frame. The DVD frame from the
/// frame-rate code stays as fallback for a stream with no usable canvas.
@objc(convertSupAtURL:toSubAtURL:language:languageName:error:)
public static func convertSup(at inputURL: URL,
toSubAt subURL: URL,
@@ -40,7 +43,21 @@ import Foundation
throw conversionError(101, "\(inputURL.lastPathComponent): no compositions found")
}
let resolution = DVDResolution.from(frameRateCode: compositions[0].frameRateCode)
// Upstream's `--full` mode: keep the source frames verbatim. The
// CLI's main.swift set this global from the flag; enabling it here
// is the framework's one behavioral adaptation of the vendored
// sources (upstream's logic and geometry code stay untouched).
fullFramePassthrough = true
// The size the muxer writes: the display resolution the PGS itself
// declares in its PCS canvas (1920x1080 for a full-HD source). Fall
// back to the frame-rate's DVD frame only if the stream defines no
// usable canvas.
let canvasWidth = compositions[0].canvasWidth
let canvasHeight = compositions[0].canvasHeight
let resolution = canvasWidth > 0 && canvasHeight > 0
? DVDResolution(width: canvasWidth, height: canvasHeight)
: DVDResolution.from(frameRateCode: compositions[0].frameRateCode)
let result = convertToDVD(compositions, resolution: resolution)
guard !result.frames.isEmpty else {
throw conversionError(102, "\(inputURL.lastPathComponent): no displayable subtitle frames")
+2
View File
@@ -0,0 +1,2 @@
# build artifact (see Makefile)
/sup2vob
+37
View File
@@ -0,0 +1,37 @@
# sup2vob — PGS (.sup) -> VobSub (.sub/.idx) converter CLI
#
# Slim CLI: the conversion sources are NOT vendored a second time here —
# they are the framework's ../MP42/Sup2Vob/ files, so the CLI and the app
# always run identical conversion code (the CLI's byte-identical `--full`
# output is the framework converter's verification oracle). Only
# main.swift lives in this directory; see its header for the single
# upstream adaptation. MP42SupConverter.swift (the framework's ObjC
# facade over the same conversion) is deliberately not compiled in.
#
# Build: make -> ./sup2vob
# Clean: make clean
SWIFTC ?= swiftc
OPTFLAGS ?= -O
TARGET := sup2vob
PREFIX ?= /usr/local
CONV := ../MP42/Sup2Vob
SOURCES := main.swift $(CONV)/PGS.swift $(CONV)/Convert.swift $(CONV)/VobSub.swift
.PHONY: all clean rebuild install
all: $(TARGET)
$(TARGET): $(SOURCES)
$(SWIFTC) $(OPTFLAGS) -o $(TARGET) $(SOURCES)
clean:
rm -f $(TARGET)
rebuild: clean all
# extractsubs (shipped in the Subler app bundle's Resources) resolves
# sup2vob from PATH, so a rebuild only takes effect once installed
# (usually: sudo make install).
install: $(TARGET)
install -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(TARGET)
+140
View File
@@ -0,0 +1,140 @@
//
// main.swift
// sup2vob CLI
//
// sup2vob's CLI entry point (https://git.technomancer.com/Scott-Files/
// sup2vob) kept identical to upstream with one adaptation: upstream
// declares the CLI globals `debugFrames` and `fullFramePassthrough` here,
// but this slim build shares the conversion sources with the framework
// target, where Convert.swift owns both declarations — re-declaring them
// would collide. Assignments ("--debug", "--full") stay upstream-exact.
// The Makefile compiles this file against ../MP42/Sup2Vob/, so the CLI and
// the framework always run the same conversion code.
//
import Foundation
let usage = """
usage: sup2vob [options] <input.sup>
Converts a Blu-ray PGS .sup file to VobSub (.sub + .idx)
options:
-o <file> output .sub path (a matching .idx is written beside it);
default: <input> with .sup replaced by .sub
--ntsc force a 720x480 DVD frame (default: auto from the PGS rate)
--pal force a 720x576 DVD frame (default: auto from the PGS rate)
--full pass through the source frame size (e.g. 1920x1080) with
no scaling
--lang <id> two-letter language id for the .idx (default: en)
--langname <n> alternative 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
-h, --help show usage and exit
"""
var outputPath: String? = nil
var input: String? = nil
var forcedResolution: DVDResolution? = nil
// fullFramePassthrough: declared in Convert.swift (see file header).
var lang = "en"
var langName = "English"
var colorSpace: ColorSpace = .bt709
var args = Array(CommandLine.arguments.dropFirst())
while !args.isEmpty {
let arg = args.removeFirst()
switch arg {
case "-h", "--help":
print(usage)
exit(0)
case "-o":
guard !args.isEmpty else { FileHandle.standardError.write("missing argument for -o\n".data(using: .utf8)!); exit(1) }
outputPath = args.removeFirst()
case "--ntsc": forcedResolution = .ntsc
case "--pal": forcedResolution = .pal
case "--full": fullFramePassthrough = true
case "--lang":
guard !args.isEmpty else { FileHandle.standardError.write("missing argument for --lang\n".data(using: .utf8)!); exit(1) }
lang = args.removeFirst()
case "--langname":
guard !args.isEmpty else { FileHandle.standardError.write("missing argument for --langname\n".data(using: .utf8)!); exit(1) }
langName = args.removeFirst()
case "--bt601": colorSpace = .bt601
case "-bt601", "--bt709": colorSpace = .bt709
case "--debug": debugFrames = true
default:
if arg.hasPrefix("-") {
FileHandle.standardError.write("unknown option: \(arg)\n".data(using: .utf8)!)
exit(1)
}
if input == nil { input = arg }
}
}
guard let input else {
FileHandle.standardError.write(usage.data(using: .utf8)!)
exit(1)
}
guard let fileData = FileManager.default.contents(atPath: input) else {
FileHandle.standardError.write("cannot read \(input)\n".data(using: .utf8)!)
exit(1)
}
let fileBytes = [UInt8](fileData)
let compositions: [Composition]
do {
compositions = try readPGS(fileBytes, space: colorSpace)
} catch let e as PGSError {
FileHandle.standardError.write("\(input): \(e)\n".data(using: .utf8)!)
exit(1)
} catch {
FileHandle.standardError.write("\(input): \(error)\n".data(using: .utf8)!)
exit(1)
}
guard !compositions.isEmpty else {
FileHandle.standardError.write("\(input): no compositions found\n".data(using: .utf8)!)
exit(1)
}
let resolution = forcedResolution ?? (fullFramePassthrough
? DVDResolution(width: compositions[0].canvasWidth, height: compositions[0].canvasHeight)
: DVDResolution.from(frameRateCode: compositions[0].frameRateCode))
let result = convertToDVD(compositions, resolution: resolution)
if debugFrames, let frame = result.frames.first {
let indexToGrayLUT: [UInt8] = [0, 85, 170, 255]
var pgm = "P5\n\(frame.width) \(frame.height)\n255\n".data(using: .ascii)!
pgm.append(contentsOf: frame.paletteIndices.map { indexToGrayLUT[Int($0)] })
try? pgm.write(to: URL(fileURLWithPath: "/tmp/suptest/dbg_frame0.pgm"))
FileHandle.standardError.write("debug: frame0 idx → /tmp/suptest/dbg_frame0.pgm (\(frame.width)x\(frame.height) x=\(frame.left) y=\(frame.top))\n".data(using: .utf8)!)
}
guard !result.frames.isEmpty else {
FileHandle.standardError.write("\(input): no displayable subtitle frames\n".data(using: .utf8)!)
exit(1)
}
let subPath = outputPath ?? (input.hasSuffix(".sup") ? String(input.dropLast(4)) + ".sub" : input + ".sub")
let idxPath = (subPath as NSString).deletingPathExtension + ".idx"
let subFile = writeSub(result.frames)
let idxText = writeIdx(resolution: resolution, fourColorPalette: result.fourColorPalette,
frames: result.frames, filePositions: subFile.filePositions,
lang: lang, langName: langName)
do {
try Data(subFile.bytes).write(to: URL(fileURLWithPath: subPath))
try idxText.write(toFile: idxPath, atomically: true, encoding: .ascii)
} catch {
FileHandle.standardError.write("write failed: \(error)\n".data(using: .utf8)!)
exit(1)
}
let visiblePaletteHex = result.fourColorPalette.dropFirst().filter { $0.isVisible }
.map { String(format: "#%02x%02x%02x", $0.r, $0.g, $0.b) }.joined(separator: " ")
FileHandle.standardError.write("""
sup2vob: \(result.frames.count) frames → \(subPath) (\(subFile.bytes.count) bytes) + \(idxPath)
frame size \(resolution.width)x\(resolution.height), palette: \(visiblePaletteHex)
""".data(using: .utf8)!)