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.9 2002-10-02 12:57:25 suxen_drol Exp $ |
* $Id: encoder.c,v 1.76.2.13 2002-10-17 19:10:57 Isibaar Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
880 |
|
|
881 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
882 |
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, |
883 |
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
"%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp); |
884 |
} |
} |
885 |
|
|
886 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
971 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
972 |
|
|
973 |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
974 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
975 |
input_valid = 0; |
input_valid = 0; |
976 |
goto ipvop_loop; |
goto ipvop_loop; |
977 |
} |
} |
998 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
999 |
|
|
1000 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1001 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1002 |
input_valid = 0; |
input_valid = 0; |
1003 |
goto ipvop_loop; |
goto ipvop_loop; |
1004 |
} |
} |
1151 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1152 |
} |
} |
1153 |
|
|
1154 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1155 |
|
pEnc->mbParam.m_quarterpel = 1; |
1156 |
|
else |
1157 |
|
pEnc->mbParam.m_quarterpel = 0; |
1158 |
|
|
1159 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1160 |
int *temp_dquants = |
int *temp_dquants = |
1161 |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
1518 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1519 |
} |
} |
1520 |
|
|
1521 |
|
#define XVID_ID "XviD" XVID_BS_VERSION |
1522 |
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
1523 |
|
|
1524 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1525 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1526 |
|
|
1588 |
int iLimit; |
int iLimit; |
1589 |
int x, y, k; |
int x, y, k; |
1590 |
int iSearchRange; |
int iSearchRange; |
1591 |
int bIntra; |
int bIntra, skip_possible; |
1592 |
|
|
1593 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1594 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1614 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1615 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1616 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1617 |
|
pEnc->mbParam.m_quarterpel, |
1618 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1619 |
stop_inter_timer(); |
stop_inter_timer(); |
1620 |
} |
} |
1622 |
start_timer(); |
start_timer(); |
1623 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1624 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1625 |
if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
if (bIntra == 0) { |
1626 |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
1627 |
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1628 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1629 |
|
} |
1630 |
|
|
1631 |
} else { |
} else { |
1632 |
|
|
1669 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1670 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1671 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1672 |
|
pEnc->mbParam.m_quarterpel, |
1673 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1674 |
stop_comp_timer(); |
stop_comp_timer(); |
1675 |
|
|
1715 |
|
|
1716 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1717 |
|
|
1718 |
if ((pMB->mode == MODE_NOT_CODED) || |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
1719 |
(pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
(pMB->dquant == NO_CHANGE); |
1720 |
pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) { |
|
1721 |
|
if(pEnc->mbParam.m_quarterpel) |
1722 |
|
skip_possible &= (pMB->qmvs[0].x == 0) & (pMB->qmvs[0].y == 0); |
1723 |
|
else |
1724 |
|
skip_possible &= (pMB->mvs[0].x == 0) & (pMB->mvs[0].y == 0); |
1725 |
|
|
1726 |
|
if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1727 |
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
1728 |
|
|
1729 |
int bSkip = 1; |
int bSkip = 1; |
1743 |
if (!bSkip) |
if (!bSkip) |
1744 |
{ |
{ |
1745 |
VECTOR predMV; |
VECTOR predMV; |
1746 |
|
if(pEnc->mbParam.m_quarterpel) |
1747 |
|
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1748 |
|
else |
1749 |
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); |
1750 |
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
1751 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1781 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1782 |
|
|
1783 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1784 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128 |
1785 |
{ |
{ |
1786 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1787 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1788 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1789 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1790 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1791 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
1792 |
{ |
{ |
1793 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1794 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1860 |
start_timer(); |
start_timer(); |
1861 |
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, |
1862 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1863 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
1864 |
stop_inter_timer(); |
stop_inter_timer(); |
1865 |
|
|
1866 |
// backward |
// backward |
1870 |
start_timer(); |
start_timer(); |
1871 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1872 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1873 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
1874 |
stop_inter_timer(); |
stop_inter_timer(); |
1875 |
|
|
1876 |
start_timer(); |
start_timer(); |