--- encoder.c 2003/12/12 14:16:40 1.95.2.58 +++ encoder.c 2004/12/10 05:37:11 1.116 @@ -21,7 +21,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: encoder.c,v 1.95.2.58 2003/12/12 14:16:40 edgomez Exp $ + * $Id: encoder.c,v 1.116 2004/12/10 05:37:11 syskin Exp $ * ****************************************************************************/ @@ -57,9 +57,7 @@ Bitstream * bs); static int FrameCodeP(Encoder * pEnc, - Bitstream * bs, - bool force_inter, - bool vol_header); + Bitstream * bs); static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, @@ -102,11 +100,22 @@ i--; } - /* if neccessary, round to 65535 accuracy */ - if (*base > 65535) { - float div = (float) *base / 65535; - *base = (int) (*base / div); - *inc = (int) (*inc / div); + if (*base > 65535 || *inc > 65535) { + int *biggest; + int *other; + float div; + + if (*base > *inc) { + biggest = base; + other = inc; + } else { + biggest = inc; + other = base; + } + + div = ((float)*biggest)/((float)65535); + *biggest = (int)(((float)*biggest)/div); + *other = (int)(((float)*other)/div); } } @@ -123,6 +132,9 @@ if (create->width%2 || create->height%2) return XVID_ERR_FAIL; + if (create->width<=0 || create->height<=0) + return XVID_ERR_FAIL; + /* allocate encoder struct */ pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); @@ -652,7 +664,7 @@ data.max_quant[i] = pEnc->mbParam.max_quant[i]; } - data.reference.csp = XVID_CSP_USER; + data.reference.csp = XVID_CSP_PLANAR; data.reference.plane[0] = pEnc->reference->image.y; data.reference.plane[1] = pEnc->reference->image.u; data.reference.plane[2] = pEnc->reference->image.v; @@ -660,7 +672,7 @@ data.reference.stride[1] = pEnc->mbParam.edged_width/2; data.reference.stride[2] = pEnc->mbParam.edged_width/2; - data.current.csp = XVID_CSP_USER; + data.current.csp = XVID_CSP_PLANAR; data.current.plane[0] = frame->image.y; data.current.plane[1] = frame->image.u; data.current.plane[2] = frame->image.v; @@ -690,7 +702,7 @@ } else { /* XVID_PLG_AFTER */ if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { - data.original.csp = XVID_CSP_USER; + data.original.csp = XVID_CSP_PLANAR; data.original.plane[0] = original->y; data.original.plane[1] = original->u; data.original.plane[2] = original->v; @@ -740,20 +752,22 @@ data.mblks = frame->sStat.mblks; data.ublks = frame->sStat.ublks; - if (stats) { - stats->type = coding2type(frame->coding_type); - stats->quant = frame->quant; - stats->vol_flags = frame->vol_flags; - stats->vop_flags = frame->vop_flags; - stats->length = frame->length; - stats->hlength = frame->length - (frame->sStat.iTextBits / 8); - stats->kblks = frame->sStat.kblks; - stats->mblks = frame->sStat.mblks; - stats->ublks = frame->sStat.ublks; - stats->sse_y = data.sse_y; - stats->sse_u = data.sse_u; - stats->sse_v = data.sse_v; - } + /* New code */ + data.stats.type = coding2type(frame->coding_type); + data.stats.quant = frame->quant; + data.stats.vol_flags = frame->vol_flags; + data.stats.vop_flags = frame->vop_flags; + data.stats.length = frame->length; + data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); + data.stats.kblks = frame->sStat.kblks; + data.stats.mblks = frame->sStat.mblks; + data.stats.ublks = frame->sStat.ublks; + data.stats.sse_y = data.sse_y; + data.stats.sse_u = data.sse_u; + data.stats.sse_v = data.sse_v; + + if (stats) + *stats = data.stats; } /* call plugins */ @@ -1084,8 +1098,9 @@ DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", pEnc->bframenum_head, pEnc->bframenum_tail, pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); + pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ - FrameCodeP(pEnc, &bs, 1, 0); + FrameCodeP(pEnc, &bs); if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { @@ -1119,7 +1134,7 @@ pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; inc_frame_num(pEnc); - pEnc->current->vol_flags = pEnc->mbParam.vol_flags; + pEnc->current->vol_flags = frame->vol_flags; pEnc->current->vop_flags = frame->vop_flags; pEnc->current->motion_flags = frame->motion; pEnc->current->fcode = pEnc->mbParam.m_fcode; @@ -1154,6 +1169,9 @@ } } + if (type != I_VOP) + pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ + /* bframes buffer overflow check */ if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { type = P_VOP; @@ -1266,7 +1284,7 @@ pEnc->iFrameNum = 1; /* ---- update vol flags at IVOP ----------- */ - pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; + pEnc->mbParam.vol_flags = pEnc->current->vol_flags; /* Aspect ratio */ switch(frame->par) { @@ -1299,9 +1317,6 @@ /* prevent vol/vop misuse */ - if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) - pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; - if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); @@ -1334,7 +1349,11 @@ pEnc->mbParam.edged_width, pEnc->mbParam.height); } - FrameCodeP(pEnc, &bs, 1, 0); + if ( FrameCodeP(pEnc, &bs) == 0 ) { + /* N-VOP, we mustn't code b-frames yet */ + call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); + goto done; + } } @@ -1425,20 +1444,6 @@ uint16_t x, y; - if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) - { - mb_width = (pEnc->mbParam.width + 31) / 32; - mb_height = (pEnc->mbParam.height + 31) / 32; - - /* 16x16->8x8 downsample requires 1 additional edge pixel*/ - /* XXX: setedges is overkill */ - start_timer(); - image_setedges(&pEnc->current->image, - pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, - pEnc->mbParam.width, pEnc->mbParam.height); - stop_edges_timer(); - } - pEnc->mbParam.m_rounding_type = 1; pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; pEnc->current->coding_type = I_VOP; @@ -1447,7 +1452,7 @@ SetMacroblockQuants(&pEnc->mbParam, pEnc->current); - BitstreamWriteVolHeader(bs, &pEnc->mbParam); + BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); @@ -1474,21 +1479,10 @@ stop_prediction_timer(); start_timer(); - if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) - { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ - qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ - qcoeff[5*64+0]=0; - } MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); stop_coding_timer(); } - if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) - { - image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, - pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, - 16, 0); - } emms(); BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ @@ -1504,49 +1498,56 @@ return 1; /* intra */ } +static __inline void +updateFcode(Statistics * sStat, Encoder * pEnc) +{ + float fSigma; + int iSearchRange; + + if (sStat->iMvCount == 0) + sStat->iMvCount = 1; -#define INTRA_THRESHOLD 0.5 -#define BFRAME_SKIP_THRESHHOLD 30 + fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); + + iSearchRange = 16 << pEnc->mbParam.m_fcode; + if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) + pEnc->mbParam.m_fcode++; + + else if ((5.0 * fSigma < iSearchRange) + && (4.0 * pEnc->fMvPrevSigma < iSearchRange) + && (pEnc->mbParam.m_fcode >= 2) ) + pEnc->mbParam.m_fcode--; + + pEnc->fMvPrevSigma = fSigma; +} + +#define BFRAME_SKIP_THRESHHOLD 30 /* FrameCodeP also handles S(GMC)-VOPs */ static int FrameCodeP(Encoder * pEnc, - Bitstream * bs, - bool force_inter, - bool vol_header) + Bitstream * bs) { - float fSigma; int bits = BitstreamPos(bs); DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); - int iLimit; int x, y, k; - int iSearchRange; - int bIntra=0, skip_possible; FRAMEINFO *const current = pEnc->current; FRAMEINFO *const reference = pEnc->reference; MBParam * const pParam = &pEnc->mbParam; int mb_width = pParam->mb_width; int mb_height = pParam->mb_height; + int coded = 1; - - /* IMAGE *pCurrent = ¤t->image; */ IMAGE *pRef = &reference->image; - if ((current->vop_flags & XVID_VOP_REDUCED)) - { - mb_width = (pParam->width + 31) / 32; - mb_height = (pParam->height + 31) / 32; - } - - if (!reference->is_edged) { start_timer(); image_setedges(pRef, pParam->edged_width, pParam->edged_height, - pParam->width, pParam->height); + pParam->width, pParam->height, 0); stop_edges_timer(); reference->is_edged = 1; } @@ -1555,11 +1556,6 @@ current->rounding_type = pParam->m_rounding_type; current->fcode = pParam->m_fcode; - if (!force_inter) - iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); - else - iLimit = mb_width * mb_height + 1; - if ((current->vop_flags & XVID_VOP_HALFPEL)) { if (reference->is_interpolated != current->rounding_type) { start_timer(); @@ -1573,6 +1569,9 @@ } } + current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = + current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; + current->coding_type = P_VOP; call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); @@ -1630,36 +1629,23 @@ } } - bIntra = - MotionEstimation(&pEnc->mbParam, current, reference, + MotionEstimation(&pEnc->mbParam, current, reference, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, - &pEnc->vGMC, iLimit); + &pEnc->vGMC, 256*4096); stop_motion_timer(); - if (bIntra == 1) return FrameCodeI(pEnc, bs); - set_timecodes(current,reference,pParam->fbase); - if (vol_header) - { BitstreamWriteVolHeader(bs, &pEnc->mbParam); - BitstreamPad(bs); - } BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); - current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = - current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; - - for (y = 0; y < mb_height; y++) { for (x = 0; x < mb_width; x++) { - MACROBLOCK *pMB = - ¤t->mbs[x + y * pParam->mb_width]; - - bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); + MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; + int skip_possible; - if (bIntra) { + if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { CodeIntraMB(pEnc, pMB); MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, dct_codes, qcoeff); @@ -1670,11 +1656,6 @@ current->sStat.kblks++; - if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) - { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ - qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ - qcoeff[5*64+0]=0; - } MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); stop_coding_timer(); continue; @@ -1689,16 +1670,14 @@ pParam->height, pParam->edged_width, (current->vol_flags & XVID_VOL_QUARTERPEL), - (current->vop_flags & XVID_VOP_REDUCED), current->rounding_type); stop_comp_timer(); pMB->field_pred = 0; - if (pMB->mode != MODE_NOT_CODED) - { pMB->cbp = - MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, + if (pMB->cbp != 0) { + pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, dct_codes, qcoeff); } @@ -1718,148 +1697,70 @@ /* Finished processing the MB, now check if to CODE or SKIP */ - skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && - (pMB->dquant == 0); + skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER); if (current->coding_type == S_VOP) skip_possible &= (pMB->mcsel == 1); - else if (current->coding_type == P_VOP) { - if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) - skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); - else - skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); + else { /* PVOP */ + const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ? + pMB->qmvs : pMB->mvs; + skip_possible &= ((mv->x|mv->y) == 0); } - if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { - -/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ + if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { + /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ + int bSkip = 1; - if (current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ - { - int bSkip = 1; + if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */ - for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) - { + for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) { int iSAD; iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, - pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, - pParam->edged_width,BFRAME_SKIP_THRESHHOLD); - if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) - { bSkip = 0; + pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, + pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); + if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { + bSkip = 0; /* could not SKIP */ + if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { + VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); + pMB->pmvs[0].x = - predMV.x; + pMB->pmvs[0].y = - predMV.y; + } else { + VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); + pMB->pmvs[0].x = - predMV.x; + pMB->pmvs[0].y = - predMV.y; + } + pMB->mode = MODE_INTER; + pMB->cbp = 0; break; } } - - if (!bSkip) { /* no SKIP, but trivial block */ - if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { - VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); - pMB->pmvs[0].x = - predMV.x; - pMB->pmvs[0].y = - predMV.y; - } - else { - VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); - pMB->pmvs[0].x = - predMV.x; - pMB->pmvs[0].y = - predMV.y; - } - pMB->mode = MODE_INTER; - pMB->cbp = 0; - MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); - stop_coding_timer(); - - continue; /* next MB */ - } } - /* do SKIP */ - - pMB->mode = MODE_NOT_CODED; - MBSkip(bs); - stop_coding_timer(); - continue; /* next MB */ - } - /* ordinary case: normal coded INTER/INTER4V block */ - - if ((current->vop_flags & XVID_VOP_GREYSCALE)) - { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ - qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ - qcoeff[5*64+0]=0; - } - - if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { - VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); - pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; - pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; - DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); - } else { - VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); - pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; - pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; - DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); - } - - - if (pMB->mode == MODE_INTER4V) - { int k; - for (k=1;k<4;k++) - { - if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { - VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k); - pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; - pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; - DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); - } else { - VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k); - pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; - pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; - DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); - } + if (bSkip) { + /* do SKIP */ + pMB->mode = MODE_NOT_CODED; + MBSkip(bs); + stop_coding_timer(); + continue; /* next MB */ } } + /* ordinary case: normal coded INTER/INTER4V block */ MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); stop_coding_timer(); - } } - if ((current->vop_flags & XVID_VOP_REDUCED)) - { - image_deblock_rrv(¤t->image, pParam->edged_width, - current->mbs, mb_width, mb_height, pParam->mb_width, - 16, 0); - } - emms(); - - if (current->sStat.iMvCount == 0) - current->sStat.iMvCount = 1; - - fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); - - iSearchRange = 1 << (3 + pParam->m_fcode); - - if ((fSigma > iSearchRange / 3) - && (pParam->m_fcode <= (3 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ - { - pParam->m_fcode++; - iSearchRange *= 2; - } else if ((fSigma < iSearchRange / 6) - && (pEnc->fMvPrevSigma >= 0) - && (pEnc->fMvPrevSigma < iSearchRange / 6) - && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ - { - pParam->m_fcode--; - iSearchRange /= 2; - } - - pEnc->fMvPrevSigma = fSigma; + updateFcode(¤t->sStat, pEnc); /* frame drop code */ #if 0 DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); #endif - if (current->sStat.kblks + current->sStat.mblks < - (pParam->frame_drop_ratio * mb_width * mb_height) / 100) + if (current->sStat.kblks + current->sStat.mblks <= + (pParam->frame_drop_ratio * mb_width * mb_height) / 100 && + ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) { current->sStat.kblks = current->sStat.mblks = 0; current->sStat.ublks = mb_width * mb_height; @@ -1875,20 +1776,23 @@ current->rounding_type = reference->rounding_type; current->fcode = reference->fcode; current->bcode = reference->bcode; + current->stamp = reference->stamp; image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); - } - - pEnc->current->is_edged = 0; /* not edged */ - pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ + coded = 0; + + } else { - /* what was this frame's interpolated reference will become - forward (past) reference in b-frame coding */ + pEnc->current->is_edged = 0; /* not edged */ + pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ - image_swap(&pEnc->vInterH, &pEnc->f_refh); - image_swap(&pEnc->vInterV, &pEnc->f_refv); - image_swap(&pEnc->vInterHV, &pEnc->f_refhv); + /* what was this frame's interpolated reference will become + forward (past) reference in b-frame coding */ + image_swap(&pEnc->vInterH, &pEnc->f_refh); + image_swap(&pEnc->vInterV, &pEnc->f_refv); + image_swap(&pEnc->vInterHV, &pEnc->f_refhv); + } /* XXX: debug { @@ -1909,7 +1813,7 @@ current->length = (BitstreamPos(bs) - bits) / 8; - return 0; /* inter */ + return coded; } @@ -1933,8 +1837,6 @@ fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ } - /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ - if (!first){ fp=fopen("C:\\XVIDDBGE.TXT","w"); } @@ -1944,7 +1846,7 @@ if (!pEnc->reference->is_edged) { image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, - pEnc->mbParam.height); + pEnc->mbParam.height, 0); pEnc->current->is_edged = 1; } @@ -1961,7 +1863,7 @@ if (!pEnc->current->is_edged) { image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, - pEnc->mbParam.height); + pEnc->mbParam.height, 0); pEnc->current->is_edged = 1; } @@ -2005,36 +1907,35 @@ if (mb->mode == MODE_NOT_CODED) { if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, - NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0); + NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); } - continue; } - if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { + mb->quant = frame->quant; + + if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { + /* we have to motion-compensate, transfer etc, + because there might be blocks to code */ + MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, - f_ref, &pEnc->f_refh, &pEnc->f_refv, - &pEnc->f_refhv, b_ref, &pEnc->vInterH, - &pEnc->vInterV, &pEnc->vInterHV, - dct_codes); - - if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; - mb->quant = frame->quant; - - if (mb->mode != MODE_DIRECT_NONE_MV) - mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); - - if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) - && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { - mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ - } + f_ref, &pEnc->f_refh, &pEnc->f_refv, + &pEnc->f_refhv, b_ref, &pEnc->vInterH, + &pEnc->vInterV, &pEnc->vInterHV, + dct_codes); + + mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); } + + if (mb->mode == MODE_DIRECT_NO4V) + mb->mode = MODE_DIRECT; - /* keep only bits 5-2 -- Chroma blocks will just be skipped by the - * coding function for BFrames, that's why we don't zero teh DC - * coeffs */ - if ((frame->vop_flags & XVID_VOP_GREYSCALE)) - mb->cbp &= 0x3C; + if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) + mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ + else + if (frame->vop_flags & XVID_VOP_GREYSCALE) + /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ + mb->cbp &= 0x3C; start_timer(); MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, @@ -2045,8 +1946,6 @@ emms(); - /* TODO: dynamic fcode/bcode ??? */ - BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ frame->length = (BitstreamPos(bs) - bits) / 8;