44 |
#include "motion.h" |
#include "motion.h" |
45 |
#include "sad.h" |
#include "sad.h" |
46 |
#include "../utils/emms.h" |
#include "../utils/emms.h" |
47 |
|
#include "../dct/fdct.h" |
48 |
|
|
49 |
|
/***************************************************************************** |
50 |
|
* Modified rounding tables -- declared in motion.h |
51 |
|
* Original tables see ISO spec tables 7-6 -> 7-9 |
52 |
|
****************************************************************************/ |
53 |
|
|
54 |
|
const uint32_t roundtab[16] = |
55 |
|
{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
56 |
|
|
57 |
|
/* K = 4 */ |
58 |
|
const uint32_t roundtab_76[16] = |
59 |
|
{ 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 }; |
60 |
|
|
61 |
|
/* K = 2 */ |
62 |
|
const uint32_t roundtab_78[8] = |
63 |
|
{ 0, 0, 1, 1, 0, 0, 0, 1 }; |
64 |
|
|
65 |
|
/* K = 1 */ |
66 |
|
const uint32_t roundtab_79[4] = |
67 |
|
{ 0, 1, 0, 0 }; |
68 |
|
|
69 |
#define INITIAL_SKIP_THRESH (10) |
#define INITIAL_SKIP_THRESH (10) |
70 |
#define FINAL_SKIP_THRESH (50) |
#define FINAL_SKIP_THRESH (50) |
72 |
#define MAX_CHROMA_SAD_FOR_SKIP (22) |
#define MAX_CHROMA_SAD_FOR_SKIP (22) |
73 |
|
|
74 |
#define CHECK_CANDIDATE(X,Y,D) { \ |
#define CHECK_CANDIDATE(X,Y,D) { \ |
75 |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
CheckCandidate((X),(Y), (D), &iDirection, data ); } |
76 |
|
|
77 |
|
/***************************************************************************** |
78 |
|
* Code |
79 |
|
****************************************************************************/ |
80 |
|
|
81 |
static __inline uint32_t |
static __inline uint32_t |
82 |
d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) |
d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) |
384 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
385 |
|
|
386 |
if (!data->qpel_precision) Reference = GetReference(x, y, data); |
if (!data->qpel_precision) Reference = GetReference(x, y, data); |
387 |
else Reference = Interpolate16x16qpel(x, y, 0, data); |
else Reference = Interpolate8x8qpel(x, y, 0, 0, data); |
388 |
|
|
389 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
390 |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
463 |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
464 |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
465 |
|
|
|
|
|
466 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
467 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
468 |
current->x = x; current->y = y; |
current->x = x; current->y = y; |
654 |
} |
} |
655 |
} |
} |
656 |
|
|
657 |
|
|
658 |
|
static void |
659 |
|
CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
660 |
|
{ |
661 |
|
|
662 |
|
static int16_t in[64], coeff[64]; |
663 |
|
int32_t bits = 0, sum; |
664 |
|
VECTOR * current; |
665 |
|
const uint8_t * ptr; |
666 |
|
int i, cbp = 0, t, xc, yc; |
667 |
|
|
668 |
|
if ( (x > data->max_dx) || (x < data->min_dx) |
669 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
670 |
|
|
671 |
|
if (!data->qpel_precision) { |
672 |
|
ptr = GetReference(x, y, data); |
673 |
|
current = data->currentMV; |
674 |
|
xc = x; yc = y; |
675 |
|
} else { // x and y are in 1/4 precision |
676 |
|
ptr = Interpolate16x16qpel(x, y, 0, data); |
677 |
|
current = data->currentQMV; |
678 |
|
xc = x/2; yc = y/2; |
679 |
|
} |
680 |
|
|
681 |
|
for(i = 0; i < 4; i++) { |
682 |
|
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
683 |
|
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
684 |
|
fdct(in); |
685 |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
686 |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
687 |
|
if (sum > 0) { |
688 |
|
cbp |= 1 << (5 - i); |
689 |
|
bits += data->temp[i] = CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
690 |
|
} else data->temp[i] = 0; |
691 |
|
} |
692 |
|
|
693 |
|
bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
694 |
|
|
695 |
|
if (bits < data->iMinSAD[0]) { // there is still a chance, adding chroma |
696 |
|
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
697 |
|
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
698 |
|
|
699 |
|
//chroma U |
700 |
|
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCU, 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
701 |
|
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
702 |
|
fdct(in); |
703 |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
704 |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
705 |
|
if (sum > 0) { |
706 |
|
cbp |= 1 << (5 - 4); |
707 |
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
708 |
|
} |
709 |
|
|
710 |
|
if (bits < data->iMinSAD[0]) { |
711 |
|
//chroma V |
712 |
|
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCV, 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
713 |
|
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
714 |
|
fdct(in); |
715 |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
716 |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
717 |
|
if (sum > 0) { |
718 |
|
cbp |= 1 << (5 - 5); |
719 |
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
720 |
|
} |
721 |
|
} |
722 |
|
} |
723 |
|
|
724 |
|
bits += cbpy_tab[15-(cbp>>2)].len; |
725 |
|
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
726 |
|
|
727 |
|
if (bits < data->iMinSAD[0]) { |
728 |
|
data->iMinSAD[0] = bits; |
729 |
|
current[0].x = x; current[0].y = y; |
730 |
|
*dir = Direction; |
731 |
|
} |
732 |
|
|
733 |
|
if (data->temp[0] + t < data->iMinSAD[1]) { |
734 |
|
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
735 |
|
if (data->temp[1] < data->iMinSAD[2]) { |
736 |
|
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
737 |
|
if (data->temp[2] < data->iMinSAD[3]) { |
738 |
|
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
739 |
|
if (data->temp[3] < data->iMinSAD[4]) { |
740 |
|
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
741 |
|
|
742 |
|
} |
743 |
|
static void |
744 |
|
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
745 |
|
{ |
746 |
|
|
747 |
|
static int16_t in[64], coeff[64]; |
748 |
|
int32_t sum, bits; |
749 |
|
VECTOR * current; |
750 |
|
const uint8_t * ptr; |
751 |
|
int cbp; |
752 |
|
|
753 |
|
if ( (x > data->max_dx) || (x < data->min_dx) |
754 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
755 |
|
|
756 |
|
if (!data->qpel_precision) { |
757 |
|
ptr = GetReference(x, y, data); |
758 |
|
current = data->currentMV; |
759 |
|
} else { // x and y are in 1/4 precision |
760 |
|
ptr = Interpolate8x8qpel(x, y, 0, 0, data); |
761 |
|
current = data->currentQMV; |
762 |
|
} |
763 |
|
|
764 |
|
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
765 |
|
fdct(in); |
766 |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
767 |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
768 |
|
if (sum > 0) { |
769 |
|
bits = CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
770 |
|
cbp = 1; |
771 |
|
} else cbp = bits = 0; |
772 |
|
|
773 |
|
bits += sum = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
774 |
|
|
775 |
|
if (bits < data->iMinSAD[0]) { |
776 |
|
data->temp[0] = cbp; |
777 |
|
data->iMinSAD[0] = bits; |
778 |
|
current[0].x = x; current[0].y = y; |
779 |
|
*dir = Direction; |
780 |
|
} |
781 |
|
} |
782 |
|
|
783 |
/* CHECK_CANDIATE FUNCTIONS END */ |
/* CHECK_CANDIATE FUNCTIONS END */ |
784 |
|
|
785 |
/* MAINSEARCH FUNCTIONS START */ |
/* MAINSEARCH FUNCTIONS START */ |
995 |
uint32_t mb_width = pParam->mb_width; |
uint32_t mb_width = pParam->mb_width; |
996 |
uint32_t mb_height = pParam->mb_height; |
uint32_t mb_height = pParam->mb_height; |
997 |
const uint32_t iEdgedWidth = pParam->edged_width; |
const uint32_t iEdgedWidth = pParam->edged_width; |
998 |
|
const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->global_flags); |
999 |
|
|
1000 |
uint32_t x, y; |
uint32_t x, y; |
1001 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
1002 |
int32_t InterBias, quant = current->quant, sad00; |
int32_t quant = current->quant, sad00; |
1003 |
|
|
1004 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
1005 |
int32_t temp[8]; |
int32_t temp[8]; |
1016 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
1017 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
1018 |
Data.qpel = pParam->m_quarterpel; |
Data.qpel = pParam->m_quarterpel; |
1019 |
Data.chroma = current->motion_flags & PMV_CHROMA16; |
Data.chroma = MotionFlags & PMV_CHROMA16; |
1020 |
Data.rrv = current->global_flags & XVID_REDUCED; |
Data.rrv = current->global_flags & XVID_REDUCED; |
1021 |
|
|
1022 |
if ((current->global_flags & XVID_REDUCED)) { |
if ((current->global_flags & XVID_REDUCED)) { |
1023 |
mb_width = (pParam->width + 31) / 32; |
mb_width = (pParam->width + 31) / 32; |
1024 |
mb_height = (pParam->height + 31) / 32; |
mb_height = (pParam->height + 31) / 32; |
1025 |
Data.qpel = Data.chroma = 0; |
Data.qpel = 0; |
1026 |
} |
} |
1027 |
|
|
1028 |
Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose) |
Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose) |
1074 |
} |
} |
1075 |
|
|
1076 |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1077 |
y, current->motion_flags, pMB->quant, |
y, MotionFlags, current->global_flags, pMB->quant, |
1078 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, |
1079 |
current->global_flags & XVID_INTER4V, pMB); |
current->global_flags & XVID_INTER4V, pMB); |
1080 |
|
|
1081 |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
1082 |
if (!(current->global_flags & XVID_GMC)) { |
if (!(current->global_flags & XVID_GMC)) { |
1083 |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
1084 |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1)) ) |
if (!(current->global_flags & XVID_MODEDECISION_BITS)) { |
1085 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
1086 |
|
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) |
1087 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1088 |
continue; |
} else { // BITS mode decision |
1089 |
} |
if (pMB->sad16 > 10) |
1090 |
} |
SkipMacroblockP(pMB, sad00); // more than 10 bits would be used for this MB - skip |
|
|
|
|
/* finally, intra decision */ |
|
1091 |
|
|
|
InterBias = MV16_INTER_BIAS; |
|
|
if (pMB->quant > 8) InterBias += 100 * (pMB->quant - 8); // to make high quants work |
|
|
if (y != 0) |
|
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
|
|
if (x != 0) |
|
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
|
|
|
|
|
if (Data.chroma) InterBias += 50; // to compensate bigger SAD |
|
|
if (Data.rrv) InterBias *= 4; |
|
|
|
|
|
if (InterBias < pMB->sad16) { |
|
|
int32_t deviation; |
|
|
if (!Data.rrv) |
|
|
deviation = dev16(pCurrent->y + (x + y * iEdgedWidth) * 16, iEdgedWidth); |
|
|
else { |
|
|
deviation = dev16(pCurrent->y + (x + y * iEdgedWidth) * 32, iEdgedWidth) |
|
|
+ dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16, iEdgedWidth) |
|
|
+ dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16 * iEdgedWidth, iEdgedWidth) |
|
|
+ dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16 * (iEdgedWidth+1), iEdgedWidth); |
|
|
} |
|
|
if (deviation < (pMB->sad16 - InterBias)) { |
|
|
if (++iIntra >= iLimit) return 1; |
|
|
SkipMacroblockP(pMB, 0); //same thing |
|
|
pMB->mode = MODE_INTRA; |
|
1092 |
} |
} |
1093 |
} |
} |
1094 |
} |
} |
1095 |
|
if (pMB->mode == MODE_INTRA) |
1096 |
|
if (++iIntra > iLimit) return 1; |
1097 |
|
} |
1098 |
} |
} |
1099 |
|
|
1100 |
if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ |
if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ |
1101 |
{ |
{ |
1102 |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
1103 |
} |
} |
|
|
|
1104 |
return 0; |
return 0; |
1105 |
} |
} |
1106 |
|
|
1164 |
} |
} |
1165 |
} |
} |
1166 |
|
|
1167 |
|
static int |
1168 |
|
ModeDecision(const uint32_t iQuant, SearchData * const Data, |
1169 |
|
int inter4v, |
1170 |
|
MACROBLOCK * const pMB, |
1171 |
|
const MACROBLOCK * const pMBs, |
1172 |
|
const int x, const int y, |
1173 |
|
const MBParam * const pParam, |
1174 |
|
const uint32_t MotionFlags, |
1175 |
|
const uint32_t GlobalFlags) |
1176 |
|
{ |
1177 |
|
|
1178 |
|
int mode = MODE_INTER; |
1179 |
|
|
1180 |
|
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision |
1181 |
|
// int intra = 0; |
1182 |
|
int sad; |
1183 |
|
int InterBias = MV16_INTER_BIAS; |
1184 |
|
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
1185 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
1186 |
|
mode = 0; //inter |
1187 |
|
sad = Data->iMinSAD[0]; |
1188 |
|
} else { |
1189 |
|
mode = MODE_INTER4V; |
1190 |
|
sad = Data->iMinSAD[1] + Data->iMinSAD[2] + |
1191 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; |
1192 |
|
Data->iMinSAD[0] = sad; |
1193 |
|
} |
1194 |
|
|
1195 |
|
/* intra decision */ |
1196 |
|
|
1197 |
|
if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work |
1198 |
|
if (y != 0) |
1199 |
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
1200 |
|
if (x != 0) |
1201 |
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
1202 |
|
|
1203 |
|
if (Data->chroma) InterBias += 50; // to compensate bigger SAD |
1204 |
|
if (Data->rrv) InterBias *= 4; |
1205 |
|
|
1206 |
|
if (InterBias < pMB->sad16) { |
1207 |
|
int32_t deviation; |
1208 |
|
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
1209 |
|
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
1210 |
|
dev16(Data->Cur+8, Data->iEdgedWidth) + |
1211 |
|
dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) + |
1212 |
|
dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth); |
1213 |
|
|
1214 |
|
if (deviation < (sad - InterBias)) return MODE_INTRA;// intra |
1215 |
|
} |
1216 |
|
return mode; |
1217 |
|
|
1218 |
|
} else { |
1219 |
|
|
1220 |
|
int bits, intra, i; |
1221 |
|
VECTOR backup[5], *v; |
1222 |
|
Data->lambda16 = iQuant; |
1223 |
|
Data->lambda8 = pParam->m_quant_type; |
1224 |
|
|
1225 |
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
1226 |
|
for (i = 0; i < 5; i++) { |
1227 |
|
Data->iMinSAD[i] = 256*4096; |
1228 |
|
backup[i] = v[i]; |
1229 |
|
} |
1230 |
|
|
1231 |
|
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
1232 |
|
if (bits == 0) return MODE_INTER; // quick stop |
1233 |
|
|
1234 |
|
if (inter4v) { |
1235 |
|
int inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
1236 |
|
if (inter4v < bits) { Data->iMinSAD[0] = bits = inter4v; mode = MODE_INTER4V; } |
1237 |
|
} |
1238 |
|
|
1239 |
|
|
1240 |
|
intra = CountMBBitsIntra(Data); |
1241 |
|
|
1242 |
|
if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; } |
1243 |
|
|
1244 |
|
return mode; |
1245 |
|
} |
1246 |
|
} |
1247 |
|
|
1248 |
static void |
static void |
1249 |
SearchP(const IMAGE * const pRef, |
SearchP(const IMAGE * const pRef, |
1250 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1254 |
const int x, |
const int x, |
1255 |
const int y, |
const int y, |
1256 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1257 |
|
const uint32_t GlobalFlags, |
1258 |
const uint32_t iQuant, |
const uint32_t iQuant, |
1259 |
SearchData * const Data, |
SearchData * const Data, |
1260 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1304 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1305 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1306 |
|
|
1307 |
if (x | y) { |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) { |
1308 |
threshA = Data->temp[0]; // that's when we keep this SAD atm |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
1309 |
if (threshA < 512) threshA = 512; |
if (threshA < 512) threshA = 512; |
1310 |
else if (threshA > 1024) threshA = 1024; |
else if (threshA > 1024) threshA = 1024; |
1311 |
} else threshA = 512; |
} else |
1312 |
|
threshA = 512; |
1313 |
|
|
1314 |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
1315 |
prevMBs + x + y * pParam->mb_width, Data->rrv); |
prevMBs + x + y * pParam->mb_width, Data->rrv); |
1323 |
|
|
1324 |
for (i = 1; i < 7; i++) { |
for (i = 1; i < 7; i++) { |
1325 |
if (!(mask = make_mask(pmv, i)) ) continue; |
if (!(mask = make_mask(pmv, i)) ) continue; |
1326 |
(*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
CheckCandidate(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
1327 |
if (Data->iMinSAD[0] <= threshA) break; |
if (Data->iMinSAD[0] <= threshA) break; |
1328 |
} |
} |
1329 |
|
|
1330 |
if ((Data->iMinSAD[0] <= threshA) || |
if ((Data->iMinSAD[0] <= threshA) || |
1331 |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
1332 |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) { |
1333 |
inter4v = 0; |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) inter4v = 0; } |
1334 |
else { |
else { |
1335 |
|
|
1336 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1338 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
1339 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1340 |
|
|
1341 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1342 |
|
|
1343 |
/* extended search, diamond starting in 0,0 and in prediction. |
/* extended search, diamond starting in 0,0 and in prediction. |
1344 |
note that this search is/might be done in halfpel positions, |
note that this search is/might be done in halfpel positions, |
1350 |
if (Data->rrv) { |
if (Data->rrv) { |
1351 |
startMV.x = RRV_MV_SCALEUP(startMV.x); |
startMV.x = RRV_MV_SCALEUP(startMV.x); |
1352 |
startMV.y = RRV_MV_SCALEUP(startMV.y); |
startMV.y = RRV_MV_SCALEUP(startMV.y); |
1353 |
} else |
} |
|
if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel? |
|
|
startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y); |
|
1354 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1355 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1356 |
|
|
1357 |
(*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data); |
CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data); |
1358 |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
MainSearchPtr(startMV.x, startMV.y, Data, 255); |
1359 |
if (bSAD < Data->iMinSAD[0]) { |
if (bSAD < Data->iMinSAD[0]) { |
1360 |
Data->currentMV[0] = backupMV; |
Data->currentMV[0] = backupMV; |
1361 |
Data->iMinSAD[0] = bSAD; } |
Data->iMinSAD[0] = bSAD; } |
1362 |
} |
} |
1363 |
|
|
1364 |
backupMV = Data->currentMV[0]; |
backupMV = Data->currentMV[0]; |
1365 |
if (MotionFlags & PMV_HALFPELREFINE16 && !Data->rrv) startMV.x = startMV.y = 1; |
startMV.x = startMV.y = 1; |
|
else startMV.x = startMV.y = 0; |
|
1366 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1367 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1368 |
|
|
1369 |
(*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data); |
CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data); |
1370 |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
MainSearchPtr(startMV.x, startMV.y, Data, 255); |
1371 |
if (bSAD < Data->iMinSAD[0]) { |
if (bSAD < Data->iMinSAD[0]) { |
1372 |
Data->currentMV[0] = backupMV; |
Data->currentMV[0] = backupMV; |
1373 |
Data->iMinSAD[0] = bSAD; } |
Data->iMinSAD[0] = bSAD; } |
1375 |
} |
} |
1376 |
} |
} |
1377 |
|
|
1378 |
if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data); |
if (MotionFlags & PMV_HALFPELREFINE16) |
1379 |
|
if ((!(MotionFlags & HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant) |
1380 |
|
SubpelRefine(Data); |
1381 |
|
|
1382 |
for(i = 0; i < 5; i++) { |
for(i = 0; i < 5; i++) { |
1383 |
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors |
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors |
1384 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1385 |
} |
} |
1386 |
|
|
1387 |
if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE16) { |
if (MotionFlags & PMV_QUARTERPELREFINE16) |
1388 |
|
if ((!(MotionFlags & QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { |
1389 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1390 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1391 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1393 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1394 |
} |
} |
1395 |
|
|
1396 |
if (Data->iMinSAD[0] < (int32_t)iQuant * 30) inter4v = 0; |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
1397 |
if (inter4v) { |
|
1398 |
|
if (inter4v && (!(GlobalFlags & XVID_MODEDECISION_BITS) || |
1399 |
|
(!(MotionFlags & QUARTERPELREFINE8_BITS)) || (!(MotionFlags & HALFPELREFINE8_BITS)) || |
1400 |
|
((!(MotionFlags & EXTSEARCH_BITS)) && (!(MotionFlags&PMV_EXTSEARCH8)) ))) { |
1401 |
|
// if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop |
1402 |
|
|
1403 |
SearchData Data8; |
SearchData Data8; |
1404 |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
1405 |
|
|
1408 |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
1409 |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1410 |
|
|
1411 |
if (Data->chroma) { |
if ((Data->chroma) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) { |
1412 |
|
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it |
1413 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
1414 |
const int div = 1 + Data->qpel; |
const int div = 1 + Data->qpel; |
1415 |
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
1424 |
} |
} |
1425 |
} |
} |
1426 |
|
|
1427 |
|
inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, GlobalFlags); |
1428 |
|
|
1429 |
if (Data->rrv) { |
if (Data->rrv) { |
1430 |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
1431 |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1432 |
} |
} |
1433 |
|
|
1434 |
if (!(inter4v) || |
if (inter4v == MODE_INTER) { |
|
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
|
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
|
|
// INTER MODE |
|
1435 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1436 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1437 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
1445 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1446 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1447 |
} |
} |
1448 |
} else { |
|
1449 |
// INTER4V MODE; all other things are already set in Search8 |
} else if (inter4v == MODE_INTER4V) { |
1450 |
pMB->mode = MODE_INTER4V; |
pMB->mode = MODE_INTER4V; |
1451 |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + |
pMB->sad16 = Data->iMinSAD[0]; |
1452 |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant; |
} else { // INTRA mode |
1453 |
|
SkipMacroblockP(pMB, 0); // not skip, but similar enough |
1454 |
|
pMB->mode = MODE_INTRA; |
1455 |
} |
} |
1456 |
|
|
1457 |
} |
} |
1458 |
|
|
1459 |
static void |
static void |
1500 |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
1501 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1502 |
|
|
1503 |
if (MotionFlags & PMV_EXTSEARCH8) { |
if (MotionFlags & PMV_EXTSEARCH8 && (!(MotionFlags & EXTSEARCH_BITS))) { |
1504 |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
1505 |
|
|
1506 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1508 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
1509 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1510 |
|
|
1511 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255); |
1512 |
|
|
1513 |
if(*(Data->iMinSAD) < temp_sad) { |
if(*(Data->iMinSAD) < temp_sad) { |
1514 |
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
1655 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
1656 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1657 |
|
|
1658 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1659 |
|
|
1660 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1661 |
|
|
1809 |
|
|
1810 |
CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; |
CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; |
1811 |
|
|
1812 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
CheckCandidate(0, 0, 255, &k, Data); |
1813 |
|
|
1814 |
// initial (fast) skip decision |
// initial (fast) skip decision |
1815 |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) { |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) { |
1832 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
1833 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1834 |
|
|
1835 |
(*MainSearchPtr)(0, 0, Data, 255); |
MainSearchPtr(0, 0, Data, 255); |
1836 |
|
|
1837 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1838 |
|
|
2295 |
double meanx,meany; |
double meanx,meany; |
2296 |
int num,oldnum; |
int num,oldnum; |
2297 |
|
|
2298 |
if (!MBmask) { fprintf(stderr,"Mem error\n"); return gmc;} |
if (!MBmask) { fprintf(stderr,"Mem error\n"); |
2299 |
|
gmc.duv[0].x= gmc.duv[0].y = |
2300 |
|
gmc.duv[1].x= gmc.duv[1].y = |
2301 |
|
gmc.duv[2].x= gmc.duv[2].y = 0; |
2302 |
|
return gmc; } |
2303 |
|
|
2304 |
// filter mask of all blocks |
// filter mask of all blocks |
2305 |
|
|
2448 |
|
|
2449 |
return gmc; |
return gmc; |
2450 |
} |
} |
2451 |
|
|
2452 |
|
// functions which perform BITS-based search/bitcount |
2453 |
|
|
2454 |
|
static int |
2455 |
|
CountMBBitsInter(SearchData * const Data, |
2456 |
|
const MACROBLOCK * const pMBs, const int x, const int y, |
2457 |
|
const MBParam * const pParam, |
2458 |
|
const uint32_t MotionFlags) |
2459 |
|
{ |
2460 |
|
int i, iDirection; |
2461 |
|
int32_t bsad[5]; |
2462 |
|
|
2463 |
|
CheckCandidate = CheckCandidateBits16; |
2464 |
|
|
2465 |
|
if (Data->qpel) { |
2466 |
|
for(i = 0; i < 5; i++) { |
2467 |
|
Data->currentMV[i].x = Data->currentQMV[i].x/2; |
2468 |
|
Data->currentMV[i].y = Data->currentQMV[i].y/2; |
2469 |
|
} |
2470 |
|
Data->qpel_precision = 1; |
2471 |
|
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
2472 |
|
|
2473 |
|
//checking if this vector is perfect. if it is, we stop. |
2474 |
|
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) |
2475 |
|
return 0; //quick stop |
2476 |
|
|
2477 |
|
if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
2478 |
|
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
2479 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
2480 |
|
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
2481 |
|
Data->qpel_precision = 0; |
2482 |
|
if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) |
2483 |
|
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
2484 |
|
} |
2485 |
|
|
2486 |
|
} else { // not qpel |
2487 |
|
|
2488 |
|
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
2489 |
|
//checking if this vector is perfect. if it is, we stop. |
2490 |
|
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) { |
2491 |
|
return 0; //inter |
2492 |
|
} |
2493 |
|
} |
2494 |
|
|
2495 |
|
if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
2496 |
|
|
2497 |
|
if (MotionFlags&HALFPELREFINE16_BITS) SubpelRefine(Data); |
2498 |
|
|
2499 |
|
if (Data->qpel) { |
2500 |
|
if (MotionFlags&(EXTSEARCH_BITS | HALFPELREFINE16_BITS)) { // there was halfpel-precision search |
2501 |
|
for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { |
2502 |
|
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match |
2503 |
|
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
2504 |
|
} |
2505 |
|
|
2506 |
|
// preparing for qpel-precision search |
2507 |
|
Data->qpel_precision = 1; |
2508 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
2509 |
|
pParam->width, pParam->height, Data->iFcode, 1, 0); |
2510 |
|
} |
2511 |
|
if (MotionFlags&QUARTERPELREFINE16_BITS) SubpelRefine(Data); |
2512 |
|
} |
2513 |
|
|
2514 |
|
if (MotionFlags&CHECKPREDICTION_BITS) { //let's check vector equal to prediction |
2515 |
|
VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; |
2516 |
|
if (!(Data->predMV.x == v->x && Data->predMV.y == v->y)) |
2517 |
|
CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); |
2518 |
|
} |
2519 |
|
return Data->iMinSAD[0]; |
2520 |
|
} |
2521 |
|
|
2522 |
|
|
2523 |
|
static int |
2524 |
|
CountMBBitsInter4v(const SearchData * const Data, |
2525 |
|
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
2526 |
|
const int x, const int y, |
2527 |
|
const MBParam * const pParam, const uint32_t MotionFlags, |
2528 |
|
const VECTOR * const backup) |
2529 |
|
{ |
2530 |
|
|
2531 |
|
int cbp = 0, bits = 0, t = 0, i, iDirection; |
2532 |
|
SearchData Data2, *Data8 = &Data2; |
2533 |
|
int sumx = 0, sumy = 0; |
2534 |
|
int16_t in[64], coeff[64]; |
2535 |
|
|
2536 |
|
memcpy(Data8, Data, sizeof(SearchData)); |
2537 |
|
CheckCandidate = CheckCandidateBits8; |
2538 |
|
|
2539 |
|
for (i = 0; i < 4; i++) { |
2540 |
|
Data8->iMinSAD = Data->iMinSAD + i + 1; |
2541 |
|
Data8->currentMV = Data->currentMV + i + 1; |
2542 |
|
Data8->currentQMV = Data->currentQMV + i + 1; |
2543 |
|
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2544 |
|
Data8->Ref = Data->Ref + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2545 |
|
Data8->RefH = Data->RefH + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2546 |
|
Data8->RefV = Data->RefV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2547 |
|
Data8->RefHV = Data->RefHV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2548 |
|
|
2549 |
|
if(Data->qpel) { |
2550 |
|
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
2551 |
|
if (i != 0) t = d_mv_bits( Data8->currentQMV->x, Data8->currentQMV->y, |
2552 |
|
Data8->predMV, Data8->iFcode, 0, 0); |
2553 |
|
} else { |
2554 |
|
Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i); |
2555 |
|
if (i != 0) t = d_mv_bits( Data8->currentMV->x, Data8->currentMV->y, |
2556 |
|
Data8->predMV, Data8->iFcode, 0, 0); |
2557 |
|
} |
2558 |
|
|
2559 |
|
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
2560 |
|
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
2561 |
|
|
2562 |
|
*Data8->iMinSAD += t; |
2563 |
|
|
2564 |
|
Data8->qpel_precision = Data8->qpel; |
2565 |
|
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
2566 |
|
if (Data8->qpel) { |
2567 |
|
if (!(Data8->currentQMV->x == backup[i+1].x && Data8->currentQMV->y == backup[i+1].y)) |
2568 |
|
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2569 |
|
} else { |
2570 |
|
if (!(Data8->currentMV->x == backup[i+1].x && Data8->currentMV->y == backup[i+1].y)) |
2571 |
|
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2572 |
|
} |
2573 |
|
|
2574 |
|
if (Data8->qpel) { |
2575 |
|
if (MotionFlags&HALFPELREFINE8_BITS || (MotionFlags&PMV_EXTSEARCH8 && MotionFlags&EXTSEARCH_BITS)) { // halfpixel motion search follows |
2576 |
|
int32_t s = *Data8->iMinSAD; |
2577 |
|
Data8->currentMV->x = Data8->currentQMV->x/2; |
2578 |
|
Data8->currentMV->y = Data8->currentQMV->y/2; |
2579 |
|
Data8->qpel_precision = 0; |
2580 |
|
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
2581 |
|
pParam->width, pParam->height, Data8->iFcode - 1, 0, 0); |
2582 |
|
|
2583 |
|
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
2584 |
|
CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); |
2585 |
|
|
2586 |
|
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) |
2587 |
|
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2588 |
|
|
2589 |
|
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); |
2590 |
|
|
2591 |
|
if(s > *Data8->iMinSAD) { //we have found a better match |
2592 |
|
Data8->currentQMV->x = 2*Data8->currentMV->x; |
2593 |
|
Data8->currentQMV->y = 2*Data8->currentMV->y; |
2594 |
|
} |
2595 |
|
|
2596 |
|
Data8->qpel_precision = 1; |
2597 |
|
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
2598 |
|
pParam->width, pParam->height, Data8->iFcode, 1, 0); |
2599 |
|
|
2600 |
|
} |
2601 |
|
if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
2602 |
|
|
2603 |
|
} else // not qpel |
2604 |
|
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement |
2605 |
|
|
2606 |
|
//checking vector equal to predicion |
2607 |
|
if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { |
2608 |
|
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
2609 |
|
if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) |
2610 |
|
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
2611 |
|
} |
2612 |
|
|
2613 |
|
bits += *Data8->iMinSAD; |
2614 |
|
if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V |
2615 |
|
|
2616 |
|
// MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else |
2617 |
|
if(Data->qpel) { |
2618 |
|
pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x; |
2619 |
|
pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y; |
2620 |
|
pMB->qmvs[i] = *Data8->currentQMV; |
2621 |
|
sumx += Data8->currentQMV->x/2; |
2622 |
|
sumy += Data8->currentQMV->y/2; |
2623 |
|
} else { |
2624 |
|
pMB->pmvs[i].x = Data8->currentMV->x - Data8->predMV.x; |
2625 |
|
pMB->pmvs[i].y = Data8->currentMV->y - Data8->predMV.y; |
2626 |
|
sumx += Data8->currentMV->x; |
2627 |
|
sumy += Data8->currentMV->y; |
2628 |
|
} |
2629 |
|
pMB->mvs[i] = *Data8->currentMV; |
2630 |
|
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
2631 |
|
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
2632 |
|
} |
2633 |
|
|
2634 |
|
if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma |
2635 |
|
const uint8_t * ptr; |
2636 |
|
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
2637 |
|
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
2638 |
|
|
2639 |
|
//chroma U |
2640 |
|
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCU, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2641 |
|
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2642 |
|
fdct(in); |
2643 |
|
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
2644 |
|
else i = quant4_inter(coeff, in, Data->lambda16); |
2645 |
|
if (i > 0) { |
2646 |
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
2647 |
|
cbp |= 1 << (5 - 4); |
2648 |
|
} |
2649 |
|
|
2650 |
|
if (bits < *Data->iMinSAD) { // still possible |
2651 |
|
//chroma V |
2652 |
|
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCV, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2653 |
|
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2654 |
|
fdct(in); |
2655 |
|
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
2656 |
|
else i = quant4_inter(coeff, in, Data->lambda16); |
2657 |
|
if (i > 0) { |
2658 |
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
2659 |
|
cbp |= 1 << (5 - 5); |
2660 |
|
} |
2661 |
|
bits += cbpy_tab[15-(cbp>>2)].len; |
2662 |
|
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
2663 |
|
} |
2664 |
|
} |
2665 |
|
|
2666 |
|
return bits; |
2667 |
|
} |
2668 |
|
|
2669 |
|
|
2670 |
|
static int |
2671 |
|
CountMBBitsIntra(const SearchData * const Data) |
2672 |
|
{ |
2673 |
|
int bits = 1; //this one is ac/dc prediction flag. always 1. |
2674 |
|
int cbp = 0, i, t, dc = 0, b_dc = 1024; |
2675 |
|
const uint32_t iQuant = Data->lambda16; |
2676 |
|
int16_t in[64], coeff[64]; |
2677 |
|
|
2678 |
|
for(i = 0; i < 4; i++) { |
2679 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, 1); |
2680 |
|
|
2681 |
|
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2682 |
|
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
2683 |
|
fdct(in); |
2684 |
|
b_dc = dc; |
2685 |
|
dc = in[0]; |
2686 |
|
in[0] -= b_dc; |
2687 |
|
if (Data->lambda8 == 0) quant_intra_c(coeff, in, iQuant, iDcScaler); |
2688 |
|
else quant4_intra_c(coeff, in, iQuant, iDcScaler); |
2689 |
|
|
2690 |
|
b_dc = dc; |
2691 |
|
dc = coeff[0]; |
2692 |
|
if (i != 0) coeff[0] -= b_dc; |
2693 |
|
|
2694 |
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; |
2695 |
|
Data->temp[i] = t; |
2696 |
|
if (t != 0) cbp |= 1 << (5 - i); |
2697 |
|
if (bits >= Data->iMinSAD[0]) break; |
2698 |
|
} |
2699 |
|
|
2700 |
|
if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma |
2701 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, 0); |
2702 |
|
//chroma U |
2703 |
|
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2704 |
|
fdct(in); |
2705 |
|
in[0] -= 1024; |
2706 |
|
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
2707 |
|
else quant4_intra(coeff, in, iQuant, iDcScaler); |
2708 |
|
|
2709 |
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2710 |
|
if (t != 0) cbp |= 1 << (5 - 4); |
2711 |
|
Data->temp[4] = t; |
2712 |
|
|
2713 |
|
if (bits < Data->iMinSAD[0]) { |
2714 |
|
//chroma V |
2715 |
|
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2716 |
|
fdct(in); |
2717 |
|
in[0] -= 1024; |
2718 |
|
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
2719 |
|
else quant4_intra(coeff, in, iQuant, iDcScaler); |
2720 |
|
|
2721 |
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2722 |
|
if (t != 0) cbp |= 1 << (5 - 5); |
2723 |
|
|
2724 |
|
Data->temp[5] = t; |
2725 |
|
|
2726 |
|
bits += t = cbpy_tab[cbp>>2].len; |
2727 |
|
Data->temp[6] = t; |
2728 |
|
|
2729 |
|
bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
2730 |
|
Data->temp[7] = t; |
2731 |
|
|
2732 |
|
} |
2733 |
|
} |
2734 |
|
|
2735 |
|
return bits; |
2736 |
|
} |