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.4 2002-09-28 13:01:02 chl 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; |
|
pEnc->mbParam.m_seconds = 0; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
414 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
|
pEnc->last_pframe = 0; |
|
|
pEnc->last_sync = 0; |
|
415 |
|
|
416 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
417 |
|
|
600 |
|
|
601 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
602 |
{ |
{ |
603 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
604 |
|
pEnc->current->stamp = pEnc->mbParam.m_stamp; |
|
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; |
|
|
|
|
605 |
} |
} |
606 |
|
|
607 |
|
|
630 |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
631 |
} |
} |
632 |
|
|
633 |
|
static __inline void |
634 |
|
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
635 |
|
{ |
636 |
|
|
637 |
|
pCur->ticks = (int32_t)pCur->stamp % time_base; |
638 |
|
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
639 |
|
|
640 |
|
/* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
641 |
|
|
642 |
|
fprintf(stderr,"WriteVop: %d - %d \n", |
643 |
|
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
644 |
|
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
645 |
|
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
646 |
|
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
647 |
|
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
648 |
|
|
649 |
|
*/ |
650 |
|
} |
651 |
|
|
652 |
|
|
653 |
|
|
654 |
|
|
655 |
|
|
656 |
/***************************************************************************** |
/***************************************************************************** |
669 |
{ |
{ |
670 |
uint16_t x, y; |
uint16_t x, y; |
671 |
Bitstream bs; |
Bitstream bs; |
672 |
uint32_t bits; |
uint32_t bits, mode; |
673 |
|
|
674 |
int input_valid = 1; |
int input_valid = 1; |
675 |
|
|
747 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
748 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
749 |
|
|
750 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
751 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
752 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
753 |
BitstreamPutBits(&bs, 0x7f, 8); |
BitstreamPutBits(&bs, 0x7f, 8); |
820 |
|
|
821 |
pFrame->intra = 0; |
pFrame->intra = 0; |
822 |
|
|
823 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
824 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
825 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
826 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
858 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
859 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
860 |
|
|
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
|
|
|
861 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
862 |
|
|
863 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
916 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
917 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
918 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
919 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
920 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
921 |
pEnc->mbParam.height) > 30) { |
pEnc->mbParam.height) > 30) {*/ |
922 |
|
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
923 |
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
924 |
|
|
925 |
/* |
/* |
926 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
927 |
*/ |
*/ |
969 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
970 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
971 |
*/ |
*/ |
972 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
973 |
/* |
/* |
974 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
975 |
*/ |
*/ |
1064 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1065 |
} |
} |
1066 |
|
|
|
|
|
1067 |
stop_global_timer(); |
stop_global_timer(); |
1068 |
write_timer(); |
write_timer(); |
1069 |
|
|
1107 |
|
|
1108 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1109 |
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; |
|
1110 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1111 |
|
|
1112 |
|
/* disable alternate scan flag if interlacing is not enabled */ |
1113 |
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
1114 |
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
1115 |
|
{ |
1116 |
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
1117 |
|
} |
1118 |
|
|
1119 |
start_timer(); |
start_timer(); |
1120 |
if (image_input |
if (image_input |
1121 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1500 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1501 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1502 |
} |
} |
1503 |
|
|
1504 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1505 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1506 |
|
|
1507 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1542 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1543 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1544 |
|
|
1545 |
pEnc->last_pframe = pEnc->current->ticks; |
// pEnc->time_pp = ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)); |
|
pEnc->last_sync = pEnc->current->ticks; |
|
1546 |
|
|
1547 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1548 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1549 |
} |
} |
1550 |
|
// pEnc->last_pframe = (int32_t)pEnc->mbParam.m_stamp; |
1551 |
|
|
1552 |
return 1; // intra |
return 1; // intra |
1553 |
} |
} |
1578 |
|
|
1579 |
start_timer(); |
start_timer(); |
1580 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1581 |
pEnc->mbParam.width, pEnc->mbParam.height, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
1582 |
stop_edges_timer(); |
stop_edges_timer(); |
1583 |
|
|
1584 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1624 |
if (vol_header) |
if (vol_header) |
1625 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1626 |
|
|
1627 |
|
|
1628 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1629 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1630 |
|
|
1631 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1766 |
|
|
1767 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1768 |
|
|
1769 |
#ifdef BFRAMES |
#ifdef FRAMEDROP |
1770 |
/* frame drop code */ |
/* frame drop code */ |
1771 |
// 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); |
1772 |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1776 |
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1777 |
|
|
1778 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1779 |
|
|
1780 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1781 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1782 |
|
|
1783 |
// copy reference frame details into the current frame |
// copy reference frame details into the current frame |
1794 |
|
|
1795 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1796 |
|
|
1797 |
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % |
// pEnc->time_pp = ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)); |
1798 |
(int32_t)pEnc->mbParam.fbase; |
// pEnc->last_pframe = (int32_t)pEnc->mbParam.m_stamp; |
|
pEnc->last_pframe = pEnc->current->ticks; |
|
1799 |
|
|
1800 |
return 0; // inter |
return 0; // inter |
1801 |
} |
} |
1829 |
// forward |
// forward |
1830 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1831 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1832 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1833 |
start_timer(); |
start_timer(); |
1834 |
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, |
1835 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1839 |
// backward |
// backward |
1840 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1841 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1842 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1843 |
start_timer(); |
start_timer(); |
1844 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1845 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1847 |
stop_inter_timer(); |
stop_inter_timer(); |
1848 |
|
|
1849 |
start_timer(); |
start_timer(); |
1850 |
|
|
1851 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1852 |
((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase, |
((int32_t)(frame->stamp - pEnc->reference->stamp)), // time_bp |
1853 |
pEnc->time_pp, |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
1854 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
1855 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1856 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1865 |
} */ |
} */ |
1866 |
|
|
1867 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1868 |
|
|
1869 |
|
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1870 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1871 |
|
|
1872 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1880 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1881 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1882 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1883 |
|
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
1884 |
|
|
1885 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1886 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1912 |
#endif |
#endif |
1913 |
start_timer(); |
start_timer(); |
1914 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1915 |
&pEnc->sStat); |
&pEnc->sStat, direction); |
1916 |
stop_coding_timer(); |
stop_coding_timer(); |
1917 |
} |
} |
1918 |
} |
} |