av/h264/h264Parser.go

86 lines
2.1 KiB
Go

/*
NAME
RtpToTsConverter.go - provides utilities for the conversion of Rtp packets
to equivalent MpegTs packets.
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon.milton@gmail.com>
LICENSE
RtpToTsConverter.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 h264
import (
"bitbucket.org/ausocean/av/itut"
"log"
"sync"
_"fmt"
_"time"
)
const (
acceptedLength = 1000
)
var (
Info *log.Logger
mutex *sync.Mutex
)
type H264Parser struct {
inputBuffer []byte
isParsing bool
OutputChan chan<- []byte
InputByteChan chan byte
}
func (p* H264Parser)Stop(){
p.isParsing = false
}
func (p* H264Parser)Parse() {
p.isParsing = true
outputBuffer := []byte{}
searchingForEnd := false
p.InputByteChan = make(chan byte, 10000)
for p.isParsing {
aByte := <-p.InputByteChan
outputBuffer = append(outputBuffer, aByte)
for i:=1; aByte == 0x00 && i != 4; i++ {
aByte = <-p.InputByteChan
outputBuffer = append(outputBuffer, aByte)
if ( aByte == 0x01 && i == 2 ) || ( aByte == 0x01 && i == 3 ) {
if searchingForEnd {
output := append(append(itut.StartCode1(),itut.AUD()...),outputBuffer[:len(outputBuffer)-(i+1)]...)
p.OutputChan<-output
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
searchingForEnd = false
}
aByte = <-p.InputByteChan
outputBuffer = append(outputBuffer, aByte)
if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 {
searchingForEnd = true
}
}
}
}
}