145 |
} |
} |
146 |
|
|
147 |
static void |
static void |
148 |
|
CheckCandidateInt_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
149 |
|
{ |
150 |
|
int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb; |
151 |
|
uint32_t t; |
152 |
|
|
153 |
|
const uint8_t *ReferenceF, *ReferenceB; |
154 |
|
VECTOR *current; |
155 |
|
|
156 |
|
if ((x > data->max_dx) || (x < data->min_dx) || |
157 |
|
(y > data->max_dy) || (y < data->min_dy)) |
158 |
|
return; |
159 |
|
|
160 |
|
if (Direction == 1) { /* x and y mean forward vector */ |
161 |
|
VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1]; |
162 |
|
xb = backward.x; |
163 |
|
yb = backward.y; |
164 |
|
xf = x; yf = y; |
165 |
|
} else { /* x and y mean backward vector */ |
166 |
|
VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0]; |
167 |
|
xf = forward.x; |
168 |
|
yf = forward.y; |
169 |
|
xb = x; yb = y; |
170 |
|
} |
171 |
|
|
172 |
|
ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data); |
173 |
|
current = data->currentQMV + Direction - 1; |
174 |
|
ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data); |
175 |
|
xcf = xf/2; ycf = yf/2; |
176 |
|
xcb = xb/2; ycb = yb/2; |
177 |
|
|
178 |
|
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
179 |
|
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
180 |
|
|
181 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
182 |
|
sad += (data->lambda16 * t * sad)>>10; |
183 |
|
|
184 |
|
if (data->chroma && sad < *data->iMinSAD) |
185 |
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
186 |
|
(ycf >> 1) + roundtab_79[ycf & 0x3], |
187 |
|
(xcb >> 1) + roundtab_79[xcb & 0x3], |
188 |
|
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
189 |
|
|
190 |
|
if (sad < *(data->iMinSAD)) { |
191 |
|
*data->iMinSAD = sad; |
192 |
|
current->x = x; current->y = y; |
193 |
|
data->dir = Direction; |
194 |
|
} |
195 |
|
|
196 |
|
if (sad < *(data->iMinSAD)) { |
197 |
|
data->iMinSAD2 = *(data->iMinSAD); |
198 |
|
data->currentQMV2.x = current->x; |
199 |
|
data->currentQMV2.y = current->y; |
200 |
|
|
201 |
|
*data->iMinSAD = sad; |
202 |
|
current->x = x; current->y = y; |
203 |
|
} else if (sad < data->iMinSAD2) { |
204 |
|
data->iMinSAD2 = sad; |
205 |
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
static void |
210 |
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
211 |
{ |
{ |
212 |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
460 |
|
|
461 |
int i; |
int i; |
462 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
463 |
|
int threshA = (MotionFlags & XVID_ME_FASTREFINE16) ? 150 : 300; |
464 |
*Data->iMinSAD = MV_MAX_ERROR; |
*Data->iMinSAD = MV_MAX_ERROR; |
465 |
Data->iFcode = iFcode; |
Data->iFcode = iFcode; |
466 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
503 |
|
|
504 |
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
505 |
|
|
506 |
if (Data->qpel && *Data->iMinSAD < *best_sad + 300) { |
if (Data->qpel && (*Data->iMinSAD < *best_sad + threshA)) { |
507 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
508 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
509 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
751 |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
752 |
} |
} |
753 |
|
|
754 |
|
/* Pretty much redundant code, just as SubpelRefine_dir above too |
755 |
|
* |
756 |
|
* TODO: Get rid off all the redundancy (SubpelRefine_Fast_dir, |
757 |
|
* CheckCandidate16no4v_qpel etc.) */ |
758 |
|
|
759 |
|
void |
760 |
|
SubpelRefine_Fast_dir(SearchData * data, CheckFunc * CheckCandidate, const int dir) |
761 |
|
{ |
762 |
|
/* Do a fast q-pel refinement */ |
763 |
|
VECTOR centerMV; |
764 |
|
VECTOR second_best; |
765 |
|
int best_sad = *data->iMinSAD; |
766 |
|
int xo, yo, xo2, yo2; |
767 |
|
int size = 2; |
768 |
|
data->iMinSAD2 = 0; |
769 |
|
|
770 |
|
/* check all halfpixel positions near our best halfpel position */ |
771 |
|
centerMV = data->currentQMV[dir-1]; |
772 |
|
*data->iMinSAD = 256 * 4096; |
773 |
|
|
774 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - size, dir); |
775 |
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, dir); |
776 |
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y, dir); |
777 |
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, dir); |
778 |
|
|
779 |
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + size, dir); |
780 |
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, dir); |
781 |
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y, dir); |
782 |
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, dir); |
783 |
|
|
784 |
|
second_best = data->currentQMV[dir-1]; |
785 |
|
|
786 |
|
/* after second_best has been found, go back to the vector we began with */ |
787 |
|
|
788 |
|
data->currentQMV[dir-1] = centerMV; |
789 |
|
*data->iMinSAD = best_sad; |
790 |
|
|
791 |
|
xo = centerMV.x; |
792 |
|
yo = centerMV.y; |
793 |
|
xo2 = second_best.x; |
794 |
|
yo2 = second_best.y; |
795 |
|
|
796 |
|
data->iMinSAD2 = 256 * 4096; |
797 |
|
|
798 |
|
if (yo == yo2) { |
799 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
800 |
|
CHECK_CANDIDATE(xo, yo-1, dir); |
801 |
|
CHECK_CANDIDATE(xo, yo+1, dir); |
802 |
|
|
803 |
|
if(best_sad <= data->iMinSAD2) return; |
804 |
|
|
805 |
|
if(data->currentQMV[dir-1].x == data->currentQMV2.x) { |
806 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo-1, dir); |
807 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo+1, dir); |
808 |
|
} else { |
809 |
|
CHECK_CANDIDATE((xo+xo2)>>1, |
810 |
|
(data->currentQMV[dir-1].x == xo) ? data->currentQMV[dir-1].y : data->currentQMV2.y, dir); |
811 |
|
} |
812 |
|
return; |
813 |
|
} |
814 |
|
|
815 |
|
if (xo == xo2) { |
816 |
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
817 |
|
CHECK_CANDIDATE(xo-1, yo, dir); |
818 |
|
CHECK_CANDIDATE(xo+1, yo, dir); |
819 |
|
|
820 |
|
if(best_sad < data->iMinSAD2) return; |
821 |
|
|
822 |
|
if(data->currentQMV[dir-1].y == data->currentQMV2.y) { |
823 |
|
CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, dir); |
824 |
|
CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, dir); |
825 |
|
} else { |
826 |
|
CHECK_CANDIDATE((data->currentQMV[dir-1].y == yo) ? data->currentQMV[dir-1].x : data->currentQMV2.x, (yo+yo2)>>1, dir); |
827 |
|
} |
828 |
|
return; |
829 |
|
} |
830 |
|
|
831 |
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
832 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
833 |
|
|
834 |
|
if(best_sad <= data->iMinSAD2) return; |
835 |
|
|
836 |
|
CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, dir); |
837 |
|
} |
838 |
|
|
839 |
static void |
static void |
840 |
SearchInterpolate(const IMAGE * const f_Ref, |
SearchInterpolate(const IMAGE * const f_Ref, |
841 |
const uint8_t * const f_RefH, |
const uint8_t * const f_RefH, |
859 |
{ |
{ |
860 |
int i, j; |
int i, j; |
861 |
int b_range[4], f_range[4]; |
int b_range[4], f_range[4]; |
862 |
int threshA = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 0 : 500; |
int threshA = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 250 : 500; |
863 |
int threshB = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 0 : 300; |
int threshB = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 150 : 300; |
864 |
|
|
865 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
866 |
*Data->iMinSAD = 4096*256; |
*Data->iMinSAD = 4096*256; |
937 |
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
938 |
Data->currentQMV[1].x = 2 * Data->currentMV[1].x; |
Data->currentQMV[1].x = 2 * Data->currentMV[1].x; |
939 |
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
940 |
|
|
941 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
942 |
|
if (MotionFlags & XVID_ME_FASTREFINE16) |
943 |
|
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 1); |
944 |
|
else |
945 |
SubpelRefine_dir(Data, CheckCandidateInt, 1); |
SubpelRefine_dir(Data, CheckCandidateInt, 1); |
946 |
|
} |
947 |
|
|
948 |
if (*Data->iMinSAD > *best_sad + threshB) return; |
if (*Data->iMinSAD > *best_sad + threshB) return; |
949 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
950 |
|
|
951 |
|
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
952 |
|
if (MotionFlags & XVID_ME_FASTREFINE16) |
953 |
|
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 2); |
954 |
|
else |
955 |
SubpelRefine_dir(Data, CheckCandidateInt, 2); |
SubpelRefine_dir(Data, CheckCandidateInt, 2); |
956 |
} |
} |
957 |
|
} |
958 |
|
|
959 |
*Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
*Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
960 |
|
|
1030 |
for (i = 0; i < pParam->mb_width; i++) { |
for (i = 0; i < pParam->mb_width; i++) { |
1031 |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
1032 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1033 |
int interpol_search; |
int interpol_search = 0; |
1034 |
|
int bf_search = 0; |
1035 |
|
int bf_thresh = 0; |
1036 |
|
|
1037 |
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
1038 |
if (b_reference->coding_type != S_VOP) |
if (b_reference->coding_type != S_VOP) |
1068 |
} |
} |
1069 |
|
|
1070 |
if (frame->motion_flags & XVID_ME_BFRAME_EARLYSTOP) { |
if (frame->motion_flags & XVID_ME_BFRAME_EARLYSTOP) { |
|
int bf_search = 0; |
|
|
int bf_thresh = 0; |
|
|
|
|
1071 |
if(i > 0 && j > 0 && i < pParam->mb_width) { |
if(i > 0 && j > 0 && i < pParam->mb_width) { |
1072 |
bf_thresh = ((&pMBs[(i-1) + j * pParam->mb_width])->sad16 + |
bf_thresh = MIN((&pMBs[(i-1) + j * pParam->mb_width])->sad16, |
1073 |
(&pMBs[i + (j-1) * pParam->mb_width])->sad16 + |
MIN((&pMBs[i + (j-1) * pParam->mb_width])->sad16, |
1074 |
(&pMBs[(i+1) + (j-1) * pParam->mb_width])->sad16) / 3; |
(&pMBs[(i+1) + (j-1) * pParam->mb_width])->sad16)); |
1075 |
|
|
1076 |
if (((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_FORWARD) && |
if (((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_FORWARD) && |
1077 |
((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_BACKWARD) && |
((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_BACKWARD) && |
1110 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
1111 |
|
|
1112 |
/* interpolate search comes last, because it uses data from forward and backward as prediction */ |
/* interpolate search comes last, because it uses data from forward and backward as prediction */ |
1113 |
if (frame->motion_flags & XVID_ME_FAST_MODEINTERPOLATE) |
if (frame->motion_flags & XVID_ME_FAST_MODEINTERPOLATE) { |
1114 |
interpol_search = (best_sad > Data.iQuant * 3 * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2)); |
|
1115 |
|
if(i > 0 && j > 0 && i < pParam->mb_width) { |
1116 |
|
if ((&pMBs[(i-1) + j * pParam->mb_width])->mode == MODE_INTERPOLATE) |
1117 |
|
interpol_search++; |
1118 |
|
if ((&pMBs[i + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE) |
1119 |
|
interpol_search++; |
1120 |
|
if ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE) |
1121 |
|
interpol_search++; |
1122 |
|
} |
1123 |
|
else |
1124 |
|
interpol_search = 1; |
1125 |
|
|
1126 |
|
interpol_search |= !(best_sad < 3 * Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2)); |
1127 |
|
} |
1128 |
else |
else |
1129 |
interpol_search = 1; |
interpol_search = 1; |
1130 |
|
|
1168 |
} |
} |
1169 |
} |
} |
1170 |
|
|
1171 |
|
|