cmd/ts-repair: added required consts and undid changes to mts pkg

This commit is contained in:
saxon 2019-02-08 00:14:22 +10:30
parent e98d7bb62e
commit 19821553e8
3 changed files with 56 additions and 75 deletions

View File

@ -11,6 +11,21 @@ import (
"github.com/Comcast/gots/packet"
)
const (
PatPid = 0
PmtPid = 4096
VideoPid = 256
HeadSize = 4
DefaultAdaptationSize = 2
AdaptationIdx = 4
AdaptationControlIdx = 3
AdaptationBodyIdx = AdaptationIdx + 1
AdaptationControlMask = 0x30
DefaultAdaptationBodySize = 1
DiscontinuityIndicatorMask = 0x80
DiscontinuityIndicatorIdx = AdaptationIdx + 1
)
// Various errors that we can encounter.
const (
errBadInPath = "No file path provided, or file does not exist"
@ -37,9 +52,9 @@ const (
)
var ccMap = map[int]byte{
mts.PatPid: 16,
mts.PmtPid: 16,
mts.VideoPid: 16,
PatPid: 16,
PmtPid: 16,
VideoPid: 16,
}
// packetNo will keep track of the ts packet number for reference.
@ -48,8 +63,8 @@ var packetNo int
// Option defines a func that performs an action on p in order to change a ts option.
type Option func(p *Packet)
// Packet is a byte array of size mts.PacketSize i.e. 188 bytes. We define this
// to allow us to write receiver funcs for the [mts.PacketSize]byte type.
// Packet is a byte array of size PacketSize i.e. 188 bytes. We define this
// to allow us to write receiver funcs for the [PacketSize]byte type.
type Packet [mts.PacketSize]byte
// CC returns the CC of p.
@ -78,12 +93,12 @@ func (p *Packet) addAdaptationField(options ...Option) error {
return errors.New(errAdaptationPresent)
}
// Create space for adaptation field.
copy(p[mts.HeadSize+mts.DefaultAdaptationSize:], p[mts.HeadSize:len(p)-mts.DefaultAdaptationSize])
copy(p[HeadSize+DefaultAdaptationSize:], p[HeadSize:len(p)-DefaultAdaptationSize])
// TODO: seperate into own function
// Update adaptation field control.
p[mts.AdaptationControlIdx] &= 0xff ^ mts.AdaptationControlMask
p[mts.AdaptationControlIdx] |= mts.AdaptationControlMask
p[AdaptationControlIdx] &= 0xff ^ AdaptationControlMask
p[AdaptationControlIdx] |= AdaptationControlMask
// Default the adaptationfield.
p.resetAdaptation()
@ -100,14 +115,14 @@ func (p *Packet) resetAdaptation() error {
if !p.hasAdaptation() {
return errors.New(errNoAdaptationField)
}
p[mts.AdaptationIdx] = mts.DefaultAdaptationBodySize
p[mts.AdaptationBodyIdx] = 0x00
p[AdaptationIdx] = DefaultAdaptationBodySize
p[AdaptationBodyIdx] = 0x00
return nil
}
// hasAdaptation returns true if p has an adaptation field and false otherwise.
func (p *Packet) hasAdaptation() bool {
afc := p[mts.AdaptationControlIdx] & mts.AdaptationControlMask
afc := p[AdaptationControlIdx] & AdaptationControlMask
if afc == 0x20 || afc == 0x30 {
return true
} else {
@ -119,12 +134,12 @@ func (p *Packet) hasAdaptation() bool {
// indicator according to f.
func DiscontinuityIndicator(f bool) Option {
return func(p *Packet) {
set := byte(mts.DiscontinuityIndicatorMask)
set := byte(DiscontinuityIndicatorMask)
if !f {
set = 0x00
}
p[mts.DiscontinuityIndicatorIdx] &= 0xff ^ mts.DiscontinuityIndicatorMask
p[mts.DiscontinuityIndicatorIdx] |= mts.DiscontinuityIndicatorMask & set
p[DiscontinuityIndicatorIdx] &= 0xff ^ DiscontinuityIndicatorMask
p[DiscontinuityIndicatorIdx] |= DiscontinuityIndicatorMask & set
}
}

View File

@ -171,6 +171,14 @@ var (
pmtTable = standardPmtTimeLocation.Bytes()
)
const (
sdtPid = 17
patPid = 0
pmtPid = 4096
videoPid = 256
streamID = 0xe0 // First video stream ID.
)
// Time related constants.
const (
// ptsOffset is the offset added to the clock to determine
@ -205,13 +213,18 @@ func NewEncoder(dst io.Writer, fps float64) *Encoder {
ptsOffset: ptsOffset,
continuity: map[int]byte{
PatPid: 0,
PmtPid: 0,
VideoPid: 0,
patPid: 0,
pmtPid: 0,
videoPid: 0,
},
}
}
const (
hasPayload = 0x1
hasAdaptationField = 0x2
)
const (
hasDTS = 0x1
hasPTS = 0x2
@ -231,7 +244,7 @@ func (e *Encoder) Encode(nalu []byte) error {
// Prepare PES data.
pesPkt := pes.Packet{
StreamID: StreamID,
StreamID: streamID,
PDI: hasPTS,
PTS: e.pts(),
Data: nalu,
@ -243,10 +256,10 @@ func (e *Encoder) Encode(nalu []byte) error {
for len(buf) != 0 {
pkt := Packet{
PUSI: pusi,
PID: VideoPid,
PID: videoPid,
RAI: pusi,
CC: e.ccFor(VideoPid),
AFC: HasAdaptationField | HasPayload,
CC: e.ccFor(videoPid),
AFC: hasAdaptationField | hasPayload,
PCRF: pusi,
}
n := pkt.FillPayload(buf)
@ -275,9 +288,9 @@ func (e *Encoder) writePSI() error {
// Write PAT.
patPkt := Packet{
PUSI: true,
PID: PatPid,
CC: e.ccFor(PatPid),
AFC: HasPayload,
PID: patPid,
CC: e.ccFor(patPid),
AFC: hasPayload,
Payload: patTable,
}
_, err := e.dst.Write(patPkt.Bytes(e.tsSpace[:PacketSize]))
@ -298,9 +311,9 @@ func (e *Encoder) writePSI() error {
// Create mts packet from pmt table.
pmtPkt := Packet{
PUSI: true,
PID: PmtPid,
CC: e.ccFor(PmtPid),
AFC: HasPayload,
PID: pmtPid,
CC: e.ccFor(pmtPid),
AFC: hasPayload,
Payload: pmtTable,
}
_, err = e.dst.Write(pmtPkt.Bytes(e.tsSpace[:PacketSize]))

View File

@ -37,53 +37,6 @@ const (
PayloadSize = 176
)
const (
SdtPid = 17
PatPid = 0
PmtPid = 4096
VideoPid = 256
StreamID = 0xe0 // First video stream ID.
HeadSize = 4
DefaultAdaptationSize = 2
)
const (
AdaptationIdx = 4
AdaptationControlIdx = 3
AdaptationBodyIdx = AdaptationIdx + 1
AdaptationControlMask = 0x30
DefaultAdaptationBodySize = 1
)
const (
HasPayload = 0x1
HasAdaptationField = 0x2
)
// Adaptation field body masks.
const (
DiscontinuityIndicatorMask = 0x80
RandomAccessIndicatorMask = 0x40
ElementaryStreamPriorityIndicatorMask = 0x20
ProgramClockReferenceFlagMask = 0x10
OriginalProgramClockReferenceFlagMask = 0x08
SplicingPointFlagMask = 0x04
TransportPrivateDataFlagMask = 0x02
AdaptationFieldExtensionMask = 0x01
)
// Adaptation field body indexes.
const (
DiscontinuityIndicatorIdx = AdaptationIdx + 1
RandomAccessIndicatorIdx = AdaptationIdx + 1
ElementaryStreamPriorityIndicatorIdx = AdaptationIdx + 1
ProgramClockReferenceFlagIdx = AdaptationIdx + 1
OriginalProgramClockReferenceFlagIdx = AdaptationIdx + 1
SplicingPointFlagIdx = AdaptationIdx + 1
TransportPrivateDataFlagIdx = AdaptationIdx + 1
AdaptationFieldExtensionFlagIdx = AdaptationIdx + 1
)
/*
The below data struct encapsulates the fields of an MPEG-TS packet. Below is
the formatting of an MPEG-TS packet for reference!
@ -182,7 +135,7 @@ func FindPMT(d []byte) (p []byte, i int, err error) {
}
for i = 0; i < len(d); i += PacketSize {
pid := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
if pid == PmtPid {
if pid == pmtPid {
p = d[i+4 : i+PacketSize]
return
}