+int stat_qdiff(double*b1, double*b2)
+{
+ int x;
+ double diff=0;
+ for(x=0;x<64;x++) {
+ double y1 = b1[x] - b2[x];
+ diff += y1*y1;
+ }
+ return (int)(diff/64);
+}
+
+int stat_absdiff(double*b1, double*b2)
+{
+ int x;
+ double diff=0;
+ for(x=0;x<64;x++) {
+ double y1 = b1[x] - b2[x];
+ diff += fabs(y1);
+ }
+ return (int)(diff/64);
+}
+
+int stat_absfreq(double*b1, double*b2)
+{
+ int x;
+ double diff=0;
+ double d1[64],d2[64];
+ memcpy(&d1, b1, 64*sizeof(double));
+ dct(d1);
+ memcpy(&d2, b2, 64*sizeof(double));
+ dct(d2);
+ for(x=0;x<64;x++) {
+ double y1 = d1[x] - d2[x];
+ diff += fabs(y1);
+ }
+ return (int)(diff/64);
+}
+
+int stat_qfreq(double*b1, double*b2)
+{
+ int x;
+ double diff=0;
+ double d1[64],d2[64];
+ memcpy(&d1, b1, 64*sizeof(double));
+ dct(d1);
+ memcpy(&d2, b2, 64*sizeof(double));
+ dct(d2);
+ for(x=0;x<64;x++) {
+ double y1 = d1[x] - d2[x];
+ diff += y1*y1;
+ }
+ return (int)(diff/64);
+}
+
+int stat_nonnull(double*b1, double*b2)
+{
+ int x;
+ int diff=0;
+ double d1[64],d2[64];
+ memcpy(&d1, b1, 64*sizeof(double));
+ dct(d1);
+ memcpy(&d2, b2, 64*sizeof(double));
+ dct(d2);
+ for(x=0;x<64;x++) {
+ int y1 = (int)((d1[x] - d2[x])/9);
+ if(y1)
+ diff++;
+ }
+ return diff;
+}
+
+void stat_filter(FILE*fi, double*d1, double*d2)
+{
+ int x,y,xx,yy,b;
+ for(b=3;b>=0;b--) {
+ int d = 1<<b;
+ double diff=0;
+ for(x=0;x<8;x+=d)
+ for(y=0;y<8;y+=d)
+ {
+ double add1=0,add2=0;
+ for(xx=x;xx<x+d;xx++)
+ for(yy=y;yy<y+d;yy++)
+ {
+ add1 += d1[yy*8+xx];
+ add2 += d2[yy*8+xx];
+ }
+ diff += fabs(add1-add2);
+ }
+ fprintf(fi, "\t%d",(int)(diff/64));
+ }
+}
+
+void qstat_filter(FILE*fi, double*d1, double*d2)
+{
+ int x,y,xx,yy,b;
+ for(b=3;b>=0;b--) {
+ int d = 1<<b;
+ double diff=0;
+ for(x=0;x<8;x+=d)
+ for(y=0;y<8;y+=d)
+ {
+ double add1=0,add2=0;
+ for(xx=x;xx<x+d;xx++)
+ for(yy=y;yy<y+d;yy++)
+ {
+ add1 += d1[yy*8+xx];
+ add2 += d2[yy*8+xx];
+ }
+ diff += (add1-add2)*(add1-add2);
+ }
+ fprintf(fi, "\t%d",(int)(diff/64));
+ }
+}
+
+void qqstat_filter(FILE*fi, double*d1, double*d2)
+{
+ int x,y,xx,yy,b;
+ for(b=3;b>=0;b--) {
+ int d = 1<<b;
+ double diff=0;
+ for(x=0;x<8;x+=d)
+ for(y=0;y<8;y+=d)
+ {
+ double add1=0,add2=0;
+ for(xx=x;xx<x+d;xx++)
+ for(yy=y;yy<y+d;yy++)
+ {
+ add1 += d1[yy*8+xx]*d1[yy*8+xx];
+ add2 += d2[yy*8+xx]*d2[yy*8+xx];
+ }
+ diff += fabs(add1-add2);
+ }
+ fprintf(fi, "\t%d",(int)(diff/64));
+ }
+}
+
+void stat(FILE*fi, int*vals, double*yold, double*ynew)
+{
+ int t;
+ int bits = coefbits8x8(vals, 0);
+ fprintf(fi, "%d\t%d\t%d\t%d\t%d\t%d", bits,
+ stat_nonnull(ynew, yold),
+ stat_qdiff(ynew,yold),
+ stat_absdiff(ynew,yold),
+ stat_absfreq(ynew,yold),
+ stat_qfreq(ynew,yold));
+ stat_filter(fi, ynew, yold);
+ qqstat_filter(fi, ynew, yold);
+ fprintf(fi, "\n");
+}
+
+void dostat(VIDEOSTREAM*s)
+{
+ int bx,by,bx2,by2;
+ int quant = 9;
+ int num = 0;
+ FILE*fi = fopen("mvd.dat", "wb");
+ fprintf(fi, "bits\tnonnull\tqdiff\tabsdiff\tabsfreq\tqfreq\tf1\tf2\tf4\tf8\tqf1\tqf2\tqf4\tqf8\n");
+ for(by=0;by<s->bby;by++)
+ for(bx=0;bx<s->bbx;bx++)
+ {
+ for(by2=0;by2<s->bby;by2++)
+ for(bx2=0;bx2<bx;bx2++)
+ {
+ fblock_t fbnew,fbdiff,fbold;
+ block_t b;
+ int t, y,c,bits;
+ getregion(&fbnew, s->current, bx*16, by*16, s->linex);
+ memcpy(&fbdiff, &fbnew, sizeof(fblock_t));
+ getregion(&fbold, s->current, bx2*16, by2*16, s->linex);
+ yuvdiff(&fbdiff, &fbold);
+ dodctandquant(&fbdiff, &b, 0, quant);
+ //quantize(&fbdiff, &b, 0, quant);
+
+ stat(fi, b.y1, fbnew.y1, fbold.y1);
+ stat(fi, b.y2, fbnew.y2, fbold.y2);
+ stat(fi, b.y3, fbnew.y3, fbold.y3);
+ stat(fi, b.y4, fbnew.y4, fbold.y4);
+ stat(fi, b.u, fbnew.u, fbold.u);
+ stat(fi, b.v, fbnew.v, fbold.v);
+
+ num++;
+ if(num==1000) {
+ fclose(fi);
+ exit(7);
+ }
+ }
+ printf("%d\n", num);fflush(stdout);
+ }
+ fclose(fi);
+ exit(7);
+}
+