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.105 2004-04-04 11:47:21 syskin Exp $ |
* $Id: encoder.c,v 1.115 2004-12-10 04:10:12 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
100 |
i--; |
i--; |
101 |
} |
} |
102 |
|
|
103 |
/* if neccessary, round to 65535 accuracy */ |
if (*base > 65535 || *inc > 65535) { |
104 |
if (*base > 65535) { |
int *biggest; |
105 |
float div = (float) *base / 65535; |
int *other; |
106 |
*base = (int) (*base / div); |
float div; |
107 |
*inc = (int) (*inc / div); |
|
108 |
|
if (*base > *inc) { |
109 |
|
biggest = base; |
110 |
|
other = inc; |
111 |
|
} else { |
112 |
|
biggest = inc; |
113 |
|
other = base; |
114 |
|
} |
115 |
|
|
116 |
|
div = ((float)*biggest)/((float)65535); |
117 |
|
*biggest = (int)(((float)*biggest)/div); |
118 |
|
*other = (int)(((float)*other)/div); |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
1317 |
|
|
1318 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1319 |
|
|
|
if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
|
|
pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; |
|
|
|
|
1320 |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1321 |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
1322 |
|
|
1444 |
|
|
1445 |
uint16_t x, y; |
uint16_t x, y; |
1446 |
|
|
|
if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
|
|
{ |
|
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
|
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
|
|
|
|
|
/* 16x16->8x8 downsample requires 1 additional edge pixel*/ |
|
|
/* XXX: setedges is overkill */ |
|
|
start_timer(); |
|
|
image_setedges(&pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height, 0); |
|
|
stop_edges_timer(); |
|
|
} |
|
|
|
|
1447 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1448 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1449 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
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 |
} |
} |
1485 |
|
|
|
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
|
|
16, 0); |
|
|
} |
|
1486 |
emms(); |
emms(); |
1487 |
|
|
1488 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
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 |
|
|
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
mb_width = (pParam->width + 31) / 32; |
|
|
mb_height = (pParam->height + 31) / 32; |
|
|
} |
|
|
|
|
|
|
|
1548 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1549 |
start_timer(); |
start_timer(); |
1550 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
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; |
1671 |
pParam->height, |
pParam->height, |
1672 |
pParam->edged_width, |
pParam->edged_width, |
1673 |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
|
(current->vop_flags & XVID_VOP_REDUCED), |
|
1674 |
current->rounding_type); |
current->rounding_type); |
1675 |
|
|
1676 |
stop_comp_timer(); |
stop_comp_timer(); |
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; |
1796 |
} |
} |
1797 |
} |
} |
1798 |
|
|
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
image_deblock_rrv(¤t->image, pParam->edged_width, |
|
|
current->mbs, mb_width, mb_height, pParam->mb_width, |
|
|
16, 0); |
|
|
} |
|
|
|
|
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 |
1882 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1883 |
} |
} |
1884 |
|
|
|
/* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ |
|
|
|
|
1885 |
if (!first){ |
if (!first){ |
1886 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1887 |
} |
} |
1952 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1953 |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
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, 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 |
|
|