2018-08-28 13:58:57 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
lex.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2021-01-16 08:52:56 +03:00
|
|
|
lex.go provides a lexer to extract separate JPEG images from a JPEG stream.
|
|
|
|
This could either be a series of descrete JPEG images, or an MJPEG stream.
|
2018-08-28 13:58:57 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
2021-01-16 08:52:56 +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.
|
|
|
|
*/
|
|
|
|
|
2021-01-18 07:59:21 +03:00
|
|
|
package jpeg
|
2018-08-28 13:58:57 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
2021-01-16 08:52:56 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2018-08-28 13:58:57 +03:00
|
|
|
)
|
|
|
|
|
2021-01-16 08:52:56 +03:00
|
|
|
var Log logger.LoggerIF
|
|
|
|
|
2018-08-28 13:58:57 +03:00
|
|
|
var noDelay = make(chan time.Time)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
close(noDelay)
|
|
|
|
}
|
|
|
|
|
2021-01-16 08:52:56 +03:00
|
|
|
// Lex parses JPEG frames read from src into separate writes to dst with
|
2018-08-28 13:58:57 +03:00
|
|
|
// successive writes being performed not earlier than the specified delay.
|
2019-04-26 14:16:43 +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
|
|
|
|
}
|
|
|
|
|
2019-04-26 14:01:12 +03:00
|
|
|
r := bufio.NewReader(src)
|
2018-08-28 13:58:57 +03:00
|
|
|
for {
|
2019-04-26 14:01:12 +03:00
|
|
|
buf := make([]byte, 2, 4<<10)
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
if n < 2 {
|
2020-01-20 07:33:49 +03:00
|
|
|
return io.ErrUnexpectedEOF
|
2019-04-26 14:01:12 +03:00
|
|
|
}
|
2018-08-28 13:58:57 +03:00
|
|
|
if err != nil {
|
2020-01-20 07:33:49 +03:00
|
|
|
return err
|
2018-08-28 13:58:57 +03:00
|
|
|
}
|
2021-01-16 08:52:56 +03:00
|
|
|
|
2019-04-26 14:01:12 +03:00
|
|
|
if !bytes.Equal(buf, []byte{0xff, 0xd8}) {
|
2021-01-16 08:52:56 +03:00
|
|
|
return fmt.Errorf("parser: not JPEG frame start: %#v", buf)
|
2019-04-26 14:01:12 +03:00
|
|
|
}
|
2021-01-16 08:52:56 +03:00
|
|
|
|
|
|
|
nImg := 1
|
|
|
|
|
2019-04-26 14:01:12 +03:00
|
|
|
var last byte
|
|
|
|
for {
|
|
|
|
b, err := r.ReadByte()
|
2018-08-28 13:58:57 +03:00
|
|
|
if err != nil {
|
2020-01-20 07:33:49 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return err
|
2018-08-28 13:58:57 +03:00
|
|
|
}
|
2021-01-16 08:52:56 +03:00
|
|
|
|
2018-08-28 13:58:57 +03:00
|
|
|
buf = append(buf, b)
|
2021-01-16 08:52:56 +03:00
|
|
|
|
|
|
|
if last == 0xff && b == 0xd8 {
|
|
|
|
nImg++
|
|
|
|
}
|
|
|
|
|
2019-04-26 14:01:12 +03:00
|
|
|
if last == 0xff && b == 0xd9 {
|
2021-01-16 08:52:56 +03:00
|
|
|
nImg--
|
|
|
|
}
|
|
|
|
|
|
|
|
if nImg == 0 {
|
|
|
|
<-tick
|
|
|
|
Log.Debug("writing buf","len(buf)",len(buf))
|
|
|
|
_, err = dst.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-26 14:01:12 +03:00
|
|
|
break
|
2018-08-28 13:58:57 +03:00
|
|
|
}
|
2021-01-16 08:52:56 +03:00
|
|
|
|
2019-04-26 14:01:12 +03:00
|
|
|
last = b
|
|
|
|
}
|
2018-08-28 13:58:57 +03:00
|
|
|
}
|
|
|
|
}
|