--- decoder.c 2002/12/12 10:37:44 1.37.2.19 +++ decoder.c 2003/01/11 21:22:24 1.37.2.29 @@ -55,10 +55,11 @@ * 22.12.2001 lock based interpolation * 01.12.2001 inital version; (c)2001 peter ross * - * $Id: decoder.c,v 1.37.2.19 2002/12/12 10:37:44 suxen_drol Exp $ + * $Id: decoder.c,v 1.37.2.29 2003/01/11 21:22:24 chl Exp $ * *************************************************************************/ +#include #include #include @@ -68,6 +69,7 @@ #include "xvid.h" #include "portab.h" +#include "global.h" #include "decoder.h" #include "bitstream/bitstream.h" @@ -97,11 +99,13 @@ { /* free existing */ + image_destroy(&dec->cur, dec->edged_width, dec->edged_height); image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); - image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); - image_destroy(&dec->refh, dec->edged_width, dec->edged_height); - image_destroy(&dec->cur, dec->edged_width, dec->edged_height); + image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); + image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); + + image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); if (dec->last_mbs) xvid_free(dec->last_mbs); @@ -135,19 +139,29 @@ xvid_free(dec); return XVID_ERR_MEMORY; } - if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { + if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) { + image_destroy(&dec->cur, dec->edged_width, dec->edged_height); + image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); + image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); + xvid_free(dec); + return XVID_ERR_MEMORY; + } + + if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) { image_destroy(&dec->cur, dec->edged_width, dec->edged_height); image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); + image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); xvid_free(dec); return XVID_ERR_MEMORY; } - if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) { + if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) { + image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); image_destroy(&dec->cur, dec->edged_width, dec->edged_height); image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); - image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); + image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); xvid_free(dec); return XVID_ERR_MEMORY; } @@ -159,8 +173,8 @@ image_destroy(&dec->cur, dec->edged_width, dec->edged_height); image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); - image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); - image_destroy(&dec->refh, dec->edged_width, dec->edged_height); + image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); + image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); xvid_free(dec); return XVID_ERR_MEMORY; } @@ -176,8 +190,8 @@ image_destroy(&dec->cur, dec->edged_width, dec->edged_height); image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); - image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); - image_destroy(&dec->refh, dec->edged_width, dec->edged_height); + image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); + image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); xvid_free(dec); return XVID_ERR_MEMORY; } @@ -207,8 +221,12 @@ image_null(&dec->cur); image_null(&dec->refn[0]); image_null(&dec->refn[1]); - image_null(&dec->refn[2]); - image_null(&dec->refh); + image_null(&dec->tmp); + image_null(&dec->qtmp); + +/* image based GMC */ + image_null(&dec->gmc); + dec->mbs = NULL; dec->last_mbs = NULL; @@ -236,10 +254,13 @@ { xvid_free(dec->last_mbs); xvid_free(dec->mbs); + + image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); /* image based GMC */ + image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); - image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); - image_destroy(&dec->refh, dec->edged_width, dec->edged_height); + image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); + image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); image_destroy(&dec->cur, dec->edged_width, dec->edged_height); xvid_free(dec); @@ -384,10 +405,6 @@ - -#define SIGN(X) (((X)>0)?1:-1) -#define ABS(X) (((X)>0)?(X):-(X)) - // decode an inter macroblock void @@ -395,7 +412,7 @@ const MACROBLOCK * pMB, const uint32_t x_pos, const uint32_t y_pos, - const uint32_t acpred_flag, + const uint32_t fcode, const uint32_t cbp, Bitstream * bs, const uint32_t quant, @@ -424,24 +441,19 @@ mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x); mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y); } - }else{ + } else { 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); for (i = 0; i < 4; i++) mv[i] = pMB->mvs[i]; } - + if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { - uv_dx = mv[0].x; - uv_dy = mv[0].y; - - if (dec->quarterpel) - { - uv_dx /= 2; - uv_dy /= 2; - } + uv_dx = mv[0].x / (1 + dec->quarterpel); + uv_dy = mv[0].y / (1 + dec->quarterpel); + uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; @@ -459,13 +471,13 @@ else { if(dec->quarterpel) { - interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, - mv[0].x, mv[0].y, stride, rounding); + interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, + mv[0].x, mv[0].y, stride, rounding); } else { interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, - mv[0].x, mv[0].y, stride, rounding); + mv[0].x, mv[0].y, stride, rounding); } interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, @@ -497,15 +509,15 @@ { interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos, mv[0].x, mv[0].y, stride, rounding); - interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos, + interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos, mv[1].x, mv[1].y, stride, rounding); - interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos + 16, + interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16, mv[2].x, mv[2].y, stride, rounding); - interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos + 16, + interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16, mv[3].x, mv[3].y, stride, rounding); - interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos, + interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos, uv_dx, uv_dy, stride2, rounding); - interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos, + interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos, uv_dx, uv_dy, stride2, rounding); // set_block(pY_Cur, stride, 32, 32, 127); @@ -513,33 +525,33 @@ else { if(dec->quarterpel) { - interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, + interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, mv[0].x, mv[0].y, stride, rounding); - interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos + 8, 16*y_pos, + interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos, mv[1].x, mv[1].y, stride, rounding); - interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos + 8, + interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8, mv[2].x, mv[2].y, stride, rounding); - interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8, + interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8, mv[3].x, mv[3].y, stride, rounding); } else { - interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, + interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos, mv[0].x, mv[0].y, stride, rounding); - interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, + interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos, mv[1].x, mv[1].y, stride, rounding); - interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, + interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8, mv[2].x, mv[2].y, stride, rounding); - interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, + interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8, mv[3].x, mv[3].y, stride, rounding); } - interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, + interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos, uv_dx, uv_dy, stride2, rounding); - interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, + interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos, uv_dx, uv_dy, stride2, rounding); } stop_comp_timer(); @@ -609,6 +621,118 @@ stop_transfer_timer(); } +static __inline int gmc_sanitize(int value, int quarterpel, int fcode) +{ + int length = 1 << (fcode+4); + + if (quarterpel) value *= 2; + + if (value < -length) + return -length; + else if (value >= length) + return length-1; + else return value; +} + + +static void +decoder_mbgmc(DECODER * dec, + MACROBLOCK * const pMB, + const uint32_t x_pos, + const uint32_t y_pos, + const uint32_t fcode, + const uint32_t cbp, + Bitstream * bs, + const uint32_t quant, + const uint32_t rounding, + const int reduced_resolution) /* no reduced res support */ +{ + + DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); + DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); + + const uint32_t stride = dec->edged_width; + const uint32_t stride2 = stride / 2; + const uint32_t next_block = stride * (reduced_resolution ? 16 : 8); + uint32_t i; + const uint32_t iQuant = pMB->quant; + uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); + uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); + uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); + + pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; + + start_timer(); + +/* this is where the calculations are done */ + + { + pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos, + stride, stride2, dec->quarterpel, rounding, &dec->cur); + + pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode); + pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode); + } + pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; + + +/* transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos << 4), stride); + transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos << 3), stride2); + transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2); +*/ + + + stop_transfer_timer(); + + if (!cbp) return; + + for (i = 0; i < 6; i++) { + int direction = dec->alternate_vertical_scan ? 2 : 0; + + if (cbp & (1 << (5 - i))) // coded + { + memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear + + start_timer(); + get_inter_block(bs, &block[i * 64], direction); + stop_coding_timer(); + + start_timer(); + if (dec->quant_type == 0) { + dequant_inter(&data[i * 64], &block[i * 64], iQuant); + } else { + dequant4_inter(&data[i * 64], &block[i * 64], iQuant); + } + stop_iquant_timer(); + + start_timer(); + idct(&data[i * 64]); + stop_idct_timer(); + } + } + +/* interlace + GMC is this possible ??? */ +/* if (dec->interlacing && pMB->field_dct) { + next_block = stride; + stride *= 2; + } +*/ + start_timer(); + if (cbp & 32) + transfer_16to8add(pY_Cur, &data[0 * 64], stride); + if (cbp & 16) + transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); + if (cbp & 8) + transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); + if (cbp & 4) + transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); + if (cbp & 2) + transfer_16to8add(pU_Cur, &data[4 * 64], stride2); + if (cbp & 1) + transfer_16to8add(pV_Cur, &data[5 * 64], stride2); + stop_transfer_timer(); +} + void decoder_iframe(DECODER * dec, @@ -619,8 +743,8 @@ { uint32_t bound; uint32_t x, y; - int mb_width = dec->mb_width; - int mb_height = dec->mb_height; + uint32_t mb_width = dec->mb_width; + uint32_t mb_height = dec->mb_height; if (reduced_resolution) { @@ -678,7 +802,7 @@ if (dec->interlacing) { mb->field_dct = BitstreamGetBit(bs); - DEBUG1("deci: field_dct: ", mb->field_dct); + DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct); } decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, @@ -716,7 +840,7 @@ mv.x = get_mv(bs, fcode); mv.y = get_mv(bs, fcode); - DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y); + DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y); mv.x += pmv.x; mv.y += pmv.y; @@ -739,21 +863,9 @@ -static __inline int gmc_sanitize(int value, int quarterpel, int fcode) -{ - int length = 1 << (fcode+4); - - if (quarterpel) value *= 2; - - if (value < -length) - return -length; - else if (value >= length) - return length-1; - else return value; -} -/* for P_VOP set gmc_mv to NULL */ +/* for P_VOP set gmc_warp to NULL */ void decoder_pframe(DECODER * dec, Bitstream * bs, @@ -762,14 +874,14 @@ int quant, int fcode, int intra_dc_threshold, - VECTOR * gmc_mv) + const WARPPOINTS *const gmc_warp) { uint32_t x, y; uint32_t bound; int cp_mb, st_mb; - int mb_width = dec->mb_width; - int mb_height = dec->mb_height; + uint32_t mb_width = dec->mb_width; + uint32_t mb_height = dec->mb_height; if (reduced_resolution) { @@ -782,6 +894,31 @@ dec->width, dec->height); stop_edges_timer(); + if (gmc_warp) + { + + // accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 + if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) ) + { + fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n", + dec->sprite_warping_accuracy,(2<sprite_warping_accuracy), + dec->sprite_warping_points); + } + + generate_GMCparameters( dec->sprite_warping_points, + (2 << dec->sprite_warping_accuracy), gmc_warp, + dec->width, dec->height, &dec->gmc_data); + +/* image warping is done block-based in decoder_mbgmc(), now */ +/* + generate_GMCimage(&dec->gmc_data, &dec->refn[0], + mb_width, mb_height, + dec->edged_width, dec->edged_width/2, + fcode, dec->quarterpel, 0, + rounding, dec->mbs, &dec->gmc); +*/ + } + bound = 0; for (y = 0; y < mb_height; y++) { @@ -805,7 +942,7 @@ 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 + if (!(BitstreamGetBit(bs))) // block _is_ coded { uint32_t mcbpc; uint32_t cbpc; @@ -830,13 +967,13 @@ acpred_flag = BitstreamGetBit(bs); } - if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) + if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) { mcsel = BitstreamGetBit(bs); } cbpy = get_cbpy(bs, intra); - DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); + DPRINTF(DPRINTF_MB, "cbpy %i mcsel %i ", cbpy,mcsel); cbp = (cbpy << 2) | cbpc; @@ -856,30 +993,30 @@ if (dec->interlacing) { if (cbp || intra) { mb->field_dct = BitstreamGetBit(bs); - DEBUG1("decp: field_dct: ", mb->field_dct); + DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct); } if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { mb->field_pred = BitstreamGetBit(bs); - DEBUG1("decp: field_pred: ", mb->field_pred); + DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred); if (mb->field_pred) { mb->field_for_top = BitstreamGetBit(bs); - DEBUG1("decp: field_for_top: ", mb->field_for_top); + DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top); mb->field_for_bot = BitstreamGetBit(bs); - DEBUG1("decp: field_for_bot: ", mb->field_for_bot); + DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot); } } } + + if (mcsel) { + decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant, + rounding, reduced_resolution); + continue; - if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { - - if (mcsel) - { - mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode); - mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode); + } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { - } else if (dec->interlacing && mb->field_pred) { + if (dec->interlacing && mb->field_pred) { get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], @@ -887,10 +1024,7 @@ } else { get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], 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 = - mb->mvs[0].y; + mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; } } else if (mb->mode == MODE_INTER4V ) { @@ -909,16 +1043,26 @@ continue; } - decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, + decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant, rounding, reduced_resolution); - } - else if (gmc_mv) /* not coded S_VOP macroblock */ + } + else if (gmc_warp) /* a not coded S(GMC)-VOP macroblock */ { - mb->mode = MODE_NOT_CODED; - mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode); - mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode); - decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution); + mb->mode = MODE_NOT_CODED_GMC; + + start_timer(); + + decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant, + rounding, reduced_resolution); + + stop_transfer_timer(); + + if(dec->out_frm && cp_mb > 0) { + output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); + cp_mb = 0; + } + st_mb = x+1; } else /* not coded P_VOP macroblock */ { @@ -1080,8 +1224,8 @@ start_timer(); if(dec->quarterpel) { - interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, + interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); } else { @@ -1235,21 +1379,21 @@ start_timer(); if(dec->quarterpel) { if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) - interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, + interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); else { - interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, + interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); - interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos + 8, 16*y_pos, + interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); - interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos + 8, + interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); - interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8, + interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); } } @@ -1273,71 +1417,71 @@ if(dec->quarterpel) { if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) - interpolate16x16_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, + interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); else { - interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos, + interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos, pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); - interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos + 8, 16*y_pos, + interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0); - interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos, 16*y_pos + 8, + interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0); - interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64, - dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8, + interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64, + dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0); } } else { - interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos, + interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos, pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); - interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, + interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8, 16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0); - interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, + interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0); - interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, + interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8, 16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0); } - interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos, + interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos, b_uv_dx, b_uv_dy, stride2, 0); - interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos, + interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos, b_uv_dx, b_uv_dy, stride2, 0); interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, - dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos, + dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos, stride, 1, 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->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8, + dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8, stride, 1, 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->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos, + dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, stride, 1, 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->refn[2].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); interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, - dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos, + dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos, stride2, 1, 8); interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, - dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos, + dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos, stride2, 1, 8); stop_comp_timer(); @@ -1467,7 +1611,9 @@ 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 ? + // skip if the co-located P_VOP macroblock is not coded + // if not codec in co-located S_VOP macroblock is _not_ automatically skipped + if (last_mb->mode == MODE_NOT_CODED) { //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); mb->cbp = 0; @@ -1581,7 +1727,7 @@ break; default: - DEBUG1("Not support B-frame mb_type =", mb->mb_type); + DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type); } } // end of FOR @@ -1607,6 +1753,27 @@ } +/* perform post processing if necessary, and output the image */ +void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, + const XVID_DEC_FRAME * frame, int pp_disable) +{ + + if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable) /* post process */ + { + /* note: image is stored to tmp */ + image_copy(&dec->tmp, img, dec->edged_width, dec->height); + image_deblock_rrv(&dec->tmp, dec->edged_width, + mbs, dec->mb_width, dec->mb_height, dec->mb_width, + 8, frame->general); + img = &dec->tmp; + } + + image_output(img, dec->width, dec->height, + dec->edged_width, frame->image, frame->stride, + frame->colorspace, dec->interlacing); +} + + int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats) @@ -1619,7 +1786,7 @@ uint32_t fcode_forward; uint32_t fcode_backward; uint32_t intra_dc_threshold; - VECTOR gmc_mv[5]; + WARPPOINTS gmc_warp; uint32_t vop_type; int success = 0; int output = 0; @@ -1628,13 +1795,39 @@ start_global_timer(); dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY); - dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; + if ((frame->general & XVID_DEC_DISCONTINUITY)) + dec->frames = 0; + + if (frame->length < 0) /* decoder flush */ + { + /* if not decoding "low_delay/packed", and this isn't low_delay and + we have a reference frame, then outout the reference frame */ + if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) + { + decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution); + output = 1; + } + + frame->length = 0; + if (stats) + { + stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING; + stats->data.vop.time_base = (int)dec->time_base; + stats->data.vop.time_increment = 0; //XXX: todo + } + + emms(); + + stop_global_timer(); + return XVID_ERR_OK; + } + BitstreamInit(&bs, frame->bitstream, frame->length); // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's - if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f) + if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f) { if (stats) stats->notify = XVID_DEC_VOP; @@ -1645,21 +1838,18 @@ return XVID_ERR_OK; } -start: - // add by chenm001 - // for support B-frame to reference last 2 frame +repeat: -xxx: - vop_type = - BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, - &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv); + vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, + &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); - DPRINTF(DPRINTF_HEADER, "vop_type=%i, packed=%i, time=%i, time_pp=%i, time_bp=%i", + DPRINTF(DPRINTF_HEADER, "vop_type=%i, packed=%i, time=%lli, time_pp=%i, time_bp=%i", vop_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); if (vop_type == - 1) { if (success) goto done; + emms(); return XVID_ERR_FAIL; } @@ -1680,9 +1870,10 @@ stats->data.vol.par_width = dec->par_width; stats->data.vol.par_height = dec->par_height; frame->length = BitstreamPos(&bs) / 8; + emms(); return XVID_ERR_OK; } - goto xxx; + goto repeat; } dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; // init pred vector to 0 @@ -1691,11 +1882,9 @@ /* packed_mode: special-N_VOP treament */ if (dec->packed_mode && vop_type == N_VOP) { - if (dec->low_delay_default) + if (dec->low_delay_default && dec->frames > 0) { - image_output(&dec->refn[0], dec->width, dec->height, - dec->edged_width, frame->image, frame->stride, - frame->colorspace, dec->interlacing); + decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution); output = 1; } /* ignore otherwise */ @@ -1713,7 +1902,7 @@ break; case S_VOP : decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, - fcode_forward, intra_dc_threshold, gmc_mv); + fcode_forward, intra_dc_threshold, &gmc_warp); break; case N_VOP : image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); @@ -1722,8 +1911,9 @@ if (reduced_resolution) { - image_deblock_rrv(&dec->refn[0], dec->edged_width, dec->mbs, - (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width); + image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, + (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width, + 16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); } /* note: for packed_mode, output is performed when the special-N_VOP is decoded */ @@ -1731,16 +1921,13 @@ { if (dec->low_delay) { - image_output(&dec->cur, dec->width, dec->height, - dec->edged_width, frame->image, frame->stride, - frame->colorspace, dec->interlacing); + decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution); output = 1; } else if (dec->frames > 0) /* is the reference frame valid? */ { - image_output(&dec->refn[0], dec->width, dec->height, - dec->edged_width, frame->image, frame->stride, - frame->colorspace, dec->interlacing); + /* output the reference frame */ + decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution); output = 1; } } @@ -1748,6 +1935,7 @@ image_swap(&dec->refn[0], &dec->refn[1]); image_swap(&dec->cur, &dec->refn[0]); mb_swap(&dec->mbs, &dec->last_mbs); + dec->last_reduced_resolution = reduced_resolution; dec->frames++; seen_something = 1; @@ -1760,7 +1948,12 @@ dec->low_delay = 1; } - if (dec->time_pp <= dec->time_bp) { + if (dec->frames < 2) + { + /* 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"); + }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, @@ -1768,23 +1961,19 @@ }else{ decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); } - - image_output(&dec->cur, dec->width, dec->height, - dec->edged_width, frame->image, frame->stride, - frame->colorspace, dec->interlacing); + + decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution); output = 1; dec->frames++; } - emms(); - BitstreamByteAlign(&bs); /* low_delay_default mode: repeat in packed_mode */ if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) { success = 1; - goto start; + goto repeat; } done : @@ -1796,22 +1985,19 @@ if (dec->packed_mode && seen_something) { /* output the recently decoded frame */ - image_output(&dec->refn[0], dec->width, dec->height, - dec->edged_width, frame->image, frame->stride, - frame->colorspace, dec->interlacing); + decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution); + output = 1; } else { + image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128); image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, "warning: nothing to output"); image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, "bframe decoder lag"); - image_output(&dec->cur, dec->width, dec->height, - dec->edged_width, frame->image, frame->stride, - frame->colorspace, dec->interlacing); + decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/); } - output = 1; } frame->length = BitstreamPos(&bs) / 8; @@ -1822,7 +2008,7 @@ stats->data.vop.time_base = (int)dec->time_base; stats->data.vop.time_increment = 0; //XXX: todo } - + emms(); stop_global_timer();