codec/mjpeg/jpeg.go: removed usage of putBuffer in parsePayload

This commit is contained in:
Saxon 2020-01-04 01:50:56 +10:30
parent 7ee8fa566c
commit 692325523c
1 changed files with 16 additions and 12 deletions

View File

@ -173,7 +173,8 @@ var defaultQuantisers = []byte{
type Context struct { type Context struct {
qTables [128][128]byte qTables [128][128]byte
qTablesLen [128]byte qTablesLen [128]byte
buf *putBuffer buf []byte
blen int
dst io.Writer dst io.Writer
} }
@ -181,7 +182,7 @@ type Context struct {
func NewContext(d io.Writer) *Context { func NewContext(d io.Writer) *Context {
return &Context{ return &Context{
dst: d, dst: d,
buf: newPutBuffer(make([]byte, maxJPEG)), buf: make([]byte, maxJPEG),
} }
} }
@ -250,12 +251,14 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
qLen = len(qTable) qLen = len(qTable)
} }
c.buf.reset() c.blen = 0
writeHeader(c.buf, int(t), int(width), int(height), qLen/64, dri, qTable) putBuf := newPutBuffer(c.buf[c.blen:])
writeHeader(putBuf, int(t), int(width), int(height), qLen/64, dri, qTable)
c.blen += putBuf.len
} }
if c.buf.len == 0 { if c.blen == 0 {
// Must have missed start of frame? So ignore and wait for start. // Must have missed start of frame? So ignore and wait for start.
return nil return nil
} }
@ -267,21 +270,22 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
// to determine if there are missing frames. // to determine if there are missing frames.
// Write frame data // Write frame data
rem := len(p) - idx rem := len(p)
_, err := c.buf.Write(p[idx : idx+rem]) f := p[idx:rem]
if err != nil { copy(c.buf[c.blen:], f)
return fmt.Errorf("could not write remaining frame data to output buffer: %w", err) c.blen += len(f)
}
idx += rem idx += rem
if m { if m {
// End of image marker. // End of image marker.
c.buf.put16(0xff00 | codeEOI) binary.BigEndian.PutUint16(c.buf[c.blen:], 0xff00|codeEOI)
c.blen += 2
_, err = c.buf.writeTo(c.dst) n, err := c.dst.Write(c.buf[0:c.blen])
if err != nil { if err != nil {
return fmt.Errorf("could not write JPEG to dst: %w", err) return fmt.Errorf("could not write JPEG to dst: %w", err)
} }
c.blen -= n
} }
return nil return nil
} }