Looks like parser is working
Parser can successfully extract individual jpeg images
|
@ -29,7 +29,9 @@ LICENSE
|
||||||
package mpegts
|
package mpegts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bitbucket.org/ausocean/av/tools"
|
//"bitbucket.org/ausocean/av/tools"
|
||||||
|
"../tools"
|
||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
//"fmt"
|
//"fmt"
|
||||||
)
|
)
|
||||||
|
@ -188,7 +190,7 @@ func (p *MpegTsPacket) ToByteSlice() (output []byte, err error) {
|
||||||
output = append(output, stuffing...)
|
output = append(output, stuffing...)
|
||||||
}
|
}
|
||||||
output = append(output, p.Payload...)
|
output = append(output, p.Payload...)
|
||||||
|
|
||||||
if len(output) != 188 {
|
if len(output) != 188 {
|
||||||
err = errors.New("Length of MPEG-TS packet is not 188! Something is wrong!")
|
err = errors.New("Length of MPEG-TS packet is not 188! Something is wrong!")
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,16 +26,20 @@ LICENSE
|
||||||
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package h264
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_"fmt"
|
_"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"bitbucket.org/ausocean/av/mpegts"
|
//"bitbucket.org/ausocean/av/mpegts"
|
||||||
"bitbucket.org/ausocean/av/rtp"
|
//"bitbucket.org/ausocean/av/rtp"
|
||||||
"bitbucket.org/ausocean/av/tools"
|
//"bitbucket.org/ausocean/av/tools"
|
||||||
"bitbucket.org/ausocean/av/itut"
|
//"bitbucket.org/ausocean/av/itut"
|
||||||
|
"../mpegts"
|
||||||
|
"../rtp"
|
||||||
|
"../tools"
|
||||||
|
"../itut"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RtpToH264Converter interface {
|
type RtpToH264Converter interface {
|
||||||
|
|
|
@ -25,13 +25,14 @@ LICENSE
|
||||||
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package h264
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bitbucket.org/ausocean/av/itut"
|
//"bitbucket.org/ausocean/av/itut"
|
||||||
|
"../itut"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
_"fmt"
|
"fmt"
|
||||||
_"time"
|
_"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -55,14 +56,15 @@ type Parser interface {
|
||||||
type h264Parser struct {
|
type h264Parser struct {
|
||||||
inputBuffer []byte
|
inputBuffer []byte
|
||||||
isParsing bool
|
isParsing bool
|
||||||
OutputChan chan<- []byte
|
parserOutputChanRef chan<- []byte
|
||||||
InputChan chan byte
|
userOutputChanRef <-chan []byte
|
||||||
|
inputChan chan byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewH264Parser() (p *h264Parser) {
|
func NewH264Parser() (p *h264Parser) {
|
||||||
p = new(h264Parser)
|
p = new(h264Parser)
|
||||||
p.isParsing = true
|
p.isParsing = true
|
||||||
p.InputChan = make(chan byte, 10000)
|
p.inputChan = make(chan byte, 10000)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,27 +76,36 @@ func (p *h264Parser)Start(){
|
||||||
go p.parse()
|
go p.parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *h264Parser)GetInput() chan byte {
|
func (p *h264Parser)GetInputChan() chan byte {
|
||||||
|
return p.inputChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *H264Parser)parse() {
|
func (p *h264Parser)GetOutputChan() <-chan []byte {
|
||||||
|
return p.userOutputChanRef
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *h264Parser)SetOutputChan(aChan chan []byte){
|
||||||
|
p.parserOutputChanRef = aChan
|
||||||
|
p.userOutputChanRef = aChan
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *h264Parser)parse() {
|
||||||
outputBuffer := make([]byte, 0, 10000)
|
outputBuffer := make([]byte, 0, 10000)
|
||||||
searchingForEnd := false
|
searchingForEnd := false
|
||||||
for p.isParsing {
|
for p.isParsing {
|
||||||
aByte := <-p.InputChan
|
aByte := <-p.inputChan
|
||||||
outputBuffer = append(outputBuffer, aByte)
|
outputBuffer = append(outputBuffer, aByte)
|
||||||
for i:=1; aByte == 0x00 && i != 4; i++ {
|
for i:=1; aByte == 0x00 && i != 4; i++ {
|
||||||
aByte = <-p.InputChan
|
aByte = <-p.inputChan
|
||||||
outputBuffer = append(outputBuffer, aByte)
|
outputBuffer = append(outputBuffer, aByte)
|
||||||
if ( aByte == 0x01 && i == 2 ) || ( aByte == 0x01 && i == 3 ) {
|
if ( aByte == 0x01 && i == 2 ) || ( aByte == 0x01 && i == 3 ) {
|
||||||
if searchingForEnd {
|
if searchingForEnd {
|
||||||
output := append(append(itut.StartCode1(),itut.AUD()...),outputBuffer[:len(outputBuffer)-(i+1)]...)
|
output := append(append(itut.StartCode1(),itut.AUD()...),outputBuffer[:len(outputBuffer)-(i+1)]...)
|
||||||
p.OutputChan<-output
|
p.parserOutputChanRef<-output
|
||||||
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
|
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
|
||||||
searchingForEnd = false
|
searchingForEnd = false
|
||||||
}
|
}
|
||||||
aByte = <-p.InputChan
|
aByte = <-p.inputChan
|
||||||
outputBuffer = append(outputBuffer, aByte)
|
outputBuffer = append(outputBuffer, aByte)
|
||||||
if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 {
|
if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 {
|
||||||
searchingForEnd = true
|
searchingForEnd = true
|
||||||
|
@ -108,14 +119,15 @@ func (p *H264Parser)parse() {
|
||||||
type mjpegParser struct {
|
type mjpegParser struct {
|
||||||
inputBuffer []byte
|
inputBuffer []byte
|
||||||
isParsing bool
|
isParsing bool
|
||||||
outputChan chan<- []byte
|
parserOutputChanRef chan<- []byte
|
||||||
|
userOutputChanRef <-chan []byte
|
||||||
inputChan chan byte
|
inputChan chan byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMJPEGParser() (p *mjpegParser){
|
func NewMJPEGParser(inputChanLen int) (p *mjpegParser){
|
||||||
p = new(mjpegParser)
|
p = new(mjpegParser)
|
||||||
p.isParsing = true
|
p.isParsing = true
|
||||||
p.InputChan = make(chan byte, 10000)
|
p.inputChan = make(chan byte, inputChanLen )
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,21 +139,31 @@ func (p *mjpegParser)Start(){
|
||||||
go p.parse()
|
go p.parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *mjpegParser)GetInput() chan byte {
|
func (p *mjpegParser)GetInputChan() chan byte {
|
||||||
return p.inputChan
|
return p.inputChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *mjpegParser)GetOutputChan() <-chan []byte {
|
||||||
|
return p.userOutputChanRef
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *mjpegParser)SetOutputChan(aChan chan []byte){
|
||||||
|
p.parserOutputChanRef = aChan
|
||||||
|
p.userOutputChanRef = aChan
|
||||||
|
}
|
||||||
|
|
||||||
func (p *mjpegParser)parse() {
|
func (p *mjpegParser)parse() {
|
||||||
outputBuffer := make([]byte, 0, 10000)
|
var outputBuffer []byte
|
||||||
for p.isParsing {
|
for p.isParsing {
|
||||||
aByte := <-p.InputChan
|
aByte := <-p.inputChan
|
||||||
outputBuffer = append(outputBuffer, aByte)
|
outputBuffer = append(outputBuffer, aByte)
|
||||||
if aByte = 0xFF && len(outputBuffer) > 1 {
|
if aByte == 0xFF && len(outputBuffer) != 0 {
|
||||||
aByte := <-p.InputChan
|
aByte := <-p.inputChan
|
||||||
outputBuffer = append(outputBuffer, aByte)
|
outputBuffer = append(outputBuffer, aByte)
|
||||||
if aByte = 0xD8 {
|
if aByte == 0xD8 {
|
||||||
p.OutputChan<-output
|
fmt.Printf("image: %v\n", outputBuffer[:len(outputBuffer)-2])
|
||||||
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
|
p.parserOutputChanRef<-outputBuffer[:len(outputBuffer)-2]
|
||||||
|
outputBuffer = outputBuffer[len(outputBuffer)-2:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,36 +2,52 @@ package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"strconv"
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
testInputFileName = "inputFiles/inputFile.avi"
|
testInputFileName = "testInput/testInput.avi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMJPEGParser(t *testing.T){
|
func TestMJPEGParser(t *testing.T){
|
||||||
parser := NewMJPEGParser()
|
fmt.Println("Opening input file!")
|
||||||
// TODO: Find mjpeg file and see if the mjpeg parser can output individual
|
// Open the input file
|
||||||
// jpeg images...
|
inputFile, err := os.Open(testInputFileName)
|
||||||
inputFile, err = os.Open(testInputFileName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Should not have got error opening file!")
|
t.Errorf("Should not have got error opening file!")
|
||||||
}
|
}
|
||||||
|
fmt.Println("Getting file stats!")
|
||||||
stats, err := inputFile.Stat()
|
stats, err := inputFile.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not get input file stats!")
|
t.Errorf("Could not get input file stats!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println("Creating space for file data!")
|
||||||
data := make([]byte, stats.Size())
|
data := make([]byte, stats.Size())
|
||||||
_, err = r.inputFile.Read(data)
|
_, err = inputFile.Read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Should not have got read error!")
|
t.Errorf("Should not have got read error!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println("Creating parser!")
|
||||||
|
parser := NewMJPEGParser(len(data)+1)
|
||||||
|
parser.SetOutputChan(make(chan []byte, 10000))
|
||||||
|
parser.Start()
|
||||||
|
fmt.Printf("len(data): %v\n", len(data))
|
||||||
for i := range data {
|
for i := range data {
|
||||||
parser.InputByteChan <- data[i]
|
parser.GetInputChan() <- data[i]
|
||||||
}
|
}
|
||||||
|
fmt.Println("Writing jpegs to files!")
|
||||||
for len(parser.GetOutputByteChan()) > 0 {
|
for i:=0; len(parser.GetOutputChan()) > 0; i++ {
|
||||||
|
// Open a new output file
|
||||||
|
outputFile, err := os.Create("testOutput/image"+strconv.Itoa(i)+".jpeg")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Should not have got error creating output file!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
outputFile.Write(<-parser.GetOutputChan())
|
||||||
|
outputFile.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |