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 |
* |
* |
49 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
50 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
51 |
|
|
52 |
|
# include "motion/motion_smp.h" |
53 |
|
|
54 |
|
|
55 |
/***************************************************************************** |
/***************************************************************************** |
56 |
* Local function prototypes |
* Local function prototypes |
57 |
****************************************************************************/ |
****************************************************************************/ |
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 |
|
|
444 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
445 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
446 |
|
|
447 |
|
/* multithreaded stuff */ |
448 |
|
if (create->num_threads > 0) { |
449 |
|
int t = create->num_threads; |
450 |
|
int rows_per_thread = (pEnc->mbParam.mb_height+t-1)/t; |
451 |
|
pEnc->num_threads = t; |
452 |
|
pEnc->motionData = xvid_malloc(t*sizeof(SMPmotionData), CACHE_LINE); |
453 |
|
if (!pEnc->motionData) |
454 |
|
goto xvid_err_nosmp; |
455 |
|
|
456 |
|
for (n = 0; n < t; n++) { |
457 |
|
pEnc->motionData[n].complete_count_self = |
458 |
|
xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE); |
459 |
|
|
460 |
|
if (!pEnc->motionData[n].complete_count_self) |
461 |
|
goto xvid_err_nosmp; |
462 |
|
|
463 |
|
if (n != 0) |
464 |
|
pEnc->motionData[n].complete_count_above = |
465 |
|
pEnc->motionData[n-1].complete_count_self; |
466 |
|
} |
467 |
|
pEnc->motionData[0].complete_count_above = |
468 |
|
pEnc->motionData[t-1].complete_count_self - 1; |
469 |
|
|
470 |
|
} else { |
471 |
|
xvid_err_nosmp: |
472 |
|
/* no SMP */ |
473 |
|
create->num_threads = 0; |
474 |
|
pEnc->motionData = NULL; |
475 |
|
} |
476 |
|
|
477 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
478 |
|
|
479 |
init_timer(); |
init_timer(); |
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; |
683 |
|
|
684 |
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
685 |
|
|
686 |
if (pEnc->num_plugins>0) |
if (pEnc->num_zones > 0) |
687 |
xvid_free(pEnc->zones); |
xvid_free(pEnc->zones); |
688 |
|
|
689 |
|
if (pEnc->num_threads > 0) { |
690 |
|
for (i = 0; i < pEnc->num_threads; i++) |
691 |
|
xvid_free(pEnc->motionData[i].complete_count_self); |
692 |
|
|
693 |
|
xvid_free(pEnc->motionData); |
694 |
|
} |
695 |
|
|
696 |
xvid_free(pEnc); |
xvid_free(pEnc); |
697 |
|
|
698 |
return 0; /* ok */ |
return 0; /* ok */ |
765 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
766 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
767 |
data.dquant_stride = pEnc->mbParam.mb_width; |
data.dquant_stride = pEnc->mbParam.mb_width; |
768 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int)); |
769 |
} |
} |
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); |
1715 |
} |
} |
1716 |
} |
} |
1717 |
|
|
1718 |
|
|
1719 |
|
if (pEnc->num_threads > 0) { |
1720 |
|
/* multithreaded motion estimation - dispatch threads */ |
1721 |
|
|
1722 |
|
void * status; |
1723 |
|
int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
1724 |
|
|
1725 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1726 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
1727 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
1728 |
|
pEnc->motionData[k].current = current; |
1729 |
|
pEnc->motionData[k].reference = reference; |
1730 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
1731 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
1732 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
1733 |
|
pEnc->motionData[k].pGMC = &pEnc->vGMC; |
1734 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
1735 |
|
pEnc->motionData[k].start_y = k; |
1736 |
|
/* todo: sort out temp space once and for all */ |
1737 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; |
1738 |
|
} |
1739 |
|
|
1740 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
1741 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
1742 |
|
(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 { |
1760 |
|
/* regular ME */ |
1761 |
|
|
1762 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1763 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1764 |
&pEnc->vGMC, 256*4096); |
&pEnc->vGMC, 256*4096); |
1765 |
|
} |
1766 |
|
|
1767 |
stop_motion_timer(); |
stop_motion_timer(); |
1768 |
|
|
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; |
2019 |
|
|
2020 |
start_timer(); |
start_timer(); |
2021 |
|
if (pEnc->num_threads > 0) { |
2022 |
|
void * status; |
2023 |
|
int k; |
2024 |
|
/* multithreaded motion estimation - dispatch threads */ |
2025 |
|
int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
2026 |
|
|
2027 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2028 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
2029 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
2030 |
|
pEnc->motionData[k].current = frame; |
2031 |
|
pEnc->motionData[k].reference = pEnc->current; |
2032 |
|
pEnc->motionData[k].fRef = f_ref; |
2033 |
|
pEnc->motionData[k].fRefH = &pEnc->f_refh; |
2034 |
|
pEnc->motionData[k].fRefV = &pEnc->f_refv; |
2035 |
|
pEnc->motionData[k].fRefHV = &pEnc->f_refhv; |
2036 |
|
pEnc->motionData[k].pRef = b_ref; |
2037 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
2038 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
2039 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
2040 |
|
pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp); |
2041 |
|
pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp); |
2042 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
2043 |
|
pEnc->motionData[k].start_y = k; |
2044 |
|
/* todo: sort out temp space once and for all */ |
2045 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; |
2046 |
|
} |
2047 |
|
|
2048 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
2049 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
2050 |
|
(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 { |
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); |
2074 |
|
} |
2075 |
stop_motion_timer(); |
stop_motion_timer(); |
2076 |
|
|
2077 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2129 |
stop_coding_timer(); |
stop_coding_timer(); |
2130 |
} |
} |
2131 |
} |
} |
|
|
|
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() */ |