1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 22:34:14 +03:00
fq/format/mpeg/mpeg_pes.go
Mattias Wadman 7c5215347d bitio,decode: Refactor bitio usage and make buffer slicing more correct
Remove bitio.Buffer layer. bitio.Buffer was a kitchen sink layer with helpers
now it's just a buffer and most functions have been moved to decode instead.

bitio package now only have primitive types and functions simialar to standard
library io and bytes packages.

Make nearly eveything internally use bitio.Bit* interfaces so that slicing work
correctly this will also make it possible to start experimenting with more
complicated silcing helpers, ex things like:
breplace(.header.bitrate; 123) to get a new buffer with bitrate changed.
2022-02-04 21:41:53 +01:00

84 lines
1.6 KiB
Go

package mpeg
// TODO: probeable?
// http://dvdnav.mplayerhq.hu/dvdinfo/mpeghdrs.html
import (
"log"
"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
)
var pesPacketFormat decode.Group
var spuFormat decode.Group
func init() {
registry.MustRegister(decode.Format{
Name: format.MPEG_PES,
Description: "MPEG Packetized elementary stream",
DecodeFn: pesDecode,
RootArray: true,
RootName: "packets",
Dependencies: []decode.Dependency{
{Names: []string{format.MPEG_PES_PACKET}, Group: &pesPacketFormat},
{Names: []string{format.MPEG_SPU}, Group: &spuFormat},
},
})
}
type subStream struct {
b []byte
l int
}
func pesDecode(d *decode.D, in interface{}) interface{} {
substreams := map[int]*subStream{}
prefix := d.PeekBits(24)
if prefix != 0b0000_0000_0000_0000_0000_0001 {
d.Errorf("no pes prefix found")
}
i := 0
spuD := d.FieldArrayValue("spus")
for d.NotEnd() {
dv, v, err := d.TryFieldFormat("packet", pesPacketFormat, nil)
if dv == nil || err != nil {
log.Printf("errs[0]: %#+v\n", err)
break
}
switch dvv := v.(type) {
case subStreamPacket:
s, ok := substreams[dvv.number]
if !ok {
s = &subStream{}
substreams[dvv.number] = s
}
s.b = append(s.b, dvv.buf...)
if s.l == 0 && len(s.b) >= 2 {
s.l = int(s.b[0])<<8 | int(s.b[1])
// TODO: zero l?
}
// TODO: is this how spu end is signalled?
if s.l == len(s.b) {
spuD.FieldFormatBitBuf("spu", bitio.NewBitReader(s.b, -1), spuFormat, nil)
s.b = nil
s.l = 0
}
}
i++
}
return nil
}