--- encoder.c 2002/07/21 23:34:07 1.62 +++ encoder.c 2002/08/07 10:09:00 1.73 @@ -39,10 +39,11 @@ * MinChen * 14.04.2002 added FrameCodeB() * - * $Id: encoder.c,v 1.62 2002/07/21 23:34:07 chl Exp $ + * $Id: encoder.c,v 1.73 2002/08/07 10:09:00 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" @@ -422,7 +424,8 @@ pEnc->mbParam.m_seconds = 0; pEnc->mbParam.m_ticks = 0; pEnc->m_framenum = 0; - pEnc->last_pframe = 1; + pEnc->last_pframe = 0; + pEnc->last_sync = 0; #endif pParam->handle = (void *) pEnc; @@ -621,11 +624,23 @@ #ifdef BFRAMES void inc_frame_num(Encoder * pEnc) { - pEnc->iFrameNum++; pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; - pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; + +/* fprintf(stderr, "ENC %c %i:%i %i\n", + pEnc->current->coding_type == I_VOP ? 'I' : pEnc->current->coding_type == P_VOP ? 'P' : 'B', + pEnc->mbParam.m_seconds, pEnc->mbParam.m_ticks,pEnc->last_sync); +*/ + + 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; + } #endif @@ -824,7 +839,7 @@ pFrame->intra = 0; - BitstreamPutBits(&bs, 0x7f, 8); + BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); @@ -1016,13 +1031,13 @@ } else { pEnc->current->quant = pFrame->bquant; } -/* if (pEnc->current->quant < 1) + 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); @@ -1042,6 +1057,8 @@ goto bvop_loop; } + pEnc->iFrameNum++; + BitstreamPad(&bs); pFrame->length = BitstreamLength(&bs); @@ -1251,10 +1268,8 @@ #ifdef BFRAMES inc_frame_num(pEnc); -#else - pEnc->iFrameNum++; #endif - + pEnc->iFrameNum++; stop_global_timer(); write_timer(); @@ -1534,6 +1549,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(); } @@ -1555,6 +1575,7 @@ #define INTRA_THRESHOLD 0.5 +#define BFRAME_SKIP_THRESHHOLD 16 static int FrameCodeP(Encoder * pEnc, @@ -1569,7 +1590,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; @@ -1692,12 +1713,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(); } } @@ -1734,7 +1803,7 @@ #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 <= + 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; @@ -1758,8 +1827,9 @@ *pBits = BitstreamPos(bs) - *pBits; #ifdef BFRAMES - pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase; - pEnc->last_pframe = pEnc->mbParam.m_ticks; + 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 @@ -1818,7 +1888,7 @@ start_timer(); MotionEstimationBVOP(&pEnc->mbParam, frame, - ((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase, + ((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, @@ -1883,12 +1953,12 @@ 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) { + 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; @@ -1902,6 +1972,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 @@ -1929,3 +2000,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); + } + } +*/