--- decoder.c 2003/06/09 19:41:53 1.49.2.8 +++ decoder.c 2003/10/03 15:49:21 1.49.2.14 @@ -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.8 2003/06/09 19:41:53 edgomez Exp $ + * $Id: decoder.c,v 1.49.2.14 2003/10/03 15:49:21 syskin Exp $ * ****************************************************************************/ @@ -54,6 +54,7 @@ #include "utils/timer.h" #include "utils/emms.h" #include "motion/motion.h" +#include "motion/gmc.h" #include "image/image.h" #include "image/colorspace.h" @@ -71,7 +72,7 @@ image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); - if (dec->last_mbs) + if (dec->last_mbs) xvid_free(dec->last_mbs); if (dec->mbs) xvid_free(dec->mbs); @@ -168,7 +169,7 @@ { DECODER *dec; - if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ + if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ return XVID_ERR_VERSION; dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); @@ -208,7 +209,7 @@ if (dec->fixed_dimensions) return decoder_resize(dec); else - return 0; + return 0; } @@ -410,12 +411,12 @@ 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 / (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]; @@ -451,7 +452,7 @@ } else { /* MODE_INTER4V */ int sum; - + if(dec->quarterpel) sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2); else @@ -475,7 +476,7 @@ mv[1].x, mv[1].y, stride, rounding); 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, uv_dx, uv_dy, stride2, rounding); @@ -483,7 +484,7 @@ uv_dx, uv_dy, stride2, rounding); /* set_block(pY_Cur, stride, 32, 32, 127); */ - } + } else { if(dec->quarterpel) { @@ -507,7 +508,7 @@ mv[1].x, mv[1].y, stride, rounding); 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); } @@ -583,19 +584,6 @@ 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, @@ -625,18 +613,27 @@ 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); + + { NEW_GMC_DATA * gmc_data = &dec->new_gmc_data; + + gmc_data->predict_16x16(gmc_data, + dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y, + stride, stride, x_pos, y_pos, rounding); + + gmc_data->predict_8x8(gmc_data, + dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u, + dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v, + stride2, stride2, x_pos, y_pos, rounding); + + gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel); 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); @@ -645,7 +642,7 @@ stop_transfer_timer(); - + if (!cbp) return; for (i = 0; i < 6; i++) { @@ -708,7 +705,7 @@ uint32_t x, y; uint32_t mb_width = dec->mb_width; uint32_t mb_height = dec->mb_height; - + if (reduced_resolution) { mb_width = (dec->width + 31) / 32; @@ -731,7 +728,7 @@ if (check_resync_marker(bs, 0)) { - bound = read_video_packet_header(bs, dec, 0, + bound = read_video_packet_header(bs, dec, 0, &quant, NULL, NULL, &intra_dc_threshold); x = bound % mb_width; y = bound / mb_width; @@ -845,7 +842,7 @@ int cp_mb, st_mb; uint32_t mb_width = dec->mb_width; uint32_t mb_height = dec->mb_height; - + if (reduced_resolution) { mb_width = (dec->width + 31) / 32; @@ -858,28 +855,20 @@ 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", +/* { + fprintf(stderr,"GMC parameters acc=%d(-> 1/%d), %d pts!!!\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); -*/ + generate_GMCparameters( dec->sprite_warping_points, + dec->sprite_warping_accuracy, gmc_warp, + dec->width, dec->height, &dec->new_gmc_data); + +/* image warping is done block-based in decoder_mbgmc(), now */ } bound = 0; @@ -895,7 +884,7 @@ if (check_resync_marker(bs, fcode - 1)) { - bound = read_video_packet_header(bs, dec, fcode - 1, + bound = read_video_packet_header(bs, dec, fcode - 1, &quant, &fcode, NULL, &intra_dc_threshold); x = bound % mb_width; y = bound / mb_width; @@ -919,21 +908,18 @@ mcbpc = get_mcbpc_inter(bs); mb->mode = mcbpc & 7; cbpc = (mcbpc >> 4); - + DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode); DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc); acpred_flag = 0; intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); - if (intra) { - acpred_flag = BitstreamGetBit(bs); - } - if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) - { mcsel = BitstreamGetBit(bs); - } + + if (intra) + acpred_flag = BitstreamGetBit(bs); cbpy = get_cbpy(bs, intra); DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel); @@ -954,7 +940,7 @@ mb->quant = quant; if (dec->interlacing) { - if (cbp || intra) { + if ((cbp || intra) && !mcsel) { mb->field_dct = BitstreamGetBit(bs); DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); } @@ -971,7 +957,7 @@ } } } - + if (mcsel) { decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant, rounding, reduced_resolution); @@ -1040,7 +1026,7 @@ if (reduced_resolution) { transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x), - dec->refn[0].y + (32*y)*dec->edged_width + (32*x), + dec->refn[0].y + (32*y)*dec->edged_width + (32*x), dec->edged_width); transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x), @@ -1054,7 +1040,7 @@ else { transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), - dec->refn[0].y + (16*y)*dec->edged_width + (16*x), + dec->refn[0].y + (16*y)*dec->edged_width + (16*x), dec->edged_width); transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x), @@ -1065,7 +1051,7 @@ dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x), dec->edged_width/2); } - + stop_transfer_timer(); if(dec->out_frm && cp_mb > 0) { @@ -1196,7 +1182,7 @@ pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); - interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8, + interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); } @@ -1572,7 +1558,7 @@ mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; /* - * skip if the co-located P_VOP macroblock is not coded + * skip if the co-located P_VOP macroblock is not coded * if not codec in co-located S_VOP macroblock is _not_ * automatically skipped */ @@ -1614,6 +1600,26 @@ quant = 1; } } + + if (dec->interlacing) { + if (mb->cbp) { + mb->field_dct = BitstreamGetBit(bs); + DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); + } + + if (mb->mb_type) { + mb->field_pred = BitstreamGetBit(bs); + DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); + + if (mb->field_pred) { + mb->field_for_top = BitstreamGetBit(bs); + DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top); + mb->field_for_bot = BitstreamGetBit(bs); + DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot); + } + } + } + } else { mb->mb_type = MODE_DIRECT_NONE_MV; mb->cbp = 0; @@ -1632,7 +1638,7 @@ get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv); case MODE_DIRECT_NONE_MV: - { + { const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; int i; @@ -1709,7 +1715,7 @@ /* perform post processing if necessary, and output the image */ -void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, +void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) { @@ -1742,9 +1748,8 @@ WARPPOINTS gmc_warp; int coding_type; int success, output, seen_something; - idctFuncPtr idct_save; - if (XVID_MAJOR(frame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ + if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */ return XVID_ERR_VERSION; start_global_timer(); @@ -1788,14 +1793,13 @@ success = 0; output = 0; seen_something = 0; - idct_save = idct; repeat: - coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, + coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); - DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i, packed=%i, time=%lli, time_pp=%i, time_bp=%i\n", + DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i, packed=%i, time=%lli, time_pp=%i, time_bp=%i\n", coding_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); if (coding_type == -1) /* nothing */ @@ -1810,7 +1814,7 @@ { if (coding_type == -3) decoder_resize(dec); - + if (stats) { stats->type = XVID_TYPE_VOL; @@ -1826,19 +1830,10 @@ return BitstreamPos(&bs)/8; /* number of bytes consumed */ } goto repeat; - } + } dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; /* init pred vector to 0 */ -#if defined(ARCH_IS_IA32) - /* - * /!\ Ugly hack /!\ - * IA32: Prior to xvid bitstream 10, we were using Walten's mmx/xmm idct - */ - if((idct == simple_idct_mmx) && (dec->bs_version < 10)) - idct = idct_mmx; -#endif - /* packed_mode: special-N_VOP treament */ if (dec->packed_mode && coding_type == N_VOP) { @@ -1857,11 +1852,11 @@ decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); break; case P_VOP : - decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, + decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, fcode_forward, intra_dc_threshold, NULL); break; case S_VOP : - decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, + decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, fcode_forward, intra_dc_threshold, &gmc_warp); break; case N_VOP : @@ -1917,7 +1912,7 @@ 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 + /* 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); @@ -1958,7 +1953,7 @@ image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, "bframe decoder lag"); - decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); + decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); if (stats) stats->type = XVID_TYPE_NOTHING; } @@ -1967,7 +1962,5 @@ emms(); stop_global_timer(); - idct = idct_save; - return BitstreamPos(&bs) / 8; /* number of bytes consumed */ }