+static void predictmvd(VIDEOSTREAM*s, int bx, int by, int*px, int*py)
+{
+ int i1,i2;
+ int x1,y1,x2,y2,x3,y3;
+ int x4,y4,p;
+ if(bx) {x1=s->mvdx[by*s->bbx+bx-1];
+ y1=s->mvdy[by*s->bbx+bx-1];
+ } else {x1=y1=0;}
+
+ if(by) {x2=s->mvdx[(by-1)*s->bbx+bx];
+ y2=s->mvdy[(by-1)*s->bbx+bx];
+ if(bx<s->bbx-1) {
+ x3=s->mvdx[(by-1)*s->bbx+bx+1];
+ y3=s->mvdy[(by-1)*s->bbx+bx+1];
+ } else {
+ x3=y3=0;
+ }
+ }
+ else {x2=x3=x1;y2=y3=y1;}
+
+ if((x1 <= x2 && x2 <= x3) ||
+ (x3 <= x2 && x2 <= x1)) {
+ x4=x2;
+ } else if((x2 <= x1 && x1 <= x3) ||
+ (x3 <= x1 && x1 <= x2)) {
+ x4=x1;
+ } else if((x1 <= x3 && x3 <= x2) ||
+ (x2 <= x3 && x3 <= x1)) {
+ x4=x3;
+ }
+
+ if((y1 <= y2 && y2 <= y3) ||
+ (y3 <= y2 && y2 <= y1)) {
+ y4=y2;
+ } else if((y2 <= y1 && y1 <= y3) ||
+ (y3 <= y1 && y1 <= y2)) {
+ y4=y1;
+ } else if((y1 <= y3 && y3 <= y2) ||
+ (y2 <= y3 && y3 <= y1)) {
+ y4=y3;
+ }
+
+ *px = x4;
+ *py = y4;
+ assert((x4>=-32 && x4<=31) && (y4>=-32 && y4<=31));
+}
+
+static inline int mvd2index(int px, int py, int x, int y, int xy)
+{
+ assert((x>=-32 && x<=31) && (y>=-32 && y<=31));
+ assert((x&1)==0 && (y&1)==0);//for now
+ assert((x&2)==0 && (y&2)==0);//for now(2)
+
+ x-=px;
+ y-=py;
+
+ if(xy)
+ x=y;
+ x+=32;
+
+ /* (x&63) */
+ if(x>63)
+ x-=64;
+ if(x<0)
+ x+=64;
+
+ assert(x>=0 && x<64);
+ return x;
+}
+
+static int encode_blockP(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
+{
+ fblock_t fb;
+ block_t b;
+ int dquant=0;
+ int has_mvd=0;
+ int has_mvd24=0;
+ int has_dc=1;
+ int mode = 0;
+ int cbpcbits = 0, cbpybits=0;
+ int diff;
+ int predictmvdx;
+ int predictmvdy;
+
+ block_t b_i;
+ int bits_i;
+
+ fblock_t fbold_v00;
+ block_t b_v00;
+ int bits_v00 = 65535;
+ int x_v00=0;
+ int y_v00=0;
+
+ diff = compareregions(s, bx, by);
+ if(diff < 20 /*TODO: should be a parameter- good values are between 32 and 48 */) {
+ swf_SetBits(tag, 1,1); /* cod=1, block skipped */
+ /* copy the region from the last frame so that we have a complete reconstruction */
+ copyregion(s, s->current, s->oldpic, bx, by);
+ return 1;
+ }
+
+ predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
+ getregion(&fb, s->current, bx*16, by*16, s->width);
+
+ { /* consider I-block */
+ fblock_t fb_i;
+ int y,c;
+ memcpy(&fb_i, &fb, sizeof(fblock_t));
+ dodctandquant(&fb_i, &b_i, 1, *quant);
+ //quantize(&fb_i, &b_i, 1, *quant);
+ getblockpatterns(&b_i, &y, &c, 1);
+ bits_i = 1; //cod
+ bits_i += mcbpc_inter[3*4+c].len;
+ bits_i += cbpy[y].len;
+ bits_i += coefbits8x8(b_i.y1, 1);
+ bits_i += coefbits8x8(b_i.y2, 1);
+ bits_i += coefbits8x8(b_i.y3, 1);
+ bits_i += coefbits8x8(b_i.y4, 1);
+ bits_i += coefbits8x8(b_i.u, 1);
+ bits_i += coefbits8x8(b_i.v, 1);
+ }
+
+ { /* consider mvd(x,y)-block */
+ fblock_t fbdiff;
+ int y,c;
+
+ x_v00=0;
+ y_v00=0;
+
+ if(s->do_motion) {
+ int hx,hy;
+ int bestx=0,besty=0,bestbits=65536;
+ int startx=-8,endx=8;
+ int starty=-8,endy=8;
+
+ if(!bx) startx=0;
+ if(!by) starty=0;
+ if(bx==s->bbx-1) endx=0;
+ if(by==s->bby-1) endy=0;
+
+ for(hx=startx;hx<=endx;hx+=4)
+ for(hy=starty;hy<=endy;hy+=4)
+ {
+ block_t b;
+ fblock_t fbold;
+ int bits = 0;
+ memcpy(&fbdiff, &fb, sizeof(fblock_t));
+ getregion(&fbold, s->oldpic, bx*16+hx/2, by*16+hy/2, s->linex);
+ yuvdiff(&fbdiff, &fbold);
+ dodctandquant(&fbdiff, &b, 0, *quant);
+ //quantize(&fbdiff, &b, 0, *quant);
+ bits += coefbits8x8(b.y1, 0);
+ bits += coefbits8x8(b.y2, 0);
+ bits += coefbits8x8(b.y3, 0);
+ bits += coefbits8x8(b.y4, 0);
+ bits += coefbits8x8(b.u, 0);
+ bits += coefbits8x8(b.v, 0);
+ if(bits<bestbits) {
+ bestbits = bits;
+ bestx = hx;
+ besty = hy;
+ }
+ }
+ x_v00 = bestx;
+ y_v00 = besty;
+ }
+
+ memcpy(&fbdiff, &fb, sizeof(fblock_t));
+ getregion(&fbold_v00, s->oldpic, bx*16+x_v00/2, by*16+y_v00/2, s->linex);
+ yuvdiff(&fbdiff, &fbold_v00);
+ dodctandquant(&fbdiff, &b_v00, 0, *quant);
+ //quantize(&fbdiff, &b_v00, 0, *quant);
+ getblockpatterns(&b_v00, &y, &c, 0);
+
+ bits_v00 = 1; //cod
+ bits_v00 += mcbpc_inter[0*4+c].len;
+ bits_v00 += cbpy[y^15].len;
+ bits_v00 += mvd[mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 0)].len; // (0,0)
+ bits_v00 += mvd[mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 1)].len;
+ bits_v00 += coefbits8x8(b_v00.y1, 0);
+ bits_v00 += coefbits8x8(b_v00.y2, 0);
+ bits_v00 += coefbits8x8(b_v00.y3, 0);
+ bits_v00 += coefbits8x8(b_v00.y4, 0);
+ bits_v00 += coefbits8x8(b_v00.u, 0);
+ bits_v00 += coefbits8x8(b_v00.v, 0);
+ }
+
+ if(bits_i > bits_v00)
+ {
+ /* mvd (0,0) block (mode=0) */
+ int t;
+ mode = 0; // mvd w/o mvd24
+ has_dc = 0;
+ memcpy(&b, &b_v00, sizeof(block_t));
+
+ getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
+ swf_SetBits(tag,0,1); // COD
+ codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
+ codehuffman(tag, cbpy, cbpybits^15);
+
+ /* vector */
+ codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 0));
+ codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 1));
+ s->mvdx[by*s->bbx+bx] = x_v00;
+ s->mvdy[by*s->bbx+bx] = y_v00;
+
+ /* luminance */
+ encode8x8(tag, b.y1, has_dc, cbpybits&8);
+ encode8x8(tag, b.y2, has_dc, cbpybits&4);
+ encode8x8(tag, b.y3, has_dc, cbpybits&2);
+ encode8x8(tag, b.y4, has_dc, cbpybits&1);
+
+ /* chrominance */
+ encode8x8(tag, b.u, has_dc, cbpcbits&2);
+ encode8x8(tag, b.v, has_dc, cbpcbits&1);
+
+ /* -- reconstruction -- */
+ dequantize(&b, 0, *quant);
+ doidct(&b);
+ for(t=0;t<64;t++) {
+ b.y1[t] = truncate256(b.y1[t] + (int)fbold_v00.y1[t]);
+ b.y2[t] = truncate256(b.y2[t] + (int)fbold_v00.y2[t]);
+ b.y3[t] = truncate256(b.y3[t] + (int)fbold_v00.y3[t]);
+ b.y4[t] = truncate256(b.y4[t] + (int)fbold_v00.y4[t]);
+ b.u[t] = truncate256(b.u[t] + (int)fbold_v00.u[t]);
+ b.v[t] = truncate256(b.v[t] + (int)fbold_v00.v[t]);
+ }
+ copyblock(s, s->current, &b, bx, by);
+ return bits_v00;
+ } else {
+ /* i block (mode=3) */
+ mode = 3;
+ has_dc = 1;
+ memcpy(&b, &b_i, sizeof(block_t));
+ getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
+ swf_SetBits(tag,0,1); // COD
+ codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
+ codehuffman(tag, cbpy, cbpybits);
+
+ /* luminance */
+ encode8x8(tag, b.y1, has_dc, cbpybits&8);
+ encode8x8(tag, b.y2, has_dc, cbpybits&4);
+ encode8x8(tag, b.y3, has_dc, cbpybits&2);
+ encode8x8(tag, b.y4, has_dc, cbpybits&1);
+
+ /* chrominance */
+ encode8x8(tag, b.u, has_dc, cbpcbits&2);
+ encode8x8(tag, b.v, has_dc, cbpcbits&1);
+
+ /* -- reconstruction -- */
+ dequantize(&b, 1, *quant);
+ doidct(&b);
+ truncateblock(&b);
+ copyblock(s, s->current, &b, bx, by);
+ return bits_i;
+ }
+
+ exit(1);
+#if 0
+ dodct(&fb);
+ quantize(&fb, &b, has_dc, *quant);
+ getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
+
+ if(!dquant && has_mvd && !has_mvd24 && !has_dc) mode = 0;
+ else if(dquant && has_mvd && !has_mvd24 && !has_dc) mode = 1;
+ else if(!dquant && has_mvd && has_mvd24 && !has_dc) mode = 2;
+ else if(!dquant && !has_mvd && !has_mvd24 && has_dc) mode = 3;
+ else if(dquant && !has_mvd && !has_mvd24 && has_dc) mode = 4;
+ else exit(1);
+
+ swf_SetBits(tag,0,1); /* cod - 1 if we're not going to code this block*/
+
+ codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
+ codehuffman(tag, cbpy, (mode==3 || mode==4)?cbpybits:cbpybits^15);
+
+ if(dquant) {
+ setQuant(tag, dquant);
+ }
+
+ if(has_mvd) {
+ /* 0,0 */
+ codehuffman(tag, mvd, 32);
+ codehuffman(tag, mvd, 32);
+ }
+ if(has_mvd24) {
+ }
+
+ /* luminance */
+ encode8x8(tag, b.y1, has_dc, cbpybits&8);
+ encode8x8(tag, b.y2, has_dc, cbpybits&4);
+ encode8x8(tag, b.y3, has_dc, cbpybits&2);
+ encode8x8(tag, b.y4, has_dc, cbpybits&1);
+
+ /* chrominance */
+ encode8x8(tag, b.u, has_dc, cbpcbits&2);
+ encode8x8(tag, b.v, has_dc, cbpcbits&1);
+#endif
+}
+
+#define TYPE_IFRAME 0
+#define TYPE_PFRAME 1