Skip to content

Commit

Permalink
Merged branch develop into master
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsuGitHub committed Aug 7, 2016
2 parents fbe23d1 + a0a4360 commit d71a997
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 60 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"os"

"github.com/small-teton/MpegTsAnalyzer/options"
Expand Down Expand Up @@ -42,7 +43,9 @@ func parseTsFile(filename string, options options.Options) error {
buf := make([]byte, bufSize)
for {
size, err := file.Read(buf)
if err != nil {
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("File read error: %s", err)
}
if pos, err = findPat(buf); err != nil {
Expand Down
48 changes: 38 additions & 10 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 All @@ -30,7 +29,7 @@ type AdaptationField struct {
originalProgramClockReferenceExtension uint16
spliceCountdown uint8
transportPrivateDataLength uint8
privateDataByte []uint8
privateDataByte []byte
adaptationFieldExtensionLength uint8
ltwFlag uint8
piecewiseRateFlag uint8
Expand All @@ -43,13 +42,45 @@ type AdaptationField struct {
}

// NewAdaptationField create new adaptation_field instance.
func NewAdaptationField() *AdaptationField { return new(AdaptationField) }
func NewAdaptationField() *AdaptationField {
af := new(AdaptationField)
af.buf = make([]byte, 0, tsPacketSize)
af.privateDataByte = make([]byte, 0, tsPacketSize)
return af
}

// 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.transportPrivateDataFlag = 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 +142,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 +235,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
29 changes: 19 additions & 10 deletions tsparser/adaptation_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@ func TestNewAdaptationField(t *testing.T) {
}
}

func TestAdaptationFieldnitialize(t *testing.T) {
var prevPcr uint64 = 0x1FFFFFFF
func TestAdaptationFieldInitialize(t *testing.T) {
var options options.Options
options.SetDumpHeader(true)
af := NewAdaptationField()
af.Initialize(1, &prevPcr, options)
af1 := NewAdaptationField()
af1.Initialize(1, options)

if af.pos != 1 {
t.Errorf("actual: 1, But got %s", af.pos)
}
if *af.prevPcr != 0x1FFFFFFF {
t.Errorf("actual: 0x1FFFFFFF, But got %s", *af.prevPcr)
if af1.pos != 1 {
t.Errorf("actual: 1, But got %s", af1.pos)
}
if !af.options.DumpHeader() {
if !af1.options.DumpHeader() {
t.Errorf("actual: true, But got false")
}

data := []byte{0x07, 0x70, 0x00, 0x00, 0x00, 0x42, 0x7E, 0x6F}

af2 := NewAdaptationField()
af2.Append(data)
if len, err := af2.Parse(); len != 7 || err != nil {
t.Errorf("Parse error: %s", err)
}
af2.Initialize(1, options)

if !reflect.DeepEqual(af1, af2) {
t.Errorf("Failed Initialize. Different in af1 and af2")
}
}

func TestAdaptationFieldAppend(t *testing.T) {
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
1 change: 1 addition & 0 deletions tsparser/mpeg_packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tsparser
60 changes: 56 additions & 4 deletions tsparser/pes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Pes struct {
nextPcr uint64
prevPcrPos int64
nextPcrPos int64
notPrintTimestamp bool

packetStartCodePrefix uint32
streamID uint8
Expand All @@ -30,7 +29,7 @@ type Pes struct {
escrFlag uint8
esRateFlag uint8
dsmTrickModeFlag uint8
additionalCopyIntoFlag uint8
additionalCopyInfoFlag uint8
pesCrcFlag uint8
pesExtentionFlag uint8
pesHeaderDataLength uint8
Expand Down Expand Up @@ -69,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 Expand Up @@ -130,7 +182,7 @@ func (p *Pes) Parse() error {
if p.dsmTrickModeFlag, err = bb.PeekUint8(1); err != nil {
return err
}
if p.additionalCopyIntoFlag, err = bb.PeekUint8(1); err != nil {
if p.additionalCopyInfoFlag, err = bb.PeekUint8(1); err != nil {
return err
}
if p.pesCrcFlag, err = bb.PeekUint8(1); err != nil {
Expand Down Expand Up @@ -281,7 +333,7 @@ func (p *Pes) Parse() error {
} // reserved
}
}
if p.additionalCopyIntoFlag == 1 {
if p.additionalCopyInfoFlag == 1 {
if err = bb.Skip(1); err != nil {
return err
} // marker_bit
Expand Down
Loading

0 comments on commit d71a997

Please sign in to comment.