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.110 2004-12-05 13:56:13 syskin Exp $ |
* $Id: encoder.c,v 1.115 2004-12-10 04:10:12 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
1479 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1480 |
|
|
1481 |
start_timer(); |
start_timer(); |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
1482 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1483 |
stop_coding_timer(); |
stop_coding_timer(); |
1484 |
} |
} |
1498 |
return 1; /* intra */ |
return 1; /* intra */ |
1499 |
} |
} |
1500 |
|
|
1501 |
|
static __inline void |
1502 |
|
updateFcode(Statistics * sStat, Encoder * pEnc) |
1503 |
|
{ |
1504 |
|
float fSigma; |
1505 |
|
int iSearchRange; |
1506 |
|
|
1507 |
#define INTRA_THRESHOLD 0.5 |
if (sStat->iMvCount == 0) |
1508 |
#define BFRAME_SKIP_THRESHHOLD 30 |
sStat->iMvCount = 1; |
1509 |
|
|
1510 |
|
fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); |
1511 |
|
|
1512 |
|
iSearchRange = 16 << pEnc->mbParam.m_fcode; |
1513 |
|
|
1514 |
|
if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) |
1515 |
|
pEnc->mbParam.m_fcode++; |
1516 |
|
|
1517 |
|
else if ((5.0 * fSigma < iSearchRange) |
1518 |
|
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1519 |
|
&& (pEnc->mbParam.m_fcode >= 2) ) |
1520 |
|
pEnc->mbParam.m_fcode--; |
1521 |
|
|
1522 |
|
pEnc->fMvPrevSigma = fSigma; |
1523 |
|
} |
1524 |
|
|
1525 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1526 |
|
|
1527 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1528 |
static int |
static int |
1529 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1530 |
Bitstream * bs) |
Bitstream * bs) |
1531 |
{ |
{ |
|
float fSigma; |
|
1532 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1533 |
|
|
1534 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1535 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1536 |
|
|
1537 |
int x, y, k; |
int x, y, k; |
|
int iSearchRange; |
|
1538 |
int skip_possible; |
int skip_possible; |
1539 |
FRAMEINFO *const current = pEnc->current; |
FRAMEINFO *const current = pEnc->current; |
1540 |
FRAMEINFO *const reference = pEnc->reference; |
FRAMEINFO *const reference = pEnc->reference; |
1543 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1544 |
int coded = 1; |
int coded = 1; |
1545 |
|
|
|
|
|
|
/* IMAGE *pCurrent = ¤t->image; */ |
|
1546 |
IMAGE *pRef = &reference->image; |
IMAGE *pRef = &reference->image; |
1547 |
|
|
1548 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1570 |
} |
} |
1571 |
} |
} |
1572 |
|
|
1573 |
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1574 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1575 |
|
|
1576 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1577 |
|
|
1578 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1641 |
|
|
1642 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1643 |
|
|
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
|
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
|
|
|
|
|
|
|
1644 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1645 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1646 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1647 |
¤t->mbs[x + y * pParam->mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
1648 |
|
|
1649 |
int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
|
|
|
if (bIntra) { |
|
1650 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1651 |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1652 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1657 |
|
|
1658 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1659 |
|
|
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
1660 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1661 |
stop_coding_timer(); |
stop_coding_timer(); |
1662 |
continue; |
continue; |
1677 |
|
|
1678 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1679 |
|
|
1680 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->cbp != 0) { |
1681 |
{ pMB->cbp = |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
1682 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1683 |
} |
} |
1684 |
|
|
1758 |
} |
} |
1759 |
/* ordinary case: normal coded INTER/INTER4V block */ |
/* ordinary case: normal coded INTER/INTER4V block */ |
1760 |
|
|
|
if ((current->vop_flags & XVID_VOP_GREYSCALE)) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
|
|
|
1761 |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1762 |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1763 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1797 |
} |
} |
1798 |
|
|
1799 |
emms(); |
emms(); |
1800 |
|
updateFcode(¤t->sStat, pEnc); |
|
if (current->sStat.iMvCount == 0) |
|
|
current->sStat.iMvCount = 1; |
|
|
|
|
|
fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); |
|
|
|
|
|
iSearchRange = 1 << (3 + pParam->m_fcode); |
|
|
|
|
|
if ((fSigma > iSearchRange / 3) |
|
|
&& (pParam->m_fcode <= (3 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
|
|
{ |
|
|
pParam->m_fcode++; |
|
|
iSearchRange *= 2; |
|
|
} else if ((fSigma < iSearchRange / 6) |
|
|
&& (pEnc->fMvPrevSigma >= 0) |
|
|
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
|
|
&& (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
|
|
{ |
|
|
pParam->m_fcode--; |
|
|
iSearchRange /= 2; |
|
|
} |
|
|
|
|
|
pEnc->fMvPrevSigma = fSigma; |
|
1801 |
|
|
1802 |
/* frame drop code */ |
/* frame drop code */ |
1803 |
#if 0 |
#if 0 |
1954 |
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
1955 |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
1956 |
} |
} |
|
|
|
1957 |
continue; |
continue; |
1958 |
} |
} |
1959 |
|
|
1960 |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
mb->quant = frame->quant; |
1961 |
|
|
1962 |
|
if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1963 |
|
/* we have to motion-compensate, transfer etc, |
1964 |
|
because there might be blocks to code */ |
1965 |
|
|
1966 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1967 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1968 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1969 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1970 |
dct_codes); |
dct_codes); |
1971 |
|
|
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
|
|
mb->quant = frame->quant; |
|
|
|
|
|
if (mb->mode != MODE_DIRECT_NONE_MV) |
|
1972 |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
|
|
|
|
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
|
|
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
|
|
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
|
|
} |
|
1973 |
} |
} |
1974 |
|
|
1975 |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
if (mb->mode == MODE_DIRECT_NO4V) |
1976 |
* coding function for BFrames, that's why we don't zero teh DC |
mb->mode = MODE_DIRECT; |
1977 |
* coeffs */ |
|
1978 |
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) |
1979 |
|
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
1980 |
|
else |
1981 |
|
if (frame->vop_flags & XVID_VOP_GREYSCALE) |
1982 |
|
/* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ |
1983 |
mb->cbp &= 0x3C; |
mb->cbp &= 0x3C; |
1984 |
|
|
1985 |
start_timer(); |
start_timer(); |
1991 |
|
|
1992 |
emms(); |
emms(); |
1993 |
|
|
|
/* TODO: dynamic fcode/bcode ??? */ |
|
|
|
|
1994 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1995 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1996 |
|
|