2
* Small jpeg decoder library
4
* Copyright (c) 2006, Luc Saillard <luc@saillard.org>
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are met:
9
* - Redistributions of source code must retain the above copyright notice,
10
* this list of conditions and the following disclaimer.
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.
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.
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.
41
#include "tinyjpeg-internal.h"
42
#include "libv4lconvert-priv.h"
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 */
68
#define trace(fmt, args...) do { \
69
FILE *f = fopen("/tmp/jpeg.log", "a"); \
70
fprintf(f, fmt, ## args); \
77
#define trace(fmt, args...) do { \
78
fprintf(stderr, fmt, ## args); \
84
#define trace(fmt, args...) do { } while (0)
87
#define error(fmt, args...) do { \
88
snprintf(priv->error_string, sizeof(priv->error_string), fmt, ## args); \
94
static char *print_bits(unsigned int value, char *bitstr)
100
if (value & (1UL<<i))
107
bitstr[j++] = (value & (1UL<<i))?'1':'0';
114
static void print_next_16bytes(int offset, const unsigned char *stream)
116
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",
118
stream[0], stream[1], stream[2], stream[3],
119
stream[4], stream[5], stream[6], stream[7],
120
stream[8], stream[9], stream[10], stream[11],
121
stream[12], stream[13], stream[14], stream[15]);
127
static const unsigned char zigzag[64] =
129
0, 1, 5, 6, 14, 15, 27, 28,
130
2, 4, 7, 13, 16, 26, 29, 42,
131
3, 8, 12, 17, 25, 30, 41, 43,
132
9, 11, 18, 24, 31, 40, 44, 53,
133
10, 19, 23, 32, 39, 45, 52, 54,
134
20, 22, 33, 38, 46, 51, 55, 60,
135
21, 34, 37, 47, 50, 56, 59, 61,
136
35, 36, 48, 49, 57, 58, 62, 63
139
/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
140
/* IMPORTANT: these are only valid for 8-bit data precision! */
141
static const unsigned char bits_dc_luminance[17] =
143
0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
145
static const unsigned char val_dc_luminance[] =
147
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
150
static const unsigned char bits_dc_chrominance[17] =
152
0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
154
static const unsigned char val_dc_chrominance[] =
156
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
159
static const unsigned char bits_ac_luminance[17] =
161
0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
163
static const unsigned char val_ac_luminance[] =
165
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
166
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
167
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
168
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
169
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
170
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
171
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
172
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
173
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
174
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
175
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
176
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
177
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
178
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
179
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
180
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
181
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
182
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
183
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
184
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
188
static const unsigned char bits_ac_chrominance[17] =
190
0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
193
static const unsigned char val_ac_chrominance[] =
195
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
196
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
197
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
198
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
199
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
200
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
201
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
202
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
203
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
204
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
205
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
206
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
207
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
208
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
209
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
210
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
211
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
212
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
213
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
214
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
218
const unsigned char pixart_quantization[][64] = {
220
0x07, 0x07, 0x08, 0x0a, 0x09, 0x07, 0x0d, 0x0b,
221
0x0c, 0x0d, 0x11, 0x10, 0x0f, 0x12, 0x17, 0x27,
222
0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
223
0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
224
0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
225
0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
226
0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
227
0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
230
0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
231
0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
232
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
233
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
234
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
235
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
236
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
237
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
242
* 4 functions to manage the stream
244
* fill_nbits: put at least nbits in the reservoir of bits.
245
* But convert any 0xff,0x00 into 0xff
246
* get_nbits: read nbits from the stream, and put it in result,
247
* bits is removed from the stream and the reservoir is filled
248
* automaticaly. The result is signed according to the number of
250
* look_nbits: read nbits from the stream without marking as read.
251
* skip_nbits: read nbits from the stream but do not return the result.
253
* stream: current pointer in the jpeg data (read bytes per bytes)
254
* nbits_in_reservoir: number of bits filled into the reservoir
255
* reservoir: register that contains bits information. Only nbits_in_reservoir
259
* Ex: 0000 0000 1010 0000 1111 0000 <== reservoir
262
* To get two bits from this example
263
* result = (reservoir >> 15) & 3
266
#define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
267
while (nbits_in_reservoir<nbits_wanted) \
270
if (stream >= priv->stream_end) { \
271
snprintf(priv->error_string, sizeof(priv->error_string), \
272
"fill_nbits error: need %u more bits\n", \
273
nbits_wanted - nbits_in_reservoir); \
274
longjmp(priv->jump_state, -EIO); \
278
if (c == 0xff && *stream == 0x00) \
281
nbits_in_reservoir+=8; \
285
/* Signed version !!!! */
286
#define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
287
fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
288
result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
289
nbits_in_reservoir -= (nbits_wanted); \
290
reservoir &= ((1U<<nbits_in_reservoir)-1); \
291
if ((unsigned int)result < (1UL<<((nbits_wanted)-1))) \
292
result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \
295
#define look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
296
fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
297
result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
300
/* To speed up the decoding, we assume that the reservoir have enough bit
302
* #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
303
* fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
304
* nbits_in_reservoir -= (nbits_wanted); \
305
* reservoir &= ((1U<<nbits_in_reservoir)-1); \
308
#define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
309
nbits_in_reservoir -= (nbits_wanted); \
310
reservoir &= ((1U<<nbits_in_reservoir)-1); \
313
#define be16_to_cpu(x) (((x)[0]<<8)|(x)[1])
315
static void resync(struct jdec_private *priv);
318
* Get the next (valid) huffman code in the stream.
320
* To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is
321
* lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code
322
* and the value by using two lookup table.
323
* Else if the value is not found, just search (linear) into an array for each
324
* bits is the code is present.
326
* If the code is not present for any reason, -1 is return.
328
static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table)
331
unsigned int extra_nbits, nbits;
334
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
335
value = huffman_table->lookup[hcode];
338
unsigned int code_size = huffman_table->code_size[value];
339
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
343
/* Decode more bits each time ... */
344
for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++)
346
nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
348
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode);
349
slowtable = huffman_table->slowtable[extra_nbits];
350
/* Search if the code is in this array */
351
while (slowtable[0]) {
352
if (slowtable[0] == hcode) {
353
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits);
359
snprintf(priv->error_string, sizeof(priv->error_string),
360
"unknown huffman code: %08x\n", (unsigned int)hcode);
361
longjmp(priv->jump_state, -EIO);
367
* Decode a single block that contains the DCT coefficients.
368
* The table coefficients is already dezigzaged at the end of the operation.
371
static void process_Huffman_data_unit(struct jdec_private *priv, int component)
374
unsigned int huff_code;
375
unsigned char size_val, count_0;
377
struct component *c = &priv->component_infos[component];
380
/* Initialize the DCT coef table */
381
memset(DCT, 0, sizeof(DCT));
383
/* DC coefficient decoding */
384
huff_code = get_next_huffman_code(priv, c->DC_table);
386
get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
387
DCT[0] += c->previous_DC;
388
c->previous_DC = DCT[0];
390
DCT[0] = c->previous_DC;
394
/* AC coefficient decoding */
398
huff_code = get_next_huffman_code(priv, c->AC_table);
400
size_val = huff_code & 0xF;
401
count_0 = huff_code >> 4;
406
break; /* EOB found, go out */
407
else if (count_0 == 0xF)
408
j += 16; /* skip 16 zeros */
412
j += count_0; /* skip count_0 zeroes */
414
get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
421
snprintf(priv->error_string, sizeof(priv->error_string),
422
"error: more then 63 AC components (%d) in huffman unit\n", (int)j);
423
longjmp(priv->jump_state, -EIO);
426
for (j = 0; j < 64; j++)
427
c->DCT[j] = DCT[zigzag[j]];
431
* Takes two array of bits, and build the huffman table for size, and code
433
* lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
434
* code_size will be used to known how many bits this symbol is encoded.
435
* slowtable will be used when the first lookup didn't give the result.
437
static int build_huffman_table(struct jdec_private *priv, const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
439
unsigned int i, j, code, code_size, val, nbits;
440
unsigned char huffsize[257], *hz;
441
unsigned int huffcode[257], *hc;
443
int slowtable_used[16-HUFFMAN_HASH_NBITS];
447
* huffsize[X] => numbers of bits to write vals[X]
450
for (i=1; i<=16; i++)
452
for (j=1; j<=bits[i]; j++)
457
memset(table->lookup, 0xff, sizeof(table->lookup));
458
for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
459
slowtable_used[i] = 0;
461
/* Build a temp array
462
* huffcode[X] => code used to write vals[X]
470
while (*hz == nbits) {
479
* Build the lookup table, and the slowtable if needed.
481
next_free_entry = -1;
482
for (i=0; huffsize[i]; i++)
486
code_size = huffsize[i];
488
trace("val=%2.2x code=%8.8x codesize=%2.2d\n", i, code, code_size);
490
table->code_size[val] = code_size;
491
if (code_size <= HUFFMAN_HASH_NBITS)
494
* Good: val can be put in the lookup table, so fill all value of this
495
* column with value val
497
int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
498
code <<= HUFFMAN_HASH_NBITS - code_size;
500
table->lookup[code++] = val;
505
/* Perhaps sorting the array will be an optimization */
506
int slowtable_index = code_size-HUFFMAN_HASH_NBITS-1;
508
if (slowtable_used[slowtable_index] == 254)
509
error("slow Huffman table overflow\n");
511
table->slowtable[slowtable_index][slowtable_used[slowtable_index]]
513
table->slowtable[slowtable_index][slowtable_used[slowtable_index] + 1]
515
slowtable_used[slowtable_index] += 2;
519
for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
520
table->slowtable[i][slowtable_used[i]] = 0;
525
static int build_default_huffman_tables(struct jdec_private *priv)
527
if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
528
&& priv->default_huffman_table_initialized)
531
if (build_huffman_table(priv, bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]))
533
if (build_huffman_table(priv, bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]))
536
if (build_huffman_table(priv, bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]))
538
if (build_huffman_table(priv, bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]))
541
priv->default_huffman_table_initialized = 1;
547
/*******************************************************************************
549
* Colorspace conversion routine
553
* YCbCr is defined per CCIR 601-1, except that Cb and Cr are
554
* normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
555
* The conversion equations to be implemented are therefore
556
* R = Y + 1.40200 * Cr
557
* G = Y - 0.34414 * Cb - 0.71414 * Cr
558
* B = Y + 1.77200 * Cb
560
******************************************************************************/
562
static unsigned char clamp(int i)
574
* YCrCb -> YUV420P (1x1)
579
static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
581
const unsigned char *s, *y;
598
for (j=0; j<8; j+=2, s+=2)
600
s += 8; /* Skip one line */
601
p += priv->width/2 - 4;
608
for (j=0; j<8; j+=2, s+=2)
610
s += 8; /* Skip one line */
611
p += priv->width/2 - 4;
616
* YCrCb -> YUV420P (2x1)
621
static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
624
const unsigned char *s, *y1;
641
s += 16; /* Skip one line */
650
s += 16; /* Skip one line */
657
* YCrCb -> YUV420P (1x2)
664
static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
666
const unsigned char *s, *y;
683
for (j=0; j<8; j+=2, s+=2)
685
p += priv->width/2 - 4;
692
for (j=0; j<8; j+=2, s+=2)
694
p += priv->width/2 - 4;
699
* YCrCb -> YUV420P (2x2)
706
static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
709
const unsigned char *s, *y1;
741
* YCrCb -> RGB24 (1x1)
746
static void YCrCB_to_RGB24_1x1(struct jdec_private *priv)
748
const unsigned char *Y, *Cb, *Cr;
751
int offset_to_next_row;
754
#define ONE_HALF (1UL << (SCALEBITS-1))
755
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
761
offset_to_next_row = priv->width*3 - 8*3;
762
for (i=0; i<8; i++) {
764
for (j=0; j<8; j++) {
767
int add_r, add_g, add_b;
770
y = (*Y++) << SCALEBITS;
773
add_r = FIX(1.40200) * cr + ONE_HALF;
774
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
775
add_b = FIX(1.77200) * cb + ONE_HALF;
777
r = (y + add_r) >> SCALEBITS;
779
g = (y + add_g) >> SCALEBITS;
781
b = (y + add_b) >> SCALEBITS;
786
p += offset_to_next_row;
796
* YCrCb -> BGR24 (1x1)
801
static void YCrCB_to_BGR24_1x1(struct jdec_private *priv)
803
const unsigned char *Y, *Cb, *Cr;
806
int offset_to_next_row;
809
#define ONE_HALF (1UL << (SCALEBITS-1))
810
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
816
offset_to_next_row = priv->width*3 - 8*3;
817
for (i=0; i<8; i++) {
819
for (j=0; j<8; j++) {
822
int add_r, add_g, add_b;
825
y = (*Y++) << SCALEBITS;
828
add_r = FIX(1.40200) * cr + ONE_HALF;
829
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
830
add_b = FIX(1.77200) * cb + ONE_HALF;
832
b = (y + add_b) >> SCALEBITS;
834
g = (y + add_g) >> SCALEBITS;
836
r = (y + add_r) >> SCALEBITS;
841
p += offset_to_next_row;
852
* YCrCb -> RGB24 (2x1)
857
static void YCrCB_to_RGB24_2x1(struct jdec_private *priv)
859
const unsigned char *Y, *Cb, *Cr;
862
int offset_to_next_row;
865
#define ONE_HALF (1UL << (SCALEBITS-1))
866
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
872
offset_to_next_row = priv->width*3 - 16*3;
873
for (i=0; i<8; i++) {
875
for (j=0; j<8; j++) {
878
int add_r, add_g, add_b;
881
y = (*Y++) << SCALEBITS;
884
add_r = FIX(1.40200) * cr + ONE_HALF;
885
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
886
add_b = FIX(1.77200) * cb + ONE_HALF;
888
r = (y + add_r) >> SCALEBITS;
890
g = (y + add_g) >> SCALEBITS;
892
b = (y + add_b) >> SCALEBITS;
895
y = (*Y++) << SCALEBITS;
896
r = (y + add_r) >> SCALEBITS;
898
g = (y + add_g) >> SCALEBITS;
900
b = (y + add_b) >> SCALEBITS;
905
p += offset_to_next_row;
915
* YCrCb -> BGR24 (2x1)
920
static void YCrCB_to_BGR24_2x1(struct jdec_private *priv)
922
const unsigned char *Y, *Cb, *Cr;
925
int offset_to_next_row;
928
#define ONE_HALF (1UL << (SCALEBITS-1))
929
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
935
offset_to_next_row = priv->width*3 - 16*3;
936
for (i=0; i<8; i++) {
938
for (j=0; j<8; j++) {
941
int add_r, add_g, add_b;
946
add_r = FIX(1.40200) * cr + ONE_HALF;
947
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
948
add_b = FIX(1.77200) * cb + ONE_HALF;
950
y = (*Y++) << SCALEBITS;
951
b = (y + add_b) >> SCALEBITS;
953
g = (y + add_g) >> SCALEBITS;
955
r = (y + add_r) >> SCALEBITS;
958
y = (*Y++) << SCALEBITS;
959
b = (y + add_b) >> SCALEBITS;
961
g = (y + add_g) >> SCALEBITS;
963
r = (y + add_r) >> SCALEBITS;
968
p += offset_to_next_row;
978
* YCrCb -> RGB24 (1x2)
985
static void YCrCB_to_RGB24_1x2(struct jdec_private *priv)
987
const unsigned char *Y, *Cb, *Cr;
988
unsigned char *p, *p2;
990
int offset_to_next_row;
993
#define ONE_HALF (1UL << (SCALEBITS-1))
994
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
997
p2 = priv->plane[0] + priv->width*3;
1001
offset_to_next_row = 2*priv->width*3 - 8*3;
1002
for (i=0; i<8; i++) {
1004
for (j=0; j<8; j++) {
1007
int add_r, add_g, add_b;
1012
add_r = FIX(1.40200) * cr + ONE_HALF;
1013
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1014
add_b = FIX(1.77200) * cb + ONE_HALF;
1016
y = (*Y++) << SCALEBITS;
1017
r = (y + add_r) >> SCALEBITS;
1019
g = (y + add_g) >> SCALEBITS;
1021
b = (y + add_b) >> SCALEBITS;
1024
y = (Y[8-1]) << SCALEBITS;
1025
r = (y + add_r) >> SCALEBITS;
1027
g = (y + add_g) >> SCALEBITS;
1029
b = (y + add_b) >> SCALEBITS;
1034
p += offset_to_next_row;
1035
p2 += offset_to_next_row;
1045
* YCrCb -> BGR24 (1x2)
1052
static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
1054
const unsigned char *Y, *Cb, *Cr;
1055
unsigned char *p, *p2;
1057
int offset_to_next_row;
1059
#define SCALEBITS 10
1060
#define ONE_HALF (1UL << (SCALEBITS-1))
1061
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
1064
p2 = priv->plane[0] + priv->width*3;
1068
offset_to_next_row = 2*priv->width*3 - 8*3;
1069
for (i=0; i<8; i++) {
1071
for (j=0; j<8; j++) {
1074
int add_r, add_g, add_b;
1079
add_r = FIX(1.40200) * cr + ONE_HALF;
1080
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1081
add_b = FIX(1.77200) * cb + ONE_HALF;
1083
y = (*Y++) << SCALEBITS;
1084
b = (y + add_b) >> SCALEBITS;
1086
g = (y + add_g) >> SCALEBITS;
1088
r = (y + add_r) >> SCALEBITS;
1091
y = (Y[8-1]) << SCALEBITS;
1092
b = (y + add_b) >> SCALEBITS;
1094
g = (y + add_g) >> SCALEBITS;
1096
r = (y + add_r) >> SCALEBITS;
1101
p += offset_to_next_row;
1102
p2 += offset_to_next_row;
1113
* YCrCb -> RGB24 (2x2)
1120
static void YCrCB_to_RGB24_2x2(struct jdec_private *priv)
1122
const unsigned char *Y, *Cb, *Cr;
1123
unsigned char *p, *p2;
1125
int offset_to_next_row;
1127
#define SCALEBITS 10
1128
#define ONE_HALF (1UL << (SCALEBITS-1))
1129
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
1132
p2 = priv->plane[0] + priv->width*3;
1136
offset_to_next_row = (priv->width*3*2) - 16*3;
1137
for (i=0; i<8; i++) {
1139
for (j=0; j<8; j++) {
1142
int add_r, add_g, add_b;
1147
add_r = FIX(1.40200) * cr + ONE_HALF;
1148
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1149
add_b = FIX(1.77200) * cb + ONE_HALF;
1151
y = (*Y++) << SCALEBITS;
1152
r = (y + add_r) >> SCALEBITS;
1154
g = (y + add_g) >> SCALEBITS;
1156
b = (y + add_b) >> SCALEBITS;
1159
y = (*Y++) << SCALEBITS;
1160
r = (y + add_r) >> SCALEBITS;
1162
g = (y + add_g) >> SCALEBITS;
1164
b = (y + add_b) >> SCALEBITS;
1167
y = (Y[16-2]) << SCALEBITS;
1168
r = (y + add_r) >> SCALEBITS;
1170
g = (y + add_g) >> SCALEBITS;
1172
b = (y + add_b) >> SCALEBITS;
1175
y = (Y[16-1]) << SCALEBITS;
1176
r = (y + add_r) >> SCALEBITS;
1178
g = (y + add_g) >> SCALEBITS;
1180
b = (y + add_b) >> SCALEBITS;
1184
p += offset_to_next_row;
1185
p2 += offset_to_next_row;
1196
* YCrCb -> BGR24 (2x2)
1203
static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
1205
const unsigned char *Y, *Cb, *Cr;
1206
unsigned char *p, *p2;
1208
int offset_to_next_row;
1210
#define SCALEBITS 10
1211
#define ONE_HALF (1UL << (SCALEBITS-1))
1212
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
1215
p2 = priv->plane[0] + priv->width*3;
1219
offset_to_next_row = (priv->width*3*2) - 16*3;
1220
for (i=0; i<8; i++) {
1222
for (j=0; j<8; j++) {
1225
int add_r, add_g, add_b;
1230
add_r = FIX(1.40200) * cr + ONE_HALF;
1231
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
1232
add_b = FIX(1.77200) * cb + ONE_HALF;
1234
y = (*Y++) << SCALEBITS;
1235
b = (y + add_b) >> SCALEBITS;
1237
g = (y + add_g) >> SCALEBITS;
1239
r = (y + add_r) >> SCALEBITS;
1242
y = (*Y++) << SCALEBITS;
1243
b = (y + add_b) >> SCALEBITS;
1245
g = (y + add_g) >> SCALEBITS;
1247
r = (y + add_r) >> SCALEBITS;
1250
y = (Y[16-2]) << SCALEBITS;
1251
b = (y + add_b) >> SCALEBITS;
1253
g = (y + add_g) >> SCALEBITS;
1255
r = (y + add_r) >> SCALEBITS;
1258
y = (Y[16-1]) << SCALEBITS;
1259
b = (y + add_b) >> SCALEBITS;
1261
g = (y + add_g) >> SCALEBITS;
1263
r = (y + add_r) >> SCALEBITS;
1267
p += offset_to_next_row;
1268
p2 += offset_to_next_row;
1280
* YCrCb -> Grey (1x1)
1285
static void YCrCB_to_Grey_1x1(struct jdec_private *priv)
1287
const unsigned char *y;
1290
int offset_to_next_row;
1294
offset_to_next_row = priv->width;
1296
for (i=0; i<8; i++) {
1299
p += offset_to_next_row;
1304
* YCrCb -> Grey (2x1)
1309
static void YCrCB_to_Grey_2x1(struct jdec_private *priv)
1311
const unsigned char *y;
1318
for (i=0; i<8; i++) {
1327
* YCrCb -> Grey (1x2)
1334
static void YCrCB_to_Grey_1x2(struct jdec_private *priv)
1336
const unsigned char *y;
1343
for (i=0; i<16; i++) {
1351
* YCrCb -> Grey (2x2)
1358
static void YCrCB_to_Grey_2x2(struct jdec_private *priv)
1360
const unsigned char *y;
1367
for (i=0; i<16; i++) {
1376
* Decode all the 3 components for 1x1
1378
static void decode_MCU_1x1_3planes(struct jdec_private *priv)
1381
process_Huffman_data_unit(priv, cY);
1382
IDCT(&priv->component_infos[cY], priv->Y, 8);
1385
process_Huffman_data_unit(priv, cCb);
1386
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1389
process_Huffman_data_unit(priv, cCr);
1390
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1394
* Decode a 1x1 directly in 1 color
1396
static void decode_MCU_1x1_1plane(struct jdec_private *priv)
1399
process_Huffman_data_unit(priv, cY);
1400
IDCT(&priv->component_infos[cY], priv->Y, 8);
1403
process_Huffman_data_unit(priv, cCb);
1404
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1407
process_Huffman_data_unit(priv, cCr);
1408
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1418
static void decode_MCU_2x1_3planes(struct jdec_private *priv)
1421
process_Huffman_data_unit(priv, cY);
1422
IDCT(&priv->component_infos[cY], priv->Y, 16);
1423
process_Huffman_data_unit(priv, cY);
1424
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
1427
process_Huffman_data_unit(priv, cCb);
1428
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1431
process_Huffman_data_unit(priv, cCr);
1432
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1435
static void pixart_decode_MCU_2x1_3planes(struct jdec_private *priv)
1437
unsigned char marker;
1439
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, 8, marker);
1440
/* I think the marker indicates which quantization table to use, iow
1441
a Pixart JPEG may have a different quantization table per MCU, most
1442
MCU's have 0x44 as marker for which our special Pixart quantization
1443
tables are correct. Unfortunately with a 7302 some blocks also have 0x48,
1444
and sometimes even other values. As 0x48 is quite common too, we really
1445
need to find out the correct table for that, as currently the blocks
1446
with a 0x48 marker look wrong. During normal operation the marker stays
1447
within the range below, if it gets out of this range we're most likely
1449
if (marker < 0x20 || marker > 0x7f) {
1450
snprintf(priv->error_string, sizeof(priv->error_string),
1451
"Pixart JPEG error: invalid MCU marker: 0x%02x\n",
1452
(unsigned int)marker);
1453
longjmp(priv->jump_state, -EIO);
1455
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, 8);
1458
process_Huffman_data_unit(priv, cY);
1459
IDCT(&priv->component_infos[cY], priv->Y, 16);
1460
process_Huffman_data_unit(priv, cY);
1461
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
1464
process_Huffman_data_unit(priv, cCb);
1465
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1468
process_Huffman_data_unit(priv, cCr);
1469
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1478
static void decode_MCU_2x1_1plane(struct jdec_private *priv)
1481
process_Huffman_data_unit(priv, cY);
1482
IDCT(&priv->component_infos[cY], priv->Y, 16);
1483
process_Huffman_data_unit(priv, cY);
1484
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
1487
process_Huffman_data_unit(priv, cCb);
1490
process_Huffman_data_unit(priv, cCr);
1502
static void decode_MCU_2x2_3planes(struct jdec_private *priv)
1505
process_Huffman_data_unit(priv, cY);
1506
IDCT(&priv->component_infos[cY], priv->Y, 16);
1507
process_Huffman_data_unit(priv, cY);
1508
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
1509
process_Huffman_data_unit(priv, cY);
1510
IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
1511
process_Huffman_data_unit(priv, cY);
1512
IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
1515
process_Huffman_data_unit(priv, cCb);
1516
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1519
process_Huffman_data_unit(priv, cCr);
1520
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1524
* Decode a 2x2 directly in GREY format (8bits)
1531
static void decode_MCU_2x2_1plane(struct jdec_private *priv)
1534
process_Huffman_data_unit(priv, cY);
1535
IDCT(&priv->component_infos[cY], priv->Y, 16);
1536
process_Huffman_data_unit(priv, cY);
1537
IDCT(&priv->component_infos[cY], priv->Y+8, 16);
1538
process_Huffman_data_unit(priv, cY);
1539
IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
1540
process_Huffman_data_unit(priv, cY);
1541
IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
1544
process_Huffman_data_unit(priv, cCb);
1547
process_Huffman_data_unit(priv, cCr);
1558
static void decode_MCU_1x2_3planes(struct jdec_private *priv)
1561
process_Huffman_data_unit(priv, cY);
1562
IDCT(&priv->component_infos[cY], priv->Y, 8);
1563
process_Huffman_data_unit(priv, cY);
1564
IDCT(&priv->component_infos[cY], priv->Y+64, 8);
1567
process_Huffman_data_unit(priv, cCb);
1568
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
1571
process_Huffman_data_unit(priv, cCr);
1572
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
1583
static void decode_MCU_1x2_1plane(struct jdec_private *priv)
1586
process_Huffman_data_unit(priv, cY);
1587
IDCT(&priv->component_infos[cY], priv->Y, 8);
1588
process_Huffman_data_unit(priv, cY);
1589
IDCT(&priv->component_infos[cY], priv->Y+64, 8);
1592
process_Huffman_data_unit(priv, cCb);
1595
process_Huffman_data_unit(priv, cCr);
1598
static void print_SOF(const unsigned char *stream)
1600
int width, height, nr_components, precision;
1602
const char *nr_components_to_string[] = {
1611
precision = stream[2];
1612
height = be16_to_cpu(stream+3);
1613
width = be16_to_cpu(stream+5);
1614
nr_components = stream[7];
1616
trace("> SOF marker\n");
1617
trace("Size:%dx%d nr_components:%d (%s) precision:%d\n",
1619
nr_components, nr_components_to_string[nr_components],
1623
/*******************************************************************************
1625
* JPEG/JFIF Parsing functions
1627
* Note: only a small subset of the jpeg file format is supported. No markers,
1628
* nor progressive stream is supported.
1630
******************************************************************************/
1632
static void build_quantization_table(float *qtable, const unsigned char *ref_table)
1634
/* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
1635
* For float AA&N IDCT method, divisors are equal to quantization
1636
* coefficients scaled by scalefactor[row]*scalefactor[col], where
1637
* scalefactor[0] = 1
1638
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
1639
* We apply a further scale factor of 8.
1640
* What's actually stored is 1/divisor so that the inner loop can
1641
* use a multiplication rather than a division.
1644
static const double aanscalefactor[8] = {
1645
1.0, 1.387039845, 1.306562965, 1.175875602,
1646
1.0, 0.785694958, 0.541196100, 0.275899379
1648
const unsigned char *zz = zigzag;
1650
for (i=0; i<8; i++) {
1651
for (j=0; j<8; j++) {
1652
*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
1658
static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
1662
const unsigned char *dqt_block_end;
1664
trace("> DQT marker\n");
1665
dqt_block_end = stream + be16_to_cpu(stream);
1666
stream += 2; /* Skip length */
1668
while (stream < dqt_block_end)
1673
error("16 bits quantization table is not supported\n");
1674
if (qi >= COMPONENTS)
1675
error("No more than %d quantization tables supported (got %d)\n",
1676
COMPONENTS, qi + 1);
1678
table = priv->Q_tables[qi];
1679
build_quantization_table(table, stream);
1682
trace("< DQT marker\n");
1686
static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
1688
int i, width, height, nr_components, cid, sampling_factor;
1690
struct component *c;
1692
trace("> SOF marker\n");
1695
height = be16_to_cpu(stream+3);
1696
width = be16_to_cpu(stream+5);
1697
nr_components = stream[7];
1700
error("Precision other than 8 is not supported\n");
1701
if (width>JPEG_MAX_WIDTH || height>JPEG_MAX_HEIGHT)
1702
error("Width and Height (%dx%d) seems suspicious\n", width, height);
1703
if (nr_components != 3)
1704
error("We only support YUV images\n");
1706
error("Height need to be a multiple of 8 (current height is %d)\n", height);
1708
error("Width need to be a multiple of 16 (current Width is %d)\n", width);
1711
for (i=0; i<nr_components; i++) {
1713
sampling_factor = *stream++;
1714
Q_table = *stream++;
1715
c = &priv->component_infos[i];
1719
c->Vfactor = sampling_factor&0xf;
1720
c->Hfactor = sampling_factor>>4;
1721
c->Q_table = priv->Q_tables[Q_table];
1722
trace("Component:%d factor:%dx%d Quantization table:%d\n",
1723
cid, c->Hfactor, c->Hfactor, Q_table );
1726
priv->width = width;
1727
priv->height = height;
1729
trace("< SOF marker\n");
1734
static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
1736
unsigned int i, cid, table;
1737
unsigned int nr_components = stream[2];
1739
trace("> SOS marker\n");
1742
if (nr_components != 3 && nr_components != 1)
1743
error("We only support YCbCr image\n");
1746
if (nr_components == 1)
1747
priv->flags |= TINYJPEG_FLAGS_PLANAR_JPEG;
1749
else if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
1750
error("SOS with more then 1 component while decoding planar JPEG\n");
1754
for (i=0;i<nr_components;i++) {
1757
if (nr_components == 1) {
1759
/* Find matching cid so we store the tables in the right component */
1760
for (i = 0; i < COMPONENTS; i++)
1761
if (priv->component_infos[i].cid == cid)
1764
if (i == COMPONENTS)
1765
error("Unknown cid in SOS: %u\n", cid);
1767
priv->current_cid = cid;
1771
trace("SOS cid: %u, using component_info: %u\n", cid, i);
1774
if ((table&0xf) >= HUFFMAN_TABLES)
1775
error("We do not support more than %d AC Huffman table\n",
1777
if ((table>>4) >= HUFFMAN_TABLES)
1778
error("We do not support more than %d DC Huffman table\n",
1780
if (cid != priv->component_infos[i].cid)
1781
error("SOS cid order (%u:%u) isn't compatible with the SOF marker (%u:%u)\n",
1782
i, cid, i, priv->component_infos[i].cid);
1783
trace("ComponentId:%u tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4);
1785
priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
1786
priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
1788
priv->stream = stream+3;
1790
/* 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 */
1791
priv->last_rst_marker_seen = 0;
1793
trace("< SOS marker\n");
1798
static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
1800
unsigned int count, i;
1801
unsigned char huff_bits[17];
1804
length = be16_to_cpu(stream) - 2;
1805
stream += 2; /* Skip length */
1807
trace("> DHT marker (length=%d)\n", length);
1812
/* We need to calculate the number of bytes 'vals' will takes */
1815
for (i=1; i<17; i++) {
1816
huff_bits[i] = *stream++;
1817
count += huff_bits[i];
1821
error("No more than 1024 bytes is allowed to describe a huffman table\n");
1822
if ( (index &0xf) >= HUFFMAN_TABLES)
1823
error("No mode than %d Huffman tables is supported\n", HUFFMAN_TABLES);
1824
trace("Huffman table %s n%d\n", (index&0xf0)?"AC":"DC", index&0xf);
1825
trace("Length of the table: %d\n", count);
1828
if (index & 0xf0 ) {
1829
if (build_huffman_table(priv, huff_bits, stream, &priv->HTAC[index&0xf]))
1832
if (build_huffman_table(priv, huff_bits, stream, &priv->HTDC[index&0xf]))
1841
trace("< DHT marker\n");
1845
static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
1847
unsigned int length;
1849
trace("> DRI marker\n");
1851
length = be16_to_cpu(stream);
1855
error("Length of DRI marker need to be 4\n");
1858
priv->restart_interval = be16_to_cpu(stream+2);
1861
trace("Restart interval = %d\n", priv->restart_interval);
1864
trace("< DRI marker\n");
1871
static void resync(struct jdec_private *priv)
1875
/* Init DC coefficients */
1876
for (i=0; i<COMPONENTS; i++)
1877
priv->component_infos[i].previous_DC = 0;
1879
priv->reservoir = 0;
1880
priv->nbits_in_reservoir = 0;
1881
if (priv->restart_interval > 0)
1882
priv->restarts_to_go = priv->restart_interval;
1884
priv->restarts_to_go = -1;
1887
static int find_next_rst_marker(struct jdec_private *priv)
1889
int rst_marker_found = 0;
1891
const unsigned char *stream = priv->stream;
1894
while (!rst_marker_found)
1896
while (*stream++ != 0xff)
1898
if (stream >= priv->stream_end)
1899
error("EOF while search for a RST marker.\n");
1901
/* Skip any padding ff byte (this is normal) */
1902
while (*stream == 0xff)
1905
if (stream >= priv->stream_end)
1906
error("EOF while search for a RST marker.\n");
1910
if ((RST+priv->last_rst_marker_seen) == marker)
1911
rst_marker_found = 1;
1912
else if (marker >= RST && marker <= RST7)
1913
error("Wrong Reset marker found, abording\n");
1914
else if (marker == EOI)
1918
priv->stream = stream;
1919
priv->last_rst_marker_seen++;
1920
priv->last_rst_marker_seen &= 7;
1925
static int find_next_sos_marker(struct jdec_private *priv)
1927
const unsigned char *stream = priv->stream;
1931
while (*stream++ != 0xff) {
1932
if (stream >= priv->stream_end)
1933
error("EOF while search for a SOS marker.\n");
1935
/* Skip any padding ff byte (this is normal) */
1936
while (*stream == 0xff) {
1938
if (stream >= priv->stream_end)
1939
error("EOF while search for a SOS marker.\n");
1942
if (*stream++ == SOS)
1943
break; /* Found it ! */
1946
priv->stream = stream;
1951
static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
1955
int sof_marker_found = 0;
1956
int dqt_marker_found = 0;
1957
int sos_marker_found = 0;
1958
int dht_marker_found = 0;
1959
const unsigned char *next_chunck;
1962
while (!sos_marker_found)
1964
if (*stream++ != 0xff)
1965
goto bogus_jpeg_format;
1966
/* Skip any padding ff byte (this is normal) */
1967
while (*stream == 0xff)
1971
chuck_len = be16_to_cpu(stream);
1972
next_chunck = stream + chuck_len;
1976
if (parse_SOF(priv, stream) < 0)
1978
sof_marker_found = 1;
1981
if (parse_DQT(priv, stream) < 0)
1983
dqt_marker_found = 1;
1986
if (parse_SOS(priv, stream) < 0)
1988
sos_marker_found = 1;
1991
if (parse_DHT(priv, stream) < 0)
1993
dht_marker_found = 1;
1996
if (parse_DRI(priv, stream) < 0)
2000
trace("> Unknown marker %2.2x\n", marker);
2004
stream = next_chunck;
2007
if ( !sof_marker_found ||
2008
(!dqt_marker_found && !(priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)))
2009
goto bogus_jpeg_format;
2011
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
2012
if (!priv->default_huffman_table_initialized) {
2013
build_quantization_table(priv->Q_tables[0], pixart_quantization[0]);
2014
build_quantization_table(priv->Q_tables[1], pixart_quantization[1]);
2018
if (!dht_marker_found) {
2019
trace("No Huffman table loaded, using the default one\n");
2020
if (build_default_huffman_tables(priv))
2025
if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
2026
|| (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
2027
error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
2028
if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
2029
|| (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
2030
error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
2031
if ( (priv->component_infos[cCb].Hfactor!=1)
2032
|| (priv->component_infos[cCr].Hfactor!=1)
2033
|| (priv->component_infos[cCb].Vfactor!=1)
2034
|| (priv->component_infos[cCr].Vfactor!=1))
2035
error("Sampling other than 1x1 for Cr and Cb is not supported\n");
2036
if ((priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG) &&
2037
( (priv->component_infos[cY].Hfactor!=2)
2038
|| (priv->component_infos[cY].Hfactor!=2)))
2039
error("Sampling other than 2x2 for Y is not supported with planar JPEG\n");
2044
error("Bogus jpeg format\n");
2048
/*******************************************************************************
2050
* Functions exported of the library.
2052
* Note: Some applications can access directly to internal pointer of the
2053
* structure. It's is not recommended, but if you have many images to
2054
* uncompress with the same parameters, some functions can be called to speedup
2057
******************************************************************************/
2060
* Allocate a new tinyjpeg decoder object.
2062
* Before calling any other functions, an object need to be called.
2064
struct jdec_private *tinyjpeg_init(void)
2066
struct jdec_private *priv;
2068
priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private));
2075
* Free a tinyjpeg object.
2077
* No others function can be called after this one.
2079
void tinyjpeg_free(struct jdec_private *priv)
2082
for (i=0; i<COMPONENTS; i++) {
2083
free(priv->components[i]);
2084
free(priv->tmp_buf[i]);
2085
priv->components[i] = NULL;
2086
priv->tmp_buf[i] = NULL;
2088
priv->tmp_buf_y_size = 0;
2089
free(priv->stream_filtered);
2094
* Initialize the tinyjpeg object and prepare the decoding of the stream.
2096
* Check if the jpeg can be decoded with this jpeg decoder.
2097
* Fill some table used for preprocessing.
2099
int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
2101
/* Identify the file */
2102
if ((buf[0] != 0xFF) || (buf[1] != SOI))
2103
error("Not a JPG file ?\n");
2105
priv->stream_end = buf + size;
2107
return parse_JFIF(priv, buf + 2);
2110
static const decode_MCU_fct decode_mcu_3comp_table[4] = {
2111
decode_MCU_1x1_3planes,
2112
decode_MCU_1x2_3planes,
2113
decode_MCU_2x1_3planes,
2114
decode_MCU_2x2_3planes,
2117
static const decode_MCU_fct pixart_decode_mcu_3comp_table[4] = {
2120
pixart_decode_MCU_2x1_3planes,
2124
static const decode_MCU_fct decode_mcu_1comp_table[4] = {
2125
decode_MCU_1x1_1plane,
2126
decode_MCU_1x2_1plane,
2127
decode_MCU_2x1_1plane,
2128
decode_MCU_2x2_1plane,
2131
static const convert_colorspace_fct convert_colorspace_yuv420p[4] = {
2132
YCrCB_to_YUV420P_1x1,
2133
YCrCB_to_YUV420P_1x2,
2134
YCrCB_to_YUV420P_2x1,
2135
YCrCB_to_YUV420P_2x2,
2138
static const convert_colorspace_fct convert_colorspace_rgb24[4] = {
2145
static const convert_colorspace_fct convert_colorspace_bgr24[4] = {
2152
static const convert_colorspace_fct convert_colorspace_grey[4] = {
2159
int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt);
2161
/* This function parses and removes the special Pixart JPEG chunk headers */
2162
static int pixart_filter(struct jdec_private *priv, unsigned char *dest,
2163
const unsigned char *src, int n)
2165
int chunksize, copied = 0;
2167
/* Skip mysterious first data byte */
2171
/* The first chunk is always 1024 bytes, 5 bytes are dropped in the
2172
kernel: 0xff 0xff 0x00 0xff 0x96, and we skip one unknown byte */
2173
chunksize = 1024 - 6;
2177
break; /* Short frame */
2179
memcpy(dest, src, chunksize);
2182
copied += chunksize;
2186
break; /* Short frame */
2188
if (src[0] != 0xff || src[1] != 0xff || src[2] != 0xff)
2189
error("Missing Pixart ff ff ff xx header, "
2190
"got: %02x %02x %02x %02x\n",
2191
src[0], src[1], src[2], src[3]);
2193
error("Unexpected Pixart chunk size: %d\n", src[3]);
2199
if (chunksize == 0) {
2200
/* 0 indicates we are done, copy whatever remains */
2201
memcpy(dest, src, n);
2205
chunksize = 2048 >> chunksize;
2207
error("Short Pixart JPEG frame\n");
2211
* Decode and convert the jpeg image into @pixfmt@ image
2213
* Note: components will be automaticaly allocated if no memory is attached.
2215
int tinyjpeg_decode(struct jdec_private *priv, int pixfmt)
2217
unsigned int x, y, xstride_by_mcu, ystride_by_mcu;
2218
unsigned int bytes_per_blocklines[3], bytes_per_mcu[3];
2219
decode_MCU_fct decode_MCU;
2220
const decode_MCU_fct *decode_mcu_table;
2221
const convert_colorspace_fct *colorspace_array_conv;
2222
convert_colorspace_fct convert_to_pixfmt;
2224
if (setjmp(priv->jump_state))
2227
if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
2228
return tinyjpeg_decode_planar(priv, pixfmt);
2230
/* To keep gcc happy initialize some array */
2231
bytes_per_mcu[1] = 0;
2232
bytes_per_mcu[2] = 0;
2233
bytes_per_blocklines[1] = 0;
2234
bytes_per_blocklines[2] = 0;
2236
decode_mcu_table = decode_mcu_3comp_table;
2237
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
2240
priv->stream_filtered =
2241
v4lconvert_alloc_buffer(priv->stream_end - priv->stream,
2242
&priv->stream_filtered,
2243
&priv->stream_filtered_bufsize);
2244
if (!priv->stream_filtered)
2245
error("Out of memory!\n");
2247
length = pixart_filter(priv, priv->stream_filtered,
2248
priv->stream, priv->stream_end - priv->stream);
2251
priv->stream = priv->stream_filtered;
2252
priv->stream_end = priv->stream + length;
2254
decode_mcu_table = pixart_decode_mcu_3comp_table;
2258
case TINYJPEG_FMT_YUV420P:
2259
colorspace_array_conv = convert_colorspace_yuv420p;
2260
if (priv->components[0] == NULL)
2261
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
2262
if (priv->components[1] == NULL)
2263
priv->components[1] = (uint8_t *)malloc(priv->width * priv->height/4);
2264
if (priv->components[2] == NULL)
2265
priv->components[2] = (uint8_t *)malloc(priv->width * priv->height/4);
2266
bytes_per_blocklines[0] = priv->width;
2267
bytes_per_blocklines[1] = priv->width/4;
2268
bytes_per_blocklines[2] = priv->width/4;
2269
bytes_per_mcu[0] = 8;
2270
bytes_per_mcu[1] = 4;
2271
bytes_per_mcu[2] = 4;
2274
case TINYJPEG_FMT_RGB24:
2275
colorspace_array_conv = convert_colorspace_rgb24;
2276
if (priv->components[0] == NULL)
2277
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
2278
bytes_per_blocklines[0] = priv->width * 3;
2279
bytes_per_mcu[0] = 3*8;
2282
case TINYJPEG_FMT_BGR24:
2283
colorspace_array_conv = convert_colorspace_bgr24;
2284
if (priv->components[0] == NULL)
2285
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
2286
bytes_per_blocklines[0] = priv->width * 3;
2287
bytes_per_mcu[0] = 3*8;
2290
case TINYJPEG_FMT_GREY:
2291
decode_mcu_table = decode_mcu_1comp_table;
2292
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)
2293
error("Greyscale output not support for PIXART JPEG's\n");
2294
colorspace_array_conv = convert_colorspace_grey;
2295
if (priv->components[0] == NULL)
2296
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
2297
bytes_per_blocklines[0] = priv->width;
2298
bytes_per_mcu[0] = 8;
2302
error("Bad pixel format\n");
2305
xstride_by_mcu = ystride_by_mcu = 8;
2306
if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
2307
decode_MCU = decode_mcu_table[0];
2308
convert_to_pixfmt = colorspace_array_conv[0];
2309
trace("Use decode 1x1 sampling\n");
2310
} else if (priv->component_infos[cY].Hfactor == 1) {
2311
decode_MCU = decode_mcu_table[1];
2312
convert_to_pixfmt = colorspace_array_conv[1];
2313
ystride_by_mcu = 16;
2314
trace("Use decode 1x2 sampling (not supported)\n");
2315
} else if (priv->component_infos[cY].Vfactor == 2) {
2316
decode_MCU = decode_mcu_table[3];
2317
convert_to_pixfmt = colorspace_array_conv[3];
2318
xstride_by_mcu = 16;
2319
ystride_by_mcu = 16;
2320
trace("Use decode 2x2 sampling\n");
2322
decode_MCU = decode_mcu_table[2];
2323
convert_to_pixfmt = colorspace_array_conv[2];
2324
xstride_by_mcu = 16;
2325
trace("Use decode 2x1 sampling\n");
2328
if (decode_MCU == NULL)
2329
error("no decode MCU function for this JPEG format (PIXART?)\n");
2333
/* Don't forget to that block can be either 8 or 16 lines */
2334
bytes_per_blocklines[0] *= ystride_by_mcu;
2335
bytes_per_blocklines[1] *= ystride_by_mcu;
2336
bytes_per_blocklines[2] *= ystride_by_mcu;
2338
bytes_per_mcu[0] *= xstride_by_mcu/8;
2339
bytes_per_mcu[1] *= xstride_by_mcu/8;
2340
bytes_per_mcu[2] *= xstride_by_mcu/8;
2342
/* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */
2343
for (y=0; y < priv->height/ystride_by_mcu; y++)
2345
//trace("Decoding row %d\n", y);
2346
priv->plane[0] = priv->components[0] + (y * bytes_per_blocklines[0]);
2347
priv->plane[1] = priv->components[1] + (y * bytes_per_blocklines[1]);
2348
priv->plane[2] = priv->components[2] + (y * bytes_per_blocklines[2]);
2349
for (x=0; x < priv->width; x+=xstride_by_mcu)
2352
convert_to_pixfmt(priv);
2353
priv->plane[0] += bytes_per_mcu[0];
2354
priv->plane[1] += bytes_per_mcu[1];
2355
priv->plane[2] += bytes_per_mcu[2];
2356
if (priv->restarts_to_go>0)
2358
priv->restarts_to_go--;
2359
if (priv->restarts_to_go == 0)
2361
priv->stream -= (priv->nbits_in_reservoir/8);
2363
if (find_next_rst_marker(priv) < 0)
2370
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
2371
/* Additional sanity check for funky Pixart format */
2372
if ((priv->stream_end - priv->stream) > 5)
2373
error("Pixart JPEG error, stream does not end with EOF marker\n");
2379
int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt)
2381
unsigned int i, x, y;
2382
uint8_t *y_buf, *u_buf, *v_buf, *p, *p2;
2385
case TINYJPEG_FMT_GREY:
2386
error("Greyscale output not supported with planar JPEG input\n");
2389
case TINYJPEG_FMT_RGB24:
2390
case TINYJPEG_FMT_BGR24:
2391
if (priv->tmp_buf_y_size < (priv->width * priv->height)) {
2392
for (i=0; i<COMPONENTS; i++) {
2393
free(priv->tmp_buf[i]);
2394
priv->tmp_buf[i] = malloc(priv->width * priv->height / (i ? 4:1));
2395
if (!priv->tmp_buf[i])
2396
error("Could not allocate memory for temporary buffers\n");
2398
priv->tmp_buf_y_size = priv->width * priv->height;
2400
y_buf = priv->tmp_buf[cY];
2401
u_buf = priv->tmp_buf[cCb];
2402
v_buf = priv->tmp_buf[cCr];
2405
case TINYJPEG_FMT_YUV420P:
2406
y_buf = priv->components[cY];
2407
u_buf = priv->components[cCb];
2408
v_buf = priv->components[cCr];
2412
error("Bad pixel format\n");
2416
if (priv->current_cid != priv->component_infos[cY].cid)
2417
error("Planar jpeg first SOS cid does not match Y cid (%u:%u)\n",
2418
priv->current_cid, priv->component_infos[cY].cid);
2423
for (y=0; y < priv->height/8; y++) {
2424
for (x=0; x < priv->width/8; x++) {
2425
process_Huffman_data_unit(priv, cY);
2426
IDCT(&priv->component_infos[cY], y_buf, priv->width);
2429
y_buf += 7 * priv->width;
2432
priv->stream -= (priv->nbits_in_reservoir/8);
2434
if (find_next_sos_marker(priv) < 0)
2436
if (parse_SOS(priv, priv->stream) < 0)
2440
if (priv->current_cid != priv->component_infos[cCb].cid)
2441
error("Planar jpeg second SOS cid does not match Cn cid (%u:%u)\n",
2442
priv->current_cid, priv->component_infos[cCb].cid);
2445
for (y=0; y < priv->height/16; y++) {
2446
for (x=0; x < priv->width/16; x++) {
2447
process_Huffman_data_unit(priv, cCb);
2448
IDCT(&priv->component_infos[cCb], u_buf, priv->width / 2);
2451
u_buf += 7 * (priv->width / 2);
2454
priv->stream -= (priv->nbits_in_reservoir/8);
2456
if (find_next_sos_marker(priv) < 0)
2458
if (parse_SOS(priv, priv->stream) < 0)
2462
if (priv->current_cid != priv->component_infos[cCr].cid)
2463
error("Planar jpeg third SOS cid does not match Cr cid (%u:%u)\n",
2464
priv->current_cid, priv->component_infos[cCr].cid);
2467
for (y=0; y < priv->height/16; y++) {
2468
for (x=0; x < priv->width/16; x++) {
2469
process_Huffman_data_unit(priv, cCr);
2470
IDCT(&priv->component_infos[cCr], v_buf, priv->width / 2);
2473
v_buf += 7 * (priv->width / 2);
2476
#define SCALEBITS 10
2477
#define ONE_HALF (1UL << (SCALEBITS-1))
2478
#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
2481
case TINYJPEG_FMT_RGB24:
2482
y_buf = priv->tmp_buf[cY];
2483
u_buf = priv->tmp_buf[cCb];
2484
v_buf = priv->tmp_buf[cCr];
2485
p = priv->components[0];
2486
p2 = priv->components[0] + priv->width * 3;
2488
for (y = 0; y < priv->height / 2; y++) {
2489
for (x = 0; x < priv->width / 2; x++) {
2491
int add_r, add_g, add_b;
2494
cb = *u_buf++ - 128;
2495
cr = *v_buf++ - 128;
2496
add_r = FIX(1.40200) * cr + ONE_HALF;
2497
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
2498
add_b = FIX(1.77200) * cb + ONE_HALF;
2500
l = (*y_buf) << SCALEBITS;
2501
r = (l + add_r) >> SCALEBITS;
2503
g = (l + add_g) >> SCALEBITS;
2505
b = (l + add_b) >> SCALEBITS;
2508
l = (y_buf[priv->width]) << SCALEBITS;
2509
r = (l + add_r) >> SCALEBITS;
2511
g = (l + add_g) >> SCALEBITS;
2513
b = (l + add_b) >> SCALEBITS;
2518
l = (*y_buf) << SCALEBITS;
2519
r = (l + add_r) >> SCALEBITS;
2521
g = (l + add_g) >> SCALEBITS;
2523
b = (l + add_b) >> SCALEBITS;
2526
l = (y_buf[priv->width]) << SCALEBITS;
2527
r = (l + add_r) >> SCALEBITS;
2529
g = (l + add_g) >> SCALEBITS;
2531
b = (l + add_b) >> SCALEBITS;
2536
y_buf += priv->width;
2537
p += priv->width * 3;
2538
p2 += priv->width * 3;
2542
case TINYJPEG_FMT_BGR24:
2543
y_buf = priv->tmp_buf[cY];
2544
u_buf = priv->tmp_buf[cCb];
2545
v_buf = priv->tmp_buf[cCr];
2546
p = priv->components[0];
2547
p2 = priv->components[0] + priv->width * 3;
2549
for (y = 0; y < priv->height / 2; y++) {
2550
for (x = 0; x < priv->width / 2; x++) {
2552
int add_r, add_g, add_b;
2555
cb = *u_buf++ - 128;
2556
cr = *v_buf++ - 128;
2557
add_r = FIX(1.40200) * cr + ONE_HALF;
2558
add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
2559
add_b = FIX(1.77200) * cb + ONE_HALF;
2561
l = (*y_buf) << SCALEBITS;
2562
b = (l + add_b) >> SCALEBITS;
2564
g = (l + add_g) >> SCALEBITS;
2566
r = (l + add_r) >> SCALEBITS;
2569
l = (y_buf[priv->width]) << SCALEBITS;
2570
b = (l + add_b) >> SCALEBITS;
2572
g = (l + add_g) >> SCALEBITS;
2574
r = (l + add_r) >> SCALEBITS;
2579
l = (*y_buf) << SCALEBITS;
2580
b = (l + add_b) >> SCALEBITS;
2582
g = (l + add_g) >> SCALEBITS;
2584
r = (l + add_r) >> SCALEBITS;
2587
l = (y_buf[priv->width]) << SCALEBITS;
2588
b = (l + add_b) >> SCALEBITS;
2590
g = (l + add_g) >> SCALEBITS;
2592
r = (l + add_r) >> SCALEBITS;
2597
y_buf += priv->width;
2598
p += priv->width * 3;
2599
p2 += priv->width * 3;
2611
const char *tinyjpeg_get_errorstring(struct jdec_private *priv)
2613
return priv->error_string;
2616
void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height)
2618
*width = priv->width;
2619
*height = priv->height;
2622
int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components)
2625
for (i=0; priv->components[i] && i<COMPONENTS; i++)
2626
components[i] = priv->components[i];
2630
int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents)
2633
if (ncomponents > COMPONENTS)
2634
ncomponents = COMPONENTS;
2635
for (i=0; i<ncomponents; i++)
2636
priv->components[i] = components[i];
2640
int tinyjpeg_set_flags(struct jdec_private *priv, int flags)
2642
int oldflags = priv->flags;
2643
priv->flags = flags;