www.pudn.com > xiaobo.zip.zip > coder_bp.c


/***** 
 
coder_BP : send BPs ; this is an "EZ" coder, 
 
Checa lossless : 
	band-then-bp : 3.43 
	bp-then-band : 3.39 
 
Lena -qu10 -l5 (37.61 psnr) 
	band-then-bp : 0.607 
	bp-then-band : 0.598 
 
*****/ 
 
#include  
#include  
#include  
#include  
#include  
 
extern int tune_param; 
 
#define ORDER1_CONTEXTS		10 
#define ORDER1_ALPHABET		16	//4 bits 
 
#define ORDER1_TOTMAX		15000 
#define ORDER1_INC			30 
 
#include "coder.h" 
 
void coderBP_encodeBandBP(coder *me,int *band,int w,int h,int fullw,int *parent,int); 
void coderBP_decodeBandBP(coder *me,int *band,int w,int h,int fullw,int *parent,int); 
 
typedef struct { 
	scontext ** o1; 
} myInfo; 
 
void coderBP_init(coder *c) 
{ 
myInfo *d; 
int i; 
 
	d = new(myInfo); 
	assert(d); 
 
	c->data = d; 
 
	d->o1 = newarray(void *,ORDER1_CONTEXTS); 
	assert(d->o1); 
 
	for(i=0;io1[i] = scontextCreate(c->arith,ORDER1_ALPHABET,0, 
				ORDER1_TOTMAX,ORDER1_INC,true); 
		assert( d->o1[i] ); 
	} 
} 
 
void coderBP_free(coder *c) 
{ 
	if ( c->data ) { 
		myInfo *d; 
		d = c->data; 
		if ( d->o1 ) { 
			int i; 
			for(i=0;io1[i] ) scontextFree(d->o1[i]); 
			} 
		} 
		free(d); 
		c->data = NULL; 
	} 
} 
 
coder coderBP = { 
		"BitPlane", 
		coderBP_init, 
		coderBP_free, 
		NULL,NULL, 
		coderBP_encodeBandBP, 
		coderBP_decodeBandBP 
	}; 
 
void coderBP_encodeBandBP(coder *me,int *band,int width,int height,int fullw,int *parent,int bitmask) 
{ 
int x,y; 
int *dp,*pp,*dpn; 
int context,block,par,A,B,C,D; 
scontext **o1 = ((myInfo *)me->data)->o1; 
arithInfo *ari = me->arith; 
int donemask,nextmask; 
	 
	for(x= bitmask,nextmask=0; x>1]); 
			A = abs(dp[x]);		B = abs(dp[x+1]); 
			C = abs(dpn[x]);	D = abs(dpn[x+1]); 
 
			context = ((A & donemask)?1:0) + ((B & donemask)?1:0) + ((C & donemask)?1:0) + ((D & donemask)?1:0); 
			if (par&nextmask) context += 5; 
 
			block = 0; // 4 bits 
			if ( A & bitmask ) block += 1; 
			if ( B & bitmask ) block += 2; 
			if ( C & bitmask ) block += 4; 
			if ( D & bitmask ) block += 8; 
 
			scontextEncode(o1[context],block); 
 
			/** send signs when we see the first 'on' bit **/ 
 
			if ( (A & nextmask) == bitmask ) arithBit(ari, signbit(dp[x]   ) ); 
			if ( (B & nextmask) == bitmask ) arithBit(ari, signbit(dp[x+1] ) ); 
			if ( (C & nextmask) == bitmask ) arithBit(ari, signbit(dpn[x]  ) ); 
			if ( (D & nextmask) == bitmask ) arithBit(ari, signbit(dpn[x+1]) );	 
 
		} 
		pp += fullw; 
		dp += fullw + fullw; 
	} 
 
} 
 
void coderBP_decodeBandBP(coder *me,int *band,int width,int height,int fullw,int *parent,int bitmask) 
{ 
int x,y,par; 
int *dp,*pp,*dpn; 
int context,block,A,B,C,D; 
scontext **o1 = ((myInfo *)me->data)->o1; 
arithInfo *ari = me->arith; 
int donemask,nextmask; 
 
	for(x= bitmask,nextmask=0; x>1]); 
			A = abs(dp[x]);		B = abs(dp[x+1]); 
			C = abs(dpn[x]);	D = abs(dpn[x+1]); 
			context = ((A & donemask)?1:0) + ((B & donemask)?1:0) + ((C & donemask)?1:0) + ((D & donemask)?1:0); 
			if (par&nextmask) context += 5; 
 
			block = scontextDecode(o1[context]); 
 
			if ( block & 1 ) {  
				if ( !A ) dp[x] = arithGetBit(ari)? -bitmask: bitmask; 
				else if ( isneg(dp[x]) ) dp[x] -= bitmask; else dp[x] += bitmask; 
			} 
			if ( block & 2 ) {  
				if ( !B ) dp[x+1] = arithGetBit(ari)? -bitmask: bitmask; 
				else if ( isneg(dp[x+1]) ) dp[x+1] -= bitmask; else dp[x+1] += bitmask; 
			} 
			if ( block & 4 ) {  
				if ( !C ) dpn[x] = arithGetBit(ari)? -bitmask: bitmask; 
				else if ( isneg(dpn[x]) ) dpn[x] -= bitmask; else dpn[x] += bitmask; 
			} 
			if ( block & 8 ) {  
				if ( !D ) dpn[x+1] = arithGetBit(ari)? -bitmask: bitmask; 
				else if ( isneg(dpn[x+1]) ) dpn[x+1] -= bitmask; else dpn[x+1] += bitmask; 
			} 
		} 
		pp += fullw; 
		dp += fullw + fullw; 
	} 
 
}