39 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
41 |
* |
* |
42 |
* $Id: encoder.c,v 1.76.2.12 2002-10-05 21:36:35 Isibaar Exp $ |
* $Id: encoder.c,v 1.76.2.14 2002-11-02 15:52:30 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
54 |
#include "global.h" |
#include "global.h" |
55 |
#include "utils/timer.h" |
#include "utils/timer.h" |
56 |
#include "image/image.h" |
#include "image/image.h" |
|
#ifdef BFRAMES |
|
57 |
#include "image/font.h" |
#include "image/font.h" |
58 |
#include "motion/sad.h" |
#include "motion/sad.h" |
|
#endif |
|
59 |
#include "motion/motion.h" |
#include "motion/motion.h" |
60 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
61 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
1507 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1508 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1509 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1510 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1511 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1512 |
|
|
1513 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1517 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1518 |
} |
} |
1519 |
|
|
1520 |
|
#define XVID_ID "XviD" XVID_BS_VERSION |
1521 |
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
1522 |
|
|
1523 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1524 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1525 |
|
|
1599 |
|
|
1600 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1601 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1602 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1603 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1604 |
|
|
1605 |
if (!force_inter) |
if (!force_inter) |
1619 |
stop_inter_timer(); |
stop_inter_timer(); |
1620 |
} |
} |
1621 |
|
|
1622 |
|
if (pEnc->current->global_flags & XVID_GMC) { |
1623 |
|
printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
1624 |
|
pEnc->current->coding_type = S_VOP; |
1625 |
|
} else |
1626 |
|
pEnc->current->coding_type = P_VOP; |
1627 |
|
|
1628 |
start_timer(); |
start_timer(); |
1629 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1630 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1640 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1641 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1642 |
iLimit); |
iLimit); |
|
|
|
1643 |
} |
} |
1644 |
stop_motion_timer(); |
stop_motion_timer(); |
1645 |
|
|
1646 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1647 |
|
|
1648 |
pEnc->current->coding_type = P_VOP; |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
1649 |
|
pEnc->current->coding_type = P_VOP; /* no global motion -> no GMC */ |
1650 |
|
|
1651 |
|
|
1652 |
if (vol_header) |
if (vol_header) |
1653 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1726 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1727 |
|
|
1728 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) |
1729 |
skip_possible &= (pMB->qmvs[0].x == 0) & (pMB->qmvs[0].y == 0); |
{ skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y); |
1730 |
|
} |
1731 |
else |
else |
1732 |
skip_possible &= (pMB->mvs[0].x == 0) & (pMB->mvs[0].y == 0); |
{ skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y); |
1733 |
|
} |
1734 |
|
|
1735 |
if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
|
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
|
1736 |
|
|
1737 |
|
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1738 |
int bSkip = 1; |
int bSkip = 1; |
|
pMB->mode = MODE_NOT_CODED; |
|
1739 |
|
|
1740 |
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1741 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1742 |
{ |
{ |
1743 |
int iSAD; |
int iSAD; |
1749 |
break; |
break; |
1750 |
} |
} |
1751 |
} |
} |
1752 |
|
|
1753 |
if (!bSkip) |
if (!bSkip) |
1754 |
{ |
{ |
1755 |
VECTOR predMV; |
VECTOR predMV; |
1756 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) { |
1757 |
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1758 |
else |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; /* with GMC, qmvs doesn't have to be (0,0)! */ |
1759 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1760 |
|
} |
1761 |
|
else { |
1762 |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1763 |
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */ |
1764 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1765 |
|
} |
1766 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1767 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1768 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1769 |
} |
} |
1770 |
else MBSkip(bs); |
else |
1771 |
|
{ |
1772 |
|
pMB->mode = MODE_NOT_CODED; |
1773 |
|
MBSkip(bs); |
1774 |
|
} |
1775 |
|
|
1776 |
} else { |
} else { |
1777 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1833 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
1834 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1835 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1836 |
|
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
1837 |
pEnc->current->fcode = pEnc->reference->fcode; |
pEnc->current->fcode = pEnc->reference->fcode; |
1838 |
pEnc->current->bcode = pEnc->reference->bcode; |
pEnc->current->bcode = pEnc->reference->bcode; |
1839 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1873 |
} |
} |
1874 |
#endif |
#endif |
1875 |
|
|
1876 |
|
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
1877 |
|
|
1878 |
// forward |
// forward |
1879 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1880 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |