--- decoder.c 2005/09/20 11:54:11 1.74 +++ decoder.c 2010/12/24 13:49:58 1.86 @@ -4,7 +4,7 @@ * - Decoder Module - * * Copyright(C) 2002 MinChen - * 2002-2004 Peter Ross + * 2002-2010 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.74 2005/09/20 11:54:11 suxen_drol Exp $ + * $Id: decoder.c,v 1.86 2010/12/24 13:49:58 Isibaar Exp $ * ****************************************************************************/ @@ -172,6 +172,8 @@ dec->width = create->width; dec->height = create->height; + dec->num_threads = MAX(0, create->num_threads); + image_null(&dec->cur); image_null(&dec->refn[0]); image_null(&dec->refn[1]); @@ -195,11 +197,23 @@ dec->low_delay = 0; dec->packed_mode = 0; dec->time_inc_resolution = 1; /* until VOL header says otherwise */ + dec->ver_id = 1; + + if (create->fourcc == ((int)('X')|((int)('V')<<8)| + ((int)('I')<<16)|((int)('D')<<24))) { /* XVID */ + dec->bs_version = 0; /* Initially assume oldest xvid version */ + } + else { + dec->bs_version = 0xffff; /* Initialize to very high value -> assume bugfree stream */ + } dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); - if (dec->fixed_dimensions) - return decoder_resize(dec); + if (dec->fixed_dimensions) { + int ret = decoder_resize(dec); + if (ret == XVID_ERR_MEMORY) create->handle = NULL; + return ret; + } else return 0; } @@ -316,7 +330,7 @@ stop_iquant_timer(); start_timer(); - idct(&data[i * 64]); + idct((short * const)&data[i * 64]); stop_idct_timer(); } @@ -406,7 +420,7 @@ /* iDCT */ start_timer(); - idct(&data[0]); + idct((short * const)&data[0]); stop_idct_timer(); /* Add this residual to the predicted block */ @@ -466,7 +480,8 @@ const uint32_t cbp, Bitstream * bs, const uint32_t rounding, - const int ref) + const int ref, + const int bvop) { uint32_t stride = dec->edged_width; uint32_t stride2 = stride / 2; @@ -487,13 +502,13 @@ start_timer(); - if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ + if ((pMB->mode != MODE_INTER4V) || (bvop)) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ uv_dx = mv[0].x; uv_dy = mv[0].y; if (dec->quarterpel) { if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { - uv_dx = (uv_dx>>1) | (uv_dx&1); + uv_dx = (uv_dx>>1) | (uv_dx&1); uv_dy = (uv_dy>>1) | (uv_dy&1); } else { @@ -581,7 +596,8 @@ const uint32_t cbp, Bitstream * bs, const uint32_t rounding, - const int ref) + const int ref, + const int bvop) { uint32_t stride = dec->edged_width; uint32_t stride2 = stride / 2; @@ -605,7 +621,7 @@ start_timer(); - if(pMB->mode!=MODE_INTER4V) /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ + if((pMB->mode!=MODE_INTER4V) || (bvop)) /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ { /* Prepare top field vector */ uvtop_dx = DIV2ROUND(mv[0].x); @@ -743,7 +759,7 @@ bound = read_video_packet_header(bs, dec, 0, &quant, NULL, NULL, &intra_dc_threshold); x = bound % mb_width; - y = bound / mb_width; + y = MIN((bound / mb_width), (mb_height-1)); } mb = &dec->mbs[y * dec->mb_width + x]; @@ -970,7 +986,7 @@ bound = read_video_packet_header(bs, dec, fcode - 1, &quant, &fcode, NULL, &intra_dc_threshold); x = bound % mb_width; - y = bound / mb_width; + y = MIN((bound / mb_width), (mb_height-1)); } mb = &dec->mbs[y * dec->mb_width + x]; @@ -1063,9 +1079,9 @@ /* See how to decode */ if(!mb->field_pred) - decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0); + decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0, 0); else - decoder_mbinter_field(dec, mb, x, y, cbp, bs, rounding, 0); + decoder_mbinter_field(dec, mb, x, y, cbp, bs, rounding, 0, 0); } else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ mb->mode = MODE_NOT_CODED_GMC; @@ -1086,7 +1102,7 @@ mb->field_pred=0; /* (!) */ decoder_mbinter(dec, mb, x, y, 0, bs, - rounding, 0); + rounding, 0, 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); @@ -1188,24 +1204,30 @@ b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3]; } 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; - - if (dec->quarterpel) { - if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { - uv_dx = (uv_dx>>1) | (uv_dx&1); - uv_dy = (uv_dy>>1) | (uv_dy&1); - b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1); - b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1); + if (dec->quarterpel) { /* for qpel the /2 shall be done before summation. We've done it right in the encoder in the past. */ + /* TODO: figure out if we ever did it wrong on the encoder side. If yes, add some workaround */ + if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { + int z; + uv_dx = 0; uv_dy = 0; + b_uv_dx = 0; b_uv_dy = 0; + for (z = 0; z < 4; z++) { + uv_dx += ((pMB->mvs[z].x>>1) | (pMB->mvs[z].x&1)); + uv_dy += ((pMB->mvs[z].y>>1) | (pMB->mvs[z].y&1)); + b_uv_dx += ((pMB->b_mvs[z].x>>1) | (pMB->b_mvs[z].x&1)); + b_uv_dy += ((pMB->b_mvs[z].y>>1) | (pMB->b_mvs[z].y&1)); } - else { - uv_dx /= 2; - uv_dy /= 2; - b_uv_dx /= 2; - b_uv_dy /= 2; - } + } + else { + 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 = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; @@ -1324,6 +1346,13 @@ return -1; } +static int __inline get_resync_len_b(const int fcode_backward, + const int fcode_forward) { + int resync_len = ((fcode_forward>fcode_backward) ? fcode_forward : fcode_backward) - 1; + if (resync_len < 1) resync_len = 1; + return resync_len; +} + static void decoder_bframe(DECODER * dec, Bitstream * bs, @@ -1335,6 +1364,7 @@ VECTOR mv; const VECTOR zeromv = {0,0}; int i; + int resync_len; if (!dec->is_edged[0]) { start_timer(); @@ -1352,24 +1382,15 @@ stop_edges_timer(); } + resync_len = get_resync_len_b(fcode_backward, fcode_forward); for (y = 0; y < dec->mb_height; y++) { /* Initialize Pred Motion Vector */ 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]; - const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; int intra_dc_threshold; /* fake variable */ - if (check_resync_marker(bs, fcode_max - 1)) { - int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, - &fcode_forward, &fcode_backward, &intra_dc_threshold); - x = bound % dec->mb_width; - y = bound / dec->mb_width; - /* reset predicted macroblocks */ - dec->p_fmv = dec->p_bmv = zeromv; - } - 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; @@ -1384,10 +1405,24 @@ if (last_mb->mode == MODE_NOT_CODED) { mb->cbp = 0; mb->mode = MODE_FORWARD; - decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); + decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1, 1); continue; } + if (check_resync_marker(bs, resync_len)) { + int bound = read_video_packet_header(bs, dec, resync_len, &quant, + &fcode_forward, &fcode_backward, &intra_dc_threshold); + + bound = MAX(0, bound--); /* valid bound must always be >0 */ + x = bound % dec->mb_width; + y = MIN((bound / dec->mb_width), (dec->mb_height-1)); + /* reset predicted macroblocks */ + dec->p_fmv = dec->p_bmv = zeromv; + /* update resync len with new fcodes */ + resync_len = get_resync_len_b(fcode_backward, fcode_forward); + continue; /* re-init loop */ + } + if (!BitstreamGetBit(bs)) { /* modb=='0' */ const uint8_t modb2 = BitstreamGetBit(bs); @@ -1467,14 +1502,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); + decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); 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, 1); + decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1, 1); break; default: @@ -1485,7 +1520,7 @@ } /* perform post processing if necessary, and output the image */ -void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, +static void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type, int quant) { @@ -1501,7 +1536,7 @@ 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, brightness, dec->frames, (coding_type == B_VOP)); + frame->general, brightness, dec->frames, (coding_type == B_VOP), dec->num_threads); img = &dec->tmp; } @@ -1607,7 +1642,7 @@ if (coding_type == -2 || coding_type == -3) { /* vol and/or resize */ if (coding_type == -3) - decoder_resize(dec); + if (decoder_resize(dec)) return XVID_ERR_MEMORY; if(stats) { stats->type = XVID_TYPE_VOL; @@ -1630,7 +1665,7 @@ goto repeat; } - dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; /* init pred vector to 0 */ + dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.x = dec->p_fmv.y = 0; /* init pred vector to 0 */ /* packed_mode: special-N_VOP treament */ if (dec->packed_mode && coding_type == N_VOP) {