Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
v11
Browse files Browse the repository at this point in the history
-cleanup
-fix jpeg cfg
-change test3d cfg, add exe cm
  • Loading branch information
kaitz committed Mar 18, 2019
1 parent 08857a4 commit 82d7f83
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 445 deletions.
149 changes: 77 additions & 72 deletions jpeg.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int zzv[64]={
int *cxt;// context hashes
int *cp; // context pointers

char *cbuf2;
int *cbuf2;
int *adv_pred;//, *sum;

//for parsing Quantization tables
Expand All @@ -100,7 +100,7 @@ int abs(int x){
// Detect invalid JPEG data. The proper response is to silently
// fall back to a non-JPEG model.
void jassert(int x,int x2) {
if (!(x)) jpeg=0;//, printf("JPEG error at %d\n",x2);
if (!(x)) jpeg=0 , printf("JPEG error at %d\n",x2);
// jpeg=0; \
// return next_jpeg;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ int jpegModel(int bpos,int pos,int y) {
int code, th, hufi, ns;
int nf,hmax, hv,x,r;
int s,comp,hc,coef;
int pr, tttt;
int pr, tttt,cpos_dc_ls_acomp, cpos_dc_mcusize_width;


// Be sure to quit on a byte boundary
Expand Down Expand Up @@ -195,7 +195,7 @@ int jpegModel(int bpos,int pos,int y) {
jassert(buf(1) > 0,29);
jassert(qnum>=0 && qnum<4,30);
qtab[qnum * 64 + ((dqt_state % 65) - 1)] = buf(1) - 1;
//printf("%d, ",buf(1) - 1);
//printf("%d %d, ",qnum * 64 + ((dqt_state % 65) - 1),buf(1) - 1);
}
dqt_state++;
}
Expand Down Expand Up @@ -227,6 +227,7 @@ int jpegModel(int bpos,int pos,int y) {
val=p+17; // pointer to values
code=0;
for ( j=0; j<16; ++j) {
jassert( j+hufi<128,1288);
hufmin[j+hufi]=code;
code=code+bufr(p+j+1);
hufmax[j+hufi]=code;
Expand Down Expand Up @@ -308,9 +309,9 @@ int jpegModel(int bpos,int pos,int y) {
i=huffbits-1;
jassert(i>=0 && i<16,16);
hufi=ac*64+sel*16;// [ac][sel];
//jassert(h[i].min<=h[i].max && h[i].val<pos && huffbits>0);
//jassert(hufmin[i+hufi]<=hufmax[i+hufi] && hufval[i+hufi]<pos && huffbits>0,1777);
if (huffcode<hufmax[i+hufi]) {
jassert(huffcode>=hufmin[i+hufi],17);
//jassert(huffcode>=hufmin[i+hufi],17);
rs=bufr(hufval[i+hufi]+huffcode-hufmin[i+hufi]);
huffsize=huffbits;
}
Expand All @@ -332,33 +333,38 @@ int jpegModel(int bpos,int pos,int y) {
r=rs>>4;
s=rs&15;
jassert(mcupos>>6==mcupos+r>>6,20);
mcupos=mcupos+r+1;
x=huffcode&(1<<s)-1;
if (s && !(x>>s-1)) x=x-(1<<s)-1;
mcupos=mcupos+(r+1);
x=huffcode&((1<<s)-1);
if (s && !(x>>s-1)) x=x-((1<<s)-1);
for ( i=r; i>=1; --i) {
cbuf2[cpos&0x1FFFF]=0;
cbuf[(cpos++)&0x1FFFF]=i<<4|s;}
cbuf[(cpos++)&0x1FFFF]=i<<4|s;
}
cbuf2[cpos&0x1FFFF]=x;
cbuf[(cpos++)&0x1FFFF]=s<<4|huffcode<<2>>s&3|12;
cbuf[(cpos++)&0x1FFFF]=(s<<4)|(((huffcode<<2)>>s)&3)|12;;
ssum=ssum+s;
}
}
else { // DC: rs = 0S, s<12
jassert(rs<12,21);
++mcupos;
x=huffcode&(1<<rs)-1;
if (rs && !(x>>rs-1)) x=x-(1<<rs)-1;
x=huffcode&((1<<rs)-1);
if (rs && !(x>>rs-1)) x=x-((1<<rs)-1);
jassert(mcupos>=0 && mcupos>>6<10,22);
comp=color[mcupos>>6];
jassert(comp>=0 && comp<4,23);
pred[comp]=pred[comp]+x;
dc=pred[comp];
jassert((cpos&63)==0,24);
cbuf2[cpos&0x1FFFF]=dc;
cbuf[(cpos++)&0x1FFFF]=dc+1023>>3;
ssum3=ssum2;
ssum2=ssum1;
ssum1=ssum;
cbuf[(cpos++)&0x1FFFF]=(dc+1023)>>3;
if ((mcupos>>6)==0) {
ssum1=0;
ssum2=ssum3;
} else {
if (color[(mcupos>>6)-1]==color[0]) {ssum3=ssum,ssum1=ssum1+ssum;}
ssum2=ssum1;
}
ssum=rs;
}
jassert(mcupos>=0 && mcupos<=mcusize,25);
Expand All @@ -374,47 +380,47 @@ int jpegModel(int bpos,int pos,int y) {
zz=mcupos&63, cpos_dc=cpos-zz;
if (zz==0) {
for ( i=0; i<8; ++i) sumu[i]=sumv[i]=0;
cpos_dc_ls_acomp = cpos_dc-ls[acomp];
cpos_dc_mcusize_width = cpos_dc-mcusize*width;
for ( i=0; i<64; ++i) {
sumu[zzu[i]]=sumu[zzu[i]]+(zzv[i]?256:181)*(zzv[i]&1?-1:+1)*(qtab[q+i]+1)*cbuf2[(cpos_dc+i-mcusize*width)&0x1FFFF];
sumv[zzv[i]]=sumv[zzv[i]]+(zzu[i]?256:181)*(zzu[i]&1?-1:+1)*(qtab[q+i]+1)*cbuf2[(cpos_dc+i-ls[acomp])&0x1FFFF];;
sumu[zzu[i]]=sumu[zzu[i]]+(zzv[i]&1?-1:1)*(zzv[i]?16*(16+zzv[i]):181)*(qtab[q+i]+1)*cbuf2[(cpos_dc_mcusize_width+i)&0x1FFFF];
sumv[zzv[i]]=sumv[zzv[i]]+(zzu[i]&1?-1:1)*(zzu[i]?16*(16+zzu[i]):181)*(qtab[q+i]+1)*cbuf2[(cpos_dc_ls_acomp+i)&0x1FFFF];
}
}
else {
sumu[zzu[zz-1]]=sumu[zzu[zz-1]]-(zzv[zz-1]?256:181)*(qtab[q+zz-1]+1)*cbuf2[(cpos-1)&0x1FFFF];
sumv[zzv[zz-1]]=sumv[zzv[zz-1]]-(zzu[zz-1]?256:181)*(qtab[q+zz-1]+1)*cbuf2[(cpos-1)&0x1FFFF];
sumu[zzu[zz-1]]=sumu[zzu[zz-1]]-((zzv[zz-1]?16*(16+zzv[zz-1]):181)*(qtab[q+zz-1]+1)*cbuf2[(cpos-1)&0x1FFFF]);
sumv[zzv[zz-1]]=sumv[zzv[zz-1]]-((zzu[zz-1]?16*(16+zzu[zz-1]):181)*(qtab[q+zz-1]+1)*cbuf2[(cpos-1)&0x1FFFF]);
}
j=cbuf2[(cpos_dc-ls[acomp])&0x1FFFF];
for ( i=0; i<3; ++i)
for ( st=0; st<8; ++st) {
zz2 = min(zz+st, 63);
p=(sumu[zzu[zz2]]*i+sumv[zzv[zz2]]*(2-i))/2;
p=p/((qtab[q+zz2]+1)*181);
if (zz2==0) p=p-j, p=(p<0?-1:+1)*ilog(14*abs(p)+1)/10;
else p=(p<0?-1:+1)*ilog(10*abs(p)+1)/10;
p=p/((qtab[q+zz2]+1)*181*(16+zzv[zz2])*(16+zzu[zz2])/256);
if (zz2==0) p=p-cbuf2[(cpos_dc-ls[acomp])&0x1FFFF];
p=(p<0?-1:+1)*ilog(10*abs(p)+1)/10;
if (st==0) {
adv_pred[i]=p;
adv_pred[i+4]=p/4;
}
else if (abs(p)>abs(adv_pred[i])+1) {
adv_pred[i]=adv_pred[i]+st*2+(p>0)<<6;
adv_pred[i]=adv_pred[i]+((st*2+(p>0))<<6);
if (abs(p/4)>abs(adv_pred[i+4])+1) adv_pred[i+4]=adv_pred[i+4]+((st*2+(p>0))<<6);
st=8;//break;
}
}
x=2*sumu[zzu[zz]]+2*sumv[zzv[zz]];
for ( i=0; i<8; ++i) {
if (zzu[zz]<i) x=x-sumu[i];
if (zzv[zz]<i) x=x-sumv[i];
}
for ( i=0; i<8; ++i) x=x-((zzu[zz]<i)*sumu[i]+(zzv[zz]<i)*sumv[i]);
x=x/((qtab[q+zz]+1)*181);
if (zz==0) x=x-cbuf2[(cpos_dc-ls[acomp])&0x1FFFF];
adv_pred[3]=(x<0?-1:+1)*ilog(10*abs(x)+1)/10;

for ( i=0; i<4; ++i) {
a=(i&1?zzv[zz]:zzu[zz]), b=(i&2?2:1);
if (a<b) x=255;
else {
zz2=zpos[zzu[zz]+8*zzv[zz]-(i&1?8:1)*b];
x=(qtab[q+zz2]+1)*cbuf2[(cpos_dc+zz2)&0x1FFFF]/(qtab[q+zz]+1);
x=(x<0?-1:+1)*ilog(8*abs(x)+1)/8;
x=((qtab[q+zz2]+1)*cbuf2[(cpos_dc+zz2)&0x1FFFF])/(qtab[q+zz]+1);
x=(x<0?-1:+1)*ilog(10*abs(x)+1)/10;
}
lcp[i]=x;
}
Expand Down Expand Up @@ -450,59 +456,58 @@ int jpegModel(int bpos,int pos,int y) {
coef=(mcupos&63)|(comp<<6);
zu=zzu[mcupos&63];
zv=zzv[mcupos&63];
jassert(coef>=0 && coef<256,26);
//jassert(coef>=0 && coef<256,26);
if (hbcount==0) {
i=hc*32;
cxt[0]=h3(++i, coef, adv_pred[2]);
cxt[1]=h3(++i, coef, adv_pred[0]);
cxt[2]=h3(++i, coef, adv_pred[1]);
cxt[3]=h4(++i, rs1, comp0, adv_pred[2]);
cxt[4]=h4(++i, rs1, comp0, adv_pred[0]);
cxt[5]=h4(++i, rs1, comp0, adv_pred[1]);
cxt[0]=h4(++i, coef, adv_pred[2], ssum2>>6);
cxt[1]=h4(++i, coef, adv_pred[0], ssum2>>6);
cxt[2]=h4(++i, coef, adv_pred[1], ssum2>>6);
cxt[3]=h3(++i, rs1, adv_pred[2]);
cxt[4]=h3(++i, rs1, adv_pred[0]);
cxt[5]=h3(++i, rs1, adv_pred[1]);
cxt[6]=h3(++i, adv_pred[2], adv_pred[0]);
cxt[7]=h4(++i, comp0, cbuf[(cpos-width*mcusize)&0x1FFFF], adv_pred[3]);
cxt[8]=h4(++i, comp0, cbuf[(cpos-ls[mcupos>>6])&0x1FFFF], adv_pred[3]);
cxt[7]=h3(++i, cbuf[(cpos-width*mcusize)&0x1FFFF], adv_pred[3]);
cxt[8]=h3(++i, cbuf[(cpos-ls[mcupos>>6])&0x1FFFF], adv_pred[3]);
cxt[9]=h4(++i, lcp[0], lcp[1], adv_pred[1]);
cxt[10]=h4(++i, lcp[0], lcp[1], coef);
cxt[11]=h4(++i, zu+8*comp0, lcp[0], lcp[2]/2);
cxt[12]=h4(++i, zv+8*comp0, lcp[1], lcp[3]/2);
cxt[13]=h3(++i, mcupos>>2, min(3, mcupos&63));
cxt[14]=h3(++i, coef, column>>1);
cxt[15]=h2(++i, dc);
cxt[16]=h3(++i, ssum>>4, coef);
cxt[17]=h3(++i, rs1, coef);
cxt[18]=h4(++i, mcupos>>3, ssum3>>3, adv_pred[1]);
cxt[19]=h4(++i, lcp[0]>>2, lcp[1]>>2, adv_pred[5]);
cxt[20]=h2(++i, adv_pred[1]);
cxt[21]=h4(++i, rs1, comp0, adv_pred[5]);
cxt[22]=h3(++i, comp0, adv_pred[2]);
cxt[23]=h3(i, comp0, adv_pred[0]);
cxt[24]=h3(i, comp0, adv_pred[1]);
cxt[25]=h3(++i, coef, adv_pred[4]);
cxt[26]=h3(++i, coef, adv_pred[6]);
cxt[27]=h4(++i, lcp[0]/14, lcp[1]/14, adv_pred[3]/16);
cxt[10]=h4(++i, lcp[0], lcp[1], mcupos&63);
cxt[11]=h4(++i, zu, lcp[0], lcp[2]/3);
cxt[12]=h4(++i, zv, lcp[1], lcp[3]/3);
cxt[13]=h2(++i, mcupos>>1);
cxt[14]=h3(++i, mcupos&63, column>>1);
cxt[15]=h4(++i, column>>3, lcp[0]+256*(lcp[2]/4), lcp[1]+256*(lcp[3]/4));
cxt[16]=h3(++i, ssum>>3, mcupos&63);
cxt[17]=h3(++i, rs1, mcupos&63);
cxt[18]=h4(++i, mcupos>>3, ssum2>>5, adv_pred[3]);
cxt[19]=h4(++i, lcp[0]/4, lcp[1]/4, adv_pred[5]);
cxt[20]=h3(++i, cbuf[(cpos-width*mcusize)&0x1FFFF],adv_pred[6]);
cxt[21]=h3(++i, cbuf[(cpos-ls[mcupos>>6])&0x1FFFF], adv_pred[4]);
cxt[22]=h2(++i, adv_pred[2]);
cxt[23]=h2(i, adv_pred[0]);
cxt[24]=h2(i, adv_pred[1]);
cxt[25]=h4(++i, zv, lcp[1], adv_pred[6]);
cxt[26]=h4(++i, zu, lcp[0], adv_pred[4]);
cxt[27]=h4(++i, lcp[0], lcp[1], adv_pred[3]);
cxt[28]=h4(++i, coef, adv_pred[1]/17, h3(lcp[(zu<zv)]/24,lcp[2]/20,lcp[3]/24));
cxt[29]=h4(++i, coef, adv_pred[3]/11, h3(lcp[(zu<zv)]/50,lcp[2*(zu*zv>1)]/50,lcp[3*(zu*zv>1)]/50));
}

// Predict next bit
mxa(1,128);
jassert(hbcount<=2,27);
for ( i=0; i<N; ++i) {
if (hbcount==0) cp[i]=find(cxt[i])+1;
else if (hbcount==1) cp[i]=cp[i]+1+(huffcode&1)*3;
else cp[i]=cp[i]+1+(huffcode&1);
smp(i,t[cp[i]],1023);
}
//jassert(hbcount<=2,27);
if (hbcount==0) for ( i=0; i<N; ++i) {cp[i]=find(cxt[i])+1; pr=smp(i,t[cp[i]],1023);mxa(0,(pr-2048)>>3);}
else if (hbcount==1) for ( i=0; i<N; ++i) {cp[i]=cp[i]+1+(huffcode&1)*3; pr=smp(i,t[cp[i]],1023);mxa(0,(pr-2048)>>3);}
else { for ( i=0; i<N; ++i) {cp[i]=cp[i]+1+(huffcode&1); pr=smp(i,t[cp[i]],1023);mxa(0,(pr-2048)>>3);}}

mxc(1); // mix statemap contexts into mixer 1
mxs(1,column==0, 2);
mxs(1, coef,256);
mxs(1,hc&511, 512);
pr=mxp(1);
pr=apm(3,pr,hc&255|(adv_pred[1]&63)<<8,7);
mxa(0,stretch(pr));
pr=apm(3,pr,hc&511|(adv_pred[1]==0?0:(abs(adv_pred[1])-4)&63)<<9,7);
pr=apm(4,pr,hc&255|coef<<8,7);
mxa(0,stretch(pr));
return 1;
return 2+(hc&255);
}
// update is called in VM after every bit
int update(int y,int c0,int bpos,int c4,int pos){
Expand All @@ -516,7 +521,7 @@ int update(int y,int c0,int bpos,int c4,int pos){

if (isjpeg) {
mxs(0, 1,8);
mxs(0, c0,256);
mxs(0, isjpeg-1,257);
mxs(0, buf(1),256);
return mxp(0);
}
Expand Down Expand Up @@ -559,7 +564,7 @@ void block(int a,int b) {
// main is called only once after VM init.
int main() {
int i,sint;
printf("JPEG model v5\n");
printf("JPEG model v6\n");
N=30;
sint=sizeof(int);
tsize=0x1000000;// MEM at -8
Expand Down Expand Up @@ -593,9 +598,9 @@ int main() {
vmi(APM1,0,0x1000,0,-1); //APM1(0) contexts, 0, no mixer
vmi(APM1,1,0x10000,0,-1);
vmi(APM1,2,0x10000,0,-1);
vmi(APM1,3,0x1000,0,-1);
vmi(APM1,3,0x8000,0,-1);
vmi(APM1,4,0x10000,0,-1);
vmi(MX,0,4,264+256+256+256+2048+1536+2048,6);
vmi(MX,0,4+1+N,264+256+256+256+2048+1536+2048+1,6);
vmi(MX,1,N+1,770,4);
w4=pr1=Misses=0;
prm=1024;
Expand Down
Loading

0 comments on commit 82d7f83

Please sign in to comment.