21 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: encoder.c,v 1.125 2006-02-23 07:22:43 syskin Exp $ |
* $Id: encoder.c,v 1.126 2006-02-24 08:46:22 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
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 |
****************************************************************************/ |
****************************************************************************/ |
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(); |
680 |
|
|
681 |
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
682 |
|
|
683 |
if (pEnc->num_plugins>0) |
if (pEnc->num_zones > 0) |
684 |
xvid_free(pEnc->zones); |
xvid_free(pEnc->zones); |
685 |
|
|
686 |
|
if (pEnc->num_threads > 0) { |
687 |
|
for (i = 0; i < pEnc->num_threads; i++) |
688 |
|
xvid_free(pEnc->motionData[i].complete_count_self); |
689 |
|
|
690 |
|
xvid_free(pEnc->motionData); |
691 |
|
} |
692 |
|
|
693 |
xvid_free(pEnc); |
xvid_free(pEnc); |
694 |
|
|
695 |
return 0; /* ok */ |
return 0; /* ok */ |
1706 |
} |
} |
1707 |
} |
} |
1708 |
|
|
1709 |
|
|
1710 |
|
if (pEnc->num_threads > 0) { |
1711 |
|
/* multithreaded motion estimation - dispatch threads */ |
1712 |
|
int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
1713 |
|
|
1714 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1715 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
1716 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
1717 |
|
pEnc->motionData[k].current = current; |
1718 |
|
pEnc->motionData[k].reference = reference; |
1719 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
1720 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
1721 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
1722 |
|
pEnc->motionData[k].pGMC = &pEnc->vGMC; |
1723 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
1724 |
|
pEnc->motionData[k].start_y = k; |
1725 |
|
/* todo: sort out temp space once and for all */ |
1726 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; |
1727 |
|
} |
1728 |
|
|
1729 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1730 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
1731 |
|
(void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); |
1732 |
|
} |
1733 |
|
} else { |
1734 |
|
/* regular ME */ |
1735 |
|
|
1736 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1737 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1738 |
&pEnc->vGMC, 256*4096); |
&pEnc->vGMC, 256*4096); |
1739 |
|
} |
1740 |
|
|
1741 |
stop_motion_timer(); |
stop_motion_timer(); |
1742 |
|
|
1745 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1746 |
|
|
1747 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1748 |
|
|
1749 |
|
if (pEnc->num_threads > 0) { |
1750 |
|
/* in multithreaded encoding, only proceed with a row of macroblocks |
1751 |
|
if ME finished with that row */ |
1752 |
|
while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != mb_width) |
1753 |
|
sched_yield(); |
1754 |
|
} |
1755 |
|
|
1756 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1757 |
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1758 |
int skip_possible; |
int skip_possible; |
1863 |
} |
} |
1864 |
} |
} |
1865 |
|
|
1866 |
|
if (pEnc->num_threads > 0) { |
1867 |
|
void * status; |
1868 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1869 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
1870 |
|
} |
1871 |
|
|
1872 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1873 |
|
current->sStat.iMvSum += pEnc->motionData[k].mvSum; |
1874 |
|
current->sStat.iMvCount += pEnc->motionData[k].mvCount; |
1875 |
|
} |
1876 |
|
} |
1877 |
|
|
1878 |
emms(); |
emms(); |
1879 |
updateFcode(¤t->sStat, pEnc); |
updateFcode(¤t->sStat, pEnc); |
1880 |
|
|
2003 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
2004 |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
2005 |
|
|
2006 |
|
frame->fcode = frame->bcode = pEnc->current->fcode; |
2007 |
|
|
2008 |
|
if (pEnc->num_threads > 0) { |
2009 |
|
int k; |
2010 |
|
/* multithreaded motion estimation - dispatch threads */ |
2011 |
|
int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
2012 |
|
|
2013 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2014 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
2015 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
2016 |
|
pEnc->motionData[k].current = frame; |
2017 |
|
pEnc->motionData[k].reference = pEnc->current; |
2018 |
|
pEnc->motionData[k].fRef = f_ref; |
2019 |
|
pEnc->motionData[k].fRefH = &pEnc->f_refh; |
2020 |
|
pEnc->motionData[k].fRefV = &pEnc->f_refv; |
2021 |
|
pEnc->motionData[k].fRefHV = &pEnc->f_refhv; |
2022 |
|
pEnc->motionData[k].pRef = b_ref; |
2023 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
2024 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
2025 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
2026 |
|
pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp); |
2027 |
|
pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp); |
2028 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
2029 |
|
pEnc->motionData[k].start_y = k; |
2030 |
|
/* todo: sort out temp space once and for all */ |
2031 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; |
2032 |
|
} |
2033 |
|
|
2034 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2035 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
2036 |
|
(void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); |
2037 |
|
} |
2038 |
|
} else { |
2039 |
start_timer(); |
start_timer(); |
2040 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
2041 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2045 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2046 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
2047 |
stop_motion_timer(); |
stop_motion_timer(); |
2048 |
|
} |
2049 |
|
|
2050 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2051 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
2059 |
frame->sStat.kblks = frame->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.ublks = 0; |
2060 |
|
|
2061 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
2062 |
|
|
2063 |
|
if (pEnc->num_threads > 0) { |
2064 |
|
/* in multithreaded encoding, only proceed with a row of macroblocks |
2065 |
|
if ME finished with that row */ |
2066 |
|
while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != pEnc->mbParam.mb_width) |
2067 |
|
sched_yield(); |
2068 |
|
} |
2069 |
|
|
2070 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
2071 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
2072 |
|
|
2111 |
} |
} |
2112 |
} |
} |
2113 |
|
|
2114 |
|
if (pEnc->num_threads > 0) { |
2115 |
|
void * status; |
2116 |
|
int k; |
2117 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2118 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
2119 |
|
} |
2120 |
|
} |
2121 |
|
|
2122 |
emms(); |
emms(); |
2123 |
|
|
2124 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |