av/parser/H264Parser.go

140 lines
3.8 KiB
Go
Raw Normal View History

2018-02-16 08:46:24 +03:00
/*
NAME
2018-03-13 03:54:37 +03:00
H264Parser.go
2018-02-16 08:46:24 +03:00
DESCRIPTION
See Readme.md
AUTHOR
2018-03-13 03:54:37 +03:00
Saxon Nelson-Milton <saxon@ausocean.org>
2018-02-16 08:46:24 +03:00
LICENSE
2018-03-13 03:54:37 +03:00
H264Parser.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
2018-02-16 08:46:24 +03:00
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 parser
import (
2018-03-13 03:54:37 +03:00
"time"
2018-05-30 10:23:57 +03:00
"bitbucket.org/ausocean/av/itut"
)
2018-03-13 03:54:37 +03:00
const (
inputChanSize = 100000
outputBufferSize = 10000
)
// h264Parser provides properties and methods to allow for the parsing of a
// h264 stream - i.e. to allow extraction of the individual access units
type h264Parser struct {
2018-03-13 03:54:37 +03:00
inputBuffer []byte
isParsing bool
parserOutputChanRef chan []byte
userOutputChanRef chan []byte
inputChan chan byte
delay uint
}
2018-03-13 03:54:37 +03:00
// NewH264Parser returns an instance of the h264Parser struct
func NewH264Parser() (p *h264Parser) {
2018-03-13 03:54:37 +03:00
p = new(h264Parser)
p.isParsing = true
p.inputChan = make(chan byte, inputChanSize)
p.delay = 0
return
}
2018-03-13 03:54:37 +03:00
// Stop simply sets the isParsing flag to false to indicate to the parser that
// we don't want to interpret incoming data anymore - this will also make the
// parser jump out of the parse func
func (p *h264Parser) Stop() {
p.isParsing = false
}
2018-03-13 03:54:37 +03:00
// Start starts the parse func as a goroutine so that incoming data is interpreted
func (p *h264Parser) Start() {
2018-05-06 10:59:05 +03:00
p.isParsing = true
2018-03-13 03:54:37 +03:00
go p.parse()
}
2018-03-13 03:54:37 +03:00
// SetDelay sets a delay inbetween each buffer output. Useful if we're parsing
// a file but want to replicate the speed of incoming video frames from a
// camera
func (p *h264Parser) SetDelay(delay uint) {
p.delay = delay
}
2018-03-13 03:54:37 +03:00
// GetInputChan returns a handle to the input channel of the parser
func (p *h264Parser) GetInputChan() chan byte {
return p.inputChan
}
2018-03-13 03:54:37 +03:00
// GetOutputChan returns a handle to the output chan of the parser
func (p *h264Parser) GetOutputChan() chan []byte {
return p.userOutputChanRef
}
2018-03-13 03:54:37 +03:00
// SetOutputChan sets the parser output chan to the passed output chan. This is
// useful if we want the parser output to go directly to a generator of some sort
// for packetization.
func (p *h264Parser) SetOutputChan(aChan chan []byte) {
p.parserOutputChanRef = aChan
p.userOutputChanRef = aChan
}
2018-03-13 03:54:37 +03:00
// parse interprets an incoming h264 stream and extracts individual frames
// aka access units
func (p *h264Parser) parse() {
outputBuffer := make([]byte, 0, outputBufferSize)
searchingForEnd := false
for p.isParsing {
2018-05-06 10:27:01 +03:00
var aByte uint8
2018-05-06 10:18:17 +03:00
if p.isParsing {
2018-05-06 10:27:01 +03:00
aByte = <-p.inputChan
2018-05-06 10:18:17 +03:00
} else {
return
}
outputBuffer = append(outputBuffer, aByte)
2018-03-13 03:54:37 +03:00
for i := 1; aByte == 0x00 && i != 4; i++ {
2018-05-06 10:18:17 +03:00
if p.isParsing {
2018-05-06 10:27:01 +03:00
aByte = <-p.inputChan
2018-05-06 10:18:17 +03:00
} else {
return
}
outputBuffer = append(outputBuffer, aByte)
2018-03-13 03:54:37 +03:00
if (aByte == 0x01 && i == 2) || (aByte == 0x01 && i == 3) {
if searchingForEnd {
2018-03-13 03:54:37 +03:00
output := append(append(itut.StartCode1(), itut.AUD()...), outputBuffer[:len(outputBuffer)-(i+1)]...)
time.Sleep(time.Duration(p.delay) * time.Millisecond)
p.parserOutputChanRef <- output
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
searchingForEnd = false
}
2018-05-06 10:18:17 +03:00
if p.isParsing {
2018-05-06 10:27:01 +03:00
aByte = <-p.inputChan
2018-05-06 10:18:17 +03:00
} else {
return
}
outputBuffer = append(outputBuffer, aByte)
2018-03-13 03:54:37 +03:00
if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 || nalType == 8 {
searchingForEnd = true
}
}
}
}
}