--- encoder.c 2002/11/07 10:28:15 1.76.2.16 +++ encoder.c 2002/12/08 06:43:34 1.76.2.25 @@ -39,7 +39,7 @@ * MinChen * 14.04.2002 added FrameCodeB() * - * $Id: encoder.c,v 1.76.2.16 2002/11/07 10:28:15 suxen_drol Exp $ + * $Id: encoder.c,v 1.76.2.25 2002/12/08 06:43:34 suxen_drol Exp $ * ****************************************************************************/ @@ -224,7 +224,7 @@ pEnc->mbParam.m_quant_type = H263_QUANT; - pEnc->sStat.fMvPrevSigma = -1; + pEnc->fMvPrevSigma = -1; /* Fill rate control parameters */ @@ -327,6 +327,7 @@ pEnc->global = pParam->global; pEnc->mbParam.max_bframes = pParam->max_bframes; pEnc->bquant_ratio = pParam->bquant_ratio; + pEnc->bquant_offset = pParam->bquant_offset; pEnc->frame_drop_ratio = pParam->frame_drop_ratio; pEnc->bframes = NULL; @@ -666,6 +667,7 @@ uint32_t bits, mode; int input_valid = 1; + int bframes_count = 0; #ifdef _DEBUG_PSNR float psnr; @@ -706,6 +708,7 @@ SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); FrameCodeP(pEnc, &bs, &bits, 1, 0); + bframes_count = 0; BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); @@ -726,7 +729,7 @@ BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); - pFrame->intra = 0; + pFrame->intra = 2; if (input_valid) queue_image(pEnc, pFrame); @@ -761,7 +764,7 @@ pEnc->current->seconds = tmp; pFrame->length = BitstreamLength(&bs); - pFrame->intra = 0; + pFrame->intra = 4; if (input_valid) queue_image(pEnc, pFrame); @@ -837,7 +840,7 @@ pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); BitstreamPutBits(&bs, 0x7f, 8); - pFrame->intra = 0; + pFrame->intra = 5; } pFrame->length = BitstreamLength(&bs); @@ -923,20 +926,33 @@ /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * ivop/pvop/bvop selection * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ - + pEnc->iFrameNum++; if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval) - || /*image_mad(&pEnc->reference->image, &pEnc->current->image, - pEnc->mbParam.edged_width, pEnc->mbParam.width, - pEnc->mbParam.height) > 30) {*/ - 2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, - &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { + || 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current, + &pEnc->mbParam, pEnc->iMaxKeyInterval, + (pFrame->intra < 0) ? pEnc->iFrameNum : 0, + bframes_count++))) { /* * This will be coded as an Intra Frame */ + if ((pEnc->current->global_flags & XVID_QUARTERPEL)) + pEnc->mbParam.m_quarterpel = 1; + else + pEnc->mbParam.m_quarterpel = 0; + + if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT; + + if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { + if (pFrame->quant_intra_matrix != NULL) + set_intra_matrix(pFrame->quant_intra_matrix); + if (pFrame->quant_inter_matrix != NULL) + set_inter_matrix(pFrame->quant_inter_matrix); + } + DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", pEnc->bframenum_head, pEnc->bframenum_tail, @@ -958,12 +974,14 @@ image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); } FrameCodeP(pEnc, &bs, &bits, 1, 0); + bframes_count = 0; pFrame->intra = 0; } else { FrameCodeI(pEnc, &bs, &bits); + bframes_count = 0; pFrame->intra = 1; pEnc->bframenum_dx50bvop = -1; @@ -982,6 +1000,7 @@ * go screwy with divx 5.00 */ } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { +// } else if (pFrame->intra == 0 || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { /* * This will be coded as a Predicted Frame */ @@ -995,10 +1014,11 @@ } FrameCodeP(pEnc, &bs, &bits, 1, 0); + bframes_count = 0; pFrame->intra = 0; pEnc->flush_bframes = 1; - if ((pEnc->global & XVID_GLOBAL_PACKED)) { + if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { BitstreamPadAlways(&bs); input_valid = 0; goto ipvop_loop; @@ -1014,24 +1034,21 @@ } if (pFrame->bquant < 1) { - pEnc->current->quant = - ((pEnc->reference->quant + - pEnc->current->quant) * pEnc->bquant_ratio) / 200; + pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * + pEnc->bquant_ratio) / 2) + pEnc->bquant_offset)/100; + } else { pEnc->current->quant = pFrame->bquant; } - if (pEnc->current->quant < 1) - pEnc->current->quant = 1; - if (pEnc->current->quant > 31) - pEnc->current->quant = 31; + if (pEnc->current->quant < 1) + pEnc->current->quant = 1; + else if (pEnc->current->quant > 31) + pEnc->current->quant = 31; - - DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", - pEnc->bframenum_head, pEnc->bframenum_tail, - pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); - - + DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", + pEnc->bframenum_head, pEnc->bframenum_tail, + pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); /* store frame into bframe buffer & swap ref back to current */ SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); @@ -1039,24 +1056,23 @@ pEnc->bframenum_tail++; - pFrame->intra = 0; +// bframe report by koepi + pFrame->intra = 2; pFrame->length = 0; input_valid = 0; goto bvop_loop; } - pEnc->iFrameNum++; - BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); if (pResult) { pResult->quant = pEnc->current->quant; - pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); - pResult->kblks = pEnc->sStat.kblks; - pResult->mblks = pEnc->sStat.mblks; - pResult->ublks = pEnc->sStat.ublks; + pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); + pResult->kblks = pEnc->current->sStat.kblks; + pResult->mblks = pEnc->current->sStat.mblks; + pResult->ublks = pEnc->current->sStat.ublks; } emms(); @@ -1241,10 +1257,10 @@ if (pResult) { pResult->quant = pEnc->current->quant; - pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); - pResult->kblks = pEnc->sStat.kblks; - pResult->mblks = pEnc->sStat.mblks; - pResult->ublks = pEnc->sStat.ublks; + pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); + pResult->kblks = pEnc->current->sStat.kblks; + pResult->mblks = pEnc->current->sStat.mblks; + pResult->ublks = pEnc->current->sStat.ublks; } emms(); @@ -1529,9 +1545,9 @@ *pBits = BitstreamPos(bs); - pEnc->sStat.iTextBits = 0; - pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; - pEnc->sStat.mblks = pEnc->sStat.ublks = 0; + pEnc->current->sStat.iTextBits = 0; + pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; + pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; for (y = 0; y < pEnc->mbParam.mb_height; y++) for (x = 0; x < pEnc->mbParam.mb_width; x++) { @@ -1553,16 +1569,14 @@ 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->sStat); + MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); stop_coding_timer(); } emms(); *pBits = BitstreamPos(bs) - *pBits; - pEnc->sStat.fMvPrevSigma = -1; - pEnc->sStat.iMvSum = 0; - pEnc->sStat.iMvCount = 0; + pEnc->fMvPrevSigma = -1; pEnc->mbParam.m_fcode = 2; if (pEnc->current->global_flags & XVID_HINTEDME_GET) { @@ -1663,8 +1677,8 @@ *pBits = BitstreamPos(bs); - pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = - pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; + pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = + pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; for (y = 0; y < pEnc->mbParam.mb_height; y++) { for (x = 0; x < pEnc->mbParam.mb_width; x++) { @@ -1714,13 +1728,13 @@ stop_prediction_timer(); if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { - pEnc->sStat.kblks++; + pEnc->current->sStat.kblks++; } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { - pEnc->sStat.mblks++; + pEnc->current->sStat.mblks++; } else { - pEnc->sStat.ublks++; + pEnc->current->sStat.ublks++; } start_timer(); @@ -1770,7 +1784,7 @@ } pMB->mode = MODE_INTER; pMB->cbp = 0; - MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); + MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); } else { @@ -1784,7 +1798,7 @@ qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ qcoeff[5*64+0]=0; } - MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); + MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); } stop_coding_timer(); @@ -1797,10 +1811,10 @@ HintedMEGet(pEnc, 0); } - if (pEnc->sStat.iMvCount == 0) - pEnc->sStat.iMvCount = 1; + if (pEnc->current->sStat.iMvCount == 0) + pEnc->current->sStat.iMvCount = 1; - fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); + fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); @@ -1810,24 +1824,23 @@ pEnc->mbParam.m_fcode++; iSearchRange *= 2; } else if ((fSigma < iSearchRange / 6) - && (pEnc->sStat.fMvPrevSigma >= 0) - && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) + && (pEnc->fMvPrevSigma >= 0) + && (pEnc->fMvPrevSigma < iSearchRange / 6) && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 { pEnc->mbParam.m_fcode--; iSearchRange /= 2; } - pEnc->sStat.fMvPrevSigma = fSigma; + pEnc->fMvPrevSigma = fSigma; -#ifdef FRAMEDROP /* frame drop code */ - // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); - if (pEnc->sStat.kblks + pEnc->sStat.mblks < + // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); + if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) { - pEnc->sStat.kblks = pEnc->sStat.mblks = 0; - pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; + pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; + pEnc->current->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; BitstreamReset(bs); @@ -1845,7 +1858,6 @@ memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); } -#endif *pBits = BitstreamPos(bs) - *pBits; @@ -1853,7 +1865,7 @@ } -static __inline void +static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, @@ -1879,7 +1891,7 @@ #endif frame->quarterpel = pEnc->mbParam.m_quarterpel; - + // forward image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, @@ -1907,7 +1919,7 @@ ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, - pEnc->current->mbs, b_ref, &pEnc->vInterH, + pEnc->current, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); @@ -1925,10 +1937,10 @@ *pBits = BitstreamPos(bs); - pEnc->sStat.iTextBits = 0; - pEnc->sStat.iMvSum = 0; - pEnc->sStat.iMvCount = 0; - pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; + frame->sStat.iTextBits = 0; + frame->sStat.iMvSum = 0; + frame->sStat.iMvCount = 0; + frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; for (y = 0; y < pEnc->mbParam.mb_height; y++) { @@ -1966,7 +1978,7 @@ #endif start_timer(); MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, - &pEnc->sStat, direction); + &frame->sStat, direction); stop_coding_timer(); } }