~ubuntu-branches/ubuntu/precise/v4l-utils/precise

« back to all changes in this revision

Viewing changes to lib/libv4lconvert/tinyjpeg.c

  • Committer: Bazaar Package Importer
  • Author(s): Gregor Jasny
  • Date: 2010-02-28 19:44:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100228194415-067hdj8rvawj91zw
Tags: upstream-0.7.90
ImportĀ upstreamĀ versionĀ 0.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
  i=31;
 
98
  while (i>0)
 
99
   {
 
100
     if (value & (1UL<<i))
 
101
       break;
 
102
     i--;
 
103
   }
 
104
  j=0;
 
105
  while (i>=0)
 
106
   {
 
107
     bitstr[j++] = (value & (1UL<<i))?'1':'0';
 
108
     i--;
 
109
   }
 
110
  bitstr[j] = 0;
 
111
  return bitstr;
 
112
}
 
113
 
 
114
static void print_next_16bytes(int offset, const unsigned char *stream)
 
115
{
 
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",
 
117
        offset,
 
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]);
 
122
}
 
123
 
 
124
#endif
 
125
 
 
126
 
 
127
static const unsigned char zigzag[64] =
 
128
{
 
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
 
137
};
 
138
 
 
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] =
 
142
{
 
143
  0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
 
144
};
 
145
static const unsigned char val_dc_luminance[] =
 
146
{
 
147
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
 
148
};
 
149
 
 
150
static const unsigned char bits_dc_chrominance[17] =
 
151
{
 
152
  0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
 
153
};
 
154
static const unsigned char val_dc_chrominance[] =
 
155
{
 
156
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
 
157
};
 
158
 
 
159
static const unsigned char bits_ac_luminance[17] =
 
160
{
 
161
  0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
 
162
};
 
163
static const unsigned char val_ac_luminance[] =
 
164
{
 
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,
 
185
  0xf9, 0xfa
 
186
};
 
187
 
 
188
static const unsigned char bits_ac_chrominance[17] =
 
189
{
 
190
  0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
 
191
};
 
192
 
 
193
static const unsigned char val_ac_chrominance[] =
 
194
{
 
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,
 
215
  0xf9, 0xfa
 
216
};
 
217
 
 
218
const unsigned char pixart_quantization[][64] = {
 
219
  {
 
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,
 
228
  },
 
229
  {
 
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,
 
238
  },
 
239
};
 
240
 
 
241
/*
 
242
 * 4 functions to manage the stream
 
243
 *
 
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
 
249
 *             bits.
 
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.
 
252
 *
 
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
 
256
 *            is valid.
 
257
 *                          nbits_in_reservoir
 
258
 *                        <--    17 bits    -->
 
259
 *            Ex: 0000 0000 1010 0000 1111 0000   <== reservoir
 
260
 *                        ^
 
261
 *                        bit 1
 
262
 *            To get two bits from this example
 
263
 *                 result = (reservoir >> 15) & 3
 
264
 *
 
265
 */
 
266
#define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
 
267
   while (nbits_in_reservoir<nbits_wanted) \
 
268
    { \
 
269
      unsigned char c; \
 
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); \
 
275
      } \
 
276
      c = *stream++; \
 
277
      reservoir <<= 8; \
 
278
      if (c == 0xff && *stream == 0x00) \
 
279
        stream++; \
 
280
      reservoir |= c; \
 
281
      nbits_in_reservoir+=8; \
 
282
    } \
 
283
}  while(0);
 
284
 
 
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; \
 
293
}  while(0);
 
294
 
 
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))); \
 
298
}  while(0);
 
299
 
 
300
/* To speed up the decoding, we assume that the reservoir have enough bit
 
301
 * slow version:
 
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); \
 
306
 * }  while(0);
 
307
 */
 
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); \
 
311
}  while(0);
 
312
 
 
313
#define be16_to_cpu(x) (((x)[0]<<8)|(x)[1])
 
314
 
 
315
static void resync(struct jdec_private *priv);
 
316
 
 
317
/**
 
318
 * Get the next (valid) huffman code in the stream.
 
319
 *
 
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.
 
325
 *
 
326
 * If the code is not present for any reason, -1 is return.
 
327
 */
 
328
static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table)
 
329
{
 
330
  int value, hcode;
 
331
  unsigned int extra_nbits, nbits;
 
332
  uint16_t *slowtable;
 
333
 
 
334
  look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
 
335
  value = huffman_table->lookup[hcode];
 
336
  if (value >= 0)
 
337
  {
 
338
     unsigned int code_size = huffman_table->code_size[value];
 
339
     skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
 
340
     return value;
 
341
  }
 
342
 
 
343
  /* Decode more bits each time ... */
 
344
  for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++)
 
345
   {
 
346
     nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
 
347
 
 
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);
 
354
           return slowtable[1];
 
355
        }
 
356
        slowtable+=2;
 
357
     }
 
358
   }
 
359
  snprintf(priv->error_string, sizeof(priv->error_string),
 
360
    "unknown huffman code: %08x\n", (unsigned int)hcode);
 
361
  longjmp(priv->jump_state, -EIO);
 
362
  return 0;
 
363
}
 
364
 
 
365
/**
 
366
 *
 
367
 * Decode a single block that contains the DCT coefficients.
 
368
 * The table coefficients is already dezigzaged at the end of the operation.
 
369
 *
 
370
 */
 
371
static void process_Huffman_data_unit(struct jdec_private *priv, int component)
 
372
{
 
373
  unsigned char j;
 
374
  unsigned int huff_code;
 
375
  unsigned char size_val, count_0;
 
376
 
 
377
  struct component *c = &priv->component_infos[component];
 
378
  short int DCT[64];
 
379
 
 
380
  /* Initialize the DCT coef table */
 
381
  memset(DCT, 0, sizeof(DCT));
 
382
 
 
383
  /* DC coefficient decoding */
 
384
  huff_code = get_next_huffman_code(priv, c->DC_table);
 
385
  if (huff_code) {
 
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];
 
389
  } else {
 
390
     DCT[0] = c->previous_DC;
 
391
  }
 
392
 
 
393
 
 
394
  /* AC coefficient decoding */
 
395
  j = 1;
 
396
  while (j<64)
 
397
   {
 
398
     huff_code = get_next_huffman_code(priv, c->AC_table);
 
399
 
 
400
     size_val = huff_code & 0xF;
 
401
     count_0 = huff_code >> 4;
 
402
 
 
403
     if (size_val == 0)
 
404
      { /* RLE */
 
405
        if (count_0 == 0)
 
406
          break;        /* EOB found, go out */
 
407
        else if (count_0 == 0xF)
 
408
          j += 16;      /* skip 16 zeros */
 
409
      }
 
410
     else
 
411
      {
 
412
        j += count_0;   /* skip count_0 zeroes */
 
413
        if (j < 64) {
 
414
          get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
 
415
          j++;
 
416
        }
 
417
      }
 
418
   }
 
419
 
 
420
  if (j > 64) {
 
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);
 
424
  }
 
425
 
 
426
  for (j = 0; j < 64; j++)
 
427
    c->DCT[j] = DCT[zigzag[j]];
 
428
}
 
429
 
 
430
/*
 
431
 * Takes two array of bits, and build the huffman table for size, and code
 
432
 *
 
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.
 
436
 */
 
437
static int build_huffman_table(struct jdec_private *priv, const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
 
438
{
 
439
  unsigned int i, j, code, code_size, val, nbits;
 
440
  unsigned char huffsize[257], *hz;
 
441
  unsigned int huffcode[257], *hc;
 
442
  int next_free_entry;
 
443
  int slowtable_used[16-HUFFMAN_HASH_NBITS];
 
444
 
 
445
  /*
 
446
   * Build a temp array
 
447
   *   huffsize[X] => numbers of bits to write vals[X]
 
448
   */
 
449
  hz = huffsize;
 
450
  for (i=1; i<=16; i++)
 
451
   {
 
452
     for (j=1; j<=bits[i]; j++)
 
453
       *hz++ = i;
 
454
   }
 
455
  *hz = 0;
 
456
 
 
457
  memset(table->lookup, 0xff, sizeof(table->lookup));
 
458
  for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
 
459
    slowtable_used[i] = 0;
 
460
 
 
461
  /* Build a temp array
 
462
   *   huffcode[X] => code used to write vals[X]
 
463
   */
 
464
  code = 0;
 
465
  hc = huffcode;
 
466
  hz = huffsize;
 
467
  nbits = *hz;
 
468
  while (*hz)
 
469
   {
 
470
     while (*hz == nbits) {
 
471
        *hc++ = code++;
 
472
        hz++;
 
473
     }
 
474
     code <<= 1;
 
475
     nbits++;
 
476
   }
 
477
 
 
478
  /*
 
479
   * Build the lookup table, and the slowtable if needed.
 
480
   */
 
481
  next_free_entry = -1;
 
482
  for (i=0; huffsize[i]; i++)
 
483
   {
 
484
     val = vals[i];
 
485
     code = huffcode[i];
 
486
     code_size = huffsize[i];
 
487
 
 
488
     trace("val=%2.2x code=%8.8x codesize=%2.2d\n", i, code, code_size);
 
489
 
 
490
     table->code_size[val] = code_size;
 
491
     if (code_size <= HUFFMAN_HASH_NBITS)
 
492
      {
 
493
        /*
 
494
         * Good: val can be put in the lookup table, so fill all value of this
 
495
         * column with value val
 
496
         */
 
497
        int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
 
498
        code <<= HUFFMAN_HASH_NBITS - code_size;
 
499
        while ( repeat-- )
 
500
          table->lookup[code++] = val;
 
501
 
 
502
      }
 
503
     else
 
504
      {
 
505
        /* Perhaps sorting the array will be an optimization */
 
506
        int slowtable_index = code_size-HUFFMAN_HASH_NBITS-1;
 
507
 
 
508
        if (slowtable_used[slowtable_index] == 254)
 
509
          error("slow Huffman table overflow\n");
 
510
 
 
511
        table->slowtable[slowtable_index][slowtable_used[slowtable_index]]
 
512
          = code;
 
513
        table->slowtable[slowtable_index][slowtable_used[slowtable_index] + 1]
 
514
          = val;
 
515
        slowtable_used[slowtable_index] += 2;
 
516
      }
 
517
   }
 
518
 
 
519
   for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
 
520
     table->slowtable[i][slowtable_used[i]] = 0;
 
521
 
 
522
   return 0;
 
523
}
 
524
 
 
525
static int build_default_huffman_tables(struct jdec_private *priv)
 
526
{
 
527
  if (   (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
 
528
      && priv->default_huffman_table_initialized)
 
529
    return 0;
 
530
 
 
531
  if (build_huffman_table(priv, bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]))
 
532
    return -1;
 
533
  if (build_huffman_table(priv, bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]))
 
534
    return -1;
 
535
 
 
536
  if (build_huffman_table(priv, bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]))
 
537
    return -1;
 
538
  if (build_huffman_table(priv, bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]))
 
539
    return -1;
 
540
 
 
541
  priv->default_huffman_table_initialized = 1;
 
542
  return 0;
 
543
}
 
544
 
 
545
 
 
546
 
 
547
/*******************************************************************************
 
548
 *
 
549
 * Colorspace conversion routine
 
550
 *
 
551
 *
 
552
 * Note:
 
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
 
559
 *
 
560
 ******************************************************************************/
 
561
 
 
562
static unsigned char clamp(int i)
 
563
{
 
564
  if (i<0)
 
565
    return 0;
 
566
  else if (i>255)
 
567
    return 255;
 
568
  else
 
569
    return i;
 
570
}
 
571
 
 
572
 
 
573
/**
 
574
 *  YCrCb -> YUV420P (1x1)
 
575
 *  .---.
 
576
 *  | 1 |
 
577
 *  `---'
 
578
 */
 
579
static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
 
580
{
 
581
  const unsigned char *s, *y;
 
582
  unsigned char *p;
 
583
  int i,j;
 
584
 
 
585
  p = priv->plane[0];
 
586
  y = priv->Y;
 
587
  for (i=0; i<8; i++)
 
588
   {
 
589
     memcpy(p, y, 8);
 
590
     p+=priv->width;
 
591
     y+=8;
 
592
   }
 
593
 
 
594
  p = priv->plane[1];
 
595
  s = priv->Cb;
 
596
  for (i=0; i<8; i+=2)
 
597
   {
 
598
     for (j=0; j<8; j+=2, s+=2)
 
599
       *p++ = *s;
 
600
     s += 8; /* Skip one line */
 
601
     p += priv->width/2 - 4;
 
602
   }
 
603
 
 
604
  p = priv->plane[2];
 
605
  s = priv->Cr;
 
606
  for (i=0; i<8; i+=2)
 
607
   {
 
608
     for (j=0; j<8; j+=2, s+=2)
 
609
       *p++ = *s;
 
610
     s += 8; /* Skip one line */
 
611
     p += priv->width/2 - 4;
 
612
   }
 
613
}
 
614
 
 
615
/**
 
616
 *  YCrCb -> YUV420P (2x1)
 
617
 *  .-------.
 
618
 *  | 1 | 2 |
 
619
 *  `-------'
 
620
 */
 
621
static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
 
622
{
 
623
  unsigned char *p;
 
624
  const unsigned char *s, *y1;
 
625
  unsigned int i;
 
626
 
 
627
  p = priv->plane[0];
 
628
  y1 = priv->Y;
 
629
  for (i=0; i<8; i++)
 
630
   {
 
631
     memcpy(p, y1, 16);
 
632
     p += priv->width;
 
633
     y1 += 16;
 
634
   }
 
635
 
 
636
  p = priv->plane[1];
 
637
  s = priv->Cb;
 
638
  for (i=0; i<8; i+=2)
 
639
   {
 
640
     memcpy(p, s, 8);
 
641
     s += 16; /* Skip one line */
 
642
     p += priv->width/2;
 
643
   }
 
644
 
 
645
  p = priv->plane[2];
 
646
  s = priv->Cr;
 
647
  for (i=0; i<8; i+=2)
 
648
   {
 
649
     memcpy(p, s, 8);
 
650
     s += 16; /* Skip one line */
 
651
     p += priv->width/2;
 
652
   }
 
653
}
 
654
 
 
655
 
 
656
/**
 
657
 *  YCrCb -> YUV420P (1x2)
 
658
 *  .---.
 
659
 *  | 1 |
 
660
 *  |---|
 
661
 *  | 2 |
 
662
 *  `---'
 
663
 */
 
664
static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
 
665
{
 
666
  const unsigned char *s, *y;
 
667
  unsigned char *p;
 
668
  int i,j;
 
669
 
 
670
  p = priv->plane[0];
 
671
  y = priv->Y;
 
672
  for (i=0; i<16; i++)
 
673
   {
 
674
     memcpy(p, y, 8);
 
675
     p+=priv->width;
 
676
     y+=8;
 
677
   }
 
678
 
 
679
  p = priv->plane[1];
 
680
  s = priv->Cb;
 
681
  for (i=0; i<8; i++)
 
682
   {
 
683
     for (j=0; j<8; j+=2, s+=2)
 
684
       *p++ = *s;
 
685
     p += priv->width/2 - 4;
 
686
   }
 
687
 
 
688
  p = priv->plane[2];
 
689
  s = priv->Cr;
 
690
  for (i=0; i<8; i++)
 
691
   {
 
692
     for (j=0; j<8; j+=2, s+=2)
 
693
       *p++ = *s;
 
694
     p += priv->width/2 - 4;
 
695
   }
 
696
}
 
697
 
 
698
/**
 
699
 *  YCrCb -> YUV420P (2x2)
 
700
 *  .-------.
 
701
 *  | 1 | 2 |
 
702
 *  |---+---|
 
703
 *  | 3 | 4 |
 
704
 *  `-------'
 
705
 */
 
706
static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
 
707
{
 
708
  unsigned char *p;
 
709
  const unsigned char *s, *y1;
 
710
  unsigned int i;
 
711
 
 
712
  p = priv->plane[0];
 
713
  y1 = priv->Y;
 
714
  for (i=0; i<16; i++)
 
715
   {
 
716
     memcpy(p, y1, 16);
 
717
     p += priv->width;
 
718
     y1 += 16;
 
719
   }
 
720
 
 
721
  p = priv->plane[1];
 
722
  s = priv->Cb;
 
723
  for (i=0; i<8; i++)
 
724
   {
 
725
     memcpy(p, s, 8);
 
726
     s += 8;
 
727
     p += priv->width/2;
 
728
   }
 
729
 
 
730
  p = priv->plane[2];
 
731
  s = priv->Cr;
 
732
  for (i=0; i<8; i++)
 
733
   {
 
734
     memcpy(p, s, 8);
 
735
     s += 8;
 
736
     p += priv->width/2;
 
737
   }
 
738
}
 
739
 
 
740
/**
 
741
 *  YCrCb -> RGB24 (1x1)
 
742
 *  .---.
 
743
 *  | 1 |
 
744
 *  `---'
 
745
 */
 
746
static void YCrCB_to_RGB24_1x1(struct jdec_private *priv)
 
747
{
 
748
  const unsigned char *Y, *Cb, *Cr;
 
749
  unsigned char *p;
 
750
  int i,j;
 
751
  int offset_to_next_row;
 
752
 
 
753
#define SCALEBITS       10
 
754
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
755
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
756
 
 
757
  p = priv->plane[0];
 
758
  Y = priv->Y;
 
759
  Cb = priv->Cb;
 
760
  Cr = priv->Cr;
 
761
  offset_to_next_row = priv->width*3 - 8*3;
 
762
  for (i=0; i<8; i++) {
 
763
 
 
764
    for (j=0; j<8; j++) {
 
765
 
 
766
       int y, cb, cr;
 
767
       int add_r, add_g, add_b;
 
768
       int r, g , b;
 
769
 
 
770
       y  = (*Y++) << SCALEBITS;
 
771
       cb = *Cb++ - 128;
 
772
       cr = *Cr++ - 128;
 
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;
 
776
 
 
777
       r = (y + add_r) >> SCALEBITS;
 
778
       *p++ = clamp(r);
 
779
       g = (y + add_g) >> SCALEBITS;
 
780
       *p++ = clamp(g);
 
781
       b = (y + add_b) >> SCALEBITS;
 
782
       *p++ = clamp(b);
 
783
 
 
784
    }
 
785
 
 
786
    p += offset_to_next_row;
 
787
  }
 
788
 
 
789
#undef SCALEBITS
 
790
#undef ONE_HALF
 
791
#undef FIX
 
792
 
 
793
}
 
794
 
 
795
/**
 
796
 *  YCrCb -> BGR24 (1x1)
 
797
 *  .---.
 
798
 *  | 1 |
 
799
 *  `---'
 
800
 */
 
801
static void YCrCB_to_BGR24_1x1(struct jdec_private *priv)
 
802
{
 
803
  const unsigned char *Y, *Cb, *Cr;
 
804
  unsigned char *p;
 
805
  int i,j;
 
806
  int offset_to_next_row;
 
807
 
 
808
#define SCALEBITS       10
 
809
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
810
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
811
 
 
812
  p = priv->plane[0];
 
813
  Y = priv->Y;
 
814
  Cb = priv->Cb;
 
815
  Cr = priv->Cr;
 
816
  offset_to_next_row = priv->width*3 - 8*3;
 
817
  for (i=0; i<8; i++) {
 
818
 
 
819
    for (j=0; j<8; j++) {
 
820
 
 
821
       int y, cb, cr;
 
822
       int add_r, add_g, add_b;
 
823
       int r, g , b;
 
824
 
 
825
       y  = (*Y++) << SCALEBITS;
 
826
       cb = *Cb++ - 128;
 
827
       cr = *Cr++ - 128;
 
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;
 
831
 
 
832
       b = (y + add_b) >> SCALEBITS;
 
833
       *p++ = clamp(b);
 
834
       g = (y + add_g) >> SCALEBITS;
 
835
       *p++ = clamp(g);
 
836
       r = (y + add_r) >> SCALEBITS;
 
837
       *p++ = clamp(r);
 
838
 
 
839
    }
 
840
 
 
841
    p += offset_to_next_row;
 
842
  }
 
843
 
 
844
#undef SCALEBITS
 
845
#undef ONE_HALF
 
846
#undef FIX
 
847
 
 
848
}
 
849
 
 
850
 
 
851
/**
 
852
 *  YCrCb -> RGB24 (2x1)
 
853
 *  .-------.
 
854
 *  | 1 | 2 |
 
855
 *  `-------'
 
856
 */
 
857
static void YCrCB_to_RGB24_2x1(struct jdec_private *priv)
 
858
{
 
859
  const unsigned char *Y, *Cb, *Cr;
 
860
  unsigned char *p;
 
861
  int i,j;
 
862
  int offset_to_next_row;
 
863
 
 
864
#define SCALEBITS       10
 
865
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
866
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
867
 
 
868
  p = priv->plane[0];
 
869
  Y = priv->Y;
 
870
  Cb = priv->Cb;
 
871
  Cr = priv->Cr;
 
872
  offset_to_next_row = priv->width*3 - 16*3;
 
873
  for (i=0; i<8; i++) {
 
874
 
 
875
    for (j=0; j<8; j++) {
 
876
 
 
877
       int y, cb, cr;
 
878
       int add_r, add_g, add_b;
 
879
       int r, g , b;
 
880
 
 
881
       y  = (*Y++) << SCALEBITS;
 
882
       cb = *Cb++ - 128;
 
883
       cr = *Cr++ - 128;
 
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;
 
887
 
 
888
       r = (y + add_r) >> SCALEBITS;
 
889
       *p++ = clamp(r);
 
890
       g = (y + add_g) >> SCALEBITS;
 
891
       *p++ = clamp(g);
 
892
       b = (y + add_b) >> SCALEBITS;
 
893
       *p++ = clamp(b);
 
894
 
 
895
       y  = (*Y++) << SCALEBITS;
 
896
       r = (y + add_r) >> SCALEBITS;
 
897
       *p++ = clamp(r);
 
898
       g = (y + add_g) >> SCALEBITS;
 
899
       *p++ = clamp(g);
 
900
       b = (y + add_b) >> SCALEBITS;
 
901
       *p++ = clamp(b);
 
902
 
 
903
    }
 
904
 
 
905
    p += offset_to_next_row;
 
906
  }
 
907
 
 
908
#undef SCALEBITS
 
909
#undef ONE_HALF
 
910
#undef FIX
 
911
 
 
912
}
 
913
 
 
914
/*
 
915
 *  YCrCb -> BGR24 (2x1)
 
916
 *  .-------.
 
917
 *  | 1 | 2 |
 
918
 *  `-------'
 
919
 */
 
920
static void YCrCB_to_BGR24_2x1(struct jdec_private *priv)
 
921
{
 
922
  const unsigned char *Y, *Cb, *Cr;
 
923
  unsigned char *p;
 
924
  int i,j;
 
925
  int offset_to_next_row;
 
926
 
 
927
#define SCALEBITS       10
 
928
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
929
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
930
 
 
931
  p = priv->plane[0];
 
932
  Y = priv->Y;
 
933
  Cb = priv->Cb;
 
934
  Cr = priv->Cr;
 
935
  offset_to_next_row = priv->width*3 - 16*3;
 
936
  for (i=0; i<8; i++) {
 
937
 
 
938
    for (j=0; j<8; j++) {
 
939
 
 
940
       int y, cb, cr;
 
941
       int add_r, add_g, add_b;
 
942
       int r, g , b;
 
943
 
 
944
       cb = *Cb++ - 128;
 
945
       cr = *Cr++ - 128;
 
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;
 
949
 
 
950
       y  = (*Y++) << SCALEBITS;
 
951
       b = (y + add_b) >> SCALEBITS;
 
952
       *p++ = clamp(b);
 
953
       g = (y + add_g) >> SCALEBITS;
 
954
       *p++ = clamp(g);
 
955
       r = (y + add_r) >> SCALEBITS;
 
956
       *p++ = clamp(r);
 
957
 
 
958
       y  = (*Y++) << SCALEBITS;
 
959
       b = (y + add_b) >> SCALEBITS;
 
960
       *p++ = clamp(b);
 
961
       g = (y + add_g) >> SCALEBITS;
 
962
       *p++ = clamp(g);
 
963
       r = (y + add_r) >> SCALEBITS;
 
964
       *p++ = clamp(r);
 
965
 
 
966
    }
 
967
 
 
968
    p += offset_to_next_row;
 
969
  }
 
970
 
 
971
#undef SCALEBITS
 
972
#undef ONE_HALF
 
973
#undef FIX
 
974
 
 
975
}
 
976
 
 
977
/**
 
978
 *  YCrCb -> RGB24 (1x2)
 
979
 *  .---.
 
980
 *  | 1 |
 
981
 *  |---|
 
982
 *  | 2 |
 
983
 *  `---'
 
984
 */
 
985
static void YCrCB_to_RGB24_1x2(struct jdec_private *priv)
 
986
{
 
987
  const unsigned char *Y, *Cb, *Cr;
 
988
  unsigned char *p, *p2;
 
989
  int i,j;
 
990
  int offset_to_next_row;
 
991
 
 
992
#define SCALEBITS       10
 
993
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
994
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
995
 
 
996
  p = priv->plane[0];
 
997
  p2 = priv->plane[0] + priv->width*3;
 
998
  Y = priv->Y;
 
999
  Cb = priv->Cb;
 
1000
  Cr = priv->Cr;
 
1001
  offset_to_next_row = 2*priv->width*3 - 8*3;
 
1002
  for (i=0; i<8; i++) {
 
1003
 
 
1004
    for (j=0; j<8; j++) {
 
1005
 
 
1006
       int y, cb, cr;
 
1007
       int add_r, add_g, add_b;
 
1008
       int r, g , b;
 
1009
 
 
1010
       cb = *Cb++ - 128;
 
1011
       cr = *Cr++ - 128;
 
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;
 
1015
 
 
1016
       y  = (*Y++) << SCALEBITS;
 
1017
       r = (y + add_r) >> SCALEBITS;
 
1018
       *p++ = clamp(r);
 
1019
       g = (y + add_g) >> SCALEBITS;
 
1020
       *p++ = clamp(g);
 
1021
       b = (y + add_b) >> SCALEBITS;
 
1022
       *p++ = clamp(b);
 
1023
 
 
1024
       y  = (Y[8-1]) << SCALEBITS;
 
1025
       r = (y + add_r) >> SCALEBITS;
 
1026
       *p2++ = clamp(r);
 
1027
       g = (y + add_g) >> SCALEBITS;
 
1028
       *p2++ = clamp(g);
 
1029
       b = (y + add_b) >> SCALEBITS;
 
1030
       *p2++ = clamp(b);
 
1031
 
 
1032
    }
 
1033
    Y += 8;
 
1034
    p += offset_to_next_row;
 
1035
    p2 += offset_to_next_row;
 
1036
  }
 
1037
 
 
1038
#undef SCALEBITS
 
1039
#undef ONE_HALF
 
1040
#undef FIX
 
1041
 
 
1042
}
 
1043
 
 
1044
/*
 
1045
 *  YCrCb -> BGR24 (1x2)
 
1046
 *  .---.
 
1047
 *  | 1 |
 
1048
 *  |---|
 
1049
 *  | 2 |
 
1050
 *  `---'
 
1051
 */
 
1052
static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
 
1053
{
 
1054
  const unsigned char *Y, *Cb, *Cr;
 
1055
  unsigned char *p, *p2;
 
1056
  int i,j;
 
1057
  int offset_to_next_row;
 
1058
 
 
1059
#define SCALEBITS       10
 
1060
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
1061
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
1062
 
 
1063
  p = priv->plane[0];
 
1064
  p2 = priv->plane[0] + priv->width*3;
 
1065
  Y = priv->Y;
 
1066
  Cb = priv->Cb;
 
1067
  Cr = priv->Cr;
 
1068
  offset_to_next_row = 2*priv->width*3 - 8*3;
 
1069
  for (i=0; i<8; i++) {
 
1070
 
 
1071
    for (j=0; j<8; j++) {
 
1072
 
 
1073
       int y, cb, cr;
 
1074
       int add_r, add_g, add_b;
 
1075
       int r, g , b;
 
1076
 
 
1077
       cb = *Cb++ - 128;
 
1078
       cr = *Cr++ - 128;
 
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;
 
1082
 
 
1083
       y  = (*Y++) << SCALEBITS;
 
1084
       b = (y + add_b) >> SCALEBITS;
 
1085
       *p++ = clamp(b);
 
1086
       g = (y + add_g) >> SCALEBITS;
 
1087
       *p++ = clamp(g);
 
1088
       r = (y + add_r) >> SCALEBITS;
 
1089
       *p++ = clamp(r);
 
1090
 
 
1091
       y  = (Y[8-1]) << SCALEBITS;
 
1092
       b = (y + add_b) >> SCALEBITS;
 
1093
       *p2++ = clamp(b);
 
1094
       g = (y + add_g) >> SCALEBITS;
 
1095
       *p2++ = clamp(g);
 
1096
       r = (y + add_r) >> SCALEBITS;
 
1097
       *p2++ = clamp(r);
 
1098
 
 
1099
    }
 
1100
    Y += 8;
 
1101
    p += offset_to_next_row;
 
1102
    p2 += offset_to_next_row;
 
1103
  }
 
1104
 
 
1105
#undef SCALEBITS
 
1106
#undef ONE_HALF
 
1107
#undef FIX
 
1108
 
 
1109
}
 
1110
 
 
1111
 
 
1112
/**
 
1113
 *  YCrCb -> RGB24 (2x2)
 
1114
 *  .-------.
 
1115
 *  | 1 | 2 |
 
1116
 *  |---+---|
 
1117
 *  | 3 | 4 |
 
1118
 *  `-------'
 
1119
 */
 
1120
static void YCrCB_to_RGB24_2x2(struct jdec_private *priv)
 
1121
{
 
1122
  const unsigned char *Y, *Cb, *Cr;
 
1123
  unsigned char *p, *p2;
 
1124
  int i,j;
 
1125
  int offset_to_next_row;
 
1126
 
 
1127
#define SCALEBITS       10
 
1128
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
1129
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
1130
 
 
1131
  p = priv->plane[0];
 
1132
  p2 = priv->plane[0] + priv->width*3;
 
1133
  Y = priv->Y;
 
1134
  Cb = priv->Cb;
 
1135
  Cr = priv->Cr;
 
1136
  offset_to_next_row = (priv->width*3*2) - 16*3;
 
1137
  for (i=0; i<8; i++) {
 
1138
 
 
1139
    for (j=0; j<8; j++) {
 
1140
 
 
1141
       int y, cb, cr;
 
1142
       int add_r, add_g, add_b;
 
1143
       int r, g , b;
 
1144
 
 
1145
       cb = *Cb++ - 128;
 
1146
       cr = *Cr++ - 128;
 
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;
 
1150
 
 
1151
       y  = (*Y++) << SCALEBITS;
 
1152
       r = (y + add_r) >> SCALEBITS;
 
1153
       *p++ = clamp(r);
 
1154
       g = (y + add_g) >> SCALEBITS;
 
1155
       *p++ = clamp(g);
 
1156
       b = (y + add_b) >> SCALEBITS;
 
1157
       *p++ = clamp(b);
 
1158
 
 
1159
       y  = (*Y++) << SCALEBITS;
 
1160
       r = (y + add_r) >> SCALEBITS;
 
1161
       *p++ = clamp(r);
 
1162
       g = (y + add_g) >> SCALEBITS;
 
1163
       *p++ = clamp(g);
 
1164
       b = (y + add_b) >> SCALEBITS;
 
1165
       *p++ = clamp(b);
 
1166
 
 
1167
       y  = (Y[16-2]) << SCALEBITS;
 
1168
       r = (y + add_r) >> SCALEBITS;
 
1169
       *p2++ = clamp(r);
 
1170
       g = (y + add_g) >> SCALEBITS;
 
1171
       *p2++ = clamp(g);
 
1172
       b = (y + add_b) >> SCALEBITS;
 
1173
       *p2++ = clamp(b);
 
1174
 
 
1175
       y  = (Y[16-1]) << SCALEBITS;
 
1176
       r = (y + add_r) >> SCALEBITS;
 
1177
       *p2++ = clamp(r);
 
1178
       g = (y + add_g) >> SCALEBITS;
 
1179
       *p2++ = clamp(g);
 
1180
       b = (y + add_b) >> SCALEBITS;
 
1181
       *p2++ = clamp(b);
 
1182
    }
 
1183
    Y  += 16;
 
1184
    p  += offset_to_next_row;
 
1185
    p2 += offset_to_next_row;
 
1186
  }
 
1187
 
 
1188
#undef SCALEBITS
 
1189
#undef ONE_HALF
 
1190
#undef FIX
 
1191
 
 
1192
}
 
1193
 
 
1194
 
 
1195
/*
 
1196
 *  YCrCb -> BGR24 (2x2)
 
1197
 *  .-------.
 
1198
 *  | 1 | 2 |
 
1199
 *  |---+---|
 
1200
 *  | 3 | 4 |
 
1201
 *  `-------'
 
1202
 */
 
1203
static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
 
1204
{
 
1205
  const unsigned char *Y, *Cb, *Cr;
 
1206
  unsigned char *p, *p2;
 
1207
  int i,j;
 
1208
  int offset_to_next_row;
 
1209
 
 
1210
#define SCALEBITS       10
 
1211
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
1212
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
1213
 
 
1214
  p = priv->plane[0];
 
1215
  p2 = priv->plane[0] + priv->width*3;
 
1216
  Y = priv->Y;
 
1217
  Cb = priv->Cb;
 
1218
  Cr = priv->Cr;
 
1219
  offset_to_next_row = (priv->width*3*2) - 16*3;
 
1220
  for (i=0; i<8; i++) {
 
1221
 
 
1222
    for (j=0; j<8; j++) {
 
1223
 
 
1224
       int y, cb, cr;
 
1225
       int add_r, add_g, add_b;
 
1226
       int r, g , b;
 
1227
 
 
1228
       cb = *Cb++ - 128;
 
1229
       cr = *Cr++ - 128;
 
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;
 
1233
 
 
1234
       y  = (*Y++) << SCALEBITS;
 
1235
       b = (y + add_b) >> SCALEBITS;
 
1236
       *p++ = clamp(b);
 
1237
       g = (y + add_g) >> SCALEBITS;
 
1238
       *p++ = clamp(g);
 
1239
       r = (y + add_r) >> SCALEBITS;
 
1240
       *p++ = clamp(r);
 
1241
 
 
1242
       y  = (*Y++) << SCALEBITS;
 
1243
       b = (y + add_b) >> SCALEBITS;
 
1244
       *p++ = clamp(b);
 
1245
       g = (y + add_g) >> SCALEBITS;
 
1246
       *p++ = clamp(g);
 
1247
       r = (y + add_r) >> SCALEBITS;
 
1248
       *p++ = clamp(r);
 
1249
 
 
1250
       y  = (Y[16-2]) << SCALEBITS;
 
1251
       b = (y + add_b) >> SCALEBITS;
 
1252
       *p2++ = clamp(b);
 
1253
       g = (y + add_g) >> SCALEBITS;
 
1254
       *p2++ = clamp(g);
 
1255
       r = (y + add_r) >> SCALEBITS;
 
1256
       *p2++ = clamp(r);
 
1257
 
 
1258
       y  = (Y[16-1]) << SCALEBITS;
 
1259
       b = (y + add_b) >> SCALEBITS;
 
1260
       *p2++ = clamp(b);
 
1261
       g = (y + add_g) >> SCALEBITS;
 
1262
       *p2++ = clamp(g);
 
1263
       r = (y + add_r) >> SCALEBITS;
 
1264
       *p2++ = clamp(r);
 
1265
    }
 
1266
    Y  += 16;
 
1267
    p  += offset_to_next_row;
 
1268
    p2 += offset_to_next_row;
 
1269
  }
 
1270
 
 
1271
#undef SCALEBITS
 
1272
#undef ONE_HALF
 
1273
#undef FIX
 
1274
 
 
1275
}
 
1276
 
 
1277
 
 
1278
 
 
1279
/**
 
1280
 *  YCrCb -> Grey (1x1)
 
1281
 *  .---.
 
1282
 *  | 1 |
 
1283
 *  `---'
 
1284
 */
 
1285
static void YCrCB_to_Grey_1x1(struct jdec_private *priv)
 
1286
{
 
1287
  const unsigned char *y;
 
1288
  unsigned char *p;
 
1289
  unsigned int i;
 
1290
  int offset_to_next_row;
 
1291
 
 
1292
  p = priv->plane[0];
 
1293
  y = priv->Y;
 
1294
  offset_to_next_row = priv->width;
 
1295
 
 
1296
  for (i=0; i<8; i++) {
 
1297
     memcpy(p, y, 8);
 
1298
     y+=8;
 
1299
     p += offset_to_next_row;
 
1300
  }
 
1301
}
 
1302
 
 
1303
/**
 
1304
 *  YCrCb -> Grey (2x1)
 
1305
 *  .-------.
 
1306
 *  | 1 | 2 |
 
1307
 *  `-------'
 
1308
 */
 
1309
static void YCrCB_to_Grey_2x1(struct jdec_private *priv)
 
1310
{
 
1311
  const unsigned char *y;
 
1312
  unsigned char *p;
 
1313
  unsigned int i;
 
1314
 
 
1315
  p = priv->plane[0];
 
1316
  y = priv->Y;
 
1317
 
 
1318
  for (i=0; i<8; i++) {
 
1319
     memcpy(p, y, 16);
 
1320
     y += 16;
 
1321
     p += priv->width;
 
1322
  }
 
1323
}
 
1324
 
 
1325
 
 
1326
/**
 
1327
 *  YCrCb -> Grey (1x2)
 
1328
 *  .---.
 
1329
 *  | 1 |
 
1330
 *  |---|
 
1331
 *  | 2 |
 
1332
 *  `---'
 
1333
 */
 
1334
static void YCrCB_to_Grey_1x2(struct jdec_private *priv)
 
1335
{
 
1336
  const unsigned char *y;
 
1337
  unsigned char *p;
 
1338
  unsigned int i;
 
1339
 
 
1340
  p = priv->plane[0];
 
1341
  y = priv->Y;
 
1342
 
 
1343
  for (i=0; i<16; i++) {
 
1344
     memcpy(p, y, 8);
 
1345
     y += 8;
 
1346
     p += priv->width;
 
1347
  }
 
1348
}
 
1349
 
 
1350
/**
 
1351
 *  YCrCb -> Grey (2x2)
 
1352
 *  .-------.
 
1353
 *  | 1 | 2 |
 
1354
 *  |---+---|
 
1355
 *  | 3 | 4 |
 
1356
 *  `-------'
 
1357
 */
 
1358
static void YCrCB_to_Grey_2x2(struct jdec_private *priv)
 
1359
{
 
1360
  const unsigned char *y;
 
1361
  unsigned char *p;
 
1362
  unsigned int i;
 
1363
 
 
1364
  p = priv->plane[0];
 
1365
  y = priv->Y;
 
1366
 
 
1367
  for (i=0; i<16; i++) {
 
1368
     memcpy(p, y, 16);
 
1369
     y += 16;
 
1370
     p += priv->width;
 
1371
  }
 
1372
}
 
1373
 
 
1374
 
 
1375
/*
 
1376
 * Decode all the 3 components for 1x1
 
1377
 */
 
1378
static void decode_MCU_1x1_3planes(struct jdec_private *priv)
 
1379
{
 
1380
  // Y
 
1381
  process_Huffman_data_unit(priv, cY);
 
1382
  IDCT(&priv->component_infos[cY], priv->Y, 8);
 
1383
 
 
1384
  // Cb
 
1385
  process_Huffman_data_unit(priv, cCb);
 
1386
  IDCT(&priv->component_infos[cCb], priv->Cb, 8);
 
1387
 
 
1388
  // Cr
 
1389
  process_Huffman_data_unit(priv, cCr);
 
1390
  IDCT(&priv->component_infos[cCr], priv->Cr, 8);
 
1391
}
 
1392
 
 
1393
/*
 
1394
 * Decode a 1x1 directly in 1 color
 
1395
 */
 
1396
static void decode_MCU_1x1_1plane(struct jdec_private *priv)
 
1397
{
 
1398
  // Y
 
1399
  process_Huffman_data_unit(priv, cY);
 
1400
  IDCT(&priv->component_infos[cY], priv->Y, 8);
 
1401
 
 
1402
  // Cb
 
1403
  process_Huffman_data_unit(priv, cCb);
 
1404
  IDCT(&priv->component_infos[cCb], priv->Cb, 8);
 
1405
 
 
1406
  // Cr
 
1407
  process_Huffman_data_unit(priv, cCr);
 
1408
  IDCT(&priv->component_infos[cCr], priv->Cr, 8);
 
1409
}
 
1410
 
 
1411
 
 
1412
/*
 
1413
 * Decode a 2x1
 
1414
 *  .-------.
 
1415
 *  | 1 | 2 |
 
1416
 *  `-------'
 
1417
 */
 
1418
static void decode_MCU_2x1_3planes(struct jdec_private *priv)
 
1419
{
 
1420
  // Y
 
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);
 
1425
 
 
1426
  // Cb
 
1427
  process_Huffman_data_unit(priv, cCb);
 
1428
  IDCT(&priv->component_infos[cCb], priv->Cb, 8);
 
1429
 
 
1430
  // Cr
 
1431
  process_Huffman_data_unit(priv, cCr);
 
1432
  IDCT(&priv->component_infos[cCr], priv->Cr, 8);
 
1433
}
 
1434
 
 
1435
static void pixart_decode_MCU_2x1_3planes(struct jdec_private *priv)
 
1436
{
 
1437
  unsigned char marker;
 
1438
 
 
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
 
1448
     decoding garbage */
 
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);
 
1454
  }
 
1455
  skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, 8);
 
1456
 
 
1457
  // Y
 
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);
 
1462
 
 
1463
  // Cb
 
1464
  process_Huffman_data_unit(priv, cCb);
 
1465
  IDCT(&priv->component_infos[cCb], priv->Cb, 8);
 
1466
 
 
1467
  // Cr
 
1468
  process_Huffman_data_unit(priv, cCr);
 
1469
  IDCT(&priv->component_infos[cCr], priv->Cr, 8);
 
1470
}
 
1471
 
 
1472
/*
 
1473
 * Decode a 2x1
 
1474
 *  .-------.
 
1475
 *  | 1 | 2 |
 
1476
 *  `-------'
 
1477
 */
 
1478
static void decode_MCU_2x1_1plane(struct jdec_private *priv)
 
1479
{
 
1480
  // Y
 
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);
 
1485
 
 
1486
  // Cb
 
1487
  process_Huffman_data_unit(priv, cCb);
 
1488
 
 
1489
  // Cr
 
1490
  process_Huffman_data_unit(priv, cCr);
 
1491
}
 
1492
 
 
1493
 
 
1494
/*
 
1495
 * Decode a 2x2
 
1496
 *  .-------.
 
1497
 *  | 1 | 2 |
 
1498
 *  |---+---|
 
1499
 *  | 3 | 4 |
 
1500
 *  `-------'
 
1501
 */
 
1502
static void decode_MCU_2x2_3planes(struct jdec_private *priv)
 
1503
{
 
1504
  // Y
 
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);
 
1513
 
 
1514
  // Cb
 
1515
  process_Huffman_data_unit(priv, cCb);
 
1516
  IDCT(&priv->component_infos[cCb], priv->Cb, 8);
 
1517
 
 
1518
  // Cr
 
1519
  process_Huffman_data_unit(priv, cCr);
 
1520
  IDCT(&priv->component_infos[cCr], priv->Cr, 8);
 
1521
}
 
1522
 
 
1523
/*
 
1524
 * Decode a 2x2 directly in GREY format (8bits)
 
1525
 *  .-------.
 
1526
 *  | 1 | 2 |
 
1527
 *  |---+---|
 
1528
 *  | 3 | 4 |
 
1529
 *  `-------'
 
1530
 */
 
1531
static void decode_MCU_2x2_1plane(struct jdec_private *priv)
 
1532
{
 
1533
  // Y
 
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);
 
1542
 
 
1543
  // Cb
 
1544
  process_Huffman_data_unit(priv, cCb);
 
1545
 
 
1546
  // Cr
 
1547
  process_Huffman_data_unit(priv, cCr);
 
1548
}
 
1549
 
 
1550
/*
 
1551
 * Decode a 1x2 mcu
 
1552
 *  .---.
 
1553
 *  | 1 |
 
1554
 *  |---|
 
1555
 *  | 2 |
 
1556
 *  `---'
 
1557
 */
 
1558
static void decode_MCU_1x2_3planes(struct jdec_private *priv)
 
1559
{
 
1560
  // Y
 
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);
 
1565
 
 
1566
  // Cb
 
1567
  process_Huffman_data_unit(priv, cCb);
 
1568
  IDCT(&priv->component_infos[cCb], priv->Cb, 8);
 
1569
 
 
1570
  // Cr
 
1571
  process_Huffman_data_unit(priv, cCr);
 
1572
  IDCT(&priv->component_infos[cCr], priv->Cr, 8);
 
1573
}
 
1574
 
 
1575
/*
 
1576
 * Decode a 1x2 mcu
 
1577
 *  .---.
 
1578
 *  | 1 |
 
1579
 *  |---|
 
1580
 *  | 2 |
 
1581
 *  `---'
 
1582
 */
 
1583
static void decode_MCU_1x2_1plane(struct jdec_private *priv)
 
1584
{
 
1585
  // Y
 
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);
 
1590
 
 
1591
  // Cb
 
1592
  process_Huffman_data_unit(priv, cCb);
 
1593
 
 
1594
  // Cr
 
1595
  process_Huffman_data_unit(priv, cCr);
 
1596
}
 
1597
 
 
1598
static void print_SOF(const unsigned char *stream)
 
1599
{
 
1600
  int width, height, nr_components, precision;
 
1601
#if DEBUG
 
1602
  const char *nr_components_to_string[] = {
 
1603
     "????",
 
1604
     "Grayscale",
 
1605
     "????",
 
1606
     "YCbCr",
 
1607
     "CYMK"
 
1608
  };
 
1609
#endif
 
1610
 
 
1611
  precision = stream[2];
 
1612
  height = be16_to_cpu(stream+3);
 
1613
  width  = be16_to_cpu(stream+5);
 
1614
  nr_components = stream[7];
 
1615
 
 
1616
  trace("> SOF marker\n");
 
1617
  trace("Size:%dx%d nr_components:%d (%s)  precision:%d\n",
 
1618
      width, height,
 
1619
      nr_components, nr_components_to_string[nr_components],
 
1620
      precision);
 
1621
}
 
1622
 
 
1623
/*******************************************************************************
 
1624
 *
 
1625
 * JPEG/JFIF Parsing functions
 
1626
 *
 
1627
 * Note: only a small subset of the jpeg file format is supported. No markers,
 
1628
 * nor progressive stream is supported.
 
1629
 *
 
1630
 ******************************************************************************/
 
1631
 
 
1632
static void build_quantization_table(float *qtable, const unsigned char *ref_table)
 
