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