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.33 2003-01-05 03:30:44 suxen_drol Exp $ |
* $Id: encoder.c,v 1.76.2.39 2003-01-15 14:36:06 syskin 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> |
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); |
661 |
if (intra < 0) return -1; |
if (intra < 0) return -1; |
662 |
if (intra == 1) return I_VOP; |
if (intra == 1) return I_VOP; |
663 |
if (intra == 2) return B_VOP; |
if (intra == 2) return B_VOP; |
664 |
|
if (intra == 3) return S_VOP; |
665 |
|
|
666 |
return P_VOP; |
return P_VOP; |
667 |
} |
} |
684 |
{ |
{ |
685 |
uint16_t x, y; |
uint16_t x, y; |
686 |
Bitstream bs; |
Bitstream bs; |
687 |
uint32_t bits, mode; |
uint32_t bits; |
688 |
|
int mode; |
689 |
|
|
690 |
int input_valid = 1; |
int input_valid = 1; |
691 |
int bframes_count = 0; |
int bframes_count = 0; |
731 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
732 |
bframes_count = 0; |
bframes_count = 0; |
733 |
|
|
734 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
735 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
736 |
|
if(pEnc->current->coding_type == P_VOP) |
737 |
pFrame->intra = 0; |
pFrame->intra = 0; |
738 |
|
else |
739 |
|
pFrame->intra = 3; |
740 |
|
|
741 |
emms(); |
emms(); |
742 |
|
|
743 |
|
if (pResult) { |
744 |
|
pResult->quant = pEnc->current->quant; |
745 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
746 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
747 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
748 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
749 |
|
} |
750 |
|
|
751 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
752 |
} |
} |
753 |
|
|
759 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
760 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
761 |
|
|
762 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
763 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
764 |
pFrame->intra = 2; |
pFrame->intra = 2; |
765 |
|
|
766 |
|
if (pResult) { |
767 |
|
pResult->quant = pEnc->current->quant; |
768 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
769 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
770 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
771 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
772 |
|
} |
773 |
|
|
774 |
if (input_valid) |
if (input_valid) |
775 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
776 |
|
|
796 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
797 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
798 |
|
|
|
BitstreamPad(&bs); |
|
799 |
|
|
800 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
801 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
802 |
|
|
803 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
804 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
805 |
|
|
806 |
|
BitstreamPadAlways(&bs); |
807 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
808 |
pFrame->intra = 4; |
pFrame->intra = 4; |
809 |
|
|
810 |
|
if (pResult) { |
811 |
|
pResult->quant = pEnc->current->quant; |
812 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
813 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
814 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
815 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
816 |
|
} |
817 |
|
|
818 |
if (input_valid) |
if (input_valid) |
819 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
820 |
|
|
888 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
889 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
890 |
|
|
891 |
BitstreamPutBits(&bs, 0x7f, 8); |
// BitstreamPutBits(&bs, 0x7f, 8); |
892 |
pFrame->intra = 5; |
pFrame->intra = 5; |
893 |
|
|
894 |
|
if (pResult) { |
895 |
|
/* |
896 |
|
* We must decide what to put there because i know some apps |
897 |
|
* are storing statistics about quantizers and just do |
898 |
|
* stats[quant]++ or stats[quant-1]++ |
899 |
|
* transcode is one of these app with its 2pass module |
900 |
|
*/ |
901 |
|
|
902 |
|
/* |
903 |
|
* For now i prefer 31 than 0 that could lead to a segfault |
904 |
|
* in transcode |
905 |
|
*/ |
906 |
|
pResult->quant = 31; |
907 |
|
|
908 |
|
pResult->hlength = 0; |
909 |
|
pResult->kblks = 0; |
910 |
|
pResult->mblks = 0; |
911 |
|
pResult->ublks = 0; |
912 |
|
} |
913 |
|
|
914 |
|
} else { |
915 |
|
|
916 |
|
if (pResult) { |
917 |
|
pResult->quant = pEnc->current->quant; |
918 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
919 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
920 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
921 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
922 |
|
} |
923 |
|
|
924 |
} |
} |
925 |
|
|
926 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
927 |
|
|
928 |
emms(); |
emms(); |
929 |
|
|
930 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
931 |
} |
} |
932 |
|
|
1064 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1065 |
bframes_count = 0; |
bframes_count = 0; |
1066 |
|
|
1067 |
|
if(pEnc->current->coding_type == P_VOP) |
1068 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1069 |
|
else |
1070 |
|
pFrame->intra = 3; |
1071 |
|
|
1072 |
|
|
1073 |
} else { |
} else { |
1074 |
|
|
1091 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
1092 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
1093 |
*/ |
*/ |
1094 |
} else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
} else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1095 |
/* |
/* |
1096 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
1097 |
*/ |
*/ |
1106 |
|
|
1107 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1108 |
bframes_count = 0; |
bframes_count = 0; |
1109 |
|
if(pEnc->current->coding_type == P_VOP) |
1110 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1111 |
|
else |
1112 |
|
pFrame->intra = 3; |
1113 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1114 |
|
|
1115 |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { |
1158 |
goto bvop_loop; |
goto bvop_loop; |
1159 |
} |
} |
1160 |
|
|
1161 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1162 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1163 |
|
|
1164 |
if (pResult) { |
if (pResult) { |
1177 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1178 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1179 |
|
|
1180 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
printf("PSNR: %f\n", psnr); |
1181 |
DEBUG(temp); |
// DEBUG(temp); |
1182 |
#endif |
#endif |
1183 |
|
|
1184 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1344 |
|
|
1345 |
} |
} |
1346 |
|
|
1347 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1348 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1349 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1350 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1351 |
|
|
1352 |
if (pResult) { |
if (pResult) { |
1370 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1371 |
|
|
1372 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1373 |
DEBUG(temp); |
// DEBUG(temp); |
1374 |
#endif |
#endif |
1375 |
|
|
1376 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1537 |
|
|
1538 |
if (intra) { |
if (intra) { |
1539 |
if (!hint->rawhints) { |
if (!hint->rawhints) { |
1540 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1541 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1542 |
} |
} |
1543 |
return; |
return; |
1643 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1644 |
|
|
1645 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1646 |
|
|
1647 |
|
BitstreamPadAlways(bs); |
1648 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1649 |
|
|
1650 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1700 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1701 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 30 |
1702 |
|
|
1703 |
|
|
1704 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1705 |
static int |
static int |
1706 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1707 |
Bitstream * bs, |
Bitstream * bs, |
1757 |
stop_inter_timer(); |
stop_inter_timer(); |
1758 |
} |
} |
1759 |
|
|
|
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 |
|
1760 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1761 |
|
|
1762 |
start_timer(); |
start_timer(); |
1763 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1764 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1765 |
else |
else |
|
|
|
1766 |
bIntra = |
bIntra = |
1767 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1768 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1772 |
|
|
1773 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1774 |
|
|
1775 |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1776 |
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) ) ) |
1777 |
|
{ |
1778 |
|
pEnc->current->coding_type = S_VOP; |
1779 |
|
|
1780 |
if (vol_header) |
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
1781 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
pEnc->mbParam.width, pEnc->mbParam.height, |
1782 |
|
&pEnc->current->gmc_data); |
1783 |
|
|
1784 |
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1785 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1786 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1787 |
|
pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, |
1788 |
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1789 |
|
|
1790 |
|
} |
1791 |
|
|
1792 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1793 |
|
if (vol_header) |
1794 |
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1795 |
|
BitstreamPadAlways(bs); |
1796 |
|
} |
1797 |
|
|
1798 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1799 |
|
|
1800 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1802 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1803 |
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; |
1804 |
|
|
1805 |
|
|
1806 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1807 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1808 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1809 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1810 |
|
|
1811 |
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1812 |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
1813 |
|
|
1814 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1815 |
|
|
1816 |
if (!bIntra) { |
if (bIntra) { |
1817 |
|
CodeIntraMB(pEnc, pMB); |
1818 |
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1819 |
|
dct_codes, qcoeff); |
1820 |
|
|
1821 |
|
start_timer(); |
1822 |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1823 |
|
stop_prediction_timer(); |
1824 |
|
|
1825 |
|
pEnc->current->sStat.kblks++; |
1826 |
|
|
1827 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1828 |
|
stop_coding_timer(); |
1829 |
|
continue; |
1830 |
|
} |
1831 |
|
|
1832 |
|
if (pEnc->current->coding_type == S_VOP) { |
1833 |
|
|
1834 |
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1835 |
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1836 |
|
pEnc->mbParam.edged_width, 65536); |
1837 |
|
|
1838 |
|
if (pEnc->current->motion_flags & PMV_CHROMA16) { |
1839 |
|
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1840 |
|
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1841 |
|
|
1842 |
|
iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1843 |
|
pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1844 |
|
} |
1845 |
|
|
1846 |
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1847 |
|
|
1848 |
|
if (pEnc->mbParam.m_quarterpel) |
1849 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1850 |
|
else |
1851 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1852 |
|
|
1853 |
|
pMB->mode = MODE_INTER; |
1854 |
|
pMB->mcsel = 1; |
1855 |
|
pMB->sad16 = iSAD; |
1856 |
|
} else { |
1857 |
|
pMB->mcsel = 0; |
1858 |
|
} |
1859 |
|
} else { |
1860 |
|
pMB->mcsel = 0; /* just a precaution */ |
1861 |
|
} |
1862 |
|
|
1863 |
start_timer(); |
start_timer(); |
1864 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1865 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1866 |
&pEnc->vInterHV, &pEnc->current->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
1867 |
|
&pEnc->current->image, |
1868 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1869 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1870 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1871 |
pEnc->mbParam.m_quarterpel, |
pEnc->mbParam.m_quarterpel, |
1872 |
(pEnc->current->global_flags & XVID_REDUCED), |
(pEnc->current->global_flags & XVID_REDUCED), |
1873 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1874 |
|
|
1875 |
stop_comp_timer(); |
stop_comp_timer(); |
1876 |
|
|
1877 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1889 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1890 |
|
|
1891 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1892 |
pMB->cbp = |
{ pMB->cbp = |
1893 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1894 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
|
} else { |
|
|
CodeIntraMB(pEnc, pMB); |
|
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
|
dct_codes, qcoeff); |
|
1895 |
} |
} |
1896 |
|
|
1897 |
start_timer(); |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
|
|
stop_prediction_timer(); |
|
|
|
|
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
|
pEnc->current->sStat.kblks++; |
|
|
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
1898 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1899 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1900 |
pEnc->current->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
1906 |
|
|
1907 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1908 |
|
|
1909 |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1910 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1911 |
|
|
1912 |
|
if (pEnc->current->coding_type == S_VOP) |
1913 |
|
skip_possible &= (pMB->mcsel == 1); |
1914 |
|
else if (pEnc->current->coding_type == P_VOP) { |
1915 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) |
1916 |
{ 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) ); |
|
} |
|
1917 |
else |
else |
1918 |
{ 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) ); |
1919 |
} |
} |
1920 |
|
|
1921 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1922 |
|
|
1923 |
/* 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; |
|
1924 |
|
|
1925 |
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 */ |
1926 |
|
{ |
1927 |
|
int bSkip = 1; |
1928 |
|
|
1929 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1930 |
{ |
{ |
1931 |
int iSAD; |
int iSAD; |
1938 |
} |
} |
1939 |
} |
} |
1940 |
|
|
1941 |
if (!bSkip) |
if (!bSkip) { /* no SKIP, but trivial block */ |
|
{ |
|
|
VECTOR predMV; |
|
1942 |
if(pEnc->mbParam.m_quarterpel) { |
if(pEnc->mbParam.m_quarterpel) { |
1943 |
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); |
1944 |
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; |
1945 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1946 |
} |
} |
1947 |
else { |
else { |
1948 |
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); |
1949 |
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; |
1950 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1951 |
} |
} |
1952 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1953 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1954 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1955 |
|
stop_coding_timer(); |
1956 |
|
|
1957 |
|
continue; /* next MB */ |
1958 |
} |
} |
1959 |
else |
} |
1960 |
{ |
/* do SKIP */ |
1961 |
|
|
1962 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1963 |
MBSkip(bs); |
MBSkip(bs); |
1964 |
|
stop_coding_timer(); |
1965 |
|
continue; /* next MB */ |
1966 |
} |
} |
1967 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1968 |
|
|
|
} else { |
|
1969 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1970 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1971 |
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 */ |
1972 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1973 |
} |
} |
1974 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
1975 |
|
if(pEnc->mbParam.m_quarterpel) { |
1976 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1977 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1978 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1979 |
|
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); |
1980 |
|
} else { |
1981 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1982 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1983 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1984 |
|
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); |
1985 |
|
} |
1986 |
|
|
1987 |
|
|
1988 |
|
if (pMB->mode == MODE_INTER4V) |
1989 |
|
{ int k; |
1990 |
|
for (k=1;k<4;k++) |
1991 |
|
{ |
1992 |
|
if(pEnc->mbParam.m_quarterpel) { |
1993 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1994 |
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1995 |
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1996 |
|
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); |
1997 |
|
} else { |
1998 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1999 |
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
2000 |
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
2001 |
|
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); |
2002 |
} |
} |
2003 |
|
|
2004 |
|
} |
2005 |
|
} |
2006 |
|
|
2007 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
2008 |
stop_coding_timer(); |
stop_coding_timer(); |
2009 |
|
|
2010 |
} |
} |
2011 |
} |
} |
2012 |
|
|
2047 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
2048 |
|
|
2049 |
/* frame drop code */ |
/* frame drop code */ |
2050 |
// 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); |
2051 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
2052 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
2053 |
{ |
{ |
2226 |
} |
} |
2227 |
#endif |
#endif |
2228 |
} |
} |
|
|
|
|
|
|
|
/* 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); |
|
|
} |
|
|
} |
|
|
*/ |
|