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.26 2002-12-09 10:47:05 suxen_drol Exp $ |
* $Id: encoder.c,v 1.76.2.37 2003-01-13 14:33:24 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); |
655 |
|
|
656 |
|
|
657 |
|
|
658 |
|
/* convert pFrame->intra to coding_type */ |
659 |
|
static int intra2coding_type(int intra) |
660 |
|
{ |
661 |
|
if (intra < 0) return -1; |
662 |
|
if (intra == 1) return I_VOP; |
663 |
|
if (intra == 2) return B_VOP; |
664 |
|
|
665 |
|
return P_VOP; |
666 |
|
} |
667 |
|
|
668 |
|
|
669 |
|
|
670 |
/***************************************************************************** |
/***************************************************************************** |
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; |
732 |
|
|
733 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
734 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
735 |
pFrame->intra = 0; |
pFrame->intra = 0; |
736 |
|
|
747 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
748 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
749 |
|
|
750 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
751 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
752 |
pFrame->intra = 2; |
pFrame->intra = 2; |
753 |
|
|
776 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
777 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
778 |
|
|
|
BitstreamPad(&bs); |
|
779 |
|
|
780 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
781 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
782 |
|
|
783 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
784 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
785 |
|
|
786 |
|
BitstreamPadAlways(&bs); |
787 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
788 |
pFrame->intra = 4; |
pFrame->intra = 4; |
789 |
|
|
802 |
if (pEnc->bframenum_dx50bvop != -1) |
if (pEnc->bframenum_dx50bvop != -1) |
803 |
{ |
{ |
804 |
|
|
805 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
806 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
807 |
|
|
808 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
809 |
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"); |
817 |
|
|
818 |
} else if (input_valid) { |
} else if (input_valid) { |
819 |
|
|
820 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
821 |
|
|
822 |
start_timer(); |
start_timer(); |
823 |
if (image_input |
if (image_input |
843 |
|
|
844 |
} else if (pEnc->queue_size > 0) { |
} else if (pEnc->queue_size > 0) { |
845 |
|
|
846 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
847 |
|
|
848 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
849 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
860 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
861 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
862 |
|
|
863 |
BitstreamPutBits(&bs, 0x7f, 8); |
// BitstreamPutBits(&bs, 0x7f, 8); |
864 |
pFrame->intra = 5; |
pFrame->intra = 5; |
865 |
} |
} |
866 |
|
|
876 |
// only inc frame num, adapt quant, etc. if we havent seen it before |
// only inc frame num, adapt quant, etc. if we havent seen it before |
877 |
if (pEnc->bframenum_dx50bvop < 0 ) |
if (pEnc->bframenum_dx50bvop < 0 ) |
878 |
{ |
{ |
879 |
|
mode = intra2coding_type(pFrame->intra); |
880 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
881 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
882 |
else |
else |
950 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
951 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
952 |
|
|
953 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
954 |
(pFrame->intra < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
955 |
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
956 |
|| 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
{ |
957 |
|
mode = I_VOP; |
958 |
|
}else{ |
959 |
|
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
960 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
961 |
(pFrame->intra < 0) ? pEnc->iFrameNum : 0, |
(mode < 0) ? pEnc->iFrameNum : 0, |
962 |
bframes_count++))) { |
bframes_count++); |
963 |
|
} |
964 |
|
|
965 |
|
if (mode == I_VOP) { |
966 |
/* |
/* |
967 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
968 |
*/ |
*/ |
996 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
997 |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
998 |
|
|
999 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
1000 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1001 |
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"); |
1002 |
} |
} |
1026 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
1027 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
1028 |
*/ |
*/ |
1029 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
} else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
|
// } else if (pFrame->intra == 0 || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
|
1030 |
/* |
/* |
1031 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
1032 |
*/ |
*/ |
1050 |
goto ipvop_loop; |
goto ipvop_loop; |
1051 |
} |
} |
1052 |
|
|
1053 |
} else { |
} else { /* mode == B_VOP */ |
1054 |
/* |
/* |
1055 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1056 |
*/ |
*/ |
1077 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1078 |
|
|
1079 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1080 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1081 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1082 |
|
|
1083 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1084 |
|
|
1090 |
goto bvop_loop; |
goto bvop_loop; |
1091 |
} |
} |
1092 |
|
|
1093 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1094 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1095 |
|
|
1096 |
if (pResult) { |
if (pResult) { |
1109 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1110 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1111 |
|
|
1112 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
printf("PSNR: %f\n", psnr); |
1113 |
DEBUG(temp); |
// DEBUG(temp); |
1114 |
#endif |
#endif |
1115 |
|
|
1116 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1158 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
1159 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
1160 |
|
|
1161 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1162 |
|
|
1163 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1164 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1276 |
|
|
1277 |
} |
} |
1278 |
|
|
1279 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1280 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1281 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1282 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1283 |
|
|
1284 |
if (pResult) { |
if (pResult) { |
1302 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1303 |
|
|
1304 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1305 |
DEBUG(temp); |
// DEBUG(temp); |
1306 |
#endif |
#endif |
1307 |
|
|
1308 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1469 |
|
|
1470 |
if (intra) { |
if (intra) { |
1471 |
if (!hint->rawhints) { |
if (!hint->rawhints) { |
1472 |
BitstreamPad(&bs); |
BitstreamPadAlways(&bs); |
1473 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1474 |
} |
} |
1475 |
return; |
return; |
1574 |
|
|
1575 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1576 |
|
|
|
/* 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)); |
|
|
|
|
1577 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1578 |
|
|
1579 |
|
BitstreamPadAlways(bs); |
1580 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1581 |
|
|
1582 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1609 |
stop_coding_timer(); |
stop_coding_timer(); |
1610 |
} |
} |
1611 |
|
|
1612 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1613 |
|
{ |
1614 |
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1615 |
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1616 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
1617 |
|
} |
1618 |
emms(); |
emms(); |
1619 |
|
|
1620 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1632 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1633 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 30 |
1634 |
|
|
1635 |
|
|
1636 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1637 |
static int |
static int |
1638 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1639 |
Bitstream * bs, |
Bitstream * bs, |
1659 |
|
|
1660 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1661 |
{ |
{ |
1662 |
// mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1663 |
// mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
|
|
|
|
/* XXX: reduced resoltion not yet supported */ |
|
|
pEnc->current->global_flags &= ~XVID_REDUCED; |
|
1664 |
} |
} |
1665 |
|
|
1666 |
|
|
1689 |
stop_inter_timer(); |
stop_inter_timer(); |
1690 |
} |
} |
1691 |
|
|
|
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 |
|
1692 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1693 |
|
|
1694 |
start_timer(); |
start_timer(); |
1695 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1696 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1697 |
if (bIntra == 0) { |
else |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
|
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
1698 |
bIntra = |
bIntra = |
1699 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1700 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1701 |
iLimit); |
iLimit); |
1702 |
} |
|
1703 |
stop_motion_timer(); |
stop_motion_timer(); |
1704 |
|
|
1705 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1706 |
|
|
1707 |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1708 |
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) ) ) |
1709 |
|
{ |
1710 |
|
pEnc->current->coding_type = S_VOP; |
1711 |
|
|
1712 |
if (vol_header) |
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
1713 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
pEnc->mbParam.width, pEnc->mbParam.height, |
1714 |
|
&pEnc->current->gmc_data); |
1715 |
|
|
1716 |
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1717 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1718 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1719 |
|
pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, |
1720 |
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1721 |
|
|
1722 |
|
} |
1723 |
|
|
1724 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1725 |
|
if (vol_header) |
1726 |
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1727 |
|
BitstreamPadAlways(bs); |
1728 |
|
} |
1729 |
|
|
1730 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1731 |
|
|
1732 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1734 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1735 |
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; |
1736 |
|
|
1737 |
|
|
1738 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1739 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1740 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1741 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1742 |
|
|
1743 |
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1744 |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
1745 |
|
|
1746 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1747 |
|
|
1748 |
if (!bIntra) { |
if (bIntra) { |
1749 |
|
CodeIntraMB(pEnc, pMB); |
1750 |
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1751 |
|
dct_codes, qcoeff); |
1752 |
|
|
1753 |
|
start_timer(); |
1754 |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1755 |
|
stop_prediction_timer(); |
1756 |
|
|
1757 |
|
pEnc->current->sStat.kblks++; |
1758 |
|
|
1759 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1760 |
|
stop_coding_timer(); |
1761 |
|
continue; |
1762 |
|
} |
1763 |
|
|
1764 |
|
if (pEnc->current->coding_type == S_VOP) { |
1765 |
|
|
1766 |
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1767 |
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1768 |
|
pEnc->mbParam.edged_width, 65536); |
1769 |
|
|
1770 |
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1771 |
|
|
1772 |
|
if (pEnc->mbParam.m_quarterpel) |
1773 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1774 |
|
else |
1775 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1776 |
|
|
1777 |
|
pMB->mode = MODE_INTER; |
1778 |
|
pMB->mcsel = 1; |
1779 |
|
pMB->sad16 = iSAD; |
1780 |
|
} else { |
1781 |
|
pMB->mcsel = 0; |
1782 |
|
} |
1783 |
|
} else { |
1784 |
|
pMB->mcsel = 0; /* just a precaution */ |
1785 |
|
} |
1786 |
|
|
1787 |
start_timer(); |
start_timer(); |
1788 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1789 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1790 |
&pEnc->vInterHV, &pEnc->current->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
1791 |
|
&pEnc->current->image, |
1792 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1793 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1794 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1795 |
pEnc->mbParam.m_quarterpel, |
pEnc->mbParam.m_quarterpel, |
1796 |
(pEnc->current->global_flags & XVID_REDUCED), |
(pEnc->current->global_flags & XVID_REDUCED), |
1797 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1798 |
|
|
1799 |
stop_comp_timer(); |
stop_comp_timer(); |
1800 |
|
|
1801 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1813 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1814 |
|
|
1815 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1816 |
pMB->cbp = |
{ pMB->cbp = |
1817 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1818 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
|
} else { |
|
|
CodeIntraMB(pEnc, pMB); |
|
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
|
dct_codes, qcoeff); |
|
1819 |
} |
} |
1820 |
|
|
1821 |
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 || |
|
1822 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1823 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1824 |
pEnc->current->sStat.mblks++; |
pEnc->current->sStat.mblks++; |
1830 |
|
|
1831 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1832 |
|
|
1833 |
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1834 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == NO_CHANGE); |
1835 |
|
|
1836 |
|
if (pEnc->current->coding_type == S_VOP) |
1837 |
|
skip_possible &= (pMB->mcsel == 1); |
1838 |
|
else if (pEnc->current->coding_type == P_VOP) { |
1839 |
if(pEnc->mbParam.m_quarterpel) |
if(pEnc->mbParam.m_quarterpel) |
1840 |
{ 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) ); |
|
} |
|
1841 |
else |
else |
1842 |
{ 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) ); |
1843 |
} |
} |
1844 |
|
|
1845 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1846 |
|
|
1847 |
/* 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; |
|
1848 |
|
|
1849 |
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 */ |
1850 |
|
{ |
1851 |
|
int bSkip = 1; |
1852 |
|
|
1853 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1854 |
{ |
{ |
1855 |
int iSAD; |
int iSAD; |
1862 |
} |
} |
1863 |
} |
} |
1864 |
|
|
1865 |
if (!bSkip) |
if (!bSkip) { /* no SKIP, but trivial block */ |
|
{ |
|
|
VECTOR predMV; |
|
1866 |
if(pEnc->mbParam.m_quarterpel) { |
if(pEnc->mbParam.m_quarterpel) { |
1867 |
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); |
1868 |
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; |
1869 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1870 |
} |
} |
1871 |
else { |
else { |
1872 |
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); |
1873 |
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; |
1874 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1875 |
} |
} |
1876 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1877 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1878 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1879 |
|
stop_coding_timer(); |
1880 |
|
|
1881 |
|
continue; /* next MB */ |
1882 |
} |
} |
1883 |
else |
} |
1884 |
{ |
/* do SKIP */ |
1885 |
|
|
1886 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1887 |
MBSkip(bs); |
MBSkip(bs); |
1888 |
|
stop_coding_timer(); |
1889 |
|
continue; /* next MB */ |
1890 |
} |
} |
1891 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1892 |
|
|
|
} else { |
|
1893 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1894 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1895 |
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 */ |
1896 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1897 |
} |
} |
1898 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
1899 |
|
if(pEnc->mbParam.m_quarterpel) { |
1900 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1901 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1902 |
|
pMB->pmvs[0].y = pMB->qmvs[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 |
|
} else { |
1905 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1906 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1907 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1908 |
|
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); |
1909 |
|
} |
1910 |
|
|
1911 |
|
|
1912 |
|
if (pMB->mode == MODE_INTER4V) |
1913 |
|
{ int k; |
1914 |
|
for (k=1;k<4;k++) |
1915 |
|
{ |
1916 |
|
if(pEnc->mbParam.m_quarterpel) { |
1917 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1918 |
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1919 |
|
pMB->pmvs[k].y = pMB->qmvs[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 |
|
} else { |
1922 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1923 |
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1924 |
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1925 |
|
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); |
1926 |
} |
} |
1927 |
|
|
1928 |
|
} |
1929 |
|
} |
1930 |
|
|
1931 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1932 |
stop_coding_timer(); |
stop_coding_timer(); |
1933 |
|
|
1934 |
} |
} |
1935 |
} |
} |
1936 |
|
|
1937 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1938 |
|
{ |
1939 |
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1940 |
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1941 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
1942 |
|
} |
1943 |
|
|
1944 |
emms(); |
emms(); |
1945 |
|
|
1946 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1971 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
1972 |
|
|
1973 |
/* frame drop code */ |
/* frame drop code */ |
1974 |
// 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); |
1975 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
1976 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
1977 |
{ |
{ |
2009 |
} |
} |
2010 |
*/ |
*/ |
2011 |
|
|
2012 |
|
|
2013 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2014 |
|
|
2015 |
return 0; // inter |
return 0; // inter |
2022 |
Bitstream * bs, |
Bitstream * bs, |
2023 |
uint32_t * pBits) |
uint32_t * pBits) |
2024 |
{ |
{ |
2025 |
int16_t dct_codes[6 * 64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
2026 |
int16_t qcoeff[6 * 64]; |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
2027 |
uint32_t x, y; |
uint32_t x, y; |
2028 |
|
|
2029 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
2150 |
} |
} |
2151 |
#endif |
#endif |
2152 |
} |
} |
|
|
|
|
|
|
|
/* 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); |
|
|
} |
|
|
} |
|
|
*/ |
|