1633
{
 
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.
 
1642
   */
 
1643
  int i, j;
 
1644
  static const double aanscalefactor[8] = {
 
1645
     1.0, 1.387039845, 1.306562965, 1.175875602,
 
1646
     1.0, 0.785694958, 0.541196100, 0.275899379
 
1647
  };
 
1648
  const unsigned char *zz = zigzag;
 
1649
 
 
1650
  for (i=0; i<8; i++) {
 
1651
     for (j=0; j<8; j++) {
 
1652
       *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
 
1653
     }
 
1654
   }
 
1655
 
 
1656
}
 
1657
 
 
1658
static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
 
1659
{
 
1660
  int qi;
 
1661
  float *table;
 
1662
  const unsigned char *dqt_block_end;
 
1663
 
 
1664
  trace("> DQT marker\n");
 
1665
  dqt_block_end = stream + be16_to_cpu(stream);
 
1666
  stream += 2;  /* Skip length */
 
1667
 
 
1668
  while (stream < dqt_block_end)
 
1669
   {
 
1670
     qi = *stream++;
 
1671
#if SANITY_CHECK
 
1672
     if (qi>>4)
 
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);
 
1677
#endif
 
1678
     table = priv->Q_tables[qi];
 
1679
     build_quantization_table(table, stream);
 
1680
     stream += 64;
 
1681
   }
 
1682
  trace("< DQT marker\n");
 
1683
  return 0;
 
1684
}
 
1685
 
 
1686
static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
 
1687
{
 
1688
  int i, width, height, nr_components, cid, sampling_factor;
 
1689
  int Q_table;
 
1690
  struct component *c;
 
1691
 
 
1692
  trace("> SOF marker\n");
 
1693
  print_SOF(stream);
 
1694
 
 
1695
  height = be16_to_cpu(stream+3);
 
1696
  width  = be16_to_cpu(stream+5);
 
1697
  nr_components = stream[7];
 
1698
#if SANITY_CHECK
 
1699
  if (stream[2] != 8)
 
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");
 
1705
  if (height%8)
 
1706
    error("Height need to be a multiple of 8 (current height is %d)\n", height);
 
1707
  if (width%16)
 
1708
    error("Width need to be a multiple of 16 (current Width is %d)\n", width);
 
1709
#endif
 
1710
  stream += 8;
 
1711
  for (i=0; i<nr_components; i++) {
 
1712
     cid = *stream++;
 
1713
     sampling_factor = *stream++;
 
1714
     Q_table = *stream++;
 
1715
     c = &priv->component_infos[i];
 
1716
#if SANITY_CHECK
 
1717
     c->cid = cid;
 
1718
#endif
 
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 );
 
1724
 
 
1725
  }
 
1726
  priv->width = width;
 
1727
  priv->height = height;
 
1728
 
 
1729
  trace("< SOF marker\n");
 
1730
 
 
1731
  return 0;
 
1732
}
 
1733
 
 
1734
static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
 
1735
{
 
1736
  unsigned int i, cid, table;
 
1737
  unsigned int nr_components = stream[2];
 
1738
 
 
1739
  trace("> SOS marker\n");
 
1740
 
 
1741
#if SANITY_CHECK
 
1742
  if (nr_components != 3 && nr_components != 1)
 
1743
    error("We only support YCbCr image\n");
 
1744
#endif
 
1745
 
 
1746
  if (nr_components == 1)
 
1747
    priv->flags |= TINYJPEG_FLAGS_PLANAR_JPEG;
 
1748
#if SANITY_CHECK
 
1749
  else if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
 
1750
    error("SOS with more then 1 component while decoding planar JPEG\n");
 
1751
#endif
 
1752
 
 
1753
  stream += 3;
 
1754
  for (i=0;i<nr_components;i++) {
 
1755
     cid = *stream++;
 
1756
     table = *stream++;
 
1757
     if (nr_components == 1) {
 
1758
#if SANITY_CHECK
 
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)
 
1762
           break;
 
1763
 
 
1764
       if (i == COMPONENTS)
 
1765
         error("Unknown cid in SOS: %u\n", cid);
 
1766
 
 
1767
       priv->current_cid = cid;
 
1768
#else
 
1769
       i = cid - 1;
 
1770
#endif
 
1771
       trace("SOS cid: %u, using component_info: %u\n", cid, i);
 
1772
     }
 
1773
#if SANITY_CHECK
 
1774
     if ((table&0xf) >= HUFFMAN_TABLES)
 
1775
        error("We do not support more than %d AC Huffman table\n",
 
1776
          HUFFMAN_TABLES);
 
1777
     if ((table>>4) >= HUFFMAN_TABLES)
 
1778
        error("We do not support more than %d DC Huffman table\n",
 
1779
          HUFFMAN_TABLES);
 
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);
 
1784
#endif
 
1785
     priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
 
1786
     priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
 
1787
  }
 
1788
  priv->stream = stream+3;
 
1789
 
 
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;
 
1792
 
 
1793
  trace("< SOS marker\n");
 
1794
 
 
1795
  return 0;
 
1796
}
 
1797
 
 
1798
static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
 
1799
{
 
1800
  unsigned int count, i;
 
1801
  unsigned char huff_bits[17];
 
1802
  int length, index;
 
1803
 
 
1804
  length = be16_to_cpu(stream) - 2;
 
1805
  stream += 2;  /* Skip length */
 
1806
 
 
1807
  trace("> DHT marker (length=%d)\n", length);
 
1808
 
 
1809
  while (length>0) {
 
1810
     index = *stream++;
 
1811
 
 
1812
     /* We need to calculate the number of bytes 'vals' will takes */
 
1813
     huff_bits[0] = 0;
 
1814
     count = 0;
 
1815
     for (i=1; i<17; i++) {
 
1816
        huff_bits[i] = *stream++;
 
1817
        count += huff_bits[i];
 
1818
     }
 
1819
#if SANITY_CHECK
 
1820
     if (count > 1024)
 
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);
 
1826
#endif
 
1827
 
 
1828
     if (index & 0xf0 ) {
 
1829
       if (build_huffman_table(priv, huff_bits, stream, &priv->HTAC[index&0xf]))
 
1830
         return -1;
 
1831
     } else {
 
1832
       if (build_huffman_table(priv, huff_bits, stream, &priv->HTDC[index&0xf]))
 
1833
         return -1;
 
1834
     }
 
1835
 
 
1836
     length -= 1;
 
1837
     length -= 16;
 
1838
     length -= count;
 
1839
     stream += count;
 
1840
  }
 
1841
  trace("< DHT marker\n");
 
1842
  return 0;
 
1843
}
 
1844
 
 
1845
static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
 
1846
{
 
1847
  unsigned int length;
 
1848
 
 
1849
  trace("> DRI marker\n");
 
1850
 
 
1851
  length = be16_to_cpu(stream);
 
1852
 
 
1853
#if SANITY_CHECK
 
1854
  if (length != 4)
 
1855
    error("Length of DRI marker need to be 4\n");
 
1856
#endif
 
1857
 
 
1858
  priv->restart_interval = be16_to_cpu(stream+2);
 
1859
 
 
1860
#if DEBUG
 
1861
  trace("Restart interval = %d\n", priv->restart_interval);
 
1862
#endif
 
1863
 
 
1864
  trace("< DRI marker\n");
 
1865
 
 
1866
  return 0;
 
1867
}
 
1868
 
 
1869
 
 
1870
 
 
1871
static void resync(struct jdec_private *priv)
 
1872
{
 
1873
  int i;
 
1874
 
 
1875
  /* Init DC coefficients */
 
1876
  for (i=0; i<COMPONENTS; i++)
 
1877
     priv->component_infos[i].previous_DC = 0;
 
1878
 
 
1879
  priv->reservoir = 0;
 
1880
  priv->nbits_in_reservoir = 0;
 
1881
  if (priv->restart_interval > 0)
 
1882
    priv->restarts_to_go = priv->restart_interval;
 
1883
  else
 
1884
    priv->restarts_to_go = -1;
 
1885
}
 
1886
 
 
1887
static int find_next_rst_marker(struct jdec_private *priv)
 
1888
{
 
1889
  int rst_marker_found = 0;
 
1890
  int marker;
 
1891
  const unsigned char *stream = priv->stream;
 
1892
 
 
1893
  /* Parse marker */
 
1894
  while (!rst_marker_found)
 
1895
   {
 
1896
     while (*stream++ != 0xff)
 
1897
      {
 
1898
        if (stream >= priv->stream_end)
 
1899
          error("EOF while search for a RST marker.\n");
 
1900
      }
 
1901
     /* Skip any padding ff byte (this is normal) */
 
1902
     while (*stream == 0xff)
 
1903
      {
 
1904
       stream++;
 
1905
       if (stream >= priv->stream_end)
 
1906
         error("EOF while search for a RST marker.\n");
 
1907
      }
 
1908
 
 
1909
     marker = *stream++;
 
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)
 
1915
       return 0;
 
1916
   }
 
1917
 
 
1918
  priv->stream = stream;
 
1919
  priv->last_rst_marker_seen++;
 
1920
  priv->last_rst_marker_seen &= 7;
 
1921
 
 
1922
  return 0;
 
1923
}
 
1924
 
 
1925
static int find_next_sos_marker(struct jdec_private *priv)
 
