blob: e7b36dc4cbdf54efd7dc88e3d7557f3df717d09e [file] [log] [blame]
Ed Tanous93f987d2017-04-17 17:52:36 -07001#pragma once
2
Ed Tanous93f987d2017-04-17 17:52:36 -07003#include <array>
4#include <aspeed/JTABLES.H>
Ed Tanous1ff48782017-04-18 12:45:08 -07005#include <ast_video_types.hpp>
6#include <cassert>
7#include <cstdint>
8#include <iostream>
Ed Tanous93f987d2017-04-17 17:52:36 -07009#include <vector>
10
Ed Tanous1ff48782017-04-18 12:45:08 -070011template <class T, class Compare>
12constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) {
13 return assert(!comp(hi, lo)), comp(v, lo) ? lo : comp(hi, v) ? hi : v;
14}
15
16template <class T>
17constexpr const T &clamp(const T &v, const T &lo, const T &hi) {
18 return clamp(v, lo, hi, std::less<>());
19}
Ed Tanous93f987d2017-04-17 17:52:36 -070020
21namespace AstVideo {
22
Ed Tanous93f987d2017-04-17 17:52:36 -070023struct COLOR_CACHE {
24 unsigned long Color[4];
25 unsigned char Index[4];
26 unsigned char BitMapBits;
27};
28
29struct RGB {
30 unsigned char B;
31 unsigned char G;
32 unsigned char R;
33 unsigned char Reserved;
34};
35
36enum class JpgBlock {
37 JPEG_NO_SKIP_CODE = 0x00,
38 JPEG_SKIP_CODE = 0x08,
39
40 JPEG_PASS2_CODE = 0x02,
41 JPEG_SKIP_PASS2_CODE = 0x0A,
42
43 LOW_JPEG_NO_SKIP_CODE = 0x04,
44 LOW_JPEG_SKIP_CODE = 0x0C,
45
46 VQ_NO_SKIP_1_COLOR_CODE = 0x05,
47 VQ_SKIP_1_COLOR_CODE = 0x0D,
48
49 VQ_NO_SKIP_2_COLOR_CODE = 0x06,
50 VQ_SKIP_2_COLOR_CODE = 0x0E,
51
52 VQ_NO_SKIP_4_COLOR_CODE = 0x07,
53 VQ_SKIP_4_COLOR_CODE = 0x0F,
54
55 FRAME_END_CODE = 0x09,
56
57};
58
59class AstJpegDecoder {
60 public:
61 AstJpegDecoder() {
62 // TODO(ed) figure out how to init this in the constructor
63 YUVBuffer.resize(800 * 600);
64 OutBuffer.resize(800 * 600);
65 for (auto &r : OutBuffer) {
66 r.R = 0x00;
67 r.G = 0x00;
68 r.B = 0x00;
69 r.Reserved = 0xAA;
70 }
71 init_jpg_table();
72 }
73
74 void load_quant_table(std::array<long, 64> &quant_table) {
75 float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f,
76 1.0f, 0.785694958f, 0.541196100f, 0.275899379f};
77 uint8_t j, row, col;
78 uint8_t tempQT[64];
79
80 // Load quantization coefficients from JPG file, scale them for DCT and
81 // reorder
82 // from zig-zag order
83 switch (Y_selector) {
84 case 0:
85 std_luminance_qt = Tbl_000Y;
86 break;
87 case 1:
88 std_luminance_qt = Tbl_014Y;
89 break;
90 case 2:
91 std_luminance_qt = Tbl_029Y;
92 break;
93 case 3:
94 std_luminance_qt = Tbl_043Y;
95 break;
96 case 4:
97 std_luminance_qt = Tbl_057Y;
98 break;
99 case 5:
100 std_luminance_qt = Tbl_071Y;
101 break;
102 case 6:
103 std_luminance_qt = Tbl_086Y;
104 break;
105 case 7:
106 std_luminance_qt = Tbl_100Y;
107 break;
108 }
109 set_quant_table(std_luminance_qt, (uint8_t)SCALEFACTOR, tempQT);
110
111 for (j = 0; j <= 63; j++) quant_table[j] = tempQT[zigzag[j]];
112 j = 0;
113 for (row = 0; row <= 7; row++)
114 for (col = 0; col <= 7; col++) {
115 quant_table[j] =
116 (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) *
117 65536);
118 j++;
119 }
120 byte_pos += 64;
121 }
122
123 void load_quant_tableCb(std::array<long, 64> &quant_table) {
124 float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f,
125 1.0f, 0.785694958f, 0.541196100f, 0.275899379f};
126 uint8_t j, row, col;
127 uint8_t tempQT[64];
128
129 // Load quantization coefficients from JPG file, scale them for DCT and
130 // reorder from zig-zag order
131 if (Mapping == 0) {
132 switch (UV_selector) {
133 case 0:
134 std_chrominance_qt = Tbl_000Y;
135 break;
136 case 1:
137 std_chrominance_qt = Tbl_014Y;
138 break;
139 case 2:
140 std_chrominance_qt = Tbl_029Y;
141 break;
142 case 3:
143 std_chrominance_qt = Tbl_043Y;
144 break;
145 case 4:
146 std_chrominance_qt = Tbl_057Y;
147 break;
148 case 5:
149 std_chrominance_qt = Tbl_071Y;
150 break;
151 case 6:
152 std_chrominance_qt = Tbl_086Y;
153 break;
154 case 7:
155 std_chrominance_qt = Tbl_100Y;
156 break;
157 }
158 } else {
159 switch (UV_selector) {
160 case 0:
161 std_chrominance_qt = Tbl_000UV;
162 break;
163 case 1:
164 std_chrominance_qt = Tbl_014UV;
165 break;
166 case 2:
167 std_chrominance_qt = Tbl_029UV;
168 break;
169 case 3:
170 std_chrominance_qt = Tbl_043UV;
171 break;
172 case 4:
173 std_chrominance_qt = Tbl_057UV;
174 break;
175 case 5:
176 std_chrominance_qt = Tbl_071UV;
177 break;
178 case 6:
179 std_chrominance_qt = Tbl_086UV;
180 break;
181 case 7:
182 std_chrominance_qt = Tbl_100UV;
183 break;
184 }
185 }
186 set_quant_table(std_chrominance_qt, (uint8_t)SCALEFACTORUV, tempQT);
187
188 for (j = 0; j <= 63; j++) {
189 quant_table[j] = tempQT[zigzag[j]];
190 }
191 j = 0;
192 for (row = 0; row <= 7; row++) {
193 for (col = 0; col <= 7; col++) {
194 quant_table[j] =
195 (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) *
196 65536);
197 j++;
198 }
199 }
200 byte_pos += 64;
201 }
202 // Note: Added for Dual_JPEG
203 void load_advance_quant_table(std::array<long, 64> &quant_table) {
204 float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f,
205 1.0f, 0.785694958f, 0.541196100f, 0.275899379f};
206 uint8_t j, row, col;
207 uint8_t tempQT[64];
208
209 // Load quantization coefficients from JPG file, scale them for DCT and
210 // reorder
211 // from zig-zag order
212 switch (advance_selector) {
213 case 0:
214 std_luminance_qt = Tbl_000Y;
215 break;
216 case 1:
217 std_luminance_qt = Tbl_014Y;
218 break;
219 case 2:
220 std_luminance_qt = Tbl_029Y;
221 break;
222 case 3:
223 std_luminance_qt = Tbl_043Y;
224 break;
225 case 4:
226 std_luminance_qt = Tbl_057Y;
227 break;
228 case 5:
229 std_luminance_qt = Tbl_071Y;
230 break;
231 case 6:
232 std_luminance_qt = Tbl_086Y;
233 break;
234 case 7:
235 std_luminance_qt = Tbl_100Y;
236 break;
237 }
238 // Note: pass ADVANCE SCALE FACTOR to sub-function in Dual-JPEG
239 set_quant_table(std_luminance_qt, (uint8_t)ADVANCESCALEFACTOR, tempQT);
240
241 for (j = 0; j <= 63; j++) quant_table[j] = tempQT[zigzag[j]];
242 j = 0;
243 for (row = 0; row <= 7; row++)
244 for (col = 0; col <= 7; col++) {
245 quant_table[j] =
246 (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) *
247 65536);
248 j++;
249 }
250 byte_pos += 64;
251 }
252
253 // Note: Added for Dual-JPEG
254 void load_advance_quant_tableCb(std::array<long, 64> &quant_table) {
255 float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f,
256 1.0f, 0.785694958f, 0.541196100f, 0.275899379f};
257 uint8_t j, row, col;
258 uint8_t tempQT[64];
259
260 // Load quantization coefficients from JPG file, scale them for DCT and
261 // reorder
262 // from zig-zag order
263 if (Mapping == 1) {
264 switch (advance_selector) {
265 case 0:
266 std_chrominance_qt = Tbl_000Y;
267 break;
268 case 1:
269 std_chrominance_qt = Tbl_014Y;
270 break;
271 case 2:
272 std_chrominance_qt = Tbl_029Y;
273 break;
274 case 3:
275 std_chrominance_qt = Tbl_043Y;
276 break;
277 case 4:
278 std_chrominance_qt = Tbl_057Y;
279 break;
280 case 5:
281 std_chrominance_qt = Tbl_071Y;
282 break;
283 case 6:
284 std_chrominance_qt = Tbl_086Y;
285 break;
286 case 7:
287 std_chrominance_qt = Tbl_100Y;
288 break;
289 }
290 } else {
291 switch (advance_selector) {
292 case 0:
293 std_chrominance_qt = Tbl_000UV;
294 break;
295 case 1:
296 std_chrominance_qt = Tbl_014UV;
297 break;
298 case 2:
299 std_chrominance_qt = Tbl_029UV;
300 break;
301 case 3:
302 std_chrominance_qt = Tbl_043UV;
303 break;
304 case 4:
305 std_chrominance_qt = Tbl_057UV;
306 break;
307 case 5:
308 std_chrominance_qt = Tbl_071UV;
309 break;
310 case 6:
311 std_chrominance_qt = Tbl_086UV;
312 break;
313 case 7:
314 std_chrominance_qt = Tbl_100UV;
315 break;
316 }
317 }
318 // Note: pass ADVANCE SCALE FACTOR to sub-function in Dual-JPEG
319 set_quant_table(std_chrominance_qt, (uint8_t)ADVANCESCALEFACTORUV, tempQT);
320
321 for (j = 0; j <= 63; j++) quant_table[j] = tempQT[zigzag[j]];
322 j = 0;
323 for (row = 0; row <= 7; row++)
324 for (col = 0; col <= 7; col++) {
325 quant_table[j] =
326 (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) *
327 65536);
328 j++;
329 }
330 byte_pos += 64;
331 }
332
333 void IDCT_transform(short *coef, uint8_t *data, uint8_t nBlock) {
334#define FIX_1_082392200 ((int)277) /* FIX(1.082392200) */
335#define FIX_1_414213562 ((int)362) /* FIX(1.414213562) */
336#define FIX_1_847759065 ((int)473) /* FIX(1.847759065) */
337#define FIX_2_613125930 ((int)669) /* FIX(2.613125930) */
338
339#define MULTIPLY(var, cons) ((int)((var) * (cons)) >> 8)
340
341 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
342 int tmp10, tmp11, tmp12, tmp13;
343 int z5, z10, z11, z12, z13;
344 int workspace[64]; /* buffers data between passes */
345
346 short *inptr = coef;
347 long *quantptr;
348 int *wsptr = workspace;
349 unsigned char *outptr;
350 unsigned char *r_limit = rlimit_table + 128;
351 int ctr, dcval, DCTSIZE = 8;
352
353 quantptr = &QT[nBlock][0];
354
355 // Pass 1: process columns from input (inptr), store into work array(wsptr)
356
357 for (ctr = 8; ctr > 0; ctr--) {
358 /* Due to quantization, we will usually find that many of the input
359 * coefficients are zero, especially the AC terms. We can exploit this
360 * by short-circuiting the IDCT calculation for any column in which all
361 * the AC terms are zero. In that case each output is equal to the
362 * DC coefficient (with scale factor as needed).
363 * With typical images and quantization tables, half or more of the
364 * column DCT calculations can be simplified this way.
365 */
366
367 if ((inptr[DCTSIZE * 1] | inptr[DCTSIZE * 2] | inptr[DCTSIZE * 3] |
368 inptr[DCTSIZE * 4] | inptr[DCTSIZE * 5] | inptr[DCTSIZE * 6] |
369 inptr[DCTSIZE * 7]) == 0) {
370 /* AC terms all zero */
371 dcval = (int)((inptr[DCTSIZE * 0] * quantptr[DCTSIZE * 0]) >> 16);
372
373 wsptr[DCTSIZE * 0] = dcval;
374 wsptr[DCTSIZE * 1] = dcval;
375 wsptr[DCTSIZE * 2] = dcval;
376 wsptr[DCTSIZE * 3] = dcval;
377 wsptr[DCTSIZE * 4] = dcval;
378 wsptr[DCTSIZE * 5] = dcval;
379 wsptr[DCTSIZE * 6] = dcval;
380 wsptr[DCTSIZE * 7] = dcval;
381
382 inptr++; /* advance pointers to next column */
383 quantptr++;
384 wsptr++;
385 continue;
386 }
387
388 /* Even part */
389
390 tmp0 = (inptr[DCTSIZE * 0] * quantptr[DCTSIZE * 0]) >> 16;
391 tmp1 = (inptr[DCTSIZE * 2] * quantptr[DCTSIZE * 2]) >> 16;
392 tmp2 = (inptr[DCTSIZE * 4] * quantptr[DCTSIZE * 4]) >> 16;
393 tmp3 = (inptr[DCTSIZE * 6] * quantptr[DCTSIZE * 6]) >> 16;
394
395 tmp10 = tmp0 + tmp2; /* phase 3 */
396 tmp11 = tmp0 - tmp2;
397
398 tmp13 = tmp1 + tmp3; /* phases 5-3 */
399 tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
400
401 tmp0 = tmp10 + tmp13; /* phase 2 */
402 tmp3 = tmp10 - tmp13;
403 tmp1 = tmp11 + tmp12;
404 tmp2 = tmp11 - tmp12;
405
406 /* Odd part */
407
408 tmp4 = (inptr[DCTSIZE * 1] * quantptr[DCTSIZE * 1]) >> 16;
409 tmp5 = (inptr[DCTSIZE * 3] * quantptr[DCTSIZE * 3]) >> 16;
410 tmp6 = (inptr[DCTSIZE * 5] * quantptr[DCTSIZE * 5]) >> 16;
411 tmp7 = (inptr[DCTSIZE * 7] * quantptr[DCTSIZE * 7]) >> 16;
412
413 z13 = tmp6 + tmp5; /* phase 6 */
414 z10 = tmp6 - tmp5;
415 z11 = tmp4 + tmp7;
416 z12 = tmp4 - tmp7;
417
418 tmp7 = z11 + z13; /* phase 5 */
419 tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
420
421 z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
422 tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
423 tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5; /* -2*(c2+c6) */
424
425 tmp6 = tmp12 - tmp7; /* phase 2 */
426 tmp5 = tmp11 - tmp6;
427 tmp4 = tmp10 + tmp5;
428
429 wsptr[DCTSIZE * 0] = (int)(tmp0 + tmp7);
430 wsptr[DCTSIZE * 7] = (int)(tmp0 - tmp7);
431 wsptr[DCTSIZE * 1] = (int)(tmp1 + tmp6);
432 wsptr[DCTSIZE * 6] = (int)(tmp1 - tmp6);
433 wsptr[DCTSIZE * 2] = (int)(tmp2 + tmp5);
434 wsptr[DCTSIZE * 5] = (int)(tmp2 - tmp5);
435 wsptr[DCTSIZE * 4] = (int)(tmp3 + tmp4);
436 wsptr[DCTSIZE * 3] = (int)(tmp3 - tmp4);
437
438 inptr++; /* advance pointers to next column */
439 quantptr++;
440 wsptr++;
441 }
442
443/* Pass 2: process rows from work array, store into output array. */
444/* Note that we must descale the results by a factor of 8 == 2**3, */
445/* and also undo the PASS1_BITS scaling. */
446
447//#define RANGE_MASK 1023; //2 bits wider than legal samples
448#define PASS1_BITS 0
449#define IDESCALE(x, n) ((int)((x) >> n))
450
451 wsptr = workspace;
452 for (ctr = 0; ctr < DCTSIZE; ctr++) {
453 outptr = data + ctr * 8;
454
455 /* Rows of zeroes can be exploited in the same way as we did with columns.
456 * However, the column calculation has created many nonzero AC terms, so
457 * the simplification applies less often (typically 5% to 10% of the time).
458 * On machines with very fast multiplication, it's possible that the
459 * test takes more time than it's worth. In that case this section
460 * may be commented out.
461 */
462 /* Even part */
463
464 tmp10 = ((int)wsptr[0] + (int)wsptr[4]);
465 tmp11 = ((int)wsptr[0] - (int)wsptr[4]);
466
467 tmp13 = ((int)wsptr[2] + (int)wsptr[6]);
468 tmp12 = MULTIPLY((int)wsptr[2] - (int)wsptr[6], FIX_1_414213562) - tmp13;
469
470 tmp0 = tmp10 + tmp13;
471 tmp3 = tmp10 - tmp13;
472 tmp1 = tmp11 + tmp12;
473 tmp2 = tmp11 - tmp12;
474
475 /* Odd part */
476
477 z13 = (int)wsptr[5] + (int)wsptr[3];
478 z10 = (int)wsptr[5] - (int)wsptr[3];
479 z11 = (int)wsptr[1] + (int)wsptr[7];
480 z12 = (int)wsptr[1] - (int)wsptr[7];
481
482 tmp7 = z11 + z13; /* phase 5 */
483 tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
484
485 z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
486 tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
487 tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5; /* -2*(c2+c6) */
488
489 tmp6 = tmp12 - tmp7; /* phase 2 */
490 tmp5 = tmp11 - tmp6;
491 tmp4 = tmp10 + tmp5;
492
493 /* Final output stage: scale down by a factor of 8 and range-limit */
494
495 outptr[0] = r_limit[IDESCALE((tmp0 + tmp7), (PASS1_BITS + 3)) & 1023L];
496 outptr[7] = r_limit[IDESCALE((tmp0 - tmp7), (PASS1_BITS + 3)) & 1023L];
497 outptr[1] = r_limit[IDESCALE((tmp1 + tmp6), (PASS1_BITS + 3)) & 1023L];
498 outptr[6] = r_limit[IDESCALE((tmp1 - tmp6), (PASS1_BITS + 3)) & 1023L];
499 outptr[2] = r_limit[IDESCALE((tmp2 + tmp5), (PASS1_BITS + 3)) & 1023L];
500 outptr[5] = r_limit[IDESCALE((tmp2 - tmp5), (PASS1_BITS + 3)) & 1023L];
501 outptr[4] = r_limit[IDESCALE((tmp3 + tmp4), (PASS1_BITS + 3)) & 1023L];
502 outptr[3] = r_limit[IDESCALE((tmp3 - tmp4), (PASS1_BITS + 3)) & 1023L];
503
504 wsptr += DCTSIZE; /* advance pointer to next row */
505 }
506 }
507 void YUVToRGB(
508 int txb, int tyb,
509 unsigned char
510 *pYCbCr, // in, Y: 256 or 64 bytes; Cb: 64 bytes; Cr: 64 bytes
511 struct RGB *pYUV, // in, Y: 256 or 64 bytes; Cb: 64 bytes; Cr: 64 bytes
512 unsigned char
513 *pBgr // out, BGR format, 16*16*3 = 768 bytes; or 8*8*3=192 bytes
514 ) {
515 int i, j, pos, m, n;
516 unsigned char cb, cr, *py, *pcb, *pcr, *py420[4];
517 int y;
518 struct RGB *pByte;
519 int nBlocksInMcu = 6;
520 unsigned int pixel_x, pixel_y;
521
522 pByte = (struct RGB *)pBgr;
523 if (yuvmode == YuvMode::YUV444) {
524 py = pYCbCr;
525 pcb = pYCbCr + 64;
526 pcr = pcb + 64;
527
528 pixel_x = txb * 8;
529 pixel_y = tyb * 8;
530 pos = (pixel_y * WIDTH) + pixel_x;
531
532 for (j = 0; j < 8; j++) {
533 for (i = 0; i < 8; i++) {
534 m = ((j << 3) + i);
535 y = py[m];
536 cb = pcb[m];
537 cr = pcr[m];
538 n = pos + i;
539 // For 2Pass. Save the YUV value
540 pYUV[n].B = cb;
541 pYUV[n].G = y;
542 pYUV[n].R = cr;
Ed Tanous1ff48782017-04-18 12:45:08 -0700543 pByte[n].B = clamp(m_Y[y] + m_CbToB[cb], 0, 0xFF);
544 pByte[n].G = clamp(m_Y[y] + m_CbToG[cb] + m_CrToG[cr], 0, 0xFF);
545 pByte[n].R = clamp(m_Y[y] + m_CrToR[cr], 0, 0xFF);
Ed Tanous93f987d2017-04-17 17:52:36 -0700546 }
547 pos += WIDTH;
548 }
549 } else {
550 for (i = 0; i < nBlocksInMcu - 2; i++) py420[i] = pYCbCr + i * 64;
551 pcb = pYCbCr + (nBlocksInMcu - 2) * 64;
552 pcr = pcb + 64;
553
554 pixel_x = txb * 16;
555 pixel_y = tyb * 16;
556 pos = (pixel_y * WIDTH) + pixel_x;
557
558 for (j = 0; j < 16; j++) {
559 for (i = 0; i < 16; i++) {
560 // block number is ((j/8) * 2 + i/8)={0, 1, 2, 3}
561 y = *(py420[(j >> 3) * 2 + (i >> 3)]++);
562 m = ((j >> 1) << 3) + (i >> 1);
563 cb = pcb[m];
564 cr = pcr[m];
565 n = pos + i;
Ed Tanous1ff48782017-04-18 12:45:08 -0700566 pByte[n].B = clamp(m_Y[y] + m_CbToB[cb], 0, 0xFF);
567 pByte[n].G = clamp(m_Y[y] + m_CbToG[cb] + m_CrToG[cr], 0, 0xFF);
568 pByte[n].R = clamp(m_Y[y] + m_CrToR[cr], 0, 0xFF);
Ed Tanous93f987d2017-04-17 17:52:36 -0700569 }
570 pos += WIDTH;
571 }
572 }
573 }
574 void YUVToBuffer(
575 int txb, int tyb,
576 unsigned char
577 *pYCbCr, // in, Y: 256 or 64 bytes; Cb: 64 bytes; Cr: 64 bytes
578 struct RGB
579 *pYUV, // out, BGR format, 16*16*3 = 768 bytes; or 8*8*3=192 bytes
580 unsigned char
581 *pBgr // out, BGR format, 16*16*3 = 768 bytes; or 8*8*3=192 bytes
582 ) {
583 int i, j, pos, m, n;
584 unsigned char cb, cr, *py, *pcb, *pcr, *py420[4];
585 int y;
586 struct RGB *pByte;
587 int nBlocksInMcu = 6;
588 unsigned int pixel_x, pixel_y;
589
590 pByte = (struct RGB *)pBgr;
591 if (yuvmode == YuvMode::YUV444) {
592 py = pYCbCr;
593 pcb = pYCbCr + 64;
594 pcr = pcb + 64;
595
596 pixel_x = txb * 8;
597 pixel_y = tyb * 8;
598 pos = (pixel_y * WIDTH) + pixel_x;
599
600 for (j = 0; j < 8; j++) {
601 for (i = 0; i < 8; i++) {
602 m = ((j << 3) + i);
603 n = pos + i;
604 y = pYUV[n].G + (py[m] - 128);
605 cb = pYUV[n].B + (pcb[m] - 128);
606 cr = pYUV[n].R + (pcr[m] - 128);
607 pYUV[n].B = cb;
608 pYUV[n].G = y;
609 pYUV[n].R = cr;
Ed Tanous1ff48782017-04-18 12:45:08 -0700610 pByte[n].B = clamp(m_Y[y] + m_CbToB[cb], 0, 0xFF);
611 pByte[n].G = clamp(m_Y[y] + m_CbToG[cb] + m_CrToG[cr], 0, 0xFF);
612 pByte[n].R = clamp(m_Y[y] + m_CrToR[cr], 0, 0xFF);
Ed Tanous93f987d2017-04-17 17:52:36 -0700613 }
614 pos += WIDTH;
615 }
616 } else {
617 for (i = 0; i < nBlocksInMcu - 2; i++) py420[i] = pYCbCr + i * 64;
618 pcb = pYCbCr + (nBlocksInMcu - 2) * 64;
619 pcr = pcb + 64;
620
621 pixel_x = txb * 16;
622 pixel_y = tyb * 16;
623 pos = (pixel_y * WIDTH) + pixel_x;
624
625 for (j = 0; j < 16; j++) {
626 for (i = 0; i < 16; i++) {
627 // block number is ((j/8) * 2 + i/8)={0, 1, 2, 3}
628 y = *(py420[(j >> 3) * 2 + (i >> 3)]++);
629 m = ((j >> 1) << 3) + (i >> 1);
630 cb = pcb[m];
631 cr = pcr[m];
632 n = pos + i;
Ed Tanous1ff48782017-04-18 12:45:08 -0700633 pByte[n].B = clamp(m_Y[y] + m_CbToB[cb], 0, 0xFF);
634 pByte[n].G = clamp(m_Y[y] + m_CbToG[cb] + m_CrToG[cr], 0, 0xFF);
635 pByte[n].R = clamp(m_Y[y] + m_CrToR[cr], 0, 0xFF);
Ed Tanous93f987d2017-04-17 17:52:36 -0700636 }
637 pos += WIDTH;
638 }
639 }
640 }
Ed Tanous1ff48782017-04-18 12:45:08 -0700641 void Decompress(int txb, int tyb, char *outBuf, uint8_t QT_TableSelection) {
Ed Tanous93f987d2017-04-17 17:52:36 -0700642 unsigned char *ptr;
643 unsigned char byTileYuv[768] = {};
644
645 memset(DCT_coeff, 0, 384 * 2);
646 ptr = byTileYuv;
647 process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 0);
648 IDCT_transform(DCT_coeff, ptr, QT_TableSelection);
649 ptr += 64;
650
651 if (yuvmode == YuvMode::YUV420) {
652 process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 64);
653 IDCT_transform(DCT_coeff + 64, ptr, QT_TableSelection);
654 ptr += 64;
655
656 process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 128);
657 IDCT_transform(DCT_coeff + 128, ptr, QT_TableSelection);
658 ptr += 64;
659
660 process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 192);
661 IDCT_transform(DCT_coeff + 192, ptr, QT_TableSelection);
662 ptr += 64;
663
664 process_Huffman_data_unit(CbDC_nr, CbAC_nr, &DCCb, 256);
665 IDCT_transform(DCT_coeff + 256, ptr, QT_TableSelection + 1);
666 ptr += 64;
667
668 process_Huffman_data_unit(CrDC_nr, CrAC_nr, &DCCr, 320);
669 IDCT_transform(DCT_coeff + 320, ptr, QT_TableSelection + 1);
670 } else {
671 process_Huffman_data_unit(CbDC_nr, CbAC_nr, &DCCb, 64);
672 IDCT_transform(DCT_coeff + 64, ptr, QT_TableSelection + 1);
673 ptr += 64;
674
675 process_Huffman_data_unit(CrDC_nr, CrAC_nr, &DCCr, 128);
676 IDCT_transform(DCT_coeff + 128, ptr, QT_TableSelection + 1);
677 }
678
679 // YUVToRGB (txb, tyb, byTileYuv, (unsigned char *)outBuf);
680 // YUVBuffer for YUV record
681 YUVToRGB(txb, tyb, byTileYuv, YUVBuffer.data(), (unsigned char *)outBuf);
Ed Tanous93f987d2017-04-17 17:52:36 -0700682 }
683
Ed Tanous1ff48782017-04-18 12:45:08 -0700684 void Decompress_2PASS(int txb, int tyb, char *outBuf,
685 uint8_t QT_TableSelection) {
Ed Tanous93f987d2017-04-17 17:52:36 -0700686 unsigned char *ptr;
687 unsigned char byTileYuv[768];
688 memset(DCT_coeff, 0, 384 * 2);
689
690 ptr = byTileYuv;
691 process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 0);
692 IDCT_transform(DCT_coeff, ptr, QT_TableSelection);
693 ptr += 64;
694
695 process_Huffman_data_unit(CbDC_nr, CbAC_nr, &DCCb, 64);
696 IDCT_transform(DCT_coeff + 64, ptr, QT_TableSelection + 1);
697 ptr += 64;
698
699 process_Huffman_data_unit(CrDC_nr, CrAC_nr, &DCCr, 128);
700 IDCT_transform(DCT_coeff + 128, ptr, QT_TableSelection + 1);
701
702 YUVToBuffer(txb, tyb, byTileYuv, YUVBuffer.data(), (unsigned char *)outBuf);
703 // YUVToRGB (txb, tyb, byTileYuv, (unsigned char *)outBuf);
Ed Tanous93f987d2017-04-17 17:52:36 -0700704 }
705
Ed Tanous1ff48782017-04-18 12:45:08 -0700706 void VQ_Decompress(int txb, int tyb, char *outBuf, uint8_t QT_TableSelection,
707 struct COLOR_CACHE *VQ) {
Ed Tanous93f987d2017-04-17 17:52:36 -0700708 unsigned char *ptr, i;
709 unsigned char byTileYuv[192];
710 int Data;
711
712 ptr = byTileYuv;
713 if (VQ->BitMapBits == 0) {
714 for (i = 0; i < 64; i++) {
715 ptr[0] = (VQ->Color[VQ->Index[0]] & 0xFF0000) >> 16;
716 ptr[64] = (VQ->Color[VQ->Index[0]] & 0x00FF00) >> 8;
717 ptr[128] = VQ->Color[VQ->Index[0]] & 0x0000FF;
718 ptr += 1;
719 }
720 } else {
721 for (i = 0; i < 64; i++) {
722 Data = (int)lookKbits(VQ->BitMapBits);
723 ptr[0] = (VQ->Color[VQ->Index[Data]] & 0xFF0000) >> 16;
724 ptr[64] = (VQ->Color[VQ->Index[Data]] & 0x00FF00) >> 8;
725 ptr[128] = VQ->Color[VQ->Index[Data]] & 0x0000FF;
726 ptr += 1;
727 skipKbits(VQ->BitMapBits);
728 }
729 }
730 // YUVToRGB (txb, tyb, byTileYuv, (unsigned char *)outBuf);
731 YUVToRGB(txb, tyb, byTileYuv, YUVBuffer.data(), (unsigned char *)outBuf);
Ed Tanous93f987d2017-04-17 17:52:36 -0700732 }
733
734 void MoveBlockIndex(void) {
735 if (yuvmode == YuvMode::YUV444) {
736 txb++;
Ed Tanous1ff48782017-04-18 12:45:08 -0700737 if (txb >= (int)(WIDTH / 8)) {
Ed Tanous93f987d2017-04-17 17:52:36 -0700738 tyb++;
Ed Tanous1ff48782017-04-18 12:45:08 -0700739 if (tyb >= (int)(HEIGHT / 8)) tyb = 0;
Ed Tanous93f987d2017-04-17 17:52:36 -0700740 txb = 0;
741 }
742 } else {
743 txb++;
Ed Tanous1ff48782017-04-18 12:45:08 -0700744 if (txb >= (int)(WIDTH / 16)) {
Ed Tanous93f987d2017-04-17 17:52:36 -0700745 tyb++;
Ed Tanous1ff48782017-04-18 12:45:08 -0700746 if (tyb >= (int)(HEIGHT / 16)) tyb = 0;
Ed Tanous93f987d2017-04-17 17:52:36 -0700747 txb = 0;
748 }
749 }
750 }
751
752 void VQ_Initialize(struct COLOR_CACHE *VQ) {
753 int i;
754
755 for (i = 0; i < 4; i++) {
756 VQ->Index[i] = i;
757 }
758 VQ->Color[0] = 0x008080;
759 VQ->Color[1] = 0xFF8080;
760 VQ->Color[2] = 0x808080;
761 VQ->Color[3] = 0xC08080;
762 }
Ed Tanous93f987d2017-04-17 17:52:36 -0700763
764 void Init_Color_Table() {
765 int i, x;
766 int nScale = 1L << 16; // equal to power(2,16)
767 int nHalf = nScale >> 1;
768
769#define FIX(x) ((int)((x)*nScale + 0.5))
770
771 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
772 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
773 /* Cr=>R value is nearest int to 1.597656 * x */
774 /* Cb=>B value is nearest int to 2.015625 * x */
775 /* Cr=>G value is scaled-up -0.8125 * x */
776 /* Cb=>G value is scaled-up -0.390625 * x */
777 for (i = 0, x = -128; i < 256; i++, x++) {
778 m_CrToR[i] = (int)(FIX(1.597656) * x + nHalf) >> 16;
779 m_CbToB[i] = (int)(FIX(2.015625) * x + nHalf) >> 16;
780 m_CrToG[i] = (int)(-FIX(0.8125) * x + nHalf) >> 16;
781 m_CbToG[i] = (int)(-FIX(0.390625) * x + nHalf) >> 16;
782 }
783 for (i = 0, x = -16; i < 256; i++, x++) {
784 m_Y[i] = (int)(FIX(1.164) * x + nHalf) >> 16;
785 }
786 // For Color Text Enchance Y Re-map. Recommend to disable in default
787 /*
788 for (i = 0; i < (VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate);
789 i++) {
790 temp = (double)i /
791 VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate;
792 temp1 = 1.0 / VideoEngineInfo->INFData.Gamma1Parameter;
793 m_Y[i] =
794 (BYTE)(VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate * pow (temp,
795 temp1));
796 if (m_Y[i] > 255) m_Y[i] = 255;
797 }
798 for (i = (VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate); i < 256;
799 i++) {
800 m_Y[i] =
801 (BYTE)((VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate) + (256 -
802 VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate) * ( pow((double)((i -
803 VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate) / (256 -
804 (VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate))), (1.0 /
805 VideoEngineInfo->INFData.Gamma2Parameter)) ));
806 if (m_Y[i] > 255) m_Y[i] = 255;
807 }
808 */
809 }
810 void load_Huffman_table(Huffman_table *HT, unsigned char *nrcode,
811 unsigned char *value, unsigned short int *Huff_code) {
812 unsigned char k, j, i;
813 unsigned int code, code_index;
814
815 for (j = 1; j <= 16; j++) {
816 HT->Length[j] = nrcode[j];
817 }
818 for (i = 0, k = 1; k <= 16; k++)
819 for (j = 0; j < HT->Length[k]; j++) {
820 HT->V[WORD_hi_lo(k, j)] = value[i];
821 i++;
822 }
823
824 code = 0;
825 for (k = 1; k <= 16; k++) {
826 HT->minor_code[k] = (unsigned short int)code;
827 for (j = 1; j <= HT->Length[k]; j++) code++;
828 HT->major_code[k] = (unsigned short int)(code - 1);
829 code *= 2;
830 if (HT->Length[k] == 0) {
831 HT->minor_code[k] = 0xFFFF;
832 HT->major_code[k] = 0;
833 }
834 }
835
836 HT->Len[0] = 2;
837 i = 2;
838
839 for (code_index = 1; code_index < 65535; code_index++) {
840 if (code_index < Huff_code[i]) {
841 HT->Len[code_index] = (unsigned char)Huff_code[i + 1];
842 } else {
843 i = i + 2;
844 HT->Len[code_index] = (unsigned char)Huff_code[i + 1];
845 }
846 }
847 }
848 void init_jpg_table() {
Ed Tanous93f987d2017-04-17 17:52:36 -0700849 Init_Color_Table();
850 prepare_range_limit_table();
851 load_Huffman_table(&HTDC[0], std_dc_luminance_nrcodes,
852 std_dc_luminance_values, DC_LUMINANCE_HUFFMANCODE);
853 load_Huffman_table(&HTAC[0], std_ac_luminance_nrcodes,
854 std_ac_luminance_values, AC_LUMINANCE_HUFFMANCODE);
855 load_Huffman_table(&HTDC[1], std_dc_chrominance_nrcodes,
856 std_dc_chrominance_values, DC_CHROMINANCE_HUFFMANCODE);
857 load_Huffman_table(&HTAC[1], std_ac_chrominance_nrcodes,
858 std_ac_chrominance_values, AC_CHROMINANCE_HUFFMANCODE);
859 }
860
861 void prepare_range_limit_table()
862 /* Allocate and fill in the sample_range_limit table */
863 {
864 int j;
865 rlimit_table = (unsigned char *)malloc(5 * 256L + 128);
866 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
867 memset((void *)rlimit_table, 0, 256);
868 rlimit_table += 256; /* allow negative subscripts of simple table */
869 /* Main part of "simple" table: limit[x] = x */
870 for (j = 0; j < 256; j++) rlimit_table[j] = j;
871 /* End of simple table, rest of first half of post-IDCT table */
872 for (j = 256; j < 640; j++) rlimit_table[j] = 255;
873
874 /* Second half of post-IDCT table */
875 memset((void *)(rlimit_table + 640), 0, 384);
876 for (j = 0; j < 128; j++) rlimit_table[j + 1024] = j;
877 }
878
879 inline unsigned short int WORD_hi_lo(uint8_t byte_high, uint8_t byte_low) {
880 return (byte_high << 8) + byte_low;
881 }
882
883 // river
884 void process_Huffman_data_unit(uint8_t DC_nr, uint8_t AC_nr,
885 signed short int *previous_DC,
886 unsigned short int position) {
887 uint8_t nr = 0;
888 uint8_t k;
889 unsigned short int tmp_Hcode;
890 uint8_t size_val, count_0;
891 unsigned short int *min_code;
892 uint8_t *huff_values;
893 uint8_t byte_temp;
894
895 min_code = HTDC[DC_nr].minor_code;
896 // maj_code=HTDC[DC_nr].major_code;
897 huff_values = HTDC[DC_nr].V;
898
899 // DC
900 k = HTDC[DC_nr].Len[(unsigned short int)(codebuf >> 16)];
901 // river
902 // tmp_Hcode=lookKbits(k);
903 tmp_Hcode = (unsigned short int)(codebuf >> (32 - k));
904 skipKbits(k);
905 size_val = huff_values[WORD_hi_lo(k, (uint8_t)(tmp_Hcode - min_code[k]))];
906 if (size_val == 0)
907 DCT_coeff[position + 0] = *previous_DC;
908 else {
909 DCT_coeff[position + 0] = *previous_DC + getKbits(size_val);
910 *previous_DC = DCT_coeff[position + 0];
911 }
912
913 // Second, AC coefficient decoding
914 min_code = HTAC[AC_nr].minor_code;
915 // maj_code=HTAC[AC_nr].major_code;
916 huff_values = HTAC[AC_nr].V;
917
918 nr = 1; // AC coefficient
919 do {
920 k = HTAC[AC_nr].Len[(unsigned short int)(codebuf >> 16)];
921 tmp_Hcode = (unsigned short int)(codebuf >> (32 - k));
922 skipKbits(k);
923
924 byte_temp =
925 huff_values[WORD_hi_lo(k, (uint8_t)(tmp_Hcode - min_code[k]))];
926 size_val = byte_temp & 0xF;
927 count_0 = byte_temp >> 4;
928 if (size_val == 0) {
929 if (count_0 != 0xF) {
930 break;
931 }
932 nr += 16;
933 } else {
934 nr += count_0; // skip count_0 zeroes
935 DCT_coeff[position + dezigzag[nr++]] = getKbits(size_val);
936 }
937 } while (nr < 64);
938 }
939
940 unsigned short int lookKbits(uint8_t k) {
941 unsigned short int revcode;
942
943 revcode = (unsigned short int)(codebuf >> (32 - k));
944
945 return (revcode);
946 }
947
948 void skipKbits(uint8_t k) {
949 unsigned long readbuf;
950
951 if ((newbits - k) <= 0) {
952 readbuf = Buffer[buffer_index];
953 buffer_index++;
954 codebuf =
955 (codebuf << k) | ((newbuf | (readbuf >> (newbits))) >> (32 - k));
956 newbuf = readbuf << (k - newbits);
957 newbits = 32 + newbits - k;
958 } else {
959 codebuf = (codebuf << k) | (newbuf >> (32 - k));
960 newbuf = newbuf << k;
961 newbits -= k;
962 }
963 }
964
965 signed short int getKbits(uint8_t k) {
966 signed short int signed_wordvalue;
967
968 // river
969 // signed_wordvalue=lookKbits(k);
970 signed_wordvalue = (unsigned short int)(codebuf >> (32 - k));
971 if (((1L << (k - 1)) & signed_wordvalue) == 0) {
972 // neg_pow2 was previously defined as the below. It seemed silly to keep
973 // a table of values around for something
974 // THat's relatively easy to compute, so it was replaced with the
975 // appropriate math
976 // signed_wordvalue = signed_wordvalue - (0xFFFF >> (16 - k));
977 std::array<signed short int, 17> neg_pow2 = {
978 0, -1, -3, -7, -15, -31, -63, -127,
979 -255, -511, -1023, -2047, -4095, -8191, -16383, -32767};
980
981 signed_wordvalue = signed_wordvalue + neg_pow2[k];
982 }
983 skipKbits(k);
984 return signed_wordvalue;
985 }
986 int init_JPG_decoding() {
987 byte_pos = 0;
988 load_quant_table(QT[0]);
989 load_quant_tableCb(QT[1]);
990 // Note: Added for Dual-JPEG
991 load_advance_quant_table(QT[2]);
992 load_advance_quant_tableCb(QT[3]);
993 return 1;
994 }
995
996 void set_quant_table(uint8_t *basic_table, uint8_t scale_factor,
997 uint8_t *newtable)
998 // Set quantization table and zigzag reorder it
999 {
1000 uint8_t i;
1001 long temp;
1002 for (i = 0; i < 64; i++) {
1003 temp = ((long)(basic_table[i] * 16) / scale_factor);
1004 /* limit the values to the valid range */
1005 if (temp <= 0L) temp = 1L;
1006 if (temp > 255L) temp = 255L; /* limit to baseline range if requested */
1007 newtable[zigzag[i]] = (uint8_t)temp;
1008 }
1009 }
1010
1011 void updatereadbuf(uint32_t *codebuf, uint32_t *newbuf, int walks,
1012 int *newbits, std::vector<uint32_t> &Buffer) {
1013 unsigned long readbuf;
1014
1015 if ((*newbits - walks) <= 0) {
1016 readbuf = Buffer[buffer_index];
1017 buffer_index++;
1018 *codebuf = (*codebuf << walks) |
1019 ((*newbuf | (readbuf >> (*newbits))) >> (32 - walks));
1020 *newbuf = readbuf << (walks - *newbits);
1021 *newbits = 32 + *newbits - walks;
1022 } else {
1023 *codebuf = (*codebuf << walks) | (*newbuf >> (32 - walks));
1024 *newbuf = *newbuf << walks;
1025 *newbits -= walks;
1026 }
1027 }
1028
1029 uint32_t decode(std::vector<uint32_t> &buffer, unsigned long width,
1030 unsigned long height, YuvMode yuvmode_in, int y_selector,
1031 int uv_selector) {
1032 uint32_t i;
1033 COLOR_CACHE Decode_Color;
1034
Ed Tanous1ff48782017-04-18 12:45:08 -07001035 if (width != WIDTH || height != HEIGHT || yuvmode_in != yuvmode ||
1036 y_selector != Y_selector || uv_selector != UV_selector) {
1037 init_JPG_decoding();
1038 }
1039
Ed Tanous93f987d2017-04-17 17:52:36 -07001040 // TODO(ed) use the enum everywhere, not just externally
Ed Tanous1ff48782017-04-18 12:45:08 -07001041 yuvmode = yuvmode_in; // 0 = YUV444, 1 = YUV420
Ed Tanous93f987d2017-04-17 17:52:36 -07001042 Y_selector = y_selector; // 0-7
1043 UV_selector = uv_selector; // 0-7
1044
1045 // TODO(ed) Magic number section. Document appropriately
1046 advance_selector = 0; // 0-7
Ed Tanous93f987d2017-04-17 17:52:36 -07001047 Mapping = 0; // 0 or 1
Ed Tanous93f987d2017-04-17 17:52:36 -07001048
Ed Tanous93f987d2017-04-17 17:52:36 -07001049 WIDTH = width;
1050 HEIGHT = height;
1051
1052 VQ_Initialize(&Decode_Color);
1053 // OutputDebugString ("In decode\n");
1054 // GetINFData (VideoEngineInfo);
Ed Tanous93f987d2017-04-17 17:52:36 -07001055 // AST2000 JPEG block is 16x16(pixels) base
Ed Tanous1ff48782017-04-18 12:45:08 -07001056
Ed Tanous93f987d2017-04-17 17:52:36 -07001057 if (yuvmode == YuvMode::YUV420) {
Ed Tanous1ff48782017-04-18 12:45:08 -07001058 auto remainder = WIDTH % 16;
Ed Tanous93f987d2017-04-17 17:52:36 -07001059 if (WIDTH % 16) {
Ed Tanous1ff48782017-04-18 12:45:08 -07001060 WIDTH = WIDTH + 16 - remainder;
Ed Tanous93f987d2017-04-17 17:52:36 -07001061 }
Ed Tanous1ff48782017-04-18 12:45:08 -07001062 remainder = HEIGHT % 16;
1063 if (remainder) {
1064 HEIGHT = HEIGHT + 16 - remainder;
Ed Tanous93f987d2017-04-17 17:52:36 -07001065 }
1066 } else {
1067 if (WIDTH % 8) {
1068 WIDTH = WIDTH + 8 - (WIDTH % 8);
1069 }
1070 if (HEIGHT % 8) {
1071 HEIGHT = HEIGHT + 8 - (HEIGHT % 8);
1072 }
1073 }
1074
Ed Tanous93f987d2017-04-17 17:52:36 -07001075 int qfactor = 16;
1076
1077 SCALEFACTOR = qfactor;
1078 SCALEFACTORUV = qfactor;
1079 ADVANCESCALEFACTOR = 16;
1080 ADVANCESCALEFACTORUV = 16;
1081
Ed Tanous93f987d2017-04-17 17:52:36 -07001082 // TODO(ed) cleanup cruft
1083 Buffer = buffer.data();
1084
1085 codebuf = buffer[0];
1086 newbuf = buffer[1];
1087 buffer_index = 2;
1088
1089 txb = tyb = 0;
1090 newbits = 32;
1091 DCY = DCCb = DCCr = 0;
1092
Ed Tanous1ff48782017-04-18 12:45:08 -07001093 static const uint32_t VQ_HEADER_MASK = 0x01;
1094 static const uint32_t VQ_NO_UPDATE_HEADER = 0x00;
1095 static const uint32_t VQ_UPDATE_HEADER = 0x01;
1096 static const int VQ_NO_UPDATE_LENGTH = 0x03;
1097 static const int VQ_UPDATE_LENGTH = 0x1B;
1098 static const uint32_t VQ_INDEX_MASK = 0x03;
1099 static const uint32_t VQ_COLOR_MASK = 0xFFFFFF;
1100
1101 static const int BLOCK_AST2100_START_LENGTH = 0x04;
1102 static const int BLOCK_AST2100_SKIP_LENGTH = 20; // S:1 H:3 X:8 Y:8
1103
Ed Tanous93f987d2017-04-17 17:52:36 -07001104 do {
1105 auto block_header = static_cast<JpgBlock>((codebuf >> 28) & 0xFF);
1106 switch (block_header) {
1107 case JpgBlock::JPEG_NO_SKIP_CODE:
1108 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits,
1109 buffer);
1110 Decompress(txb, tyb, (char *)OutBuffer.data(), 0);
1111 break;
1112 case JpgBlock::FRAME_END_CODE:
1113 return 0;
1114 break;
1115 case JpgBlock::JPEG_SKIP_CODE:
1116
1117 txb = (codebuf & 0x0FF00000) >> 20;
1118 tyb = (codebuf & 0x0FF000) >> 12;
1119
1120 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits,
1121 buffer);
1122 Decompress(txb, tyb, (char *)OutBuffer.data(), 0);
1123 break;
1124 case JpgBlock::VQ_NO_SKIP_1_COLOR_CODE:
1125 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits,
1126 buffer);
1127 Decode_Color.BitMapBits = 0;
1128
1129 for (i = 0; i < 1; i++) {
1130 Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK);
1131 if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) {
1132 updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits,
1133 buffer);
1134 } else {
1135 Decode_Color.Color[Decode_Color.Index[i]] =
1136 ((codebuf >> 5) & VQ_COLOR_MASK);
1137 updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits,
1138 buffer);
1139 }
1140 }
1141 VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color);
1142 break;
1143 case JpgBlock::VQ_SKIP_1_COLOR_CODE:
1144 txb = (codebuf & 0x0FF00000) >> 20;
1145 tyb = (codebuf & 0x0FF000) >> 12;
1146
1147 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits,
1148 buffer);
1149 Decode_Color.BitMapBits = 0;
1150
1151 for (i = 0; i < 1; i++) {
1152 Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK);
1153 if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) {
1154 updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits,
1155 buffer);
1156 } else {
1157 Decode_Color.Color[Decode_Color.Index[i]] =
1158 ((codebuf >> 5) & VQ_COLOR_MASK);
1159 updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits,
1160 buffer);
1161 }
1162 }
1163 VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color);
1164 break;
1165
1166 case JpgBlock::VQ_NO_SKIP_2_COLOR_CODE:
1167 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits,
1168 buffer);
1169 Decode_Color.BitMapBits = 1;
1170
1171 for (i = 0; i < 2; i++) {
1172 Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK);
1173 if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) {
1174 updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits,
1175 buffer);
1176 } else {
1177 Decode_Color.Color[Decode_Color.Index[i]] =
1178 ((codebuf >> 5) & VQ_COLOR_MASK);
1179 updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits,
1180 buffer);
1181 }
1182 }
1183 VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color);
1184 break;
1185 case JpgBlock::VQ_SKIP_2_COLOR_CODE:
1186 txb = (codebuf & 0x0FF00000) >> 20;
1187 tyb = (codebuf & 0x0FF000) >> 12;
1188
1189 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits,
1190 buffer);
1191 Decode_Color.BitMapBits = 1;
1192
1193 for (i = 0; i < 2; i++) {
1194 Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK);
1195 if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) {
1196 updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits,
1197 buffer);
1198 } else {
1199 Decode_Color.Color[Decode_Color.Index[i]] =
1200 ((codebuf >> 5) & VQ_COLOR_MASK);
1201 updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits,
1202 buffer);
1203 }
1204 }
1205 VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color);
1206
1207 break;
1208 case JpgBlock::VQ_NO_SKIP_4_COLOR_CODE:
1209 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits,
1210 buffer);
1211 Decode_Color.BitMapBits = 2;
1212
1213 for (i = 0; i < 4; i++) {
1214 Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK);
1215 if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) {
1216 updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits,
1217 buffer);
1218 } else {
1219 Decode_Color.Color[Decode_Color.Index[i]] =
1220 ((codebuf >> 5) & VQ_COLOR_MASK);
1221 updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits,
1222 buffer);
1223 }
1224 }
1225 VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color);
1226
1227 break;
1228
1229 case JpgBlock::VQ_SKIP_4_COLOR_CODE:
1230 txb = (codebuf & 0x0FF00000) >> 20;
1231 tyb = (codebuf & 0x0FF000) >> 12;
1232
1233 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits,
1234 buffer);
1235 Decode_Color.BitMapBits = 2;
1236
1237 for (i = 0; i < 4; i++) {
1238 Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK);
1239 if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) {
1240 updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits,
1241 buffer);
1242 } else {
1243 Decode_Color.Color[Decode_Color.Index[i]] =
1244 ((codebuf >> 5) & VQ_COLOR_MASK);
1245 updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits,
1246 buffer);
1247 }
1248 }
1249 VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color);
1250
1251 break;
1252 case JpgBlock::JPEG_SKIP_PASS2_CODE:
1253 txb = (codebuf & 0x0FF00000) >> 20;
1254 tyb = (codebuf & 0x0FF000) >> 12;
1255
1256 updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits,
1257 buffer);
1258 Decompress_2PASS(txb, tyb, (char *)OutBuffer.data(), 2);
1259
1260 break;
1261 default:
1262 // TODO(ed) propogate errors upstream
1263 return -1;
1264 break;
1265 }
1266 MoveBlockIndex();
1267
1268 } while (buffer_index <= buffer.size());
1269
1270 return -1;
1271 }
1272
1273#ifdef cimg_version
1274 void dump_to_bitmap_file() {
1275 cimg_library::CImg<unsigned char> image(WIDTH, HEIGHT, 1, 3);
1276 for (int y = 0; y < WIDTH; y++) {
1277 for (int x = 0; x < HEIGHT; x++) {
1278 auto pixel = OutBuffer[x + (y * WIDTH)];
1279 image(x, y, 0) = pixel.R;
1280 image(x, y, 1) = pixel.G;
1281 image(x, y, 2) = pixel.B;
1282 }
1283 }
1284 image.save("/tmp/file2.bmp");
1285 }
1286#endif
1287
1288 private:
1289 YuvMode yuvmode;
1290 // WIDTH and HEIGHT are the modes your display used
1291 unsigned long WIDTH;
1292 unsigned long HEIGHT;
Ed Tanous93f987d2017-04-17 17:52:36 -07001293 unsigned char Y_selector;
1294 int SCALEFACTOR;
1295 int SCALEFACTORUV;
1296 int ADVANCESCALEFACTOR;
1297 int ADVANCESCALEFACTORUV;
1298 int Mapping;
1299 unsigned char UV_selector;
1300 unsigned char advance_selector;
Ed Tanous93f987d2017-04-17 17:52:36 -07001301 int byte_pos; // current byte position
1302
1303 // quantization tables, no more than 4 quantization tables
1304 std::array<std::array<long, 64>, 4> QT;
1305
1306 // DC huffman tables , no more than 4 (0..3)
1307 std::array<Huffman_table, 4> HTDC;
1308 // AC huffman tables (0..3)
1309 std::array<Huffman_table, 4> HTAC;
1310 std::array<int, 256> m_CrToR;
1311 std::array<int, 256> m_CbToB;
1312 std::array<int, 256> m_CrToG;
1313 std::array<int, 256> m_CbToG;
1314 std::array<int, 256> m_Y;
1315 unsigned long buffer_index;
1316 uint32_t codebuf, newbuf, readbuf;
1317 uint8_t *std_luminance_qt;
1318 uint8_t *std_chrominance_qt;
1319
1320 signed short int DCY, DCCb, DCCr; // Coeficientii DC pentru Y,Cb,Cr
1321 signed short int DCT_coeff[384];
1322 // std::vector<signed short int> DCT_coeff; // Current DCT_coefficients
1323 // quantization table number for Y, Cb, Cr
1324 uint8_t YQ_nr = 0, CbQ_nr = 1, CrQ_nr = 1;
1325 // DC Huffman table number for Y,Cb, Cr
1326 uint8_t YDC_nr = 0, CbDC_nr = 1, CrDC_nr = 1;
1327 // AC Huffman table number for Y,Cb, Cr
1328 uint8_t YAC_nr = 0, CbAC_nr = 1, CrAC_nr = 1;
1329 int txb, tyb;
1330 int newbits;
1331 uint8_t *rlimit_table;
1332 std::vector<RGB> YUVBuffer;
1333 // TODO(ed) this shouldn't exist. It is cruft that needs cleaning up'
1334 uint32_t *Buffer;
1335
1336 public:
1337 std::vector<RGB> OutBuffer;
1338};
1339}