1 /* SPDX-License-Identifier: GPL-2.0-only */
6 * written in August 2001 by Michael Schroeder <mls@suse.de>
10 #define __LITTLE_ENDIAN
15 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
16 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
17 #define ITOINT(a) ((a) >> ISHIFT)
33 int (*func
) __P((void *));
37 /*********************************/
42 struct dec_hufftbl
*dhuff
;
43 struct enc_hufftbl
*ehuff
;
47 int dc
; /* old dc value */
51 int next
; /* when to switch to next scan */
53 int cid
; /* component id */
54 int hv
; /* horiz/vert, copied from comp */
55 int tq
; /* quant tbl, copied from comp */
58 /*********************************/
60 #define DECBITS 10 /* seems to be the optimum */
65 unsigned char vals
[256];
66 unsigned int llvals
[1 << DECBITS
];
69 static void decode_mcus
__P((struct in
*, int *, int, struct scan
*, int *));
70 static int dec_readmarker
__P((struct in
*));
71 static void dec_makehuff
__P((struct dec_hufftbl
*, int *, unsigned char *));
73 static void setinput
__P((struct in
*, unsigned char *));
74 /*********************************/
79 static void idctqtab
__P((unsigned char *, PREC
*));
80 static void idct
__P((int *, int *, PREC
*, PREC
, int));
81 static void scaleidctqtab
__P((PREC
*, PREC
));
83 /*********************************/
85 static void initcol
__P((PREC
[][64]));
87 static void col221111
__P((int *, unsigned char *, int));
88 static void col221111_16
__P((int *, unsigned char *, int));
89 static void col221111_32
__P((int *, unsigned char *, int));
91 /*********************************/
104 static unsigned char *datap
;
106 static int getbyte(void)
111 static int getword(void)
127 int nc
; /* number of components */
128 int ns
; /* number of scans */
129 int dri
; /* restart interval */
130 int nm
; /* mcus til next marker */
131 int rm
; /* next restart marker */
134 static struct jpginfo info
;
135 static struct comp comps
[MAXCOMP
];
137 static struct scan dscans
[MAXCOMP
];
139 static unsigned char quant
[4][64];
141 static struct dec_hufftbl dhuff
[4];
143 #define dec_huffdc (dhuff + 0)
144 #define dec_huffac (dhuff + 2)
146 static struct in glob_in
;
148 static int readtables(int till
)
150 int m
, l
, i
, j
, lq
, pq
, tq
;
154 if (getbyte() != 0xff)
174 for (i
= 0; i
< 64; i
++)
175 quant
[tq
][i
] = getbyte();
184 unsigned char huffvals
[256];
190 if (tc
> 1 || th
> 1)
192 for (i
= 0; i
< 16; i
++)
193 hufflen
[i
] = getbyte();
196 for (i
= 0; i
< 16; i
++) {
197 for (j
= 0; j
< hufflen
[i
]; j
++)
198 huffvals
[k
++] = getbyte();
201 dec_makehuff(dhuff
+ tt
, hufflen
,
208 info
.dri
= getword();
221 static void dec_initscans(void)
225 info
.nm
= info
.dri
+ 1;
227 for (i
= 0; i
< info
.ns
; i
++)
231 static int dec_checkmarker(void)
235 if (dec_readmarker(&glob_in
) != info
.rm
)
238 info
.rm
= (info
.rm
+ 1) & ~0x08;
239 for (i
= 0; i
< info
.ns
; i
++)
244 void jpeg_fetch_size(unsigned char *buf
, int *width
, int *height
)
256 int jpeg_check_size(unsigned char *buf
, int width
, int height
)
264 if (height
!= getword() || width
!= getword())
269 int jpeg_decode(unsigned char *buf
, unsigned char *pic
,
270 int width
, int height
, int depth
, struct jpeg_decdata
*decdata
)
272 int i
, j
, m
, tac
, tdc
;
273 int mcusx
, mcusy
, mx
, my
;
276 if (!decdata
|| !buf
|| !pic
)
279 if (getbyte() != 0xff)
281 if (getbyte() != M_SOI
)
283 if (readtables(M_SOF0
))
284 return ERR_BAD_TABLES
;
289 if (((getword() + 15) & ~15) != height
)
290 return ERR_HEIGHT_MISMATCH
;
291 if (((getword() + 15) & ~15) != width
)
292 return ERR_WIDTH_MISMATCH
;
293 if ((height
& 15) || (width
& 15))
294 return ERR_BAD_WIDTH_OR_HEIGHT
;
296 if (info
.nc
> MAXCOMP
)
297 return ERR_TOO_MANY_COMPPS
;
298 for (i
= 0; i
< info
.nc
; i
++) {
300 comps
[i
].cid
= getbyte();
301 comps
[i
].hv
= getbyte();
302 v
= comps
[i
].hv
& 15;
303 h
= comps
[i
].hv
>> 4;
304 comps
[i
].tq
= getbyte();
306 return ERR_ILLEGAL_HV
;
308 return ERR_QUANT_TABLE_SELECTOR
;
310 if (readtables(M_SOS
))
311 return ERR_BAD_TABLES
;
315 return ERR_NOT_YCBCR_221111
;
316 for (i
= 0; i
< 3; i
++) {
317 dscans
[i
].cid
= getbyte();
321 if (tdc
> 1 || tac
> 1)
322 return ERR_QUANT_TABLE_SELECTOR
;
323 for (j
= 0; j
< info
.nc
; j
++)
324 if (comps
[j
].cid
== dscans
[i
].cid
)
327 return ERR_UNKNOWN_CID_IN_SCAN
;
328 dscans
[i
].hv
= comps
[j
].hv
;
329 dscans
[i
].tq
= comps
[j
].tq
;
330 dscans
[i
].hudc
.dhuff
= dec_huffdc
+ tdc
;
331 dscans
[i
].huac
.dhuff
= dec_huffac
+ tac
;
338 if (i
!= 0 || j
!= 63 || m
!= 0)
339 return ERR_NOT_SEQUENTIAL_DCT
;
341 if (dscans
[0].cid
!= 1 || dscans
[1].cid
!= 2 || dscans
[2].cid
!= 3)
342 return ERR_NOT_YCBCR_221111
;
344 if (dscans
[0].hv
!= 0x22 || dscans
[1].hv
!= 0x11
345 || dscans
[2].hv
!= 0x11)
346 return ERR_NOT_YCBCR_221111
;
352 idctqtab(quant
[dscans
[0].tq
], decdata
->dquant
[0]);
353 idctqtab(quant
[dscans
[1].tq
], decdata
->dquant
[1]);
354 idctqtab(quant
[dscans
[2].tq
], decdata
->dquant
[2]);
355 initcol(decdata
->dquant
);
356 setinput(&glob_in
, datap
);
360 dscans
[0].next
= 6 - 4;
361 dscans
[1].next
= 6 - 4 - 1;
362 dscans
[2].next
= 6 - 4 - 1 - 1; /* 411 encoding */
363 for (my
= 0; my
< mcusy
; my
++) {
364 for (mx
= 0; mx
< mcusx
; mx
++) {
365 if (info
.dri
&& !--info
.nm
)
366 if (dec_checkmarker())
367 return ERR_WRONG_MARKER
;
369 decode_mcus(&glob_in
, decdata
->dcts
, 6, dscans
, max
);
370 idct(decdata
->dcts
, decdata
->out
, decdata
->dquant
[0],
371 IFIX(128.5), max
[0]);
372 idct(decdata
->dcts
+ 64, decdata
->out
+ 64,
373 decdata
->dquant
[0], IFIX(128.5), max
[1]);
374 idct(decdata
->dcts
+ 128, decdata
->out
+ 128,
375 decdata
->dquant
[0], IFIX(128.5), max
[2]);
376 idct(decdata
->dcts
+ 192, decdata
->out
+ 192,
377 decdata
->dquant
[0], IFIX(128.5), max
[3]);
378 idct(decdata
->dcts
+ 256, decdata
->out
+ 256,
379 decdata
->dquant
[1], IFIX(0.5), max
[4]);
380 idct(decdata
->dcts
+ 320, decdata
->out
+ 320,
381 decdata
->dquant
[2], IFIX(0.5), max
[5]);
385 col221111_32(decdata
->out
, pic
386 + (my
* 16 * mcusx
+ mx
) * 16 * 4,
390 col221111(decdata
->out
, pic
391 + (my
* 16 * mcusx
+ mx
) * 16 * 3,
395 col221111_16(decdata
->out
, pic
396 + (my
* 16 * mcusx
+ mx
) * (16 * 2),
400 return ERR_DEPTH_MISMATCH
;
405 m
= dec_readmarker(&glob_in
);
412 /****************************************************************/
413 /************** huffman decoder ***************/
414 /****************************************************************/
416 static int fillbits
__P((struct in
*, int, unsigned int));
418 __P((struct in
*, struct dec_hufftbl
*, int *, int, int));
420 static void setinput(struct in
*in
, unsigned char *p
)
428 static int fillbits(struct in
*in
, int le
, unsigned int bi
)
434 in
->bits
= bi
<< 16, le
+= 16;
444 m
= in
->func(in
->data
);
451 bi
= bi
<< 16, le
+= 16;
458 in
->bits
= bi
; /* tmp... 2 return values needed */
462 static int dec_readmarker(struct in
*in
)
466 in
->left
= fillbits(in
, in
->left
, in
->bits
);
475 #define LEBI_DCL int le, bi
476 #define LEBI_GET(in) (le = in->left, bi = in->bits)
477 #define LEBI_PUT(in) (in->left = le, in->bits = bi)
479 #define GETBITS(in, n) ( \
480 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
482 bi >> le & ((1 << (n)) - 1) \
485 #define UNGETBITS(in, n) ( \
490 static int dec_rec2(struct in
*in
, struct dec_hufftbl
*hu
, int *runp
, int c
,
497 UNGETBITS(in
, i
& 127);
501 for (i
= DECBITS
; (c
= ((c
<< 1) | GETBITS(in
, 1)))
502 >= (hu
->maxcode
[i
]); i
++)
505 in
->marker
= M_BADHUFF
;
508 i
= hu
->vals
[hu
->valptr
[i
] + c
- hu
->maxcode
[i
- 1] * 2];
512 if (i
== 0) { /* sigh, 0xf0 is 11 bit */
518 if (c
< (1 << (i
- 1)))
524 #define DEC_REC(in, hu, r, i) ( \
525 r = GETBITS(in, DECBITS), \
529 UNGETBITS(in, i & 127), \
536 i = dec_rec2(in, hu, &r, r, i), \
542 static void decode_mcus(struct in
*in
, int *dct
, int n
, struct scan
*sc
,
545 struct dec_hufftbl
*hu
;
549 memset(dct
, 0, n
* 64 * sizeof(*dct
));
553 *dct
++ = (sc
->dc
+= DEC_REC(in
, hu
, r
, t
));
558 t
= DEC_REC(in
, hu
, r
, t
);
559 if (t
== 0 && r
== 0) {
574 static void dec_makehuff(struct dec_hufftbl
*hu
, int *hufflen
,
575 unsigned char *huffvals
)
577 int code
, k
, i
, j
, d
, x
, c
, v
;
578 for (i
= 0; i
< (1 << DECBITS
); i
++)
584 * value v already known, run r, backup u bits:
585 * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
586 * value unknown, size b bits, run r, backup u bits:
587 * 000000000000bbbb 0000 rrrr 0 uuuuuuu
588 * value and size unknown:
589 * 0000000000000000 0000 0000 0 0000000
593 for (i
= 0; i
< 16; i
++, code
<<= 1) { /* sizes */
595 for (j
= 0; j
< hufflen
[i
]; j
++) {
596 hu
->vals
[k
] = *huffvals
++;
598 c
= code
<< (DECBITS
- 1 - i
);
599 v
= hu
->vals
[k
] & 0x0f; /* size */
600 for (d
= 1 << (DECBITS
- 1 - i
); --d
>= 0;) {
601 /* both fit in table */
602 if (v
+ i
< DECBITS
) {
603 x
= d
>> (DECBITS
- 1 - v
-
605 if (v
&& x
< (1 << (v
- 1)))
607 x
= x
<< 16 | (hu
->vals
[k
]
609 (DECBITS
- (i
+ 1 + v
))
612 x
= v
<< 16 | (hu
->vals
[k
]
615 hu
->llvals
[c
| d
] = x
;
621 hu
->maxcode
[i
] = code
;
623 hu
->maxcode
[16] = 0x20000; /* always terminate decode */
626 /****************************************************************/
627 /************** idct ***************/
628 /****************************************************************/
630 #define ONE ((PREC)IFIX(1.))
631 #define S2 ((PREC)IFIX(0.382683432))
632 #define C2 ((PREC)IFIX(0.923879532))
633 #define C4 ((PREC)IFIX(0.707106781))
635 #define S22 ((PREC)IFIX(2 * 0.382683432))
636 #define C22 ((PREC)IFIX(2 * 0.923879532))
637 #define IC4 ((PREC)IFIX(1 / 0.707106781))
639 #define C3IC1 ((PREC)IFIX(0.847759065)) /* c3/c1 */
640 #define C5IC1 ((PREC)IFIX(0.566454497)) /* c5/c1 */
641 #define C7IC1 ((PREC)IFIX(0.198912367)) /* c7/c1 */
643 #define XPP(a, b) (t = a + b, b = a - b, a = t)
644 #define XMP(a, b) (t = a - b, b = a + b, a = t)
645 #define XPM(a, b) (t = a + b, b = b - a, a = t)
647 #define ROT(a, b, s, c) (t = IMULT(a + b, s), \
648 a = IMULT(a, c - s) + t, \
649 b = IMULT(b, c + s) - t)
655 t2 = IMULT(t2, IC4) - t3, \
661 t5 = IMULT(t5, IC4), \
662 ROT(t4, t6, S22, C22), \
672 static unsigned char zig2
[64] = {
673 0, 2, 3, 9, 10, 20, 21, 35,
674 14, 16, 25, 31, 39, 46, 50, 57,
675 5, 7, 12, 18, 23, 33, 37, 48,
676 27, 29, 41, 44, 52, 55, 59, 62,
677 15, 26, 30, 40, 45, 51, 56, 58,
678 1, 4, 8, 11, 19, 22, 34, 36,
679 28, 42, 43, 53, 54, 60, 61, 63,
680 6, 13, 17, 24, 32, 38, 47, 49
683 void idct(int *in
, int *out
, PREC
*lquant
, PREC off
, int max
)
685 PREC t0
, t1
, t2
, t3
, t4
, t5
, t6
, t7
, t
;
688 unsigned char *zig2p
;
692 t0
+= in
[0] * lquant
[0];
693 for (i
= 0; i
< 64; i
++)
699 for (i
= 0; i
< 8; i
++) {
701 t0
+= in
[j
] * lquant
[j
];
703 t5
= in
[j
] * lquant
[j
];
705 t2
= in
[j
] * lquant
[j
];
707 t7
= in
[j
] * lquant
[j
];
709 t1
= in
[j
] * lquant
[j
];
711 t4
= in
[j
] * lquant
[j
];
713 t3
= in
[j
] * lquant
[j
];
715 t6
= in
[j
] * lquant
[j
];
728 for (i
= 0; i
< 8; i
++) {
738 out
[8 * i
+ 0] = ITOINT(t0
);
739 out
[8 * i
+ 1] = ITOINT(t1
);
740 out
[8 * i
+ 2] = ITOINT(t2
);
741 out
[8 * i
+ 3] = ITOINT(t3
);
742 out
[8 * i
+ 4] = ITOINT(t4
);
743 out
[8 * i
+ 5] = ITOINT(t5
);
744 out
[8 * i
+ 6] = ITOINT(t6
);
745 out
[8 * i
+ 7] = ITOINT(t7
);
749 static unsigned char zig
[64] = {
750 0, 1, 5, 6, 14, 15, 27, 28,
751 2, 4, 7, 13, 16, 26, 29, 42,
752 3, 8, 12, 17, 25, 30, 41, 43,
753 9, 11, 18, 24, 31, 40, 44, 53,
754 10, 19, 23, 32, 39, 45, 52, 54,
755 20, 22, 33, 38, 46, 51, 55, 60,
756 21, 34, 37, 47, 50, 56, 59, 61,
757 35, 36, 48, 49, 57, 58, 62, 63
760 static PREC aaidct
[8] = {
761 IFIX(0.3535533906), IFIX(0.4903926402),
762 IFIX(0.4619397663), IFIX(0.4157348062),
763 IFIX(0.3535533906), IFIX(0.2777851165),
764 IFIX(0.1913417162), IFIX(0.0975451610)
768 static void idctqtab(unsigned char *qin
, PREC
*qout
)
772 for (i
= 0; i
< 8; i
++)
773 for (j
= 0; j
< 8; j
++)
774 qout
[zig
[i
* 8 + j
]] = qin
[zig
[i
* 8 + j
]] *
775 IMULT(aaidct
[i
], aaidct
[j
]);
778 static void scaleidctqtab(PREC
*q
, PREC sc
)
782 for (i
= 0; i
< 64; i
++)
783 q
[i
] = IMULT(q
[i
], sc
);
786 /****************************************************************/
787 /************** color decoder ***************/
788 /****************************************************************/
793 * YCbCr Color transformation:
795 * y:0..255 Cb:-128..127 Cr:-128..127
797 * R = Y + 1.40200 * Cr
798 * G = Y - 0.34414 * Cb - 0.71414 * Cr
799 * B = Y + 1.77200 * Cb
804 * Cg = 0.19421 * Cb + .50937 * Cr;
810 * Cg = (50 * Cb + 130 * Cr + 128) >> 8;
813 static void initcol(PREC q
[][64])
815 scaleidctqtab(q
[1], IFIX(1.77200));
816 scaleidctqtab(q
[2], IFIX(1.40200));
819 /* This is optimized for the stupid sun SUNWspro compiler. */
820 #define STORECLAMP(a, x) \
823 (unsigned int)(x) >= 256 ? \
824 ((a) = (x) < 0 ? 0 : 255) \
829 #define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
833 #define CBCRCG(yin, xin) \
835 cb = outc[0 + yin * 8 + xin], \
836 cr = outc[64 + yin * 8 + xin], \
837 cg = (50 * cb + 130 * cr + 128) >> 8 \
842 #define CBCRCG(yin, xin) \
844 cb = outc[0 + yin*8 + xin], \
845 cr = outc[64 + yin*8 + xin], \
846 cg = (3 * cb + 8 * cr) >> 4 \
851 #define PIC(yin, xin, p, xout) \
853 y = outy[(yin) * 8 + xin], \
854 STORECLAMP(p[(xout) * 3 + 0], y + cr), \
855 STORECLAMP(p[(xout) * 3 + 1], y - cg), \
856 STORECLAMP(p[(xout) * 3 + 2], y + cb) \
859 #ifdef __LITTLE_ENDIAN
860 #define PIC_16(yin, xin, p, xout, add) \
862 y = outy[(yin) * 8 + xin], \
863 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
864 ((CLAMP(y - cg + add) & 0xfc) << 3) | \
865 ((CLAMP(y + cb + add*2+1)) >> 3), \
866 p[(xout) * 2 + 0] = y & 0xff, \
867 p[(xout) * 2 + 1] = y >> 8 \
871 #define PIC_16(yin, xin, p, xout, add) \
873 y = outy[(yin) * 8 + xin], \
874 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 7) | \
875 ((CLAMP(y - cg + add*2+1) & 0xf8) << 2) | \
876 ((CLAMP(y + cb + add*2+1)) >> 3), \
877 p[(xout) * 2 + 0] = y >> 8, \
878 p[(xout) * 2 + 1] = y & 0xff \
881 #define PIC_16(yin, xin, p, xout, add) \
883 y = outy[(yin) * 8 + xin], \
884 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
885 ((CLAMP(y - cg + add) & 0xfc) << 3) | \
886 ((CLAMP(y + cb + add*2+1)) >> 3), \
887 p[(xout) * 2 + 0] = y >> 8, \
888 p[(xout) * 2 + 1] = y & 0xff \
893 #define PIC_32(yin, xin, p, xout) \
895 y = outy[(yin) * 8 + xin], \
896 STORECLAMP(p[(xout) * 4 + 0], y + cr), \
897 STORECLAMP(p[(xout) * 4 + 1], y - cg), \
898 STORECLAMP(p[(xout) * 4 + 2], y + cb), \
899 p[(xout) * 4 + 3] = 0 \
902 #define PIC221111(xin) \
905 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
906 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
907 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
908 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
911 #define PIC221111_16(xin) \
914 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0, 3), \
915 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1, 0), \
916 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0, 1), \
917 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1, 2) \
920 #define PIC221111_32(xin) \
923 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
924 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
925 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
926 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
929 static void col221111(int *out
, unsigned char *pic
, int width
)
932 unsigned char *pic0
, *pic1
;
940 for (i
= 2; i
> 0; i
--) {
941 for (j
= 4; j
> 0; j
--) {
942 for (k
= 0; k
< 8; k
++)
949 outy
+= 64 * 2 - 16 * 4;
953 static void col221111_16(int *out
, unsigned char *pic
, int width
)
956 unsigned char *pic0
, *pic1
;
964 for (i
= 2; i
> 0; i
--) {
965 for (j
= 4; j
> 0; j
--) {
966 for (k
= 0; k
< 8; k
++)
973 outy
+= 64 * 2 - 16 * 4;
977 static void col221111_32(int *out
, unsigned char *pic
, int width
)
980 unsigned char *pic0
, *pic1
;
988 for (i
= 2; i
> 0; i
--) {
989 for (j
= 4; j
> 0; j
--) {
990 for (k
= 0; k
< 8; k
++)
997 outy
+= 64 * 2 - 16 * 4;