1926
{
 
1927
  const unsigned char *stream = priv->stream;
 
1928
 
 
1929
  /* Parse marker */
 
1930
  while (1) {
 
1931
    while (*stream++ != 0xff) {
 
1932
      if (stream >= priv->stream_end)
 
1933
        error("EOF while search for a SOS marker.\n");
 
1934
    }
 
1935
    /* Skip any padding ff byte (this is normal) */
 
1936
    while (*stream == 0xff) {
 
1937
      stream++;
 
1938
      if (stream >= priv->stream_end)
 
1939
        error("EOF while search for a SOS marker.\n");
 
1940
    }
 
1941
 
 
1942
    if (*stream++ == SOS)
 
1943
      break; /* Found it ! */
 
1944
   }
 
1945
 
 
1946
  priv->stream = stream;
 
1947
 
 
1948
  return 0;
 
1949
}
 
1950
 
 
1951
static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
 
1952
{
 
1953
  int chuck_len;
 
1954
  int marker;
 
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;
 
1960
 
 
1961
  /* Parse marker */
 
1962
  while (!sos_marker_found)
 
1963
   {
 
1964
     if (*stream++ != 0xff)
 
1965
       goto bogus_jpeg_format;
 
1966
     /* Skip any padding ff byte (this is normal) */
 
1967
     while (*stream == 0xff)
 
1968
       stream++;
 
1969
 
 
1970
     marker = *stream++;
 
1971
     chuck_len = be16_to_cpu(stream);
 
1972
     next_chunck = stream + chuck_len;
 
1973
     switch (marker)
 
1974
      {
 
1975
       case SOF:
 
1976
         if (parse_SOF(priv, stream) < 0)
 
1977
           return -1;
 
1978
         sof_marker_found = 1;
 
1979
         break;
 
1980
       case DQT:
 
1981
         if (parse_DQT(priv, stream) < 0)
 
1982
           return -1;
 
1983
         dqt_marker_found = 1;
 
1984
         break;
 
1985
       case SOS:
 
1986
         if (parse_SOS(priv, stream) < 0)
 
1987
           return -1;
 
1988
         sos_marker_found = 1;
 
1989
         break;
 
1990
       case DHT:
 
1991
         if (parse_DHT(priv, stream) < 0)
 
1992
           return -1;
 
1993
         dht_marker_found = 1;
 
1994
         break;
 
1995
       case DRI:
 
1996
         if (parse_DRI(priv, stream) < 0)
 
1997
           return -1;
 
1998
         break;
 
1999
       default:
 
2000
         trace("> Unknown marker %2.2x\n", marker);
 
2001
         break;
 
2002
      }
 
2003
 
 
2004
     stream = next_chunck;
 
2005
   }
 
2006
 
 
2007
  if ( !sof_marker_found ||
 
2008
      (!dqt_marker_found && !(priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)))
 
2009
    goto bogus_jpeg_format;
 
2010
 
 
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]);
 
2015
    }
 
2016
  }
 
2017
 
 
2018
  if (!dht_marker_found) {
 
2019
    trace("No Huffman table loaded, using the default one\n");
 
2020
    if (build_default_huffman_tables(priv))
 
2021
      return -1;
 
2022
  }
 
2023
 
 
2024
#ifdef SANITY_CHECK
 
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");
 
2040
#endif
 
2041
 
 
2042
  return 0;
 
2043
bogus_jpeg_format:
 
2044
  error("Bogus jpeg format\n");
 
2045
  return -1;
 
2046
}
 
2047
 
 
2048
/*******************************************************************************
 
2049
 *
 
2050
 * Functions exported of the library.
 
2051
 *
 
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
 
2055
 * the decoding.
 
2056
 *
 
2057
 ******************************************************************************/
 
2058
 
 
2059
/**
 
2060
 * Allocate a new tinyjpeg decoder object.
 
2061
 *
 
2062
 * Before calling any other functions, an object need to be called.
 
2063
 */
 
2064
struct jdec_private *tinyjpeg_init(void)
 
2065
{
 
2066
  struct jdec_private *priv;
 
2067
 
 
2068
  priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private));
 
2069
  if (priv == NULL)
 
2070
    return NULL;
 
2071
  return priv;
 
2072
}
 
2073
 
 
2074
/**
 
2075
 * Free a tinyjpeg object.
 
2076
 *
 
2077
 * No others function can be called after this one.
 
2078
 */
 
2079
void tinyjpeg_free(struct jdec_private *priv)
 
2080
{
 
2081
  int i;
 
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;
 
2087
  }
 
2088
  priv->tmp_buf_y_size = 0;
 
2089
  free(priv->stream_filtered);
 
2090
  free(priv);
 
2091
}
 
2092
 
 
2093
/**
 
2094
 * Initialize the tinyjpeg object and prepare the decoding of the stream.
 
2095
 *
 
2096
 * Check if the jpeg can be decoded with this jpeg decoder.
 
2097
 * Fill some table used for preprocessing.
 
2098
 */
 
2099
int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
 
2100
{
 
2101
  /* Identify the file */
 
2102
  if ((buf[0] != 0xFF) || (buf[1] != SOI))
 
2103
    error("Not a JPG file ?\n");
 
2104
 
 
2105
  priv->stream_end = buf + size;
 
2106
 
 
2107
  return parse_JFIF(priv, buf + 2);
 
2108
}
 
2109
 
 
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,
 
2115
};
 
2116
 
 
2117
static const decode_MCU_fct pixart_decode_mcu_3comp_table[4] = {
 
2118
   NULL,
 
2119
   NULL,
 
2120
   pixart_decode_MCU_2x1_3planes,
 
2121
   NULL,
 
2122
};
 
2123
 
 
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,
 
2129
};
 
2130
 
 
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,
 
2136
};
 
2137
 
 
2138
static const convert_colorspace_fct convert_colorspace_rgb24[4] = {
 
2139
   YCrCB_to_RGB24_1x1,
 
2140
   YCrCB_to_RGB24_1x2,
 
2141
   YCrCB_to_RGB24_2x1,
 
2142
   YCrCB_to_RGB24_2x2,
 
2143
};
 
2144
 
 
2145
static const convert_colorspace_fct convert_colorspace_bgr24[4] = {
 
2146
   YCrCB_to_BGR24_1x1,
 
2147
   YCrCB_to_BGR24_1x2,
 
2148
   YCrCB_to_BGR24_2x1,
 
2149
   YCrCB_to_BGR24_2x2,
 
2150
};
 
2151
 
 
2152
static const convert_colorspace_fct convert_colorspace_grey[4] = {
 
2153
   YCrCB_to_Grey_1x1,
 
2154
   YCrCB_to_Grey_1x2,
 
2155
   YCrCB_to_Grey_2x1,
 
2156
   YCrCB_to_Grey_2x2,
 
2157
};
 
2158
 
 
2159
int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt);
 
2160
 
 
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)
 
2164
{
 
2165
        int chunksize, copied = 0;
 
2166
 
 
2167
        /* Skip mysterious first data byte */
 
2168
        src++;
 
2169
        n--;
 
2170
 
 
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;
 
2174
 
 
2175
        while (1) {
 
2176
                if (n < chunksize)
 
2177
                        break; /* Short frame */
 
2178
 
 
2179
                memcpy(dest, src, chunksize);
 
2180
                dest += chunksize;
 
2181
                src += chunksize;
 
2182
                copied += chunksize;
 
2183
                n -= chunksize;
 
2184
 
 
2185
                if (n < 4)
 
2186
                        break; /* Short frame */
 
2187
 
 
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]);
 
2192
                if (src[3] > 6)
 
2193
                        error("Unexpected Pixart chunk size: %d\n", src[3]);
 
2194
 
 
2195
                chunksize = src[3];
 
2196
                src += 4;
 
2197
                n -= 4;
 
2198
 
 
2199
                if (chunksize == 0) {
 
2200
                        /* 0 indicates we are done, copy whatever remains */
 
2201
                        memcpy(dest, src, n);
 
2202
                        return copied + n;
 
2203
                }
 
2204
 
 
2205
                chunksize = 2048 >> chunksize;
 
2206
        }
 
2207
        error("Short Pixart JPEG frame\n");
 
2208
}
 
2209
 
 
2210
/**
 
2211
 * Decode and convert the jpeg image into @pixfmt@ image
 
2212
 *
 
2213
 * Note: components will be automaticaly allocated if no memory is attached.
 
2214
 */
 
2215
int tinyjpeg_decode(struct jdec_private *priv, int pixfmt)
 
2216
{
 
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;
 
2223
 
 
2224
  if (setjmp(priv->jump_state))
 
2225
    return -1;
 
2226
 
 
2227
  if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
 
2228
    return tinyjpeg_decode_planar(priv, pixfmt);
 
2229
 
 
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;
 
2235
 
 
2236
  decode_mcu_table = decode_mcu_3comp_table;
 
2237
  if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
 
2238
    int length;
 
2239
 
 
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");
 
2246
 
 
2247
    length =  pixart_filter(priv, priv->stream_filtered,
 
2248
                            priv->stream, priv->stream_end - priv->stream);
 
2249
    if (length < 0)
 
2250
      return length;
 
2251
    priv->stream = priv->stream_filtered;
 
2252
    priv->stream_end = priv->stream + length;
 
2253
 
 
2254
    decode_mcu_table = pixart_decode_mcu_3comp_table;
 
2255
  }
 
2256
 
 
2257
  switch (pixfmt) {
 
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;
 
2272
       break;
 
2273
 
 
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;
 
2280
       break;
 
2281
 
 
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;
 
2288
       break;
 
2289
 
 
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;
 
2299
       break;
 
2300
 
 
2301
     default:
 
2302
       error("Bad pixel format\n");
 
2303
  }
 
2304
 
 
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");
 
2321
  } else {
 
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");
 
2326
  }
 
2327
 
 
2328
  if (decode_MCU == NULL)
 
2329
    error("no decode MCU function for this JPEG format (PIXART?)\n");
 
2330
 
 
2331
  resync(priv);
 
