--- decoder.c 2004/04/10 04:30:07 1.53 +++ decoder.c 2005/03/27 03:59:41 1.69 @@ -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.53 2004/04/10 04:30:07 suxen_drol Exp $ + * $Id: decoder.c,v 1.69 2005/03/27 03:59:41 suxen_drol Exp $ * ****************************************************************************/ @@ -46,8 +46,8 @@ #include "dct/fdct.h" #include "utils/mem_transfer.h" #include "image/interpolate8x8.h" -#include "image/reduced.h" #include "image/font.h" +#include "image/qpel.h" #include "bitstream/mbcoding.h" #include "prediction/mbprediction.h" @@ -222,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); @@ -269,8 +270,7 @@ Bitstream * bs, const uint32_t quant, const uint32_t intra_dc_threshold, - const unsigned int bound, - const int reduced_resolution) + const unsigned int bound) { DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); @@ -283,15 +283,9 @@ uint32_t iQuant = pMB->quant; uint8_t *pY_Cur, *pU_Cur, *pV_Cur; - if (reduced_resolution) { - pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5); - pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4); - pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4); - }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); - } + 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); memset(block, 0, 6 * 64 * sizeof(int16_t)); /* clear */ @@ -338,7 +332,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(); @@ -361,24 +355,12 @@ } start_timer(); - - if (reduced_resolution) - { - next_block*=2; - copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride); - copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride); - copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride); - copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride); - copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2); - copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2); - }else{ - transfer_16to8copy(pY_Cur, &data[0 * 64], stride); - transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); - transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); - transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride); - transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); - transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); - } + transfer_16to8copy(pY_Cur, &data[0 * 64], stride); + transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); + transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); + transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride); + transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); + transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); stop_transfer_timer(); } @@ -389,74 +371,108 @@ uint8_t * pY_Cur, uint8_t * pU_Cur, uint8_t * pV_Cur, - const int reduced_resolution, const MACROBLOCK * pMB) { - DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE); - DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); + DECLARE_ALIGNED_MATRIX(data, 1, 64, int16_t, CACHE_LINE); int stride = dec->edged_width; - int next_block = stride * (reduced_resolution ? 16 : 8); - const int stride2 = stride/2; + int next_block = stride * 8; int i; const uint32_t iQuant = pMB->quant; const int direction = dec->alternate_vertical_scan ? 2 : 0; - const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter; + typedef void (*get_inter_block_function_t)( + Bitstream * bs, + int16_t * block, + int direction, + const int quant, + const uint16_t *matrix); + typedef void (*add_residual_function_t)( + uint8_t *predicted_block, + const int16_t *residual, + int stride); + + const get_inter_block_function_t get_inter_block = (dec->quant_type == 0) + ? (get_inter_block_function_t)get_inter_block_h263 + : (get_inter_block_function_t)get_inter_block_mpeg; - for (i = 0; i < 6; i++) { + uint8_t *dst[6]; + int strides[6]; + + + if (dec->interlacing && pMB->field_dct) { + next_block = stride; + stride *= 2; + } - if (cbp & (1 << (5 - i))) { /* coded */ + dst[0] = pY_Cur; + dst[2] = pY_Cur + next_block; + dst[1] = dst[0] + 8; + dst[3] = dst[2] + 8; + dst[4] = pU_Cur; + dst[5] = pV_Cur; + strides[0] = strides[1] = strides[2] = strides[3] = stride; + strides[4] = stride/2; + strides[5] = stride/2; + + for (i = 0; i < 6; i++) { + /* Process only coded blocks */ + if (cbp & (1 << (5 - i))) { - memset(block, 0, 64 * sizeof(int16_t)); /* clear */ + /* Clear the block */ + memset(&data[0], 0, 64*sizeof(int16_t)); + /* Decode coeffs and dequantize on the fly */ start_timer(); - get_inter_block(bs, block, direction); + get_inter_block(bs, &data[0], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices)); stop_coding_timer(); + /* iDCT */ start_timer(); - dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices); - stop_iquant_timer(); + idct(&data[0]); + stop_idct_timer(); + /* Add this residual to the predicted block */ start_timer(); - idct(&data[i * 64]); - stop_idct_timer(); + transfer_16to8add(dst[i], &data[0], strides[i]); + stop_transfer_timer(); } } +} - if (dec->interlacing && pMB->field_dct) { - next_block = stride; - stride *= 2; - } - - start_timer(); - if (reduced_resolution) { - if (cbp & 32) - add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride); - if (cbp & 16) - add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride); - if (cbp & 8) - add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride); - if (cbp & 4) - add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride); - if (cbp & 2) - add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2); - if (cbp & 1) - add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2); - } else { - 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(); +static void __inline +validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec) +{ + /* clip a vector to valid range + prevents crashes if bitstream is broken + */ + int shift = 5 + dec->quarterpel; + int xborder_high = (int)(dec->mb_width - x_pos) << shift; + int xborder_low = (-(int)x_pos-1) << shift; + int yborder_high = (int)(dec->mb_height - y_pos) << shift; + int yborder_low = (-(int)y_pos-1) << shift; + +#define CHECK_MV(mv) \ + do { \ + if ((mv).x > xborder_high) { \ + DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \ + (mv).x = xborder_high; \ + } else if ((mv).x < xborder_low) { \ + DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \ + (mv).x = xborder_low; \ + } \ + if ((mv).y > yborder_high) { \ + DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \ + (mv).y = yborder_high; \ + } else if ((mv).y < yborder_low) { \ + DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \ + (mv).y = yborder_low; \ + } \ + } while (0) + + CHECK_MV(mv[0]); + CHECK_MV(mv[1]); + CHECK_MV(mv[2]); + CHECK_MV(mv[3]); } /* decode an inter macroblock */ @@ -468,7 +484,6 @@ const uint32_t cbp, Bitstream * bs, const uint32_t rounding, - const int reduced_resolution, const int ref) { uint32_t stride = dec->edged_width; @@ -480,21 +495,13 @@ int uv_dx, uv_dy; VECTOR mv[4]; /* local copy of mvs */ - if (reduced_resolution) { - pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5); - pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4); - pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4); - for (i = 0; i < 4; i++) { - mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x); - mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y); - } - } 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]; - } + 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]; + + validate_vector(mv, x_pos, y_pos, dec); start_timer(); @@ -509,10 +516,7 @@ uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; - if (reduced_resolution) - interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos, - mv[0].x, mv[0].y, stride, rounding); - else if (dec->quarterpel) + if (dec->quarterpel) 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, mv[0].x, mv[0].y, stride, rounding); @@ -533,21 +537,7 @@ uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf]; - if (reduced_resolution) { - 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, - 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, - 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); - interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos, - uv_dx, uv_dy, stride2, rounding); - - } else if (dec->quarterpel) { + if (dec->quarterpel) { 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); @@ -573,23 +563,15 @@ } /* chroma */ - if (reduced_resolution) { - 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, - uv_dx, uv_dy, stride2, rounding); - } else { - interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos, - uv_dx, uv_dy, stride2, rounding); - interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos, - uv_dx, uv_dy, stride2, rounding); - } + interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos, + uv_dx, uv_dy, stride2, rounding); + interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos, + uv_dx, uv_dy, stride2, rounding); stop_comp_timer(); if (cbp) - decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, - reduced_resolution, pMB); + decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB); } static void @@ -636,7 +618,7 @@ stop_transfer_timer(); if (cbp) - decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB); + decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB); } @@ -644,19 +626,13 @@ static void decoder_iframe(DECODER * dec, Bitstream * bs, - int reduced_resolution, int quant, int intra_dc_threshold) { uint32_t bound; 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; - mb_height = (dec->height + 31) / 32; - } + const uint32_t mb_width = dec->mb_width; + const uint32_t mb_height = dec->mb_height; bound = 0; @@ -712,7 +688,7 @@ } decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, - intra_dc_threshold, bound, reduced_resolution); + intra_dc_threshold, bound); } if(dec->out_frm) @@ -761,20 +737,6 @@ mv.y -= range; } - /* clip to valid range */ - - if (mv.x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) ) - mv.x = (int)(dec->mb_width - x) << (5 + dec->quarterpel); - - else if (mv.x < (int)(-x-1) << (5 + dec->quarterpel)) - mv.x = (int)(-x-1) << (5 + dec->quarterpel); - - if (mv.y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) ) - mv.y = (int)(dec->mb_height - y) << (5 + dec->quarterpel); - - else if (mv.y < ((int)(-y-1)) << (5 + dec->quarterpel) ) - mv.y = (int)(-y-1) << (5 + dec->quarterpel); - ret_mv->x = mv.x; ret_mv->y = mv.y; } @@ -784,7 +746,6 @@ decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, - int reduced_resolution, int quant, int fcode, int intra_dc_threshold, @@ -793,19 +754,17 @@ uint32_t x, y; uint32_t bound; int cp_mb, st_mb; - uint32_t mb_width = dec->mb_width; - uint32_t mb_height = dec->mb_height; + const uint32_t mb_width = dec->mb_width; + const uint32_t mb_height = dec->mb_height; - if (reduced_resolution) { - mb_width = (dec->width + 31) / 32; - mb_height = (dec->height + 31) / 32; + if (!dec->is_edged[0]) { + start_timer(); + image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, + dec->width, dec->height, dec->bs_version); + dec->is_edged[0] = 1; + stop_edges_timer(); } - start_timer(); - image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, - dec->width, dec->height, dec->bs_version); - stop_edges_timer(); - if (gmc_warp) { /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */ generate_GMCparameters( dec->sprite_warping_points, @@ -915,12 +874,11 @@ mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; 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, reduced_resolution); + intra_dc_threshold, bound); continue; } - decoder_mbinter(dec, mb, x, y, cbp, bs, - rounding, reduced_resolution, 0); + decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0); } else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ mb->mode = MODE_NOT_CODED_GMC; @@ -940,7 +898,7 @@ mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; decoder_mbinter(dec, mb, x, y, 0, bs, - rounding, reduced_resolution, 0); + rounding, 0); if(dec->out_frm && cp_mb > 0) { output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); @@ -986,20 +944,6 @@ else if (mv_y > high) mv_y -= range; - - /* clip to valid range */ - if (mv_x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) ) - mv_x = (int)(dec->mb_width - x) << (5 + dec->quarterpel); - - else if (mv_x < (int)(-x-1) << (5 + dec->quarterpel)) - mv_x = (int)(-x-1) << (5 + dec->quarterpel); - - if (mv_y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) ) - mv_y = (int)(dec->mb_height - y) << (5 + dec->quarterpel); - - else if (mv_y < ((int)(-y-1)) << (5 + dec->quarterpel) ) - mv_y = (int)(-y-1) << (5 + dec->quarterpel); - mv->x = mv_x; mv->y = mv_y; } @@ -1009,7 +953,7 @@ decoder_bf_interpolate_mbinter(DECODER * dec, IMAGE forward, IMAGE backward, - const MACROBLOCK * pMB, + MACROBLOCK * pMB, const uint32_t x_pos, const uint32_t y_pos, Bitstream * bs, @@ -1026,10 +970,12 @@ pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); + validate_vector(pMB->mvs, x_pos, y_pos, dec); + validate_vector(pMB->b_mvs, x_pos, y_pos, dec); + if (!direct) { uv_dx = pMB->mvs[0].x; uv_dy = pMB->mvs[0].y; - b_uv_dx = pMB->b_mvs[0].x; b_uv_dy = pMB->b_mvs[0].y; @@ -1042,21 +988,20 @@ uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; - b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3]; b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3]; } else { - if(dec->quarterpel) { - uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2); - uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2); - b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2); - b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2); - } else { - uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; - uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; - b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; - b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; + uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; + uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; + b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; + b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; + + if (dec->quarterpel) { + uv_dx /= 2; + uv_dy /= 2; + b_uv_dx /= 2; + b_uv_dy /= 2; } uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; @@ -1104,73 +1049,43 @@ if(dec->quarterpel) { if(!direct) { - 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); + interpolate16x16_add_quarterpel(dec->cur.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->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->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->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->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); + interpolate8x8_add_quarterpel(dec->cur.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_add_quarterpel(dec->cur.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_add_quarterpel(dec->cur.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_add_quarterpel(dec->cur.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->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->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->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->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->tmp.u, backward.u, 8 * x_pos, 8 * y_pos, - b_uv_dx, b_uv_dy, stride2, 0); - 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->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->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->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->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->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->tmp.v + (8 * y_pos * stride2) + 8 * x_pos, - stride2, 1, 8); + interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos, 16 * y_pos, + pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); + interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8, + 16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0); + interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos, + 16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0); + interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8, + 16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0); + } + + interpolate8x8_add_switch(dec->cur.u, backward.u, 8 * x_pos, 8 * y_pos, + b_uv_dx, b_uv_dy, stride2, 0); + interpolate8x8_add_switch(dec->cur.v, backward.v, 8 * x_pos, 8 * y_pos, + b_uv_dx, b_uv_dy, stride2, 0); stop_comp_timer(); if (cbp) - decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB); + decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB); } /* for decode B-frame dbquant */ @@ -1215,15 +1130,23 @@ 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(); - image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, - dec->width, dec->height, dec->bs_version); - image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, - dec->width, dec->height, dec->bs_version); - stop_edges_timer(); + if (!dec->is_edged[0]) { + start_timer(); + image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, + dec->width, dec->height, dec->bs_version); + dec->is_edged[0] = 1; + stop_edges_timer(); + } + + if (!dec->is_edged[1]) { + start_timer(); + image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, + dec->width, dec->height, dec->bs_version); + dec->is_edged[1] = 1; + stop_edges_timer(); + } for (y = 0; y < dec->mb_height; y++) { /* Initialize Pred Motion Vector */ @@ -1257,7 +1180,7 @@ if (last_mb->mode == MODE_NOT_CODED) { mb->cbp = 0; mb->mode = MODE_FORWARD; - decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); + decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); continue; } @@ -1310,14 +1233,15 @@ 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], @@ -1339,14 +1263,14 @@ 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); + decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0); break; case MODE_FORWARD: 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); + decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); break; default: @@ -1361,17 +1285,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) || frame->brightness!=0) + 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, frame->brightness, dec->frames, (coding_type == B_VOP)); + frame->general, brightness, dec->frames, (coding_type == B_VOP)); img = &dec->tmp; } @@ -1394,7 +1320,6 @@ } } - int decoder_decode(DECODER * dec, xvid_dec_frame_t * frame, xvid_dec_stats_t * stats) @@ -1402,7 +1327,6 @@ Bitstream bs; uint32_t rounding; - uint32_t reduced_resolution; uint32_t quant = 2; uint32_t fcode_forward; uint32_t fcode_backward; @@ -1453,15 +1377,20 @@ success = 0; output = 0; - if (stats) stats->type = XVID_TYPE_NOTHING; seen_something = 0; repeat: - coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, + coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &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=%" +#if defined(_MSC_VER) + "I64" +#else + "ll" +#endif + "i, 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 */ @@ -1492,6 +1421,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 */ @@ -1504,14 +1438,14 @@ } else if (coding_type != B_VOP) { switch(coding_type) { case I_VOP : - decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); + decoder_iframe(dec, &bs, quant, intra_dc_threshold); break; case P_VOP : - decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, + decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold, NULL); break; case S_VOP : - decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, + decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold, &gmc_warp); break; case N_VOP : @@ -1522,12 +1456,6 @@ break; } - if (reduced_resolution) { - image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, - (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width, - 16, 0); - } - /* note: for packed_mode, output is performed when the special-N_VOP is decoded */ if (!(dec->low_delay_default && dec->packed_mode)) { if (dec->low_delay) { @@ -1541,9 +1469,10 @@ } image_swap(&dec->refn[0], &dec->refn[1]); + dec->is_edged[1] = dec->is_edged[0]; image_swap(&dec->cur, &dec->refn[0]); + dec->is_edged[0] = 0; SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); - dec->last_reduced_resolution = reduced_resolution; dec->last_coding_type = coding_type; dec->frames++; @@ -1553,7 +1482,7 @@ if (dec->low_delay) { DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n"); - dec->low_delay = 1; + dec->low_delay = 0; } if (dec->frames < 2) { @@ -1588,19 +1517,22 @@ done : - /* low_delay_default mode: if we've gotten here without outputting anything, - then output the recently decoded frame, or print an error message */ - if (dec->low_delay_default && output == 0) { - if (dec->packed_mode && seen_something) { - /* output the recently decoded frame */ + /* if we reach here without outputing anything _and_ + the calling application has specified low_delay_default, + we *must* output something. + this always occurs on the first call to decode() call + when bframes are present in the bitstream. it may also + occur if no vops were seen in the bitstream + + if packed_mode is enabled, then we output the recently + decoded frame (the very first ivop). otherwise we have + nothing to display, and therefore output a black screen. + */ + if (dec->low_delay_default && output == 0) { + if (dec->packed_mode && seen_something) { decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); - } else { + } 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"); - decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant); if (stats) stats->type = XVID_TYPE_NOTHING; }