Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpeg_ts: Rewrite and cleanup #542

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ var (
MPEG_PES_Packet = &decode.Group{Name: "mpeg_pes_packet"}
MPEG_SPU = &decode.Group{Name: "mpeg_spu"}
MPEG_TS = &decode.Group{Name: "mpeg_ts"}
MPEG_TS_Packet = &decode.Group{Name: "mpeg_ts_packet"}
MPEG_TS_PAT = &decode.Group{Name: "mpeg_ts_pat"}
MPEG_TS_PMT = &decode.Group{Name: "mpeg_ts_pmt"}
MPEG_TS_SDT = &decode.Group{Name: "mpeg_ts_sdt"}
MsgPack = &decode.Group{Name: "msgpack"}
Ogg = &decode.Group{Name: "ogg"}
Ogg_Page = &decode.Group{Name: "ogg_page"}
Expand Down Expand Up @@ -398,3 +402,41 @@ type Pg_Heap_In struct {
type Pg_BTree_In struct {
Page int `doc:"First page number in file, default is 0"`
}

type MpegTsIn struct {
MaxSyncSeek int `doc:"Max byte distance to next sync"`
}

type MpegTsStream struct {
ProgramPid int
Type int
}

type MpegTsProgram struct {
Number int
Pid int
StreamPids []int
}

type MpegTsPacketIn struct {
ProgramMap map[int]MpegTsProgram
StreamMap map[int]MpegTsStream
ContinuityMap map[int]int
}

type MpegTsPacketOut struct {
Pid int
TransportErrorIndicator bool
ContinuityCounter int
TransportScramblingControl int
PayloadUnitStart bool
Payload []byte
}

type MpegTsPatOut struct {
PidMap map[int]int // pid to program number that has pmt
}

type MpegTsPmtOut struct {
Streams map[int]MpegTsStream
}
59 changes: 32 additions & 27 deletions format/mpeg/mpeg_pes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package mpeg
// http://dvdnav.mplayerhq.hu/dvdinfo/mpeghdrs.html

import (
"log"

"github.com/wader/fq/format"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
Expand All @@ -20,8 +22,8 @@ func init() {
&decode.Format{
Description: "MPEG Packetized elementary stream",
DecodeFn: pesDecode,
RootArray: true,
RootName: "packets",
// RootArray: true,
RootName: "packets",
Dependencies: []decode.Dependency{
{Groups: []*decode.Group{format.MPEG_PES_Packet}, Out: &pesPacketGroup},
{Groups: []*decode.Group{format.MPEG_SPU}, Out: &mpegSpuGroup},
Expand All @@ -46,36 +48,39 @@ func pesDecode(d *decode.D) any {

spuD := d.FieldArrayValue("spus")

for d.NotEnd() {
dv, v, err := d.TryFieldFormat("packet", &pesPacketGroup, nil)
if dv == nil || err != nil {
break
}

switch dvv := v.(type) {
case subStreamPacket:
s, ok := substreams[dvv.number]
if !ok {
s = &subStream{}
substreams[dvv.number] = s
d.FieldArray("packets", func(d *decode.D) {
for d.NotEnd() {
dv, v, err := d.TryFieldFormat("packet", &pesPacketGroup, nil)
if dv == nil || err != nil {
log.Printf("err: %#+v\n", err)
break
}
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?
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), &mpegSpuGroup, nil)
s.b = nil
s.l = 0
}
}

// TODO: is this how spu end is signalled?
if s.l == len(s.b) {
spuD.FieldFormatBitBuf("spu", bitio.NewBitReader(s.b, -1), &mpegSpuGroup, nil)
s.b = nil
s.l = 0
}
i++
}

i++
}
})

return nil
}
6 changes: 3 additions & 3 deletions format/mpeg/mpeg_pes_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ func pesPacketDecode(d *decode.D) any {
// nop
}

if d.BitsLeft() > 0 {
d.FieldRawLen("data", d.BitsLeft())
}
// if d.BitsLeft() > 0 {
// d.FieldRawLen("data", d.BitsLeft())
// }

return v
}
Loading