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

View File

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

View File

@ -37,53 +37,6 @@ const (
PayloadSize = 176 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 below data struct encapsulates the fields of an MPEG-TS packet. Below is
the formatting of an MPEG-TS packet for reference! 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 { for i = 0; i < len(d); i += PacketSize {
pid := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2]) pid := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
if pid == PmtPid { if pid == pmtPid {
p = d[i+4 : i+PacketSize] p = d[i+4 : i+PacketSize]
return return
} }