| #pragma once |
| |
| #include <cstdint> |
| #include <array> |
| #include <aspeed/JTABLES.H> |
| #include <vector> |
| |
| #include <ast_video_types.hpp> |
| #include <iostream> |
| |
| namespace AstVideo { |
| |
| static const uint32_t VQ_HEADER_MASK = 0x01; |
| static const uint32_t VQ_NO_UPDATE_HEADER = 0x00; |
| static const uint32_t VQ_UPDATE_HEADER = 0x01; |
| static const int VQ_NO_UPDATE_LENGTH = 0x03; |
| static const int VQ_UPDATE_LENGTH = 0x1B; |
| static const uint32_t VQ_INDEX_MASK = 0x03; |
| static const uint32_t VQ_COLOR_MASK = 0xFFFFFF; |
| |
| static const int BLOCK_AST2100_START_LENGTH = 0x04; |
| static const int BLOCK_AST2100_SKIP_LENGTH = 20; // S:1 H:3 X:8 Y:8 |
| |
| struct COLOR_CACHE { |
| unsigned long Color[4]; |
| unsigned char Index[4]; |
| unsigned char BitMapBits; |
| }; |
| |
| struct RGB { |
| unsigned char B; |
| unsigned char G; |
| unsigned char R; |
| unsigned char Reserved; |
| }; |
| |
| enum class JpgBlock { |
| JPEG_NO_SKIP_CODE = 0x00, |
| JPEG_SKIP_CODE = 0x08, |
| |
| JPEG_PASS2_CODE = 0x02, |
| JPEG_SKIP_PASS2_CODE = 0x0A, |
| |
| LOW_JPEG_NO_SKIP_CODE = 0x04, |
| LOW_JPEG_SKIP_CODE = 0x0C, |
| |
| VQ_NO_SKIP_1_COLOR_CODE = 0x05, |
| VQ_SKIP_1_COLOR_CODE = 0x0D, |
| |
| VQ_NO_SKIP_2_COLOR_CODE = 0x06, |
| VQ_SKIP_2_COLOR_CODE = 0x0E, |
| |
| VQ_NO_SKIP_4_COLOR_CODE = 0x07, |
| VQ_SKIP_4_COLOR_CODE = 0x0F, |
| |
| FRAME_END_CODE = 0x09, |
| |
| }; |
| |
| class AstJpegDecoder { |
| public: |
| AstJpegDecoder() { |
| // TODO(ed) figure out how to init this in the constructor |
| YUVBuffer.resize(800 * 600); |
| OutBuffer.resize(800 * 600); |
| for (auto &r : OutBuffer) { |
| r.R = 0x00; |
| r.G = 0x00; |
| r.B = 0x00; |
| r.Reserved = 0xAA; |
| } |
| init_jpg_table(); |
| } |
| |
| void load_quant_table(std::array<long, 64> &quant_table) { |
| float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f, |
| 1.0f, 0.785694958f, 0.541196100f, 0.275899379f}; |
| uint8_t j, row, col; |
| uint8_t tempQT[64]; |
| |
| // Load quantization coefficients from JPG file, scale them for DCT and |
| // reorder |
| // from zig-zag order |
| switch (Y_selector) { |
| case 0: |
| std_luminance_qt = Tbl_000Y; |
| break; |
| case 1: |
| std_luminance_qt = Tbl_014Y; |
| break; |
| case 2: |
| std_luminance_qt = Tbl_029Y; |
| break; |
| case 3: |
| std_luminance_qt = Tbl_043Y; |
| break; |
| case 4: |
| std_luminance_qt = Tbl_057Y; |
| break; |
| case 5: |
| std_luminance_qt = Tbl_071Y; |
| break; |
| case 6: |
| std_luminance_qt = Tbl_086Y; |
| break; |
| case 7: |
| std_luminance_qt = Tbl_100Y; |
| break; |
| } |
| set_quant_table(std_luminance_qt, (uint8_t)SCALEFACTOR, tempQT); |
| |
| for (j = 0; j <= 63; j++) quant_table[j] = tempQT[zigzag[j]]; |
| j = 0; |
| for (row = 0; row <= 7; row++) |
| for (col = 0; col <= 7; col++) { |
| quant_table[j] = |
| (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) * |
| 65536); |
| j++; |
| } |
| byte_pos += 64; |
| } |
| |
| void load_quant_tableCb(std::array<long, 64> &quant_table) { |
| float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f, |
| 1.0f, 0.785694958f, 0.541196100f, 0.275899379f}; |
| uint8_t j, row, col; |
| uint8_t tempQT[64]; |
| |
| // Load quantization coefficients from JPG file, scale them for DCT and |
| // reorder from zig-zag order |
| if (Mapping == 0) { |
| switch (UV_selector) { |
| case 0: |
| std_chrominance_qt = Tbl_000Y; |
| break; |
| case 1: |
| std_chrominance_qt = Tbl_014Y; |
| break; |
| case 2: |
| std_chrominance_qt = Tbl_029Y; |
| break; |
| case 3: |
| std_chrominance_qt = Tbl_043Y; |
| break; |
| case 4: |
| std_chrominance_qt = Tbl_057Y; |
| break; |
| case 5: |
| std_chrominance_qt = Tbl_071Y; |
| break; |
| case 6: |
| std_chrominance_qt = Tbl_086Y; |
| break; |
| case 7: |
| std_chrominance_qt = Tbl_100Y; |
| break; |
| } |
| } else { |
| switch (UV_selector) { |
| case 0: |
| std_chrominance_qt = Tbl_000UV; |
| break; |
| case 1: |
| std_chrominance_qt = Tbl_014UV; |
| break; |
| case 2: |
| std_chrominance_qt = Tbl_029UV; |
| break; |
| case 3: |
| std_chrominance_qt = Tbl_043UV; |
| break; |
| case 4: |
| std_chrominance_qt = Tbl_057UV; |
| break; |
| case 5: |
| std_chrominance_qt = Tbl_071UV; |
| break; |
| case 6: |
| std_chrominance_qt = Tbl_086UV; |
| break; |
| case 7: |
| std_chrominance_qt = Tbl_100UV; |
| break; |
| } |
| } |
| set_quant_table(std_chrominance_qt, (uint8_t)SCALEFACTORUV, tempQT); |
| |
| for (j = 0; j <= 63; j++) { |
| quant_table[j] = tempQT[zigzag[j]]; |
| } |
| j = 0; |
| for (row = 0; row <= 7; row++) { |
| for (col = 0; col <= 7; col++) { |
| quant_table[j] = |
| (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) * |
| 65536); |
| j++; |
| } |
| } |
| byte_pos += 64; |
| } |
| // Note: Added for Dual_JPEG |
| void load_advance_quant_table(std::array<long, 64> &quant_table) { |
| float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f, |
| 1.0f, 0.785694958f, 0.541196100f, 0.275899379f}; |
| uint8_t j, row, col; |
| uint8_t tempQT[64]; |
| |
| // Load quantization coefficients from JPG file, scale them for DCT and |
| // reorder |
| // from zig-zag order |
| switch (advance_selector) { |
| case 0: |
| std_luminance_qt = Tbl_000Y; |
| break; |
| case 1: |
| std_luminance_qt = Tbl_014Y; |
| break; |
| case 2: |
| std_luminance_qt = Tbl_029Y; |
| break; |
| case 3: |
| std_luminance_qt = Tbl_043Y; |
| break; |
| case 4: |
| std_luminance_qt = Tbl_057Y; |
| break; |
| case 5: |
| std_luminance_qt = Tbl_071Y; |
| break; |
| case 6: |
| std_luminance_qt = Tbl_086Y; |
| break; |
| case 7: |
| std_luminance_qt = Tbl_100Y; |
| break; |
| } |
| // Note: pass ADVANCE SCALE FACTOR to sub-function in Dual-JPEG |
| set_quant_table(std_luminance_qt, (uint8_t)ADVANCESCALEFACTOR, tempQT); |
| |
| for (j = 0; j <= 63; j++) quant_table[j] = tempQT[zigzag[j]]; |
| j = 0; |
| for (row = 0; row <= 7; row++) |
| for (col = 0; col <= 7; col++) { |
| quant_table[j] = |
| (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) * |
| 65536); |
| j++; |
| } |
| byte_pos += 64; |
| } |
| |
| // Note: Added for Dual-JPEG |
| void load_advance_quant_tableCb(std::array<long, 64> &quant_table) { |
| float scalefactor[8] = {1.0f, 1.387039845f, 1.306562965f, 1.175875602f, |
| 1.0f, 0.785694958f, 0.541196100f, 0.275899379f}; |
| uint8_t j, row, col; |
| uint8_t tempQT[64]; |
| |
| // Load quantization coefficients from JPG file, scale them for DCT and |
| // reorder |
| // from zig-zag order |
| if (Mapping == 1) { |
| switch (advance_selector) { |
| case 0: |
| std_chrominance_qt = Tbl_000Y; |
| break; |
| case 1: |
| std_chrominance_qt = Tbl_014Y; |
| break; |
| case 2: |
| std_chrominance_qt = Tbl_029Y; |
| break; |
| case 3: |
| std_chrominance_qt = Tbl_043Y; |
| break; |
| case 4: |
| std_chrominance_qt = Tbl_057Y; |
| break; |
| case 5: |
| std_chrominance_qt = Tbl_071Y; |
| break; |
| case 6: |
| std_chrominance_qt = Tbl_086Y; |
| break; |
| case 7: |
| std_chrominance_qt = Tbl_100Y; |
| break; |
| } |
| } else { |
| switch (advance_selector) { |
| case 0: |
| std_chrominance_qt = Tbl_000UV; |
| break; |
| case 1: |
| std_chrominance_qt = Tbl_014UV; |
| break; |
| case 2: |
| std_chrominance_qt = Tbl_029UV; |
| break; |
| case 3: |
| std_chrominance_qt = Tbl_043UV; |
| break; |
| case 4: |
| std_chrominance_qt = Tbl_057UV; |
| break; |
| case 5: |
| std_chrominance_qt = Tbl_071UV; |
| break; |
| case 6: |
| std_chrominance_qt = Tbl_086UV; |
| break; |
| case 7: |
| std_chrominance_qt = Tbl_100UV; |
| break; |
| } |
| } |
| // Note: pass ADVANCE SCALE FACTOR to sub-function in Dual-JPEG |
| set_quant_table(std_chrominance_qt, (uint8_t)ADVANCESCALEFACTORUV, tempQT); |
| |
| for (j = 0; j <= 63; j++) quant_table[j] = tempQT[zigzag[j]]; |
| j = 0; |
| for (row = 0; row <= 7; row++) |
| for (col = 0; col <= 7; col++) { |
| quant_table[j] = |
| (long)((quant_table[j] * scalefactor[row] * scalefactor[col]) * |
| 65536); |
| j++; |
| } |
| byte_pos += 64; |
| } |
| |
| void IDCT_transform(short *coef, uint8_t *data, uint8_t nBlock) { |
| #define FIX_1_082392200 ((int)277) /* FIX(1.082392200) */ |
| #define FIX_1_414213562 ((int)362) /* FIX(1.414213562) */ |
| #define FIX_1_847759065 ((int)473) /* FIX(1.847759065) */ |
| #define FIX_2_613125930 ((int)669) /* FIX(2.613125930) */ |
| |
| #define MULTIPLY(var, cons) ((int)((var) * (cons)) >> 8) |
| |
| int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; |
| int tmp10, tmp11, tmp12, tmp13; |
| int z5, z10, z11, z12, z13; |
| int workspace[64]; /* buffers data between passes */ |
| |
| short *inptr = coef; |
| long *quantptr; |
| int *wsptr = workspace; |
| unsigned char *outptr; |
| unsigned char *r_limit = rlimit_table + 128; |
| int ctr, dcval, DCTSIZE = 8; |
| |
| quantptr = &QT[nBlock][0]; |
| |
| // Pass 1: process columns from input (inptr), store into work array(wsptr) |
| |
| for (ctr = 8; ctr > 0; ctr--) { |
| /* Due to quantization, we will usually find that many of the input |
| * coefficients are zero, especially the AC terms. We can exploit this |
| * by short-circuiting the IDCT calculation for any column in which all |
| * the AC terms are zero. In that case each output is equal to the |
| * DC coefficient (with scale factor as needed). |
| * With typical images and quantization tables, half or more of the |
| * column DCT calculations can be simplified this way. |
| */ |
| |
| if ((inptr[DCTSIZE * 1] | inptr[DCTSIZE * 2] | inptr[DCTSIZE * 3] | |
| inptr[DCTSIZE * 4] | inptr[DCTSIZE * 5] | inptr[DCTSIZE * 6] | |
| inptr[DCTSIZE * 7]) == 0) { |
| /* AC terms all zero */ |
| dcval = (int)((inptr[DCTSIZE * 0] * quantptr[DCTSIZE * 0]) >> 16); |
| |
| wsptr[DCTSIZE * 0] = dcval; |
| wsptr[DCTSIZE * 1] = dcval; |
| wsptr[DCTSIZE * 2] = dcval; |
| wsptr[DCTSIZE * 3] = dcval; |
| wsptr[DCTSIZE * 4] = dcval; |
| wsptr[DCTSIZE * 5] = dcval; |
| wsptr[DCTSIZE * 6] = dcval; |
| wsptr[DCTSIZE * 7] = dcval; |
| |
| inptr++; /* advance pointers to next column */ |
| quantptr++; |
| wsptr++; |
| continue; |
| } |
| |
| /* Even part */ |
| |
| tmp0 = (inptr[DCTSIZE * 0] * quantptr[DCTSIZE * 0]) >> 16; |
| tmp1 = (inptr[DCTSIZE * 2] * quantptr[DCTSIZE * 2]) >> 16; |
| tmp2 = (inptr[DCTSIZE * 4] * quantptr[DCTSIZE * 4]) >> 16; |
| tmp3 = (inptr[DCTSIZE * 6] * quantptr[DCTSIZE * 6]) >> 16; |
| |
| tmp10 = tmp0 + tmp2; /* phase 3 */ |
| tmp11 = tmp0 - tmp2; |
| |
| tmp13 = tmp1 + tmp3; /* phases 5-3 */ |
| tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */ |
| |
| tmp0 = tmp10 + tmp13; /* phase 2 */ |
| tmp3 = tmp10 - tmp13; |
| tmp1 = tmp11 + tmp12; |
| tmp2 = tmp11 - tmp12; |
| |
| /* Odd part */ |
| |
| tmp4 = (inptr[DCTSIZE * 1] * quantptr[DCTSIZE * 1]) >> 16; |
| tmp5 = (inptr[DCTSIZE * 3] * quantptr[DCTSIZE * 3]) >> 16; |
| tmp6 = (inptr[DCTSIZE * 5] * quantptr[DCTSIZE * 5]) >> 16; |
| tmp7 = (inptr[DCTSIZE * 7] * quantptr[DCTSIZE * 7]) >> 16; |
| |
| z13 = tmp6 + tmp5; /* phase 6 */ |
| z10 = tmp6 - tmp5; |
| z11 = tmp4 + tmp7; |
| z12 = tmp4 - tmp7; |
| |
| tmp7 = z11 + z13; /* phase 5 */ |
| tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ |
| |
| z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ |
| tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ |
| tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5; /* -2*(c2+c6) */ |
| |
| tmp6 = tmp12 - tmp7; /* phase 2 */ |
| tmp5 = tmp11 - tmp6; |
| tmp4 = tmp10 + tmp5; |
| |
| wsptr[DCTSIZE * 0] = (int)(tmp0 + tmp7); |
| wsptr[DCTSIZE * 7] = (int)(tmp0 - tmp7); |
| wsptr[DCTSIZE * 1] = (int)(tmp1 + tmp6); |
| wsptr[DCTSIZE * 6] = (int)(tmp1 - tmp6); |
| wsptr[DCTSIZE * 2] = (int)(tmp2 + tmp5); |
| wsptr[DCTSIZE * 5] = (int)(tmp2 - tmp5); |
| wsptr[DCTSIZE * 4] = (int)(tmp3 + tmp4); |
| wsptr[DCTSIZE * 3] = (int)(tmp3 - tmp4); |
| |
| inptr++; /* advance pointers to next column */ |
| quantptr++; |
| wsptr++; |
| } |
| |
| /* Pass 2: process rows from work array, store into output array. */ |
| /* Note that we must descale the results by a factor of 8 == 2**3, */ |
| /* and also undo the PASS1_BITS scaling. */ |
| |
| //#define RANGE_MASK 1023; //2 bits wider than legal samples |
| #define PASS1_BITS 0 |
| #define IDESCALE(x, n) ((int)((x) >> n)) |
| |
| wsptr = workspace; |
| for (ctr = 0; ctr < DCTSIZE; ctr++) { |
| outptr = data + ctr * 8; |
| |
| /* Rows of zeroes can be exploited in the same way as we did with columns. |
| * However, the column calculation has created many nonzero AC terms, so |
| * the simplification applies less often (typically 5% to 10% of the time). |
| * On machines with very fast multiplication, it's possible that the |
| * test takes more time than it's worth. In that case this section |
| * may be commented out. |
| */ |
| /* Even part */ |
| |
| tmp10 = ((int)wsptr[0] + (int)wsptr[4]); |
| tmp11 = ((int)wsptr[0] - (int)wsptr[4]); |
| |
| tmp13 = ((int)wsptr[2] + (int)wsptr[6]); |
| tmp12 = MULTIPLY((int)wsptr[2] - (int)wsptr[6], FIX_1_414213562) - tmp13; |
| |
| tmp0 = tmp10 + tmp13; |
| tmp3 = tmp10 - tmp13; |
| tmp1 = tmp11 + tmp12; |
| tmp2 = tmp11 - tmp12; |
| |
| /* Odd part */ |
| |
| z13 = (int)wsptr[5] + (int)wsptr[3]; |
| z10 = (int)wsptr[5] - (int)wsptr[3]; |
| z11 = (int)wsptr[1] + (int)wsptr[7]; |
| z12 = (int)wsptr[1] - (int)wsptr[7]; |
| |
| tmp7 = z11 + z13; /* phase 5 */ |
| tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ |
| |
| z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ |
| tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ |
| tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5; /* -2*(c2+c6) */ |
| |
| tmp6 = tmp12 - tmp7; /* phase 2 */ |
| tmp5 = tmp11 - tmp6; |
| tmp4 = tmp10 + tmp5; |
| |
| /* Final output stage: scale down by a factor of 8 and range-limit */ |
| |
| outptr[0] = r_limit[IDESCALE((tmp0 + tmp7), (PASS1_BITS + 3)) & 1023L]; |
| outptr[7] = r_limit[IDESCALE((tmp0 - tmp7), (PASS1_BITS + 3)) & 1023L]; |
| outptr[1] = r_limit[IDESCALE((tmp1 + tmp6), (PASS1_BITS + 3)) & 1023L]; |
| outptr[6] = r_limit[IDESCALE((tmp1 - tmp6), (PASS1_BITS + 3)) & 1023L]; |
| outptr[2] = r_limit[IDESCALE((tmp2 + tmp5), (PASS1_BITS + 3)) & 1023L]; |
| outptr[5] = r_limit[IDESCALE((tmp2 - tmp5), (PASS1_BITS + 3)) & 1023L]; |
| outptr[4] = r_limit[IDESCALE((tmp3 + tmp4), (PASS1_BITS + 3)) & 1023L]; |
| outptr[3] = r_limit[IDESCALE((tmp3 - tmp4), (PASS1_BITS + 3)) & 1023L]; |
| |
| wsptr += DCTSIZE; /* advance pointer to next row */ |
| } |
| } |
| void YUVToRGB( |
| int txb, int tyb, |
| unsigned char |
| *pYCbCr, // in, Y: 256 or 64 bytes; Cb: 64 bytes; Cr: 64 bytes |
| struct RGB *pYUV, // in, Y: 256 or 64 bytes; Cb: 64 bytes; Cr: 64 bytes |
| unsigned char |
| *pBgr // out, BGR format, 16*16*3 = 768 bytes; or 8*8*3=192 bytes |
| ) { |
| int i, j, pos, m, n; |
| unsigned char cb, cr, *py, *pcb, *pcr, *py420[4]; |
| int y; |
| struct RGB *pByte; |
| int nBlocksInMcu = 6; |
| unsigned int pixel_x, pixel_y; |
| |
| pByte = (struct RGB *)pBgr; |
| if (yuvmode == YuvMode::YUV444) { |
| py = pYCbCr; |
| pcb = pYCbCr + 64; |
| pcr = pcb + 64; |
| |
| pixel_x = txb * 8; |
| pixel_y = tyb * 8; |
| pos = (pixel_y * WIDTH) + pixel_x; |
| |
| for (j = 0; j < 8; j++) { |
| for (i = 0; i < 8; i++) { |
| m = ((j << 3) + i); |
| y = py[m]; |
| cb = pcb[m]; |
| cr = pcr[m]; |
| n = pos + i; |
| // For 2Pass. Save the YUV value |
| pYUV[n].B = cb; |
| pYUV[n].G = y; |
| pYUV[n].R = cr; |
| pByte[n].B = rlimit_table[m_Y[y] + m_CbToB[cb]]; |
| pByte[n].G = rlimit_table[m_Y[y] + m_CbToG[cb] + m_CrToG[cr]]; |
| pByte[n].R = rlimit_table[m_Y[y] + m_CrToR[cr]]; |
| /* |
| std::cout << "set y:" << n / 800 << " x:" << n % 800 << " to " |
| << " B:" << static_cast<uint32_t>(pByte[n].B) |
| << " G:" << static_cast<uint32_t>(pByte[n].G) |
| << " R:" << static_cast<uint32_t>(pByte[n].R) << "\n"; |
| */ |
| } |
| pos += WIDTH; |
| } |
| } else { |
| for (i = 0; i < nBlocksInMcu - 2; i++) py420[i] = pYCbCr + i * 64; |
| pcb = pYCbCr + (nBlocksInMcu - 2) * 64; |
| pcr = pcb + 64; |
| |
| pixel_x = txb * 16; |
| pixel_y = tyb * 16; |
| pos = (pixel_y * WIDTH) + pixel_x; |
| |
| for (j = 0; j < 16; j++) { |
| for (i = 0; i < 16; i++) { |
| // block number is ((j/8) * 2 + i/8)={0, 1, 2, 3} |
| y = *(py420[(j >> 3) * 2 + (i >> 3)]++); |
| m = ((j >> 1) << 3) + (i >> 1); |
| cb = pcb[m]; |
| cr = pcr[m]; |
| n = pos + i; |
| pByte[n].B = rlimit_table[m_Y[y] + m_CbToB[cb]]; |
| pByte[n].G = rlimit_table[m_Y[y] + m_CbToG[cb] + m_CrToG[cr]]; |
| pByte[n].R = rlimit_table[m_Y[y] + m_CrToR[cr]]; |
| } |
| pos += WIDTH; |
| } |
| } |
| } |
| void YUVToBuffer( |
| int txb, int tyb, |
| unsigned char |
| *pYCbCr, // in, Y: 256 or 64 bytes; Cb: 64 bytes; Cr: 64 bytes |
| struct RGB |
| *pYUV, // out, BGR format, 16*16*3 = 768 bytes; or 8*8*3=192 bytes |
| unsigned char |
| *pBgr // out, BGR format, 16*16*3 = 768 bytes; or 8*8*3=192 bytes |
| ) { |
| int i, j, pos, m, n; |
| unsigned char cb, cr, *py, *pcb, *pcr, *py420[4]; |
| int y; |
| struct RGB *pByte; |
| int nBlocksInMcu = 6; |
| unsigned int pixel_x, pixel_y; |
| |
| pByte = (struct RGB *)pBgr; |
| if (yuvmode == YuvMode::YUV444) { |
| py = pYCbCr; |
| pcb = pYCbCr + 64; |
| pcr = pcb + 64; |
| |
| pixel_x = txb * 8; |
| pixel_y = tyb * 8; |
| pos = (pixel_y * WIDTH) + pixel_x; |
| |
| for (j = 0; j < 8; j++) { |
| for (i = 0; i < 8; i++) { |
| m = ((j << 3) + i); |
| n = pos + i; |
| y = pYUV[n].G + (py[m] - 128); |
| cb = pYUV[n].B + (pcb[m] - 128); |
| cr = pYUV[n].R + (pcr[m] - 128); |
| pYUV[n].B = cb; |
| pYUV[n].G = y; |
| pYUV[n].R = cr; |
| pByte[n].B = rlimit_table[m_Y[y] + m_CbToB[cb]]; |
| pByte[n].G = rlimit_table[m_Y[y] + m_CbToG[cb] + m_CrToG[cr]]; |
| pByte[n].R = rlimit_table[m_Y[y] + m_CrToR[cr]]; |
| } |
| pos += WIDTH; |
| } |
| } else { |
| for (i = 0; i < nBlocksInMcu - 2; i++) py420[i] = pYCbCr + i * 64; |
| pcb = pYCbCr + (nBlocksInMcu - 2) * 64; |
| pcr = pcb + 64; |
| |
| pixel_x = txb * 16; |
| pixel_y = tyb * 16; |
| pos = (pixel_y * WIDTH) + pixel_x; |
| |
| for (j = 0; j < 16; j++) { |
| for (i = 0; i < 16; i++) { |
| // block number is ((j/8) * 2 + i/8)={0, 1, 2, 3} |
| y = *(py420[(j >> 3) * 2 + (i >> 3)]++); |
| m = ((j >> 1) << 3) + (i >> 1); |
| cb = pcb[m]; |
| cr = pcr[m]; |
| n = pos + i; |
| pByte[n].B = rlimit_table[m_Y[y] + m_CbToB[cb]]; |
| pByte[n].G = rlimit_table[m_Y[y] + m_CbToG[cb] + m_CrToG[cr]]; |
| pByte[n].R = rlimit_table[m_Y[y] + m_CrToR[cr]]; |
| } |
| pos += WIDTH; |
| } |
| } |
| } |
| int Decompress(int txb, int tyb, char *outBuf, uint8_t QT_TableSelection) { |
| unsigned char *ptr; |
| unsigned char byTileYuv[768] = {}; |
| |
| memset(DCT_coeff, 0, 384 * 2); |
| ptr = byTileYuv; |
| process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 0); |
| IDCT_transform(DCT_coeff, ptr, QT_TableSelection); |
| ptr += 64; |
| |
| if (yuvmode == YuvMode::YUV420) { |
| process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 64); |
| IDCT_transform(DCT_coeff + 64, ptr, QT_TableSelection); |
| ptr += 64; |
| |
| process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 128); |
| IDCT_transform(DCT_coeff + 128, ptr, QT_TableSelection); |
| ptr += 64; |
| |
| process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 192); |
| IDCT_transform(DCT_coeff + 192, ptr, QT_TableSelection); |
| ptr += 64; |
| |
| process_Huffman_data_unit(CbDC_nr, CbAC_nr, &DCCb, 256); |
| IDCT_transform(DCT_coeff + 256, ptr, QT_TableSelection + 1); |
| ptr += 64; |
| |
| process_Huffman_data_unit(CrDC_nr, CrAC_nr, &DCCr, 320); |
| IDCT_transform(DCT_coeff + 320, ptr, QT_TableSelection + 1); |
| } else { |
| process_Huffman_data_unit(CbDC_nr, CbAC_nr, &DCCb, 64); |
| IDCT_transform(DCT_coeff + 64, ptr, QT_TableSelection + 1); |
| ptr += 64; |
| |
| process_Huffman_data_unit(CrDC_nr, CrAC_nr, &DCCr, 128); |
| IDCT_transform(DCT_coeff + 128, ptr, QT_TableSelection + 1); |
| } |
| |
| // YUVToRGB (txb, tyb, byTileYuv, (unsigned char *)outBuf); |
| // YUVBuffer for YUV record |
| YUVToRGB(txb, tyb, byTileYuv, YUVBuffer.data(), (unsigned char *)outBuf); |
| if (txb == 0 && tyb == 0) { |
| for (int i=0; i < 10; i++) { |
| auto pixel = YUVBuffer[i]; |
| std::cout << "YUBuffer " << static_cast<int>(pixel.R) << " " |
| << static_cast<int>(pixel.G) << static_cast<int>(pixel.B) |
| << "\n"; |
| } |
| } |
| return 1; |
| } |
| |
| int Decompress_2PASS(int txb, int tyb, char *outBuf, |
| uint8_t QT_TableSelection) { |
| unsigned char *ptr; |
| unsigned char byTileYuv[768]; |
| memset(DCT_coeff, 0, 384 * 2); |
| |
| ptr = byTileYuv; |
| process_Huffman_data_unit(YDC_nr, YAC_nr, &DCY, 0); |
| IDCT_transform(DCT_coeff, ptr, QT_TableSelection); |
| ptr += 64; |
| |
| process_Huffman_data_unit(CbDC_nr, CbAC_nr, &DCCb, 64); |
| IDCT_transform(DCT_coeff + 64, ptr, QT_TableSelection + 1); |
| ptr += 64; |
| |
| process_Huffman_data_unit(CrDC_nr, CrAC_nr, &DCCr, 128); |
| IDCT_transform(DCT_coeff + 128, ptr, QT_TableSelection + 1); |
| |
| YUVToBuffer(txb, tyb, byTileYuv, YUVBuffer.data(), (unsigned char *)outBuf); |
| // YUVToRGB (txb, tyb, byTileYuv, (unsigned char *)outBuf); |
| |
| return 1; |
| } |
| |
| int VQ_Decompress(int txb, int tyb, char *outBuf, uint8_t QT_TableSelection, |
| struct COLOR_CACHE *VQ) { |
| unsigned char *ptr, i; |
| unsigned char byTileYuv[192]; |
| int Data; |
| |
| ptr = byTileYuv; |
| if (VQ->BitMapBits == 0) { |
| for (i = 0; i < 64; i++) { |
| ptr[0] = (VQ->Color[VQ->Index[0]] & 0xFF0000) >> 16; |
| ptr[64] = (VQ->Color[VQ->Index[0]] & 0x00FF00) >> 8; |
| ptr[128] = VQ->Color[VQ->Index[0]] & 0x0000FF; |
| ptr += 1; |
| } |
| } else { |
| for (i = 0; i < 64; i++) { |
| Data = (int)lookKbits(VQ->BitMapBits); |
| ptr[0] = (VQ->Color[VQ->Index[Data]] & 0xFF0000) >> 16; |
| ptr[64] = (VQ->Color[VQ->Index[Data]] & 0x00FF00) >> 8; |
| ptr[128] = VQ->Color[VQ->Index[Data]] & 0x0000FF; |
| ptr += 1; |
| skipKbits(VQ->BitMapBits); |
| } |
| } |
| // YUVToRGB (txb, tyb, byTileYuv, (unsigned char *)outBuf); |
| YUVToRGB(txb, tyb, byTileYuv, YUVBuffer.data(), (unsigned char *)outBuf); |
| |
| return 1; |
| } |
| |
| void MoveBlockIndex(void) { |
| if (yuvmode == YuvMode::YUV444) { |
| txb++; |
| if (txb >= (int)(tmp_WIDTH / 8)) { |
| tyb++; |
| if (tyb >= (int)(tmp_HEIGHT / 8)) tyb = 0; |
| txb = 0; |
| } |
| } else { |
| txb++; |
| if (txb >= (int)(tmp_WIDTH / 16)) { |
| tyb++; |
| if (tyb >= (int)(tmp_HEIGHT / 16)) tyb = 0; |
| txb = 0; |
| } |
| } |
| } |
| |
| void VQ_Initialize(struct COLOR_CACHE *VQ) { |
| int i; |
| |
| for (i = 0; i < 4; i++) { |
| VQ->Index[i] = i; |
| } |
| VQ->Color[0] = 0x008080; |
| VQ->Color[1] = 0xFF8080; |
| VQ->Color[2] = 0x808080; |
| VQ->Color[3] = 0xC08080; |
| } |
| void init_QT() {} |
| |
| void Init_Color_Table() { |
| int i, x; |
| int nScale = 1L << 16; // equal to power(2,16) |
| int nHalf = nScale >> 1; |
| |
| #define FIX(x) ((int)((x)*nScale + 0.5)) |
| |
| /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ |
| /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ |
| /* Cr=>R value is nearest int to 1.597656 * x */ |
| /* Cb=>B value is nearest int to 2.015625 * x */ |
| /* Cr=>G value is scaled-up -0.8125 * x */ |
| /* Cb=>G value is scaled-up -0.390625 * x */ |
| for (i = 0, x = -128; i < 256; i++, x++) { |
| m_CrToR[i] = (int)(FIX(1.597656) * x + nHalf) >> 16; |
| m_CbToB[i] = (int)(FIX(2.015625) * x + nHalf) >> 16; |
| m_CrToG[i] = (int)(-FIX(0.8125) * x + nHalf) >> 16; |
| m_CbToG[i] = (int)(-FIX(0.390625) * x + nHalf) >> 16; |
| } |
| for (i = 0, x = -16; i < 256; i++, x++) { |
| m_Y[i] = (int)(FIX(1.164) * x + nHalf) >> 16; |
| } |
| // For Color Text Enchance Y Re-map. Recommend to disable in default |
| /* |
| for (i = 0; i < (VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate); |
| i++) { |
| temp = (double)i / |
| VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate; |
| temp1 = 1.0 / VideoEngineInfo->INFData.Gamma1Parameter; |
| m_Y[i] = |
| (BYTE)(VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate * pow (temp, |
| temp1)); |
| if (m_Y[i] > 255) m_Y[i] = 255; |
| } |
| for (i = (VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate); i < 256; |
| i++) { |
| m_Y[i] = |
| (BYTE)((VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate) + (256 - |
| VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate) * ( pow((double)((i - |
| VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate) / (256 - |
| (VideoEngineInfo->INFData.Gamma1_Gamma2_Seperate))), (1.0 / |
| VideoEngineInfo->INFData.Gamma2Parameter)) )); |
| if (m_Y[i] > 255) m_Y[i] = 255; |
| } |
| */ |
| } |
| void load_Huffman_table(Huffman_table *HT, unsigned char *nrcode, |
| unsigned char *value, unsigned short int *Huff_code) { |
| unsigned char k, j, i; |
| unsigned int code, code_index; |
| |
| for (j = 1; j <= 16; j++) { |
| HT->Length[j] = nrcode[j]; |
| } |
| for (i = 0, k = 1; k <= 16; k++) |
| for (j = 0; j < HT->Length[k]; j++) { |
| HT->V[WORD_hi_lo(k, j)] = value[i]; |
| i++; |
| } |
| |
| code = 0; |
| for (k = 1; k <= 16; k++) { |
| HT->minor_code[k] = (unsigned short int)code; |
| for (j = 1; j <= HT->Length[k]; j++) code++; |
| HT->major_code[k] = (unsigned short int)(code - 1); |
| code *= 2; |
| if (HT->Length[k] == 0) { |
| HT->minor_code[k] = 0xFFFF; |
| HT->major_code[k] = 0; |
| } |
| } |
| |
| HT->Len[0] = 2; |
| i = 2; |
| |
| for (code_index = 1; code_index < 65535; code_index++) { |
| if (code_index < Huff_code[i]) { |
| HT->Len[code_index] = (unsigned char)Huff_code[i + 1]; |
| } else { |
| i = i + 2; |
| HT->Len[code_index] = (unsigned char)Huff_code[i + 1]; |
| } |
| } |
| } |
| void init_jpg_table() { |
| init_QT(); |
| Init_Color_Table(); |
| prepare_range_limit_table(); |
| load_Huffman_table(&HTDC[0], std_dc_luminance_nrcodes, |
| std_dc_luminance_values, DC_LUMINANCE_HUFFMANCODE); |
| load_Huffman_table(&HTAC[0], std_ac_luminance_nrcodes, |
| std_ac_luminance_values, AC_LUMINANCE_HUFFMANCODE); |
| load_Huffman_table(&HTDC[1], std_dc_chrominance_nrcodes, |
| std_dc_chrominance_values, DC_CHROMINANCE_HUFFMANCODE); |
| load_Huffman_table(&HTAC[1], std_ac_chrominance_nrcodes, |
| std_ac_chrominance_values, AC_CHROMINANCE_HUFFMANCODE); |
| } |
| |
| void prepare_range_limit_table() |
| /* Allocate and fill in the sample_range_limit table */ |
| { |
| int j; |
| rlimit_table = (unsigned char *)malloc(5 * 256L + 128); |
| /* First segment of "simple" table: limit[x] = 0 for x < 0 */ |
| memset((void *)rlimit_table, 0, 256); |
| rlimit_table += 256; /* allow negative subscripts of simple table */ |
| /* Main part of "simple" table: limit[x] = x */ |
| for (j = 0; j < 256; j++) rlimit_table[j] = j; |
| /* End of simple table, rest of first half of post-IDCT table */ |
| for (j = 256; j < 640; j++) rlimit_table[j] = 255; |
| |
| /* Second half of post-IDCT table */ |
| memset((void *)(rlimit_table + 640), 0, 384); |
| for (j = 0; j < 128; j++) rlimit_table[j + 1024] = j; |
| } |
| |
| inline unsigned short int WORD_hi_lo(uint8_t byte_high, uint8_t byte_low) { |
| return (byte_high << 8) + byte_low; |
| } |
| |
| // river |
| void process_Huffman_data_unit(uint8_t DC_nr, uint8_t AC_nr, |
| signed short int *previous_DC, |
| unsigned short int position) { |
| uint8_t nr = 0; |
| uint8_t k; |
| unsigned short int tmp_Hcode; |
| uint8_t size_val, count_0; |
| unsigned short int *min_code; |
| uint8_t *huff_values; |
| uint8_t byte_temp; |
| |
| min_code = HTDC[DC_nr].minor_code; |
| // maj_code=HTDC[DC_nr].major_code; |
| huff_values = HTDC[DC_nr].V; |
| |
| // DC |
| k = HTDC[DC_nr].Len[(unsigned short int)(codebuf >> 16)]; |
| // river |
| // tmp_Hcode=lookKbits(k); |
| tmp_Hcode = (unsigned short int)(codebuf >> (32 - k)); |
| skipKbits(k); |
| size_val = huff_values[WORD_hi_lo(k, (uint8_t)(tmp_Hcode - min_code[k]))]; |
| if (size_val == 0) |
| DCT_coeff[position + 0] = *previous_DC; |
| else { |
| DCT_coeff[position + 0] = *previous_DC + getKbits(size_val); |
| *previous_DC = DCT_coeff[position + 0]; |
| } |
| |
| // Second, AC coefficient decoding |
| min_code = HTAC[AC_nr].minor_code; |
| // maj_code=HTAC[AC_nr].major_code; |
| huff_values = HTAC[AC_nr].V; |
| |
| nr = 1; // AC coefficient |
| do { |
| k = HTAC[AC_nr].Len[(unsigned short int)(codebuf >> 16)]; |
| tmp_Hcode = (unsigned short int)(codebuf >> (32 - k)); |
| skipKbits(k); |
| |
| byte_temp = |
| huff_values[WORD_hi_lo(k, (uint8_t)(tmp_Hcode - min_code[k]))]; |
| size_val = byte_temp & 0xF; |
| count_0 = byte_temp >> 4; |
| if (size_val == 0) { |
| if (count_0 != 0xF) { |
| break; |
| } |
| nr += 16; |
| } else { |
| nr += count_0; // skip count_0 zeroes |
| DCT_coeff[position + dezigzag[nr++]] = getKbits(size_val); |
| } |
| } while (nr < 64); |
| } |
| |
| unsigned short int lookKbits(uint8_t k) { |
| unsigned short int revcode; |
| |
| revcode = (unsigned short int)(codebuf >> (32 - k)); |
| |
| return (revcode); |
| } |
| |
| void skipKbits(uint8_t k) { |
| unsigned long readbuf; |
| |
| if ((newbits - k) <= 0) { |
| readbuf = Buffer[buffer_index]; |
| buffer_index++; |
| codebuf = |
| (codebuf << k) | ((newbuf | (readbuf >> (newbits))) >> (32 - k)); |
| newbuf = readbuf << (k - newbits); |
| newbits = 32 + newbits - k; |
| } else { |
| codebuf = (codebuf << k) | (newbuf >> (32 - k)); |
| newbuf = newbuf << k; |
| newbits -= k; |
| } |
| } |
| |
| signed short int getKbits(uint8_t k) { |
| signed short int signed_wordvalue; |
| |
| // river |
| // signed_wordvalue=lookKbits(k); |
| signed_wordvalue = (unsigned short int)(codebuf >> (32 - k)); |
| if (((1L << (k - 1)) & signed_wordvalue) == 0) { |
| // neg_pow2 was previously defined as the below. It seemed silly to keep |
| // a table of values around for something |
| // THat's relatively easy to compute, so it was replaced with the |
| // appropriate math |
| // signed_wordvalue = signed_wordvalue - (0xFFFF >> (16 - k)); |
| std::array<signed short int, 17> neg_pow2 = { |
| 0, -1, -3, -7, -15, -31, -63, -127, |
| -255, -511, -1023, -2047, -4095, -8191, -16383, -32767}; |
| |
| signed_wordvalue = signed_wordvalue + neg_pow2[k]; |
| } |
| skipKbits(k); |
| return signed_wordvalue; |
| } |
| int init_JPG_decoding() { |
| byte_pos = 0; |
| load_quant_table(QT[0]); |
| load_quant_tableCb(QT[1]); |
| // Note: Added for Dual-JPEG |
| load_advance_quant_table(QT[2]); |
| load_advance_quant_tableCb(QT[3]); |
| return 1; |
| } |
| |
| void set_quant_table(uint8_t *basic_table, uint8_t scale_factor, |
| uint8_t *newtable) |
| // Set quantization table and zigzag reorder it |
| { |
| uint8_t i; |
| long temp; |
| for (i = 0; i < 64; i++) { |
| temp = ((long)(basic_table[i] * 16) / scale_factor); |
| /* limit the values to the valid range */ |
| if (temp <= 0L) temp = 1L; |
| if (temp > 255L) temp = 255L; /* limit to baseline range if requested */ |
| newtable[zigzag[i]] = (uint8_t)temp; |
| } |
| } |
| |
| void updatereadbuf(uint32_t *codebuf, uint32_t *newbuf, int walks, |
| int *newbits, std::vector<uint32_t> &Buffer) { |
| unsigned long readbuf; |
| |
| if ((*newbits - walks) <= 0) { |
| readbuf = Buffer[buffer_index]; |
| buffer_index++; |
| *codebuf = (*codebuf << walks) | |
| ((*newbuf | (readbuf >> (*newbits))) >> (32 - walks)); |
| *newbuf = readbuf << (walks - *newbits); |
| *newbits = 32 + *newbits - walks; |
| } else { |
| *codebuf = (*codebuf << walks) | (*newbuf >> (32 - walks)); |
| *newbuf = *newbuf << walks; |
| *newbits -= walks; |
| } |
| } |
| |
| uint32_t decode(std::vector<uint32_t> &buffer, unsigned long width, |
| unsigned long height, YuvMode yuvmode_in, int y_selector, |
| int uv_selector) { |
| uint32_t i; |
| COLOR_CACHE Decode_Color; |
| |
| // TODO(ed) use the enum everywhere, not just externally |
| yuvmode = yuvmode_in; // 0 = YUV444, 1 = YUV420 |
| Y_selector = y_selector; // 0-7 |
| UV_selector = uv_selector; // 0-7 |
| |
| // TODO(ed) Magic number section. Document appropriately |
| advance_selector = 0; // 0-7 |
| First_Frame = 1; // 0 or 1 |
| Mapping = 0; // 0 or 1 |
| /* |
| if (yuvmode == YuvMode::YUV420) { |
| Y_selector = 4; |
| UV_selector = 7; |
| Mapping = 0; |
| } else { // YUV444 |
| Y_selector = 7; |
| UV_selector = 7; |
| Mapping = 0; |
| } |
| */ |
| auto test = static_cast<int>(yuvmode); |
| std::cout << "YUVmode " << test << " " << static_cast<int>(Y_selector) << static_cast<int>(UV_selector) << "\n"; |
| |
| tmp_WIDTH = width; |
| tmp_HEIGHT = height; |
| WIDTH = width; |
| HEIGHT = height; |
| |
| VQ_Initialize(&Decode_Color); |
| // OutputDebugString ("In decode\n"); |
| // GetINFData (VideoEngineInfo); |
| // WIDTH = VideoEngineInfo->SourceModeInfo.X = 640; |
| // HEIGHT = VideoEngineInfo->SourceModeInfo.Y = 480; |
| // AST2000 JPEG block is 16x16(pixels) base |
| if (yuvmode == YuvMode::YUV420) { |
| if (WIDTH % 16) { |
| WIDTH = WIDTH + 16 - (WIDTH % 16); |
| } |
| if (HEIGHT % 16) { |
| HEIGHT = HEIGHT + 16 - (HEIGHT % 16); |
| } |
| } else { |
| if (WIDTH % 8) { |
| WIDTH = WIDTH + 8 - (WIDTH % 8); |
| } |
| if (HEIGHT % 8) { |
| HEIGHT = HEIGHT + 8 - (HEIGHT % 8); |
| } |
| } |
| |
| // tmp_WDITH, tmp_HEIGHT are for block position |
| // tmp_WIDTH = VideoEngineInfo->DestinationModeInfo.X; |
| // tmp_HEIGHT = VideoEngineInfo->DestinationModeInfo.Y; |
| if (yuvmode == YuvMode::YUV420) { |
| if (tmp_WIDTH % 16) { |
| tmp_WIDTH = tmp_WIDTH + 16 - (tmp_WIDTH % 16); |
| } |
| if (tmp_HEIGHT % 16) { |
| tmp_HEIGHT = tmp_HEIGHT + 16 - (tmp_HEIGHT % 16); |
| } |
| } else { |
| if (tmp_WIDTH % 8) { |
| tmp_WIDTH = tmp_WIDTH + 8 - (tmp_WIDTH % 8); |
| } |
| if (tmp_HEIGHT % 8) { |
| tmp_HEIGHT = tmp_HEIGHT + 8 - (tmp_HEIGHT % 8); |
| } |
| } |
| |
| int qfactor = 16; |
| |
| SCALEFACTOR = qfactor; |
| SCALEFACTORUV = qfactor; |
| ADVANCESCALEFACTOR = 16; |
| ADVANCESCALEFACTORUV = 16; |
| |
| if (First_Frame == 1) { |
| init_jpg_table(); |
| init_JPG_decoding(); |
| } |
| // TODO(ed) cleanup cruft |
| Buffer = buffer.data(); |
| |
| codebuf = buffer[0]; |
| newbuf = buffer[1]; |
| buffer_index = 2; |
| |
| txb = tyb = 0; |
| newbits = 32; |
| DCY = DCCb = DCCr = 0; |
| |
| do { |
| auto block_header = static_cast<JpgBlock>((codebuf >> 28) & 0xFF); |
| switch (block_header) { |
| case JpgBlock::JPEG_NO_SKIP_CODE: |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits, |
| buffer); |
| Decompress(txb, tyb, (char *)OutBuffer.data(), 0); |
| break; |
| case JpgBlock::FRAME_END_CODE: |
| return 0; |
| break; |
| case JpgBlock::JPEG_SKIP_CODE: |
| |
| txb = (codebuf & 0x0FF00000) >> 20; |
| tyb = (codebuf & 0x0FF000) >> 12; |
| |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits, |
| buffer); |
| Decompress(txb, tyb, (char *)OutBuffer.data(), 0); |
| break; |
| case JpgBlock::VQ_NO_SKIP_1_COLOR_CODE: |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits, |
| buffer); |
| Decode_Color.BitMapBits = 0; |
| |
| for (i = 0; i < 1; i++) { |
| Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK); |
| if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) { |
| updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits, |
| buffer); |
| } else { |
| Decode_Color.Color[Decode_Color.Index[i]] = |
| ((codebuf >> 5) & VQ_COLOR_MASK); |
| updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits, |
| buffer); |
| } |
| } |
| VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color); |
| break; |
| case JpgBlock::VQ_SKIP_1_COLOR_CODE: |
| txb = (codebuf & 0x0FF00000) >> 20; |
| tyb = (codebuf & 0x0FF000) >> 12; |
| |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits, |
| buffer); |
| Decode_Color.BitMapBits = 0; |
| |
| for (i = 0; i < 1; i++) { |
| Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK); |
| if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) { |
| updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits, |
| buffer); |
| } else { |
| Decode_Color.Color[Decode_Color.Index[i]] = |
| ((codebuf >> 5) & VQ_COLOR_MASK); |
| updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits, |
| buffer); |
| } |
| } |
| VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color); |
| break; |
| |
| case JpgBlock::VQ_NO_SKIP_2_COLOR_CODE: |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits, |
| buffer); |
| Decode_Color.BitMapBits = 1; |
| |
| for (i = 0; i < 2; i++) { |
| Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK); |
| if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) { |
| updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits, |
| buffer); |
| } else { |
| Decode_Color.Color[Decode_Color.Index[i]] = |
| ((codebuf >> 5) & VQ_COLOR_MASK); |
| updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits, |
| buffer); |
| } |
| } |
| VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color); |
| break; |
| case JpgBlock::VQ_SKIP_2_COLOR_CODE: |
| txb = (codebuf & 0x0FF00000) >> 20; |
| tyb = (codebuf & 0x0FF000) >> 12; |
| |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits, |
| buffer); |
| Decode_Color.BitMapBits = 1; |
| |
| for (i = 0; i < 2; i++) { |
| Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK); |
| if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) { |
| updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits, |
| buffer); |
| } else { |
| Decode_Color.Color[Decode_Color.Index[i]] = |
| ((codebuf >> 5) & VQ_COLOR_MASK); |
| updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits, |
| buffer); |
| } |
| } |
| VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color); |
| |
| break; |
| case JpgBlock::VQ_NO_SKIP_4_COLOR_CODE: |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_START_LENGTH, &newbits, |
| buffer); |
| Decode_Color.BitMapBits = 2; |
| |
| for (i = 0; i < 4; i++) { |
| Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK); |
| if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) { |
| updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits, |
| buffer); |
| } else { |
| Decode_Color.Color[Decode_Color.Index[i]] = |
| ((codebuf >> 5) & VQ_COLOR_MASK); |
| updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits, |
| buffer); |
| } |
| } |
| VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color); |
| |
| break; |
| |
| case JpgBlock::VQ_SKIP_4_COLOR_CODE: |
| txb = (codebuf & 0x0FF00000) >> 20; |
| tyb = (codebuf & 0x0FF000) >> 12; |
| |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits, |
| buffer); |
| Decode_Color.BitMapBits = 2; |
| |
| for (i = 0; i < 4; i++) { |
| Decode_Color.Index[i] = ((codebuf >> 29) & VQ_INDEX_MASK); |
| if (((codebuf >> 31) & VQ_HEADER_MASK) == VQ_NO_UPDATE_HEADER) { |
| updatereadbuf(&codebuf, &newbuf, VQ_NO_UPDATE_LENGTH, &newbits, |
| buffer); |
| } else { |
| Decode_Color.Color[Decode_Color.Index[i]] = |
| ((codebuf >> 5) & VQ_COLOR_MASK); |
| updatereadbuf(&codebuf, &newbuf, VQ_UPDATE_LENGTH, &newbits, |
| buffer); |
| } |
| } |
| VQ_Decompress(txb, tyb, (char *)OutBuffer.data(), 0, &Decode_Color); |
| |
| break; |
| case JpgBlock::JPEG_SKIP_PASS2_CODE: |
| txb = (codebuf & 0x0FF00000) >> 20; |
| tyb = (codebuf & 0x0FF000) >> 12; |
| |
| updatereadbuf(&codebuf, &newbuf, BLOCK_AST2100_SKIP_LENGTH, &newbits, |
| buffer); |
| Decompress_2PASS(txb, tyb, (char *)OutBuffer.data(), 2); |
| |
| break; |
| default: |
| // TODO(ed) propogate errors upstream |
| return -1; |
| break; |
| } |
| MoveBlockIndex(); |
| |
| } while (buffer_index <= buffer.size()); |
| |
| return -1; |
| } |
| |
| #ifdef cimg_version |
| void dump_to_bitmap_file() { |
| cimg_library::CImg<unsigned char> image(WIDTH, HEIGHT, 1, 3); |
| for (int y = 0; y < WIDTH; y++) { |
| for (int x = 0; x < HEIGHT; x++) { |
| auto pixel = OutBuffer[x + (y * WIDTH)]; |
| image(x, y, 0) = pixel.R; |
| image(x, y, 1) = pixel.G; |
| image(x, y, 2) = pixel.B; |
| } |
| } |
| image.save("/tmp/file2.bmp"); |
| } |
| #endif |
| |
| private: |
| YuvMode yuvmode; |
| // WIDTH and HEIGHT are the modes your display used |
| unsigned long WIDTH; |
| unsigned long HEIGHT; |
| unsigned long tmp_HEIGHT; |
| unsigned long tmp_WIDTH; |
| unsigned char Y_selector; |
| int SCALEFACTOR; |
| int SCALEFACTORUV; |
| int ADVANCESCALEFACTOR; |
| int ADVANCESCALEFACTORUV; |
| int Mapping; |
| unsigned char UV_selector; |
| unsigned char advance_selector; |
| unsigned char First_Frame; |
| int byte_pos; // current byte position |
| |
| // quantization tables, no more than 4 quantization tables |
| std::array<std::array<long, 64>, 4> QT; |
| |
| // DC huffman tables , no more than 4 (0..3) |
| std::array<Huffman_table, 4> HTDC; |
| // AC huffman tables (0..3) |
| std::array<Huffman_table, 4> HTAC; |
| std::array<int, 256> m_CrToR; |
| std::array<int, 256> m_CbToB; |
| std::array<int, 256> m_CrToG; |
| std::array<int, 256> m_CbToG; |
| std::array<int, 256> m_Y; |
| unsigned long buffer_index; |
| uint32_t codebuf, newbuf, readbuf; |
| uint8_t *std_luminance_qt; |
| uint8_t *std_chrominance_qt; |
| |
| signed short int DCY, DCCb, DCCr; // Coeficientii DC pentru Y,Cb,Cr |
| signed short int DCT_coeff[384]; |
| // std::vector<signed short int> DCT_coeff; // Current DCT_coefficients |
| // quantization table number for Y, Cb, Cr |
| uint8_t YQ_nr = 0, CbQ_nr = 1, CrQ_nr = 1; |
| // DC Huffman table number for Y,Cb, Cr |
| uint8_t YDC_nr = 0, CbDC_nr = 1, CrDC_nr = 1; |
| // AC Huffman table number for Y,Cb, Cr |
| uint8_t YAC_nr = 0, CbAC_nr = 1, CrAC_nr = 1; |
| int txb, tyb; |
| int newbits; |
| uint8_t *rlimit_table; |
| std::vector<RGB> YUVBuffer; |
| // TODO(ed) this shouldn't exist. It is cruft that needs cleaning up' |
| uint32_t *Buffer; |
| |
| public: |
| std::vector<RGB> OutBuffer; |
| }; |
| } |