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.18 2002-11-12 14:44:53 syskin Exp $ |
* $Id: encoder.c,v 1.76.2.25 2002-12-08 06:43:34 suxen_drol Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
224 |
|
|
225 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
226 |
|
|
227 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
228 |
|
|
229 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
230 |
|
|
327 |
pEnc->global = pParam->global; |
pEnc->global = pParam->global; |
328 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
329 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
330 |
|
pEnc->bquant_offset = pParam->bquant_offset; |
331 |
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
332 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
333 |
|
|
729 |
|
|
730 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
731 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
732 |
pFrame->intra = 0; |
pFrame->intra = 2; |
733 |
|
|
734 |
if (input_valid) |
if (input_valid) |
735 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
764 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
765 |
|
|
766 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
767 |
pFrame->intra = 0; |
pFrame->intra = 4; |
768 |
|
|
769 |
if (input_valid) |
if (input_valid) |
770 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
840 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
841 |
|
|
842 |
BitstreamPutBits(&bs, 0x7f, 8); |
BitstreamPutBits(&bs, 0x7f, 8); |
843 |
pFrame->intra = 0; |
pFrame->intra = 5; |
844 |
} |
} |
845 |
|
|
846 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
926 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
927 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
928 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
929 |
|
pEnc->iFrameNum++; |
930 |
|
|
931 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
932 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
939 |
/* |
/* |
940 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
941 |
*/ |
*/ |
942 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
943 |
|
pEnc->mbParam.m_quarterpel = 1; |
944 |
|
else |
945 |
|
pEnc->mbParam.m_quarterpel = 0; |
946 |
|
|
947 |
|
if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
948 |
|
|
949 |
|
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
950 |
|
if (pFrame->quant_intra_matrix != NULL) |
951 |
|
set_intra_matrix(pFrame->quant_intra_matrix); |
952 |
|
if (pFrame->quant_inter_matrix != NULL) |
953 |
|
set_inter_matrix(pFrame->quant_inter_matrix); |
954 |
|
} |
955 |
|
|
956 |
|
|
957 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
958 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1000 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
1001 |
*/ |
*/ |
1002 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
1003 |
|
// } else if (pFrame->intra == 0 || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
1004 |
/* |
/* |
1005 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
1006 |
*/ |
*/ |
1034 |
} |
} |
1035 |
|
|
1036 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1037 |
pEnc->current->quant = |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1038 |
((pEnc->reference->quant + |
pEnc->bquant_ratio) / 2) + pEnc->bquant_offset)/100; |
1039 |
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
|
1040 |
} else { |
} else { |
1041 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1042 |
} |
} |
1043 |
|
|
1044 |
if (pEnc->current->quant < 1) |
if (pEnc->current->quant < 1) |
1045 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
1046 |
|
else if (pEnc->current->quant > 31) |
|
if (pEnc->current->quant > 31) |
|
1047 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
1048 |
|
|
|
|
|
1049 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1050 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1051 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1052 |
|
|
|
|
|
|
|
|
1053 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1054 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1055 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
1056 |
|
|
1057 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1058 |
|
|
1059 |
pFrame->intra = 0; |
// bframe report by koepi |
1060 |
|
pFrame->intra = 2; |
1061 |
pFrame->length = 0; |
pFrame->length = 0; |
1062 |
|
|
1063 |
input_valid = 0; |
input_valid = 0; |
1064 |
goto bvop_loop; |
goto bvop_loop; |
1065 |
} |
} |
1066 |
|
|
|
pEnc->iFrameNum++; |
|
|
|
|
1067 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1068 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1069 |
|
|
1070 |
if (pResult) { |
if (pResult) { |
1071 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1072 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1073 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1074 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1075 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
1076 |
} |
} |
1077 |
|
|
1078 |
emms(); |
emms(); |
1257 |
|
|
1258 |
if (pResult) { |
if (pResult) { |
1259 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1260 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1261 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1262 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1263 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
1264 |
} |
} |
1265 |
|
|
1266 |
emms(); |
emms(); |
1545 |
|
|
1546 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1547 |
|
|
1548 |
pEnc->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1549 |
pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1550 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1551 |
|
|
1552 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
1553 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1569 |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1570 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1571 |
} |
} |
1572 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1573 |
stop_coding_timer(); |
stop_coding_timer(); |
1574 |
} |
} |
1575 |
|
|
1576 |
emms(); |
emms(); |
1577 |
|
|
1578 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1579 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
1580 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1581 |
|
|
1582 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1677 |
|
|
1678 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1679 |
|
|
1680 |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1681 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1682 |
|
|
1683 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1684 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1728 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1729 |
|
|
1730 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1731 |
pEnc->sStat.kblks++; |
pEnc->current->sStat.kblks++; |
1732 |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1733 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1734 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1735 |
pEnc->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
1736 |
} else { |
} else { |
1737 |
pEnc->sStat.ublks++; |
pEnc->current->sStat.ublks++; |
1738 |
} |
} |
1739 |
|
|
1740 |
start_timer(); |
start_timer(); |
1784 |
} |
} |
1785 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1786 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1787 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1788 |
} |
} |
1789 |
else |
else |
1790 |
{ |
{ |
1798 |
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 */ |
1799 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1800 |
} |
} |
1801 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1802 |
} |
} |
1803 |
|
|
1804 |
stop_coding_timer(); |
stop_coding_timer(); |
1811 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1812 |
} |
} |
1813 |
|
|
1814 |
if (pEnc->sStat.iMvCount == 0) |
if (pEnc->current->sStat.iMvCount == 0) |
1815 |
pEnc->sStat.iMvCount = 1; |
pEnc->current->sStat.iMvCount = 1; |
1816 |
|
|
1817 |
fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
1818 |
|
|
1819 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1820 |
|
|
1824 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1825 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1826 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1827 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
1828 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
1829 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
1830 |
{ |
{ |
1831 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1832 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1833 |
} |
} |
1834 |
|
|
1835 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
1836 |
|
|
|
#ifdef FRAMEDROP |
|
1837 |
/* frame drop code */ |
/* frame drop code */ |
1838 |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
1839 |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
1840 |
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1841 |
{ |
{ |
1842 |
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; |
1843 |
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
pEnc->current->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1844 |
|
|
1845 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1846 |
|
|
1858 |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1859 |
|
|
1860 |
} |
} |
|
#endif |
|
1861 |
|
|
1862 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1863 |
|
|
1865 |
} |
} |
1866 |
|
|
1867 |
|
|
1868 |
static __inline void |
static void |
1869 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
1870 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
1871 |
Bitstream * bs, |
Bitstream * bs, |
1937 |
|
|
1938 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1939 |
|
|
1940 |
pEnc->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
1941 |
pEnc->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
1942 |
pEnc->sStat.iMvCount = 0; |
frame->sStat.iMvCount = 0; |
1943 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
1944 |
|
|
1945 |
|
|
1946 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1978 |
#endif |
#endif |
1979 |
start_timer(); |
start_timer(); |
1980 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1981 |
&pEnc->sStat, direction); |
&frame->sStat, direction); |
1982 |
stop_coding_timer(); |
stop_coding_timer(); |
1983 |
} |
} |
1984 |
} |
} |