Skip to content

Commit

Permalink
Improve dump options
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuuram committed Feb 1, 2019
1 parent af07844 commit e9ee94f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Flags:
--dump-ts-payload Dump TS packet payload binary.
--dump-adaptation-field Dump TS packet adaptation_field detail.
--dump-psi Dump PSI(PAT/PMT) detail.
-n, --not-dump-timestamp Not Dump PCR/PTS/DTS timestamps.
-t, --dump-timestamp Dump PCR/PTS/DTS timestamps.
Args:
<input> Input file name.
Expand Down
32 changes: 25 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@ import (

const tsPacketSize = 188

var (
dumpHeader = kingpin.Flag("dump-ts-header", "Dump TS packet header.").Bool()
dumpPayload = kingpin.Flag("dump-ts-payload", "Dump TS packet payload binary.").Bool()
dumpAdaptationField = kingpin.Flag("dump-adaptation-field", "Dump TS packet adaptation_field detail.").Bool()
dumpPsi = kingpin.Flag("dump-psi", "Dump PSI(PAT/PMT) detail.").Bool()
dumpPesHeader = kingpin.Flag("dump-pes-header", "Dump PES packet header detail.").Bool()
dumpTimestamp = kingpin.Flag("dump-timestamp", "Dump PCR/PTS/DTS timestamps.").Short('t').Bool()
)

func main() {
var options options.Options
filename := kingpin.Arg("input", "Input file name.").Required().String()
options.SetDumpHeader(*kingpin.Flag("dump-ts-header", "Dump TS packet header.").Bool())
options.SetDumpPayload(*kingpin.Flag("dump-ts-payload", "Dump TS packet payload binary.").Bool())
options.SetDumpAdaptationField(*kingpin.Flag("dump-adaptation-field", "Dump TS packet adaptation_field detail.").Bool())
options.SetDumpPsi(*kingpin.Flag("dump-psi", "Dump PSI(PAT/PMT) detail.").Bool())
options.SetNotDumpTimestamp(*kingpin.Flag("not-dump-timestamp", "Not Dump PCR/PTS/DTS timestamps.").Short('n').Bool())
kingpin.Parse()

var options options.Options
options.SetDumpHeader(*dumpHeader)
options.SetDumpPayload(*dumpPayload)
options.SetDumpAdaptationField(*dumpAdaptationField)
options.SetDumpPsi(*dumpPsi)
options.SetDumpPesHeader(*dumpPesHeader)
options.SetDumpTimestamp(*dumpTimestamp)

if err := parseTsFile(*filename, options); err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -68,6 +79,9 @@ func parseTsFile(filename string, options options.Options) error {
return fmt.Errorf("File seek error: %s", err)
}
fmt.Printf("Detected PAT: PMT pid = 0x%02x\n", pmtPid)
if options.DumpPsi() {
pat.Dump()
}

// Parse PMT
err = tsparser.BufferPsi(file, &pos, pmtPid, pmt, options)
Expand All @@ -82,7 +96,11 @@ func parseTsFile(filename string, options options.Options) error {
return fmt.Errorf("File seek error: %s", err)
}
fmt.Println("Detected PMT")
pmt.DumpProgramInfos()
if options.DumpPsi() {
pmt.Dump()
} else {
pmt.DumpProgramInfos()
}

err = tsparser.BufferPes(file, &pos, pcrPid, programs, options)
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type Options struct {
dumpPayload bool
dumpAdaptationField bool
dumpPsi bool
notDumpTimestamp bool
dumpPesHeader bool
dumpTimestamp bool
}

// DumpHeader return flag data "--dump-ts-header"
Expand All @@ -21,8 +22,11 @@ func (o *Options) DumpAdaptationField() bool { return o.dumpAdaptationField }
// DumpPsi return flag data "--dump-psi"
func (o *Options) DumpPsi() bool { return o.dumpPsi }

// DumpPesHeader return flag data "--dump-pes-header"
func (o *Options) DumpPesHeader() bool { return o.dumpPesHeader }

// NotDumpTimestamp return flag data "--not-dump-timestamp"
func (o *Options) NotDumpTimestamp() bool { return o.notDumpTimestamp }
func (o *Options) DumpTimestamp() bool { return o.dumpTimestamp }

// SetDumpHeader set value to "--dump-ts-header"
func (o *Options) SetDumpHeader(v bool) { o.dumpHeader = v }
Expand All @@ -36,5 +40,8 @@ func (o *Options) SetDumpAdaptationField(v bool) { o.dumpAdaptationField = v }
// SetDumpPsi set value to "--dump-psi"
func (o *Options) SetDumpPsi(v bool) { o.dumpPsi = v }

// SetDumpPesHeader set value to "--dump-pes-header"
func (o *Options) SetDumpPesHeader(v bool) { o.dumpPesHeader = v }

// SetNotDumpTimestamp set value to "--not-dump-timestamp"
func (o *Options) SetNotDumpTimestamp(v bool) { o.notDumpTimestamp = v }
func (o *Options) SetDumpTimestamp(v bool) { o.dumpTimestamp = v }
16 changes: 11 additions & 5 deletions tsparser/mpeg_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramI
pid := tsPacket.Pid()
pes, exist := pesMap[pid]
if tsPacket.HasAf() && tsPacket.adaptationField.PcrFlag() && pcrPid != 0 && pid == pcrPid {
if !options.NotDumpTimestamp() {
if options.DumpTimestamp() {
tsPacket.adaptationField.DumpPcr(lastPcr)
}
maxPcrInterval = math.Max(maxPcrInterval, float64(tsPacket.Pcr()-lastPcr))
Expand All @@ -110,10 +110,13 @@ func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramI
if tsPacket.PayloadUnitStartIndicator() {
if pes != nil {
pes.Parse()
if !options.NotDumpTimestamp() {
if options.DumpTimestamp() {
pcrDelay := pes.DumpTimestamp()
maxDelay = math.Max(maxDelay, pcrDelay)
}
if options.DumpPesHeader() {
pes.DumpHeader()
}
} else {
pes = NewPes()
pesMap[pid] = pes
Expand All @@ -135,9 +138,12 @@ func BufferPes(file *os.File, pos *int64, pcrPid uint16, programInfos []ProgramI

*pos += int64(size)
}
fmt.Println("-----------------------------")
fmt.Printf("Max PCR interval: %fms\n", maxPcrInterval/300/90)
fmt.Printf("PCR-PTS max gap: %fms\n", maxDelay)
if options.DumpTimestamp() {
fmt.Println("-----------------------------")
fmt.Printf("Max PCR interval: %fms\n", maxPcrInterval/300/90)
fmt.Printf("PCR-PTS max gap: %fms\n", maxDelay)
}

return nil
}

Expand Down
49 changes: 45 additions & 4 deletions tsparser/pes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tsparser

import (
"fmt"

"github.com/small-teton/MpegTsAnalyzer/bitbuffer"
)

Expand Down Expand Up @@ -359,7 +358,7 @@ func (p *Pes) DumpTimestamp() float64 {
fmt.Printf("0x%08x PTS: 0x%08x[%012fms] (pid:0x%02x) (delay:%fms)\n", p.pos, p.pts, float32(p.pts)/90, p.pid, pcrDelay)
}
if p.ptsDtsFlags == 3 {
fmt.Printf("0x%08x PTS: 0x%08x[%012fms] (pid:0x%02x)\n", p.pos, p.pts, float32(p.pts)/90, p.pid)
//fmt.Printf("0x%08x PTS: 0x%08x[%012fms] (pid:0x%02x)\n", p.pos, p.pts, float32(p.pts)/90, p.pid)
prevPcr := float32(p.prevPcr) / 300 / 90
nextPcr := float32(p.nextPcr) / 300 / 90
pcrDelay = float32(p.dts)/90 - (prevPcr + (nextPcr-prevPcr)*(float32(p.pos-p.prevPcrPos)/float32(p.nextPcrPos-p.prevPcrPos)))
Expand All @@ -369,6 +368,48 @@ func (p *Pes) DumpTimestamp() float64 {
}

// Dump PES header detail.
func (p *Pes) Dump() {

func (p *Pes) DumpHeader() {
fmt.Printf("\n===========================================\n")
fmt.Printf(" PES")
fmt.Printf("\n===========================================\n")
fmt.Printf("PES : packet_start_code_prefix : %d\n", p.packetStartCodePrefix)
fmt.Printf("PES : stream_id : %d\n", p.streamID)
fmt.Printf("PES : pes_packet_length : %d\n", p.pesPacketLength)
fmt.Printf("PES : pes_scrambling_control : %d\n", p.pesScramblingControl)
fmt.Printf("PES : pes_priority : %d\n", p.pesPriority)
fmt.Printf("PES : data_alignment_indicator : %d\n", p.dataAlignmentIndicator)
fmt.Printf("PES : copyright : %d\n", p.copyright)
fmt.Printf("PES : original_or_copy : %d\n", p.originalOrCopy)
fmt.Printf("PES : pts_dts_flags : %d\n", p.ptsDtsFlags)
fmt.Printf("PES : escr_flag : %d\n", p.escrFlag)
fmt.Printf("PES : es_rate_flag : %d\n", p.esRateFlag)
fmt.Printf("PES : dsm_trick_mode_flag : %d\n", p.dsmTrickModeFlag)
fmt.Printf("PES : additional_copy_info_flag : %d\n", p.additionalCopyInfoFlag)
fmt.Printf("PES : pes_crc_flag : %d\n", p.pesCrcFlag)
fmt.Printf("PES : pes_extention_flag : %d\n", p.pesExtentionFlag)
fmt.Printf("PES : pes_header_data_length : %d\n", p.pesHeaderDataLength)
fmt.Printf("PES : pts : %d\n", p.pts)
fmt.Printf("PES : dts : %d\n", p.dts)
fmt.Printf("PES : escr : %d\n", p.escr)
fmt.Printf("PES : escr_base : %d\n", p.escrBase)
fmt.Printf("PES : escr_extention : %d\n", p.escrExtention)
fmt.Printf("PES : es_rate : %d\n", p.esRate)
fmt.Printf("PES : trick_mode_control : %d\n", p.trickModeControl)
fmt.Printf("PES : field_id : %d\n", p.fieldID)
fmt.Printf("PES : intra_slice_refresh : %d\n", p.intraSliceRefresh)
fmt.Printf("PES : frequency_truncation : %d\n", p.frequencyTruncation)
fmt.Printf("PES : rep_cntrl : %d\n", p.repCntrl)
fmt.Printf("PES : additional_copy_info : %d\n", p.additionalCopyInfo)
fmt.Printf("PES : previous_pes_packet_crc : %d\n", p.previousPesPacketCrc)
fmt.Printf("PES : pes_private_data_flag : %d\n", p.pesPrivateDataFlag)
fmt.Printf("PES : pack_header_field_flag : %d\n", p.packHeaderFieldFlag)
fmt.Printf("PES : program_packet_sequence_counter_flag : %d\n", p.programPacketSequenceCounterFlag)
fmt.Printf("PES : p-std_buffer_flag : %d\n", p.pStdBufferFlag)
fmt.Printf("PES : pes_extention_flag2 : %d\n", p.pesExtentionFlag2)
fmt.Printf("PES : program_packet_sequence_counter : %d\n", p.programPacketSequenceCounter)
fmt.Printf("PES : mpeg1_mpeg2_identifer : %d\n", p.mpeg1Mpeg2Identifer)
fmt.Printf("PES : original_stuff_length : %d\n", p.originalStuffLength)
fmt.Printf("PES : p-std_buffer_scale : %d\n", p.pStdBufferScale)
fmt.Printf("PES : p-std_buffer_size : %d\n", p.pStdBufferSize)
fmt.Printf("PES : pes_extention_field_length : %d\n", p.pesExtentionFieldLength)
}
10 changes: 9 additions & 1 deletion tsparser/ts_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ func (tp *TsPacket) Parse() error {
if tp.adaptationFieldControl == 3 {
tp.payload = tp.buf[tsHeaderSize+afLength+1:]
}

if tp.options.DumpHeader() {
tp.DumpHeader()
}
if tp.options.DumpPayload() {
tp.DumpPayload()
}

return nil
}

Expand Down Expand Up @@ -152,7 +160,7 @@ func (tp *TsPacket) DumpHeader() {
}

// DumpData print this TsPacket payload binary.
func (tp *TsPacket) DumpData() {
func (tp *TsPacket) DumpPayload() {
fmt.Printf("===============================================================\n")
fmt.Printf(" Dump TS Data\n")
fmt.Printf("===============================================================\n")
Expand Down

0 comments on commit e9ee94f

Please sign in to comment.