2332
 
 
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;
 
2337
 
 
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;
 
2341
 
 
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++)
 
2344
   {
 
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)
 
2350
      {
 
2351
        decode_MCU(priv);
 
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)
 
2357
         {
 
2358
           priv->restarts_to_go--;
 
2359
           if (priv->restarts_to_go == 0)
 
2360
            {
 
2361
              priv->stream -= (priv->nbits_in_reservoir/8);
 
2362
              resync(priv);
 
2363
              if (find_next_rst_marker(priv) < 0)
 
2364
                return -1;
 
2365
            }
 
2366
         }
 
2367
      }
 
2368
   }
 
2369
 
 
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");
 
2374
  }
 
2375
 
 
2376
  return 0;
 
2377
}
 
2378
 
 
2379
int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt)
 
2380
{
 
2381
  unsigned int i, x, y;
 
2382
  uint8_t *y_buf, *u_buf, *v_buf, *p, *p2;
 
2383
 
 
2384
  switch (pixfmt) {
 
2385
  case TINYJPEG_FMT_GREY:
 
2386
    error("Greyscale output not supported with planar JPEG input\n");
 
2387
    break;
 
2388
 
 
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");
 
2397
      }
 
2398
      priv->tmp_buf_y_size = priv->width * priv->height;
 
2399
    }
 
2400
    y_buf = priv->tmp_buf[cY];
 
2401
    u_buf = priv->tmp_buf[cCb];
 
2402
    v_buf = priv->tmp_buf[cCr];
 
2403
    break;
 
2404
 
 
2405
  case TINYJPEG_FMT_YUV420P:
 
2406
    y_buf = priv->components[cY];
 
2407
    u_buf = priv->components[cCb];
 
2408
    v_buf = priv->components[cCr];
 
2409
    break;
 
2410
 
 
2411
  default:
 
2412
    error("Bad pixel format\n");
 
2413
  }
 
2414
 
 
2415
#if SANITY_CHECK
 
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);
 
2419
#endif
 
2420
 
 
2421
  resync(priv);
 
2422
 
 
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);
 
2427
      y_buf += 8;
 
2428
    }
 
2429
    y_buf += 7 * priv->width;
 
2430
  }
 
2431
 
 
2432
  priv->stream -= (priv->nbits_in_reservoir/8);
 
2433
  resync(priv);
 
2434
  if (find_next_sos_marker(priv) < 0)
 
2435
    return -1;
 
2436
  if (parse_SOS(priv, priv->stream) < 0)
 
2437
    return -1;
 
2438
 
 
2439
#if SANITY_CHECK
 
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);
 
2443
#endif
 
2444
 
 
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);
 
2449
      u_buf += 8;
 
2450
    }
 
2451
    u_buf += 7 * (priv->width / 2);
 
2452
  }
 
2453
 
 
2454
  priv->stream -= (priv->nbits_in_reservoir/8);
 
2455
  resync(priv);
 
2456
  if (find_next_sos_marker(priv) < 0)
 
2457
    return -1;
 
2458
  if (parse_SOS(priv, priv->stream) < 0)
 
2459
    return -1;
 
2460
 
 
2461
#if SANITY_CHECK
 
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);
 
2465
#endif
 
2466
 
 
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);
 
2471
      v_buf += 8;
 
2472
    }
 
2473
    v_buf += 7 * (priv->width / 2);
 
2474
  }
 
2475
 
 
2476
#define SCALEBITS       10
 
2477
#define ONE_HALF        (1UL << (SCALEBITS-1))
 
2478
#define FIX(x)          ((int)((x) * (1UL<<SCALEBITS) + 0.5))
 
2479
 
 
2480
  switch (pixfmt) {
 
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;
 
2487
 
 
2488
    for (y = 0; y < priv->height / 2; y++) {
 
2489
      for (x = 0; x < priv->width / 2; x++) {
 
2490
        int l, cb, cr;
 
2491
        int add_r, add_g, add_b;
 
2492
        int r, g , b;
 
2493
 
 
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;
 
2499
 
 
2500
        l  = (*y_buf) << SCALEBITS;
 
2501
        r = (l + add_r) >> SCALEBITS;
 
2502
        *p++ = clamp(r);
 
2503
        g = (l + add_g) >> SCALEBITS;
 
2504
        *p++ = clamp(g);
 
2505
        b = (l + add_b) >> SCALEBITS;
 
2506
        *p++ = clamp(b);
 
2507
 
 
2508
        l  = (y_buf[priv->width]) << SCALEBITS;
 
2509
        r = (l + add_r) >> SCALEBITS;
 
2510
        *p2++ = clamp(r);
 
2511
        g = (l + add_g) >> SCALEBITS;
 
2512
        *p2++ = clamp(g);
 
2513
        b = (l + add_b) >> SCALEBITS;
 
2514
        *p2++ = clamp(b);
 
2515
 
 
2516
        y_buf++;
 
2517
 
 
2518
        l  = (*y_buf) << SCALEBITS;
 
2519
        r = (l + add_r) >> SCALEBITS;
 
2520
        *p++ = clamp(r);
 
2521
        g = (l + add_g) >> SCALEBITS;
 
2522
        *p++ = clamp(g);
 
2523
        b = (l + add_b) >> SCALEBITS;
 
2524
        *p++ = clamp(b);
 
2525
 
 
2526
        l  = (y_buf[priv->width]) << SCALEBITS;
 
2527
        r = (l + add_r) >> SCALEBITS;
 
2528
        *p2++ = clamp(r);
 
2529
        g = (l + add_g) >> SCALEBITS;
 
2530
        *p2++ = clamp(g);
 
2531
        b = (l + add_b) >> SCALEBITS;
 
2532
        *p2++ = clamp(b);
 
2533
 
 
2534
        y_buf++;
 
2535
      }
 
2536
      y_buf += priv->width;
 
2537
      p  += priv->width * 3;
 
2538
      p2 += priv->width * 3;
 
2539
    }
 
2540
    break;
 
2541
 
 
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;
 
2548
 
 
2549
    for (y = 0; y < priv->height / 2; y++) {
 
2550
      for (x = 0; x < priv->width / 2; x++) {
 
2551
        int l, cb, cr;
 
2552
        int add_r, add_g, add_b;
 
2553
        int r, g , b;
 
2554
 
 
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;
 
2560
 
 
2561
        l  = (*y_buf) << SCALEBITS;
 
2562
        b = (l + add_b) >> SCALEBITS;
 
2563
        *p++ = clamp(b);
 
2564
        g = (l + add_g) >> SCALEBITS;
 
2565
        *p++ = clamp(g);
 
2566
        r = (l + add_r) >> SCALEBITS;
 
2567
        *p++ = clamp(r);
 
2568
 
 
2569
        l  = (y_buf[priv->width]) << SCALEBITS;
 
2570
        b = (l + add_b) >> SCALEBITS;
 
2571
        *p2++ = clamp(b);
 
2572
        g = (l + add_g) >> SCALEBITS;
 
2573
        *p2++ = clamp(g);
 
2574
        r = (l + add_r) >> SCALEBITS;
 
2575
        *p2++ = clamp(r);
 
2576
 
 
2577
        y_buf++;
 
2578
 
 
2579
        l  = (*y_buf) << SCALEBITS;
 
2580
        b = (l + add_b) >> SCALEBITS;
 
2581
        *p++ = clamp(b);
 
2582
        g = (l + add_g) >> SCALEBITS;
 
2583
        *p++ = clamp(g);
 
2584
        r = (l + add_r) >> SCALEBITS;
 
2585
        *p++ = clamp(r);
 
2586
 
 
2587
        l  = (y_buf[priv->width]) << SCALEBITS;
 
2588
        b = (l + add_b) >> SCALEBITS;
 
2589
        *p2++ = clamp(b);
 
2590
        g = (l + add_g) >> SCALEBITS;
 
2591
        *p2++ = clamp(g);
 
2592
        r = (l + add_r) >> SCALEBITS;
 
2593
        *p2++ = clamp(r);
 
2594
 
 
2595
        y_buf++;
 
2596
      }
 
2597
      y_buf += priv->width;
 
2598
      p  += priv->width * 3;
 
2599
      p2 += priv->width * 3;
 
2600
    }
 
2601
    break;
 
2602
  }
 
2603
 
 
2604
#undef SCALEBITS
 
2605
#undef ONE_HALF
 
2606
#undef FIX
 
2607
 
 
2608
  return 0;
 
2609
}
 
2610
 
 
2611
const char *tinyjpeg_get_errorstring(struct jdec_private *priv)
 
2612
{
 
2613
  return priv->error_string;
 
2614
}
 
2615
 
 
2616
void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height)
 
2617
{
 
2618
  *width = priv->width;
 
2619
  *height = priv->height;
 
2620
}
 
2621
 
 
2622
int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components)
 
2623
{
 
2624
  int i;
 
2625
  for (i=0; priv->components[i] && i<COMPONENTS; i++)
 
2626
    components[i] = priv->components[i];
 
2627
  return 0;
 
2628
}
 
2629
 
 
2630
int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents)
 
2631
{
 
2632
  unsigned int i;
 
2633
  if (ncomponents > COMPONENTS)
 
2634
    ncomponents = COMPONENTS;
 
2635
  for (i=0; i<ncomponents; i++)
 
2636
    priv->components[i] = components[i];
 
2637
  return 0;
 
2638
}
 
2639
 
 
2640
int tinyjpeg_set_flags(struct jdec_private *priv, int flags)
 
2641
{
 
2642
  int oldflags = priv->flags;
 
2643
  priv->flags = flags;
 
2644
  return oldflags;
 
2645
}
 
2646