2018-08-28 13:58:57 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
lex.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-05-03 13:22:23 +03:00
|
|
|
lex.go provides a lexer to lex h264 bytestream into access units.
|
2018-08-28 13:58:57 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
lex.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 http://www.gnu.org/licenses.
|
|
|
|
*/
|
2019-05-03 13:22:23 +03:00
|
|
|
|
2019-05-06 09:17:51 +03:00
|
|
|
// lex.go provides a lexer to lex h264 bytestream into access units.
|
|
|
|
|
2019-04-26 14:01:12 +03:00
|
|
|
package h264
|
2018-08-28 13:58:57 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-22 05:53:29 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2018-08-28 13:58:57 +03:00
|
|
|
"io"
|
|
|
|
"time"
|
2019-05-22 04:37:39 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/codec/codecutil"
|
2019-05-22 05:53:29 +03:00
|
|
|
"bitbucket.org/ausocean/av/protocol/rtp"
|
2018-08-28 13:58:57 +03:00
|
|
|
)
|
|
|
|
|
2019-05-27 07:37:04 +03:00
|
|
|
// NAL types (from https://tools.ietf.org/html/rfc6184#page-13)
|
2019-05-22 08:19:25 +03:00
|
|
|
const (
|
|
|
|
// Single nal units bounds.
|
|
|
|
typeSingleNALULowBound = 1
|
|
|
|
typeSingleNALUHighBound = 23
|
|
|
|
|
|
|
|
// Single-time aggregation packets.
|
|
|
|
typeSTAPA = 24
|
|
|
|
typeSTAPB = 25
|
|
|
|
|
|
|
|
// Multi-time aggregation packets.
|
|
|
|
typeMTAP16 = 26
|
|
|
|
typeMTAP24 = 27
|
|
|
|
|
|
|
|
// Fragmentation packets.
|
|
|
|
typeFUA = 28
|
|
|
|
typeFUB = 29
|
|
|
|
)
|
|
|
|
|
2018-08-28 13:58:57 +03:00
|
|
|
var noDelay = make(chan time.Time)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
close(noDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
var h264Prefix = [...]byte{0x00, 0x00, 0x01, 0x09, 0xf0}
|
|
|
|
|
2019-05-19 15:14:23 +03:00
|
|
|
// LexFromBytestream lexes H.264 NAL units read from src into separate writes
|
|
|
|
// to dst with successive writes being performed not earlier than the specified
|
|
|
|
// delay. NAL units are split after type 1 (Coded slice of a non-IDR picture), 5
|
2018-08-28 13:58:57 +03:00
|
|
|
// (Coded slice of a IDR picture) and 8 (Picture parameter set).
|
2019-05-19 15:14:23 +03:00
|
|
|
func LexFromBytestream(dst io.Writer, src io.Reader, delay time.Duration) error {
|
2018-08-28 13:58:57 +03:00
|
|
|
var tick <-chan time.Time
|
|
|
|
if delay == 0 {
|
|
|
|
tick = noDelay
|
|
|
|
} else {
|
|
|
|
ticker := time.NewTicker(delay)
|
|
|
|
defer ticker.Stop()
|
|
|
|
tick = ticker.C
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufSize = 8 << 10
|
|
|
|
|
2019-05-22 04:37:39 +03:00
|
|
|
c := codecutil.NewByteScanner(src, make([]byte, 4<<10)) // Standard file buffer size.
|
2018-08-28 13:58:57 +03:00
|
|
|
|
|
|
|
buf := make([]byte, len(h264Prefix), bufSize)
|
|
|
|
copy(buf, h264Prefix[:])
|
|
|
|
writeOut := false
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
var b byte
|
|
|
|
var err error
|
2019-05-22 04:37:39 +03:00
|
|
|
buf, b, err = c.ScanUntil(buf, 0x00)
|
2018-08-28 13:58:57 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 1; b == 0x0 && n < 4; n++ {
|
2019-05-22 04:37:39 +03:00
|
|
|
b, err = c.ReadByte()
|
2018-08-28 13:58:57 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
buf = append(buf, b)
|
|
|
|
|
|
|
|
if b != 0x1 || (n != 2 && n != 3) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeOut {
|
|
|
|
<-tick
|
2019-03-09 07:58:07 +03:00
|
|
|
_, err := dst.Write(buf[:len(buf)-(n+1)])
|
2018-08-28 13:58:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf = make([]byte, len(h264Prefix)+n, bufSize)
|
|
|
|
copy(buf, h264Prefix[:])
|
|
|
|
buf = append(buf, 1)
|
|
|
|
writeOut = false
|
|
|
|
}
|
|
|
|
|
2019-05-22 04:37:39 +03:00
|
|
|
b, err = c.ReadByte()
|
2018-08-28 13:58:57 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
buf = append(buf, b)
|
|
|
|
|
|
|
|
// http://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-H.264-200305-S!!PDF-E&type=items
|
|
|
|
// Table 7-1 NAL unit type codes
|
|
|
|
const (
|
2019-01-11 16:13:54 +03:00
|
|
|
nonIdrPic = 1
|
|
|
|
idrPic = 5
|
|
|
|
suppEnhInfo = 6
|
|
|
|
paramSet = 8
|
2018-08-28 13:58:57 +03:00
|
|
|
)
|
|
|
|
switch nalTyp := b & 0x1f; nalTyp {
|
2019-01-11 16:13:54 +03:00
|
|
|
case nonIdrPic, idrPic, paramSet, suppEnhInfo:
|
2018-08-28 13:58:57 +03:00
|
|
|
writeOut = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(buf) == len(h264Prefix) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
<-tick
|
2019-03-09 07:58:07 +03:00
|
|
|
_, err := dst.Write(buf)
|
2018-08-28 13:58:57 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-05-22 05:53:29 +03:00
|
|
|
|
|
|
|
// Buffer sizes.
|
|
|
|
const (
|
2019-05-24 04:03:34 +03:00
|
|
|
maxAUSize = 100000 // Max access unit size in bytes.
|
|
|
|
maxRTPSize = 1500 // Max ethernet transmission unit in bytes.
|
2019-05-22 05:53:29 +03:00
|
|
|
)
|
|
|
|
|
2019-05-22 08:39:54 +03:00
|
|
|
// RTPLexer is a lexer for lexing H264 from RTP packets.
|
2019-05-22 05:53:29 +03:00
|
|
|
type RTPLexer struct {
|
|
|
|
buf *bytes.Buffer // Holds the current access unit.
|
|
|
|
frag bool // Indicates if we're currently dealing with a fragmentation packet.
|
|
|
|
}
|
|
|
|
|
2019-05-22 08:39:54 +03:00
|
|
|
// NewRTPLexer returns a new RTPLexer.
|
2019-05-22 05:53:29 +03:00
|
|
|
func NewRTPLexer() *RTPLexer {
|
|
|
|
return &RTPLexer{
|
|
|
|
buf: bytes.NewBuffer(make([]byte, 0, maxAUSize))}
|
|
|
|
}
|
|
|
|
|
2019-05-24 04:04:52 +03:00
|
|
|
// Lex extracts H264 access units from an RTP stream. This function
|
2019-05-22 05:53:29 +03:00
|
|
|
// expects that each read from src will provide a single RTP packet.
|
2019-05-22 08:19:25 +03:00
|
|
|
func (l *RTPLexer) Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
|
2019-05-22 05:53:29 +03:00
|
|
|
buf := make([]byte, maxRTPSize)
|
|
|
|
for {
|
|
|
|
n, err := src.Read(buf)
|
|
|
|
switch err {
|
|
|
|
case nil: // Do nothing.
|
|
|
|
case io.EOF:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("source read error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get payload from RTP packet.
|
|
|
|
payload, err := rtp.Payload(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get RTP payload, failed with err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nalType := payload[0] & 0x1f
|
|
|
|
|
|
|
|
// If not currently fragmented then we ignore current write.
|
|
|
|
if l.frag && nalType != typeFUA {
|
|
|
|
l.buf.Reset()
|
|
|
|
l.frag = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:19:27 +03:00
|
|
|
if typeSingleNALULowBound <= nalType && nalType <= typeSingleNALUHighBound {
|
2019-05-22 05:53:29 +03:00
|
|
|
l.writeWithPrefix(payload)
|
|
|
|
} else {
|
|
|
|
switch nalType {
|
|
|
|
case typeSTAPA:
|
|
|
|
l.handleSTAPA(payload)
|
|
|
|
case typeFUA:
|
|
|
|
l.handleFUA(payload)
|
|
|
|
case typeSTAPB:
|
|
|
|
panic("STAP-B type unsupported")
|
|
|
|
case typeMTAP16:
|
|
|
|
panic("MTAP16 type unsupported")
|
|
|
|
case typeMTAP24:
|
|
|
|
panic("MTAP24 type unsupported")
|
|
|
|
case typeFUB:
|
|
|
|
panic("FU-B type unsupported")
|
|
|
|
default:
|
|
|
|
panic("unsupported type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markerIsSet, err := rtp.Marker(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get marker bit, failed with err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if markerIsSet {
|
|
|
|
l.buf.WriteTo(dst)
|
|
|
|
l.buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-24 04:06:00 +03:00
|
|
|
// handleSTAPA parses NAL units from an aggregation packet and writes
|
2019-05-22 05:53:29 +03:00
|
|
|
// them to the Lexers buffer buf.
|
|
|
|
func (l *RTPLexer) handleSTAPA(d []byte) {
|
|
|
|
for i := 1; i < len(d); {
|
|
|
|
size := int(binary.BigEndian.Uint16(d[i:]))
|
|
|
|
|
|
|
|
// Skip over NAL unit size.
|
|
|
|
const sizeOfFieldLen = 2
|
|
|
|
i += sizeOfFieldLen
|
|
|
|
|
|
|
|
// Get the NALU.
|
|
|
|
nalu := d[i : i+size]
|
|
|
|
i += size
|
|
|
|
l.writeWithPrefix(nalu)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 04:06:30 +03:00
|
|
|
// handleFUA parses NAL units from fragmentation packets and writes
|
2019-05-22 05:53:29 +03:00
|
|
|
// them to the Lexer's buf.
|
|
|
|
func (l *RTPLexer) handleFUA(d []byte) {
|
|
|
|
// Get start and end indiciators from FU header.
|
|
|
|
const FUHeadIdx = 1
|
|
|
|
start := d[FUHeadIdx]&0x80 != 0
|
|
|
|
end := d[FUHeadIdx]&0x40 != 0
|
|
|
|
|
|
|
|
// If start, form new header, skip FU indicator only and set first byte to
|
|
|
|
// new header. Otherwise, skip over both FU indicator and FU header.
|
|
|
|
if start {
|
2019-05-27 07:53:33 +03:00
|
|
|
newHead := (d[0] & 0xe0) | (d[1] & 0x1f)
|
|
|
|
d = d[1:]
|
|
|
|
d[0] = newHead
|
2019-05-24 04:08:06 +03:00
|
|
|
if end {
|
|
|
|
panic("bad fragmentation packet")
|
|
|
|
}
|
2019-05-22 05:53:29 +03:00
|
|
|
l.frag = true
|
|
|
|
l.writeWithPrefix(d)
|
2019-05-23 07:25:25 +03:00
|
|
|
} else {
|
2019-05-27 07:53:33 +03:00
|
|
|
d = d[2:]
|
2019-05-23 07:25:25 +03:00
|
|
|
if end {
|
|
|
|
l.frag = false
|
|
|
|
}
|
2019-05-22 05:53:29 +03:00
|
|
|
l.writeNoPrefix(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write writes a NAL unit to the Lexer's buf in byte stream format using the
|
|
|
|
// start code.
|
|
|
|
func (l *RTPLexer) writeWithPrefix(d []byte) {
|
|
|
|
const prefix = "\x00\x00\x00\x01"
|
|
|
|
l.buf.Write([]byte(prefix))
|
|
|
|
l.buf.Write(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeNoPrefix writes data to the Lexer's buf. This is used for non start
|
|
|
|
// fragmentations of a NALU.
|
|
|
|
func (l *RTPLexer) writeNoPrefix(d []byte) {
|
|
|
|
l.buf.Write(d)
|
|
|
|
}
|