--- trunk/xvidcore/src/decoder.c 2002/07/07 16:36:24 270 +++ trunk/xvidcore/src/decoder.c 2002/07/09 01:09:33 271 @@ -32,7 +32,6 @@ * * History: * - * 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 * 08.05.2002 add low_delay support for B_VOP decode @@ -50,7 +49,7 @@ * 22.12.2001 lock based interpolation * 01.12.2001 inital version; (c)2001 peter ross * - * $Id: decoder.c,v 1.24 2002-07-03 12:32:50 suxen_drol Exp $ + * $Id: decoder.c,v 1.25 2002-07-09 01:09:33 chenm001 Exp $ * *************************************************************************/ @@ -169,6 +168,122 @@ } + +/* **************************************************************** +NIC 28.06.2002 + riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla + usare nella dll caricata dall'IM1 player + IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) +**************************************************************** */ +//XVID_DEC_FRAME * frame + +int +IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) +{ + DECODER *dec; + Bitstream bs; + uint32_t rounding; + uint32_t quant; + uint32_t fcode_forward; + uint32_t fcode_backward; + uint32_t intra_dc_threshold; + + dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); + if (dec == NULL) { + return XVID_ERR_MEMORY; + } + param->handle = dec; + + + BitstreamInit(&bs, frame->bitstream, frame->length);//NIC + + //NIC + BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, + &fcode_backward, &intra_dc_threshold); + + + param->width = dec->width; //NIC added + param->height = dec->height; //NIC added + //dec->width = param->width; //NIC commentate + //dec->height = param->height; //NIC commentate + + dec->mb_width = (dec->width + 15) / 16; + dec->mb_height = (dec->height + 15) / 16; + + dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; + dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; + dec->low_delay = 0; + + if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { + xvid_free(dec); + return XVID_ERR_MEMORY; + } + + if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { + image_destroy(&dec->cur, dec->edged_width, dec->edged_height); + xvid_free(dec); + return XVID_ERR_MEMORY; + } + // add by chenm001 + // for support B-frame to reference last 2 frame + if (image_create(&dec->refn[1], 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); + xvid_free(dec); + return XVID_ERR_MEMORY; + } + if (image_create(&dec->refn[2], 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; + } + + dec->mbs = + xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, + CACHE_LINE); + if (dec->mbs == NULL) { + 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); + xvid_free(dec); + return XVID_ERR_MEMORY; + } + + memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); + + // add by chenm001 + // for skip MB flag + dec->last_mbs = + xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, + CACHE_LINE); + if (dec->last_mbs == NULL) { + xvid_free(dec->mbs); + 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); + xvid_free(dec); + return XVID_ERR_MEMORY; + } + + memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); + + init_timer(); + + // add by chenm001 + // for support B-frame to save reference frame's time + dec->frames = -1; + dec->time = dec->time_base = dec->last_time_base = 0; + + return XVID_ERR_OK; +} +/* **************************************************************** + END NIC +**************************************************************** */ + int decoder_destroy(DECODER * dec) { @@ -204,8 +319,7 @@ const uint32_t cbp, Bitstream * bs, const uint32_t quant, - const uint32_t intra_dc_threshold, - const unsigned int bound) + const uint32_t intra_dc_threshold) { DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); @@ -231,7 +345,7 @@ start_timer(); predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], - iQuant, iDcScaler, predictors, bound); + iQuant, iDcScaler, predictors); if (!acpred_flag) { pMB->acpred_directions[i] = 0; } @@ -250,8 +364,6 @@ block[i * 64 + 0] = dc_dif; start_coeff = 1; - - DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif); } else { start_coeff = 0; } @@ -426,39 +538,30 @@ int quant, int intra_dc_threshold) { - uint32_t bound; - uint32_t x, y; - bound = 0; + uint32_t x, y; for (y = 0; y < dec->mb_height; y++) { for (x = 0; x < dec->mb_width; x++) { - MACROBLOCK *mb; + MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; + uint32_t mcbpc; uint32_t cbpc; uint32_t acpred_flag; uint32_t cbpy; uint32_t cbp; - while (BitstreamShowBits(bs, 9) == 1) - BitstreamSkip(bs, 9); - - if (check_resync_marker(bs, 0)) - { - 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); acpred_flag = BitstreamGetBit(bs); + if (mb->mode == MODE_STUFFING) { + DEBUG("-- STUFFING ?"); + continue; + } + cbpy = get_cbpy(bs, 1); cbp = (cbpy << 2) | cbpc; @@ -478,7 +581,7 @@ } decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, - intra_dc_threshold, bound); + intra_dc_threshold); } } @@ -492,8 +595,7 @@ int y, int k, VECTOR * mv, - int fcode, - const int bound) + int fcode) { int scale_fac = 1 << (fcode - 1); @@ -501,18 +603,23 @@ int low = ((-32) * scale_fac); int range = (64 * scale_fac); - VECTOR pmv; + VECTOR pmv[4]; + int32_t psad[4]; + int mv_x, mv_y; + int pmv_x, pmv_y; - pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k); + + get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad); + + pmv_x = pmv[0].x; + pmv_y = pmv[0].y; 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); - - mv_x += pmv.x; - mv_y += pmv.y; + mv_x += pmv_x; + mv_y += pmv_y; if (mv_x < low) { mv_x += range; @@ -542,32 +649,15 @@ { uint32_t x, 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 = 0; - for (y = 0; y < dec->mb_height; y++) { for (x = 0; x < dec->mb_width; x++) { - MACROBLOCK *mb; - - // skip stuffing - while (BitstreamShowBits(bs, 10) == 1) - BitstreamSkip(bs, 10); - - if (check_resync_marker(bs, fcode - 1)) - { - 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)); + MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded if (!(BitstreamGetBit(bs))) // not_coded @@ -582,9 +672,6 @@ 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); @@ -593,21 +680,21 @@ acpred_flag = BitstreamGetBit(bs); } - cbpy = get_cbpy(bs, intra); - DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); + if (mb->mode == MODE_STUFFING) { + DEBUG("-- STUFFING ?"); + continue; + } + cbpy = get_cbpy(bs, intra); cbp = (cbpy << 2) | cbpc; if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { - int dquant = dquant_table[BitstreamGetBits(bs, 2)]; - DPRINTF(DPRINTF_MB, "dquant %i", dquant); - quant += dquant; + quant += dquant_table[BitstreamGetBits(bs, 2)]; if (quant > 31) { quant = 31; - } else if (quant < 1) { + } else if (mb->quant < 1) { quant = 1; } - DPRINTF(DPRINTF_MB, "quant %i", quant); } mb->quant = quant; @@ -631,12 +718,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); + fcode); get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], - fcode, bound); + fcode); } else { get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], - fcode, bound); + fcode); 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 = @@ -644,10 +731,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); - 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); + get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); + get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode); + get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode); + get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode); } else // MODE_INTRA, MODE_INTRA_Q { mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = @@ -655,7 +742,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); + intra_dc_threshold); continue; } @@ -1129,7 +1216,7 @@ if (quant > 31) { quant = 31; - } else if (quant < 1) { + } else if (mb->quant < 1) { quant = 1; } } else { @@ -1272,27 +1359,27 @@ case P_VOP: decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold); -#ifdef BFRAMES_DEC + printf("P_VOP Time=%ld\n",dec->time); DEBUG1("P_VOP Time=", dec->time); -#endif break; case I_VOP: decoder_iframe(dec, &bs, quant, intra_dc_threshold); -#ifdef BFRAMES_DEC + printf("I_VOP Time=%ld\n",dec->time); DEBUG1("I_VOP Time=", dec->time); -#endif break; case B_VOP: #ifdef BFRAMES_DEC if (dec->time_pp > dec->time_bp) { + printf("I_VOP Time=%ld\n",dec->time); DEBUG1("B_VOP Time=", dec->time); decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); } else { DEBUG("broken B-frame!"); } #else + printf("Che minchia ne so\n"); image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); #endif break; @@ -1300,6 +1387,7 @@ case N_VOP: // vop not coded // when low_delay==0, N_VOP's should interpolate between the past and future frames image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); + printf("N_VOP vop not coded\n"); break; default: