--- decoder.c 2004/02/29 12:57:58 1.49.2.34 +++ decoder.c 2004/06/04 11:54:41 1.51.2.5 @@ -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.49.2.34 2004/02/29 12:57:58 edgomez Exp $ + * $Id: decoder.c,v 1.51.2.5 2004/06/04 11:54:41 edgomez Exp $ * ****************************************************************************/ @@ -77,6 +77,8 @@ xvid_free(dec->last_mbs); if (dec->mbs) xvid_free(dec->mbs); + if (dec->qscale) + xvid_free(dec->qscale); /* realloc */ dec->mb_width = (dec->width + 15) / 16; @@ -161,6 +163,13 @@ memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); + /* nothing happens if that fails */ + dec->qscale = + xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE); + + if (dec->qscale) + memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height); + return 0; } @@ -200,9 +209,9 @@ /* image based GMC */ image_null(&dec->gmc); - dec->mbs = NULL; dec->last_mbs = NULL; + dec->qscale = NULL; init_timer(); init_postproc(&dec->postproc); @@ -213,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); @@ -228,6 +238,7 @@ { xvid_free(dec->last_mbs); xvid_free(dec->mbs); + xvid_free(dec->qscale); /* image based GMC */ image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); @@ -328,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(); @@ -486,6 +497,33 @@ mv[i] = pMB->mvs[i]; } + for (i = 0; i < 4; i++) { + /* clip to valid range */ + int border = (int)(dec->mb_width - x_pos) << (5 + dec->quarterpel); + if (mv[i].x > border) { + DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); + mv[i].x = border; + } else { + border = (-(int)x_pos-1) << (5 + dec->quarterpel); + if (mv[i].x < border) { + DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); + mv[i].x = border; + } + } + + border = (int)(dec->mb_height - y_pos) << (5 + dec->quarterpel); + if (mv[i].y > border) { + DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); + mv[i].y = border; + } else { + border = (-(int)y_pos-1) << (5 + dec->quarterpel); + if (mv[i].y < border) { + DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); + mv[i].y = border; + } + } + } + start_timer(); if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ @@ -937,7 +975,9 @@ get_b_motion_vector(Bitstream * bs, VECTOR * mv, int fcode, - const VECTOR pmv) + const VECTOR pmv, + const DECODER * const dec, + const int x, const int y) { const int scale_fac = 1 << (fcode - 1); const int high = (32 * scale_fac) - 1; @@ -1052,8 +1092,8 @@ pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); - interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, - 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); + interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8, + pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); } interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx, @@ -1175,7 +1215,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(); @@ -1266,18 +1305,19 @@ switch (mb->mode) { case MODE_DIRECT: - get_b_motion_vector(bs, &mv, 1, zeromv); + get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y); 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], @@ -1285,10 +1325,10 @@ break; case MODE_INTERPOLATE: - get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv); + get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y); dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; - get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv); + get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y); dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0]; decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], @@ -1296,14 +1336,14 @@ break; case MODE_BACKWARD: - get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv); + get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y); dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0); break; case MODE_FORWARD: - get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv); + get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y); dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); @@ -1342,6 +1382,14 @@ stats->type = coding2type(coding_type); stats->data.vop.time_base = (int)dec->time_base; stats->data.vop.time_increment = 0; /* XXX: todo */ + stats->data.vop.qscale_stride = dec->mb_width; + stats->data.vop.qscale = dec->qscale; + if (stats->data.vop.qscale != NULL && mbs != NULL) { + int i; + for (i = 0; i < dec->mb_width*dec->mb_height; i++) + stats->data.vop.qscale[i] = mbs[i].quant; + } else + stats->data.vop.qscale = NULL; } } @@ -1354,7 +1402,7 @@ Bitstream bs; uint32_t rounding; uint32_t reduced_resolution; - uint32_t quant; + uint32_t quant = 2; uint32_t fcode_forward; uint32_t fcode_backward; uint32_t intra_dc_threshold; @@ -1442,6 +1490,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 */