3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
|
* Copyright(C) 2002 Michael Militzer |
7 |
|
* |
8 |
* This program is an implementation of a part of one or more MPEG-4 |
* This program is an implementation of a part of one or more MPEG-4 |
9 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
* to use this software module in hardware or software products are |
* to use this software module in hardware or software products are |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
|
/***************************************************************************** |
|
|
* |
|
|
* History |
|
|
* |
|
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
|
|
* MinChen <chenm001@163.com> |
|
|
* 20.06.2002 bframe patch |
|
|
* 08.05.2002 fix some problem in DEBUG mode; |
|
|
* MinChen <chenm001@163.com> |
|
|
* 14.04.2002 added FrameCodeB() |
|
|
* |
|
|
* $Id$ |
|
|
* |
|
|
****************************************************************************/ |
|
|
|
|
|
|
|
33 |
#include <stdlib.h> |
#include <stdlib.h> |
34 |
#include <stdio.h> |
#include <stdio.h> |
35 |
#include <math.h> |
#include <math.h> |
410 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
411 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
412 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
413 |
pEnc->last_pframe = 1; |
pEnc->last_pframe = 0; |
414 |
|
pEnc->last_sync = 0; |
415 |
#endif |
#endif |
416 |
|
|
417 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
607 |
} |
} |
608 |
|
|
609 |
|
|
|
#ifdef BFRAMES |
|
610 |
void inc_frame_num(Encoder * pEnc) |
void inc_frame_num(Encoder * pEnc) |
611 |
{ |
{ |
|
pEnc->iFrameNum++; |
|
612 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
613 |
|
|
614 |
|
#ifdef BFRAMES |
615 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
616 |
|
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
617 |
|
pEnc->mbParam.m_seconds = 1; // more than 1 second since last I or P is not supported. |
618 |
|
else |
619 |
|
pEnc->mbParam.m_seconds = 0; |
620 |
|
|
621 |
|
if (pEnc->current->coding_type != B_VOP) |
622 |
|
pEnc->last_sync = pEnc->mbParam.m_ticks; |
623 |
|
#else |
624 |
|
|
625 |
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
626 |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
627 |
} |
|
628 |
#endif |
#endif |
629 |
|
|
630 |
|
} |
631 |
|
|
632 |
|
|
633 |
#ifdef BFRAMES |
#ifdef BFRAMES |
634 |
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
824 |
|
|
825 |
pFrame->intra = 0; |
pFrame->intra = 0; |
826 |
|
|
827 |
BitstreamPutBits(&bs, 0x7f, 8); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
828 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
829 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
830 |
|
|
1042 |
goto bvop_loop; |
goto bvop_loop; |
1043 |
} |
} |
1044 |
|
|
1045 |
|
pEnc->iFrameNum++; |
1046 |
|
|
1047 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1048 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1049 |
|
|
1118 |
|
|
1119 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1120 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
|
#ifdef BFRAMES |
|
1121 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1122 |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
#endif |
|
1123 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1124 |
|
|
1125 |
start_timer(); |
start_timer(); |
1249 |
DEBUG(temp); |
DEBUG(temp); |
1250 |
#endif |
#endif |
1251 |
|
|
|
#ifdef BFRAMES |
|
1252 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
|
#else |
|
1253 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
|
#endif |
|
|
|
|
1254 |
|
|
1255 |
stop_global_timer(); |
stop_global_timer(); |
1256 |
write_timer(); |
write_timer(); |
1530 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1531 |
|
|
1532 |
start_timer(); |
start_timer(); |
1533 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1534 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1535 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1536 |
|
qcoeff[5*64+0]=0; |
1537 |
|
} |
1538 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1539 |
stop_coding_timer(); |
stop_coding_timer(); |
1540 |
} |
} |
1571 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1572 |
|
|
1573 |
int iLimit; |
int iLimit; |
1574 |
int k; |
int x, y, k; |
|
int x, y; |
|
1575 |
int iSearchRange; |
int iSearchRange; |
1576 |
int bIntra; |
int bIntra; |
1577 |
|
|
1723 |
} |
} |
1724 |
if (!bSkip) |
if (!bSkip) |
1725 |
{ |
{ |
1726 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1727 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1728 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1729 |
|
qcoeff[5*64+0]=0; |
1730 |
|
} |
1731 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1732 |
pMB->cbp = 0x80; /* trick! so cbp!=0, but still nothing is written to bs */ |
pMB->cbp = 0x80; /* trick! so cbp!=0, but still nothing is written to bs */ |
1733 |
} |
} |
1734 |
else |
else |
1735 |
MBSkip(bs); |
MBSkip(bs); |
1736 |
|
|
|
|
|
1737 |
#else |
#else |
1738 |
MBSkip(bs); /* without B-frames, no precautions are needed */ |
MBSkip(bs); /* without B-frames, no precautions are needed */ |
1739 |
|
|
1740 |
#endif |
#endif |
1741 |
|
|
1742 |
} else { |
} else { |
1743 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1744 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1745 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1746 |
|
qcoeff[5*64+0]=0; |
1747 |
|
} |
1748 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1749 |
} |
} |
1750 |
|
|
1784 |
#ifdef BFRAMES |
#ifdef BFRAMES |
1785 |
/* frame drop code */ |
/* frame drop code */ |
1786 |
// 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); |
1787 |
if (pEnc->sStat.kblks + pEnc->sStat.mblks <= |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1788 |
(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) |
1789 |
{ |
{ |
1790 |
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1808 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1809 |
|
|
1810 |
#ifdef BFRAMES |
#ifdef BFRAMES |
1811 |
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) % |
1812 |
pEnc->last_pframe = pEnc->mbParam.m_ticks; |
(int32_t)pEnc->mbParam.fbase; |
1813 |
|
pEnc->last_pframe = pEnc->current->ticks; |
1814 |
#endif |
#endif |
1815 |
|
|
1816 |
return 0; // inter |
return 0; // inter |
1869 |
|
|
1870 |
start_timer(); |
start_timer(); |
1871 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1872 |
((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, |
1873 |
pEnc->time_pp, |
pEnc->time_pp, |
1874 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
1875 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1930 |
|
|
1931 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1932 |
mb->cbp = |
mb->cbp = |
1933 |
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
|
qcoeff); |
|
1934 |
//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); |
1935 |
|
|
1936 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |