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.102 2004-03-22 22:36:23 edgomez Exp $ |
* $Id: encoder.c,v 1.123 2006-01-19 22:25:18 Isibaar Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
57 |
Bitstream * bs); |
Bitstream * bs); |
58 |
|
|
59 |
static int FrameCodeP(Encoder * pEnc, |
static int FrameCodeP(Encoder * pEnc, |
60 |
Bitstream * bs, |
Bitstream * bs); |
|
bool force_inter, |
|
|
bool vol_header); |
|
61 |
|
|
62 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
63 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
85 |
/* |
/* |
86 |
* Simplify the "fincr/fbase" fraction |
* Simplify the "fincr/fbase" fraction |
87 |
*/ |
*/ |
88 |
|
static int |
89 |
|
gcd(int a, int b) |
90 |
|
{ |
91 |
|
int r ; |
92 |
|
|
93 |
|
if (b > a) { |
94 |
|
r = a; |
95 |
|
a = b; |
96 |
|
b = r; |
97 |
|
} |
98 |
|
|
99 |
|
while ((r = a % b)) { |
100 |
|
a = b; |
101 |
|
b = r; |
102 |
|
} |
103 |
|
return b; |
104 |
|
} |
105 |
|
|
106 |
static void |
static void |
107 |
simplify_time(int *inc, int *base) |
simplify_time(int *inc, int *base) |
108 |
{ |
{ |
109 |
/* common factor */ |
/* common factor */ |
110 |
int i = *inc; |
const int s = gcd(*inc, *base); |
111 |
while (i > 1) { |
*inc /= s; |
112 |
if (*inc % i == 0 && *base % i == 0) { |
*base /= s; |
113 |
*inc /= i; |
|
114 |
*base /= i; |
if (*base > 65535 || *inc > 65535) { |
115 |
i = *inc; |
int *biggest; |
116 |
continue; |
int *other; |
117 |
} |
float div; |
118 |
i--; |
|
119 |
|
if (*base > *inc) { |
120 |
|
biggest = base; |
121 |
|
other = inc; |
122 |
|
} else { |
123 |
|
biggest = inc; |
124 |
|
other = base; |
125 |
} |
} |
126 |
|
|
127 |
/* if neccessary, round to 65535 accuracy */ |
div = ((float)*biggest)/((float)65535); |
128 |
if (*base > 65535) { |
*biggest = (unsigned int)(((float)*biggest)/div); |
129 |
float div = (float) *base / 65535; |
*other = (unsigned int)(((float)*other)/div); |
|
*base = (int) (*base / div); |
|
|
*inc = (int) (*inc / div); |
|
130 |
} |
} |
131 |
} |
} |
132 |
|
|
143 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
144 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
145 |
|
|
146 |
|
if (create->width<=0 || create->height<=0) |
147 |
|
return XVID_ERR_FAIL; |
148 |
|
|
149 |
/* allocate encoder struct */ |
/* allocate encoder struct */ |
150 |
|
|
151 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
157 |
|
|
158 |
/* global flags */ |
/* global flags */ |
159 |
pEnc->mbParam.global_flags = create->global; |
pEnc->mbParam.global_flags = create->global; |
160 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED)) |
161 |
|
pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA; |
162 |
|
|
163 |
/* width, height */ |
/* width, height */ |
164 |
pEnc->mbParam.width = create->width; |
pEnc->mbParam.width = create->width; |
172 |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
173 |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
174 |
if (pEnc->mbParam.fincr>0) |
if (pEnc->mbParam.fincr>0) |
175 |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase); |
176 |
|
|
177 |
/* zones */ |
/* zones */ |
178 |
if(create->num_zones > 0) { |
if(create->num_zones > 0) { |
203 |
|
|
204 |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
205 |
pinfo.version = XVID_VERSION; |
pinfo.version = XVID_VERSION; |
206 |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) { |
207 |
pEnc->mbParam.plugin_flags |= pinfo.flags; |
pEnc->mbParam.plugin_flags |= pinfo.flags; |
208 |
} |
} |
209 |
|
|
220 |
pcreate.param = create->plugins[n].param; |
pcreate.param = create->plugins[n].param; |
221 |
|
|
222 |
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
223 |
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) { |
224 |
pEnc->plugins[n].func = create->plugins[n].func; |
pEnc->plugins[n].func = create->plugins[n].func; |
225 |
} |
} |
226 |
} |
} |
238 |
goto xvid_err_memory1a; |
goto xvid_err_memory1a; |
239 |
} |
} |
240 |
|
|
241 |
|
/* temp lambdas */ |
242 |
|
if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
243 |
|
pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width * |
244 |
|
pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE); |
245 |
|
if (pEnc->temp_lambda == NULL) |
246 |
|
goto xvid_err_memory1a; |
247 |
|
} |
248 |
|
|
249 |
/* bframes */ |
/* bframes */ |
250 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
251 |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
527 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
528 |
} |
} |
529 |
|
|
530 |
|
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
531 |
|
xvid_free(pEnc->temp_lambda); |
532 |
|
} |
533 |
|
|
534 |
xvid_err_memory0: |
xvid_err_memory0: |
535 |
for (n=0; n<pEnc->num_plugins;n++) { |
for (n=0; n<pEnc->num_plugins;n++) { |
536 |
if (pEnc->plugins[n].func) { |
if (pEnc->plugins[n].func) { |
537 |
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); |
538 |
} |
} |
539 |
} |
} |
540 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
639 |
|
|
640 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
641 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
642 |
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); |
643 |
} |
} |
644 |
} |
} |
645 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
663 |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
664 |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
665 |
{ |
{ |
666 |
unsigned int i, j; |
unsigned int i, j, k; |
667 |
xvid_plg_data_t data; |
xvid_plg_data_t data; |
668 |
|
|
669 |
/* set data struct */ |
/* set data struct */ |
725 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
726 |
} |
} |
727 |
|
|
728 |
|
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
729 |
|
int block = 0; |
730 |
|
data.lambda = pEnc->temp_lambda; |
731 |
|
for(i = 0;i < pEnc->mbParam.mb_height; i++) |
732 |
|
for(j = 0;j < pEnc->mbParam.mb_width; j++) |
733 |
|
for (k = 0; k < 6; k++) |
734 |
|
data.lambda[block++] = 1.0f; |
735 |
|
} |
736 |
|
|
737 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
738 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
739 |
data.original.csp = XVID_CSP_PLANAR; |
data.original.csp = XVID_CSP_PLANAR; |
808 |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
809 |
emms(); |
emms(); |
810 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
811 |
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) { |
812 |
continue; |
continue; |
813 |
} |
} |
814 |
} |
} |
837 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
838 |
} |
} |
839 |
} |
} |
840 |
|
|
841 |
|
if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
842 |
|
for (j = 0; j < pEnc->mbParam.mb_height; j++) |
843 |
|
for (i = 0; i < pEnc->mbParam.mb_width; i++) |
844 |
|
for (k = 0; k < 6; k++) { |
845 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = |
846 |
|
(int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]); |
847 |
|
} |
848 |
|
} else { |
849 |
|
for (j = 0; j<pEnc->mbParam.mb_height; j++) |
850 |
|
for (i = 0; i<pEnc->mbParam.mb_width; i++) |
851 |
|
for (k = 0; k < 6; k++) { |
852 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP; |
853 |
|
} |
854 |
|
} |
855 |
|
|
856 |
|
|
857 |
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 */ |
858 |
} |
} |
859 |
|
|
923 |
#endif |
#endif |
924 |
} |
} |
925 |
|
|
|
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; |
|
|
} |
|
|
|
|
926 |
static void |
static void |
927 |
simplify_par(int *par_width, int *par_height) |
simplify_par(int *par_width, int *par_height) |
928 |
{ |
{ |
1048 |
} |
} |
1049 |
|
|
1050 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
1051 |
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); |
1052 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
1053 |
|
|
1054 |
goto done; |
goto done; |
1080 |
|
|
1081 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
1082 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
1083 |
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); |
1084 |
|
|
1085 |
/* flush complete: reset counters */ |
/* flush complete: reset counters */ |
1086 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
1108 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1109 |
|
|
1110 |
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) { |
1111 |
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); |
1112 |
} |
} |
1113 |
|
|
1114 |
/* 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 */ |
1131 |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1132 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1133 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1134 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1135 |
|
|
1136 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs); |
1137 |
|
|
1138 |
|
|
1139 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1140 |
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); |
1141 |
}else{ |
}else{ |
1142 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1143 |
goto done; |
goto done; |
1186 |
type = frame->type; |
type = frame->type; |
1187 |
pEnc->current->quant = frame->quant; |
pEnc->current->quant = frame->quant; |
1188 |
|
|
1189 |
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); |
1190 |
|
|
1191 |
if (type > 0){ /* XVID_TYPE_?VOP */ |
if (type > 0){ /* XVID_TYPE_?VOP */ |
1192 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1261 |
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) |
1262 |
{ |
{ |
1263 |
if (pEnc->current->stamp > 0) { |
if (pEnc->current->stamp > 0) { |
1264 |
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); |
1265 |
} |
} |
1266 |
else |
else |
1267 |
stats->type = XVID_TYPE_NOTHING; |
stats->type = XVID_TYPE_NOTHING; |
1289 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1290 |
|
|
1291 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1292 |
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"); |
1293 |
} |
} |
1294 |
|
|
1295 |
/* convert B-VOP quant to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1350 |
|
|
1351 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1352 |
|
|
|
if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
|
|
pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; |
|
|
|
|
1353 |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1354 |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
1355 |
|
|
1382 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1383 |
} |
} |
1384 |
|
|
1385 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1386 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1387 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1388 |
|
goto done; |
1389 |
|
} |
1390 |
} |
} |
1391 |
|
|
1392 |
|
|
1405 |
|
|
1406 |
/* packed or no-bframes or no-bframes-queued: output stats */ |
/* packed or no-bframes or no-bframes-queued: output stats */ |
1407 |
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 ) { |
1408 |
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); |
1409 |
} |
} |
1410 |
|
|
1411 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1477 |
|
|
1478 |
uint16_t x, y; |
uint16_t x, y; |
1479 |
|
|
|
if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
|
|
{ |
|
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
|
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
|
|
|
|
|
/* 16x16->8x8 downsample requires 1 additional edge pixel*/ |
|
|
/* XXX: setedges is overkill */ |
|
|
start_timer(); |
|
|
image_setedges(&pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height, 0); |
|
|
stop_edges_timer(); |
|
|
} |
|
|
|
|
1480 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1481 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1482 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1494 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1495 |
|
|
1496 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1497 |
|
pEnc->current->sStat.iMVBits = 0; |
1498 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1499 |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1500 |
|
|
1513 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1514 |
|
|
1515 |
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; |
|
|
} |
|
1516 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1517 |
stop_coding_timer(); |
stop_coding_timer(); |
1518 |
} |
} |
1519 |
|
|
|
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
|
|
16, 0); |
|
|
} |
|
1520 |
emms(); |
emms(); |
1521 |
|
|
1522 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1532 |
return 1; /* intra */ |
return 1; /* intra */ |
1533 |
} |
} |
1534 |
|
|
1535 |
|
static __inline void |
1536 |
|
updateFcode(Statistics * sStat, Encoder * pEnc) |
1537 |
|
{ |
1538 |
|
float fSigma; |
1539 |
|
int iSearchRange; |
1540 |
|
|
1541 |
|
if (sStat->iMvCount == 0) |
1542 |
|
sStat->iMvCount = 1; |
1543 |
|
|
1544 |
#define INTRA_THRESHOLD 0.5 |
fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); |
1545 |
#define BFRAME_SKIP_THRESHHOLD 30 |
|
1546 |
|
iSearchRange = 16 << pEnc->mbParam.m_fcode; |
1547 |
|
|
1548 |
|
if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) |
1549 |
|
pEnc->mbParam.m_fcode++; |
1550 |
|
|
1551 |
|
else if ((5.0 * fSigma < iSearchRange) |
1552 |
|
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1553 |
|
&& (pEnc->mbParam.m_fcode >= 2) ) |
1554 |
|
pEnc->mbParam.m_fcode--; |
1555 |
|
|
1556 |
|
pEnc->fMvPrevSigma = fSigma; |
1557 |
|
} |
1558 |
|
|
1559 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1560 |
|
|
1561 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1562 |
static int |
static int |
1563 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1564 |
Bitstream * bs, |
Bitstream * bs) |
|
bool force_inter, |
|
|
bool vol_header) |
|
1565 |
{ |
{ |
|
float fSigma; |
|
1566 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1567 |
|
|
1568 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1569 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1570 |
|
|
|
int iLimit; |
|
1571 |
int x, y, k; |
int x, y, k; |
|
int iSearchRange; |
|
|
int bIntra=0, skip_possible; |
|
1572 |
FRAMEINFO *const current = pEnc->current; |
FRAMEINFO *const current = pEnc->current; |
1573 |
FRAMEINFO *const reference = pEnc->reference; |
FRAMEINFO *const reference = pEnc->reference; |
1574 |
MBParam * const pParam = &pEnc->mbParam; |
MBParam * const pParam = &pEnc->mbParam; |
1575 |
int mb_width = pParam->mb_width; |
int mb_width = pParam->mb_width; |
1576 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1577 |
|
int coded = 1; |
1578 |
|
|
|
|
|
|
/* IMAGE *pCurrent = ¤t->image; */ |
|
1579 |
IMAGE *pRef = &reference->image; |
IMAGE *pRef = &reference->image; |
1580 |
|
|
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
mb_width = (pParam->width + 31) / 32; |
|
|
mb_height = (pParam->height + 31) / 32; |
|
|
} |
|
|
|
|
|
|
|
1581 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1582 |
start_timer(); |
start_timer(); |
1583 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1590 |
current->rounding_type = pParam->m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1591 |
current->fcode = pParam->m_fcode; |
current->fcode = pParam->m_fcode; |
1592 |
|
|
|
if (!force_inter) |
|
|
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
|
|
else |
|
|
iLimit = mb_width * mb_height + 1; |
|
|
|
|
1593 |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1594 |
if (reference->is_interpolated != current->rounding_type) { |
if (reference->is_interpolated != current->rounding_type) { |
1595 |
start_timer(); |
start_timer(); |
1596 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y, |
1597 |
&pEnc->vInterHV, pParam->edged_width, |
pEnc->vInterHV.y, pParam->edged_width, |
1598 |
pParam->edged_height, |
pParam->edged_height, |
1599 |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1600 |
current->rounding_type); |
current->rounding_type); |
1603 |
} |
} |
1604 |
} |
} |
1605 |
|
|
1606 |
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1607 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = |
1608 |
|
current->sStat.iMVBits = 0; |
1609 |
|
|
1610 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1611 |
|
|
1612 |
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 |
} |
} |
1666 |
|
|
|
bIntra = |
|
1667 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1668 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1669 |
&pEnc->vGMC, iLimit); |
&pEnc->vGMC, 256*4096); |
1670 |
|
|
1671 |
|
|
1672 |
stop_motion_timer(); |
stop_motion_timer(); |
1673 |
|
|
|
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
|
|
|
|
1674 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
|
if (vol_header) |
|
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, current); |
|
|
BitstreamPad(bs); |
|
|
} |
|
1675 |
|
|
1676 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1677 |
|
|
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
|
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
|
|
|
|
|
|
|
1678 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1679 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1680 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1681 |
¤t->mbs[x + y * pParam->mb_width]; |
int skip_possible; |
1682 |
|
|
1683 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
|
|
|
if (bIntra) { |
|
1684 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1685 |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1686 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1691 |
|
|
1692 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1693 |
|
|
|
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; |
|
|
} |
|
1694 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1695 |
stop_coding_timer(); |
stop_coding_timer(); |
1696 |
continue; |
continue; |
1705 |
pParam->height, |
pParam->height, |
1706 |
pParam->edged_width, |
pParam->edged_width, |
1707 |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
|
(current->vop_flags & XVID_VOP_REDUCED), |
|
1708 |
current->rounding_type); |
current->rounding_type); |
1709 |
|
|
1710 |
stop_comp_timer(); |
stop_comp_timer(); |
1711 |
|
|
1712 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1713 |
|
|
1714 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->cbp != 0) { |
1715 |
{ pMB->cbp = |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
1716 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1717 |
} |
} |
1718 |
|
|
1732 |
|
|
1733 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1734 |
|
|
1735 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER); |
|
(pMB->dquant == 0); |
|
1736 |
|
|
1737 |
if (current->coding_type == S_VOP) |
if (current->coding_type == S_VOP) |
1738 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1739 |
else if (current->coding_type == P_VOP) { |
else { /* PVOP */ |
1740 |
if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) |
const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ? |
1741 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
pMB->qmvs : pMB->mvs; |
1742 |
else |
skip_possible &= ((mv->x|mv->y) == 0); |
|
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
|
1743 |
} |
} |
1744 |
|
|
1745 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
|
|
|
1746 |
/* 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 */ |
|
|
{ |
|
1747 |
int bSkip = 1; |
int bSkip = 1; |
1748 |
|
|
1749 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */ |
1750 |
{ |
|
1751 |
|
for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) { |
1752 |
int iSAD; |
int iSAD; |
1753 |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1754 |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1755 |
pParam->edged_width,BFRAME_SKIP_THRESHHOLD); |
pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); |
1756 |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { |
1757 |
{ bSkip = 0; |
bSkip = 0; /* could not SKIP */ |
1758 |
break; |
if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { |
|
} |
|
|
} |
|
|
|
|
|
if (!bSkip) { /* no SKIP, but trivial block */ |
|
|
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
|
1759 |
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); |
1760 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1761 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1762 |
} |
} else { |
|
else { |
|
1763 |
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); |
1764 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1765 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1766 |
} |
} |
1767 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1768 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1769 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
break; |
1770 |
stop_coding_timer(); |
} |
|
|
|
|
continue; /* next MB */ |
|
1771 |
} |
} |
1772 |
} |
} |
|
/* do SKIP */ |
|
1773 |
|
|
1774 |
|
if (bSkip) { |
1775 |
|
/* do SKIP */ |
1776 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1777 |
MBSkip(bs); |
MBSkip(bs); |
1778 |
stop_coding_timer(); |
stop_coding_timer(); |
1779 |
continue; /* next MB */ |
continue; /* next MB */ |
1780 |
} |
} |
|
/* 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); |
|
|
} |
|
|
|
|
|
} |
|
1781 |
} |
} |
1782 |
|
|
1783 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1784 |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1785 |
stop_coding_timer(); |
stop_coding_timer(); |
|
|
|
|
} |
|
1786 |
} |
} |
|
|
|
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
image_deblock_rrv(¤t->image, pParam->edged_width, |
|
|
current->mbs, mb_width, mb_height, pParam->mb_width, |
|
|
16, 0); |
|
1787 |
} |
} |
1788 |
|
|
1789 |
emms(); |
emms(); |
1790 |
|
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; |
|
1791 |
|
|
1792 |
/* frame drop code */ |
/* frame drop code */ |
1793 |
#if 0 |
#if 0 |
1794 |
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); |
1795 |
#endif |
#endif |
1796 |
if (current->sStat.kblks + current->sStat.mblks < |
if (current->sStat.kblks + current->sStat.mblks <= |
1797 |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1798 |
|
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
1799 |
{ |
{ |
1800 |
current->sStat.kblks = current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1801 |
current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1811 |
current->rounding_type = reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1812 |
current->fcode = reference->fcode; |
current->fcode = reference->fcode; |
1813 |
current->bcode = reference->bcode; |
current->bcode = reference->bcode; |
1814 |
|
current->stamp = reference->stamp; |
1815 |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1816 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1817 |
} |
coded = 0; |
1818 |
|
|
1819 |
|
} else { |
1820 |
|
|
1821 |
pEnc->current->is_edged = 0; /* not edged */ |
pEnc->current->is_edged = 0; /* not edged */ |
1822 |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1827 |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1828 |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1829 |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1830 |
|
} |
1831 |
|
|
1832 |
/* XXX: debug |
/* XXX: debug |
1833 |
{ |
{ |
1848 |
|
|
1849 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1850 |
|
|
1851 |
return 0; /* inter */ |
return coded; |
1852 |
} |
} |
1853 |
|
|
1854 |
|
|
1872 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1873 |
} |
} |
1874 |
|
|
|
/* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ |
|
|
|
|
1875 |
if (!first){ |
if (!first){ |
1876 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1877 |
} |
} |
1887 |
|
|
1888 |
if (pEnc->reference->is_interpolated != 0) { |
if (pEnc->reference->is_interpolated != 0) { |
1889 |
start_timer(); |
start_timer(); |
1890 |
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, |
1891 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1892 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1893 |
stop_inter_timer(); |
stop_inter_timer(); |
1904 |
|
|
1905 |
if (pEnc->current->is_interpolated != 0) { |
if (pEnc->current->is_interpolated != 0) { |
1906 |
start_timer(); |
start_timer(); |
1907 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y, |
1908 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1909 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1910 |
stop_inter_timer(); |
stop_inter_timer(); |
1912 |
} |
} |
1913 |
|
|
1914 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1915 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1916 |
|
|
1917 |
start_timer(); |
start_timer(); |
1918 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1928 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
1929 |
|
|
1930 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
1931 |
|
frame->sStat.iMVBits = 0; |
1932 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
1933 |
frame->sStat.iMvCount = 0; |
frame->sStat.iMvCount = 0; |
1934 |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
1943 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1944 |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1945 |
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, |
1946 |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0); |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
1947 |
} |
} |
|
|
|
1948 |
continue; |
continue; |
1949 |
} |
} |
1950 |
|
|
1951 |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
mb->quant = frame->quant; |
1952 |
|
|
1953 |
|
if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1954 |
|
/* we have to motion-compensate, transfer etc, |
1955 |
|
because there might be blocks to code */ |
1956 |
|
|
1957 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1958 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1959 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1960 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1961 |
dct_codes); |
dct_codes); |
1962 |
|
|
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
|
|
mb->quant = frame->quant; |
|
|
|
|
|
if (mb->mode != MODE_DIRECT_NONE_MV) |
|
1963 |
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 */ |
|
|
} |
|
1964 |
} |
} |
1965 |
|
|
1966 |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
if (mb->mode == MODE_DIRECT_NO4V) |
1967 |
* coding function for BFrames, that's why we don't zero teh DC |
mb->mode = MODE_DIRECT; |
1968 |
* coeffs */ |
|
1969 |
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) |
1970 |
|
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
1971 |
|
else |
1972 |
|
if (frame->vop_flags & XVID_VOP_GREYSCALE) |
1973 |
|
/* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ |
1974 |
mb->cbp &= 0x3C; |
mb->cbp &= 0x3C; |
1975 |
|
|
1976 |
start_timer(); |
start_timer(); |
1982 |
|
|
1983 |
emms(); |
emms(); |
1984 |
|
|
|
/* TODO: dynamic fcode/bcode ??? */ |
|
|
|
|
1985 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1986 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1987 |
|
|