📄 jchuff.c
字号:
* This routine also performs some validation checks on the table. * * Note this is also used by jcphuff.c. */GLOBAL(void)jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl ** pdtbl){ JHUFF_TBL *htbl; c_derived_tbl *dtbl; int p, i, l, lastp, si, maxsymbol; char huffsize[257]; unsigned int huffcode[257]; unsigned int code; //int k1; //pwhsu++:20031022 unsigned int *curtbl; //pwhsu++:20040107 unsigned int codesize; //pwhsu++:20040107 /* Note that huffsize[] and huffcode[] are filled in code-length order, * paralleling the order of the symbols themselves in htbl->huffval[]. */ /* Find the input Huffman table */ if (tblno < 0 || tblno >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); htbl = isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; if (htbl == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); /* Allocate a workspace if we haven't already done so. */ if (*pdtbl == NULL) *pdtbl = (c_derived_tbl *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(c_derived_tbl)); dtbl = *pdtbl; /* Figure C.1: make table of Huffman code length for each symbol */ p = 0; for (l = 1; l <= 16; l++) { i = (int) htbl->bits[l]; if (i < 0 || p + i > 256) /* protect against table overrun */ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); while (i--) huffsize[p++] = (char) l; } huffsize[p] = 0; lastp = p; /* Figure C.2: generate the codes themselves */ /* We also validate that the counts represent a legal Huffman code tree. */ code = 0; si = huffsize[0]; p = 0; while (huffsize[p]) { while (((int) huffsize[p]) == si) { huffcode[p++] = code; code++; } /* code is now 1 more than the last code used for codelength si; but * it must still fit in si bits, since no code is allowed to be all ones. */ if (((INT32) code) >= (((INT32) 1) << si)) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); code <<= 1; si++; } /* Figure C.3: generate encoding tables */ /* These are code and size indexed by symbol value */ /* Set all codeless symbols to have code length 0; * this lets us detect duplicate VAL entries here, and later * allows emit_bits to detect any attempt to emit such symbols. */// MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi)); //pwhsu--:20040128 memzero //pwhsu++:20031022 /* for ( k1=0; k1<256; k1++){ if (isDC){ rinfo.HuffsiDC[tblno][k1] = 0; } else{ rinfo.HuffsiAC[tblno][k1] = 0; } } //pwhsu++:20031022 */ /* This is also a convenient place to check for out-of-range * and duplicated VAL entries. We allow 0..255 for AC symbols * but only 0..15 for DC. (We could constrain them further * based on data depth and mode, but this seems enough.) */ maxsymbol = isDC ? 15 : 255; //pwhsu++:20040107 if(isDC){ curtbl = tblno ? huftbl1_dc : huftbl0_dc; }else{ curtbl = tblno ? huftbl1_ac : huftbl0_ac; } for (p = 0; p < lastp; p++) { i = htbl->huffval[p]; //if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);// dtbl->ehufco[i] = huffcode[p]; /*// dtbl->ehufsi[i] = huffsize[p]; //pwhsu++:20031022 // if (isDC){// rinfo.HuffcoDC[tblno][i] = huffcode[p];// rinfo.HuffsiDC[tblno][i] = huffsize[p]; // }// else{// rinfo.HuffcoAC[tblno][i] = huffcode[p];// rinfo.HuffsiAC[tblno][i] = huffsize[p];// } //pwhsu++:20031022 */ codesize = (huffcode[p]<< (5 + i%16) ) | (huffsize[p]+i%16);#ifdef AHB_interface //__asm{ //STR codesize, [curtbl+i] //} curtbl[i]= codesize;#else curtbl[i] = codesize;#endif }}LOCAL(boolean)flush_bits (void){ unsigned char curbits; unsigned char curbits2; unsigned char tmp; unsigned int buf1; unsigned char buf2; //unsigned int *vpe_stop = (unsigned int *) (VPE); //__asm{ READ_VADR(curbits) READ_VLASTWORD(buf1) //} tmp = (((curbits-1) & 0xff) % 8); curbits2 = 7-tmp; /* fill any partial byte with ones */ if (curbits2 != 0){ BitstreamPutBits((0x7F)>>tmp, curbits2); buf2 = ( buf1 >> ( ( 32-((curbits)&0x1F))&0x18 ) ) & 0xFF; switch(tmp){ case 0: buf2 = buf2 & 0x80 | 0x7f; break; case 1: buf2 = buf2 & 0xc0 | 0x3f; break; case 2: buf2 = buf2 & 0xe0 | 0x1f; break; case 3: buf2 = buf2 & 0xf0 | 0x0f; break; case 4: buf2 = buf2 & 0xf8 | 0x07; break; case 5: buf2 = buf2 & 0xfc | 0x03; break; case 6: buf2 = buf2 & 0xfe | 0x01; break; default: buf2 = 0x0; break; } if(buf2 == 0xff){ //__asm{ //check the write procedure is done //MCR p15, 0, 0, c7, c10, 4 //} FA526_DrainWriteBuffer(); BitstreamPutBits(0, 8); } } return TRUE;}/* * Emit a restart marker & resynchronize predictions. */LOCAL(boolean)emit_restart (int restart_num){ if (! flush_bits()) return FALSE; //BitstreamPutBits(0xFF,8); BitstreamPutBits( 0xFF00 | JPEG_RST0 + restart_num,16); //__asm { SET_PYDCR(0) SET_PUVDCR(0) //} return TRUE;}/* * Finish up at the end of a Huffman-compressed scan. */METHODDEF(void)finish_pass_huff (j_compress_ptr cinfo){ flush_bits();}/* * Module initialization routine for Huffman entropy encoding. */GLOBAL(void)jinit_huff_encoder (j_compress_ptr cinfo){ huff_entropy_ptr entropy; int i; entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_encoder)); cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;// entropy->pub.start_pass = start_pass_huff; /* Mark tables unallocated */ for (i = 0; i < NUM_HUFF_TBLS; i++) { entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; }}//pwhsu++:20031030//The modified function of Encode one MCUboolean Encode_mcu (j_compress_ptr cinfo){ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; //working_state state; /* Update restart-interval state too */ //if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; //} /* Emit restart marker if needed */ //if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { if (! emit_restart(entropy->next_restart_num)) return FALSE; } //} return TRUE;}//pwhsu++:20031030
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -