2 |
* |
* |
3 |
* Modifications: |
* Modifications: |
4 |
* |
* |
5 |
|
* 25.04.2002 partial prevMB conversion |
6 |
|
* 22.04.2002 remove some compile warning by chenm001 <chenm001@163.com> |
7 |
|
* 14.04.2002 added MotionEstimationBVOP() |
8 |
* 02.04.2002 add EPZS(^2) as ME algorithm, use PMV_USESQUARES to choose between |
* 02.04.2002 add EPZS(^2) as ME algorithm, use PMV_USESQUARES to choose between |
9 |
* EPZS and EPZS^2 |
* EPZS and EPZS^2 |
10 |
* 08.02.2002 split up PMVfast into three routines: PMVFast, PMVFast_MainLoop |
* 08.02.2002 split up PMVfast into three routines: PMVFast, PMVFast_MainLoop |
43 |
#include "../prediction/mbprediction.h" |
#include "../prediction/mbprediction.h" |
44 |
#include "../global.h" |
#include "../global.h" |
45 |
#include "../utils/timer.h" |
#include "../utils/timer.h" |
46 |
|
#include "motion.h" |
47 |
#include "sad.h" |
#include "sad.h" |
48 |
|
|
49 |
// very large value |
// very large value |
72 |
#define EVEN(A) (((A)<0?(A)+1:(A)) & ~1) |
#define EVEN(A) (((A)<0?(A)+1:(A)) & ~1) |
73 |
|
|
74 |
|
|
|
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
|
|
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
|
#define SIGN(X) (((X)>0)?1:-1) |
|
|
|
|
75 |
int32_t PMVfastSearch16( |
int32_t PMVfastSearch16( |
76 |
const uint8_t * const pRef, |
const uint8_t * const pRef, |
77 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
80 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
81 |
const int x, const int y, |
const int x, const int y, |
82 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
83 |
|
const uint32_t iQuant, |
84 |
|
const uint32_t iFcode, |
85 |
const MBParam * const pParam, |
const MBParam * const pParam, |
86 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
87 |
|
const MACROBLOCK * const prevMBs, |
88 |
VECTOR * const currMV, |
VECTOR * const currMV, |
89 |
VECTOR * const currPMV); |
VECTOR * const currPMV); |
90 |
|
|
96 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
97 |
const int x, const int y, |
const int x, const int y, |
98 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
99 |
|
const uint32_t iQuant, |
100 |
|
const uint32_t iFcode, |
101 |
const MBParam * const pParam, |
const MBParam * const pParam, |
102 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
103 |
|
const MACROBLOCK * const prevMBs, |
104 |
VECTOR * const currMV, |
VECTOR * const currMV, |
105 |
VECTOR * const currPMV); |
VECTOR * const currPMV); |
106 |
|
|
112 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
113 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
114 |
const int x, const int y, |
const int x, const int y, |
115 |
const int start_x, int start_y, |
const int start_x, const int start_y, |
116 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
117 |
|
const uint32_t iQuant, |
118 |
|
const uint32_t iFcode, |
119 |
const MBParam * const pParam, |
const MBParam * const pParam, |
120 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
121 |
|
const MACROBLOCK * const prevMBs, |
122 |
VECTOR * const currMV, |
VECTOR * const currMV, |
123 |
VECTOR * const currPMV); |
VECTOR * const currPMV); |
124 |
|
|
129 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
130 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
131 |
const int x, const int y, |
const int x, const int y, |
132 |
const int start_x, int start_y, |
const int start_x, const int start_y, |
133 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
134 |
|
const uint32_t iQuant, |
135 |
|
const uint32_t iFcode, |
136 |
const MBParam * const pParam, |
const MBParam * const pParam, |
137 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
138 |
|
const MACROBLOCK * const prevMBs, |
139 |
VECTOR * const currMV, |
VECTOR * const currMV, |
140 |
VECTOR * const currPMV); |
VECTOR * const currPMV); |
141 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
|
/* calculate the min/max range (in halfpixels) |
|
|
relative to the _MACROBLOCK_ position |
|
|
*/ |
|
|
|
|
|
static void __inline get_range( |
|
|
int32_t * const min_dx, int32_t * const max_dx, |
|
|
int32_t * const min_dy, int32_t * const max_dy, |
|
|
const uint32_t x, const uint32_t y, |
|
|
const uint32_t block_sz, // block dimension, 8 or 16 |
|
|
const uint32_t width, const uint32_t height, |
|
|
const uint32_t fcode) |
|
|
{ |
|
|
|
|
|
const int search_range = 32 << (fcode - 1); |
|
|
const int high = search_range - 1; |
|
|
const int low = -search_range; |
|
|
|
|
|
// convert full-pixel measurements to half pixel |
|
|
const int hp_width = 2 * width; |
|
|
const int hp_height = 2 * height; |
|
|
const int hp_edge = 2 * block_sz; |
|
|
const int hp_x = 2 * (x) * block_sz; // we need _right end_ of block, not x-coordinate |
|
|
const int hp_y = 2 * (y) * block_sz; // same for _bottom end_ |
|
|
|
|
|
*max_dx = MIN(high, hp_width - hp_x); |
|
|
*max_dy = MIN(high, hp_height - hp_y); |
|
|
*min_dx = MAX(low, -(hp_edge + hp_x)); |
|
|
*min_dy = MAX(low, -(hp_edge + hp_y)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* getref: calculate reference image pointer |
|
|
* the decision to use interpolation h/v/hv or the normal image is |
|
|
* based on dx & dy. |
|
|
*/ |
|
|
|
|
|
static __inline const uint8_t * get_ref( |
|
|
const uint8_t * const refn, |
|
|
const uint8_t * const refh, |
|
|
const uint8_t * const refv, |
|
|
const uint8_t * const refhv, |
|
|
const uint32_t x, const uint32_t y, |
|
|
const uint32_t block, // block dimension, 8 or 16 |
|
|
const int32_t dx, const int32_t dy, |
|
|
const uint32_t stride) |
|
|
{ |
|
|
|
|
|
switch ( ((dx&1)<<1) + (dy&1) ) // ((dx%2)?2:0)+((dy%2)?1:0) |
|
|
{ |
|
|
case 0 : return refn + (x*block+dx/2) + (y*block+dy/2)*stride; |
|
|
case 1 : return refv + (x*block+dx/2) + (y*block+(dy-1)/2)*stride; |
|
|
case 2 : return refh + (x*block+(dx-1)/2) + (y*block+dy/2)*stride; |
|
|
default : |
|
|
case 3 : return refhv + (x*block+(dx-1)/2) + (y*block+(dy-1)/2)*stride; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* This is somehow a copy of get_ref, but with MV instead of X,Y */ |
|
|
|
|
|
static __inline const uint8_t * get_ref_mv( |
|
|
const uint8_t * const refn, |
|
|
const uint8_t * const refh, |
|
|
const uint8_t * const refv, |
|
|
const uint8_t * const refhv, |
|
|
const uint32_t x, const uint32_t y, |
|
|
const uint32_t block, // block dimension, 8 or 16 |
|
|
const VECTOR* mv, // measured in half-pel! |
|
|
const uint32_t stride) |
|
|
{ |
|
|
|
|
|
switch ( (((mv->x)&1)<<1) + ((mv->y)&1) ) |
|
|
{ |
|
|
case 0 : return refn + (x*block+(mv->x)/2) + (y*block+(mv->y)/2)*stride; |
|
|
case 1 : return refv + (x*block+(mv->x)/2) + (y*block+((mv->y)-1)/2)*stride; |
|
|
case 2 : return refh + (x*block+((mv->x)-1)/2) + (y*block+(mv->y)/2)*stride; |
|
|
default : |
|
|
case 3 : return refhv + (x*block+((mv->x)-1)/2) + (y*block+((mv->y)-1)/2)*stride; |
|
|
} |
|
|
|
|
|
} |
|
235 |
|
|
236 |
#ifndef SEARCH16 |
#ifndef SEARCH16 |
237 |
#define SEARCH16 PMVfastSearch16 |
#define SEARCH16 PMVfastSearch16 |
245 |
#endif |
#endif |
246 |
|
|
247 |
bool MotionEstimation( |
bool MotionEstimation( |
|
MACROBLOCK * const pMBs, |
|
248 |
MBParam * const pParam, |
MBParam * const pParam, |
249 |
const IMAGE * const pRef, |
FRAMEINFO * const current, |
250 |
|
FRAMEINFO * const reference, |
251 |
const IMAGE * const pRefH, |
const IMAGE * const pRefH, |
252 |
const IMAGE * const pRefV, |
const IMAGE * const pRefV, |
253 |
const IMAGE * const pRefHV, |
const IMAGE * const pRefHV, |
|
IMAGE * const pCurrent, |
|
254 |
const uint32_t iLimit) |
const uint32_t iLimit) |
255 |
|
|
256 |
{ |
{ |
257 |
const uint32_t iWcount = pParam->mb_width; |
const uint32_t iWcount = pParam->mb_width; |
258 |
const uint32_t iHcount = pParam->mb_height; |
const uint32_t iHcount = pParam->mb_height; |
259 |
|
MACROBLOCK * pMBs = current->mbs; |
260 |
|
IMAGE * pCurrent = ¤t->image; |
261 |
|
|
262 |
|
MACROBLOCK * prevMBs = reference->mbs; // previous frame |
263 |
|
IMAGE * pRef = &reference->image; |
264 |
|
|
265 |
|
|
266 |
uint32_t i, j, iIntra = 0; |
uint32_t i, j, iIntra = 0; |
267 |
|
|
275 |
if (sadInit) |
if (sadInit) |
276 |
(*sadInit)(); |
(*sadInit)(); |
277 |
|
|
278 |
|
|
279 |
|
/* eventhough we have a seperate prevMBs, |
280 |
|
pmvfast/epsz does something "funny" with the previous frames data */ |
281 |
|
|
282 |
|
for (i = 0; i < iHcount; i++) |
283 |
|
for (j = 0; j < iWcount; j++) |
284 |
|
{ |
285 |
|
pMBs[j + i * iWcount].mvs[0] = prevMBs[j + i * iWcount].mvs[0]; |
286 |
|
pMBs[j + i * iWcount].mvs[1] = prevMBs[j + i * iWcount].mvs[1]; |
287 |
|
pMBs[j + i * iWcount].mvs[2] = prevMBs[j + i * iWcount].mvs[2]; |
288 |
|
pMBs[j + i * iWcount].mvs[3] = prevMBs[j + i * iWcount].mvs[3]; |
289 |
|
} |
290 |
|
|
291 |
|
/*dprintf("*** BEFORE ***"); |
292 |
|
for (i = 0; i < iHcount; i++) |
293 |
|
for (j = 0; j < iWcount; j++) |
294 |
|
{ |
295 |
|
dprintf(" [%i,%i] mode=%i dquant=%i mvs=(%i %i %i %i) sad8=(%i %i %i %i) sad16=(%i)", j,i, |
296 |
|
pMBs[j + i * iWcount].mode, |
297 |
|
pMBs[j + i * iWcount].dquant, |
298 |
|
pMBs[j + i * iWcount].mvs[0], |
299 |
|
pMBs[j + i * iWcount].mvs[1], |
300 |
|
pMBs[j + i * iWcount].mvs[2], |
301 |
|
pMBs[j + i * iWcount].mvs[3], |
302 |
|
prevMBs[j + i * iWcount].sad8[0], |
303 |
|
prevMBs[j + i * iWcount].sad8[1], |
304 |
|
prevMBs[j + i * iWcount].sad8[2], |
305 |
|
prevMBs[j + i * iWcount].sad8[3], |
306 |
|
prevMBs[j + i * iWcount].sad16); |
307 |
|
} |
308 |
|
*/ |
309 |
|
|
310 |
// note: i==horizontal, j==vertical |
// note: i==horizontal, j==vertical |
311 |
for (i = 0; i < iHcount; i++) |
for (i = 0; i < iHcount; i++) |
312 |
for (j = 0; j < iWcount; j++) |
for (j = 0; j < iWcount; j++) |
313 |
{ |
{ |
314 |
MACROBLOCK *pMB = &pMBs[j + i * iWcount]; |
MACROBLOCK *pMB = &pMBs[j + i * iWcount]; |
315 |
|
MACROBLOCK *prevMB = &prevMBs[j + i * iWcount]; |
316 |
|
|
317 |
sad16 = SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
sad16 = SEARCH16(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
318 |
j, i, pParam->motion_flags, |
j, i, current->motion_flags, current->quant, current->fcode, |
319 |
pParam, pMBs, &mv16, &pmv16); |
pParam, pMBs, prevMBs, &mv16, &pmv16); |
320 |
pMB->sad16=sad16; |
pMB->sad16=sad16; |
321 |
|
|
322 |
|
|
332 |
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
333 |
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
334 |
|
|
335 |
|
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
336 |
|
|
337 |
iIntra++; |
iIntra++; |
338 |
if(iIntra >= iLimit) |
if(iIntra >= iLimit) |
339 |
return 1; |
return 1; |
341 |
continue; |
continue; |
342 |
} |
} |
343 |
|
|
344 |
if (pParam->global_flags & XVID_INTER4V) |
if (current->global_flags & XVID_INTER4V) |
345 |
{ |
{ |
346 |
pMB->sad8[0] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
pMB->sad8[0] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
347 |
2 * j, 2 * i, mv16.x, mv16.y, pParam->motion_flags, |
2 * j, 2 * i, mv16.x, mv16.y, |
348 |
pParam, pMBs, &pMB->mvs[0], &pMB->pmvs[0]); |
current->motion_flags, current->quant, current->fcode, |
349 |
|
pParam, pMBs, prevMBs, &pMB->mvs[0], &pMB->pmvs[0]); |
350 |
|
|
351 |
pMB->sad8[1] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
pMB->sad8[1] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
352 |
2 * j + 1, 2 * i, mv16.x, mv16.y, pParam->motion_flags, |
2 * j + 1, 2 * i, mv16.x, mv16.y, |
353 |
pParam, pMBs, &pMB->mvs[1], &pMB->pmvs[1]); |
current->motion_flags, current->quant, current->fcode, |
354 |
|
pParam, pMBs, prevMBs, &pMB->mvs[1], &pMB->pmvs[1]); |
355 |
|
|
356 |
pMB->sad8[2] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
pMB->sad8[2] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
357 |
2 * j, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, |
2 * j, 2 * i + 1, mv16.x, mv16.y, |
358 |
pParam, pMBs, &pMB->mvs[2], &pMB->pmvs[2]); |
current->motion_flags, current->quant, current->fcode, |
359 |
|
pParam, pMBs, prevMBs, &pMB->mvs[2], &pMB->pmvs[2]); |
360 |
|
|
361 |
pMB->sad8[3] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
pMB->sad8[3] = SEARCH8(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, |
362 |
2 * j + 1, 2 * i + 1, mv16.x, mv16.y, pParam->motion_flags, |
2 * j + 1, 2 * i + 1, mv16.x, mv16.y, |
363 |
pParam, pMBs, &pMB->mvs[3], &pMB->pmvs[3]); |
current->motion_flags, current->quant, current->fcode, |
364 |
|
pParam, pMBs, prevMBs, &pMB->mvs[3], &pMB->pmvs[3]); |
365 |
|
|
366 |
sad8 = pMB->sad8[0] + pMB->sad8[1] + pMB->sad8[2] + pMB->sad8[3]; |
sad8 = pMB->sad8[0] + pMB->sad8[1] + pMB->sad8[2] + pMB->sad8[3]; |
367 |
} |
} |
371 |
mpeg4: if (sad8 < sad16 - nb/2+1) use_inter4v |
mpeg4: if (sad8 < sad16 - nb/2+1) use_inter4v |
372 |
*/ |
*/ |
373 |
|
|
374 |
if (pMB->dquant == NO_CHANGE) { |
if (!(current->global_flags & XVID_LUMIMASKING) || pMB->dquant == NO_CHANGE) |
375 |
if (((pParam->global_flags & XVID_INTER4V)==0) || |
{ |
376 |
(sad16 < (sad8 + (int32_t)(IMV16X16 * pParam->quant)))) { |
if (((current->global_flags & XVID_INTER4V)==0) || |
377 |
|
(sad16 < (sad8 + (int32_t)(IMV16X16 * current->quant)))) |
378 |
|
{ |
379 |
|
|
380 |
sad8 = sad16; |
sad8 = sad16; |
381 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
398 |
} |
} |
399 |
} |
} |
400 |
|
|
401 |
|
/* dprintf("*** AFTER ***", pMBs[0].b_mvs[0].x); |
402 |
|
for (i = 0; i < iHcount; i++) |
403 |
|
for (j = 0; j < iWcount; j++) |
404 |
|
{ |
405 |
|
dprintf(" [%i,%i] mode=%i dquant=%i mvs=(%i %i %i %i) sad8=(%i %i %i %i) sad16=(%i)", j,i, |
406 |
|
pMBs[j + i * iWcount].mode, |
407 |
|
pMBs[j + i * iWcount].dquant, |
408 |
|
pMBs[j + i * iWcount].mvs[0], |
409 |
|
pMBs[j + i * iWcount].mvs[1], |
410 |
|
pMBs[j + i * iWcount].mvs[2], |
411 |
|
pMBs[j + i * iWcount].mvs[3], |
412 |
|
pMBs[j + i * iWcount].sad8[0], |
413 |
|
pMBs[j + i * iWcount].sad8[1], |
414 |
|
pMBs[j + i * iWcount].sad8[2], |
415 |
|
pMBs[j + i * iWcount].sad8[3], |
416 |
|
pMBs[j + i * iWcount].sad16); |
417 |
|
} |
418 |
|
*/ |
419 |
|
|
420 |
return 0; |
return 0; |
421 |
} |
} |
422 |
|
|
530 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
531 |
const int x, const int y, |
const int x, const int y, |
532 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
533 |
|
const uint32_t iQuant, |
534 |
|
const uint32_t iFcode, |
535 |
MBParam * const pParam, |
MBParam * const pParam, |
536 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
537 |
|
const MACROBLOCK * const prevMBs, |
538 |
VECTOR * const currMV, |
VECTOR * const currMV, |
539 |
VECTOR * const currPMV) |
VECTOR * const currPMV) |
540 |
{ |
{ |
541 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
|
const int32_t iQuant = pParam->quant; |
|
542 |
const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; |
const uint8_t * cur = pCur->y + x*16 + y*16*iEdgedWidth; |
543 |
int32_t iSAD; |
int32_t iSAD; |
544 |
int32_t pred_x,pred_y; |
int32_t pred_x,pred_y; |
860 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
861 |
const int x, const int y, |
const int x, const int y, |
862 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
863 |
|
const uint32_t iQuant, |
864 |
|
const uint32_t iFcode, |
865 |
const MBParam * const pParam, |
const MBParam * const pParam, |
866 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
867 |
|
const MACROBLOCK * const prevMBs, |
868 |
VECTOR * const currMV, |
VECTOR * const currMV, |
869 |
VECTOR * const currPMV) |
VECTOR * const currPMV) |
870 |
{ |
{ |
871 |
const uint32_t iWcount = pParam->mb_width; |
const uint32_t iWcount = pParam->mb_width; |
|
const int32_t iFcode = pParam->fixed_code; |
|
|
const int32_t iQuant = pParam->quant; |
|
872 |
const int32_t iWidth = pParam->width; |
const int32_t iWidth = pParam->width; |
873 |
const int32_t iHeight = pParam->height; |
const int32_t iHeight = pParam->height; |
874 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
890 |
VECTOR pmv[4]; |
VECTOR pmv[4]; |
891 |
int32_t psad[4]; |
int32_t psad[4]; |
892 |
|
|
893 |
MACROBLOCK * const pMB = pMBs + x + y * iWcount; |
const MACROBLOCK * const pMB = pMBs + x + y * iWcount; |
894 |
|
const MACROBLOCK * const prevMB = prevMBs + x + y * iWcount; |
895 |
|
|
896 |
static int32_t threshA,threshB; |
static int32_t threshA,threshB; |
897 |
int32_t bPredEq; |
int32_t bPredEq; |
935 |
If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 |
If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 |
936 |
*/ |
*/ |
937 |
|
|
938 |
if ((bPredEq) && (MVequal(pmv[0],pMB->mvs[0]) ) ) |
if ((bPredEq) && (MVequal(pmv[0],prevMB->mvs[0]) ) ) |
939 |
iFound=2; |
iFound=2; |
940 |
|
|
941 |
/* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. |
/* Step 3: If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. |
989 |
iEdgedWidth, MV_MAX_ERROR); |
iEdgedWidth, MV_MAX_ERROR); |
990 |
iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; |
iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; |
991 |
|
|
992 |
if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,pMB->mvs[0])) && (iMinSAD < pMB->sad16) ) ) |
if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,prevMB->mvs[0])) && ((uint32_t)iMinSAD < prevMB->sad16) ) ) |
993 |
{ |
{ |
994 |
|
|
995 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1010 |
CHECK_MV16_ZERO; |
CHECK_MV16_ZERO; |
1011 |
|
|
1012 |
// previous frame MV is always possible |
// previous frame MV is always possible |
1013 |
CHECK_MV16_CANDIDATE(pMB->mvs[0].x,pMB->mvs[0].y); |
CHECK_MV16_CANDIDATE(prevMB->mvs[0].x,prevMB->mvs[0].y); |
1014 |
|
|
1015 |
// left neighbour, if allowed |
// left neighbour, if allowed |
1016 |
if (x != 0) |
if (x != 0) |
1032 |
CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); |
CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); |
1033 |
|
|
1034 |
// top right neighbour, if allowed |
// top right neighbour, if allowed |
1035 |
if (x != (iWcount-1)) |
if ((uint32_t)x != (iWcount-1)) |
1036 |
{ |
{ |
1037 |
if (!(MotionFlags & PMV_HALFPEL16 )) |
if (!(MotionFlags & PMV_HALFPEL16 )) |
1038 |
{ pmv[3].x = EVEN(pmv[3].x); |
{ pmv[3].x = EVEN(pmv[3].x); |
1046 |
If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. |
If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. |
1047 |
*/ |
*/ |
1048 |
|
|
1049 |
if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[0]) && (iMinSAD < pMB->sad16) ) ) |
if ( (iMinSAD <= threshA) || ( MVequal(*currMV,prevMB->mvs[0]) && ((uint32_t)iMinSAD < prevMB->sad16) ) ) |
1050 |
{ |
{ |
1051 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1052 |
goto PMVfast16_Terminate_without_Refine; |
goto PMVfast16_Terminate_without_Refine; |
1232 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
1233 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
1234 |
const int x, const int y, |
const int x, const int y, |
1235 |
const int start_x, int start_y, |
const int start_x, const int start_y, |
1236 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1237 |
|
const uint32_t iQuant, |
1238 |
|
const uint32_t iFcode, |
1239 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1240 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
1241 |
|
const MACROBLOCK * const prevMBs, |
1242 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1243 |
VECTOR * const currPMV) |
VECTOR * const currPMV) |
1244 |
{ |
{ |
1245 |
const uint32_t iWcount = pParam->mb_width; |
const uint32_t iWcount = pParam->mb_width; |
|
|
|
|
const int32_t iFcode = pParam->fixed_code; |
|
|
const int32_t iQuant = pParam->quant; |
|
1246 |
const int32_t iWidth = pParam->width; |
const int32_t iWidth = pParam->width; |
1247 |
const int32_t iHeight = pParam->height; |
const int32_t iHeight = pParam->height; |
1248 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
1261 |
VECTOR newMV; |
VECTOR newMV; |
1262 |
VECTOR backupMV; |
VECTOR backupMV; |
1263 |
|
|
1264 |
MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; |
const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; |
1265 |
|
const MACROBLOCK * const prevMB = prevMBs + (x>>1) + (y>>1) * iWcount; |
1266 |
|
|
1267 |
static int32_t threshA,threshB; |
static int32_t threshA,threshB; |
1268 |
int32_t iFound,bPredEq; |
int32_t iFound,bPredEq; |
1341 |
iEdgedWidth); |
iEdgedWidth); |
1342 |
iMinSAD += calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, (uint8_t)iFcode) * iQuant; |
iMinSAD += calc_delta_8(currMV->x - pmv[0].x, currMV->y - pmv[0].y, (uint8_t)iFcode) * iQuant; |
1343 |
|
|
1344 |
if ( (iMinSAD < 256/4 ) || ( (MVequal(*currMV,pMB->mvs[iSubBlock])) && (iMinSAD < pMB->sad8[iSubBlock]) ) ) |
if ( (iMinSAD < 256/4 ) || ( (MVequal(*currMV,pMB->mvs[iSubBlock])) && ((uint32_t)iMinSAD < prevMB->sad8[iSubBlock]) ) ) |
1345 |
{ |
{ |
1346 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1347 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1400 |
If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. |
If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. |
1401 |
*/ |
*/ |
1402 |
|
|
1403 |
if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[iSubBlock]) && (iMinSAD < pMB->sad8[iSubBlock]) ) ) |
if ( (iMinSAD <= threshA) || ( MVequal(*currMV,pMB->mvs[iSubBlock]) && ((uint32_t)iMinSAD < prevMB->sad8[iSubBlock]) ) ) |
1404 |
{ |
{ |
1405 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1406 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1490 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
1491 |
const int x, const int y, |
const int x, const int y, |
1492 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1493 |
|
const uint32_t iQuant, |
1494 |
|
const uint32_t iFcode, |
1495 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1496 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
1497 |
|
const MACROBLOCK * const prevMBs, |
1498 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1499 |
VECTOR * const currPMV) |
VECTOR * const currPMV) |
1500 |
{ |
{ |
1501 |
const uint32_t iWcount = pParam->mb_width; |
const uint32_t iWcount = pParam->mb_width; |
1502 |
const uint32_t iHcount = pParam->mb_height; |
const uint32_t iHcount = pParam->mb_height; |
|
const int32_t iFcode = pParam->fixed_code; |
|
|
const int32_t iQuant = pParam->quant; |
|
1503 |
|
|
1504 |
const int32_t iWidth = pParam->width; |
const int32_t iWidth = pParam->width; |
1505 |
const int32_t iHeight = pParam->height; |
const int32_t iHeight = pParam->height; |
1519 |
int32_t psad[8]; |
int32_t psad[8]; |
1520 |
|
|
1521 |
static MACROBLOCK * oldMBs = NULL; |
static MACROBLOCK * oldMBs = NULL; |
1522 |
MACROBLOCK * const pMB = pMBs + x + y * iWcount; |
const MACROBLOCK * const pMB = pMBs + x + y * iWcount; |
1523 |
|
const MACROBLOCK * const prevMB = prevMBs + x + y * iWcount; |
1524 |
MACROBLOCK * oldMB = NULL; |
MACROBLOCK * oldMB = NULL; |
1525 |
|
|
1526 |
static int32_t thresh2; |
static int32_t thresh2; |
1583 |
iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; |
iMinSAD += calc_delta_16(currMV->x-pmv[0].x, currMV->y-pmv[0].y, (uint8_t)iFcode) * iQuant; |
1584 |
|
|
1585 |
// thresh1 is fixed to 256 |
// thresh1 is fixed to 256 |
1586 |
if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,pMB->mvs[0])) && (iMinSAD < pMB->sad16) ) ) |
if ( (iMinSAD < 256 ) || ( (MVequal(*currMV,pMB->mvs[0])) && ((uint32_t)iMinSAD < prevMB->sad16) ) ) |
1587 |
{ |
{ |
1588 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1589 |
goto EPZS16_Terminate_without_Refine; |
goto EPZS16_Terminate_without_Refine; |
1635 |
CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); |
CHECK_MV16_CANDIDATE(pmv[2].x,pmv[2].y); |
1636 |
|
|
1637 |
// top right neighbour, if allowed |
// top right neighbour, if allowed |
1638 |
if (x != (iWcount-1)) |
if ((uint32_t)x != (iWcount-1)) |
1639 |
{ |
{ |
1640 |
if (!(MotionFlags & PMV_HALFPEL16 )) |
if (!(MotionFlags & PMV_HALFPEL16 )) |
1641 |
{ pmv[3].x = EVEN(pmv[3].x); |
{ pmv[3].x = EVEN(pmv[3].x); |
1650 |
*/ |
*/ |
1651 |
|
|
1652 |
if ( (iMinSAD <= thresh2) |
if ( (iMinSAD <= thresh2) |
1653 |
|| ( MVequal(*currMV,pMB->mvs[0]) && (iMinSAD <= pMB->sad16) ) ) |
|| ( MVequal(*currMV,pMB->mvs[0]) && ((uint32_t)iMinSAD <= prevMB->sad16) ) ) |
1654 |
{ |
{ |
1655 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1656 |
goto EPZS16_Terminate_without_Refine; |
goto EPZS16_Terminate_without_Refine; |
1676 |
|
|
1677 |
// right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs |
// right neighbour, if allowed (this value is not written yet, so take it from pMB->mvs |
1678 |
|
|
1679 |
if (x != iWcount-1) |
if ((uint32_t)x != iWcount-1) |
1680 |
CHECK_MV16_CANDIDATE((pMB+1)->mvs[0].x,oldMB->mvs[0].y); |
CHECK_MV16_CANDIDATE((pMB+1)->mvs[0].x,oldMB->mvs[0].y); |
1681 |
|
|
1682 |
// bottom neighbour, dito |
// bottom neighbour, dito |
1683 |
if (y != iHcount-1) |
if ((uint32_t)y != iHcount-1) |
1684 |
CHECK_MV16_CANDIDATE((pMB+iWcount)->mvs[0].x,oldMB->mvs[0].y); |
CHECK_MV16_CANDIDATE((pMB+iWcount)->mvs[0].x,oldMB->mvs[0].y); |
1685 |
|
|
1686 |
/* Terminate if MinSAD <= T_3 (here T_3 = T_2) */ |
/* Terminate if MinSAD <= T_3 (here T_3 = T_2) */ |
1776 |
const int x, const int y, |
const int x, const int y, |
1777 |
const int start_x, const int start_y, |
const int start_x, const int start_y, |
1778 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1779 |
|
const uint32_t iQuant, |
1780 |
|
const uint32_t iFcode, |
1781 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1782 |
MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
1783 |
|
const MACROBLOCK * const prevMBs, |
1784 |
VECTOR * const currMV, |
VECTOR * const currMV, |
1785 |
VECTOR * const currPMV) |
VECTOR * const currPMV) |
1786 |
{ |
{ |
1787 |
const uint32_t iWcount = pParam->mb_width; |
const uint32_t iWcount = pParam->mb_width; |
|
const int32_t iFcode = pParam->fixed_code; |
|
|
const int32_t iQuant = pParam->quant; |
|
|
|
|
1788 |
const int32_t iWidth = pParam->width; |
const int32_t iWidth = pParam->width; |
1789 |
const int32_t iHeight = pParam->height; |
const int32_t iHeight = pParam->height; |
1790 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
1806 |
|
|
1807 |
const int32_t iSubBlock = ((y&1)<<1) + (x&1); |
const int32_t iSubBlock = ((y&1)<<1) + (x&1); |
1808 |
|
|
1809 |
MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; |
const MACROBLOCK * const pMB = pMBs + (x>>1) + (y>>1) * iWcount; |
1810 |
|
const MACROBLOCK * const prevMB = prevMBs + (x>>1) + (y>>1) * iWcount; |
1811 |
|
|
1812 |
int32_t bPredEq; |
int32_t bPredEq; |
1813 |
int32_t iMinSAD,iSAD=9999; |
int32_t iMinSAD,iSAD=9999; |
1970 |
return iMinSAD; |
return iMinSAD; |
1971 |
} |
} |
1972 |
|
|
1973 |
|
|
1974 |
|
|
1975 |
|
|
1976 |
|
|
1977 |
|
/* *********************************************************** |
1978 |
|
bvop motion estimation |
1979 |
|
// TODO: need to incorporate prediction here (eg. sad += calc_delta_16) |
1980 |
|
***************************************************************/ |
1981 |
|
|
1982 |
|
/* |
1983 |
|
void MotionEstimationBVOP( |
1984 |
|
MBParam * const pParam, |
1985 |
|
FRAMEINFO * const frame, |
1986 |
|
|
1987 |
|
// forward (past) reference |
1988 |
|
const MACROBLOCK * const f_mbs, |
1989 |
|
const IMAGE * const f_ref, |
1990 |
|
const IMAGE * const f_refH, |
1991 |
|
const IMAGE * const f_refV, |
1992 |
|
const IMAGE * const f_refHV, |
1993 |
|
// backward (future) reference |
1994 |
|
const MACROBLOCK * const b_mbs, |
1995 |
|
const IMAGE * const b_ref, |
1996 |
|
const IMAGE * const b_refH, |
1997 |
|
const IMAGE * const b_refV, |
1998 |
|
const IMAGE * const b_refHV) |
1999 |
|
{ |
2000 |
|
const uint32_t mb_width = pParam->mb_width; |
2001 |
|
const uint32_t mb_height = pParam->mb_height; |
2002 |
|
const int32_t edged_width = pParam->edged_width; |
2003 |
|
|
2004 |
|
int32_t i,j; |
2005 |
|
|
2006 |
|
int32_t f_sad16; |
2007 |
|
int32_t b_sad16; |
2008 |
|
int32_t i_sad16; |
2009 |
|
int32_t d_sad16; |
2010 |
|
int32_t best_sad; |
2011 |
|
|
2012 |
|
VECTOR pmv_dontcare; |
2013 |
|
|
2014 |
|
// note: i==horizontal, j==vertical |
2015 |
|
for (j = 0; j < mb_height; j++) |
2016 |
|
{ |
2017 |
|
for (i = 0; i < mb_width; i++) |
2018 |
|
{ |
2019 |
|
MACROBLOCK *mb = &frame->mbs[i + j*mb_width]; |
2020 |
|
const MACROBLOCK *f_mb = &f_mbs[i + j*mb_width]; |
2021 |
|
const MACROBLOCK *b_mb = &b_mbs[i + j*mb_width]; |
2022 |
|
|
2023 |
|
if (b_mb->mode == MODE_INTER |
2024 |
|
&& b_mb->cbp == 0 |
2025 |
|
&& b_mb->mvs[0].x == 0 |
2026 |
|
&& b_mb->mvs[0].y == 0) |
2027 |
|
{ |
2028 |
|
mb->mode = MB_IGNORE; |
2029 |
|
mb->mvs[0].x = 0; |
2030 |
|
mb->mvs[0].y = 0; |
2031 |
|
mb->b_mvs[0].x = 0; |
2032 |
|
mb->b_mvs[0].y = 0; |
2033 |
|
continue; |
2034 |
|
} |
2035 |
|
|
2036 |
|
|
2037 |
|
// forward search |
2038 |
|
f_sad16 = SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
2039 |
|
&frame->image, |
2040 |
|
i, j, |
2041 |
|
frame->motion_flags, frame->quant, frame->fcode, |
2042 |
|
pParam, |
2043 |
|
f_mbs, |
2044 |
|
&mb->mvs[0], &pmv_dontcare); // ignore pmv |
2045 |
|
|
2046 |
|
// backward search |
2047 |
|
b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2048 |
|
&frame->image, |
2049 |
|
i, j, |
2050 |
|
frame->motion_flags, frame->quant, frame->bcode, |
2051 |
|
pParam, |
2052 |
|
b_mbs, |
2053 |
|
&mb->b_mvs[0], &pmv_dontcare); // ignore pmv |
2054 |
|
|
2055 |
|
// interpolate search (simple, but effective) |
2056 |
|
i_sad16 = sad16bi_c( |
2057 |
|
frame->image.y + i*16 + j*16*edged_width, |
2058 |
|
get_ref(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
2059 |
|
i, j, 16, mb->mvs[0].x, mb->mvs[0].y, edged_width), |
2060 |
|
get_ref(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2061 |
|
i, j, 16, mb->b_mvs[0].x, mb->b_mvs[0].x, edged_width), |
2062 |
|
edged_width); |
2063 |
|
|
2064 |
|
// TODO: direct search |
2065 |
|
// predictor + range of [-32,32] |
2066 |
|
d_sad16 = 65535; |
2067 |
|
|
2068 |
|
|
2069 |
|
if (f_sad16 < b_sad16) |
2070 |
|
{ |
2071 |
|
best_sad = f_sad16; |
2072 |
|
mb->mode = MB_FORWARD; |
2073 |
|
} |
2074 |
|
else |
2075 |
|
{ |
2076 |
|
best_sad = b_sad16; |
2077 |
|
mb->mode = MB_BACKWARD; |
2078 |
|
} |
2079 |
|
|
2080 |
|
if (i_sad16 < best_sad) |
2081 |
|
{ |
2082 |
|
best_sad = i_sad16; |
2083 |
|
mb->mode = MB_INTERPOLATE; |
2084 |
|
} |
2085 |
|
|
2086 |
|
if (d_sad16 < best_sad) |
2087 |
|
{ |
2088 |
|
best_sad = d_sad16; |
2089 |
|
mb->mode = MB_DIRECT; |
2090 |
|
} |
2091 |
|
|
2092 |
|
} |
2093 |
|
} |
2094 |
|
} |
2095 |
|
|
2096 |
|
*/ |