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.64 2002-07-24 23:17:19 chl Exp $ |
* $Id: encoder.c,v 1.76 2002-09-03 17:25:18 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
56 |
#include "image/image.h" |
#include "image/image.h" |
57 |
#ifdef BFRAMES |
#ifdef BFRAMES |
58 |
#include "image/font.h" |
#include "image/font.h" |
59 |
|
#include "motion/sad.h" |
60 |
#endif |
#endif |
61 |
#include "motion/motion.h" |
#include "motion/motion.h" |
62 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
424 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
425 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
426 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
427 |
pEnc->last_pframe = 1; |
pEnc->last_pframe = 0; |
428 |
|
pEnc->last_sync = 0; |
429 |
#endif |
#endif |
430 |
|
|
431 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
621 |
} |
} |
622 |
|
|
623 |
|
|
|
#ifdef BFRAMES |
|
624 |
void inc_frame_num(Encoder * pEnc) |
void inc_frame_num(Encoder * pEnc) |
625 |
{ |
{ |
|
pEnc->iFrameNum++; |
|
626 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
627 |
|
|
628 |
|
#ifdef BFRAMES |
629 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
630 |
|
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
631 |
|
pEnc->mbParam.m_seconds = 1; // more than 1 second since last I or P is not supported. |
632 |
|
else |
633 |
|
pEnc->mbParam.m_seconds = 0; |
634 |
|
|
635 |
|
if (pEnc->current->coding_type != B_VOP) |
636 |
|
pEnc->last_sync = pEnc->mbParam.m_ticks; |
637 |
|
#else |
638 |
|
|
639 |
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
640 |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
641 |
} |
|
642 |
#endif |
#endif |
643 |
|
|
644 |
|
} |
645 |
|
|
646 |
|
|
647 |
#ifdef BFRAMES |
#ifdef BFRAMES |
648 |
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
838 |
|
|
839 |
pFrame->intra = 0; |
pFrame->intra = 0; |
840 |
|
|
841 |
BitstreamPutBits(&bs, 0x7f, 8); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
842 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
843 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
844 |
|
|
1056 |
goto bvop_loop; |
goto bvop_loop; |
1057 |
} |
} |
1058 |
|
|
1059 |
|
pEnc->iFrameNum++; |
1060 |
|
|
1061 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1062 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1063 |
|
|
1132 |
|
|
1133 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1134 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
|
#ifdef BFRAMES |
|
1135 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1136 |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
#endif |
|
1137 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1138 |
|
|
1139 |
start_timer(); |
start_timer(); |
1263 |
DEBUG(temp); |
DEBUG(temp); |
1264 |
#endif |
#endif |
1265 |
|
|
|
#ifdef BFRAMES |
|
1266 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
|
#else |
|
1267 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
|
#endif |
|
|
|
|
1268 |
|
|
1269 |
stop_global_timer(); |
stop_global_timer(); |
1270 |
write_timer(); |
write_timer(); |
1544 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1545 |
|
|
1546 |
start_timer(); |
start_timer(); |
1547 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1548 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1549 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1550 |
|
qcoeff[5*64+0]=0; |
1551 |
|
} |
1552 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1553 |
stop_coding_timer(); |
stop_coding_timer(); |
1554 |
} |
} |
1570 |
|
|
1571 |
|
|
1572 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1573 |
|
#define BFRAME_SKIP_THRESHHOLD 16 |
1574 |
|
|
1575 |
static int |
static int |
1576 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1585 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1586 |
|
|
1587 |
int iLimit; |
int iLimit; |
1588 |
uint32_t x, y; |
int x, y, k; |
1589 |
int iSearchRange; |
int iSearchRange; |
1590 |
int bIntra; |
int bIntra; |
1591 |
|
|
1713 |
} |
} |
1714 |
|
|
1715 |
start_timer(); |
start_timer(); |
1716 |
|
|
1717 |
|
/* Finished processing the MB, now check if to CODE or SKIP */ |
1718 |
|
|
1719 |
|
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
1720 |
|
pMB->mvs[0].y == 0) { |
1721 |
|
|
1722 |
|
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
1723 |
|
|
1724 |
|
#ifdef BFRAMES |
1725 |
|
int iSAD=BFRAME_SKIP_THRESHHOLD; |
1726 |
|
int bSkip=1; |
1727 |
|
|
1728 |
|
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1729 |
|
{ |
1730 |
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1731 |
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1732 |
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1733 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD) |
1734 |
|
{ bSkip = 0; |
1735 |
|
break; |
1736 |
|
} |
1737 |
|
} |
1738 |
|
if (!bSkip) |
1739 |
|
{ |
1740 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1741 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1742 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1743 |
|
qcoeff[5*64+0]=0; |
1744 |
|
} |
1745 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1746 |
|
pMB->cbp = 0x80; /* trick! so cbp!=0, but still nothing is written to bs */ |
1747 |
|
} |
1748 |
|
else |
1749 |
|
MBSkip(bs); |
1750 |
|
|
1751 |
|
#else |
1752 |
|
MBSkip(bs); /* without B-frames, no precautions are needed */ |
1753 |
|
|
1754 |
|
#endif |
1755 |
|
|
1756 |
|
} else { |
1757 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1758 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1759 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1760 |
|
qcoeff[5*64+0]=0; |
1761 |
|
} |
1762 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1763 |
|
} |
1764 |
|
|
1765 |
stop_coding_timer(); |
stop_coding_timer(); |
1766 |
} |
} |
1767 |
} |
} |
1798 |
#ifdef BFRAMES |
#ifdef BFRAMES |
1799 |
/* frame drop code */ |
/* frame drop code */ |
1800 |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1801 |
if (pEnc->sStat.kblks + pEnc->sStat.mblks <= |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1802 |
(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) |
1803 |
{ |
{ |
1804 |
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1822 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1823 |
|
|
1824 |
#ifdef BFRAMES |
#ifdef BFRAMES |
1825 |
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase; |
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % |
1826 |
pEnc->last_pframe = pEnc->mbParam.m_ticks; |
(int32_t)pEnc->mbParam.fbase; |
1827 |
|
pEnc->last_pframe = pEnc->current->ticks; |
1828 |
#endif |
#endif |
1829 |
|
|
1830 |
return 0; // inter |
return 0; // inter |
1883 |
|
|
1884 |
start_timer(); |
start_timer(); |
1885 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1886 |
((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase, |
((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase, |
1887 |
pEnc->time_pp, |
pEnc->time_pp, |
1888 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
1889 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1944 |
|
|
1945 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1946 |
mb->cbp = |
mb->cbp = |
1947 |
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
|
qcoeff); |
|
1948 |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1949 |
|
|
1950 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |