psi: restructuring to make neater

This commit is contained in:
saxon 2018-12-11 17:16:26 +10:30
parent 68eec9de98
commit d8c46eefaa
4 changed files with 100 additions and 94 deletions

43
stream/mts/psi/op.go Normal file
View File

@ -0,0 +1,43 @@
package psi
import (
"encoding/binary"
"errors"
)
func TimeToBytes(time uint64) []byte {
s := make([]byte, 8)
binary.BigEndian.PutUint64(s, time)
return s
}
func chkTime(d []byte) error {
if d[descTagIndx] != timeDescTag {
return errors.New("PSI does not contain a time descriptor, cannot update")
}
return nil
}
// Updatetime
func UpdateTime(d []byte, t int) error {
err := chkTime(d)
if err != nil {
return err
}
ts := TimeToBytes(uint64(t))
for i := range d[timeIndx : timeIndx+8] {
d[i+timeIndx] = ts[i]
}
return nil
}
func TimeOf(d []byte) (t uint64, err error) {
err = chkTime(d)
if err != nil {
return 0, err
}
for i := range d[timeIndx : timeIndx+8] {
t |= uint64(d[i+timeIndx]) << uint(56-i*8)
}
return t, nil
}

View File

@ -1,66 +1,11 @@
package psi package psi
import ( import (
"encoding/binary"
"errors"
"hash/crc32" "hash/crc32"
"math/bits" "math/bits"
) )
// TODO: Finish off mts/psi so that we can create pat and pmt tables instead // Lengths of section definitions
// of hardcoding.
var (
StdPat = []byte{
0x00, // pointer
// ---- section included in data sent to CRC32 during check
// table header
0x00, // table id
0xb0, // section syntax indicator:1|private bit:1|reserved:2|section length:2|more bytes...:2
0x0d, // more bytes...
// syntax section
0x00, 0x01, // table id extension
0xc1, // reserved bits:2|version:5|use now:1 1100 0001
0x00, // section number
0x00, // last section number
// table data
0x00, 0x01, // Program number
0xf0, 0x00, // reserved:3|program map PID:13
// 0x2a, 0xb1, 0x04, 0xb2, // CRC
// ----
}
StdPmt = []byte{
0x00, // pointer
// ---- section included in data sent to CRC32 during check
// table header
0x02, // table id
0xb0, // section syntax indicator:1|private bit:1|reserved:2|section length:2|more bytes...:2
0x12, // more bytes...
// syntax section
0x00, 0x01, // table id extension
0xc1, // reserved bits:3|version:5|use now:1
0x00, // section number
0x00, // last section number
// table data
0xe1, 0x00, // reserved:3|PCR PID:13
0xf0, 0x00, // reserved:4|unused:2|program info length:10
// No program descriptors since program info length is 0.
// elementary stream info data
0x1b, // stream type
0xe1, 0x00, // reserved:3|elementary PID:13
0xf0, 0x00, // reserved:4|unused:2|ES info length:10
// No elementary stream descriptors since ES info length is 0.
// 0x15, 0xbd, 0x4d, 0x56, // CRC
// ----
}
)
// Some common lengths
const ( const (
ESSDDefLen = 5 ESSDDefLen = 5
DescDefLen = 2 DescDefLen = 2
@ -76,6 +21,7 @@ const (
PMTTableID = 0x02 PMTTableID = 0x02
) )
// Consts relating to time description
const ( const (
timeDescTag = 234 timeDescTag = 234
descTagIndx = 13 descTagIndx = 13
@ -353,40 +299,3 @@ func crc32_Update(crc uint32, tab *crc32.Table, p []byte) uint32 {
} }
return crc return crc
} }
func TimeToBytes(time uint64) []byte {
s := make([]byte, 8)
binary.BigEndian.PutUint64(s, time)
return s
}
func chkTime(d []byte) error {
if d[descTagIndx] != timeDescTag {
return errors.New("PSI does not contain a time descriptor, cannot update")
}
return nil
}
// Updatetime
func UpdateTime(d []byte, t int) error {
err := chkTime(d)
if err != nil {
return err
}
ts := TimeToBytes(uint64(t))
for i := range d[timeIndx : timeIndx+8] {
d[i+timeIndx] = ts[i]
}
return nil
}
func GetTime(d []byte) (t uint64, err error) {
err = chkTime(d)
if err != nil {
return 0, err
}
for i := range d[timeIndx : timeIndx+8] {
t |= uint64(d[i+timeIndx]) << uint(56-i*8)
}
return t, nil
}

View File

@ -204,7 +204,7 @@ func TestTimeUpdate(t *testing.T) {
} }
func TestTimeGet(t *testing.T) { func TestTimeGet(t *testing.T) {
s, err := GetTime(StdPmtTime) s, err := TimeOf(StdPmtTime)
if err != nil { if err != nil {
t.Errorf("Getting timestamp failed with err: %v", err) t.Errorf("Getting timestamp failed with err: %v", err)
} }

54
stream/mts/psi/std.go Normal file
View File

@ -0,0 +1,54 @@
package psi
// TODO: Finish off mts/psi so that we can create pat and pmt tables instead
// of hardcoding.
var (
StdPat = []byte{
0x00, // pointer
// ---- section included in data sent to CRC32 during check
// table header
0x00, // table id
0xb0, // section syntax indicator:1|private bit:1|reserved:2|section length:2|more bytes...:2
0x0d, // more bytes...
// syntax section
0x00, 0x01, // table id extension
0xc1, // reserved bits:2|version:5|use now:1 1100 0001
0x00, // section number
0x00, // last section number
// table data
0x00, 0x01, // Program number
0xf0, 0x00, // reserved:3|program map PID:13
// 0x2a, 0xb1, 0x04, 0xb2, // CRC
// ----
}
StdPmt = []byte{
0x00, // pointer
// ---- section included in data sent to CRC32 during check
// table header
0x02, // table id
0xb0, // section syntax indicator:1|private bit:1|reserved:2|section length:2|more bytes...:2
0x12, // more bytes...
// syntax section
0x00, 0x01, // table id extension
0xc1, // reserved bits:3|version:5|use now:1
0x00, // section number
0x00, // last section number
// table data
0xe1, 0x00, // reserved:3|PCR PID:13
0xf0, 0x00, // reserved:4|unused:2|program info length:10
// No program descriptors since program info length is 0.
// elementary stream info data
0x1b, // stream type
0xe1, 0x00, // reserved:3|elementary PID:13
0xf0, 0x00, // reserved:4|unused:2|ES info length:10
// No elementary stream descriptors since ES info length is 0.
// 0x15, 0xbd, 0x4d, 0x56, // CRC
// ----
}
)