// // MP42SupConverter.swift // MP42Foundation // // Objective-C entry point for the vendored sup2vob converter // (https://git.technomancer.com/Scott-Files/sup2vob): BLU-RAY PGS .sup → // VobSub .sub/.idx. MP42VobSubImporter drives it so a dragged-in .sup lands // on the framework's existing VobSub import/mux path unchanged. // import Foundation @objc(MP42SupConverter) public class SupConverter: NSObject { @objc public static let errorDomain = "org.galad.MP42Foundation.SubConverter" /// Converts the BLU-RAY PGS `inputURL` (.sup) to a VobSub pair: the .sub is /// written to `subURL` and the matching .idx beside it (same stem, `.idx` /// extension). `language`/`languageName` only decorate the .idx `id:`/`alt:` /// lines — the importer overwrites the track language from the source /// filename, the way the .srt importer does. /// /// 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, language: String, languageName: String) throws { let fileBytes = [UInt8](try Data(contentsOf: inputURL)) let compositions: [Composition] do { compositions = try readPGS(fileBytes, space: .bt709) } catch let supError as PGSError { throw conversionError(100, "\(inputURL.lastPathComponent): \(supError)") } guard !compositions.isEmpty else { throw conversionError(101, "\(inputURL.lastPathComponent): no compositions found") } // 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") } let subFile = writeSub(result.frames) let idxText = writeIdx(resolution: resolution, fourColorPalette: result.fourColorPalette, frames: result.frames, filePositions: subFile.filePositions, lang: language, langName: languageName) let idxURL = subURL.deletingPathExtension().appendingPathExtension("idx") try Data(subFile.bytes).write(to: subURL) try idxText.write(to: idxURL, atomically: true, encoding: .ascii) } private static func conversionError(_ code: Int, _ message: String) -> NSError { NSError(domain: errorDomain, code: code, userInfo: [NSLocalizedDescriptionKey: message]) } }