📄 zc030x_jpeg.c
字号:
} /* Return the found code */ return(inArray[MatchFound].Code); }// Modified by Cyril (Created)#ifdef CompileTable // Note that this is not the default quantization tables, because they // integrate apart of the iDCT in them (in fact the cos(2kpi/8) * sqrt(2) // This avoid computing those factors for each decoding. // If you need to implement an other jpeg decoder, you could use the default // tables defined in JpegLib // Define default quantification tables unsigned char LumaQuantTable[64] = { 32, 33 , 36 , 32 , 36 , 37 , 54 , 39, 33, 46 , 50 , 58 , 61 , 78 , 96 , 70, 26, 50 , 54 , 67 , 99 , 114, 110, 69, 37, 65 , 73 , 82 , 131, 118, 111, 63, 48, 72 , 104, 122, 136, 128, 112, 61, 62, 126, 119, 162, 172, 128, 103, 43, 56, 90 , 98 , 101, 112, 96 , 70 , 31, 34, 42 , 40 , 40 , 43 , 39 , 30 , 15}; unsigned short ChromaQuantTable[64] = { 36 , 49 , 62 , 112, 200, 157, 108, 55, 49 , 84 , 94 , 215, 277, 217, 150, 76, 62 , 94 , 191, 307, 261, 205, 141, 72, 112, 215, 307, 276, 235, 184, 127, 64, 200, 277, 261, 235, 200, 157, 108, 55, 157, 217, 205, 184, 157, 123, 85 , 43, 108, 150, 141, 127, 108, 85 , 58 , 29, 55 , 76 , 72 , 64 , 55 , 43 , 29 , 15 }; // // Standard Huffman Decompression Tables (taken from jpeglib) // // DC Luminance /* number-of-symbols-of-each-code-length count */ unsigned char bits_dc_luminance[16] = { /* 0-base */ 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; unsigned char val_dc_luminance[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // DC Chrominance /* number-of-symbols-of-each-code-length count */ unsigned char bits_dc_chrominance[16] = { /* 0-base */ 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; unsigned char val_dc_chrominance[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // AC Luminance /* number-of-symbols-of-each-code-length count */ unsigned char bits_ac_luminance[16] = { /* 0-base */ 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; unsigned char val_ac_luminance[] = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,\ 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,\ 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,\ 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,\ 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,\ 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,\ 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,\ 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,\ 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,\ 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,\ 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,\ 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,\ 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,\ 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,\ 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,\ 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,\ 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,\ 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,\ 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,\ 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,\ 0xf9, 0xfa }; // AC Chrominance /* number-of-symbols-of-each-code-length count */ unsigned char bits_ac_chrominance[16] = { /* 0-base */ 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; unsigned char val_ac_chrominance[] = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,\ 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,\ 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,\ 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,\ 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,\ 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,\ 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,\ 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,\ 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,\ 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,\ 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,\ 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,\ 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,\ 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,\ 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,\ 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,\ 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,\ 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,\ 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,\ 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,\ 0xf9, 0xfa };#endif/* Init the decoder */ void zc030x_jpeg_init(){ /* Iterators */ u32 i, j;#ifndef CompileTable FILE * pFileTable = NULL; u8 aoTables[1024]; u8 tmp1, tmp2; u32 lFileSize; // Read quantizer file (and scale them by square root of 2) // pFileTable = fopen("tables.quant", "rb"); fseek(pFileTable, 0, SEEK_END); lFileSize = ftell(pFileTable); fseek(pFileTable, 0, SEEK_SET); fread(aoTables, 1, 2, pFileTable); PDEBUG(3,"%02X %02X - %d", aoTables[0], aoTables[1], lFileSize); if (aoTables[0] != 0xff || aoTables[1] != 0xdb) { // Don't remove backet as debugmsg is a macro that can be voided PDEBUG(3,"Error with tables.quant files"); } fread(aoTables, 1, lFileSize - 2, pFileTable); JPGGetQuantTables(aoTables); // Advance 2 bytes tmp1 = JPGGetByte(aoTables); tmp2 = JPGGetByte(aoTables); PDEBUG(3,"Read %02X %02X - must be FF DB", tmp1, tmp2); JPGGetQuantTables(aoTables); findex = 0; fclose(pFileTable); #else /* Save the quantization tables for luminance */ for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) QuantTable[0][j][i] = LumaQuantTable[i * 8 + j]; /* Save the quantization tables for chrominance */ for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) QuantTable[1][j][i] = ChromaQuantTable[i * 8 + j]; #endif // The table above are the same as in the file "tables.default" // When testing, it's easier to change a file (doesn't need recompiling)#ifndef CompileTable // Save huffman tables pFileTable = fopen("tables.default", "rb"); fseek(pFileTable, 0, SEEK_END); lFileSize = ftell(pFileTable); fseek(pFileTable, 0, SEEK_SET); fread(aoTables, 1, 2, pFileTable); PDEBUG(3,"%02X %02X - %d", aoTables[0], aoTables[1], lFileSize); if (aoTables[0] != 0xff || aoTables[1] != 0xc4) { // Don't remove backet as debugmsg is a macro that can be voided PDEBUG(3,"Error with tables.default files"); } fread(aoTables, 1, lFileSize - 2, pFileTable); JPGGetHuffTables(aoTables); // Advance 2 bytes tmp1 = JPGGetByte(aoTables); tmp2 = JPGGetByte(aoTables); PDEBUG(3,"Read %02X %02X - must be FF C4", tmp1, tmp2); JPGGetHuffTables(aoTables); // Advance 2 bytes // Advance 2 bytes tmp1 = JPGGetByte(aoTables); tmp2 = JPGGetByte(aoTables); PDEBUG(3,"Read %02X %02X - must be FF C4", tmp1, tmp2); JPGGetHuffTables(aoTables); // Advance 2 bytes // Advance 2 bytes tmp1 = JPGGetByte(aoTables); tmp2 = JPGGetByte(aoTables); PDEBUG(3,"Read %02X %02X - must be FF C4", tmp1, tmp2); JPGGetHuffTables(aoTables); fclose(pFileTable); findex = 0;#else /* Save the huffman tables */ SaveTable((PJPGHuffmanEntry)HuffmanDC0, bits_dc_luminance, val_dc_luminance); SaveTable((PJPGHuffmanEntry)HuffmanAC0, bits_ac_luminance, val_ac_luminance); SaveTable((PJPGHuffmanEntry)HuffmanDC1, bits_dc_chrominance, val_dc_chrominance); SaveTable((PJPGHuffmanEntry)HuffmanAC1, bits_ac_chrominance, val_ac_chrominance);#endif PDEBUG(3,"-- JPEG compliant decoder initialized --"); }/* Fast integer IDCT */void jpeg_idct_ifast (s16 inarray[8][8], s16 outarray[8][8], u16 QuantNum){ /* Temporary variable */ /* Please note that even if results are 16 bits, we are using processor sized variable to speed up the process */ s32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; s32 tmp10, tmp11, tmp12, tmp13; s32 z5, z10, z11, z12, z13; u32 ctr; s16 warray[8][8]; /* Pass 1: process columns from input, store into work array. */ for (ctr = 0; ctr < 8; 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 (inarray[1][ctr] == 0 && inarray[2][ctr] == 0 && inarray[3][ctr] == 0 && inarray[4][ctr] == 0 && inarray[5][ctr] == 0 && inarray[6][ctr] == 0 && inarray[7][ctr] == 0) { /* AC terms all zero */ s16 dcval = inarray[0][ctr] * QuantTable[QuantNum][0][ctr]; warray[0][ctr] = dcval; warray[1][ctr] = dcval; warray[2][ctr] = dcval; warray[3][ctr] = dcval; warray[4][ctr] = dcval; warray[5][ctr] = dcval; warray[6][ctr] = dcval; warray[7][ctr] = dcval; continue; } /* Even part */ tmp0 = inarray[0][ctr] * QuantTable[QuantNum][0][ctr]; tmp1 = inarray[2][ctr] * QuantTable[QuantNum][2][ctr]; tmp2 = inarray[4][ctr] * QuantTable[QuantNum][4][ctr]; tmp3 = inarray[6][ctr] * QuantTable[QuantNum][6][ctr]; tmp10 = tmp0 + tmp2; /* phase 3 */ tmp11 = tmp0 - tmp2; tmp13 = tmp1 + tmp3; /* phases 5-3 */ tmp12 = (((tmp1 - tmp3)*( FIX_1_414213562)) >> 8) - tmp13; /* 2*c4 */ tmp0 = tmp10 + tmp13; /* phase 2 */ tmp3 = tmp10 - tmp13; tmp1 = tmp11 + tmp12; tmp2 = tmp11 - tmp12; /* Odd part */ tmp4 = inarray[1][ctr] * QuantTable[QuantNum][1][ctr]; tmp5 = inarray[3][ctr] * QuantTable[QuantNum][3][ctr]; tmp6 = inarray[5][ctr] * QuantTable[QuantNum][5][ctr]; tmp7 = inarray[7][ctr] * QuantTable[QuantNum][7][ctr]; z13 = tmp6 + tmp5; /* phase 6 */ z10 = tmp6 - tmp5; z11 = tmp4 + tmp7; z12 = tmp4 - tmp7; tmp7 = z11 + z13; /* phase 5 */ tmp11 = ((z11 - z13) * FIX_1_414213562) >> 8; /* 2*c4 */ z5 = ((z10 + z12) * FIX_1_847759065) >> 8; /* 2*c2 */ tmp10 = ((z12 * FIX_1_082392200) >> 8) - z5; /* 2*(c2-c6) */ tmp12 = ((z10 * - FIX_2_613125930) >> 8) + z5; /* -2*(c2+c6) */ tmp6 = tmp12 - tmp7; /* phase 2 */ tmp5 = tmp11 - tmp6; tmp4 = tmp10 + tmp5; warray[0][ctr] = (tmp0 + tmp7); warray[7][ctr] = (tmp0 - tmp7); warray[1][ctr] = (tmp1 + tmp6); warray[6][ctr] = (tmp1 - tmp6); warray[2][ctr] = (tmp2 + tmp5); warray[5][ctr] = (tmp2 - tmp5); warray[4][ctr] = (tmp3 + tmp4); warray[3][ctr] = (tmp3 - tmp4); } /* 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. */ for (ctr = 0; ctr < 8; ctr++) { /* 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. */ if (warray[ctr][1] == 0 && warray[ctr][2] == 0 && warray[ctr][3] == 0 && warray[ctr][4] == 0 && warray[ctr][5] == 0 && warray[ctr][6] == 0 && warray[ctr][7] == 0) { /* AC terms all zero */ s16 dcval = (warray[ctr][0] >> 5)+128; if (dcval<0) dcval = 0; if (dcval>255) dcval = 255; outarray[ctr][0] = dcval; outarray[ctr][1] = dcval; outarray[ctr][2] = dcval; outarray[ctr][3] = dcval; outarray[ctr][4] = dcval; outarray[ctr][5] = dcval; outarray[ctr][6] = dcval; outarray[ctr][7] = dcval; continue; } /* Even part */ tmp10 = warray[ctr][0] + warray[ctr][4]; tmp11 = warray[ctr][0] - warray[ctr][4]; tmp13 = warray[ctr][2] + warray[ctr][6]; tmp12 = (((warray[ctr][2] - warray[ctr][6]) * FIX_1_414213562) >> 8) - tmp13; tmp0 = tmp10 + tmp13; tmp3 = tmp10 - tmp13; tmp1 = tmp11 + tmp12; tmp2 = tmp11 - tmp12; /* Odd part */ z13 = warray[ctr][5] + warray[ctr][3]; z10 = warray[ctr][5] - warray[ctr][3]; z11 = warray[ctr][1] + warray[ctr][7]; z12 = warray[ctr][1] - warray[ctr][7]; tmp7 = z11 + z13; /* phase 5 */ tmp11 = ((z11 - z13) * FIX_1_414213562) >> 8; /* 2*c4 */ z5 = ((z10 + z12) * FIX_1_847759065) >> 8; /* 2*c2 */ tmp10 = ((z12 * FIX_1_082392200) >> 8) - z5; /* 2*(c2-c6) */ tmp12 = ((z10 * - FIX_2_613125930) >> 8) + 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 */ outarray[ctr][0] = ((tmp0 + tmp7) >> 5)+128;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -