some functions converted to oo-style, block difference checking,
[swftools.git] / lib / h.263 / mkvideo.c
index 814ed3f..fda9044 100644 (file)
@@ -17,7 +17,9 @@ typedef struct _VIDEOSTREAM
 {
     int width;
     int height;
+    int frame;
     RGBA*oldpic;
+    RGBA*current;
 } VIDEOSTREAM;
 
 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
@@ -29,9 +31,12 @@ void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width
     swf_SetU8(tag, 1); /* smoothing on */
     swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */
 
-    stream->width = width;
-    stream->height = height;
-    stream->oldpic = 0;
+    memset(stream, 0, sizeof(VIDEOSTREAM));
+    stream->width = width&~15;
+    stream->height = height&~15;
+    stream->oldpic = (RGBA*)malloc(width*height*sizeof(RGBA));
+
+    memset(stream->oldpic, 0, width*height*sizeof(RGBA));
 }
 
 typedef struct _block_t
@@ -54,7 +59,7 @@ typedef struct _fblock_t
     double v[64];
 } fblock_t;
 
-void fzigzag(double*src) 
+static void fzigzag(double*src) 
 {
     int table[64] = {
        0, 1, 5, 6, 14, 15, 27, 28, 
@@ -78,7 +83,7 @@ void fzigzag(double*src)
 #define SQRT2 1.414214
 #define RSQRT2 (1.0/1.414214)
 
-double table[8][8] =
+static double table[8][8] =
 {
 {0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548},
 {0.980785280403230,0.831469612302545,0.555570233019602,0.195090322016128,-0.195090322016128,-0.555570233019602,-0.831469612302545,-0.980785280403230},
@@ -90,7 +95,7 @@ double table[8][8] =
 {0.195090322016128,-0.555570233019602,0.831469612302545,-0.980785280403231,0.980785280403230,-0.831469612302545,0.555570233019602,-0.195090322016129}
 };
 
-void dct(double*src)
+static void dct(double*src)
 {
     double tmp[64];
     int x,y,u,v,t;
@@ -117,7 +122,7 @@ void dct(double*src)
     }
 }
 
-void idct(double*src)
+static void idct(double*src)
 {
     double tmp[64];
     int x,y,u,v;
@@ -143,7 +148,7 @@ void idct(double*src)
     }
 }
 
-void getregion(fblock_t* bb, RGBA*pic, int bx, int by, int width, int height)
+static void getregion(fblock_t* bb, RGBA*pic, int bx, int by, int width, int height)
 {
     RGBA*p1 = &pic[by*width*16+bx*16];
     RGBA*p2 = p1;
@@ -174,7 +179,29 @@ void getregion(fblock_t* bb, RGBA*pic, int bx, int by, int width, int height)
     }
 }
 
-int valtodc(int val)
+int compareregions(VIDEOSTREAM*s, int bx, int by)
+{
+    int linex = s->width;
+    RGBA*p1 = &s->current[by*linex*16+bx*16];
+    RGBA*p2 = &s->oldpic[by*linex*16+bx*16];
+    int diff = 0;
+    int x,y;
+    for(y=0;y<16;y++) {
+       for(x=0;x<16;x++) {
+           RGBA*m = &p1[x];
+           RGBA*n = &p2[x];
+           int r = m->r - n->r;
+           int g = m->g - n->g;
+           int b = m->b - n->b;
+           diff += r*r+g*g+b*b;
+       }
+       p1+=linex;
+       p2+=linex;
+    }
+    return diff;
+}
+
+static int valtodc(int val)
 {
     assert(val>=0);
 
@@ -192,7 +219,7 @@ int valtodc(int val)
     return val;
 }
 
-void codehuffman(TAG*tag, struct huffcode*table, int index)
+static void codehuffman(TAG*tag, struct huffcode*table, int index)
 {
     /* TODO: !optimize! */
     int i=0;
@@ -205,7 +232,7 @@ void codehuffman(TAG*tag, struct huffcode*table, int index)
     }
 }
 
-void quantize8x8(double*src, int*dest, int has_dc, int quant)
+static void quantize8x8(double*src, int*dest, int has_dc, int quant)
 {
     int t,pos=0;
     if(has_dc) {
@@ -226,7 +253,7 @@ void quantize8x8(double*src, int*dest, int has_dc, int quant)
     }
 }
 
-int hascoef(int*b, int has_dc)
+static int hascoef(int*b, int has_dc)
 {
     int t;
     int pos=0;
@@ -240,7 +267,7 @@ int hascoef(int*b, int has_dc)
     return 0;
 }
 
-void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
+static void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
 {
     int t;
     int pos=0;
@@ -259,9 +286,10 @@ void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
            if(bb[last])
                break;
        }
-       assert(bb[last]);
        /* blocks without coefficients should not be included
-          in the cbpy/cbpc patterns */
+          in the cbpy/cbpc patterns: */
+       assert(bb[last]);
+
        while(1) {
            int run=0;
            int level=0;
@@ -312,7 +340,7 @@ void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
     }
 }
 
-void dodct(fblock_t*fb)
+static void dodct(fblock_t*fb)
 {
     dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4); 
     dct(fb->u);  dct(fb->v);  
@@ -324,7 +352,7 @@ void dodct(fblock_t*fb)
     fzigzag(fb->v); 
 }
 
-void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
+static void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
 {
     quantize8x8(fb->y1,b->y1,has_dc,quant); 
     quantize8x8(fb->y2,b->y2,has_dc,quant); 
@@ -334,7 +362,7 @@ void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
     quantize8x8(fb->v,b->v,has_dc,quant);   
 }
 
-void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
+static void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
 {
     *cbpybits = 0;
     *cbpcbits = 0;
@@ -348,7 +376,7 @@ void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
     *cbpcbits|=hascoef(b->v, has_dc)*1;
 }
 
-void setQuant(TAG*tag, int dquant)
+static void setQuant(TAG*tag, int dquant)
 {
     int code = 0;
     /* 00 01 10 11
@@ -367,26 +395,38 @@ void setQuant(TAG*tag, int dquant)
     }
 }
 
-void change_quant(int quant, int*dquant)
+static void change_quant(int quant, int*dquant)
 {
     /* TODO */
     *dquant = 0;
 }
 
-void encode_blockI(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
+void decode_blockI(VIDEOSTREAM*s, block_t*b, int bx, int by)
+{
+    int x,y;
+    /* FIXME */
+    for(x=0;x<16;x++)
+    for(y=0;y<16;y++) {
+       s->oldpic[(y+by*16)*s->width+(x+bx*16)] = s->current[(y+by*16)*s->width+(x+bx*16)];
+    }
+}
+
+void encode_blockI(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
 {
     fblock_t fb;
     block_t b;
     int dquant=0;
     int cbpcbits = 0, cbpybits=0;
 
-    getregion(&fb, pic, bx, by, width, height);
+    getregion(&fb, s->current, bx, by, s->width, s->height);
     dodct(&fb);
     
     change_quant(*quant, &dquant);
     *quant+=dquant;
-
     quantize(&fb, &b, 1, *quant);
+
+    decode_blockI(s, &b, bx, by);
+
     getblockpatterns(&b, &cbpybits, &cbpcbits, 1);
 
     if(dquant) {
@@ -412,7 +452,7 @@ void encode_blockI(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int
     encode8x8(tag, b.v, 1, cbpcbits&1);
 }
 
-void encode_blockP(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
+void encode_blockP(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
 {
     fblock_t fb;
     block_t b;
@@ -423,7 +463,16 @@ void encode_blockP(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int
     int mode = 0;
     int cbpcbits = 0, cbpybits=0;
 
-    getregion(&fb, pic, bx, by, width, height);
+    {
+       int diff;
+       diff = compareregions(s, bx, by);
+       if(diff < 64/*TODO: should be a parameter*/) {
+           swf_SetBits(tag, 1,1); /* cod=1, block skipped */
+           return;
+       }
+    }
+
+    getregion(&fb, s->current, bx, by, s->width, s->height);
     dodct(&fb);
 
     change_quant(*quant, &dquant);
@@ -509,48 +558,48 @@ static void writeHeader(TAG*tag, int width, int height, int frame, int quant, in
     swf_SetBits(tag, 0, 1); /* No extra info */
 }
 
-void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
+void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic)
 {
     int bx, by, bbx, bby;
     int quant = 7;
 
-    /* TODO: width not divisible by 16 will get us in trouble */
-    width=width&~15; height=height&~15;
+    writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
 
-    writeHeader(tag, width, height, frame, quant, TYPE_IFRAME);
+    bbx = (s->width+15)/16;
+    bby = (s->height+15)/16;
 
-    bbx = (width+15)/16;
-    bby = (height+15)/16;
+    s->current = pic;
 
     for(by=0;by<bby;by++)
     {
        for(bx=0;bx<bbx;bx++)
        {
-           encode_blockI(tag, pic, bx, by, width, height, &quant);
+           encode_blockI(tag, s, bx, by, &quant);
        }
     }
+    s->frame++;
 }
 
-void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
+void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic)
 {
     int bx, by, bbx, bby;
     int quant = 7;
 
-    /* TODO: width not divisible by 16 will get us in trouble */
-    width=width&~15; height=height&~15;
+    writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
 
-    writeHeader(tag, width, height, frame, quant, TYPE_PFRAME);
+    bbx = (s->width+15)/16;
+    bby = (s->height+15)/16;
 
-    bbx = (width+15)/16;
-    bby = (height+15)/16;
+    s->current = pic;
 
     for(by=0;by<bby;by++)
     {
        for(bx=0;bx<bbx;bx++)
        {
-           encode_blockP(tag, pic, bx, by, width, height, &quant);
+           encode_blockP(tag, s, bx, by, &quant);
        }
     }
+    s->frame++;
 }
 
 int main(int argn, char*argv[])
@@ -559,19 +608,21 @@ int main(int argn, char*argv[])
     int t;
     SWF swf;
     TAG * tag;
-    RGBA* pic, rgb;
+    RGBA* pic, *pic2, rgb;
     SWFPLACEOBJECT obj;
     int width = 0;
     int height = 0;
-    int frames = 2;
+    int frames = 100;
     unsigned char*data;
     char* fname = "/home/kramm/pics/peppers.png";
     VIDEOSTREAM stream;
+    double d = 1.0;
 
     memset(&stream, 0, sizeof(stream));
 
     getPNG(fname, &width, &height, &data);
     pic = (RGBA*)malloc(width*height*sizeof(RGBA));
+    pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
     memcpy(pic, data, width*height*sizeof(RGBA));
     free(data);
 
@@ -596,12 +647,22 @@ int main(int argn, char*argv[])
     
     for(t=0;t<frames;t++)
     {
+       int x,y;
+       double xx,yy;
+       for(y=0,yy=0;y<height;y++,yy+=d)  {
+           RGBA*line = &pic[((int)yy)*width];
+           for(x=0,xx=0;x<width;x++,xx+=d) {
+               pic2[y*width+x] = line[((int)xx)];
+           }
+       }
+       printf("frame:%d\n", t);fflush(stdout);
+
        tag = swf_InsertTag(tag, ST_VIDEOFRAME);
        swf_SetU16(tag, 33);
        if(t==0)
-           swf_SetVideoStreamIFrame(tag, &stream, pic, width, height, t);
+           swf_SetVideoStreamIFrame(tag, &stream, pic2);
        else
-           swf_SetVideoStreamPFrame(tag, &stream, pic, width, height, t);
+           swf_SetVideoStreamPFrame(tag, &stream, pic2);
 
        tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
        swf_GetPlaceObject(0, &obj);
@@ -616,6 +677,7 @@ int main(int argn, char*argv[])
        swf_SetPlaceObject(tag,&obj);
 
        tag = swf_InsertTag(tag, ST_SHOWFRAME);
+       d-=0.005;
     }
    
     tag = swf_InsertTag(tag, ST_END);