+/**
+ * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
+ */
+void ff_clean_h263_qscales(MpegEncContext *s){
+ int i;
+ int8_t * const qscale_table= s->current_picture.qscale_table;
+
+ for(i=1; i<s->mb_num; i++){
+ if(qscale_table[i] - qscale_table[i-1] >2)
+ qscale_table[i]= qscale_table[i-1]+2;
+ }
+ for(i=s->mb_num-2; i>=0; i--){
+ if(qscale_table[i] - qscale_table[i+1] >2)
+ qscale_table[i]= qscale_table[i+1]+2;
+ }
+}
+
+/**
+ * modify mb_type & qscale so that encoding is acually possible in mpeg4
+ */
+void ff_clean_mpeg4_qscales(MpegEncContext *s){
+ int i;
+ int8_t * const qscale_table= s->current_picture.qscale_table;
+
+ ff_clean_h263_qscales(s);
+
+ for(i=1; i<s->mb_num; i++){
+ if(qscale_table[i] != qscale_table[i-1] && (s->mb_type[i]&MB_TYPE_INTER4V)){
+ s->mb_type[i]&= ~MB_TYPE_INTER4V;
+ s->mb_type[i]|= MB_TYPE_INTER;
+ }
+ }
+
+ if(s->pict_type== B_TYPE){
+ int odd=0;
+ /* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than
+ for the actual adaptive quantization */
+
+ for(i=0; i<s->mb_num; i++){
+ odd += qscale_table[i]&1;
+ }
+
+ if(2*odd > s->mb_num) odd=1;
+ else odd=0;
+
+ for(i=0; i<s->mb_num; i++){
+ if((qscale_table[i]&1) != odd)
+ qscale_table[i]++;
+ if(qscale_table[i] > 31)
+ qscale_table[i]= 31;
+ }
+
+ for(i=1; i<s->mb_num; i++){
+ if(qscale_table[i] != qscale_table[i-1] && (s->mb_type[i]&MB_TYPE_DIRECT)){
+ s->mb_type[i]&= ~MB_TYPE_DIRECT;
+ s->mb_type[i]|= MB_TYPE_BIDIR;
+ }
+ }
+ }
+}
+
+#endif //CONFIG_ENCODERS
+
+void ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
+ const int mb_index= s->mb_x + s->mb_y*s->mb_width;
+ int xy= s->block_index[0];
+ uint16_t time_pp= s->pp_time;
+ uint16_t time_pb= s->pb_time;
+ int i;
+
+ //FIXME avoid divides
+ switch(s->co_located_type_table[mb_index]){
+ case 0:
+ s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
+ s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
+ s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
+ : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
+ s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1]
+ : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
+ if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
+ s->mv_type= MV_TYPE_16X16;
+ else
+ s->mv_type= MV_TYPE_8X8;
+ break;
+ case CO_LOCATED_TYPE_4MV:
+ s->mv_type = MV_TYPE_8X8;
+ for(i=0; i<4; i++){
+ xy= s->block_index[i];
+ s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
+ s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
+ s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0]
+ : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
+ s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1]
+ : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
+ }
+ break;
+ case CO_LOCATED_TYPE_FIELDMV:
+ s->mv_type = MV_TYPE_FIELD;
+ for(i=0; i<2; i++){
+ if(s->top_field_first){
+ time_pp= s->pp_field_time - s->field_select_table[mb_index][i] + i;
+ time_pb= s->pb_field_time - s->field_select_table[mb_index][i] + i;
+ }else{
+ time_pp= s->pp_field_time + s->field_select_table[mb_index][i] - i;
+ time_pb= s->pb_field_time + s->field_select_table[mb_index][i] - i;
+ }
+ s->mv[0][i][0] = s->field_mv_table[mb_index][i][0]*time_pb/time_pp + mx;
+ s->mv[0][i][1] = s->field_mv_table[mb_index][i][1]*time_pb/time_pp + my;
+ s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->field_mv_table[mb_index][i][0]
+ : s->field_mv_table[mb_index][i][0]*(time_pb - time_pp)/time_pp;
+ s->mv[1][i][1] = my ? s->mv[0][i][1] - s->field_mv_table[mb_index][i][1]
+ : s->field_mv_table[mb_index][i][1]*(time_pb - time_pp)/time_pp;
+ }
+ break;
+ }
+}
+
+#ifdef CONFIG_ENCODERS