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.76.2.1 2002-09-23 20:36:01 chl Exp $ |
* $Id: encoder.c,v 1.76.2.10 2002-10-03 08:24:27 syskin Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
410 |
pEnc->queue_tail = 0; |
pEnc->queue_tail = 0; |
411 |
pEnc->queue_size = 0; |
pEnc->queue_size = 0; |
412 |
|
|
413 |
|
pEnc->mbParam.m_stamp = 0; |
414 |
|
|
|
pEnc->mbParam.m_seconds = 0; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
415 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
416 |
pEnc->last_pframe = 0; |
pEnc->current->stamp = 0; |
417 |
pEnc->last_sync = 0; |
pEnc->reference->stamp = 0; |
418 |
|
|
419 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
420 |
|
|
603 |
|
|
604 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
605 |
{ |
{ |
606 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->current->stamp = pEnc->mbParam.m_stamp; // first frame is zero |
607 |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
|
|
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
|
|
pEnc->mbParam.m_seconds = 1; |
|
|
// more than 1 second since last I or P is not supported. |
|
|
else |
|
|
pEnc->mbParam.m_seconds = 0; |
|
|
|
|
608 |
} |
} |
609 |
|
|
610 |
|
|
633 |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
634 |
} |
} |
635 |
|
|
636 |
|
static __inline void |
637 |
|
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
638 |
|
{ |
639 |
|
|
640 |
|
pCur->ticks = (int32_t)pCur->stamp % time_base; |
641 |
|
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
642 |
|
|
643 |
|
//HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
644 |
|
|
645 |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
646 |
|
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
647 |
|
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
648 |
|
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
649 |
|
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
650 |
|
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
651 |
|
|
652 |
|
*/ |
653 |
|
} |
654 |
|
|
655 |
|
|
656 |
|
|
657 |
|
|
658 |
|
|
659 |
/***************************************************************************** |
/***************************************************************************** |
672 |
{ |
{ |
673 |
uint16_t x, y; |
uint16_t x, y; |
674 |
Bitstream bs; |
Bitstream bs; |
675 |
uint32_t bits; |
uint32_t bits, mode; |
676 |
|
|
677 |
int input_valid = 1; |
int input_valid = 1; |
678 |
|
|
744 |
if (pEnc->bframenum_head > 0) { |
if (pEnc->bframenum_head > 0) { |
745 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
746 |
|
|
747 |
|
/* write an empty marker to the bitstream. |
748 |
|
|
749 |
|
for divx5 decoder compatibility, this marker must consist |
750 |
|
of a not-coded p-vop, with a time_base of zero, and time_increment |
751 |
|
indentical to the future-referece frame. |
752 |
|
*/ |
753 |
|
|
754 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
755 |
|
int tmp; |
756 |
|
|
757 |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
758 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
759 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
760 |
|
|
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
|
761 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
762 |
BitstreamPutBits(&bs, 0x7f, 8); |
|
763 |
|
tmp = pEnc->current->seconds; |
764 |
|
pEnc->current->seconds = 0; /* force time_base = 0 */ |
765 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
766 |
|
pEnc->current->seconds = tmp; |
767 |
|
|
768 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
769 |
pFrame->intra = 0; |
pFrame->intra = 0; |
825 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
826 |
pEnc->queue_size--; |
pEnc->queue_size--; |
827 |
|
|
828 |
} else if (BitstreamPos(&bs) == 0) { |
} else { |
829 |
|
|
830 |
|
/* if nothing was encoded, write an 'ignore this frame' flag |
831 |
|
to the bitstream */ |
832 |
|
|
833 |
|
if (BitstreamPos(&bs) == 0) { |
834 |
|
|
835 |
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
836 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
837 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
838 |
|
|
839 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
840 |
pFrame->intra = 0; |
pFrame->intra = 0; |
841 |
|
} |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
|
|
BitstreamPad(&bs); |
|
|
pFrame->length = BitstreamLength(&bs); |
|
|
|
|
|
return XVID_ERR_OK; |
|
|
|
|
|
} else { |
|
842 |
|
|
843 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
844 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
869 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
870 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
871 |
|
|
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
|
|
|
872 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
873 |
|
|
874 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
927 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
928 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
929 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
930 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
931 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
932 |
pEnc->mbParam.height) > 30) { |
pEnc->mbParam.height) > 30) {*/ |
933 |
|
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
934 |
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
935 |
|
|
936 |
/* |
/* |
937 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
938 |
*/ |
*/ |
980 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
981 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
982 |
*/ |
*/ |
983 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
984 |
/* |
/* |
985 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
986 |
*/ |
*/ |
1075 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1076 |
} |
} |
1077 |
|
|
|
|
|
1078 |
stop_global_timer(); |
stop_global_timer(); |
1079 |
write_timer(); |
write_timer(); |
1080 |
|
|
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; |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
1121 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1122 |
|
|
1123 |
|
inc_frame_num(pEnc); |
1124 |
|
|
1125 |
|
/* disable alternate scan flag if interlacing is not enabled */ |
1126 |
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
1127 |
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
1128 |
|
{ |
1129 |
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
1130 |
|
} |
1131 |
|
|
1132 |
start_timer(); |
start_timer(); |
1133 |
if (image_input |
if (image_input |
1134 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1256 |
DEBUG(temp); |
DEBUG(temp); |
1257 |
#endif |
#endif |
1258 |
|
|
|
inc_frame_num(pEnc); |
|
1259 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1260 |
|
|
1261 |
stop_global_timer(); |
stop_global_timer(); |
1512 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1513 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1514 |
} |
} |
1515 |
|
|
1516 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1517 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1518 |
|
|
1519 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1554 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1555 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1556 |
|
|
|
pEnc->last_pframe = pEnc->current->ticks; |
|
|
pEnc->last_sync = pEnc->current->ticks; |
|
|
|
|
1557 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1558 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1559 |
} |
} |
1587 |
|
|
1588 |
start_timer(); |
start_timer(); |
1589 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1590 |
pEnc->mbParam.width, pEnc->mbParam.height, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
1591 |
stop_edges_timer(); |
stop_edges_timer(); |
1592 |
|
|
1593 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1613 |
start_timer(); |
start_timer(); |
1614 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1615 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1616 |
if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
if (bIntra == 0) { |
1617 |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
1618 |
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1619 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1620 |
|
} |
1621 |
|
|
1622 |
} else { |
} else { |
1623 |
|
|
1636 |
if (vol_header) |
if (vol_header) |
1637 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1638 |
|
|
1639 |
|
|
1640 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1641 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1642 |
|
|
1643 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1778 |
|
|
1779 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1780 |
|
|
1781 |
#ifdef BFRAMES |
#ifdef FRAMEDROP |
1782 |
/* frame drop code */ |
/* frame drop code */ |
1783 |
// 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); |
1784 |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1788 |
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1789 |
|
|
1790 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1791 |
|
|
1792 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1793 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1794 |
|
|
1795 |
// copy reference frame details into the current frame |
// copy reference frame details into the current frame |
1806 |
|
|
1807 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1808 |
|
|
|
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % |
|
|
(int32_t)pEnc->mbParam.fbase; |
|
|
pEnc->last_pframe = pEnc->current->ticks; |
|
|
|
|
1809 |
return 0; // inter |
return 0; // inter |
1810 |
} |
} |
1811 |
|
|
1838 |
// forward |
// forward |
1839 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1840 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1841 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1842 |
start_timer(); |
start_timer(); |
1843 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1844 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1848 |
// backward |
// backward |
1849 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1850 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1851 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1852 |
start_timer(); |
start_timer(); |
1853 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1854 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1856 |
stop_inter_timer(); |
stop_inter_timer(); |
1857 |
|
|
1858 |
start_timer(); |
start_timer(); |
1859 |
|
|
1860 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1861 |
((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase, |
((int32_t)(pEnc->current->stamp - frame->stamp)), // time_bp |
1862 |
pEnc->time_pp, |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
1863 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
1864 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1865 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1874 |
} */ |
} */ |
1875 |
|
|
1876 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1877 |
|
|
1878 |
|
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1879 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1880 |
|
|
1881 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1889 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1890 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1891 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1892 |
|
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
1893 |
|
|
1894 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1895 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1921 |
#endif |
#endif |
1922 |
start_timer(); |
start_timer(); |
1923 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1924 |
&pEnc->sStat); |
&pEnc->sStat, direction); |
1925 |
stop_coding_timer(); |
stop_coding_timer(); |
1926 |
} |
} |
1927 |
} |
} |