codec/mjpeg: removed byteStream type and its usage

This commit is contained in:
Saxon 2020-01-04 01:31:39 +10:30
parent f67fcd35e7
commit 7ee8fa566c
3 changed files with 25 additions and 588 deletions

View File

@ -33,6 +33,7 @@ LICENSE
package mjpeg
import (
"encoding/binary"
"errors"
"fmt"
"io"
@ -186,20 +187,19 @@ func NewContext(d io.Writer) *Context {
// ParsePayload will parse an RTP/JPEG payload and append to current image.
func (c *Context) ParsePayload(p []byte, m bool) error {
b := newByteStream(p)
_ = b.get8() // Ignore type-specific flag
off := b.get24() // Fragment offset.
t := b.get8() // Type.
q := b.get8() // Quantization value.
width := b.get8() // Picture width.
height := b.get8() // Picture height.
idx := 1 // Ignore type-specific flag (skip to index 1).
off := get24(p[idx:]) // Fragment offset (3 bytes).
t := int(p[idx+3]) // Type (1 byte).
q := p[idx+4] // Quantization value (1 byte).
width := p[idx+5] // Picture width (1 byte).
height := p[idx+6] // Picture height (1 byte).
idx += 7
var dri int // Restart interval.
if t&0x40 != 0 {
dri = b.get16()
_ = b.get16() // Ignore restart count.
dri = int(binary.BigEndian.Uint16(p[idx:]))
idx += 4 // Ignore restart count (2 bytes).
t &= ^0x40
}
@ -213,16 +213,18 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
var qLen int
if q > 127 {
_ = b.get8() // Ignore first byte (reserved for future use).
prec := b.get8() // The size of coefficients.
qLen = b.get16() // The length of the quantization table.
idx++
prec := p[idx] // The size of coefficients (1 byte).
qLen = int(binary.BigEndian.Uint16(p[idx+1:])) // The length of the quantization table (2 bytes).
idx += 3
if prec != 0 {
panic("unsupported precision")
}
if qLen > 0 {
qTable = b.getBuf(qLen)
qTable = p[idx : idx+qLen]
idx += qLen
if q < 255 && c.qTablesLen[q-128] == 0 && qLen <= 128 {
copy(c.qTables[q-128][:], qTable)
@ -244,13 +246,13 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
if q == 0 || q > 99 {
return errReservedQ
}
qTable = defaultQTable(q)
qTable = defaultQTable(int(q))
qLen = len(qTable)
}
c.buf.reset()
writeHeader(c.buf, t, width, height, qLen/64, dri, qTable)
writeHeader(c.buf, int(t), int(width), int(height), qLen/64, dri, qTable)
}
if c.buf.len == 0 {
@ -265,10 +267,12 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
// to determine if there are missing frames.
// Write frame data
err := b.writeTo(c.buf, b.remaining())
rem := len(p) - idx
_, err := c.buf.Write(p[idx : idx+rem])
if err != nil {
return fmt.Errorf("could not write remaining frame data to output buffer: %w", err)
}
idx += rem
if m {
// End of image marker.
@ -282,6 +286,10 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
return nil
}
func get24(p []byte) int {
return int(p[0]<<16) | int(p[1]<<8) | int(p[2])
}
// writeHeader writes a JPEG header to the writer w.
func writeHeader(p *putBuffer, _type, width, height, nbqTab, dri int, qtable []byte) {
width <<= 3

File diff suppressed because one or more lines are too long

View File

@ -72,47 +72,3 @@ func (p *putBuffer) put16At(v uint16, i int) {
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
}