2020-06-08 03:29:51 +03:00
|
|
|
package ogg
|
|
|
|
|
|
|
|
// https://xiph.org/ogg/doc/framing.html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-28 02:44:09 +03:00
|
|
|
"fmt"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
var oggPageGroup decode.Group
|
|
|
|
var vorbisPacketGroup decode.Group
|
|
|
|
var opusPacketGroup decode.Group
|
|
|
|
var flacMetadatablockGroup decode.Group
|
|
|
|
var flacFrameGroup decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2023-03-29 01:36:55 +03:00
|
|
|
interp.RegisterFormat(
|
|
|
|
format.Ogg,
|
|
|
|
&decode.Format{
|
|
|
|
Description: "OGG file",
|
|
|
|
Groups: []*decode.Group{format.Probe},
|
|
|
|
DecodeFn: decodeOgg,
|
|
|
|
Dependencies: []decode.Dependency{
|
2023-05-01 14:19:04 +03:00
|
|
|
{Groups: []*decode.Group{format.Ogg_Page}, Out: &oggPageGroup},
|
|
|
|
{Groups: []*decode.Group{format.Vorbis_Packet}, Out: &vorbisPacketGroup},
|
|
|
|
{Groups: []*decode.Group{format.Opus_Packet}, Out: &opusPacketGroup},
|
|
|
|
{Groups: []*decode.Group{format.FLAC_Metadatablock}, Out: &flacMetadatablockGroup},
|
|
|
|
{Groups: []*decode.Group{format.FLAC_Frame}, Out: &flacFrameGroup},
|
2023-03-29 01:36:55 +03:00
|
|
|
},
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
vorbisIdentification = []byte("\x01vorbis")
|
|
|
|
opusIdentification = []byte("OpusHead")
|
2021-10-28 02:44:09 +03:00
|
|
|
flacIdentification = []byte("\x7fFLAC")
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type streamCodec int
|
|
|
|
|
|
|
|
const (
|
|
|
|
codecUnknown streamCodec = iota
|
|
|
|
codecVorbis
|
|
|
|
codecOpus
|
2021-10-28 02:44:09 +03:00
|
|
|
codecFlac
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type stream struct {
|
2021-10-28 02:44:09 +03:00
|
|
|
sequenceNo uint32
|
|
|
|
packetBuf []byte
|
|
|
|
packetD *decode.D
|
|
|
|
codec streamCodec
|
2023-05-01 14:19:04 +03:00
|
|
|
flacStreamInfo format.FLAC_Stream_Info
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
func decodeOgg(d *decode.D) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
validPages := 0
|
|
|
|
streams := map[uint32]*stream{}
|
2021-11-21 15:08:18 +03:00
|
|
|
streamsD := d.FieldArrayValue("streams")
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("pages", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for !d.End() {
|
2023-03-29 01:36:55 +03:00
|
|
|
_, dv, _ := d.TryFieldFormat("page", &oggPageGroup, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
if dv == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-05-01 14:19:04 +03:00
|
|
|
oggPageOut, ok := dv.(format.Ogg_Page_Out)
|
2020-06-08 03:29:51 +03:00
|
|
|
if !ok {
|
|
|
|
panic("page decode is not a oggPageOut")
|
|
|
|
}
|
|
|
|
|
|
|
|
s, sFound := streams[oggPageOut.StreamSerialNumber]
|
|
|
|
if !sFound {
|
|
|
|
var packetsD *decode.D
|
2021-11-05 17:04:26 +03:00
|
|
|
streamsD.FieldStruct("stream", func(d *decode.D) {
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldValueUint("serial_number", uint64(oggPageOut.StreamSerialNumber))
|
2021-11-21 15:08:18 +03:00
|
|
|
packetsD = d.FieldArrayValue("packets")
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
s = &stream{
|
|
|
|
sequenceNo: oggPageOut.SequenceNo,
|
|
|
|
packetD: packetsD,
|
|
|
|
codec: codecUnknown,
|
|
|
|
}
|
|
|
|
streams[oggPageOut.StreamSerialNumber] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// if !sFound && !oggPageOut.IsFirstPage {
|
|
|
|
// // TODO: not first page and we haven't seen the stream before
|
|
|
|
// // log.Println("not first page and we haven't seen the stream before")
|
|
|
|
// }
|
|
|
|
// hasData := len(s.packetBuf) > 0
|
|
|
|
// if oggPageOut.IsContinuedPacket && !hasData {
|
|
|
|
// // TODO: continuation but we haven't seen any packet data yet
|
|
|
|
// // log.Println("continuation but we haven't seen any packet data yet")
|
|
|
|
// }
|
|
|
|
// if !oggPageOut.IsFirstPage && s.sequenceNo+1 != oggPageOut.SequenceNo {
|
|
|
|
// // TODO: page gap
|
|
|
|
// // log.Println("page gap")
|
|
|
|
// }
|
|
|
|
|
2021-11-30 15:12:14 +03:00
|
|
|
for _, bs := range oggPageOut.Segments {
|
|
|
|
s.packetBuf = append(s.packetBuf, bs...)
|
|
|
|
if len(bs) < 255 {
|
2022-01-24 23:21:48 +03:00
|
|
|
br := bitio.NewBitReader(s.packetBuf, -1)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
if s.codec == codecUnknown {
|
2022-01-24 23:21:48 +03:00
|
|
|
if bytes.HasPrefix(s.packetBuf, vorbisIdentification) {
|
2020-06-08 03:29:51 +03:00
|
|
|
s.codec = codecVorbis
|
2022-01-24 23:21:48 +03:00
|
|
|
} else if bytes.HasPrefix(s.packetBuf, opusIdentification) {
|
2020-06-08 03:29:51 +03:00
|
|
|
s.codec = codecOpus
|
2022-01-24 23:21:48 +03:00
|
|
|
} else if bytes.HasPrefix(s.packetBuf, flacIdentification) {
|
2021-10-28 02:44:09 +03:00
|
|
|
s.codec = codecFlac
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch s.codec {
|
|
|
|
case codecVorbis:
|
|
|
|
// TODO: err
|
2023-03-29 01:36:55 +03:00
|
|
|
if _, _, err := s.packetD.TryFieldFormatBitBuf("packet", br, &vorbisPacketGroup, nil); err != nil {
|
2022-01-24 23:21:48 +03:00
|
|
|
s.packetD.FieldRootBitBuf("packet", br)
|
2021-11-20 18:22:00 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
case codecOpus:
|
|
|
|
// TODO: err
|
2023-03-29 01:36:55 +03:00
|
|
|
if _, _, err := s.packetD.TryFieldFormatBitBuf("packet", br, &opusPacketGroup, nil); err != nil {
|
2022-01-24 23:21:48 +03:00
|
|
|
s.packetD.FieldRootBitBuf("packet", br)
|
2021-11-20 18:22:00 +03:00
|
|
|
}
|
2021-10-28 02:44:09 +03:00
|
|
|
case codecFlac:
|
2022-01-24 23:21:48 +03:00
|
|
|
if len(s.packetBuf) == 0 {
|
2021-10-28 02:44:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2022-01-24 23:21:48 +03:00
|
|
|
case s.packetBuf[0] == 0x7f:
|
|
|
|
s.packetD.FieldStructRootBitBufFn("packet", br, func(d *decode.D) {
|
2021-10-28 02:44:09 +03:00
|
|
|
d.FieldU8("type")
|
|
|
|
d.FieldUTF8("signature", 4)
|
|
|
|
d.FieldU8("major")
|
|
|
|
d.FieldU8("minor")
|
|
|
|
d.FieldU16("header_packets")
|
|
|
|
d.FieldUTF8("flac_signature", 4)
|
2023-03-29 01:36:55 +03:00
|
|
|
_, v := d.FieldFormat("metadatablock", &flacMetadatablockGroup, nil)
|
2023-05-01 14:19:04 +03:00
|
|
|
flacMetadatablockOut, ok := v.(format.FLAC_Metadatablock_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", flacMetadatablockOut))
|
2021-10-28 02:44:09 +03:00
|
|
|
}
|
|
|
|
s.flacStreamInfo = flacMetadatablockOut.StreamInfo
|
|
|
|
})
|
2022-01-24 23:21:48 +03:00
|
|
|
case s.packetBuf[0] == 0xff:
|
2023-03-29 01:36:55 +03:00
|
|
|
s.packetD.FieldFormatBitBuf("packet", br, &flacFrameGroup, nil)
|
2021-10-28 02:44:09 +03:00
|
|
|
default:
|
2023-03-29 01:36:55 +03:00
|
|
|
s.packetD.FieldFormatBitBuf("packet", br, &flacMetadatablockGroup, nil)
|
2021-10-28 02:44:09 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
case codecUnknown:
|
2022-01-24 23:21:48 +03:00
|
|
|
s.packetD.FieldRootBitBuf("packet", br)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.packetBuf = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.sequenceNo = oggPageOut.SequenceNo
|
|
|
|
if oggPageOut.IsLastPage {
|
|
|
|
delete(streams, oggPageOut.StreamSerialNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
validPages++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if validPages == 0 {
|
2021-11-17 18:26:13 +03:00
|
|
|
d.Fatalf("no pages found")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|