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