comparison motion_est_template.c @ 936:caa77cd960c0 libavcodec

qpel encoding 4mv+b frames encoding finally fixed chroma ME 5 comparission functions for ME b frame encoding speedup wmv2 codec (unfinished) user specified diamond size for EPZS
author michaelni
date Fri, 27 Dec 2002 23:51:46 +0000
parents
children 371bc36a9c5c
comparison
equal deleted inserted replaced
935:c9bbd35064b6 936:caa77cd960c0
1 /*
2 * Motion estimation
3 * Copyright (c) 2002 Michael Niedermayer
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21 //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
22 //Note, the last line is there to kill these ugly unused var warnings
23 #define LOAD_COMMON(x, y)\
24 uint32_t * const score_map= s->me.score_map;\
25 const int stride= s->linesize;\
26 const int uvstride= s->uvlinesize;\
27 const int time_pp= s->pp_time;\
28 const int time_pb= s->pb_time;\
29 uint8_t * const src_y= s->new_picture.data[0] + ((y) * stride) + (x);\
30 uint8_t * const src_u= s->new_picture.data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
31 uint8_t * const src_v= s->new_picture.data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
32 uint8_t * const ref_y= ref_picture->data[0] + ((y) * stride) + (x);\
33 uint8_t * const ref_u= ref_picture->data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
34 uint8_t * const ref_v= ref_picture->data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
35 uint8_t * const ref2_y= s->next_picture.data[0] + ((y) * stride) + (x);\
36 op_pixels_func (*hpel_put)[4];\
37 op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
38 op_pixels_func (*chroma_hpel_put)[4];\
39 qpel_mc_func (*qpel_put)[16];\
40 qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
41 const __attribute__((unused)) int unu= time_pp + time_pb + (int)src_u + (int)src_v + (int)ref_u + (int)ref_v\
42 + (int)ref2_y + (int)hpel_avg + (int)qpel_avg;\
43 if(s->no_rounding /*FIXME b_type*/){\
44 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
45 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
46 qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
47 }else{\
48 hpel_put=& s->dsp.put_pixels_tab[size];\
49 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
50 qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
51 }
52
53
54 #ifdef CMP_HPEL
55
56 #define CHECK_HALF_MV(dx, dy, x, y)\
57 {\
58 const int hx= 2*(x)+(dx);\
59 const int hy= 2*(y)+(dy);\
60 CMP_HPEL(d, dx, dy, x, y, size);\
61 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
62 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
63 }
64
65 #if 0
66 static int RENAME(hpel_motion_search)(MpegEncContext * s,
67 int *mx_ptr, int *my_ptr, int dmin,
68 int xmin, int ymin, int xmax, int ymax,
69 int pred_x, int pred_y, Picture *ref_picture,
70 int n, int size)
71 {
72 UINT8 *ptr;
73
74 const int xx = 16 * s->mb_x + 8*(n&1);
75 const int yy = 16 * s->mb_y + 8*(n>>1);
76 const int mx = *mx_ptr;
77 const int my = *my_ptr;
78
79 LOAD_COMMON(xx, yy);
80
81 // INIT;
82 //FIXME factorize
83 me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
84
85 if(s->no_rounding /*FIXME b_type*/){
86 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
87 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
88 }else{
89 hpel_put=& s->dsp.put_pixels_tab[size];
90 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
91 }
92 cmp= s->dsp.me_cmp[size];
93 chroma_cmp= s->dsp.me_cmp[size+1];
94 cmp_sub= s->dsp.me_sub_cmp[size];
95 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
96
97 if(s->me.skip){ //FIXME somehow move up (benchmark)
98 *mx_ptr = 0;
99 *my_ptr = 0;
100 return dmin;
101 }
102
103 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
104 CMP_HPEL(dmin, 0, 0, mx, my, size);
105 if(mx || my)
106 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
107 }
108
109 if (mx > xmin && mx < xmax &&
110 my > ymin && my < ymax) {
111 int bx=2*mx, by=2*my;
112 int d= dmin;
113
114 CHECK_HALF_MV(1, 1, mx-1, my-1)
115 CHECK_HALF_MV(0, 1, mx , my-1)
116 CHECK_HALF_MV(1, 1, mx , my-1)
117 CHECK_HALF_MV(1, 0, mx-1, my )
118 CHECK_HALF_MV(1, 0, mx , my )
119 CHECK_HALF_MV(1, 1, mx-1, my )
120 CHECK_HALF_MV(0, 1, mx , my )
121 CHECK_HALF_MV(1, 1, mx , my )
122
123 assert(bx < xmin*2 || bx > xmax*2 || by < ymin*2 || by > ymax*2);
124
125 *mx_ptr = bx;
126 *my_ptr = by;
127 }else{
128 *mx_ptr =2*mx;
129 *my_ptr =2*my;
130 }
131
132 return dmin;
133 }
134
135 #else
136 static int RENAME(hpel_motion_search)(MpegEncContext * s,
137 int *mx_ptr, int *my_ptr, int dmin,
138 int xmin, int ymin, int xmax, int ymax,
139 int pred_x, int pred_y, Picture *ref_picture,
140 int n, int size, uint16_t * const mv_penalty)
141 {
142 const int xx = 16 * s->mb_x + 8*(n&1);
143 const int yy = 16 * s->mb_y + 8*(n>>1);
144 const int mx = *mx_ptr;
145 const int my = *my_ptr;
146 const int penalty_factor= s->me.sub_penalty_factor;
147 me_cmp_func cmp_sub, chroma_cmp_sub;
148
149 LOAD_COMMON(xx, yy);
150
151 //FIXME factorize
152
153 cmp_sub= s->dsp.me_sub_cmp[size];
154 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
155
156 if(s->me.skip){ //FIXME move out of hpel?
157 *mx_ptr = 0;
158 *my_ptr = 0;
159 return dmin;
160 }
161
162 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
163 CMP_HPEL(dmin, 0, 0, mx, my, size);
164 if(mx || my)
165 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
166 }
167
168 if (mx > xmin && mx < xmax &&
169 my > ymin && my < ymax) {
170 int bx=2*mx, by=2*my;
171 int d= dmin;
172 const int index= (my<<ME_MAP_SHIFT) + mx;
173 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
174 + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*penalty_factor;
175 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
176 + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*penalty_factor;
177 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
178 + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*penalty_factor;
179 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
180 + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*penalty_factor;
181
182 if(t<=b){
183 CHECK_HALF_MV(0, 1, mx ,my-1)
184 if(l<=r){
185 CHECK_HALF_MV(1, 1, mx-1, my-1)
186 if(t+r<=b+l){
187 CHECK_HALF_MV(1, 1, mx , my-1)
188 }else{
189 CHECK_HALF_MV(1, 1, mx-1, my )
190 }
191 CHECK_HALF_MV(1, 0, mx-1, my )
192 }else{
193 CHECK_HALF_MV(1, 1, mx , my-1)
194 if(t+l<=b+r){
195 CHECK_HALF_MV(1, 1, mx-1, my-1)
196 }else{
197 CHECK_HALF_MV(1, 1, mx , my )
198 }
199 CHECK_HALF_MV(1, 0, mx , my )
200 }
201 }else{
202 if(l<=r){
203 if(t+l<=b+r){
204 CHECK_HALF_MV(1, 1, mx-1, my-1)
205 }else{
206 CHECK_HALF_MV(1, 1, mx , my )
207 }
208 CHECK_HALF_MV(1, 0, mx-1, my)
209 CHECK_HALF_MV(1, 1, mx-1, my)
210 }else{
211 if(t+r<=b+l){
212 CHECK_HALF_MV(1, 1, mx , my-1)
213 }else{
214 CHECK_HALF_MV(1, 1, mx-1, my)
215 }
216 CHECK_HALF_MV(1, 0, mx , my)
217 CHECK_HALF_MV(1, 1, mx , my)
218 }
219 CHECK_HALF_MV(0, 1, mx , my)
220 }
221 assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
222
223 *mx_ptr = bx;
224 *my_ptr = by;
225 }else{
226 *mx_ptr =2*mx;
227 *my_ptr =2*my;
228 }
229
230 return dmin;
231 }
232 #endif
233
234 #endif /* CMP_HPEL */
235
236 #ifdef CMP_QPEL
237
238 #define CHECK_QUARTER_MV(dx, dy, x, y)\
239 {\
240 const int hx= 4*(x)+(dx);\
241 const int hy= 4*(y)+(dy);\
242 CMP_QPEL(d, dx, dy, x, y, size);\
243 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
244 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
245 }
246
247 static int RENAME(qpel_motion_search)(MpegEncContext * s,
248 int *mx_ptr, int *my_ptr, int dmin,
249 int xmin, int ymin, int xmax, int ymax,
250 int pred_x, int pred_y, Picture *ref_picture,
251 int n, int size, uint16_t * const mv_penalty)
252 {
253 const int xx = 16 * s->mb_x + 8*(n&1);
254 const int yy = 16 * s->mb_y + 8*(n>>1);
255 const int mx = *mx_ptr;
256 const int my = *my_ptr;
257 const int penalty_factor= s->me.sub_penalty_factor;
258 const int map_generation= s->me.map_generation;
259 uint32_t *map= s->me.map;
260 me_cmp_func cmp, chroma_cmp;
261 me_cmp_func cmp_sub, chroma_cmp_sub;
262
263 LOAD_COMMON(xx, yy);
264
265 cmp= s->dsp.me_cmp[size];
266 chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
267 //FIXME factorize
268
269 cmp_sub= s->dsp.me_sub_cmp[size];
270 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
271
272 if(s->me.skip){ //FIXME somehow move up (benchmark)
273 *mx_ptr = 0;
274 *my_ptr = 0;
275 return dmin;
276 }
277
278 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
279 CMP_QPEL(dmin, 0, 0, mx, my, size);
280 if(mx || my)
281 dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
282 }
283
284 if (mx > xmin && mx < xmax &&
285 my > ymin && my < ymax) {
286 int bx=4*mx, by=4*my;
287 int d= dmin;
288 int i, nx, ny;
289 const int index= (my<<ME_MAP_SHIFT) + mx;
290 const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
291 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
292 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
293 const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
294 const int c= score_map[(index )&(ME_MAP_SIZE-1)];
295 int best[8];
296 int best_pos[8][2];
297
298 memset(best, 64, sizeof(int)*8);
299 #if 1
300 if(s->avctx->dia_size>=2){
301 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
302 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
303 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
304 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
305
306 for(ny= -3; ny <= 3; ny++){
307 for(nx= -3; nx <= 3; nx++){
308 const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
309 const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
310 const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
311 int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
312 int i;
313
314 if((nx&3)==0 && (ny&3)==0) continue;
315
316 score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
317
318 // if(nx&1) score-=1024*s->me.penalty_factor;
319 // if(ny&1) score-=1024*s->me.penalty_factor;
320
321 for(i=0; i<8; i++){
322 if(score < best[i]){
323 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
324 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
325 best[i]= score;
326 best_pos[i][0]= nx + 4*mx;
327 best_pos[i][1]= ny + 4*my;
328 break;
329 }
330 }
331 }
332 }
333 }else{
334 int tl;
335 const int cx = 4*(r - l);
336 const int cx2= r + l - 2*c;
337 const int cy = 4*(b - t);
338 const int cy2= b + t - 2*c;
339 int cxy;
340
341 if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
342 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
343 }else{
344 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
345 }
346
347 cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
348
349 assert(16*cx2 + 4*cx + 32*c == 32*r);
350 assert(16*cx2 - 4*cx + 32*c == 32*l);
351 assert(16*cy2 + 4*cy + 32*c == 32*b);
352 assert(16*cy2 - 4*cy + 32*c == 32*t);
353 assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
354
355 for(ny= -3; ny <= 3; ny++){
356 for(nx= -3; nx <= 3; nx++){
357 int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
358 int i;
359
360 if((nx&3)==0 && (ny&3)==0) continue;
361
362 score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
363 // if(nx&1) score-=32*s->me.penalty_factor;
364 // if(ny&1) score-=32*s->me.penalty_factor;
365
366 for(i=0; i<8; i++){
367 if(score < best[i]){
368 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
369 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
370 best[i]= score;
371 best_pos[i][0]= nx + 4*mx;
372 best_pos[i][1]= ny + 4*my;
373 break;
374 }
375 }
376 }
377 }
378 }
379 for(i=0; i<8; i++){
380 nx= best_pos[i][0];
381 ny= best_pos[i][1];
382 CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
383 }
384 #if 0
385 nx= FFMAX(4*mx - bx, bx - 4*mx);
386 ny= FFMAX(4*my - by, by - 4*my);
387
388 static int stats[4][4];
389 stats[nx][ny]++;
390 if(256*256*256*64 % (stats[0][0]+1) ==0){
391 for(i=0; i<16; i++){
392 if((i&3)==0) printf("\n");
393 printf("%6d ", stats[0][i]);
394 }
395 printf("\n");
396 }
397 #endif
398 #else
399
400 CHECK_QUARTER_MV(2, 2, mx-1, my-1)
401 CHECK_QUARTER_MV(0, 2, mx , my-1)
402 CHECK_QUARTER_MV(2, 2, mx , my-1)
403 CHECK_QUARTER_MV(2, 0, mx , my )
404 CHECK_QUARTER_MV(2, 2, mx , my )
405 CHECK_QUARTER_MV(0, 2, mx , my )
406 CHECK_QUARTER_MV(2, 2, mx-1, my )
407 CHECK_QUARTER_MV(2, 0, mx-1, my )
408
409 nx= bx;
410 ny= by;
411
412 for(i=0; i<8; i++){
413 int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
414 int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
415 CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
416 }
417 #endif
418 #if 0
419 //outer ring
420 CHECK_QUARTER_MV(1, 3, mx-1, my-1)
421 CHECK_QUARTER_MV(1, 2, mx-1, my-1)
422 CHECK_QUARTER_MV(1, 1, mx-1, my-1)
423 CHECK_QUARTER_MV(2, 1, mx-1, my-1)
424 CHECK_QUARTER_MV(3, 1, mx-1, my-1)
425 CHECK_QUARTER_MV(0, 1, mx , my-1)
426 CHECK_QUARTER_MV(1, 1, mx , my-1)
427 CHECK_QUARTER_MV(2, 1, mx , my-1)
428 CHECK_QUARTER_MV(3, 1, mx , my-1)
429 CHECK_QUARTER_MV(3, 2, mx , my-1)
430 CHECK_QUARTER_MV(3, 3, mx , my-1)
431 CHECK_QUARTER_MV(3, 0, mx , my )
432 CHECK_QUARTER_MV(3, 1, mx , my )
433 CHECK_QUARTER_MV(3, 2, mx , my )
434 CHECK_QUARTER_MV(3, 3, mx , my )
435 CHECK_QUARTER_MV(2, 3, mx , my )
436 CHECK_QUARTER_MV(1, 3, mx , my )
437 CHECK_QUARTER_MV(0, 3, mx , my )
438 CHECK_QUARTER_MV(3, 3, mx-1, my )
439 CHECK_QUARTER_MV(2, 3, mx-1, my )
440 CHECK_QUARTER_MV(1, 3, mx-1, my )
441 CHECK_QUARTER_MV(1, 2, mx-1, my )
442 CHECK_QUARTER_MV(1, 1, mx-1, my )
443 CHECK_QUARTER_MV(1, 0, mx-1, my )
444 #endif
445 assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
446
447 *mx_ptr = bx;
448 *my_ptr = by;
449 }else{
450 *mx_ptr =4*mx;
451 *my_ptr =4*my;
452 }
453
454 return dmin;
455 }
456
457 #endif /* CMP_QPEL */
458
459 #define CHECK_MV(x,y)\
460 {\
461 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
462 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
463 if(map[index]!=key){\
464 CMP(d, x, y, size);\
465 map[index]= key;\
466 score_map[index]= d;\
467 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
468 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
469 }\
470 }
471
472 #define CHECK_MV_DIR(x,y,new_dir)\
473 {\
474 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
475 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
476 if(map[index]!=key){\
477 CMP(d, x, y, size);\
478 map[index]= key;\
479 score_map[index]= d;\
480 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
481 if(d<dmin){\
482 best[0]=x;\
483 best[1]=y;\
484 dmin=d;\
485 next_dir= new_dir;\
486 }\
487 }\
488 }
489
490 #define check(x,y,S,v)\
491 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
492 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
493 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
494 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
495
496
497 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
498 Picture *ref_picture,
499 int const pred_x, int const pred_y, int const penalty_factor,
500 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
501 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
502 )
503 {
504 me_cmp_func cmp, chroma_cmp;
505 int next_dir=-1;
506 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
507
508 cmp= s->dsp.me_cmp[size];
509 chroma_cmp= s->dsp.me_cmp[size+1];
510
511 for(;;){
512 int d;
513 const int dir= next_dir;
514 const int x= best[0];
515 const int y= best[1];
516 next_dir=-1;
517
518 //printf("%d", dir);
519 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
520 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
521 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
522 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
523
524 if(next_dir==-1){
525 return dmin;
526 }
527 }
528 }
529
530 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
531 Picture *ref_picture,
532 int const pred_x, int const pred_y, int const penalty_factor,
533 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
534 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
535 )
536 {
537 me_cmp_func cmp, chroma_cmp;
538 int dia_size=1;
539 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
540
541 cmp= s->dsp.me_cmp[size];
542 chroma_cmp= s->dsp.me_cmp[size+1];
543
544 for(dia_size=1; dia_size<=s->avctx->dia_size; dia_size++){
545 int dir, start, end;
546 const int x= best[0];
547 const int y= best[1];
548
549 start= FFMAX(0, y + dia_size - ymax);
550 end = FFMIN(dia_size, xmax - x);
551 for(dir= start; dir<end; dir++){
552 int d;
553
554 //check(x + dir,y + dia_size - dir,0, a0)
555 CHECK_MV(x + dir , y + dia_size - dir);
556 }
557
558 start= FFMAX(0, x + dia_size - xmax);
559 end = FFMIN(dia_size, y - ymin);
560 for(dir= start; dir<end; dir++){
561 int d;
562
563 //check(x + dia_size - dir, y - dir,0, a1)
564 CHECK_MV(x + dia_size - dir, y - dir );
565 }
566
567 start= FFMAX(0, -y + dia_size + ymin );
568 end = FFMIN(dia_size, x - xmin);
569 for(dir= start; dir<end; dir++){
570 int d;
571
572 //check(x - dir,y - dia_size + dir,0, a2)
573 CHECK_MV(x - dir , y - dia_size + dir);
574 }
575
576 start= FFMAX(0, -x + dia_size + xmin );
577 end = FFMIN(dia_size, ymax - y);
578 for(dir= start; dir<end; dir++){
579 int d;
580
581 //check(x - dia_size + dir, y + dir,0, a3)
582 CHECK_MV(x - dia_size + dir, y + dir );
583 }
584
585 if(x!=best[0] || y!=best[1])
586 dia_size=0;
587 }
588 return dmin;
589 }
590
591 static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
592 int *mx_ptr, int *my_ptr,
593 int P[10][2], int pred_x, int pred_y,
594 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, uint16_t * const mv_penalty)
595 {
596 int best[2]={0, 0};
597 int d, dmin;
598 const int shift= 1+s->quarter_sample;
599 uint32_t *map= s->me.map;
600 int map_generation;
601 const int penalty_factor= s->me.penalty_factor;
602 const int size=0;
603 me_cmp_func cmp, chroma_cmp;
604 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
605
606 cmp= s->dsp.me_cmp[size];
607 chroma_cmp= s->dsp.me_cmp[size+1];
608
609 map_generation= update_map_generation(s);
610
611 CMP(dmin, 0, 0, size);
612 map[0]= map_generation;
613 score_map[0]= dmin;
614
615 /* first line */
616 if ((s->mb_y == 0 || s->first_slice_line)) {
617 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
618 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift)
619 }else{
620 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
621 |P_TOP[0] |P_TOP[1]
622 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0 && s->avctx->dia_size==0){
623 *mx_ptr= 0;
624 *my_ptr= 0;
625 s->me.skip=1;
626 return dmin;
627 }
628 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
629 if(dmin>256*2){
630 CHECK_MV(P_LAST[0] >>shift, P_LAST[1] >>shift)
631 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
632 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
633 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
634 }
635 }
636 if(dmin>256*4){
637 CHECK_MV(P_LAST_RIGHT[0] >>shift, P_LAST_RIGHT[1] >>shift)
638 CHECK_MV(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
639 }
640 #if 0 //doest only slow things down
641 if(dmin>512*3){
642 int step;
643 dmin= score_map[0];
644 best[0]= best[1]=0;
645 for(step=128; step>0; step>>=1){
646 const int step2= step;
647 int y;
648 for(y=-step2+best[1]; y<=step2+best[1]; y+=step){
649 int x;
650 if(y<ymin || y>ymax) continue;
651
652 for(x=-step2+best[0]; x<=step2+best[0]; x+=step){
653 if(x<xmin || x>xmax) continue;
654 if(x==best[0] && y==best[1]) continue;
655 CHECK_MV(x,y)
656 }
657 }
658 }
659 }
660 #endif
661 //check(best[0],best[1],0, b0)
662 if(s->avctx->dia_size<2)
663 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
664 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
665 shift, map, map_generation, size, mv_penalty);
666 else
667 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
668 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
669 shift, map, map_generation, size, mv_penalty);
670
671 //check(best[0],best[1],0, b1)
672 *mx_ptr= best[0];
673 *my_ptr= best[1];
674
675 // printf("%d %d %d \n", best[0], best[1], dmin);
676 return dmin;
677 }
678
679 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
680 static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
681 int *mx_ptr, int *my_ptr,
682 int P[10][2], int pred_x, int pred_y,
683 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, uint16_t * const mv_penalty)
684 {
685 int best[2]={0, 0};
686 int d, dmin;
687 const int shift= 1+s->quarter_sample;
688 uint32_t *map= s->me.map;
689 int map_generation;
690 const int penalty_factor= s->me.penalty_factor;
691 const int size=1;
692 me_cmp_func cmp, chroma_cmp;
693 LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
694
695 cmp= s->dsp.me_cmp[size];
696 chroma_cmp= s->dsp.me_cmp[size+1];
697
698 map_generation= update_map_generation(s);
699
700 dmin = 1000000;
701 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
702 /* first line */
703 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
704 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
705 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift)
706 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
707 }else{
708 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
709 //FIXME try some early stop
710 if(dmin>64*2){
711 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
712 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
713 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
714 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
715 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift)
716 }
717 }
718 if(dmin>64*4){
719 CHECK_MV(P_LAST_RIGHT[0]>>shift, P_LAST_RIGHT[1]>>shift)
720 CHECK_MV(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
721 }
722
723 if(s->avctx->dia_size<2)
724 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
725 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
726 shift, map, map_generation, size, mv_penalty);
727 else
728 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
729 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
730 shift, map, map_generation, size, mv_penalty);
731 *mx_ptr= best[0];
732 *my_ptr= best[1];
733
734 // printf("%d %d %d \n", best[0], best[1], dmin);
735 return dmin;
736 }
737 #endif /* !CMP_DIRECT */