2020-06-08 03:29:51 +03:00
|
|
|
package matroska
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/draft-ietf-cellar-ebml-00
|
|
|
|
// https://matroska.org/technical/specs/index.html
|
|
|
|
// https://www.matroska.org/technical/basics.html
|
|
|
|
// https://www.matroska.org/technical/codec_specs.html
|
|
|
|
// https://wiki.xiph.org/MatroskaOpus
|
|
|
|
|
|
|
|
// TODO: refactor simepleblock/block to just defer decode etc?
|
|
|
|
// TODO: CRC
|
|
|
|
// TODO: handle garbage (see tcl and example files)
|
|
|
|
// TODO: could use md5 here somehow, see flac.go
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/matroska/ebml"
|
|
|
|
"github.com/wader/fq/format/matroska/ebml_matroska"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/ranges"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-12-09 19:15:21 +03:00
|
|
|
//go:embed matroska.jq
|
2022-09-10 19:28:54 +03:00
|
|
|
//go:embed matroska.md
|
2020-06-08 03:29:51 +03:00
|
|
|
var matroskaFS embed.FS
|
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
var aacFrameGroup decode.Group
|
|
|
|
var av1CCRGroup decode.Group
|
|
|
|
var av1FrameGroup decode.Group
|
|
|
|
var flacFrameGroup decode.Group
|
|
|
|
var flacMetadatablocksGroup decode.Group
|
|
|
|
var imageGroup decode.Group
|
|
|
|
var mp3FrameGroup decode.Group
|
|
|
|
var mpegASCFrameGroup decode.Group
|
|
|
|
var mpegAVCAUGroup decode.Group
|
|
|
|
var mpegAVCDCRGroup decode.Group
|
|
|
|
var mpegHEVCDCRGroup decode.Group
|
|
|
|
var mpegHEVCSampleGroup decode.Group
|
|
|
|
var mpegPESPacketSampleGroup decode.Group
|
|
|
|
var mpegSPUFrameGroup decode.Group
|
|
|
|
var opusPacketFrameGroup decode.Group
|
|
|
|
var vorbisPacketGroup decode.Group
|
|
|
|
var vp8FrameGroup decode.Group
|
|
|
|
var vp9CFMGroup decode.Group
|
|
|
|
var vp9FrameGroup decode.Group
|
|
|
|
|
|
|
|
var codecToGroup map[string]*decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2023-03-29 01:36:55 +03:00
|
|
|
interp.RegisterFormat(
|
|
|
|
format.Matroska,
|
|
|
|
&decode.Format{
|
|
|
|
Description: "Matroska file",
|
|
|
|
Groups: []*decode.Group{format.Probe},
|
|
|
|
DecodeFn: matroskaDecode,
|
2023-05-01 14:19:04 +03:00
|
|
|
DefaultInArg: format.Matroska_In{
|
2023-03-29 01:36:55 +03:00
|
|
|
DecodeSamples: true,
|
|
|
|
},
|
|
|
|
Dependencies: []decode.Dependency{
|
2023-05-01 14:19:04 +03:00
|
|
|
{Groups: []*decode.Group{format.AAC_Frame}, Out: &aacFrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.AV1_CCR}, Out: &av1CCRGroup},
|
|
|
|
{Groups: []*decode.Group{format.AV1_Frame}, Out: &av1FrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.AVC_AU}, Out: &mpegAVCAUGroup},
|
|
|
|
{Groups: []*decode.Group{format.AVC_DCR}, Out: &mpegAVCDCRGroup},
|
|
|
|
{Groups: []*decode.Group{format.FLAC_Frame}, Out: &flacFrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.FLAC_Metadatablocks}, Out: &flacMetadatablocksGroup},
|
|
|
|
{Groups: []*decode.Group{format.HEVC_AU}, Out: &mpegHEVCSampleGroup},
|
|
|
|
{Groups: []*decode.Group{format.HEVC_DCR}, Out: &mpegHEVCDCRGroup},
|
2023-03-29 01:36:55 +03:00
|
|
|
{Groups: []*decode.Group{format.Image}, Out: &imageGroup},
|
2023-05-01 14:19:04 +03:00
|
|
|
{Groups: []*decode.Group{format.MP3_Frame}, Out: &mp3FrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.MPEG_ASC}, Out: &mpegASCFrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.MPEG_PES_Packet}, Out: &mpegPESPacketSampleGroup},
|
|
|
|
{Groups: []*decode.Group{format.MPEG_SPU}, Out: &mpegSPUFrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.Opus_Packet}, Out: &opusPacketFrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.Vorbis_Packet}, Out: &vorbisPacketGroup},
|
|
|
|
{Groups: []*decode.Group{format.VP8_Frame}, Out: &vp8FrameGroup},
|
|
|
|
{Groups: []*decode.Group{format.VP9_CFM}, Out: &vp9CFMGroup},
|
|
|
|
{Groups: []*decode.Group{format.VP9_Frame}, Out: &vp9FrameGroup},
|
2023-03-29 01:36:55 +03:00
|
|
|
},
|
|
|
|
})
|
2022-07-27 14:20:48 +03:00
|
|
|
interp.RegisterFS(matroskaFS)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
codecToGroup = map[string]*decode.Group{
|
|
|
|
"A_VORBIS": &vorbisPacketGroup,
|
|
|
|
"A_MPEG/L3": &mp3FrameGroup,
|
|
|
|
"A_FLAC": &flacFrameGroup,
|
|
|
|
"A_AAC": &aacFrameGroup,
|
|
|
|
"A_OPUS": &opusPacketFrameGroup,
|
|
|
|
"V_VP8": &vp8FrameGroup,
|
|
|
|
"V_VP9": &vp9FrameGroup,
|
|
|
|
"V_AV1": &av1FrameGroup,
|
|
|
|
"V_VOBSUB": &mpegSPUFrameGroup,
|
|
|
|
"V_MPEG4/ISO/AVC": &mpegAVCAUGroup,
|
|
|
|
"V_MPEGH/ISO/HEVC": &mpegHEVCSampleGroup,
|
|
|
|
"V_MPEG2": &mpegPESPacketSampleGroup,
|
|
|
|
"S_VOBSUB": &mpegSPUFrameGroup,
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:28:19 +03:00
|
|
|
const (
|
|
|
|
lacingTypeNone = 0b00
|
|
|
|
lacingTypeXiph = 0b01
|
|
|
|
lacingTypeFixed = 0b10
|
|
|
|
lacingTypeEBML = 0b11
|
|
|
|
)
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
var lacingTypeNames = scalar.UintMapSymStr{
|
2022-08-28 19:28:19 +03:00
|
|
|
lacingTypeNone: "none",
|
|
|
|
lacingTypeXiph: "xiph",
|
|
|
|
lacingTypeFixed: "fixed",
|
|
|
|
lacingTypeEBML: "ebml",
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:03:41 +03:00
|
|
|
const tagSizeUnknown = 0xffffffffffffff
|
|
|
|
|
2022-08-28 19:28:19 +03:00
|
|
|
func decodeLacingFn(d *decode.D, lacingType int, fn func(d *decode.D)) {
|
|
|
|
if lacingType == lacingTypeNone {
|
|
|
|
fn(d)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -1 size means rest of buffer, used last sometimes
|
|
|
|
var laceSizes []int64
|
|
|
|
|
|
|
|
switch lacingType {
|
|
|
|
case lacingTypeXiph:
|
|
|
|
numLaces := int(d.FieldU8("num_laces"))
|
|
|
|
d.FieldArray("lace_sizes", func(d *decode.D) {
|
|
|
|
for i := 0; i < numLaces; i++ {
|
2022-09-30 14:58:23 +03:00
|
|
|
s := int64(d.FieldUintFn("lace_size", decodeXiphLaceSize))
|
2022-08-28 19:28:19 +03:00
|
|
|
laceSizes = append(laceSizes, s)
|
|
|
|
}
|
|
|
|
laceSizes = append(laceSizes, -1)
|
|
|
|
})
|
|
|
|
case lacingTypeEBML:
|
|
|
|
numLaces := int(d.FieldU8("num_laces"))
|
|
|
|
d.FieldArray("lace_sizes", func(d *decode.D) {
|
2022-09-30 14:58:23 +03:00
|
|
|
s := int64(d.FieldUintFn("lace_size", decodeVint)) // first is unsigned, not ranged shifted
|
2022-08-28 19:28:19 +03:00
|
|
|
laceSizes = append(laceSizes, s)
|
|
|
|
for i := 0; i < numLaces-1; i++ {
|
2022-09-30 14:58:23 +03:00
|
|
|
d := int64(d.FieldUintFn("lace_size_delta", decodeRawVint))
|
2022-08-28 19:28:19 +03:00
|
|
|
// range shifting
|
|
|
|
switch {
|
|
|
|
case d&0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1000_0000 == 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1000_0000:
|
|
|
|
// value -(2^6^-1) to 2^6^-1 (ie 0_to 2^7^-2 minus 2^6^-1, half of the range)
|
|
|
|
d -= 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1011_1111
|
|
|
|
case d&0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1100_0000_0000_0000 == 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0100_0000_0000_0000:
|
|
|
|
// value -(2^13^-1) to 2^13^-1
|
|
|
|
d -= 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0101_1111_1111_1111
|
|
|
|
case d&0b1111_1111_1111_1111_1111_1111_1111_1111_1110_0000_0000_0000_0000_0000 == 0b0000_0000_0000_0000_0000_0000_0000_0000_0010_0000_0000_0000_0000_0000:
|
|
|
|
// value -(2^20^-1) to 2^20^-1
|
|
|
|
d -= 0b0000_0000_0000_0000_0000_0000_0000_0000_0010_1111_1111_1111_1111_1111
|
|
|
|
case d&0b1111_1111_1111_1111_1111_1111_1111_0000_0000_0000_0000_0000_0000_0000 == 0b0000_0000_0000_0000_0000_0000_0001_0000_0000_0000_0000_0000_0000_0000:
|
|
|
|
// value -(2^27^-1) to 2^27^-1
|
|
|
|
d -= 0b0000_0000_0000_0000_0000_0000_0001_0111_1111_1111_1111_1111_1111_1111
|
|
|
|
case d&0b1111_1111_1111_1111_1111_1000_0000_0000_0000_0000_0000_0000_0000_0000 == 0b0000_0000_0000_0000_0000_1000_0000_0000_0000_0000_0000_0000_0000_0000:
|
|
|
|
// value -(2^34^-1) to 2^34^-1
|
|
|
|
d -= 0b0000_0000_0000_0000_0000_1011_1111_1111_1111_1111_1111_1111_1111_1111
|
|
|
|
case d&0b1111_1111_1111_1100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 == 0b0000_0000_0000_0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000:
|
|
|
|
// value -(2^41^-1) to 2^41^-1
|
|
|
|
d -= 0b0000_0000_0000_0101_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
|
|
|
|
case d&0b1111_1110_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 == 0b0000_0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000:
|
|
|
|
// value -(2^48^-1) to 2^48^-1
|
|
|
|
d -= 0b0000_0010_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
|
|
|
|
}
|
|
|
|
s += d
|
|
|
|
laceSizes = append(laceSizes, s)
|
|
|
|
}
|
|
|
|
laceSizes = append(laceSizes, -1)
|
|
|
|
})
|
|
|
|
case lacingTypeFixed:
|
|
|
|
numLaces := int(d.FieldU8("num_laces"))
|
|
|
|
fixedSize := (d.BitsLeft() / 8) / int64(numLaces+1)
|
|
|
|
for i := 0; i < numLaces+1; i++ {
|
|
|
|
laceSizes = append(laceSizes, fixedSize)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.FieldArray("laces", func(d *decode.D) {
|
|
|
|
for _, laceSize := range laceSizes {
|
|
|
|
s := laceSize * 8
|
|
|
|
if laceSize == -1 {
|
|
|
|
s = d.BitsLeft()
|
|
|
|
}
|
|
|
|
d.FramedFn(s, fn)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
// TODO: smarter?
|
|
|
|
func decodeRawVintWidth(d *decode.D) (uint64, int) {
|
|
|
|
n := d.U8()
|
|
|
|
w := 1
|
|
|
|
for i := 0; i <= 7 && (n&(1<<(7-i))) == 0; i++ {
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
for i := 1; i < w; i++ {
|
|
|
|
n = n<<8 | d.U8()
|
|
|
|
}
|
|
|
|
return n, w
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeRawVint(d *decode.D) uint64 {
|
|
|
|
n, _ := decodeRawVintWidth(d)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:06:22 +03:00
|
|
|
func peekRawVint(d *decode.D) uint64 {
|
|
|
|
n, w := decodeRawVintWidth(d)
|
|
|
|
d.SeekRel(int64(-w) * 8)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func decodeVint(d *decode.D) uint64 {
|
|
|
|
n, w := decodeRawVintWidth(d)
|
|
|
|
m := (uint64(1<<((w-1)*8+(8-w))) - 1)
|
|
|
|
return n & m
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:28:19 +03:00
|
|
|
func decodeXiphLaceSize(d *decode.D) uint64 {
|
|
|
|
var s uint64
|
|
|
|
for {
|
|
|
|
n := d.U8()
|
|
|
|
s += n
|
|
|
|
if n < 255 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type track struct {
|
|
|
|
parentD *decode.D
|
|
|
|
number int
|
|
|
|
codec string
|
|
|
|
codecPrivatePos int64
|
|
|
|
codecPrivateTagSize int64
|
2022-05-20 16:10:41 +03:00
|
|
|
formatInArg any
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type block struct {
|
|
|
|
d *decode.D
|
|
|
|
r ranges.Range
|
|
|
|
simple bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type decodeContext struct {
|
|
|
|
currentTrack *track
|
|
|
|
tracks []*track
|
|
|
|
blocks []block
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:06:22 +03:00
|
|
|
func decodeMaster(d *decode.D, bitsLimit int64, elm *ebml.Master, unknownSize bool, dc *decodeContext) {
|
2020-06-08 03:29:51 +03:00
|
|
|
tagEndBit := d.Pos() + bitsLimit
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("elements", func(d *decode.D) {
|
2023-02-08 14:03:41 +03:00
|
|
|
for d.Pos() < tagEndBit && !d.End() {
|
2023-02-08 15:06:22 +03:00
|
|
|
// current we assume master with unknown size has ended if a valid parent is found
|
|
|
|
// TODO:
|
|
|
|
// https://github.com/ietf-wg-cellar/ebml-specification/blob/master/specification.markdown#unknown-data-size
|
|
|
|
// > Any valid EBML Element according to the EBML Schema, Global Elements excluded, that
|
|
|
|
// > is not a Descendant Element of the Unknown-Sized Element but shares a common direct
|
|
|
|
// > parent, such as a Top-Level Element.
|
|
|
|
// TODO: What to do if peeked is unknown?
|
|
|
|
// TODO: Handle garbage between element
|
|
|
|
if unknownSize {
|
|
|
|
peekTagID := peekRawVint(d)
|
|
|
|
_, validParent := ebml.FindParentID(ebml_matroska.IDToElement, elm.GetID(), ebml.ID(peekTagID))
|
|
|
|
if validParent {
|
|
|
|
break
|
2022-07-16 19:39:57 +03:00
|
|
|
}
|
2023-02-08 15:06:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
d.FieldStruct("element", func(d *decode.D) {
|
|
|
|
var childElm ebml.Element
|
|
|
|
childElm = &ebml.Unknown{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
tagID := d.FieldUintFn("id", decodeRawVint, scalar.UintFn(func(s scalar.Uint) (scalar.Uint, error) {
|
|
|
|
n := s.Actual
|
2021-11-05 17:04:26 +03:00
|
|
|
var ok bool
|
2023-02-08 15:06:22 +03:00
|
|
|
childElm, ok = elm.Master[ebml.ID(n)]
|
2021-11-05 17:04:26 +03:00
|
|
|
if !ok {
|
2023-02-08 15:06:22 +03:00
|
|
|
childElm, ok = ebml.Global.Master[ebml.ID(n)]
|
2021-11-05 17:04:26 +03:00
|
|
|
if !ok {
|
2023-02-08 15:06:22 +03:00
|
|
|
childElm = &ebml.Unknown{}
|
2022-09-30 14:58:23 +03:00
|
|
|
return scalar.Uint{Actual: n, DisplayFormat: scalar.NumberHex, Description: "Unknown"}, nil
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 15:06:22 +03:00
|
|
|
return scalar.Uint{
|
|
|
|
Actual: n,
|
|
|
|
DisplayFormat: scalar.NumberHex,
|
|
|
|
Sym: childElm.GetName(),
|
|
|
|
Description: childElm.GetDefinition(),
|
|
|
|
}, nil
|
2021-12-02 00:48:25 +03:00
|
|
|
}))
|
2023-02-08 15:06:22 +03:00
|
|
|
d.FieldValueStr("type", childElm.GetType())
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
if tagID == ebml_matroska.TrackEntryID {
|
|
|
|
dc.currentTrack = &track{}
|
|
|
|
dc.tracks = append(dc.tracks, dc.currentTrack)
|
|
|
|
}
|
|
|
|
|
2022-01-01 21:36:14 +03:00
|
|
|
const maxStringTagSize = 100 * 1024 * 1024
|
2023-02-08 14:03:41 +03:00
|
|
|
tagSize := d.FieldUintFn("size", decodeVint, scalar.UintMapDescription{
|
2023-02-10 12:29:25 +03:00
|
|
|
0xffffffffffffff: "Unknown size",
|
2023-02-08 14:03:41 +03:00
|
|
|
})
|
2023-02-08 15:06:22 +03:00
|
|
|
unknownSize := tagSize == tagSizeUnknown
|
|
|
|
if unknownSize {
|
2023-02-08 14:03:41 +03:00
|
|
|
tagSize = uint64(d.BitsLeft() / 8)
|
|
|
|
}
|
2022-01-01 21:36:14 +03:00
|
|
|
|
|
|
|
// assert sane tag size
|
|
|
|
// TODO: strings are limited for now because they are read into memory
|
2023-02-08 15:06:22 +03:00
|
|
|
switch childElm.(type) {
|
|
|
|
case *ebml.Integer,
|
|
|
|
*ebml.Uinteger,
|
|
|
|
*ebml.Float:
|
2022-01-01 21:36:14 +03:00
|
|
|
if tagSize > 8 {
|
|
|
|
d.Fatalf("invalid tagSize %d for number type", tagSize)
|
|
|
|
}
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.String,
|
|
|
|
*ebml.UTF8:
|
2022-01-01 21:36:14 +03:00
|
|
|
if tagSize > maxStringTagSize {
|
|
|
|
d.Errorf("tagSize %d > maxStringTagSize %d", tagSize, maxStringTagSize)
|
|
|
|
}
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Unknown,
|
|
|
|
*ebml.Binary,
|
|
|
|
*ebml.Date,
|
|
|
|
*ebml.Master:
|
2022-01-01 21:36:14 +03:00
|
|
|
// nop
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:06:22 +03:00
|
|
|
switch childElm := childElm.(type) {
|
|
|
|
case *ebml.Unknown:
|
2022-06-06 22:17:36 +03:00
|
|
|
d.FieldRawLen("data", int64(tagSize)*8)
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Integer:
|
2022-09-30 14:58:23 +03:00
|
|
|
var sm []scalar.SintMapper
|
2023-02-08 15:06:22 +03:00
|
|
|
if childElm.Enums != nil {
|
|
|
|
sm = append(sm, scalar.SintFn(func(s scalar.Sint) (scalar.Sint, error) {
|
|
|
|
if e, ok := childElm.Enums[s.Actual]; ok {
|
|
|
|
s.Sym = e.Name
|
|
|
|
s.Description = e.Description
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}))
|
2022-09-30 14:58:23 +03:00
|
|
|
}
|
|
|
|
d.FieldS("value", int(tagSize)*8, sm...)
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Uinteger:
|
2022-09-30 14:58:23 +03:00
|
|
|
var sm []scalar.UintMapper
|
2023-02-08 15:06:22 +03:00
|
|
|
if childElm.Enums != nil {
|
|
|
|
sm = append(sm, scalar.UintFn(func(s scalar.Uint) (scalar.Uint, error) {
|
|
|
|
if e, ok := childElm.Enums[s.Actual]; ok {
|
|
|
|
s.Sym = e.Name
|
|
|
|
s.Description = e.Description
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}))
|
2022-09-30 14:58:23 +03:00
|
|
|
}
|
|
|
|
v := d.FieldU("value", int(tagSize)*8, sm...)
|
2020-06-08 03:29:51 +03:00
|
|
|
if dc.currentTrack != nil && tagID == ebml_matroska.TrackNumberID {
|
|
|
|
dc.currentTrack.number = int(v)
|
|
|
|
}
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Float:
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldF("value", int(tagSize)*8)
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.String:
|
2022-09-30 14:58:23 +03:00
|
|
|
var sm []scalar.StrMapper
|
2023-02-08 15:06:22 +03:00
|
|
|
sm = append(sm, scalar.StrFn(func(s scalar.Str) (scalar.Str, error) {
|
|
|
|
if e, ok := childElm.Enums[s.Actual]; ok {
|
|
|
|
s.Sym = e.Name
|
|
|
|
s.Description = e.Description
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}))
|
2022-09-30 14:58:23 +03:00
|
|
|
v := d.FieldUTF8("value", int(tagSize), sm...)
|
2020-06-08 03:29:51 +03:00
|
|
|
if dc.currentTrack != nil && tagID == ebml_matroska.CodecIDID {
|
|
|
|
dc.currentTrack.codec = v
|
|
|
|
}
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.UTF8:
|
2021-11-17 18:13:10 +03:00
|
|
|
d.FieldUTF8NullFixedLen("value", int(tagSize))
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Date:
|
2020-06-08 03:29:51 +03:00
|
|
|
// TODO:
|
|
|
|
/*
|
|
|
|
proc type_date {size label _extra} {
|
|
|
|
set s [clock scan {2001-01-01 00:00:00}]
|
|
|
|
set frac 0
|
|
|
|
switch $size {
|
|
|
|
0 {}
|
|
|
|
8 {
|
|
|
|
set nano [int64]
|
|
|
|
set s [clock add $s [expr $nano/1000000000] seconds]
|
|
|
|
set frac [expr ($nano%1000000000)/1000000000.0]
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
bytes $size $label
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry $label "[clock format $s] ${frac}s" $size [expr [pos]-$size]
|
|
|
|
}
|
|
|
|
*/
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("value", int64(tagSize)*8)
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Binary:
|
2020-06-08 03:29:51 +03:00
|
|
|
switch tagID {
|
|
|
|
case ebml_matroska.SimpleBlockID:
|
|
|
|
dc.blocks = append(dc.blocks, block{
|
|
|
|
d: d,
|
|
|
|
r: ranges.Range{Start: d.Pos(), Len: int64(tagSize) * 8},
|
|
|
|
simple: true,
|
|
|
|
})
|
|
|
|
d.SeekRel(int64(tagSize) * 8)
|
|
|
|
case ebml_matroska.BlockID:
|
|
|
|
dc.blocks = append(dc.blocks, block{
|
|
|
|
d: d,
|
|
|
|
r: ranges.Range{Start: d.Pos(), Len: int64(tagSize) * 8},
|
|
|
|
})
|
|
|
|
d.SeekRel(int64(tagSize) * 8)
|
|
|
|
case ebml_matroska.CodecPrivateID:
|
|
|
|
if dc.currentTrack != nil {
|
|
|
|
dc.currentTrack.parentD = d
|
|
|
|
dc.currentTrack.codecPrivatePos = d.Pos()
|
|
|
|
dc.currentTrack.codecPrivateTagSize = int64(tagSize) * 8
|
|
|
|
}
|
|
|
|
d.SeekRel(int64(tagSize) * 8)
|
|
|
|
case ebml_matroska.FileDataID:
|
2023-03-29 01:36:55 +03:00
|
|
|
d.FieldFormatOrRawLen("value", int64(tagSize)*8, &imageGroup, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("value", int64(tagSize)*8)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:06:22 +03:00
|
|
|
case *ebml.Master:
|
|
|
|
decodeMaster(d, int64(tagSize)*8, childElm, unknownSize, dc)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
func matroskaDecode(d *decode.D) any {
|
2023-05-01 14:19:04 +03:00
|
|
|
var mi format.Matroska_In
|
2023-02-18 23:10:48 +03:00
|
|
|
d.ArgAs(&mi)
|
2023-02-08 13:21:08 +03:00
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
ebmlHeaderID := uint64(0x1a45dfa3)
|
2023-03-10 03:14:53 +03:00
|
|
|
if d.PeekUintBits(32) != ebmlHeaderID {
|
2021-11-17 18:26:13 +03:00
|
|
|
d.Errorf("no EBML header found")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
dc := &decodeContext{tracks: []*track{}}
|
2023-02-08 15:06:22 +03:00
|
|
|
decodeMaster(d, d.BitsLeft(), ebml_matroska.RootElement, false, dc)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
trackNumberToTrack := map[int]*track{}
|
|
|
|
for _, t := range dc.tracks {
|
|
|
|
trackNumberToTrack[t.number] = t
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range dc.tracks {
|
|
|
|
// no CodecPrivate found
|
|
|
|
if t.parentD == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:46:59 +03:00
|
|
|
// TODO: refactor, one DecodeRangeFn or refactor to add FieldFormatRangeFn?
|
2020-06-08 03:29:51 +03:00
|
|
|
switch t.codec {
|
|
|
|
case "A_VORBIS":
|
2021-11-05 17:04:26 +03:00
|
|
|
t.parentD.RangeFn(t.codecPrivatePos, t.codecPrivateTagSize, func(d *decode.D) {
|
2022-08-28 19:28:19 +03:00
|
|
|
decodeLacingFn(d, lacingTypeXiph, func(d *decode.D) {
|
2023-02-08 13:21:08 +03:00
|
|
|
if mi.DecodeSamples {
|
2023-03-29 01:36:55 +03:00
|
|
|
d.FieldFormat("packet", &vorbisPacketGroup, nil)
|
2023-02-08 13:21:08 +03:00
|
|
|
} else {
|
|
|
|
d.FieldRawLen("packet", d.BitsLeft())
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
case "A_AAC":
|
2023-03-29 01:36:55 +03:00
|
|
|
_, v := t.parentD.FieldFormatRange("value", t.codecPrivatePos, t.codecPrivateTagSize, &mpegASCFrameGroup, nil)
|
2023-05-01 14:19:04 +03:00
|
|
|
mpegASCOut, ok := v.(format.MPEG_ASC_Out)
|
2022-12-19 21:12:48 +03:00
|
|
|
if !ok {
|
2022-02-07 17:59:00 +03:00
|
|
|
panic(fmt.Sprintf("expected mpegASCOut got %#+v", v))
|
|
|
|
}
|
|
|
|
//nolint:gosimple
|
2023-05-01 14:19:04 +03:00
|
|
|
t.formatInArg = format.AAC_Frame_In{ObjectType: mpegASCOut.ObjectType}
|
2020-06-08 03:29:51 +03:00
|
|
|
case "A_OPUS":
|
2023-03-29 01:36:55 +03:00
|
|
|
t.parentD.FieldFormatRange("value", t.codecPrivatePos, t.codecPrivateTagSize, &opusPacketFrameGroup, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
case "A_FLAC":
|
2021-11-05 17:04:26 +03:00
|
|
|
t.parentD.RangeFn(t.codecPrivatePos, t.codecPrivateTagSize, func(d *decode.D) {
|
|
|
|
d.FieldStruct("value", func(d *decode.D) {
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldUTF8("magic", 4, d.StrAssert("fLaC"))
|
2023-03-29 01:36:55 +03:00
|
|
|
_, v := d.FieldFormat("metadatablocks", &flacMetadatablocksGroup, nil)
|
2023-05-01 14:19:04 +03:00
|
|
|
flacMetadatablockOut, ok := v.(format.FLAC_Metadatablocks_Out)
|
2022-12-19 21:12:48 +03:00
|
|
|
if !ok {
|
2021-11-16 19:11:26 +03:00
|
|
|
panic(fmt.Sprintf("expected FlacMetadatablockOut got %#+v", v))
|
2021-09-17 16:46:13 +03:00
|
|
|
}
|
|
|
|
if flacMetadatablockOut.HasStreamInfo {
|
2023-05-01 14:19:04 +03:00
|
|
|
t.formatInArg = format.FLAC_Frame_In{BitsPerSample: int(flacMetadatablockOut.StreamInfo.BitsPerSample)}
|
2021-09-17 16:46:13 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
case "V_MPEG4/ISO/AVC":
|
2023-03-29 01:36:55 +03:00
|
|
|
_, v := t.parentD.FieldFormatRange("value", t.codecPrivatePos, t.codecPrivateTagSize, &mpegAVCDCRGroup, nil)
|
2023-05-01 14:19:04 +03:00
|
|
|
avcDcrOut, ok := v.(format.AVC_DCR_Out)
|
2022-12-19 21:12:48 +03:00
|
|
|
if !ok {
|
2021-11-16 19:11:26 +03:00
|
|
|
panic(fmt.Sprintf("expected AvcDcrOut got %#+v", v))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2023-05-01 14:19:04 +03:00
|
|
|
t.formatInArg = format.AVC_AU_In{LengthSize: avcDcrOut.LengthSize} //nolint:gosimple
|
2020-06-08 03:29:51 +03:00
|
|
|
case "V_MPEGH/ISO/HEVC":
|
2023-03-29 01:36:55 +03:00
|
|
|
_, v := t.parentD.FieldFormatRange("value", t.codecPrivatePos, t.codecPrivateTagSize, &mpegHEVCDCRGroup, nil)
|
2023-05-01 14:19:04 +03:00
|
|
|
hevcDcrOut, ok := v.(format.HEVC_DCR_Out)
|
2022-12-19 21:12:48 +03:00
|
|
|
if !ok {
|
2021-11-16 19:11:26 +03:00
|
|
|
panic(fmt.Sprintf("expected HevcDcrOut got %#+v", v))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2023-05-01 14:19:04 +03:00
|
|
|
t.formatInArg = format.HEVC_AU_In{LengthSize: hevcDcrOut.LengthSize} //nolint:gosimple
|
2020-06-08 03:29:51 +03:00
|
|
|
case "V_AV1":
|
2023-03-29 01:36:55 +03:00
|
|
|
t.parentD.FieldFormatRange("value", t.codecPrivatePos, t.codecPrivateTagSize, &av1CCRGroup, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
case "V_VP9":
|
2023-03-29 01:36:55 +03:00
|
|
|
t.parentD.FieldFormatRange("value", t.codecPrivatePos, t.codecPrivateTagSize, &vp9CFMGroup, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
2021-11-05 17:04:26 +03:00
|
|
|
t.parentD.RangeFn(t.codecPrivatePos, t.codecPrivateTagSize, func(d *decode.D) {
|
|
|
|
d.FieldRawLen("value", d.BitsLeft())
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range dc.blocks {
|
2021-11-05 17:04:26 +03:00
|
|
|
b.d.RangeFn(b.r.Start, b.r.Len, func(d *decode.D) {
|
2022-08-28 19:28:19 +03:00
|
|
|
var lacing uint64
|
2022-09-30 14:58:23 +03:00
|
|
|
trackNumber := d.FieldUintFn("track_number", decodeVint)
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU16("timestamp")
|
|
|
|
if b.simple {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("flags", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("key_frame")
|
|
|
|
d.FieldU3("reserved")
|
|
|
|
d.FieldBool("invisible")
|
2022-08-28 19:28:19 +03:00
|
|
|
lacing = d.FieldU2("lacing", lacingTypeNames)
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("discardable")
|
|
|
|
})
|
|
|
|
} else {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("flags", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU4("reserved")
|
|
|
|
d.FieldBool("invisible")
|
2022-08-28 19:28:19 +03:00
|
|
|
lacing = d.FieldU2("lacing", lacingTypeNames)
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("not_used")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:28:19 +03:00
|
|
|
var f *decode.Group
|
|
|
|
var track *track
|
|
|
|
track, trackOk := trackNumberToTrack[int(trackNumber)]
|
|
|
|
if trackOk {
|
2023-03-29 01:36:55 +03:00
|
|
|
f = codecToGroup[track.codec]
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:28:19 +03:00
|
|
|
decodeLacingFn(d, int(lacing), func(d *decode.D) {
|
2023-02-08 13:21:08 +03:00
|
|
|
if mi.DecodeSamples && f != nil {
|
2023-03-29 01:36:55 +03:00
|
|
|
d.FieldFormat("packet", f, track.formatInArg)
|
2022-08-28 19:28:19 +03:00
|
|
|
} else {
|
|
|
|
d.FieldRawLen("packet", d.BitsLeft())
|
|
|
|
}
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|