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