+ 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
+}
+
+static int bmid = 0;
+void setdbgpic(TAG*tag, RGBA*pic, int width, int height)
+{
+ MATRIX m;
+ tag = tag->prev;
+
+ tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
+ swf_SetU16(tag, 133);
+
+ tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
+ swf_SetU16(tag, 1000+bmid);
+ swf_SetLosslessBits(tag, width, height, (void*)pic, BMF_32BIT);
+
+ tag = swf_InsertTag(tag, ST_DEFINESHAPE);
+ swf_SetU16(tag, 2000+bmid);
+ swf_ShapeSetBitmapRect(tag, 1000+bmid, width, height);
+
+ tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
+ swf_GetMatrix(0,&m);
+ m.tx = width*20;
+ swf_ObjectPlace(tag, 2000+bmid, 133, &m, 0, 0);
+
+ bmid++;
+}
+
+#define TYPE_IFRAME 0
+#define TYPE_PFRAME 1
+
+static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
+{
+ U32 i32;