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.128 2006-03-03 11:54:58 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(); |
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 */ |
1709 |
} |
} |
1710 |
} |
} |
1711 |
|
|
1712 |
|
|
1713 |
|
if (pEnc->num_threads > 0) { |
1714 |
|
/* multithreaded motion estimation - dispatch threads */ |
1715 |
|
|
1716 |
|
void * status; |
1717 |
|
int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
1718 |
|
|
1719 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1720 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
1721 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
1722 |
|
pEnc->motionData[k].current = current; |
1723 |
|
pEnc->motionData[k].reference = reference; |
1724 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
1725 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
1726 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
1727 |
|
pEnc->motionData[k].pGMC = &pEnc->vGMC; |
1728 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
1729 |
|
pEnc->motionData[k].start_y = k; |
1730 |
|
/* todo: sort out temp space once and for all */ |
1731 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; |
1732 |
|
} |
1733 |
|
|
1734 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
1735 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
1736 |
|
(void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); |
1737 |
|
} |
1738 |
|
|
1739 |
|
MotionEstimateSMP(&pEnc->motionData[0]); |
1740 |
|
|
1741 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
1742 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
1743 |
|
} |
1744 |
|
|
1745 |
|
current->fcode = 0; |
1746 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1747 |
|
current->sStat.iMvSum += pEnc->motionData[k].mvSum; |
1748 |
|
current->sStat.iMvCount += pEnc->motionData[k].mvCount; |
1749 |
|
if (pEnc->motionData[k].minfcode > current->fcode) |
1750 |
|
current->fcode = pEnc->motionData[k].minfcode; |
1751 |
|
} |
1752 |
|
|
1753 |
|
} else { |
1754 |
|
/* regular ME */ |
1755 |
|
|
1756 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1757 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1758 |
&pEnc->vGMC, 256*4096); |
&pEnc->vGMC, 256*4096); |
1759 |
|
} |
1760 |
|
|
1761 |
stop_motion_timer(); |
stop_motion_timer(); |
1762 |
|
|
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 |
start_timer(); |
start_timer(); |
2009 |
|
if (pEnc->num_threads > 0) { |
2010 |
|
void * status; |
2011 |
|
int k; |
2012 |
|
/* multithreaded motion estimation - dispatch threads */ |
2013 |
|
int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
2014 |
|
|
2015 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2016 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
2017 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
2018 |
|
pEnc->motionData[k].current = frame; |
2019 |
|
pEnc->motionData[k].reference = pEnc->current; |
2020 |
|
pEnc->motionData[k].fRef = f_ref; |
2021 |
|
pEnc->motionData[k].fRefH = &pEnc->f_refh; |
2022 |
|
pEnc->motionData[k].fRefV = &pEnc->f_refv; |
2023 |
|
pEnc->motionData[k].fRefHV = &pEnc->f_refhv; |
2024 |
|
pEnc->motionData[k].pRef = b_ref; |
2025 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
2026 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
2027 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
2028 |
|
pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp); |
2029 |
|
pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp); |
2030 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
2031 |
|
pEnc->motionData[k].start_y = k; |
2032 |
|
/* todo: sort out temp space once and for all */ |
2033 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; |
2034 |
|
} |
2035 |
|
|
2036 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
2037 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
2038 |
|
(void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); |
2039 |
|
} |
2040 |
|
|
2041 |
|
SMPMotionEstimationBVOP(&pEnc->motionData[0]); |
2042 |
|
|
2043 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
2044 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
2045 |
|
} |
2046 |
|
|
2047 |
|
frame->fcode = frame->bcode = 0; |
2048 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2049 |
|
if (pEnc->motionData[k].minfcode > frame->fcode) |
2050 |
|
frame->fcode = pEnc->motionData[k].minfcode; |
2051 |
|
if (pEnc->motionData[k].minbcode > frame->bcode) |
2052 |
|
frame->bcode = pEnc->motionData[k].minbcode; |
2053 |
|
} |
2054 |
|
} else { |
2055 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
2056 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2057 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
2059 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2060 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2061 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
2062 |
|
} |
2063 |
stop_motion_timer(); |
stop_motion_timer(); |
2064 |
|
|
2065 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2117 |
stop_coding_timer(); |
stop_coding_timer(); |
2118 |
} |
} |
2119 |
} |
} |
|
|
|
2120 |
emms(); |
emms(); |
2121 |
|
|
2122 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |