3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2002-2010 Michael Militzer <isibaar@xvid.org> |
7 |
* 2002-2003 Peter Ross <pross@xvid.org> |
* 2002-2003 Peter Ross <pross@xvid.org> |
8 |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
9 |
* |
* |
435 |
/* timestamp stuff */ |
/* timestamp stuff */ |
436 |
|
|
437 |
pEnc->mbParam.m_stamp = 0; |
pEnc->mbParam.m_stamp = 0; |
438 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = create->start_frame_num; |
439 |
pEnc->current->stamp = 0; |
pEnc->current->stamp = 0; |
440 |
pEnc->reference->stamp = 0; |
pEnc->reference->stamp = 0; |
441 |
|
|
662 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
663 |
} |
} |
664 |
|
|
665 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) { |
666 |
|
xvid_free(pEnc->temp_lambda); |
667 |
|
} |
668 |
|
|
669 |
if (pEnc->num_plugins>0) { |
if (pEnc->num_plugins>0) { |
670 |
xvid_plg_destroy_t pdestroy; |
xvid_plg_destroy_t pdestroy; |
770 |
|
|
771 |
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
772 |
int block = 0; |
int block = 0; |
773 |
|
emms(); |
774 |
data.lambda = pEnc->temp_lambda; |
data.lambda = pEnc->temp_lambda; |
775 |
for(i = 0;i < pEnc->mbParam.mb_height; i++) |
for(i = 0;i < pEnc->mbParam.mb_height; i++) |
776 |
for(j = 0;j < pEnc->mbParam.mb_width; j++) |
for(j = 0;j < pEnc->mbParam.mb_width; j++) |
997 |
return; |
return; |
998 |
} |
} |
999 |
|
|
|
|
|
1000 |
/***************************************************************************** |
/***************************************************************************** |
1001 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
1002 |
* |
* |
1306 |
if (pEnc->current->stamp > 0) { |
if (pEnc->current->stamp > 0) { |
1307 |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1308 |
} |
} |
1309 |
else |
else if (stats) { |
1310 |
stats->type = XVID_TYPE_NOTHING; |
stats->type = XVID_TYPE_NOTHING; |
1311 |
} |
} |
1312 |
|
} |
1313 |
|
|
1314 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1315 |
* closed-gop |
* closed-gop |
1655 |
|
|
1656 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1657 |
|
|
1658 |
|
if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) { |
1659 |
|
image_block_variance(¤t->image, pParam->edged_width, current->mbs, |
1660 |
|
pParam->mb_width, pParam->mb_height); |
1661 |
|
} |
1662 |
|
|
1663 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1664 |
|
|
1665 |
SetMacroblockQuants(&pEnc->mbParam, current); |
SetMacroblockQuants(&pEnc->mbParam, current); |
1718 |
|
|
1719 |
if (pEnc->num_threads > 0) { |
if (pEnc->num_threads > 0) { |
1720 |
/* multithreaded motion estimation - dispatch threads */ |
/* multithreaded motion estimation - dispatch threads */ |
1721 |
|
|
1722 |
|
void * status; |
1723 |
int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
1724 |
|
|
1725 |
for (k = 0; k < pEnc->num_threads; k++) { |
for (k = 0; k < pEnc->num_threads; k++) { |
1737 |
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; |
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; |
1738 |
} |
} |
1739 |
|
|
1740 |
for (k = 0; k < pEnc->num_threads; k++) { |
for (k = 1; k < pEnc->num_threads; k++) { |
1741 |
pthread_create(&pEnc->motionData[k].handle, NULL, |
pthread_create(&pEnc->motionData[k].handle, NULL, |
1742 |
(void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); |
(void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); |
1743 |
} |
} |
1744 |
|
|
1745 |
|
MotionEstimateSMP(&pEnc->motionData[0]); |
1746 |
|
|
1747 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
1748 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
1749 |
|
} |
1750 |
|
|
1751 |
|
current->fcode = 0; |
1752 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1753 |
|
current->sStat.iMvSum += pEnc->motionData[k].mvSum; |
1754 |
|
current->sStat.iMvCount += pEnc->motionData[k].mvCount; |
1755 |
|
if (pEnc->motionData[k].minfcode > current->fcode) |
1756 |
|
current->fcode = pEnc->motionData[k].minfcode; |
1757 |
|
} |
1758 |
|
|
1759 |
} else { |
} else { |
1760 |
/* regular ME */ |
/* regular ME */ |
1761 |
|
|
1771 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1772 |
|
|
1773 |
for (y = 0; y < mb_height; y++) { |
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(); |
|
|
} |
|
|
|
|
1774 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1775 |
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1776 |
int skip_possible; |
int skip_possible; |
1881 |
} |
} |
1882 |
} |
} |
1883 |
|
|
|
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; |
|
|
} |
|
|
} |
|
|
|
|
1884 |
emms(); |
emms(); |
1885 |
updateFcode(¤t->sStat, pEnc); |
updateFcode(¤t->sStat, pEnc); |
1886 |
|
|
1888 |
#if 0 |
#if 0 |
1889 |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
1890 |
#endif |
#endif |
1891 |
if (current->sStat.kblks + current->sStat.mblks <= |
if (current->sStat.kblks + current->sStat.mblks < |
1892 |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1893 |
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
1894 |
{ |
{ |
2007 |
} |
} |
2008 |
|
|
2009 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
2010 |
|
|
2011 |
|
if (pEnc->current->vop_flags & XVID_VOP_RD_PSNRHVSM) { |
2012 |
|
image_block_variance(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs, |
2013 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height); |
2014 |
|
} |
2015 |
|
|
2016 |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
2017 |
|
|
2018 |
frame->fcode = frame->bcode = pEnc->current->fcode; |
frame->fcode = frame->bcode = pEnc->current->fcode; |
2019 |
|
|
2020 |
|
start_timer(); |
2021 |
if (pEnc->num_threads > 0) { |
if (pEnc->num_threads > 0) { |
2022 |
|
void * status; |
2023 |
int k; |
int k; |
2024 |
/* multithreaded motion estimation - dispatch threads */ |
/* multithreaded motion estimation - dispatch threads */ |
2025 |
int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
2045 |
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; |
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; |
2046 |
} |
} |
2047 |
|
|
2048 |
for (k = 0; k < pEnc->num_threads; k++) { |
for (k = 1; k < pEnc->num_threads; k++) { |
2049 |
pthread_create(&pEnc->motionData[k].handle, NULL, |
pthread_create(&pEnc->motionData[k].handle, NULL, |
2050 |
(void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); |
(void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); |
2051 |
} |
} |
2052 |
|
|
2053 |
|
SMPMotionEstimationBVOP(&pEnc->motionData[0]); |
2054 |
|
|
2055 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
2056 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
2057 |
|
} |
2058 |
|
|
2059 |
|
frame->fcode = frame->bcode = 0; |
2060 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2061 |
|
if (pEnc->motionData[k].minfcode > frame->fcode) |
2062 |
|
frame->fcode = pEnc->motionData[k].minfcode; |
2063 |
|
if (pEnc->motionData[k].minbcode > frame->bcode) |
2064 |
|
frame->bcode = pEnc->motionData[k].minbcode; |
2065 |
|
} |
2066 |
} else { |
} else { |
|
start_timer(); |
|
2067 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
2068 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2069 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
2071 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2072 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2073 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
|
stop_motion_timer(); |
|
2074 |
} |
} |
2075 |
|
stop_motion_timer(); |
2076 |
|
|
2077 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2078 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
2086 |
frame->sStat.kblks = frame->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.ublks = 0; |
2087 |
|
|
2088 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
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(); |
|
|
} |
|
|
|
|
2089 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
2090 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
2091 |
|
|
2129 |
stop_coding_timer(); |
stop_coding_timer(); |
2130 |
} |
} |
2131 |
} |
} |
|
|
|
|
if (pEnc->num_threads > 0) { |
|
|
void * status; |
|
|
int k; |
|
|
for (k = 0; k < pEnc->num_threads; k++) { |
|
|
pthread_join(pEnc->motionData[k].handle, &status); |
|
|
} |
|
|
} |
|
|
|
|
2132 |
emms(); |
emms(); |
2133 |
|
|
2134 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |