av/stream/mts/encoder.go

282 lines
6.2 KiB
Go
Raw Normal View History

/*
NAME
2018-08-19 14:09:57 +03:00
encoder.go
DESCRIPTION
See Readme.md
AUTHOR
Dan Kortschak <dan@ausocean.org>
2018-02-28 16:46:59 +03:00
Saxon Nelson-Milton <saxon@ausocean.org>
LICENSE
2018-08-19 14:09:57 +03:00
encoder.go is Copyright (C) 2017-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.
*/
package mts
import (
"encoding/binary"
"hash/crc32"
"math/bits"
2018-08-18 06:51:50 +03:00
"time"
2018-08-20 01:19:02 +03:00
"bitbucket.org/ausocean/av/stream/mts/pes"
)
2018-08-17 18:30:51 +03:00
const psiPacketSize = 184
2018-04-17 08:20:23 +03:00
2018-02-28 17:42:00 +03:00
// TODO: really need to finish the at and pmt stuff - this is too hacky
2018-01-16 08:49:18 +03:00
var (
patTable = []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:3|version:5|use now:1
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
// ----
}
pmtTable = []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
// ----
}
2018-01-16 08:49:18 +03:00
)
func init() {
// Generate IEEE polynomial table
// for the big-endian algorithm.
crcTable := crc32_MakeTable(bits.Reverse32(crc32.IEEE))
patTable = completePSI(patTable, crcTable)
pmtTable = completePSI(pmtTable, crcTable)
}
func completePSI(psi []byte, tab *crc32.Table) []byte {
var buf [4]byte
crc := crc32_Update(0xffffffff, tab, psi[1:])
binary.BigEndian.PutUint32(buf[:], crc)
dst := make([]byte, len(psi), psiPacketSize)
copy(dst, psi)
dst = append(dst, buf[:]...)
for len(dst) < cap(dst) {
dst = append(dst, 0xff)
}
return dst
}
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
2018-02-28 17:42:00 +03:00
}
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)
2018-02-28 17:42:00 +03:00
}
return crc
2018-02-28 17:42:00 +03:00
}
2018-01-16 08:49:18 +03:00
const (
sdtPid = 17
patPid = 0
pmtPid = 4096
videoPid = 256
streamID = 0xe0 // First video stream ID.
)
// Time related constants.
const (
// ptsOffset is the offset added to the clock to determine
// the current presentation timestamp,
2018-08-18 06:51:50 +03:00
ptsOffset = 700 * time.Millisecond
// pcrFreq is the base Program Clock Reference frequency.
pcrFreq = 90000 // Hz
2018-01-16 08:49:18 +03:00
)
2018-08-19 14:09:57 +03:00
// Encoder encapsulates properties of an mpegts generator.
type Encoder struct {
stream chan []byte
2018-08-18 07:06:14 +03:00
clock time.Duration
frameInterval time.Duration
ptsOffset time.Duration
continuity map[int]byte
2017-12-13 09:52:18 +03:00
}
2018-08-19 14:09:57 +03:00
// NewEncoder returns an Encoder with the specified frame rate.
func NewEncoder(fps float64) *Encoder {
return &Encoder{
stream: make(chan []byte, 1),
2018-08-18 07:06:14 +03:00
frameInterval: time.Duration(float64(time.Second) / fps),
ptsOffset: ptsOffset,
continuity: map[int]byte{
2018-08-18 05:36:34 +03:00
patPid: 0,
pmtPid: 0,
videoPid: 0,
},
}
2018-01-08 04:12:26 +03:00
}
// Stream returns a channel of streaming packets.
func (e *Encoder) Stream() <-chan []byte {
return e.stream
2018-02-28 17:42:00 +03:00
}
const (
hasPayload = 0x1
hasAdaptationField = 0x2
)
const (
hasDTS = 0x1
hasPTS = 0x2
)
2018-02-28 17:42:00 +03:00
// generate handles the incoming data and generates equivalent mpegts packets -
// sending them to the output channel
func (e *Encoder) Encode(nalu []byte) error {
// Write PAT
patPkt := Packet{
PUSI: true,
PID: patPid,
CC: e.ccFor(patPid),
AFC: hasPayload,
Payload: patTable,
}
e.stream <- patPkt.Bytes()
// Write PMT.
pmtPkt := Packet{
PUSI: true,
PID: pmtPid,
CC: e.ccFor(pmtPid),
AFC: hasPayload,
Payload: pmtTable,
}
e.stream <- pmtPkt.Bytes()
// Prepare PES data.
pesPkt := pes.Packet{
StreamID: streamID,
PDI: hasPTS,
PTS: e.pts(),
Data: nalu,
HeaderLength: 5,
}
buf := pesPkt.Bytes()
pusi := true
for len(buf) != 0 {
pkt := Packet{
PUSI: pusi,
PID: videoPid,
RAI: pusi,
CC: e.ccFor(videoPid),
AFC: hasAdaptationField | hasPayload,
PCRF: pusi,
}
n := pkt.FillPayload(buf)
buf = buf[n:]
if pusi {
// If the packet has a Payload Unit Start Indicator
// flag set then we need to write a PCR.
pkt.PCR = e.pcr()
pusi = false
}
e.stream <- pkt.Bytes()
}
e.tick()
return nil
2017-12-13 09:52:18 +03:00
}
2018-08-18 07:06:14 +03:00
// tick advances the clock one frame interval.
2018-08-19 14:09:57 +03:00
func (e *Encoder) tick() {
e.clock += e.frameInterval
2018-08-18 07:06:14 +03:00
}
// pts retuns the current presentation timestamp.
2018-08-19 14:09:57 +03:00
func (e *Encoder) pts() uint64 {
return uint64((e.clock + e.ptsOffset).Seconds() * pcrFreq)
}
2018-08-18 07:06:14 +03:00
// pcr returns the current program clock reference.
2018-08-19 14:09:57 +03:00
func (e *Encoder) pcr() uint64 {
return uint64(e.clock.Seconds() * pcrFreq)
}
// ccFor returns the next continuity counter for pid.
2018-08-19 14:09:57 +03:00
func (e *Encoder) ccFor(pid int) byte {
cc := e.continuity[pid]
const continuityCounterMask = 0xf
2018-08-19 14:09:57 +03:00
e.continuity[pid] = (cc + 1) & continuityCounterMask
return cc
}