blob: fb9400daac9f8aef5c0733d6e043fb6cfd6ebd36
1 | /*******************************************************************************# |
2 | # guvcview http://guvcview.sourceforge.net # |
3 | # # |
4 | # Paulo Assis <pj.assis@gmail.com> # |
5 | # Nobuhiro Iwamatsu <iwamatsu@nigauri.org> # |
6 | # Add UYVY color support(Macbook iSight) # |
7 | # # |
8 | # This program is free software; you can redistribute it and/or modify # |
9 | # it under the terms of the GNU General Public License as published by # |
10 | # the Free Software Foundation; either version 2 of the License, or # |
11 | # (at your option) any later version. # |
12 | # # |
13 | # This program is distributed in the hope that it will be useful, # |
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
16 | # GNU General Public License for more details. # |
17 | # # |
18 | # You should have received a copy of the GNU General Public License # |
19 | # along with this program; if not, write to the Free Software # |
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # |
21 | # # |
22 | ********************************************************************************/ |
23 | |
24 | /*******************************************************************************# |
25 | # # |
26 | # MJpeg decoding and frame capture taken from luvcview # |
27 | # # |
28 | # # |
29 | ********************************************************************************/ |
30 | |
31 | /* support for internationalization - i18n */ |
32 | //#define LOG_NDEBUG 0 |
33 | #define LOG_TAG "CameraHAL_MJPEGDecode" |
34 | //reinclude because of a bug with the log macros |
35 | #include <utils/Log.h> |
36 | #include "DebugUtils.h" |
37 | #include "jutils.h" |
38 | #include "huffman.h" |
39 | #include "colorspaces.h" |
40 | #include <linux/videodev2.h> |
41 | |
42 | /*********************************/ |
43 | |
44 | static int huffman_init(void); |
45 | |
46 | static void decode_mcus |
47 | __P((struct in *, int *, int, struct scan *, int *)); |
48 | |
49 | static int dec_readmarker __P((struct in *)); |
50 | |
51 | static void dec_makehuff |
52 | __P((struct dec_hufftbl *, int *, BYTE *)); |
53 | |
54 | static void setinput __P((struct in *, BYTE *)); |
55 | /*********************************/ |
56 | |
57 | #undef PREC |
58 | #define PREC int |
59 | |
60 | static void idctqtab __P((BYTE *, PREC *)); |
61 | |
62 | inline static void idct(int *in, int *out, int *quant, long off, int max); |
63 | |
64 | /*********************************/ |
65 | //static void col221111 __P((int *, unsigned char *, int)); |
66 | |
67 | typedef void (*ftopict) (int * out, addr *pic, int width) ; |
68 | |
69 | /*********************************/ |
70 | static BYTE *datap; |
71 | |
72 | static int getbyte(void) |
73 | { |
74 | return *datap++; |
75 | } |
76 | |
77 | static int getword(void) |
78 | { |
79 | int c1, c2; |
80 | c1 = *datap++; |
81 | c2 = *datap++; |
82 | return c1 << 8 | c2; |
83 | } |
84 | |
85 | struct comp |
86 | { |
87 | int cid; |
88 | int hv; |
89 | int tq; |
90 | }; |
91 | |
92 | #define MAXCOMP 4 |
93 | struct jpginfo |
94 | { |
95 | int nc; /* number of components */ |
96 | int ns; /* number of scans */ |
97 | int dri; /* restart interval */ |
98 | int nm; /* mcus til next marker */ |
99 | int rm; /* next restart marker */ |
100 | }; |
101 | |
102 | static struct jpginfo info; |
103 | static struct comp comps[MAXCOMP]; |
104 | |
105 | static struct scan dscans[MAXCOMP]; |
106 | |
107 | static unsigned char quant[4][64]; |
108 | |
109 | static struct dec_hufftbl dhuff[4]; |
110 | |
111 | #define dec_huffdc (dhuff + 0) |
112 | #define dec_huffac (dhuff + 2) |
113 | |
114 | static struct in in; |
115 | |
116 | /*read jpeg tables (huffman and quantization) |
117 | * args: |
118 | * till: Marker (frame - SOF0 scan - SOS) |
119 | * isDHT: flag indicating the presence of huffman tables (if 0 must use default ones - MJPG frame) |
120 | */ |
121 | static int readtables(int till, int *isDHT) |
122 | { |
123 | int m, l, i, j, lq, pq, tq; |
124 | int tc, th, tt; |
125 | |
126 | for (;;) |
127 | { |
128 | if (getbyte() != 0xff) |
129 | return -1; |
130 | if ((m = getbyte()) == till) |
131 | break; |
132 | |
133 | switch (m) |
134 | { |
135 | case 0xc2: |
136 | return 0; |
137 | /*read quantization tables (Lqt and Cqt)*/ |
138 | case M_DQT: |
139 | lq = getword(); |
140 | while (lq > 2) |
141 | { |
142 | pq = getbyte(); |
143 | /*Lqt=0x00 Cqt=0x01*/ |
144 | tq = pq & 15; |
145 | if (tq > 3) |
146 | return -1; |
147 | pq >>= 4; |
148 | if (pq != 0) |
149 | return -1; |
150 | for (i = 0; i < 64; i++) |
151 | quant[tq][i] = getbyte(); |
152 | lq -= 64 + 1; |
153 | } |
154 | break; |
155 | /*read huffman table*/ |
156 | case M_DHT: |
157 | l = getword(); |
158 | while (l > 2) |
159 | { |
160 | int hufflen[16], k; |
161 | BYTE huffvals[256]; |
162 | |
163 | tc = getbyte(); |
164 | th = tc & 15; |
165 | tc >>= 4; |
166 | tt = tc * 2 + th; |
167 | if (tc > 1 || th > 1) |
168 | return -1; |
169 | |
170 | for (i = 0; i < 16; i++) |
171 | hufflen[i] = getbyte(); |
172 | l -= 1 + 16; |
173 | k = 0; |
174 | for (i = 0; i < 16; i++) |
175 | { |
176 | for (j = 0; j < hufflen[i]; j++) |
177 | huffvals[k++] = getbyte(); |
178 | l -= hufflen[i]; |
179 | } |
180 | dec_makehuff(dhuff + tt, hufflen, huffvals); |
181 | } |
182 | /* has huffman tables defined (JPEG)*/ |
183 | *isDHT= 1; |
184 | break; |
185 | /*restart interval*/ |
186 | case M_DRI: |
187 | l = getword(); |
188 | info.dri = getword(); |
189 | break; |
190 | case 0xff: |
191 | l = getbyte(); |
192 | return 0; |
193 | default: |
194 | l = getword(); |
195 | while (l-- > 2) |
196 | getbyte(); |
197 | break; |
198 | } |
199 | } |
200 | return 0; |
201 | } |
202 | |
203 | static void dec_initscans(void) |
204 | { |
205 | int i; |
206 | |
207 | info.nm = info.dri + 1; |
208 | info.rm = M_RST0; |
209 | for (i = 0; i < info.ns; i++) |
210 | dscans[i].dc = 0; |
211 | } |
212 | |
213 | static int dec_checkmarker(void) |
214 | { |
215 | int i; |
216 | |
217 | if (dec_readmarker(&in) != info.rm) |
218 | return -1; |
219 | info.nm = info.dri; |
220 | info.rm = (info.rm + 1) & ~0x08; |
221 | for (i = 0; i < info.ns; i++) |
222 | dscans[i].dc = 0; |
223 | return 0; |
224 | } |
225 | |
226 | /*jpeg decode |
227 | * args: |
228 | * pic: pointer to picture data ( decoded image - yuyv format) |
229 | * buf: pointer to input data ( compressed jpeg ) |
230 | * with: picture width |
231 | * height: picture height |
232 | */ |
233 | int jpeg_decode(BYTE **pic, BYTE *buf, int width, int height, unsigned int outformat) |
234 | { |
235 | struct jpeg_decdata *decdata; |
236 | int i=0, j=0, m=0, tac=0, tdc=0; |
237 | int intwidth=0, intheight=0; |
238 | int mcusx=0, mcusy=0, mx=0, my=0; |
239 | int ypitch=0 ,xpitch=0,bpp=0,pitch=0,x=0,y=0; |
240 | int mb=0; |
241 | int max[6]; |
242 | ftopict convert; |
243 | int err = 0; |
244 | int isInitHuffman = 0; |
245 | int mb_x,mb_y; |
246 | int mc_x,mc_y; |
247 | int down_sampling = 1; |
248 | decdata = (struct jpeg_decdata *)malloc(sizeof(struct jpeg_decdata)); |
249 | memset(&info,0x0,sizeof(info)); |
250 | for(i=0;i<6;i++) |
251 | max[i]=0; |
252 | |
253 | if (!decdata) |
254 | { |
255 | err = -1; |
256 | goto error; |
257 | } |
258 | if ((buf == NULL)||(*pic == NULL)) |
259 | { |
260 | err = -1; |
261 | goto error; |
262 | } |
263 | datap = buf; |
264 | /*check SOI (0xFFD8)*/ |
265 | if (getbyte() != 0xff) |
266 | { |
267 | err = ERR_NO_SOI; |
268 | goto error; |
269 | } |
270 | if (getbyte() != M_SOI) |
271 | { |
272 | err = ERR_NO_SOI; |
273 | goto error; |
274 | } |
275 | /*read tables - if exist, up to start frame marker (0xFFC0)*/ |
276 | if (readtables(M_SOF0, &isInitHuffman)) |
277 | { |
278 | err = ERR_BAD_TABLES; |
279 | goto error; |
280 | } |
281 | getword(); /*header lenght*/ |
282 | i = getbyte(); /*precision (8 bit)*/ |
283 | if (i != 8) |
284 | { |
285 | err = ERR_NOT_8BIT; |
286 | goto error; |
287 | } |
288 | intheight = getword(); /*height*/ |
289 | intwidth = getword(); /*width */ |
290 | if ((intheight & 7) || (intwidth & 7)) /*must be even*/ |
291 | { |
292 | err = ERR_BAD_WIDTH_OR_HEIGHT; |
293 | goto error; |
294 | } |
295 | info.nc = getbyte(); /*number of components*/ |
296 | if (info.nc > MAXCOMP) |
297 | { |
298 | err = ERR_TOO_MANY_COMPPS; |
299 | goto error; |
300 | } |
301 | /*for each component*/ |
302 | for (i = 0; i < info.nc; i++) |
303 | { |
304 | int h, v; |
305 | comps[i].cid = getbyte(); /*component id*/ |
306 | comps[i].hv = getbyte(); |
307 | v = comps[i].hv & 15; /*vertical sampling */ |
308 | h = comps[i].hv >> 4; /*horizontal sampling */ |
309 | comps[i].tq = getbyte(); /*quantization table used*/ |
310 | if (h > 3 || v > 3) |
311 | { |
312 | err = ERR_ILLEGAL_HV; |
313 | goto error; |
314 | } |
315 | if (comps[i].tq > 3) |
316 | { |
317 | err = ERR_QUANT_TABLE_SELECTOR; |
318 | goto error; |
319 | } |
320 | } |
321 | /*read tables - if exist, up to start of scan marker (0xFFDA)*/ |
322 | if (readtables(M_SOS,&isInitHuffman)) |
323 | { |
324 | err = ERR_BAD_TABLES; |
325 | goto error; |
326 | } |
327 | getword(); /* header lenght */ |
328 | info.ns = getbyte(); /* number of scans */ |
329 | if (!info.ns) |
330 | { |
331 | err = ERR_NOT_YCBCR_221111; |
332 | goto error; |
333 | } |
334 | /*for each scan*/ |
335 | for (i = 0; i < info.ns; i++) |
336 | { |
337 | dscans[i].cid = getbyte(); /*component id*/ |
338 | tdc = getbyte(); |
339 | tac = tdc & 15; /*ac table*/ |
340 | tdc >>= 4; /*dc table*/ |
341 | if (tdc > 1 || tac > 1) |
342 | { |
343 | err = ERR_QUANT_TABLE_SELECTOR; |
344 | goto error; |
345 | } |
346 | for (j = 0; j < info.nc; j++) |
347 | if (comps[j].cid == dscans[i].cid) |
348 | break; |
349 | if (j == info.nc) |
350 | { |
351 | err = ERR_UNKNOWN_CID_IN_SCAN; |
352 | goto error; |
353 | } |
354 | dscans[i].hv = comps[j].hv; |
355 | dscans[i].tq = comps[j].tq; |
356 | dscans[i].hudc.dhuff = dec_huffdc + tdc; |
357 | dscans[i].huac.dhuff = dec_huffac + tac; |
358 | } |
359 | |
360 | i = getbyte(); /*0 */ |
361 | j = getbyte(); /*63*/ |
362 | m = getbyte(); /*0 */ |
363 | |
364 | if (i != 0 || j != 63 || m != 0) |
365 | { |
366 | CAMHAL_LOGDA("hmm FW error,not seq DCT ??\n"); |
367 | } |
368 | |
369 | /*build huffman tables*/ |
370 | if(!isInitHuffman) |
371 | { |
372 | if(huffman_init() < 0) |
373 | return -ERR_BAD_TABLES; |
374 | } |
375 | /* |
376 | if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3) |
377 | { |
378 | err = ERR_NOT_YCBCR_221111; |
379 | goto error; |
380 | } |
381 | |
382 | if (dscans[1].hv != 0x11 || dscans[2].hv != 0x11) |
383 | { |
384 | err = ERR_NOT_YCBCR_221111; |
385 | goto error; |
386 | } |
387 | */ |
388 | /* if internal width and external are not the same or heigth too |
389 | and pic not allocated realloc the good size and mark the change |
390 | need 1 macroblock line more ?? */ |
391 | if (intwidth != width) |
392 | { |
393 | err = ERR_WIDTH_MISMATCH; |
394 | goto error; |
395 | } |
396 | |
397 | if (intheight != height) |
398 | { |
399 | err = ERR_HEIGHT_MISMATCH; |
400 | goto error; |
401 | } |
402 | |
403 | switch (dscans[0].hv) |
404 | { |
405 | case 0x22: // 411 |
406 | mb=6; |
407 | mcusx = width >> 4; |
408 | mcusy = height >> 4; |
409 | bpp=2; |
410 | xpitch = 16 * bpp; |
411 | pitch = width * bpp; // YUYV out |
412 | ypitch = 16 * pitch; |
413 | mb_x = 16; |
414 | mb_y = 16; |
415 | mc_y = 8; |
416 | if(outformat == V4L2_PIX_FMT_NV21){ |
417 | convert = yuv420pto420sp; //choose the right conversion function |
418 | mc_x = 16; |
419 | down_sampling = 1; |
420 | }else{ |
421 | convert = yuv420pto420p; |
422 | mc_x = 8; |
423 | down_sampling = 2; |
424 | } |
425 | break; |
426 | case 0x21: //422 |
427 | mb=4; |
428 | mcusx = width >> 4; |
429 | mcusy = height >> 3; |
430 | bpp=2; |
431 | xpitch = 16 * bpp; |
432 | pitch = width * bpp; // YUYV out |
433 | ypitch = 8 * pitch; |
434 | mb_x = 16; |
435 | mb_y = 8; |
436 | mc_y = 8; |
437 | if(outformat == V4L2_PIX_FMT_NV21){ |
438 | convert = yuv422pto420sp; //choose the right conversion function |
439 | mc_x = 16; |
440 | down_sampling = 2; |
441 | }else{ |
442 | convert = yuv422pto420p; |
443 | mc_x = 8; |
444 | down_sampling = 4; |
445 | } |
446 | break; |
447 | case 0x11: //444 |
448 | mcusx = width >> 3; |
449 | mcusy = height >> 3; |
450 | bpp=2; |
451 | xpitch = 8 * bpp; |
452 | pitch = width * bpp; // YUYV out |
453 | ypitch = 8 * pitch; |
454 | if (info.ns==1) |
455 | { |
456 | mb = 1; |
457 | //convert = yuv400pto422; //choose the right conversion function |
458 | CAMHAL_LOGEA("Format YUV400 Not Supproted"); |
459 | } |
460 | else |
461 | { |
462 | mb=3; |
463 | //convert = yuv444pto422; //choose the right conversion function |
464 | CAMHAL_LOGEA("Format YUV444 Not Supproted"); |
465 | } |
466 | err = ERR_NOT_SUPPORTED; |
467 | goto error; |
468 | break; |
469 | default: |
470 | err = ERR_NOT_YCBCR_221111; |
471 | goto error; |
472 | break; |
473 | } |
474 | |
475 | addr paddr; |
476 | idctqtab(quant[dscans[0].tq], decdata->dquant[0]); |
477 | idctqtab(quant[dscans[1].tq], decdata->dquant[1]); |
478 | idctqtab(quant[dscans[2].tq], decdata->dquant[2]); |
479 | setinput(&in, datap); |
480 | dec_initscans(); |
481 | |
482 | dscans[0].next = 2; |
483 | dscans[1].next = 1; |
484 | dscans[2].next = 0; /* 4xx encoding */ |
485 | for (my = 0,y=0; my < mcusy; my++,y+=ypitch) |
486 | { |
487 | for (mx = 0,x=0; mx < mcusx; mx++,x+=xpitch) |
488 | { |
489 | if (info.dri && !--info.nm){ |
490 | if (dec_checkmarker()) |
491 | { |
492 | err = ERR_WRONG_MARKER; |
493 | goto error; |
494 | } |
495 | } |
496 | switch (mb) |
497 | { |
498 | case 6: |
499 | decode_mcus(&in, decdata->dcts, mb, dscans, max); |
500 | idct(decdata->dcts, decdata->out, decdata->dquant[0], |
501 | IFIX(128.5), max[0]); |
502 | idct(decdata->dcts + 64, decdata->out + 64, |
503 | decdata->dquant[0], IFIX(128.5), max[1]); |
504 | idct(decdata->dcts + 128, decdata->out + 128, |
505 | decdata->dquant[0], IFIX(128.5), max[2]); |
506 | idct(decdata->dcts + 192, decdata->out + 192, |
507 | decdata->dquant[0], IFIX(128.5), max[3]); |
508 | idct(decdata->dcts + 256, decdata->out + 256, |
509 | decdata->dquant[1], IFIX(0.5), max[4]); |
510 | idct(decdata->dcts + 320, decdata->out + 320, |
511 | decdata->dquant[2], IFIX(0.5), max[5]); |
512 | break; |
513 | |
514 | case 4: |
515 | decode_mcus(&in, decdata->dcts, mb, dscans, max); |
516 | idct(decdata->dcts, decdata->out, decdata->dquant[0], |
517 | IFIX(128.5), max[0]); |
518 | idct(decdata->dcts + 64, decdata->out + 64, |
519 | decdata->dquant[0], IFIX(128.5), max[1]); |
520 | idct(decdata->dcts + 128, decdata->out + 256, |
521 | decdata->dquant[1], IFIX(0.5), max[4]); |
522 | idct(decdata->dcts + 192, decdata->out + 320, |
523 | decdata->dquant[2], IFIX(0.5), max[5]); |
524 | break; |
525 | |
526 | case 3: |
527 | decode_mcus(&in, decdata->dcts, mb, dscans, max); |
528 | idct(decdata->dcts, decdata->out, decdata->dquant[0], |
529 | IFIX(128.5), max[0]); |
530 | idct(decdata->dcts + 64, decdata->out + 256, |
531 | decdata->dquant[1], IFIX(0.5), max[4]); |
532 | idct(decdata->dcts + 128, decdata->out + 320, |
533 | decdata->dquant[2], IFIX(0.5), max[5]); |
534 | break; |
535 | |
536 | case 1: |
537 | decode_mcus(&in, decdata->dcts, mb, dscans, max); |
538 | idct(decdata->dcts, decdata->out, decdata->dquant[0], |
539 | IFIX(128.5), max[0]); |
540 | break; |
541 | } // switch enc411 |
542 | |
543 | paddr.y = *pic + my * width * mb_y + mx * mb_x; |
544 | paddr.v = *pic + width * height + my * width * mc_y / down_sampling + mx * mc_x; |
545 | paddr.u = *pic + width * height*5/4 + my * width * mc_y / down_sampling + mx * mc_x; |
546 | convert(decdata->out,&paddr,width); |
547 | } |
548 | } |
549 | #if 0 |
550 | m = dec_readmarker(&in); |
551 | if (m != M_EOI) |
552 | { |
553 | err = ERR_NO_EOI; |
554 | goto error; |
555 | } |
556 | #endif |
557 | free(decdata); |
558 | return 0; |
559 | error: |
560 | CAMHAL_LOGDB("decode failed:%d",err); |
561 | free(decdata); |
562 | return err; |
563 | } |
564 | |
565 | /****************************************************************/ |
566 | /************** huffman decoder ***************/ |
567 | /****************************************************************/ |
568 | static int huffman_init(void) |
569 | { |
570 | int tc, th, tt; |
571 | unsigned char *ptr= (unsigned char *) JPEGHuffmanTable ; |
572 | int i, j, l; |
573 | l = JPG_HUFFMAN_TABLE_LENGTH ; |
574 | while (l > 0) |
575 | { |
576 | int hufflen[16], k; |
577 | unsigned char huffvals[256]; |
578 | |
579 | tc = *ptr++; |
580 | th = tc & 15; |
581 | tc >>= 4; |
582 | tt = tc * 2 + th; |
583 | if (tc > 1 || th > 1) |
584 | return -ERR_BAD_TABLES; |
585 | for (i = 0; i < 16; i++) |
586 | hufflen[i] = *ptr++; |
587 | l -= 1 + 16; |
588 | k = 0; |
589 | for (i = 0; i < 16; i++) |
590 | { |
591 | for (j = 0; j < hufflen[i]; j++) |
592 | huffvals[k++] = *ptr++; |
593 | l -= hufflen[i]; |
594 | } |
595 | dec_makehuff(dhuff + tt, hufflen, huffvals); |
596 | } |
597 | return 0; |
598 | } |
599 | |
600 | static int fillbits __P((struct in *, int, unsigned int)); |
601 | static int dec_rec2 |
602 | __P((struct in *, struct dec_hufftbl *, int *, int, int)); |
603 | |
604 | static void setinput(in, p) |
605 | struct in *in; |
606 | unsigned char *p; |
607 | { |
608 | in->p = p; |
609 | in->left = 0; |
610 | in->bits = 0; |
611 | in->marker = 0; |
612 | } |
613 | |
614 | static int fillbits(in, le, bi) |
615 | struct in *in; |
616 | int le; |
617 | unsigned int bi; |
618 | { |
619 | int b, m; |
620 | |
621 | if (in->marker) |
622 | { |
623 | if (le <= 16) |
624 | in->bits = bi << 16, le += 16; |
625 | return le; |
626 | } |
627 | while (le <= 24) |
628 | { |
629 | b = *in->p++; |
630 | if (b == 0xff && (m = *in->p++) != 0) |
631 | { |
632 | if (m == M_EOF) |
633 | { |
634 | if (in->func && (m = in->func(in->data)) == 0) |
635 | continue; |
636 | } |
637 | in->marker = m; |
638 | if (le <= 16) |
639 | bi = bi << 16, le += 16; |
640 | break; |
641 | } |
642 | bi = bi << 8 | b; |
643 | le += 8; |
644 | } |
645 | in->bits = bi; /* tmp... 2 return values needed */ |
646 | return le; |
647 | } |
648 | |
649 | static int dec_readmarker(in) |
650 | struct in *in; |
651 | { |
652 | int m; |
653 | |
654 | in->left = fillbits(in, in->left, in->bits); |
655 | if ((m = in->marker) == 0) |
656 | return 0; |
657 | in->left = 0; |
658 | in->marker = 0; |
659 | return m; |
660 | } |
661 | |
662 | #define LEBI_DCL int le, bi |
663 | #define LEBI_GET(in) (le = in->left, bi = in->bits) |
664 | #define LEBI_PUT(in) (in->left = le, in->bits = bi) |
665 | |
666 | #define GETBITS(in, n) ( \ |
667 | (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \ |
668 | (le -= (n)), \ |
669 | bi >> le & ((1 << (n)) - 1) \ |
670 | ) |
671 | |
672 | #define UNGETBITS(in, n) ( \ |
673 | le += (n) \ |
674 | ) |
675 | |
676 | |
677 | static int dec_rec2(in, hu, runp, c, i) |
678 | struct in *in; |
679 | struct dec_hufftbl *hu; |
680 | int *runp; |
681 | int c, i; |
682 | { |
683 | LEBI_DCL; |
684 | |
685 | LEBI_GET(in); |
686 | if (i) |
687 | { |
688 | UNGETBITS(in, i & 127); |
689 | *runp = i >> 8 & 15; |
690 | i >>= 16; |
691 | } |
692 | else |
693 | { |
694 | for (i = DECBITS; |
695 | (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++); |
696 | if (i >= 16) |
697 | { |
698 | in->marker = M_BADHUFF; |
699 | return 0; |
700 | } |
701 | i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2]; |
702 | *runp = i >> 4; |
703 | i &= 15; |
704 | } |
705 | if (i == 0) |
706 | { /* sigh, 0xf0 is 11 bit */ |
707 | LEBI_PUT(in); |
708 | return 0; |
709 | } |
710 | /* receive part */ |
711 | c = GETBITS(in, i); |
712 | if (c < (1 << (i - 1))) |
713 | c += (-1 << i) + 1; |
714 | LEBI_PUT(in); |
715 | return c; |
716 | } |
717 | |
718 | #define DEC_REC(in, hu, r, i) ( \ |
719 | r = GETBITS(in, DECBITS), \ |
720 | i = hu->llvals[r], \ |
721 | i & 128 ? \ |
722 | ( \ |
723 | UNGETBITS(in, i & 127), \ |
724 | r = i >> 8 & 15, \ |
725 | i >> 16 \ |
726 | ) \ |
727 | : \ |
728 | ( \ |
729 | LEBI_PUT(in), \ |
730 | i = dec_rec2(in, hu, &r, r, i), \ |
731 | LEBI_GET(in), \ |
732 | i \ |
733 | ) \ |
734 | ) |
735 | |
736 | static void decode_mcus(in, dct, n, sc, maxp) |
737 | struct in *in; |
738 | int *dct; |
739 | int n; |
740 | struct scan *sc; |
741 | int *maxp; |
742 | { |
743 | struct dec_hufftbl *hu; |
744 | int i = 0, r = 0, t = 0; |
745 | LEBI_DCL; |
746 | |
747 | memset(dct, 0, n * 64 * sizeof(*dct)); |
748 | LEBI_GET(in); |
749 | while (n-- > 0) |
750 | { |
751 | hu = sc->hudc.dhuff; |
752 | *dct++ = (sc->dc += DEC_REC(in, hu, r, t)); |
753 | |
754 | hu = sc->huac.dhuff; |
755 | i = 63; |
756 | while (i > 0) |
757 | { |
758 | t = DEC_REC(in, hu, r, t); |
759 | if (t == 0 && r == 0) |
760 | { |
761 | dct += i; |
762 | break; |
763 | } |
764 | dct += r; |
765 | *dct++ = t; |
766 | i -= r + 1; |
767 | } |
768 | *maxp++ = 64 - i; |
769 | if (n == sc->next) |
770 | sc++; |
771 | } |
772 | LEBI_PUT(in); |
773 | } |
774 | |
775 | static void dec_makehuff(hu, hufflen, huffvals) |
776 | struct dec_hufftbl *hu; |
777 | int *hufflen; |
778 | unsigned char *huffvals; |
779 | { |
780 | int code, k, i, j, d, x, c, v; |
781 | for (i = 0; i < (1 << DECBITS); i++) |
782 | hu->llvals[i] = 0; |
783 | |
784 | /* |
785 | * llvals layout: |
786 | * |
787 | * value v already known, run r, backup u bits: |
788 | * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu |
789 | * value unknown, size b bits, run r, backup u bits: |
790 | * 000000000000bbbb 0000 rrrr 0 uuuuuuu |
791 | * value and size unknown: |
792 | * 0000000000000000 0000 0000 0 0000000 |
793 | */ |
794 | code = 0; |
795 | k = 0; |
796 | for (i = 0; i < 16; i++, code <<= 1) |
797 | { /* sizes */ |
798 | hu->valptr[i] = k; |
799 | for (j = 0; j < hufflen[i]; j++) |
800 | { |
801 | hu->vals[k] = *huffvals++; |
802 | if (i < DECBITS) |
803 | { |
804 | c = code << (DECBITS - 1 - i); |
805 | v = hu->vals[k] & 0x0f; /* size */ |
806 | for (d = 1 << (DECBITS - 1 - i); --d >= 0;) |
807 | { |
808 | if (v + i < DECBITS) |
809 | { /* both fit in table */ |
810 | x = d >> (DECBITS - 1 - v - i); |
811 | if (v && x < (1 << (v - 1))) |
812 | x += (-1 << v) + 1; |
813 | x = x << 16 | (hu->vals[k] & 0xf0) << 4 | |
814 | (DECBITS - (i + 1 + v)) | 128; |
815 | } |
816 | else |
817 | x = v << 16 | (hu->vals[k] & 0xf0) << 4 | |
818 | (DECBITS - (i + 1)); |
819 | hu->llvals[c | d] = x; |
820 | } |
821 | } |
822 | code++; |
823 | k++; |
824 | } |
825 | hu->maxcode[i] = code; |
826 | } |
827 | hu->maxcode[16] = 0x20000; /* always terminate decode */ |
828 | } |
829 | |
830 | /****************************************************************/ |
831 | /************** idct ***************/ |
832 | /****************************************************************/ |
833 | |
834 | #define IMULT(a, b) (((a) * (b)) >> ISHIFT) |
835 | #define ITOINT(a) ((a) >> ISHIFT) |
836 | |
837 | #define S22 ((PREC)IFIX(2 * 0.382683432)) |
838 | #define C22 ((PREC)IFIX(2 * 0.923879532)) |
839 | #define IC4 ((PREC)IFIX(1 / 0.707106781)) |
840 | |
841 | //zigzag order used by idct |
842 | static unsigned char zig2[64] = { |
843 | 0, 2, 3, 9, 10, 20, 21, 35, |
844 | 14, 16, 25, 31, 39, 46, 50, 57, |
845 | 5, 7, 12, 18, 23, 33, 37, 48, |
846 | 27, 29, 41, 44, 52, 55, 59, 62, |
847 | 15, 26, 30, 40, 45, 51, 56, 58, |
848 | 1, 4, 8, 11, 19, 22, 34, 36, |
849 | 28, 42, 43, 53, 54, 60, 61, 63, |
850 | 6, 13, 17, 24, 32, 38, 47, 49 |
851 | }; |
852 | |
853 | /*inverse dct for jpeg decoding |
854 | * args: |
855 | * in: pointer to input data ( mcu - after huffman decoding) |
856 | * out: pointer to data with output of idct (to be filled) |
857 | * quant: pointer to quantization data tables |
858 | * off: offset value (128.5 or 0.5) |
859 | * max: maximum input mcu index? |
860 | */ |
861 | inline static void idct(int *in, int *out, int *quant, long off, int max) |
862 | { |
863 | long t0, t1, t2, t3, t4, t5, t6, t7; // t ; |
864 | long tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; |
865 | long tmp[64], *tmpp; |
866 | int i, j, te; |
867 | unsigned char *zig2p; |
868 | |
869 | t0 = off; |
870 | if (max == 1) //single color mcu |
871 | { |
872 | t0 += in[0] * quant[0]; //only DC available |
873 | for (i = 0; i < 64; i++) // fill mcu with DC value |
874 | out[i] = ITOINT(t0); |
875 | return; |
876 | } |
877 | zig2p = zig2; |
878 | tmpp = tmp; |
879 | for (i = 0; i < 8; i++) //apply quantization table in zigzag order |
880 | { |
881 | j = *zig2p++; |
882 | t0 += in[j] * (long) quant[j]; |
883 | j = *zig2p++; |
884 | t5 = in[j] * (long) quant[j]; |
885 | j = *zig2p++; |
886 | t2 = in[j] * (long) quant[j]; |
887 | j = *zig2p++; |
888 | t7 = in[j] * (long) quant[j]; |
889 | j = *zig2p++; |
890 | t1 = in[j] * (long) quant[j]; |
891 | j = *zig2p++; |
892 | t4 = in[j] * (long) quant[j]; |
893 | j = *zig2p++; |
894 | t3 = in[j] * (long) quant[j]; |
895 | j = *zig2p++; |
896 | t6 = in[j] * (long) quant[j]; |
897 | |
898 | |
899 | if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) |
900 | { |
901 | tmpp[0 * 8] = t0; //DC |
902 | tmpp[1 * 8] = t0; |
903 | tmpp[2 * 8] = t0; |
904 | tmpp[3 * 8] = t0; |
905 | tmpp[4 * 8] = t0; |
906 | tmpp[5 * 8] = t0; |
907 | tmpp[6 * 8] = t0; |
908 | tmpp[7 * 8] = t0; |
909 | |
910 | tmpp++; |
911 | t0 = 0; |
912 | continue; |
913 | } |
914 | //IDCT; |
915 | tmp0 = t0 + t1; |
916 | t1 = t0 - t1; |
917 | tmp2 = t2 - t3; |
918 | t3 = t2 + t3; |
919 | tmp2 = IMULT(tmp2, IC4) - t3; |
920 | tmp3 = tmp0 + t3; |
921 | t3 = tmp0 - t3; |
922 | tmp1 = t1 + tmp2; |
923 | tmp2 = t1 - tmp2; |
924 | tmp4 = t4 - t7; |
925 | t7 = t4 + t7; |
926 | tmp5 = t5 + t6; |
927 | t6 = t5 - t6; |
928 | tmp6 = tmp5 - t7; |
929 | t7 = tmp5 + t7; |
930 | tmp5 = IMULT(tmp6, IC4); |
931 | tmp6 = IMULT((tmp4 + t6), S22); |
932 | tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6; |
933 | t6 = IMULT(t6, (C22 + S22)) - tmp6; |
934 | t6 = t6 - t7; |
935 | t5 = tmp5 - t6; |
936 | t4 = tmp4 - t5; |
937 | |
938 | tmpp[0 * 8] = tmp3 + t7; //t0; |
939 | tmpp[1 * 8] = tmp1 + t6; //t1; |
940 | tmpp[2 * 8] = tmp2 + t5; //t2; |
941 | tmpp[3 * 8] = t3 + t4; //t3; |
942 | tmpp[4 * 8] = t3 - t4; //t4; |
943 | tmpp[5 * 8] = tmp2 - t5; //t5; |
944 | tmpp[6 * 8] = tmp1 - t6; //t6; |
945 | tmpp[7 * 8] = tmp3 - t7; //t7; |
946 | tmpp++; |
947 | t0 = 0; |
948 | } |
949 | for (i = 0, j = 0; i < 8; i++) |
950 | { |
951 | t0 = tmp[j + 0]; |
952 | t1 = tmp[j + 1]; |
953 | t2 = tmp[j + 2]; |
954 | t3 = tmp[j + 3]; |
955 | t4 = tmp[j + 4]; |
956 | t5 = tmp[j + 5]; |
957 | t6 = tmp[j + 6]; |
958 | t7 = tmp[j + 7]; |
959 | if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) |
960 | { |
961 | te = ITOINT(t0); |
962 | out[j + 0] = te; |
963 | out[j + 1] = te; |
964 | out[j + 2] = te; |
965 | out[j + 3] = te; |
966 | out[j + 4] = te; |
967 | out[j + 5] = te; |
968 | out[j + 6] = te; |
969 | out[j + 7] = te; |
970 | j += 8; |
971 | continue; |
972 | } |
973 | //IDCT; |
974 | tmp0 = t0 + t1; |
975 | t1 = t0 - t1; |
976 | tmp2 = t2 - t3; |
977 | t3 = t2 + t3; |
978 | tmp2 = IMULT(tmp2, IC4) - t3; |
979 | tmp3 = tmp0 + t3; |
980 | t3 = tmp0 - t3; |
981 | tmp1 = t1 + tmp2; |
982 | tmp2 = t1 - tmp2; |
983 | tmp4 = t4 - t7; |
984 | t7 = t4 + t7; |
985 | tmp5 = t5 + t6; |
986 | t6 = t5 - t6; |
987 | tmp6 = tmp5 - t7; |
988 | t7 = tmp5 + t7; |
989 | tmp5 = IMULT(tmp6, IC4); |
990 | tmp6 = IMULT((tmp4 + t6), S22); |
991 | tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6; |
992 | t6 = IMULT(t6, (C22 + S22)) - tmp6; |
993 | t6 = t6 - t7; |
994 | t5 = tmp5 - t6; |
995 | t4 = tmp4 - t5; |
996 | |
997 | out[j + 0] = ITOINT(tmp3 + t7); |
998 | out[j + 1] = ITOINT(tmp1 + t6); |
999 | out[j + 2] = ITOINT(tmp2 + t5); |
1000 | out[j + 3] = ITOINT(t3 + t4); |
1001 | out[j + 4] = ITOINT(t3 - t4); |
1002 | out[j + 5] = ITOINT(tmp2 - t5); |
1003 | out[j + 6] = ITOINT(tmp1 - t6); |
1004 | out[j + 7] = ITOINT(tmp3 - t7); |
1005 | j += 8; |
1006 | } |
1007 | } |
1008 | |
1009 | static unsigned char zig[64] = { |
1010 | 0, 1, 5, 6, 14, 15, 27, 28, |
1011 | 2, 4, 7, 13, 16, 26, 29, 42, |
1012 | 3, 8, 12, 17, 25, 30, 41, 43, |
1013 | 9, 11, 18, 24, 31, 40, 44, 53, |
1014 | 10, 19, 23, 32, 39, 45, 52, 54, |
1015 | 20, 22, 33, 38, 46, 51, 55, 60, |
1016 | 21, 34, 37, 47, 50, 56, 59, 61, |
1017 | 35, 36, 48, 49, 57, 58, 62, 63 |
1018 | }; |
1019 | |
1020 | //coef used in idct |
1021 | static PREC aaidct[8] = { |
1022 | IFIX(0.3535533906), IFIX(0.4903926402), |
1023 | IFIX(0.4619397663), IFIX(0.4157348062), |
1024 | IFIX(0.3535533906), IFIX(0.2777851165), |
1025 | IFIX(0.1913417162), IFIX(0.0975451610) |
1026 | }; |
1027 | |
1028 | |
1029 | static void idctqtab(qin, qout) |
1030 | unsigned char *qin; |
1031 | PREC *qout; |
1032 | { |
1033 | int i, j; |
1034 | |
1035 | for (i = 0; i < 8; i++) |
1036 | for (j = 0; j < 8; j++) |
1037 | qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] * |
1038 | IMULT(aaidct[i], aaidct[j]); |
1039 | } |
1040 | |
1041 |