1 /*
2  * Small jpeg decoder library
3  *
4  * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
5  * All rights reserved.
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  *  this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  *  this list of conditions and the following disclaimer in the documentation
14  *  and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the author nor the names of its contributors may be
17  *  used to endorse or promote products derived from this software without
18  *  specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <errno.h>
39 
40 #include "tinyjpeg.h"
41 #include "tinyjpeg-internal.h"
42 #include "libv4lconvert-priv.h"
43 
44 enum std_markers {
45 	DQT  = 0xDB, /* Define Quantization Table */
46 	SOF  = 0xC0, /* Start of Frame (size information) */
47 	DHT  = 0xC4, /* Huffman Table */
48 	SOI  = 0xD8, /* Start of Image */
49 	SOS  = 0xDA, /* Start of Scan */
50 	RST  = 0xD0, /* Reset Marker d0 -> .. */
51 	RST7 = 0xD7, /* Reset Marker .. -> d7 */
52 	EOI  = 0xD9, /* End of Image */
53 	DRI  = 0xDD, /* Define Restart Interval */
54 	APP0 = 0xE0,
55 };
56 
57 #define cY	0
58 #define cCb	1
59 #define cCr	2
60 
61 #define BLACK_Y 0
62 #define BLACK_U 127
63 #define BLACK_V 127
64 
65 #if DEBUG
66 #if LOG2FILE
67 
68 #define trace(fmt, args...) do { \
69 	FILE *f = fopen("/tmp/jpeg.log", "a"); \
70 	fprintf(f, fmt, ## args); \
71 	fflush(f); \
72 	fclose(f); \
73 } while (0)
74 
75 #else
76 
77 #define trace(fmt, args...) do { \
78 	fprintf(stderr, fmt, ## args); \
79 	fflush(stderr); \
80 } while (0)
81 #endif
82 
83 #else
84 #define trace(fmt, args...) do { } while (0)
85 #endif
86 
87 #define error(fmt, args...) do { \
88 	snprintf(priv->error_string, sizeof(priv->error_string), fmt, ## args); \
89 	return -1; \
90 } while (0)
91 
92 
93 #if 0
94 static char *print_bits(unsigned int value, char *bitstr)
95 {
96 	int i, j;
97 
98 	i = 31;
99 	while (i > 0) {
100 		if (value & (1UL << i))
101 			break;
102 		i--;
103 	}
104 	j = 0;
105 	while (i >= 0) {
106 		bitstr[j++] = (value & (1UL << i)) ? '1' : '0';
107 		i--;
108 	}
109 	bitstr[j] = 0;
110 	return bitstr;
111 }
112 
113 static void print_next_16bytes(int offset, const unsigned char *stream)
114 {
115 	trace("%4.4x: %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
116 			offset,
117 			stream[0], stream[1], stream[2], stream[3],
118 			stream[4], stream[5], stream[6], stream[7],
119 			stream[8], stream[9], stream[10], stream[11],
120 			stream[12], stream[13], stream[14], stream[15]);
121 }
122 
123 #endif
124 
125 
126 static const unsigned char zigzag[64] = {
127 	0,  1,  5,  6, 14, 15, 27, 28,
128 	2,  4,  7, 13, 16, 26, 29, 42,
129 	3,  8, 12, 17, 25, 30, 41, 43,
130 	9, 11, 18, 24, 31, 40, 44, 53,
131 	10, 19, 23, 32, 39, 45, 52, 54,
132 	20, 22, 33, 38, 46, 51, 55, 60,
133 	21, 34, 37, 47, 50, 56, 59, 61,
134 	35, 36, 48, 49, 57, 58, 62, 63
135 };
136 
137 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
138 /* IMPORTANT: these are only valid for 8-bit data precision! */
139 static const unsigned char bits_dc_luminance[17] = {
140 	0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
141 };
142 static const unsigned char val_dc_luminance[] = {
143 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
144 };
145 
146 static const unsigned char bits_dc_chrominance[17] = {
147 	0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
148 };
149 static const unsigned char val_dc_chrominance[] = {
150 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
151 };
152 
153 static const unsigned char bits_ac_luminance[17] = {
154 	0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
155 };
156 static const unsigned char val_ac_luminance[] = {
157 	0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
158 	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
159 	0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
160 	0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
161 	0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
162 	0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
163 	0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
164 	0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
165 	0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
166 	0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
167 	0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
168 	0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
169 	0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
170 	0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
171 	0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
172 	0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
173 	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
174 	0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
175 	0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
176 	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
177 	0xf9, 0xfa
178 };
179 
180 static const unsigned char bits_ac_chrominance[17] = {
181 	0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
182 };
183 
184 static const unsigned char val_ac_chrominance[] = {
185 	0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
186 	0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
187 	0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
188 	0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
189 	0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
190 	0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
191 	0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
192 	0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
193 	0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
194 	0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
195 	0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
196 	0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
197 	0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
198 	0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
199 	0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
200 	0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
201 	0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
202 	0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
203 	0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
204 	0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
205 	0xf9, 0xfa
206 };
207 
208 #if 0 /* unused */
209 /* Standard JPEG quantization tables from Annex K of the JPEG standard.
210    Note unlike in Annex K the entries here are in zigzag order! */
211 const unsigned char standard_quantization[][64] = { {
212 		0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
213 		0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
214 		0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
215 		0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
216 		0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
217 		0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
218 		0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
219 		0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
220 	},
221 	{
222 		0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
223 		0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
224 		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
225 		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
226 		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
227 		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
228 		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
229 		0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
230 	},
231 };
232 #endif
233 
234 /*
235  * 4 functions to manage the stream
236  *
237  *  fill_nbits: put at least nbits in the reservoir of bits.
238  *              But convert any 0xff,0x00 into 0xff
239  *  get_nbits: read nbits from the stream, and put it in result,
240  *             bits is removed from the stream and the reservoir is filled
241  *             automaticaly. The result is signed according to the number of
242  *             bits.
243  *  look_nbits: read nbits from the stream without marking as read.
244  *  skip_nbits: read nbits from the stream but do not return the result.
245  *
246  * stream: current pointer in the jpeg data (read bytes per bytes)
247  * nbits_in_reservoir: number of bits filled into the reservoir
248  * reservoir: register that contains bits information. Only nbits_in_reservoir
249  *            is valid.
250  *                          nbits_in_reservoir
251  *                        <--    17 bits    -->
252  *            Ex: 0000 0000 1010 0000 1111 0000   <== reservoir
253  *                        ^
254  *                        bit 1
255  *            To get two bits from this example
256  *                 result = (reservoir >> 15) & 3
257  *
258  */
259 #define fill_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted) do { \
260 	while (nbits_in_reservoir < nbits_wanted) { \
261 		unsigned char c; \
262 		if (stream >= priv->stream_end) { \
263 			snprintf(priv->error_string, sizeof(priv->error_string), \
264 					"fill_nbits error: need %u more bits\n", \
265 					nbits_wanted - nbits_in_reservoir); \
266 			longjmp(priv->jump_state, -EIO); \
267 		} \
268 		c = *stream++; \
269 		reservoir <<= 8; \
270 		if (c == 0xff && *stream == 0x00) \
271 			stream++; \
272 		reservoir |= c; \
273 		nbits_in_reservoir += 8; \
274 	} \
275 }  while (0);
276 
277 /* Signed version !!!! */
278 #define get_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted, result) do { \
279 	fill_nbits(reservoir, nbits_in_reservoir, stream, (nbits_wanted)); \
280 	result = ((reservoir) >> (nbits_in_reservoir - (nbits_wanted))); \
281 	nbits_in_reservoir -= (nbits_wanted);  \
282 	reservoir &= ((1U << nbits_in_reservoir) - 1); \
283 	if ((unsigned int)result < (1UL << ((nbits_wanted) - 1))) \
284 		result += (0xFFFFFFFFUL << (nbits_wanted)) + 1; \
285 }  while (0);
286 
287 #define look_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted, result) do { \
288 	fill_nbits(reservoir, nbits_in_reservoir, stream, (nbits_wanted)); \
289 	result = ((reservoir) >> (nbits_in_reservoir - (nbits_wanted))); \
290 }  while (0);
291 
292 /* To speed up the decoding, we assume that the reservoir have enough bit
293  * slow version:
294  * #define skip_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted) do { \
295  *   fill_nbits(reservoir, nbits_in_reservoir, stream, (nbits_wanted)); \
296  *   nbits_in_reservoir -= (nbits_wanted); \
297  *   reservoir &= ((1U << nbits_in_reservoir) - 1); \
298  * }  while(0);
299  */
300 #define skip_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted) do { \
301 	nbits_in_reservoir -= (nbits_wanted); \
302 	reservoir &= ((1U << nbits_in_reservoir) - 1); \
303 }  while (0);
304 
305 #define be16_to_cpu(x) (((x)[0] << 8) | (x)[1])
306 
307 static void resync(struct jdec_private *priv);
308 
309 /**
310  * Get the next (valid) huffman code in the stream.
311  *
312  * To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is
313  * lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code
314  * and the value by using two lookup table.
315  * Else if the value is not found, just search (linear) into an array for each
316  * bits is the code is present.
317  *
318  * If the code is not present for any reason, -1 is return.
319  */
get_next_huffman_code(struct jdec_private * priv,struct huffman_table * huffman_table)320 static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table)
321 {
322 	int value, hcode;
323 	unsigned int extra_nbits, nbits;
324 	uint16_t *slowtable;
325 
326 	look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
327 	value = huffman_table->lookup[hcode];
328 	if (value >= 0) {
329 		unsigned int code_size = huffman_table->code_size[value];
330 
331 		skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
332 		return value;
333 	}
334 
335 	/* Decode more bits each time ... */
336 	for (extra_nbits = 0; extra_nbits < 16 - HUFFMAN_HASH_NBITS; extra_nbits++) {
337 		nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
338 
339 		look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode);
340 		slowtable = huffman_table->slowtable[extra_nbits];
341 		/* Search if the code is in this array */
342 		while (slowtable[0]) {
343 			if (slowtable[0] == hcode) {
344 				skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits);
345 				return slowtable[1];
346 			}
347 			slowtable += 2;
348 		}
349 	}
350 	snprintf(priv->error_string, sizeof(priv->error_string),
351 			"unknown huffman code: %08x\n", (unsigned int)hcode);
352 	longjmp(priv->jump_state, -EIO);
353 	return 0;
354 }
355 
356 /**
357  *
358  * Decode a single block that contains the DCT coefficients.
359  * The table coefficients is already dezigzaged at the end of the operation.
360  *
361  */
process_Huffman_data_unit(struct jdec_private * priv,int component)362 static void process_Huffman_data_unit(struct jdec_private *priv, int component)
363 {
364 	unsigned char j;
365 	unsigned int huff_code;
366 	unsigned char size_val, count_0;
367 
368 	struct component *c = &priv->component_infos[component];
369 	short int DCT[64];
370 
371 	/* Initialize the DCT coef table */
372 	memset(DCT, 0, sizeof(DCT));
373 
374 	/* DC coefficient decoding */
375 	huff_code = get_next_huffman_code(priv, c->DC_table);
376 	if (huff_code) {
377 		get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
378 		DCT[0] += c->previous_DC;
379 		c->previous_DC = DCT[0];
380 	} else {
381 		DCT[0] = c->previous_DC;
382 	}
383 
384 
385 	/* AC coefficient decoding */
386 	j = 1;
387 	while (j < 64) {
388 		huff_code = get_next_huffman_code(priv, c->AC_table);
389 
390 		size_val = huff_code & 0xF;
391 		count_0 = huff_code >> 4;
392 
393 		if (size_val == 0) { /* RLE */
394 			if (count_0 == 0)
395 				break;	/* EOB found, go out */
396 			if (count_0 == 0xF)
397 				j += 16;	/* skip 16 zeros */
398 		} else {
399 			j += count_0;	/* skip count_0 zeroes */
400 			if (j < 64) {
401 				get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
402 				j++;
403 			}
404 		}
405 	}
406 
407 	if (j > 64) {
408 		snprintf(priv->error_string, sizeof(priv->error_string),
409 				"error: more than 63 AC components (%d) in huffman unit\n", (int)j);
410 		longjmp(priv->jump_state, -EIO);
411 	}
412 
413 	for (j = 0; j < 64; j++)
414 		c->DCT[j] = DCT[zigzag[j]];
415 }
416 
417 /*
418  * Takes two array of bits, and build the huffman table for size, and code
419  *
420  * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
421  * code_size will be used to known how many bits this symbol is encoded.
422  * slowtable will be used when the first lookup didn't give the result.
423  */
build_huffman_table(struct jdec_private * priv,const unsigned char * bits,const unsigned char * vals,struct huffman_table * table)424 static int build_huffman_table(struct jdec_private *priv, const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
425 {
426 	unsigned int i, j, code, code_size, val, nbits;
427 	unsigned char huffsize[257], *hz;
428 	unsigned int huffcode[257], *hc;
429 	int slowtable_used[16 - HUFFMAN_HASH_NBITS];
430 
431 	/*
432 	 * Build a temp array
433 	 *   huffsize[X] => numbers of bits to write vals[X]
434 	 */
435 	hz = huffsize;
436 	for (i = 1; i <= 16; i++) {
437 		for (j = 1; j <= bits[i]; j++)
438 			*hz++ = i;
439 	}
440 	*hz = 0;
441 
442 	memset(table->lookup, 0xff, sizeof(table->lookup));
443 	for (i = 0; i < (16 - HUFFMAN_HASH_NBITS); i++)
444 		slowtable_used[i] = 0;
445 
446 	/* Build a temp array
447 	 *   huffcode[X] => code used to write vals[X]
448 	 */
449 	code = 0;
450 	hc = huffcode;
451 	hz = huffsize;
452 	nbits = *hz;
453 	while (*hz) {
454 		while (*hz == nbits) {
455 			*hc++ = code++;
456 			hz++;
457 		}
458 		code <<= 1;
459 		nbits++;
460 	}
461 
462 	/*
463 	 * Build the lookup table, and the slowtable if needed.
464 	 */
465 	for (i = 0; huffsize[i]; i++) {
466 		val = vals[i];
467 		code = huffcode[i];
468 		code_size = huffsize[i];
469 
470 		trace("val=%2.2x code=%8.8x codesize=%2.2d\n", i, code, code_size);
471 
472 		table->code_size[val] = code_size;
473 		if (code_size <= HUFFMAN_HASH_NBITS) {
474 			/*
475 			 * Good: val can be put in the lookup table, so fill all value of this
476 			 * column with value val
477 			 */
478 			int repeat = 1UL << (HUFFMAN_HASH_NBITS - code_size);
479 
480 			code <<= HUFFMAN_HASH_NBITS - code_size;
481 			while (repeat--)
482 				table->lookup[code++] = val;
483 
484 		} else {
485 			/* Perhaps sorting the array will be an optimization */
486 			int slowtable_index = code_size - HUFFMAN_HASH_NBITS - 1;
487 
488 			if (slowtable_used[slowtable_index] == 254)
489 				error("slow Huffman table overflow\n");
490 
491 			table->slowtable[slowtable_index][slowtable_used[slowtable_index]]
492 				= code;
493 			table->slowtable[slowtable_index][slowtable_used[slowtable_index] + 1]
494 				= val;
495 			slowtable_used[slowtable_index] += 2;
496 		}
497 	}
498 
499 	for (i = 0; i < (16 - HUFFMAN_HASH_NBITS); i++)
500 		table->slowtable[i][slowtable_used[i]] = 0;
501 
502 	return 0;
503 }
504 
build_default_huffman_tables(struct jdec_private * priv)505 static int build_default_huffman_tables(struct jdec_private *priv)
506 {
507 	if ((priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
508 			&& priv->default_huffman_table_initialized)
509 		return 0;
510 
511 	if (build_huffman_table(priv, bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]))
512 		return -1;
513 	if (build_huffman_table(priv, bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]))
514 		return -1;
515 
516 	if (build_huffman_table(priv, bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]))
517 		return -1;
518 	if (build_huffman_table(priv, bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]))
519 		return -1;
520 
521 	priv->default_huffman_table_initialized = 1;
522 	return 0;
523 }
524 
525 
526 
527 /*******************************************************************************
528  *
529  * Colorspace conversion routine
530  *
531  *
532  * Note:
533  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
534  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
535  * The conversion equations to be implemented are therefore
536  *      R = Y                + 1.40200 * Cr
537  *      G = Y - 0.34414 * Cb - 0.71414 * Cr
538  *      B = Y + 1.77200 * Cb
539  *
540  ******************************************************************************/
541 
clamp(int i)542 static unsigned char clamp(int i)
543 {
544 	if (i < 0)
545 		return 0;
546 	if (i > 255)
547 		return 255;
548 	return i;
549 }
550 
551 
552 /**
553  *  YCrCb -> YUV420P (1x1)
554  *  .---.
555  *  | 1 |
556  *  `---'
557  */
YCrCB_to_YUV420P_1x1(struct jdec_private * priv)558 static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
559 {
560 	const unsigned char *s, *y;
561 	unsigned char *p;
562 	int i, j;
563 
564 	p = priv->plane[0];
565 	y = priv->Y;
566 	for (i = 0; i < 8; i++) {
567 		memcpy(p, y, 8);
568 		p += priv->width;
569 		y += 8;
570 	}
571 
572 	p = priv->plane[1];
573 	s = priv->Cb;
574 	for (i = 0; i < 8; i += 2) {
575 		for (j = 0; j < 8; j += 2, s += 2)
576 			*p++ = *s;
577 		s += 8; /* Skip one line */
578 		p += priv->width / 2 - 4;
579 	}
580 
581 	p = priv->plane[2];
582 	s = priv->Cr;
583 	for (i = 0; i < 8; i += 2) {
584 		for (j = 0; j < 8; j += 2, s += 2)
585 			*p++ = *s;
586 		s += 8; /* Skip one line */
587 		p += priv->width / 2 - 4;
588 	}
589 }
590 
591 /**
592  *  YCrCb -> YUV420P (2x1)
593  *  .-------.
594  *  | 1 | 2 |
595  *  `-------'
596  */
YCrCB_to_YUV420P_2x1(struct jdec_private * priv)597 static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
598 {
599 	unsigned char *p;
600 	const unsigned char *s, *y1;
601 	unsigned int i;
602 
603 	p = priv->plane[0];
604 	y1 = priv->Y;
605 	for (i = 0; i < 8; i++) {
606 		memcpy(p, y1, 16);
607 		p += priv->width;
608 		y1 += 16;
609 	}
610 
611 	p = priv->plane[1];
612 	s = priv->Cb;
613 	for (i = 0; i < 8; i += 2) {
614 		memcpy(p, s, 8);
615 		s += 16; /* Skip one line */
616 		p += priv->width / 2;
617 	}
618 
619 	p = priv->plane[2];
620 	s = priv->Cr;
621 	for (i = 0; i < 8; i += 2) {
622 		memcpy(p, s, 8);
623 		s += 16; /* Skip one line */
624 		p += priv->width/2;
625 	}
626 }
627 
628 
629 /**
630  *  YCrCb -> YUV420P (1x2)
631  *  .---.
632  *  | 1 |
633  *  |---|
634  *  | 2 |
635  *  `---'
636  */
YCrCB_to_YUV420P_1x2(struct jdec_private * priv)637 static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
638 {
639 	const unsigned char *s, *y;
640 	unsigned char *p;
641 	int i, j;
642 
643 	p = priv->plane[0];
644 	y = priv->Y;
645 	for (i = 0; i < 16; i++) {
646 		memcpy(p, y, 8);
647 		p += priv->width;
648 		y += 8;
649 	}
650 
651 	p = priv->plane[1];
652 	s = priv->Cb;
653 	for (i = 0; i < 8; i++) {
654 		for (j = 0; j < 8; j += 2, s += 2)
655 			*p++ = *s;
656 		p += priv->width / 2 - 4;
657 	}
658 
659 	p = priv->plane[2];
660 	s = priv->Cr;
661 	for (i = 0; i < 8; i++) {
662 		for (j = 0; j < 8; j += 2, s += 2)
663 			*p++ = *s;
664 		p += priv->width / 2 - 4;
665 	}
666 }
667 
668 /**
669  *  YCrCb -> YUV420P (2x2)
670  *  .-------.
671  *  | 1 | 2 |
672  *  |---+---|
673  *  | 3 | 4 |
674  *  `-------'
675  */
YCrCB_to_YUV420P_2x2(struct jdec_private * priv)676 static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
677 {
678 	unsigned char *p;
679 	const unsigned char *s, *y1;
680 	unsigned int i;
681 
682 	p = priv->plane[0];
683 	y1 = priv->Y;
684 	for (i = 0; i < 16; i++) {
685 		memcpy(p, y1, 16);
686 		p += priv->width;
687 		y1 += 16;
688 	}
689 
690 	p = priv->plane[1];
691 	s = priv->Cb;
692 	for (i = 0; i < 8; i++) {
693 		memcpy(p, s, 8);
694 		s += 8;
695 		p += priv->width / 2;
696 	}
697 
698 	p = priv->plane[2];
699 	s = priv->Cr;
700 	for (i = 0; i < 8; i++) {
701 		memcpy(p, s, 8);
702 		s += 8;
703 		p += priv->width / 2;
704 	}
705 }
706 
707 /**
708  *  YCrCb -> RGB24 (1x1)
709  *  .---.
710  *  | 1 |
711  *  `---'
712  */
YCrCB_to_RGB24_1x1(struct jdec_private * priv)713 static void YCrCB_to_RGB24_1x1(struct jdec_private *priv)
714 {
715 	const unsigned char *Y, *Cb, *Cr;
716 	unsigned char *p;
717 	int i, j;
718 	int offset_to_next_row;
719 
720 #define SCALEBITS       10
721 #define ONE_HALF        (1UL << (SCALEBITS - 1))
722 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
723 
724 	p = priv->plane[0];
725 	Y = priv->Y;
726 	Cb = priv->Cb;
727 	Cr = priv->Cr;
728 	offset_to_next_row = priv->width * 3 - 8 * 3;
729 
730 	for (i = 0; i < 8; i++) {
731 		for (j = 0; j < 8; j++) {
732 			int y, cb, cr;
733 			int add_r, add_g, add_b;
734 			int r, g , b;
735 
736 			y  = (*Y++) << SCALEBITS;
737 			cb = *Cb++ - 128;
738 			cr = *Cr++ - 128;
739 			add_r = FIX(1.40200) * cr + ONE_HALF;
740 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
741 			add_b = FIX(1.77200) * cb + ONE_HALF;
742 
743 			r = (y + add_r) >> SCALEBITS;
744 			*p++ = clamp(r);
745 			g = (y + add_g) >> SCALEBITS;
746 			*p++ = clamp(g);
747 			b = (y + add_b) >> SCALEBITS;
748 			*p++ = clamp(b);
749 
750 		}
751 
752 		p += offset_to_next_row;
753 	}
754 
755 #undef SCALEBITS
756 #undef ONE_HALF
757 #undef FIX
758 }
759 
760 /**
761  *  YCrCb -> BGR24 (1x1)
762  *  .---.
763  *  | 1 |
764  *  `---'
765  */
YCrCB_to_BGR24_1x1(struct jdec_private * priv)766 static void YCrCB_to_BGR24_1x1(struct jdec_private *priv)
767 {
768 	const unsigned char *Y, *Cb, *Cr;
769 	unsigned char *p;
770 	int i, j;
771 	int offset_to_next_row;
772 
773 #define SCALEBITS       10
774 #define ONE_HALF        (1UL << (SCALEBITS - 1))
775 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
776 
777 	p = priv->plane[0];
778 	Y = priv->Y;
779 	Cb = priv->Cb;
780 	Cr = priv->Cr;
781 	offset_to_next_row = priv->width * 3 - 8 * 3;
782 
783 	for (i = 0; i < 8; i++) {
784 		for (j = 0; j < 8; j++) {
785 			int y, cb, cr;
786 			int add_r, add_g, add_b;
787 			int r, g , b;
788 
789 			y  = (*Y++) << SCALEBITS;
790 			cb = *Cb++ - 128;
791 			cr = *Cr++ - 128;
792 			add_r = FIX(1.40200) * cr + ONE_HALF;
793 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
794 			add_b = FIX(1.77200) * cb + ONE_HALF;
795 
796 			b = (y + add_b) >> SCALEBITS;
797 			*p++ = clamp(b);
798 			g = (y + add_g) >> SCALEBITS;
799 			*p++ = clamp(g);
800 			r = (y + add_r) >> SCALEBITS;
801 			*p++ = clamp(r);
802 
803 		}
804 		p += offset_to_next_row;
805 	}
806 
807 #undef SCALEBITS
808 #undef ONE_HALF
809 #undef FIX
810 }
811 
812 
813 /**
814  *  YCrCb -> RGB24 (2x1)
815  *  .-------.
816  *  | 1 | 2 |
817  *  `-------'
818  */
YCrCB_to_RGB24_2x1(struct jdec_private * priv)819 static void YCrCB_to_RGB24_2x1(struct jdec_private *priv)
820 {
821 	const unsigned char *Y, *Cb, *Cr;
822 	unsigned char *p;
823 	int i, j;
824 	int offset_to_next_row;
825 
826 #define SCALEBITS       10
827 #define ONE_HALF        (1UL << (SCALEBITS - 1))
828 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
829 
830 	p = priv->plane[0];
831 	Y = priv->Y;
832 	Cb = priv->Cb;
833 	Cr = priv->Cr;
834 	offset_to_next_row = priv->width * 3 - 16 * 3;
835 
836 	for (i = 0; i < 8; i++) {
837 		for (j = 0; j < 8; j++) {
838 			int y, cb, cr;
839 			int add_r, add_g, add_b;
840 			int r, g , b;
841 
842 			y  = (*Y++) << SCALEBITS;
843 			cb = *Cb++ - 128;
844 			cr = *Cr++ - 128;
845 			add_r = FIX(1.40200) * cr + ONE_HALF;
846 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
847 			add_b = FIX(1.77200) * cb + ONE_HALF;
848 
849 			r = (y + add_r) >> SCALEBITS;
850 			*p++ = clamp(r);
851 			g = (y + add_g) >> SCALEBITS;
852 			*p++ = clamp(g);
853 			b = (y + add_b) >> SCALEBITS;
854 			*p++ = clamp(b);
855 
856 			y  = (*Y++) << SCALEBITS;
857 			r = (y + add_r) >> SCALEBITS;
858 			*p++ = clamp(r);
859 			g = (y + add_g) >> SCALEBITS;
860 			*p++ = clamp(g);
861 			b = (y + add_b) >> SCALEBITS;
862 			*p++ = clamp(b);
863 		}
864 
865 		p += offset_to_next_row;
866 	}
867 
868 #undef SCALEBITS
869 #undef ONE_HALF
870 #undef FIX
871 }
872 
873 /*
874  *  YCrCb -> BGR24 (2x1)
875  *  .-------.
876  *  | 1 | 2 |
877  *  `-------'
878  */
YCrCB_to_BGR24_2x1(struct jdec_private * priv)879 static void YCrCB_to_BGR24_2x1(struct jdec_private *priv)
880 {
881 	const unsigned char *Y, *Cb, *Cr;
882 	unsigned char *p;
883 	int i, j;
884 	int offset_to_next_row;
885 
886 #define SCALEBITS       10
887 #define ONE_HALF        (1UL << (SCALEBITS - 1))
888 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
889 
890 	p = priv->plane[0];
891 	Y = priv->Y;
892 	Cb = priv->Cb;
893 	Cr = priv->Cr;
894 	offset_to_next_row = priv->width * 3 - 16 * 3;
895 
896 	for (i = 0; i < 8; i++) {
897 		for (j = 0; j < 8; j++) {
898 			int y, cb, cr;
899 			int add_r, add_g, add_b;
900 			int r, g , b;
901 
902 			cb = *Cb++ - 128;
903 			cr = *Cr++ - 128;
904 			add_r = FIX(1.40200) * cr + ONE_HALF;
905 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
906 			add_b = FIX(1.77200) * cb + ONE_HALF;
907 
908 			y  = (*Y++) << SCALEBITS;
909 			b = (y + add_b) >> SCALEBITS;
910 			*p++ = clamp(b);
911 			g = (y + add_g) >> SCALEBITS;
912 			*p++ = clamp(g);
913 			r = (y + add_r) >> SCALEBITS;
914 			*p++ = clamp(r);
915 
916 			y  = (*Y++) << SCALEBITS;
917 			b = (y + add_b) >> SCALEBITS;
918 			*p++ = clamp(b);
919 			g = (y + add_g) >> SCALEBITS;
920 			*p++ = clamp(g);
921 			r = (y + add_r) >> SCALEBITS;
922 			*p++ = clamp(r);
923 		}
924 
925 		p += offset_to_next_row;
926 	}
927 
928 #undef SCALEBITS
929 #undef ONE_HALF
930 #undef FIX
931 }
932 
933 /**
934  *  YCrCb -> RGB24 (1x2)
935  *  .---.
936  *  | 1 |
937  *  |---|
938  *  | 2 |
939  *  `---'
940  */
YCrCB_to_RGB24_1x2(struct jdec_private * priv)941 static void YCrCB_to_RGB24_1x2(struct jdec_private *priv)
942 {
943 	const unsigned char *Y, *Cb, *Cr;
944 	unsigned char *p, *p2;
945 	int i, j;
946 	int offset_to_next_row;
947 
948 #define SCALEBITS       10
949 #define ONE_HALF        (1UL << (SCALEBITS - 1))
950 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
951 
952 	p = priv->plane[0];
953 	p2 = priv->plane[0] + priv->width * 3;
954 	Y = priv->Y;
955 	Cb = priv->Cb;
956 	Cr = priv->Cr;
957 	offset_to_next_row = 2 * priv->width * 3 - 8 * 3;
958 
959 	for (i = 0; i < 8; i++) {
960 		for (j = 0; j < 8; j++) {
961 			int y, cb, cr;
962 			int add_r, add_g, add_b;
963 			int r, g , b;
964 
965 			cb = *Cb++ - 128;
966 			cr = *Cr++ - 128;
967 			add_r = FIX(1.40200) * cr + ONE_HALF;
968 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
969 			add_b = FIX(1.77200) * cb + ONE_HALF;
970 
971 			y  = (*Y++) << SCALEBITS;
972 			r = (y + add_r) >> SCALEBITS;
973 			*p++ = clamp(r);
974 			g = (y + add_g) >> SCALEBITS;
975 			*p++ = clamp(g);
976 			b = (y + add_b) >> SCALEBITS;
977 			*p++ = clamp(b);
978 
979 			y  = (Y[8-1]) << SCALEBITS;
980 			r = (y + add_r) >> SCALEBITS;
981 			*p2++ = clamp(r);
982 			g = (y + add_g) >> SCALEBITS;
983 			*p2++ = clamp(g);
984 			b = (y + add_b) >> SCALEBITS;
985 			*p2++ = clamp(b);
986 
987 		}
988 		Y += 8;
989 		p += offset_to_next_row;
990 		p2 += offset_to_next_row;
991 	}
992 
993 #undef SCALEBITS
994 #undef ONE_HALF
995 #undef FIX
996 }
997 
998 /*
999  *  YCrCb -> BGR24 (1x2)
1000  *  .---.
1001  *  | 1 |
1002  *  |---|
1003  *  | 2 |
1004  *  `---'
1005  */
YCrCB_to_BGR24_1x2(struct jdec_private * priv)1006 static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
1007 {
1008 	const unsigned char *Y, *Cb, *Cr;
1009 	unsigned char *p, *p2;
1010 	int i, j;
1011 	int offset_to_next_row;
1012 
1013 #define SCALEBITS       10
1014 #define ONE_HALF        (1UL << (SCALEBITS - 1))
1015 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
1016 
1017 	p = priv->plane[0];
1018 	p2 = priv->plane[0] + priv->width * 3;
1019 	Y = priv->Y;
1020 	Cb = priv->Cb;
1021 	Cr = priv->Cr;
1022 	offset_to_next_row = 2 * priv->width * 3 - 8 * 3;
1023 
1024 	for (i = 0; i < 8; i++) {
1025 		for (j = 0; j < 8; j++) {
1026 			int y, cb, cr;
1027 			int add_r, add_g, add_b;
1028 			int r, g , b;
1029 
1030 			cb = *Cb++ - 128;
1031 			cr = *Cr++ - 128;
1032 			add_r = FIX(1.40200) * cr + ONE_HALF;
1033 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1034 			add_b = FIX(1.77200) * cb + ONE_HALF;
1035 
1036 			y  = (*Y++) << SCALEBITS;
1037 			b = (y + add_b) >> SCALEBITS;
1038 			*p++ = clamp(b);
1039 			g = (y + add_g) >> SCALEBITS;
1040 			*p++ = clamp(g);
1041 			r = (y + add_r) >> SCALEBITS;
1042 			*p++ = clamp(r);
1043 
1044 			y  = (Y[8-1]) << SCALEBITS;
1045 			b = (y + add_b) >> SCALEBITS;
1046 			*p2++ = clamp(b);
1047 			g = (y + add_g) >> SCALEBITS;
1048 			*p2++ = clamp(g);
1049 			r = (y + add_r) >> SCALEBITS;
1050 			*p2++ = clamp(r);
1051 
1052 		}
1053 		Y += 8;
1054 		p += offset_to_next_row;
1055 		p2 += offset_to_next_row;
1056 	}
1057 
1058 #undef SCALEBITS
1059 #undef ONE_HALF
1060 #undef FIX
1061 }
1062 
1063 
1064 /**
1065  *  YCrCb -> RGB24 (2x2)
1066  *  .-------.
1067  *  | 1 | 2 |
1068  *  |---+---|
1069  *  | 3 | 4 |
1070  *  `-------'
1071  */
YCrCB_to_RGB24_2x2(struct jdec_private * priv)1072 static void YCrCB_to_RGB24_2x2(struct jdec_private *priv)
1073 {
1074 	const unsigned char *Y, *Cb, *Cr;
1075 	unsigned char *p, *p2;
1076 	int i, j;
1077 	int offset_to_next_row;
1078 
1079 #define SCALEBITS       10
1080 #define ONE_HALF        (1UL << (SCALEBITS - 1))
1081 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
1082 
1083 	p = priv->plane[0];
1084 	p2 = priv->plane[0] + priv->width * 3;
1085 	Y = priv->Y;
1086 	Cb = priv->Cb;
1087 	Cr = priv->Cr;
1088 	offset_to_next_row = (priv->width * 3 * 2) - 16 * 3;
1089 
1090 	for (i = 0; i < 8; i++) {
1091 		for (j = 0; j < 8; j++) {
1092 			int y, cb, cr;
1093 			int add_r, add_g, add_b;
1094 			int r, g , b;
1095 
1096 			cb = *Cb++ - 128;
1097 			cr = *Cr++ - 128;
1098 			add_r = FIX(1.40200) * cr + ONE_HALF;
1099 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1100 			add_b = FIX(1.77200) * cb + ONE_HALF;
1101 
1102 			y  = (*Y++) << SCALEBITS;
1103 			r = (y + add_r) >> SCALEBITS;
1104 			*p++ = clamp(r);
1105 			g = (y + add_g) >> SCALEBITS;
1106 			*p++ = clamp(g);
1107 			b = (y + add_b) >> SCALEBITS;
1108 			*p++ = clamp(b);
1109 
1110 			y  = (*Y++) << SCALEBITS;
1111 			r = (y + add_r) >> SCALEBITS;
1112 			*p++ = clamp(r);
1113 			g = (y + add_g) >> SCALEBITS;
1114 			*p++ = clamp(g);
1115 			b = (y + add_b) >> SCALEBITS;
1116 			*p++ = clamp(b);
1117 
1118 			y  = (Y[16-2]) << SCALEBITS;
1119 			r = (y + add_r) >> SCALEBITS;
1120 			*p2++ = clamp(r);
1121 			g = (y + add_g) >> SCALEBITS;
1122 			*p2++ = clamp(g);
1123 			b = (y + add_b) >> SCALEBITS;
1124 			*p2++ = clamp(b);
1125 
1126 			y  = (Y[16-1]) << SCALEBITS;
1127 			r = (y + add_r) >> SCALEBITS;
1128 			*p2++ = clamp(r);
1129 			g = (y + add_g) >> SCALEBITS;
1130 			*p2++ = clamp(g);
1131 			b = (y + add_b) >> SCALEBITS;
1132 			*p2++ = clamp(b);
1133 		}
1134 		Y  += 16;
1135 		p  += offset_to_next_row;
1136 		p2 += offset_to_next_row;
1137 	}
1138 
1139 #undef SCALEBITS
1140 #undef ONE_HALF
1141 #undef FIX
1142 }
1143 
1144 
1145 /*
1146  *  YCrCb -> BGR24 (2x2)
1147  *  .-------.
1148  *  | 1 | 2 |
1149  *  |---+---|
1150  *  | 3 | 4 |
1151  *  `-------'
1152  */
YCrCB_to_BGR24_2x2(struct jdec_private * priv)1153 static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
1154 {
1155 	const unsigned char *Y, *Cb, *Cr;
1156 	unsigned char *p, *p2;
1157 	int i, j;
1158 	int offset_to_next_row;
1159 
1160 #define SCALEBITS       10
1161 #define ONE_HALF        (1UL << (SCALEBITS - 1))
1162 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
1163 
1164 	p = priv->plane[0];
1165 	p2 = priv->plane[0] + priv->width * 3;
1166 	Y = priv->Y;
1167 	Cb = priv->Cb;
1168 	Cr = priv->Cr;
1169 	offset_to_next_row = (priv->width * 3 * 2) - 16 * 3;
1170 
1171 	for (i = 0; i < 8; i++) {
1172 		for (j = 0; j < 8; j++) {
1173 			int y, cb, cr;
1174 			int add_r, add_g, add_b;
1175 			int r, g , b;
1176 
1177 			cb = *Cb++ - 128;
1178 			cr = *Cr++ - 128;
1179 			add_r = FIX(1.40200) * cr + ONE_HALF;
1180 			add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1181 			add_b = FIX(1.77200) * cb + ONE_HALF;
1182 
1183 			y  = (*Y++) << SCALEBITS;
1184 			b = (y + add_b) >> SCALEBITS;
1185 			*p++ = clamp(b);
1186 			g = (y + add_g) >> SCALEBITS;
1187 			*p++ = clamp(g);
1188 			r = (y + add_r) >> SCALEBITS;
1189 			*p++ = clamp(r);
1190 
1191 			y  = (*Y++) << SCALEBITS;
1192 			b = (y + add_b) >> SCALEBITS;
1193 			*p++ = clamp(b);
1194 			g = (y + add_g) >> SCALEBITS;
1195 			*p++ = clamp(g);
1196 			r = (y + add_r) >> SCALEBITS;
1197 			*p++ = clamp(r);
1198 
1199 			y  = (Y[16-2]) << SCALEBITS;
1200 			b = (y + add_b) >> SCALEBITS;
1201 			*p2++ = clamp(b);
1202 			g = (y + add_g) >> SCALEBITS;
1203 			*p2++ = clamp(g);
1204 			r = (y + add_r) >> SCALEBITS;
1205 			*p2++ = clamp(r);
1206 
1207 			y  = (Y[16-1]) << SCALEBITS;
1208 			b = (y + add_b) >> SCALEBITS;
1209 			*p2++ = clamp(b);
1210 			g = (y + add_g) >> SCALEBITS;
1211 			*p2++ = clamp(g);
1212 			r = (y + add_r) >> SCALEBITS;
1213 			*p2++ = clamp(r);
1214 		}
1215 		Y  += 16;
1216 		p  += offset_to_next_row;
1217 		p2 += offset_to_next_row;
1218 	}
1219 
1220 #undef SCALEBITS
1221 #undef ONE_HALF
1222 #undef FIX
1223 }
1224 
1225 
1226 
1227 /**
1228  *  YCrCb -> Grey (1x1)
1229  *  .---.
1230  *  | 1 |
1231  *  `---'
1232  */
YCrCB_to_Grey_1x1(struct jdec_private * priv)1233 static void YCrCB_to_Grey_1x1(struct jdec_private *priv)
1234 {
1235 	const unsigned char *y;
1236 	unsigned char *p;
1237 	unsigned int i;
1238 	int offset_to_next_row;
1239 
1240 	p = priv->plane[0];
1241 	y = priv->Y;
1242 	offset_to_next_row = priv->width;
1243 
1244 	for (i = 0; i < 8; i++) {
1245 		memcpy(p, y, 8);
1246 		y += 8;
1247 		p += offset_to_next_row;
1248 	}
1249 }
1250 
1251 /**
1252  *  YCrCb -> Grey (2x1)
1253  *  .-------.
1254  *  | 1 | 2 |
1255  *  `-------'
1256  */
YCrCB_to_Grey_2x1(struct jdec_private * priv)1257 static void YCrCB_to_Grey_2x1(struct jdec_private *priv)
1258 {
1259 	const unsigned char *y;
1260 	unsigned char *p;
1261 	unsigned int i;
1262 
1263 	p = priv->plane[0];
1264 	y = priv->Y;
1265 
1266 	for (i = 0; i < 8; i++) {
1267 		memcpy(p, y, 16);
1268 		y += 16;
1269 		p += priv->width;
1270 	}
1271 }
1272 
1273 
1274 /**
1275  *  YCrCb -> Grey (1x2)
1276  *  .---.
1277  *  | 1 |
1278  *  |---|
1279  *  | 2 |
1280  *  `---'
1281  */
YCrCB_to_Grey_1x2(struct jdec_private * priv)1282 static void YCrCB_to_Grey_1x2(struct jdec_private *priv)
1283 {
1284 	const unsigned char *y;
1285 	unsigned char *p;
1286 	unsigned int i;
1287 
1288 	p = priv->plane[0];
1289 	y = priv->Y;
1290 
1291 	for (i = 0; i < 16; i++) {
1292 		memcpy(p, y, 8);
1293 		y += 8;
1294 		p += priv->width;
1295 	}
1296 }
1297 
1298 /**
1299  *  YCrCb -> Grey (2x2)
1300  *  .-------.
1301  *  | 1 | 2 |
1302  *  |---+---|
1303  *  | 3 | 4 |
1304  *  `-------'
1305  */
YCrCB_to_Grey_2x2(struct jdec_private * priv)1306 static void YCrCB_to_Grey_2x2(struct jdec_private *priv)
1307 {
1308 	const unsigned char *y;
1309 	unsigned char *p;
1310 	unsigned int i;
1311 
1312 	p = priv->plane[0];
1313 	y = priv->Y;
1314 
1315 	for (i = 0; i < 16; i++) {
1316 		memcpy(p, y, 16);
1317 		y += 16;
1318 		p += priv->width;
1319 	}
1320 }
1321 
1322 
1323 /*
1324  * Decode all the 3 components for 1x1
1325  */
decode_MCU_1x1_3planes(struct jdec_private * priv)1326 static void decode_MCU_1x1_3planes(struct jdec_private *priv)
1327 {
1328 	// Y
1329 	process_Huffman_data_unit(priv, cY);
1330 	IDCT(&priv->component_infos[cY], priv->Y, 8);
1331 
1332 	// Cb
1333 	process_Huffman_data_unit(priv, cCb);
1334 	IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1335 
1336 	// Cr
1337 	process_Huffman_data_unit(priv, cCr);
1338 	IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1339 }
1340 
1341 /*
1342  * Decode a 1x1 directly in 1 color
1343  */
decode_MCU_1x1_1plane(struct jdec_private * priv)1344 static void decode_MCU_1x1_1plane(struct jdec_private *priv)
1345 {
1346 	// Y
1347 	process_Huffman_data_unit(priv, cY);
1348 	IDCT(&priv->component_infos[cY], priv->Y, 8);
1349 
1350 	// Cb
1351 	process_Huffman_data_unit(priv, cCb);
1352 	IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1353 
1354 	// Cr
1355 	process_Huffman_data_unit(priv, cCr);
1356 	IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1357 }
1358 
1359 
1360 /*
1361  * Decode a 2x1
1362  *  .-------.
1363  *  | 1 | 2 |
1364  *  `-------'
1365  */
decode_MCU_2x1_3planes(struct jdec_private * priv)1366 static void decode_MCU_2x1_3planes(struct jdec_private *priv)
1367 {
1368 	// Y
1369 	process_Huffman_data_unit(priv, cY);
1370 	IDCT(&priv->component_infos[cY], priv->Y, 16);
1371 	process_Huffman_data_unit(priv, cY);
1372 	IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
1373 
1374 	// Cb
1375 	process_Huffman_data_unit(priv, cCb);
1376 	IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1377 
1378 	// Cr
1379 	process_Huffman_data_unit(priv, cCr);
1380 	IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1381 }
1382 
1383 static void build_quantization_table(float *qtable, const unsigned char *ref_table);
1384 
pixart_decode_MCU_2x1_3planes(struct jdec_private * priv)1385 static void pixart_decode_MCU_2x1_3planes(struct jdec_private *priv)
1386 {
1387 	unsigned char marker;
1388 
1389 	look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream,
1390 		   8, marker);
1391 
1392 	/* Sometimes the pac7302 switches chrominance setting halfway though a
1393 	   frame, with a quite ugly looking result, so we drop such frames. */
1394 	if (priv->first_marker == 0)
1395 		priv->first_marker = marker;
1396 	else if ((marker & 0x80) != (priv->first_marker & 0x80)) {
1397 		snprintf(priv->error_string, sizeof(priv->error_string),
1398 			"Pixart JPEG error: chrominance changed halfway\n");
1399 		longjmp(priv->jump_state, -EIO);
1400 	}
1401 
1402 	/* Pixart JPEG MCU-s are preceded by a marker indicating the quality
1403 	   setting with which the MCU is compressed, IOW the MCU-s may have a
1404 	   different quantization table per MCU. So if the marker changes we
1405 	   need to rebuild the quantization tables. */
1406 	if (marker != priv->marker) {
1407 		int i, j, comp, lumi;
1408 		unsigned char qt[64];
1409 		/* These values have been found by trial and error and seem to
1410 		   work reasonably. Markers with index 0 - 7 are never
1411 		   generated by the hardware, so they are likely wrong. */
1412 		const int qfactor[32] = {
1413 			 25,   30,  35,  40,  45,  50,  55,  60,
1414 			 65,   70,  75,  80,  85,  90,  95, 100,
1415 			100,  100, 120, 140, 160, 180, 210, 240,
1416 			270,  300, 330, 360, 390, 420, 450, 480
1417 		};
1418 		/* These tables were found in SPC230NC.SYS */
1419 		const unsigned char pixart_q[][64] = { {
1420 			0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
1421 			0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
1422 			0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
1423 			0x08, 0x08, 0x08, 0x08, 0x10, 0x10, 0x10, 0x10,
1424 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
1425 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
1426 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x20, 0x20,
1427 			0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1428 		}, {
1429 			0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x10, 0x10,
1430 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
1431 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
1432 			0x10, 0x10, 0x10, 0x10, 0x20, 0x20, 0x20, 0x20,
1433 			0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1434 			0x20, 0x20, 0x20, 0x40, 0x40, 0x40, 0x40, 0x40,
1435 			0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
1436 			0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
1437 		}, {
1438 			0x08, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x10, 0x10,
1439 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
1440 			0x10, 0x10, 0x10, 0x10, 0x10, 0x20, 0x20, 0x20,
1441 			0x20, 0x20, 0x20, 0x20, 0x40, 0x40, 0x40, 0x40,
1442 			0x40, 0x40, 0x40, 0x40, 0x63, 0x63, 0x63, 0x63,
1443 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1444 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1445 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1446 		}, {
1447 			0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x20, 0x20,
1448 			0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x63,
1449 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1450 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1451 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1452 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1453 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1454 			0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
1455 		} };
1456 
1457 		i = (marker & 0x7c) >> 2; /* Bits 0 and 1 are always 0 */
1458 		comp = qfactor[i];
1459 		lumi = (marker & 0x40) ? 1 : 0;
1460 		/* printf("marker %02x comp %d lumi %d\n", marker, comp, lumi); */
1461 
1462 		/* Note the DC quantization factor is fixed! */
1463 		qt[0] = pixart_q[lumi][0];
1464 		for (i = 1; i < 64; i++) {
1465 			j = (pixart_q[lumi][i] * comp + 50) / 100;
1466 			qt[i] = (j < 255) ? j : 255;
1467 		}
1468 		build_quantization_table(priv->Q_tables[0], qt);
1469 
1470 		/* If bit 7 of the marker is set chrominance uses the
1471 		   luminance quantization table */
1472 		if (!(marker & 0x80)) {
1473 			qt[0] = pixart_q[3][0];
1474 			for (i = 1; i < 64; i++) {
1475 				j = (pixart_q[3][i] * comp + 50) / 100;
1476 				qt[i] = (j < 255) ? j : 255;
1477 			}
1478 		}
1479 		build_quantization_table(priv->Q_tables[1], qt);
1480 
1481 		priv->marker = marker;
1482 	}
1483 	skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, 8);
1484 
1485 	// Y
1486 	process_Huffman_data_unit(priv, cY);
1487 	IDCT(&priv->component_infos[cY], priv->Y, 16);
1488 	process_Huffman_data_unit(priv, cY);
1489 	IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
1490 
1491 	// Cb
1492 	process_Huffman_data_unit(priv, cCb);
1493 	IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1494 
1495 	// Cr
1496 	process_Huffman_data_unit(priv, cCr);
1497 	IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1498 }
1499 
1500 /*
1501  * Decode a 2x1
1502  *  .-------.
1503  *  | 1 | 2 |
1504  *  `-------'
1505  */
decode_MCU_2x1_1plane(struct jdec_private * priv)1506 static void decode_MCU_2x1_1plane(struct jdec_private *priv)
1507 {
1508 	// Y
1509 	process_Huffman_data_unit(priv, cY);
1510 	IDCT(&priv->component_infos[cY], priv->Y, 16);
1511 	process_Huffman_data_unit(priv, cY);
1512 	IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
1513 
1514 	// Cb
1515 	process_Huffman_data_unit(priv, cCb);
1516 
1517 	// Cr
1518 	process_Huffman_data_unit(priv, cCr);
1519 }
1520 
1521 
1522 /*
1523  * Decode a 2x2
1524  *  .-------.
1525  *  | 1 | 2 |
1526  *  |---+---|
1527  *  | 3 | 4 |
1528  *  `-------'
1529  */
decode_MCU_2x2_3planes(struct jdec_private * priv)1530 static void decode_MCU_2x2_3planes(struct jdec_private *priv)
1531 {
1532 	// Y
1533 	process_Huffman_data_unit(priv, cY);
1534 	IDCT(&priv->component_infos[cY], priv->Y, 16);
1535 	process_Huffman_data_unit(priv, cY);
1536 	IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
1537 	process_Huffman_data_unit(priv, cY);
1538 	IDCT(&priv->component_infos[cY], priv->Y + 64 * 2, 16);
1539 	process_Huffman_data_unit(priv, cY);
1540 	IDCT(&priv->component_infos[cY], priv->Y + 64 * 2 + 8, 16);
1541 
1542 	// Cb
1543 	process_Huffman_data_unit(priv, cCb);
1544 	IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1545 
1546 	// Cr
1547 	process_Huffman_data_unit(priv, cCr);
1548 	IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1549 }
1550 
1551 /*
1552  * Decode a 2x2 directly in GREY format (8bits)
1553  *  .-------.
1554  *  | 1 | 2 |
1555  *  |---+---|
1556  *  | 3 | 4 |
1557  *  `-------'
1558  */
decode_MCU_2x2_1plane(struct jdec_private * priv)1559 static void decode_MCU_2x2_1plane(struct jdec_private *priv)
1560 {
1561 	// Y
1562 	process_Huffman_data_unit(priv, cY);
1563 	IDCT(&priv->component_infos[cY], priv->Y, 16);
1564 	process_Huffman_data_unit(priv, cY);
1565 	IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
1566 	process_Huffman_data_unit(priv, cY);
1567 	IDCT(&priv->component_infos[cY], priv->Y + 64 * 2, 16);
1568 	process_Huffman_data_unit(priv, cY);
1569 	IDCT(&priv->component_infos[cY], priv->Y + 64 * 2 + 8, 16);
1570 
1571 	// Cb
1572 	process_Huffman_data_unit(priv, cCb);
1573 
1574 	// Cr
1575 	process_Huffman_data_unit(priv, cCr);
1576 }
1577 
1578 /*
1579  * Decode a 1x2 mcu
1580  *  .---.
1581  *  | 1 |
1582  *  |---|
1583  *  | 2 |
1584  *  `---'
1585  */
decode_MCU_1x2_3planes(struct jdec_private * priv)1586 static void decode_MCU_1x2_3planes(struct jdec_private *priv)
1587 {
1588 	// Y
1589 	process_Huffman_data_unit(priv, cY);
1590 	IDCT(&priv->component_infos[cY], priv->Y, 8);
1591 	process_Huffman_data_unit(priv, cY);
1592 	IDCT(&priv->component_infos[cY], priv->Y + 64, 8);
1593 
1594 	// Cb
1595 	process_Huffman_data_unit(priv, cCb);
1596 	IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1597 
1598 	// Cr
1599 	process_Huffman_data_unit(priv, cCr);
1600 	IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1601 }
1602 
1603 /*
1604  * Decode a 1x2 mcu
1605  *  .---.
1606  *  | 1 |
1607  *  |---|
1608  *  | 2 |
1609  *  `---'
1610  */
decode_MCU_1x2_1plane(struct jdec_private * priv)1611 static void decode_MCU_1x2_1plane(struct jdec_private *priv)
1612 {
1613 	// Y
1614 	process_Huffman_data_unit(priv, cY);
1615 	IDCT(&priv->component_infos[cY], priv->Y, 8);
1616 	process_Huffman_data_unit(priv, cY);
1617 	IDCT(&priv->component_infos[cY], priv->Y + 64, 8);
1618 
1619 	// Cb
1620 	process_Huffman_data_unit(priv, cCb);
1621 
1622 	// Cr
1623 	process_Huffman_data_unit(priv, cCr);
1624 }
1625 
print_SOF(const unsigned char * stream)1626 static void print_SOF(const unsigned char *stream)
1627 {
1628 #if DEBUG
1629 	int width, height, nr_components, precision;
1630 	const char *nr_components_to_string[] = {
1631 		"????",
1632 		"Grayscale",
1633 		"????",
1634 		"YCbCr",
1635 		"CYMK"
1636 	};
1637 
1638 	precision = stream[2];
1639 	height = be16_to_cpu(stream + 3);
1640 	width  = be16_to_cpu(stream + 5);
1641 	nr_components = stream[7];
1642 
1643 	trace("> SOF marker\n");
1644 	trace("Size:%dx%d nr_components:%d (%s)  precision:%d\n",
1645 			width, height,
1646 			nr_components, nr_components_to_string[nr_components],
1647 			precision);
1648 #endif
1649 }
1650 
1651 /*******************************************************************************
1652  *
1653  * JPEG/JFIF Parsing functions
1654  *
1655  * Note: only a small subset of the jpeg file format is supported. No markers,
1656  * nor progressive stream is supported.
1657  *
1658  ******************************************************************************/
1659 
build_quantization_table(float * qtable,const unsigned char * ref_table)1660 static void build_quantization_table(float *qtable, const unsigned char *ref_table)
1661 {
1662 	/* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
1663 	 * For float AA&N IDCT method, divisors are equal to quantization
1664 	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
1665 	 *   scalefactor[0] = 1
1666 	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
1667 	 * We apply a further scale factor of 8.
1668 	 * What's actually stored is 1/divisor so that the inner loop can
1669 	 * use a multiplication rather than a division.
1670 	 */
1671 	int i, j;
1672 	static const double aanscalefactor[8] = {
1673 		1.0, 1.387039845, 1.306562965, 1.175875602,
1674 		1.0, 0.785694958, 0.541196100, 0.275899379
1675 	};
1676 	const unsigned char *zz = zigzag;
1677 
1678 	for (i = 0; i < 8; i++)
1679 		for (j = 0; j < 8; j++)
1680 			*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
1681 
1682 }
1683 
parse_DQT(struct jdec_private * priv,const unsigned char * stream)1684 static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
1685 {
1686 	int qi;
1687 	float *table;
1688 	const unsigned char *dqt_block_end;
1689 
1690 	trace("> DQT marker\n");
1691 	dqt_block_end = stream + be16_to_cpu(stream);
1692 	stream += 2;	/* Skip length */
1693 
1694 	while (stream < dqt_block_end) {
1695 		qi = *stream++;
1696 #if SANITY_CHECK
1697 		if (qi >> 4)
1698 			error("16 bits quantization table is not supported\n");
1699 		if (qi >= COMPONENTS)
1700 			error("No more than %d quantization tables supported (got %d)\n",
1701 					COMPONENTS, qi + 1);
1702 #endif
1703 		table = priv->Q_tables[qi];
1704 		build_quantization_table(table, stream);
1705 		stream += 64;
1706 	}
1707 	trace("< DQT marker\n");
1708 	return 0;
1709 }
1710 
parse_SOF(struct jdec_private * priv,const unsigned char * stream)1711 static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
1712 {
1713 	int i, width, height, nr_components, cid, sampling_factor;
1714 	int Q_table;
1715 	struct component *c;
1716 
1717 	trace("> SOF marker\n");
1718 	print_SOF(stream);
1719 
1720 	height = be16_to_cpu(stream+3);
1721 	width  = be16_to_cpu(stream+5);
1722 	nr_components = stream[7];
1723 #if SANITY_CHECK
1724 	if (stream[2] != 8)
1725 		error("Precision other than 8 is not supported\n");
1726 	if (width > JPEG_MAX_WIDTH || height > JPEG_MAX_HEIGHT)
1727 		error("Width and Height (%dx%d) seems suspicious\n", width, height);
1728 	if (nr_components != 3)
1729 		error("We only support YUV images\n");
1730 	if (height % 8)
1731 		error("Height need to be a multiple of 8 (current height is %d)\n", height);
1732 	if (width % 16)
1733 		error("Width need to be a multiple of 16 (current Width is %d)\n", width);
1734 #endif
1735 	stream += 8;
1736 	for (i = 0; i < nr_components; i++) {
1737 		cid = *stream++;
1738 		sampling_factor = *stream++;
1739 		Q_table = *stream++;
1740 		c = &priv->component_infos[i];
1741 #if SANITY_CHECK
1742 		c->cid = cid;
1743 #endif
1744 		c->Vfactor = sampling_factor & 0xf;
1745 		c->Hfactor = sampling_factor >> 4;
1746 		c->Q_table = priv->Q_tables[Q_table];
1747 		trace("Component:%d  factor:%dx%d  Quantization table:%d\n",
1748 				cid, c->Hfactor, c->Hfactor, Q_table);
1749 
1750 	}
1751 	priv->width = width;
1752 	priv->height = height;
1753 
1754 	trace("< SOF marker\n");
1755 
1756 	return 0;
1757 }
1758 
parse_SOS(struct jdec_private * priv,const unsigned char * stream)1759 static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
1760 {
1761 	unsigned int i, cid, table;
1762 	unsigned int nr_components = stream[2];
1763 
1764 	trace("> SOS marker\n");
1765 
1766 #if SANITY_CHECK
1767 	if (nr_components != 3 && nr_components != 1)
1768 		error("We only support YCbCr image\n");
1769 #endif
1770 
1771 	if (nr_components == 1)
1772 		priv->flags |= TINYJPEG_FLAGS_PLANAR_JPEG;
1773 #if SANITY_CHECK
1774 	else if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
1775 		error("SOS with more than 1 component while decoding planar JPEG\n");
1776 #endif
1777 
1778 	stream += 3;
1779 	for (i = 0; i < nr_components; i++) {
1780 		cid = *stream++;
1781 		table = *stream++;
1782 		if (nr_components == 1) {
1783 #if SANITY_CHECK
1784 			/* Find matching cid so we store the tables in the right component */
1785 			for (i = 0; i < COMPONENTS; i++)
1786 				if (priv->component_infos[i].cid == cid)
1787 					break;
1788 
1789 			if (i == COMPONENTS)
1790 				error("Unknown cid in SOS: %u\n", cid);
1791 
1792 			priv->current_cid = cid;
1793 #else
1794 			i = cid - 1;
1795 #endif
1796 			trace("SOS cid: %u, using component_info: %u\n", cid, i);
1797 		}
1798 #if SANITY_CHECK
1799 		if ((table & 0xf) >= HUFFMAN_TABLES)
1800 			error("We do not support more than %d AC Huffman table\n",
1801 					HUFFMAN_TABLES);
1802 		if ((table >> 4) >= HUFFMAN_TABLES)
1803 			error("We do not support more than %d DC Huffman table\n",
1804 					HUFFMAN_TABLES);
1805 		if (cid != priv->component_infos[i].cid)
1806 			error("SOS cid order (%u:%u) isn't compatible with the SOF marker (%u:%u)\n",
1807 					i, cid, i, priv->component_infos[i].cid);
1808 		trace("ComponentId:%u  tableAC:%d tableDC:%d\n", cid, table & 0xf, table >> 4);
1809 #endif
1810 		priv->component_infos[i].AC_table = &priv->HTAC[table & 0xf];
1811 		priv->component_infos[i].DC_table = &priv->HTDC[table >> 4];
1812 	}
1813 	priv->stream = stream + 3;
1814 
1815 	/* ITU-T T.81 (9/92) chapter E.1.3 clearly states that RSTm is to be set to 0 at the beginning of each scan */
1816 	priv->last_rst_marker_seen = 0;
1817 
1818 	trace("< SOS marker\n");
1819 
1820 	return 0;
1821 }
1822 
parse_DHT(struct jdec_private * priv,const unsigned char * stream)1823 static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
1824 {
1825 	unsigned int count, i;
1826 	unsigned char huff_bits[17];
1827 	int length, index;
1828 
1829 	length = be16_to_cpu(stream) - 2;
1830 	stream += 2;	/* Skip length */
1831 
1832 	trace("> DHT marker (length=%d)\n", length);
1833 
1834 	while (length > 0) {
1835 		index = *stream++;
1836 
1837 		/* We need to calculate the number of bytes 'vals' will takes */
1838 		huff_bits[0] = 0;
1839 		count = 0;
1840 		for (i = 1; i < 17; i++) {
1841 			huff_bits[i] = *stream++;
1842 			count += huff_bits[i];
1843 		}
1844 #if SANITY_CHECK
1845 		if (count > 1024)
1846 			error("No more than 1024 bytes is allowed to describe a huffman table\n");
1847 		if ((index & 0xf) >= HUFFMAN_TABLES)
1848 			error("No mode than %d Huffman tables is supported\n", HUFFMAN_TABLES);
1849 		trace("Huffman table %s n%d\n", (index & 0xf0) ? "AC" : "DC", index & 0xf);
1850 		trace("Length of the table: %d\n", count);
1851 #endif
1852 
1853 		if (index & 0xf0) {
1854 			if (build_huffman_table(priv, huff_bits, stream, &priv->HTAC[index & 0xf]))
1855 				return -1;
1856 		} else {
1857 			if (build_huffman_table(priv, huff_bits, stream, &priv->HTDC[index & 0xf]))
1858 				return -1;
1859 		}
1860 
1861 		length -= 1;
1862 		length -= 16;
1863 		length -= count;
1864 		stream += count;
1865 	}
1866 	trace("< DHT marker\n");
1867 	return 0;
1868 }
1869 
parse_DRI(struct jdec_private * priv,const unsigned char * stream)1870 static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
1871 {
1872 	unsigned int length;
1873 
1874 	trace("> DRI marker\n");
1875 
1876 	length = be16_to_cpu(stream);
1877 
1878 #if SANITY_CHECK
1879 	if (length != 4)
1880 		error("Length of DRI marker need to be 4\n");
1881 #endif
1882 
1883 	priv->restart_interval = be16_to_cpu(stream + 2);
1884 
1885 #if DEBUG
1886 	trace("Restart interval = %d\n", priv->restart_interval);
1887 #endif
1888 
1889 	trace("< DRI marker\n");
1890 
1891 	return 0;
1892 }
1893 
1894 
1895 
resync(struct jdec_private * priv)1896 static void resync(struct jdec_private *priv)
1897 {
1898 	int i;
1899 
1900 	/* Init DC coefficients */
1901 	for (i = 0; i < COMPONENTS; i++)
1902 		priv->component_infos[i].previous_DC = 0;
1903 
1904 	priv->reservoir = 0;
1905 	priv->nbits_in_reservoir = 0;
1906 	if (priv->restart_interval > 0)
1907 		priv->restarts_to_go = priv->restart_interval;
1908 	else
1909 		priv->restarts_to_go = -1;
1910 }
1911 
find_next_rst_marker(struct jdec_private * priv)1912 static int find_next_rst_marker(struct jdec_private *priv)
1913 {
1914 	int rst_marker_found = 0;
1915 	int marker;
1916 	const unsigned char *stream = priv->stream;
1917 
1918 	/* Parse marker */
1919 	while (!rst_marker_found) {
1920 		while (*stream++ != 0xff) {
1921 			if (stream >= priv->stream_end)
1922 				error("EOF while search for a RST marker.\n");
1923 		}
1924 		/* Skip any padding ff byte (this is normal) */
1925 		while (*stream == 0xff) {
1926 			stream++;
1927 			if (stream >= priv->stream_end)
1928 				error("EOF while search for a RST marker.\n");
1929 		}
1930 
1931 		marker = *stream++;
1932 		if ((RST + priv->last_rst_marker_seen) == marker)
1933 			rst_marker_found = 1;
1934 		else if (marker >= RST && marker <= RST7)
1935 			error("Wrong Reset marker found, abording\n");
1936 		else if (marker == EOI)
1937 			return 0;
1938 	}
1939 
1940 	priv->stream = stream;
1941 	priv->last_rst_marker_seen++;
1942 	priv->last_rst_marker_seen &= 7;
1943 
1944 	return 0;
1945 }
1946 
find_next_sos_marker(struct jdec_private * priv)1947 static int find_next_sos_marker(struct jdec_private *priv)
1948 {
1949 	const unsigned char *stream = priv->stream;
1950 
1951 	/* Parse marker */
1952 	while (1) {
1953 		while (*stream++ != 0xff) {
1954 			if (stream >= priv->stream_end)
1955 				error("EOF while search for a SOS marker.\n");
1956 		}
1957 		/* Skip any padding ff byte (this is normal) */
1958 		while (*stream == 0xff) {
1959 			stream++;
1960 			if (stream >= priv->stream_end)
1961 				error("EOF while search for a SOS marker.\n");
1962 		}
1963 
1964 		if (*stream++ == SOS)
1965 			break; /* Found it ! */
1966 	}
1967 
1968 	priv->stream = stream;
1969 
1970 	return 0;
1971 }
1972 
parse_JFIF(struct jdec_private * priv,const unsigned char * stream)1973 static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
1974 {
1975 	int chuck_len;
1976 	int marker;
1977 	int sof_marker_found = 0;
1978 	int dqt_marker_found = 0;
1979 	int sos_marker_found = 0;
1980 	int dht_marker_found = 0;
1981 	const unsigned char *next_chunck;
1982 
1983 	/* Parse marker */
1984 	while (!sos_marker_found) {
1985 		if (*stream++ != 0xff)
1986 			goto bogus_jpeg_format;
1987 		/* Skip any padding ff byte (this is normal) */
1988 		while (*stream == 0xff)
1989 			stream++;
1990 
1991 		marker = *stream++;
1992 		chuck_len = be16_to_cpu(stream);
1993 		next_chunck = stream + chuck_len;
1994 		switch (marker) {
1995 		case SOF:
1996 			if (parse_SOF(priv, stream) < 0)
1997 				return -1;
1998 			sof_marker_found = 1;
1999 			break;
2000 		case DQT:
2001 			if (parse_DQT(priv, stream) < 0)
2002 				return -1;
2003 			dqt_marker_found = 1;
2004 			break;
2005 		case SOS:
2006 			if (parse_SOS(priv, stream) < 0)
2007 				return -1;
2008 			sos_marker_found = 1;
2009 			break;
2010 		case DHT:
2011 			if (parse_DHT(priv, stream) < 0)
2012 				return -1;
2013 			dht_marker_found = 1;
2014 			break;
2015 		case DRI:
2016 			if (parse_DRI(priv, stream) < 0)
2017 				return -1;
2018 			break;
2019 		default:
2020 			trace("> Unknown marker %2.2x\n", marker);
2021 			break;
2022 		}
2023 
2024 		stream = next_chunck;
2025 	}
2026 
2027 	if (!sof_marker_found ||
2028 			(!dqt_marker_found && !(priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)))
2029 		goto bogus_jpeg_format;
2030 
2031 	if (!dht_marker_found) {
2032 		trace("No Huffman table loaded, using the default one\n");
2033 		if (build_default_huffman_tables(priv))
2034 			return -1;
2035 	}
2036 
2037 #ifdef SANITY_CHECK
2038 	if ((priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
2039 			|| (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
2040 		error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
2041 	if ((priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
2042 			|| (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
2043 		error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
2044 	if ((priv->component_infos[cCb].Hfactor != 1)
2045 			|| (priv->component_infos[cCr].Hfactor != 1)
2046 			|| (priv->component_infos[cCb].Vfactor != 1)
2047 			|| (priv->component_infos[cCr].Vfactor != 1))
2048 		error("Sampling other than 1x1 for Cr and Cb is not supported\n");
2049 	if ((priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG) &&
2050 			((priv->component_infos[cY].Hfactor != 2)
2051 			    || (priv->component_infos[cY].Vfactor != 2)))
2052 		error("Sampling other than 2x2 for Y is not supported with planar JPEG\n");
2053 #endif
2054 
2055 	return 0;
2056 bogus_jpeg_format:
2057 	error("Bogus jpeg format\n");
2058 	return -1;
2059 }
2060 
2061 /*******************************************************************************
2062  *
2063  * Functions exported of the library.
2064  *
2065  * Note: Some applications can access directly to internal pointer of the
2066  * structure. It's is not recommended, but if you have many images to
2067  * uncompress with the same parameters, some functions can be called to speedup
2068  * the decoding.
2069  *
2070  ******************************************************************************/
2071 
2072 /**
2073  * Allocate a new tinyjpeg decoder object.
2074  *
2075  * Before calling any other functions, an object need to be called.
2076  */
tinyjpeg_init(void)2077 struct jdec_private *tinyjpeg_init(void)
2078 {
2079 	struct jdec_private *priv;
2080 
2081 	priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private));
2082 	if (priv == NULL)
2083 		return NULL;
2084 	return priv;
2085 }
2086 
2087 /**
2088  * Free a tinyjpeg object.
2089  *
2090  * No others function can be called after this one.
2091  */
tinyjpeg_free(struct jdec_private * priv)2092 void tinyjpeg_free(struct jdec_private *priv)
2093 {
2094 	int i;
2095 
2096 	for (i = 0; i < COMPONENTS; i++) {
2097 		free(priv->components[i]);
2098 		free(priv->tmp_buf[i]);
2099 		priv->components[i] = NULL;
2100 		priv->tmp_buf[i] = NULL;
2101 	}
2102 	priv->tmp_buf_y_size = 0;
2103 	free(priv->stream_filtered);
2104 	free(priv);
2105 }
2106 
2107 /**
2108  * Initialize the tinyjpeg object and prepare the decoding of the stream.
2109  *
2110  * Check if the jpeg can be decoded with this jpeg decoder.
2111  * Fill some table used for preprocessing.
2112  */
tinyjpeg_parse_header(struct jdec_private * priv,const unsigned char * buf,unsigned int size)2113 int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
2114 {
2115 	/* Identify the file */
2116 	if ((buf[0] != 0xFF) || (buf[1] != SOI))
2117 		error("Not a JPG file ?\n");
2118 
2119 	priv->stream_end = buf + size;
2120 
2121 	return parse_JFIF(priv, buf + 2);
2122 }
2123 
2124 static const decode_MCU_fct decode_mcu_3comp_table[4] = {
2125 	decode_MCU_1x1_3planes,
2126 	decode_MCU_1x2_3planes,
2127 	decode_MCU_2x1_3planes,
2128 	decode_MCU_2x2_3planes,
2129 };
2130 
2131 static const decode_MCU_fct pixart_decode_mcu_3comp_table[4] = {
2132 	NULL,
2133 	NULL,
2134 	pixart_decode_MCU_2x1_3planes,
2135 	NULL,
2136 };
2137 
2138 static const decode_MCU_fct decode_mcu_1comp_table[4] = {
2139 	decode_MCU_1x1_1plane,
2140 	decode_MCU_1x2_1plane,
2141 	decode_MCU_2x1_1plane,
2142 	decode_MCU_2x2_1plane,
2143 };
2144 
2145 static const convert_colorspace_fct convert_colorspace_yuv420p[4] = {
2146 	YCrCB_to_YUV420P_1x1,
2147 	YCrCB_to_YUV420P_1x2,
2148 	YCrCB_to_YUV420P_2x1,
2149 	YCrCB_to_YUV420P_2x2,
2150 };
2151 
2152 static const convert_colorspace_fct convert_colorspace_rgb24[4] = {
2153 	YCrCB_to_RGB24_1x1,
2154 	YCrCB_to_RGB24_1x2,
2155 	YCrCB_to_RGB24_2x1,
2156 	YCrCB_to_RGB24_2x2,
2157 };
2158 
2159 static const convert_colorspace_fct convert_colorspace_bgr24[4] = {
2160 	YCrCB_to_BGR24_1x1,
2161 	YCrCB_to_BGR24_1x2,
2162 	YCrCB_to_BGR24_2x1,
2163 	YCrCB_to_BGR24_2x2,
2164 };
2165 
2166 static const convert_colorspace_fct convert_colorspace_grey[4] = {
2167 	YCrCB_to_Grey_1x1,
2168 	YCrCB_to_Grey_1x2,
2169 	YCrCB_to_Grey_2x1,
2170 	YCrCB_to_Grey_2x2,
2171 };
2172 
2173 int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt);
2174 
2175 /* This function parses and removes the special Pixart JPEG chunk headers */
pixart_filter(struct jdec_private * priv,unsigned char * dest,const unsigned char * src,int n)2176 static int pixart_filter(struct jdec_private *priv, unsigned char *dest,
2177 		const unsigned char *src, int n)
2178 {
2179 	int chunksize, copied = 0;
2180 
2181 	/* The first data bytes encodes the image size:
2182 	   0x60: 160x120
2183 	   0x61: 320x240
2184 	   0x62: 640x480
2185 	   160x120 images are not chunked due to their small size!
2186 	*/
2187 	if (src[0] == 0x60) {
2188 			memcpy(dest, src + 1, n - 1);
2189 			return n - 1;
2190 	}
2191 
2192 	src++;
2193 	n--;
2194 
2195 	/* The first chunk is always 1024 bytes, 5 bytes are dropped in the
2196 kernel: 0xff 0xff 0x00 0xff 0x96, and we skip one unknown byte */
2197 	chunksize = 1024 - 6;
2198 
2199 	while (1) {
2200 		if (n < chunksize)
2201 			break; /* Short frame */
2202 
2203 		memcpy(dest, src, chunksize);
2204 		dest += chunksize;
2205 		src += chunksize;
2206 		copied += chunksize;
2207 		n -= chunksize;
2208 
2209 		if (n < 4)
2210 			break; /* Short frame */
2211 
2212 		if (src[0] != 0xff || src[1] != 0xff || src[2] != 0xff)
2213 			error("Missing Pixart ff ff ff xx header, "
2214 			      "got: %02x %02x %02x %02x, copied sofar: %d\n",
2215 			      src[0], src[1], src[2], src[3], copied);
2216 		if (src[3] > 6)
2217 			error("Unexpected Pixart chunk size: %d\n", src[3]);
2218 
2219 		chunksize = src[3];
2220 		src += 4;
2221 		n -= 4;
2222 
2223 		if (chunksize == 0) {
2224 			/* 0 indicates we are done, copy whatever remains */
2225 			memcpy(dest, src, n);
2226 			return copied + n;
2227 		}
2228 
2229 		chunksize = 2048 >> chunksize;
2230 	}
2231 	error("Short Pixart JPEG frame\n");
2232 }
2233 
2234 /**
2235  * Decode and convert the jpeg image into @pixfmt@ image
2236  *
2237  * Note: components will be automaticaly allocated if no memory is attached.
2238  */
tinyjpeg_decode(struct jdec_private * priv,int pixfmt)2239 int tinyjpeg_decode(struct jdec_private *priv, int pixfmt)
2240 {
2241 	unsigned int x, y, xstride_by_mcu, ystride_by_mcu;
2242 	unsigned int bytes_per_blocklines[3], bytes_per_mcu[3];
2243 	decode_MCU_fct decode_MCU;
2244 	const decode_MCU_fct *decode_mcu_table;
2245 	const convert_colorspace_fct *colorspace_array_conv;
2246 	convert_colorspace_fct convert_to_pixfmt;
2247 
2248 	if (setjmp(priv->jump_state))
2249 		return -1;
2250 
2251 	if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
2252 		return tinyjpeg_decode_planar(priv, pixfmt);
2253 
2254 	/* To keep gcc happy initialize some array */
2255 	bytes_per_mcu[1] = 0;
2256 	bytes_per_mcu[2] = 0;
2257 	bytes_per_blocklines[1] = 0;
2258 	bytes_per_blocklines[2] = 0;
2259 
2260 	decode_mcu_table = decode_mcu_3comp_table;
2261 	if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
2262 		int length;
2263 
2264 		priv->stream_filtered =
2265 			v4lconvert_alloc_buffer(priv->stream_end - priv->stream,
2266 					&priv->stream_filtered,
2267 					&priv->stream_filtered_bufsize);
2268 		if (!priv->stream_filtered)
2269 			error("Out of memory!\n");
2270 
2271 		length =  pixart_filter(priv, priv->stream_filtered,
2272 				priv->stream, priv->stream_end - priv->stream);
2273 		if (length < 0)
2274 			return length;
2275 		priv->stream = priv->stream_filtered;
2276 		priv->stream_end = priv->stream + length;
2277 		priv->first_marker = 0;
2278 
2279 		decode_mcu_table = pixart_decode_mcu_3comp_table;
2280 	}
2281 
2282 	switch (pixfmt) {
2283 	case TINYJPEG_FMT_YUV420P:
2284 		colorspace_array_conv = convert_colorspace_yuv420p;
2285 		if (priv->components[0] == NULL)
2286 			priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
2287 		if (priv->components[1] == NULL)
2288 			priv->components[1] = (uint8_t *)malloc(priv->width * priv->height/4);
2289 		if (priv->components[2] == NULL)
2290 			priv->components[2] = (uint8_t *)malloc(priv->width * priv->height/4);
2291 		bytes_per_blocklines[0] = priv->width;
2292 		bytes_per_blocklines[1] = priv->width/4;
2293 		bytes_per_blocklines[2] = priv->width/4;
2294 		bytes_per_mcu[0] = 8;
2295 		bytes_per_mcu[1] = 4;
2296 		bytes_per_mcu[2] = 4;
2297 		break;
2298 
2299 	case TINYJPEG_FMT_RGB24:
2300 		colorspace_array_conv = convert_colorspace_rgb24;
2301 		if (priv->components[0] == NULL)
2302 			priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
2303 		bytes_per_blocklines[0] = priv->width * 3;
2304 		bytes_per_mcu[0] = 3*8;
2305 		break;
2306 
2307 	case TINYJPEG_FMT_BGR24:
2308 		colorspace_array_conv = convert_colorspace_bgr24;
2309 		if (priv->components[0] == NULL)
2310 			priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
2311 		bytes_per_blocklines[0] = priv->width * 3;
2312 		bytes_per_mcu[0] = 3*8;
2313 		break;
2314 
2315 	case TINYJPEG_FMT_GREY:
2316 		decode_mcu_table = decode_mcu_1comp_table;
2317 		if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)
2318 			error("Greyscale output not support for PIXART JPEG's\n");
2319 		colorspace_array_conv = convert_colorspace_grey;
2320 		if (priv->components[0] == NULL)
2321 			priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
2322 		bytes_per_blocklines[0] = priv->width;
2323 		bytes_per_mcu[0] = 8;
2324 		break;
2325 
2326 	default:
2327 		error("Bad pixel format\n");
2328 	}
2329 
2330 	xstride_by_mcu = ystride_by_mcu = 8;
2331 	if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
2332 		decode_MCU = decode_mcu_table[0];
2333 		convert_to_pixfmt = colorspace_array_conv[0];
2334 		trace("Use decode 1x1 sampling\n");
2335 	} else if (priv->component_infos[cY].Hfactor == 1) {
2336 		decode_MCU = decode_mcu_table[1];
2337 		convert_to_pixfmt = colorspace_array_conv[1];
2338 		ystride_by_mcu = 16;
2339 		trace("Use decode 1x2 sampling (not supported)\n");
2340 	} else if (priv->component_infos[cY].Vfactor == 2) {
2341 		decode_MCU = decode_mcu_table[3];
2342 		convert_to_pixfmt = colorspace_array_conv[3];
2343 		xstride_by_mcu = 16;
2344 		ystride_by_mcu = 16;
2345 		trace("Use decode 2x2 sampling\n");
2346 	} else {
2347 		decode_MCU = decode_mcu_table[2];
2348 		convert_to_pixfmt = colorspace_array_conv[2];
2349 		xstride_by_mcu = 16;
2350 		trace("Use decode 2x1 sampling\n");
2351 	}
2352 
2353 	if (decode_MCU == NULL)
2354 		error("no decode MCU function for this JPEG format (PIXART?)\n");
2355 
2356 	resync(priv);
2357 
2358 	/* Don't forget to that block can be either 8 or 16 lines */
2359 	bytes_per_blocklines[0] *= ystride_by_mcu;
2360 	bytes_per_blocklines[1] *= ystride_by_mcu;
2361 	bytes_per_blocklines[2] *= ystride_by_mcu;
2362 
2363 	bytes_per_mcu[0] *= xstride_by_mcu / 8;
2364 	bytes_per_mcu[1] *= xstride_by_mcu / 8;
2365 	bytes_per_mcu[2] *= xstride_by_mcu / 8;
2366 
2367 	/* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */
2368 	for (y = 0; y < priv->height / ystride_by_mcu; y++) {
2369 		//trace("Decoding row %d\n", y);
2370 		priv->plane[0] = priv->components[0] + (y * bytes_per_blocklines[0]);
2371 		priv->plane[1] = priv->components[1] + (y * bytes_per_blocklines[1]);
2372 		priv->plane[2] = priv->components[2] + (y * bytes_per_blocklines[2]);
2373 		for (x = 0; x < priv->width; x += xstride_by_mcu) {
2374 			decode_MCU(priv);
2375 			convert_to_pixfmt(priv);
2376 			priv->plane[0] += bytes_per_mcu[0];
2377 			priv->plane[1] += bytes_per_mcu[1];
2378 			priv->plane[2] += bytes_per_mcu[2];
2379 			if (priv->restarts_to_go > 0) {
2380 				priv->restarts_to_go--;
2381 				if (priv->restarts_to_go == 0) {
2382 					priv->stream -= (priv->nbits_in_reservoir / 8);
2383 					resync(priv);
2384 					if (find_next_rst_marker(priv) < 0)
2385 						return -1;
2386 				}
2387 			}
2388 		}
2389 	}
2390 
2391 	if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
2392 		/* Additional sanity check for funky Pixart format */
2393 		if ((priv->stream_end - priv->stream) > 5)
2394 			error("Pixart JPEG error, stream does not end with EOF marker\n");
2395 	}
2396 
2397 	return 0;
2398 }
2399 
tinyjpeg_decode_planar(struct jdec_private * priv,int pixfmt)2400 int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt)
2401 {
2402 	unsigned int i, x, y;
2403 	uint8_t *y_buf, *u_buf, *v_buf, *p, *p2;
2404 
2405 	switch (pixfmt) {
2406 	case TINYJPEG_FMT_GREY:
2407 		error("Greyscale output not supported with planar JPEG input\n");
2408 		break;
2409 
2410 	case TINYJPEG_FMT_RGB24:
2411 	case TINYJPEG_FMT_BGR24:
2412 		if (priv->tmp_buf_y_size < (priv->width * priv->height)) {
2413 			for (i = 0; i < COMPONENTS; i++) {
2414 				free(priv->tmp_buf[i]);
2415 				priv->tmp_buf[i] = malloc(priv->width * priv->height / (i ? 4 : 1));
2416 				if (!priv->tmp_buf[i])
2417 					error("Could not allocate memory for temporary buffers\n");
2418 			}
2419 			priv->tmp_buf_y_size = priv->width * priv->height;
2420 		}
2421 		y_buf = priv->tmp_buf[cY];
2422 		u_buf = priv->tmp_buf[cCb];
2423 		v_buf = priv->tmp_buf[cCr];
2424 		break;
2425 
2426 	case TINYJPEG_FMT_YUV420P:
2427 		y_buf = priv->components[cY];
2428 		u_buf = priv->components[cCb];
2429 		v_buf = priv->components[cCr];
2430 		break;
2431 
2432 	default:
2433 		error("Bad pixel format\n");
2434 	}
2435 
2436 #if SANITY_CHECK
2437 	if (priv->current_cid != priv->component_infos[cY].cid)
2438 		error("Planar jpeg first SOS cid does not match Y cid (%u:%u)\n",
2439 				priv->current_cid, priv->component_infos[cY].cid);
2440 #endif
2441 
2442 	resync(priv);
2443 
2444 	for (y = 0; y < priv->height / 8; y++) {
2445 		for (x = 0; x < priv->width / 8; x++) {
2446 			process_Huffman_data_unit(priv, cY);
2447 			IDCT(&priv->component_infos[cY], y_buf, priv->width);
2448 			y_buf += 8;
2449 		}
2450 		y_buf += 7 * priv->width;
2451 	}
2452 
2453 	priv->stream -= (priv->nbits_in_reservoir/8);
2454 	resync(priv);
2455 	if (find_next_sos_marker(priv) < 0)
2456 		return -1;
2457 	if (parse_SOS(priv, priv->stream) < 0)
2458 		return -1;
2459 
2460 #if SANITY_CHECK
2461 	if (priv->current_cid != priv->component_infos[cCb].cid)
2462 		error("Planar jpeg second SOS cid does not match Cn cid (%u:%u)\n",
2463 				priv->current_cid, priv->component_infos[cCb].cid);
2464 #endif
2465 
2466 	for (y = 0; y < priv->height / 16; y++) {
2467 		for (x = 0; x < priv->width / 16; x++) {
2468 			process_Huffman_data_unit(priv, cCb);
2469 			IDCT(&priv->component_infos[cCb], u_buf, priv->width / 2);
2470 			u_buf += 8;
2471 		}
2472 		u_buf += 7 * (priv->width / 2);
2473 	}
2474 
2475 	priv->stream -= (priv->nbits_in_reservoir / 8);
2476 	resync(priv);
2477 	if (find_next_sos_marker(priv) < 0)
2478 		return -1;
2479 	if (parse_SOS(priv, priv->stream) < 0)
2480 		return -1;
2481 
2482 #if SANITY_CHECK
2483 	if (priv->current_cid != priv->component_infos[cCr].cid)
2484 		error("Planar jpeg third SOS cid does not match Cr cid (%u:%u)\n",
2485 				priv->current_cid, priv->component_infos[cCr].cid);
2486 #endif
2487 
2488 	for (y = 0; y < priv->height / 16; y++) {
2489 		for (x = 0; x < priv->width / 16; x++) {
2490 			process_Huffman_data_unit(priv, cCr);
2491 			IDCT(&priv->component_infos[cCr], v_buf, priv->width / 2);
2492 			v_buf += 8;
2493 		}
2494 		v_buf += 7 * (priv->width / 2);
2495 	}
2496 
2497 #define SCALEBITS       10
2498 #define ONE_HALF        (1UL << (SCALEBITS - 1))
2499 #define FIX(x)          ((int)((x) * (1UL << SCALEBITS) + 0.5))
2500 
2501 	switch (pixfmt) {
2502 	case TINYJPEG_FMT_RGB24:
2503 		y_buf = priv->tmp_buf[cY];
2504 		u_buf = priv->tmp_buf[cCb];
2505 		v_buf = priv->tmp_buf[cCr];
2506 		p = priv->components[0];
2507 		p2 = priv->components[0] + priv->width * 3;
2508 
2509 		for (y = 0; y < priv->height / 2; y++) {
2510 			for (x = 0; x < priv->width / 2; x++) {
2511 				int l, cb, cr;
2512 				int add_r, add_g, add_b;
2513 				int r, g , b;
2514 
2515 				cb = *u_buf++ - 128;
2516 				cr = *v_buf++ - 128;
2517 				add_r = FIX(1.40200) * cr + ONE_HALF;
2518 				add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
2519 				add_b = FIX(1.77200) * cb + ONE_HALF;
2520 
2521 				l  = (*y_buf) << SCALEBITS;
2522 				r = (l + add_r) >> SCALEBITS;
2523 				*p++ = clamp(r);
2524 				g = (l + add_g) >> SCALEBITS;
2525 				*p++ = clamp(g);
2526 				b = (l + add_b) >> SCALEBITS;
2527 				*p++ = clamp(b);
2528 
2529 				l  = (y_buf[priv->width]) << SCALEBITS;
2530 				r = (l + add_r) >> SCALEBITS;
2531 				*p2++ = clamp(r);
2532 				g = (l + add_g) >> SCALEBITS;
2533 				*p2++ = clamp(g);
2534 				b = (l + add_b) >> SCALEBITS;
2535 				*p2++ = clamp(b);
2536 
2537 				y_buf++;
2538 
2539 				l  = (*y_buf) << SCALEBITS;
2540 				r = (l + add_r) >> SCALEBITS;
2541 				*p++ = clamp(r);
2542 				g = (l + add_g) >> SCALEBITS;
2543 				*p++ = clamp(g);
2544 				b = (l + add_b) >> SCALEBITS;
2545 				*p++ = clamp(b);
2546 
2547 				l  = (y_buf[priv->width]) << SCALEBITS;
2548 				r = (l + add_r) >> SCALEBITS;
2549 				*p2++ = clamp(r);
2550 				g = (l + add_g) >> SCALEBITS;
2551 				*p2++ = clamp(g);
2552 				b = (l + add_b) >> SCALEBITS;
2553 				*p2++ = clamp(b);
2554 
2555 				y_buf++;
2556 			}
2557 			y_buf += priv->width;
2558 			p  += priv->width * 3;
2559 			p2 += priv->width * 3;
2560 		}
2561 		break;
2562 
2563 	case TINYJPEG_FMT_BGR24:
2564 		y_buf = priv->tmp_buf[cY];
2565 		u_buf = priv->tmp_buf[cCb];
2566 		v_buf = priv->tmp_buf[cCr];
2567 		p = priv->components[0];
2568 		p2 = priv->components[0] + priv->width * 3;
2569 
2570 		for (y = 0; y < priv->height / 2; y++) {
2571 			for (x = 0; x < priv->width / 2; x++) {
2572 				int l, cb, cr;
2573 				int add_r, add_g, add_b;
2574 				int r, g , b;
2575 
2576 				cb = *u_buf++ - 128;
2577 				cr = *v_buf++ - 128;
2578 				add_r = FIX(1.40200) * cr + ONE_HALF;
2579 				add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
2580 				add_b = FIX(1.77200) * cb + ONE_HALF;
2581 
2582 				l  = (*y_buf) << SCALEBITS;
2583 				b = (l + add_b) >> SCALEBITS;
2584 				*p++ = clamp(b);
2585 				g = (l + add_g) >> SCALEBITS;
2586 				*p++ = clamp(g);
2587 				r = (l + add_r) >> SCALEBITS;
2588 				*p++ = clamp(r);
2589 
2590 				l  = (y_buf[priv->width]) << SCALEBITS;
2591 				b = (l + add_b) >> SCALEBITS;
2592 				*p2++ = clamp(b);
2593 				g = (l + add_g) >> SCALEBITS;
2594 				*p2++ = clamp(g);
2595 				r = (l + add_r) >> SCALEBITS;
2596 				*p2++ = clamp(r);
2597 
2598 				y_buf++;
2599 
2600 				l  = (*y_buf) << SCALEBITS;
2601 				b = (l + add_b) >> SCALEBITS;
2602 				*p++ = clamp(b);
2603 				g = (l + add_g) >> SCALEBITS;
2604 				*p++ = clamp(g);
2605 				r = (l + add_r) >> SCALEBITS;
2606 				*p++ = clamp(r);
2607 
2608 				l  = (y_buf[priv->width]) << SCALEBITS;
2609 				b = (l + add_b) >> SCALEBITS;
2610 				*p2++ = clamp(b);
2611 				g = (l + add_g) >> SCALEBITS;
2612 				*p2++ = clamp(g);
2613 				r = (l + add_r) >> SCALEBITS;
2614 				*p2++ = clamp(r);
2615 
2616 				y_buf++;
2617 			}
2618 			y_buf += priv->width;
2619 			p  += priv->width * 3;
2620 			p2 += priv->width * 3;
2621 		}
2622 		break;
2623 	}
2624 
2625 #undef SCALEBITS
2626 #undef ONE_HALF
2627 #undef FIX
2628 
2629 	return 0;
2630 }
2631 
tinyjpeg_get_errorstring(struct jdec_private * priv)2632 const char *tinyjpeg_get_errorstring(struct jdec_private *priv)
2633 {
2634 	return priv->error_string;
2635 }
2636 
tinyjpeg_get_size(struct jdec_private * priv,unsigned int * width,unsigned int * height)2637 void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height)
2638 {
2639 	*width = priv->width;
2640 	*height = priv->height;
2641 }
2642 
tinyjpeg_get_components(struct jdec_private * priv,unsigned char ** components)2643 int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components)
2644 {
2645 	int i;
2646 
2647 	for (i = 0; i < COMPONENTS && priv->components[i]; i++)
2648 		components[i] = priv->components[i];
2649 	return 0;
2650 }
2651 
tinyjpeg_set_components(struct jdec_private * priv,unsigned char ** components,unsigned int ncomponents)2652 int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents)
2653 {
2654 	unsigned int i;
2655 
2656 	if (ncomponents > COMPONENTS)
2657 		ncomponents = COMPONENTS;
2658 	for (i = 0; i < ncomponents; i++)
2659 		priv->components[i] = components[i];
2660 	return 0;
2661 }
2662 
tinyjpeg_set_flags(struct jdec_private * priv,int flags)2663 int tinyjpeg_set_flags(struct jdec_private *priv, int flags)
2664 {
2665 	int oldflags = priv->flags;
2666 
2667 	priv->flags = flags;
2668 	return oldflags;
2669 }
2670 
2671