put_bits(&s->pb, 1, 1); s->misc_bits++; s->last_bits++; s->skip_count++; s->mb_skiped=1; return;
if(s->max_b_frames>0){ int i; const int offset= (s->mb_x + s->mb_y*s->linesize)*16; uint8_t *p_pic= s->new_picture[0] + offset; s->mb_skiped=1; for(i=0; i<s->max_b_frames; i++){ uint8_t *b_pic= s->coded_order[i+1].picture[0] + offset; int diff= pix_abs16x16(p_pic, b_pic, s->linesize); if(diff>s->qscale*70){ s->mb_skiped=0; break; } } }else s->mb_skiped=1;  if(s->mb_skiped==1){  put_bits(&s->pb, 1, 1); s->misc_bits++; s->last_bits++; s->skip_count++; return; }
void mpeg4_encode_mb(MpegEncContext * s,		    DCTELEM block[6][64],		    int motion_x, int motion_y){    int cbpc, cbpy, i, pred_x, pred_y;    int bits;        //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);    if (!s->mb_intra) {        /* compute cbp */        int cbp = 0;        for (i = 0; i < 6; i++) {            if (s->block_last_index[i] >= 0)                cbp |= 1 << (5 - i);        }        if(s->pict_type==B_TYPE){            static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */            int mb_type=  mb_type_table[s->mv_dir];                        if(s->mb_x==0){                s->last_mv[0][0][0]=                 s->last_mv[0][0][1]=                 s->last_mv[1][0][0]=                 s->last_mv[1][0][1]= 0;            }            /* nothing to do if this MB was skiped in the next P Frame */            if(s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]){                s->skip_count++;                s->mv[0][0][0]=                 s->mv[0][0][1]=                 s->mv[1][0][0]=                 s->mv[1][0][1]= 0;                s->mv_dir= MV_DIR_FORWARD; //doesnt matter                return;            }            if ((cbp | motion_x | motion_y | mb_type) ==0) {                /* direct MB with MV={0,0} */                put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */                s->misc_bits++;                s->last_bits++;                s->skip_count++;                return;            }            put_bits(&s->pb, 1, 0);	/* mb coded modb1=0 */            put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge            put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)            if(cbp) put_bits(&s->pb, 6, cbp);                        if(cbp && mb_type)                put_bits(&s->pb, 1, 0); /* no q-scale change */            bits= get_bit_count(&s->pb);            s->misc_bits+= bits - s->last_bits;            s->last_bits=bits;            switch(mb_type)            {            case 0: /* direct */                h263_encode_motion(s, motion_x, 1);                h263_encode_motion(s, motion_y, 1);                                break;            case 1: /* bidir */                h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);                h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);                h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);                h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);                s->last_mv[0][0][0]= s->mv[0][0][0];                s->last_mv[0][0][1]= s->mv[0][0][1];                s->last_mv[1][0][0]= s->mv[1][0][0];                s->last_mv[1][0][1]= s->mv[1][0][1];                break;            case 2: /* backward */                h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);                h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);                s->last_mv[1][0][0]= motion_x;                s->last_mv[1][0][1]= motion_y;                break;            case 3: /* forward */                h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);                h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);                s->last_mv[0][0][0]= motion_x;                s->last_mv[0][0][1]= motion_y;                break;            default:                printf("unknown mb type\n");                return;            }            bits= get_bit_count(&s->pb);            s->mv_bits+= bits - s->last_bits;            s->last_bits=bits;            /* encode each block */            for (i = 0; i < 6; i++) {                mpeg4_encode_block(s, block[i], i, 0, zigzag_direct);            }            bits= get_bit_count(&s->pb);            s->p_tex_bits+= bits - s->last_bits;            s->last_bits=bits;        }else{ /* s->pict_type==B_TYPE */            if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) {                /* skip macroblock */                put_bits(&s->pb, 1, 1);                s->misc_bits++;                s->last_bits++;                s->skip_count++;                s->mb_skiped=1; // we need that for b-frames                return;            }            put_bits(&s->pb, 1, 0);	/* mb coded */            if(s->mv_type==MV_TYPE_16X16){                cbpc = cbp & 3;                put_bits(&s->pb,                        inter_MCBPC_bits[cbpc],                        inter_MCBPC_code[cbpc]);                cbpy = cbp >> 2;                cbpy ^= 0xf;                put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);                                    bits= get_bit_count(&s->pb);                s->misc_bits+= bits - s->last_bits;                s->last_bits=bits;                /* motion vectors: 16x16 mode */                h263_pred_motion(s, 0, &pred_x, &pred_y);                            h263_encode_motion(s, motion_x - pred_x, s->f_code);                h263_encode_motion(s, motion_y - pred_y, s->f_code);            }else{                cbpc = (cbp & 3)+16;                put_bits(&s->pb,                        inter_MCBPC_bits[cbpc],                        inter_MCBPC_code[cbpc]);                cbpy = cbp >> 2;                cbpy ^= 0xf;                put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);                bits= get_bit_count(&s->pb);                s->misc_bits+= bits - s->last_bits;                s->last_bits=bits;                for(i=0; i<4; i++){                    /* motion vectors: 8x8 mode*/                    h263_pred_motion(s, i, &pred_x, &pred_y);                    h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);                    h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);                }            }            bits= get_bit_count(&s->pb);            s->mv_bits+= bits - s->last_bits;            s->last_bits=bits;            /* encode each block */            for (i = 0; i < 6; i++) {                mpeg4_encode_block(s, block[i], i, 0, zigzag_direct);            }            bits= get_bit_count(&s->pb);            s->p_tex_bits+= bits - s->last_bits;            s->last_bits=bits;            s->p_count++;        }    } else {        int cbp;        int dc_diff[6];   //dc values with the dc prediction subtracted         int dir[6];  //prediction direction        int zigzag_last_index[6];        UINT8 *scan_table[6];        for(i=0; i<6; i++){            const int level= block[i][0];            UINT16 *dc_ptr;            dc_diff[i]= level - mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);            if (i < 4) {                *dc_ptr = level * s->y_dc_scale;            } else {                *dc_ptr = level * s->c_dc_scale;            }        }        s->ac_pred= decide_ac_pred(s, block, dir);        if(s->ac_pred){            for(i=0; i<6; i++){                UINT8 *st;                int last_index;                mpeg4_inv_pred_ac(s, block[i], i, dir[i]);                if (dir[i]==0) st = ff_alternate_vertical_scan; /* left */                else           st = ff_alternate_horizontal_scan; /* top */                for(last_index=63; last_index>=0; last_index--) //FIXME optimize                    if(block[i][st[last_index]]) break;                zigzag_last_index[i]= s->block_last_index[i];                s->block_last_index[i]= last_index;                scan_table[i]= st;            }        }else{            for(i=0; i<6; i++)                scan_table[i]= zigzag_direct;        }        /* compute cbp */        cbp = 0;        for (i = 0; i < 6; i++) {            if (s->block_last_index[i] >= 1)                cbp |= 1 << (5 - i);        }        cbpc = cbp & 3;        if (s->pict_type == I_TYPE) {            put_bits(&s->pb,                intra_MCBPC_bits[cbpc],                intra_MCBPC_code[cbpc]);        } else {            put_bits(&s->pb, 1, 0);	/* mb coded */            put_bits(&s->pb,                inter_MCBPC_bits[cbpc + 4],                inter_MCBPC_code[cbpc + 4]);        }        put_bits(&s->pb, 1, s->ac_pred);        cbpy = cbp >> 2;        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);        bits= get_bit_count(&s->pb);        s->misc_bits+= bits - s->last_bits;        s->last_bits=bits;        /* encode each block */        for (i = 0; i < 6; i++) {            mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i]);        }        bits= get_bit_count(&s->pb);        s->i_tex_bits+= bits - s->last_bits;        s->last_bits=bits;        s->i_count++;        /* restore ac coeffs & last_index stuff if we messed them up with the prediction */        if(s->ac_pred){            for(i=0; i<6; i++){                int j;                    INT16 *ac_val;                ac_val = s->ac_val[0][0] + s->block_index[i] * 16;                if(dir[i]){                    for(j=1; j<8; j++)                         block[i][block_permute_op(j   )]= ac_val[j+8];                }else{                    for(j=1; j<8; j++)                         block[i][block_permute_op(j<<3)]= ac_val[j  ];                }                s->block_last_index[i]= zigzag_last_index[i];            }        }    }}