55 |
#define GET_REFERENCE(X, Y, REF) { \ |
#define GET_REFERENCE(X, Y, REF) { \ |
56 |
switch ( (((X)&1)<<1) + ((Y)&1) ) \ |
switch ( (((X)&1)<<1) + ((Y)&1) ) \ |
57 |
{ \ |
{ \ |
58 |
case 0 : REF = data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
case 0 : REF = (uint8_t *)data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
59 |
case 1 : REF = data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
case 1 : REF = (uint8_t *)data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
60 |
case 2 : REF = data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
case 2 : REF = (uint8_t *)data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
61 |
default : REF = data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
62 |
} \ |
} \ |
63 |
} |
} |
64 |
|
// I hate those macros :/ |
65 |
|
#define GET_REFERENCE2(X, Y, REF) { \ |
66 |
|
switch ( (((X)&1)<<1) + ((Y)&1) ) \ |
67 |
|
{ \ |
68 |
|
case 0 : REF = (uint8_t *)data->bRef + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
69 |
|
case 1 : REF = (uint8_t *)data->bRefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
70 |
|
case 2 : REF = (uint8_t *)data->bRefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
71 |
|
default : REF = (uint8_t *)data->bRefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
72 |
|
} \ |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
#define iDiamondSize 2 |
#define iDiamondSize 2 |
77 |
|
|
100 |
return xb + yb; |
return xb + yb; |
101 |
} |
} |
102 |
|
|
103 |
|
static int32_t |
104 |
|
ChromaSAD(int dx, int dy, const SearchData * const data) |
105 |
|
{ |
106 |
|
int sad; |
107 |
|
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
108 |
|
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
109 |
|
|
110 |
|
switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0) |
111 |
|
case 0: |
112 |
|
sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
113 |
|
sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
114 |
|
break; |
115 |
|
case 1: |
116 |
|
dx = dx / 2; dy = (dy - 1) / 2; |
117 |
|
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
118 |
|
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
119 |
|
break; |
120 |
|
case 2: |
121 |
|
dx = (dx - 1) / 2; dy = dy / 2; |
122 |
|
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
123 |
|
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
124 |
|
break; |
125 |
|
default: |
126 |
|
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
127 |
|
interpolate8x8_halfpel_hv(data->RefQ, |
128 |
|
data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
129 |
|
data->rounding); |
130 |
|
sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2); |
131 |
|
interpolate8x8_halfpel_hv(data->RefQ, |
132 |
|
data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
133 |
|
data->rounding); |
134 |
|
sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2); |
135 |
|
break; |
136 |
|
} |
137 |
|
return sad; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
/* CHECK_CANDIATE FUNCTIONS START */ |
/* CHECK_CANDIATE FUNCTIONS START */ |
142 |
|
|
143 |
|
|
144 |
static void |
static void |
145 |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
146 |
{ |
{ |
159 |
|
|
160 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
161 |
|
|
162 |
if(data->quarterpel) |
if (data->qpel) t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
163 |
t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
164 |
else |
|
165 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
166 |
|
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
167 |
|
|
168 |
data->temp[0] += lambda_vec16[data->iQuant] * t; |
if (data->chroma) data->temp[0] += ChromaSAD(x, y, data); |
|
data->temp[1] += lambda_vec8[data->iQuant] * t; |
|
169 |
|
|
170 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
171 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
200 |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
201 |
} |
} |
202 |
|
|
203 |
if(data->quarterpel) |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
204 |
sad = lambda_vec16[data->iQuant] * |
if (data->qpel) //only to be used in b-frames' ME |
205 |
d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
sad += (data->lambda16 * d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode) * sad)/1000; |
206 |
else |
else |
207 |
sad = lambda_vec16[data->iQuant] * |
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
|
d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
|
|
|
|
sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
|
208 |
|
|
209 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
210 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
230 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
231 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
232 |
|
|
233 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases |
234 |
switch( ((x&1)<<1) + (y&1) ) |
switch( ((x&1)<<1) + (y&1) ) |
235 |
{ |
{ |
236 |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
237 |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference); |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference); |
238 |
break; |
break; |
239 |
|
|
240 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
241 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
|
|
|
242 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
243 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
244 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
246 |
break; |
break; |
247 |
|
|
248 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
249 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
|
|
|
250 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
251 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
252 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
255 |
|
|
256 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
257 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
258 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
259 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
260 |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
269 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1); |
270 |
|
|
271 |
t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
272 |
data->temp[0] += lambda_vec16[data->iQuant] * t; |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
273 |
data->temp[1] += lambda_vec8[data->iQuant] * t; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
274 |
|
|
275 |
|
if (data->chroma) |
276 |
|
data->temp[0] += ChromaSAD(x/2, y/2, data); |
277 |
|
|
278 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
279 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
296 |
// CheckCandidate16no4v variant which expects x and y in quarter pixel resolution |
// CheckCandidate16no4v variant which expects x and y in quarter pixel resolution |
297 |
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
298 |
// around currentMV! |
// around currentMV! |
299 |
|
// this function is for B-frames' search only |
300 |
{ |
{ |
|
int32_t sad; |
|
301 |
uint8_t * Reference = (uint8_t *) data->RefQ; |
uint8_t * Reference = (uint8_t *) data->RefQ; |
302 |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
303 |
VECTOR halfpelMV = *(data->currentMV); |
VECTOR halfpelMV = *(data->currentMV); |
304 |
|
|
305 |
int32_t iEdgedWidth = data->iEdgedWidth; |
int32_t iEdgedWidth = data->iEdgedWidth; |
306 |
uint32_t rounding = data->rounding; |
int32_t sad; |
307 |
|
|
308 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
309 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
310 |
|
|
311 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases |
312 |
switch( ((x&1)<<1) + (y&1) ) |
switch( ((x&1)<<1) + (y&1) ) |
313 |
{ |
{ |
314 |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
315 |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference); |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference); |
316 |
break; |
break; |
317 |
|
|
318 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
319 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
320 |
|
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0); |
321 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0); |
322 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
323 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
|
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding); |
|
324 |
break; |
break; |
325 |
|
|
326 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
327 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
328 |
|
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0); |
329 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0); |
330 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
331 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
|
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding); |
|
332 |
break; |
break; |
333 |
|
|
334 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
335 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
336 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
337 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
338 |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
339 |
|
|
340 |
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
341 |
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
342 |
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
343 |
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
344 |
break; |
break; |
345 |
} |
} |
346 |
|
|
347 |
sad = lambda_vec16[data->iQuant] * |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
348 |
d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
|
sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
|
349 |
|
|
350 |
if (sad < *(data->iMinSAD)) { |
if (sad < data->iMinSAD[0]) { |
351 |
*(data->iMinSAD) = sad; |
data->iMinSAD[0] = sad; |
352 |
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
353 |
// *dir = Direction; |
/* *dir = Direction;*/ } |
|
} |
|
354 |
} |
} |
355 |
|
|
356 |
static void |
static void |
357 |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
358 |
{ |
{ |
359 |
|
// maximum speed - for P/B/I decision |
360 |
int32_t sad; |
int32_t sad; |
361 |
|
|
362 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
363 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
364 |
|
|
365 |
sad = lambda_vec16[data->iQuant] * |
sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
|
d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
|
|
|
|
sad += sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
|
366 |
data->iEdgedWidth, 256*4096); |
data->iEdgedWidth, 256*4096); |
367 |
|
|
368 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
397 |
default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break; |
default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break; |
398 |
} |
} |
399 |
|
|
400 |
sad = lambda_vec16[data->iQuant] * |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
|
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode) ); |
|
401 |
|
|
402 |
sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
if (data->qpel) |
403 |
|
sad += (data->lambda16 * |
404 |
|
( d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode) + |
405 |
|
d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
406 |
|
else |
407 |
|
sad += (data->lambda16 * |
408 |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
409 |
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
410 |
|
|
411 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
412 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
414 |
*dir = Direction; } |
*dir = Direction; } |
415 |
} |
} |
416 |
|
|
417 |
|
|
418 |
static void |
static void |
419 |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateInt_qpel(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data) |
420 |
{ |
{ |
421 |
|
// CheckCandidateInt variant which expects x and y in quarter pixel resolution |
422 |
|
|
423 |
int32_t sad; |
int32_t sad; |
424 |
|
const int xb = data->currentQMV[1].x; |
425 |
|
const int yb = data->currentQMV[1].y; |
426 |
|
uint8_t * ReferenceF = (uint8_t *)data->RefQ; |
427 |
|
uint8_t * ReferenceB = (uint8_t *)data->RefQ + 16; |
428 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
429 |
|
VECTOR halfpelMV; |
430 |
|
const int32_t iEdgedWidth = data->iEdgedWidth; |
431 |
|
|
432 |
|
if (( xf > data->max_dx) || ( xf < data->min_dx) |
433 |
|
|| ( yf > data->max_dy) || (yf < data->min_dy)) return; |
434 |
|
|
435 |
|
halfpelMV.x = xf/2; //forward first |
436 |
|
halfpelMV.y = yf/2; |
437 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
438 |
|
switch( ((xf&1)<<1) + (yf&1) ) |
439 |
|
{ |
440 |
|
case 0: // pure halfpel position - shouldn't happen during a refinement step |
441 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF); |
442 |
|
break; |
443 |
|
|
444 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
445 |
|
GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2); |
446 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
447 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
448 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
449 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
450 |
|
break; |
451 |
|
|
452 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
453 |
|
GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref2); |
454 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
455 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
456 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
457 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
458 |
|
break; |
459 |
|
|
460 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
461 |
|
// bottom left/right) during qpel refinement |
462 |
|
GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2); |
463 |
|
GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref3); |
464 |
|
GET_REFERENCE(xf - halfpelMV.x, yf - halfpelMV.y, ref4); |
465 |
|
|
466 |
|
interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
467 |
|
interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
468 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
469 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
470 |
|
break; |
471 |
|
} |
472 |
|
|
473 |
|
halfpelMV.x = xb/2; //backward |
474 |
|
halfpelMV.y = yb/2; |
475 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
476 |
|
switch( ((xb&1)<<1) + (yb&1) ) |
477 |
|
{ |
478 |
|
case 0: // pure halfpel position - shouldn't happen during a refinement step |
479 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB); |
480 |
|
break; |
481 |
|
|
482 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
483 |
|
GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2); |
484 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
485 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
486 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
487 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
488 |
|
break; |
489 |
|
|
490 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
491 |
|
GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref2); |
492 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
493 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
494 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
495 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
496 |
|
break; |
497 |
|
|
498 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
499 |
|
// bottom left/right) during qpel refinement |
500 |
|
GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2); |
501 |
|
GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref3); |
502 |
|
GET_REFERENCE2(xb - halfpelMV.x, yb - halfpelMV.y, ref4); |
503 |
|
|
504 |
|
interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
505 |
|
interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
506 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
507 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
508 |
|
break; |
509 |
|
} |
510 |
|
|
511 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
512 |
|
|
513 |
|
sad += (data->lambda16 * |
514 |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
515 |
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
516 |
|
|
517 |
|
if (sad < *(data->iMinSAD)) { |
518 |
|
*(data->iMinSAD) = sad; |
519 |
|
data->currentQMV->x = xf; data->currentQMV->y = yf; |
520 |
|
*dir = Direction; } |
521 |
|
} |
522 |
|
|
523 |
|
static void |
524 |
|
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
525 |
|
{ |
526 |
|
int32_t sad = 0; |
527 |
int k; |
int k; |
528 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
529 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
531 |
|
|
532 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
533 |
|
|
|
sad = lambda_vec16[data->iQuant] * d_mv_bits(x, y, 1); |
|
|
|
|
534 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
535 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
536 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
568 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
569 |
} |
} |
570 |
|
|
571 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
572 |
|
|
573 |
|
if (sad < *(data->iMinSAD)) { |
574 |
|
*(data->iMinSAD) = sad; |
575 |
|
data->currentMV->x = x; data->currentMV->y = y; |
576 |
|
*dir = Direction; } |
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
|
static void |
581 |
|
CheckCandidateDirect_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
582 |
|
{ |
583 |
|
int32_t sad = 0; |
584 |
|
int k; |
585 |
|
VECTOR mvs, b_mvs, halfpelMV; |
586 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
587 |
|
uint8_t *ReferenceF, *ReferenceB; |
588 |
|
const uint32_t iEdgedWidth = data->iEdgedWidth; |
589 |
|
|
590 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
591 |
|
|
592 |
|
for (k = 0; k < 4; k++) { |
593 |
|
ReferenceF = (uint8_t *)data->RefQ; |
594 |
|
ReferenceB = (uint8_t *)data->RefQ + 64; |
595 |
|
|
596 |
|
mvs.x = data->directmvF[k].x + x; |
597 |
|
b_mvs.x = ((x == 0) ? |
598 |
|
data->directmvB[k].x |
599 |
|
: mvs.x - data->referencemv[k].x); |
600 |
|
|
601 |
|
mvs.y = data->directmvF[k].y + y; |
602 |
|
b_mvs.y = ((y == 0) ? |
603 |
|
data->directmvB[k].y |
604 |
|
: mvs.y - data->referencemv[k].y); |
605 |
|
|
606 |
|
if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx ) |
607 |
|
|| ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy ) |
608 |
|
|| ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx ) |
609 |
|
|| ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return; |
610 |
|
|
611 |
|
halfpelMV.x = mvs.x/2; //forward first |
612 |
|
halfpelMV.y = mvs.y/2; |
613 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
614 |
|
switch( ((mvs.x&1)<<1) + (mvs.y&1) ) { |
615 |
|
case 0: // pure halfpel position |
616 |
|
GET_REFERENCE(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceF); |
617 |
|
break; |
618 |
|
|
619 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
620 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
621 |
|
interpolate8x8_avg2(ReferenceF, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
622 |
|
ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
623 |
|
break; |
624 |
|
|
625 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
626 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
627 |
|
interpolate8x8_avg2(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
628 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
629 |
|
break; |
630 |
|
|
631 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
632 |
|
// bottom left/right) during qpel refinement |
633 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
634 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
635 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4); |
636 |
|
interpolate8x8_avg4(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
637 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
638 |
|
ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
639 |
|
ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
640 |
|
break; |
641 |
|
} |
642 |
|
|
643 |
|
halfpelMV.x = b_mvs.x/2; |
644 |
|
halfpelMV.y = b_mvs.y/2; |
645 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in most cases |
646 |
|
switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) { |
647 |
|
case 0: // pure halfpel position |
648 |
|
GET_REFERENCE2(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceB); |
649 |
|
break; |
650 |
|
|
651 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
652 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
653 |
|
interpolate8x8_avg2(ReferenceB, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
654 |
|
ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
655 |
|
break; |
656 |
|
|
657 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
658 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
659 |
|
interpolate8x8_avg2(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
660 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
661 |
|
break; |
662 |
|
|
663 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
664 |
|
// bottom left/right) during qpel refinement |
665 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
666 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
667 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4); |
668 |
|
interpolate8x8_avg4(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
669 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
670 |
|
ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
671 |
|
ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
672 |
|
break; |
673 |
|
} |
674 |
|
|
675 |
|
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
676 |
|
ReferenceF, |
677 |
|
ReferenceB, |
678 |
|
data->iEdgedWidth); |
679 |
|
if (sad > *(data->iMinSAD)) return; |
680 |
|
} |
681 |
|
|
682 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
683 |
|
|
684 |
|
if (sad < *(data->iMinSAD)) { |
685 |
|
*(data->iMinSAD) = sad; |
686 |
|
data->currentMV->x = x; data->currentMV->y = y; |
687 |
|
*dir = Direction; } |
688 |
|
} |
689 |
|
|
690 |
|
static void |
691 |
|
CheckCandidateDirectno4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
692 |
|
{ |
693 |
|
int32_t sad = 0; |
694 |
|
VECTOR mvs, b_mvs, halfpelMV; |
695 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
696 |
|
const uint32_t iEdgedWidth = data->iEdgedWidth; |
697 |
|
uint8_t * ReferenceF = (uint8_t *)data->RefQ; |
698 |
|
uint8_t * ReferenceB = (uint8_t *)data->RefQ + 64; |
699 |
|
|
700 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
701 |
|
|
702 |
|
mvs.x = data->directmvF[0].x + x; |
703 |
|
b_mvs.x = ((x == 0) ? |
704 |
|
data->directmvB[0].x |
705 |
|
: mvs.x - data->referencemv[0].x); |
706 |
|
|
707 |
|
mvs.y = data->directmvF[0].y + y; |
708 |
|
b_mvs.y = ((y == 0) ? |
709 |
|
data->directmvB[0].y |
710 |
|
: mvs.y - data->referencemv[0].y); |
711 |
|
|
712 |
|
if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx ) |
713 |
|
|| ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy ) |
714 |
|
|| ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx ) |
715 |
|
|| ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return; |
716 |
|
|
717 |
|
halfpelMV.x = mvs.x/2; //forward first |
718 |
|
halfpelMV.y = mvs.y/2; |
719 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
720 |
|
switch( ((mvs.x&1)<<1) + (mvs.y&1) ) { |
721 |
|
case 0: // pure halfpel position |
722 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF); |
723 |
|
break; |
724 |
|
|
725 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
726 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
727 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
728 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
729 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
730 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
731 |
|
break; |
732 |
|
|
733 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
734 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
735 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
736 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
737 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
738 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
739 |
|
break; |
740 |
|
|
741 |
|
default: // x and y in qpel resolution |
742 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
743 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
744 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4); |
745 |
|
|
746 |
|
interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
747 |
|
interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
748 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
749 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
750 |
|
break; |
751 |
|
} |
752 |
|
|
753 |
|
halfpelMV.x = b_mvs.x/2; //backward |
754 |
|
halfpelMV.y = b_mvs.y/2; |
755 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); |
756 |
|
switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) |
757 |
|
{ |
758 |
|
case 0: // pure halfpel position |
759 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB); |
760 |
|
break; |
761 |
|
|
762 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
763 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
764 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
765 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
766 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
767 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
768 |
|
break; |
769 |
|
|
770 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
771 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
772 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
773 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
774 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
775 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
776 |
|
break; |
777 |
|
|
778 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
779 |
|
// bottom left/right) during qpel refinement |
780 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
781 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
782 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4); |
783 |
|
|
784 |
|
interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
785 |
|
interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
786 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
787 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
788 |
|
break; |
789 |
|
} |
790 |
|
|
791 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
792 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
793 |
|
|
794 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
795 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
796 |
data->currentMV->x = x; data->currentMV->y = y; |
data->currentMV->x = x; data->currentMV->y = y; |
797 |
*dir = Direction; } |
*dir = Direction; } |
798 |
} |
} |
799 |
|
|
800 |
|
|
801 |
static void |
static void |
802 |
CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
803 |
{ |
{ |
808 |
|
|
809 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
810 |
|
|
|
sad = lambda_vec16[data->iQuant] * d_mv_bits(x, y, 1); |
|
|
|
|
811 |
mvs.x = data->directmvF[0].x + x; |
mvs.x = data->directmvF[0].x + x; |
812 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
813 |
data->directmvB[0].x |
data->directmvB[0].x |
837 |
default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break; |
default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break; |
838 |
} |
} |
839 |
|
|
840 |
sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
841 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
842 |
|
|
843 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
844 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
849 |
static void |
static void |
850 |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
851 |
{ |
{ |
852 |
int32_t sad; |
int32_t sad; int t; |
853 |
const uint8_t * Reference; |
const uint8_t * Reference; |
854 |
|
|
855 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
864 |
} |
} |
865 |
|
|
866 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
867 |
|
if (data->qpel) t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode); |
868 |
|
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
869 |
|
|
870 |
if(data->quarterpel) |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
|
sad += lambda_vec8[data->iQuant] * d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
|
|
else |
|
|
sad += lambda_vec8[data->iQuant] * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
871 |
|
|
872 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
873 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
877 |
|
|
878 |
static void |
static void |
879 |
CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
880 |
// CheckCandidate16no4v variant which expects x and y in quarter pixel resolution |
// CheckCandidate8 variant which expects x and y in quarter pixel resolution |
881 |
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
882 |
// around currentMV! |
// around currentMV! |
883 |
|
|
893 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
894 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
895 |
|
|
896 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
897 |
switch( ((x&1)<<1) + (y&1) ) |
switch( ((x&1)<<1) + (y&1) ) |
898 |
{ |
{ |
899 |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
900 |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference); |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference); |
901 |
break; |
break; |
902 |
|
|
903 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
904 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
905 |
|
|
906 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
907 |
break; |
break; |
908 |
|
|
909 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
910 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
911 |
|
|
912 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
914 |
|
|
915 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
916 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
917 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
918 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
919 |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
923 |
} |
} |
924 |
|
|
925 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
926 |
sad += lambda_vec8[data->iQuant] * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
sad += (data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode) * (sad+NEIGH_8X8_BIAS))/100; |
927 |
|
|
928 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
929 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
1171 |
|
|
1172 |
uint32_t x, y; |
uint32_t x, y; |
1173 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
1174 |
int32_t InterBias, quant = current->quant; |
int32_t InterBias, quant = current->quant, sad00; |
1175 |
uint8_t *qimage; |
uint8_t *qimage; |
1176 |
|
|
1177 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
1187 |
Data.temp = temp; |
Data.temp = temp; |
1188 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
1189 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
1190 |
Data.quarterpel = pParam->m_quarterpel; |
Data.qpel = pParam->m_quarterpel; |
1191 |
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
1192 |
|
|
1193 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
1194 |
return 1; // allocate some mem for qpel interpolated blocks |
return 1; // allocate some mem for qpel interpolated blocks |
1195 |
// somehow this is dirty since I think we shouldn't use malloc outside |
// somehow this is dirty since I think we shouldn't use malloc outside |
1196 |
// encoder_create() - so please fix me! |
// encoder_create() - so please fix me! |
1197 |
|
Data.RefQ = qimage; |
1198 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
1199 |
|
|
1200 |
for (y = 0; y < pParam->mb_height; y++) { |
for (y = 0; y < pParam->mb_height; y++) { |
1201 |
for (x = 0; x < pParam->mb_width; x++) { |
for (x = 0; x < pParam->mb_width; x++) { |
|
|
|
1202 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
1203 |
int32_t sad00 = pMB->sad16 |
|
1204 |
|
pMB->sad16 |
1205 |
= sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
= sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
1206 |
pRef->y + (x + y * pParam->edged_width) * 16, |
pRef->y + (x + y * pParam->edged_width) * 16, |
1207 |
pParam->edged_width, pMB->sad8 ); |
pParam->edged_width, pMB->sad8 ); |
1208 |
|
|
1209 |
|
if (Data.chroma) { |
1210 |
|
pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8, |
1211 |
|
pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2); |
1212 |
|
|
1213 |
|
pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8, |
1214 |
|
pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2); |
1215 |
|
} |
1216 |
|
|
1217 |
|
sad00 = pMB->sad16; //if no gmc; else sad00 = (..) |
1218 |
|
|
1219 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
1220 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
1221 |
pMB->quant = current->quant; } |
pMB->quant = current->quant; |
1222 |
else |
} else { |
1223 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
1224 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
1225 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
1226 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
1227 |
|
} |
1228 |
pMB->quant = quant; |
pMB->quant = quant; |
1229 |
} |
} |
1230 |
|
|
1231 |
//initial skip decision |
//initial skip decision |
1232 |
|
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1233 |
if ((pMB->dquant == NO_CHANGE) && (sad00 <= MAX_SAD00_FOR_SKIP * pMB->quant) |
if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
1234 |
&& (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) ) { |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH) |
1235 |
if (pMB->sad16 < pMB->quant * INITIAL_SKIP_THRESH) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
1236 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1237 |
continue; |
continue; |
|
sad00 = 256 * 4096; |
|
1238 |
} |
} |
1239 |
} else sad00 = 256*4096; // skip not allowed - for final skip decision |
} |
1240 |
|
|
1241 |
SearchP(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1242 |
y, current->motion_flags, pMB->quant, |
y, current->motion_flags, pMB->quant, |
1243 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, |
1244 |
current->global_flags & XVID_INTER4V, pMB); |
current->global_flags & XVID_INTER4V, pMB); |
1245 |
|
|
1246 |
/* 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?" */ |
1247 |
if (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
if (current->coding_type == P_VOP) { |
1248 |
if ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
1249 |
{ SkipMacroblockP(pMB, sad00); continue; } |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) ) |
1250 |
|
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
1251 |
|
SkipMacroblockP(pMB, sad00); |
1252 |
|
continue; |
1253 |
|
} |
1254 |
|
} |
1255 |
|
|
1256 |
/* finally, intra decision */ |
/* finally, intra decision */ |
1257 |
|
|
1258 |
InterBias = MV16_INTER_BIAS; |
InterBias = MV16_INTER_BIAS; |
1259 |
if (pMB->quant > 8) InterBias += 50 * (pMB->quant - 8); // to make high quants work |
if (pMB->quant > 8) InterBias += 100 * (pMB->quant - 8); // to make high quants work |
1260 |
if (y != 0) |
if (y != 0) |
1261 |
if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 50; |
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
1262 |
if (x != 0) |
if (x != 0) |
1263 |
if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 50; |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
1264 |
|
|
1265 |
|
if (Data.chroma) InterBias += 50; // to compensate bigger SAD |
1266 |
|
|
1267 |
if (InterBias < pMB->sad16) { |
if (InterBias < pMB->sad16) { |
1268 |
const int32_t deviation = |
const int32_t deviation = |
1283 |
} |
} |
1284 |
} |
} |
1285 |
free(qimage); |
free(qimage); |
1286 |
|
|
1287 |
|
if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */ |
1288 |
|
current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode ); |
1289 |
|
else |
1290 |
|
current->GMC_MV = zeroMV; |
1291 |
|
|
1292 |
return 0; |
return 0; |
1293 |
} |
} |
1294 |
|
|
1346 |
} |
} |
1347 |
|
|
1348 |
static void |
static void |
1349 |
SearchP(const uint8_t * const pRef, |
SearchP(const IMAGE * const pRef, |
1350 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1351 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
1352 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
|
const uint8_t * const pRefQ, |
|
1353 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
1354 |
const int x, |
const int x, |
1355 |
const int y, |
const int y, |
1366 |
int i, iDirection = 255, mask, threshA; |
int i, iDirection = 255, mask, threshA; |
1367 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1368 |
|
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
|
|
|
1369 |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
1370 |
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, |
1371 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
1373 |
Data->predMV = pmv[0]; |
Data->predMV = pmv[0]; |
1374 |
|
|
1375 |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
1376 |
Data->Ref = pRef + (x + Data->iEdgedWidth*y)*16; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1377 |
|
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1378 |
|
|
1379 |
|
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
1380 |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
1381 |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
1382 |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
1383 |
Data->RefQ = pRefQ; |
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1384 |
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1385 |
|
|
1386 |
Data->iQuant = iQuant; |
Data->lambda16 = lambda_vec16[iQuant]; |
1387 |
|
Data->lambda8 = lambda_vec8[iQuant]; |
1388 |
|
|
1389 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
1390 |
Data->min_dx = EVEN(Data->min_dx); |
Data->min_dx = EVEN(Data->min_dx); |
1394 |
|
|
1395 |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
1396 |
|
|
|
if (inter4v) CheckCandidate = CheckCandidate16; |
|
|
else CheckCandidate = CheckCandidate16no4v; |
|
|
|
|
1397 |
for(i = 0; i < 5; i++) |
for(i = 0; i < 5; i++) |
1398 |
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
1399 |
|
|
1400 |
if(Data->quarterpel) |
if (pParam->m_quarterpel) { |
1401 |
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1402 |
i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode); |
i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode); |
1403 |
else |
} else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
|
i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
|
1404 |
|
|
1405 |
Data->iMinSAD[0] = pMB->sad16 + lambda_vec16[iQuant] * i; |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
1406 |
Data->iMinSAD[1] = pMB->sad8[0] + lambda_vec8[iQuant] * i; |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
1407 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
1408 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1409 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1417 |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
1418 |
prevMBs + x + y * pParam->mb_width); |
prevMBs + x + y * pParam->mb_width); |
1419 |
|
|
1420 |
if (inter4v) CheckCandidate = CheckCandidate16; |
if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16; |
1421 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1422 |
|
|
|
|
|
1423 |
/* main loop. checking all predictions */ |
/* main loop. checking all predictions */ |
1424 |
|
|
1425 |
for (i = 1; i < 7; i++) { |
for (i = 1; i < 7; i++) { |
1453 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1454 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1455 |
|
|
1456 |
CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data); |
(*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data); |
1457 |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
1458 |
if (bSAD < Data->iMinSAD[0]) { |
if (bSAD < Data->iMinSAD[0]) { |
1459 |
Data->currentMV[0] = backupMV; |
Data->currentMV[0] = backupMV; |
1466 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1467 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1468 |
|
|
1469 |
CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data); |
(*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data); |
1470 |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
1471 |
if (bSAD < Data->iMinSAD[0]) { |
if (bSAD < Data->iMinSAD[0]) { |
1472 |
Data->currentMV[0] = backupMV; |
Data->currentMV[0] = backupMV; |
1484 |
|
|
1485 |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
1486 |
|
|
|
if(inter4v) |
|
1487 |
CheckCandidate = CheckCandidate16_qpel; |
CheckCandidate = CheckCandidate16_qpel; |
|
else |
|
|
CheckCandidate = CheckCandidate16no4v_qpel; |
|
|
|
|
1488 |
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, |
1489 |
pParam->width, pParam->height, Data->iFcode, 0); // get real range |
pParam->width, pParam->height, Data->iFcode, 0); |
1490 |
|
|
1491 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
1492 |
} |
} |
1493 |
|
|
1494 |
|
if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0; |
1495 |
if (inter4v) { |
if (inter4v) { |
1496 |
SearchData Data8; |
SearchData Data8; |
1497 |
Data8.iFcode = Data->iFcode; |
Data8.iFcode = Data->iFcode; |
1498 |
Data8.iQuant = Data->iQuant; |
Data8.lambda8 = Data->lambda8; |
1499 |
Data8.iEdgedWidth = Data->iEdgedWidth; |
Data8.iEdgedWidth = Data->iEdgedWidth; |
1500 |
|
Data8.RefQ = Data->RefQ; |
1501 |
|
Data8.qpel = Data->qpel; |
1502 |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
1503 |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
1504 |
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); |
1505 |
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); |
1506 |
|
|
1507 |
|
if (Data->chroma) { |
1508 |
|
int sum, dx, dy; |
1509 |
|
|
1510 |
|
if(pParam->m_quarterpel) { |
1511 |
|
sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2; |
1512 |
|
} else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1513 |
|
dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1514 |
|
|
1515 |
|
if(pParam->m_quarterpel) { |
1516 |
|
sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2; |
1517 |
|
} else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1518 |
|
dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1519 |
|
|
1520 |
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
1521 |
|
} |
1522 |
} |
} |
1523 |
|
|
1524 |
if (!(inter4v) || |
if (!(inter4v) || |
1538 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1539 |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
1540 |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
1541 |
} |
} else { |
|
else { |
|
1542 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1543 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1544 |
} |
} |
1560 |
const int block, |
const int block, |
1561 |
SearchData * const Data) |
SearchData * const Data) |
1562 |
{ |
{ |
|
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
1563 |
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
1564 |
Data->currentMV = OldData->currentMV + 1 + block; |
Data->currentMV = OldData->currentMV + 1 + block; |
1565 |
Data->currentQMV = OldData->currentQMV + 1 + block; |
Data->currentQMV = OldData->currentQMV + 1 + block; |
|
Data->quarterpel = OldData->quarterpel; |
|
1566 |
|
|
1567 |
if(Data->quarterpel) // add d_mv_bits[qpel] everywhere but not in 0 (it's already there) |
if(pParam->m_quarterpel) { |
1568 |
{ |
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
1569 |
if (block != 0) |
if (block != 0) *(Data->iMinSAD) += (Data->lambda8 * |
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
1570 |
d_mv_bits(Data->currentQMV->x - Data->predQMV.x, |
d_mv_bits(Data->currentQMV->x - Data->predQMV.x, |
1571 |
Data->currentQMV->y - Data->predQMV.y, |
Data->currentQMV->y - Data->predQMV.y, |
1572 |
Data->iFcode); |
Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
1573 |
|
} else { |
1574 |
} else // add d_mv_bits[hpel] everywhere but not in 0 (it's already there) |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
1575 |
if (block != 0) |
if (block != 0) *(Data->iMinSAD) += (Data->lambda8 * |
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
1576 |
d_mv_bits(Data->currentMV->x - Data->predMV.x, |
d_mv_bits(Data->currentMV->x - Data->predMV.x, |
1577 |
Data->currentMV->y - Data->predMV.y, |
Data->currentMV->y - Data->predMV.y, |
1578 |
Data->iFcode); |
Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
1579 |
|
} |
1580 |
|
|
1581 |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
1582 |
|
|
1584 |
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
1585 |
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
1586 |
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
Data->RefQ = OldData->RefQ; |
|
1587 |
|
|
1588 |
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
1589 |
|
|
1590 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
1591 |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
1592 |
CheckCandidate = CheckCandidate8; |
CheckCandidate = CheckCandidate8; |
1593 |
|
|
1594 |
if (MotionFlags & PMV_EXTSEARCH8) { |
if (MotionFlags & PMV_EXTSEARCH8) { |
1601 |
|
|
1602 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
1603 |
|
|
1604 |
if(*(Data->iMinSAD) < temp_sad) { //found a better match? |
if(*(Data->iMinSAD) < temp_sad) { |
1605 |
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
1606 |
Data->currentQMV->y = 2 * Data->currentMV->y; |
Data->currentQMV->y = 2 * Data->currentMV->y; |
1607 |
} |
} |
1618 |
} |
} |
1619 |
} |
} |
1620 |
|
|
1621 |
if((Data->quarterpel) && (!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
if(pParam->m_quarterpel) { |
1622 |
|
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
1623 |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
|
|
|
1624 |
CheckCandidate = CheckCandidate8_qpel; |
CheckCandidate = CheckCandidate8_qpel; |
|
|
|
1625 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
1626 |
pParam->width, pParam->height, OldData->iFcode, 0); // get real range |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
1627 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
1628 |
} |
} |
1629 |
} |
} |
1630 |
|
} |
1631 |
|
|
1632 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1633 |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
1641 |
pMB->mvs[block] = *(Data->currentMV); |
pMB->mvs[block] = *(Data->currentMV); |
1642 |
pMB->qmvs[block] = *(Data->currentQMV); |
pMB->qmvs[block] = *(Data->currentQMV); |
1643 |
|
|
1644 |
pMB->sad8[block] = 4 * (*Data->iMinSAD); // Isibaar: why? |
pMB->sad8[block] = 4 * (*Data->iMinSAD); |
1645 |
} |
} |
1646 |
|
|
1647 |
/* B-frames code starts here */ |
/* B-frames code starts here */ |
1730 |
pParam->width, pParam->height, iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, iFcode, pParam->m_quarterpel); |
1731 |
|
|
1732 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
1733 |
|
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
1734 |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
1735 |
|
|
1736 |
Data->currentMV->x = Data->currentMV->y = 0; |
Data->currentMV->x = Data->currentMV->y = 0; |
1753 |
|
|
1754 |
HalfpelRefine(Data); |
HalfpelRefine(Data); |
1755 |
|
|
1756 |
|
if (Data->qpel) { |
1757 |
|
Data->currentQMV->x = 2*Data->currentMV->x; |
1758 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
1759 |
|
CheckCandidate = CheckCandidate16no4v_qpel; |
1760 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1761 |
|
pParam->width, pParam->height, iFcode, pParam->m_quarterpel); |
1762 |
|
QuarterpelRefine(Data); |
1763 |
|
} |
1764 |
|
|
1765 |
// three bits are needed to code backward mode. four for forward |
// three bits are needed to code backward mode. four for forward |
1766 |
// we treat the bits just like they were vector's |
// we treat the bits just like they were vector's |
1767 |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * lambda_vec16[Data->iQuant]; |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
1768 |
else *Data->iMinSAD += 3 * lambda_vec16[Data->iQuant]; |
else *Data->iMinSAD += 3 * Data->lambda16; |
|
|
|
1769 |
|
|
1770 |
if (*Data->iMinSAD < *best_sad) { |
if (*Data->iMinSAD < *best_sad) { |
1771 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1772 |
pMB->mode = mode_current; |
pMB->mode = mode_current; |
1773 |
|
if (Data->qpel) { |
1774 |
|
pMB->pmvs[0].x = Data->currentQMV->x - predMV->x; |
1775 |
|
pMB->pmvs[0].y = Data->currentQMV->y - predMV->y; |
1776 |
|
if (mode_current == MODE_FORWARD) |
1777 |
|
pMB->qmvs[0] = *Data->currentQMV; |
1778 |
|
else |
1779 |
|
pMB->b_qmvs[0] = *Data->currentQMV; |
1780 |
|
} else { |
1781 |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
1782 |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
1783 |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
} |
1784 |
else pMB->b_mvs[0] = *Data->currentMV; |
if (mode_current == MODE_FORWARD) |
1785 |
|
pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
1786 |
|
else |
1787 |
|
pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
1788 |
|
|
1789 |
} |
} |
1790 |
|
|
1791 |
} |
} |
1816 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1817 |
|
|
1818 |
*Data->iMinSAD = 256*4096; |
*Data->iMinSAD = 256*4096; |
|
Data->referencemv = b_mb->mvs; |
|
1819 |
|
|
1820 |
Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16; |
Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16; |
1821 |
Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16; |
Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16; |
1830 |
Data->max_dy = 2 * pParam->height - 2 * (y) * 16; |
Data->max_dy = 2 * pParam->height - 2 * (y) * 16; |
1831 |
Data->min_dx = -(2 * 16 + 2 * (x) * 16); |
Data->min_dx = -(2 * 16 + 2 * (x) * 16); |
1832 |
Data->min_dy = -(2 * 16 + 2 * (y) * 16); |
Data->min_dy = -(2 * 16 + 2 * (y) * 16); |
1833 |
|
if (Data->qpel) { //we measure in qpixels |
1834 |
|
Data->max_dx *= 2; |
1835 |
|
Data->max_dy *= 2; |
1836 |
|
Data->min_dx *= 2; |
1837 |
|
Data->min_dy *= 2; |
1838 |
|
Data->referencemv = b_mb->qmvs; |
1839 |
|
} else Data->referencemv = b_mb->mvs; |
1840 |
|
|
1841 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
1842 |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
1861 |
} |
} |
1862 |
} |
} |
1863 |
|
|
1864 |
|
if (Data->qpel) { |
1865 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) |
1866 |
CheckCandidate = CheckCandidateDirect; |
CheckCandidate = CheckCandidateDirect_qpel; |
1867 |
|
else CheckCandidate = CheckCandidateDirectno4v_qpel; |
1868 |
|
} else { |
1869 |
|
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
1870 |
else CheckCandidate = CheckCandidateDirectno4v; |
else CheckCandidate = CheckCandidateDirectno4v; |
1871 |
|
} |
1872 |
|
|
1873 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
(*CheckCandidate)(0, 0, 255, &k, Data); |
1874 |
|
|
1875 |
// skip decision |
// skip decision |
1876 |
if (*Data->iMinSAD - 2 * lambda_vec16[Data->iQuant] < (int32_t)Data->iQuant * SKIP_THRESH_B) { |
if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) { |
1877 |
//possible skip - checking chroma. everything copied from MC |
//possible skip - checking chroma. everything copied from MC |
1878 |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
1879 |
int sum, dx, dy, b_dx, b_dy; |
int sum, dx, dy, b_dx, b_dy; |
1880 |
|
|
1881 |
|
if (Data->qpel) { |
1882 |
|
sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2; |
1883 |
|
dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1884 |
|
sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2; |
1885 |
|
dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1886 |
|
|
1887 |
|
sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2; |
1888 |
|
b_dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1889 |
|
sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2; |
1890 |
|
b_dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1891 |
|
|
1892 |
|
} else { |
1893 |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1894 |
dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
|
1895 |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1896 |
dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
1897 |
|
|
1898 |
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
1899 |
b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
|
1900 |
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
1901 |
b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
1902 |
|
} |
1903 |
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
1904 |
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
1905 |
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
1909 |
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
1910 |
Data->iEdgedWidth/2); |
Data->iEdgedWidth/2); |
1911 |
|
|
1912 |
if ((uint32_t) sum < MAX_CHROMA_SAD_FOR_SKIP * Data->iQuant) { |
if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { |
1913 |
pMB->mode = MODE_DIRECT_NONE_MV; |
pMB->mode = MODE_DIRECT_NONE_MV; |
1914 |
return *Data->iMinSAD; |
return *Data->iMinSAD; |
1915 |
} |
} |
1926 |
|
|
1927 |
(*MainSearchPtr)(0, 0, Data, 255); |
(*MainSearchPtr)(0, 0, Data, 255); |
1928 |
|
|
1929 |
HalfpelRefine(Data); |
HalfpelRefine(Data); //or qpel refine, if we're in qpel mode |
1930 |
|
|
1931 |
*Data->iMinSAD += 1 * lambda_vec16[Data->iQuant]; // one bit is needed to code direct mode. we treat this bit just like it was vector's |
*Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode |
1932 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1933 |
|
|
1934 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) |
1946 |
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
1947 |
? Data->directmvB[k].y |
? Data->directmvB[k].y |
1948 |
: pMB->mvs[k].y - Data->referencemv[k].y); |
: pMB->mvs[k].y - Data->referencemv[k].y); |
1949 |
|
if (Data->qpel) { |
1950 |
|
pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; |
1951 |
|
pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; |
1952 |
|
pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; |
1953 |
|
pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; |
1954 |
|
} |
1955 |
|
|
1956 |
if (b_mb->mode != MODE_INTER4V) { |
if (b_mb->mode != MODE_INTER4V) { |
1957 |
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
1958 |
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
1959 |
|
pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; |
1960 |
|
pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; |
1961 |
break; |
break; |
1962 |
} |
} |
1963 |
} |
} |
1989 |
{ |
{ |
1990 |
|
|
1991 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
|
|
|
1992 |
int iDirection, i, j; |
int iDirection, i, j; |
1993 |
SearchData bData; |
SearchData bData; |
1994 |
|
|
1995 |
bData.iMinSAD = fData->iMinSAD; |
*(bData.iMinSAD = fData->iMinSAD) = 4096*256; |
|
*bData.iMinSAD = 4096*256; |
|
1996 |
bData.Cur = fData->Cur; |
bData.Cur = fData->Cur; |
1997 |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
1998 |
bData.currentMV = fData->currentMV + 1; |
bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1; |
1999 |
bData.iQuant = fData->iQuant; |
bData.lambda16 = fData->lambda16; |
2000 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
2001 |
|
|
2002 |
bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16; |
bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16; |
2007 |
bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16; |
bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16; |
2008 |
bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16; |
bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16; |
2009 |
bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16; |
bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16; |
2010 |
|
bData.RefQ = fData->RefQ; |
2011 |
|
|
2012 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
2013 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
2014 |
|
|
2015 |
fData->currentMV[0] = pMB->mvs[0]; |
fData->currentMV[0] = fData->currentMV[2]; |
|
fData->currentMV[1] = pMB->b_mvs[0]; |
|
2016 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel); |
2017 |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel); |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel); |
2018 |
|
|
2043 |
// backward MV moves |
// backward MV moves |
2044 |
i = fData->currentMV[1].x; j = fData->currentMV[1].y; |
i = fData->currentMV[1].x; j = fData->currentMV[1].y; |
2045 |
fData->currentMV[2] = fData->currentMV[0]; |
fData->currentMV[2] = fData->currentMV[0]; |
|
|
|
2046 |
CheckCandidateInt(i + 1, j, 0, &iDirection, &bData); |
CheckCandidateInt(i + 1, j, 0, &iDirection, &bData); |
2047 |
CheckCandidateInt(i, j + 1, 0, &iDirection, &bData); |
CheckCandidateInt(i, j + 1, 0, &iDirection, &bData); |
2048 |
CheckCandidateInt(i - 1, j, 0, &iDirection, &bData); |
CheckCandidateInt(i - 1, j, 0, &iDirection, &bData); |
2050 |
|
|
2051 |
} while (!(iDirection)); |
} while (!(iDirection)); |
2052 |
|
|
2053 |
// two bits are needed to code interpolate mode. we treat the bits just like they were vector's |
*fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode. |
2054 |
*fData->iMinSAD += 2 * lambda_vec16[fData->iQuant]; |
|
2055 |
|
if (fData->qpel) { |
2056 |
|
CheckCandidate = CheckCandidateInt_qpel; |
2057 |
|
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0); |
2058 |
|
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0); |
2059 |
|
fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x; |
2060 |
|
fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; |
2061 |
|
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
2062 |
|
fData->currentQMV[1].y = 2 * fData->currentMV[1].y; |
2063 |
|
// QuarterpelRefine(fData); |
2064 |
|
fData->currentQMV[2] = fData->currentQMV[0]; |
2065 |
|
// QuarterpelRefine(&bData); |
2066 |
|
} |
2067 |
|
|
2068 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
2069 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
2070 |
pMB->mvs[0] = fData->currentMV[0]; |
pMB->mvs[0] = fData->currentMV[0]; |
2071 |
pMB->b_mvs[0] = fData->currentMV[1]; |
pMB->b_mvs[0] = fData->currentMV[1]; |
2072 |
pMB->mode = MODE_INTERPOLATE; |
pMB->mode = MODE_INTERPOLATE; |
2073 |
|
if (fData->qpel) { |
2074 |
|
pMB->qmvs[0] = fData->currentQMV[0]; |
2075 |
|
pMB->b_qmvs[0] = fData->currentQMV[1]; |
2076 |
|
pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
2077 |
|
pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; |
2078 |
|
pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; |
2079 |
|
pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; |
2080 |
|
} else { |
2081 |
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
2082 |
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
2083 |
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
2084 |
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
2085 |
} |
} |
2086 |
} |
} |
2087 |
|
} |
2088 |
|
|
2089 |
void |
void |
2090 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
2098 |
const IMAGE * const f_refV, |
const IMAGE * const f_refV, |
2099 |
const IMAGE * const f_refHV, |
const IMAGE * const f_refHV, |
2100 |
// backward (future) reference |
// backward (future) reference |
2101 |
const MACROBLOCK * const b_mbs, |
const FRAMEINFO * const b_reference, |
2102 |
const IMAGE * const b_ref, |
const IMAGE * const b_ref, |
2103 |
const IMAGE * const b_refH, |
const IMAGE * const b_refH, |
2104 |
const IMAGE * const b_refV, |
const IMAGE * const b_refV, |
2108 |
int32_t best_sad, skip_sad; |
int32_t best_sad, skip_sad; |
2109 |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
2110 |
static const VECTOR zeroMV={0,0}; |
static const VECTOR zeroMV={0,0}; |
2111 |
|
const MACROBLOCK * const b_mbs = b_reference->mbs; |
2112 |
|
|
2113 |
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
2114 |
|
|
2115 |
const int32_t TRB = time_pp - time_bp; |
const int32_t TRB = time_pp - time_bp; |
2116 |
const int32_t TRD = time_pp; |
const int32_t TRD = time_pp; |
2117 |
|
uint8_t * qimage; |
2118 |
|
|
2119 |
// some pre-inintialized data for the rest of the search |
// some pre-inintialized data for the rest of the search |
2120 |
|
|
2121 |
SearchData Data; |
SearchData Data; |
2122 |
int32_t iMinSAD; |
int32_t iMinSAD; |
2123 |
VECTOR currentMV[3]; |
VECTOR currentMV[3]; |
2124 |
|
VECTOR currentQMV[3]; |
2125 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
2126 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
2127 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2128 |
Data.iQuant = frame->quant; |
Data.lambda16 = lambda_vec16[frame->quant]; |
2129 |
|
Data.qpel = pParam->m_quarterpel; |
2130 |
|
|
2131 |
// note: i==horizontal, j==vertical |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
2132 |
|
return; // allocate some mem for qpel interpolated blocks |
2133 |
|
// somehow this is dirty since I think we shouldn't use malloc outside |
2134 |
|
// encoder_create() - so please fix me! |
2135 |
|
Data.RefQ = qimage; |
2136 |
|
|
2137 |
|
// note: i==horizontal, j==vertical |
2138 |
for (j = 0; j < pParam->mb_height; j++) { |
for (j = 0; j < pParam->mb_height; j++) { |
2139 |
|
|
2140 |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
2143 |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
2144 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
2145 |
|
|
2146 |
/* special case, if collocated block is SKIPed: 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 */ |
2147 |
|
if (b_reference->coding_type != S_VOP) |
2148 |
if (b_mb->mode == MODE_NOT_CODED) { |
if (b_mb->mode == MODE_NOT_CODED) { |
2149 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
2150 |
continue; |
continue; |
2151 |
} |
} |
2152 |
|
|
2153 |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
2154 |
|
pMB->quant = frame->quant; |
2155 |
|
|
2156 |
/* direct search comes first, because it (1) checks for SKIP-mode |
/* direct search comes first, because it (1) checks for SKIP-mode |
2157 |
and (2) sets very good predictions for forward and backward search */ |
and (2) sets very good predictions for forward and backward search */ |
|
|
|
2158 |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
2159 |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
2160 |
&frame->image, |
&frame->image, |
2168 |
|
|
2169 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
2170 |
|
|
|
// best_sad = 256*4096; //uncomment to disable Directsearch. |
|
|
// To disable any other mode, just comment the function call |
|
|
|
|
2171 |
// forward search |
// forward search |
2172 |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
2173 |
&frame->image, i, j, |
&frame->image, i, j, |
2185 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
2186 |
|
|
2187 |
// 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 |
2188 |
|
/* |
2189 |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
2190 |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2191 |
&frame->image, |
&frame->image, |
2196 |
&f_predMV, &b_predMV, |
&f_predMV, &b_predMV, |
2197 |
pMB, &best_sad, |
pMB, &best_sad, |
2198 |
&Data); |
&Data); |
2199 |
|
*/ |
2200 |
switch (pMB->mode) { |
switch (pMB->mode) { |
2201 |
case MODE_FORWARD: |
case MODE_FORWARD: |
2202 |
f_count++; |
f_count++; |
2203 |
f_predMV = pMB->mvs[0]; |
if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0]; |
2204 |
|
else f_predMV = pMB->mvs[0]; |
2205 |
break; |
break; |
2206 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
2207 |
b_count++; |
b_count++; |
2208 |
b_predMV = pMB->b_mvs[0]; |
if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0]; |
2209 |
|
else b_predMV = pMB->b_mvs[0]; |
2210 |
break; |
break; |
2211 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
2212 |
i_count++; |
i_count++; |
2213 |
|
if (pParam->m_quarterpel) { |
2214 |
|
f_predMV = pMB->qmvs[0]; |
2215 |
|
b_predMV = pMB->b_qmvs[0]; |
2216 |
|
} else { |
2217 |
f_predMV = pMB->mvs[0]; |
f_predMV = pMB->mvs[0]; |
2218 |
b_predMV = pMB->b_mvs[0]; |
b_predMV = pMB->b_mvs[0]; |
2219 |
|
} |
2220 |
break; |
break; |
2221 |
case MODE_DIRECT: |
case MODE_DIRECT: |
2222 |
case MODE_DIRECT_NO4V: |
case MODE_DIRECT_NO4V: |
2227 |
} |
} |
2228 |
} |
} |
2229 |
} |
} |
2230 |
|
free(qimage); |
|
// fprintf(debug,"B-Stat: F: %04d B: %04d I: %04d D: %04d, N: %04d\n", |
|
|
// f_count,b_count,i_count,d_count,n_count); |
|
|
|
|
2231 |
} |
} |
2232 |
|
|
2233 |
/* Hinted ME starts here */ |
/* Hinted ME starts here */ |
2234 |
|
|
2235 |
static void |
static void |
2236 |
Search8hinted(const SearchData * const OldData, |
SearchPhinted ( const IMAGE * const pRef, |
|
const int x, const int y, |
|
|
const uint32_t MotionFlags, |
|
|
const MBParam * const pParam, |
|
|
MACROBLOCK * const pMB, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const int block, |
|
|
SearchData * const Data) |
|
|
{ |
|
|
int32_t temp_sad; |
|
|
MainSearchFunc *MainSearchPtr; |
|
|
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
|
|
Data->currentMV = OldData->currentMV + 1 + block; |
|
|
Data->currentQMV = OldData->currentQMV + 1 + block; |
|
|
Data->quarterpel = OldData->quarterpel; |
|
|
|
|
|
if (block != 0) { |
|
|
if(pParam->m_quarterpel) { |
|
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
|
d_mv_bits( Data->currentQMV->x - Data->predQMV.x, |
|
|
Data->currentQMV->y - Data->predQMV.y, |
|
|
Data->iFcode); |
|
|
} |
|
|
else { |
|
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
|
d_mv_bits( Data->currentMV->x - Data->predMV.x, |
|
|
Data->currentMV->y - Data->predMV.y, |
|
|
Data->iFcode); |
|
|
} |
|
|
} |
|
|
|
|
|
Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefQ = OldData->RefQ; |
|
|
|
|
|
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
|
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
|
|
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
|
|
CheckCandidate = CheckCandidate8; |
|
|
|
|
|
temp_sad = *(Data->iMinSAD); // store current MinSAD |
|
|
|
|
|
if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch; |
|
|
else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
|
|
else MainSearchPtr = DiamondSearch; |
|
|
|
|
|
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
|
|
|
|
|
if(*(Data->iMinSAD) < temp_sad) { |
|
|
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
|
|
Data->currentQMV->y = 2 * Data->currentMV->y; |
|
|
} |
|
|
|
|
|
if (MotionFlags & PMV_HALFPELREFINE8) { |
|
|
temp_sad = *(Data->iMinSAD); // store current MinSAD |
|
|
|
|
|
HalfpelRefine(Data); // perform halfpel refine of current best vector |
|
|
|
|
|
if(*(Data->iMinSAD) < temp_sad) { // we have found a better match |
|
|
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
|
|
Data->currentQMV->y = 2 * Data->currentMV->y; |
|
|
} |
|
|
} |
|
|
|
|
|
if((Data->quarterpel) && (!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
|
|
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
|
|
|
|
|
CheckCandidate = CheckCandidate8_qpel; |
|
|
|
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
|
|
pParam->width, pParam->height, OldData->iFcode, 0); // get real range |
|
|
|
|
|
QuarterpelRefine(Data); |
|
|
} |
|
|
|
|
|
if(pParam->m_quarterpel) { |
|
|
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
|
|
pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y; |
|
|
} |
|
|
else { |
|
|
pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; |
|
|
pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; |
|
|
} |
|
|
|
|
|
pMB->mvs[block] = *(Data->currentMV); |
|
|
pMB->qmvs[block] = *(Data->currentQMV); |
|
|
|
|
|
pMB->sad8[block] = 4 * (*Data->iMinSAD); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
SearchPhinted ( const uint8_t * const pRef, |
|
2237 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
2238 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
2239 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
|
const uint8_t * const pRefQ, |
|
2240 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
2241 |
const int x, |
const int x, |
2242 |
const int y, |
const int y, |
2249 |
SearchData * const Data) |
SearchData * const Data) |
2250 |
{ |
{ |
2251 |
|
|
|
const int32_t iEdgedWidth = pParam->edged_width; |
|
|
|
|
2252 |
int i, t; |
int i, t; |
2253 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
2254 |
|
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
2255 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
2256 |
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
2257 |
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, |
2258 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
2259 |
|
|
2260 |
Data->Cur = pCur->y + (x + y * iEdgedWidth) * 16; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
2261 |
Data->Ref = pRef + (x + iEdgedWidth*y)*16; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
2262 |
Data->RefH = pRefH + (x + iEdgedWidth*y) * 16; |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
Data->RefV = pRefV + (x + iEdgedWidth*y) * 16; |
|
|
Data->RefHV = pRefHV + (x + iEdgedWidth*y) * 16; |
|
|
Data->RefQ = pRefQ; |
|
2263 |
|
|
2264 |
Data->iQuant = iQuant; |
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
2265 |
|
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
2266 |
|
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
2267 |
|
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
2268 |
|
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
2269 |
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
2270 |
|
|
2271 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
2272 |
Data->min_dx = EVEN(Data->min_dx); |
Data->min_dx = EVEN(Data->min_dx); |
2279 |
|
|
2280 |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
2281 |
|
|
2282 |
if (inter4v) |
if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16; |
|
CheckCandidate = CheckCandidate16; |
|
2283 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
2284 |
|
|
|
|
|
2285 |
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
2286 |
pMB->mvs[0].y = EVEN(pMB->mvs[0].y); |
pMB->mvs[0].y = EVEN(pMB->mvs[0].y); |
2287 |
if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed |
if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed |
2315 |
} |
} |
2316 |
|
|
2317 |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
|
if(inter4v) |
|
|
CheckCandidate = CheckCandidate16_qpel; |
|
|
else |
|
|
CheckCandidate = CheckCandidate16no4v_qpel; |
|
|
|
|
2318 |
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, |
2319 |
pParam->width, pParam->height, Data->iFcode, 0); // get real range |
pParam->width, pParam->height, Data->iFcode, 0); |
2320 |
|
CheckCandidate = CheckCandidate16_qpel; |
2321 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
2322 |
} |
} |
2323 |
|
|
2324 |
if (inter4v) { |
if (inter4v) { |
2325 |
SearchData Data8; |
SearchData Data8; |
2326 |
Data8.iFcode = Data->iFcode; |
Data8.iFcode = Data->iFcode; |
2327 |
Data8.iQuant = Data->iQuant; |
Data8.lambda8 = Data->lambda8; |
2328 |
Data8.iEdgedWidth = Data->iEdgedWidth; |
Data8.iEdgedWidth = Data->iEdgedWidth; |
2329 |
Search8hinted(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Data8.RefQ = Data->RefQ; |
2330 |
Search8hinted(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Data8.qpel = Data->qpel; |
2331 |
Search8hinted(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
2332 |
Search8hinted(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
2333 |
|
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
2334 |
|
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
2335 |
|
|
2336 |
|
if (Data->chroma) { |
2337 |
|
int sum, dx, dy; |
2338 |
|
|
2339 |
|
if(pParam->m_quarterpel) |
2340 |
|
sum = (pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2); |
2341 |
|
else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
2342 |
|
dy = (sum ? SIGN(sum) * |
2343 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
2344 |
|
|
2345 |
|
if(pParam->m_quarterpel) |
2346 |
|
sum = (pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2); |
2347 |
|
else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
2348 |
|
dx = (sum ? SIGN(sum) * |
2349 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
2350 |
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
2351 |
|
} |
2352 |
} |
} |
2353 |
|
|
2354 |
if (!(inter4v) || |
if (!(inter4v) || |
2355 |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] + |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] + |
2356 |
Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
2357 |
// INTER MODE |
// INTER MODE |
|
|
|
2358 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
2359 |
pMB->mvs[0] = pMB->mvs[1] |
pMB->mvs[0] = pMB->mvs[1] |
2360 |
= pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
= pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
2361 |
|
|
2362 |
|
pMB->qmvs[0] = pMB->qmvs[1] |
2363 |
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
2364 |
|
|
2365 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = |
2366 |
pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
2367 |
|
|
2368 |
|
if(pParam->m_quarterpel) { |
2369 |
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
2370 |
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
2371 |
|
} else { |
2372 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
2373 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
2374 |
|
} |
2375 |
} else { |
} else { |
2376 |
// INTER4V MODE; all other things are already set in Search8hinted |
// INTER4V MODE; all other things are already set in Search8 |
2377 |
pMB->mode = MODE_INTER4V; |
pMB->mode = MODE_INTER4V; |
2378 |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
2379 |
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
2397 |
uint8_t *qimage; |
uint8_t *qimage; |
2398 |
int32_t temp[5], quant = current->quant; |
int32_t temp[5], quant = current->quant; |
2399 |
int32_t iMinSAD[5]; |
int32_t iMinSAD[5]; |
2400 |
VECTOR currentMV[5]; |
VECTOR currentMV[5], currentQMV[5]; |
|
VECTOR currentQMV[5]; |
|
2401 |
SearchData Data; |
SearchData Data; |
2402 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
2403 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
2406 |
Data.temp = temp; |
Data.temp = temp; |
2407 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
2408 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
2409 |
|
Data.qpel = pParam->m_quarterpel; |
2410 |
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
2411 |
|
|
2412 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
2413 |
return; // allocate some mem for qpel interpolated blocks |
return; // allocate some mem for qpel interpolated blocks |
2414 |
// somehow this is dirty since I think we shouldn't use malloc outside |
// somehow this is dirty since I think we shouldn't use malloc outside |
2415 |
// encoder_create() - so please fix me! |
// encoder_create() - so please fix me! |
2416 |
|
|
2417 |
|
Data.RefQ = qimage; |
2418 |
|
|
2419 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2420 |
|
|
2421 |
for (y = 0; y < pParam->mb_height; y++) { |
for (y = 0; y < pParam->mb_height; y++) { |
2426 |
//intra mode is copied from the first pass. At least for the time being |
//intra mode is copied from the first pass. At least for the time being |
2427 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
2428 |
|
|
|
|
|
2429 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
2430 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
2431 |
pMB->quant = current->quant; } |
pMB->quant = current->quant; } |
2432 |
else |
else { |
2433 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
2434 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
2435 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
2436 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
2437 |
|
} |
2438 |
pMB->quant = quant; |
pMB->quant = quant; |
2439 |
} |
} |
2440 |
|
|
2441 |
SearchPhinted(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x, |
SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
2442 |
y, current->motion_flags, pMB->quant, |
y, current->motion_flags, pMB->quant, |
2443 |
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
2444 |
&Data); |
&Data); |
2459 |
SearchData * const Data) |
SearchData * const Data) |
2460 |
{ |
{ |
2461 |
|
|
2462 |
int i, mask; |
int i = 255, mask; |
2463 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
|
|
|
2464 |
*(Data->iMinSAD) = MV_MAX_ERROR; |
*(Data->iMinSAD) = MV_MAX_ERROR; |
2465 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
2466 |
|
//median is only used as prediction. it doesn't have to be real |
2467 |
|
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
2468 |
|
else |
2469 |
|
if (x == 1) //left macroblock does not have any vector now |
2470 |
|
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
2471 |
|
else if (y == 1) // top macroblock don't have it's vector |
2472 |
|
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
2473 |
|
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
2474 |
|
|
2475 |
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, |
2476 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
2477 |
|
|
2478 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
2479 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
2480 |
|
|
|
CheckCandidate = CheckCandidate16no4vI; |
|
|
|
|
2481 |
pmv[1].x = EVEN(pMB->mvs[0].x); |
pmv[1].x = EVEN(pMB->mvs[0].x); |
2482 |
pmv[1].y = EVEN(pMB->mvs[0].y); |
pmv[1].y = EVEN(pMB->mvs[0].y); |
2483 |
pmv[0].x = EVEN(Data->predMV.x); |
pmv[2].x = EVEN(Data->predMV.x); |
2484 |
pmv[0].y = EVEN(Data->predMV.y); |
pmv[2].y = EVEN(Data->predMV.y); |
2485 |
pmv[2].x = pmv[2].y = 0; |
pmv[0].x = pmv[0].y = 0; |
2486 |
|
|
2487 |
|
(*CheckCandidate)(0, 0, 255, &i, Data); |
2488 |
|
|
2489 |
|
//early skip for 0,0 |
2490 |
|
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
2491 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
2492 |
|
pMB->mode = MODE_NOT_CODED; |
2493 |
|
return 0; |
2494 |
|
} |
2495 |
|
|
|
CheckCandidate16no4vI(pmv[0].x, pmv[0].y, 255, &i, Data); |
|
2496 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
2497 |
CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data); |
(*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data); |
2498 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
2499 |
CheckCandidate16no4vI(0, 0, mask, &i, Data); |
(*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data); |
2500 |
|
|
2501 |
|
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
2502 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
2503 |
|
|
2504 |
pMB->mvs[0] = pMB->mvs[1] |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
2505 |
= pMB->mvs[2] = pMB->mvs[3] = *Data->currentMV; // all, for future get_pmv() |
pMB->mode = MODE_INTER; |
|
|
|
2506 |
return *(Data->iMinSAD); |
return *(Data->iMinSAD); |
2507 |
} |
} |
2508 |
|
|
2509 |
#define INTRA_THRESH 1350 |
#define INTRA_THRESH 1350 |
2510 |
#define INTER_THRESH 900 |
#define INTER_THRESH 900 |
2511 |
|
|
2512 |
|
|
2513 |
int |
int |
2514 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2515 |
const IMAGE * const pCurrent, |
FRAMEINFO * const Current, |
2516 |
MBParam * const pParam, |
MBParam * const pParam, |
2517 |
MACROBLOCK * const pMBs, |
int maxIntra, //maximum number if non-I frames |
2518 |
const uint32_t iFcode) |
int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
2519 |
|
int bCount) // number if B frames in a row |
2520 |
{ |
{ |
2521 |
uint32_t x, y, intra = 0; |
uint32_t x, y, intra = 0; |
2522 |
int sSAD = 0; |
int sSAD = 0; |
2523 |
|
MACROBLOCK * const pMBs = Current->mbs; |
2524 |
|
const IMAGE * const pCurrent = &Current->image; |
2525 |
|
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
2526 |
|
|
2527 |
VECTOR currentMV; |
VECTOR currentMV; |
2528 |
int32_t iMinSAD; |
int32_t iMinSAD; |
2530 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
2531 |
Data.currentMV = ¤tMV; |
Data.currentMV = ¤tMV; |
2532 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2533 |
Data.iFcode = iFcode; |
Data.iFcode = Current->fcode; |
2534 |
Data.iQuant = 2; |
CheckCandidate = CheckCandidate16no4vI; |
2535 |
|
|
2536 |
|
if (intraCount < 10) // we're right after an I frame |
2537 |
|
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
2538 |
|
else |
2539 |
|
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
2540 |
|
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
2541 |
|
|
2542 |
|
|
2543 |
|
InterThresh += 300 * (1 - bCount); |
2544 |
|
if (InterThresh < 200) InterThresh = 200; |
2545 |
|
|
2546 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2547 |
|
|
2548 |
for (y = 0; y < pParam->mb_height-1; y++) { |
for (y = 1; y < pParam->mb_height-1; y++) { |
2549 |
for (x = 0; x < pParam->mb_width; x++) { |
for (x = 1; x < pParam->mb_width-1; x++) { |
2550 |
int sad, dev; |
int sad, dev; |
2551 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
2552 |
|
|
2553 |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
2554 |
pParam, pMBs, pMB, &Data); |
pParam, pMBs, pMB, &Data); |
2555 |
|
|
2556 |
if ( x != 0 && y != 0 && x != pParam->mb_width-1 ) { //no edge macroblocks, they just don't work |
if (sad > IntraThresh) { |
|
if (sad > INTRA_THRESH) { |
|
2557 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
2558 |
pParam->edged_width); |
pParam->edged_width); |
2559 |
if (dev + INTRA_THRESH < sad) intra++; |
if (dev + IntraThresh < sad) { |
2560 |
if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
pMB->mode = MODE_INTRA; |
2561 |
|
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
2562 |
} |
} |
|
sSAD += sad; |
|
2563 |
} |
} |
2564 |
|
sSAD += sad; |
2565 |
} |
} |
2566 |
} |
} |
2567 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2568 |
if (sSAD > INTER_THRESH ) return 1; //P frame |
if (sSAD > InterThresh ) return 1; //P frame |
2569 |
emms(); |
emms(); |
2570 |
return 0; // B frame |
return 0; // B frame |
2571 |
|
|
2595 |
min = -min; |
min = -min; |
2596 |
max += 1; |
max += 1; |
2597 |
if (min > max) max = min; |
if (min > max) max = min; |
2598 |
|
if (pParam->m_quarterpel) max *= 2; |
2599 |
|
|
2600 |
for (i = 1; (max > 32 << (i - 1)); i++); |
for (i = 1; (max > 32 << (i - 1)); i++); |
2601 |
return i; |
return i; |
2602 |
} |
} |
2603 |
|
|
2604 |
|
static void |
2605 |
|
CheckGMC(int x, int y, const int dir, int * iDirection, |
2606 |
|
const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC, |
2607 |
|
const MBParam * const pParam) |
2608 |
|
{ |
2609 |
|
uint32_t mx, my, a, count = 0; |
2610 |
|
|
2611 |
|
for (my = 1; my < pParam->mb_height-1; my++) |
2612 |
|
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
2613 |
|
VECTOR mv; |
2614 |
|
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
2615 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue; |
2616 |
|
mv = pMB->mvs[0]; |
2617 |
|
a = ABS(mv.x - x) + ABS(mv.y - y); |
2618 |
|
if (a < 6) count += 6 - a; |
2619 |
|
} |
2620 |
|
|
2621 |
|
if (count > *bestcount) { |
2622 |
|
*bestcount = count; |
2623 |
|
*iDirection = dir; |
2624 |
|
GMC->x = x; GMC->y = y; |
2625 |
|
} |
2626 |
|
} |
2627 |
|
|
2628 |
|
|
2629 |
|
static VECTOR |
2630 |
|
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
2631 |
|
{ |
2632 |
|
|
2633 |
|
uint32_t count, bestcount = 0; |
2634 |
|
int x, y; |
2635 |
|
VECTOR gmc = {0,0}; |
2636 |
|
int step, min_x, max_x, min_y, max_y; |
2637 |
|
uint32_t mx, my; |
2638 |
|
int iDirection, bDirection; |
2639 |
|
|
2640 |
|
min_x = min_y = -32<<iFcode; |
2641 |
|
max_x = max_y = 32<<iFcode; |
2642 |
|
|
2643 |
|
//step1: let's find a rough camera panning |
2644 |
|
for (step = 32; step >= 2; step /= 2) { |
2645 |
|
bestcount = 0; |
2646 |
|
for (y = min_y; y <= max_y; y += step) |
2647 |
|
for (x = min_x ; x <= max_x; x += step) { |
2648 |
|
count = 0; |
2649 |
|
//for all macroblocks |
2650 |
|
for (my = 1; my < pParam->mb_height-1; my++) |
2651 |
|
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
2652 |
|
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
2653 |
|
VECTOR mv; |
2654 |
|
|
2655 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
2656 |
|
continue; |
2657 |
|
|
2658 |
|
mv = pMB->mvs[0]; |
2659 |
|
if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */ |
2660 |
|
count++; |
2661 |
|
} |
2662 |
|
if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; } |
2663 |
|
} |
2664 |
|
min_x = gmc.x - step; |
2665 |
|
max_x = gmc.x + step; |
2666 |
|
min_y = gmc.y - step; |
2667 |
|
max_y = gmc.y + step; |
2668 |
|
|
2669 |
|
} |
2670 |
|
|
2671 |
|
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
2672 |
|
gmc.x = gmc.y = 0; //no camara pan, no GMC |
2673 |
|
|
2674 |
|
// step2: let's refine camera panning using gradiend-descent approach. |
2675 |
|
// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) |
2676 |
|
bestcount = 0; |
2677 |
|
CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2678 |
|
do { |
2679 |
|
x = gmc.x; y = gmc.y; |
2680 |
|
bDirection = iDirection; iDirection = 0; |
2681 |
|
if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2682 |
|
if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2683 |
|
if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2684 |
|
if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2685 |
|
|
2686 |
|
} while (iDirection); |
2687 |
|
|
2688 |
|
if (pParam->m_quarterpel) { |
2689 |
|
gmc.x *= 2; |
2690 |
|
gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */ |
2691 |
|
} |
2692 |
|
|
2693 |
|
return gmc; |
2694 |
|
} |