1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Description: jpegd image file.
18 */
19 #include "jpegd_image.h"
20
21 static const unsigned char g_default_huffman_table[418] = { 0x01, 0xA2, /* 418 :huffman_table size */
22 /* table K.3 - table for luminance DC coefficient differences */
23 0x00,
24 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
25 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x0A, 0x0B,
26
27 /* table K.4 - table for chrominance DC coefficient differences */
28 0x01,
29 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
30 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x0A, 0x0B,
31
32 /* table K.5 - table for luminance AC coefficients */
33 0x10,
34 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7D,
35 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
36 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
37 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
38 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
39 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
40 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
41 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
42 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
43 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
44 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
45 0xF9, 0xFA,
46
47 /* table K.6 - table for chrominance AC coefficients */
48 0x11,
49 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
50 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
51 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
52 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
53 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
54 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
55 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
56 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
57 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
58 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
59 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
60 0xF9, 0xFA,
61 };
62
63 static const unsigned char g_default_quant_table[160] = { 0x00, 0x84, /* 160 :quant_table size */
64 /* table K.1 - luminance quantization table */
65 0x00,
66 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40,
67 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51,
68 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 87,
69 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99,
70
71 /* table K.2 - chrominance quantization table */
72 0x01,
73 17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, 99, 99,
74 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
75 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
76 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
77 };
78
jpegd_decode_dht_huffman_length(huffman_tab * huffman,const unsigned char ** str,unsigned char * ptr,unsigned int * error_flag)79 unsigned int jpegd_decode_dht_huffman_length(huffman_tab *huffman, const unsigned char **str,
80 unsigned char *ptr, unsigned int *error_flag)
81 {
82 unsigned int i, tab_len;
83 int max_code = 0;
84 const unsigned char *stream = *str;
85 for (i = 0, tab_len = 0; i < JPEGD_CODE_LEN; i++) {
86 ptr[i] = *stream++;
87
88 if (ptr[i]) {
89 huffman->min_code[i] = (unsigned int)(max_code) << 1;
90 huffman->max_code[i] = huffman->min_code[i] + ptr[i] - 1;
91 huffman->max_code16[i] =
92 ((unsigned int)(huffman->max_code[i] + 1) << (15 - i)) - 1; /* 16bit maximum shift left number15 */
93 huffman->len[i] = ptr[i];
94 huffman->huffman_val_ptr[i] = tab_len;
95 max_code = huffman->max_code[i] + 1;
96 *error_flag |= (unsigned int)((huffman->max_code[i] + 1) > (1 << (i + 1)));
97 } else {
98 huffman->min_code[i] = (unsigned int)max_code << 1;
99 huffman->max_code[i] = -1;
100 huffman->max_code16[i] = -1;
101 huffman->len[i] = 0;
102 huffman->huffman_val_ptr[i] = tab_len;
103 max_code = (unsigned int)max_code << 1;
104 }
105 tab_len += ptr[i];
106 }
107 huffman->max_code16[JPEGD_CODE_LEN - 1] = 0x0000ffff; /* 0x0000ffff:default value */
108 huffman->max_code[JPEGD_CODE_LEN - 1] = huffman->min_code[JPEGD_CODE_LEN - 1] + ptr[JPEGD_CODE_LEN - 1] - 1;
109 *str += JPEGD_CODE_LEN;
110 return tab_len;
111 }
112
jpegd_decode_dht_huffman_val(huffman_tab * huffman,const unsigned char ** str,const unsigned char * ptr,unsigned int tab_len)113 void jpegd_decode_dht_huffman_val(huffman_tab *huffman, const unsigned char **str, const unsigned char *ptr,
114 unsigned int tab_len)
115 {
116 unsigned int i;
117 unsigned int ii = 0;
118 unsigned int jj;
119 const unsigned char *stream = *str;
120 for (i = 0; i < sizeof(huffman->huffman_val); i++) {
121 huffman->huffman_val[i] = 0;
122 }
123 for (i = 0; (i < tab_len) && (i < JPEGD_HUFFMAN_LEN); i++) {
124 huffman->huffman_val[i] = *stream++;
125 }
126
127 for (i = 0, jj = 0; i < 8; i++) { /* finds the first 8 codes when Lookup table */
128 for (ii = 0; ii < ptr[i]; ii++) {
129 int count = 1 << (7 - i); /* 7:finds the first 8 codes when Lookup table */
130 unsigned char huffmancode = huffman->huffman_val[huffman->huffman_val_ptr[i] + ii];
131 while ((count-- != 0) && (jj < JPEGD_HUFFMAN_LEN)) {
132 huffman->fast_huffman_len[jj] = i + 1;
133 huffman->fast_huffman_code[jj++] = huffmancode;
134 }
135 }
136 }
137 huffman->u_code32 = (jj - 1) << 24; /* Extended 32 bit, set 0 to low 24 bits */
138 *str += tab_len;
139 return;
140 }
141
decode_dht(motion_jpeg_obj * mjpeg,const unsigned char * stream)142 int decode_dht(motion_jpeg_obj *mjpeg, const unsigned char *stream)
143 {
144 huffman_tab *huffman = &mjpeg->huffman_tmp;
145 unsigned int error_flag = 0;
146 unsigned int tab_len;
147 unsigned char ptr[JPEGD_CODE_LEN];
148 unsigned int k = 2; /* 2:DHT maker takes 2 bytes */
149 unsigned int lh;
150 unsigned char code, tc, th;
151
152 lh = (stream[0] << 8) + stream[1]; /* 0,1:The first two bytes are dht length, shift left 8 bits */
153 stream += 2; /* Data length is 2 bytes */
154
155 while (k < lh) {
156 code = *stream++;
157 tc = (code >> 4) & 0x1; /* get top 4 bits 0x1:get 0 bit value */
158 th = code & 0x1; /* 0x1:get 0 bit value */
159 error_flag |= (unsigned int)(code & 0xEE); /* 0xEE:lh max value */
160 if (error_flag != 0) {
161 return HI_JPEG_DEC_DHT_ERR;
162 }
163 tab_len = jpegd_decode_dht_huffman_length(huffman, &stream, ptr, &error_flag);
164 k += 17 + tab_len; /* 17:tc and th take one byte, length take 16 bytes */
165 error_flag |= (unsigned int)(tab_len > 255); /* 255 max table length */
166 if ((k > lh) || error_flag) {
167 return HI_JPEG_DEC_DHT_ERR;
168 }
169
170 jpegd_decode_dht_huffman_val(huffman, &stream, ptr, tab_len);
171 mjpeg->h_tab[(tc << 1) + th] = *huffman;
172 }
173
174 return HI_JPEG_DEC_OK;
175 }
176
check_sof_bit(frame_header * header,motion_jpeg_obj * mjpeg,int ls)177 unsigned int check_sof_bit(frame_header *header, motion_jpeg_obj *mjpeg, int ls)
178 {
179 if (header->color_depth != 8) { /* only support 8 sample precision */
180 return HI_ERR_NOT_BASELINE;
181 }
182
183 if (ls != (header->nf * 3 + 8)) { /* every components takes 3 bytes, previous info take 8 bytes */
184 return HI_ERR_FRAME_HEADER;
185 }
186
187 if ((header->y_height > mjpeg->max_height) || (header->y_height == 0)) {
188 return HI_UNSUPPORT_PIC_SIZE;
189 }
190
191 if ((header->y_width > mjpeg->max_width) || (header->y_width == 0)) {
192 return HI_UNSUPPORT_PIC_SIZE;
193 }
194 if ((header->nf != 3) && (header->nf != 1)) { /* number of components in frame can be 1 or 3 */
195 return HI_UNSUPPORT_PIC_STRUCT;
196 }
197 return HI_SUCCESS;
198 }
199
check_sof_component_bit(frame_header * header,motion_jpeg_obj * mjpeg)200 unsigned int check_sof_component_bit(frame_header *header, motion_jpeg_obj *mjpeg)
201 {
202 if (header->nf == 1) {
203 if (header->h[COM0] != 0x11) { /* 0x11: only support H = V = 1 */
204 return HI_UNSUPPORT_PIC_STRUCT;
205 }
206 if (header->tq[COM0] & 0xfc) { /* 0xfc: tq range[0, 3] */
207 return HI_UNSUPPORT_PIC_STRUCT;
208 }
209 } else {
210 /* 0xfc: tq range[0, 3] */
211 if ((header->tq[COM0] & 0xfc) || (header->tq[COM1] & 0xfc) || (header->tq[COM2] & 0xfc)) {
212 return HI_ERR_NOT_BASELINE;
213 }
214 /* 0x41:not support H=4,V=1, 0x14:not support H=1, V=4 */
215 if ((header->h[COM0] == 0x41) || (header->h[COM0] == 0x14)) {
216 return HI_UNSUPPORT_YUV411;
217 }
218
219 if ((header->h[COM1] != 0x11) || (header->h[COM2] != 0x11)) { /* 0x11: chroma component H = V = 1 */
220 return HI_UNSUPPORT_PIC_STRUCT;
221 }
222 }
223 return HI_SUCCESS;
224 }
225
get_sof_pic_format(frame_header * header,motion_jpeg_obj * mjpeg)226 unsigned int get_sof_pic_format(frame_header *header, motion_jpeg_obj *mjpeg)
227 {
228 if (header->nf == 1) {
229 mjpeg->width_in_mcu = (header->y_width + 7) >> 3; /* 7 3 each mcu is a DU */
230 mjpeg->height_in_mcu = (header->y_height + 7) >> 3; /* 7 3 each mcu is a DU */
231 mjpeg->y_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
232 mjpeg->pic_format = PICTURE_FORMAT_YUV400; /* YUV100 */
233 } else {
234 if (header->h[0] == 0x11) { /* 0x11: only support H = V = 1 */
235 mjpeg->width_in_mcu = (header->y_width + 7) >> 3; /* 7 3 each mcu is a DU */
236 mjpeg->height_in_mcu = (header->y_height + 7) >> 3; /* 7 3 each mcu is a DU */
237 mjpeg->y_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
238 mjpeg->c_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
239 mjpeg->pic_format = PICTURE_FORMAT_YUV444; /* YUV111 */
240 } else if (header->h[0] == 0x22) { /* 0x22: only support H = V = 2 */
241 mjpeg->width_in_mcu = (header->y_width + 15) >> 4; /* 15 4 2 DU wide for each mcu */
242 mjpeg->height_in_mcu = (header->y_height + 15) >> 4; /* 15 4 2 DU height for each mcu */
243 mjpeg->y_stride = mjpeg->width_in_mcu << 4; /* 4: 2 DU wide for each mcu */
244 mjpeg->c_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
245 mjpeg->pic_format = PICTURE_FORMAT_YUV420; /* YUV420 */
246 } else if (header->h[0] == 0x21) { /* 0x21:support H=2,V=1 */
247 mjpeg->width_in_mcu = (header->y_width + 15) >> 4; /* 15 4 2 DU wide for each mcu */
248 mjpeg->height_in_mcu = (header->y_height + 7) >> 3; /* 7 3 each mcu is a DU */
249 mjpeg->y_stride = mjpeg->width_in_mcu << 4; /* 4: 2 DU wide for each mcu */
250 mjpeg->c_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
251 mjpeg->pic_format = PICTURE_FORMAT_YUV422; /* YUV422 */
252 } else if (header->h[0] == 0x12) { /* 0x12:support H=2,V=1 */
253 mjpeg->width_in_mcu = (header->y_width + 7) >> 3; /* 7 3 each mcu is a DU */
254 mjpeg->height_in_mcu = (header->y_height + 15) >> 4; /* 15 4 2 DU height for each mcu */
255 mjpeg->y_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
256 mjpeg->c_stride = mjpeg->width_in_mcu << 3; /* 3: each mcu is a DU */
257 mjpeg->pic_format = PICTURE_FORMAT_YUV422V;
258 } else {
259 mjpeg->pic_format = PICTURE_FORMAT_BUTT; /* do not support */
260 return HI_UNSUPPORT_PIC_STRUCT;
261 }
262 }
263 return HI_SUCCESS;
264 }
265
decode_sof0(motion_jpeg_obj * mjpeg,const unsigned char * stream)266 int decode_sof0(motion_jpeg_obj *mjpeg, const unsigned char *stream) /* 0xFF C0 */
267 {
268 unsigned int error_flag;
269 frame_header header = {0};
270 int ls;
271 int i = 0;
272
273 ls = (stream[0] << 8) + stream[1]; /* 0,1:The first two bytes are SOF length, shift left 8 bits */
274 header.color_depth = stream[2]; /* 2: Sample precision */
275 header.y_height = (stream[3] << 8) + stream[4]; /* 3,4,8:get height */
276 header.y_width = (stream[5] << 8) + stream[6]; /* 5,6,8:get width */
277 header.nf = stream[7]; /* 7:number of components in frame */
278 stream += 8; /* 8:bit number */
279 error_flag = check_sof_bit(&header, mjpeg, ls);
280 if (error_flag != HI_SUCCESS) {
281 hi_trace("CheckSOFBit error:0x%x!\n", error_flag);
282 return error_flag;
283 }
284
285 header.ci[0] = stream[i++];
286 header.h[0] = stream[i++];
287 header.tq[0] = stream[i++]; /* 2 array index */
288
289 if (header.nf == 3) { /* 3 number of components in frame */
290 header.ci[1] = stream[i++]; /* 3 array index */
291 header.h[1] = stream[i++]; /* 4 array index */
292 header.tq[1] = stream[i++]; /* 5 array index */
293 header.ci[2] = stream[i++]; /* 2 6 array index */
294 header.h[2] = stream[i++]; /* 2 7 array index */
295 header.tq[2] = stream[i++]; /* 2 8 array index */
296 }
297 error_flag = check_sof_component_bit(&header, mjpeg);
298 if (error_flag != HI_SUCCESS) {
299 hi_trace("check_sof_component_bit error:0x%x!\n", error_flag);
300 return error_flag;
301 }
302 error_flag = get_sof_pic_format(&header, mjpeg);
303 if (error_flag != HI_SUCCESS) {
304 hi_trace("decode_sof_pic_format error:0x%x!\n", error_flag);
305 return error_flag;
306 }
307 header.max_mcu_number = mjpeg->width_in_mcu * mjpeg->height_in_mcu;
308 header.restart_interval_logic = mjpeg->frame.restart_interval;
309 header.restart_interval = (mjpeg->frame.restart_interval != 0) ?
310 mjpeg->frame.restart_interval : header.max_mcu_number;
311 mjpeg->frame = header;
312 return HI_JPEG_DEC_OK;
313 }
314
check_sos_bit(motion_jpeg_obj * mjpeg,scan_header * scan,unsigned int index)315 int check_sos_bit(motion_jpeg_obj *mjpeg, scan_header *scan, unsigned int index)
316 {
317 frame_header *fh = &mjpeg->frame;
318 if (scan->cs[index] != fh->ci[index]) {
319 return HI_ERR_SCAN_HEADER;
320 }
321 if (scan->td[index] > 1 || scan->ta[index] > 1) {
322 return HI_ERR_NOT_BASELINE;
323 }
324 return HI_SUCCESS;
325 }
326
decode_sos(motion_jpeg_obj * mjpeg,unsigned char * stream)327 int decode_sos(motion_jpeg_obj *mjpeg, unsigned char *stream) /* 0xFF DA */
328 {
329 unsigned int error_flag = 0;
330 scan_header scan;
331 unsigned char code;
332 unsigned int i;
333 frame_header *fh = &mjpeg->frame;
334 unsigned int ls = (stream[0] << 8) + stream[1]; /* 0,1:The first two bytes are SOF length, shift left 8 bits */
335 unsigned int ns = stream[2]; /* 2:number of components in frame */
336 stream += 3; /* 3 ls and ns 's number of bytes */
337
338 if (ns != fh->nf || ls != ns * 2 + 6 || ns > 4) { /* 2:cs takes 1 byte, td and ta each take 4 bits, else take 6 */
339 return HI_ERR_SCAN_HEADER;
340 }
341
342 for (i = 0; (i < ns) && (error_flag == 0); i++) {
343 scan.cs[i] = *stream++;
344 code = *stream++;
345 scan.td[i] = code >> 4; /* the top 4 bits are ah */
346 scan.ta[i] = code & 0xf; /* 0xf:get low 4 bits */
347 if (i < 3) { /* 3 number of components in frame */
348 mjpeg->q_tab_components[i] = &mjpeg->q_tab[(3 & fh->tq[i])]; /* 3 get low 2 bits */
349 mjpeg->h_tab_components_dc[i] = &mjpeg->h_tab[(1 & scan.td[i])];
350 mjpeg->h_tab_components_ac[i] =
351 &mjpeg->h_tab[2 + (1 & scan.ta[i])]; /* 2 each dc and ac take half of huffman table */
352 }
353
354 error_flag = check_sos_bit(mjpeg, &scan, i);
355 if (error_flag != HI_SUCCESS) {
356 hi_trace("check_sos_bit0 err:0x%x\n", error_flag);
357 return error_flag;
358 }
359 }
360 scan.ns = ns;
361 scan.ss = *stream++;
362 scan.se = *stream++;
363 code = *stream++;
364 scan.ah = code >> 4; /* the top 4 bits are ah */
365 scan.al = code & 0xf; /* 0xf:get low 4 bits */
366
367 if ((scan.ss != 0) || (scan.se != 63) || (scan.ah != 0) || (scan.al != 0)) { /* baseline se must be 63 */
368 return HI_ERR_NOT_BASELINE;
369 }
370
371 if (error_flag == 0) {
372 mjpeg->scan = scan;
373 return HI_JPEG_DEC_OK;
374 } else {
375 return HI_JPEG_DEC_SCAN_ERR;
376 }
377 }
378
decode_dri(motion_jpeg_obj * mjpeg,const unsigned char * stream)379 int decode_dri(motion_jpeg_obj *mjpeg, const unsigned char *stream)
380 {
381 int lr = (stream[0] << 8) + stream[1]; /* 8:shift left */
382 int ri = (stream[2] << 8) + stream[3]; /* 2,3:array index 8:shift left */
383
384 if (lr != 4) { /* DRI data length is 4 */
385 return 1;
386 }
387
388 mjpeg->frame.restart_interval = ri;
389 mjpeg->frame.restart_interval_logic = ri;
390
391 if (mjpeg->frame.max_mcu_number == 0) {
392 mjpeg->frame.restart_interval = ri;
393 } else {
394 mjpeg->frame.restart_interval = (ri == 0) ? mjpeg->frame.max_mcu_number : ri;
395 }
396
397 return 0;
398 }
399
decode_dqt(motion_jpeg_obj * mjpeg,const unsigned char * stream)400 int decode_dqt(motion_jpeg_obj *mjpeg, const unsigned char *stream)
401 {
402 unsigned int error_flag = 0;
403 unsigned char *quant = NULL;
404 unsigned int i, code, pq, tq;
405 int k = 2;
406 int lq = (stream[0] << 8) + stream[1]; /* 0,1:The first two bytes are DQT length, shift left 8 bits */
407 stream += 2; /* 2: data length */
408
409 while ((k < lq) && (error_flag == 0)) {
410 k += 65; /* 65 jpegd quant length(64) + 1 */
411 code = *stream++;
412 pq = code >> 4; /* pq:top 4 bits */
413 tq = code & 0xf; /* 0xf:get low 4 bits */
414 error_flag |= (unsigned int)(pq != 0); /* pq must be 0 in baseline */
415 error_flag |= (unsigned int)(tq > 3); /* tq should be (0 ~ 3), inclusive */
416 error_flag |= (unsigned int)(k > lq);
417 if (error_flag) {
418 return HI_JPEG_DEC_FRAME_ERR;
419 }
420
421 quant = mjpeg->q_tab[tq & 3].quant; /* 3:Prevent decoding errors and array overflow */
422 for (i = 0; i < 64; i++) { /* 64 jpegd quant length */
423 quant[i] = *stream++;
424 }
425 }
426 return 0;
427 }
428
init_default_huffman_table(motion_jpeg_obj * mjpeg)429 void init_default_huffman_table(motion_jpeg_obj *mjpeg)
430 {
431 int ret;
432 ret = decode_dht(mjpeg, g_default_huffman_table);
433 if (ret != HI_JPEG_DEC_OK) {
434 hi_trace("jpeg decode DHT error!\n");
435 }
436 }
437
init_default_quant_table(motion_jpeg_obj * mjpeg)438 void init_default_quant_table(motion_jpeg_obj *mjpeg)
439 {
440 int ret;
441 ret = decode_dqt(mjpeg, g_default_quant_table);
442 if (ret != 0) {
443 hi_trace("jpeg decode DQT error!\n");
444 }
445 }
446
447