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.102 2004-03-22 22:36:23 edgomez Exp $ |
* $Id: encoder.c,v 1.111 2004-12-08 12:43:48 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
57 |
Bitstream * bs); |
Bitstream * bs); |
58 |
|
|
59 |
static int FrameCodeP(Encoder * pEnc, |
static int FrameCodeP(Encoder * pEnc, |
60 |
Bitstream * bs, |
Bitstream * bs); |
|
bool force_inter, |
|
|
bool vol_header); |
|
61 |
|
|
62 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
63 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
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 |
|
|
132 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
133 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
134 |
|
|
135 |
|
if (create->width<=0 || create->height<=0) |
136 |
|
return XVID_ERR_FAIL; |
137 |
|
|
138 |
/* allocate encoder struct */ |
/* allocate encoder struct */ |
139 |
|
|
140 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
1098 |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1099 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1100 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1101 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1102 |
|
|
1103 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs); |
1104 |
|
|
1105 |
|
|
1106 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
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 |
|
|
1349 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1350 |
} |
} |
1351 |
|
|
1352 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1353 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1354 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1355 |
|
goto done; |
1356 |
|
} |
1357 |
} |
} |
1358 |
|
|
1359 |
|
|
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() */ |
1511 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1512 |
static int |
static int |
1513 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1514 |
Bitstream * bs, |
Bitstream * bs) |
|
bool force_inter, |
|
|
bool vol_header) |
|
1515 |
{ |
{ |
1516 |
float fSigma; |
float fSigma; |
1517 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1519 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1520 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1521 |
|
|
|
int iLimit; |
|
1522 |
int x, y, k; |
int x, y, k; |
1523 |
int iSearchRange; |
int iSearchRange; |
1524 |
int bIntra=0, skip_possible; |
int skip_possible; |
1525 |
FRAMEINFO *const current = pEnc->current; |
FRAMEINFO *const current = pEnc->current; |
1526 |
FRAMEINFO *const reference = pEnc->reference; |
FRAMEINFO *const reference = pEnc->reference; |
1527 |
MBParam * const pParam = &pEnc->mbParam; |
MBParam * const pParam = &pEnc->mbParam; |
1528 |
int mb_width = pParam->mb_width; |
int mb_width = pParam->mb_width; |
1529 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1530 |
|
int coded = 1; |
1531 |
|
|
1532 |
|
|
1533 |
/* IMAGE *pCurrent = ¤t->image; */ |
/* IMAGE *pCurrent = ¤t->image; */ |
1534 |
IMAGE *pRef = &reference->image; |
IMAGE *pRef = &reference->image; |
1535 |
|
|
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
mb_width = (pParam->width + 31) / 32; |
|
|
mb_height = (pParam->height + 31) / 32; |
|
|
} |
|
|
|
|
|
|
|
1536 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1537 |
start_timer(); |
start_timer(); |
1538 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1545 |
current->rounding_type = pParam->m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1546 |
current->fcode = pParam->m_fcode; |
current->fcode = pParam->m_fcode; |
1547 |
|
|
|
if (!force_inter) |
|
|
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
|
|
else |
|
|
iLimit = mb_width * mb_height + 1; |
|
|
|
|
1548 |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1549 |
if (reference->is_interpolated != current->rounding_type) { |
if (reference->is_interpolated != current->rounding_type) { |
1550 |
start_timer(); |
start_timer(); |
1558 |
} |
} |
1559 |
} |
} |
1560 |
|
|
1561 |
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1562 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1563 |
|
|
1564 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1565 |
|
|
1566 |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1618 |
} |
} |
1619 |
} |
} |
1620 |
|
|
|
bIntra = |
|
1621 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1622 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1623 |
&pEnc->vGMC, iLimit); |
&pEnc->vGMC, 256*4096); |
1624 |
|
|
1625 |
|
|
1626 |
stop_motion_timer(); |
stop_motion_timer(); |
1627 |
|
|
|
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
|
|
|
|
1628 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
|
if (vol_header) |
|
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, current); |
|
|
BitstreamPad(bs); |
|
|
} |
|
1629 |
|
|
1630 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1631 |
|
|
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
|
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
|
|
|
|
|
|
|
1632 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1633 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1634 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1635 |
¤t->mbs[x + y * pParam->mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
1636 |
|
|
1637 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1638 |
|
|
1639 |
if (bIntra) { |
if (bIntra) { |
1640 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1666 |
pParam->height, |
pParam->height, |
1667 |
pParam->edged_width, |
pParam->edged_width, |
1668 |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
|
(current->vop_flags & XVID_VOP_REDUCED), |
|
1669 |
current->rounding_type); |
current->rounding_type); |
1670 |
|
|
1671 |
stop_comp_timer(); |
stop_comp_timer(); |
1798 |
} |
} |
1799 |
} |
} |
1800 |
|
|
|
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); |
|
|
} |
|
|
|
|
1801 |
emms(); |
emms(); |
1802 |
|
|
1803 |
if (current->sStat.iMvCount == 0) |
if (current->sStat.iMvCount == 0) |
1805 |
|
|
1806 |
fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); |
fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); |
1807 |
|
|
1808 |
iSearchRange = 1 << (3 + pParam->m_fcode); |
iSearchRange = 16 << pParam->m_fcode; |
1809 |
|
|
1810 |
if ((fSigma > iSearchRange / 3) |
if ((3.0 * fSigma > iSearchRange) && (pParam->m_fcode <= 5) ) |
|
&& (pParam->m_fcode <= (3 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
|
|
{ |
|
1811 |
pParam->m_fcode++; |
pParam->m_fcode++; |
1812 |
iSearchRange *= 2; |
|
1813 |
} else if ((fSigma < iSearchRange / 6) |
else if ((5.0 * fSigma < iSearchRange) |
1814 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1815 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pParam->m_fcode >= 2) ) /* minimum search range 32 */ |
|
&& (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
|
|
{ |
|
1816 |
pParam->m_fcode--; |
pParam->m_fcode--; |
|
iSearchRange /= 2; |
|
|
} |
|
1817 |
|
|
1818 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
1819 |
|
|
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; |
1839 |
current->rounding_type = reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1840 |
current->fcode = reference->fcode; |
current->fcode = reference->fcode; |
1841 |
current->bcode = reference->bcode; |
current->bcode = reference->bcode; |
1842 |
|
current->stamp = reference->stamp; |
1843 |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1844 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1845 |
} |
coded = 0; |
1846 |
|
|
1847 |
|
} else { |
1848 |
|
|
1849 |
pEnc->current->is_edged = 0; /* not edged */ |
pEnc->current->is_edged = 0; /* not edged */ |
1850 |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1855 |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1856 |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1857 |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1858 |
|
} |
1859 |
|
|
1860 |
/* XXX: debug |
/* XXX: debug |
1861 |
{ |
{ |
1876 |
|
|
1877 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1878 |
|
|
1879 |
return 0; /* inter */ |
return coded; |
1880 |
} |
} |
1881 |
|
|
1882 |
|
|
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 |
|
|
1976 |
continue; |
continue; |