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.31 2002-12-14 06:07:02 suxen_drol Exp $ |
* $Id: encoder.c,v 1.76.2.35 2003-01-11 14:59:23 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
|
|
|
46 |
#include <stdlib.h> |
#include <stdlib.h> |
47 |
#include <stdio.h> |
#include <stdio.h> |
48 |
#include <math.h> |
#include <math.h> |
72 |
****************************************************************************/ |
****************************************************************************/ |
73 |
|
|
74 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
75 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
76 |
|
|
77 |
/***************************************************************************** |
/***************************************************************************** |
78 |
* Local function prototypes |
* Local function prototypes |
319 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
320 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
321 |
|
|
322 |
|
/* Create full bitplane for GMC, this might be wasteful */ |
323 |
|
if (image_create |
324 |
|
(&pEnc->vGMC, pEnc->mbParam.edged_width, |
325 |
|
pEnc->mbParam.edged_height) < 0) |
326 |
|
goto xvid_err_memory3; |
327 |
|
|
328 |
|
|
329 |
/* B Frames specific init */ |
/* B Frames specific init */ |
491 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
492 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
493 |
|
|
494 |
|
/* destroy GMC image */ |
495 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
496 |
|
pEnc->mbParam.edged_height); |
497 |
|
|
498 |
|
|
499 |
xvid_err_memory2: |
xvid_err_memory2: |
500 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
501 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
683 |
{ |
{ |
684 |
uint16_t x, y; |
uint16_t x, y; |
685 |
Bitstream bs; |
Bitstream bs; |
686 |
uint32_t bits, mode; |
uint32_t bits; |
687 |
|
int mode; |
688 |
|
|
689 |
int input_valid = 1; |
int input_valid = 1; |
690 |
int bframes_count = 0; |
int bframes_count = 0; |
723 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
724 |
|
|
725 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
726 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
727 |
|
|
728 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
729 |
|
|
730 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
731 |
bframes_count = 0; |
bframes_count = 0; |
801 |
if (pEnc->bframenum_dx50bvop != -1) |
if (pEnc->bframenum_dx50bvop != -1) |
802 |
{ |
{ |
803 |
|
|
804 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
805 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
806 |
|
|
807 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
808 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
816 |
|
|
817 |
} else if (input_valid) { |
} else if (input_valid) { |
818 |
|
|
819 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
820 |
|
|
821 |
start_timer(); |
start_timer(); |
822 |
if (image_input |
if (image_input |
842 |
|
|
843 |
} else if (pEnc->queue_size > 0) { |
} else if (pEnc->queue_size > 0) { |
844 |
|
|
845 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
846 |
|
|
847 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
848 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
995 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
996 |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
997 |
|
|
998 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
999 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1000 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
1001 |
} |
} |
1076 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1077 |
|
|
1078 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1079 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1080 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1081 |
|
|
1082 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1083 |
|
|
1108 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1109 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1110 |
|
|
1111 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
printf("PSNR: %f\n", psnr); |
1112 |
DEBUG(temp); |
// DEBUG(temp); |
1113 |
#endif |
#endif |
1114 |
|
|
1115 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1157 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
1158 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
1159 |
|
|
1160 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1161 |
|
|
1162 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1163 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1301 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1302 |
|
|
1303 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1304 |
DEBUG(temp); |
// DEBUG(temp); |
1305 |
#endif |
#endif |
1306 |
|
|
1307 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1573 |
|
|
1574 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1575 |
|
|
|
/* XXX: move this stuff to BitstreamWriteVolHeader */ |
|
|
#define DIVX501B481P "DivX501b481p" |
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) { |
|
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
|
|
} |
|
|
|
|
|
#define XVID_ID "XviD" XVID_BS_VERSION |
|
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
|
|
|
|
1576 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1577 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1578 |
|
|
1629 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1630 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 30 |
1631 |
|
|
1632 |
|
|
1633 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1634 |
static int |
static int |
1635 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1636 |
Bitstream * bs, |
Bitstream * bs, |
1686 |
stop_inter_timer(); |
stop_inter_timer(); |
1687 |
} |
} |
1688 |
|
|
|
if (pEnc->current->global_flags & XVID_GMC) { |
|
|
// printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
|
|
DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
|
|
pEnc->current->coding_type = S_VOP; |
|
|
} else |
|
1689 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1690 |
|
|
1691 |
start_timer(); |
start_timer(); |
1692 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1693 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1694 |
else |
else |
|
|
|
1695 |
bIntra = |
bIntra = |
1696 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1697 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1701 |
|
|
1702 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1703 |
|
|
1704 |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1705 |
pEnc->current->coding_type = P_VOP; /* no global motion -> no GMC */ |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
1706 |
|
{ |
1707 |
|
pEnc->current->coding_type = S_VOP; |
1708 |
|
|
1709 |
|
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
1710 |
|
pEnc->mbParam.width, pEnc->mbParam.height, |
1711 |
|
&pEnc->current->gmc_data); |
1712 |
|
|
1713 |
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1714 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1715 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1716 |
|
pEnc->mbParam.m_fcode, 0, 0, |
1717 |
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1718 |
|
|
1719 |
|
} |
1720 |
|
|
1721 |
if (vol_header) |
if (vol_header) |
1722 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1723 |
|
|
|
|
|
1724 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1725 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1726 |
|
|
1729 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1730 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1731 |
|
|
1732 |
|
|
1733 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1734 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1735 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1736 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1737 |
|
|
1738 |
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1739 |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
1740 |
|
|
1741 |
|
|
1742 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1743 |
|
|
1744 |
if (!bIntra) { |
if (bIntra) { |
1745 |
|
CodeIntraMB(pEnc, pMB); |
1746 |
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1747 |
|
dct_codes, qcoeff); |
1748 |
|
|
1749 |
|
start_timer(); |
1750 |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1751 |
|
stop_prediction_timer(); |
1752 |
|
|
1753 |
|
pEnc->current->sStat.kblks++; |
1754 |
|
|
1755 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1756 |
|
continue; |
1757 |
|
} |
1758 |
|
|
1759 |
|
if (pEnc->current->coding_type == S_VOP) { |
1760 |
|
|
1761 |
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1762 |
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1763 |
|
pEnc->mbParam.edged_width,65536); |
1764 |
|
|
1765 |
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1766 |
|
if (pEnc->mbParam.m_quarterpel) |
1767 |
|
pMB->qmvs[0] = pMB->qmvs[1] =pMB->qmvs[2] =pMB->qmvs[3] = pMB->amv; |
1768 |
|
else |
1769 |
|
pMB->mvs[0] = pMB->mvs[1] =pMB->mvs[2] =pMB->mvs[3] = pMB->amv; |
1770 |
|
|
1771 |
|
pMB->mode = MODE_INTER; |
1772 |
|
pMB->mcsel = 1; |
1773 |
|
pMB->sad16 = iSAD; |
1774 |
|
} else { |
1775 |
|
pMB->mcsel = 0; |
1776 |
|
} |
1777 |
|
} else |
1778 |
|
{ |
1779 |
|
pMB->mcsel = 0; /* just a precaution */ |
1780 |
|
} |
1781 |
|
|
1782 |
start_timer(); |
start_timer(); |
1783 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1784 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1785 |
&pEnc->vInterHV, &pEnc->current->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
1786 |
|
&pEnc->current->image, |
1787 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1788 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1789 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1790 |
pEnc->mbParam.m_quarterpel, |
pEnc->mbParam.m_quarterpel, |
1791 |
(pEnc->current->global_flags & XVID_REDUCED), |
(pEnc->current->global_flags & XVID_REDUCED), |
1792 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1793 |
|
|
1794 |
stop_comp_timer(); |
stop_comp_timer(); |
1795 |
|
|
1796 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1808 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1809 |
|
|
1810 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1811 |
pMB->cbp = |
{ pMB->cbp = |
1812 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1813 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
|
} else { |
|
|
CodeIntraMB(pEnc, pMB); |
|
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
|
dct_codes, qcoeff); |
|
1814 |
} |
} |
1815 |
|
|
|
start_timer(); |
|
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
|
|
stop_prediction_timer(); |
|
1816 |
|
|
1817 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
pEnc->current->sStat.kblks++; |
|
|
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
1818 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1819 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1820 |
pEnc->current->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
1829 |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
1830 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1831 |
|
|
1832 |
|
if (pEnc->current->coding_type == S_VOP) |
1833 |
|
skip_possible &= (pMB->mcsel == 1); |
1834 |
|
else if (pEnc->current->coding_type == P_VOP) { |
1835 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) |
1836 |
{ skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y); |
skip_possible &= (pMB->qmvs[0].x == 0) & (pMB->qmvs[0].y == 0); |
|
} |
|
1837 |
else |
else |
1838 |
{ skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y); |
skip_possible &= (pMB->mvs[0].x == 0) & (pMB->mvs[0].y == 0); |
1839 |
} |
} |
1840 |
|
|
1841 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1842 |
|
|
1843 |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
|
int bSkip = 1; |
|
1844 |
|
|
1845 |
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1846 |
|
{ |
1847 |
|
int bSkip = 1; |
1848 |
|
|
1849 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1850 |
{ |
{ |
1851 |
int iSAD; |
int iSAD; |
1858 |
} |
} |
1859 |
} |
} |
1860 |
|
|
1861 |
if (!bSkip) |
if (!bSkip) { /* no SKIP, but trivial block */ |
|
{ |
|
|
VECTOR predMV; |
|
1862 |
if(pEnc->mbParam.m_quarterpel) { |
if(pEnc->mbParam.m_quarterpel) { |
1863 |
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1864 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; /* with GMC, qmvs doesn't have to be (0,0)! */ |
pMB->pmvs[0].x = - predMV.x; |
1865 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1866 |
} |
} |
1867 |
else { |
else { |
1868 |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1869 |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */ |
pMB->pmvs[0].x = - predMV.x; |
1870 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1871 |
} |
} |
1872 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1873 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1874 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1875 |
|
stop_coding_timer(); |
1876 |
|
continue; /* next MB */ |
1877 |
} |
} |
1878 |
else |
} |
1879 |
{ |
/* do SKIP */ |
1880 |
|
|
1881 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1882 |
MBSkip(bs); |
MBSkip(bs); |
1883 |
|
stop_coding_timer(); |
1884 |
|
continue; /* next MB */ |
1885 |
} |
} |
1886 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1887 |
|
|
|
} else { |
|
1888 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1889 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1890 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1891 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1892 |
} |
} |
1893 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
1894 |
|
if(pEnc->mbParam.m_quarterpel) { |
1895 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1896 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1897 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1898 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1899 |
|
} else { |
1900 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1901 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1902 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1903 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1904 |
|
} |
1905 |
|
|
1906 |
|
|
1907 |
|
if (pMB->mode == MODE_INTER4V) |
1908 |
|
{ int k; |
1909 |
|
for (k=1;k<4;k++) |
1910 |
|
{ |
1911 |
|
if(pEnc->mbParam.m_quarterpel) { |
1912 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1913 |
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1914 |
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1915 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
1916 |
|
} else { |
1917 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1918 |
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1919 |
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1920 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
1921 |
} |
} |
1922 |
|
|
1923 |
|
} |
1924 |
|
} |
1925 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1926 |
stop_coding_timer(); |
stop_coding_timer(); |
1927 |
} |
} |
1928 |
} |
} |
1964 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
1965 |
|
|
1966 |
/* frame drop code */ |
/* frame drop code */ |
1967 |
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
1968 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
1969 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
1970 |
{ |
{ |
2015 |
Bitstream * bs, |
Bitstream * bs, |
2016 |
uint32_t * pBits) |
uint32_t * pBits) |
2017 |
{ |
{ |
2018 |
int16_t dct_codes[6 * 64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
2019 |
int16_t qcoeff[6 * 64]; |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
2020 |
uint32_t x, y; |
uint32_t x, y; |
2021 |
|
|
2022 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
2143 |
} |
} |
2144 |
#endif |
#endif |
2145 |
} |
} |
|
|
|
|
|
|
|
/* in case internal output is needed somewhere... */ |
|
|
/* { |
|
|
FILE *filehandle; |
|
|
filehandle=fopen("last-b.pgm","wb"); |
|
|
if (filehandle) |
|
|
{ |
|
|
fprintf(filehandle,"P5\n\n"); // |
|
|
fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); |
|
|
fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); |
|
|
fclose(filehandle); |
|
|
} |
|
|
} |
|
|
*/ |
|