2020-06-08 03:29:51 +03:00
|
|
|
package ogg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
2022-01-24 23:21:48 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-11-24 23:20:46 +03:00
|
|
|
"github.com/wader/fq/pkg/checksum"
|
2021-08-17 13:06:32 +03:00
|
|
|
"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.OGG_PAGE,
|
|
|
|
Description: "OGG page",
|
|
|
|
DecodeFn: pageDecode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:33:50 +03:00
|
|
|
func pageDecode(d *decode.D, _ any) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
p := format.OggPageOut{}
|
|
|
|
startPos := d.Pos()
|
|
|
|
|
2021-11-24 13:15:42 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldUTF8("capture_pattern", 4, d.AssertStr("OggS"))
|
2021-11-24 13:15:42 +03:00
|
|
|
d.FieldU8("version", d.AssertU(0))
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldU5("unused_flags")
|
|
|
|
p.IsLastPage = d.FieldBool("last_page")
|
|
|
|
p.IsFirstPage = d.FieldBool("first_page")
|
|
|
|
p.IsContinuedPacket = d.FieldBool("continued_packet")
|
2021-11-24 13:15:42 +03:00
|
|
|
d.FieldU64("granule_position")
|
|
|
|
p.StreamSerialNumber = uint32(d.FieldU32("bitstream_serial_number"))
|
|
|
|
p.SequenceNo = uint32(d.FieldU32("page_sequence_no"))
|
2022-05-07 13:46:34 +03:00
|
|
|
d.FieldU32("crc", scalar.ActualHex)
|
2020-06-08 03:29:51 +03:00
|
|
|
pageSegments := d.FieldU8("page_segments")
|
|
|
|
var segmentTable []uint64
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("segment_table", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < pageSegments; i++ {
|
|
|
|
segmentTable = append(segmentTable, d.FieldU8("segment_size"))
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("segments", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for _, ss := range segmentTable {
|
2022-06-30 13:13:36 +03:00
|
|
|
bs := d.ReadAllBits(d.FieldRawLen("segment", int64(ss)*8))
|
2021-11-30 15:12:14 +03:00
|
|
|
p.Segments = append(p.Segments, bs)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
endPos := d.Pos()
|
|
|
|
|
2021-11-24 13:15:42 +03:00
|
|
|
pageChecksumValue := d.FieldGet("crc")
|
2021-11-24 23:20:46 +03:00
|
|
|
pageCRC := &checksum.CRC{Bits: 32, Table: checksum.Poly04c11db7Table}
|
2022-06-30 13:13:36 +03:00
|
|
|
d.Copy(pageCRC, bitio.NewIOReader(d.BitBufRange(startPos, pageChecksumValue.Range.Start-startPos))) // header before checksum
|
|
|
|
d.Copy(pageCRC, bytes.NewReader([]byte{0, 0, 0, 0})) // zero checksum bits
|
|
|
|
d.Copy(pageCRC, bitio.NewIOReader(d.BitBufRange(pageChecksumValue.Range.Stop(), endPos-pageChecksumValue.Range.Stop()))) // rest of page
|
2021-11-24 13:15:42 +03:00
|
|
|
_ = pageChecksumValue.TryScalarFn(d.ValidateUBytes(pageCRC.Sum(nil)))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|