Today read the next XviD search algorithm, summed up.
Xvid_me_advdiamondsearch, as the name implies, is the advanced Diamond search algorithm. The search template it uses is a large diamond.
*
* *
* * *
* *
*
(Fig. 1)
As shown in Figure 1, the Big Diamond Search template has a total of 8 directions: left, right, top, down, Left-top, Left-down, Right-top, Right-down
Since the coordinate system of the image is shown in Figure 2:
X--
Y
|
V
(Fig. 2)
XviD these 8 directions in the following numbers
Top (4)
TL (5) TR (6)
Left (1) Right (2)
DL (9) DR (10)
Down (8)
And the main idea of this algorithm is:
Bdirection represents sad to meet the direction of the condition,
When the algorithm is initialized, the bdirection is set to 255 to ensure that the left, right, top, and down four directions are searched first. Then see if idirection is nonzero, non-zero indicates a better orientation, records the direction into bdirection, then updates the coordinates (x, y), and then (x, y) searches orthogonal to the bdirection direction. Similarly, if you search for a better orientation, update bdirection and (x, y) accordingly.
If *idirection is 0
Then it means that a local minimum point has been searched. But this point is not necessarily the least global.
So you have to search in the slash direction before exiting (Left-top, Left-down, Right-top, Right-down).
The specific search for which direction is to see the situation.
XviD Advanced Diamond Search algorithm
1void
2 Xvid_me_advdiamondsearch (intXintY, Searchdata *ConstData
3intBdirection, Checkfunc *ConstCheckcandidate)
4 {
5
6/* Directions:1-Left (x-1); 2-right (x+1), 4-up (y-1); 8-down (y+1) */
7
8unsignedint*ConstIdirection = &data->dir;
9
10 for(;;) {/* Forever */
*idirection = 0;
12if(Bdirection & 1) Check_candidate (X-idiamondsize, y, 1); Left
13if(Bdirection & 2) Check_candidate (x + idiamondsize, y, 2); Right
14if(Bdirection & 4) Check_candidate (x, Y-idiamondsize, 4); Top
15if(Bdirection & 8) Check_candidate (x, y + idiamondsize, 8); Down
16
+/-Now we ' re doing diagonal checks to our candidate */
18
19if(*idirection) {/* If anything found */
Bdirection = *idirection;
*idirection = 0;
x = data->currentmv->x; y = data->currentmv->y;
23if(Bdirection & 3) {/* Our candidate are left or right */
Check_candidate (x, y + idiamondsize, 8);
Check_candidate (x, Y-idiamondsize, 4);
26}Else{/* what remains.
Check_candidate (x + idiamondsize, y, 2);
Check_candidate (X-idiamondsize, y, 1);
29}
30
31if(*idirection) {
Bdirection + = *idirection;
x = data->currentmv->x; y = data->currentmv->y;
34}
35}Else{/* about-to-quit, eh-not-so fast ... */
36Switch(bdirection) {//hky:bdirection:the Best Direction
37 Case2:
Check_candidate (x + idiamondsize, y-idiamondsize, 2 + 4);
Check_candidate (x + idiamondsize, y + idiamondsize, 2 + 8);
40 Break;
41 Case1:
Check_candidate (X-idiamondsize, y-idiamondsize, 1 + 4);
Check_candidate (x-idiamondsize, y + idiamondsize, 1 + 8);
44 Break;
45 Case2 + 4:
Check_candidate (X-idiamondsize, y-idiamondsize, 1 + 4);
Check_candidate (x + idiamondsize, y-idiamondsize, 2 + 4);
Check_candidate (x + idiamondsize, y + idiamondsize, 2 + 8);
49 Break;
50 Case4:
Wuyi check_candidate (x + idiamondsize, y-idiamondsize, 2 + 4);
Check_candidate (X-idiamondsize, y-idiamondsize, 1 + 4);
53 Break;
54 Case8:
Check_candidate (x + idiamondsize, y + idiamondsize, 2 + 8);
Check_candidate (x-idiamondsize, y + idiamondsize, 1 + 8);
57 Break;
58 Case1 + 4:
Check_candidate (x-idiamondsize, y + idiamondsize, 1 + 8);
Check_candidate (X-idiamondsize, y-idiamondsize, 1 + 4);
Check_candidate (x + idiamondsize, y-idiamondsize, 2 + 4);
62 Break;
63 Case2 + 8:
Check_candidate (x + idiamondsize, y-idiamondsize, 2 + 4);
Check_candidate (x + idiamondsize, y + idiamondsize, 2 + 8);
Check_candidate (x-idiamondsize, y + idiamondsize, 1 + 8);
67 Break;
68 Case1 + 8:
Check_candidate (X-idiamondsize, y-idiamondsize, 1 + 4);
Check_candidate (x-idiamondsize, y + idiamondsize, 1 + 8);
Check_candidate (x + idiamondsize, y + idiamondsize, 2 + 8);
72 Break;
73default:/* 1+2+4+8 = = We didn ' t find anything at all */
Check_candidate (X-idiamondsize, y-idiamondsize, 1 + 4);
Check_candidate (x-idiamondsize, y + idiamondsize, 1 + 8);
Check_candidate (x + idiamondsize, y-idiamondsize, 2 + 4);
Check_candidate (x + idiamondsize, y + idiamondsize, 2 + 8);
78 Break;
79}
80if(!*idirection) Break; /* OK, the end. Really * *
Bayi bdirection = *idirection;
data->currentmv->x; x = y = data->currentmv->y;
83}
84}
85}
86