2019-01-24 14:59:43 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-01-25 08:40:13 +03:00
|
|
|
"errors"
|
2019-01-24 14:59:43 +03:00
|
|
|
"flag"
|
2019-01-25 07:33:57 +03:00
|
|
|
"fmt"
|
2019-01-24 14:59:43 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/stream/mts"
|
|
|
|
"github.com/Comcast/gots/packet"
|
|
|
|
)
|
|
|
|
|
2019-02-07 16:44:22 +03:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// Various errors that we can encounter.
|
2019-01-24 14:59:43 +03:00
|
|
|
const (
|
2019-01-25 08:40:13 +03:00
|
|
|
errBadInPath = "No file path provided, or file does not exist"
|
|
|
|
errCantCreateOut = "Can't create output file"
|
|
|
|
errCantGetPid = "Can't get pid from packet"
|
|
|
|
errReadFail = "Read failed"
|
|
|
|
errWriteFail = "Write to file failed"
|
|
|
|
errBadMode = "Bad fix mode"
|
|
|
|
errAdaptationPresent = "Adaptation field is already present in packet"
|
|
|
|
errNoAdaptationField = "No adaptation field in this packet"
|
2019-01-25 07:33:57 +03:00
|
|
|
)
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// Consts describing flag usage.
|
2019-01-25 07:33:57 +03:00
|
|
|
const (
|
|
|
|
inUsage = "The path to the file to be repaired"
|
|
|
|
outUsage = "Output file path"
|
|
|
|
modeUsage = "Fix mode: 0 = cc-shift, 1 = di-update"
|
|
|
|
)
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// Repair modes.
|
2019-01-25 07:33:57 +03:00
|
|
|
const (
|
|
|
|
ccShift = iota
|
|
|
|
diUpdate
|
2019-01-24 14:59:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var ccMap = map[int]byte{
|
2019-02-07 16:44:22 +03:00
|
|
|
PatPid: 16,
|
|
|
|
PmtPid: 16,
|
|
|
|
VideoPid: 16,
|
2019-01-24 14:59:43 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// packetNo will keep track of the ts packet number for reference.
|
2019-01-25 07:33:57 +03:00
|
|
|
var packetNo int
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// Option defines a func that performs an action on p in order to change a ts option.
|
2019-01-25 07:33:57 +03:00
|
|
|
type Option func(p *Packet)
|
|
|
|
|
2019-02-07 16:44:22 +03:00
|
|
|
// 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.
|
2019-01-24 14:59:43 +03:00
|
|
|
type Packet [mts.PacketSize]byte
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// CC returns the CC of p.
|
2019-01-25 07:33:57 +03:00
|
|
|
func (p *Packet) CC() byte {
|
|
|
|
return (*p)[3] & 0x0f
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// setCC sets the CC of p.
|
2019-01-24 14:59:43 +03:00
|
|
|
func (p *Packet) setCC(cc byte) {
|
|
|
|
(*p)[3] |= cc & 0xf
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// setDI sets the discontinuity counter of p.
|
2019-01-25 07:33:57 +03:00
|
|
|
func (p *Packet) setDI(di bool) {
|
|
|
|
if di {
|
|
|
|
p[5] |= 0x80
|
|
|
|
} else {
|
|
|
|
p[5] &= 0x7f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// addAdaptationField adds an adaptation field to p, and applys the passed options to this field.
|
|
|
|
// TODO: this will probably break if we already have adaptation field.
|
|
|
|
func (p *Packet) addAdaptationField(options ...Option) error {
|
|
|
|
if p.hasAdaptation() {
|
|
|
|
return errors.New(errAdaptationPresent)
|
|
|
|
}
|
|
|
|
// Create space for adaptation field.
|
2019-02-07 16:44:22 +03:00
|
|
|
copy(p[HeadSize+DefaultAdaptationSize:], p[HeadSize:len(p)-DefaultAdaptationSize])
|
2019-01-25 07:33:57 +03:00
|
|
|
|
|
|
|
// TODO: seperate into own function
|
2019-01-25 08:40:13 +03:00
|
|
|
// Update adaptation field control.
|
2019-02-07 16:44:22 +03:00
|
|
|
p[AdaptationControlIdx] &= 0xff ^ AdaptationControlMask
|
|
|
|
p[AdaptationControlIdx] |= AdaptationControlMask
|
2019-01-25 08:40:13 +03:00
|
|
|
// Default the adaptationfield.
|
2019-01-25 07:33:57 +03:00
|
|
|
p.resetAdaptation()
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// Apply and options that have bee passed.
|
2019-01-25 07:33:57 +03:00
|
|
|
for _, option := range options {
|
|
|
|
option(p)
|
|
|
|
}
|
2019-01-25 08:40:13 +03:00
|
|
|
return nil
|
2019-01-25 07:33:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// resetAdaptation sets fields in ps adaptation field to 0 if the adaptation field
|
|
|
|
// exists, otherwise an error is returned.
|
|
|
|
func (p *Packet) resetAdaptation() error {
|
|
|
|
if !p.hasAdaptation() {
|
|
|
|
return errors.New(errNoAdaptationField)
|
|
|
|
}
|
2019-02-07 16:44:22 +03:00
|
|
|
p[AdaptationIdx] = DefaultAdaptationBodySize
|
|
|
|
p[AdaptationBodyIdx] = 0x00
|
2019-01-25 08:40:13 +03:00
|
|
|
return nil
|
2019-01-25 07:33:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// hasAdaptation returns true if p has an adaptation field and false otherwise.
|
2019-01-25 07:33:57 +03:00
|
|
|
func (p *Packet) hasAdaptation() bool {
|
2019-02-07 16:44:22 +03:00
|
|
|
afc := p[AdaptationControlIdx] & AdaptationControlMask
|
2019-01-25 07:33:57 +03:00
|
|
|
if afc == 0x20 || afc == 0x30 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// DiscontinuityIndicator returns and Option that will set p's discontinuity
|
|
|
|
// indicator according to f.
|
2019-01-25 07:33:57 +03:00
|
|
|
func DiscontinuityIndicator(f bool) Option {
|
|
|
|
return func(p *Packet) {
|
2019-02-07 16:44:22 +03:00
|
|
|
set := byte(DiscontinuityIndicatorMask)
|
2019-01-25 07:33:57 +03:00
|
|
|
if !f {
|
|
|
|
set = 0x00
|
|
|
|
}
|
2019-02-07 16:44:22 +03:00
|
|
|
p[DiscontinuityIndicatorIdx] &= 0xff ^ DiscontinuityIndicatorMask
|
|
|
|
p[DiscontinuityIndicatorIdx] |= DiscontinuityIndicatorMask & set
|
2019-01-25 07:33:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 14:59:43 +03:00
|
|
|
func main() {
|
|
|
|
// Deal with input flags
|
2019-01-25 07:33:57 +03:00
|
|
|
inPtr := flag.String("in", "", inUsage)
|
|
|
|
outPtr := flag.String("out", "out.ts", outUsage)
|
|
|
|
modePtr := flag.Int("mode", diUpdate, modeUsage)
|
2019-01-24 14:59:43 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Try and open the given input file, otherwise panic - we can't do anything
|
|
|
|
inFile, err := os.Open(*inPtr)
|
|
|
|
defer inFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(errBadInPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try and create output file, otherwise panic - we can't do anything
|
|
|
|
outFile, err := os.Create(*outPtr)
|
|
|
|
defer outFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(errCantCreateOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read each packet from the input file reader
|
|
|
|
var p Packet
|
|
|
|
for {
|
|
|
|
// If we get an end of file then return, otherwise we panic - can't do anything else
|
|
|
|
if _, err := inFile.Read(p[:mts.PacketSize]); err == io.EOF {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
panic(errReadFail + ": " + err.Error())
|
|
|
|
}
|
2019-01-25 07:33:57 +03:00
|
|
|
packetNo++
|
2019-01-25 08:40:13 +03:00
|
|
|
|
|
|
|
// Get the pid from the packet
|
2019-01-24 14:59:43 +03:00
|
|
|
pid, err := packet.Pid((*packet.Packet)(&p))
|
|
|
|
if err != nil {
|
|
|
|
panic(errCantGetPid)
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// Get the cc from the packet and also the expected cc (if exists)
|
2019-01-25 07:33:57 +03:00
|
|
|
cc := p.CC()
|
|
|
|
expect, exists := expectedCC(int(pid))
|
|
|
|
if !exists {
|
|
|
|
updateCCMap(int(pid), cc)
|
|
|
|
} else {
|
|
|
|
switch *modePtr {
|
2019-01-25 08:40:13 +03:00
|
|
|
// ccShift mode shifts all CC regardless of presence of Discontinuities or not
|
2019-01-25 07:33:57 +03:00
|
|
|
case ccShift:
|
|
|
|
p.setCC(expect)
|
2019-01-25 08:40:13 +03:00
|
|
|
// diUpdate mode finds discontinuities and sets the discontinuity indicator to true.
|
|
|
|
// If we have a pat or pmt then we need to add an adaptation field and then set the DI.
|
2019-01-25 07:33:57 +03:00
|
|
|
case diUpdate:
|
|
|
|
if cc != expect {
|
2019-01-25 07:48:27 +03:00
|
|
|
fmt.Printf("***** Discontinuity found (packetNo: %v pid: %v, cc: %v, expect: %v)\n", packetNo, pid, cc, expect)
|
2019-01-25 07:33:57 +03:00
|
|
|
if p.hasAdaptation() {
|
|
|
|
p.setDI(true)
|
|
|
|
} else {
|
|
|
|
p.addAdaptationField(DiscontinuityIndicator(true))
|
|
|
|
}
|
|
|
|
updateCCMap(int(pid), p.CC())
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(errBadMode)
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 08:40:13 +03:00
|
|
|
|
|
|
|
// Write this packet to the output file.
|
2019-01-24 14:59:43 +03:00
|
|
|
if _, err := outFile.Write(p[:]); err != nil {
|
|
|
|
panic(errWriteFail + ": " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// expectedCC returns the expected cc for the given pid. If the cc hasn't been
|
|
|
|
// used yet, then 16 and false is returned.
|
2019-01-25 07:33:57 +03:00
|
|
|
func expectedCC(pid int) (byte, bool) {
|
2019-01-24 14:59:43 +03:00
|
|
|
cc := ccMap[pid]
|
2019-01-25 07:33:57 +03:00
|
|
|
if cc == 16 {
|
|
|
|
return 16, false
|
|
|
|
}
|
|
|
|
ccMap[pid] = (cc + 1) & 0xf
|
|
|
|
return cc, true
|
|
|
|
}
|
|
|
|
|
2019-01-25 08:40:13 +03:00
|
|
|
// updateCCMap updates the cc for the passed pid.
|
2019-01-25 07:33:57 +03:00
|
|
|
func updateCCMap(pid int, cc byte) {
|
2019-01-24 14:59:43 +03:00
|
|
|
ccMap[pid] = (cc + 1) & 0xf
|
|
|
|
}
|