📄 jdarith.c
字号:
/* * jdarith.c *This programe is reedited by Fujian Shi(fieagle@yahoo.com.cn). * The primitive code is writed by Guido Vollbeding <guivol@esc.de>. * This program is designed to finish arithmetic decoding. */#include "commondecls.h"#define RIGHT_SHIFT(x,shft) x>=0? x>>shft:-((-x)>>shft)/* The following two definitions specify the allocation chunk size * for the statistics area. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least * 49 statistics bins for DC, and 245 statistics bins for AC coding. * Note that we use one additional AC bin for codings with fixed * probability (0.5), thus the minimum number for AC is 246. * * We use a compact representation with 1 byte per statistics bin, * thus the numbers directly represent byte sizes. * This 1 byte per statistics bin contains the meaning of the MPS * (more probable symbol) in the highest bit (mask 0x80), and the * index into the probability estimation state machine table * in the lower bits (mask 0x7F). */LOCAL (void)fill_input_buffer (j_compress_ptr cinfo){ jpeg_source_mgr *src = cinfo->src; size_t nbytes; nbytes = JFREAD(cinfo->inputfile, cinfo->inbuffer[0], 4096); if (nbytes <= 0) { /* Insert a fake EOI marker to jump out the decoding programe*/ cinfo->inbuffer[0][0] = (JOCTET) 0xFF; cinfo->inbuffer[0][1] = (JOCTET) 0xD9; nbytes = 2; } src->next_input_byte = cinfo->inbuffer[0]; src->bytes_in_buffer = nbytes; }LOCAL(int)get_byte (j_compress_ptr cinfo)/* Read next input byte; we do not support suspension in this module. */{ jpeg_source_mgr * src = cinfo->src; int data; if (src->bytes_in_buffer == 0){ printf("yes\n"); fill_input_buffer(cinfo); } src->bytes_in_buffer--; data=*src->next_input_byte++; return (data);}/* * The core arithmetic decoding routine (common in JPEG and JBIG). * This needs to go as fast as possible. * Machine-dependent optimization facilities * are not utilized in this portable implementation. * However, this code should be fairly efficient and * may be a good base for further optimizations anyway. * * Return value is 0 or 1 (binary decision). * * Note: I've changed the handling of the code base & bit * buffer register C compared to other implementations * based on the standards layout & procedures. * While it also contains both the actual base of the * coding interval (16 bits) and the next-bits buffer, * the cut-point between these two parts is floating * (instead of fixed) with the bit shift counter CT. * Thus, we also need only one (variable instead of * fixed size) shift for the LPS/MPS decision, and * we can get away with any renormalization update * of C (except for new data insertion, of course). * * I've also introduced a new scheme for accessing * the probability estimation state machine table, * derived from Markus Kuhn's JBIG implementation. */LOCAL(int)arith_decode (j_compress_ptr cinfo, unsigned char *st){ extern const INT32 jaritab[]; register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; register unsigned char nl, nm; register INT32 qe, temp; register int sv, data,cx; /* Fetch values from our compact representation of Table D.2: * Qe values and probability estimation state machine */ cx=(e->c>>16)&0x0000ffff; /*printf("%x",cx);*/ sv = *st; qe = jaritab[sv & 0x7F]; /* => Qe_Value */ nl = qe & 0xFF; qe =(qe>>8)&0x00ffffff ; /* Next_Index_LPS + Switch_MPS */ nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ temp = e->a - qe; e->a = temp; /*temp <<= e->ct;*/ if (cx >= temp) { cx -= temp; /* Conditional LPS (less probable symbol) exchange */ if (e->a < qe) { e->a = qe; *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ } else { e->a = qe; *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ sv ^= 0x80; /* Exchange LPS/MPS */ } } else if (e->a < 0x8000L) { /* Conditional MPS (more probable symbol) exchange */ if (e->a < qe) { *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ sv ^= 0x80; /* Exchange LPS/MPS */ } else { *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ } } e->c =(e->c & 0xFFFF)|(cx<<16); /* Renormalization & data input per section D.2.6 */ while (e->a < 0x8000L) { if (e->ct == 0) { /* Need to fetch next data byte */ e->ct=8; if (cinfo->unread_marker){ printf("%x \n",cinfo->unread_marker); data = 0; } else { data = get_byte(cinfo); /* read next input byte */ if (data==0xff) { if ((data=get_byte(cinfo))==0) data=0xff; else { cinfo->unread_marker=data; data=0; } } e->c +=(data<<8); } } e->a <<=1; e->c <<=1; e->ct--; } return (sv >> 7);}voiddecode_row (j_compress_ptr cinfo,int i){ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; unsigned char * st; unsigned char context_a=0; int num,ci, tbl, k, ke; int v, v2, m,sign,Ra=0,Rc=0,predic_val=0; entropy->context=0; /* Encode the MCU data blocks */ for (num = 0; num < cinfo->image_width; num++) { /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ /*printf("%x ",entropy->context);*/ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ st =entropy->dc_stats + entropy->context*5+entropy->context_b[num]; /*predic_val=last_val+((entropy->val_b[num]-Rc)>>1);*/ /*predic_val=entropy->val_b[num]+RIGHT_SHIFT(last_val-Rc,1);*/ /*predic_val=RIGHT_SHIFT(Ra+entropy->val_b[num],1);*/ /* if (i==0) printf("num:%d bin:%d *st:%x laval:%d pre:%d\n",num,context_a,*st,last_val,predic_val);*/ /* Figure F.4: Encode_DC_DIFF */ if ((arith_decode(cinfo,st)) == 0) { entropy->context = 0; /* zero diff category */ v=0; } else { sign=arith_decode(cinfo, st+1); st +=2; st +=sign; if ((m = arith_decode(cinfo, st)) != 0) { if ( entropy->context_b[num]>8 ) st=entropy->dc_stats+129; else st = entropy->dc_stats + 100; /* Table H.3: X1 = X1_context(Db)*/ /*st=entropy->dc_stats+20;*/ while (arith_decode(cinfo, st)) { m <<=1; st += 1; } } if (m < (int) (((INT32) 1 << cinfo->arith_dc_L) >> 1)) entropy->context = 0; /* zero diff category */ else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U)>>1 )) entropy->context = 12 + (sign * 4); /* large diff category */ else entropy->context = 4 + (sign * 4); /* small diff category */ /* Figure F.24: Decoding the magnitude bit pattern of v */ v = m; st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m; /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ v += 1; if (sign) v = -v; } predic_val=return_pixel_val(cinfo,Ra,entropy->val_b[num],Rc,entropy->val_b[num+1],v); /*if (i==0) printf("row[%d]:%x v:%d\n",num,predic_val,v);*/ entropy->context_b[num]=entropy->context; Rc=entropy->val_b[num]; Ra=entropy->val_b[num]=predic_val; emit_byte(cinfo,predic_val); } }/*We assume the compressed data is larger than 8 bytes*/void /*Here we can try to use function pointer later for study.*/ initial_decoder(j_compress_ptr cinfo){ arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; int data; data=get_byte(cinfo); e->c +=(data<<8); e->c <<=8; data=get_byte(cinfo); e->c +=(data<<8); e->c <<=8;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -