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.12 2002-10-05 21:36:35 Isibaar Exp $ |
* $Id: encoder.c,v 1.76.2.17 2002-11-11 20:53:34 Isibaar Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
54 |
#include "global.h" |
#include "global.h" |
55 |
#include "utils/timer.h" |
#include "utils/timer.h" |
56 |
#include "image/image.h" |
#include "image/image.h" |
|
#ifdef BFRAMES |
|
57 |
#include "image/font.h" |
#include "image/font.h" |
58 |
#include "motion/sad.h" |
#include "motion/sad.h" |
|
#endif |
|
59 |
#include "motion/motion.h" |
#include "motion/motion.h" |
60 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
61 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
107 |
}; |
}; |
108 |
|
|
109 |
|
|
|
static void __inline |
|
|
image_null(IMAGE * image) |
|
|
{ |
|
|
image->y = image->u = image->v = NULL; |
|
|
} |
|
|
|
|
|
|
|
110 |
/***************************************************************************** |
/***************************************************************************** |
111 |
* Encoder creation |
* Encoder creation |
112 |
* |
* |
616 |
start_timer(); |
start_timer(); |
617 |
if (image_input |
if (image_input |
618 |
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
619 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
620 |
return; |
return; |
621 |
stop_conv_timer(); |
stop_conv_timer(); |
622 |
|
|
711 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
712 |
pFrame->intra = 0; |
pFrame->intra = 0; |
713 |
|
|
714 |
|
emms(); |
715 |
|
|
716 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
717 |
} |
} |
718 |
|
|
731 |
if (input_valid) |
if (input_valid) |
732 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
733 |
|
|
734 |
|
emms(); |
735 |
|
|
736 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
737 |
} |
} |
738 |
|
|
766 |
if (input_valid) |
if (input_valid) |
767 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
768 |
|
|
769 |
|
emms(); |
770 |
|
|
771 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
772 |
} |
} |
773 |
} |
} |
798 |
start_timer(); |
start_timer(); |
799 |
if (image_input |
if (image_input |
800 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
801 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
802 |
|
{ |
803 |
|
emms(); |
804 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
805 |
|
} |
806 |
stop_conv_timer(); |
stop_conv_timer(); |
807 |
|
|
808 |
// queue input frame, and dequue next image |
// queue input frame, and dequue next image |
841 |
} |
} |
842 |
|
|
843 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
844 |
|
emms(); |
845 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
846 |
} |
} |
847 |
|
|
981 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
982 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
983 |
*/ |
*/ |
984 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
} else if ((pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) || (mode != 0)) { |
985 |
|
|
986 |
/* |
/* |
987 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
988 |
*/ |
*/ |
999 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1000 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1001 |
|
|
1002 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { |
1003 |
BitstreamPadAlways(&bs); |
BitstreamPadAlways(&bs); |
1004 |
input_valid = 0; |
input_valid = 0; |
1005 |
goto ipvop_loop; |
goto ipvop_loop; |
1080 |
stop_global_timer(); |
stop_global_timer(); |
1081 |
write_timer(); |
write_timer(); |
1082 |
|
|
1083 |
|
emms(); |
1084 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1085 |
} |
} |
1086 |
|
|
1135 |
start_timer(); |
start_timer(); |
1136 |
if (image_input |
if (image_input |
1137 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1138 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0) |
1139 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1140 |
stop_conv_timer(); |
stop_conv_timer(); |
1141 |
|
|
1512 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1513 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1514 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1515 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1516 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1517 |
|
|
1518 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1522 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1523 |
} |
} |
1524 |
|
|
1525 |
|
#define XVID_ID "XviD" XVID_BS_VERSION |
1526 |
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
1527 |
|
|
1528 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1529 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1530 |
|
|
1604 |
|
|
1605 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1606 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1607 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1608 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1609 |
|
|
1610 |
if (!force_inter) |
if (!force_inter) |
1624 |
stop_inter_timer(); |
stop_inter_timer(); |
1625 |
} |
} |
1626 |
|
|
1627 |
|
if (pEnc->current->global_flags & XVID_GMC) { |
1628 |
|
// printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
1629 |
|
DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
1630 |
|
pEnc->current->coding_type = S_VOP; |
1631 |
|
} else |
1632 |
|
pEnc->current->coding_type = P_VOP; |
1633 |
|
|
1634 |
start_timer(); |
start_timer(); |
1635 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1636 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1646 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1647 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1648 |
iLimit); |
iLimit); |
|
|
|
1649 |
} |
} |
1650 |
stop_motion_timer(); |
stop_motion_timer(); |
1651 |
|
|
1652 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1653 |
|
|
1654 |
pEnc->current->coding_type = P_VOP; |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
1655 |
|
pEnc->current->coding_type = P_VOP; /* no global motion -> no GMC */ |
1656 |
|
|
1657 |
|
|
1658 |
if (vol_header) |
if (vol_header) |
1659 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1732 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1733 |
|
|
1734 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) |
1735 |
skip_possible &= (pMB->qmvs[0].x == 0) & (pMB->qmvs[0].y == 0); |
{ skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y); |
1736 |
|
} |
1737 |
else |
else |
1738 |
skip_possible &= (pMB->mvs[0].x == 0) & (pMB->mvs[0].y == 0); |
{ skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y); |
1739 |
|
} |
1740 |
|
|
1741 |
if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
|
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
|
1742 |
|
|
1743 |
|
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1744 |
int bSkip = 1; |
int bSkip = 1; |
|
pMB->mode = MODE_NOT_CODED; |
|
1745 |
|
|
1746 |
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1747 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1748 |
{ |
{ |
1749 |
int iSAD; |
int iSAD; |
1755 |
break; |
break; |
1756 |
} |
} |
1757 |
} |
} |
1758 |
|
|
1759 |
if (!bSkip) |
if (!bSkip) |
1760 |
{ |
{ |
1761 |
VECTOR predMV; |
VECTOR predMV; |
1762 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) { |
1763 |
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1764 |
else |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; /* with GMC, qmvs doesn't have to be (0,0)! */ |
1765 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1766 |
|
} |
1767 |
|
else { |
1768 |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1769 |
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */ |
1770 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1771 |
|
} |
1772 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1773 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1774 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1775 |
} |
} |
1776 |
else MBSkip(bs); |
else |
1777 |
|
{ |
1778 |
|
pMB->mode = MODE_NOT_CODED; |
1779 |
|
MBSkip(bs); |
1780 |
|
} |
1781 |
|
|
1782 |
} else { |
} else { |
1783 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1839 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
1840 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1841 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1842 |
|
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
1843 |
pEnc->current->fcode = pEnc->reference->fcode; |
pEnc->current->fcode = pEnc->reference->fcode; |
1844 |
pEnc->current->bcode = pEnc->reference->bcode; |
pEnc->current->bcode = pEnc->reference->bcode; |
1845 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1879 |
} |
} |
1880 |
#endif |
#endif |
1881 |
|
|
1882 |
|
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
1883 |
|
|
1884 |
// forward |
// forward |
1885 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1886 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |