--- decoder.c 2002/06/28 15:14:40 1.22 +++ decoder.c 2002/07/15 23:50:31 1.30 @@ -32,9 +32,14 @@ * * History: * + * 15.07.2002 fix a bug in B-frame decode at DIRECT mode + * MinChen + * 10.07.2002 added BFRAMES_DEC_DEBUG support + * Fix a little bug for low_delay flage + * MinChen * 28.06.2002 added basic resync support to iframe/pframe_decode() - * 22.06.2002 added primative N_VOP support - * #define BFRAMES_DEC now enables Minchenm's bframe decoder + * 22.06.2002 added primative N_VOP support + * #define BFRAMES_DEC now enables Minchen's bframe decoder * 08.05.2002 add low_delay support for B_VOP decode * MinChen * 05.05.2002 fix some B-frame decode problem @@ -50,13 +55,17 @@ * 22.12.2001 lock based interpolation * 01.12.2001 inital version; (c)2001 peter ross * - * $Id: decoder.c,v 1.22 2002/06/28 15:14:40 suxen_drol Exp $ + * $Id: decoder.c,v 1.30 2002/07/15 23:50:31 chenm001 Exp $ * *************************************************************************/ #include #include +#ifdef BFRAMES_DEC_DEBUG + #define BFRAMES_DEC +#endif + #include "xvid.h" #include "portab.h" @@ -205,8 +214,7 @@ Bitstream * bs, const uint32_t quant, const uint32_t intra_dc_threshold, - const unsigned int bound_x, - const unsigned int bound_y) + const unsigned int bound) { DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); @@ -232,7 +240,7 @@ start_timer(); predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], - iQuant, iDcScaler, predictors, bound_x, bound_y); + iQuant, iDcScaler, predictors, bound); if (!acpred_flag) { pMB->acpred_directions[i] = 0; } @@ -251,6 +259,8 @@ block[i * 64 + 0] = dc_dif; start_coeff = 1; + + DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif); } else { start_coeff = 0; } @@ -425,31 +435,33 @@ int quant, int intra_dc_threshold) { - uint32_t bound_x, bound_y; + uint32_t bound; uint32_t x, y; - bound_x = bound_y = 0; + bound = 0; for (y = 0; y < dec->mb_height; y++) { for (x = 0; x < dec->mb_width; x++) { MACROBLOCK *mb; - uint32_t mcbpc; uint32_t cbpc; uint32_t acpred_flag; uint32_t cbpy; uint32_t cbp; - skip_stuffing(bs); + while (BitstreamShowBits(bs, 9) == 1) + BitstreamSkip(bs, 9); + if (check_resync_marker(bs, 0)) { - int mbnum = read_video_packet_header(bs, 0); - x = bound_x = mbnum % dec->mb_width; - y = bound_y = mbnum / dec->mb_width; + bound = read_video_packet_header(bs, 0, &quant); + x = bound % dec->mb_width; + y = bound / dec->mb_width; } - mb = &dec->mbs[y * dec->mb_width + x]; + DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); + mcbpc = get_mcbpc_intra(bs); mb->mode = mcbpc & 7; cbpc = (mcbpc >> 4); @@ -468,6 +480,10 @@ } } mb->quant = quant; + mb->mvs[0].x = mb->mvs[0].y = + mb->mvs[1].x = mb->mvs[1].y = + mb->mvs[2].x = mb->mvs[2].y = + mb->mvs[3].x = mb->mvs[3].y =0; if (dec->interlacing) { mb->field_dct = BitstreamGetBit(bs); @@ -475,7 +491,7 @@ } decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, - intra_dc_threshold, bound_x, bound_y); + intra_dc_threshold, bound); } } @@ -490,8 +506,7 @@ int k, VECTOR * mv, int fcode, - const int bound_x, - const int bound_y) + const int bound) { int scale_fac = 1 << (fcode - 1); @@ -499,23 +514,18 @@ int low = ((-32) * scale_fac); int range = (64 * scale_fac); - VECTOR pmv[4]; - int32_t psad[4]; - + VECTOR pmv; int mv_x, mv_y; - int pmv_x, pmv_y; - - get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad, bound_x, bound_y); - - pmv_x = pmv[0].x; - pmv_y = pmv[0].y; + pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k); mv_x = get_mv(bs, fcode); mv_y = get_mv(bs, fcode); - mv_x += pmv_x; - mv_y += pmv_y; + DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y); + + mv_x += pmv.x; + mv_y += pmv.y; if (mv_x < low) { mv_x += range; @@ -545,28 +555,33 @@ { uint32_t x, y; - uint32_t bound_x, bound_y; + uint32_t bound; start_timer(); image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); stop_edges_timer(); - bound_x = bound_y = 0; + bound = 0; for (y = 0; y < dec->mb_height; y++) { for (x = 0; x < dec->mb_width; x++) { MACROBLOCK *mb; - skip_stuffing(bs); - if (check_resync_marker(bs, 0)) + // skip stuffing + while (BitstreamShowBits(bs, 10) == 1) + BitstreamSkip(bs, 10); + + if (check_resync_marker(bs, fcode - 1)) { - int mbnum = read_video_packet_header(bs, 0); - x = bound_x = mbnum % dec->mb_width; - y = bound_y = mbnum / dec->mb_width; + bound = read_video_packet_header(bs, fcode - 1, &quant); + x = bound % dec->mb_width; + y = bound / dec->mb_width; } mb = &dec->mbs[y * dec->mb_width + x]; + DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); + //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded if (!(BitstreamGetBit(bs))) // not_coded { @@ -580,6 +595,9 @@ mcbpc = get_mcbpc_inter(bs); mb->mode = mcbpc & 7; cbpc = (mcbpc >> 4); + + DPRINTF(DPRINTF_MB, "mode %i", mb->mode); + DPRINTF(DPRINTF_MB, "cbpc %i", cbpc); acpred_flag = 0; intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); @@ -589,15 +607,20 @@ } cbpy = get_cbpy(bs, intra); + DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); + cbp = (cbpy << 2) | cbpc; if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { - quant += dquant_table[BitstreamGetBits(bs, 2)]; + int dquant = dquant_table[BitstreamGetBits(bs, 2)]; + DPRINTF(DPRINTF_MB, "dquant %i", dquant); + quant += dquant; if (quant > 31) { quant = 31; - } else if (mb->quant < 1) { + } else if (quant < 1) { quant = 1; } + DPRINTF(DPRINTF_MB, "quant %i", quant); } mb->quant = quant; @@ -621,12 +644,12 @@ if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { if (dec->interlacing && mb->field_pred) { get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], - fcode, bound_x, bound_y); + fcode, bound); get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], - fcode, bound_x, bound_y); + fcode, bound); } else { get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], - fcode, bound_x, bound_y); + fcode, bound); mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = @@ -634,10 +657,10 @@ } } else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { - get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound_x, bound_y); - get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound_x, bound_y); - get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound_x, bound_y); - get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound_x, bound_y); + get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); + get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound); + get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound); + get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound); } else // MODE_INTRA, MODE_INTRA_Q { mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = @@ -645,7 +668,7 @@ mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, - intra_dc_threshold, bound_x, bound_y); + intra_dc_threshold, bound); continue; } @@ -776,6 +799,7 @@ pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); + if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { uv_dx = pMB->mvs[0].x; uv_dy = pMB->mvs[0].y; @@ -869,7 +893,6 @@ const MACROBLOCK * pMB, const uint32_t x_pos, const uint32_t y_pos, - const uint32_t cbp, Bitstream * bs) { @@ -884,11 +907,13 @@ int b_uv_dx, b_uv_dy; uint32_t i; uint8_t *pY_Cur, *pU_Cur, *pV_Cur; + const uint32_t cbp = pMB->cbp; pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); + if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { uv_dx = pMB->mvs[0].x; uv_dy = pMB->mvs[0].y; @@ -978,7 +1003,6 @@ stride2); interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, stride2); - stop_comp_timer(); for (i = 0; i < 6; i++) { @@ -1069,40 +1093,57 @@ int fcode_forward, int fcode_backward) { - uint32_t x, y; - VECTOR mv, zeromv; + VECTOR mv; + const VECTOR zeromv = {0,0}; +#ifdef BFRAMES_DEC_DEBUG + FILE *fp; + static char first=0; +#define BFRAME_DEBUG if (!first && fp){ \ + fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \ + } +#endif start_timer(); image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); - //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); + image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, + dec->width, dec->height, dec->interlacing); stop_edges_timer(); +#ifdef BFRAMES_DEC_DEBUG + if (!first){ + fp=fopen("C:\\XVIDDBG.TXT","w"); + } +#endif for (y = 0; y < dec->mb_height; y++) { // Initialize Pred Motion Vector - dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; + dec->p_fmv = dec->p_bmv = zeromv; for (x = 0; x < dec->mb_width; x++) { MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; - mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y = - 0; + mv = + mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = + mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; // the last P_VOP is skip macroblock ? if (last_mb->mode == MODE_NOT_CODED) { //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); - mb->mb_type = MODE_FORWARD; mb->cbp = 0; - mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; - mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; - mb->quant = 8; +#ifdef BFRAMES_DEC_DEBUG + mb->mb_type = MODE_NOT_CODED; + BFRAME_DEBUG +#endif + mb->mb_type = MODE_FORWARD; + mb->quant = last_mb->quant; + //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; + //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; - decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); + decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); continue; } - //t=BitstreamShowBits(bs,32); if (!BitstreamGetBit(bs)) { // modb=='0' const uint8_t modb2 = BitstreamGetBit(bs); @@ -1119,84 +1160,72 @@ if (quant > 31) { quant = 31; - } else if (mb->quant < 1) { + } else if (quant < 1) { quant = 1; } - } else { - quant = 8; } - mb->quant = quant; } else { mb->mb_type = MODE_DIRECT_NONE_MV; mb->cbp = 0; } - mb->mode = MODE_INTER; + mb->quant = quant; + mb->mode = MODE_INTER4V; //DEBUG1("Switch bm_type=",mb->mb_type); +#ifdef BFRAMES_DEC_DEBUG + BFRAME_DEBUG +#endif switch (mb->mb_type) { case MODE_DIRECT: - get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); + get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv); case MODE_DIRECT_NONE_MV: { // Because this file is a C file not C++ so I use '{' to define var - const int64_t TRB = dec->time_pp - dec->time_bp, TRD = - dec->time_pp; + const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; int i; for (i = 0; i < 4; i++) { - mb->mvs[i].x = - (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + - mb->mvs[0].x); - mb->b_mvs[i].x = - (int32_t) ((mb->mvs[0].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 + - mb->mvs[0].y); - mb->b_mvs[i].y = - (int32_t) ((mb->mvs[0].y == - 0) ? ((TRB - - TRD) * last_mb->mvs[i].y) / - TRD : mb->mvs[i].y - last_mb->mvs[i].y); + 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); } //DEBUG("B-frame Direct!\n"); } - mb->mode = MODE_INTER4V; decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], - mb, x, y, mb->cbp, bs); + mb, x, y, bs); break; case MODE_INTERPOLATE: get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); - dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = - mb->mvs[0].x; - dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = - mb->mvs[0].y; + dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv); - dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = - mb->b_mvs[3].x = mb->b_mvs[0].x; - dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = - mb->b_mvs[3].y = mb->b_mvs[0].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], - mb, x, y, mb->cbp, bs); + mb, x, y, bs); //DEBUG("B-frame Bidir!\n"); break; case MODE_BACKWARD: get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv); - dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = - mb->mvs[0].x; - dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = - mb->mvs[0].y; + dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; + mb->mode = MODE_INTER; decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); //DEBUG("B-frame Backward!\n"); break; @@ -1204,21 +1233,27 @@ case MODE_FORWARD: get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); - dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = - mb->mvs[0].x; - dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = - mb->mvs[0].y; + dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; + mb->mode = MODE_INTER; decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); //DEBUG("B-frame Forward!\n"); break; default: - DEBUG1("Not support B-frame mb_type =", mb->mb_type); + //DEBUG1("Not support B-frame mb_type =", mb->mb_type); + ; } } // end of FOR } +#ifdef BFRAMES_DEC_DEBUG + if (!first){ + first=1; + if (fp) + fclose(fp); + } +#endif } // swap two MACROBLOCK array @@ -1262,12 +1297,16 @@ case P_VOP: decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold); +#ifdef BFRAMES_DEC DEBUG1("P_VOP Time=", dec->time); +#endif break; case I_VOP: decoder_iframe(dec, &bs, quant, intra_dc_threshold); +#ifdef BFRAMES_DEC DEBUG1("I_VOP Time=", dec->time); +#endif break; case B_VOP: @@ -1292,17 +1331,24 @@ return XVID_ERR_FAIL; } +#ifdef BFRAMES_DEC_DEBUG + if (frame->length != BitstreamPos(&bs) / 8){ + DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8); + } +#endif frame->length = BitstreamPos(&bs) / 8; + #ifdef BFRAMES_DEC // test if no B_VOP if (dec->low_delay) { #endif - image_output(&dec->cur, dec->width, dec->height, dec->edged_width, + image_output(&dec->cur, dec->width, dec->height, dec->edged_width, frame->image, frame->stride, frame->colorspace); + #ifdef BFRAMES_DEC } else { - if (dec->frames >= 1) { + if (dec->frames >= 0) { start_timer(); if ((vop_type == I_VOP || vop_type == P_VOP)) { image_output(&dec->refn[0], dec->width, dec->height, @@ -1322,7 +1368,7 @@ image_swap(&dec->refn[0], &dec->refn[1]); image_swap(&dec->cur, &dec->refn[0]); // swap MACROBLOCK - if (dec->low_delay && vop_type == P_VOP) + if (!dec->low_delay && vop_type == P_VOP) mb_swap(&dec->mbs, &dec->last_mbs); }