117 |
Encoder *pEnc; |
Encoder *pEnc; |
118 |
int n; |
int n; |
119 |
|
|
120 |
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ |
121 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
122 |
|
|
123 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
188 |
pcreate.zones = pEnc->zones; |
pcreate.zones = pEnc->zones; |
189 |
pcreate.width = pEnc->mbParam.width; |
pcreate.width = pEnc->mbParam.width; |
190 |
pcreate.height = pEnc->mbParam.height; |
pcreate.height = pEnc->mbParam.height; |
191 |
|
pcreate.mb_width = pEnc->mbParam.mb_width; |
192 |
|
pcreate.mb_height = pEnc->mbParam.mb_height; |
193 |
pcreate.fincr = pEnc->mbParam.fincr; |
pcreate.fincr = pEnc->mbParam.fincr; |
194 |
pcreate.fbase = pEnc->mbParam.fbase; |
pcreate.fbase = pEnc->mbParam.fbase; |
195 |
pcreate.param = create->plugins[n].param; |
pcreate.param = create->plugins[n].param; |
228 |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
229 |
|
|
230 |
/* max keyframe interval */ |
/* max keyframe interval */ |
231 |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval; |
|
(10 * pEnc->mbParam.fbase) / pEnc->mbParam.fincr : create->max_key_interval; |
|
232 |
|
|
233 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
234 |
|
|
391 |
image_null(&pEnc->queue[n].image); |
image_null(&pEnc->queue[n].image); |
392 |
|
|
393 |
|
|
394 |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
|
{ |
|
395 |
if (image_create |
if (image_create |
396 |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
397 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
398 |
goto xvid_err_memory5; |
goto xvid_err_memory5; |
|
|
|
399 |
} |
} |
400 |
|
|
|
|
|
401 |
/* timestamp stuff */ |
/* timestamp stuff */ |
402 |
|
|
403 |
pEnc->mbParam.m_stamp = 0; |
pEnc->mbParam.m_stamp = 0; |
422 |
|
|
423 |
xvid_err_memory5: |
xvid_err_memory5: |
424 |
|
|
425 |
if (pEnc->mbParam.max_bframes > 0) { |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
426 |
int i; |
image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
|
|
|
|
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
|
427 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
428 |
} |
} |
429 |
|
|
430 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
|
} |
|
431 |
|
|
432 |
xvid_err_memory4: |
xvid_err_memory4: |
433 |
|
|
441 |
|
|
442 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
443 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
444 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
445 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
446 |
} |
} |
447 |
|
|
448 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
531 |
int i; |
int i; |
532 |
|
|
533 |
/* B Frames specific */ |
/* B Frames specific */ |
|
if (pEnc->mbParam.max_bframes > 0) { |
|
|
|
|
534 |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
|
535 |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
536 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
537 |
} |
} |
|
xvid_free(pEnc->queue); |
|
|
} |
|
538 |
|
|
539 |
|
xvid_free(pEnc->queue); |
540 |
|
|
541 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
542 |
|
|
547 |
|
|
548 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
549 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
550 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
551 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
552 |
} |
} |
553 |
|
|
650 |
data.mb_height = pEnc->mbParam.mb_height; |
data.mb_height = pEnc->mbParam.mb_height; |
651 |
data.fincr = frame->fincr; |
data.fincr = frame->fincr; |
652 |
data.fbase = pEnc->mbParam.fbase; |
data.fbase = pEnc->mbParam.fbase; |
653 |
|
data.bquant_ratio = pEnc->mbParam.bquant_ratio; |
654 |
|
data.bquant_offset = pEnc->mbParam.bquant_offset; |
655 |
|
|
656 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
657 |
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
729 |
|
|
730 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
731 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
732 |
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant; |
733 |
} |
} |
734 |
} |
} |
735 |
|
|
759 |
} |
} |
760 |
|
|
761 |
/* call plugins */ |
/* call plugins */ |
762 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
763 |
emms(); |
emms(); |
764 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
765 |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
855 |
int type; |
int type; |
856 |
Bitstream bs; |
Bitstream bs; |
857 |
|
|
858 |
if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */ |
859 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
860 |
|
|
861 |
xFrame->out_flags = 0; |
xFrame->out_flags = 0; |
892 |
|
|
893 |
if (xFrame->quant_intra_matrix) |
if (xFrame->quant_intra_matrix) |
894 |
{ |
{ |
895 |
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix)); |
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char)); |
896 |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
897 |
} |
} |
898 |
|
|
899 |
if (xFrame->quant_inter_matrix) |
if (xFrame->quant_inter_matrix) |
900 |
{ |
{ |
901 |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix)); |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char)); |
902 |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
903 |
} |
} |
904 |
|
|
998 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
999 |
|
|
1000 |
/* convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
1001 |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1002 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1003 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1004 |
|
|
1005 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1006 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1075 |
}else{ |
}else{ |
1076 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1077 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1078 |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold, |
1079 |
|
pEnc->bframes[pEnc->bframenum_head]->mbs); |
1080 |
} |
} |
1081 |
} |
} |
1082 |
|
|
1089 |
|
|
1090 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1091 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
1092 |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
"%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
1093 |
} |
} |
1094 |
|
|
1095 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1168 |
} |
} |
1169 |
|
|
1170 |
/* convert B-VOP quant to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1171 |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1172 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1173 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1174 |
type = P_VOP; |
type = P_VOP; |
1175 |
} |
} |
1176 |
|
|
1193 |
|
|
1194 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1195 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1196 |
|
switch(frame->par) { |
1197 |
|
case XVID_PAR_11_VGA: |
1198 |
|
case XVID_PAR_43_PAL: |
1199 |
|
case XVID_PAR_43_NTSC: |
1200 |
|
case XVID_PAR_169_PAL: |
1201 |
|
case XVID_PAR_169_NTSC: |
1202 |
|
case XVID_PAR_EXT: |
1203 |
|
pEnc->mbParam.par = frame->par; |
1204 |
|
break; |
1205 |
|
default: |
1206 |
|
pEnc->mbParam.par = XVID_PAR_EXT; |
1207 |
|
break; |
1208 |
|
} |
1209 |
|
pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1; |
1210 |
|
pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1; |
1211 |
|
|
1212 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1213 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1261 |
* on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1262 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1263 |
|
|
1264 |
done_flush: |
/*done_flush:*/ |
1265 |
|
|
1266 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1267 |
|
|
1293 |
{ |
{ |
1294 |
unsigned int i,j; |
unsigned int i,j; |
1295 |
int quant = frame->quant; |
int quant = frame->quant; |
1296 |
|
if (quant > 31) |
1297 |
|
frame->quant = quant = 31; |
1298 |
|
else if (quant < 1) |
1299 |
|
frame->quant = quant = 1; |
1300 |
|
|
1301 |
for (j=0; j<pParam->mb_height; j++) |
for (j=0; j<pParam->mb_height; j++) |
1302 |
for (i=0; i<pParam->mb_width; i++) { |
for (i=0; i<pParam->mb_width; i++) { |
1304 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1305 |
if (quant > 31) |
if (quant > 31) |
1306 |
quant = 31; |
quant = 31; |
1307 |
if (quant < 1) |
else if (quant < 1) |
1308 |
quant = 1; |
quant = 1; |
1309 |
pMB->quant = quant; |
pMB->quant = quant; |
1310 |
} |
} |
1368 |
|
|
1369 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1370 |
|
|
1371 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1372 |
|
|
1373 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1374 |
|
|
1375 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1408 |
} |
} |
1409 |
emms(); |
emms(); |
1410 |
|
|
1411 |
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1412 |
#if 0 |
|
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
|
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1413 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1414 |
|
|
1415 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1416 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1417 |
|
|
1418 |
|
pEnc->current->is_edged = 0; /* not edged */ |
1419 |
|
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1420 |
|
|
1421 |
return 1; /* intra */ |
return 1; /* intra */ |
1422 |
} |
} |
1423 |
|
|
1460 |
} |
} |
1461 |
|
|
1462 |
|
|
1463 |
|
if (!reference->is_edged) { |
1464 |
start_timer(); |
start_timer(); |
1465 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1466 |
pParam->width, pParam->height); |
pParam->width, pParam->height); |
1467 |
stop_edges_timer(); |
stop_edges_timer(); |
1468 |
|
reference->is_edged = 1; |
1469 |
|
} |
1470 |
|
|
1471 |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
1472 |
current->rounding_type = pParam->m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1478 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1479 |
|
|
1480 |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1481 |
|
if (reference->is_interpolated != current->rounding_type) { |
1482 |
start_timer(); |
start_timer(); |
1483 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1484 |
&pEnc->vInterHV, pParam->edged_width, |
&pEnc->vInterHV, pParam->edged_width, |
1486 |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1487 |
current->rounding_type); |
current->rounding_type); |
1488 |
stop_inter_timer(); |
stop_inter_timer(); |
1489 |
|
reference->is_interpolated = current->rounding_type; |
1490 |
|
} |
1491 |
} |
} |
1492 |
|
|
1493 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1501 |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
1502 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1503 |
|
|
1504 |
if (current->motion_flags & XVID_GME_REFINE) { |
if (current->motion_flags & XVID_ME_GME_REFINE) { |
1505 |
gmcval = GlobalMotionEstRefine(¤t->warp, |
gmcval = GlobalMotionEstRefine(¤t->warp, |
1506 |
current->mbs, pParam, |
current->mbs, pParam, |
1507 |
current, reference, |
current, reference, |
1510 |
&pEnc->vInterH, |
&pEnc->vInterH, |
1511 |
&pEnc->vInterV, |
&pEnc->vInterV, |
1512 |
&pEnc->vInterHV); |
&pEnc->vInterHV); |
1513 |
gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height); |
} else { |
|
} |
|
|
|
|
1514 |
gmcval = globalSAD(¤t->warp, pParam, current->mbs, |
gmcval = globalSAD(¤t->warp, pParam, current->mbs, |
1515 |
current, |
current, |
1516 |
&reference->image, |
&reference->image, |
1517 |
¤t->image, |
¤t->image, |
1518 |
pEnc->vGMC.y); |
pEnc->vGMC.y); |
1519 |
|
} |
1520 |
|
|
1521 |
gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); |
gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); |
1522 |
|
|
1523 |
/* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ |
/* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ |
1559 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
1560 |
if (vol_header) |
if (vol_header) |
1561 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1562 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1563 |
} |
} |
1564 |
|
|
1565 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
1573 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1574 |
¤t->mbs[x + y * pParam->mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
1575 |
|
|
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
|
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
|
|
|
|
1576 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1577 |
|
|
1578 |
if (bIntra) { |
if (bIntra) { |
1586 |
|
|
1587 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1588 |
|
|
1589 |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1590 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1591 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1592 |
|
qcoeff[5*64+0]=0; |
1593 |
|
} |
1594 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1595 |
stop_coding_timer(); |
stop_coding_timer(); |
1596 |
continue; |
continue; |
1597 |
} |
} |
1598 |
|
|
|
if (current->coding_type == S_VOP) { |
|
|
|
|
|
int32_t iSAD = sad16(current->image.y + 16*y*pParam->edged_width + 16*x, |
|
|
pEnc->vGMC.y + 16*y*pParam->edged_width + 16*x, |
|
|
pParam->edged_width, 65536); |
|
|
|
|
|
if (current->motion_flags & XVID_ME_CHROMA16) { |
|
|
iSAD += sad8(current->image.u + 8*y*(pParam->edged_width/2) + 8*x, |
|
|
pEnc->vGMC.u + 8*y*(pParam->edged_width/2) + 8*x, pParam->edged_width/2); |
|
|
|
|
|
iSAD += sad8(current->image.v + 8*y*(pParam->edged_width/2) + 8*x, |
|
|
pEnc->vGMC.v + 8*y*(pParam->edged_width/2) + 8*x, pParam->edged_width/2); |
|
|
} |
|
|
|
|
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
|
|
|
|
|
if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) |
|
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
|
|
else |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
|
|
|
|
|
pMB->mode = MODE_INTER; |
|
|
pMB->mcsel = 1; |
|
|
pMB->sad16 = iSAD; |
|
|
} else { |
|
|
pMB->mcsel = 0; |
|
|
} |
|
|
} else { |
|
|
pMB->mcsel = 0; /* just a precaution */ |
|
|
} |
|
|
|
|
1599 |
start_timer(); |
start_timer(); |
1600 |
MBMotionCompensation(pMB, x, y, &reference->image, |
MBMotionCompensation(pMB, x, y, &reference->image, |
1601 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1795 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1796 |
} |
} |
1797 |
|
|
1798 |
|
pEnc->current->is_edged = 0; /* not edged */ |
1799 |
|
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1800 |
|
|
1801 |
|
/* what was this frame's interpolated reference will become |
1802 |
|
forward (past) reference in b-frame coding */ |
1803 |
|
|
1804 |
|
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1805 |
|
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1806 |
|
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1807 |
|
|
1808 |
|
|
1809 |
/* XXX: debug |
/* XXX: debug |
1810 |
{ |
{ |
1811 |
char s[100]; |
char s[100]; |
1821 |
} |
} |
1822 |
*/ |
*/ |
1823 |
|
|
1824 |
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
|
#if 0 |
|
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
|
|
if ((pParam->global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1825 |
|
|
1826 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1827 |
|
|
1857 |
#endif |
#endif |
1858 |
|
|
1859 |
/* forward */ |
/* forward */ |
1860 |
|
if (!pEnc->reference->is_edged) { |
1861 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1862 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1863 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1864 |
|
pEnc->current->is_edged = 1; |
1865 |
|
} |
1866 |
|
|
1867 |
|
if (pEnc->reference->is_interpolated != 0) { |
1868 |
start_timer(); |
start_timer(); |
1869 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1870 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1871 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1872 |
stop_inter_timer(); |
stop_inter_timer(); |
1873 |
|
pEnc->reference->is_interpolated = 0; |
1874 |
|
} |
1875 |
|
|
1876 |
/* backward */ |
/* backward */ |
1877 |
|
if (!pEnc->current->is_edged) { |
1878 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1879 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1880 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1881 |
|
pEnc->current->is_edged = 1; |
1882 |
|
} |
1883 |
|
|
1884 |
|
if (pEnc->current->is_interpolated != 0) { |
1885 |
start_timer(); |
start_timer(); |
1886 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1887 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1888 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1889 |
stop_inter_timer(); |
stop_inter_timer(); |
1890 |
|
pEnc->current->is_interpolated = 0; |
1891 |
|
} |
1892 |
|
|
1893 |
start_timer(); |
start_timer(); |
1894 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1915 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1916 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1917 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0; |
|
1918 |
|
|
1919 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1920 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1921 |
/* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */ |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1922 |
|
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
1923 |
|
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0); |
1924 |
|
} |
1925 |
|
|
1926 |
continue; |
continue; |
1927 |
} |
} |
1928 |
|
|
1945 |
} |
} |
1946 |
} |
} |
1947 |
|
|
1948 |
#ifdef BFRAMES_DEC_DEBUG |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
1949 |
BFRAME_DEBUG |
* coding function for BFrames, that's why we don't zero teh DC |
1950 |
#endif |
* coeffs */ |
1951 |
|
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
1952 |
|
mb->cbp &= 0x3C; |
1953 |
|
|
1954 |
start_timer(); |
start_timer(); |
1955 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, |
1956 |
&frame->sStat, direction); |
&frame->sStat); |
1957 |
stop_coding_timer(); |
stop_coding_timer(); |
1958 |
} |
} |
1959 |
} |
} |
1962 |
|
|
1963 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1964 |
|
|
1965 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1966 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1967 |
|
|
1968 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |