--- encoder.c 2006/02/24 08:46:22 1.126 +++ encoder.c 2010/11/23 11:00:35 1.132 @@ -3,9 +3,9 @@ * XVID MPEG-4 VIDEO CODEC * - Encoder main module - * - * Copyright(C) 2002 Michael Militzer - * 2002-2003 Peter Ross - * 2002 Daniel Smith + * Copyright(C) 2002-2010 Michael Militzer + * 2002-2003 Peter Ross + * 2002 Daniel Smith * * This program is free software ; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -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.126 2006/02/24 08:46:22 syskin Exp $ + * $Id: encoder.c,v 1.132 2010/11/23 11:00:35 Isibaar Exp $ * ****************************************************************************/ @@ -435,7 +435,7 @@ /* timestamp stuff */ pEnc->mbParam.m_stamp = 0; - pEnc->m_framenum = 0; + pEnc->m_framenum = create->start_frame_num; pEnc->current->stamp = 0; pEnc->reference->stamp = 0; @@ -662,6 +662,9 @@ xvid_free(pEnc->temp_dquants); } + if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) { + xvid_free(pEnc->temp_lambda); + } if (pEnc->num_plugins>0) { xvid_plg_destroy_t pdestroy; @@ -767,6 +770,7 @@ if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { int block = 0; + emms(); data.lambda = pEnc->temp_lambda; for(i = 0;i < pEnc->mbParam.mb_height; i++) for(j = 0;j < pEnc->mbParam.mb_width; j++) @@ -1303,8 +1307,9 @@ if (pEnc->current->stamp > 0) { call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); } - else - stats->type = XVID_TYPE_NOTHING; + else if (stats) { + stats->type = XVID_TYPE_NOTHING; + } } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -1709,6 +1714,8 @@ if (pEnc->num_threads > 0) { /* multithreaded motion estimation - dispatch threads */ + + void * status; int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; for (k = 0; k < pEnc->num_threads; k++) { @@ -1726,10 +1733,25 @@ pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; } - for (k = 0; k < pEnc->num_threads; k++) { + for (k = 1; k < pEnc->num_threads; k++) { pthread_create(&pEnc->motionData[k].handle, NULL, (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); } + + MotionEstimateSMP(&pEnc->motionData[0]); + + for (k = 1; k < pEnc->num_threads; k++) { + pthread_join(pEnc->motionData[k].handle, &status); + } + + current->fcode = 0; + for (k = 0; k < pEnc->num_threads; k++) { + current->sStat.iMvSum += pEnc->motionData[k].mvSum; + current->sStat.iMvCount += pEnc->motionData[k].mvCount; + if (pEnc->motionData[k].minfcode > current->fcode) + current->fcode = pEnc->motionData[k].minfcode; + } + } else { /* regular ME */ @@ -1745,14 +1767,6 @@ BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); for (y = 0; y < mb_height; y++) { - - if (pEnc->num_threads > 0) { - /* in multithreaded encoding, only proceed with a row of macroblocks - if ME finished with that row */ - while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != mb_width) - sched_yield(); - } - for (x = 0; x < mb_width; x++) { MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; int skip_possible; @@ -1863,18 +1877,6 @@ } } - if (pEnc->num_threads > 0) { - void * status; - for (k = 0; k < pEnc->num_threads; k++) { - pthread_join(pEnc->motionData[k].handle, &status); - } - - for (k = 0; k < pEnc->num_threads; k++) { - current->sStat.iMvSum += pEnc->motionData[k].mvSum; - current->sStat.iMvCount += pEnc->motionData[k].mvCount; - } - } - emms(); updateFcode(¤t->sStat, pEnc); @@ -1882,7 +1884,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 && ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) { @@ -2005,7 +2007,9 @@ frame->fcode = frame->bcode = pEnc->current->fcode; + start_timer(); if (pEnc->num_threads > 0) { + void * status; int k; /* multithreaded motion estimation - dispatch threads */ int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; @@ -2031,12 +2035,25 @@ pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; } - for (k = 0; k < pEnc->num_threads; k++) { + for (k = 1; k < pEnc->num_threads; k++) { pthread_create(&pEnc->motionData[k].handle, NULL, (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); } + + SMPMotionEstimationBVOP(&pEnc->motionData[0]); + + for (k = 1; k < pEnc->num_threads; k++) { + pthread_join(pEnc->motionData[k].handle, &status); + } + + frame->fcode = frame->bcode = 0; + for (k = 0; k < pEnc->num_threads; k++) { + if (pEnc->motionData[k].minfcode > frame->fcode) + frame->fcode = pEnc->motionData[k].minfcode; + if (pEnc->motionData[k].minbcode > frame->bcode) + frame->bcode = pEnc->motionData[k].minbcode; + } } else { - start_timer(); MotionEstimationBVOP(&pEnc->mbParam, frame, ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ @@ -2044,8 +2061,8 @@ &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, pEnc->current, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); - stop_motion_timer(); } + stop_motion_timer(); set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); @@ -2059,14 +2076,6 @@ frame->sStat.kblks = frame->sStat.ublks = 0; for (y = 0; y < pEnc->mbParam.mb_height; y++) { - - if (pEnc->num_threads > 0) { - /* in multithreaded encoding, only proceed with a row of macroblocks - if ME finished with that row */ - while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != pEnc->mbParam.mb_width) - sched_yield(); - } - for (x = 0; x < pEnc->mbParam.mb_width; x++) { MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; @@ -2110,15 +2119,6 @@ stop_coding_timer(); } } - - if (pEnc->num_threads > 0) { - void * status; - int k; - for (k = 0; k < pEnc->num_threads; k++) { - pthread_join(pEnc->motionData[k].handle, &status); - } - } - emms(); BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */