--- trunk/xvidcore/src/encoder.c 2002/07/11 00:15:59 290 +++ trunk/xvidcore/src/encoder.c 2002/09/03 17:25:45 387 @@ -39,10 +39,11 @@ * MinChen * 14.04.2002 added FrameCodeB() * - * $Id: encoder.c,v 1.54 2002-07-11 00:15:59 chenm001 Exp $ + * $Id: encoder.c,v 1.76 2002-09-03 17:25:18 chl Exp $ * ****************************************************************************/ + #include #include #include @@ -55,6 +56,7 @@ #include "image/image.h" #ifdef BFRAMES #include "image/font.h" +#include "motion/sad.h" #endif #include "motion/motion.h" #include "bitstream/cbp.h" @@ -209,10 +211,9 @@ /* 1 keyframe each 10 seconds */ - if (pParam->max_key_interval == 0) + if (pParam->max_key_interval <= 0) pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; - pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); if (pEnc == NULL) return XVID_ERR_MEMORY; @@ -237,6 +238,10 @@ pEnc->mbParam.m_quant_type = H263_QUANT; +#ifdef _SMP + pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS); +#endif + pEnc->sStat.fMvPrevSigma = -1; /* Fill rate control parameters */ @@ -341,6 +346,7 @@ pEnc->global = pParam->global; pEnc->mbParam.max_bframes = pParam->max_bframes; pEnc->bquant_ratio = pParam->bquant_ratio; + pEnc->frame_drop_ratio = pParam->frame_drop_ratio; pEnc->bframes = NULL; if (pEnc->mbParam.max_bframes > 0) { @@ -418,6 +424,8 @@ pEnc->mbParam.m_seconds = 0; pEnc->mbParam.m_ticks = 0; pEnc->m_framenum = 0; + pEnc->last_pframe = 0; + pEnc->last_sync = 0; #endif pParam->handle = (void *) pEnc; @@ -613,17 +621,28 @@ } -#ifdef BFRAMES void inc_frame_num(Encoder * pEnc) { - pEnc->iFrameNum++; pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; +#ifdef BFRAMES + pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; + if (pEnc->mbParam.m_ticks < pEnc->last_sync) + pEnc->mbParam.m_seconds = 1; // more than 1 second since last I or P is not supported. + else + pEnc->mbParam.m_seconds = 0; + + if (pEnc->current->coding_type != B_VOP) + pEnc->last_sync = pEnc->mbParam.m_ticks; +#else + pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; -} + #endif +} + #ifdef BFRAMES void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) @@ -819,7 +838,7 @@ pFrame->intra = 0; - BitstreamPutBits(&bs, 0x7f, 8); + BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); @@ -848,12 +867,12 @@ else pEnc->current->quant = pFrame->quant; - if (pEnc->current->quant < 1) +/* if (pEnc->current->quant < 1) pEnc->current->quant = 1; if (pEnc->current->quant > 31) pEnc->current->quant = 31; - +*/ pEnc->current->global_flags = pFrame->general; pEnc->current->motion_flags = pFrame->motion; @@ -1000,10 +1019,6 @@ * This will be coded as a Bidirectional Frame */ - DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", - pEnc->bframenum_head, pEnc->bframenum_tail, - pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); - if ((pEnc->global & XVID_GLOBAL_DEBUG)) { image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); } @@ -1015,6 +1030,18 @@ } else { pEnc->current->quant = pFrame->bquant; } + if (pEnc->current->quant < 1) + pEnc->current->quant = 1; + + 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); + + /* store frame into bframe buffer & swap ref back to current */ SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); @@ -1029,6 +1056,8 @@ goto bvop_loop; } + pEnc->iFrameNum++; + BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); @@ -1103,10 +1132,8 @@ pEnc->current->global_flags = pFrame->general; pEnc->current->motion_flags = pFrame->motion; -#ifdef BFRAMES pEnc->current->seconds = pEnc->mbParam.m_seconds; pEnc->current->ticks = pEnc->mbParam.m_ticks; -#endif pEnc->mbParam.hint = &pFrame->hint; start_timer(); @@ -1236,12 +1263,8 @@ DEBUG(temp); #endif -#ifdef BFRAMES inc_frame_num(pEnc); -#else pEnc->iFrameNum++; -#endif - stop_global_timer(); write_timer(); @@ -1521,6 +1544,11 @@ stop_prediction_timer(); start_timer(); + if (pEnc->current->global_flags & XVID_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->sStat); stop_coding_timer(); } @@ -1542,6 +1570,7 @@ #define INTRA_THRESHOLD 0.5 +#define BFRAME_SKIP_THRESHHOLD 16 static int FrameCodeP(Encoder * pEnc, @@ -1556,7 +1585,7 @@ DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); int iLimit; - uint32_t x, y; + int x, y, k; int iSearchRange; int bIntra; @@ -1595,20 +1624,18 @@ } else { #ifdef _SMP - if (NUMTHREADS > 1) - bIntra = - SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, - &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, - iLimit); - else + if (pEnc->mbParam.num_threads > 1) + bIntra = + SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, + &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, + iLimit); + else #endif - - bIntra = - MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, - &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, - iLimit); - - + bIntra = + MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, + &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, + iLimit); + } stop_motion_timer(); @@ -1681,12 +1708,60 @@ 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++; - } else { + } else { pEnc->sStat.ublks++; - } + } start_timer(); - MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); + + /* Finished processing the MB, now check if to CODE or SKIP */ + + if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && + pMB->mvs[0].y == 0) { + +/* This is a candidate for SKIPping, but check intermediate B-frames first */ + +#ifdef BFRAMES + int iSAD=BFRAME_SKIP_THRESHHOLD; + int bSkip=1; + + for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) + { + iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, + pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, + pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); + if (iSAD >= BFRAME_SKIP_THRESHHOLD) + { bSkip = 0; + break; + } + } + if (!bSkip) + { + if (pEnc->current->global_flags & XVID_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; + } + MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); + pMB->cbp = 0x80; /* trick! so cbp!=0, but still nothing is written to bs */ + } + else + MBSkip(bs); + +#else + MBSkip(bs); /* without B-frames, no precautions are needed */ + +#endif + + } else { + if (pEnc->current->global_flags & XVID_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; + } + MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); + } + stop_coding_timer(); } } @@ -1720,8 +1795,38 @@ pEnc->sStat.fMvPrevSigma = fSigma; +#ifdef BFRAMES + /* 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 < + (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; + + BitstreamReset(bs); + BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); + + // copy reference frame details into the current frame + pEnc->current->quant = pEnc->reference->quant; + pEnc->current->motion_flags = pEnc->reference->motion_flags; + pEnc->current->rounding_type = pEnc->reference->rounding_type; + pEnc->current->fcode = pEnc->reference->fcode; + pEnc->current->bcode = pEnc->reference->bcode; + image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); + memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); + + } +#endif + *pBits = BitstreamPos(bs) - *pBits; +#ifdef BFRAMES + pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % + (int32_t)pEnc->mbParam.fbase; + pEnc->last_pframe = pEnc->current->ticks; +#endif + return 0; // inter } @@ -1777,7 +1882,10 @@ stop_inter_timer(); start_timer(); - MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, + MotionEstimationBVOP(&pEnc->mbParam, frame, + ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase, + pEnc->time_pp, + pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); @@ -1836,16 +1944,15 @@ mb->quant = frame->quant; mb->cbp = - MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, - qcoeff); + MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); - - if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) - && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { - mb->mode = 5; // skipped + if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) + && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) { + mb->mode = MODE_DIRECT_NONE_MV; // skipped } +/* update predictors for forward and backward vectors */ if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { mb->pmvs[0].x = mb->mvs[0].x - forward.x; mb->pmvs[0].y = mb->mvs[0].y - forward.y; @@ -1859,6 +1966,7 @@ backward.x = mb->b_mvs[0].x; backward.y = mb->b_mvs[0].y; } + // DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y); #ifdef BFRAMES_DEC_DEBUG @@ -1886,3 +1994,18 @@ #endif } #endif + + +/* in case internal output is needed somewhere... */ +/* { + FILE *filehandle; + filehandle=fopen("last-b.pgm","wb"); + if (filehandle) + { + fprintf(filehandle,"P5\n\n"); // + fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); + fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); + fclose(filehandle); + } + } +*/