More variable renaming and comment cleanup.
This commit is contained in:
1 parent
5c40c7d7e7
commit
51120481e2
5 files changed
+512
-496
No files matched your search
+195
-195
@@ -19,23 +19,23 @@ import Foundation
|
||||
|
||||
struct NibbleWriter {
|
||||
private(set) var bytes: [UInt8] = []
|
||||
private var pending: UInt8? // high nibble awaiting its low nibble
|
||||
private var pendingHighNibble: UInt8? // high nibble awaiting its low nibble
|
||||
|
||||
mutating func nibble(_ n: UInt8) {
|
||||
if let hi = pending {
|
||||
bytes.append((hi << 4) | (n & 0x0F))
|
||||
pending = nil
|
||||
mutating func nibble(_ value: UInt8) {
|
||||
if let high = pendingHighNibble {
|
||||
bytes.append((high << 4) | (value & 0x0F))
|
||||
pendingHighNibble = nil
|
||||
} else {
|
||||
pending = n & 0x0F
|
||||
pendingHighNibble = value & 0x0F
|
||||
}
|
||||
}
|
||||
|
||||
/// Pad the stream to a whole byte (only meaningful once every line has
|
||||
/// been terminated, i.e. at the end of the RLE stream).
|
||||
mutating func padToByte() {
|
||||
if let hi = pending {
|
||||
bytes.append(hi << 4)
|
||||
pending = nil
|
||||
if let high = pendingHighNibble {
|
||||
bytes.append(high << 4)
|
||||
pendingHighNibble = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,31 +43,31 @@ struct NibbleWriter {
|
||||
/// of line, so a dangling half byte would desynchronize the next line.
|
||||
/// Matches ffmpeg's encoder `if (ncnt & 1) PUTNIBBLE(0)`.
|
||||
mutating func alignLine() {
|
||||
if pending != nil { nibble(0) }
|
||||
if pendingHighNibble != nil { nibble(0) }
|
||||
}
|
||||
|
||||
private mutating func emitRunValue(_ v: Int) {
|
||||
precondition(v >= 4 && v <= 1023)
|
||||
if v <= 15 {
|
||||
nibble(UInt8(v))
|
||||
} else if v <= 63 {
|
||||
nibble(UInt8(v >> 4)); nibble(UInt8(v & 15))
|
||||
} else if v <= 255 {
|
||||
nibble(0); nibble(UInt8(v >> 4)); nibble(UInt8(v & 15))
|
||||
private mutating func emitRunValue(_ encodedValue: Int) {
|
||||
precondition(encodedValue >= 4 && encodedValue <= 1023)
|
||||
if encodedValue <= 15 {
|
||||
nibble(UInt8(encodedValue))
|
||||
} else if encodedValue <= 63 {
|
||||
nibble(UInt8(encodedValue >> 4)); nibble(UInt8(encodedValue & 15))
|
||||
} else if encodedValue <= 255 {
|
||||
nibble(0); nibble(UInt8(encodedValue >> 4)); nibble(UInt8(encodedValue & 15))
|
||||
} else {
|
||||
nibble(0); nibble(UInt8(v >> 8)); nibble(UInt8((v >> 4) & 15)); nibble(UInt8(v & 15))
|
||||
nibble(0); nibble(UInt8(encodedValue >> 8)); nibble(UInt8((encodedValue >> 4) & 15)); nibble(UInt8(encodedValue & 15))
|
||||
}
|
||||
}
|
||||
|
||||
mutating func run(_ r: Int, color c: Int) {
|
||||
var r = r
|
||||
while r > 255 { emitRunValue(255 * 4 + c); r -= 255 }
|
||||
emitRunValue(r * 4 + c)
|
||||
mutating func run(_ runLength: Int, color colorIndex: Int) {
|
||||
var remaining = runLength
|
||||
while remaining > 255 { emitRunValue(255 * 4 + colorIndex); remaining -= 255 }
|
||||
emitRunValue(remaining * 4 + colorIndex)
|
||||
}
|
||||
|
||||
/// End of line: fill the remainder (possibly zero pixels) with color c.
|
||||
mutating func endLine(color c: Int) {
|
||||
nibble(0); nibble(0); nibble(0); nibble(UInt8(c & 3))
|
||||
mutating func endLine(color colorIndex: Int) {
|
||||
nibble(0); nibble(0); nibble(0); nibble(UInt8(colorIndex & 3))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,29 +77,29 @@ struct NibbleWriter {
|
||||
/// terminator — because the decoder treats the fill code as "paint the rest of
|
||||
/// the line", so an unconditional fill would be mistaken for the next line's
|
||||
/// first run. Short runs that end the line just use their normal code.
|
||||
func encodeField(_ s: DVDSup, rows: [Int]) -> [UInt8] {
|
||||
var w = NibbleWriter()
|
||||
func encodeField(_ frame: DVDSup, rows: [Int]) -> [UInt8] {
|
||||
var writer = NibbleWriter()
|
||||
for row in rows {
|
||||
var x = 0
|
||||
let base = row * s.w
|
||||
while x < s.w {
|
||||
let c = Int(s.idx[base + x])
|
||||
var run = 1
|
||||
while x + run < s.w && Int(s.idx[base + x + run]) == c { run += 1 }
|
||||
if run >= 64 && x + run == s.w {
|
||||
w.endLine(color: c) // EOL fill paints the remaining pixels
|
||||
var col = 0
|
||||
let rowStart = row * frame.width
|
||||
while col < frame.width {
|
||||
let colorIndex = Int(frame.paletteIndices[rowStart + col])
|
||||
var runLength = 1
|
||||
while col + runLength < frame.width && Int(frame.paletteIndices[rowStart + col + runLength]) == colorIndex { runLength += 1 }
|
||||
if runLength >= 64 && col + runLength == frame.width {
|
||||
writer.endLine(color: colorIndex) // EOL fill paints the remaining pixels
|
||||
} else {
|
||||
w.run(run, color: c)
|
||||
writer.run(runLength, color: colorIndex)
|
||||
}
|
||||
x += run
|
||||
col += runLength
|
||||
}
|
||||
w.alignLine()
|
||||
writer.alignLine()
|
||||
}
|
||||
w.padToByte()
|
||||
return w.bytes
|
||||
writer.padToByte()
|
||||
return writer.bytes
|
||||
}
|
||||
|
||||
// MARK: - Classic VobSub SPU (the byte-for-byte format BDSup2Sub/mkvmerge emit)
|
||||
// MARK: - Classic VobSub SPU
|
||||
//
|
||||
// One SPU (this is exactly what mkvmerge stores per MKV block after stripping
|
||||
// the 0x20 sub-packet IDs, and what libavcodec's dvdsub decoder consumes):
|
||||
@@ -120,73 +120,73 @@ func encodeField(_ s: DVDSup, rows: [Int]) -> [UInt8] {
|
||||
// spec convention every consumer implements by ADDING them to the packet's
|
||||
// own timestamp (VLC parse.c: `i_start = i_pts + GetWBE(date) * 11ms`;
|
||||
// ffmpeg dvdsubdec: `(date << 10) / 90` ms as ASS-style relative times;
|
||||
// ffmpeg dvdsubenc feeds back the relative ASS display time; BDSup2Sub's own
|
||||
// output carries date 0 on the start block and the duration on the end one).
|
||||
// ffmpeg dvdsubenc feeds back the relative ASS display time)
|
||||
// An absolute `startTicks >> 10` here re-adds the PTS to itself: VLC queues
|
||||
// every caption minutes past its real slot (nothing shows on screen) and
|
||||
// MPlayer-derived renderers clamp/drop the late ones outright.
|
||||
func createSupFrame(_ s: DVDSup) -> [UInt8] {
|
||||
let evenRows = stride(from: 0, to: s.h, by: 2).map { $0 }
|
||||
let oddRows = stride(from: 1, to: s.h, by: 2).map { $0 }
|
||||
let even = encodeField(s, rows: evenRows)
|
||||
let odd = encodeField(s, rows: oddRows)
|
||||
|
||||
func be16(_ v: Int) -> [UInt8] {
|
||||
let u = UInt16(truncatingIfNeeded: v)
|
||||
return [UInt8(u >> 8), UInt8(u & 0xFF)]
|
||||
func createSupFrame(_ frame: DVDSup) -> [UInt8] {
|
||||
let evenRows = stride(from: 0, to: frame.height, by: 2).map { $0 }
|
||||
let oddRows = stride(from: 1, to: frame.height, by: 2).map { $0 }
|
||||
let evenFieldRLE = encodeField(frame, rows: evenRows)
|
||||
let oddFieldRLE = encodeField(frame, rows: oddRows)
|
||||
|
||||
func bigEndian16(_ value: Int) -> [UInt8] {
|
||||
let bits = UInt16(truncatingIfNeeded: value)
|
||||
return [UInt8(bits >> 8), UInt8(bits & 0xFF)]
|
||||
}
|
||||
|
||||
let off1 = 4 // even-field RLE start
|
||||
let off2 = 4 + even.count // odd-field RLE start
|
||||
let cmdPos = 4 + even.count + odd.count
|
||||
let block2 = cmdPos + 24 + (s.forced ? 1 : 0)
|
||||
let total = block2 + 6
|
||||
precondition(total <= 0xFFFF, "SPU larger than 64 KiB")
|
||||
let evenFieldOffset = 4 // even-field RLE start
|
||||
let oddFieldOffset = 4 + evenFieldRLE.count // odd-field RLE start
|
||||
let commandsOffset = 4 + evenFieldRLE.count + oddFieldRLE.count
|
||||
let endBlockOffset = commandsOffset + 24 + (frame.forced ? 1 : 0)
|
||||
let totalSize = endBlockOffset + 6
|
||||
precondition(totalSize <= 0xFFFF, "SPU larger than 64 KiB")
|
||||
|
||||
var out: [UInt8] = []
|
||||
out.append(contentsOf: be16(total))
|
||||
out.append(contentsOf: be16(cmdPos))
|
||||
out.append(contentsOf: even)
|
||||
out.append(contentsOf: odd)
|
||||
var spuBytes: [UInt8] = []
|
||||
spuBytes.append(contentsOf: bigEndian16(totalSize))
|
||||
spuBytes.append(contentsOf: bigEndian16(commandsOffset))
|
||||
spuBytes.append(contentsOf: evenFieldRLE)
|
||||
spuBytes.append(contentsOf: oddFieldRLE)
|
||||
|
||||
// command block 1: start date = 0 (relative to packet PTS) + display params
|
||||
out.append(contentsOf: be16(0))
|
||||
out.append(contentsOf: be16(block2)) // next → block 2
|
||||
out.append(0x01) // set start date
|
||||
spuBytes.append(contentsOf: bigEndian16(0))
|
||||
spuBytes.append(contentsOf: bigEndian16(endBlockOffset)) // next → block 2
|
||||
spuBytes.append(0x01) // set start date
|
||||
// colormap identity (color n → palette row n). ffmpeg's dvdsubenc emits
|
||||
// this same 32/10 layout; the jar's 03/20 is DirectVobSub's permuted
|
||||
// this same 32/10 layout; the 03/20 variant is DirectVobSub's permuted
|
||||
// dialect (its palette rows are ordered for that). Both decode to the
|
||||
// intended colors in VLC/ffmpeg, which read the 32/10 form as identity.
|
||||
out.append(0x03); out.append(0x32); out.append(0x10)
|
||||
out.append(0x04); out.append(0xFF); out.append(0xF0) // alpha: 0 clear, 1-3 opaque
|
||||
out.append(0x05) // position, 12-bit x1/x2/y1/y2
|
||||
let x2 = s.x + s.w - 1, y2 = s.y + s.h - 1
|
||||
out.append(UInt8(s.x >> 4))
|
||||
out.append(UInt8((s.x << 4) & 0xF0) | UInt8((x2 >> 8) & 0x0F))
|
||||
out.append(UInt8(x2 & 0xFF))
|
||||
out.append(UInt8(s.y >> 4))
|
||||
out.append(UInt8((s.y << 4) & 0xF0) | UInt8((y2 >> 8) & 0x0F))
|
||||
out.append(UInt8(y2 & 0xFF))
|
||||
out.append(0x06) // RLE field offsets
|
||||
out.append(contentsOf: be16(off1))
|
||||
out.append(contentsOf: be16(off2))
|
||||
if s.forced { out.append(0x00) } // menu → forced flag
|
||||
out.append(0xFF) // end of block
|
||||
spuBytes.append(0x03); spuBytes.append(0x32); spuBytes.append(0x10)
|
||||
spuBytes.append(0x04); spuBytes.append(0xFF); spuBytes.append(0xF0) // alpha: 0 clear, 1-3 opaque
|
||||
spuBytes.append(0x05) // position, 12-bit x1/x2/y1/y2
|
||||
let rightEdge = frame.left + frame.width - 1, bottomEdge = frame.top + frame.height - 1
|
||||
spuBytes.append(UInt8(frame.left >> 4))
|
||||
spuBytes.append(UInt8((frame.left << 4) & 0xF0) | UInt8((rightEdge >> 8) & 0x0F))
|
||||
spuBytes.append(UInt8(rightEdge & 0xFF))
|
||||
spuBytes.append(UInt8(frame.top >> 4))
|
||||
spuBytes.append(UInt8((frame.top << 4) & 0xF0) | UInt8((bottomEdge >> 8) & 0x0F))
|
||||
spuBytes.append(UInt8(bottomEdge & 0xFF))
|
||||
spuBytes.append(0x06) // RLE field offsets
|
||||
spuBytes.append(contentsOf: bigEndian16(evenFieldOffset))
|
||||
spuBytes.append(contentsOf: bigEndian16(oddFieldOffset))
|
||||
if frame.forced { spuBytes.append(0x00) } // menu → forced flag
|
||||
spuBytes.append(0xFF) // end of block
|
||||
|
||||
// command block 2: end date as display DURATION (relative to the packet
|
||||
// PTS, like the start word); next == own start → clean decoder exit
|
||||
out.append(contentsOf: be16(Int((s.end - s.start) >> 10)))
|
||||
out.append(contentsOf: be16(block2)) // self-reference
|
||||
out.append(0x02) // set end date
|
||||
out.append(0xFF)
|
||||
precondition(out.count == total)
|
||||
return out
|
||||
spuBytes.append(contentsOf: bigEndian16(Int((frame.endTicks - frame.startTicks) >> 10)))
|
||||
spuBytes.append(contentsOf: bigEndian16(endBlockOffset)) // self-reference
|
||||
spuBytes.append(0x02) // set end date
|
||||
spuBytes.append(0xFF)
|
||||
precondition(spuBytes.count == totalSize)
|
||||
return spuBytes
|
||||
}
|
||||
|
||||
// MARK: - MPEG program-stream wrapper (.sub file container)
|
||||
//
|
||||
// Each SPU becomes one or more 0x800-byte sectors, laid out exactly like
|
||||
// BDSup2Sub's output (which mkvmerge's VobSub demuxer accepts verbatim):
|
||||
// Each SPU becomes one or more 0x800-byte sectors
|
||||
// (which mkvmerge's VobSub demuxer accepts verbatim):
|
||||
//
|
||||
// every sector: PACK header (14 B, constant) + PES packet
|
||||
// PES, 1st chunk: '00 00 01 BD' len '81 80' hdrlen=5 + 5 B PTS (payload ≤ 2020)
|
||||
@@ -197,19 +197,19 @@ func createSupFrame(_ s: DVDSup) -> [UInt8] {
|
||||
// mkvmerge strips the leading 0x20 of every payload and concatenates the rest
|
||||
// into the MKV block, so the SPU reassembles exactly.
|
||||
|
||||
let kPackHeader: [UInt8] = [0x00, 0x00, 0x01, 0xBA, 0x44, 0x02, 0xC4, 0x82,
|
||||
0x04, 0xA9, 0x01, 0x89, 0xC3, 0xF8]
|
||||
let packHeaderBytes: [UInt8] = [0x00, 0x00, 0x01, 0xBA, 0x44, 0x02, 0xC4, 0x82,
|
||||
0x04, 0xA9, 0x01, 0x89, 0xC3, 0xF8]
|
||||
|
||||
/// Standard 40-bit MPEG-2 PTS, '0011' prefix. mkvmerge cross-checks the PES
|
||||
/// PTS against the .idx timestamp and warns on any difference, so ticks are
|
||||
/// rounded through the same milliseconds value `timeStrIdx` prints (BDSup2Sub
|
||||
/// does the same: its PTS words are always ms × 90).
|
||||
/// rounded through the same milliseconds value `idxTimestamp` prints
|
||||
/// (its PTS words are always ms × 90).
|
||||
func encodePTS(_ ticks: Int64) -> [UInt8] {
|
||||
let ms = (ticks + 45) / 90
|
||||
let pts = UInt64(ms * 90) & 0x1_FFFF_FFFF
|
||||
let wholeMs = (ticks + 45) / 90
|
||||
let pts = UInt64(wholeMs * 90) & 0x1_FFFF_FFFF
|
||||
return [
|
||||
// '0010' prefix nibble — mkvmerge's VobSub reader flags the textually
|
||||
// correct '0011' as "PTS error" but accepts BDSup2Sub's '0010' dialect.
|
||||
// correct '0011' as "PTS error" but accepts '0010' dialect.
|
||||
0x21 | UInt8((pts >> 30) & 7) << 1,
|
||||
UInt8((pts >> 22) & 0xFF),
|
||||
UInt8(((pts >> 15) & 0x7F) << 1 | 1),
|
||||
@@ -220,127 +220,127 @@ func encodePTS(_ ticks: Int64) -> [UInt8] {
|
||||
|
||||
struct VobSubFile {
|
||||
var bytes: [UInt8]
|
||||
var filepos: [Int] // byte offset of each record's first sector (→ .idx)
|
||||
var filePositions: [Int] // byte offset of each record's first sector (→ .idx filepos:)
|
||||
}
|
||||
|
||||
func wrapPS(_ subs: [DVDSup]) -> VobSubFile {
|
||||
var out: [UInt8] = []
|
||||
var positions: [Int] = []
|
||||
for s in subs {
|
||||
let spu = createSupFrame(s)
|
||||
func wrapPS(_ frames: [DVDSup]) -> VobSubFile {
|
||||
var fileBytes: [UInt8] = []
|
||||
var filePositions: [Int] = []
|
||||
for frame in frames {
|
||||
let spu = createSupFrame(frame)
|
||||
|
||||
// Slice the SPU into PES payloads: the first payload is 2020 bytes
|
||||
// (2048 − PACK 14 − PES header 14 with PTS), later ones 2025.
|
||||
var chunks: [[UInt8]] = []
|
||||
var i = 0
|
||||
var maxFrag = 2020 - 1
|
||||
while i < spu.count {
|
||||
chunks.append([0x20] + Array(spu[i..<min(i + maxFrag, spu.count)]))
|
||||
i += maxFrag
|
||||
maxFrag = 2025 - 1
|
||||
var pesPayloads: [[UInt8]] = []
|
||||
var sliceStart = 0
|
||||
var maxSliceSize = 2020 - 1
|
||||
while sliceStart < spu.count {
|
||||
pesPayloads.append([0x20] + Array(spu[sliceStart..<min(sliceStart + maxSliceSize, spu.count)]))
|
||||
sliceStart += maxSliceSize
|
||||
maxSliceSize = 2025 - 1
|
||||
}
|
||||
|
||||
positions.append(out.count)
|
||||
for (ci, payload) in chunks.enumerated() {
|
||||
var sec = [UInt8](repeating: 0xFF, count: 0x800)
|
||||
sec.replaceSubrange(0..<14, with: kPackHeader)
|
||||
let hdrlen = ci == 0 ? 5 : 0
|
||||
let pesLen = payload.count + 3 + hdrlen // counts the hdrlen byte
|
||||
sec[14] = 0x00; sec[15] = 0x00; sec[16] = 0x01; sec[17] = 0xBD
|
||||
sec[18] = UInt8(pesLen >> 8); sec[19] = UInt8(pesLen & 0xFF)
|
||||
sec[20] = 0x81
|
||||
sec[21] = ci == 0 ? 0x80 : 0x00 // PTS present only first
|
||||
sec[22] = UInt8(hdrlen)
|
||||
var o = 23
|
||||
if ci == 0 {
|
||||
sec.replaceSubrange(o..<o + 5, with: encodePTS(s.start))
|
||||
o += 5
|
||||
filePositions.append(fileBytes.count)
|
||||
for (payloadIndex, payload) in pesPayloads.enumerated() {
|
||||
var sector = [UInt8](repeating: 0xFF, count: 0x800)
|
||||
sector.replaceSubrange(0..<14, with: packHeaderBytes)
|
||||
let headerLength = payloadIndex == 0 ? 5 : 0
|
||||
let pesFieldLength = payload.count + 3 + headerLength // counts the hdrlen byte
|
||||
sector[14] = 0x00; sector[15] = 0x00; sector[16] = 0x01; sector[17] = 0xBD
|
||||
sector[18] = UInt8(pesFieldLength >> 8); sector[19] = UInt8(pesFieldLength & 0xFF)
|
||||
sector[20] = 0x81
|
||||
sector[21] = payloadIndex == 0 ? 0x80 : 0x00 // PTS present only first
|
||||
sector[22] = UInt8(headerLength)
|
||||
var cursor = 23
|
||||
if payloadIndex == 0 {
|
||||
sector.replaceSubrange(cursor..<cursor + 5, with: encodePTS(frame.startTicks))
|
||||
cursor += 5
|
||||
}
|
||||
sec.replaceSubrange(o..<o + payload.count, with: payload)
|
||||
o += payload.count
|
||||
sector.replaceSubrange(cursor..<cursor + payload.count, with: payload)
|
||||
cursor += payload.count
|
||||
|
||||
// padding PES (private_stream_2) fills the rest of the last sector;
|
||||
// payload bytes are never truncated by our sizing.
|
||||
let left = 0x800 - o
|
||||
if left >= 6 {
|
||||
let padLen = left - 6
|
||||
sec[o] = 0x00; sec[o + 1] = 0x00; sec[o + 2] = 0x01; sec[o + 3] = 0xBE
|
||||
sec[o + 4] = UInt8(padLen >> 8); sec[o + 5] = UInt8(padLen & 0xFF)
|
||||
let bytesLeft = 0x800 - cursor
|
||||
if bytesLeft >= 6 {
|
||||
let padFieldLength = bytesLeft - 6
|
||||
sector[cursor] = 0x00; sector[cursor + 1] = 0x00; sector[cursor + 2] = 0x01; sector[cursor + 3] = 0xBE
|
||||
sector[cursor + 4] = UInt8(padFieldLength >> 8); sector[cursor + 5] = UInt8(padFieldLength & 0xFF)
|
||||
}
|
||||
out.append(contentsOf: sec)
|
||||
fileBytes.append(contentsOf: sector)
|
||||
}
|
||||
}
|
||||
return VobSubFile(bytes: out, filepos: positions)
|
||||
return VobSubFile(bytes: fileBytes, filePositions: filePositions)
|
||||
}
|
||||
|
||||
// MARK: - .idx writer
|
||||
|
||||
func hex6(_ c: RGBA) -> String { String(format: "%02x%02x%02x", c.r, c.g, c.b) }
|
||||
func rgbHex(_ color: RGBA) -> String { String(format: "%02x%02x%02x", color.r, color.g, color.b) }
|
||||
|
||||
func timeStrIdx(_ ticks: Int64) -> String {
|
||||
let ms = (ticks + 45) / 90
|
||||
let h = ms / 3_600_000
|
||||
let m = (ms % 3_600_000) / 60_000
|
||||
let s = (ms % 60_000) / 1000
|
||||
let mm = ms % 1000
|
||||
return String(format: "%02d:%02d:%02d:%03d", h, m, s, mm)
|
||||
func idxTimestamp(_ ticks: Int64) -> String {
|
||||
let totalMs = (ticks + 45) / 90
|
||||
let hours = totalMs / 3_600_000
|
||||
let minutes = (totalMs % 3_600_000) / 60_000
|
||||
let seconds = (totalMs % 60_000) / 1000
|
||||
let millis = totalMs % 1000
|
||||
return String(format: "%02d:%02d:%02d:%03d", hours, minutes, seconds, millis)
|
||||
}
|
||||
|
||||
func writeIdx(resolution: DVDResolution, palette4: [RGBA], subs: [DVDSup],
|
||||
filepos: [Int], lang: String, langName: String) -> String {
|
||||
var L: [String] = []
|
||||
L.append("# VobSub index file, v7 (do not modify this line!)")
|
||||
L.append("# Created by sup2vob (BDSup2Sub-compatible output)")
|
||||
L.append("")
|
||||
L.append("# Frame size")
|
||||
L.append("size: \(resolution.width)x\(resolution.height)")
|
||||
L.append("")
|
||||
L.append("# Origin - upper-left corner")
|
||||
L.append("org: 0, 0")
|
||||
L.append("")
|
||||
L.append("# Scaling")
|
||||
L.append("scale: 100%, 100%")
|
||||
L.append("")
|
||||
L.append("# Alpha blending")
|
||||
L.append("alpha: 100%")
|
||||
L.append("")
|
||||
L.append("# Smoothing")
|
||||
L.append("smooth: OFF")
|
||||
L.append("")
|
||||
L.append("# Fade in/out in milliseconds")
|
||||
L.append("fadein/out: 0, 0")
|
||||
L.append("")
|
||||
L.append("# Force subtitle placement relative to (org.x, org.y)")
|
||||
L.append("align: OFF at LEFT TOP")
|
||||
L.append("")
|
||||
L.append("# For correcting non-progressive desync. (in millisecs or hh:mm:ss:ms)")
|
||||
L.append("time offset: 0")
|
||||
L.append("")
|
||||
L.append("# ON: displays only forced subtitles, OFF: shows everything")
|
||||
L.append("forced subs: OFF")
|
||||
L.append("")
|
||||
L.append("# The palette of the generated file")
|
||||
var pal16 = Array(repeating: RGBA.transparent, count: 16)
|
||||
for (i, c) in palette4.prefix(4).enumerated() { pal16[i] = c }
|
||||
L.append("palette: " + pal16.map { hex6($0) }.joined(separator: ", "))
|
||||
L.append("")
|
||||
L.append("# Custom colors (transp idxs and the four colors)")
|
||||
L.append("custom colors: OFF, tridx: 1000, colors: 000000, 444444, 888888, cccccc")
|
||||
L.append("")
|
||||
L.append("# Language index in use")
|
||||
L.append("langidx: 0")
|
||||
L.append("")
|
||||
L.append("# \(langName)")
|
||||
L.append("id: \(lang), index: 0")
|
||||
L.append("# Decomment next line to activate alternative name in DirectVobSub / Windows Media Player 6.x")
|
||||
L.append("# alt: \(langName)")
|
||||
L.append("# Vob/Cell ID: 1, 1 (PTS: 0)")
|
||||
for (k, s) in subs.enumerated() {
|
||||
L.append("timestamp: \(timeStrIdx(s.start)), filepos: \(String(format: "%09x", filepos[k]))")
|
||||
func writeIdx(resolution: DVDResolution, fourColorPalette: [RGBA], frames: [DVDSup],
|
||||
filePositions: [Int], lang: String, langName: String) -> String {
|
||||
var lines: [String] = []
|
||||
lines.append("# VobSub index file, v7 (do not modify this line!)")
|
||||
lines.append("# Created by sup2vob")
|
||||
lines.append("")
|
||||
lines.append("# Frame size")
|
||||
lines.append("size: \(resolution.width)x\(resolution.height)")
|
||||
lines.append("")
|
||||
lines.append("# Origin - upper-left corner")
|
||||
lines.append("org: 0, 0")
|
||||
lines.append("")
|
||||
lines.append("# Scaling")
|
||||
lines.append("scale: 100%, 100%")
|
||||
lines.append("")
|
||||
lines.append("# Alpha blending")
|
||||
lines.append("alpha: 100%")
|
||||
lines.append("")
|
||||
lines.append("# Smoothing")
|
||||
lines.append("smooth: OFF")
|
||||
lines.append("")
|
||||
lines.append("# Fade in/out in milliseconds")
|
||||
lines.append("fadein/out: 0, 0")
|
||||
lines.append("")
|
||||
lines.append("# Force subtitle placement relative to (org.x, org.y)")
|
||||
lines.append("align: OFF at LEFT TOP")
|
||||
lines.append("")
|
||||
lines.append("# For correcting non-progressive desync. (in millisecs or hh:mm:ss:ms)")
|
||||
lines.append("time offset: 0")
|
||||
lines.append("")
|
||||
lines.append("# ON: displays only forced subtitles, OFF: shows everything")
|
||||
lines.append("forced subs: OFF")
|
||||
lines.append("")
|
||||
lines.append("# The palette of the generated file")
|
||||
var palette16 = Array(repeating: RGBA.transparent, count: 16)
|
||||
for (slot, color) in fourColorPalette.prefix(4).enumerated() { palette16[slot] = color }
|
||||
lines.append("palette: " + palette16.map { rgbHex($0) }.joined(separator: ", "))
|
||||
lines.append("")
|
||||
lines.append("# Custom colors (transp idxs and the four colors)")
|
||||
lines.append("custom colors: OFF, tridx: 1000, colors: 000000, 444444, 888888, cccccc")
|
||||
lines.append("")
|
||||
lines.append("# Language index in use")
|
||||
lines.append("langidx: 0")
|
||||
lines.append("")
|
||||
lines.append("# \(langName)")
|
||||
lines.append("id: \(lang), index: 0")
|
||||
lines.append("# Decomment next line to activate alternative name in DirectVobSub / Windows Media Player 6.x")
|
||||
lines.append("# alt: \(langName)")
|
||||
lines.append("# Vob/Cell ID: 1, 1 (PTS: 0)")
|
||||
for (frameIndex, frame) in frames.enumerated() {
|
||||
lines.append("timestamp: \(idxTimestamp(frame.startTicks)), filepos: \(String(format: "%09x", filePositions[frameIndex]))")
|
||||
}
|
||||
return L.joined(separator: "\n") + "\n"
|
||||
return lines.joined(separator: "\n") + "\n"
|
||||
}
|
||||
|
||||
func writeSub(_ subs: [DVDSup]) -> VobSubFile {
|
||||
wrapPS(subs)
|
||||
func writeSub(_ frames: [DVDSup]) -> VobSubFile {
|
||||
wrapPS(frames)
|
||||
}
|
||||
Reference in new issue
Block a user