Commit | Line | Data |
---|---|---|
1457ab52 MN |
1 | /* |
2 | * Motion estimation | |
8f2ab833 | 3 | * Copyright (c) 2002-2004 Michael Niedermayer |
1457ab52 MN |
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 | */ | |
983e3246 MN |
20 | |
21 | /** | |
22 | * @file motion_est_template.c | |
23 | * Motion estimation template. | |
24 | */ | |
2750b827 | 25 | |
1457ab52 | 26 | //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...) |
bb198e19 | 27 | #define LOAD_COMMON\ |
af4091f1 MN |
28 | uint32_t * const score_map= c->score_map;\ |
29 | const int xmin= c->xmin;\ | |
30 | const int ymin= c->ymin;\ | |
31 | const int xmax= c->xmax;\ | |
32 | const int ymax= c->ymax;\ | |
33 | uint8_t *mv_penalty= c->current_mv_penalty;\ | |
34 | const int pred_x= c->pred_x;\ | |
35 | const int pred_y= c->pred_y;\ | |
1457ab52 | 36 | |
1457ab52 MN |
37 | #define CHECK_HALF_MV(dx, dy, x, y)\ |
38 | {\ | |
39 | const int hx= 2*(x)+(dx);\ | |
40 | const int hy= 2*(y)+(dy);\ | |
2750b827 | 41 | d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\ |
1457ab52 MN |
42 | d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\ |
43 | COPY3_IF_LT(dmin, d, bx, hx, by, hy)\ | |
44 | } | |
45 | ||
46 | #if 0 | |
2750b827 | 47 | static int hpel_motion_search)(MpegEncContext * s, |
1457ab52 | 48 | int *mx_ptr, int *my_ptr, int dmin, |
2750b827 MN |
49 | uint8_t *ref_data[3], |
50 | int size) | |
1457ab52 | 51 | { |
1457ab52 MN |
52 | const int xx = 16 * s->mb_x + 8*(n&1); |
53 | const int yy = 16 * s->mb_y + 8*(n>>1); | |
54 | const int mx = *mx_ptr; | |
55 | const int my = *my_ptr; | |
af4091f1 | 56 | const int penalty_factor= c->sub_penalty_factor; |
1457ab52 | 57 | |
bb198e19 | 58 | LOAD_COMMON |
1457ab52 MN |
59 | |
60 | // INIT; | |
61 | //FIXME factorize | |
62 | me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub; | |
63 | ||
64 | if(s->no_rounding /*FIXME b_type*/){ | |
65 | hpel_put= &s->dsp.put_no_rnd_pixels_tab[size]; | |
66 | chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1]; | |
67 | }else{ | |
68 | hpel_put=& s->dsp.put_pixels_tab[size]; | |
69 | chroma_hpel_put= &s->dsp.put_pixels_tab[size+1]; | |
70 | } | |
2750b827 MN |
71 | cmpf= s->dsp.me_cmp[size]; |
72 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
1457ab52 MN |
73 | cmp_sub= s->dsp.me_sub_cmp[size]; |
74 | chroma_cmp_sub= s->dsp.me_sub_cmp[size+1]; | |
75 | ||
af4091f1 | 76 | if(c->skip){ //FIXME somehow move up (benchmark) |
1457ab52 MN |
77 | *mx_ptr = 0; |
78 | *my_ptr = 0; | |
79 | return dmin; | |
80 | } | |
81 | ||
1f202b0d | 82 | if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ |
1457ab52 MN |
83 | CMP_HPEL(dmin, 0, 0, mx, my, size); |
84 | if(mx || my) | |
85 | dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor; | |
86 | } | |
87 | ||
88 | if (mx > xmin && mx < xmax && | |
89 | my > ymin && my < ymax) { | |
90 | int bx=2*mx, by=2*my; | |
91 | int d= dmin; | |
92 | ||
93 | CHECK_HALF_MV(1, 1, mx-1, my-1) | |
94 | CHECK_HALF_MV(0, 1, mx , my-1) | |
95 | CHECK_HALF_MV(1, 1, mx , my-1) | |
96 | CHECK_HALF_MV(1, 0, mx-1, my ) | |
97 | CHECK_HALF_MV(1, 0, mx , my ) | |
98 | CHECK_HALF_MV(1, 1, mx-1, my ) | |
99 | CHECK_HALF_MV(0, 1, mx , my ) | |
100 | CHECK_HALF_MV(1, 1, mx , my ) | |
101 | ||
b07a5980 | 102 | assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2); |
1457ab52 MN |
103 | |
104 | *mx_ptr = bx; | |
105 | *my_ptr = by; | |
106 | }else{ | |
107 | *mx_ptr =2*mx; | |
108 | *my_ptr =2*my; | |
109 | } | |
110 | ||
111 | return dmin; | |
112 | } | |
113 | ||
114 | #else | |
2750b827 | 115 | static int hpel_motion_search(MpegEncContext * s, |
1457ab52 | 116 | int *mx_ptr, int *my_ptr, int dmin, |
2750b827 MN |
117 | int src_index, int ref_index, |
118 | int size, int h) | |
1457ab52 | 119 | { |
af4091f1 | 120 | MotionEstContext * const c= &s->me; |
1457ab52 MN |
121 | const int mx = *mx_ptr; |
122 | const int my = *my_ptr; | |
af4091f1 | 123 | const int penalty_factor= c->sub_penalty_factor; |
1457ab52 | 124 | me_cmp_func cmp_sub, chroma_cmp_sub; |
67725183 | 125 | int bx=2*mx, by=2*my; |
1457ab52 | 126 | |
bb198e19 | 127 | LOAD_COMMON |
af4091f1 | 128 | int flags= c->sub_flags; |
1457ab52 MN |
129 | |
130 | //FIXME factorize | |
131 | ||
132 | cmp_sub= s->dsp.me_sub_cmp[size]; | |
133 | chroma_cmp_sub= s->dsp.me_sub_cmp[size+1]; | |
134 | ||
af4091f1 | 135 | if(c->skip){ //FIXME move out of hpel? |
1457ab52 MN |
136 | *mx_ptr = 0; |
137 | *my_ptr = 0; | |
138 | return dmin; | |
139 | } | |
140 | ||
1f202b0d | 141 | if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ |
2750b827 | 142 | dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); |
1d0eab1d | 143 | if(mx || my || size>0) |
1457ab52 MN |
144 | dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor; |
145 | } | |
146 | ||
147 | if (mx > xmin && mx < xmax && | |
148 | my > ymin && my < ymax) { | |
1457ab52 MN |
149 | int d= dmin; |
150 | const int index= (my<<ME_MAP_SHIFT) + mx; | |
151 | const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] | |
af4091f1 | 152 | + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor; |
1457ab52 | 153 | const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)] |
af4091f1 | 154 | + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor; |
1457ab52 | 155 | const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)] |
af4091f1 | 156 | + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor; |
1457ab52 | 157 | const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] |
af4091f1 | 158 | + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor; |
b07a5980 | 159 | |
67725183 | 160 | #if 1 |
b07a5980 | 161 | int key; |
af4091f1 | 162 | int map_generation= c->map_generation; |
802f454e | 163 | #ifndef NDEBUG |
af4091f1 | 164 | uint32_t *map= c->map; |
802f454e | 165 | #endif |
b07a5980 MN |
166 | key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation; |
167 | assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key); | |
168 | key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation; | |
169 | assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key); | |
170 | key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation; | |
171 | assert(map[(index+1)&(ME_MAP_SIZE-1)] == key); | |
172 | key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation; | |
173 | assert(map[(index-1)&(ME_MAP_SIZE-1)] == key); | |
174 | #endif | |
1457ab52 MN |
175 | if(t<=b){ |
176 | CHECK_HALF_MV(0, 1, mx ,my-1) | |
177 | if(l<=r){ | |
178 | CHECK_HALF_MV(1, 1, mx-1, my-1) | |
179 | if(t+r<=b+l){ | |
180 | CHECK_HALF_MV(1, 1, mx , my-1) | |
181 | }else{ | |
182 | CHECK_HALF_MV(1, 1, mx-1, my ) | |
183 | } | |
184 | CHECK_HALF_MV(1, 0, mx-1, my ) | |
185 | }else{ | |
186 | CHECK_HALF_MV(1, 1, mx , my-1) | |
187 | if(t+l<=b+r){ | |
188 | CHECK_HALF_MV(1, 1, mx-1, my-1) | |
189 | }else{ | |
190 | CHECK_HALF_MV(1, 1, mx , my ) | |
191 | } | |
192 | CHECK_HALF_MV(1, 0, mx , my ) | |
193 | } | |
194 | }else{ | |
195 | if(l<=r){ | |
196 | if(t+l<=b+r){ | |
197 | CHECK_HALF_MV(1, 1, mx-1, my-1) | |
198 | }else{ | |
199 | CHECK_HALF_MV(1, 1, mx , my ) | |
200 | } | |
201 | CHECK_HALF_MV(1, 0, mx-1, my) | |
202 | CHECK_HALF_MV(1, 1, mx-1, my) | |
203 | }else{ | |
204 | if(t+r<=b+l){ | |
205 | CHECK_HALF_MV(1, 1, mx , my-1) | |
206 | }else{ | |
207 | CHECK_HALF_MV(1, 1, mx-1, my) | |
208 | } | |
209 | CHECK_HALF_MV(1, 0, mx , my) | |
210 | CHECK_HALF_MV(1, 1, mx , my) | |
211 | } | |
212 | CHECK_HALF_MV(0, 1, mx , my) | |
213 | } | |
214 | assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2); | |
1457ab52 MN |
215 | } |
216 | ||
67725183 MN |
217 | *mx_ptr = bx; |
218 | *my_ptr = by; | |
219 | ||
1457ab52 MN |
220 | return dmin; |
221 | } | |
222 | #endif | |
223 | ||
2750b827 MN |
224 | static int inline get_mb_score(MpegEncContext * s, int mx, int my, int src_index, |
225 | int ref_index) | |
67725183 MN |
226 | { |
227 | // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp; | |
af4091f1 | 228 | MotionEstContext * const c= &s->me; |
67725183 | 229 | const int size= 0; |
bb198e19 | 230 | const int h= 16; |
af4091f1 MN |
231 | const int penalty_factor= c->mb_penalty_factor; |
232 | const int flags= c->mb_flags; | |
2750b827 MN |
233 | const int qpel= flags & FLAG_QPEL; |
234 | const int mask= 1+2*qpel; | |
67725183 MN |
235 | me_cmp_func cmp_sub, chroma_cmp_sub; |
236 | int d; | |
237 | ||
bb198e19 | 238 | LOAD_COMMON |
67725183 MN |
239 | |
240 | //FIXME factorize | |
241 | ||
242 | cmp_sub= s->dsp.mb_cmp[size]; | |
243 | chroma_cmp_sub= s->dsp.mb_cmp[size+1]; | |
244 | ||
af4091f1 | 245 | assert(!c->skip); |
1f202b0d | 246 | assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp); |
67725183 | 247 | |
2750b827 | 248 | d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); |
67725183 MN |
249 | //FIXME check cbp before adding penalty for (0,0) vector |
250 | if(mx || my || size>0) | |
251 | d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor; | |
252 | ||
253 | return d; | |
254 | } | |
255 | ||
1457ab52 MN |
256 | #define CHECK_QUARTER_MV(dx, dy, x, y)\ |
257 | {\ | |
258 | const int hx= 4*(x)+(dx);\ | |
259 | const int hy= 4*(y)+(dy);\ | |
2750b827 | 260 | d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
1457ab52 MN |
261 | d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\ |
262 | COPY3_IF_LT(dmin, d, bx, hx, by, hy)\ | |
263 | } | |
264 | ||
2750b827 | 265 | static int qpel_motion_search(MpegEncContext * s, |
1457ab52 | 266 | int *mx_ptr, int *my_ptr, int dmin, |
2750b827 MN |
267 | int src_index, int ref_index, |
268 | int size, int h) | |
1457ab52 | 269 | { |
af4091f1 | 270 | MotionEstContext * const c= &s->me; |
1457ab52 MN |
271 | const int mx = *mx_ptr; |
272 | const int my = *my_ptr; | |
af4091f1 MN |
273 | const int penalty_factor= c->sub_penalty_factor; |
274 | const int map_generation= c->map_generation; | |
1f202b0d | 275 | const int subpel_quality= c->avctx->me_subpel_quality; |
af4091f1 | 276 | uint32_t *map= c->map; |
2750b827 | 277 | me_cmp_func cmpf, chroma_cmpf; |
1457ab52 MN |
278 | me_cmp_func cmp_sub, chroma_cmp_sub; |
279 | ||
bb198e19 | 280 | LOAD_COMMON |
af4091f1 | 281 | int flags= c->sub_flags; |
1457ab52 | 282 | |
2750b827 MN |
283 | cmpf= s->dsp.me_cmp[size]; |
284 | chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME | |
1457ab52 MN |
285 | //FIXME factorize |
286 | ||
287 | cmp_sub= s->dsp.me_sub_cmp[size]; | |
288 | chroma_cmp_sub= s->dsp.me_sub_cmp[size+1]; | |
289 | ||
af4091f1 | 290 | if(c->skip){ //FIXME somehow move up (benchmark) |
1457ab52 MN |
291 | *mx_ptr = 0; |
292 | *my_ptr = 0; | |
293 | return dmin; | |
294 | } | |
295 | ||
1f202b0d | 296 | if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ |
2750b827 | 297 | dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); |
1d0eab1d | 298 | if(mx || my || size>0) |
1457ab52 MN |
299 | dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor; |
300 | } | |
301 | ||
302 | if (mx > xmin && mx < xmax && | |
303 | my > ymin && my < ymax) { | |
304 | int bx=4*mx, by=4*my; | |
305 | int d= dmin; | |
306 | int i, nx, ny; | |
307 | const int index= (my<<ME_MAP_SHIFT) + mx; | |
308 | const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)]; | |
309 | const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]; | |
310 | const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]; | |
311 | const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)]; | |
312 | const int c= score_map[(index )&(ME_MAP_SIZE-1)]; | |
313 | int best[8]; | |
314 | int best_pos[8][2]; | |
315 | ||
316 | memset(best, 64, sizeof(int)*8); | |
317 | #if 1 | |
826f429a | 318 | if(s->me.dia_size>=2){ |
1457ab52 MN |
319 | const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; |
320 | const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; | |
321 | const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)]; | |
322 | const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)]; | |
323 | ||
324 | for(ny= -3; ny <= 3; ny++){ | |
325 | for(nx= -3; nx <= 3; nx++){ | |
0da6315a MN |
326 | //FIXME this could overflow (unlikely though) |
327 | const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t; | |
328 | const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c; | |
329 | const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b; | |
330 | int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10; | |
1457ab52 MN |
331 | int i; |
332 | ||
333 | if((nx&3)==0 && (ny&3)==0) continue; | |
334 | ||
0da6315a | 335 | score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor; |
1457ab52 | 336 | |
af4091f1 MN |
337 | // if(nx&1) score-=1024*c->penalty_factor; |
338 | // if(ny&1) score-=1024*c->penalty_factor; | |
1457ab52 MN |
339 | |
340 | for(i=0; i<8; i++){ | |
341 | if(score < best[i]){ | |
342 | memmove(&best[i+1], &best[i], sizeof(int)*(7-i)); | |
343 | memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i)); | |
344 | best[i]= score; | |
345 | best_pos[i][0]= nx + 4*mx; | |
346 | best_pos[i][1]= ny + 4*my; | |
347 | break; | |
348 | } | |
349 | } | |
350 | } | |
351 | } | |
352 | }else{ | |
353 | int tl; | |
0da6315a | 354 | //FIXME this could overflow (unlikely though) |
1457ab52 MN |
355 | const int cx = 4*(r - l); |
356 | const int cx2= r + l - 2*c; | |
357 | const int cy = 4*(b - t); | |
358 | const int cy2= b + t - 2*c; | |
359 | int cxy; | |
360 | ||
361 | if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME | |
362 | tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; | |
363 | }else{ | |
2750b827 | 364 | tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);//FIXME wrong if chroma me is different |
1457ab52 MN |
365 | } |
366 | ||
367 | cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c; | |
368 | ||
369 | assert(16*cx2 + 4*cx + 32*c == 32*r); | |
370 | assert(16*cx2 - 4*cx + 32*c == 32*l); | |
371 | assert(16*cy2 + 4*cy + 32*c == 32*b); | |
372 | assert(16*cy2 - 4*cy + 32*c == 32*t); | |
373 | assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl); | |
374 | ||
375 | for(ny= -3; ny <= 3; ny++){ | |
376 | for(nx= -3; nx <= 3; nx++){ | |
0da6315a | 377 | //FIXME this could overflow (unlikely though) |
1457ab52 MN |
378 | int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor |
379 | int i; | |
380 | ||
381 | if((nx&3)==0 && (ny&3)==0) continue; | |
382 | ||
383 | score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor; | |
af4091f1 MN |
384 | // if(nx&1) score-=32*c->penalty_factor; |
385 | // if(ny&1) score-=32*c->penalty_factor; | |
1457ab52 MN |
386 | |
387 | for(i=0; i<8; i++){ | |
388 | if(score < best[i]){ | |
389 | memmove(&best[i+1], &best[i], sizeof(int)*(7-i)); | |
390 | memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i)); | |
391 | best[i]= score; | |
392 | best_pos[i][0]= nx + 4*mx; | |
393 | best_pos[i][1]= ny + 4*my; | |
394 | break; | |
395 | } | |
396 | } | |
397 | } | |
398 | } | |
399 | } | |
826f429a | 400 | for(i=0; i<subpel_quality; i++){ |
1457ab52 MN |
401 | nx= best_pos[i][0]; |
402 | ny= best_pos[i][1]; | |
403 | CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2) | |
404 | } | |
826f429a | 405 | |
1457ab52 | 406 | #if 0 |
826f429a MN |
407 | const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; |
408 | const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; | |
409 | const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)]; | |
410 | const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)]; | |
411 | // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){ | |
412 | if(tl<br){ | |
413 | ||
414 | // nx= FFMAX(4*mx - bx, bx - 4*mx); | |
415 | // ny= FFMAX(4*my - by, by - 4*my); | |
1457ab52 | 416 | |
826f429a MN |
417 | static int stats[7][7], count; |
418 | count++; | |
419 | stats[4*mx - bx + 3][4*my - by + 3]++; | |
420 | if(256*256*256*64 % count ==0){ | |
421 | for(i=0; i<49; i++){ | |
422 | if((i%7)==0) printf("\n"); | |
1457ab52 MN |
423 | printf("%6d ", stats[0][i]); |
424 | } | |
425 | printf("\n"); | |
426 | } | |
826f429a | 427 | } |
1457ab52 MN |
428 | #endif |
429 | #else | |
430 | ||
431 | CHECK_QUARTER_MV(2, 2, mx-1, my-1) | |
432 | CHECK_QUARTER_MV(0, 2, mx , my-1) | |
433 | CHECK_QUARTER_MV(2, 2, mx , my-1) | |
434 | CHECK_QUARTER_MV(2, 0, mx , my ) | |
435 | CHECK_QUARTER_MV(2, 2, mx , my ) | |
436 | CHECK_QUARTER_MV(0, 2, mx , my ) | |
437 | CHECK_QUARTER_MV(2, 2, mx-1, my ) | |
438 | CHECK_QUARTER_MV(2, 0, mx-1, my ) | |
439 | ||
440 | nx= bx; | |
441 | ny= by; | |
442 | ||
443 | for(i=0; i<8; i++){ | |
444 | int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1}; | |
445 | int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1}; | |
446 | CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2) | |
447 | } | |
448 | #endif | |
449 | #if 0 | |
450 | //outer ring | |
451 | CHECK_QUARTER_MV(1, 3, mx-1, my-1) | |
452 | CHECK_QUARTER_MV(1, 2, mx-1, my-1) | |
453 | CHECK_QUARTER_MV(1, 1, mx-1, my-1) | |
454 | CHECK_QUARTER_MV(2, 1, mx-1, my-1) | |
455 | CHECK_QUARTER_MV(3, 1, mx-1, my-1) | |
456 | CHECK_QUARTER_MV(0, 1, mx , my-1) | |
457 | CHECK_QUARTER_MV(1, 1, mx , my-1) | |
458 | CHECK_QUARTER_MV(2, 1, mx , my-1) | |
459 | CHECK_QUARTER_MV(3, 1, mx , my-1) | |
460 | CHECK_QUARTER_MV(3, 2, mx , my-1) | |
461 | CHECK_QUARTER_MV(3, 3, mx , my-1) | |
462 | CHECK_QUARTER_MV(3, 0, mx , my ) | |
463 | CHECK_QUARTER_MV(3, 1, mx , my ) | |
464 | CHECK_QUARTER_MV(3, 2, mx , my ) | |
465 | CHECK_QUARTER_MV(3, 3, mx , my ) | |
466 | CHECK_QUARTER_MV(2, 3, mx , my ) | |
467 | CHECK_QUARTER_MV(1, 3, mx , my ) | |
468 | CHECK_QUARTER_MV(0, 3, mx , my ) | |
469 | CHECK_QUARTER_MV(3, 3, mx-1, my ) | |
470 | CHECK_QUARTER_MV(2, 3, mx-1, my ) | |
471 | CHECK_QUARTER_MV(1, 3, mx-1, my ) | |
472 | CHECK_QUARTER_MV(1, 2, mx-1, my ) | |
473 | CHECK_QUARTER_MV(1, 1, mx-1, my ) | |
474 | CHECK_QUARTER_MV(1, 0, mx-1, my ) | |
475 | #endif | |
476 | assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4); | |
477 | ||
478 | *mx_ptr = bx; | |
479 | *my_ptr = by; | |
480 | }else{ | |
481 | *mx_ptr =4*mx; | |
482 | *my_ptr =4*my; | |
483 | } | |
484 | ||
485 | return dmin; | |
486 | } | |
487 | ||
1457ab52 MN |
488 | |
489 | #define CHECK_MV(x,y)\ | |
490 | {\ | |
491 | const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ | |
492 | const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ | |
ae1dbde1 MN |
493 | assert((x) >= xmin);\ |
494 | assert((x) <= xmax);\ | |
495 | assert((y) >= ymin);\ | |
496 | assert((y) <= ymax);\ | |
b07a5980 | 497 | /*printf("check_mv %d %d\n", x, y);*/\ |
1457ab52 | 498 | if(map[index]!=key){\ |
2750b827 | 499 | d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
1457ab52 MN |
500 | map[index]= key;\ |
501 | score_map[index]= d;\ | |
502 | d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\ | |
b07a5980 | 503 | /*printf("score:%d\n", d);*/\ |
1457ab52 MN |
504 | COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\ |
505 | }\ | |
506 | } | |
507 | ||
b07a5980 MN |
508 | #define CHECK_CLIPED_MV(ax,ay)\ |
509 | {\ | |
9c3d33d6 MN |
510 | const int x= ax;\ |
511 | const int y= ay;\ | |
512 | const int x2= FFMAX(xmin, FFMIN(x, xmax));\ | |
513 | const int y2= FFMAX(ymin, FFMIN(y, ymax));\ | |
514 | CHECK_MV(x2, y2)\ | |
b07a5980 MN |
515 | } |
516 | ||
1457ab52 MN |
517 | #define CHECK_MV_DIR(x,y,new_dir)\ |
518 | {\ | |
519 | const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ | |
520 | const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ | |
b07a5980 | 521 | /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\ |
1457ab52 | 522 | if(map[index]!=key){\ |
2750b827 | 523 | d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
1457ab52 MN |
524 | map[index]= key;\ |
525 | score_map[index]= d;\ | |
526 | d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\ | |
b07a5980 | 527 | /*printf("score:%d\n", d);*/\ |
1457ab52 MN |
528 | if(d<dmin){\ |
529 | best[0]=x;\ | |
530 | best[1]=y;\ | |
531 | dmin=d;\ | |
532 | next_dir= new_dir;\ | |
533 | }\ | |
534 | }\ | |
535 | } | |
536 | ||
537 | #define check(x,y,S,v)\ | |
538 | if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\ | |
539 | if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\ | |
540 | if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\ | |
541 | if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\ | |
542 | ||
2750b827 | 543 | #define LOAD_COMMON2\ |
af4091f1 | 544 | uint32_t *map= c->map;\ |
2750b827 MN |
545 | const int qpel= flags&FLAG_QPEL;\ |
546 | const int shift= 1+qpel;\ | |
1457ab52 | 547 | |
2750b827 MN |
548 | static always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin, |
549 | int src_index, int ref_index, int const penalty_factor, | |
550 | int size, int h, int flags) | |
1457ab52 | 551 | { |
af4091f1 | 552 | MotionEstContext * const c= &s->me; |
2750b827 | 553 | me_cmp_func cmpf, chroma_cmpf; |
1457ab52 | 554 | int next_dir=-1; |
bb198e19 | 555 | LOAD_COMMON |
2750b827 | 556 | LOAD_COMMON2 |
af4091f1 | 557 | int map_generation= c->map_generation; |
1457ab52 | 558 | |
2750b827 MN |
559 | cmpf= s->dsp.me_cmp[size]; |
560 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
1457ab52 | 561 | |
b07a5980 MN |
562 | { /* ensure that the best point is in the MAP as h/qpel refinement needs it */ |
563 | const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation; | |
564 | const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1); | |
565 | if(map[index]!=key){ //this will be executed only very rarey | |
2750b827 | 566 | score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags); |
b07a5980 MN |
567 | map[index]= key; |
568 | } | |
569 | } | |
570 | ||
1457ab52 MN |
571 | for(;;){ |
572 | int d; | |
573 | const int dir= next_dir; | |
574 | const int x= best[0]; | |
575 | const int y= best[1]; | |
576 | next_dir=-1; | |
577 | ||
578 | //printf("%d", dir); | |
579 | if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0) | |
580 | if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1) | |
581 | if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2) | |
582 | if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3) | |
583 | ||
584 | if(next_dir==-1){ | |
585 | return dmin; | |
586 | } | |
587 | } | |
588 | } | |
589 | ||
2750b827 MN |
590 | static int funny_diamond_search(MpegEncContext * s, int *best, int dmin, |
591 | int src_index, int ref_index, int const penalty_factor, | |
592 | int size, int h, int flags) | |
b07a5980 | 593 | { |
af4091f1 | 594 | MotionEstContext * const c= &s->me; |
2750b827 | 595 | me_cmp_func cmpf, chroma_cmpf; |
b07a5980 | 596 | int dia_size; |
bb198e19 | 597 | LOAD_COMMON |
2750b827 | 598 | LOAD_COMMON2 |
af4091f1 | 599 | int map_generation= c->map_generation; |
b07a5980 | 600 | |
2750b827 MN |
601 | cmpf= s->dsp.me_cmp[size]; |
602 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
b07a5980 MN |
603 | |
604 | for(dia_size=1; dia_size<=4; dia_size++){ | |
605 | int dir; | |
606 | const int x= best[0]; | |
607 | const int y= best[1]; | |
608 | ||
609 | if(dia_size&(dia_size-1)) continue; | |
610 | ||
611 | if( x + dia_size > xmax | |
612 | || x - dia_size < xmin | |
613 | || y + dia_size > ymax | |
614 | || y - dia_size < ymin) | |
615 | continue; | |
616 | ||
617 | for(dir= 0; dir<dia_size; dir+=2){ | |
618 | int d; | |
619 | ||
620 | CHECK_MV(x + dir , y + dia_size - dir); | |
621 | CHECK_MV(x + dia_size - dir, y - dir ); | |
622 | CHECK_MV(x - dir , y - dia_size + dir); | |
623 | CHECK_MV(x - dia_size + dir, y + dir ); | |
624 | } | |
625 | ||
626 | if(x!=best[0] || y!=best[1]) | |
627 | dia_size=0; | |
628 | #if 0 | |
629 | { | |
630 | int dx, dy, i; | |
631 | static int stats[8*8]; | |
632 | dx= ABS(x-best[0]); | |
633 | dy= ABS(y-best[1]); | |
634 | if(dy>dx){ | |
635 | dx^=dy; dy^=dx; dx^=dy; | |
636 | } | |
637 | stats[dy*8 + dx] ++; | |
638 | if(256*256*256*64 % (stats[0]+1)==0){ | |
639 | for(i=0; i<64; i++){ | |
640 | if((i&7)==0) printf("\n"); | |
641 | printf("%8d ", stats[i]); | |
642 | } | |
643 | printf("\n"); | |
644 | } | |
645 | } | |
646 | #endif | |
647 | } | |
648 | return dmin; | |
649 | } | |
650 | ||
651 | #define SAB_CHECK_MV(ax,ay)\ | |
652 | {\ | |
653 | const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\ | |
654 | const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\ | |
655 | /*printf("sab check %d %d\n", ax, ay);*/\ | |
656 | if(map[index]!=key){\ | |
2750b827 | 657 | d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
b07a5980 MN |
658 | map[index]= key;\ |
659 | score_map[index]= d;\ | |
660 | d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\ | |
661 | /*printf("score: %d\n", d);*/\ | |
662 | if(d < minima[minima_count-1].height){\ | |
663 | int j=0;\ | |
664 | \ | |
665 | while(d >= minima[j].height) j++;\ | |
666 | \ | |
667 | memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\ | |
668 | \ | |
669 | minima[j].checked= 0;\ | |
670 | minima[j].height= d;\ | |
671 | minima[j].x= ax;\ | |
672 | minima[j].y= ay;\ | |
673 | \ | |
674 | i=-1;\ | |
675 | continue;\ | |
676 | }\ | |
677 | }\ | |
678 | } | |
679 | ||
4994af2f | 680 | #define MAX_SAB_SIZE ME_MAP_SIZE |
2750b827 MN |
681 | static int sab_diamond_search(MpegEncContext * s, int *best, int dmin, |
682 | int src_index, int ref_index, int const penalty_factor, | |
683 | int size, int h, int flags) | |
b07a5980 | 684 | { |
af4091f1 | 685 | MotionEstContext * const c= &s->me; |
2750b827 | 686 | me_cmp_func cmpf, chroma_cmpf; |
b07a5980 | 687 | Minima minima[MAX_SAB_SIZE]; |
af4091f1 | 688 | const int minima_count= ABS(c->dia_size); |
b07a5980 | 689 | int i, j; |
bb198e19 | 690 | LOAD_COMMON |
2750b827 | 691 | LOAD_COMMON2 |
af4091f1 | 692 | int map_generation= c->map_generation; |
b07a5980 | 693 | |
2750b827 MN |
694 | cmpf= s->dsp.me_cmp[size]; |
695 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
b07a5980 MN |
696 | |
697 | for(j=i=0; i<ME_MAP_SIZE; i++){ | |
698 | uint32_t key= map[i]; | |
699 | ||
700 | key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1)); | |
701 | ||
702 | if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue; | |
703 | ||
704 | assert(j<MAX_SAB_SIZE); //max j = number of predictors | |
705 | ||
706 | minima[j].height= score_map[i]; | |
707 | minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS; | |
708 | minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1); | |
709 | minima[j].x-= (1<<(ME_MAP_MV_BITS-1)); | |
710 | minima[j].y-= (1<<(ME_MAP_MV_BITS-1)); | |
711 | minima[j].checked=0; | |
712 | if(minima[j].x || minima[j].y) | |
713 | minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor; | |
714 | ||
715 | j++; | |
716 | } | |
717 | ||
718 | qsort(minima, j, sizeof(Minima), minima_cmp); | |
719 | ||
720 | for(; j<minima_count; j++){ | |
721 | minima[j].height=256*256*256*64; | |
722 | minima[j].checked=0; | |
723 | minima[j].x= minima[j].y=0; | |
724 | } | |
725 | ||
726 | for(i=0; i<minima_count; i++){ | |
727 | const int x= minima[i].x; | |
728 | const int y= minima[i].y; | |
729 | int d; | |
730 | ||
731 | if(minima[i].checked) continue; | |
732 | ||
733 | if( x >= xmax || x <= xmin | |
734 | || y >= ymax || y <= ymin) | |
735 | continue; | |
736 | ||
737 | SAB_CHECK_MV(x-1, y) | |
738 | SAB_CHECK_MV(x+1, y) | |
739 | SAB_CHECK_MV(x , y-1) | |
740 | SAB_CHECK_MV(x , y+1) | |
741 | ||
742 | minima[i].checked= 1; | |
743 | } | |
744 | ||
745 | best[0]= minima[0].x; | |
746 | best[1]= minima[0].y; | |
747 | dmin= minima[0].height; | |
748 | ||
749 | if( best[0] < xmax && best[0] > xmin | |
750 | && best[1] < ymax && best[1] > ymin){ | |
751 | int d; | |
752 | //ensure that the refernece samples for hpel refinement are in the map | |
753 | CHECK_MV(best[0]-1, best[1]) | |
754 | CHECK_MV(best[0]+1, best[1]) | |
755 | CHECK_MV(best[0], best[1]-1) | |
756 | CHECK_MV(best[0], best[1]+1) | |
757 | } | |
758 | return dmin; | |
759 | } | |
760 | ||
2750b827 MN |
761 | static int var_diamond_search(MpegEncContext * s, int *best, int dmin, |
762 | int src_index, int ref_index, int const penalty_factor, | |
763 | int size, int h, int flags) | |
1457ab52 | 764 | { |
af4091f1 | 765 | MotionEstContext * const c= &s->me; |
2750b827 | 766 | me_cmp_func cmpf, chroma_cmpf; |
b07a5980 | 767 | int dia_size; |
bb198e19 | 768 | LOAD_COMMON |
2750b827 | 769 | LOAD_COMMON2 |
af4091f1 | 770 | int map_generation= c->map_generation; |
1457ab52 | 771 | |
2750b827 MN |
772 | cmpf= s->dsp.me_cmp[size]; |
773 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
1457ab52 | 774 | |
af4091f1 | 775 | for(dia_size=1; dia_size<=c->dia_size; dia_size++){ |
1457ab52 MN |
776 | int dir, start, end; |
777 | const int x= best[0]; | |
778 | const int y= best[1]; | |
779 | ||
780 | start= FFMAX(0, y + dia_size - ymax); | |
b07a5980 | 781 | end = FFMIN(dia_size, xmax - x + 1); |
1457ab52 MN |
782 | for(dir= start; dir<end; dir++){ |
783 | int d; | |
784 | ||
785 | //check(x + dir,y + dia_size - dir,0, a0) | |
786 | CHECK_MV(x + dir , y + dia_size - dir); | |
787 | } | |
788 | ||
789 | start= FFMAX(0, x + dia_size - xmax); | |
b07a5980 | 790 | end = FFMIN(dia_size, y - ymin + 1); |
1457ab52 MN |
791 | for(dir= start; dir<end; dir++){ |
792 | int d; | |
793 | ||
794 | //check(x + dia_size - dir, y - dir,0, a1) | |
795 | CHECK_MV(x + dia_size - dir, y - dir ); | |
796 | } | |
797 | ||
798 | start= FFMAX(0, -y + dia_size + ymin ); | |
b07a5980 | 799 | end = FFMIN(dia_size, x - xmin + 1); |
1457ab52 MN |
800 | for(dir= start; dir<end; dir++){ |
801 | int d; | |
802 | ||
803 | //check(x - dir,y - dia_size + dir,0, a2) | |
804 | CHECK_MV(x - dir , y - dia_size + dir); | |
805 | } | |
806 | ||
807 | start= FFMAX(0, -x + dia_size + xmin ); | |
b07a5980 | 808 | end = FFMIN(dia_size, ymax - y + 1); |
1457ab52 MN |
809 | for(dir= start; dir<end; dir++){ |
810 | int d; | |
811 | ||
812 | //check(x - dia_size + dir, y + dir,0, a3) | |
813 | CHECK_MV(x - dia_size + dir, y + dir ); | |
814 | } | |
815 | ||
816 | if(x!=best[0] || y!=best[1]) | |
817 | dia_size=0; | |
b07a5980 MN |
818 | #if 0 |
819 | { | |
820 | int dx, dy, i; | |
821 | static int stats[8*8]; | |
822 | dx= ABS(x-best[0]); | |
823 | dy= ABS(y-best[1]); | |
824 | stats[dy*8 + dx] ++; | |
825 | if(256*256*256*64 % (stats[0]+1)==0){ | |
826 | for(i=0; i<64; i++){ | |
827 | if((i&7)==0) printf("\n"); | |
828 | printf("%6d ", stats[i]); | |
829 | } | |
830 | printf("\n"); | |
831 | } | |
832 | } | |
833 | #endif | |
1457ab52 MN |
834 | } |
835 | return dmin; | |
836 | } | |
837 | ||
2750b827 MN |
838 | static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin, |
839 | int src_index, int ref_index, int const penalty_factor, | |
840 | int size, int h, int flags){ | |
af4091f1 MN |
841 | MotionEstContext * const c= &s->me; |
842 | if(c->dia_size==-1) | |
2750b827 | 843 | return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
af4091f1 | 844 | else if(c->dia_size<-1) |
2750b827 | 845 | return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
af4091f1 | 846 | else if(c->dia_size<2) |
2750b827 MN |
847 | return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
848 | else | |
849 | return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); | |
850 | } | |
851 | ||
852 | static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr, | |
853 | int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], | |
854 | int ref_mv_scale, int flags) | |
1457ab52 | 855 | { |
af4091f1 | 856 | MotionEstContext * const c= &s->me; |
1457ab52 | 857 | int best[2]={0, 0}; |
2750b827 | 858 | int d, dmin; |
1457ab52 | 859 | int map_generation; |
af4091f1 | 860 | const int penalty_factor= c->penalty_factor; |
1457ab52 | 861 | const int size=0; |
bb198e19 MN |
862 | const int h=16; |
863 | const int ref_mv_stride= s->mb_stride; //pass as arg FIXME | |
864 | const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME | |
2750b827 MN |
865 | me_cmp_func cmpf, chroma_cmpf; |
866 | ||
bb198e19 | 867 | LOAD_COMMON |
2750b827 | 868 | LOAD_COMMON2 |
1457ab52 | 869 | |
2750b827 MN |
870 | cmpf= s->dsp.me_cmp[size]; |
871 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
1457ab52 | 872 | |
af4091f1 | 873 | map_generation= update_map_generation(c); |
1457ab52 | 874 | |
2750b827 | 875 | dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags); |
1457ab52 MN |
876 | map[0]= map_generation; |
877 | score_map[0]= dmin; | |
878 | ||
879 | /* first line */ | |
9c3d33d6 | 880 | if (s->first_slice_line) { |
1457ab52 | 881 | CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
b07a5980 MN |
882 | CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
883 | (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
1457ab52 MN |
884 | }else{ |
885 | if(dmin<256 && ( P_LEFT[0] |P_LEFT[1] | |
886 | |P_TOP[0] |P_TOP[1] | |
b07a5980 | 887 | |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){ |
1457ab52 MN |
888 | *mx_ptr= 0; |
889 | *my_ptr= 0; | |
af4091f1 | 890 | c->skip=1; |
1457ab52 MN |
891 | return dmin; |
892 | } | |
893 | CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) | |
894 | if(dmin>256*2){ | |
b07a5980 MN |
895 | CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
896 | (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
1457ab52 MN |
897 | CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift) |
898 | CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift) | |
899 | CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) | |
900 | } | |
901 | } | |
902 | if(dmin>256*4){ | |
af4091f1 | 903 | if(c->pre_pass){ |
f931ff7b MN |
904 | CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, |
905 | (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16) | |
9c3d33d6 MN |
906 | if(!s->first_slice_line) |
907 | CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, | |
908 | (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
f931ff7b MN |
909 | }else{ |
910 | CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, | |
911 | (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) | |
80ee9fc0 | 912 | if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line |
9c3d33d6 MN |
913 | CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, |
914 | (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
f931ff7b | 915 | } |
1457ab52 | 916 | } |
b07a5980 | 917 | |
1f202b0d MN |
918 | if(c->avctx->last_predictor_count){ |
919 | const int count= c->avctx->last_predictor_count; | |
b07a5980 MN |
920 | const int xstart= FFMAX(0, s->mb_x - count); |
921 | const int ystart= FFMAX(0, s->mb_y - count); | |
922 | const int xend= FFMIN(s->mb_width , s->mb_x + count + 1); | |
923 | const int yend= FFMIN(s->mb_height, s->mb_y + count + 1); | |
924 | int mb_y; | |
925 | ||
926 | for(mb_y=ystart; mb_y<yend; mb_y++){ | |
927 | int mb_x; | |
928 | for(mb_x=xstart; mb_x<xend; mb_x++){ | |
929 | const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride; | |
930 | int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16; | |
931 | int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16; | |
932 | ||
933 | if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue; | |
934 | CHECK_MV(mx,my) | |
1457ab52 MN |
935 | } |
936 | } | |
937 | } | |
b07a5980 | 938 | |
1457ab52 | 939 | //check(best[0],best[1],0, b0) |
2750b827 | 940 | dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
1457ab52 MN |
941 | |
942 | //check(best[0],best[1],0, b1) | |
943 | *mx_ptr= best[0]; | |
944 | *my_ptr= best[1]; | |
945 | ||
946 | // printf("%d %d %d \n", best[0], best[1], dmin); | |
947 | return dmin; | |
948 | } | |
949 | ||
2750b827 MN |
950 | //this function is dedicated to the braindamaged gcc |
951 | static inline int epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, | |
952 | int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], | |
953 | int ref_mv_scale) | |
954 | { | |
af4091f1 | 955 | MotionEstContext * const c= &s->me; |
2750b827 | 956 | //FIXME convert other functions in the same way if faster |
af4091f1 | 957 | switch(c->flags){ |
2750b827 MN |
958 | case 0: |
959 | return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0); | |
960 | // case FLAG_QPEL: | |
961 | // return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL); | |
962 | default: | |
af4091f1 | 963 | return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags); |
2750b827 MN |
964 | } |
965 | } | |
966 | ||
967 | static int epzs_motion_search4(MpegEncContext * s, | |
968 | int *mx_ptr, int *my_ptr, int P[10][2], | |
969 | int src_index, int ref_index, int16_t (*last_mv)[2], | |
970 | int ref_mv_scale) | |
1457ab52 | 971 | { |
af4091f1 | 972 | MotionEstContext * const c= &s->me; |
1457ab52 MN |
973 | int best[2]={0, 0}; |
974 | int d, dmin; | |
1457ab52 | 975 | int map_generation; |
af4091f1 | 976 | const int penalty_factor= c->penalty_factor; |
1457ab52 | 977 | const int size=1; |
bb198e19 | 978 | const int h=8; |
7bc9090a MN |
979 | const int ref_mv_stride= s->mb_stride; |
980 | const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; | |
2750b827 | 981 | me_cmp_func cmpf, chroma_cmpf; |
bb198e19 | 982 | LOAD_COMMON |
af4091f1 | 983 | int flags= c->flags; |
2750b827 | 984 | LOAD_COMMON2 |
1457ab52 | 985 | |
2750b827 MN |
986 | cmpf= s->dsp.me_cmp[size]; |
987 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
1457ab52 | 988 | |
af4091f1 | 989 | map_generation= update_map_generation(c); |
1457ab52 MN |
990 | |
991 | dmin = 1000000; | |
992 | //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); | |
993 | /* first line */ | |
9c3d33d6 | 994 | if (s->first_slice_line) { |
1457ab52 | 995 | CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
b07a5980 MN |
996 | CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
997 | (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
1457ab52 MN |
998 | CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) |
999 | }else{ | |
1000 | CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
1001 | //FIXME try some early stop | |
1002 | if(dmin>64*2){ | |
1003 | CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) | |
1004 | CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) | |
1005 | CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) | |
1006 | CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) | |
b07a5980 MN |
1007 | CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
1008 | (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
1457ab52 MN |
1009 | } |
1010 | } | |
1011 | if(dmin>64*4){ | |
b07a5980 MN |
1012 | CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, |
1013 | (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) | |
80ee9fc0 | 1014 | if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line |
9c3d33d6 MN |
1015 | CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, |
1016 | (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
1457ab52 MN |
1017 | } |
1018 | ||
2750b827 | 1019 | dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
bb198e19 MN |
1020 | |
1021 | *mx_ptr= best[0]; | |
1022 | *my_ptr= best[1]; | |
1023 | ||
1024 | // printf("%d %d %d \n", best[0], best[1], dmin); | |
1025 | return dmin; | |
1026 | } | |
1027 | ||
1028 | //try to merge with above FIXME (needs PSNR test) | |
2750b827 MN |
1029 | static int epzs_motion_search2(MpegEncContext * s, |
1030 | int *mx_ptr, int *my_ptr, int P[10][2], | |
1031 | int src_index, int ref_index, int16_t (*last_mv)[2], | |
1032 | int ref_mv_scale) | |
bb198e19 | 1033 | { |
af4091f1 | 1034 | MotionEstContext * const c= &s->me; |
bb198e19 MN |
1035 | int best[2]={0, 0}; |
1036 | int d, dmin; | |
bb198e19 | 1037 | int map_generation; |
af4091f1 | 1038 | const int penalty_factor= c->penalty_factor; |
bb198e19 MN |
1039 | const int size=0; //FIXME pass as arg |
1040 | const int h=8; | |
1041 | const int ref_mv_stride= s->mb_stride; | |
1042 | const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; | |
2750b827 | 1043 | me_cmp_func cmpf, chroma_cmpf; |
bb198e19 | 1044 | LOAD_COMMON |
af4091f1 | 1045 | int flags= c->flags; |
2750b827 | 1046 | LOAD_COMMON2 |
bb198e19 | 1047 | |
2750b827 MN |
1048 | cmpf= s->dsp.me_cmp[size]; |
1049 | chroma_cmpf= s->dsp.me_cmp[size+1]; | |
bb198e19 | 1050 | |
af4091f1 | 1051 | map_generation= update_map_generation(c); |
bb198e19 MN |
1052 | |
1053 | dmin = 1000000; | |
1054 | //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); | |
1055 | /* first line */ | |
9c3d33d6 | 1056 | if (s->first_slice_line) { |
bb198e19 MN |
1057 | CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
1058 | CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, | |
1059 | (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
1060 | CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
1061 | }else{ | |
1062 | CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
1063 | //FIXME try some early stop | |
1064 | if(dmin>64*2){ | |
1065 | CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) | |
1066 | CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) | |
1067 | CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) | |
1068 | CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) | |
1069 | CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, | |
1070 | (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
1071 | } | |
1072 | } | |
1073 | if(dmin>64*4){ | |
1074 | CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, | |
1075 | (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) | |
80ee9fc0 | 1076 | if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line |
9c3d33d6 MN |
1077 | CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, |
1078 | (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
bb198e19 MN |
1079 | } |
1080 | ||
2750b827 | 1081 | dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
b07a5980 | 1082 | |
1457ab52 MN |
1083 | *mx_ptr= best[0]; |
1084 | *my_ptr= best[1]; | |
1085 | ||
1086 | // printf("%d %d %d \n", best[0], best[1], dmin); | |
1087 | return dmin; | |
1088 | } |