mirror of https://bitbucket.org/ausocean/av.git
256 lines
5.0 KiB
Go
256 lines
5.0 KiB
Go
/*
|
|
NAME
|
|
encoder.go
|
|
|
|
DESCRIPTION
|
|
See Readme.md
|
|
|
|
AUTHOR
|
|
Dan Kortschak <dan@ausocean.org>
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
LICENSE
|
|
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"
|
|
"io"
|
|
"math/bits"
|
|
"time"
|
|
|
|
"bitbucket.org/ausocean/av/stream/mts/pes"
|
|
"bitbucket.org/ausocean/av/stream/mts/psi"
|
|
)
|
|
|
|
var (
|
|
patTable []byte
|
|
pmtTable []byte
|
|
)
|
|
|
|
const (
|
|
psiPacketSize = 184
|
|
psiSendCount = 100
|
|
)
|
|
|
|
func init() {
|
|
// Generate IEEE polynomial table
|
|
// for the big-endian algorithm.
|
|
crcTable := crc32_MakeTable(bits.Reverse32(crc32.IEEE))
|
|
|
|
patTable = completePSI(psi.StdPat, crcTable)
|
|
pmtTable = completePSI(psi.StdPmt, 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
|
|
}
|
|
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
|
|
}
|
|
|
|
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,
|
|
ptsOffset = 700 * time.Millisecond
|
|
|
|
// pcrFreq is the base Program Clock Reference frequency.
|
|
pcrFreq = 90000 // Hz
|
|
)
|
|
|
|
// Encoder encapsulates properties of an mpegts generator.
|
|
type Encoder struct {
|
|
dst io.Writer
|
|
|
|
clock time.Duration
|
|
frameInterval time.Duration
|
|
ptsOffset time.Duration
|
|
|
|
psiCount uint
|
|
|
|
continuity map[int]byte
|
|
}
|
|
|
|
// NewEncoder returns an Encoder with the specified frame rate.
|
|
func NewEncoder(dst io.Writer, fps float64) *Encoder {
|
|
return &Encoder{
|
|
dst: dst,
|
|
|
|
frameInterval: time.Duration(float64(time.Second) / fps),
|
|
ptsOffset: ptsOffset,
|
|
|
|
continuity: map[int]byte{
|
|
patPid: 0,
|
|
pmtPid: 0,
|
|
videoPid: 0,
|
|
},
|
|
}
|
|
}
|
|
|
|
const (
|
|
hasPayload = 0x1
|
|
hasAdaptationField = 0x2
|
|
)
|
|
|
|
const (
|
|
hasDTS = 0x1
|
|
hasPTS = 0x2
|
|
)
|
|
|
|
// generate handles the incoming data and generates equivalent mpegts packets -
|
|
// sending them to the output channel
|
|
func (e *Encoder) Encode(nalu []byte) error {
|
|
if e.psiCount <= 0 {
|
|
err := e.writePSI()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
e.psiCount--
|
|
// 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
|
|
}
|
|
_, err := e.dst.Write(pkt.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
e.tick()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (e *Encoder) writePSI() error {
|
|
// Write PAT
|
|
patPkt := Packet{
|
|
PUSI: true,
|
|
PID: patPid,
|
|
CC: e.ccFor(patPid),
|
|
AFC: hasPayload,
|
|
Payload: patTable,
|
|
}
|
|
_, err := e.dst.Write(patPkt.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Write PMT.
|
|
pmtPkt := Packet{
|
|
PUSI: true,
|
|
PID: pmtPid,
|
|
CC: e.ccFor(pmtPid),
|
|
AFC: hasPayload,
|
|
Payload: pmtTable,
|
|
}
|
|
_, err = e.dst.Write(pmtPkt.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
e.psiCount = psiSendCount
|
|
return nil
|
|
}
|
|
|
|
// tick advances the clock one frame interval.
|
|
func (e *Encoder) tick() {
|
|
e.clock += e.frameInterval
|
|
}
|
|
|
|
// pts retuns the current presentation timestamp.
|
|
func (e *Encoder) pts() uint64 {
|
|
return uint64((e.clock + e.ptsOffset).Seconds() * pcrFreq)
|
|
}
|
|
|
|
// pcr returns the current program clock reference.
|
|
func (e *Encoder) pcr() uint64 {
|
|
return uint64(e.clock.Seconds() * pcrFreq)
|
|
}
|
|
|
|
// ccFor returns the next continuity counter for pid.
|
|
func (e *Encoder) ccFor(pid int) byte {
|
|
cc := e.continuity[pid]
|
|
const continuityCounterMask = 0xf
|
|
e.continuity[pid] = (cc + 1) & continuityCounterMask
|
|
return cc
|
|
}
|