--- decoder.c 2004/03/22 22:36:23 1.51 +++ decoder.c 2004/08/29 11:36:22 1.51.2.8 @@ -20,7 +20,7 @@ * along with this program ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: decoder.c,v 1.51 2004/03/22 22:36:23 edgomez Exp $ + * $Id: decoder.c,v 1.51.2.8 2004/08/29 11:36:22 edgomez Exp $ * ****************************************************************************/ @@ -222,6 +222,7 @@ dec->time = dec->time_base = dec->last_time_base = 0; dec->low_delay = 0; dec->packed_mode = 0; + dec->time_inc_resolution = 1; /* until VOL header says otherwise */ dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); @@ -338,7 +339,7 @@ stop_coding_timer(); start_timer(); - add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors); + add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version); stop_prediction_timer(); start_timer(); @@ -459,6 +460,42 @@ stop_transfer_timer(); } +static void __inline +validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec) +{ + /* clip a vector to valid range + prevents crashes if bitstream is broken + */ + int shift = 5 + dec->quarterpel; + int xborder_high = (int)(dec->mb_width - x_pos) << shift; + int xborder_low = (-(int)x_pos-1) << shift; + int yborder_high = (int)(dec->mb_height - y_pos) << shift; + int yborder_low = (-(int)y_pos-1) << shift; + +#define CHECK_MV(mv) \ + do { \ + if ((mv).x > xborder_high) { \ + DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \ + (mv).x = xborder_high; \ + } else if ((mv).x < xborder_low) { \ + DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \ + (mv).x = xborder_low; \ + } \ + if ((mv).y > yborder_high) { \ + DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \ + (mv).y = yborder_high; \ + } else if ((mv).y < yborder_low) { \ + DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \ + (mv).y = yborder_low; \ + } \ + } while (0) + + CHECK_MV(mv[0]); + CHECK_MV(mv[1]); + CHECK_MV(mv[2]); + CHECK_MV(mv[3]); +} + /* decode an inter macroblock */ static void decoder_mbinter(DECODER * dec, @@ -496,6 +533,8 @@ mv[i] = pMB->mvs[i]; } + validate_vector(mv, x_pos, y_pos, dec); + start_timer(); if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ @@ -761,20 +800,6 @@ mv.y -= range; } - /* clip to valid range */ - - if (mv.x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) ) - mv.x = (int)(dec->mb_width - x) << (5 + dec->quarterpel); - - else if (mv.x < (int)(-x-1) << (5 + dec->quarterpel)) - mv.x = (int)(-x-1) << (5 + dec->quarterpel); - - if (mv.y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) ) - mv.y = (int)(dec->mb_height - y) << (5 + dec->quarterpel); - - else if (mv.y < ((int)(-y-1)) << (5 + dec->quarterpel) ) - mv.y = (int)(-y-1) << (5 + dec->quarterpel); - ret_mv->x = mv.x; ret_mv->y = mv.y; } @@ -986,20 +1011,6 @@ else if (mv_y > high) mv_y -= range; - - /* clip to valid range */ - if (mv_x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) ) - mv_x = (int)(dec->mb_width - x) << (5 + dec->quarterpel); - - else if (mv_x < (int)(-x-1) << (5 + dec->quarterpel)) - mv_x = (int)(-x-1) << (5 + dec->quarterpel); - - if (mv_y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) ) - mv_y = (int)(dec->mb_height - y) << (5 + dec->quarterpel); - - else if (mv_y < ((int)(-y-1)) << (5 + dec->quarterpel) ) - mv_y = (int)(-y-1) << (5 + dec->quarterpel); - mv->x = mv_x; mv->y = mv_y; } @@ -1009,7 +1020,7 @@ decoder_bf_interpolate_mbinter(DECODER * dec, IMAGE forward, IMAGE backward, - const MACROBLOCK * pMB, + MACROBLOCK * pMB, const uint32_t x_pos, const uint32_t y_pos, Bitstream * bs, @@ -1026,6 +1037,9 @@ pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); + validate_vector(pMB->mvs, x_pos, y_pos, dec); + validate_vector(pMB->b_mvs, x_pos, y_pos, dec); + if (!direct) { uv_dx = pMB->mvs[0].x; uv_dy = pMB->mvs[0].y; @@ -1140,32 +1154,32 @@ interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos, - stride, 1, 8); + stride, 0, 8); interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8, - stride, 1, 8); + stride, 0, 8); interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, - stride, 1, 8); + stride, 0, 8); interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, - stride, 1, 8); + stride, 0, 8); interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos, - stride2, 1, 8); + stride2, 0, 8); interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos, - stride2, 1, 8); + stride2, 0, 8); stop_comp_timer(); @@ -1215,7 +1229,6 @@ uint32_t x, y; VECTOR mv; const VECTOR zeromv = {0,0}; - const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; int i; start_timer(); @@ -1310,14 +1323,15 @@ case MODE_DIRECT_NONE_MV: for (i = 0; i < 4; i++) { - mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x); - mb->b_mvs[i].x = (int32_t) ((mv.x == 0) - ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD - : mb->mvs[i].x - last_mb->mvs[i].x); - mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y); - mb->b_mvs[i].y = (int32_t) ((mv.y == 0) - ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD - : mb->mvs[i].y - last_mb->mvs[i].y); + mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x; + mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y; + + mb->b_mvs[i].x = (mv.x) + ? mb->mvs[i].x - last_mb->mvs[i].x + : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp; + mb->b_mvs[i].y = (mv.y) + ? mb->mvs[i].y - last_mb->mvs[i].y + : last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp; } decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], @@ -1393,7 +1407,6 @@ } } - int decoder_decode(DECODER * dec, xvid_dec_frame_t * frame, xvid_dec_stats_t * stats) @@ -1490,6 +1503,11 @@ goto repeat; } + if(dec->frames == 0 && coding_type != I_VOP) { + /* 1st frame is not an i-vop */ + goto repeat; + } + dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; /* init pred vector to 0 */ /* packed_mode: special-N_VOP treament */ @@ -1551,7 +1569,7 @@ if (dec->low_delay) { DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n"); - dec->low_delay = 1; + dec->low_delay = 0; } if (dec->frames < 2) {