--- trunk/xvidcore/src/encoder.c 2004/03/22 22:36:25 1382 +++ trunk/xvidcore/src/encoder.c 2004/04/04 11:47:21 1406 @@ -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.102 2004-03-22 22:36:23 edgomez Exp $ + * $Id: encoder.c,v 1.105 2004-04-04 11:47:21 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, @@ -123,6 +121,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); @@ -1086,8 +1087,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) { @@ -1339,7 +1341,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; + } } @@ -1517,9 +1523,7 @@ /* 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); @@ -1527,15 +1531,15 @@ 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; + int 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; */ @@ -1560,11 +1564,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(); @@ -1635,21 +1634,14 @@ } } - 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, current); - BitstreamPad(bs); - } BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); @@ -1662,7 +1654,7 @@ MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; - bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); + int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); if (bIntra) { CodeIntraMB(pEnc, pMB); @@ -1863,8 +1855,9 @@ #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; @@ -1880,20 +1873,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 { @@ -1914,7 +1910,7 @@ current->length = (BitstreamPos(bs) - bits) / 8; - return 0; /* inter */ + return coded; }