diff --git a/CHANGELOG.md b/CHANGELOG.md index 4115b522..82dffe5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,35 +9,20 @@ its series' file in [changelog/](changelog/) when a newer one ships. ## [Unreleased] -## [0.66.2] - 2026-11-10 +## [0.67.0] - 2026-11-17 -### Security +### Added -- `serve`, `PlanHLS`: a Matroska whose Segment declares more bytes than the - file holds (a forged size, a file cut short) no longer has a subtitle - request ask the kernel to read ahead over the declared size, one call per - chunk, with the rendition locked meanwhile (since 0.66.0). -- `edit-inplace`: a Tags, Chapters or Attachments element after the clusters - that declares more bytes than the file holds is voided up to the end of the - file; the edit wrote its declared size, from one allocation of that size. -- `demux`: an empty ALAC packet is refused by name (`--raw` keeps the stored - payloads); a few bytes of laced empty packets held 2 KiB of packet table - each (since 0.66.0). - -### Fixed - -- `edit-inplace`: a second edit of a file whose Info or Tracks an earlier - edit had moved to the end of the Segment replaces them. It appended a - second copy behind the first, which a full read kept returning: the edit - reported success and was not seen (since 0.66.0). A file edited twice by - 0.66.0 or 0.66.1 is repaired by `mkvgo edit` (full rewrite). -- `to-hls`, `serve` and the other HLS commands: an encrypted text subtitle - track is refused by name, or dropped with its reason under - `--skip-unsupported`, as `to-mp4` does; since 0.66.0 it was listed as a - WebVTT rendition built from its undecrypted bytes. -- `serve` on a remote source with a subtitle index: the scan asks for each - block it reads again, not for the whole minute of media ahead in one request - (since 0.66.0). -- Every command names `--strict`, `--skip-unsupported`, `--synthesize-index` - and `--no-iframes` in its refusals; all but `demux`, `to-mp4`, `from-mp4` - and `to-hls` printed the library's `Options` field names. +- `subtitle.VobSubDecoder` and `subtitle.DVBSubDecoder` decode the bitmap + subtitles of DVD rips (`S_VOBSUB`) and of broadcast captures (`S_DVBSUB`) + block by block, as `subtitle.PGSDecoder` does PGS. Both return the PGS + decoder's display sets (`subtitle.BitmapDisplaySet`, the same type): each + picture in straight-alpha NRGBA with its position on the subtitle plane, an + empty set where the screen clears. +- `subtitle.ParseVobSubIndex` with `VobSubIndex.ForEachUnit`, and + `subtitle.ForEachSUPDisplaySet`, read the bitmap subtitles that sit beside a + video: a VobSub `.idx`/`.sub` pair and a PGS `.sup`, one unit or display set + at a time, each with its time, in the form the decoders take. +- `subtitle.PGSDisplaySet` gains `Delay` and `Duration`, which a VobSub unit's + start and stop commands set, and `Timeout`, a DVB page's time-out; each is + zero where the codec has no such thing. diff --git a/changelog/0.60-0.69.md b/changelog/0.60-0.69.md index 72a32c26..be5a53ea 100644 --- a/changelog/0.60-0.69.md +++ b/changelog/0.60-0.69.md @@ -2,6 +2,39 @@ Archived entries of mkvgo 0.60.x to 0.69.x, as they were published; a version moves here once a newer one ships. The current versions are in [CHANGELOG.md](../CHANGELOG.md). +## [0.66.2] - 2026-11-10 + +### Security + +- `serve`, `PlanHLS`: a Matroska whose Segment declares more bytes than the + file holds (a forged size, a file cut short) no longer has a subtitle + request ask the kernel to read ahead over the declared size, one call per + chunk, with the rendition locked meanwhile (since 0.66.0). +- `edit-inplace`: a Tags, Chapters or Attachments element after the clusters + that declares more bytes than the file holds is voided up to the end of the + file; the edit wrote its declared size, from one allocation of that size. +- `demux`: an empty ALAC packet is refused by name (`--raw` keeps the stored + payloads); a few bytes of laced empty packets held 2 KiB of packet table + each (since 0.66.0). + +### Fixed + +- `edit-inplace`: a second edit of a file whose Info or Tracks an earlier + edit had moved to the end of the Segment replaces them. It appended a + second copy behind the first, which a full read kept returning: the edit + reported success and was not seen (since 0.66.0). A file edited twice by + 0.66.0 or 0.66.1 is repaired by `mkvgo edit` (full rewrite). +- `to-hls`, `serve` and the other HLS commands: an encrypted text subtitle + track is refused by name, or dropped with its reason under + `--skip-unsupported`, as `to-mp4` does; since 0.66.0 it was listed as a + WebVTT rendition built from its undecrypted bytes. +- `serve` on a remote source with a subtitle index: the scan asks for each + block it reads again, not for the whole minute of media ahead in one request + (since 0.66.0). +- Every command names `--strict`, `--skip-unsupported`, `--synthesize-index` + and `--no-iframes` in its refusals; all but `demux`, `to-mp4`, `from-mp4` + and `to-hls` printed the library's `Options` field names. + ## [0.66.1] - 2026-11-08 ### Fixed diff --git a/docs/library.md b/docs/library.md index e6d5e5ca..5050fb1e 100644 --- a/docs/library.md +++ b/docs/library.md @@ -11,7 +11,7 @@ task-first starting point (CLI and Go side by side). | `mkv/reader` | `github.com/gravity-zero/mkvgo/mkv/reader` | Parse MKV files into `Container` | | `mkv/writer` | `github.com/gravity-zero/mkvgo/mkv/writer` | Write `Container` to MKV bytes | | `mkv/ops` | `github.com/gravity-zero/mkvgo/mkv/ops` | High-level operations | -| `mkv/subtitle` | `github.com/gravity-zero/mkvgo/mkv/subtitle` | SRT/ASS parsing | +| `mkv/subtitle` | `github.com/gravity-zero/mkvgo/mkv/subtitle` | SRT/ASS parsing, PGS/VobSub/DVB bitmap decoding | | `matroska` | `github.com/gravity-zero/mkvgo/matroska` | Facade -- re-exports everything | | `ebml` | `github.com/gravity-zero/mkvgo/ebml` | Low-level EBML codec | @@ -1909,6 +1909,116 @@ consumer's decision, not an extractor's. CLI: `mkvgo extract-subtitle -format pgs -o ` writes one PNG per cue plus a `cues.json` manifest, driven by the streaming form. +### Bitmap subtitles: VobSub and DVB + +DVD rips carry `S_VOBSUB` and broadcast captures `S_DVBSUB`: pictures again, +in two more codecs. `subtitle.VobSubDecoder` and `subtitle.DVBSubDecoder` are +block-level decoders with the shape of `subtitle.PGSDecoder`: built from the +track's CodecPrivate, fed the track's blocks in file order, `Reset` when the +read skips blocks. Both return `[]subtitle.BitmapDisplaySet`, which is the +type `PGSDisplaySet` names, so one consumer serves the three codecs: pictures +in straight-alpha `*image.NRGBA`, each with its position on the subtitle +plane (`ScreenW`/`ScreenH`), and a set with no object where the screen clears. + +```go +// VobSub: a unit is one block, or several when a muxer cut it; it is dated by the block that starts it. +dec, err := subtitle.NewVobSubDecoder(track.CodecPrivate) +var unitTime int64 +for each block of the track { + data, err := track.DecodePayload(block.Data) // a track stored compressed + if !dec.Continues(data) { + unitTime = block.Timecode + } + sets, err := dec.Decode(data) + for _, ds := range sets { + start := unitTime + ds.Delay.Milliseconds() + // ds.Objects: X, Y, Forced, Image; none = the screen clears at start + } +} + +// DVB: a block is a display set, shown at the block's time. +dvb := subtitle.NewDVBSubDecoder(track.CodecPrivate) +for each block of the track { + data, err := track.DecodePayload(block.Data) + sets, err := dvb.Decode(data) + for _, ds := range sets { + // ds.Objects at block.Timecode; none = the screen clears + } +} +``` + +What differs from PGS: + +- **VobSub** reads its 16 colours and its plane size from the CodecPrivate (the + header of a `.idx` file); a track without them is refused, its pictures + having no colours. A block is one subpicture unit and gives one set, which + carries its own timing: `Delay` before the picture shows and `Duration` + until its stop command, zero when the unit has none (the block's duration or + the next block then ends it). `Forced` is the unit's forced-display command. + A unit a muxer spread over several blocks comes out with the block that + completes it, and its time is the first block's: `Continues`, asked ahead + of each `Decode`, tells whether the block is the rest of a unit or starts + one. A block that is a whole unit of its own never completes the one before, + which is dropped, and so starts one. A stop at + the date of the start gives an empty set. Colour and contrast changes + within a picture (command `0x07`) are not applied. +- **DVB** keeps regions, their pixels and the colour tables between blocks: a + display set sends only what changed, until a page composition declares a + mode change or an acquisition point. A set's objects are the regions on + screen, one picture each; a region the page places several times is read + once, its placements sharing the picture. The CodecPrivate names the + service's composition and ancillary pages, the segments of other pages being + ignored; with one too short to name them every page is decoded. 2, 4 and + 8-bit pixels, map tables, the default colour tables and the display + definition (plane size, window) are read; character objects are not drawn. + `Forced`, `Delay` and `Duration` are never set: a set ends where the next + one begins. `Timeout` is the page's time-out, whole seconds after which a + set nothing replaced leaves the screen: a bound, not a duration. +- Both cut each picture down to its visible pixels, `X`, `Y` moving with the + cut, and leave a picture with no visible pixel out of its set: a region that + was only erased reads as a clear. + +Both bound what a block can cost, as the PGS decoder does: a picture or a +region is at most 4096 pixels a side, a DVB block fills and draws at most four +such planes, an epoch's regions hold at most 16 MiB; past a bound, as on any +malformed block, `Decode` returns an error and the next block is decoded +normally. A DVB block refused part-way returns, with the error, the sets it +completed ahead of it and the one it was composing; bytes that are no segment, +or a pixel-data block of an undefined type, end what they are in without an +error. + +### Bitmap subtitles beside the video: .idx/.sub and .sup + +The same pictures also travel as files of their own: VobSub as a `.idx` and +a `.sub`, PGS as a `.sup`. Two readers turn them into what the decoders take, +one unit or display set at a time, so a whole file is never held. + +```go +ix, err := subtitle.ParseVobSubIndex(idxFile) // the .idx: header, languages, times +dec, err := subtitle.NewVobSubDecoder(ix.Header) +err = ix.ForEachUnit(subFile, 0, func(timeMs int64, unit []byte) error { + sets, err := dec.Decode(unit) // as for a Matroska block + return err +}) + +pgs := subtitle.NewPGSDecoder() +err = subtitle.ForEachSUPDisplaySet(supFile, func(timeMs int64, set []byte) error { + sets, err := pgs.Decode(set) + return err +}) +``` + +`VobSubIndex.Streams` lists the languages of the pair (`Language`, `Name`, +`Index`) and, for each, the time and the position of every subpicture, the +`delay:` and `time offset:` lines applied. `ForEachUnit` reads the `.sub` (an +MPEG-2 program stream, through an `io.ReaderAt`) and gathers each unit from +the packets of its language; an index entry that points into the unit before +it, or at the end of the `.sub`, is passed over, and a unit the `.sub` holds +only in part is an error. `ForEachSUPDisplaySet` gives each display set at the +presentation time of its composition segment; a file cut inside a segment gives +the sets ahead of it, then an error, and one that ends between two segments +gives its last set as it stands. + --- ## Editing MP4/MOV natively diff --git a/internal/testdata/dvbsub.mkv b/internal/testdata/dvbsub.mkv new file mode 100644 index 00000000..d9d2e3eb Binary files /dev/null and b/internal/testdata/dvbsub.mkv differ diff --git a/internal/testdata/dvbsub_set13.png b/internal/testdata/dvbsub_set13.png new file mode 100644 index 00000000..c741f93c Binary files /dev/null and b/internal/testdata/dvbsub_set13.png differ diff --git a/internal/testdata/pgs_sub.sup b/internal/testdata/pgs_sub.sup new file mode 100644 index 00000000..1f60bd1a Binary files /dev/null and b/internal/testdata/pgs_sub.sup differ diff --git a/internal/testdata/pgs_sub_set0.png b/internal/testdata/pgs_sub_set0.png new file mode 100644 index 00000000..77ac5e0d Binary files /dev/null and b/internal/testdata/pgs_sub_set0.png differ diff --git a/internal/testdata/vobsub.idx b/internal/testdata/vobsub.idx new file mode 100644 index 00000000..c663f290 --- /dev/null +++ b/internal/testdata/vobsub.idx @@ -0,0 +1,62 @@ +# VobSub index file, v7 (do not modify this line!) +# +# Generated by MPlayer dev-SVN-r25315 +# See for more information about MPlayer +# See for more information about Vobsub +# +size: 720x480 +palette: 0d00ee, ee450d, 101010, ebebeb, 0ce60b, ec14ed, ebff0b, 0d637e, a1a1a1, c5c5c5, 0e640c, 89db89, 0e0089, a2bdd4, ebcf0b, 7e127e +# ON: displays only forced subtitles, OFF: shows everything +forced subs: OFF + +# Language index in use +langidx: 0 + +id: en, index: 0 +timestamp: 00:02:12:499, filepos: 000000000 +timestamp: 00:02:27:355, filepos: 000001000 +timestamp: 00:03:00:797, filepos: 000002000 +timestamp: 00:03:03:433, filepos: 000003000 +timestamp: 00:03:05:919, filepos: 000004800 +timestamp: 00:03:05:910, filepos: 000005000 +timestamp: 00:03:08:663, filepos: 000005800 +timestamp: 00:03:10:014, filepos: 000006800 +timestamp: 00:03:19:724, filepos: 000007800 +timestamp: 00:03:21:175, filepos: 000008800 +timestamp: 00:03:22:819, filepos: 000009000 +timestamp: 00:03:24:762, filepos: 00000a000 +timestamp: 00:03:26:806, filepos: 00000b000 +timestamp: 00:03:28:716, filepos: 00000c000 +timestamp: 00:03:30:051, filepos: 00000d000 +timestamp: 00:03:31:644, filepos: 00000e000 +timestamp: 00:03:34:380, filepos: 00000f800 +timestamp: 00:03:37:225, filepos: 000011000 +timestamp: 00:03:39:652, filepos: 000012000 +timestamp: 00:03:43:531, filepos: 000012800 +timestamp: 00:03:47:510, filepos: 000013800 +timestamp: 00:03:50:872, filepos: 000015000 +timestamp: 00:03:53:124, filepos: 000016000 +timestamp: 00:03:57:303, filepos: 000017800 +timestamp: 00:04:00:106, filepos: 000019000 +timestamp: 00:04:33:556, filepos: 00001a000 +timestamp: 00:04:55:445, filepos: 00001b000 +timestamp: 00:05:00:049, filepos: 00001b800 +timestamp: 00:05:02:018, filepos: 00001c800 +timestamp: 00:05:02:035, filepos: 00001d000 +timestamp: 00:05:04:203, filepos: 00001d800 +timestamp: 00:05:05:947, filepos: 00001e800 +timestamp: 00:05:07:957, filepos: 00001f800 +timestamp: 00:05:21:295, filepos: 000020800 +timestamp: 00:05:23:356, filepos: 000021800 +timestamp: 00:05:24:640, filepos: 000022800 +timestamp: 00:05:27:193, filepos: 000023800 +timestamp: 00:05:28:369, filepos: 000024800 +timestamp: 00:05:28:361, filepos: 000025000 +timestamp: 00:05:29:946, filepos: 000025800 +timestamp: 00:05:31:230, filepos: 000026000 +timestamp: 00:05:32:924, filepos: 000027000 +timestamp: 00:05:42:600, filepos: 000028000 +timestamp: 00:05:46:771, filepos: 000028800 +timestamp: 00:05:57:640, filepos: 000029800 +timestamp: 00:05:59:834, filepos: 00002a800 +timestamp: 00:06:05:248, filepos: 00002b800 diff --git a/internal/testdata/vobsub.mkv b/internal/testdata/vobsub.mkv new file mode 100644 index 00000000..90f5c0ea Binary files /dev/null and b/internal/testdata/vobsub.mkv differ diff --git a/internal/testdata/vobsub.sub b/internal/testdata/vobsub.sub new file mode 100644 index 00000000..56b38234 Binary files /dev/null and b/internal/testdata/vobsub.sub differ diff --git a/internal/testdata/vobsub_cue0.png b/internal/testdata/vobsub_cue0.png new file mode 100644 index 00000000..51c9c67e Binary files /dev/null and b/internal/testdata/vobsub_cue0.png differ diff --git a/mkv/subtitle/bitmap.go b/mkv/subtitle/bitmap.go new file mode 100644 index 00000000..0bfba992 --- /dev/null +++ b/mkv/subtitle/bitmap.go @@ -0,0 +1,60 @@ +package subtitle + +// bitmap.go - what the bitmap subtitle decoders (PGS, VobSub, DVB) hand back: pictures and where they sit on the +// subtitle plane, in one shape whatever the codec. + +import "image" + +// BitmapDisplaySet is one decoded display set of any bitmap subtitle codec: the type PGSDisplaySet names, which the +// VobSub and DVB decoders return as well. +type BitmapDisplaySet = PGSDisplaySet + +// BitmapObject is one picture of a BitmapDisplaySet: the type PGSObject names. +type BitmapObject = PGSObject + +// trimTransparent cuts a picture down to the rectangle its visible pixels cover, and says where that rectangle +// starts in the original; nil when no pixel is visible. +func trimTransparent(img *image.NRGBA) (*image.NRGBA, image.Point) { + w, h := img.Rect.Dx(), img.Rect.Dy() + rowVisible := func(y int) bool { + row := img.Pix[y*img.Stride : y*img.Stride+4*w] + for i := 3; i < len(row); i += 4 { + if row[i] != 0 { + return true + } + } + return false + } + top, bottom := 0, h + for top < h && !rowVisible(top) { + top++ + } + if top == h { + return nil, image.Point{} + } + for !rowVisible(bottom - 1) { + bottom-- + } + left, right := w, 0 + for y := top; y < bottom; y++ { + row := img.Pix[y*img.Stride : y*img.Stride+4*w] + for x := 0; x < left; x++ { + if row[4*x+3] != 0 { + left = x + } + } + for x := w - 1; x >= right; x-- { + if row[4*x+3] != 0 { + right = x + 1 + } + } + } + if top == 0 && left == 0 && bottom == h && right == w { + return img, image.Point{} + } + out := image.NewNRGBA(image.Rect(0, 0, right-left, bottom-top)) + for y := top; y < bottom; y++ { + copy(out.Pix[(y-top)*out.Stride:(y-top+1)*out.Stride], img.Pix[y*img.Stride+4*left:]) + } + return out, image.Pt(left, top) +} diff --git a/mkv/subtitle/bitmap_samples_test.go b/mkv/subtitle/bitmap_samples_test.go new file mode 100644 index 00000000..2be54758 --- /dev/null +++ b/mkv/subtitle/bitmap_samples_test.go @@ -0,0 +1,350 @@ +package subtitle_test + +// Real tracks: a DVD's VobSub and a broadcast's DVB subtitles, each remuxed to Matroska with its packets untouched. +// The golden pictures are the mainstream decoder's rendering of one display set on the whole subtitle plane. + +import ( + "bytes" + "context" + "image" + "image/png" + "io" + "os" + "testing" + "time" + + "github.com/gravity-zero/mkvgo/mkv" + "github.com/gravity-zero/mkvgo/mkv/reader" + "github.com/gravity-zero/mkvgo/mkv/subtitle" +) + +const ( + vobsubSample = "../../internal/testdata/vobsub.mkv" + dvbsubSample = "../../internal/testdata/dvbsub.mkv" +) + +// sampleTrack reads the one track of a sample and all its blocks. +func sampleTrack(tb testing.TB, path string) (*mkv.Track, []mkv.Block) { + tb.Helper() + c, err := reader.Open(context.Background(), path) + if err != nil { + tb.Fatal(err) + } + f, err := os.Open(path) + if err != nil { + tb.Fatal(err) + } + defer f.Close() + br, err := reader.NewBlockReader(f, c.Info.TimecodeScale) + if err != nil { + tb.Fatal(err) + } + var blocks []mkv.Block + for { + b, err := br.Next() + if err == io.EOF { + return &c.Tracks[0], blocks + } + if err != nil { + tb.Fatal(err) + } + b.Data = append([]byte(nil), b.Data...) + blocks = append(blocks, b) + } +} + +// planeDiff counts the pixels of the subtitle plane where a display set and the golden rendering differ; two fully +// transparent pixels are the same whatever colour they carry. +func planeDiff(tb testing.TB, ds subtitle.BitmapDisplaySet, golden string) int { + tb.Helper() + f, err := os.Open(golden) + if err != nil { + tb.Fatal(err) + } + defer f.Close() + decoded, err := png.Decode(f) + if err != nil { + tb.Fatal(err) + } + ref, ok := decoded.(*image.NRGBA) + if !ok || ref.Bounds().Dx() != ds.ScreenW || ref.Bounds().Dy() != ds.ScreenH { + tb.Fatalf("%s is a %T of %v, want an NRGBA of the %dx%d plane", golden, decoded, decoded.Bounds().Size(), ds.ScreenW, ds.ScreenH) + } + plane := image.NewNRGBA(ref.Bounds()) + for _, o := range ds.Objects { + b := o.Image.Bounds() + for y := 0; y < b.Dy(); y++ { + src := o.Image.Pix[y*o.Image.Stride : y*o.Image.Stride+4*b.Dx()] + copy(plane.Pix[plane.PixOffset(o.X, o.Y+y):], src) + } + } + diff := 0 + for i := 0; i < len(ref.Pix); i += 4 { + if ref.Pix[i+3] == 0 && plane.Pix[i+3] == 0 { + continue + } + if [4]byte(ref.Pix[i:i+4]) != [4]byte(plane.Pix[i:i+4]) { + diff++ + } + } + return diff +} + +type sampleCue struct { + timeMs, durMs int64 + x, y, w, h int +} + +func TestVobSubDecoder_RealTrack(t *testing.T) { + track, blocks := sampleTrack(t, vobsubSample) + if track.Codec != "vobsub" || len(blocks) != 46 { + t.Fatalf("sample holds a %q track of %d blocks, want vobsub and 46", track.Codec, len(blocks)) + } + d, err := subtitle.NewVobSubDecoder(track.CodecPrivate) + if err != nil { + t.Fatal(err) + } + var cues []sampleCue + var first subtitle.BitmapDisplaySet + var unitTime int64 + for i, b := range blocks { + if !d.Continues(b.Data) { + unitTime = b.Timecode + } + sets, err := d.Decode(b.Data) + if err != nil { + t.Fatalf("block %d at %d ms: %v", i, b.Timecode, err) + } + for _, ds := range sets { + if ds.ScreenW != 720 || ds.ScreenH != 480 || len(ds.Objects) != 1 || ds.Objects[0].Forced || ds.Delay != 0 { + t.Fatalf("block %d: plane %dx%d, %d objects, delay %v; want one unforced picture on 720x480, undelayed", i, ds.ScreenW, ds.ScreenH, len(ds.Objects), ds.Delay) + } + if len(cues) == 0 { + first = ds + } + o := ds.Objects[0] + cues = append(cues, sampleCue{unitTime, ds.Duration.Milliseconds(), o.X, o.Y, o.Image.Bounds().Dx(), o.Image.Bounds().Dy()}) + } + } + // Three of the 46 blocks hold the first part of a unit the muxer cut in two. + if len(cues) != 43 { + t.Fatalf("got %d cues, want 43", len(cues)) + } + for i, want := range map[int]sampleCue{ + 0: {132499, 4960, 259, 379, 200, 24}, + 1: {147355, 4733, 259, 76, 203, 63}, + 4: {185919, 2696, 234, 340, 250, 63}, // a unit over two blocks of one time + 27: {302018, 1524, 236, 379, 247, 30}, // a unit over two blocks, the second at 302035 ms + 42: {359834, 1262, 232, 379, 254, 24}, + } { + if cues[i] != want { + t.Errorf("cue %d = %+v, want %+v", i, cues[i], want) + } + } + if n := planeDiff(t, first, "../../internal/testdata/vobsub_cue0.png"); n != 0 { + t.Errorf("the first cue differs from its golden rendering on %d pixels", n) + } +} + +func TestDVBSubDecoder_RealTrack(t *testing.T) { + track, blocks := sampleTrack(t, dvbsubSample) + if track.Codec != "dvbsub" || len(blocks) != 46 { + t.Fatalf("sample holds a %q track of %d blocks, want dvbsub and 46", track.Codec, len(blocks)) + } + d := subtitle.NewDVBSubDecoder(track.CodecPrivate) + type shown struct { + timeMs int64 + objects []sampleCue + } + var sets []shown + var golden subtitle.BitmapDisplaySet + for i, b := range blocks { + got, err := d.Decode(b.Data) + if err != nil { + t.Fatalf("block %d at %d ms: %v", i, b.Timecode, err) + } + if len(got) != 1 || got[0].ScreenW != 720 || got[0].ScreenH != 576 || got[0].Delay != 0 || got[0].Duration != 0 || got[0].Timeout != 15*time.Second { + t.Fatalf("block %d: %d sets; want one on 720x576, with no time of its own but the page's 15 s time-out", i, len(got)) + } + s := shown{timeMs: b.Timecode} + for _, o := range got[0].Objects { + s.objects = append(s.objects, sampleCue{x: o.X, y: o.Y, w: o.Image.Bounds().Dx(), h: o.Image.Bounds().Dy()}) + } + sets = append(sets, s) + if i == 13 { + golden = got[0] + } + } + // The screen is empty for the first seven display sets and the last three; the 36 between each show a picture. + for i, s := range sets { + if clear := i < 7 || i > 42; clear != (len(s.objects) == 0) { + t.Errorf("set %d at %d ms shows %d objects", i, s.timeMs, len(s.objects)) + } + } + for i, want := range map[int]shown{ + 7: {15600, []sampleCue{{x: 78, y: 416, w: 88, h: 36}}}, + 12: {17400, []sampleCue{{x: 78, y: 416, w: 406, h: 36}}}, // the line, word after word + 13: {17760, []sampleCue{{x: 78, y: 380, w: 406, h: 36}, {x: 78, y: 416, w: 148, h: 36}}}, + 42: {29360, []sampleCue{{x: 78, y: 380, w: 460, h: 36}, {x: 78, y: 416, w: 178, h: 36}}}, + 43: {31040, nil}, + } { + got := sets[i] + if got.timeMs != want.timeMs || len(got.objects) != len(want.objects) { + t.Errorf("set %d = %+v, want %+v", i, got, want) + continue + } + for k := range want.objects { + if got.objects[k] != want.objects[k] { + t.Errorf("set %d object %d = %+v, want %+v", i, k, got.objects[k], want.objects[k]) + } + } + } + if n := planeDiff(t, golden, "../../internal/testdata/dvbsub_set13.png"); n != 0 { + t.Errorf("display set 13 differs from its golden rendering on %d pixels", n) + } +} + +// The .idx/.sub pair the VobSub sample was muxed from gives the same units at the same times as the Matroska. +func TestVobSubIndex_RealPair(t *testing.T) { + _, blocks := sampleTrack(t, vobsubSample) + type unit struct { + timeMs int64 + data []byte + } + var want []unit // a unit the muxer cut in two is the two blocks, at the time of the first + for _, b := range blocks { + if n := len(want); n > 0 && len(want[n-1].data) < int(want[n-1].data[0])<<8|int(want[n-1].data[1]) { + want[n-1].data = append(want[n-1].data, b.Data...) + continue + } + want = append(want, unit{b.Timecode, b.Data}) + } + + idx, err := os.Open("../../internal/testdata/vobsub.idx") + if err != nil { + t.Fatal(err) + } + defer idx.Close() + ix, err := subtitle.ParseVobSubIndex(idx) + if err != nil { + t.Fatal(err) + } + if len(ix.Streams) != 1 || ix.Streams[0].Language != "en" || ix.Streams[0].Index != 0 || len(ix.Streams[0].Entries) != 47 { + t.Fatalf("index = %d streams, the first %q with %d entries; want one, en, 47", len(ix.Streams), ix.Streams[0].Language, len(ix.Streams[0].Entries)) + } + sub, err := os.Open("../../internal/testdata/vobsub.sub") + if err != nil { + t.Fatal(err) + } + defer sub.Close() + d, err := subtitle.NewVobSubDecoder(ix.Header) + if err != nil { + t.Fatal(err) + } + var got []unit + var first subtitle.BitmapDisplaySet + err = ix.ForEachUnit(sub, 0, func(timeMs int64, data []byte) error { + sets, err := d.Decode(data) + if err != nil || len(sets) != 1 { + t.Errorf("unit at %d ms: %d sets, err %v", timeMs, len(sets), err) + } else if len(got) == 0 { + first = sets[0] + } + got = append(got, unit{timeMs, append([]byte(nil), data...)}) + return nil + }) + if err != nil { + t.Fatal(err) + } + // 47 entries: three are the second packet of a unit, the last sits at the end of the .sub. + if len(got) != 43 || len(want) != 43 { + t.Fatalf("the pair gives %d units, the Matroska %d; want 43 each", len(got), len(want)) + } + for i := range want { + if got[i].timeMs != want[i].timeMs || !bytes.Equal(got[i].data, want[i].data) { + t.Errorf("unit %d: %d ms and %d bytes from the pair, %d ms and %d bytes from the Matroska", i, got[i].timeMs, len(got[i].data), want[i].timeMs, len(want[i].data)) + } + } + if n := planeDiff(t, first, "../../internal/testdata/vobsub_cue0.png"); n != 0 { + t.Errorf("the first unit differs from its golden rendering on %d pixels", n) + } +} + +// A real .sup: one whole display set, then one the file stops in the middle of. +func TestForEachSUPDisplaySet_RealFile(t *testing.T) { + f, err := os.Open("../../internal/testdata/pgs_sub.sup") + if err != nil { + t.Fatal(err) + } + defer f.Close() + d := subtitle.NewPGSDecoder() + var times []int64 + var first subtitle.BitmapDisplaySet + err = subtitle.ForEachSUPDisplaySet(f, func(timeMs int64, set []byte) error { + sets, err := d.Decode(set) + if err != nil || len(sets) != 1 { + t.Errorf("display set at %d ms: %d sets, err %v", timeMs, len(sets), err) + } else if len(times) == 0 { + first = sets[0] + } + times = append(times, timeMs) + return nil + }) + if err != nil { + t.Fatal(err) + } + if len(times) != 2 || times[0] != 67 || times[1] != 401 { + t.Fatalf("display sets at %v ms, want 67 and 401", times) + } + if first.ScreenW != 1920 || first.ScreenH != 1080 || len(first.Objects) != 2 { + t.Fatalf("first set: plane %dx%d, %d objects; want 1920x1080 and 2", first.ScreenW, first.ScreenH, len(first.Objects)) + } + for i, want := range []sampleCue{{x: 383, y: 956, w: 1156, h: 67}, {x: 100, y: 958, w: 123, h: 63}} { + o := first.Objects[i] + if got := (sampleCue{x: o.X, y: o.Y, w: o.Image.Bounds().Dx(), h: o.Image.Bounds().Dy()}); got != want { + t.Errorf("object %d = %+v, want %+v", i, got, want) + } + } + if n := planeDiff(t, first, "../../internal/testdata/pgs_sub_set0.png"); n != 0 { + t.Errorf("the first display set differs from its golden rendering on %d pixels", n) + } +} + +// decodeTrack is what a benchmark iteration does: a fresh decoder over every block of the track. +func decodeTrack(b *testing.B, blocks []mkv.Block, decode func([]byte) ([]subtitle.BitmapDisplaySet, error)) { + for _, blk := range blocks { + if _, err := decode(blk.Data); err != nil { + b.Fatal(err) + } + } +} + +func trackBytes(blocks []mkv.Block) (n int64) { + for _, b := range blocks { + n += int64(len(b.Data)) + } + return n +} + +func BenchmarkVobSubTrack(b *testing.B) { + track, blocks := sampleTrack(b, vobsubSample) + b.SetBytes(trackBytes(blocks)) + b.ReportAllocs() + for b.Loop() { + d, err := subtitle.NewVobSubDecoder(track.CodecPrivate) + if err != nil { + b.Fatal(err) + } + decodeTrack(b, blocks, d.Decode) + } +} + +func BenchmarkDVBSubTrack(b *testing.B) { + track, blocks := sampleTrack(b, dvbsubSample) + b.SetBytes(trackBytes(blocks)) + b.ReportAllocs() + for b.Loop() { + d := subtitle.NewDVBSubDecoder(track.CodecPrivate) + decodeTrack(b, blocks, d.Decode) + } +} diff --git a/mkv/subtitle/dvbsub.go b/mkv/subtitle/dvbsub.go new file mode 100644 index 00000000..1755e220 --- /dev/null +++ b/mkv/subtitle/dvbsub.go @@ -0,0 +1,715 @@ +package subtitle + +// dvbsub.go - a decoder for DVB subtitles (S_DVBSUB, ETSI EN 300 743), the bitmap subtitles of broadcast captures. +// A Matroska block holds the segments of one display set: a page composition listing the regions on screen, and the +// region compositions, colour tables (CLUT) and objects that changed. An object is a run-length picture drawn into +// every region that lists it; a region is a rectangle of 2, 4 or 8-bit pixels read through its CLUT. +// +// State spans blocks: within an epoch a display set carries only what changed, so regions, their pixels and the +// CLUTs are kept until a page composition declares a mode change or an acquisition point. Blocks are fed in file +// order; a region the read never saw defined is left out, not invented. +// +// Written from the specification, not ported: mkvgo is MIT and links no third-party subtitle decoder. + +import ( + "encoding/binary" + "fmt" + "image" + "time" +) + +const dvbSync = 0x0f // the first byte of every segment + +// Segment types. +const ( + dvbSegPage = 0x10 + dvbSegRegion = 0x11 + dvbSegCLUT = 0x12 + dvbSegObject = 0x13 + dvbSegDisplay = 0x14 + dvbSegEnd = 0x80 +) + +// Pixel-data sub-block types of an object. +const ( + dvbData2Bit = 0x10 + dvbData4Bit = 0x11 + dvbData8Bit = 0x12 + dvbMap2To4 = 0x20 + dvbMap2To8 = 0x21 + dvbMap4To8 = 0x22 + dvbDataEndRow = 0xf0 +) + +const ( + // dvbMaxDim is the widest and tallest display the format defines, and so the bound on a region. + dvbMaxDim = 4096 + // dvbMaxEpochBytes bounds what an epoch's regions hold together: they live until the next mode change. + dvbMaxEpochBytes = 16 << 20 + // dvbMaxBlockPixels bounds the work of one block, counted in pixels filled, drawn and handed back. + dvbMaxBlockPixels = 4 * dvbMaxDim * dvbMaxDim + // dvbObjectRefBytes is what one object reference of a region counts for in dvbMaxEpochBytes. + dvbObjectRefBytes = 24 +) + +// dvbCLUT is one CLUT family: the colours of 2, 4 and 8-bit pixels, straight RGBA. +type dvbCLUT struct { + c4 [4][4]uint8 + c16 [16][4]uint8 + c256 [256][4]uint8 +} + +var dvbDefaultCLUT = newDVBDefaultCLUT() + +// newDVBDefaultCLUT builds the contents every CLUT has before a definition changes them (clause 10). +func newDVBDefaultCLUT() *dvbCLUT { + bit := func(i, mask, v int) uint8 { + if i&mask != 0 { + return uint8(v) + } + return 0 + } + c := &dvbCLUT{} + c.c4 = [4][4]uint8{{}, {255, 255, 255, 255}, {0, 0, 0, 255}, {127, 127, 127, 255}} + for i := 1; i < 16; i++ { + v := 255 + if i&8 != 0 { + v = 127 + } + c.c16[i] = [4]uint8{bit(i, 1, v), bit(i, 2, v), bit(i, 4, v), 255} + } + for i := 1; i < 256; i++ { + rgb := func(low, high, base int) [3]uint8 { + return [3]uint8{ + uint8(base) + bit(i, 0x01, low) + bit(i, 0x10, high), + uint8(base) + bit(i, 0x02, low) + bit(i, 0x20, high), + uint8(base) + bit(i, 0x04, low) + bit(i, 0x40, high), + } + } + var p [3]uint8 + a := uint8(255) + switch { + case i&0x88 == 0 && i&0x70 == 0: + p, a = rgb(255, 0, 0), 63 + case i&0x88 == 0: + p = rgb(85, 170, 0) + case i&0x88 == 0x08: + p, a = rgb(85, 170, 0), 127 + case i&0x88 == 0x80: + p = rgb(43, 85, 127) + default: + p = rgb(43, 85, 0) + } + c.c256[i] = [4]uint8{p[0], p[1], p[2], a} + } + return c +} + +type dvbObjectRef struct { + id uint16 + x, y int +} + +// dvbRegion is a region's pixels, as CLUT entries, and the objects that draw into it. +type dvbRegion struct { + w, h, depth int + clut uint8 + pix []byte + objects []dvbObjectRef +} + +type dvbPlacement struct { + region uint8 + x, y int +} + +// DVBSubDecoder decodes a DVB subtitle track block by block, carrying epoch state forward. +type DVBSubDecoder struct { + pages [2]uint16 // composition and ancillary page + anyPage bool + screenW, screenH int // 0 until a display definition says otherwise + winX, winY int + regions map[uint8]*dvbRegion + cluts map[uint8]*dvbCLUT + page []dvbPlacement + timeout time.Duration // the page's time-out + held int // what the epoch's regions hold (dvbMaxEpochBytes) + budget int // what the block being decoded may still fill and draw (dvbMaxBlockPixels) +} + +// NewDVBSubDecoder reads the composition and ancillary page IDs from the track's CodecPrivate, the segments of +// other pages being another service's; with a CodecPrivate too short to hold them every page is decoded. +func NewDVBSubDecoder(codecPrivate []byte) *DVBSubDecoder { + if len(codecPrivate) < 4 { + return &DVBSubDecoder{anyPage: true} + } + return &DVBSubDecoder{pages: [2]uint16{binary.BigEndian.Uint16(codecPrivate), binary.BigEndian.Uint16(codecPrivate[2:])}} +} + +// Reset drops the epoch: call it when the read skips blocks, so a display set cannot draw on another part's regions. +func (d *DVBSubDecoder) Reset() { + d.regions, d.cluts, d.page, d.held, d.timeout = nil, nil, nil, 0, 0 + d.screenW, d.screenH, d.winX, d.winY = 0, 0, 0, 0 +} + +// Decode decodes one Matroska block into the display sets it completes, almost always one: a set's objects are the +// regions on screen, each cut down to its visible pixels, and a set with none clears the screen; its Timeout is the +// page's, the end of a set being the next one. The images are freshly allocated and owned by the caller; a region +// a page places more than once shares one image between its placements. Bytes that are no +// segment end the block. A segment that cannot be read is an error, returned with the sets completed ahead of it +// and the one being composed, which show the state the decoder is left in. +// +// A block of a track stored compressed (mkv.Track.Compression) is decoded first: mkv.Track.DecodePayload. +func (d *DVBSubDecoder) Decode(block []byte) ([]BitmapDisplaySet, error) { + var out []BitmapDisplaySet + d.budget = dvbMaxBlockPixels + composed := false // a page composition is waiting for the end of its display set + emit := func() error { + composed = false + ds, err := d.compose() + if err == nil { + out = append(out, ds) + } + return err + } + fail := func(err error) ([]BitmapDisplaySet, error) { + if composed { + _ = emit() + } + return out, err + } + + b := block + if len(b) >= 2 && b[0] == 0x20 && b[1] == 0x00 { // the PES data field's own header, when a muxer kept it + b = b[2:] + } + for len(b) > 0 && b[0] == dvbSync { + if len(b) < 6 { + return fail(fmt.Errorf("DVB subtitle: %d trailing byte(s) where a segment header needs 6", len(b))) + } + typ, page, size := b[1], binary.BigEndian.Uint16(b[2:]), int(binary.BigEndian.Uint16(b[4:])) + if size > len(b)-6 { + return fail(fmt.Errorf("DVB subtitle: a %#02x segment declares %d bytes, %d remain in the block", typ, size, len(b)-6)) + } + p := b[6 : 6+size] + b = b[6+size:] + if !d.anyPage && page != d.pages[0] && page != d.pages[1] { + continue + } + var err error + switch typ { + case dvbSegPage: + if composed { // the display set before this one left out its end segment + err = emit() + } + if err == nil { + err = d.parsePage(p) + composed = err == nil + } + case dvbSegRegion: + err = d.parseRegion(p) + case dvbSegCLUT: + err = d.parseCLUT(p) + case dvbSegObject: + err = d.parseObject(p) + case dvbSegDisplay: + err = d.parseDisplay(p) + case dvbSegEnd: + if composed { + err = emit() + } + } + if err != nil { + return fail(err) + } + } + if composed { + if err := emit(); err != nil { + return out, err + } + } + return out, nil +} + +// spend takes n pixels of work from the block's budget. +func (d *DVBSubDecoder) spend(n int) error { + if d.budget -= n; d.budget < 0 { + return fmt.Errorf("DVB subtitle: the block fills and draws more than %d pixels", dvbMaxBlockPixels) + } + return nil +} + +func (d *DVBSubDecoder) parseDisplay(p []byte) error { + if len(p) < 5 { + return fmt.Errorf("DVB subtitle: a display definition segment needs 5 bytes, got %d", len(p)) + } + d.screenW = int(binary.BigEndian.Uint16(p[1:])) + 1 + d.screenH = int(binary.BigEndian.Uint16(p[3:])) + 1 + d.winX, d.winY = 0, 0 + if p[0]&0x08 != 0 { + if len(p) < 13 { + return fmt.Errorf("DVB subtitle: a display definition segment declares a window and holds %d of its 13 bytes", len(p)) + } + d.winX, d.winY = int(binary.BigEndian.Uint16(p[5:])), int(binary.BigEndian.Uint16(p[9:])) + } + return nil +} + +// parsePage replaces the list of regions on screen; a mode change or an acquisition point starts a new epoch. +func (d *DVBSubDecoder) parsePage(p []byte) error { + const head, entry = 2, 6 + if len(p) < head || (len(p)-head)%entry != 0 { + return fmt.Errorf("DVB subtitle: a page composition segment holds %d bytes, not %d plus %d a region", len(p), head, entry) + } + if state := p[1] >> 2 & 3; state == 1 || state == 2 { + d.regions, d.cluts, d.held = nil, nil, 0 + } + d.page, d.timeout = d.page[:0], time.Duration(p[0])*time.Second + for e := p[head:]; len(e) > 0; e = e[entry:] { + d.page = append(d.page, dvbPlacement{region: e[0], x: int(binary.BigEndian.Uint16(e[2:])), y: int(binary.BigEndian.Uint16(e[4:]))}) + } + return nil +} + +// parseRegion defines or updates a region: its size and depth, its fill, and the objects that draw into it. +func (d *DVBSubDecoder) parseRegion(p []byte) error { + const head = 10 + if len(p) < head { + return fmt.Errorf("DVB subtitle: a region composition segment needs %d bytes, got %d", head, len(p)) + } + id, fill := p[0], p[1]&0x08 != 0 + w, h := int(binary.BigEndian.Uint16(p[2:])), int(binary.BigEndian.Uint16(p[4:])) + if w < 1 || h < 1 || w > dvbMaxDim || h > dvbMaxDim { + return fmt.Errorf("DVB subtitle: region %d is %dx%d, outside 1..%d", id, w, h, dvbMaxDim) + } + var depth int + var background byte + switch p[6] >> 2 & 7 { + case 1: + depth, background = 2, p[9]>>2&3 + case 2: + depth, background = 4, p[9]>>4 + case 3: + depth, background = 8, p[8] + default: + return fmt.Errorf("DVB subtitle: region %d declares pixel depth code %d, not 2, 4 or 8 bits", id, p[6]>>2&7) + } + var refs []dvbObjectRef + for e := p[head:]; len(e) > 0; { + if len(e) < 6 { + return fmt.Errorf("DVB subtitle: region %d ends on %d byte(s) of an object reference of 6", id, len(e)) + } + kind := e[2] >> 6 + ref := dvbObjectRef{id: binary.BigEndian.Uint16(e), x: int(binary.BigEndian.Uint16(e[2:]) & 0xfff), y: int(binary.BigEndian.Uint16(e[4:]) & 0xfff)} + e = e[6:] + if kind == 1 || kind == 2 { // a character object carries its two colours and no picture + if len(e) < 2 { + return fmt.Errorf("DVB subtitle: region %d ends inside a character object reference", id) + } + e = e[2:] + continue + } + refs = append(refs, ref) + } + + r := d.regions[id] + held := d.held + dvbObjectRefBytes*len(refs) + if r != nil { + held -= dvbObjectRefBytes * len(r.objects) + } + fresh := r == nil || r.w != w || r.h != h || r.depth != depth + if fresh { + held += w * h + if r != nil { + held -= len(r.pix) + } + } + if held > dvbMaxEpochBytes { + return fmt.Errorf("DVB subtitle: region %d brings the regions held since the last mode change to %d bytes (limit %d)", id, held, dvbMaxEpochBytes) + } + if fresh || fill { + if err := d.spend(w * h); err != nil { + return err + } + } + if fresh { + r = &dvbRegion{w: w, h: h, depth: depth, pix: make([]byte, w*h)} + if d.regions == nil { + d.regions = make(map[uint8]*dvbRegion, 2) + } + d.regions[id] = r + } + d.held = held + r.clut, r.objects = p[7], refs + if fill && (!fresh || background != 0) { + for i := range r.pix { + r.pix[i] = background + } + } + return nil +} + +// parseCLUT updates the entries a CLUT definition carries; the others keep their contents. +func (d *DVBSubDecoder) parseCLUT(p []byte) error { + if len(p) < 2 { + return fmt.Errorf("DVB subtitle: a CLUT definition segment needs 2 bytes, got %d", len(p)) + } + c := d.cluts[p[0]] + if c == nil { + c = new(dvbCLUT) + *c = *dvbDefaultCLUT + if d.cluts == nil { + d.cluts = make(map[uint8]*dvbCLUT, 1) + } + d.cluts[p[0]] = c + } + for e := p[2:]; len(e) > 0; { + n := 4 + if len(e) >= 2 && e[1]&1 != 0 { + n = 6 + } + if len(e) < n { + return fmt.Errorf("DVB subtitle: CLUT %d ends on %d byte(s) of an entry of %d", p[0], len(e), n) + } + entry, flags := int(e[0]), e[1] + v := binary.BigEndian.Uint16(e[2:]) // 6 bits of Y, 4 of Cr, 4 of Cb, 2 of T: the most significant ones + y, cr, cb, t := uint8(v>>10)<<2, uint8(v>>6&0xf)<<4, uint8(v>>2&0xf)<<4, uint8(v&3)<<6 + if n == 6 { + y, cr, cb, t = e[2], e[3], e[4], e[5] + } + e = e[n:] + var col [4]uint8 // Y 0 is the format's fully transparent colour + if y != 0 { + r, g, b := ycrcbToRGB(y, cr, cb) + col = [4]uint8{r, g, b, 255 - t} + } + if flags&0x80 != 0 && entry < len(c.c4) { + c.c4[entry] = col + } + if flags&0x40 != 0 && entry < len(c.c16) { + c.c16[entry] = col + } + if flags&0x20 != 0 { + c.c256[entry] = col + } + } + return nil +} + +// parseObject draws an object's two fields into every region that lists it. Only pixel objects are drawn. +func (d *DVBSubDecoder) parseObject(p []byte) error { + if len(p) < 3 { + return fmt.Errorf("DVB subtitle: an object data segment needs 3 bytes, got %d", len(p)) + } + id := binary.BigEndian.Uint16(p) + if p[2]>>2&3 != 0 { + return nil + } + if len(p) < 7 { + return fmt.Errorf("DVB subtitle: object %d declares no field lengths", id) + } + topLen, bottomLen := int(binary.BigEndian.Uint16(p[3:])), int(binary.BigEndian.Uint16(p[5:])) + if topLen+bottomLen > len(p)-7 { + return fmt.Errorf("DVB subtitle: object %d declares %d bytes of fields, its segment holds %d", id, topLen+bottomLen, len(p)-7) + } + top, bottom := p[7:7+topLen], p[7+topLen:7+topLen+bottomLen] + if bottomLen == 0 { + bottom = top + } + for _, r := range d.regions { + if err := d.spend(len(r.objects)); err != nil { + return err + } + for _, ref := range r.objects { + if ref.id != id { + continue + } + pen := dvbPen{d: d, r: r, skipOne: p[2]&2 != 0} + if err := pen.field(top, ref.x, ref.y); err != nil { + return fmt.Errorf("DVB subtitle: object %d: %w", id, err) + } + if err := pen.field(bottom, ref.x, ref.y+1); err != nil { + return fmt.Errorf("DVB subtitle: object %d: %w", id, err) + } + } + } + return nil +} + +// dvbPen draws one object into one region. +type dvbPen struct { + d *DVBSubDecoder + r *dvbRegion + skipOne bool // pixel code 1 leaves the region's pixel as it is + x, y int + table []byte // what the codes of the string being read map to, nil for the codes themselves + err error +} + +// run writes n pixels of one code at the pen, clipped to the region, and moves the pen. +func (p *dvbPen) run(code byte, n int) { + if p.err != nil { + return + } + if p.err = p.d.spend(n); p.err != nil { + return + } + x0, x1 := p.x, min(p.x+n, p.r.w) + p.x += n + if p.skipOne && code == 1 || p.y >= p.r.h || x0 >= x1 { + return + } + if p.table != nil { + code = p.table[code] + } + row := p.r.pix[p.y*p.r.w+x0 : p.y*p.r.w+x1] + for i := range row { + row[i] = code + } +} + +// field draws one field's pixel-data sub-blocks: every other line of the object, from x, y. +func (p *dvbPen) field(data []byte, x, y int) error { + map2to4 := [4]byte{0x0, 0x7, 0x8, 0xf} + map2to8 := [4]byte{0x00, 0x77, 0x88, 0xff} + var map4to8 [16]byte + for i := range map4to8 { + map4to8[i] = byte(i) * 0x11 + } + if p.err = p.d.spend(8 * len(data)); p.err != nil { // reading a bit counts as drawing a pixel + return p.err + } + p.x, p.y = x, y + depth := p.r.depth + for len(data) > 0 && p.err == nil { + typ := data[0] + data = data[1:] + tableLen := 0 + switch typ { + case dvbData2Bit: + switch depth { + case 8: + p.table = map2to8[:] + case 4: + p.table = map2to4[:] + default: + p.table = nil + } + data = data[p.string2(data):] + case dvbData4Bit: + if depth < 4 { + return fmt.Errorf("a 4-bit pixel string in a %d-bit region", depth) + } + p.table = nil + if depth == 8 { + p.table = map4to8[:] + } + data = data[p.string4(data):] + case dvbData8Bit: + if depth < 8 { + return fmt.Errorf("an 8-bit pixel string in a %d-bit region", depth) + } + p.table = nil + data = data[p.string8(data):] + case dvbMap2To4: + tableLen = 2 + case dvbMap2To8: + tableLen = 4 + case dvbMap4To8: + tableLen = 16 + case dvbDataEndRow: + p.x, p.y = x, p.y+2 + default: // a type the format does not define: its length is not known, the field ends here + return p.err + } + if tableLen == 0 { + continue + } + if len(data) < tableLen { + return fmt.Errorf("a map table of %d bytes, %d remain", tableLen, len(data)) + } + switch typ { + case dvbMap2To4: + map2to4 = [4]byte{data[0] >> 4, data[0] & 0xf, data[1] >> 4, data[1] & 0xf} + case dvbMap2To8: + copy(map2to8[:], data) + case dvbMap4To8: + copy(map4to8[:], data) + } + data = data[tableLen:] + } + return p.err +} + +// dvbBits reads a pixel code string bit by bit; past its end it reads zeros, which every string ends on. +type dvbBits struct { + data []byte + pos int +} + +func (b *dvbBits) take(n int) int { + v := 0 + for ; n > 0; n-- { + v <<= 1 + if b.pos>>3 < len(b.data) { + v |= int(b.data[b.pos>>3]>>(7-b.pos&7)) & 1 + } + b.pos++ + } + return v +} + +// used is how many whole bytes the string took, the stuffing bits that end it included. +func (b *dvbBits) used() int { return min((b.pos+7)>>3, len(b.data)) } + +// string2 draws a 2-bit/pixel code string and returns the bytes it took (clause 7.2.5.2). +func (p *dvbPen) string2(data []byte) int { + b := dvbBits{data: data} + for p.err == nil { + if v := b.take(2); v != 0 { + p.run(byte(v), 1) + continue + } + if b.take(1) == 1 { + n := b.take(3) + 3 + p.run(byte(b.take(2)), n) + continue + } + if b.take(1) == 1 { + p.run(0, 1) + continue + } + switch b.take(2) { + case 0: + return b.used() + case 1: + p.run(0, 2) + case 2: + n := b.take(4) + 12 + p.run(byte(b.take(2)), n) + case 3: + n := b.take(8) + 29 + p.run(byte(b.take(2)), n) + } + } + return b.used() +} + +// string4 draws a 4-bit/pixel code string and returns the bytes it took. +func (p *dvbPen) string4(data []byte) int { + b := dvbBits{data: data} + for p.err == nil { + if v := b.take(4); v != 0 { + p.run(byte(v), 1) + continue + } + if b.take(1) == 0 { + n := b.take(3) + if n == 0 { + return b.used() + } + p.run(0, n+2) + continue + } + if b.take(1) == 0 { + n := b.take(2) + 4 + p.run(byte(b.take(4)), n) + continue + } + switch b.take(2) { + case 0: + p.run(0, 1) + case 1: + p.run(0, 2) + case 2: + n := b.take(4) + 9 + p.run(byte(b.take(4)), n) + case 3: + n := b.take(8) + 25 + p.run(byte(b.take(4)), n) + } + } + return b.used() +} + +// string8 draws an 8-bit/pixel code string and returns the bytes it took. +func (p *dvbPen) string8(data []byte) int { + b := dvbBits{data: data} + for p.err == nil { + if v := b.take(8); v != 0 { + p.run(byte(v), 1) + continue + } + long, n := b.take(1), b.take(7) + if long == 1 { + p.run(byte(b.take(8)), n) + } else if n == 0 { + return b.used() + } else { + p.run(0, n) + } + } + return b.used() +} + +// compose turns the page into a display set: each region on screen, read through its CLUT, where the page puts it. +// A region the page places several times is read once, its placements sharing the picture. +func (d *DVBSubDecoder) compose() (BitmapDisplaySet, error) { + ds := BitmapDisplaySet{ScreenW: d.screenW, ScreenH: d.screenH, Timeout: d.timeout} + if ds.ScreenW == 0 { // no display definition: the format's standard-definition plane + ds.ScreenW, ds.ScreenH = 720, 576 + } + type picture struct { + img *image.NRGBA // nil when the region shows nothing + at image.Point + } + var seen map[uint8]picture + for _, pl := range d.page { + pic, ok := seen[pl.region] + if !ok { + r := d.regions[pl.region] + if r == nil { + if err := d.spend(1); err != nil { + return ds, err + } + continue + } + if err := d.spend(r.w * r.h); err != nil { + return ds, err + } + pic.img, pic.at = trimTransparent(d.render(r)) + if seen == nil { + seen = make(map[uint8]picture, len(d.page)) + } + seen[pl.region] = pic + } else if err := d.spend(1); err != nil { + return ds, err + } + if pic.img != nil { + ds.Objects = append(ds.Objects, BitmapObject{X: d.winX + pl.x + pic.at.X, Y: d.winY + pl.y + pic.at.Y, Image: pic.img}) + } + } + return ds, nil +} + +// render reads a region's pixels through its CLUT, or the default one when the epoch defined none of that ID. +func (d *DVBSubDecoder) render(r *dvbRegion) *image.NRGBA { + clut := d.cluts[r.clut] + if clut == nil { + clut = dvbDefaultCLUT + } + colours := clut.c256[:] + switch r.depth { + case 4: + colours = clut.c16[:] + case 2: + colours = clut.c4[:] + } + img := image.NewNRGBA(image.Rect(0, 0, r.w, r.h)) + for i, v := range r.pix { + if c := colours[int(v)&(len(colours)-1)]; c[3] != 0 { + copy(img.Pix[4*i:4*i+4], c[:]) + } + } + return img +} diff --git a/mkv/subtitle/dvbsub_test.go b/mkv/subtitle/dvbsub_test.go new file mode 100644 index 00000000..724579ea --- /dev/null +++ b/mkv/subtitle/dvbsub_test.go @@ -0,0 +1,631 @@ +package subtitle + +import ( + "bytes" + "runtime" + "strings" + "testing" + "time" +) + +// --- synthetic DVB builders: segments byte by byte, so the tests pin the wire format --- + +const dvbTestPage, dvbTestAncillary = 0x0001, 0x0152 + +var dvbTestPrivate = []byte{0x00, 0x01, 0x01, 0x52, 0x10} + +func dvbSeg(typ byte, page uint16, p []byte) []byte { + return append([]byte{dvbSync, typ, byte(page >> 8), byte(page), byte(len(p) >> 8), byte(len(p))}, p...) +} + +// dvbPCS is a page composition: state 0 normal case, 1 acquisition point, 2 mode change. +func dvbPCS(state byte, at ...dvbPlacement) []byte { + p := []byte{15, state << 2} + for _, a := range at { + p = append(p, a.region, 0, byte(a.x>>8), byte(a.x), byte(a.y>>8), byte(a.y)) + } + return dvbSeg(dvbSegPage, dvbTestPage, p) +} + +// dvbRCS is a region composition; depthCode is 1, 2 or 3 for 2, 4 or 8 bits. +func dvbRCS(id byte, fill bool, w, h int, depthCode, clut, background byte, refs ...dvbObjectRef) []byte { + flags := byte(0) + if fill { + flags = 0x08 + } + p := []byte{id, flags, byte(w >> 8), byte(w), byte(h >> 8), byte(h), depthCode<<5 | depthCode<<2, clut, background, background<<4 | background&3<<2} + for _, r := range refs { + p = append(p, byte(r.id>>8), byte(r.id), byte(r.x>>8)&0xf, byte(r.x), byte(r.y>>8)&0xf, byte(r.y)) + } + return dvbSeg(dvbSegRegion, dvbTestPage, p) +} + +// dvbCDS is a CLUT definition; an entry is its ID, its flags, then Y, Cr, Cb, T in full range. +func dvbCDS(id byte, entries ...[6]byte) []byte { + p := []byte{id, 0} + for _, e := range entries { + p = append(p, e[:]...) + } + return dvbSeg(dvbSegCLUT, dvbTestPage, p) +} + +func dvbODS(id uint16, nonModifying bool, top, bottom []byte) []byte { + flags := byte(0) + if nonModifying { + flags = 2 + } + p := []byte{byte(id >> 8), byte(id), flags, byte(len(top) >> 8), byte(len(top)), byte(len(bottom) >> 8), byte(len(bottom))} + return dvbSeg(dvbSegObject, dvbTestPage, append(append(p, top...), bottom...)) +} + +// dvbGrow appends bytes to a segment's payload and declares them in its length. +func dvbGrow(seg []byte, more ...byte) []byte { + out := append(append([]byte(nil), seg...), more...) + out[4], out[5] = byte((len(out)-6)>>8), byte(len(out)-6) + return out +} + +func dvbEDS() []byte { return dvbSeg(dvbSegEnd, dvbTestPage, nil) } + +// dvbBitString packs a string of 0s and 1s, spaces ignored, into bytes, zero-padded. +func dvbBitString(s string) []byte { + s = strings.ReplaceAll(s, " ", "") + out := make([]byte, (len(s)+7)/8) + for i, c := range s { + if c == '1' { + out[i/8] |= 0x80 >> (i % 8) + } + } + return out +} + +// 4-bit entries of CLUT 0: 1 white, 2 blue, 3 white at half transparency. +var dvbTestCLUT = dvbCDS(0, [6]byte{1, 0x41, 235, 128, 128, 0}, [6]byte{2, 0x41, 82, 90, 240, 0}, [6]byte{3, 0x41, 235, 128, 128, 0x80}) + +// A 4x2 object in 4-bit strings: codes 1, 1, 2, 3 on the top line, a run of four 2s on the bottom one. +var ( + dvbTop4x2 = []byte{dvbData4Bit, 0x11, 0x23, 0x00, dvbDataEndRow} + dvbBottom4x2 = []byte{dvbData4Bit, 0x08, 0x20, 0x00, dvbDataEndRow} +) + +func dvbShow4x2() []byte { + return concat( + dvbPCS(2, dvbPlacement{region: 0, x: 100, y: 500}), + dvbRCS(0, true, 4, 2, 2, 0, 0, dvbObjectRef{id: 7}), + dvbTestCLUT, + dvbODS(7, false, dvbTop4x2, dvbBottom4x2), + dvbEDS(), + ) +} + +func newDVBSub(t *testing.T) *DVBSubDecoder { + t.Helper() + return NewDVBSubDecoder(dvbTestPrivate) +} + +func decodeOneDVB(t *testing.T, d *DVBSubDecoder, block []byte) BitmapDisplaySet { + t.Helper() + sets, err := d.Decode(block) + if err != nil { + t.Fatal(err) + } + if len(sets) != 1 { + t.Fatalf("got %d display sets, want 1", len(sets)) + } + return sets[0] +} + +// --- tests --- + +func TestDVBSubDecoder_ShowThenClear(t *testing.T) { + d := newDVBSub(t) + ds := decodeOneDVB(t, d, dvbShow4x2()) + if ds.ScreenW != 720 || ds.ScreenH != 576 { + t.Errorf("plane %dx%d, want the 720x576 of a stream without display definition", ds.ScreenW, ds.ScreenH) + } + if len(ds.Objects) != 1 { + t.Fatalf("got %d objects, want 1", len(ds.Objects)) + } + o := ds.Objects[0] + if o.X != 100 || o.Y != 500 || o.Forced { + t.Errorf("object at %d,%d forced=%v, want 100,500 not forced", o.X, o.Y, o.Forced) + } + if b := o.Image.Bounds(); b.Dx() != 4 || b.Dy() != 2 { + t.Fatalf("image %dx%d, want 4x2", b.Dx(), b.Dy()) + } + br, bg, bb := ycrcbToRGB(82, 90, 240) + white, blue, half := [4]uint8{255, 255, 255, 255}, [4]uint8{br, bg, bb, 255}, [4]uint8{255, 255, 255, 127} + want := [2][4][4]uint8{{white, white, blue, half}, {blue, blue, blue, blue}} + for y := range want { + for x := range want[y] { + if got := nrgbaAt(t, o.Image, x, y); got != want[y][x] { + t.Errorf("pixel %d,%d = %v, want %v", x, y, got, want[y][x]) + } + } + } + if ds := decodeOneDVB(t, d, concat(dvbPCS(0), dvbEDS())); !ds.Clear() { + t.Errorf("a page with no region gave %d objects, want a clear", len(ds.Objects)) + } +} + +// Within an epoch a display set sends only what changed: the region keeps the pixels of the blocks before. +func TestDVBSubDecoder_NormalCaseKeepsTheRegion(t *testing.T) { + d := newDVBSub(t) + at := dvbPlacement{region: 0, x: 10, y: 20} + decodeOneDVB(t, d, concat(dvbPCS(2, at), dvbRCS(0, true, 12, 2, 2, 0, 0, dvbObjectRef{id: 7}), dvbTestCLUT, + dvbODS(7, false, dvbTop4x2, dvbBottom4x2), dvbEDS())) + ds := decodeOneDVB(t, d, concat(dvbPCS(0, at), dvbRCS(0, false, 12, 2, 2, 0, 0, dvbObjectRef{id: 8, x: 8}), + dvbODS(8, false, dvbTop4x2, dvbBottom4x2), dvbEDS())) + if len(ds.Objects) != 1 || ds.Objects[0].Image.Bounds().Dx() != 12 { + t.Fatalf("got %d objects, the first %v wide; want one of 12, both words", len(ds.Objects), ds.Objects[0].Image.Bounds().Dx()) + } + img := ds.Objects[0].Image + if nrgbaAt(t, img, 0, 0)[3] == 0 || nrgbaAt(t, img, 8, 0)[3] == 0 || nrgbaAt(t, img, 5, 0)[3] != 0 { + t.Errorf("the region lost the first object, or the second, or the gap between them is painted") + } + // A page that only lists the region again shows it as it is. + if ds := decodeOneDVB(t, d, concat(dvbPCS(0, at), dvbEDS())); len(ds.Objects) != 1 { + t.Errorf("a page listing a kept region gave %d objects, want 1", len(ds.Objects)) + } +} + +func TestDVBSubDecoder_ModeChangeAndResetStartAnEpoch(t *testing.T) { + at := dvbPlacement{region: 0, x: 10, y: 20} + for name, forget := range map[string]func(d *DVBSubDecoder) []byte{ + "mode change": func(*DVBSubDecoder) []byte { return dvbPCS(2, at) }, + "acquisition point": func(*DVBSubDecoder) []byte { return dvbPCS(1, at) }, + "Reset": func(d *DVBSubDecoder) []byte { d.Reset(); return dvbPCS(0, at) }, + } { + d := newDVBSub(t) + decodeOneDVB(t, d, dvbShow4x2()) + if ds := decodeOneDVB(t, d, concat(forget(d), dvbEDS())); !ds.Clear() { + t.Errorf("%s: a region of the epoch before is still shown", name) + } + if d.held != 0 { + t.Errorf("%s: %d bytes still counted as held", name, d.held) + } + } +} + +func TestDVBSubDecoder_BottomFieldAbsentRepeatsTheTop(t *testing.T) { + ds := decodeOneDVB(t, newDVBSub(t), concat(dvbPCS(2, dvbPlacement{}), dvbRCS(0, true, 4, 2, 2, 0, 0, dvbObjectRef{id: 7}), + dvbTestCLUT, dvbODS(7, false, dvbTop4x2, nil), dvbEDS())) + img := ds.Objects[0].Image + for x := 0; x < 4; x++ { + if nrgbaAt(t, img, x, 0) != nrgbaAt(t, img, x, 1) { + t.Errorf("column %d: the bottom line is not the top one", x) + } + } +} + +func TestDVBSubDecoder_InvisibleRegionIsNoObject(t *testing.T) { + ds := decodeOneDVB(t, newDVBSub(t), concat(dvbPCS(2, dvbPlacement{}), dvbRCS(0, true, 64, 8, 2, 0, 0), dvbEDS())) + if !ds.Clear() { + t.Errorf("a region filled with the transparent entry gave %d objects, want a clear", len(ds.Objects)) + } +} + +func TestDVBSubDecoder_RegionTrimmedToItsVisiblePixels(t *testing.T) { + ds := decodeOneDVB(t, newDVBSub(t), concat(dvbPCS(2, dvbPlacement{x: 50, y: 60}), + dvbRCS(0, true, 40, 10, 2, 0, 0, dvbObjectRef{id: 7, x: 20, y: 4}), dvbTestCLUT, dvbODS(7, false, dvbTop4x2, dvbBottom4x2), dvbEDS())) + o := ds.Objects[0] + if o.X != 70 || o.Y != 64 || o.Image.Bounds().Dx() != 4 || o.Image.Bounds().Dy() != 2 { + t.Errorf("object at %d,%d of %v, want 70,64 of 4x2", o.X, o.Y, o.Image.Bounds().Size()) + } +} + +func TestDVBSubDecoder_UndefinedRegionIsSkipped(t *testing.T) { + ds := decodeOneDVB(t, newDVBSub(t), concat(dvbPCS(0, dvbPlacement{region: 9}), dvbEDS())) + if !ds.Clear() { + t.Errorf("a region never defined gave %d objects", len(ds.Objects)) + } +} + +func TestDVBSubDecoder_DisplayDefinition(t *testing.T) { + dds := func(p ...byte) []byte { return dvbSeg(dvbSegDisplay, dvbTestPage, p) } + body := concat(dvbPCS(2, dvbPlacement{x: 100, y: 500}), dvbRCS(0, true, 4, 2, 2, 0, 0, dvbObjectRef{id: 7}), dvbTestCLUT, + dvbODS(7, false, dvbTop4x2, dvbBottom4x2), dvbEDS()) + d := newDVBSub(t) + ds := decodeOneDVB(t, d, concat(dds(0x00, 0x07, 0x7f, 0x04, 0x37), body)) + if ds.ScreenW != 1920 || ds.ScreenH != 1080 || ds.Objects[0].X != 100 { + t.Errorf("plane %dx%d, object at x %d; want 1920x1080 and 100", ds.ScreenW, ds.ScreenH, ds.Objects[0].X) + } + // The definition holds for the display sets that follow, and a window shifts what the page places. + if ds := decodeOneDVB(t, d, body); ds.ScreenW != 1920 { + t.Errorf("plane width %d on the next display set, want 1920", ds.ScreenW) + } + ds = decodeOneDVB(t, d, concat(dds(0x08, 0x07, 0x7f, 0x04, 0x37, 0x00, 0x40, 0x07, 0x3f, 0x00, 0x20, 0x04, 0x17), body)) + if o := ds.Objects[0]; o.X != 164 || o.Y != 532 { + t.Errorf("object at %d,%d inside a window at 64,32; want 164,532", o.X, o.Y) + } +} + +func TestDVBSubDecoder_OtherPagesAreAnotherService(t *testing.T) { + foreign := dvbSeg(dvbSegPage, 0x0099, []byte{15, 2 << 2}) + d := newDVBSub(t) + if sets, err := d.Decode(concat(foreign, dvbSeg(dvbSegEnd, 0x0099, nil))); err != nil || len(sets) != 0 { + t.Errorf("another page's display set gave %d sets, err %v", len(sets), err) + } + // The ancillary page's CLUT and objects serve the composition page. + clut := append([]byte(nil), dvbTestCLUT...) + clut[2], clut[3] = byte(dvbTestAncillary>>8), byte(dvbTestAncillary&0xff) + ds := decodeOneDVB(t, d, concat(dvbPCS(2, dvbPlacement{}), dvbRCS(0, true, 4, 2, 2, 0, 0, dvbObjectRef{id: 7}), clut, + dvbODS(7, false, dvbTop4x2, dvbBottom4x2), dvbEDS())) + if got := nrgbaAt(t, ds.Objects[0].Image, 0, 0); got != [4]uint8{255, 255, 255, 255} { + t.Errorf("pixel = %v, want the white of the ancillary page's CLUT", got) + } + all := NewDVBSubDecoder(nil) + if sets, _ := all.Decode(concat(foreign, dvbSeg(dvbSegEnd, 0x0099, nil))); len(sets) != 1 { + t.Errorf("without CodecPrivate a page gave %d sets, want 1", len(sets)) + } +} + +func TestDVBSubDecoder_DisplaySetBoundaries(t *testing.T) { + show := dvbShow4x2() + noEnd := show[:len(show)-len(dvbEDS())] + for name, tc := range map[string]struct { + block []byte + sets int + }{ + "no end segment": {noEnd, 1}, + "two sets": {concat(show, dvbPCS(0), dvbEDS()), 2}, + "two sets, no end segments": {concat(noEnd, dvbPCS(0)), 2}, + "PES header and end marker": {concat([]byte{0x20, 0x00}, show, []byte{0xff}), 1}, + "end without composition": {dvbEDS(), 0}, + "segments of unknown types": {concat(dvbSeg(0x15, dvbTestPage, []byte{1, 2, 3}), show), 1}, + } { + sets, err := newDVBSub(t).Decode(tc.block) + if err != nil || len(sets) != tc.sets { + t.Errorf("%s: %d sets, err %v; want %d", name, len(sets), err, tc.sets) + } + } +} + +// dvbDraw draws one field into the top line of a fresh region and returns that line's CLUT entries. +func dvbDraw(t *testing.T, depth, w int, nonModifying bool, fill byte, data []byte) ([]byte, error) { + t.Helper() + r := &dvbRegion{w: w, h: 2, depth: depth, pix: bytes.Repeat([]byte{fill}, 2*w)} + pen := dvbPen{d: &DVBSubDecoder{budget: 1 << 20}, r: r, skipOne: nonModifying} + err := pen.field(data, 0, 0) + return r.pix[:w], err +} + +func dvbRuns(pairs ...int) []byte { + var out []byte + for i := 0; i < len(pairs); i += 2 { + out = append(out, bytes.Repeat([]byte{byte(pairs[i])}, pairs[i+1])...) + } + return out +} + +func TestDVBSub_PixelCodeStrings(t *testing.T) { + for _, tc := range []struct { + name string + depth int + data []byte + want []byte + }{ + {"2-bit", 2, + concat([]byte{dvbData2Bit}, dvbBitString("01 10 11 0001 000001 00 1 010 11 0000 10 0011 10 0000 11 00000001 01 000000")), + dvbRuns(1, 1, 2, 1, 3, 1, 0, 1, 0, 2, 3, 5, 2, 15, 1, 30)}, + {"4-bit", 4, + concat([]byte{dvbData4Bit}, dvbBitString("0001 1111 0000 1100 0000 1101 0000 0 011 0000 10 01 0111 0000 1110 0010 0101 0000 1111 00000011 1001 0000 0000")), + dvbRuns(1, 1, 15, 1, 0, 1, 0, 2, 0, 5, 7, 5, 5, 11, 9, 28)}, + {"8-bit", 8, + []byte{dvbData8Bit, 0x41, 0x00, 0x05, 0x00, 0x83, 0x99, 0x00, 0x00}, + dvbRuns(0x41, 1, 0, 5, 0x99, 3)}, + {"two strings on one line", 4, + []byte{dvbData4Bit, 0x12, 0x00, dvbData4Bit, 0x34, 0x00}, + []byte{1, 2, 3, 4}}, + {"2-bit codes in a 4-bit region", 4, + concat([]byte{dvbData2Bit}, dvbBitString("01 10 11 000000")), + []byte{0x7, 0x8, 0xf}}, + {"2-bit codes through a sent 2-to-4 table", 4, + concat([]byte{dvbMap2To4, 0x12, 0x34, dvbData2Bit}, dvbBitString("01 10 11 000000")), + []byte{2, 3, 4}}, + {"2-bit codes in an 8-bit region", 8, + concat([]byte{dvbData2Bit}, dvbBitString("01 10 11 000000")), + []byte{0x77, 0x88, 0xff}}, + {"2-bit codes through a sent 2-to-8 table", 8, + concat([]byte{dvbMap2To8, 9, 10, 11, 12, dvbData2Bit}, dvbBitString("01 10 11 000000")), + []byte{10, 11, 12}}, + {"4-bit codes in an 8-bit region", 8, + []byte{dvbData4Bit, 0x1f, 0x00}, + []byte{0x11, 0xff}}, + {"4-bit codes through a sent 4-to-8 table", 8, + concat([]byte{dvbMap4To8}, bytes.Repeat([]byte{0x42}, 16), []byte{dvbData4Bit, 0x1f, 0x00}), + []byte{0x42, 0x42}}, + {"a string cut short ends there", 4, + []byte{dvbData4Bit, 0x12}, + []byte{1, 2}}, + } { + t.Run(tc.name, func(t *testing.T) { + got, err := dvbDraw(t, tc.depth, len(tc.want), false, 0, tc.data) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, tc.want) { + t.Errorf("line %v\nwant %v", got, tc.want) + } + }) + } +} + +func TestDVBSub_NonModifyingColourAndClipping(t *testing.T) { + got, err := dvbDraw(t, 4, 3, true, 9, []byte{dvbData4Bit, 0x12, 0x10, 0x00}) + if err != nil || !bytes.Equal(got, []byte{9, 2, 9}) { + t.Errorf("line %v, err %v; want code 1 to leave the region's 9", got, err) + } + // A run past the region's edge stops at it, and lines below the region are not drawn. + data := concat([]byte{dvbData4Bit}, dvbBitString("0000 1111 11111111 0011 0000 0000"), []byte{dvbDataEndRow, dvbData4Bit, 0x55, 0x00, dvbDataEndRow, dvbData4Bit, 0x66, 0x00}) + got, err = dvbDraw(t, 4, 5, false, 0, data) + if err != nil || !bytes.Equal(got, []byte{3, 3, 3, 3, 3}) { + t.Errorf("line %v, err %v; want the run clipped to the 5 pixels of the region", got, err) + } +} + +func TestDVBSub_PixelDataRefusals(t *testing.T) { + for name, tc := range map[string]struct { + depth int + data []byte + want string + }{ + "4-bit string in a 2-bit region": {2, []byte{dvbData4Bit, 0x10, 0x00}, "a 4-bit pixel string in a 2-bit region"}, + "8-bit string in a 4-bit region": {4, []byte{dvbData8Bit, 0x10, 0x00, 0x00}, "an 8-bit pixel string in a 4-bit region"}, + "map table cut short": {8, []byte{dvbMap4To8, 1, 2, 3}, "a map table of 16 bytes, 3 remain"}, + } { + if _, err := dvbDraw(t, tc.depth, 4, false, 0, tc.data); err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: err = %v, want one naming %q", name, err, tc.want) + } + } +} + +func TestDVBSub_CLUT(t *testing.T) { + d := newDVBSub(t) + // Entry 5 of the 16-entry table in reduced range, entry 6 of all three tables, entry 7 with Y 0. + reduced := dvbSeg(dvbSegCLUT, dvbTestPage, []byte{3, 0, 5, 0x40, 0xeb<<2&0xfc | 0x02, 0x00<<6 | 0x08<<2 | 0x01}) + full := dvbCDS(3, [6]byte{6, 0xe1, 235, 128, 128, 0x40}, [6]byte{7, 0x41, 0, 128, 128, 0}) + if _, err := d.Decode(concat(reduced, full)); err != nil { + t.Fatal(err) + } + c := d.cluts[3] + r, g, b := ycrcbToRGB(0xeb<<2&0xfc, 0x80, 0x80) + if c.c16[5] != [4]uint8{r, g, b, 255 - 0x40} { + t.Errorf("reduced-range entry = %v, want %v", c.c16[5], [4]uint8{r, g, b, 255 - 0x40}) + } + if want := [4]uint8{255, 255, 255, 255 - 0x40}; c.c16[6] != want || c.c256[6] != want { + t.Errorf("entry 6 = %v and %v, want %v in both tables", c.c16[6], c.c256[6], want) + } + if c.c4[2] != dvbDefaultCLUT.c4[2] { + t.Errorf("entry 6 of a 4-entry table changed entry %v", c.c4) + } + if c.c16[7] != [4]uint8{} { + t.Errorf("an entry of Y 0 = %v, want fully transparent", c.c16[7]) + } + if c.c16[1] != dvbDefaultCLUT.c16[1] { + t.Errorf("an entry the definition did not carry lost its default") + } +} + +// The defaults of clause 10, one entry of each of its cases. +func TestDVBSub_DefaultCLUT(t *testing.T) { + c := dvbDefaultCLUT + for name, tc := range map[string]struct{ got, want [4]uint8 }{ + "4: transparent": {c.c4[0], [4]uint8{}}, + "4: white": {c.c4[1], [4]uint8{255, 255, 255, 255}}, + "4: black": {c.c4[2], [4]uint8{0, 0, 0, 255}}, + "4: grey": {c.c4[3], [4]uint8{127, 127, 127, 255}}, + "16: transparent": {c.c16[0], [4]uint8{}}, + "16: red": {c.c16[1], [4]uint8{255, 0, 0, 255}}, + "16: white": {c.c16[7], [4]uint8{255, 255, 255, 255}}, + "16: black": {c.c16[8], [4]uint8{0, 0, 0, 255}}, + "16: half blue": {c.c16[12], [4]uint8{0, 0, 127, 255}}, + "256: transparent": {c.c256[0], [4]uint8{}}, + "256: red at 75 %": {c.c256[1], [4]uint8{255, 0, 0, 63}}, + "256: full red": {c.c256[0x11], [4]uint8{255, 0, 0, 255}}, + "256: two thirds of green": {c.c256[0x20], [4]uint8{0, 170, 0, 255}}, + "256: half transparent": {c.c256[0x4c], [4]uint8{0, 0, 255, 127}}, + "256: light": {c.c256[0x91], [4]uint8{255, 127, 127, 255}}, + "256: dark": {c.c256[0x99], [4]uint8{128, 0, 0, 255}}, + } { + if tc.got != tc.want { + t.Errorf("%s = %v, want %v", name, tc.got, tc.want) + } + } +} + +func TestDVBSubDecoder_Malformed(t *testing.T) { + seg := func(typ byte, p ...byte) []byte { return dvbSeg(typ, dvbTestPage, p) } + for name, tc := range map[string]struct { + block []byte + want string + }{ + "short header": {[]byte{dvbSync, 0x10, 0}, "a segment header needs 6"}, + "segment past the block": {[]byte{dvbSync, 0x10, 0, 1, 0, 9, 0}, "declares 9 bytes"}, + "page cut short": {seg(dvbSegPage, 15), "page composition segment holds 1 bytes"}, + "page with half a region": {seg(dvbSegPage, 15, 0, 1, 0, 0), "page composition segment holds 5 bytes"}, + "region cut short": {seg(dvbSegRegion, 0, 0, 0, 4), "region composition segment needs 10"}, + "region of no width": {dvbRCS(0, true, 0, 2, 2, 0, 0), "outside 1..4096"}, + "region too tall": {dvbRCS(0, true, 4, 5000, 2, 0, 0), "outside 1..4096"}, + "region of no depth": {dvbRCS(0, true, 4, 2, 0, 0, 0), "pixel depth code 0"}, + "half an object ref": {dvbGrow(dvbRCS(0, true, 4, 2, 2, 0, 0), 1, 2, 3), "of an object reference of 6"}, + "CLUT cut short": {seg(dvbSegCLUT, 0), "CLUT definition segment needs 2"}, + "CLUT entry cut short": {seg(dvbSegCLUT, 0, 0, 1, 0x41, 235, 128), "of an entry of 6"}, + "object cut short": {seg(dvbSegObject, 0, 7), "object data segment needs 3"}, + "object without lengths": {seg(dvbSegObject, 0, 7, 0, 0, 5), "declares no field lengths"}, + "object fields too long": {seg(dvbSegObject, 0, 7, 0, 0, 5, 0, 5, 1, 2, 3), "declares 10 bytes of fields"}, + "display cut short": {seg(dvbSegDisplay, 0, 7, 0x7f), "display definition segment needs 5"}, + "display window missing": {seg(dvbSegDisplay, 8, 7, 0x7f, 4, 0x37, 0, 0), "declares a window"}, + "bad object data": {concat(dvbPCS(2), dvbRCS(0, true, 4, 2, 1, 0, 0, dvbObjectRef{id: 7}), dvbODS(7, false, dvbTop4x2, nil)), + "object 7: a 4-bit pixel string in a 2-bit region"}, + } { + sets, err := newDVBSub(t).Decode(tc.block) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: %d sets, err = %v, want one naming %q", name, len(sets), err, tc.want) + } + } +} + +// Objects that are not pictures (characters, a coding the format added later) are passed over. +func TestDVBSubDecoder_NonPixelObjectsIgnored(t *testing.T) { + chars := dvbSeg(dvbSegObject, dvbTestPage, []byte{0, 7, 1 << 2, 1, 0, 0x41}) + region := dvbGrow(dvbRCS(0, true, 4, 2, 2, 0, 0), 0, 7, 0x40, 0, 0, 0, 1, 2) // a character object reference, two bytes longer + ds := decodeOneDVB(t, newDVBSub(t), concat(dvbPCS(2, dvbPlacement{}), region, chars, dvbEDS())) + if !ds.Clear() { + t.Errorf("a character object drew %d objects", len(ds.Objects)) + } +} + +// A few bytes naming a full-size region to fill again and again are stopped by the block's budget. +func TestDVBSubBlockPixelsBounded(t *testing.T) { + fill := dvbRCS(0, true, dvbMaxDim, dvbMaxDim, 3, 0, 1) + d := newDVBSub(t) + if _, err := d.Decode(concat(fill, fill, fill, fill)); err != nil { + t.Fatalf("four fills of a full-size region: %v", err) + } + _, err := d.Decode(concat(fill, fill, fill, fill, fill)) + if err == nil || !strings.Contains(err.Error(), "fills and draws more than") { + t.Fatalf("err = %v, want the block's pixel bound", err) + } + if _, err := d.Decode(fill); err != nil { + t.Errorf("the block after a refused one: %v", err) + } +} + +func TestDVBSubEpochRegionsBounded(t *testing.T) { + d := newDVBSub(t) + if _, err := d.Decode(dvbRCS(0, false, dvbMaxDim, dvbMaxDim, 3, 0, 0)); err != nil { + t.Fatal(err) + } + _, err := d.Decode(dvbRCS(1, false, 64, 64, 3, 0, 0)) + if err == nil || !strings.Contains(err.Error(), "brings the regions held since the last mode change") { + t.Fatalf("err = %v, want the epoch bound", err) + } + // Defining the first region again, smaller, gives its bytes back. + if _, err := d.Decode(concat(dvbRCS(0, false, 64, 64, 3, 0, 0), dvbRCS(1, false, 64, 64, 3, 0, 0))); err != nil { + t.Fatalf("after shrinking region 0: %v", err) + } + if d.held != 2*64*64 { + t.Errorf("held = %d, want %d", d.held, 2*64*64) + } +} + +// An object a region lists thousands of times costs its data each time, and the block's budget counts it. +func TestDVBSubObjectListedManyTimesBounded(t *testing.T) { + refs := make([]dvbObjectRef, 10000) + for i := range refs { + refs[i].id = 7 + } + long := concat([]byte{dvbData4Bit}, dvbBitString("0000 1111 11111111 0001 0000 0000"), []byte{dvbDataEndRow}) + top := bytes.Repeat(long, 200) + block := concat(dvbPCS(2, dvbPlacement{}), dvbRCS(0, true, 64, 64, 2, 0, 0, refs...)) + for i := 0; i < 8; i++ { + block = append(block, dvbODS(7, false, top, nil)...) + } + _, err := newDVBSub(t).Decode(block) + if err == nil || !strings.Contains(err.Error(), "fills and draws more than") { + t.Fatalf("err = %v, want the block's pixel bound", err) + } +} + +// A page may place one region many times: it is read through its CLUT once per display set, the placements sharing +// the picture. 32 placements of a 1024x1024 region each made a picture of 4 MiB. +func TestDVBSubRegionRenderedOncePerDisplaySet(t *testing.T) { + at := make([]dvbPlacement, 32) + for i := range at { + at[i] = dvbPlacement{region: 0, x: i, y: 2 * i} + } + block := concat(dvbPCS(2, at...), dvbRCS(0, true, 1024, 1024, 3, 0, 1), dvbEDS()) + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + sets, err := newDVBSub(t).Decode(block) + runtime.ReadMemStats(&after) + if err != nil { + t.Fatal(err) + } + if len(sets) != 1 || len(sets[0].Objects) != 32 { + t.Fatalf("%d display sets, want one placing the region 32 times", len(sets)) + } + for i, o := range sets[0].Objects { + if o.X != i || o.Y != 2*i || o.Image != sets[0].Objects[0].Image { + t.Fatalf("placement %d at %d,%d with its own picture; want %d,%d sharing the first", i, o.X, o.Y, i, 2*i) + } + } + if alloc := after.TotalAlloc - before.TotalAlloc; alloc > 12<<20 { + t.Errorf("32 placements of one 1024x1024 region allocated %d MiB, want the region and one picture, 5 MiB", alloc>>20) + } +} + +// The budget counts a region once per display set too: 8 placements of a full-size region are one picture. +func TestDVBSubPlacementsOfOneRegionCostOnePicture(t *testing.T) { + at := make([]dvbPlacement, 8) + block := concat(dvbPCS(2, at...), dvbRCS(0, true, dvbMaxDim, dvbMaxDim, 3, 0, 1), dvbEDS()) + if _, err := newDVBSub(t).Decode(block); err != nil { + t.Fatalf("8 placements of one region: %v", err) + } +} + +func TestNewDVBSubDecoder_ShortCodecPrivateDecodesEveryPage(t *testing.T) { + for n := 1; n <= 3; n++ { + d := NewDVBSubDecoder(make([]byte, n)) + if sets, err := d.Decode(concat(dvbSeg(dvbSegPage, 0x0099, []byte{15, 2 << 2}), dvbSeg(dvbSegEnd, 0x0099, nil))); err != nil || len(sets) != 1 { + t.Errorf("%d byte(s): a page gave %d sets, err %v; want it decoded", n, len(sets), err) + } + } +} + +// Bytes that are no segment end the block: the display set composed ahead of them is given, without an error. +func TestDVBSubDecoder_JunkAfterTheSegments(t *testing.T) { + show := dvbShow4x2() + noEnd := show[:len(show)-len(dvbEDS())] + for name, block := range map[string][]byte{ + "after the end segment": concat(show, []byte{0x47, 0x1f, 0xff, 0x10}), + "in place of it": concat(noEnd, []byte{0x00, 0x00}), + } { + sets, err := newDVBSub(t).Decode(block) + if err != nil || len(sets) != 1 || len(sets[0].Objects) != 1 { + t.Errorf("%s: %d sets, err %v; want the display set", name, len(sets), err) + } + } +} + +// A pixel-data block of a type the format does not define ends its field: what was drawn ahead of it stays. +func TestDVBSubDecoder_UnknownPixelDataEndsTheField(t *testing.T) { + top := concat(dvbTop4x2, []byte{0x33, 0x01, 0x02}) + ds := decodeOneDVB(t, newDVBSub(t), concat(dvbPCS(2, dvbPlacement{}), dvbRCS(0, true, 4, 2, 2, 0, 0, dvbObjectRef{id: 7}), + dvbTestCLUT, dvbODS(7, false, top, dvbBottom4x2), dvbEDS())) + if len(ds.Objects) != 1 || ds.Objects[0].Image.Bounds().Dy() != 2 { + t.Fatalf("%d objects; want the picture, both lines drawn", len(ds.Objects)) + } +} + +// A block refused part-way gives back, with the error, the display sets it had completed and the one it was composing. +func TestDVBSubDecoder_ErrorKeepsTheSetsAheadOfIt(t *testing.T) { + bad := dvbSeg(dvbSegRegion, dvbTestPage, []byte{0, 0, 0, 4}) + d := newDVBSub(t) + sets, err := d.Decode(concat(dvbShow4x2(), dvbPCS(0, dvbPlacement{x: 100, y: 500}), bad, dvbEDS())) + if err == nil || !strings.Contains(err.Error(), "region composition segment needs") { + t.Fatalf("err = %v, want the region's", err) + } + if len(sets) != 2 || len(sets[0].Objects) != 1 || len(sets[1].Objects) != 1 { + t.Fatalf("%d sets with the error; want the completed one and the one being composed", len(sets)) + } + // The next block starts from the state the error left, which is the one the sets showed. + if ds := decodeOneDVB(t, d, concat(dvbPCS(0, dvbPlacement{x: 100, y: 500}), dvbEDS())); len(ds.Objects) != 1 { + t.Errorf("the block after the error shows %d objects, want the region still held", len(ds.Objects)) + } +} + +// The page's time-out is given as it is, a bound on how long the set stays without a successor, not its duration. +func TestDVBSubDecoder_Timeout(t *testing.T) { + d := newDVBSub(t) + ds := decodeOneDVB(t, d, dvbShow4x2()) + if ds.Timeout != 15*time.Second || ds.Duration != 0 || ds.Delay != 0 { + t.Errorf("timeout %v duration %v delay %v; want the page's 15 s and no duration", ds.Timeout, ds.Duration, ds.Delay) + } + short := dvbSeg(dvbSegPage, dvbTestPage, []byte{3, 0}) + if ds := decodeOneDVB(t, d, concat(short, dvbEDS())); ds.Timeout != 3*time.Second { + t.Errorf("timeout %v after a page of 3 s", ds.Timeout) + } +} diff --git a/mkv/subtitle/parse_fuzz_test.go b/mkv/subtitle/parse_fuzz_test.go index d74eb5c6..1cf6b5bf 100644 --- a/mkv/subtitle/parse_fuzz_test.go +++ b/mkv/subtitle/parse_fuzz_test.go @@ -57,3 +57,91 @@ func FuzzPGSDecode(f *testing.F) { }) }) } + +// FuzzVobSubDecode: any bytes handed to a VobSub decoder, in one block or split in two, give display sets or an +// error, never a panic, within the input's time budget. +func FuzzVobSubDecode(f *testing.F) { + f.Add(vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}, spuSeq{date: 300, cmds: []byte{spuStop}}), 7) + f.Add(vobsubSPU(nil, nil, spuSeq{date: 45, cmds: []byte{spuStop}}), 0) + f.Add([]byte{0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04}, 0) + f.Fuzz(func(t *testing.T, data []byte, cut int) { + costguard.Fuzz(t, len(data), func() { + d, err := NewVobSubDecoder([]byte(vobsubIdx)) + if err != nil { + t.Fatal(err) + } + if cut > 0 && cut < len(data) { + _, _ = d.Decode(data[:cut]) + _, _ = d.Decode(data[cut:]) + return + } + _, _ = d.Decode(data) + }) + }) +} + +// FuzzVobSubHeader: any CodecPrivate gives a decoder or an error, never a panic. +func FuzzVobSubHeader(f *testing.F) { + f.Add([]byte(vobsubIdx)) + f.Add([]byte("size: 0x0\npalette: ,\n")) + f.Fuzz(func(t *testing.T, data []byte) { + costguard.Fuzz(t, len(data), func() { _, _ = NewVobSubDecoder(data) }) + }) +} + +// FuzzDVBSubDecode: any bytes handed to a DVB subtitle decoder, in one block or split in two, with or without the +// page filter, give display sets or an error, never a panic, within the input's time budget. +func FuzzDVBSubDecode(f *testing.F) { + f.Add(dvbShow4x2(), 0, true) + f.Add(concat(dvbShow4x2(), dvbPCS(0), dvbEDS()), len(dvbShow4x2()), false) + f.Add(concat(dvbPCS(2, dvbPlacement{}), dvbRCS(0, true, 720, 36, 3, 0, 1, dvbObjectRef{id: 1}), + dvbODS(1, true, []byte{dvbMap2To8, 1, 2, 3, 4, dvbData2Bit, 0x6c, 0x00, dvbDataEndRow, dvbData8Bit, 0x41, 0x00, 0x00}, nil)), 9, true) + f.Fuzz(func(t *testing.T, data []byte, cut int, filter bool) { + costguard.Fuzz(t, len(data), func() { + var private []byte + if filter { + private = dvbTestPrivate + } + d := NewDVBSubDecoder(private) + if cut > 0 && cut < len(data) { + _, _ = d.Decode(data[:cut]) + _, _ = d.Decode(data[cut:]) + return + } + _, _ = d.Decode(data) + }) + }) +} + +// FuzzVobSubFiles: any .idx and any .sub give units or an error, never a panic, within the input's time budget. +func FuzzVobSubFiles(f *testing.F) { + unit := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + f.Add([]byte(vobsubIdxFile), concat(subPack(0x20, true, unit[:9]), subPadding(5), subPack(0x20, false, unit[9:]))) + f.Add([]byte("id: en, index: 0\ntimestamp: 00:00:00:000, filepos: 0\n"), []byte{0, 0, 1, 0xbd, 0, 4, 0x80, 0, 0, 0x20}) + f.Fuzz(func(t *testing.T, idx, sub []byte) { + costguard.Fuzz(t, len(idx)+len(sub), func() { + ix, err := ParseVobSubIndex(bytes.NewReader(idx)) + if err != nil { + return + } + for i := range ix.Streams { + _ = ix.ForEachUnit(bytes.NewReader(sub), i, func(int64, []byte) error { return nil }) + } + }) + }) +} + +// FuzzSUPFile: any bytes read as a .sup give display sets or an error, never a panic. +func FuzzSUPFile(f *testing.F) { + f.Add(supFile(concat(pgsPCS(1920, 1080, pgsEpochStart, 0), pgsEND()), pgsPCS(1920, 1080, 0, 0))) + f.Add([]byte("PG")) + f.Fuzz(func(t *testing.T, data []byte) { + costguard.Fuzz(t, len(data), func() { + d := NewPGSDecoder() + _ = ForEachSUPDisplaySet(bytes.NewReader(data), func(_ int64, set []byte) error { + _, _ = d.Decode(set) + return nil + }) + }) + }) +} diff --git a/mkv/subtitle/pgs.go b/mkv/subtitle/pgs.go index 9fe81fb8..ba49ec8d 100644 --- a/mkv/subtitle/pgs.go +++ b/mkv/subtitle/pgs.go @@ -27,6 +27,7 @@ import ( "fmt" "image" "image/color" + "time" ) // Segment types, from the display set's segment header (type, 16-bit size). @@ -90,6 +91,12 @@ type PGSObject struct { type PGSDisplaySet struct { ScreenW, ScreenH int // the subtitle plane the positions are expressed in Objects []PGSObject + // Delay is how long after the block's time the set takes effect, and Duration how long its pictures then stay + // on screen: a VobSub unit's start and stop commands. Zero when the stream says neither, as PGS and DVB never do. + Delay, Duration time.Duration + // Timeout is how long the set may stay on screen when nothing replaces or clears it: a DVB page's time-out, a + // bound of whole seconds, not a duration. Zero for PGS and VobSub. + Timeout time.Duration } // Clear reports whether this display set empties the screen rather than filling diff --git a/mkv/subtitle/pgsfile.go b/mkv/subtitle/pgsfile.go new file mode 100644 index 00000000..a1d54b86 --- /dev/null +++ b/mkv/subtitle/pgsfile.go @@ -0,0 +1,61 @@ +package subtitle + +// pgsfile.go - PGS as the .sup file that sits beside a video: the segments of the stream one after another, each +// behind "PG" and its presentation and decoding times on the 90 kHz clock. + +import ( + "bufio" + "encoding/binary" + "errors" + "fmt" + "io" +) + +// supMaxDisplaySet bounds the segments gathered for one display set: what an epoch's objects may hold. +const supMaxDisplaySet = pgsMaxEpochBytes + +// ForEachSUPDisplaySet reads a .sup file and calls fn with each display set: its time, the presentation time of its +// composition segment, and its segments as a Matroska block holds them, which is what PGSDecoder.Decode takes. A +// file cut short inside a segment gives the display sets ahead of it, then an error; one that ends between two +// segments gives its last display set as it stands, without its end segment. An error from fn stops the read and is +// returned as is. +func ForEachSUPDisplaySet(r io.Reader, fn func(timeMs int64, displaySet []byte) error) error { + br := bufio.NewReader(r) + var set []byte + var timeMs int64 + timed := false + for n := 0; ; n++ { + var head [13]byte + if _, err := io.ReadFull(br, head[:]); err != nil { + if !errors.Is(err, io.EOF) { + return fmt.Errorf("PGS file: segment %d is cut short in its header: %w", n, err) + } + if len(set) == 0 { + return nil + } + return fn(timeMs, set) // a last display set without its end segment + } + if head[0] != 'P' || head[1] != 'G' { + return fmt.Errorf("PGS file: segment %d starts with %q, not \"PG\": the file is not a .sup", n, head[:2]) + } + typ, size := head[10], int(binary.BigEndian.Uint16(head[11:13])) + if len(set)+3+size > supMaxDisplaySet { + return fmt.Errorf("PGS file: the display set at %d ms holds more than %d bytes", timeMs, supMaxDisplaySet) + } + set = append(set, head[10:13]...) + set = append(set, make([]byte, size)...) + if _, err := io.ReadFull(br, set[len(set)-size:]); err != nil { + return fmt.Errorf("PGS file: segment %d is cut short in its %d bytes: %w", n, size, err) + } + if typ == pgsSegPCS || !timed { + timeMs, timed = int64(binary.BigEndian.Uint32(head[2:6]))/90, true + } + if typ != pgsSegEND { + continue + } + if err := fn(timeMs, set); err != nil { + return err + } + set, timed = nil, false + } +} diff --git a/mkv/subtitle/sidecar_test.go b/mkv/subtitle/sidecar_test.go new file mode 100644 index 00000000..b4821b5a --- /dev/null +++ b/mkv/subtitle/sidecar_test.go @@ -0,0 +1,286 @@ +package subtitle + +import ( + "bytes" + "errors" + "io" + "strings" + "testing" +) + +const vobsubIdxFile = vobsubIdx + `langidx: 0 +time offset: 100 + +id: en, index: 0 +alt: English +timestamp: 00:00:01:000, filepos: 000000000 +timestamp: 00:00:01:010, filepos: 000000800 +delay: 00:00:10:000 +timestamp: 01:02:03:004, filepos: 00000a000 + +id: fr, index: 1 +timestamp: 00:00:05:500, filepos: 000001000 +delay: -00:00:00:500 +timestamp: 00:00:07:000, filepos: 000002000 +` + +func TestParseVobSubIndex(t *testing.T) { + ix, err := ParseVobSubIndex(strings.NewReader(vobsubIdxFile)) + if err != nil { + t.Fatal(err) + } + if _, err := NewVobSubDecoder(ix.Header); err != nil { + t.Errorf("the header does not make a decoder: %v", err) + } + if strings.Contains(string(ix.Header), "timestamp") || !strings.Contains(string(ix.Header), "langidx: 0") { + t.Errorf("header = %q, want every line ahead of the first stream and none after", ix.Header) + } + want := []VobSubStream{ + {Language: "en", Name: "English", Index: 0, Entries: []VobSubEntry{{1100, 0}, {1110, 0x800}, {3723104 + 10000, 0xa000}}}, + {Language: "fr", Index: 1, Entries: []VobSubEntry{{5600, 0x1000}, {6600, 0x2000}}}, + } + if len(ix.Streams) != len(want) { + t.Fatalf("got %d streams, want %d", len(ix.Streams), len(want)) + } + for i, w := range want { + g := ix.Streams[i] + if g.Language != w.Language || g.Name != w.Name || g.Index != w.Index || len(g.Entries) != len(w.Entries) { + t.Errorf("stream %d = %+v, want %+v", i, g, w) + continue + } + for k := range w.Entries { + if g.Entries[k] != w.Entries[k] { + t.Errorf("stream %d entry %d = %+v, want %+v", i, k, g.Entries[k], w.Entries[k]) + } + } + } +} + +func TestParseVobSubIndex_Refusals(t *testing.T) { + for name, tc := range map[string]struct{ idx, want string }{ + "no stream": {vobsubIdx, "names no stream"}, + "bad stream": {"id: en, index: x\n", "does not name a stream"}, + "stream past 31": {"id: en, index: 32\n", "does not name a stream"}, + "orphan timestamp": {"timestamp: 00:00:01:000, filepos: 0\n", "ahead of any"}, + "bad time": {"id: en, index: 0\ntimestamp: 00:00:01, filepos: 0\n", "is not \"timestamp:"}, + "bad position": {"id: en, index: 0\ntimestamp: 00:00:01:000, filepos: zz\n", "is not \"timestamp:"}, + "bad delay": {"id: en, index: 0\ndelay: soon\n", "is not HH:MM:SS:mmm"}, + "endless line": {"id: en, index: 0\n" + strings.Repeat("x", vobsubMaxIndexLine+1), "token too long"}, + } { + if _, err := ParseVobSubIndex(strings.NewReader(tc.idx)); err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: err = %v, want one naming %q", name, err, tc.want) + } + } +} + +// subPack is one pack of a .sub: a pack header, then a private-stream packet carrying payload for a substream. +func subPack(substream byte, pts bool, payload []byte) []byte { + head := []byte{0x80, 0x00, 0x00} + if pts { + head = []byte{0x80, 0x80, 0x05, 0x21, 0x00, 0x01, 0x00, 0x01} + } + pes := append(append(head, substream), payload...) + out := []byte{0, 0, 1, 0xba, 0x44, 0, 4, 0, 4, 1, 1, 0x89, 0xc3, 0xf8} + return append(append(out, 0, 0, 1, 0xbd, byte(len(pes)>>8), byte(len(pes))), pes...) +} + +func subPadding(n int) []byte { + return append([]byte{0, 0, 1, 0xbe, byte(n >> 8), byte(n)}, bytes.Repeat([]byte{0xff}, n)...) +} + +// A unit over two packs, the other language's pack and padding between them; the index also lists the second pack. +func TestVobSubIndex_ForEachUnit(t *testing.T) { + en := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}, spuSeq{date: 300, cmds: []byte{spuStop}}) + fr := vobsubSPU(nil, nil, spuSeq{date: 45, cmds: []byte{spuStop}}) + first := subPack(0x20, true, en[:9]) + other := subPack(0x21, true, fr) + pad := subPadding(5) + second := subPack(0x20, false, en[9:]) + third := subPack(0x20, true, en) + sub := concat(first, other, pad, second, third) + at := func(parts ...[]byte) int64 { return int64(len(concat(parts...))) } + ix := &VobSubIndex{Streams: []VobSubStream{ + {Language: "en", Index: 0, Entries: []VobSubEntry{{1000, 0}, {1000, at(first, other, pad)}, {5000, at(first, other, pad, second)}}}, + {Language: "fr", Index: 1, Entries: []VobSubEntry{{2000, at(first)}}}, + }} + type got struct { + timeMs int64 + unit []byte + } + read := func(stream int) (out []got) { + t.Helper() + err := ix.ForEachUnit(bytes.NewReader(sub), stream, func(timeMs int64, unit []byte) error { + out = append(out, got{timeMs, unit}) + return nil + }) + if err != nil { + t.Fatal(err) + } + return out + } + units := read(0) + if len(units) != 2 || units[0].timeMs != 1000 || units[1].timeMs != 5000 || !bytes.Equal(units[0].unit, en) || !bytes.Equal(units[1].unit, en) { + t.Fatalf("stream 0 gave %d units (%+v), want the unit at 1000 ms and the one at 5000 ms", len(units), units) + } + if units := read(1); len(units) != 1 || units[0].timeMs != 2000 || !bytes.Equal(units[0].unit, fr) { + t.Fatalf("stream 1 gave %+v, want its one unit", units) + } + sets, err := newVobSub(t).Decode(units[0].unit) + if err != nil || len(sets) != 1 || len(sets[0].Objects) != 1 { + t.Errorf("the unit read from the .sub decodes to %d sets, err %v", len(sets), err) + } + stop := errors.New("stop") + if err := ix.ForEachUnit(bytes.NewReader(sub), 0, func(int64, []byte) error { return stop }); err != stop { + t.Errorf("err = %v, want the callback's own", err) + } +} + +// An index entry at the end of the .sub, which some tools write last, is no unit and no error. +func TestVobSubIndex_ForEachUnit_EntryPastTheEnd(t *testing.T) { + unit := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + sub := subPack(0x20, true, unit) + ix := &VobSubIndex{Streams: []VobSubStream{{Entries: []VobSubEntry{{1000, 0}, {2000, int64(len(sub))}, {3000, 1 << 20}}}}} + n := 0 + err := ix.ForEachUnit(bytes.NewReader(sub), 0, func(int64, []byte) error { n++; return nil }) + if err != nil || n != 1 { + t.Errorf("%d units, err %v; want the one unit the .sub holds", n, err) + } +} + +func TestVobSubIndex_ForEachUnit_Refusals(t *testing.T) { + unit := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + one := func(pos int64) *VobSubIndex { + return &VobSubIndex{Streams: []VobSubStream{{Index: 0, Entries: []VobSubEntry{{1000, pos}}}}} + } + mpeg1 := subPack(0x20, true, unit) + mpeg1[4] = 0x21 + for name, tc := range map[string]struct { + sub []byte + ix *VobSubIndex + stream int + want string + }{ + "no such stream": {nil, one(0), 3, "stream 3 of an index of 1"}, + "not a packet": {[]byte("RIFF....WAVE"), one(0), 0, "does not start an MPEG packet"}, + "MPEG-1 pack": {mpeg1, one(0), 0, "not an MPEG-2 program stream pack"}, + "unit cut short": {subPack(0x20, true, unit[:9]), one(0), 0, "the file ends 9 bytes into a unit of"}, + "packet cut short": {subPack(0x20, true, unit)[:30], one(0), 0, "cut short"}, + "program end": {concat(subPack(0x20, true, unit[:9]), []byte{0, 0, 1, 0xb9, 0, 0}), one(0), 0, "the stream ends 9 bytes into a unit"}, + "empty packet": {[]byte{0, 0, 1, 0xbd, 0, 3, 0x80, 0, 0}, one(0), 0, "holds no payload"}, + "unit of no size": {subPack(0x20, true, []byte{0, 2, 0, 0}), one(0), 0, "the unit declares 2 bytes"}, + "only another language": {bytes.Repeat(subPack(0x21, true, unit), 1), one(0), 0, "no packet at byte"}, + } { + err := tc.ix.ForEachUnit(bytes.NewReader(tc.sub), tc.stream, func(int64, []byte) error { return nil }) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: err = %v, want one naming %q", name, err, tc.want) + } + } +} + +// supFile wraps display set segments as a .sup holds them, every segment of a set at the set's time. +func supFile(sets ...[]byte) []byte { + var out []byte + for i, set := range sets { + pts := uint32(i+1) * 90000 + for len(set) > 0 { + n := 3 + int(set[1])<<8 + int(set[2]) + out = append(out, 'P', 'G', byte(pts>>24), byte(pts>>16), byte(pts>>8), byte(pts), 0, 0, 0, 0) + out = append(out, set[:n]...) + set = set[n:] + } + } + return out +} + +func TestForEachSUPDisplaySet(t *testing.T) { + show := concat(pgsPCS(1920, 1080, pgsEpochStart, 0, synthObject{id: 1, x: 10, y: 20}), pgsWDS(10, 20, 4, 2), + pgsPDS(0, palWhite, palBlue), pgsODS(1, 4, 2, rle4x2), pgsEND()) + clear := concat(pgsPCS(1920, 1080, 0, 0), pgsEND()) + noEnd := clear[:len(clear)-len(pgsEND())] + type got struct { + timeMs int64 + set []byte + } + read := func(file []byte) (out []got, err error) { + err = ForEachSUPDisplaySet(bytes.NewReader(file), func(timeMs int64, set []byte) error { + out = append(out, got{timeMs, append([]byte(nil), set...)}) + return nil + }) + return out, err + } + sets, err := read(supFile(show, clear, noEnd)) + if err != nil { + t.Fatal(err) + } + if len(sets) != 3 || sets[0].timeMs != 1000 || sets[1].timeMs != 2000 || sets[2].timeMs != 3000 { + t.Fatalf("got %d sets (%v), want three at 1, 2 and 3 s", len(sets), sets) + } + if !bytes.Equal(sets[0].set, show) || !bytes.Equal(sets[1].set, clear) || !bytes.Equal(sets[2].set, noEnd) { + t.Errorf("a display set is not the segments the file holds for it") + } + d := NewPGSDecoder() + if ds, err := d.Decode(sets[0].set); err != nil || len(ds) != 1 || len(ds[0].Objects) != 1 { + t.Errorf("the first set decodes to %d sets, err %v", len(ds), err) + } + + file := supFile(show, clear) + for name, tc := range map[string]struct { + file []byte + sets int + want string + }{ + "cut in a header": {file[:len(file)-len(clear)-20+5], 1, "is cut short in its header"}, + "cut in a segment": {file[:len(supFile(show))+15], 1, "is cut short in its"}, + "not a .sup": {[]byte("1\n00:00:01,000 --> 00:00:02,000\nHello\n"), 0, "the file is not a .sup"}, + } { + sets, err := read(tc.file) + if err == nil || !strings.Contains(err.Error(), tc.want) || len(sets) != tc.sets { + t.Errorf("%s: %d sets, err = %v; want %d sets and an error naming %q", name, len(sets), err, tc.sets, tc.want) + } + } + if sets, err := read(nil); err != nil || len(sets) != 0 { + t.Errorf("an empty file: %d sets, err %v", len(sets), err) + } + stop := io.ErrClosedPipe + if err := ForEachSUPDisplaySet(bytes.NewReader(file), func(int64, []byte) error { return stop }); err != stop { + t.Errorf("err = %v, want the callback's own", err) + } +} + +func TestForEachSUPDisplaySet_Bounded(t *testing.T) { + seg := pgsSegment(pgsSegODS, make([]byte, 65535)) + var sets [][]byte + big := bytes.Repeat(seg, supMaxDisplaySet/len(seg)+1) + sets = append(sets, big) + err := ForEachSUPDisplaySet(bytes.NewReader(supFile(sets...)), func(int64, []byte) error { return nil }) + if err == nil || !strings.Contains(err.Error(), "holds more than") { + t.Errorf("err = %v, want the display set bound", err) + } +} + +// eofReaderAt returns io.EOF together with the last bytes of the file, as io.ReaderAt allows. + +// eofReaderAt returns io.EOF together with the last bytes of the file, as io.ReaderAt allows. +type eofReaderAt struct{ data []byte } + +func (r eofReaderAt) ReadAt(p []byte, off int64) (int, error) { + if off >= int64(len(r.data)) { + return 0, io.EOF + } + n := copy(p, r.data[off:]) + if off+int64(n) == int64(len(r.data)) { + return n, io.EOF + } + return n, nil +} + +func TestVobSubIndex_ForEachUnit_ReaderAtEOFWithTheLastBytes(t *testing.T) { + unit := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + sub := subPack(0x20, true, unit) + ix := &VobSubIndex{Streams: []VobSubStream{{Entries: []VobSubEntry{{1000, 0}}}}} + var got []byte + err := ix.ForEachUnit(eofReaderAt{sub}, 0, func(_ int64, u []byte) error { got = u; return nil }) + if err != nil || !bytes.Equal(got, unit) { + t.Errorf("err %v, %d bytes; want the unit that ends the file", err, len(got)) + } +} diff --git a/mkv/subtitle/vobsub.go b/mkv/subtitle/vobsub.go new file mode 100644 index 00000000..1c1dcce0 --- /dev/null +++ b/mkv/subtitle/vobsub.go @@ -0,0 +1,325 @@ +package subtitle + +// vobsub.go - a decoder for DVD subpictures (S_VOBSUB), the bitmap subtitles of DVD rips. A Matroska block holds +// one subpicture unit: a four-colour picture, run-length coded as two interlaced fields, and the control sequences +// that give its colours, its place on the plane and when it is shown and removed. The sixteen colours the picture +// picks from and the plane's size are in the track's CodecPrivate, the text header of a .idx file. +// +// A subpicture stands alone: the only thing the decoder holds between blocks is the start of a unit that a muxer +// spread over several of them. +// +// Written from the format description, not ported: mkvgo is MIT and links no third-party subtitle decoder. + +import ( + "bytes" + "encoding/binary" + "fmt" + "image" + "strconv" + "strings" + "time" +) + +// Commands of a subpicture control sequence. +const ( + spuForcedStart = 0x00 // show, even with subtitles off + spuStart = 0x01 + spuStop = 0x02 + spuColours = 0x03 // the palette entry of each of the four pixel values + spuContrast = 0x04 // the opacity of each, 0..15 + spuArea = 0x05 + spuOffsets = 0x06 // where each field's data starts + spuChange = 0x07 // colour and contrast changes within the picture, not applied + spuEnd = 0xff +) + +// vobsubMaxDim is the widest and tallest area the 12-bit coordinates can name: the bound on what one block decodes. +const vobsubMaxDim = 4096 + +// VobSubDecoder decodes a VobSub track block by block. +type VobSubDecoder struct { + screenW, screenH int + palette [16][3]uint8 + pending []byte // a unit whose first blocks hold less than it declares, at most 64 KiB +} + +// Reset drops a unit left incomplete: call it when the read skips blocks, so the next one is not taken for its rest. +func (d *VobSubDecoder) Reset() { d.pending = nil } + +// Continues reports whether Decode will take block as the rest of the unit the blocks before it started, rather +// than as the start of a unit: a caller asks it ahead of each Decode, a unit's time being that of its first block. +func (d *VobSubDecoder) Continues(block []byte) bool { + return len(d.pending) > 0 && !wholeSPU(block) +} + +// NewVobSubDecoder reads the plane size and the palette from the track's CodecPrivate. +func NewVobSubDecoder(codecPrivate []byte) (*VobSubDecoder, error) { + d := &VobSubDecoder{} + var sized, coloured bool + for _, line := range strings.Split(string(bytes.TrimRight(codecPrivate, "\x00")), "\n") { + key, val, ok := strings.Cut(strings.TrimSpace(line), ":") + if !ok || strings.HasPrefix(key, "#") { + continue + } + val = strings.TrimSpace(val) + switch strings.ToLower(strings.TrimSpace(key)) { + case "size": + ws, hs, _ := strings.Cut(strings.ToLower(val), "x") + w, errW := strconv.Atoi(strings.TrimSpace(ws)) + h, errH := strconv.Atoi(strings.TrimSpace(hs)) + if errW != nil || errH != nil || w <= 0 || h <= 0 || w > vobsubMaxDim || h > vobsubMaxDim { + return nil, fmt.Errorf("VobSub: the CodecPrivate's size %q is not a width x height within 1..%d", val, vobsubMaxDim) + } + d.screenW, d.screenH, sized = w, h, true + case "palette": + cols := strings.Split(val, ",") + if len(cols) != len(d.palette) { + return nil, fmt.Errorf("VobSub: the CodecPrivate's palette holds %d colours, a subpicture picks from %d", len(cols), len(d.palette)) + } + for i, c := range cols { + rgb, err := strconv.ParseUint(strings.TrimSpace(c), 16, 24) + if err != nil { + return nil, fmt.Errorf("VobSub: the CodecPrivate's palette entry %d, %q, is not an RRGGBB colour", i, strings.TrimSpace(c)) + } + d.palette[i] = [3]uint8{uint8(rgb >> 16), uint8(rgb >> 8), uint8(rgb)} + } + coloured = true + } + } + if !sized || !coloured { + return nil, fmt.Errorf("VobSub: the track's CodecPrivate lacks the size: or palette: line of a .idx header, without which its pictures have no colours") + } + return d, nil +} + +// spuState is what the control sequences of one subpicture say. +type spuState struct { + colour, alpha [4]uint8 // per pixel value 0..3 + x1, y1, x2, y2 int + offset [2]int + hasArea bool + hasOffsets bool + started bool + forced bool + stopped bool + locked bool // the picture is on screen and visible: later sequences no longer restyle it + startAt int + stopAt int +} + +func (s *spuState) visible() bool { return s.alpha != [4]uint8{} } + +// Decode decodes one Matroska block, a whole subpicture unit: one display set holding its picture, cut down to its +// visible pixels, with the Delay before it shows and the Duration its stop command gives it (zero when it has +// none), or an empty set for a unit that shows nothing. A unit spread over several blocks gives nothing until the +// block that completes it, and its time is the first block's: Continues tells, ahead of Decode, whether a block +// starts a unit. A block that is a whole unit of its own never completes the one before, which is dropped. +// +// A block of a track stored compressed (mkv.Track.Compression) is decoded first: mkv.Track.DecodePayload. +func (d *VobSubDecoder) Decode(block []byte) ([]BitmapDisplaySet, error) { + if d.Continues(block) { + block = append(d.pending, block...) + } + d.pending = nil // a block that is a unit of its own does not complete the one before, which is dropped + if len(block) < 4 { + return nil, fmt.Errorf("VobSub: a subpicture needs 4 bytes of header, the block holds %d", len(block)) + } + size := int(binary.BigEndian.Uint16(block[0:2])) + ctrl := int(binary.BigEndian.Uint16(block[2:4])) + if ctrl < 4 || ctrl+4 > size { + return nil, fmt.Errorf("VobSub: the subpicture places its control sequences at byte %d of %d", ctrl, size) + } + if size > len(block) { + d.pending = append([]byte(nil), block...) + return nil, nil + } + spu := block[:size] + st := spuState{colour: [4]uint8{0, 1, 2, 3}, alpha: [4]uint8{0, 15, 15, 15}} + if err := st.read(spu, ctrl); err != nil { + return nil, err + } + + ds := BitmapDisplaySet{ScreenW: d.screenW, ScreenH: d.screenH} + if !st.started { + if !st.stopped { + return nil, nil + } + ds.Delay = spuDate(st.stopAt) + return []BitmapDisplaySet{ds}, nil + } + if st.stopped && st.stopAt == st.startAt { // shown and removed at once: nothing is seen + ds.Delay = spuDate(st.startAt) + return []BitmapDisplaySet{ds}, nil + } + if !st.hasArea || !st.hasOffsets { + return nil, fmt.Errorf("VobSub: the subpicture starts a display without naming its area and its data") + } + img, err := d.picture(spu[:ctrl], &st) + if err != nil { + return nil, err + } + ds.Delay = spuDate(st.startAt) + if st.stopped && st.stopAt > st.startAt { + ds.Duration = spuDate(st.stopAt - st.startAt) + } + if img, at := trimTransparent(img); img != nil { + ds.Objects = []BitmapObject{{X: st.x1 + at.X, Y: st.y1 + at.Y, Forced: st.forced, Image: img}} + } + return []BitmapDisplaySet{ds}, nil +} + +// wholeSPU reports a block that holds exactly the unit its header declares, control sequences inside it. +func wholeSPU(block []byte) bool { + if len(block) < 8 { + return false + } + size, ctrl := int(binary.BigEndian.Uint16(block[0:2])), int(binary.BigEndian.Uint16(block[2:4])) + return size == len(block) && ctrl >= 4 && ctrl+4 <= size +} + +// spuDate converts a control sequence's date, in units of 1024 ticks of the 90 kHz clock. +func spuDate(units int) time.Duration { + return time.Duration(units) * 1024 * time.Second / 90000 +} + +// read walks the chain of control sequences from ctrl, each naming the next; the last names itself. +func (s *spuState) read(spu []byte, ctrl int) error { + for pos := ctrl; ; { + if pos+4 > len(spu) { + return fmt.Errorf("VobSub: a control sequence at byte %d runs past the subpicture's %d bytes", pos, len(spu)) + } + date := int(binary.BigEndian.Uint16(spu[pos:])) + next := int(binary.BigEndian.Uint16(spu[pos+2:])) + if err := s.commands(spu, pos+4, date); err != nil { + return err + } + if s.started && s.visible() { + s.locked = true + } + if next == pos { + return nil + } + if next < pos+4 { + return fmt.Errorf("VobSub: the control sequence at byte %d names byte %d as the next one, which is not after it", pos, next) + } + pos = next + } +} + +// commands applies one control sequence. An unknown command ends it: its length is not known. +func (s *spuState) commands(spu []byte, p, date int) error { + need := func(n int) error { + if p+n > len(spu) { + return fmt.Errorf("VobSub: command %#02x at byte %d needs %d more byte(s), the subpicture ends first", spu[p-1], p-1, n) + } + return nil + } + for p < len(spu) { + cmd := spu[p] + p++ + switch cmd { + case spuForcedStart, spuStart: + if !s.started { + s.started, s.forced, s.startAt = true, cmd == spuForcedStart, date + } + case spuStop: + if !s.stopped { + s.stopped, s.stopAt = true, date + } + case spuColours, spuContrast: + if err := need(2); err != nil { + return err + } + v := [4]uint8{spu[p+1] & 0xf, spu[p+1] >> 4, spu[p] & 0xf, spu[p] >> 4} + if !s.locked && cmd == spuColours { + s.colour = v + } else if !s.locked { + s.alpha = v + } + p += 2 + case spuArea: + if err := need(6); err != nil { + return err + } + if !s.locked { + s.x1 = int(spu[p])<<4 | int(spu[p+1])>>4 + s.x2 = int(spu[p+1]&0xf)<<8 | int(spu[p+2]) + s.y1 = int(spu[p+3])<<4 | int(spu[p+4])>>4 + s.y2 = int(spu[p+4]&0xf)<<8 | int(spu[p+5]) + s.hasArea = true + } + p += 6 + case spuOffsets: + if err := need(4); err != nil { + return err + } + if !s.locked { + s.offset = [2]int{int(binary.BigEndian.Uint16(spu[p:])), int(binary.BigEndian.Uint16(spu[p+2:]))} + s.hasOffsets = true + } + p += 4 + case spuChange: + if err := need(2); err != nil { + return err + } + n := int(binary.BigEndian.Uint16(spu[p:])) + if n < 2 || p+n > len(spu) { + return fmt.Errorf("VobSub: a colour change command at byte %d declares %d bytes, %d remain", p-1, n, len(spu)-p) + } + p += n + default: // spuEnd, or a command this decoder does not know + return nil + } + } + return nil +} + +// picture expands the two fields into one image: the even lines from the first, the odd lines from the second. +func (d *VobSubDecoder) picture(data []byte, s *spuState) (*image.NRGBA, error) { + w, h := s.x2-s.x1+1, s.y2-s.y1+1 + if w <= 0 || h <= 0 { + return nil, fmt.Errorf("VobSub: the subpicture's area runs from %d,%d to %d,%d, which holds no pixel", s.x1, s.y1, s.x2, s.y2) + } + for _, off := range s.offset { + if off < 4 || off >= len(data) { + return nil, fmt.Errorf("VobSub: the subpicture places a field's data at byte %d, outside the %d bytes of picture data", off, len(data)) + } + } + var rgba [4][4]uint8 + for v := range rgba { + c := d.palette[s.colour[v]] + rgba[v] = [4]uint8{c[0], c[1], c[2], s.alpha[v] * 17} + } + img := image.NewNRGBA(image.Rect(0, 0, w, h)) + nib := [2]int{s.offset[0] * 2, s.offset[1] * 2} // each field's read position, in nibbles + for y := 0; y < h; y++ { + pos := nib[y&1] + for x := 0; x < w; { + code := 0 + for n := 0; n < 4; n++ { + if pos>>1 >= len(data) { + return nil, fmt.Errorf("VobSub: the picture's data ends on line %d of %d", y, h) + } + code = code<<4 | int(data[pos>>1]>>(4*(1-pos&1)))&0xf + pos++ + if code >= 4<<(2*n) { + break + } + } + run := code >> 2 + if run == 0 || run > w-x { // a zero run fills the rest of the line + run = w - x + } + if c := rgba[code&3]; c[3] != 0 { + off := img.PixOffset(x, y) + for i := 0; i < run; i++ { + copy(img.Pix[off:off+4], c[:]) + off += 4 + } + } + x += run + } + nib[y&1] = (pos + 1) &^ 1 // a line ends on a byte boundary + } + return img, nil +} diff --git a/mkv/subtitle/vobsub_test.go b/mkv/subtitle/vobsub_test.go new file mode 100644 index 00000000..d2a210fe --- /dev/null +++ b/mkv/subtitle/vobsub_test.go @@ -0,0 +1,356 @@ +package subtitle + +import ( + "strings" + "testing" + "time" +) + +const vobsubIdx = "# VobSub index file, v7\nsize: 720x576\npalette: 000000, ff0000, 00ff00, 0000ff, ffffff, 101010, 202020, 303030, 404040, 505050, 606060, 707070, 808080, 909090, a0a0a0, b0b0b0\nforced subs: OFF\n" + +type spuSeq struct { + date int + cmds []byte +} + +// vobsubSPU assembles a subpicture unit: the two fields' data at bytes 4 and 4+len(top), then the chained sequences. +func vobsubSPU(top, bottom []byte, seqs ...spuSeq) []byte { + ctrl := 4 + len(top) + len(bottom) + out := []byte{0, 0, byte(ctrl >> 8), byte(ctrl)} + out = append(append(out, top...), bottom...) + for i, s := range seqs { + next := len(out) // the last sequence names itself + if i < len(seqs)-1 { + next += 4 + len(s.cmds) + 1 + } + out = append(out, byte(s.date>>8), byte(s.date), byte(next>>8), byte(next)) + out = append(append(out, s.cmds...), spuEnd) + } + out[0], out[1] = byte(len(out)>>8), byte(len(out)) + return out +} + +func spuArea4x2(x, y int) []byte { + x2, y2 := x+3, y+1 + return []byte{spuArea, byte(x >> 4), byte(x<<4) | byte(x2>>8), byte(x2), byte(y >> 4), byte(y<<4) | byte(y2>>8), byte(y2)} +} + +// Top line: two pixels of value 1, one of 2, one of 3. Bottom line: value 2 to the end of the line. +var ( + spuTop4x2 = []byte{0x96, 0x70} + spuBottom4x2 = []byte{0x00, 0x02} +) + +// spuShow4x2 is the usual first sequence: values 0..3 take palette entries 0, 1, 2, 4, value 0 transparent. +func spuShow4x2(start byte) []byte { + cmds := []byte{start, spuColours, 0x42, 0x10, spuContrast, 0xff, 0xf0} + cmds = append(cmds, spuArea4x2(100, 400)...) + return append(cmds, spuOffsets, 0, 4, 0, 6) +} + +func newVobSub(t *testing.T) *VobSubDecoder { + t.Helper() + d, err := NewVobSubDecoder([]byte(vobsubIdx)) + if err != nil { + t.Fatal(err) + } + return d +} + +func decodeOneVobSub(t *testing.T, block []byte) BitmapDisplaySet { + t.Helper() + sets, err := newVobSub(t).Decode(block) + if err != nil { + t.Fatal(err) + } + if len(sets) != 1 { + t.Fatalf("got %d display sets, want 1", len(sets)) + } + return sets[0] +} + +func TestVobSubDecoder_Picture(t *testing.T) { + block := vobsubSPU(spuTop4x2, spuBottom4x2, + spuSeq{date: 0, cmds: spuShow4x2(spuStart)}, + spuSeq{date: 300, cmds: []byte{spuStop}}) + ds := decodeOneVobSub(t, block) + if ds.ScreenW != 720 || ds.ScreenH != 576 { + t.Errorf("plane %dx%d, want 720x576", ds.ScreenW, ds.ScreenH) + } + if ds.Delay != 0 || ds.Duration != 300*1024*time.Second/90000 { + t.Errorf("delay %v duration %v, want 0 and %v", ds.Delay, ds.Duration, 300*1024*time.Second/90000) + } + if len(ds.Objects) != 1 { + t.Fatalf("got %d objects, want 1", len(ds.Objects)) + } + o := ds.Objects[0] + if o.X != 100 || o.Y != 400 || o.Forced { + t.Errorf("object at %d,%d forced=%v, want 100,400 not forced", o.X, o.Y, o.Forced) + } + if b := o.Image.Bounds(); b.Dx() != 4 || b.Dy() != 2 { + t.Fatalf("image %dx%d, want 4x2", b.Dx(), b.Dy()) + } + red, green, white := [4]uint8{255, 0, 0, 255}, [4]uint8{0, 255, 0, 255}, [4]uint8{255, 255, 255, 255} + want := [2][4][4]uint8{{red, red, green, white}, {green, green, green, green}} + for y := range want { + for x := range want[y] { + if got := nrgbaAt(t, o.Image, x, y); got != want[y][x] { + t.Errorf("pixel %d,%d = %v, want %v", x, y, got, want[y][x]) + } + } + } +} + +func TestVobSubDecoder_TransparentAndPartialAlpha(t *testing.T) { + cmds := []byte{spuStart, spuColours, 0x42, 0x13, spuContrast, 0x08, 0xf0} + cmds = append(cmds, spuArea4x2(0, 0)...) + cmds = append(cmds, spuOffsets, 0, 4, 0, 6) + // Top line: value 0 twice, value 1, value 2. + ds := decodeOneVobSub(t, vobsubSPU([]byte{0x85, 0x60}, spuBottom4x2, spuSeq{cmds: cmds})) + img := ds.Objects[0].Image + if got := nrgbaAt(t, img, 0, 0); got != [4]uint8{} { + t.Errorf("a pixel of contrast 0 = %v, want untouched", got) + } + if got := nrgbaAt(t, img, 2, 0); got != [4]uint8{255, 0, 0, 255} { + t.Errorf("pixel value 1 = %v, want opaque red", got) + } + if got := nrgbaAt(t, img, 3, 0); got != [4]uint8{0, 255, 0, 8 * 17} { + t.Errorf("pixel value 2 = %v, want green at alpha %d", got, 8*17) + } +} + +func TestVobSubDecoder_ForcedStartAndDelay(t *testing.T) { + ds := decodeOneVobSub(t, vobsubSPU(spuTop4x2, spuBottom4x2, + spuSeq{date: 90, cmds: spuShow4x2(spuForcedStart)}, + spuSeq{date: 180, cmds: []byte{spuStop}})) + if !ds.Objects[0].Forced { + t.Error("a forced start display gave a picture that is not Forced") + } + if want := 90 * 1024 * time.Second / 90000; ds.Delay != want || ds.Duration != want { + t.Errorf("delay %v duration %v, want %v each", ds.Delay, ds.Duration, want) + } +} + +func TestVobSubDecoder_NoStopLeavesTheDurationOpen(t *testing.T) { + ds := decodeOneVobSub(t, vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)})) + if ds.Duration != 0 || ds.Clear() { + t.Errorf("duration %v clear=%v, want an open-ended picture", ds.Duration, ds.Clear()) + } +} + +func TestVobSubDecoder_StopOnlyClears(t *testing.T) { + ds := decodeOneVobSub(t, vobsubSPU(nil, nil, spuSeq{date: 45, cmds: []byte{spuStop}})) + if !ds.Clear() || ds.Delay != 45*1024*time.Second/90000 { + t.Errorf("clear=%v delay=%v, want an empty set delayed by the stop date", ds.Clear(), ds.Delay) + } + if ds.ScreenW != 720 || ds.ScreenH != 576 { + t.Errorf("plane %dx%d, want 720x576", ds.ScreenW, ds.ScreenH) + } +} + +func TestVobSubDecoder_NothingToShowOrRemove(t *testing.T) { + sets, err := newVobSub(t).Decode(vobsubSPU(nil, nil, spuSeq{cmds: []byte{spuColours, 0x42, 0x10}})) + if err != nil || len(sets) != 0 { + t.Errorf("got %d sets, err %v; want none", len(sets), err) + } +} + +// A picture that starts fully transparent takes the contrast a later sequence gives it; one that is visible keeps its own. +func TestVobSubDecoder_FadeIn(t *testing.T) { + first := spuShow4x2(spuStart) + first[5], first[6] = 0, 0 + ds := decodeOneVobSub(t, vobsubSPU(spuTop4x2, spuBottom4x2, + spuSeq{cmds: first}, + spuSeq{date: 10, cmds: []byte{spuContrast, 0xff, 0xf0}}, + spuSeq{date: 20, cmds: []byte{spuContrast, 0x11, 0x10}})) + if got := nrgbaAt(t, ds.Objects[0].Image, 0, 0); got != [4]uint8{255, 0, 0, 255} { + t.Errorf("pixel = %v, want the first visible contrast, opaque red", got) + } +} + +func TestVobSubDecoder_RunLengthForms(t *testing.T) { + area := func(w int) []byte { + x2 := w - 1 + return []byte{spuArea, 0, byte(x2 >> 8), byte(x2), 0, 0, 0} + } + for _, tc := range []struct { + name string + w int + data []byte + runs []int // lengths of the alternating runs of values 1 and 2 + }{ + {"one nibble", 4, []byte{0xd6}, []int{3, 1}}, + {"two nibbles", 16, []byte{0x3d, 0x60}, []int{15, 1}}, + {"three nibbles", 64, []byte{0x0f, 0xd6}, []int{63, 1}}, + {"four nibbles", 256, []byte{0x03, 0xfd, 0x60}, []int{255, 1}}, + {"to the end of the line", 300, []byte{0x96, 0x00, 0x01}, []int{2, 1, 297}}, + {"a run past the line stops at its end", 3, []byte{0x3d}, []int{3}}, + } { + t.Run(tc.name, func(t *testing.T) { + cmds := append([]byte{spuStart, spuColours, 0x42, 0x10}, area(tc.w)...) + cmds = append(cmds, spuOffsets, 0, 4, 0, 4) + ds := decodeOneVobSub(t, vobsubSPU(tc.data, nil, spuSeq{cmds: cmds})) + img, x := ds.Objects[0].Image, 0 + for i, n := range tc.runs { + want := [4]uint8{255, 0, 0, 255} + if tc.name != "to the end of the line" && i%2 == 1 || tc.name == "to the end of the line" && i == 1 { + want = [4]uint8{0, 255, 0, 255} + } + for ; n > 0; n, x = n-1, x+1 { + if got := nrgbaAt(t, img, x, 0); got != want { + t.Fatalf("pixel %d = %v, want %v", x, got, want) + } + } + } + if x != tc.w { + t.Fatalf("the runs cover %d pixels of %d", x, tc.w) + } + }) + } +} + +func TestNewVobSubDecoder_Refusals(t *testing.T) { + for name, tc := range map[string]struct{ idx, want string }{ + "empty": {"", "lacks the size: or palette: line"}, + "no palette": {"size: 720x576\n", "lacks the size: or palette: line"}, + "no size": {"palette: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f\n", "lacks the size: or palette: line"}, + "short palette": {"size: 720x576\npalette: 000000, ffffff\n", "holds 2 colours"}, + "bad colour": {"size: 720x576\npalette: zz,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f\n", "not an RRGGBB colour"}, + "bad size": {"size: 720\npalette: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f\n", "not a width x height"}, + "oversized plane": {"size: 70000x576\npalette: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f\n", "not a width x height"}, + } { + if _, err := NewVobSubDecoder([]byte(tc.idx)); err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: err = %v, want one naming %q", name, err, tc.want) + } + } +} + +func TestNewVobSubDecoder_ToleratesTheHeaderAsMuxersWriteIt(t *testing.T) { + d, err := NewVobSubDecoder([]byte("\r\n# comment: x\r\nSize: 720 x 480\r\nPalette: 0d00ee,ee450d,101010,ebebeb,0ce60b,ec14ed,ebff0b,0d637e,a1a1a1,c5c5c5,0e640c,89db89,0e0089,a2bdd4,ebcf0b,7e127e\r\n\x00\x00")) + if err != nil { + t.Fatal(err) + } + if d.screenW != 720 || d.screenH != 480 || d.palette[1] != [3]uint8{0xee, 0x45, 0x0d} { + t.Errorf("read %dx%d, palette[1] %v", d.screenW, d.screenH, d.palette[1]) + } +} + +func TestVobSubDecoder_Malformed(t *testing.T) { + good := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + mut := func(f func(b []byte) []byte) []byte { return f(append([]byte(nil), good...)) } + show := func(cmds ...byte) []byte { return vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: cmds}) } + for name, tc := range map[string]struct { + block []byte + want string + }{ + "short header": {[]byte{0, 4, 0}, "needs 4 bytes of header"}, + "control before data": {mut(func(b []byte) []byte { b[2], b[3] = 0, 2; return b }), "places its control sequences"}, + "control at the end": {mut(func(b []byte) []byte { b[2], b[3] = b[0], b[1]; return b }), "places its control sequences"}, + "backward chain": {mut(func(b []byte) []byte { b[10], b[11] = 0, 4; return b }), "is not after it"}, + "truncated command": {show(spuStart, spuArea, 0, 0), "needs 6 more byte(s)"}, + "no area": {show(spuStart, spuOffsets, 0, 4, 0, 6), "without naming its area"}, + "no offsets": {show(append([]byte{spuStart}, spuArea4x2(0, 0)...)...), "without naming its area"}, + "inverted area": {show(spuStart, spuArea, 0x00, 0x40, 0x00, 0, 0, 1, spuOffsets, 0, 4, 0, 6), "holds no pixel"}, + "field outside": {show(append(append([]byte{spuStart}, spuArea4x2(0, 0)...), spuOffsets, 0, 4, 0, 200)...), "outside the"}, + "bad change length": {show(spuChange, 0, 1), "colour change command"}, + "data ends early": {vobsubSPU([]byte{0x96, 0x70}, nil, spuSeq{cmds: append(append([]byte{spuStart}, spuArea4x2(0, 0)...), spuOffsets, 0, 4, 0, 5)}), + "data ends on line"}, + } { + sets, err := newVobSub(t).Decode(tc.block) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: %d sets, err = %v, want one naming %q", name, len(sets), err, tc.want) + } + } +} + +func TestVobSubDecoder_SkipsWhatItDoesNotApply(t *testing.T) { + cmds := append([]byte{spuChange, 0, 4, 0xaa, 0xbb}, spuShow4x2(spuStart)...) + ds := decodeOneVobSub(t, append(vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: cmds}), 0, 0, 0)) + if len(ds.Objects) != 1 { + t.Fatalf("a colour change command or bytes after the unit lost the picture") + } +} + +// A unit a muxer spread over two blocks comes out with the second; Reset forgets the first. +func TestVobSubDecoder_UnitOverTwoBlocks(t *testing.T) { + block := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + d := newVobSub(t) + if sets, err := d.Decode(block[:7]); err != nil || len(sets) != 0 { + t.Fatalf("the first part gave %d sets, err %v; want nothing yet", len(sets), err) + } + sets, err := d.Decode(block[7:]) + if err != nil || len(sets) != 1 || len(sets[0].Objects) != 1 { + t.Fatalf("the second part gave %d sets, err %v; want the picture", len(sets), err) + } + if _, err := d.Decode(block[:7]); err != nil { + t.Fatal(err) + } + d.Reset() + if sets, err := d.Decode(block); err != nil || len(sets) != 1 { + t.Fatalf("after Reset a whole unit gave %d sets, err %v; want its picture", len(sets), err) + } +} + +// A block that declares more than it holds is not completed by a block that is a whole unit of its own. +func TestVobSubDecoder_WholeUnitAfterAnIncompleteOne(t *testing.T) { + unit := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}, spuSeq{date: 300, cmds: []byte{spuStop}}) + damaged := append([]byte(nil), unit...) + damaged[0] = 0x40 // declares 16 KiB and more + d := newVobSub(t) + if sets, err := d.Decode(damaged); err != nil || len(sets) != 0 { + t.Fatalf("the damaged block gave %d sets, err %v; want nothing yet", len(sets), err) + } + for i := 0; i < 3; i++ { + sets, err := d.Decode(unit) + if err != nil || len(sets) != 1 || len(sets[0].Objects) != 1 { + t.Fatalf("whole unit %d after the damaged block: %d sets, err %v; want its picture", i, len(sets), err) + } + } + if d.Continues(unit[7:]) { + t.Error("the damaged block's bytes are still held") + } +} + +// A unit takes the time of the block that starts it: the first of the blocks it is spread over, and the block +// itself when it drops an incomplete unit ahead of it. +func TestVobSubDecoder_ContinuesDatesTheUnit(t *testing.T) { + unit := vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{cmds: spuShow4x2(spuStart)}) + damaged := append([]byte(nil), unit...) + damaged[0] = 0x40 // declares 16 KiB and more + d := newVobSub(t) + if d.Continues(unit) || d.Continues(unit[:7]) { + t.Fatal("a new decoder takes a block as the rest of a unit") + } + var unitTime int64 + for i, b := range []struct { + timeMs int64 + data []byte + sets int + wantTime int64 + }{ + {1000, damaged, 0, 0}, + {132499, unit, 1, 132499}, // a whole unit: the damaged one is dropped, and its time with it + {200000, unit[:7], 0, 0}, + {200017, unit[7:], 1, 200000}, // the rest of a unit: the time of its first block + {300000, unit, 1, 300000}, + } { + if !d.Continues(b.data) { + unitTime = b.timeMs + } + sets, err := d.Decode(b.data) + if err != nil || len(sets) != b.sets { + t.Fatalf("block %d at %d ms: %d sets, err %v; want %d", i, b.timeMs, len(sets), err, b.sets) + } + if len(sets) == 1 && unitTime != b.wantTime { + t.Errorf("block %d: its unit is dated %d ms, want %d", i, unitTime, b.wantTime) + } + } +} + +// A stop at the date of the start shows nothing: an empty set, not a picture without an end. +func TestVobSubDecoder_StopAtTheStartShowsNothing(t *testing.T) { + ds := decodeOneVobSub(t, vobsubSPU(spuTop4x2, spuBottom4x2, spuSeq{date: 20, cmds: append(spuShow4x2(spuStart), spuStop)})) + if !ds.Clear() || ds.Delay != 20*1024*time.Second/90000 || ds.Duration != 0 { + t.Errorf("clear=%v delay=%v duration=%v; want an empty set at the unit's date", ds.Clear(), ds.Delay, ds.Duration) + } +} diff --git a/mkv/subtitle/vobsubfile.go b/mkv/subtitle/vobsubfile.go new file mode 100644 index 00000000..f3702f44 --- /dev/null +++ b/mkv/subtitle/vobsubfile.go @@ -0,0 +1,238 @@ +package subtitle + +// vobsubfile.go - VobSub as the two files that sit beside a video: the .idx, a text file holding the header a +// VobSubDecoder takes and, per language, the time and the position of each subpicture; and the .sub, an MPEG +// program stream whose private-stream packets carry the subpicture units, a unit over as many packets as it needs. + +import ( + "bufio" + "encoding/binary" + "errors" + "fmt" + "io" + "strconv" + "strings" +) + +const ( + // vobsubMaxIndexLine bounds one line of a .idx; the palette line, the longest, is under 200 bytes. + vobsubMaxIndexLine = 64 << 10 + // vobsubMaxUnitScan bounds the bytes of the .sub read for one unit: its 64 KiB at most, in 2 KiB packs + // interleaved with those of the other languages. + vobsubMaxUnitScan = 4 << 20 +) + +// VobSubIndex is a parsed .idx file. +type VobSubIndex struct { + // Header is the part of the file ahead of the first language: what NewVobSubDecoder takes. + Header []byte + Streams []VobSubStream +} + +// VobSubStream is one language of a .idx/.sub pair. +type VobSubStream struct { + Language string // the id: of the stream, as written ("en", "fr") + Name string // its alt: line, when it has one + // Index is the stream's number in the .sub, the low bits of its packets' substream ID. + Index int + Entries []VobSubEntry +} + +// VobSubEntry places one subpicture: when it is shown and where its first packet starts in the .sub. +type VobSubEntry struct { + TimeMs int64 + Pos int64 +} + +// ParseVobSubIndex reads a .idx file. An entry's time has the stream's delay: lines and the header's +// time offset: applied. +func ParseVobSubIndex(r io.Reader) (*VobSubIndex, error) { + ix := &VobSubIndex{} + sc := bufio.NewScanner(r) + sc.Buffer(make([]byte, 0, 4096), vobsubMaxIndexLine) + var offsetMs, delayMs int64 + var cur *VobSubStream + for n := 1; sc.Scan(); n++ { + line := strings.TrimSpace(sc.Text()) + key, val, ok := strings.Cut(line, ":") + key, val = strings.ToLower(strings.TrimSpace(key)), strings.TrimSpace(val) + if !ok || strings.HasPrefix(line, "#") { + key = "" + } + switch key { + case "id": + lang, rest, _ := strings.Cut(val, ",") + _, num, _ := strings.Cut(rest, ":") + index, err := strconv.Atoi(strings.TrimSpace(num)) + if err != nil || index < 0 || index > 31 { + return nil, fmt.Errorf("VobSub index, line %d: %q does not name a stream as \"id: xx, index: 0..31\"", n, line) + } + ix.Streams = append(ix.Streams, VobSubStream{Language: strings.TrimSpace(lang), Index: index}) + cur, delayMs = &ix.Streams[len(ix.Streams)-1], 0 + continue + case "time offset": + if cur == nil { + offsetMs, _ = strconv.ParseInt(val, 10, 64) + } + case "alt": + if cur != nil { + cur.Name = val + continue + } + case "delay": + if cur != nil { + d, ok := parseVobSubTime(val) + if !ok { + return nil, fmt.Errorf("VobSub index, line %d: delay %q is not HH:MM:SS:mmm", n, val) + } + delayMs += d + continue + } + case "timestamp": + if cur == nil { + return nil, fmt.Errorf("VobSub index, line %d: a timestamp ahead of any \"id:\" line", n) + } + ts, rest, _ := strings.Cut(val, ",") + _, hex, _ := strings.Cut(rest, ":") + t, okT := parseVobSubTime(strings.TrimSpace(ts)) + pos, err := strconv.ParseInt(strings.TrimSpace(hex), 16, 64) + if !okT || err != nil || pos < 0 { + return nil, fmt.Errorf("VobSub index, line %d: %q is not \"timestamp: HH:MM:SS:mmm, filepos: hex\"", n, line) + } + cur.Entries = append(cur.Entries, VobSubEntry{TimeMs: t + delayMs + offsetMs, Pos: pos}) + continue + } + if cur == nil { + ix.Header = append(append(ix.Header, line...), '\n') + } + } + if err := sc.Err(); err != nil { + return nil, fmt.Errorf("VobSub index: %w", err) + } + if len(ix.Streams) == 0 { + return nil, fmt.Errorf("VobSub index: the file names no stream (no \"id:\" line): it is not a .idx") + } + return ix, nil +} + +// parseVobSubTime reads [-]HH:MM:SS:mmm as milliseconds. +func parseVobSubTime(s string) (int64, bool) { + neg := strings.HasPrefix(s, "-") + parts := strings.Split(strings.TrimPrefix(s, "-"), ":") + if len(parts) != 4 { + return 0, false + } + var ms int64 + for i, scale := range [4]int64{3600000, 60000, 1000, 1} { + v, err := strconv.ParseInt(strings.TrimSpace(parts[i]), 10, 32) + if err != nil || v < 0 { + return 0, false + } + ms += v * scale + } + if neg { + ms = -ms + } + return ms, true +} + +// ForEachUnit reads from the .sub every subpicture unit of one stream of the index, in the index's order, and +// calls fn with its time and its bytes: what VobSubDecoder.Decode takes as a block. An entry placed at or past the +// end of the .sub is passed over; a unit the .sub holds in part is an error. An error from fn stops the read and is +// returned as is. +func (ix *VobSubIndex) ForEachUnit(sub io.ReaderAt, stream int, fn func(timeMs int64, unit []byte) error) error { + if stream < 0 || stream >= len(ix.Streams) { + return fmt.Errorf("VobSub: stream %d of an index of %d", stream, len(ix.Streams)) + } + s := &ix.Streams[stream] + end := int64(-1) + for _, e := range s.Entries { + if e.Pos < end { // an index entry for a later packet of the unit before + continue + } + unit, next, err := readVobSubUnit(sub, e.Pos, byte(0x20+s.Index)) + if err == errVobSubPastEnd { + continue + } + if err != nil { + return fmt.Errorf("VobSub: the unit at %d ms (byte %d of the .sub): %w", e.TimeMs, e.Pos, err) + } + end = next + if err := fn(e.TimeMs, unit); err != nil { + return err + } + } + return nil +} + +// errVobSubPastEnd is an index entry placed at or past the end of the .sub, as some tools write a last one. +var errVobSubPastEnd = errors.New("the entry is past the end of the .sub") + +// readVobSubUnit gathers the unit that starts in the pack at pos from the packets of one substream, and returns it +// with the position after its last packet. +func readVobSubUnit(sub io.ReaderAt, pos int64, substream byte) ([]byte, int64, error) { + var unit []byte + want := -1 + var head [14]byte + for start := pos; pos-start < vobsubMaxUnitScan; { + if n, err := readFullAt(sub, head[:6], pos); err != nil { + if n == 0 && pos == start && errors.Is(err, io.EOF) { + return nil, 0, errVobSubPastEnd + } + if errors.Is(err, io.EOF) && want >= 0 { + return nil, 0, fmt.Errorf("the file ends %d bytes into a unit of %d", len(unit), want) + } + return nil, 0, fmt.Errorf("no packet at byte %d: %w", pos, err) + } + if head[0] != 0 || head[1] != 0 || head[2] != 1 { + return nil, 0, fmt.Errorf("byte %d does not start an MPEG packet", pos) + } + switch head[3] { + case 0xba: // pack header: 14 bytes and the stuffing its last byte counts + if _, err := readFullAt(sub, head[:], pos); err != nil { + return nil, 0, fmt.Errorf("a pack header cut short at byte %d: %w", pos, err) + } + if head[4]>>6 != 1 { + return nil, 0, fmt.Errorf("the pack at byte %d is not an MPEG-2 program stream pack", pos) + } + pos += 14 + int64(head[13]&7) + case 0xb9: // program end + return nil, 0, fmt.Errorf("the stream ends %d bytes into a unit of %d", len(unit), want) + case 0xbd: // private stream 1: the subpictures + size := int(binary.BigEndian.Uint16(head[4:6])) + pkt := make([]byte, size) + if _, err := readFullAt(sub, pkt, pos+6); err != nil { + return nil, 0, fmt.Errorf("a packet of %d bytes cut short at byte %d: %w", size, pos, err) + } + pos += 6 + int64(size) + if size < 4 || 3+int(pkt[2]) >= size { + return nil, 0, fmt.Errorf("a packet of %d bytes holds no payload", size) + } + if pkt[3+int(pkt[2])] != substream { + continue // another language + } + unit = append(unit, pkt[4+int(pkt[2]):]...) + if want < 0 && len(unit) >= 2 { + want = int(binary.BigEndian.Uint16(unit)) + if want < 4 { + return nil, 0, fmt.Errorf("the unit declares %d bytes", want) + } + } + if want >= 0 && len(unit) >= want { + return unit[:want], pos, nil + } + default: // system header, padding, any other stream + pos += 6 + int64(binary.BigEndian.Uint16(head[4:6])) + } + } + return nil, 0, fmt.Errorf("the unit is not complete within %d bytes of the .sub", vobsubMaxUnitScan) +} + +// readFullAt is ReadAt without the io.EOF a reader may return along with the last bytes of its file. +func readFullAt(r io.ReaderAt, p []byte, off int64) (int, error) { + n, err := r.ReadAt(p, off) + if n == len(p) { + return n, nil + } + return n, err +}