2019-11-15 07:41:53 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
2019-11-23 06:53:35 +03:00
|
|
|
jpeg.go contains code ported from FFmpeg's C implementation of an RTP
|
2019-11-23 08:04:59 +03:00
|
|
|
JPEG-compressed Video Depacketizer following RFC 2435. See
|
|
|
|
https://ffmpeg.org/doxygen/2.6/rtpdec__jpeg_8c_source.html and
|
|
|
|
https://tools.ietf.org/html/rfc2435).
|
2019-11-23 06:53:35 +03:00
|
|
|
|
2019-11-23 08:04:59 +03:00
|
|
|
This code can be used to build JPEG images from an RTP/JPEG stream.
|
2019-11-15 07:41:53 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-11-23 06:53:35 +03:00
|
|
|
Copyright (c) 2012 Samuel Pitoiset.
|
|
|
|
|
|
|
|
This file is part of FFmpeg.
|
2019-11-15 07:41:53 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
FFmpeg is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2019-11-15 07:41:53 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
FFmpeg 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
|
|
|
|
Lesser General Public License for more details.
|
2019-11-15 07:41:53 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with FFmpeg; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2019-11-15 07:41:53 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
package mjpeg
|
|
|
|
|
2019-11-15 08:55:35 +03:00
|
|
|
import (
|
2020-01-03 18:01:39 +03:00
|
|
|
"encoding/binary"
|
2019-11-23 06:53:35 +03:00
|
|
|
"errors"
|
2019-11-15 08:55:35 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
2019-11-15 08:11:02 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
const maxJPEG = 1000000 // 1 MB (arbitrary)
|
|
|
|
|
2019-11-15 07:41:53 +03:00
|
|
|
// JPEG marker codes.
|
|
|
|
const (
|
2019-11-16 15:42:08 +03:00
|
|
|
codeSOI = 0xd8 // Start of image.
|
|
|
|
codeDRI = 0xdd // Define restart interval.
|
|
|
|
codeDQT = 0xdb // Define quantization tables.
|
2019-11-22 05:35:11 +03:00
|
|
|
codeDHT = 0xc4 // Define huffman tables.
|
2019-11-16 15:42:08 +03:00
|
|
|
codeSOS = 0xda // Start of scan.
|
|
|
|
codeAPP0 = 0xe0 // TODO: find out what this is.
|
|
|
|
codeSOF0 = 0xc0 // Baseline
|
2019-11-20 06:10:07 +03:00
|
|
|
codeEOI = 0xd9 // End of image.
|
2019-11-16 15:42:08 +03:00
|
|
|
)
|
|
|
|
|
2020-01-01 11:04:51 +03:00
|
|
|
// Density units.
|
|
|
|
const (
|
|
|
|
unitNone = iota
|
2020-01-01 11:45:32 +03:00
|
|
|
unitPxIN // Pixels per inch.
|
|
|
|
unitPxCM // Pixels per centimeter.
|
2020-01-01 11:04:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// JFIF header fields.
|
|
|
|
const (
|
|
|
|
jfifLabel = "JFIF\000"
|
|
|
|
jfifVer = 0x0201
|
|
|
|
jfifDensityUnit = unitNone // Units for pixel density fields.
|
|
|
|
jfifXDensity = 1 // Horizontal pixel desnity.
|
|
|
|
jfifYDensity = 1 // Vertical pixel density.
|
|
|
|
jfifXThumbCnt = 0 // Horizontal pixel count of embedded thumbnail.
|
|
|
|
jfifYThumbCnt = 0 // Vertical pixel count of embedded thumbnail.
|
|
|
|
jfifHeadLen = 16 // Length of JFIF header segment excluding APP0 marker.
|
|
|
|
)
|
|
|
|
|
2020-01-02 03:50:21 +03:00
|
|
|
// SOF0 (start of frame) header fields.
|
2020-01-01 11:45:32 +03:00
|
|
|
const (
|
2020-01-02 04:00:51 +03:00
|
|
|
sofLen = 17 // Length of SOF0 segment excluding marker.
|
|
|
|
sofPrecision = 8 // Data precision in bits/sample.
|
|
|
|
sofNoOfComponents = 3 // Number of components (1 = grey scaled, 3 = color YcbCr or YIQ 4 = color CMYK)
|
2020-01-01 11:45:32 +03:00
|
|
|
)
|
|
|
|
|
2020-01-02 03:50:21 +03:00
|
|
|
// SOS (start of scan) header fields.
|
|
|
|
const (
|
2020-01-02 04:00:51 +03:00
|
|
|
sosLen = 12 // Length of SOS segment excluding marker.
|
|
|
|
sosComponentsInScan = 3 // Number of components in scan.
|
2020-01-02 03:50:21 +03:00
|
|
|
)
|
|
|
|
|
2020-01-07 05:22:31 +03:00
|
|
|
// Errors returned from ParsePayload.
|
2019-11-23 06:53:35 +03:00
|
|
|
var (
|
2020-01-07 05:22:31 +03:00
|
|
|
ErrNoQTable = errors.New("no quantization table")
|
|
|
|
ErrReservedQ = errors.New("q value is reserved")
|
|
|
|
ErrUnimplementedType = errors.New("unimplemented RTP/JPEG type")
|
|
|
|
ErrUnsupportedPrecision = errors.New("unsupported precision")
|
2020-01-07 05:36:56 +03:00
|
|
|
ErrNoFrameStart = errors.New("missing start of frame")
|
2019-11-23 06:53:35 +03:00
|
|
|
)
|
|
|
|
|
2019-12-29 14:46:09 +03:00
|
|
|
// Slices used in the creation of huffman tables.
|
2019-11-16 15:42:08 +03:00
|
|
|
var (
|
|
|
|
bitsDCLum = []byte{0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
bitsDCChr = []byte{0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
|
|
|
|
bitsACLum = []byte{0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d}
|
|
|
|
bitsACChr = []byte{0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77}
|
|
|
|
valDC = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
|
|
|
valACLum = []byte{
|
|
|
|
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
|
|
|
|
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
|
|
|
|
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
|
|
|
|
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
|
|
|
|
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
|
|
|
|
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
|
|
|
|
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
|
|
|
|
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
|
|
|
|
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
|
|
|
|
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
|
|
|
|
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
|
|
|
|
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
|
|
|
|
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
|
|
|
|
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
|
|
|
|
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
|
|
|
|
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
|
|
|
|
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
|
|
|
|
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
|
|
|
|
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
|
|
|
|
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
|
|
|
|
0xf9, 0xfa,
|
|
|
|
}
|
|
|
|
|
|
|
|
valACChr = []byte{
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
|
|
|
|
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
|
|
|
|
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
|
|
|
|
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
|
|
|
|
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
|
|
|
|
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
|
|
|
|
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
|
|
|
|
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
|
|
|
|
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
|
|
|
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
|
|
|
|
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
|
|
|
|
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
|
|
|
|
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
|
|
|
|
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
|
|
|
|
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
|
|
|
|
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
|
|
|
|
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
|
|
|
|
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
|
|
|
|
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
|
|
|
|
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
|
|
|
|
0xf9, 0xfa,
|
|
|
|
}
|
2019-11-23 06:53:35 +03:00
|
|
|
)
|
2019-11-15 08:55:35 +03:00
|
|
|
|
2019-12-29 14:46:09 +03:00
|
|
|
var defaultQuantisers = []byte{
|
|
|
|
// Luma table.
|
|
|
|
16, 11, 12, 14, 12, 10, 16, 14,
|
|
|
|
13, 14, 18, 17, 16, 19, 24, 40,
|
|
|
|
26, 24, 22, 22, 24, 49, 35, 37,
|
|
|
|
29, 40, 58, 51, 61, 60, 57, 51,
|
|
|
|
56, 55, 64, 72, 92, 78, 64, 68,
|
|
|
|
87, 69, 55, 56, 80, 109, 81, 87,
|
|
|
|
95, 98, 103, 104, 103, 62, 77, 113,
|
|
|
|
121, 112, 100, 120, 92, 101, 103, 99,
|
|
|
|
|
|
|
|
/* chroma table */
|
|
|
|
17, 18, 18, 24, 21, 24, 47, 26,
|
|
|
|
26, 47, 99, 66, 56, 66, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:10:54 +03:00
|
|
|
// Context describes a RTP/JPEG parsing context that will keep track of the current
|
2019-12-23 05:24:31 +03:00
|
|
|
// JPEG (held by p), and the state of the quantization tables.
|
2019-12-29 15:10:54 +03:00
|
|
|
type Context struct {
|
2019-11-23 06:53:35 +03:00
|
|
|
qTables [128][128]byte
|
|
|
|
qTablesLen [128]byte
|
2020-01-03 18:20:56 +03:00
|
|
|
buf []byte
|
|
|
|
blen int
|
2019-12-29 15:17:38 +03:00
|
|
|
dst io.Writer
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
2019-11-15 08:55:35 +03:00
|
|
|
|
2019-12-29 15:17:38 +03:00
|
|
|
// NewContext will return a new Context with destination d.
|
2019-12-29 15:10:54 +03:00
|
|
|
func NewContext(d io.Writer) *Context {
|
|
|
|
return &Context{
|
2019-12-29 15:17:38 +03:00
|
|
|
dst: d,
|
2020-01-03 18:20:56 +03:00
|
|
|
buf: make([]byte, maxJPEG),
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
2019-11-15 08:55:35 +03:00
|
|
|
|
2019-12-23 05:29:25 +03:00
|
|
|
// ParsePayload will parse an RTP/JPEG payload and append to current image.
|
2019-12-29 15:10:54 +03:00
|
|
|
func (c *Context) ParsePayload(p []byte, m bool) error {
|
2020-01-03 18:01:39 +03:00
|
|
|
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
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2020-01-07 03:44:43 +03:00
|
|
|
var dri uint16 // Restart interval.
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
if t&0x40 != 0 {
|
2020-01-07 03:44:43 +03:00
|
|
|
dri = binary.BigEndian.Uint16(p[idx:])
|
2020-01-03 18:01:39 +03:00
|
|
|
idx += 4 // Ignore restart count (2 bytes).
|
2019-11-23 06:53:35 +03:00
|
|
|
t &= ^0x40
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
if t > 1 {
|
2020-01-07 05:22:31 +03:00
|
|
|
return ErrUnimplementedType
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// Parse quantization table if our offset is 0.
|
|
|
|
if off == 0 {
|
|
|
|
var qTable []byte
|
|
|
|
var qLen int
|
|
|
|
|
|
|
|
if q > 127 {
|
2020-01-03 18:01:39 +03:00
|
|
|
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
|
2019-11-23 06:53:35 +03:00
|
|
|
|
|
|
|
if prec != 0 {
|
2020-01-07 05:22:31 +03:00
|
|
|
return ErrUnsupportedPrecision
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 05:31:34 +03:00
|
|
|
q -= 128
|
2019-11-23 06:53:35 +03:00
|
|
|
if qLen > 0 {
|
2020-01-03 18:01:39 +03:00
|
|
|
qTable = p[idx : idx+qLen]
|
|
|
|
idx += qLen
|
2019-11-23 06:53:35 +03:00
|
|
|
|
2020-01-07 05:31:34 +03:00
|
|
|
if q < 127 && c.qTablesLen[q] == 0 && qLen <= 0 {
|
|
|
|
copy(c.qTables[q][:], qTable)
|
|
|
|
c.qTablesLen[q] = byte(qLen)
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-07 05:31:34 +03:00
|
|
|
if q == 127 {
|
2020-01-07 05:22:31 +03:00
|
|
|
return ErrNoQTable
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 05:31:34 +03:00
|
|
|
if c.qTablesLen[q] == 0 {
|
2019-11-23 06:53:35 +03:00
|
|
|
return fmt.Errorf("no quantization tables known for q %d yet", q)
|
|
|
|
}
|
|
|
|
|
2020-01-07 05:31:34 +03:00
|
|
|
qTable = c.qTables[q][:]
|
|
|
|
qLen = int(c.qTablesLen[q])
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
|
|
|
} else { // q <= 127
|
|
|
|
if q == 0 || q > 99 {
|
2020-01-07 05:22:31 +03:00
|
|
|
return ErrReservedQ
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
2020-01-03 18:01:39 +03:00
|
|
|
qTable = defaultQTable(int(q))
|
2019-11-23 06:53:35 +03:00
|
|
|
qLen = len(qTable)
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
|
|
|
|
2020-01-04 06:23:04 +03:00
|
|
|
c.blen = writeHeader(c.buf[c.blen:], int(t), int(width), int(height), qLen/64, dri, qTable)
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
|
|
|
|
2020-01-03 18:20:56 +03:00
|
|
|
if c.blen == 0 {
|
2019-11-23 06:53:35 +03:00
|
|
|
// Must have missed start of frame? So ignore and wait for start.
|
2020-01-07 05:36:56 +03:00
|
|
|
return ErrNoFrameStart
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// TODO: check that timestamp is consistent
|
|
|
|
// This will need expansion to RTP package to create Timestamp parsing func.
|
|
|
|
|
|
|
|
// TODO: could also check offset with how many bytes we currently have
|
|
|
|
// to determine if there are missing frames.
|
|
|
|
|
|
|
|
// Write frame data
|
2020-01-03 18:20:56 +03:00
|
|
|
rem := len(p)
|
|
|
|
f := p[idx:rem]
|
|
|
|
copy(c.buf[c.blen:], f)
|
|
|
|
c.blen += len(f)
|
2020-01-03 18:01:39 +03:00
|
|
|
idx += rem
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
if m {
|
|
|
|
// End of image marker.
|
2020-01-03 18:20:56 +03:00
|
|
|
binary.BigEndian.PutUint16(c.buf[c.blen:], 0xff00|codeEOI)
|
|
|
|
c.blen += 2
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2020-01-03 18:20:56 +03:00
|
|
|
n, err := c.dst.Write(c.buf[0:c.blen])
|
2019-11-16 15:42:08 +03:00
|
|
|
if err != nil {
|
2019-11-23 06:53:35 +03:00
|
|
|
return fmt.Errorf("could not write JPEG to dst: %w", err)
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
2020-01-03 18:20:56 +03:00
|
|
|
c.blen -= n
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
2019-11-23 06:53:35 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// writeHeader writes a JPEG header to the writer w.
|
2020-01-07 03:44:43 +03:00
|
|
|
func writeHeader(p []byte, _type, width, height, nbqTab int, dri uint16, qtable []byte) int {
|
2019-11-23 06:53:35 +03:00
|
|
|
width <<= 3
|
|
|
|
height <<= 3
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// Indicate start of image.
|
2020-01-04 05:40:40 +03:00
|
|
|
idx := 0
|
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0xff00|codeSOI)
|
2019-11-22 05:35:11 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// Write JFIF header.
|
2020-01-07 05:39:27 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx+2:], 0xff00|codeAPP0)
|
|
|
|
binary.BigEndian.PutUint16(p[idx+4:], jfifHeadLen)
|
|
|
|
idx += 6
|
2020-01-04 05:40:40 +03:00
|
|
|
|
2020-01-07 05:41:25 +03:00
|
|
|
copy(p[idx:], jfifLabel)
|
|
|
|
idx += len(jfifLabel)
|
2020-01-04 05:40:40 +03:00
|
|
|
|
|
|
|
binary.BigEndian.PutUint16(p[idx:], jfifVer)
|
|
|
|
p[idx+2] = jfifDensityUnit
|
|
|
|
binary.BigEndian.PutUint16(p[idx+3:], jfifXDensity)
|
|
|
|
binary.BigEndian.PutUint16(p[idx+5:], jfifYDensity)
|
|
|
|
p[idx+7] = jfifXThumbCnt
|
|
|
|
p[idx+8] = jfifYThumbCnt
|
|
|
|
idx += 9
|
2019-11-23 06:53:35 +03:00
|
|
|
|
|
|
|
// If we want to define restart interval then write that.
|
|
|
|
if dri != 0 {
|
2020-01-04 05:40:40 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0xff00|codeDRI)
|
|
|
|
binary.BigEndian.PutUint16(p[idx+2:], 4)
|
2020-01-07 05:42:15 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx+4:], dri)
|
2020-01-04 05:40:40 +03:00
|
|
|
idx += 6
|
2019-11-22 05:35:11 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// Define quantization tables.
|
2020-01-04 05:40:40 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0xff00|codeDQT)
|
2019-11-23 06:53:35 +03:00
|
|
|
|
|
|
|
// Calculate table size and create slice for table.
|
|
|
|
ts := 2 + nbqTab*(1+64)
|
2020-01-07 05:44:18 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx+2:], uint16(ts))
|
|
|
|
idx += 4
|
2019-11-23 06:53:35 +03:00
|
|
|
|
|
|
|
for i := 0; i < nbqTab; i++ {
|
2020-01-04 05:40:40 +03:00
|
|
|
p[idx] = byte(i)
|
|
|
|
idx++
|
|
|
|
|
2020-01-07 06:23:26 +03:00
|
|
|
copy(p[idx:], qtable[64*i:(64*i)+64])
|
2019-11-22 05:35:11 +03:00
|
|
|
}
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// Define huffman table.
|
2020-01-04 05:40:40 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0xff00|codeDHT)
|
|
|
|
idx += 2
|
|
|
|
lenIdx := idx
|
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0)
|
|
|
|
idx += 2
|
|
|
|
idx += writeHuffman(p[idx:], 0, 0, bitsDCLum, valDC)
|
|
|
|
idx += writeHuffman(p[idx:], 0, 1, bitsDCChr, valDC)
|
|
|
|
idx += writeHuffman(p[idx:], 1, 0, bitsACLum, valACLum)
|
|
|
|
idx += writeHuffman(p[idx:], 1, 1, bitsACChr, valACChr)
|
|
|
|
binary.BigEndian.PutUint16(p[lenIdx:], uint16(idx-lenIdx))
|
2019-11-23 06:53:35 +03:00
|
|
|
|
2019-11-16 15:42:08 +03:00
|
|
|
// Start of frame.
|
2020-01-04 05:40:40 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0xff00|codeSOF0)
|
|
|
|
idx += 2
|
2019-11-16 15:42:08 +03:00
|
|
|
|
|
|
|
// Derive sample type.
|
|
|
|
sample := 1
|
|
|
|
if _type != 0 {
|
|
|
|
sample = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive matrix number.
|
2020-01-07 03:38:36 +03:00
|
|
|
var mtxNo uint8
|
2019-11-16 15:42:08 +03:00
|
|
|
if nbqTab == 2 {
|
|
|
|
mtxNo = 1
|
|
|
|
}
|
|
|
|
|
2020-01-04 05:40:40 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx:], sofLen)
|
|
|
|
p[idx+2] = byte(sofPrecision)
|
|
|
|
binary.BigEndian.PutUint16(p[idx+3:], uint16(height))
|
|
|
|
binary.BigEndian.PutUint16(p[idx+5:], uint16(width))
|
|
|
|
p[idx+7] = byte(sofNoOfComponents)
|
|
|
|
idx += 8
|
2020-01-01 11:45:32 +03:00
|
|
|
|
|
|
|
// TODO: find meaning of these fields.
|
2020-01-07 03:38:36 +03:00
|
|
|
idx += copy(p[idx:], []byte{1, uint8((2 << 4) | sample), 0, 2, 1<<4 | 1, mtxNo, 3, 1<<4 | 1, mtxNo})
|
2019-11-16 15:42:08 +03:00
|
|
|
|
|
|
|
// Write start of scan.
|
2020-01-04 05:40:40 +03:00
|
|
|
binary.BigEndian.PutUint16(p[idx:], 0xff00|codeSOS)
|
|
|
|
binary.BigEndian.PutUint16(p[idx+2:], sosLen)
|
|
|
|
p[idx+4] = sosComponentsInScan
|
|
|
|
idx += 5
|
2020-01-02 03:50:21 +03:00
|
|
|
|
|
|
|
// TODO: find out what remaining fields are.
|
2020-01-07 03:38:36 +03:00
|
|
|
idx += copy(p[idx:], []byte{1, 0, 2, 17, 3, 17, 0, 63, 0})
|
2020-01-04 05:40:40 +03:00
|
|
|
|
|
|
|
return idx
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 06:10:07 +03:00
|
|
|
// writeHuffman write a JPEG huffman table to w.
|
2020-01-04 05:40:40 +03:00
|
|
|
func writeHuffman(p []byte, class, id int, bits, values []byte) int {
|
|
|
|
idx := 0
|
|
|
|
p[idx] = uint8(class<<4 | id)
|
|
|
|
idx++
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-20 06:10:07 +03:00
|
|
|
var n int
|
|
|
|
for i := 1; i <= 16; i++ {
|
|
|
|
n += int(bits[i])
|
|
|
|
}
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2020-01-07 06:23:26 +03:00
|
|
|
idx += copy(p[idx:], bits[1:17])
|
|
|
|
idx += copy(p[idx:], values[0:n])
|
2020-01-04 05:40:40 +03:00
|
|
|
|
|
|
|
return idx
|
2019-11-23 06:53:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaultQTable returns a default quantization table.
|
|
|
|
func defaultQTable(q int) []byte {
|
|
|
|
f := clip(q, q, 99)
|
|
|
|
const tabLen = 128
|
|
|
|
tab := make([]byte, tabLen)
|
|
|
|
|
|
|
|
if q < 50 {
|
|
|
|
q = 5000 / f
|
|
|
|
} else {
|
|
|
|
q = 200 - f*2
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
for i := 0; i < tabLen; i++ {
|
|
|
|
v := (int(defaultQuantisers[i])*q + 50) / 100
|
|
|
|
v = clip(v, 1, 255)
|
|
|
|
tab[i] = byte(v)
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
2019-11-23 06:53:35 +03:00
|
|
|
return tab
|
|
|
|
}
|
2019-11-16 15:42:08 +03:00
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// clip clips the value v to the bounds defined by min and max.
|
|
|
|
func clip(v, min, max int) int {
|
|
|
|
if v < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
|
|
|
|
if v > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
2019-11-16 15:42:08 +03:00
|
|
|
}
|
2020-01-07 05:36:56 +03:00
|
|
|
|
|
|
|
// get24 parses an int24 from p using big endian order.
|
|
|
|
func get24(p []byte) int {
|
|
|
|
return int(p[0]<<16) | int(p[1]<<8) | int(p[2])
|
|
|
|
}
|