⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coder_bpb.c

📁 好东西呢
💻 C
📖 第 1 页 / 共 2 页
字号:

//#define ADD_CUR_WEIGHTS	// hurts !?
//#define LOCAL_LENS		// hurts
#define SCALEDOWN_LENS
#define SCALEDOWN_SHIFT	0	// makes len* the very previous!

//#define LOG

#define FIFTH_SHAPE		// helps a peetle
//#define XX_SHAPES		// hurts a tad
//#define NO_XX			// X3 and X4 help an awful lot
#define BIG_SHAPE_CNTX	// helps 0.006
// #define BIG_SIGN_CONTEXT // hurts a teeny

/*****

	these are the weights for combining the various
	moments to make an estimate	of the local activity

context = ((CA * VD + CB * P + CC * N + CD * W + CE * NW + CF * NE + CG * X1 + CH * X2 + CI * X3 + CJ * X4)>>8)

<>	these need to be trained on a test set	(not trivial)

	and very important ! little random changes make 0.01 bpp diff

******/

#define C1A 64
#define C1B 64
#define C1C 64
#define C1D 64
#define C1E 64
#define C1F 64
#define C1G 64
#define C1H 64
#define C1I 64
#define C1J 64

#define C2A 33
#define C2B 33
#define C2C 33
#define C2D 33
#define C2E 33
#define C2F 33
#define C2G 33
#define C2H 33
#define C2I 33
#define C2J 33

#define C3A 300
#define C3B 170
#define C3C 40
#define C3D 40
#define C3E 40
#define C3F 40
#define C3G 20
#define C3H 20
#define C3I 0
#define C3J 0

#define C4A 64
#define C4B 32
#define C4C 64
#define C4D 64
#define C4E 64
#define C4F 64
#define C4G 32
#define C4H 32
#define C4I 32
#define C4J 32

/*****

binary version : codes each bit-pel as a separate binary event

coding signs helps about 0.03 (better than the 0.02 observed
	in earlier more primitive sign coders)

our LOE :
	don't actually compare the four contexts' MPS's, but just the four coders'
	recent performance: choose the coder that had the lowest entropy on the
	last N pixels. (some decaying record).

instead of LOE, blend them based on confidence
		(this is unusually easy because of the fact that
		the alphabet is binary)
	something like P_tot = W_tot * Sum P[n] / w[n]

	where w is the weight = actual recent coded len
		(TMW uses 2^(-w) instead of 1/w )
		(that may actually be better cuz we can do it with shifts;
		you need some subtlety : find the smallest weight and factor it out first)

* we're very similar to ECECOW, but getting stomped. 
  he beats us by almost 1.0 PSNR at the same bitrate

---

e512 lossless, l6

	lena : 4.141	(ececow : 4.06)
	barb : 4.468	(ececow : 4.34)

*****/

#include <stdio.h>
#include <stdlib.h>
#include <crblib/inc.h>
#include <crblib/arithc.h>
#include <crblib/intmath.h>
#include "coder.h"
#include "subbands.h"

extern int tune_param;

#define	VAL_CONTEXTS		20
#define	VAL_CONTEXT_MAX		(VAL_CONTEXTS -1)
#define SHAPE_BASE			VAL_CONTEXTS
#define SHAPE(x)			(SHAPE_BASE<<(x))

#ifdef BIG_SHAPE_CNTX
#define NUM_SHAPES			5
#else
#define NUM_SHAPES			2
#endif

#define NUM_CONTEXTS		(VAL_CONTEXTS<<NUM_SHAPES)

#ifdef BIG_SIGN_CONTEXT
#define SIGN_CONTEXTS	729	// 3^6
#else
#define SIGN_CONTEXTS	81	// 3^4
#endif // BIG_SIGN_CONTEXT

#define TOTMAX			4000
#define INC				30

#define P0_INIT			8
#define P1_INIT			0

#define bitModel(bit,P0,PT)	do { PT += INC; if (!(bit)) P0 += INC;  if ( PT > TOTMAX ) { PT >>= 1; P0 >>= 1; P0++; PT += 2; } } while(0)
#define bitEnc(bit,ari,P0,PT)	do { arithEncBit(ari,P0,PT,bit);	bitModel(bit,P0,PT); } while(0)
#define bitDec(bit,ari,P0,PT)	do { bit = arithDecBit(ari,P0,PT);	bitModel(bit,P0,PT); } while(0)

#define AddSignContext(context,val,mask) do { context *= 3; if( abs(val)&(mask) ) { if ( (val) > 0 ) context ++; else context += 2; } } while(0)

