2020-06-08 03:29:51 +03:00
|
|
|
package mpeg
|
|
|
|
|
|
|
|
// http://www.mpucoder.com/DVD/spu.html
|
|
|
|
// http://sam.zoy.org/writings/dvd/subtitles/
|
|
|
|
// TODO: still some unknown data before and after pixel data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.MPEG_SPU,
|
|
|
|
Description: "Sub Picture Unit (DVD subtitle)",
|
|
|
|
DecodeFn: spuDecode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
//nolint:revive
|
2020-06-08 03:29:51 +03:00
|
|
|
const (
|
|
|
|
CMD_END = 0xff
|
|
|
|
FSTA_DSP = 0x00
|
|
|
|
STA_DSP = 0x01
|
|
|
|
STP_DSP = 0x02
|
|
|
|
SET_COLOR = 0x03
|
|
|
|
SET_CONTR = 0x04
|
|
|
|
SET_DAREA = 0x05
|
|
|
|
SET_DSPXA = 0x06
|
|
|
|
CHG_COLCON = 0x07
|
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var commandNames = scalar.UToSymStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
CMD_END: "CMD_END",
|
|
|
|
FSTA_DSP: "FSTA_DSP",
|
|
|
|
STA_DSP: "STA_DSP",
|
|
|
|
STP_DSP: "STP_DSP",
|
|
|
|
SET_COLOR: "SET_COLOR",
|
|
|
|
SET_CONTR: "SET_CONTR",
|
|
|
|
SET_DAREA: "SET_DAREA",
|
|
|
|
SET_DSPXA: "SET_DSPXA",
|
|
|
|
CHG_COLCON: "CHG_COLCON",
|
|
|
|
}
|
|
|
|
|
|
|
|
func rleValue(d *decode.D) (uint64, uint64, int) {
|
|
|
|
p := uint(d.PeekBits(8))
|
|
|
|
|
2021-11-19 18:44:06 +03:00
|
|
|
// match zero prefix
|
2020-06-08 03:29:51 +03:00
|
|
|
switch {
|
|
|
|
case p&0b1111_1100 == 0:
|
|
|
|
// 000000nnnnnnnncc
|
|
|
|
d.U6()
|
|
|
|
return d.U8(), d.U2(), 16
|
|
|
|
case p&0b1111_0000 == 0:
|
|
|
|
// 0000nnnnnncc
|
|
|
|
d.U4()
|
|
|
|
return d.U6(), d.U2(), 12
|
|
|
|
case p&0b1100_0000 == 0:
|
|
|
|
// 00nnnncc
|
|
|
|
d.U2()
|
|
|
|
return d.U4(), d.U2(), 8
|
|
|
|
default:
|
|
|
|
// nncc
|
|
|
|
return d.U2(), d.U2(), 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:41:26 +03:00
|
|
|
//nolint:unparam
|
2022-05-20 16:10:41 +03:00
|
|
|
func decodeLines(d *decode.D, lines int, width int) []string {
|
2020-06-08 03:29:51 +03:00
|
|
|
var ls []string
|
|
|
|
|
|
|
|
for i := 0; i < lines; i++ {
|
|
|
|
l := ""
|
|
|
|
for x := 0; x < width; {
|
|
|
|
n, c, b := rleValue(d)
|
|
|
|
pixel := " "
|
|
|
|
if c != 0 {
|
|
|
|
pixel = fmt.Sprintf("%d", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 && b == 16 {
|
|
|
|
l += strings.Repeat(pixel, width-len(l))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
x += int(n)
|
|
|
|
}
|
|
|
|
if d.ByteAlignBits() > 0 {
|
|
|
|
d.U(d.ByteAlignBits())
|
|
|
|
}
|
|
|
|
|
|
|
|
ls = append(ls, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:33:50 +03:00
|
|
|
func spuDecode(d *decode.D, _ any) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU16("size")
|
|
|
|
dcsqtOffset := d.FieldU16("dcsqt_offset")
|
|
|
|
|
2022-01-24 18:53:55 +03:00
|
|
|
// to catch infinite loops
|
|
|
|
offsetSeen := map[uint64]struct{}{}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
d.SeekAbs(int64(dcsqtOffset) * 8)
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("dcsqt", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
lastDCSQ := false
|
|
|
|
|
|
|
|
for !lastDCSQ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("dcsq", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
dcsqStart := uint64(d.Pos() / 8)
|
|
|
|
d.FieldU16("delay")
|
|
|
|
offset := d.FieldU16("offset")
|
|
|
|
if offset == dcsqStart {
|
|
|
|
lastDCSQ = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:53:55 +03:00
|
|
|
if _, ok := offsetSeen[offset]; ok {
|
|
|
|
d.Fatalf("dcsqt loop detected for %d", offset)
|
|
|
|
}
|
|
|
|
offsetSeen[offset] = struct{}{}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
var pxdTFOffset int64
|
|
|
|
var pxdBFOffset int64
|
|
|
|
var width uint64
|
|
|
|
var height uint64
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("commands", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
seenEnd := false
|
|
|
|
for !seenEnd {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("command", func(d *decode.D) {
|
2021-12-02 00:48:25 +03:00
|
|
|
cmd := d.FieldU8("type", commandNames)
|
2020-06-08 03:29:51 +03:00
|
|
|
switch cmd {
|
|
|
|
case CMD_END:
|
|
|
|
seenEnd = true
|
|
|
|
case FSTA_DSP:
|
|
|
|
// no args
|
|
|
|
case STA_DSP:
|
|
|
|
// no args
|
|
|
|
case STP_DSP:
|
|
|
|
// no args
|
|
|
|
case SET_COLOR:
|
|
|
|
d.FieldU4("a0")
|
|
|
|
d.FieldU4("a1")
|
|
|
|
d.FieldU4("a2")
|
|
|
|
d.FieldU4("a3")
|
|
|
|
case SET_CONTR:
|
|
|
|
d.FieldU4("a0")
|
|
|
|
d.FieldU4("a1")
|
|
|
|
d.FieldU4("a2")
|
|
|
|
d.FieldU4("a3")
|
|
|
|
case SET_DAREA:
|
|
|
|
startX := d.FieldU12("start_x")
|
|
|
|
endX := d.FieldU12("end_x")
|
|
|
|
startY := d.FieldU12("start_y")
|
|
|
|
endY := d.FieldU12("end_y")
|
|
|
|
width = endX - startX + 1
|
|
|
|
height = endY - startY + 1
|
|
|
|
case SET_DSPXA:
|
|
|
|
pxdTFOffset = int64(d.FieldU16("offset_top_field")) * 8
|
|
|
|
pxdBFOffset = int64(d.FieldU16("offset_bottom_field")) * 8
|
|
|
|
case CHG_COLCON:
|
|
|
|
size := d.FieldU16("size")
|
|
|
|
// TODO
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("data", int64(size)*8)
|
2022-02-12 18:11:41 +03:00
|
|
|
default:
|
|
|
|
d.Fatalf("unknown command %d", cmd)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
halfHeight := int(height) / 2
|
|
|
|
// var tLines []string
|
|
|
|
// var bLines []string
|
|
|
|
|
|
|
|
if pxdTFOffset != 0 {
|
|
|
|
d.SeekAbs(pxdTFOffset)
|
|
|
|
/*tLines*/ _ = decodeLines(d, halfHeight, int(width))
|
2021-11-05 17:04:26 +03:00
|
|
|
d.RangeFn(pxdTFOffset, d.Pos()-pxdTFOffset, func(d *decode.D) {
|
|
|
|
d.FieldRawLen("top_pixels", d.BitsLeft())
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
if pxdBFOffset != 0 {
|
|
|
|
d.SeekAbs(pxdBFOffset)
|
|
|
|
/*bLines*/ _ = decodeLines(d, halfHeight, int(width))
|
2021-11-05 17:04:26 +03:00
|
|
|
d.RangeFn(pxdBFOffset, d.Pos()-pxdBFOffset, func(d *decode.D) {
|
|
|
|
d.FieldRawLen("bottom_pixels", d.BitsLeft())
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// var lines []string
|
|
|
|
// for i := 0; i < halfHeight; i++ {
|
|
|
|
// lines = append(lines, tLines[i])
|
|
|
|
// lines = append(lines, bLines[i])
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for _, l := range lines {
|
|
|
|
// log.Printf("l: '%s'\n", l)
|
|
|
|
// }
|
|
|
|
|
|
|
|
d.SeekAbs(int64(offset) * 8)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|