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.95.2.53 2003-11-16 17:41:03 edgomez Exp $ |
* $Id: encoder.c,v 1.95.2.54 2003-11-19 15:42:38 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
265 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
266 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
267 |
image_null(&pEnc->vInterV); |
image_null(&pEnc->vInterV); |
|
image_null(&pEnc->vInterVf); |
|
268 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
|
image_null(&pEnc->vInterHVf); |
|
269 |
|
|
270 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
271 |
if (image_create |
if (image_create |
309 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
310 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
311 |
if (image_create |
if (image_create |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
|
if (image_create |
|
312 |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
313 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
314 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
|
if (image_create |
|
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
315 |
|
|
316 |
/* Create full bitplane for GMC, this might be wasteful */ |
/* Create full bitplane for GMC, this might be wasteful */ |
317 |
if (image_create |
if (image_create |
462 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
463 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
464 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
465 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
466 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
467 |
|
|
468 |
/* destroy GMC image */ |
/* destroy GMC image */ |
469 |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
551 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
552 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
553 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
554 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
555 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
|
|
|
556 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
557 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
558 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
661 |
data.type = *type; |
data.type = *type; |
662 |
data.quant = *quant; |
data.quant = *quant; |
663 |
|
|
664 |
|
data.vol_flags = frame->vol_flags; |
665 |
|
data.vop_flags = frame->vop_flags; |
666 |
|
data.motion_flags = frame->motion_flags; |
667 |
|
|
668 |
|
} else if (opt == XVID_PLG_FRAME) { |
669 |
|
data.type = coding2type(frame->coding_type); |
670 |
|
data.quant = frame->quant; |
671 |
|
|
672 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
673 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
674 |
data.dquant_stride = pEnc->mbParam.mb_width; |
data.dquant_stride = pEnc->mbParam.mb_width; |
675 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
676 |
} |
} |
677 |
|
|
|
data.vol_flags = frame->vol_flags; |
|
|
data.vop_flags = frame->vop_flags; |
|
|
data.motion_flags = frame->motion_flags; |
|
|
|
|
678 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
679 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
680 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_USER; |
759 |
*type = data.type; |
*type = data.type; |
760 |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
761 |
|
|
762 |
|
frame->vol_flags = data.vol_flags; |
763 |
|
frame->vop_flags = data.vop_flags; |
764 |
|
frame->motion_flags = data.motion_flags; |
765 |
|
|
766 |
|
} else if (opt == XVID_PLG_FRAME) { |
767 |
|
|
768 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
769 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
770 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
776 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
777 |
} |
} |
778 |
} |
} |
779 |
|
frame->mbs[0].quant = data.quant; /* BEFORE2 will not affect the quant in stats */ |
|
frame->vol_flags = data.vol_flags; |
|
|
frame->vop_flags = data.vop_flags; |
|
|
frame->motion_flags = data.motion_flags; |
|
780 |
} |
} |
781 |
|
|
782 |
|
|
783 |
} |
} |
784 |
|
|
785 |
|
|
943 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
944 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
945 |
|
|
946 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant); |
947 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
948 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
949 |
|
|
1281 |
|
|
1282 |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1283 |
{ |
{ |
1284 |
unsigned int i,j; |
unsigned int i; |
1285 |
int quant = frame->quant; |
MACROBLOCK * pMB = frame->mbs; |
1286 |
|
int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */ |
1287 |
if (quant > 31) |
if (quant > 31) |
1288 |
frame->quant = quant = 31; |
frame->quant = quant = 31; |
1289 |
else if (quant < 1) |
else if (quant < 1) |
1290 |
frame->quant = quant = 1; |
frame->quant = quant = 1; |
1291 |
|
|
1292 |
for (j=0; j<pParam->mb_height; j++) |
for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) { |
|
for (i=0; i<pParam->mb_width; i++) { |
|
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
|
1293 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1294 |
if (quant > 31) |
if (quant > 31) |
1295 |
quant = 31; |
quant = 31; |
1296 |
else if (quant < 1) |
else if (quant < 1) |
1297 |
quant = 1; |
quant = 1; |
1298 |
pMB->quant = quant; |
pMB->quant = quant; |
1299 |
|
pMB++; |
1300 |
} |
} |
1301 |
} |
} |
1302 |
|
|
1352 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1353 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1354 |
|
|
1355 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1356 |
|
|
1357 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1358 |
|
|
1359 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1362 |
|
|
1363 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1364 |
|
|
1365 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1366 |
|
|
1367 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1368 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1484 |
|
|
1485 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1486 |
|
|
1487 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1488 |
|
|
1489 |
SetMacroblockQuants(&pEnc->mbParam, current); |
SetMacroblockQuants(&pEnc->mbParam, current); |
1490 |
|
|
1555 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1556 |
} |
} |
1557 |
|
|
1558 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1559 |
|
|
1560 |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1561 |
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1776 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1777 |
|
|
1778 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
1779 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant); |
1780 |
|
|
1781 |
/* copy reference frame details into the current frame */ |
/* copy reference frame details into the current frame */ |
1782 |
current->quant = reference->quant; |
current->quant = reference->quant; |
1883 |
pEnc->current->is_interpolated = 0; |
pEnc->current->is_interpolated = 0; |
1884 |
} |
} |
1885 |
|
|
1886 |
|
frame->coding_type = B_VOP; |
1887 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1888 |
|
|
1889 |
start_timer(); |
start_timer(); |
1890 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1891 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
1896 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1897 |
stop_motion_timer(); |
stop_motion_timer(); |
1898 |
|
|
|
frame->coding_type = B_VOP; |
|
|
|
|
1899 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1900 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
1901 |
|
|
1902 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
1903 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |