av/codec/h264/lex.go

123 lines
2.9 KiB
Go
Raw Normal View History

2018-08-28 13:58:57 +03:00
/*
NAME
lex.go
DESCRIPTION
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>
2019-06-06 19:30:31 +03:00
Saxon Nelson-Milton <saxon@ausocean.org>
2018-08-28 13:58:57 +03:00
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-06-06 19:30:31 +03:00
// Package h264 provides a h264 bytestream lexer and RTP H264 access unit
// extracter.
package h264
2018-08-28 13:58:57 +03:00
import (
"fmt"
2018-08-28 13:58:57 +03:00
"io"
"time"
"bitbucket.org/ausocean/av/codec/codecutil"
)
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-06-06 19:30:31 +03:00
// Lex 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-06-06 19:30:31 +03:00
func Lex(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
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
2018-08-28 13:58:57 +03:00
for {
var b byte
var err error
buf, b, err = c.ScanUntil(buf, 0x00)
2018-08-28 13:58:57 +03:00
if err != nil {
return fmt.Errorf("can't scan until: %w", err)
2018-08-28 13:58:57 +03:00
}
for n := 1; b == 0x0 && n < 4; n++ {
b, err = c.ReadByte()
2018-08-28 13:58:57 +03:00
if err != nil {
return fmt.Errorf("can't read byte: %w", err)
2018-08-28 13:58:57 +03:00
}
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
}
b, err = c.ReadByte()
2018-08-28 13:58:57 +03:00
if err != nil {
return fmt.Errorf("can't read byte: %w", err)
2018-08-28 13:58:57 +03:00
}
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 (
nonIdrPic = 1
idrPic = 5
suppEnhInfo = 6
paramSet = 8
2018-08-28 13:58:57 +03:00
)
switch nalTyp := b & 0x1f; nalTyp {
case nonIdrPic, idrPic, paramSet, suppEnhInfo:
2018-08-28 13:58:57 +03:00
writeOut = true
}
}
}
}