1 /*
2 * jpeg-f.cc --
3 *
4 * FIXME: This file needs a description here.
5 */
6
7 /*
8 * This code is derived from the Independent JPEG Group's JPEG software:
9 *
10 * Copyright (C) 1991, 1992, Thomas G. Lane.
11 * This file is part of the Independent JPEG Group's software.
12 * For conditions of distribution and use, see the accompanying
13 * README.IJPG file.
14 */
15
16 /*FIXME this doesn go here */
17 #define DESCALE(x,n) (((x) + (1 << ((n) - 1))) >> (n))
18 #define CONST_BITS 13
19 #define MULTIPLY(a, b) ((a) * (b))
20 #define CONST_SCALE (1 << CONST_BITS)
21 #define FIX(x) ((int)((x) * CONST_SCALE + 0.5))
22 #define PASS1_BITS 2
23
24 #include "jpeg.h"
25 #include "endian.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/param.h>
30 #include <netinet/in.h>
31
32 /*
33 * Ck = cos(k pi / 16)
34 * Sk = sin(k pi / 16)
35 */
36 #define C1 0.98078528
37 #define C2 0.92387953
38 #define C3 0.83146961
39 #define C4 0.70710678
40 #define C5 0.55557023
41 #define C6 0.38268343
42 #define C7 0.19509032
43 #define S1 C7
44 #define S3 C5
45 #define S6 C2
46 #define C_1 C1
47 #define S_1 (-S1)
48 #define C_3 C3
49 #define S_3 (-S3)
50
51 void idct(short* data, u_int* mask, u_char* p, int stride);
52 void v_rdct(short* data, u_int* mask, u_char* p, int stride, const int* qt);
53
54 #if defined(__osf__) || defined(sgi)
55 #include <string.h>
56 #ifdef sgi
57 #include <bstring.h>
58 #endif
59 #elif !defined(__svr4__)
60 #include <bstring.h>
61 #endif
62
63 #define u_word u_int
64
65 /*
66 * These two macros stolen from nv.
67 */
68 /* Sick little macro which will limit x to [0..255] with logical ops */
69 #define UCLIMIT(x, t) ((t = (x)), (t &= ~(t>>31)), (t | ~((t-256) >> 31)))
70 /* A variant of above which will limit x to [-128..127] */
71 #define SCLIMIT(x, t) (UCLIMIT((x)+128, t)-128)
72
73 /*
74 * ZAG[i] is the natural-order position of the i'th element of zigzag order.
75 * If the incoming data is corrupted, huff_decode_mcu could attempt to
76 * reference values beyond the end of the array. To avoid a wild store,
77 * we put some extra zeroes after the real entries.
78 */
79 #ifdef notdef
80 static const int ZAG[] = {
81 0, 1, 8, 16, 9, 2, 3, 10,
82 17, 24, 32, 25, 18, 11, 4, 5,
83 12, 19, 26, 33, 40, 48, 41, 34,
84 27, 20, 13, 6, 7, 14, 21, 28,
85 35, 42, 49, 56, 57, 50, 43, 36,
86 29, 22, 15, 23, 30, 37, 44, 51,
87 58, 59, 52, 45, 38, 31, 39, 46,
88 53, 60, 61, 54, 47, 55, 62, 63,
89 /* extra entries in case k>63 below */
90 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0
92 };
93 #else
94 /* column order */
95 static const int ZAG[] = {
96 0, 8, 1, 2, 9, 16, 24, 17,
97 10, 3, 4, 11, 18, 25, 32, 40,
98 33, 26, 19, 12, 5, 6, 13, 20,
99 27, 34, 41, 48, 56, 49, 42, 35,
100 28, 21, 14, 7, 15, 22, 29, 36,
101 43, 50, 57, 58, 51, 44, 37, 30,
102 23, 31, 38, 45, 52, 59, 60, 53,
103 46, 39, 47, 54, 61, 62, 55, 63,
104 /* extra entries in case k>63 below */
105 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0
107 };
108 #endif
109
110 class JpegDecoder_420 : public JpegDecoder {
111 public:
112 JpegDecoder_420(const config&);
113 virtual int decode(u_char* in, int len);
114 };
115
116 class JpegDecoder_422 : public JpegDecoder {
117 public:
118 JpegDecoder_422(const config&);
119 virtual int decode(u_char* in, int len);
120 };
121
122 JpegDecoder_420::JpegDecoder_420(const config& c) : JpegDecoder(c)
123 {
124 csss_ = 420;
125 }
126
127 JpegDecoder_422::JpegDecoder_422(const config& c) : JpegDecoder(c)
128 {
129 csss_ = 422;
130 }
131
132 JpegDecoder* JpegDecoder::create(const config& c)
133 {
134 if (c.ncomp == 3 && c.comp[0].hsf == 2 &&
135 c.comp[1].hsf == 1 && c.comp[1].vsf == 1 &&
136 c.comp[2].hsf == 1 && c.comp[2].vsf == 1) {
137 if (c.comp[0].vsf == 2)
138 return (new JpegDecoder_420(c));
139 if (c.comp[0].vsf == 1)
140 return (new JpegDecoder_422(c));
141 }
142 return (0);
143 }
144
145 int quality_to_qfactor(int v)
146 {
147 if (v < 1)
148 v = 5000;
149 else if (v < 50)
150 v = 5000 / v;
151 else if (v < 100)
152 v = 200 - v * 2;
153 else
154 v = 1;
155
156 return (v);
157 }
158
159 /*
160 * Tables from IJPG software
161 */
162 static const int std_luminance_quant_tbl[64] = {
163 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40,
164 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51,
165 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 87,
166 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99
167 };
168
169 static const int std_chrominance_quant_tbl[64] = {
170 17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, 99, 99,
171 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
172 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
173 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
174 };
175
176 static const unsigned char dc_luminance_bits[17] =
177 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
178 static const unsigned char dc_luminance_val[] =
179 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
180
181 static const unsigned char dc_chrominance_bits[17] =
182 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
183 static const unsigned char dc_chrominance_val[] =
184 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
185
186 static const unsigned char ac_luminance_bits[17] =
187 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
188 static const unsigned char ac_luminance_val[] =
189 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
190 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
191 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
192 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
193 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
194 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
195 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
196 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
197 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
198 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
199 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
200 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
201 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
202 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
203 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
204 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
205 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
206 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
207 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
208 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
209 0xf9, 0xfa };
210
211 static const unsigned char ac_chrominance_bits[17] =
212 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
213 static const unsigned char ac_chrominance_val[] =
214 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
215 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
216 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
217 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
218 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
219 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
220 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
221 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
222 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
223 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
224 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
225 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
226 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
227 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
228 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
229 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
230 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
231 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
232 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
233 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
234 0xf9, 0xfa };
235
236 void JpegDecoder::defaults(JpegDecoder::config& c)
237 {
238 c.width = 0;
239 c.height = 0;
240
241 c.precision = 8;
242 c.ncomp = 3;
243 /* Y */
244 c.comp[0].id = 0;
245 c.comp[0].hsf = 2;
246 c.comp[0].vsf = 1;
247 c.comp[0].qno = 0;
248 /* U */
249 c.comp[1].id = 1;
250 c.comp[1].hsf = 1;
251 c.comp[1].vsf = 1;
252 c.comp[1].qno = 1;
253 /* V */
254 c.comp[2].id = 2;
255 c.comp[2].hsf = 1;
256 c.comp[2].vsf = 1;
257 c.comp[2].qno = 1;
258
259 c.dc_huffbits[0] = dc_luminance_bits;
260 c.dc_huffval[0] = dc_luminance_val;
261 c.dc_huffbits[1] = dc_chrominance_bits;
262 c.dc_huffval[1] = dc_chrominance_val;
263 c.ac_huffbits[0] = ac_luminance_bits;
264 c.ac_huffval[0] = ac_luminance_val;
265 c.ac_huffbits[1] = ac_chrominance_bits;
266 c.ac_huffval[1] = ac_chrominance_val;
267 for (int i = 2; i < 4; ++i) {
268 c.ac_huffbits[i] = 0;
269 c.ac_huffval[i] = 0;
270 c.dc_huffbits[i] = 0;
271 c.dc_huffval[i] = 0;
272 }
273 c.comp[0].dc_tbl_no = 0;
274 c.comp[0].ac_tbl_no = 0;
275 c.comp[1].dc_tbl_no = 1;
276 c.comp[1].ac_tbl_no = 1;
277 c.comp[2].dc_tbl_no = 1;
278 c.comp[2].ac_tbl_no = 1;
279 }
280
281 /*
282 * Set the quantizer for this configuration. Q is the IJPG quality
283 * factor, which has a value in [0,100].
284 */
285 void JpegDecoder::quantizer(JpegDecoder::config& c, int q)
286 {
287 q = quality_to_qfactor(q);
288 for (int i = 0; i < 64; i++) {
289 int val = (q * std_luminance_quant_tbl[i] + 50) / 100;
290 if (val < 0)
291 val = 1;
292 if (val > 32768)
293 val = 32768;
294 c.qtab[0][i] = val;
295 }
296 for (i = 0; i < 64; i++) {
297 int val = (q * std_chrominance_quant_tbl[i] + 50) / 100;
298 if (val < 0)
299 val = 1;
300 if (val > 32768)
301 val = 32768;
302 c.qtab[1][i] = val;
303 }
304 }
305
306 JpegDecoder::JpegDecoder(const config& c) : color_(1),
307 width_(-1), height_(-1), csss_(0),
308 cache_(0), thresh_(0)
309 {
310 for (int i = NUM_HUFF_TBLS; --i >= 0; ) {
311 dcht_[i] = 0;
312 acht_[i] = 0;
313 }
314 bzero((char*)comp_, sizeof(comp_));
315 init(c);
316 }
317
318 JpegDecoder::~JpegDecoder()
319 {
320 freehufftab();
321 delete comp_[0].frm;
322 }
323
324 void
325 JpegDecoder::fold_idct_q(const short *in, int *out)
326 {
327 for (int i = 0; i < 64; ++i) {
328 int v = in[i];
329 int k = ZAG[i];
330 int s, c;
331 switch (k & 7) {
332 default:
333 c = v * FIX(1);
334 s = 0;
335 break;
336 case 1:
337 c = v * FIX(C_1 / C4);
338 s = v * FIX(S_1 / C4);
339 break;
340 case 2:
341 c = v * FIX(C6 / C4);
342 s = v * FIX(S6 / C4);
343 break;
344 case 3:
345 c = v * FIX(C_3 / C4);
346 s = v * FIX(S_3 / C4);
347 break;
348 case 5:
349 c = v * FIX(C_3 / C4);
350 s = v * -FIX(S_3 / C4);
351 break;
352 case 6:
353 c = v * FIX(C6 / C4);
354 s = v * -FIX(S6 / C4);
355 break;
356 case 7:
357 c = v * FIX(C_1 / C4);
358 s = v * -FIX(S_1 / C4);
359 break;
360 }
361 /* row to col ordering */
362 #ifdef notdef
363 k = (k & 7) << 3 | k >> 3;
364 #endif
365 k <<= 1;
366 out[k] = c;
367 out[k + 1] = s;
368 }
369 }
370
371 void JpegDecoder::init(const config& c)
372 {
373 rlen_ = 0;
374 for (int i = 0; i < 4; ++i)
375 fold_idct_q(c.qtab[i], qt_[i]);
376
377 ncomp_ = c.ncomp;/*FIXME*/
378 if (width_ != c.width || height_ != c.height) {
379 width_ = c.width;
380 height_ = c.height;
381
382 /*
383 * Allocate the frame store
384 */
385 delete comp_[0].frm;
386 int size = size_ = width_ * height_;
387
388 /*FIXME this allocates more than we need for 4:1:1*/
389 /*FIXME compute sizes based on csss factors*/
390 u_char* p = new u_char[2 * size];
391 /* Initialize to gray */
392 memset((char*)p, 0x80, 2 * size);
393
394 comp_[0].frm = p;
395 p += size;
396 comp_[1].frm = p;
397 p += size / 2;
398 comp_[2].frm = p;
399 }
400 for (i = ncomp_; --i >= 0; ) {
401 int id = c.comp[i].id;
402 comp_[id].hsf = c.comp[i].hsf;
403 comp_[id].vsf = c.comp[i].vsf;
404 comp_[id].qno = c.comp[i].qno;
405 comp_[id].dc_tbl_no = c.comp[i].dc_tbl_no;
406 comp_[id].ac_tbl_no = c.comp[i].ac_tbl_no;
407 }
408 /*
409 * FIXME should check if huffman table won't change
410 * before reallocating.
411 */
412 freehufftab();
413 for (i = 0; i < 4; ++i) {
414 if (c.dc_huffval[i] != 0) {
415 int id = c.comp[i].id;
416 dcht_[id] = huffbuild(c.dc_huffbits[i],
417 c.dc_huffval[i]);
418 }
419 if (c.ac_huffval[i] != 0) {
420 int id = c.comp[i].id;
421 acht_[id] = huffbuild(c.ac_huffbits[i],
422 c.ac_huffval[i]);
423 }
424 }
425
426 int maxh = 1;
427 int maxv = 1;
428 for (i = ncomp_; --i >= 0; ) {
429 if (maxh < comp_[i].hsf)
430 maxh = comp_[i].hsf;
431 if (maxv < comp_[i].vsf)
432 maxh = comp_[i].vsf;
433 }
434 mcu_cols_ = (width_ + 8 * maxh - 1) / (8 * maxh);
435 mcu_rows_ = (height_ + 8 * maxv - 1) / (8 * maxv);
436
437 int nmcu = mcu_cols_ * mcu_rows_;
438 int nblk = nmcu * comp_[0].hsf * comp_[0].vsf;
439 nblk += nmcu * comp_[1].hsf * comp_[1].vsf;
440 nblk += nmcu * comp_[2].hsf * comp_[2].vsf;
441 #define NCC 6
442 int ns = NCC * nblk;
443 delete cache_;
444 cache_ = new short[ns];
445 bzero((char*)cache_, ns * sizeof(*cache_));
446 }
447
448 void JpegDecoder::freehufftab()
449 {
450 for (int i = 0; i < 4; ++i) {
451 if (dcht_[i] != 0)
452 free(dcht_[i]);
453 if (acht_[i] != 0)
454 free(acht_[i]);
455 }
456 }
457
458 void JpegDecoder::fill(u_int dc, u_char* out, int stride) const
459 {
460 int t;
461 dc = UCLIMIT(dc, t) & 0xff;
462 dc |= dc << 8;
463 dc |= dc << 16;
464 #ifdef INT_64
465 INT_64 xdc = dc;
466 xdc |= xdc << 32;
467 *(INT_64 *)out = xdc;
468 out += stride;
469 *(INT_64 *)out = xdc;
470 out += stride;
471 *(INT_64 *)out = xdc;
472 out += stride;
473 *(INT_64 *)out = xdc;
474 out += stride;
475 *(INT_64 *)out = xdc;
476 out += stride;
477 *(INT_64 *)out = xdc;
478 out += stride;
479 *(INT_64 *)out = xdc;
480 out += stride;
481 *(INT_64 *)out = xdc;
482 #else
483 *(u_word*)out = dc;
484 *(u_word*)(out + 4) = dc;
485 out += stride;
486 *(u_word*)out = dc;
487 *(u_word*)(out + 4) = dc;
488 out += stride;
489 *(u_word*)out = dc;
490 *(u_word*)(out + 4) = dc;
491 out += stride;
492 *(u_word*)out = dc;
493 *(u_word*)(out + 4) = dc;
494 out += stride;
495 *(u_word*)out = dc;
496 *(u_word*)(out + 4) = dc;
497 out += stride;
498 *(u_word*)out = dc;
499 *(u_word*)(out + 4) = dc;
500 out += stride;
501 *(u_word*)out = dc;
502 *(u_word*)(out + 4) = dc;
503 out += stride;
504 *(u_word*)out = dc;
505 *(u_word*)(out + 4) = dc;
506 #endif
507 }
508
509 #if NCC != 6
510
511 @BUG in blkdiff@
512 #endif
513 #define ABS(t) ((t) - (((t) >> 31 & (t)) << 1))
514 inline int blkdiff(short* blk, short* cache)
515 {
516 int t = blk[0] - cache[0];
517 int d = ABS(t);
518 t = blk[1] - cache[1];
519 d += ABS(t);
520 t = blk[2] - cache[2];
521 d += ABS(t);
522 t = blk[3] - cache[3];
523 d += ABS(t);
524 t = blk[4] - cache[4];
525 d += ABS(t);
526 t = blk[5] - cache[5];
527 d += ABS(t);
528 return (d);
529 }
530
531 #ifdef notdef
532 /* FIXME this is negligibly faster than above, and has a bug */
533 inline int blkdiff(short* blk, short* cache)
534 {
535 u_int* p0 = (u_int*)blk;
536 u_int* p1 = (u_int*)cache;
537
538 int m = ~0x80008000;
539 int t = (p0[0] >> 1) & m;
540 t += (~p1[0] >> 1) & m;
541 int v = t << 17 >> 17;
542 int d = ABS(v);
543 t = (t << 1) >> 17;
544 d += ABS(v);
545
546 t = (p0[1] >> 1) & m;
547 t += (~p1[1] >> 1) & m;
548 v = t << 17 >> 17;
549 d += ABS(v);
550 t = (t << 1) >> 17;
551 d += ABS(v);
552
553 t = (p0[2] >> 1) & m;
554 t += (~p1[2] >> 1) & m;
555 v = t << 17 >> 17;
556 d += ABS(v);
557 t = (t << 1) >> 17;
558 d += ABS(v);
559
560 return (d);
561 }
562 #endif
563
564 int JpegDecoder::rdqt(const u_char* p)
565 {
566 #ifdef notyet
567 int len = *p++ << 8;
568 len |= *p++;
569
570 const u_char* ep = p + len - 2;
571 while (p < ep) {
572 int n = *p++;
573 int prec = n >> 4;
574 n &= 0x0f;
575 if (n >= 4) {
576 /*FIXME illegal number*/
577 return (-1);
578 }
579 short* qt = qt_[n];
580 for (int i = 0; i < 64; i++) {
581 int v = *p++;
582 if (prec)
583 v = (v << 8) + *p++;
584 qt[i] = v;
585 }
586 }
587 return (len);
588 #else
589 abort();
590 return (0);
591 #endif
592 }
593
594 void JpegDecoder::restart()
595 {
596 int c;
597 nbb_ = 0;
598 /*FIXMEwhat if ff is sitting in bit buffer?*/
599 /* Scan for next JPEG marker */
600 do {
601 do { /* skip any non-FF bytes */
602 c = *inb_++;
603 } while (c != 0xFF);
604 do {
605 /* skip any duplicate FFs */
606 /* we don't increment nbytes here since extra FFs are legal */
607 c = *inb_++;
608 } while (c == 0xFF);
609 } while (c == 0); /* repeat if it was a stuffed FF/00 */
610 #ifdef notdef
611 if (nbytes != 1)
612 WARNMS2(cinfo->emethods,
613 "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
614 nbytes-1, c);
615
616 #endif
617 #ifdef notdef
618 if (c != (RST0 + cinfo->next_restart_num)) {
619 /* Uh-oh, the restart markers have been messed up too. */
620 /* Let the file-format module try to figure out how to resync. */
621 (*cinfo->methods->resync_to_restart) (cinfo, c);
622 } else
623 TRACEMS1(cinfo->emethods, 2, "RST%d", cinfo->next_restart_num);
624 #endif
625 /* Re-initialize DC predictions to 0 */
626 comp_[0].dc = 0;
627 comp_[1].dc = 0;
628 comp_[2].dc = 0;
629 #ifdef notdef
630 cinfo->next_restart_num = (cinfo->next_restart_num + 1) & 7;
631 #endif
632 }
633
634 u_char* JpegDecoder::parseJFIF(u_char* in)
635 {
636 int t;
637 while (in < end_) {
638 if (*in++ != 0xff)
639 continue;
640 /*FIXME need more checks for buffer overflow*/
641 switch (*in++) {
642
643 default:
644 /* Don't know. Keep looking for SOS. */
645 continue;
646
647 case 0xdb:
648 /* quantization table */
649 t = rdqt(in);
650 if (t < 0)
651 /*FIXME*/
652 return (end_);
653 in += t;
654 continue;
655
656 case 0xdd:
657 /* restart interval definition */
658 t = *in++ << 8;
659 t |= *in++;
660 if (t != 4)
661 /* FIXME bad length */
662 ;
663 rlen_ = *in++ << 8;
664 rlen_ |= *in++;
665 rcnt_ = 0;
666 continue;
667
668 case 0xda:
669 /* start-of-scan marker */
670 if (in + 2 <= end_) {
671 /* skip over SOS */
672 int t = *in++ << 8;
673 t |= *in++;
674 in += (t - 2);
675 }
676 return (in);
677 }
678 }
679 /*FIXME*/
680 return (end_);
681 }
682
683 int JpegDecoder_422::decode(u_char* in, int len)
684 {
685 minx_ = mcu_cols_;
686 miny_ = mcu_rows_;
687 maxx_ = 0;
688 maxy_ = 0;
689
690 inb_ = in;
691 end_ = in + len;
692 nbb_ = 0;
693 /*
694 * If first symbol is a marker (a not a stuffed ff),
695 * assume a jfif header is present and parse it.
696 * FIXME this could change state that needs to be
697 * communicated back to caller.
698 */
699 if (in[0] == 0xff && in[1] != 0)
700 inb_ = parseJFIF(inb_);
701
702 u_char* p0 = comp_[0].frm;
703 u_char* p1 = comp_[1].frm;
704 huffreset();
705 short* cache = cache_;
706 u_long ablk[sizeof(short) * 64 / sizeof(u_long)];
707 short* blk = (short*)ablk;
708
709 const int* qt0 = qt_[comp_[0].qno];
710 const int* qt1 = qt_[comp_[1].qno];
711 for (int y = 0; y < mcu_rows_; ++y) {
712 for (int x = 0; x < mcu_cols_; ++x) {
713 /* FIXME use INT_64 */
714 u_int mask[2];
715 /*
716 * If we're handling restart markers,
717 * check if we need to resync.
718 */
719 if (rlen_ != 0 && --rcnt_ <= 0) {
720 rcnt_ = rlen_;
721 restart();
722 }
723
724 int nc = huffparse(comp_[0], blk, cache, mask);
725 int dontskip = nc;
726 cache += NCC;
727 if (nc != 0)
728 v_rdct(blk, mask, p0, width_, qt0);
729 nc = huffparse(comp_[0], blk, cache, mask);
730 dontskip |= nc;
731 cache += NCC;
732 if (nc != 0)
733 v_rdct(blk, mask, p0 + 8, width_, qt0);
734 p0 += 16;
735 if (color_) {
736 /*
737 * If we found above that the luminance
738 * planes exceeded the threhold, decode
739 * the choma planes unconditionally.
740 * Otherwise, see if they can be
741 * suppressed too.
742 */
743 if (dontskip) {
744 huffparsef(comp_[1], blk, cache, mask);
745 cache += NCC;
746 v_rdct(blk, mask, p1, width_ / 2, qt1);
747 huffparsef(comp_[2], blk, cache, mask);
748 cache += NCC;
749 v_rdct(blk, mask, p1 + size_ / 2,
750 width_ / 2, qt1);
751 } else {
752 nc = huffparse(comp_[1], blk,
753 cache, mask);
754 cache += NCC;
755 if (nc != 0)
756 v_rdct(blk, mask, p1,
757 width_ / 2, qt1);
758 dontskip |= nc;
759 nc = huffparse(comp_[2], blk,
760 cache, mask);
761 cache += NCC;
762 if (nc != 0)
763 v_rdct(blk, mask,
764 p1 + size_ / 2,
765 width_ / 2, qt1);
766 dontskip |= nc;
767 }
768 p1 += 8;
769 } else {
770 (void)huffskip(comp_[1]);
771 (void)huffskip(comp_[2]);
772 cache += 2 * NCC;
773 }
774 /* Update bounding box */
775 /* FIXME these can be locals */
776 if (dontskip) {
777 if (x < minx_)
778 minx_ = x;
779 if (x > maxx_)
780 maxx_ = x;
781 if (y < miny_)
782 miny_ = y;
783 if (y > maxy_)
784 maxy_ = y;
785 }
786 }
787 /*
788 * We're at the end of the current line.
789 * Back up to the beggining, then skip down
790 * one row to the start of the next mcu.
791 */
792 p0 -= width_;
793 p0 += 8 * width_;
794 p1 -= width_ / 2;
795 p1 += 8 * width_ / 2;
796 }
797 /*
798 * Convert mcu coords to pixel coords.
799 * A 4:2:2 mcu is 16x8.
800 */
801 minx_ <<= 4;
802 maxx_ <<= 4;
803 miny_ <<= 3;
804 maxy_ <<= 3;
805 maxx_ += 16;
806 maxy_ += 8;
807
808 return (0);
809 }
810
811 int JpegDecoder_420::decode(u_char* in, int len)
812 {
813 #ifdef notdef
814 minx_ = mcu_cols_;
815 miny_ = mcu_rows_;
816 maxx_ = 0;
817 maxy_ = 0;
818
819 inb_ = in;
820 end_ = in + len;
821 nbb_ = 0;
822 /*
823 * FIXME break this into a separate routine that gets called
824 * only when this crap is present
825 */
826 /*
827 * If first symbol is a marker (a not a stuffed ff),
828 * assume a jfif header is present and parse it.
829 * FIXME this could change state that needs to be
830 * communicated back to caller.
831 */
832 if (in[0] == 0xff && in[1] != 0)
833 inb_ = parseJFIF(inb_);
834
835 u_char* p0 = comp_[0].frm;
836 u_char* p1 = comp_[1].frm;
837 huffreset();
838 short* cache = cache_;
839 u_long ablk[sizeof(short) * 64 / sizeof(u_long)];
840 short* blk = (short*)ablk;
841
842 for (int y = mcu_rows_ / 2; --y >= 0; ) {
843 for (int x = mcu_cols_; --x >= 0; ) {
844 if (rlen_ != 0 && --rcnt_ <= 0) {
845 rcnt_ = rlen_;
846 restart();
847 }
848 int skip = huffparse(comp_[0], blk, cache);
849 cache += NCC;
850 if (!skip)
851 blit(blk, p0, width_, qt_);
852 int allskip = skip;
853 skip = huffparse(comp_[0], blk, cache);
854 allskip &= skip;
855 cache += NCC;
856 if (!skip)
857 blit(blk, p0 + 8, width_, qt_);
858 skip = huffparse(comp_[0], blk, cache);
859 allskip &= skip;
860 cache += NCC;
861 if (!skip)
862 blit(blk, p0 + width_, width_);
863 skip = huffparse(comp_[0], blk, cache);
864 allskip &= skip;
865 cache += NCC;
866 if (!skip)
867 blit(blk, p0 + width_ + 8, width_);
868
869 p0 += 16;
870 if (color_) {
871 skip = huffparse(comp_[1], blk, cache);
872 cache += NCC;
873 allskip &= skip;
874 if (!skip)
875 blit(blk, p1, width_ / 2);
876 skip = huffparse(comp_[2], blk, cache);
877 cache += NCC;
878 allskip &= skip;
879 if (!skip)
880 blit(blk, p1 + width_ / 2, width_ / 2);
881 p1 += 8;
882 } else {
883 huffskip(comp_[1]);
884 huffskip(comp_[2]);
885 }
886 /* Update bounding box */
887 /* FIXME these can be locals */
888 if (!allskip) {
889 if (x < minx_)
890 minx_ = x;
891 if (x > maxx_)
892 maxx_ = x;
893 if (y < miny_)
894 miny_ = y;
895 if (y > maxy_)
896 maxy_ = y;
897 }
898 }
899 p0 -= width_;
900 p0 += 16 * width_;
901 p1 -= width_ / 2;
902 p1 += 8 * width_ / 2;
903 }
904 /*
905 * Convert mcu coords to pixel coords.
906 * A 4:1:1 mcu is 16x16.
907 */
908 minx_ <<= 4;
909 maxx_ <<= 4;
910 miny_ <<= 4;
911 maxy_ <<= 4;
912 maxx_ += 16;
913 maxy_ += 16;
914 #endif
915
916 return (0);
917 }
918
919 /* Figure F.12: extend sign bit */
920
921 #ifdef notdef
922 #define huff_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
923
924 static const int extend_test[16] = /* entry n is 2**(n-1) */
925 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
926 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
927
928 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
929 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
930 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
931 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
932 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
933 #else
934 /* is this really faster? */
935 inline int
936 huff_EXTEND(int x, int s)
937 {
938 register int b = x >> (s - 1);
939 register int m = ((b & 1) - 1) << s;
940 return ((x | m) + (~b & 1));
941 }
942 #endif
943
944 /*
945 * Read the next 16 bits off the bit string into the bit buffer.
946 * Skip over zero-stuffed ff's but make no attempt to verify
947 * that they aren't some other marker (which shouldn't be in the
948 * middle of a block anyway).
949 */
950 #define HUFFRQ(bb) \
951 { \
952 register int v; \
953 register u_char *cp = inb_; \
954 \
955 bb <<= 16; \
956 v = *cp++; \
957 if (v == 0xff) ++cp; \
958 bb |= v << 8; \
959 v = *cp++; \
960 if (v == 0xff) ++cp; \
961 bb |= v; \
962 inb_ = cp; \
963 \
964 }
965
966 #define MASK(s) ((1 << (s)) - 1)
967
968 #define HUFF_DECODE(ht, nbb, bb, result) { \
969 register int s_, v_; \
970 \
971 if (nbb < 16) { \
972 HUFFRQ(bb); \
973 nbb += 16; \
974 } \
975 v_ = (bb >> (nbb - 16)) & 0xffff; \
976 s_ = (ht)[v_]; \
977 nbb -= (s_ >> 8); \
978 result = s_ & 0xff; \
979 }
980
981 #define GET_BITS(n, nbb, bb, result) \
982 { \
983 nbb -= n; \
984 if (nbb < 0) { \
985 HUFFRQ(bb); \
986 nbb += 16; \
987 } \
988 (result) = ((bb >> nbb) & MASK(n)); \
989 }
990
991 #define SKIP_BITS(n, nbb, bb) \
992 { \
993 nbb -= n; \
994 if (nbb < 0) { \
995 HUFFRQ(bb); \
996 nbb += 16; \
997 } \
998 }
999
1000 int JpegDecoder::huffdc(component& p)
1001 {
1002 /* Decode a single block's worth of coefficients */
1003
1004 /* Section F.2.2.1: decode the DC coefficient difference */
1005 register int bb = bb_;
1006 register int nbb = nbb_;
1007 u_short* ht = dcht_[p.dc_tbl_no];
1008 register int s, r;
1009 HUFF_DECODE(ht, nbb, bb, s);
1010 if (s != 0) {
1011 GET_BITS(s, nbb, bb, r);
1012 s = huff_EXTEND(r, s);
1013 }
1014 /* Convert DC difference to actual value, update predictor */
1015 s += p.dc;
1016 p.dc = s;
1017
1018 /* Section F.2.2.2: decode the AC coefficients */
1019 ht = acht_[p.ac_tbl_no];
1020 for (register int k = 1; k < 64; ) {
1021 /* Symbol-1 */
1022 register int v;
1023 HUFF_DECODE(ht, nbb, bb, v);
1024 s = v & 15;
1025 r = v >> 4;
1026 if (s != 0) {
1027 k += r;
1028 /* Symbol-2 */
1029 SKIP_BITS(s, nbb, bb);
1030 ++k;
1031 } else {
1032 if (r != 15)
1033 /* end of block */
1034 break;
1035 k += 16;
1036 }
1037 }
1038 nbb_ = nbb;
1039 bb_ = bb;
1040
1041 return (0);
1042 }
1043
1044 /*
1045 * Parse a huffman-encoded 8x8 block. Blocks are independent
1046 * of eachother, except for the dc predictor, and blocks can
1047 * start and end on arbitrary bit boundaries. No markers should
1048 * appear in the bit stream, and ff bytes should be zero stuffed
1049 * (i.e., replaced with ff 00).
1050 *
1051 * The block is coded as a sequence of pairs of symbols. Where the
1052 * first symbol is a huffman-encoded value <r,n> where r is a four-bit
1053 * runlength and n is the length of the second symbol (of the pair),
1054 * which follows verbatim in the bit string.
1055 */
1056 int JpegDecoder::huffparse(component& p, short* blk, short* cache,
1057 u_int* mask)
1058 {
1059 register int bb = bb_;
1060 register int nbb = nbb_;
1061 u_short* ht = dcht_[p.dc_tbl_no];
1062 register int s, r;
1063 HUFF_DECODE(ht, nbb, bb, s);
1064 if (s != 0) {
1065 GET_BITS(s, nbb, bb, r);
1066 s = huff_EXTEND(r, s);
1067 }
1068
1069 /* update predictor */
1070 s += p.dc;
1071 p.dc = s;
1072 blk[0] = s;
1073
1074 #if NCC != 6
1075 @BUG@
1076 #endif
1077 u_int sblk[3];
1078 #if BYTE_ORDER == LITTLE_ENDIAN
1079 sblk[0] = s;
1080 #else
1081 sblk[0] = s << 16;
1082 #endif
1083 sblk[1] = 0;
1084 sblk[2] = 0;
1085
1086 /*
1087 * First, grab only a few low frequency coefficients.
1088 * If they aren't sufficiently different from the current
1089 * block, skip over this block quickly.
1090 */
1091 ht = acht_[p.ac_tbl_no];
1092 register int k = 1;
1093 int m0 = 0;
1094 for (;;) {
1095 register int v;
1096 /* Symbol-1 */
1097 HUFF_DECODE(ht, nbb, bb, v);
1098 s = v & 15;
1099 r = v >> 4;
1100 if (s != 0) {
1101 k += r;
1102 if (k >= 6) {
1103 k -= r;
1104 break;
1105 }
1106 /* Symbol-2 */
1107 GET_BITS(s, nbb, bb, v);
1108 s = huff_EXTEND(v, s);
1109 ((short*)sblk)[k] = s;/*FIXME*/
1110