/* NAME mpegts.go - provides a data structure intended to encapsulate the properties of an MpegTs packet and also functions to allow manipulation of these packets. DESCRIPTION See Readme.md AUTHOR Saxon A. Nelson-Milton LICENSE mpegts.go is Copyright (C) 2017 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 [GNU licenses](http://www.gnu.org/licenses). */ package mts import ( "errors" ) const ( PacketSize = 188 PayloadSize = 176 ) const ( SdtPid = 17 PatPid = 0 PmtPid = 4096 VideoPid = 256 StreamID = 0xe0 // First video stream ID. HeadSize = 4 DefaultAdaptationSize = 2 ) const ( AdaptationIdx = 4 AdaptationControlIdx = 3 AdaptationBodyIdx = AdaptationIdx + 1 AdaptationControlMask = 0x30 DefaultAdaptationBodySize = 1 ) const ( HasPayload = 0x1 HasAdaptationField = 0x2 ) // Adaptation field body masks. const ( DiscontinuityIndicatorMask = 0x80 RandomAccessIndicatorMask = 0x40 ElementaryStreamPriorityIndicatorMask = 0x20 ProgramClockReferenceFlagMask = 0x10 OriginalProgramClockReferenceFlagMask = 0x08 SplicingPointFlagMask = 0x04 TransportPrivateDataFlagMask = 0x02 AdaptationFieldExtensionMask = 0x01 ) // Adaptation field body indexes. const ( DiscontinuityIndicatorIdx = AdaptationIdx + 1 RandomAccessIndicatorIdx = AdaptationIdx + 1 ElementaryStreamPriorityIndicatorIdx = AdaptationIdx + 1 ProgramClockReferenceFlagIdx = AdaptationIdx + 1 OriginalProgramClockReferenceFlagIdx = AdaptationIdx + 1 SplicingPointFlagIdx = AdaptationIdx + 1 TransportPrivateDataFlagIdx = AdaptationIdx + 1 AdaptationFieldExtensionFlagIdx = AdaptationIdx + 1 ) /* The below data struct encapsulates the fields of an MPEG-TS packet. Below is the formatting of an MPEG-TS packet for reference! MPEG-TS Packet Formatting ============================================================================ | octet no | bit 0 | bit 1 | bit 2 | bit 3 | bit 4 | bit 5 | bit 6 | bit 7 | ============================================================================ | octet 0 | sync byte (0x47) | ---------------------------------------------------------------------------- | octet 1 | TEI | PUSI | Prior | PID | ---------------------------------------------------------------------------- | octet 2 | PID cont. | ---------------------------------------------------------------------------- | octet 3 | TSC | AFC | CC | ---------------------------------------------------------------------------- | octet 4 | AFL | ---------------------------------------------------------------------------- | octet 5 | DI | RAI | ESPI | PCRF | OPCRF | SPF | TPDF | AFEF | ---------------------------------------------------------------------------- | optional | PCR (48 bits => 6 bytes) | ---------------------------------------------------------------------------- | - | PCR cont. | ---------------------------------------------------------------------------- | - | PCR cont. | ---------------------------------------------------------------------------- | - | PCR cont. | ---------------------------------------------------------------------------- | - | PCR cont. | ---------------------------------------------------------------------------- | - | PCR cont. | ---------------------------------------------------------------------------- | optional | OPCR (48 bits => 6 bytes) | ---------------------------------------------------------------------------- | - | OPCR cont. | ---------------------------------------------------------------------------- | - | OPCR cont. | ---------------------------------------------------------------------------- | - | OPCR cont. | ---------------------------------------------------------------------------- | - | OPCR cont. | ---------------------------------------------------------------------------- | - | OPCR cont. | ---------------------------------------------------------------------------- | optional | SC | ---------------------------------------------------------------------------- | optional | TPDL | ---------------------------------------------------------------------------- | optional | TPD (variable length) | ---------------------------------------------------------------------------- | - | ... | ---------------------------------------------------------------------------- | optional | Extension (variable length) | ---------------------------------------------------------------------------- | - | ... | ---------------------------------------------------------------------------- | optional | Stuffing (variable length) | ---------------------------------------------------------------------------- | - | ... | ---------------------------------------------------------------------------- | optional | Payload (variable length) | ---------------------------------------------------------------------------- | - | ... | ---------------------------------------------------------------------------- */ type Packet struct { TEI bool // Transport Error Indicator PUSI bool // Payload Unit Start Indicator Priority bool // Tranposrt priority indicator PID uint16 // Packet identifier TSC byte // Transport Scrambling Control AFC byte // Adaption Field Control CC byte // Continuity Counter DI bool // Discontinouty indicator RAI bool // random access indicator ESPI bool // Elementary stream priority indicator PCRF bool // PCR flag OPCRF bool // OPCR flag SPF bool // Splicing point flag TPDF bool // Transport private data flag AFEF bool // Adaptation field extension flag PCR uint64 // Program clock reference OPCR uint64 // Original program clock reference SC byte // Splice countdown TPDL byte // Tranposrt private data length TPD []byte // Private data Ext []byte // Adaptation field extension Payload []byte // Mpeg ts Payload } // FindPMT will take a clip of mpegts and try to find a PMT table - if one // is found, then it is returned along with its index, otherwise nil, -1 and an error is returned. func FindPMT(d []byte) (p []byte, i int, err error) { if len(d) < PacketSize { return nil, -1, errors.New("Mmpegts data not of valid length") } for i = 0; i < len(d); i += PacketSize { pid := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2]) if pid == PmtPid { p = d[i+4 : i+PacketSize] return } } return nil, -1, errors.New("Could not find pmt table in mpegts data") } // FillPayload takes a channel and fills the packets Payload field until the // channel is empty or we've the packet reaches capacity func (p *Packet) FillPayload(data []byte) int { currentPktLen := 6 + asInt(p.PCRF)*6 + asInt(p.OPCRF)*6 + asInt(p.SPF)*1 + asInt(p.TPDF)*1 + len(p.TPD) p.Payload = make([]byte, PayloadSize-currentPktLen) return copy(p.Payload, data) } func asInt(b bool) int { if b { return 1 } return 0 } func asByte(b bool) byte { if b { return 1 } return 0 } // ToByteSlice interprets the fields of the ts packet instance and outputs a // corresponding byte slice func (p *Packet) Bytes(buf []byte) []byte { if buf == nil || cap(buf) != PacketSize { buf = make([]byte, 0, PacketSize) } buf = buf[:0] stuffingLength := 182 - len(p.Payload) - len(p.TPD) - asInt(p.PCRF)*6 - asInt(p.OPCRF)*6 - asInt(p.SPF) var stuffing []byte if stuffingLength > 0 { stuffing = make([]byte, stuffingLength) } for i := range stuffing { stuffing[i] = 0xFF } afl := 1 + asInt(p.PCRF)*6 + asInt(p.OPCRF)*6 + asInt(p.SPF) + asInt(p.TPDF) + len(p.TPD) + len(stuffing) buf = append(buf, []byte{ 0x47, (asByte(p.TEI)<<7 | asByte(p.PUSI)<<6 | asByte(p.Priority)<<5 | byte((p.PID&0xFF00)>>8)), byte(p.PID & 0x00FF), (p.TSC<<6 | p.AFC<<4 | p.CC), }...) if p.AFC == 3 || p.AFC == 2 { buf = append(buf, []byte{ byte(afl), (asByte(p.DI)<<7 | asByte(p.RAI)<<6 | asByte(p.ESPI)<<5 | asByte(p.PCRF)<<4 | asByte(p.OPCRF)<<3 | asByte(p.SPF)<<2 | asByte(p.TPDF)<<1 | asByte(p.AFEF)), }...) for i := 40; p.PCRF && i >= 0; i -= 8 { buf = append(buf, byte((p.PCR<<15)>>uint(i))) } for i := 40; p.OPCRF && i >= 0; i -= 8 { buf = append(buf, byte(p.OPCR>>uint(i))) } if p.SPF { buf = append(buf, p.SC) } if p.TPDF { buf = append(buf, append([]byte{p.TPDL}, p.TPD...)...) } buf = append(buf, p.Ext...) buf = append(buf, stuffing...) } buf = append(buf, p.Payload...) return buf }