Skip to content

Commit

Permalink
Improve the allocation of memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsuGitHub committed Aug 7, 2016
1 parent 3520f62 commit af2adb5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 34 deletions.
38 changes: 30 additions & 8 deletions tsparser/adaptation_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
// AdaptationField adaptation_field data.
type AdaptationField struct {
pcr uint64
prevPcr *uint64
pos int64
options options.Options
buf []byte
Expand Down Expand Up @@ -46,10 +45,36 @@ type AdaptationField struct {
func NewAdaptationField() *AdaptationField { return new(AdaptationField) }

// Initialize Set Params for TsPacket
func (af *AdaptationField) Initialize(pos int64, prevPcr *uint64, options options.Options) {
func (af *AdaptationField) Initialize(pos int64, options options.Options) {
af.pcr = 0
af.pos = pos
af.prevPcr = prevPcr
af.options = options
af.buf = af.buf[0:0]

af.adaptationFieldLength = 0
af.discontinuityIndicator = 0
af.randomAccessIndicator = 0
af.elementaryStreamPriorityIndicator = 0
af.pcrFlag = 0
af.oPcrFlag = 0
af.splicingPointFlag = 0
af.adaptationFieldExtensionFlag = 0
af.programClockReferenceBase = 0
af.programClockReferenceExtension = 0
af.originalProgramClockReferenceBase = 0
af.originalProgramClockReferenceExtension = 0
af.spliceCountdown = 0
af.transportPrivateDataLength = 0
af.privateDataByte = af.privateDataByte[0:0]
af.adaptationFieldExtensionLength = 0
af.ltwFlag = 0
af.piecewiseRateFlag = 0
af.seamlessSpliceFlag = 0
af.ltwValidFlag = 0
af.ltwOffset = 0
af.piecewiseRate = 0
af.spliceType = 0
af.dtsNextAu = 0
}

// Append append adaptation_field data for buffer.
Expand Down Expand Up @@ -111,9 +136,6 @@ func (af *AdaptationField) Parse() (uint8, error) {
pcrBase := af.programClockReferenceBase
pcrExt := uint64(af.programClockReferenceExtension)
af.pcr = pcrBase*300 + pcrExt
if af.prevPcr != nil {
*(af.prevPcr) = af.pcr
}
}
if af.oPcrFlag == 1 {
if af.originalProgramClockReferenceBase, err = bb.PeekUint64(33); err != nil {
Expand Down Expand Up @@ -207,10 +229,10 @@ func (af *AdaptationField) Parse() (uint8, error) {
}

// DumpPcr print PCR.
func (af *AdaptationField) DumpPcr() {
func (af *AdaptationField) DumpPcr(prevPcr uint64) {
if af.pcrFlag == 1 {
pcrMilisec := float64(af.pcr) / 300 / 90
pcrInterval := float64(af.pcr-*af.prevPcr) / 300 / 90
pcrInterval := float64(af.pcr-prevPcr) / 300 / 90
fmt.Printf("0x%08x PCR: 0x%08x[%012fms] (Interval:%012fms)\n", af.pos, af.pcr, pcrMilisec, pcrInterval)
}
}
Expand Down
32 changes: 15 additions & 17 deletions tsparser/mpeg_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const tsPacketSize = 188
func BufferPsi(file *os.File, pos *int64, pid uint16, mpegPacket MpegPacket, options options.Options) error {
tsBuffer := make([]byte, tsPacketSize)
isBuffering := false
var prevPcr uint64
tsPacket := NewTsPacket()

for {
size, err := file.Read(tsBuffer)
Expand All @@ -37,8 +37,7 @@ func BufferPsi(file *os.File, pos *int64, pid uint16, mpegPacket MpegPacket, opt
break
}

tsPacket := NewTsPacket()
tsPacket.Initialize(*pos, &prevPcr, options)
tsPacket.Initialize(*pos, options)
tsPacket.Append(tsBuffer)
tsPacket.Parse()
if tsPacket.Pid() != pid {
Expand Down Expand Up @@ -69,7 +68,6 @@ func BufferPsi(file *os.File, pos *int64, pid uint16, mpegPacket MpegPacket, opt
// BufferPes buffer PSI data from TS payload
func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramInfo, options options.Options) error {
tsBuffer := make([]byte, tsPacketSize)
var prevPcr uint64
pesMap := make(map[uint16]*Pes)
for _, val := range programInfos {
pesMap[val.elementaryPid] = nil
Expand All @@ -78,6 +76,7 @@ func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramI
var lastPcrPos int64
var maxDelay float64
var maxPcrInterval float64
tsPacket := NewTsPacket()

for {
size, err := file.Read(tsBuffer)
Expand All @@ -90,17 +89,18 @@ func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramI
break
}

tmpPrevPcr := prevPcr
tsPacket := NewTsPacket()
tsPacket.Initialize(*pos, &prevPcr, options)
tsPacket.Initialize(*pos, options)
tsPacket.Append(tsBuffer)
tsPacket.Parse()
pid := tsPacket.Pid()
pes, exist := pesMap[pid]
if tsPacket.HasAf() && tsPacket.adaptationField.PcrFlag() && pcrPid != 0 && pid == pcrPid {
if !options.NotDumpTimestamp() {
tsPacket.adaptationField.DumpPcr(lastPcr)
}
maxPcrInterval = math.Max(maxPcrInterval, float64(tsPacket.Pcr()-lastPcr))
lastPcr = tsPacket.Pcr()
lastPcrPos = *pos
maxPcrInterval = math.Max(maxPcrInterval, float64(lastPcr-tmpPrevPcr))
}
if !exist {
*pos += int64(size)
Expand All @@ -114,19 +114,17 @@ func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramI
pcrDelay := pes.DumpTimestamp()
maxDelay = math.Max(maxDelay, pcrDelay)
}
} else {
pes = NewPes()
pesMap[pid] = pes
}
pesMap[pid] = NewPes()
newPes, _ := pesMap[pid]
newPes.pid = pid
newPes.pos = *pos
newPes.SetContinuityCounter(tsPacket.ContinuityCounter())
newPes.prevPcr = lastPcr
newPes.prevPcrPos = lastPcrPos
newPes.Append(tsPacket.Payload()) // read until pointer_field
pes.Initialize(pid, *pos, lastPcr, lastPcrPos)
pes.SetContinuityCounter(tsPacket.ContinuityCounter())
pes.Append(tsPacket.Payload()) // read until pointer_field

} else if tsPacket.ContinuityCounter() == (pes.ContinuityCounter()+1) || (tsPacket.ContinuityCounter() == 0x0 && pes.ContinuityCounter() == 0xF) {
pes.SetContinuityCounter(tsPacket.ContinuityCounter())
if tsPacket.HasAf() && tsPacket.adaptationField.PcrFlag() && pcrPid != 0 && pid == pcrPid {
if pes.nextPcr == 0 && lastPcr > pes.prevPcr {
pes.nextPcr = lastPcr
pes.nextPcrPos = lastPcrPos
}
Expand Down
53 changes: 53 additions & 0 deletions tsparser/pes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,59 @@ func NewPes() *Pes {
return pes
}

// Initialize Set Params for PES
func (p *Pes) Initialize(pid uint16, pos int64, prevPcr uint64, prevPcrPos int64) {
p.pid = pid
p.continuityCounter = 0
p.buf = p.buf[0:0]
p.pos = pos
p.prevPcr = prevPcr
p.nextPcr = 0
p.prevPcrPos = prevPcrPos
p.nextPcrPos = 0

p.packetStartCodePrefix = 0
p.streamID = 0
p.pesPacketLength = 0
p.pesScramblingControl = 0
p.pesPriority = 0
p.dataAlignmentIndicator = 0
p.copyright = 0
p.originalOrCopy = 0
p.ptsDtsFlags = 0
p.escrFlag = 0
p.esRateFlag = 0
p.dsmTrickModeFlag = 0
p.additionalCopyInfoFlag = 0
p.pesCrcFlag = 0
p.pesExtentionFlag = 0
p.pesHeaderDataLength = 0
p.pts = 0
p.dts = 0
p.escr = 0
p.escrBase = 0
p.escrExtention = 0
p.esRate = 0
p.trickModeControl = 0
p.fieldID = 0
p.intraSliceRefresh = 0
p.frequencyTruncation = 0
p.repCntrl = 0
p.additionalCopyInfo = 0
p.previousPesPacketCrc = 0
p.pesPrivateDataFlag = 0
p.packHeaderFieldFlag = 0
p.programPacketSequenceCounterFlag = 0
p.pStdBufferFlag = 0
p.pesExtentionFlag2 = 0
p.programPacketSequenceCounter = 0
p.mpeg1Mpeg2Identifer = 0
p.originalStuffLength = 0
p.pStdBufferScale = 0
p.pStdBufferSize = 0
p.pesExtentionFieldLength = 0
}

// ContinuityCounter return current continuity_counter of TsPacket.
func (p *Pes) ContinuityCounter() uint8 { return p.continuityCounter }

Expand Down
25 changes: 16 additions & 9 deletions tsparser/ts_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const tsHeaderSize = 4
// TsPacket is mpeg2-ts packet. It has fixed size(188byte).
type TsPacket struct {
pos int64
prevPcr *uint64
options options.Options
buf []byte
payload []byte
Expand All @@ -33,14 +32,26 @@ type TsPacket struct {
func NewTsPacket() *TsPacket {
tp := new(TsPacket)
tp.buf = make([]byte, 0, tsPacketSize)
tp.adaptationField = NewAdaptationField()
return tp
}

// Initialize Set Params for TsPacket
func (tp *TsPacket) Initialize(pos int64, prevPcr *uint64, options options.Options) {
func (tp *TsPacket) Initialize(pos int64, options options.Options) {
tp.pos = pos
tp.prevPcr = prevPcr
tp.options = options

tp.buf = tp.buf[0:0]
tp.payload = tp.buf[0:0]
tp.syncByte = 0
tp.transportErrorIndicator = 0
tp.payloadUnitStartIndicator = 0
tp.transportPriority = 0
tp.pid = 0
tp.transportScramblingControl = 0
tp.adaptationFieldControl = 0
tp.continuityCounter = 0
tp.adaptationField.Initialize(tp.pos, tp.options)
}

// Append append ts payload data for buffer.
Expand Down Expand Up @@ -92,15 +103,11 @@ func (tp *TsPacket) Parse() error {

var afLength uint8
if tp.adaptationFieldControl == 2 || tp.adaptationFieldControl == 3 {
tp.adaptationField.Initialize(tp.pos, tp.prevPcr, tp.options)
tp.adaptationField = NewAdaptationField()
tp.adaptationField.Append(tp.Payload())
tp.adaptationField.Initialize(tp.pos, tp.options)
tp.adaptationField.Append(tp.buf[tsHeaderSize:])
if afLength, err = tp.adaptationField.Parse(); err != nil {
return err
}
if tp.adaptationField.PcrFlag() {
tp.adaptationField.DumpPcr()
}
if tp.options.DumpAdaptationField() {
tp.adaptationField.Dump()
}
Expand Down

0 comments on commit af2adb5

Please sign in to comment.