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 {
qTables [128][128]byte
qTablesLen [128]byte
buf *putBuffer
buf []byte
blen int
dst io.Writer
}
@ -181,7 +182,7 @@ type Context struct {
func NewContext(d io.Writer) *Context {
return &Context{
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)
}
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.
return nil
}
@ -267,21 +270,22 @@ func (c *Context) ParsePayload(p []byte, m bool) error {
// to determine if there are missing frames.
// Write frame data
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)
}
rem := len(p)
f := p[idx:rem]
copy(c.buf[c.blen:], f)
c.blen += len(f)
idx += rem
if m {
// 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 {
return fmt.Errorf("could not write JPEG to dst: %w", err)
}
c.blen -= n
}
return nil
}