2018-12-12 02:48:05 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
psi.go
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
psi.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2018-12-04 08:33:04 +03:00
|
|
|
package psi
|
|
|
|
|
2018-12-06 04:46:26 +03:00
|
|
|
import (
|
|
|
|
"hash/crc32"
|
|
|
|
"math/bits"
|
|
|
|
)
|
|
|
|
|
2018-12-11 09:46:26 +03:00
|
|
|
// Lengths of section definitions
|
2018-12-05 14:16:06 +03:00
|
|
|
const (
|
2018-12-05 17:11:53 +03:00
|
|
|
ESSDDefLen = 5
|
|
|
|
DescDefLen = 2
|
|
|
|
PMTDefLen = 4
|
|
|
|
PATLen = 4
|
2018-12-05 17:58:14 +03:00
|
|
|
TSSDefLen = 5
|
|
|
|
PSIDefLen = 3
|
2018-12-05 14:16:06 +03:00
|
|
|
)
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Table Type IDs
|
|
|
|
const (
|
|
|
|
PATTableID = 0x00
|
|
|
|
PMTTableID = 0x02
|
|
|
|
)
|
|
|
|
|
2018-12-11 09:46:26 +03:00
|
|
|
// Consts relating to time description
|
2018-12-06 08:32:40 +03:00
|
|
|
const (
|
2018-12-12 09:32:03 +03:00
|
|
|
timeDescTag = 234
|
|
|
|
timeTagIndx = 13
|
|
|
|
timeDataIndx = 15
|
|
|
|
timeDataSize = 8
|
2018-12-12 08:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Consts relating to gps description
|
|
|
|
const (
|
2018-12-12 09:32:03 +03:00
|
|
|
gpsDescTag = 235
|
|
|
|
gpsTagIndx = 23
|
|
|
|
gpsDataIndx = 25
|
|
|
|
gpsDataSize = 32 // bytes
|
2018-12-06 08:32:40 +03:00
|
|
|
)
|
|
|
|
|
2018-12-04 08:33:04 +03:00
|
|
|
// Program specific information
|
|
|
|
type PSI struct {
|
2018-12-06 06:20:17 +03:00
|
|
|
Pf byte // Point field
|
|
|
|
Pfb []byte // Pointer filler bytes
|
|
|
|
Tid byte // Table ID
|
|
|
|
Ssi bool // Section syntax indicator (1 for PAT, PMT, CAT)
|
|
|
|
Pb bool // Private bit (0 for PAT, PMT, CAT)
|
|
|
|
Sl uint16 // Section length
|
|
|
|
Tss *TSS // Table syntax section (length defined by SL) if length 0 then nil
|
|
|
|
Crc uint32 // crc32 of entire table excluding pointer field, pointer filler bytes and the trailing CRC32
|
2018-12-04 08:33:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Table syntax section
|
|
|
|
type TSS struct {
|
2018-12-06 06:20:17 +03:00
|
|
|
Tide uint16 // Table ID extension
|
|
|
|
V byte // Version number
|
|
|
|
Cni bool // Current/next indicator
|
|
|
|
Sn byte // Section number
|
|
|
|
Lsn byte // Last section number
|
|
|
|
Sd SD // Specific data PAT/PMT
|
2018-12-04 08:33:04 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 12:01:29 +03:00
|
|
|
// Specific Data, (could be PAT or PMT)
|
2018-12-04 08:33:04 +03:00
|
|
|
type SD interface {
|
2018-12-05 12:01:29 +03:00
|
|
|
Bytes() []byte
|
2018-12-04 08:33:04 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 12:01:29 +03:00
|
|
|
// Program association table, implements SD
|
|
|
|
type PAT struct {
|
2018-12-06 06:20:17 +03:00
|
|
|
Pn uint16 // Program Number
|
|
|
|
Pmpid uint16 // Program map PID
|
2018-12-05 14:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Program mapping table, implements SD
|
|
|
|
type PMT struct {
|
2018-12-06 06:20:17 +03:00
|
|
|
Pcrpid uint16 // Program clock reference pid
|
|
|
|
Pil uint16 // Program info length
|
|
|
|
Pd []Desc // Program descriptors
|
2018-12-07 08:23:38 +03:00
|
|
|
Essd *ESSD // Elementary stream specific data
|
2018-12-04 08:33:04 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 14:16:06 +03:00
|
|
|
// Elementary stream specific data
|
|
|
|
type ESSD struct {
|
2018-12-06 06:20:17 +03:00
|
|
|
St byte // Stream type
|
|
|
|
Epid uint16 // Elementary pid
|
|
|
|
Esil uint16 // Elementary stream
|
|
|
|
Esd []Desc // Elementary stream desriptors
|
2018-12-05 14:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Descriptor
|
|
|
|
type Desc struct {
|
2018-12-06 06:20:17 +03:00
|
|
|
Dt byte // Descriptor tag
|
|
|
|
Dl byte // Descriptor length
|
|
|
|
Dd []byte // Descriptor data
|
2018-12-05 12:01:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// ReadPSI creates a PSI data structure from a given byte slice that represents a PSI
|
2018-12-05 14:16:06 +03:00
|
|
|
func ReadPSI(data []byte) *PSI {
|
2018-12-06 06:20:17 +03:00
|
|
|
psi := PSI{}
|
|
|
|
pos := 0
|
|
|
|
psi.Pf = data[pos]
|
|
|
|
if psi.Pf != 0 {
|
2018-12-11 07:22:18 +03:00
|
|
|
panic("No support for pointer filler bytes")
|
2018-12-06 06:20:17 +03:00
|
|
|
}
|
|
|
|
psi.Tid = data[pos]
|
|
|
|
pos++
|
|
|
|
psi.Ssi = byteToBool(data[pos] & 0x80)
|
|
|
|
psi.Pb = byteToBool(data[pos] & 0x40)
|
|
|
|
psi.Sl = uint16(data[pos]&0x03)<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
psi.Tss = readTSS(data[pos:], &psi)
|
|
|
|
return &psi
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadTSS creates a TSS data structure from a given byte slice that represents a TSS
|
|
|
|
func readTSS(data []byte, p *PSI) *TSS {
|
|
|
|
tss := TSS{}
|
|
|
|
pos := 0
|
|
|
|
tss.Tide = uint16(data[pos])<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
tss.V = (data[pos] & 0x3e) >> 1
|
|
|
|
tss.Cni = byteToBool(data[pos] & 0x01)
|
|
|
|
pos++
|
|
|
|
tss.Sn = data[pos]
|
|
|
|
pos++
|
|
|
|
tss.Lsn = data[pos]
|
|
|
|
pos++
|
2018-12-06 08:32:40 +03:00
|
|
|
switch p.Tid {
|
|
|
|
case PATTableID:
|
|
|
|
tss.Sd = readPAT(data[pos:], &tss)
|
|
|
|
case PMTTableID:
|
|
|
|
tss.Sd = readPMT(data[pos:], &tss)
|
|
|
|
default:
|
2018-12-06 06:20:17 +03:00
|
|
|
panic("Can't yet deal with tables that are not PAT or PMT")
|
|
|
|
}
|
2018-12-06 08:32:40 +03:00
|
|
|
return &tss
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPAT(data []byte, p *TSS) *PAT {
|
|
|
|
pat := PAT{}
|
|
|
|
pos := 0
|
|
|
|
pat.Pn = uint16(data[pos])<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
pat.Pmpid = uint16(data[pos]&0x1f)<<8 | uint16(data[pos+1])
|
|
|
|
return &pat
|
|
|
|
}
|
2018-12-06 06:20:17 +03:00
|
|
|
|
2018-12-06 08:32:40 +03:00
|
|
|
func readPMT(data []byte, p *TSS) *PAT {
|
|
|
|
pmt := PMT{}
|
|
|
|
pos := 0
|
|
|
|
pmt.Pcrpid = uint16(data[pos]&0x1f)<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
pmt.Pil = uint16(data[pos]&0x03)<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
if pmt.Pil != 0 {
|
|
|
|
pmt.Pd = readDescs(data[pos:], int(pmt.Pil))
|
|
|
|
}
|
|
|
|
pos += int(pmt.Pil)
|
|
|
|
// TODO Read ES stuff
|
|
|
|
pmt.Essd = readEssd(data[pos:])
|
2018-12-05 14:16:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 08:32:40 +03:00
|
|
|
// readDescs reads provides a slice of Descs given a byte slice that represents Descs
|
|
|
|
// and the no of bytes that the descs accumilate
|
|
|
|
func readDescs(data []byte, descLen int) (o []Desc) {
|
|
|
|
pos := 0
|
|
|
|
o = make([]Desc, 1)
|
|
|
|
o[0].Dt = data[pos]
|
|
|
|
pos++
|
|
|
|
o[0].Dl = data[pos]
|
|
|
|
pos++
|
|
|
|
o[0].Dd = make([]byte, o[0].Dl)
|
|
|
|
for i := 0; i < int(o[0].Dl); i++ {
|
|
|
|
o[0].Dd[i] = data[pos]
|
|
|
|
pos++
|
|
|
|
}
|
|
|
|
if 2+len(o[0].Dd) != descLen {
|
|
|
|
panic("No support for reading more than one descriptor")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func readEssd(data []byte) *ESSD {
|
|
|
|
essd := ESSD{}
|
|
|
|
pos := 0
|
|
|
|
essd.St = data[pos]
|
2018-12-07 08:23:38 +03:00
|
|
|
pos++
|
|
|
|
essd.Epid = uint16(data[pos]&0x1f)<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
essd.Esil = uint16(data[pos]&0x03)<<8 | uint16(data[pos+1])
|
|
|
|
pos += 2
|
|
|
|
essd.Esd = readDescs(data[pos:], int(essd.Esil))
|
|
|
|
return &essd
|
2018-12-06 08:32:40 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Bytes outputs a byte slice representation of the PSI
|
2018-12-05 17:58:14 +03:00
|
|
|
func (p *PSI) Bytes() []byte {
|
2018-12-11 07:22:18 +03:00
|
|
|
out := make([]byte, 4)
|
2018-12-06 06:20:17 +03:00
|
|
|
out[0] = p.Pf
|
2018-12-11 07:22:18 +03:00
|
|
|
if p.Pf != 0 {
|
|
|
|
panic("No support for pointer filler bytes")
|
2018-12-05 17:58:14 +03:00
|
|
|
}
|
2018-12-11 07:22:18 +03:00
|
|
|
out[1] = p.Tid
|
|
|
|
out[2] = 0x80 | 0x30 | (0x03 & byte(p.Sl>>8))
|
|
|
|
out[3] = byte(p.Sl)
|
2018-12-06 06:20:17 +03:00
|
|
|
out = append(out, p.Tss.Bytes()...)
|
2018-12-11 07:22:18 +03:00
|
|
|
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[3:])
|
2018-12-06 04:46:26 +03:00
|
|
|
out = append(out, make([]byte, 0, 4)...)
|
|
|
|
out = append(out, byte(crc32>>24))
|
|
|
|
out = append(out, byte(crc32>>16))
|
|
|
|
out = append(out, byte(crc32>>8))
|
|
|
|
out = append(out, byte(crc32))
|
|
|
|
return out
|
2018-12-05 12:01:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Bytes outputs a byte slice representation of the TSS
|
2018-12-05 17:58:14 +03:00
|
|
|
func (t *TSS) Bytes() []byte {
|
|
|
|
out := make([]byte, TSSDefLen)
|
2018-12-06 06:20:17 +03:00
|
|
|
out[0] = byte(t.Tide >> 8)
|
|
|
|
out[1] = byte(t.Tide)
|
|
|
|
out[2] = 0xc0 | (0x3e & (t.V << 1)) | (0x01 & boolToByte(t.Cni))
|
|
|
|
out[3] = t.Sn
|
|
|
|
out[4] = t.Lsn
|
|
|
|
out = append(out, t.Sd.Bytes()...)
|
2018-12-05 17:58:14 +03:00
|
|
|
return out
|
2018-12-05 12:01:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Bytes outputs a byte slice representation of the PAT
|
2018-12-05 17:11:53 +03:00
|
|
|
func (p *PAT) Bytes() []byte {
|
|
|
|
out := make([]byte, PATLen)
|
2018-12-06 06:20:17 +03:00
|
|
|
out[0] = byte(p.Pn >> 8)
|
|
|
|
out[1] = byte(p.Pn)
|
|
|
|
out[2] = 0xe0 | (0x1f & byte(p.Pmpid>>8))
|
|
|
|
out[3] = byte(p.Pmpid)
|
2018-12-05 17:11:53 +03:00
|
|
|
return out
|
2018-12-05 12:01:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Bytes outputs a byte slice representation of the PMT
|
2018-12-05 17:04:29 +03:00
|
|
|
func (p *PMT) Bytes() []byte {
|
2018-12-05 17:11:53 +03:00
|
|
|
out := make([]byte, PMTDefLen)
|
2018-12-11 08:32:57 +03:00
|
|
|
out[0] = 0xe0 | (0x1f & byte(p.Pcrpid>>8)) // byte 10
|
2018-12-06 06:20:17 +03:00
|
|
|
out[1] = byte(p.Pcrpid)
|
|
|
|
out[2] = 0xf0 | (0x03 & byte(p.Pil>>8))
|
|
|
|
out[3] = byte(p.Pil)
|
|
|
|
for _, d := range p.Pd {
|
2018-12-05 17:04:29 +03:00
|
|
|
out = append(out, d.Bytes()...)
|
|
|
|
}
|
2018-12-06 08:32:40 +03:00
|
|
|
out = append(out, p.Essd.Bytes()...)
|
2018-12-05 17:04:29 +03:00
|
|
|
return out
|
2018-12-05 12:01:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Bytes outputs a byte slice representation of the Desc
|
2018-12-05 17:04:29 +03:00
|
|
|
func (d *Desc) Bytes() []byte {
|
2018-12-05 17:11:53 +03:00
|
|
|
out := make([]byte, DescDefLen)
|
2018-12-06 06:20:17 +03:00
|
|
|
out[0] = d.Dt
|
|
|
|
out[1] = d.Dl
|
|
|
|
out = append(out, d.Dd...)
|
2018-12-05 17:04:29 +03:00
|
|
|
return out
|
2018-12-04 08:33:04 +03:00
|
|
|
}
|
2018-12-05 14:16:06 +03:00
|
|
|
|
2018-12-06 06:20:17 +03:00
|
|
|
// Bytes outputs a byte slice representation of the ESSD
|
2018-12-05 17:04:29 +03:00
|
|
|
func (e *ESSD) Bytes() []byte {
|
2018-12-05 17:11:53 +03:00
|
|
|
out := make([]byte, ESSDDefLen)
|
2018-12-06 06:20:17 +03:00
|
|
|
out[0] = e.St
|
|
|
|
out[1] = 0xe0 | (0x1f & byte(e.Epid>>8))
|
|
|
|
out[2] = byte(e.Epid)
|
|
|
|
out[3] = 0xf0 | (0x03 & byte(e.Esil>>8))
|
|
|
|
out[4] = byte(e.Esil)
|
|
|
|
for _, d := range e.Esd {
|
2018-12-05 16:34:19 +03:00
|
|
|
out = append(out, d.Bytes()...)
|
2018-12-05 14:16:06 +03:00
|
|
|
}
|
2018-12-05 17:04:29 +03:00
|
|
|
return out
|
2018-12-05 14:16:06 +03:00
|
|
|
}
|
2018-12-05 17:58:14 +03:00
|
|
|
|
2018-12-06 08:32:40 +03:00
|
|
|
func boolToByte(b bool) (o byte) {
|
2018-12-05 17:58:14 +03:00
|
|
|
if b {
|
2018-12-06 08:32:40 +03:00
|
|
|
o = 0x01
|
2018-12-05 17:58:14 +03:00
|
|
|
}
|
2018-12-06 08:32:40 +03:00
|
|
|
return
|
2018-12-05 17:58:14 +03:00
|
|
|
}
|
2018-12-06 04:46:26 +03:00
|
|
|
|
2018-12-06 08:32:40 +03:00
|
|
|
func byteToBool(b byte) (o bool) {
|
|
|
|
if b != 0 {
|
|
|
|
o = true
|
2018-12-06 06:20:17 +03:00
|
|
|
}
|
2018-12-06 08:32:40 +03:00
|
|
|
return
|
2018-12-06 06:20:17 +03:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:46:26 +03:00
|
|
|
func crc32_MakeTable(poly uint32) *crc32.Table {
|
|
|
|
var t crc32.Table
|
|
|
|
for i := range t {
|
|
|
|
crc := uint32(i) << 24
|
|
|
|
for j := 0; j < 8; j++ {
|
|
|
|
if crc&0x80000000 != 0 {
|
|
|
|
crc = (crc << 1) ^ poly
|
|
|
|
} else {
|
|
|
|
crc <<= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t[i] = crc
|
|
|
|
}
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func crc32_Update(crc uint32, tab *crc32.Table, p []byte) uint32 {
|
|
|
|
for _, v := range p {
|
|
|
|
crc = tab[byte(crc>>24)^v] ^ (crc << 8)
|
|
|
|
}
|
|
|
|
return crc
|
|
|
|
}
|