--- encoder.c 2003/11/19 15:42:38 1.95.2.54 +++ encoder.c 2004/03/30 12:31:52 1.103 @@ -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.54 2003/11/19 15:42:38 syskin Exp $ + * $Id: encoder.c,v 1.103 2004/03/30 12:31:52 syskin Exp $ * ****************************************************************************/ @@ -123,6 +123,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); @@ -250,6 +253,14 @@ if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) goto xvid_err_memory2; + /* allocate quant matrix memory */ + + pEnc->mbParam.mpeg_quant_matrices = + xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); + + if (pEnc->mbParam.mpeg_quant_matrices == NULL) + goto xvid_err_memory2a; + /* allocate interpolation image memory */ if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { @@ -403,6 +414,7 @@ create->handle = (void *) pEnc; init_timer(); + init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices); return 0; /* ok */ @@ -469,6 +481,8 @@ image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); + xvid_err_memory2a: + xvid_free(pEnc->mbParam.mpeg_quant_matrices); xvid_err_memory2: xvid_free(pEnc->current->mbs); @@ -597,6 +611,8 @@ xvid_free(pEnc->plugins); } + xvid_free(pEnc->mbParam.mpeg_quant_matrices); + if (pEnc->num_plugins>0) xvid_free(pEnc->zones); @@ -639,7 +655,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; @@ -647,7 +663,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; @@ -677,7 +693,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; @@ -727,20 +743,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 */ @@ -776,15 +794,13 @@ frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; } } - frame->mbs[0].quant = data.quant; /* BEFORE2 will not affect the quant in stats */ + frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ } } - - static __inline void inc_frame_num(Encoder * pEnc) { pEnc->current->frame_num = pEnc->m_framenum; @@ -800,6 +816,34 @@ pEnc->m_framenum--; /* debug ticker */ } +static __inline void +MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) +{ + if (pMB->cbp == 0) { + /* we want to code dquant but the quantizer value will not be used yet + let's find out if we can postpone dquant to next MB + */ + if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { + pMB->dquant = 0; /* it's the last MB of all, the easiest case */ + return; + } else { + MACROBLOCK * next = pMB + 1; + const MACROBLOCK * prev = pMB - 1; + if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) + /* mode allows dquant change in the future */ + if (abs(next->quant - prev->quant) <= 2) { + /* quant change is not out of range */ + pMB->quant = prev->quant; + pMB->dquant = 0; + next->dquant = next->quant - prev->quant; + return; + } + } + } + /* couldn't skip this dquant */ + pMB->mode = MODE_INTER_Q; +} + static __inline void @@ -807,20 +851,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; +} /***************************************************************************** @@ -1000,6 +1089,7 @@ 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); @@ -1035,7 +1125,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; @@ -1070,6 +1160,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; @@ -1182,7 +1275,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: @@ -1193,17 +1288,22 @@ 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) - set_intra_matrix(frame->quant_intra_matrix); + set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); if (frame->quant_inter_matrix != NULL) - set_inter_matrix(frame->quant_inter_matrix); + set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); } /* prevent vol/vop misuse */ @@ -1243,7 +1343,11 @@ pEnc->mbParam.edged_width, pEnc->mbParam.height); } - FrameCodeP(pEnc, &bs, 1, 0); + if ( FrameCodeP(pEnc, &bs, 1, 0) == 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; + } } @@ -1344,7 +1448,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(); } @@ -1356,7 +1460,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); @@ -1440,6 +1544,7 @@ MBParam * const pParam = &pEnc->mbParam; int mb_width = pParam->mb_width; int mb_height = pParam->mb_height; + int coded = 1; /* IMAGE *pCurrent = ¤t->image; */ @@ -1455,7 +1560,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; } @@ -1551,7 +1656,7 @@ set_timecodes(current,reference,pParam->fbase); if (vol_header) - { BitstreamWriteVolHeader(bs, &pEnc->mbParam); + { BitstreamWriteVolHeader(bs, &pEnc->mbParam, current); BitstreamPad(bs); } @@ -1603,10 +1708,6 @@ stop_comp_timer(); - if (pMB->dquant != 0) { - pMB->mode = MODE_INTER_Q; - } - pMB->field_pred = 0; if (pMB->mode != MODE_NOT_CODED) @@ -1615,6 +1716,10 @@ dct_codes, qcoeff); } + if (pMB->dquant != 0) + MBSetDquant(pMB, x, y, &pEnc->mbParam); + + 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) { @@ -1767,7 +1872,7 @@ #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 < + if (current->sStat.kblks + current->sStat.mblks <= (pParam->frame_drop_ratio * mb_width * mb_height) / 100) { current->sStat.kblks = current->sStat.mblks = 0; @@ -1784,20 +1889,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 { @@ -1818,7 +1926,7 @@ current->length = (BitstreamPos(bs) - bits) / 8; - return 0; /* inter */ + return coded; } @@ -1853,7 +1961,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; } @@ -1870,7 +1978,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; }