blob: 93fec8c0b5324ea4211cbd23d64f6eb8b179d725 [file] [log] [blame]
Ed Tanous93f987d2017-04-17 17:52:36 -07001
Ed Tanous93f987d2017-04-17 17:52:36 -07002static unsigned char zigzag[64] = {
3 0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42,
4 3, 8, 12, 17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 40, 44, 53,
5 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60,
6 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63};
7
8static unsigned char dezigzag[64 + 15] = {
9 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40,
10 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36,
11 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61,
12 54, 47, 55, 62, 63,
13 // let corrupt input sample past end
14 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63};
15
16static unsigned char *std_luminance_qt;
17static unsigned char *std_chrominance_qt;
18
19// Standard Huffman tables (cf. JPEG standard section K.3) */
20
21static unsigned char std_dc_luminance_nrcodes[17] = {0, 0, 1, 5, 1, 1, 1, 1, 1,
22 1, 0, 0, 0, 0, 0, 0, 0};
23static unsigned char std_dc_luminance_values[12] = {0, 1, 2, 3, 4, 5,
24 6, 7, 8, 9, 10, 11};
25
26static unsigned char std_dc_chrominance_nrcodes[17] = {
27 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0};
28static unsigned char std_dc_chrominance_values[12] = {0, 1, 2, 3, 4, 5,
29 6, 7, 8, 9, 10, 11};
30
31static unsigned char std_ac_luminance_nrcodes[17] = {
32 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d};
33static unsigned char std_ac_luminance_values[162] = {
34 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
35 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
36 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
37 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
38 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
39 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
40 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
41 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
42 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3,
43 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
44 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
45 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
46 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4,
47 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};
48
49static unsigned char std_ac_chrominance_nrcodes[17] = {
50 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77};
51static unsigned char std_ac_chrominance_values[162] = {
52 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41,
53 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
54 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
55 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
56 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44,
57 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
58 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74,
59 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
60 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a,
61 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
62 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
63 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
64 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
65 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};
66
67unsigned short int DC_LUMINANCE_HUFFMANCODE[13 * 2] = {
68 /* 0 */ 0x0000, 0,
69 /* 1 */ 0x4000, 2,
70 /* 2 */ 0x6000, 3,
71 /* 3 */ 0x8000, 3,
72 /* 4 */ 0xA000, 3,
73 /* 5 */ 0xC000, 3,
74 /* 6 */ 0xE000, 3,
75 /* 7 */ 0xF000, 4,
76 /* 8 */ 0xF800, 5,
77 /* 9 */ 0xFC00, 6,
78 /* 10 */ 0xFE00, 7,
79 /* 11 */ 0xFF00, 8,
80 /* 12 */ 0xFFFF, 9,
81};
82
83unsigned short int DC_CHROMINANCE_HUFFMANCODE[13 * 2] = {
84 /* 0 */ 0x0000, 0,
85 /* 1 */ 0x4000, 2,
86 /* 2 */ 0x8000, 2,
87 /* 3 */ 0xC000, 2,
88 /* 4 */ 0xE000, 3,
89 /* 5 */ 0xF000, 4,
90 /* 6 */ 0xF800, 5,
91 /* 7 */ 0xFC00, 6,
92 /* 8 */ 0xFE00, 7,
93 /* 9 */ 0xFF00, 8,
94 /* 10 */ 0xFF80, 9,
95 /* 11 */ 0xFFC0, 10,
96 /* 12 */ 0xFFFF, 11,
97};
98
99unsigned short int AC_LUMINANCE_HUFFMANCODE[39 * 2] = {
100 /* 0 */ 0x0000, 0,
101 /* 1 */ 0x4000, 2,
102 /* 2 */ 0x8000, 2,
103 /* 3 */ 0xA000, 3,
104 /* 4 */ 0xB000, 4,
105 /* 5 */ 0xC000, 4,
106 /* 6 */ 0xD000, 4,
107 /* 7 */ 0xD800, 5,
108 /* 8 */ 0xE000, 5,
109 /* 9 */ 0xE800, 5,
110 /* 10 */ 0xEC00, 6,
111 /* 11 */ 0xF000, 6,
112 /* 12 */ 0xF200, 7,
113 /* 13 */ 0xF400, 7,
114 /* 14 */ 0xF600, 7,
115 /* 15 */ 0xF800, 7,
116 /* 16 */ 0xF900, 8,
117 /* 17 */ 0xFA00, 8,
118 /* 18 */ 0xFB00, 8,
119 /* 19 */ 0xFB80, 9,
120 /* 20 */ 0xFC00, 9,
121 /* 21 */ 0xFC80, 9,
122 /* 22 */ 0xFD00, 9,
123 /* 23 */ 0xFD80, 9,
124 /* 24 */ 0xFDC0, 10,
125 /* 25 */ 0xFE00, 10,
126 /* 26 */ 0xFE40, 10,
127 /* 27 */ 0xFE80, 10,
128 /* 28 */ 0xFEC0, 10,
129 /* 29 */ 0xFEE0, 11,
130 /* 30 */ 0xFF00, 11,
131 /* 31 */ 0xFF20, 11,
132 /* 32 */ 0xFF40, 11,
133 /* 33 */ 0xFF50, 12,
134 /* 34 */ 0xFF60, 12,
135 /* 35 */ 0xFF70, 12,
136 /* 36 */ 0xFF80, 12,
137 /* 37 */ 0xFF82, 15,
138 /* 38 */ 0xFFFF, 16,
139};
140
141unsigned short int AC_CHROMINANCE_HUFFMANCODE[45 * 2] = {
142 /* 0 */ 0x0000, 0,
143 /* 1 */ 0x4000, 2,
144 /* 2 */ 0x8000, 2,
145 /* 3 */ 0xA000, 3,
146 /* 4 */ 0xB000, 4,
147 /* 5 */ 0xC000, 4,
148 /* 6 */ 0xC800, 5,
149 /* 7 */ 0xD000, 5,
150 /* 8 */ 0xD800, 5,
151 /* 9 */ 0xE000, 5,
152 /* 10 */ 0xE400, 6,
153 /* 11 */ 0xE800, 6,
154 /* 12 */ 0xEC00, 6,
155 /* 13 */ 0xF000, 6,
156 /* 14 */ 0xF200, 7,
157 /* 15 */ 0xF400, 7,
158 /* 16 */ 0xF600, 7,
159 /* 17 */ 0xF700, 8,
160 /* 18 */ 0xF800, 8,
161 /* 19 */ 0xF900, 8,
162 /* 20 */ 0xFA00, 8,
163 /* 21 */ 0xFA80, 9,
164 /* 22 */ 0xFB00, 9,
165 /* 23 */ 0xFB80, 9,
166 /* 24 */ 0xFC00, 9,
167 /* 25 */ 0xFC80, 9,
168 /* 26 */ 0xFD00, 9,
169 /* 27 */ 0xFD80, 9,
170 /* 28 */ 0xFDC0, 10,
171 /* 29 */ 0xFE00, 10,
172 /* 30 */ 0xFE40, 10,
173 /* 31 */ 0xFE80, 10,
174 /* 32 */ 0xFEC0, 10,
175 /* 33 */ 0xFEE0, 11,
176 /* 34 */ 0xFF00, 11,
177 /* 35 */ 0xFF20, 11,
178 /* 36 */ 0xFF40, 11,
179 /* 37 */ 0xFF50, 12,
180 /* 38 */ 0xFF60, 12,
181 /* 39 */ 0xFF70, 12,
182 /* 40 */ 0xFF80, 12,
183 /* 41 */ 0xFF84, 14,
184 /* 42 */ 0xFF86, 15,
185 /* 43 */ 0xFF88, 15,
186 /* 44 */ 0xFFFF, 16,
187};
188
189//[100]=========================
190static unsigned char Tbl_100Y[64] = {
191 2, 1, 1, 2, 3, 5, 6, 7, 1, 1, 1, 2, 3, 7, 7, 6,
192 1, 1, 2, 3, 5, 7, 8, 7, 1, 2, 2, 3, 6, 10, 10, 7,
193 2, 2, 4, 7, 8, 13, 12, 9, 3, 4, 6, 8, 10, 13, 14, 11,
194 6, 8, 9, 10, 12, 15, 15, 12, 9, 11, 11, 12, 14, 12, 12, 12};
195static unsigned char Tbl_100UV[64] = {
196 3, 3, 4, 8, 18, 18, 18, 18, 3, 3, 4, 12, 18, 18, 18, 18,
197 4, 4, 10, 18, 18, 18, 18, 18, 8, 12, 18, 18, 18, 18, 18, 18,
198 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
199 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18};
200
201//[086]=========================
202static unsigned char Tbl_086Y[64] = {
203 3, 2, 1, 3, 4, 7, 9, 11, 2, 2, 2, 3, 4, 10, 11, 10,
204 2, 2, 3, 4, 7, 10, 12, 10, 2, 3, 4, 5, 9, 16, 15, 11,
205 3, 4, 6, 10, 12, 20, 19, 14, 4, 6, 10, 12, 15, 19, 21, 17,
206 9, 12, 14, 16, 19, 22, 22, 18, 13, 17, 17, 18, 21, 18, 19, 18};
207static unsigned char Tbl_086UV[64] = {
208 4, 5, 6, 13, 27, 27, 27, 27, 5, 5, 7, 18, 27, 27, 27, 27,
209 6, 7, 15, 27, 27, 27, 27, 27, 13, 18, 27, 27, 27, 27, 27, 27,
210 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
211 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27};
212
213//[071]=========================
214static unsigned char Tbl_071Y[64] = {
215 6, 4, 3, 6, 9, 15, 19, 22, 4, 4, 5, 7, 9, 21, 22, 20,
216 5, 4, 6, 9, 15, 21, 25, 21, 5, 6, 8, 10, 19, 32, 30, 23,
217 6, 8, 13, 21, 25, 40, 38, 28, 9, 13, 20, 24, 30, 39, 42, 34,
218 18, 24, 29, 32, 38, 45, 45, 37, 27, 34, 35, 36, 42, 37, 38, 37};
219static unsigned char Tbl_071UV[64] = {
220 9, 10, 13, 26, 55, 55, 55, 55, 10, 11, 14, 37, 55, 55, 55, 55,
221 13, 14, 31, 55, 55, 55, 55, 55, 26, 37, 55, 55, 55, 55, 55, 55,
222 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
223 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55};
224//[057]=========================
225static unsigned char Tbl_057Y[64] = {
226 9, 6, 5, 9, 13, 22, 28, 34, 6, 6, 7, 10, 14, 32, 33, 30,
227 7, 7, 9, 13, 22, 32, 38, 31, 7, 9, 12, 16, 28, 48, 45, 34,
228 10, 12, 20, 31, 38, 61, 57, 43, 13, 19, 30, 36, 45, 58, 63, 51,
229 27, 36, 43, 48, 57, 68, 67, 56, 40, 51, 53, 55, 63, 56, 57, 55};
230static unsigned char Tbl_057UV[64] = {
231 13, 14, 19, 38, 80, 80, 80, 80, 14, 17, 21, 53, 80, 80, 80, 80,
232 19, 21, 45, 80, 80, 80, 80, 80, 38, 53, 80, 80, 80, 80, 80, 80,
233 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
234 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
235
236//[043]=========================
237static unsigned char Tbl_043Y[64] = {
238 11, 7, 7, 11, 17, 28, 36, 43, 8, 8, 10, 13, 18, 41, 43, 39,
239 10, 9, 11, 17, 28, 40, 49, 40, 10, 12, 15, 20, 36, 62, 57, 44,
240 12, 15, 26, 40, 48, 78, 74, 55, 17, 25, 39, 46, 58, 74, 81, 66,
241 35, 46, 56, 62, 74, 86, 86, 72, 51, 66, 68, 70, 80, 71, 74, 71};
242static unsigned char Tbl_043UV[64] = {
243 18, 19, 26, 51, 108, 108, 108, 108, 19, 22, 28, 72, 108,
244 108, 108, 108, 26, 28, 61, 108, 108, 108, 108, 108, 51, 72,
245 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108,
246 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108,
247 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108};
248
249//[029]=========================
250static unsigned char Tbl_029Y[64] = {
251 14, 9, 9, 14, 21, 36, 46, 55, 10, 10, 12, 17, 23, 52, 54, 49,
252 12, 11, 14, 21, 36, 51, 62, 50, 12, 15, 19, 26, 46, 78, 72, 56,
253 16, 19, 33, 50, 61, 98, 93, 69, 21, 31, 49, 58, 73, 94, 102, 83,
254 44, 58, 70, 78, 93, 109, 108, 91, 65, 83, 86, 88, 101, 90, 93, 89};
255static unsigned char Tbl_029UV[64] = {
256 22, 24, 32, 63, 133, 133, 133, 133, 24, 28, 34, 88, 133,
257 133, 133, 133, 32, 34, 75, 133, 133, 133, 133, 133, 63, 88,
258 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
259 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
260 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133};
261
262//[014]=========================
263static unsigned char Tbl_014Y[64] = {
264 17, 12, 10, 17, 26, 43, 55, 66, 13, 13, 15, 20, 28, 63, 65, 60,
265 15, 14, 17, 26, 43, 62, 75, 61, 15, 18, 24, 31, 55, 95, 87, 67,
266 19, 24, 40, 61, 74, 119, 112, 84, 26, 38, 60, 70, 88, 113, 123, 100,
267 53, 70, 85, 95, 112, 132, 131, 110, 78, 100, 103, 107, 122, 109, 112, 108};
268static unsigned char Tbl_014UV[64] = {
269 27, 29, 39, 76, 160, 160, 160, 160, 29, 34, 42, 107, 160,
270 160, 160, 160, 39, 42, 91, 160, 160, 160, 160, 160, 76, 107,
271 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
272 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
273 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160};
274//[000]=========================
275static unsigned char Tbl_000Y[64] = {
276 20, 13, 12, 20, 30, 50, 63, 76, 15, 15, 17, 23, 32, 72, 75, 68,
277 17, 16, 20, 30, 50, 71, 86, 70, 17, 21, 27, 36, 63, 108, 100, 77,
278 22, 27, 46, 70, 85, 136, 128, 96, 30, 43, 68, 80, 101, 130, 141, 115,
279 61, 80, 97, 108, 128, 151, 150, 126, 90, 115, 118, 122, 140, 125, 128, 123};
280static unsigned char Tbl_000UV[64] = {
281 31, 33, 45, 88, 185, 185, 185, 185, 33, 39, 48, 123, 185,
282 185, 185, 185, 45, 48, 105, 185, 185, 185, 185, 185, 88, 123,
283 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185,
284 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185,
285 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185};
286
287typedef struct {
288 unsigned char Length[17]; // k =1-16 ; L[k] indicates the number of Huffman
289 // codes of length k
290 unsigned short int minor_code[17]; // indicates the value of the smallest
291 // Huffman code of length k
292 unsigned short int major_code[17]; // similar, but the highest code
293 unsigned char V[65536]; // V[k][j] = Value associated to the j-th Huffman
294 // code of length k
295 // High nibble = nr of previous 0 coefficients
296 // Low nibble = size (in bits) of the coefficient which will be taken from the
297 // data stream
298 unsigned char Len[65536];
299} Huffman_table;