--- decoder.c 2004/01/24 14:14:15 1.49.2.29 +++ decoder.c 2004/04/15 12:05:19 1.55 @@ -4,7 +4,7 @@ * - Decoder Module - * * Copyright(C) 2002 MinChen - * 2002-2003 Peter Ross + * 2002-2004 Peter Ross * * This program is free software ; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -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.29 2004/01/24 14:14:15 syskin Exp $ + * $Id: decoder.c,v 1.55 2004/04/15 12:05:19 suxen_drol 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); @@ -228,6 +237,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); @@ -486,6 +496,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 */ @@ -779,7 +816,7 @@ start_timer(); image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, - dec->width, dec->height); + dec->width, dec->height, dec->bs_version); stop_edges_timer(); if (gmc_warp) { @@ -851,12 +888,12 @@ mb->quant = quant; if (dec->interlacing) { - if ((cbp || intra) && !mcsel) { + if (cbp || intra) { mb->field_dct = BitstreamGetBit(bs); DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); } - if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { + if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) { mb->field_pred = BitstreamGetBit(bs); DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); @@ -937,7 +974,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 +1091,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, @@ -1180,9 +1219,9 @@ start_timer(); image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, - dec->width, dec->height); + dec->width, dec->height, dec->bs_version); image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, - dec->width, dec->height); + dec->width, dec->height, dec->bs_version); stop_edges_timer(); for (y = 0; y < dec->mb_height; y++) { @@ -1266,7 +1305,7 @@ 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++) { @@ -1285,10 +1324,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 +1335,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); @@ -1321,16 +1360,19 @@ xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type, int quant) { + const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0; + if (dec->cartoon_mode) frame->general &= ~XVID_FILMEFFECT; - if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) && mbs != NULL) /* post process */ + if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0) + && mbs != NULL) /* post process */ { /* note: image is stored to tmp */ image_copy(&dec->tmp, img, dec->edged_width, dec->height); image_postproc(&dec->postproc, &dec->tmp, dec->edged_width, mbs, dec->mb_width, dec->mb_height, dec->mb_width, - frame->general, dec->frames, (coding_type == B_VOP)); + frame->general, brightness, dec->frames, (coding_type == B_VOP)); img = &dec->tmp; } @@ -1342,6 +1384,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 +1404,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; @@ -1510,13 +1560,13 @@ /* attemping to decode a bvop without atleast 2 reference frames */ image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, "broken b-frame, mising ref frames"); - stats->type = XVID_TYPE_NOTHING; + if (stats) stats->type = XVID_TYPE_NOTHING; } else if (dec->time_pp <= dec->time_bp) { /* this occurs when dx50_bvop_compatibility==0 sequences are decoded in vfw. */ image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp); - stats->type = XVID_TYPE_NOTHING; + if (stats) stats->type = XVID_TYPE_NOTHING; } else { decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); @@ -1526,7 +1576,9 @@ dec->frames++; } - BitstreamByteAlign(&bs); +#if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */ + BitstreamByteAlign(&bs); +#endif /* low_delay_default mode: repeat in packed_mode */ if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) { @@ -1557,5 +1609,5 @@ emms(); stop_global_timer(); - return BitstreamPos(&bs) / 8; /* number of bytes consumed */ + return (BitstreamPos(&bs) + 7) / 8; /* number of bytes consumed */ }