mirror of https://bitbucket.org/ausocean/av.git
119 lines
2.4 KiB
Go
119 lines
2.4 KiB
Go
/*
|
|
DESCRIPTION
|
|
utils.go provides buffer utilities used by jpeg.go.
|
|
|
|
TODO: make this exported in codecutil.
|
|
|
|
AUTHOR
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
LICENSE
|
|
Copyright (C) 2019 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.
|
|
*/
|
|
|
|
package mjpeg
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"io"
|
|
)
|
|
|
|
type putBuffer struct {
|
|
len int
|
|
bytes []byte
|
|
}
|
|
|
|
func newPutBuffer(b []byte) *putBuffer { return &putBuffer{bytes: b} }
|
|
|
|
func (p *putBuffer) Write(b []byte) (int, error) {
|
|
copy(p.bytes[p.len:], b)
|
|
p.len += len(b)
|
|
return len(b), nil
|
|
}
|
|
|
|
func (p *putBuffer) writeTo(d io.Writer) (int, error) {
|
|
n, err := d.Write(p.bytes[0:p.len])
|
|
p.len -= n
|
|
return n, err
|
|
}
|
|
|
|
func (p *putBuffer) put16(v uint16) {
|
|
binary.BigEndian.PutUint16(p.bytes[p.len:], v)
|
|
p.len += 2
|
|
}
|
|
|
|
func (p *putBuffer) put8(v uint8) {
|
|
p.bytes[p.len] = byte(v)
|
|
p.len++
|
|
}
|
|
|
|
func (p *putBuffer) putBytes(src []byte) {
|
|
copy(p.bytes[p.len:], src)
|
|
p.len += len(src)
|
|
}
|
|
|
|
func (p *putBuffer) put16At(v uint16, i int) {
|
|
binary.BigEndian.PutUint16(p.bytes[i:], v)
|
|
}
|
|
|
|
func (p *putBuffer) reset() {
|
|
p.len = 0
|
|
}
|
|
|
|
type byteStream struct {
|
|
bytes []byte
|
|
i int
|
|
}
|
|
|
|
func newByteStream(b []byte) *byteStream { return &byteStream{bytes: b} }
|
|
|
|
func (b *byteStream) get24() int {
|
|
v := int(b.bytes[b.i])<<16 | int(b.bytes[b.i+1])<<8 | int(b.bytes[b.i+2])
|
|
b.i += 3
|
|
return v
|
|
}
|
|
|
|
func (b *byteStream) get8() int {
|
|
v := int(b.bytes[b.i])
|
|
b.i++
|
|
return v
|
|
}
|
|
|
|
func (b *byteStream) get16() int {
|
|
v := int(binary.BigEndian.Uint16(b.bytes[b.i:]))
|
|
b.i += 2
|
|
return v
|
|
}
|
|
|
|
func (b *byteStream) getBuf(n int) []byte {
|
|
v := b.bytes[b.i : b.i+n]
|
|
b.i += n
|
|
return v
|
|
}
|
|
|
|
func (b *byteStream) remaining() int {
|
|
return len(b.bytes) - b.i
|
|
}
|
|
|
|
func (b *byteStream) writeTo(w io.Writer, n int) error {
|
|
_n, err := w.Write(b.bytes[b.i : b.i+n])
|
|
b.i += _n
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|