mirror of https://bitbucket.org/ausocean/av.git
Merged in h265-lexer (pull request #189)
codec/h265: h265 lexer and testing
This commit is contained in:
commit
6b64314d71
|
@ -0,0 +1,192 @@
|
|||
/*
|
||||
NAME
|
||||
lex.go
|
||||
|
||||
DESCRIPTION
|
||||
lex.go provides a lexer for taking RTP HEVC (H265) and lexing into access units.
|
||||
|
||||
AUTHORS
|
||||
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
||||
|
||||
LICENSE
|
||||
Copyright (C) 2019 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
|
||||
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
||||
*/
|
||||
|
||||
// Package h265 provides an RTP h265 lexer that can extract h265 access units
|
||||
// from an RTP stream.
|
||||
package h265
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/ausocean/av/protocol/rtp"
|
||||
)
|
||||
|
||||
// NALU types.
|
||||
const (
|
||||
typeAggregation = 48
|
||||
typeFragmentation = 49
|
||||
typePACI = 50
|
||||
)
|
||||
|
||||
// Buffer sizes.
|
||||
const (
|
||||
maxAUSize = 100000
|
||||
maxRTPSize = 4096
|
||||
)
|
||||
|
||||
// Lexer is an H265 lexer.
|
||||
type Lexer struct {
|
||||
donl bool // Indicates whether DONL and DOND will be used for the RTP stream.
|
||||
buf *bytes.Buffer // Holds the current access unit.
|
||||
frag bool // Indicates if we're currently dealing with a fragmentation packet.
|
||||
}
|
||||
|
||||
// NewLexer returns a new Lexer.
|
||||
func NewLexer(donl bool) *Lexer {
|
||||
return &Lexer{
|
||||
donl: donl,
|
||||
buf: bytes.NewBuffer(make([]byte, 0, maxAUSize))}
|
||||
}
|
||||
|
||||
// Lex continually reads RTP packets from the io.Reader src and lexes into
|
||||
// access units which are written to the io.Writer dst. Lex expects that for
|
||||
// each read from src, a single RTP packet is received.
|
||||
func (l *Lexer) Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
|
||||
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] >> 1) & 0x3f
|
||||
|
||||
// If not currently fragmented then we ignore current write.
|
||||
if l.frag && nalType != typeFragmentation {
|
||||
l.buf.Reset()
|
||||
l.frag = false
|
||||
continue
|
||||
}
|
||||
|
||||
switch nalType {
|
||||
case typeAggregation:
|
||||
l.handleAggregation(payload)
|
||||
case typeFragmentation:
|
||||
l.handleFragmentation(payload)
|
||||
case typePACI:
|
||||
l.handlePACI(payload)
|
||||
default:
|
||||
l.writeWithPrefix(payload)
|
||||
}
|
||||
|
||||
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 {
|
||||
_, err := l.buf.WriteTo(dst)
|
||||
if err != nil {
|
||||
// TODO: work out what to do here.
|
||||
}
|
||||
l.buf.Reset()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleAggregation parses NAL units from an aggregation packet and writes
|
||||
// them to the Lexers buffer buf.
|
||||
func (l *Lexer) handleAggregation(d []byte) {
|
||||
idx := 2
|
||||
for idx < len(d) {
|
||||
if l.donl {
|
||||
switch idx {
|
||||
case 2:
|
||||
idx += 2
|
||||
default:
|
||||
idx++
|
||||
}
|
||||
}
|
||||
size := int(binary.BigEndian.Uint16(d[idx:]))
|
||||
idx += 2
|
||||
nalu := d[idx : idx+size]
|
||||
idx += size
|
||||
l.writeWithPrefix(nalu)
|
||||
}
|
||||
}
|
||||
|
||||
// handleFragmentation parses NAL units from fragmentation packets and writes
|
||||
// them to the Lexer's buf.
|
||||
func (l *Lexer) handleFragmentation(d []byte) {
|
||||
// Get start and end indiciators from FU header.
|
||||
start := d[2]&0x80 != 0
|
||||
end := d[2]&0x40 != 0
|
||||
|
||||
d = d[3:]
|
||||
if l.donl {
|
||||
d = d[2:]
|
||||
}
|
||||
|
||||
switch {
|
||||
case start && !end:
|
||||
l.frag = true
|
||||
l.writeWithPrefix(d)
|
||||
case !start && end:
|
||||
l.frag = false
|
||||
fallthrough
|
||||
case !start && !end:
|
||||
l.writeNoPrefix(d)
|
||||
default:
|
||||
panic("bad fragmentation packet")
|
||||
}
|
||||
}
|
||||
|
||||
// handlePACI will handl PACI packets
|
||||
//
|
||||
// TODO: complete this
|
||||
func (l *Lexer) handlePACI(d []byte) {
|
||||
panic("unsupported nal type")
|
||||
}
|
||||
|
||||
// write writes a NAL unit to the Lexer's buf in byte stream format using the
|
||||
// start code.
|
||||
func (l *Lexer) 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 *Lexer) writeNoPrefix(d []byte) {
|
||||
l.buf.Write(d)
|
||||
}
|
|
@ -0,0 +1,262 @@
|
|||
/*
|
||||
NAME
|
||||
lex_test.go
|
||||
|
||||
DESCRIPTION
|
||||
lex_test.go provides tests to check validity of the Lexer found in lex.go.
|
||||
|
||||
AUTHORS
|
||||
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
||||
|
||||
LICENSE
|
||||
Copyright (C) 2019 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
|
||||
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
||||
*/
|
||||
|
||||
package h265
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// rtpReader provides the RTP stream.
|
||||
type rtpReader struct {
|
||||
packets [][]byte
|
||||
idx int
|
||||
}
|
||||
|
||||
// Read implements io.Reader.
|
||||
func (r *rtpReader) Read(p []byte) (int, error) {
|
||||
if r.idx == len(r.packets) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
b := r.packets[r.idx]
|
||||
n := copy(p, b)
|
||||
if n < len(r.packets[r.idx]) {
|
||||
r.packets[r.idx] = r.packets[r.idx][n:]
|
||||
} else {
|
||||
r.idx++
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// destination holds the access units extracted during the lexing process.
|
||||
type destination [][]byte
|
||||
|
||||
// Write implements io.Writer.
|
||||
func (d *destination) Write(p []byte) (int, error) {
|
||||
t := make([]byte, len(p))
|
||||
copy(t, p)
|
||||
*d = append([][]byte(*d), t)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// TestLex checks that the Lexer can correctly extract H265 access units from
|
||||
// HEVC RTP stream in RTP payload format.
|
||||
func TestLex(t *testing.T) {
|
||||
const rtpVer = 2
|
||||
|
||||
tests := []struct {
|
||||
donl bool
|
||||
packets [][]byte
|
||||
expect [][]byte
|
||||
}{
|
||||
{
|
||||
donl: false,
|
||||
packets: [][]byte{
|
||||
{ // Single NAL unit.
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x01, 0x02, 0x03, 0x04, // NAL Data.
|
||||
},
|
||||
{ // Fragmentation (start packet).
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x62, 0x00, // NAL header (type49).
|
||||
0x80, // FU header.
|
||||
0x01, 0x02, 0x03, // FU payload.
|
||||
},
|
||||
{ // Fragmentation (middle packet)
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x62, 0x00, // NAL header (type 49).
|
||||
0x00, // FU header.
|
||||
0x04, 0x05, 0x06, // FU payload.
|
||||
},
|
||||
{ // Fragmentation (end packet)
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x62, 0x00, // NAL header (type 49).
|
||||
0x40, // FU header.
|
||||
0x07, 0x08, 0x09, // FU payload
|
||||
},
|
||||
|
||||
{ // Aggregation. Make last packet of access unit => marker bit true.
|
||||
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x60, 0x00, // NAL header (type 49).
|
||||
0x00, 0x04, // NAL 1 size.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL 1 data.
|
||||
0x00, 0x04, // NAL 2 size.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL 2 data.
|
||||
},
|
||||
{ // Singla NAL
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
},
|
||||
{ // Singla NAL. Make last packet of access unit => marker bit true.
|
||||
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
},
|
||||
},
|
||||
expect: [][]byte{
|
||||
// First access unit.
|
||||
{
|
||||
// NAL 1
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
// NAL 2
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x01, 0x02, 0x03, // FU payload.
|
||||
0x04, 0x05, 0x06, // FU payload.
|
||||
0x07, 0x08, 0x09, // FU payload.
|
||||
// NAL 3
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
// NAL 4
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL 2 data
|
||||
},
|
||||
// Second access unit.
|
||||
{
|
||||
// NAL 1
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x01, 0x02, 0x03, 0x04, // Data.
|
||||
// NAL 2
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x01, 0x02, 0x03, 0x04, // Data.
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
donl: true,
|
||||
packets: [][]byte{
|
||||
{ // Single NAL unit.
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x00, 0x00, // DONL
|
||||
0x01, 0x02, 0x03, 0x04, // NAL Data.
|
||||
},
|
||||
{ // Fragmentation (start packet).
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x62, 0x00, // NAL header (type49).
|
||||
0x80, // FU header.
|
||||
0x00, 0x00, // DONL
|
||||
0x01, 0x02, 0x03, // FU payload.
|
||||
},
|
||||
{ // Fragmentation (middle packet)
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x62, 0x00, // NAL header (type 49).
|
||||
0x00, // FU header.
|
||||
0x00, 0x00, // DONL
|
||||
0x04, 0x05, 0x06, // FU payload.
|
||||
},
|
||||
{ // Fragmentation (end packet)
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x62, 0x00, // NAL header (type 49).
|
||||
0x40, // FU header.
|
||||
0x00, 0x00, // DONL
|
||||
0x07, 0x08, 0x09, // FU payload
|
||||
},
|
||||
|
||||
{ // Aggregation. Make last packet of access unit => marker bit true.
|
||||
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x60, 0x00, // NAL header (type 49).
|
||||
0x00, 0x00, // DONL
|
||||
0x00, 0x04, // NAL 1 size.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL 1 data.
|
||||
0x00, // DOND
|
||||
0x00, 0x04, // NAL 2 size.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL 2 data.
|
||||
},
|
||||
{ // Singla NAL
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x40, 0x00, // NAL header (type=32 VPS)
|
||||
0x00, 0x00, // DONL.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
},
|
||||
{ // Singla NAL. Make last packet of access unit => marker bit true.
|
||||
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x00, 0x00, // DONL
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
},
|
||||
},
|
||||
expect: [][]byte{
|
||||
// First access unit.
|
||||
{
|
||||
// NAL 1
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x00, 0x00, // DONL
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
// NAL 2
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x01, 0x02, 0x03, // FU payload.
|
||||
0x04, 0x05, 0x06, // FU payload.
|
||||
0x07, 0x08, 0x09, // FU payload.
|
||||
// NAL 3
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL data.
|
||||
// NAL 4
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x01, 0x02, 0x03, 0x04, // NAL 2 data
|
||||
},
|
||||
// Second access unit.
|
||||
{
|
||||
// NAL 1
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x00, 0x00, // DONL
|
||||
0x01, 0x02, 0x03, 0x04, // Data.
|
||||
// NAL 2
|
||||
0x00, 0x00, 0x00, 0x01, // Start code.
|
||||
0x40, 0x00, // NAL header (type=32 VPS).
|
||||
0x00, 0x00, // DONL
|
||||
0x01, 0x02, 0x03, 0x04, // Data.
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for testNum, test := range tests {
|
||||
r := &rtpReader{packets: test.packets}
|
||||
d := &destination{}
|
||||
err := NewLexer(test.donl).Lex(d, r, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("error lexing: %v\n", err)
|
||||
}
|
||||
|
||||
for i, accessUnit := range test.expect {
|
||||
for j, part := range accessUnit {
|
||||
if part != [][]byte(*d)[i][j] {
|
||||
t.Fatalf("did not get expected data for test: %v.\nGot: %v\nWant: %v\n", testNum, d, test.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,6 +34,19 @@ import (
|
|||
|
||||
const badVer = "incompatible RTP version"
|
||||
|
||||
// Marker returns the state of the RTP marker bit, and an error if parsing fails.
|
||||
func Marker(d []byte) (bool, error) {
|
||||
if len(d) < defaultHeadSize {
|
||||
panic("invalid RTP packet length")
|
||||
}
|
||||
|
||||
if version(d) != rtpVer {
|
||||
return false, errors.New(badVer)
|
||||
}
|
||||
|
||||
return d[1]&0x80 != 0, nil
|
||||
}
|
||||
|
||||
// Payload returns the payload from an RTP packet provided the version is
|
||||
// compatible, otherwise an error is returned.
|
||||
func Payload(d []byte) ([]byte, error) {
|
||||
|
|
Loading…
Reference in New Issue