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 |
****************************************************************************/ |
****************************************************************************/ |
88 |
/* |
/* |
89 |
* Simplify the "fincr/fbase" fraction |
* Simplify the "fincr/fbase" fraction |
90 |
*/ |
*/ |
91 |
|
static int |
92 |
|
gcd(int a, int b) |
93 |
|
{ |
94 |
|
int r ; |
95 |
|
|
96 |
|
if (b > a) { |
97 |
|
r = a; |
98 |
|
a = b; |
99 |
|
b = r; |
100 |
|
} |
101 |
|
|
102 |
|
while ((r = a % b)) { |
103 |
|
a = b; |
104 |
|
b = r; |
105 |
|
} |
106 |
|
return b; |
107 |
|
} |
108 |
|
|
109 |
static void |
static void |
110 |
simplify_time(int *inc, int *base) |
simplify_time(int *inc, int *base) |
111 |
{ |
{ |
112 |
/* common factor */ |
/* common factor */ |
113 |
int i = *inc; |
const int s = gcd(*inc, *base); |
114 |
while (i > 1) { |
*inc /= s; |
115 |
if (*inc % i == 0 && *base % i == 0) { |
*base /= s; |
|
*inc /= i; |
|
|
*base /= i; |
|
|
i = *inc; |
|
|
continue; |
|
|
} |
|
|
i--; |
|
|
} |
|
116 |
|
|
117 |
if (*base > 65535 || *inc > 65535) { |
if (*base > 65535 || *inc > 65535) { |
118 |
int *biggest; |
int *biggest; |
128 |
} |
} |
129 |
|
|
130 |
div = ((float)*biggest)/((float)65535); |
div = ((float)*biggest)/((float)65535); |
131 |
*biggest = (int)(((float)*biggest)/div); |
*biggest = (unsigned int)(((float)*biggest)/div); |
132 |
*other = (int)(((float)*other)/div); |
*other = (unsigned int)(((float)*other)/div); |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
160 |
|
|
161 |
/* global flags */ |
/* global flags */ |
162 |
pEnc->mbParam.global_flags = create->global; |
pEnc->mbParam.global_flags = create->global; |
163 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED)) |
164 |
|
pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA; |
165 |
|
|
166 |
/* width, height */ |
/* width, height */ |
167 |
pEnc->mbParam.width = create->width; |
pEnc->mbParam.width = create->width; |
175 |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
176 |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
177 |
if (pEnc->mbParam.fincr>0) |
if (pEnc->mbParam.fincr>0) |
178 |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase); |
179 |
|
|
180 |
/* zones */ |
/* zones */ |
181 |
if(create->num_zones > 0) { |
if(create->num_zones > 0) { |
206 |
|
|
207 |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
208 |
pinfo.version = XVID_VERSION; |
pinfo.version = XVID_VERSION; |
209 |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) { |
210 |
pEnc->mbParam.plugin_flags |= pinfo.flags; |
pEnc->mbParam.plugin_flags |= pinfo.flags; |
211 |
} |
} |
212 |
|
|
223 |
pcreate.param = create->plugins[n].param; |
pcreate.param = create->plugins[n].param; |
224 |
|
|
225 |
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
226 |
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
227 |
pEnc->plugins[n].func = create->plugins[n].func; |
pEnc->plugins[n].func = create->plugins[n].func; |
228 |
} |
} |
229 |
} |
} |
241 |
goto xvid_err_memory1a; |
goto xvid_err_memory1a; |
242 |
} |
} |
243 |
|
|
244 |
|
/* temp lambdas */ |
245 |
|
if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
246 |
|
pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width * |
247 |
|
pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE); |
248 |
|
if (pEnc->temp_lambda == NULL) |
249 |
|
goto xvid_err_memory1a; |
250 |
|
} |
251 |
|
|
252 |
/* bframes */ |
/* bframes */ |
253 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
254 |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
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(); |
560 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
561 |
} |
} |
562 |
|
|
563 |
|
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
564 |
|
xvid_free(pEnc->temp_lambda); |
565 |
|
} |
566 |
|
|
567 |
xvid_err_memory0: |
xvid_err_memory0: |
568 |
for (n=0; n<pEnc->num_plugins;n++) { |
for (n=0; n<pEnc->num_plugins;n++) { |
569 |
if (pEnc->plugins[n].func) { |
if (pEnc->plugins[n].func) { |
570 |
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0); |
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL); |
571 |
} |
} |
572 |
} |
} |
573 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
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; |
675 |
|
|
676 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
677 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
678 |
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0); |
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL); |
679 |
} |
} |
680 |
} |
} |
681 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
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 */ |
706 |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
707 |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
708 |
{ |
{ |
709 |
unsigned int i, j; |
unsigned int i, j, k; |
710 |
xvid_plg_data_t data; |
xvid_plg_data_t data; |
711 |
|
|
712 |
/* set data struct */ |
/* set data struct */ |
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) { |
772 |
|
int block = 0; |
773 |
|
emms(); |
774 |
|
data.lambda = pEnc->temp_lambda; |
775 |
|
for(i = 0;i < pEnc->mbParam.mb_height; i++) |
776 |
|
for(j = 0;j < pEnc->mbParam.mb_width; j++) |
777 |
|
for (k = 0; k < 6; k++) |
778 |
|
data.lambda[block++] = 1.0f; |
779 |
} |
} |
780 |
|
|
781 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
852 |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
853 |
emms(); |
emms(); |
854 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
855 |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) { |
856 |
continue; |
continue; |
857 |
} |
} |
858 |
} |
} |
881 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
882 |
} |
} |
883 |
} |
} |
884 |
|
|
885 |
|
if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
886 |
|
for (j = 0; j < pEnc->mbParam.mb_height; j++) |
887 |
|
for (i = 0; i < pEnc->mbParam.mb_width; i++) |
888 |
|
for (k = 0; k < 6; k++) { |
889 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = |
890 |
|
(int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]); |
891 |
|
} |
892 |
|
} else { |
893 |
|
for (j = 0; j<pEnc->mbParam.mb_height; j++) |
894 |
|
for (i = 0; i<pEnc->mbParam.mb_width; i++) |
895 |
|
for (k = 0; k < 6; k++) { |
896 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP; |
897 |
|
} |
898 |
|
} |
899 |
|
|
900 |
|
|
901 |
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
902 |
} |
} |
903 |
|
|
967 |
#endif |
#endif |
968 |
} |
} |
969 |
|
|
|
static int |
|
|
gcd(int a, int b) |
|
|
{ |
|
|
int r ; |
|
|
|
|
|
if (b > a) { |
|
|
r = a; |
|
|
a = b; |
|
|
b = r; |
|
|
} |
|
|
|
|
|
while ((r = a % b)) { |
|
|
a = b; |
|
|
b = r; |
|
|
} |
|
|
return b; |
|
|
} |
|
|
|
|
970 |
static void |
static void |
971 |
simplify_par(int *par_width, int *par_height) |
simplify_par(int *par_width, int *par_height) |
972 |
{ |
{ |
997 |
return; |
return; |
998 |
} |
} |
999 |
|
|
|
|
|
1000 |
/***************************************************************************** |
/***************************************************************************** |
1001 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
1002 |
* |
* |
1091 |
} |
} |
1092 |
|
|
1093 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
1094 |
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats); |
1095 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
1096 |
|
|
1097 |
goto done; |
goto done; |
1123 |
|
|
1124 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
1125 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
1126 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1127 |
|
|
1128 |
/* flush complete: reset counters */ |
/* flush complete: reset counters */ |
1129 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
1151 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1152 |
|
|
1153 |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
1154 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1155 |
} |
} |
1156 |
|
|
1157 |
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
1180 |
|
|
1181 |
|
|
1182 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1183 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1184 |
}else{ |
}else{ |
1185 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1186 |
goto done; |
goto done; |
1229 |
type = frame->type; |
type = frame->type; |
1230 |
pEnc->current->quant = frame->quant; |
pEnc->current->quant = frame->quant; |
1231 |
|
|
1232 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats); |
1233 |
|
|
1234 |
if (type > 0){ /* XVID_TYPE_?VOP */ |
if (type > 0){ /* XVID_TYPE_?VOP */ |
1235 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1304 |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) |
1305 |
{ |
{ |
1306 |
if (pEnc->current->stamp > 0) { |
if (pEnc->current->stamp > 0) { |
1307 |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, 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 |
1333 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1334 |
|
|
1335 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1336 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP"); |
1337 |
} |
} |
1338 |
|
|
1339 |
/* convert B-VOP quant to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1428 |
|
|
1429 |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1430 |
/* N-VOP, we mustn't code b-frames yet */ |
/* N-VOP, we mustn't code b-frames yet */ |
1431 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || |
1432 |
|
pEnc->mbParam.max_bframes == 0) |
1433 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1434 |
goto done; |
goto done; |
1435 |
} |
} |
1436 |
} |
} |
1451 |
|
|
1452 |
/* packed or no-bframes or no-bframes-queued: output stats */ |
/* packed or no-bframes or no-bframes-queued: output stats */ |
1453 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) { |
1454 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1455 |
} |
} |
1456 |
|
|
1457 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1540 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1541 |
|
|
1542 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1543 |
|
pEnc->current->sStat.iMVBits = 0; |
1544 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1545 |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1546 |
|
|
1559 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1560 |
|
|
1561 |
start_timer(); |
start_timer(); |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
1562 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1563 |
stop_coding_timer(); |
stop_coding_timer(); |
1564 |
} |
} |
1578 |
return 1; /* intra */ |
return 1; /* intra */ |
1579 |
} |
} |
1580 |
|
|
1581 |
|
static __inline void |
1582 |
|
updateFcode(Statistics * sStat, Encoder * pEnc) |
1583 |
|
{ |
1584 |
|
float fSigma; |
1585 |
|
int iSearchRange; |
1586 |
|
|
1587 |
#define INTRA_THRESHOLD 0.5 |
if (sStat->iMvCount == 0) |
1588 |
#define BFRAME_SKIP_THRESHHOLD 30 |
sStat->iMvCount = 1; |
1589 |
|
|
1590 |
|
fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); |
1591 |
|
|
1592 |
|
iSearchRange = 16 << pEnc->mbParam.m_fcode; |
1593 |
|
|
1594 |
|
if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) |
1595 |
|
pEnc->mbParam.m_fcode++; |
1596 |
|
|
1597 |
|
else if ((5.0 * fSigma < iSearchRange) |
1598 |
|
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1599 |
|
&& (pEnc->mbParam.m_fcode >= 2) ) |
1600 |
|
pEnc->mbParam.m_fcode--; |
1601 |
|
|
1602 |
|
pEnc->fMvPrevSigma = fSigma; |
1603 |
|
} |
1604 |
|
|
1605 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1606 |
|
|
1607 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1608 |
static int |
static int |
1609 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1610 |
Bitstream * bs) |
Bitstream * bs) |
1611 |
{ |
{ |
|
float fSigma; |
|
1612 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1613 |
|
|
1614 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1615 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1616 |
|
|
1617 |
int x, y, k; |
int x, y, k; |
|
int iSearchRange; |
|
|
int skip_possible; |
|
1618 |
FRAMEINFO *const current = pEnc->current; |
FRAMEINFO *const current = pEnc->current; |
1619 |
FRAMEINFO *const reference = pEnc->reference; |
FRAMEINFO *const reference = pEnc->reference; |
1620 |
MBParam * const pParam = &pEnc->mbParam; |
MBParam * const pParam = &pEnc->mbParam; |
1622 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1623 |
int coded = 1; |
int coded = 1; |
1624 |
|
|
|
|
|
|
/* IMAGE *pCurrent = ¤t->image; */ |
|
1625 |
IMAGE *pRef = &reference->image; |
IMAGE *pRef = &reference->image; |
1626 |
|
|
1627 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1639 |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1640 |
if (reference->is_interpolated != current->rounding_type) { |
if (reference->is_interpolated != current->rounding_type) { |
1641 |
start_timer(); |
start_timer(); |
1642 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y, |
1643 |
&pEnc->vInterHV, pParam->edged_width, |
pEnc->vInterHV.y, pParam->edged_width, |
1644 |
pParam->edged_height, |
pParam->edged_height, |
1645 |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1646 |
current->rounding_type); |
current->rounding_type); |
1649 |
} |
} |
1650 |
} |
} |
1651 |
|
|
1652 |
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1653 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = |
1654 |
|
current->sStat.iMVBits = 0; |
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 |
|
|
1770 |
|
|
1771 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1772 |
|
|
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
|
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
|
|
|
|
|
|
|
1773 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1774 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1775 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1776 |
¤t->mbs[x + y * pParam->mb_width]; |
int skip_possible; |
|
|
|
|
int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
|
1777 |
|
|
1778 |
if (bIntra) { |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1779 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1780 |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1781 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1786 |
|
|
1787 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1788 |
|
|
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
1789 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1790 |
stop_coding_timer(); |
stop_coding_timer(); |
1791 |
continue; |
continue; |
1806 |
|
|
1807 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1808 |
|
|
1809 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->cbp != 0) { |
1810 |
{ pMB->cbp = |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
1811 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1812 |
} |
} |
1813 |
|
|
1827 |
|
|
1828 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1829 |
|
|
1830 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER); |
|
(pMB->dquant == 0); |
|
1831 |
|
|
1832 |
if (current->coding_type == S_VOP) |
if (current->coding_type == S_VOP) |
1833 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1834 |
else if (current->coding_type == P_VOP) { |
else { /* PVOP */ |
1835 |
if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) |
const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ? |
1836 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
pMB->qmvs : pMB->mvs; |
1837 |
else |
skip_possible &= ((mv->x|mv->y) == 0); |
|
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
|
1838 |
} |
} |
1839 |
|
|
1840 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
|
|
|
1841 |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
|
|
|
|
if (current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
|
|
{ |
|
1842 |
int bSkip = 1; |
int bSkip = 1; |
1843 |
|
|
1844 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */ |
1845 |
{ |
|
1846 |
|
for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) { |
1847 |
int iSAD; |
int iSAD; |
1848 |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1849 |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1850 |
pParam->edged_width,BFRAME_SKIP_THRESHHOLD); |
pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); |
1851 |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { |
1852 |
{ bSkip = 0; |
bSkip = 0; /* could not SKIP */ |
1853 |
break; |
if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { |
|
} |
|
|
} |
|
|
|
|
|
if (!bSkip) { /* no SKIP, but trivial block */ |
|
|
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
|
1854 |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1855 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1856 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1857 |
} |
} else { |
|
else { |
|
1858 |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1859 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1860 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1861 |
} |
} |
1862 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1863 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1864 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
break; |
1865 |
stop_coding_timer(); |
} |
|
|
|
|
continue; /* next MB */ |
|
1866 |
} |
} |
1867 |
} |
} |
|
/* do SKIP */ |
|
1868 |
|
|
1869 |
|
if (bSkip) { |
1870 |
|
/* do SKIP */ |
1871 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1872 |
MBSkip(bs); |
MBSkip(bs); |
1873 |
stop_coding_timer(); |
stop_coding_timer(); |
1874 |
continue; /* next MB */ |
continue; /* next MB */ |
1875 |
} |
} |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
|
|
|
|
|
if ((current->vop_flags & XVID_VOP_GREYSCALE)) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
|
|
|
|
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
|
|
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
|
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
|
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
|
|
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
|
|
} else { |
|
|
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
|
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
|
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
|
|
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
|
|
} |
|
|
|
|
|
|
|
|
if (pMB->mode == MODE_INTER4V) |
|
|
{ int k; |
|
|
for (k=1;k<4;k++) |
|
|
{ |
|
|
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
|
|
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k); |
|
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
|
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
|
|
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
|
|
} else { |
|
|
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k); |
|
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
|
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
|
|
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
|
|
} |
|
|
|
|
|
} |
|
1876 |
} |
} |
1877 |
|
|
1878 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1879 |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1880 |
stop_coding_timer(); |
stop_coding_timer(); |
|
|
|
1881 |
} |
} |
1882 |
} |
} |
1883 |
|
|
1884 |
emms(); |
emms(); |
1885 |
|
updateFcode(¤t->sStat, pEnc); |
|
if (current->sStat.iMvCount == 0) |
|
|
current->sStat.iMvCount = 1; |
|
|
|
|
|
fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); |
|
|
|
|
|
iSearchRange = 1 << (3 + pParam->m_fcode); |
|
|
|
|
|
if ((fSigma > iSearchRange / 3) |
|
|
&& (pParam->m_fcode <= (3 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
|
|
{ |
|
|
pParam->m_fcode++; |
|
|
iSearchRange *= 2; |
|
|
} else if ((fSigma < iSearchRange / 6) |
|
|
&& (pEnc->fMvPrevSigma >= 0) |
|
|
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
|
|
&& (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
|
|
{ |
|
|
pParam->m_fcode--; |
|
|
iSearchRange /= 2; |
|
|
} |
|
|
|
|
|
pEnc->fMvPrevSigma = fSigma; |
|
1886 |
|
|
1887 |
/* frame drop code */ |
/* frame drop code */ |
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 |
{ |
{ |
1895 |
current->sStat.kblks = current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0; |
1896 |
current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1897 |
|
|
1898 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1982 |
|
|
1983 |
if (pEnc->reference->is_interpolated != 0) { |
if (pEnc->reference->is_interpolated != 0) { |
1984 |
start_timer(); |
start_timer(); |
1985 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y, |
1986 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1987 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1988 |
stop_inter_timer(); |
stop_inter_timer(); |
1999 |
|
|
2000 |
if (pEnc->current->is_interpolated != 0) { |
if (pEnc->current->is_interpolated != 0) { |
2001 |
start_timer(); |
start_timer(); |
2002 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y, |
2003 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
2004 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
2005 |
stop_inter_timer(); |
stop_inter_timer(); |
2007 |
} |
} |
2008 |
|
|
2009 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
2010 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
|
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); |
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); |
2078 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
2079 |
|
|
2080 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
2081 |
|
frame->sStat.iMVBits = 0; |
2082 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
2083 |
frame->sStat.iMvCount = 0; |
frame->sStat.iMvCount = 0; |
2084 |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
2095 |
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
2096 |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
2097 |
} |
} |
|
|
|
2098 |
continue; |
continue; |
2099 |
} |
} |
2100 |
|
|
2101 |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
mb->quant = frame->quant; |
2102 |
|
|
2103 |
|
if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
2104 |
|
/* we have to motion-compensate, transfer etc, |
2105 |
|
because there might be blocks to code */ |
2106 |
|
|
2107 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
2108 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
2109 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
2110 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
2111 |
dct_codes); |
dct_codes); |
2112 |
|
|
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
|
|
mb->quant = frame->quant; |
|
|
|
|
|
if (mb->mode != MODE_DIRECT_NONE_MV) |
|
2113 |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
|
|
|
|
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
|
|
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
|
|
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
|
|
} |
|
2114 |
} |
} |
2115 |
|
|
2116 |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
if (mb->mode == MODE_DIRECT_NO4V) |
2117 |
* coding function for BFrames, that's why we don't zero teh DC |
mb->mode = MODE_DIRECT; |
2118 |
* coeffs */ |
|
2119 |
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) |
2120 |
|
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
2121 |
|
else |
2122 |
|
if (frame->vop_flags & XVID_VOP_GREYSCALE) |
2123 |
|
/* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ |
2124 |
mb->cbp &= 0x3C; |
mb->cbp &= 0x3C; |
2125 |
|
|
2126 |
start_timer(); |
start_timer(); |
2129 |
stop_coding_timer(); |
stop_coding_timer(); |
2130 |
} |
} |
2131 |
} |
} |
|
|
|
2132 |
emms(); |
emms(); |
2133 |
|
|
|
/* TODO: dynamic fcode/bcode ??? */ |
|
|
|
|
2134 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
2135 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
2136 |
|
|