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.30 2003-06-13 12:11:27 suxen_drol Exp $ |
* $Id: encoder.c,v 1.95.2.35 2003-07-29 23:02:29 edgomez Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
38 |
#include "image/font.h" |
#include "image/font.h" |
39 |
#include "motion/sad.h" |
#include "motion/sad.h" |
40 |
#include "motion/motion.h" |
#include "motion/motion.h" |
41 |
|
#include "motion/gmc.h" |
42 |
|
|
43 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
44 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
45 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
904 |
|
|
905 |
if (xFrame->quant_intra_matrix) |
if (xFrame->quant_intra_matrix) |
906 |
{ |
{ |
907 |
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)); |
908 |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
909 |
} |
} |
910 |
|
|
911 |
if (xFrame->quant_inter_matrix) |
if (xFrame->quant_inter_matrix) |
912 |
{ |
{ |
913 |
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)); |
914 |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
915 |
} |
} |
916 |
|
|
1196 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1197 |
} |
} |
1198 |
|
|
1199 |
|
pEnc->iFrameNum = 1; |
1200 |
|
|
1201 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1202 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1356 |
|
|
1357 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1358 |
|
|
1359 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1360 |
|
|
1361 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1362 |
|
|
1363 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1396 |
} |
} |
1397 |
emms(); |
emms(); |
1398 |
|
|
1399 |
/* 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() */ |
1400 |
#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 |
|
1401 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1402 |
|
|
1403 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1424 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1425 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1426 |
|
|
|
int mb_width = pEnc->mbParam.mb_width; |
|
|
int mb_height = pEnc->mbParam.mb_height; |
|
|
|
|
1427 |
int iLimit; |
int iLimit; |
1428 |
int x, y, k; |
int x, y, k; |
1429 |
int iSearchRange; |
int iSearchRange; |
1430 |
int bIntra, skip_possible; |
int bIntra=0, skip_possible; |
1431 |
|
FRAMEINFO *const current = pEnc->current; |
1432 |
|
FRAMEINFO *const reference = pEnc->reference; |
1433 |
|
MBParam * const pParam = &pEnc->mbParam; |
1434 |
|
int mb_width = pParam->mb_width; |
1435 |
|
int mb_height = pParam->mb_height; |
1436 |
|
|
|
/* IMAGE *pCurrent = &pEnc->current->image; */ |
|
|
IMAGE *pRef = &pEnc->reference->image; |
|
1437 |
|
|
1438 |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
/* IMAGE *pCurrent = ¤t->image; */ |
1439 |
|
IMAGE *pRef = &reference->image; |
1440 |
|
|
1441 |
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
1442 |
{ |
{ |
1443 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pParam->width + 31) / 32; |
1444 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pParam->height + 31) / 32; |
1445 |
} |
} |
1446 |
|
|
1447 |
|
|
1448 |
start_timer(); |
start_timer(); |
1449 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1450 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pParam->width, pParam->height); |
1451 |
stop_edges_timer(); |
stop_edges_timer(); |
1452 |
|
|
1453 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
1454 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1455 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
current->fcode = pParam->m_fcode; |
1456 |
|
|
1457 |
if (!force_inter) |
if (!force_inter) |
1458 |
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
1459 |
else |
else |
1460 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1461 |
|
|
1462 |
if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1463 |
start_timer(); |
start_timer(); |
1464 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1465 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pParam->edged_width, |
1466 |
pEnc->mbParam.edged_height, |
pParam->edged_height, |
1467 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1468 |
pEnc->current->rounding_type); |
current->rounding_type); |
1469 |
stop_inter_timer(); |
stop_inter_timer(); |
1470 |
} |
} |
1471 |
|
|
1472 |
pEnc->current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1473 |
|
|
1474 |
|
|
1475 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, current); |
1476 |
|
|
1477 |
start_timer(); |
start_timer(); |
1478 |
bIntra = MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1479 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
{ int gmcval; |
1480 |
iLimit); |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
1481 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1482 |
|
|
1483 |
|
if (current->motion_flags & XVID_GME_REFINE) { |
1484 |
|
gmcval = GlobalMotionEstRefine(¤t->warp, |
1485 |
|
current->mbs, pParam, |
1486 |
|
current, reference, |
1487 |
|
¤t->image, |
1488 |
|
&reference->image, |
1489 |
|
&pEnc->vInterH, |
1490 |
|
&pEnc->vInterV, |
1491 |
|
&pEnc->vInterHV); |
1492 |
|
gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height); |
1493 |
|
} |
1494 |
|
|
1495 |
|
gmcval = globalSAD(¤t->warp, pParam, current->mbs, |
1496 |
|
current, |
1497 |
|
&reference->image, |
1498 |
|
¤t->image, |
1499 |
|
pEnc->vGMC.y); |
1500 |
|
gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); |
1501 |
|
|
1502 |
|
/* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ |
1503 |
|
generate_GMCparameters( 3, 3, ¤t->warp, |
1504 |
|
pParam->width, pParam->height, |
1505 |
|
¤t->new_gmc_data); |
1506 |
|
|
1507 |
|
if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) || |
1508 |
|
(current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) ) |
1509 |
|
{ |
1510 |
|
current->coding_type = S_VOP; |
1511 |
|
|
1512 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1513 |
|
pParam->mb_width, pParam->mb_height, |
1514 |
|
pParam->edged_width, pParam->edged_width/2, |
1515 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1516 |
|
current->rounding_type, current->mbs, &pEnc->vGMC); |
1517 |
|
|
1518 |
stop_motion_timer(); |
} else { |
1519 |
|
|
1520 |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1521 |
|
pParam->mb_width, pParam->mb_height, |
1522 |
|
pParam->edged_width, pParam->edged_width/2, |
1523 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1524 |
|
current->rounding_type, current->mbs, NULL); /* no warping, just AMV */ |
1525 |
|
} |
1526 |
|
} |
1527 |
|
|
1528 |
if ( ( pEnc->current->vol_flags & XVID_VOL_GMC ) |
bIntra = |
1529 |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
MotionEstimation(&pEnc->mbParam, current, reference, |
1530 |
{ |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1531 |
pEnc->current->coding_type = S_VOP; |
&pEnc->vGMC, iLimit); |
1532 |
|
|
|
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
&pEnc->current->gmc_data); |
|
|
|
|
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
|
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
|
|
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0, |
|
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
|
1533 |
|
|
1534 |
} |
stop_motion_timer(); |
1535 |
|
|
1536 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
1537 |
|
|
1538 |
|
set_timecodes(current,reference,pParam->fbase); |
1539 |
if (vol_header) |
if (vol_header) |
1540 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1541 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1542 |
} |
} |
1543 |
|
|
1544 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
1545 |
|
|
1546 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1547 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1548 |
|
|
1549 |
|
|
1550 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1551 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1552 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1553 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
|
|
|
|
/* 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 */ |
|
1554 |
|
|
1555 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1556 |
|
|
1557 |
if (bIntra) { |
if (bIntra) { |
1558 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1559 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1560 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1561 |
|
|
1562 |
start_timer(); |
start_timer(); |
1563 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(current, x, y, pParam->mb_width, qcoeff); |
1564 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1565 |
|
|
1566 |
pEnc->current->sStat.kblks++; |
current->sStat.kblks++; |
1567 |
|
|
1568 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1569 |
stop_coding_timer(); |
stop_coding_timer(); |
1570 |
continue; |
continue; |
1571 |
} |
} |
1572 |
|
|
|
if (pEnc->current->coding_type == S_VOP) { |
|
|
|
|
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
|
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
|
|
pEnc->mbParam.edged_width, 65536); |
|
|
|
|
|
if (pEnc->current->motion_flags & XVID_ME_CHROMA16) { |
|
|
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
|
|
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
|
|
|
|
|
iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
|
|
pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
|
|
} |
|
|
|
|
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
|
|
|
|
|
if ((pEnc->mbParam.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 */ |
|
|
} |
|
|
|
|
1573 |
start_timer(); |
start_timer(); |
1574 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &reference->image, |
1575 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1576 |
&pEnc->vInterHV, &pEnc->vGMC, |
&pEnc->vInterHV, &pEnc->vGMC, |
1577 |
&pEnc->current->image, |
¤t->image, |
1578 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pParam->width, |
1579 |
pEnc->mbParam.height, |
pParam->height, |
1580 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1581 |
(pEnc->current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
1582 |
(pEnc->current->vop_flags & XVID_VOP_REDUCED), |
(current->vop_flags & XVID_VOP_REDUCED), |
1583 |
pEnc->current->rounding_type); |
current->rounding_type); |
1584 |
|
|
1585 |
stop_comp_timer(); |
stop_comp_timer(); |
1586 |
|
|
1592 |
|
|
1593 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1594 |
{ pMB->cbp = |
{ pMB->cbp = |
1595 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
1596 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1597 |
} |
} |
1598 |
|
|
1599 |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1600 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1601 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1602 |
pEnc->current->sStat.mblks++; |
current->sStat.mblks++; |
1603 |
} else { |
} else { |
1604 |
pEnc->current->sStat.ublks++; |
current->sStat.ublks++; |
1605 |
} |
} |
1606 |
|
|
1607 |
start_timer(); |
start_timer(); |
1611 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1612 |
(pMB->dquant == 0); |
(pMB->dquant == 0); |
1613 |
|
|
1614 |
if (pEnc->current->coding_type == S_VOP) |
if (current->coding_type == S_VOP) |
1615 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1616 |
else if (pEnc->current->coding_type == P_VOP) { |
else if (current->coding_type == P_VOP) { |
1617 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) |
1618 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1619 |
else |
else |
1620 |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1624 |
|
|
1625 |
/* 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 */ |
1626 |
|
|
1627 |
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
if (current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1628 |
{ |
{ |
1629 |
int bSkip = 1; |
int bSkip = 1; |
1630 |
|
|
1631 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1632 |
{ |
{ |
1633 |
int iSAD; |
int iSAD; |
1634 |
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1635 |
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1636 |
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
pParam->edged_width,BFRAME_SKIP_THRESHHOLD); |
1637 |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
1638 |
{ bSkip = 0; |
{ bSkip = 0; |
1639 |
break; |
break; |
1641 |
} |
} |
1642 |
|
|
1643 |
if (!bSkip) { /* no SKIP, but trivial block */ |
if (!bSkip) { /* no SKIP, but trivial block */ |
1644 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1645 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1646 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1647 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1648 |
} |
} |
1649 |
else { |
else { |
1650 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1651 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1652 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1653 |
} |
} |
1654 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1655 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1656 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1657 |
stop_coding_timer(); |
stop_coding_timer(); |
1658 |
|
|
1659 |
continue; /* next MB */ |
continue; /* next MB */ |
1668 |
} |
} |
1669 |
/* ordinary case: normal coded INTER/INTER4V block */ |
/* ordinary case: normal coded INTER/INTER4V block */ |
1670 |
|
|
1671 |
if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE)) |
if ((current->vop_flags & XVID_VOP_GREYSCALE)) |
1672 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1673 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1674 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1675 |
} |
} |
1676 |
|
|
1677 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1678 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1679 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1680 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1681 |
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); |
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); |
1682 |
} else { |
} else { |
1683 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1684 |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1685 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1686 |
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); |
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); |
1691 |
{ int k; |
{ int k; |
1692 |
for (k=1;k<4;k++) |
for (k=1;k<4;k++) |
1693 |
{ |
{ |
1694 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1695 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k); |
1696 |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1697 |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1698 |
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); |
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); |
1699 |
} else { |
} else { |
1700 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k); |
1701 |
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1702 |
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1703 |
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); |
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); |
1706 |
} |
} |
1707 |
} |
} |
1708 |
|
|
1709 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1710 |
stop_coding_timer(); |
stop_coding_timer(); |
1711 |
|
|
1712 |
} |
} |
1713 |
} |
} |
1714 |
|
|
1715 |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
if ((current->vop_flags & XVID_VOP_REDUCED)) |
1716 |
{ |
{ |
1717 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(¤t->image, pParam->edged_width, |
1718 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
current->mbs, mb_width, mb_height, pParam->mb_width, |
1719 |
16, 0); |
16, 0); |
1720 |
} |
} |
1721 |
|
|
1722 |
emms(); |
emms(); |
1723 |
|
|
1724 |
if (pEnc->current->sStat.iMvCount == 0) |
if (current->sStat.iMvCount == 0) |
1725 |
pEnc->current->sStat.iMvCount = 1; |
current->sStat.iMvCount = 1; |
1726 |
|
|
1727 |
fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); |
1728 |
|
|
1729 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pParam->m_fcode); |
1730 |
|
|
1731 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1732 |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
&& (pParam->m_fcode <= (3 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
1733 |
{ |
{ |
1734 |
pEnc->mbParam.m_fcode++; |
pParam->m_fcode++; |
1735 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1736 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1737 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
1738 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
1739 |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
&& (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
1740 |
{ |
{ |
1741 |
pEnc->mbParam.m_fcode--; |
pParam->m_fcode--; |
1742 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1743 |
} |
} |
1744 |
|
|
1746 |
|
|
1747 |
/* frame drop code */ |
/* frame drop code */ |
1748 |
#if 0 |
#if 0 |
1749 |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
1750 |
#endif |
#endif |
1751 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (current->sStat.kblks + current->sStat.mblks < |
1752 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
1753 |
{ |
{ |
1754 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1755 |
pEnc->current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1756 |
|
|
1757 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1758 |
|
|
1759 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(current,reference,pParam->fbase); |
1760 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0); |
1761 |
|
|
1762 |
/* copy reference frame details into the current frame */ |
/* copy reference frame details into the current frame */ |
1763 |
pEnc->current->quant = pEnc->reference->quant; |
current->quant = reference->quant; |
1764 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
current->motion_flags = reference->motion_flags; |
1765 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1766 |
pEnc->current->fcode = pEnc->reference->fcode; |
current->fcode = reference->fcode; |
1767 |
pEnc->current->bcode = pEnc->reference->bcode; |
current->bcode = reference->bcode; |
1768 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1769 |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1770 |
} |
} |
1771 |
|
|
1772 |
/* XXX: debug |
/* XXX: debug |
1773 |
{ |
{ |
1774 |
char s[100]; |
char s[100]; |
1775 |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
1776 |
image_dump_yuvpgm(&pEnc->current->image, |
image_dump_yuvpgm(¤t->image, |
1777 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1778 |
pEnc->mbParam.width, pEnc->mbParam.height, s); |
pParam->width, pParam->height, s); |
1779 |
|
|
1780 |
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
1781 |
image_dump_yuvpgm(&pEnc->reference->image, |
image_dump_yuvpgm(&reference->image, |
1782 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1783 |
pEnc->mbParam.width, pEnc->mbParam.height, s); |
pParam->width, pParam->height, s); |
1784 |
} |
} |
1785 |
*/ |
*/ |
1786 |
|
|
1787 |
/* 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 ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1788 |
|
|
1789 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1790 |
|
|
1791 |
return 0; /* inter */ |
return 0; /* inter */ |
1792 |
} |
} |
1905 |
|
|
1906 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1907 |
|
|
1908 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1909 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1910 |
|
|
1911 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |