--- encoder.c 2003/11/30 16:13:15 1.95.2.56 +++ encoder.c 2004/07/18 12:02:46 1.107 @@ -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.56 2003/11/30 16:13:15 edgomez Exp $ + * $Id: encoder.c,v 1.107 2004/07/18 12:02:46 edgomez 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 */ @@ -846,20 +860,65 @@ { pCur->ticks = (int32_t)pCur->stamp % time_base; - pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; - - /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ + pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; -/* fprintf(stderr,"WriteVop: %d - %d \n", +#if 0 /* HEAVY DEBUG OUTPUT */ + fprintf(stderr,"WriteVop: %d - %d \n", ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); - fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", + fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", pCur->coding_type, pCur->stamp, pRef->stamp, time_base); - fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", + fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); +#endif +} -*/ +static int +gcd(int a, int b) +{ + int r ; + + if (b > a) { + r = a; + a = b; + b = r; + } + + while ((r = a % b)) { + a = b; + b = r; + } + return b; } +static void +simplify_par(int *par_width, int *par_height) +{ + + int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; + int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; + int divisor = gcd(_par_width, _par_height); + + _par_width /= divisor; + _par_height /= divisor; + + /* 2^8 precision maximum */ + if (_par_width>255 || _par_height>255) { + float div; + emms(); + if (_par_width>_par_height) + div = (float)_par_width/255; + else + div = (float)_par_height/255; + + _par_width = (int)((float)_par_width/div); + _par_height = (int)((float)_par_height/div); + } + + *par_width = _par_width; + *par_height = _par_height; + + return; +} /***************************************************************************** @@ -1039,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) { @@ -1074,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; @@ -1109,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; @@ -1221,7 +1284,9 @@ 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) { case XVID_PAR_11_VGA: case XVID_PAR_43_PAL: @@ -1232,11 +1297,16 @@ pEnc->mbParam.par = frame->par; break; default: - pEnc->mbParam.par = XVID_PAR_EXT; + pEnc->mbParam.par = XVID_PAR_11_VGA; break; } - pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1; - pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1; + + /* For extended PAR only, we try to sanityse/simplify par values */ + if (pEnc->mbParam.par == XVID_PAR_EXT) { + pEnc->mbParam.par_width = frame->par_width; + pEnc->mbParam.par_height = frame->par_height; + simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); + } if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { if (frame->quant_intra_matrix != NULL) @@ -1282,7 +1352,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; + } } @@ -1383,7 +1457,7 @@ start_timer(); image_setedges(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, - pEnc->mbParam.width, pEnc->mbParam.height); + pEnc->mbParam.width, pEnc->mbParam.height, 0); stop_edges_timer(); } @@ -1395,7 +1469,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); @@ -1460,9 +1534,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); @@ -1470,15 +1542,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; */ @@ -1494,7 +1566,7 @@ 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; } @@ -1503,11 +1575,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(); @@ -1578,21 +1645,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); - BitstreamPad(bs); - } BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); @@ -1605,7 +1665,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); @@ -1806,8 +1866,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; @@ -1823,20 +1884,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 { @@ -1857,7 +1921,7 @@ current->length = (BitstreamPos(bs) - bits) / 8; - return 0; /* inter */ + return coded; } @@ -1892,7 +1956,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; } @@ -1909,7 +1973,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; }