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; |
1488 |
stop_coding_timer(); |
stop_coding_timer(); |
1489 |
} |
} |
1490 |
|
|
|
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); |
|
|
} |
|
1491 |
emms(); |
emms(); |
1492 |
|
|
1493 |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1503 |
return 1; /* intra */ |
return 1; /* intra */ |
1504 |
} |
} |
1505 |
|
|
1506 |
|
static __inline void |
1507 |
|
updateFcode(Statistics * sStat, Encoder * pEnc) |
1508 |
|
{ |
1509 |
|
float fSigma; |
1510 |
|
int iSearchRange; |
1511 |
|
|
1512 |
|
if (sStat->iMvCount == 0) |
1513 |
|
sStat->iMvCount = 1; |
1514 |
|
|
1515 |
#define INTRA_THRESHOLD 0.5 |
fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); |
1516 |
#define BFRAME_SKIP_THRESHHOLD 30 |
|
1517 |
|
iSearchRange = 16 << pEnc->mbParam.m_fcode; |
1518 |
|
|
1519 |
|
if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) |
1520 |
|
pEnc->mbParam.m_fcode++; |
1521 |
|
|
1522 |
|
else if ((5.0 * fSigma < iSearchRange) |
1523 |
|
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1524 |
|
&& (pEnc->mbParam.m_fcode >= 2) ) |
1525 |
|
pEnc->mbParam.m_fcode--; |
1526 |
|
|
1527 |
|
pEnc->fMvPrevSigma = fSigma; |
1528 |
|
} |
1529 |
|
|
1530 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1531 |
|
|
1532 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1533 |
static int |
static int |
1534 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1535 |
Bitstream * bs) |
Bitstream * bs) |
1536 |
{ |
{ |
|
float fSigma; |
|
1537 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1538 |
|
|
1539 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1540 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1541 |
|
|
1542 |
int x, y, k; |
int x, y, k; |
|
int iSearchRange; |
|
1543 |
int skip_possible; |
int skip_possible; |
1544 |
FRAMEINFO *const current = pEnc->current; |
FRAMEINFO *const current = pEnc->current; |
1545 |
FRAMEINFO *const reference = pEnc->reference; |
FRAMEINFO *const reference = pEnc->reference; |
1548 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1549 |
int coded = 1; |
int coded = 1; |
1550 |
|
|
|
|
|
|
/* IMAGE *pCurrent = ¤t->image; */ |
|
1551 |
IMAGE *pRef = &reference->image; |
IMAGE *pRef = &reference->image; |
1552 |
|
|
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
mb_width = (pParam->width + 31) / 32; |
|
|
mb_height = (pParam->height + 31) / 32; |
|
|
} |
|
|
|
|
|
|
|
1553 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1554 |
start_timer(); |
start_timer(); |
1555 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1575 |
} |
} |
1576 |
} |
} |
1577 |
|
|
1578 |
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1579 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1580 |
|
|
1581 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1582 |
|
|
1583 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1646 |
|
|
1647 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1648 |
|
|
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
|
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
|
|
|
|
|
|
|
1649 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1650 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1651 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1664 |
|
|
1665 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1666 |
|
|
1667 |
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) { |
1668 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1669 |
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 */ |
1670 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1671 |
} |
} |
1683 |
pParam->height, |
pParam->height, |
1684 |
pParam->edged_width, |
pParam->edged_width, |
1685 |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
|
(current->vop_flags & XVID_VOP_REDUCED), |
|
1686 |
current->rounding_type); |
current->rounding_type); |
1687 |
|
|
1688 |
stop_comp_timer(); |
stop_comp_timer(); |
1689 |
|
|
1690 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1691 |
|
|
1692 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->cbp != 0) { |
1693 |
{ pMB->cbp = |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
1694 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1695 |
} |
} |
1696 |
|
|
1814 |
} |
} |
1815 |
} |
} |
1816 |
|
|
|
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); |
|
|
} |
|
|
|
|
1817 |
emms(); |
emms(); |
1818 |
|
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; |
|
1819 |
|
|
1820 |
/* frame drop code */ |
/* frame drop code */ |
1821 |
#if 0 |
#if 0 |
1822 |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
1823 |
#endif |
#endif |
1824 |
if (current->sStat.kblks + current->sStat.mblks <= |
if (current->sStat.kblks + current->sStat.mblks <= |
1825 |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1826 |
|
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
1827 |
{ |
{ |
1828 |
current->sStat.kblks = current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1829 |
current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1900 |
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); \ |
1901 |
} |
} |
1902 |
|
|
|
/* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ |
|
|
|
|
1903 |
if (!first){ |
if (!first){ |
1904 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1905 |
} |
} |
1970 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1971 |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1972 |
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, |
1973 |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0); |
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
1974 |
} |
} |
|
|
|
1975 |
continue; |
continue; |
1976 |
} |
} |
1977 |
|
|
1978 |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
mb->quant = frame->quant; |
1979 |
|
|
1980 |
|
if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1981 |
|
/* we have to motion-compensate, transfer etc, |
1982 |
|
because there might be blocks to code */ |
1983 |
|
|
1984 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1985 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1986 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1987 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1988 |
dct_codes); |
dct_codes); |
1989 |
|
|
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
|
|
mb->quant = frame->quant; |
|
|
|
|
|
if (mb->mode != MODE_DIRECT_NONE_MV) |
|
1990 |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1991 |
|
} |
1992 |
|
|
1993 |
|
if (mb->mode == MODE_DIRECT_NO4V) |
1994 |
|
mb->mode = MODE_DIRECT; |
1995 |
|
|
1996 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) |
|
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
|
1997 |
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
|
} |
|
|
} |
|
1998 |
|
|
1999 |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
2000 |
* coding function for BFrames, that's why we don't zero teh DC |
* coding function for BFrames, that's why we don't zero teh DC |
2001 |
* coeffs */ |
* coeffs */ |
2002 |
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
if (frame->vop_flags & XVID_VOP_GREYSCALE) |
2003 |
mb->cbp &= 0x3C; |
mb->cbp &= 0x3C; |
2004 |
|
|
2005 |
start_timer(); |
start_timer(); |