#ifdef LOG
int nbest1=0,nbest2=0,nbest3=0,nbest4=0;
#endif

typedef struct {
	int p0,pt;
} binContext;

void coderBPbin_encodeSubbandBP(coder *me,subband_leaf *sb,int);
void coderBPbin_decodeSubbandBP(coder *me,subband_leaf *sb,int);

typedef struct {
	binContext signs[SIGN_CONTEXTS];
	binContext	stats1[NUM_CONTEXTS],
				stats2[NUM_CONTEXTS],
				stats3[NUM_CONTEXTS],
				stats4[NUM_CONTEXTS];
} myInfo;

void coderBPbin_init(coder *c)
{
myInfo *d;
int i;

	if ( (d = new(myInfo)) == NULL )
		errexit("ozero init failed");

	c->data = d;

	for(i=0;i<NUM_CONTEXTS;i++) {
		d->stats1[i].p0 = P0_INIT+1; d->stats1[i].pt = 2+P0_INIT+P1_INIT;
		d->stats2[i].p0 = P0_INIT+1; d->stats2[i].pt = 2+P0_INIT+P1_INIT;
		d->stats3[i].p0 = P0_INIT+1; d->stats3[i].pt = 2+P0_INIT+P1_INIT;
		d->stats4[i].p0 = P0_INIT+1; d->stats4[i].pt = 2+P0_INIT+P1_INIT;
	}

	for(i=0;i<SIGN_CONTEXTS;i++) {
		d->signs[i].p0 = 1;
		d->signs[i].pt = 2;
	}

}

void coderBPbin_free(coder *c)
{
	if ( c->data ) {
		myInfo *d;
		d = c->data;
		free(d);
		c->data = NULL;
	}
}

coder coderBPbin = {
		"BitPlane Binary",
		coderBPbin_init,
		coderBPbin_free,
		NULL,NULL,NULL,NULL,NULL,NULL,
		coderBPbin_encodeSubbandBP,
		coderBPbin_decodeSubbandBP
	};

/**********

lazy way to pass the state from getStats to fixStats
	and also interacts with the codeBand()

these are re-initialized at each codeBand() call, so this is
	quite re-entrant as long as we aren't multi-threaded
	(that is, no more than one call to codeBand() at a time)

*********/

static int VD;
static binContext *stats1,*stats2,*stats3,*stats4;
static binContext *s1,*s2,*s3,*s4;
static int len1,len2,len3,len4;
static int *sister_x,*sister_y,sister_trans,*parent,parent_step,parent_mask,parent_shift;
static int p0,pt,donemask,nextmask;

int intentropy(int p0,int pt)
{
return ((p0*(log2x16(pt) - log2x16(p0)) + (pt-p0)*(log2x16(pt) - log2x16(pt-p0)))<<3)/pt;
}
int LOEweight(int p0,int pt)
{
if ( (pt - p0) < p0 ) p0 = (pt - p0);
return log2x16(pt) - log2x16(p0);
}

static void getStats(int *dp,int *pp,int x,int y,int width,int height,int fullw)
{
int shapes;
int P,N,S,E,W,NE,NW,X1,X2,X3,X4;
int diff,lena,lenb;
binContext *sa,*sb;

	/*** elaborate context-making ***/

	VD	= abs(*dp)&donemask;	// current val already done

	P	= abs(*pp)&nextmask;

	if ( y == 0 ) {
		N = NW = NE = VD;
		if ( x == 0 ) W = VD; else W = abs(dp[-1]) & nextmask;
	} else if ( x == 0 ) {
		W = NW = 0;
		N  = abs(dp[-fullw])	& nextmask;
		NE = abs(dp[1-fullw])	& nextmask;
	} else {
		N = abs(dp[-fullw])		& nextmask;
		W = abs(dp[-1])			& nextmask;
		NW = abs(dp[-1-fullw])	& nextmask;
		if ( x == (width-1) ) NE = VD;
		else NE = abs(dp[1-fullw]) & nextmask;
	}

	if ( y < (height-1) ) S = abs(dp[fullw]) & donemask; else S = VD;
	if ( x < (width-1) ) E = abs(dp[1])	& donemask;	else E = VD;

	if ( sister_x ) {
			X1 = S; X2 = E;
		switch(sister_trans) {
			case 0:
				X3 = abs( sister_x[ x + fullw*y ] ) & nextmask;
				X4 = abs( sister_y[ x + fullw*y ] ) & nextmask;
				break;
			case 1:
				X3 = abs( sister_x[ y + fullw*x ] ) & nextmask;
				X4 = abs( sister_y[ x + fullw*y ] ) & nextmask;
				break;
			case 2:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -