+ if (last_index > -1)
+ put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
+}
+
+void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
+ int motion_x, int motion_y)
+{
+ H261Context *h = (H261Context *)s;
+ int mvd, mv_diff_x, mv_diff_y, i, cbp;
+ cbp = 63; // avoid warning
+ mvd = 0;
+
+ h->current_mba++;
+ h->mtype = 0;
+
+ if (!s->mb_intra) {
+ /* compute cbp */
+ cbp = get_cbp(s, block);
+
+ /* mvd indicates if this block is motion compensated */
+ mvd = motion_x | motion_y;
+
+ if ((cbp | mvd | s->dquant) == 0) {
+ /* skip macroblock */
+ s->skip_count++;
+ h->current_mv_x = 0;
+ h->current_mv_y = 0;
+ return;
+ }
+ }
+
+ /* MB is not skipped, encode MBA */
+ put_bits(&s->pb,
+ ff_h261_mba_bits[(h->current_mba - h->previous_mba) - 1],
+ ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
+
+ /* calculate MTYPE */
+ if (!s->mb_intra) {
+ h->mtype++;
+
+ if (mvd || s->loop_filter)
+ h->mtype += 3;
+ if (s->loop_filter)
+ h->mtype += 3;
+ if (cbp || s->dquant)
+ h->mtype++;
+ assert(h->mtype > 1);
+ }
+
+ if (s->dquant)
+ h->mtype++;
+
+ put_bits(&s->pb,
+ ff_h261_mtype_bits[h->mtype],
+ ff_h261_mtype_code[h->mtype]);
+
+ h->mtype = ff_h261_mtype_map[h->mtype];
+
+ if (IS_QUANT(h->mtype)) {
+ ff_set_qscale(s, s->qscale + s->dquant);
+ put_bits(&s->pb, 5, s->qscale);
+ }
+
+ if (IS_16X16(h->mtype)) {
+ mv_diff_x = (motion_x >> 1) - h->current_mv_x;
+ mv_diff_y = (motion_y >> 1) - h->current_mv_y;
+ h->current_mv_x = (motion_x >> 1);
+ h->current_mv_y = (motion_y >> 1);
+ h261_encode_motion(h, mv_diff_x);
+ h261_encode_motion(h, mv_diff_y);
+ }
+
+ h->previous_mba = h->current_mba;
+
+ if (HAS_CBP(h->mtype)) {
+ assert(cbp > 0);
+ put_bits(&s->pb,
+ ff_h261_cbp_tab[cbp - 1][1],
+ ff_h261_cbp_tab[cbp - 1][0]);
+ }
+ for (i = 0; i < 6; i++)
+ /* encode each block */
+ h261_encode_block(h, block[i], i);
+
+ if ((h->current_mba == 11) || (h->current_mba == 22) ||
+ (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
+ h->current_mv_x = 0;
+ h->current_mv_y = 0;