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.123 2006-01-19 22:25:18 Isibaar Exp $ |
* $Id: encoder.c,v 1.127 2006-02-24 14:18:59 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 */ |
762 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
763 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
764 |
data.dquant_stride = pEnc->mbParam.mb_width; |
data.dquant_stride = pEnc->mbParam.mb_width; |
765 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int)); |
766 |
} |
} |
767 |
|
|
768 |
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
1424 |
|
|
1425 |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1426 |
/* N-VOP, we mustn't code b-frames yet */ |
/* N-VOP, we mustn't code b-frames yet */ |
1427 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || |
1428 |
|
pEnc->mbParam.max_bframes == 0) |
1429 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1430 |
goto done; |
goto done; |
1431 |
} |
} |
1706 |
} |
} |
1707 |
} |
} |
1708 |
|
|
1709 |
|
|
1710 |
|
if (pEnc->num_threads > 0) { |
1711 |
|
/* multithreaded motion estimation - dispatch threads */ |
1712 |
|
|
1713 |
|
void * status; |
1714 |
|
int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
1715 |
|
|
1716 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1717 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
1718 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
1719 |
|
pEnc->motionData[k].current = current; |
1720 |
|
pEnc->motionData[k].reference = reference; |
1721 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
1722 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
1723 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
1724 |
|
pEnc->motionData[k].pGMC = &pEnc->vGMC; |
1725 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
1726 |
|
pEnc->motionData[k].start_y = k; |
1727 |
|
/* todo: sort out temp space once and for all */ |
1728 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; |
1729 |
|
} |
1730 |
|
|
1731 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
1732 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
1733 |
|
(void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); |
1734 |
|
} |
1735 |
|
|
1736 |
|
MotionEstimateSMP(&pEnc->motionData[0]); |
1737 |
|
|
1738 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
1739 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
1740 |
|
} |
1741 |
|
|
1742 |
|
current->fcode = 0; |
1743 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
1744 |
|
current->sStat.iMvSum += pEnc->motionData[k].mvSum; |
1745 |
|
current->sStat.iMvCount += pEnc->motionData[k].mvCount; |
1746 |
|
if (pEnc->motionData[k].minfcode > current->fcode) |
1747 |
|
current->fcode = pEnc->motionData[k].minfcode; |
1748 |
|
} |
1749 |
|
|
1750 |
|
} else { |
1751 |
|
/* regular ME */ |
1752 |
|
|
1753 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1754 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1755 |
&pEnc->vGMC, 256*4096); |
&pEnc->vGMC, 256*4096); |
1756 |
|
} |
1757 |
|
|
1758 |
stop_motion_timer(); |
stop_motion_timer(); |
1759 |
|
|
1883 |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1884 |
( (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)) ) |
1885 |
{ |
{ |
1886 |
current->sStat.kblks = current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0; |
1887 |
current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1888 |
|
|
1889 |
BitstreamReset(bs); |
BitstreamReset(bs); |
2000 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
2001 |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
2002 |
|
|
2003 |
|
frame->fcode = frame->bcode = pEnc->current->fcode; |
2004 |
|
|
2005 |
start_timer(); |
start_timer(); |
2006 |
|
if (pEnc->num_threads > 0) { |
2007 |
|
void * status; |
2008 |
|
int k; |
2009 |
|
/* multithreaded motion estimation - dispatch threads */ |
2010 |
|
int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; |
2011 |
|
|
2012 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2013 |
|
memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); |
2014 |
|
pEnc->motionData[k].pParam = &pEnc->mbParam; |
2015 |
|
pEnc->motionData[k].current = frame; |
2016 |
|
pEnc->motionData[k].reference = pEnc->current; |
2017 |
|
pEnc->motionData[k].fRef = f_ref; |
2018 |
|
pEnc->motionData[k].fRefH = &pEnc->f_refh; |
2019 |
|
pEnc->motionData[k].fRefV = &pEnc->f_refv; |
2020 |
|
pEnc->motionData[k].fRefHV = &pEnc->f_refhv; |
2021 |
|
pEnc->motionData[k].pRef = b_ref; |
2022 |
|
pEnc->motionData[k].pRefH = &pEnc->vInterH; |
2023 |
|
pEnc->motionData[k].pRefV = &pEnc->vInterV; |
2024 |
|
pEnc->motionData[k].pRefHV = &pEnc->vInterHV; |
2025 |
|
pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp); |
2026 |
|
pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp); |
2027 |
|
pEnc->motionData[k].y_step = pEnc->num_threads; |
2028 |
|
pEnc->motionData[k].start_y = k; |
2029 |
|
/* todo: sort out temp space once and for all */ |
2030 |
|
pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; |
2031 |
|
} |
2032 |
|
|
2033 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
2034 |
|
pthread_create(&pEnc->motionData[k].handle, NULL, |
2035 |
|
(void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); |
2036 |
|
} |
2037 |
|
|
2038 |
|
SMPMotionEstimationBVOP(&pEnc->motionData[0]); |
2039 |
|
|
2040 |
|
for (k = 1; k < pEnc->num_threads; k++) { |
2041 |
|
pthread_join(pEnc->motionData[k].handle, &status); |
2042 |
|
} |
2043 |
|
|
2044 |
|
frame->fcode = frame->bcode = 0; |
2045 |
|
for (k = 0; k < pEnc->num_threads; k++) { |
2046 |
|
if (pEnc->motionData[k].minfcode > frame->fcode) |
2047 |
|
frame->fcode = pEnc->motionData[k].minfcode; |
2048 |
|
if (pEnc->motionData[k].minbcode > frame->bcode) |
2049 |
|
frame->bcode = pEnc->motionData[k].minbcode; |
2050 |
|
} |
2051 |
|
} else { |
2052 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
2053 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2054 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
2056 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2057 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2058 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
2059 |
|
} |
2060 |
stop_motion_timer(); |
stop_motion_timer(); |
2061 |
|
|
2062 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2114 |
stop_coding_timer(); |
stop_coding_timer(); |
2115 |
} |
} |
2116 |
} |
} |
|
|
|
2117 |
emms(); |
emms(); |
2118 |
|
|
2119 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |