Mercurial > libavcodec.hg
annotate motion_est_template.c @ 2015:3ab8f3e2ae6a libavcodec
moving motion estimation specific variables from MpegEncContext -> MotionEstContext
| author | michael |
|---|---|
| date | Tue, 11 May 2004 01:38:46 +0000 |
| parents | 15c885db82a8 |
| children | c200977cdf78 |
| 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 | |
| 1950 | 224 static int inline get_mb_score(MpegEncContext * s, int mx, int my, int src_index, |
| 225 int ref_index) | |
| 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; |
| 1013 | 229 const int size= 0; |
| 1708 | 230 const int h= 16; |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
231 const int penalty_factor= c->mb_penalty_factor; |
|
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
232 const int flags= c->mb_flags; |
| 1950 | 233 const int qpel= flags & FLAG_QPEL; |
| 234 const int mask= 1+2*qpel; | |
| 1013 | 235 me_cmp_func cmp_sub, chroma_cmp_sub; |
| 236 int d; | |
| 237 | |
| 1708 | 238 LOAD_COMMON |
| 1013 | 239 |
| 240 //FIXME factorize | |
| 241 | |
| 242 cmp_sub= s->dsp.mb_cmp[size]; | |
| 243 chroma_cmp_sub= s->dsp.mb_cmp[size+1]; | |
| 244 | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
245 assert(!c->skip); |
|
2015
3ab8f3e2ae6a
moving motion estimation specific variables from MpegEncContext -> MotionEstContext
michael
parents:
2014
diff
changeset
|
246 assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp); |
| 1013 | 247 |
| 1950 | 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); |
| 1013 | 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 | |
| 936 | 256 #define CHECK_QUARTER_MV(dx, dy, x, y)\ |
| 257 {\ | |
| 258 const int hx= 4*(x)+(dx);\ | |
| 259 const int hy= 4*(y)+(dy);\ | |
| 1950 | 260 d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
| 936 | 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 | |
| 1950 | 265 static int qpel_motion_search(MpegEncContext * s, |
| 936 | 266 int *mx_ptr, int *my_ptr, int dmin, |
| 1950 | 267 int src_index, int ref_index, |
| 268 int size, int h) | |
| 936 | 269 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
270 MotionEstContext * const c= &s->me; |
| 936 | 271 const int mx = *mx_ptr; |
| 272 const int my = *my_ptr; | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
273 const int penalty_factor= c->sub_penalty_factor; |
|
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
274 const int map_generation= c->map_generation; |
|
2015
3ab8f3e2ae6a
moving motion estimation specific variables from MpegEncContext -> MotionEstContext
michael
parents:
2014
diff
changeset
|
275 const int subpel_quality= c->avctx->me_subpel_quality; |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
276 uint32_t *map= c->map; |
| 1950 | 277 me_cmp_func cmpf, chroma_cmpf; |
| 936 | 278 me_cmp_func cmp_sub, chroma_cmp_sub; |
| 279 | |
| 1708 | 280 LOAD_COMMON |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
281 int flags= c->sub_flags; |
| 936 | 282 |
| 1950 | 283 cmpf= s->dsp.me_cmp[size]; |
| 284 chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME | |
| 936 | 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 | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
290 if(c->skip){ //FIXME somehow move up (benchmark) |
| 936 | 291 *mx_ptr = 0; |
| 292 *my_ptr = 0; | |
| 293 return dmin; | |
| 294 } | |
| 295 | |
|
2015
3ab8f3e2ae6a
moving motion estimation specific variables from MpegEncContext -> MotionEstContext
michael
parents:
2014
diff
changeset
|
296 if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ |
| 1950 | 297 dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); |
| 1011 | 298 if(mx || my || size>0) |
| 936 | 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 | |
| 954 | 318 if(s->me.dia_size>=2){ |
| 936 | 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++){ | |
| 326 const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t; | |
| 327 const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c; | |
| 328 const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b; | |
| 329 int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2; | |
| 330 int i; | |
| 331 | |
| 332 if((nx&3)==0 && (ny&3)==0) continue; | |
| 333 | |
| 334 score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor; | |
| 335 | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
336 // if(nx&1) score-=1024*c->penalty_factor; |
|
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
337 // if(ny&1) score-=1024*c->penalty_factor; |
| 936 | 338 |
| 339 for(i=0; i<8; i++){ | |
| 340 if(score < best[i]){ | |
| 341 memmove(&best[i+1], &best[i], sizeof(int)*(7-i)); | |
| 342 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i)); | |
| 343 best[i]= score; | |
| 344 best_pos[i][0]= nx + 4*mx; | |
| 345 best_pos[i][1]= ny + 4*my; | |
| 346 break; | |
| 347 } | |
| 348 } | |
| 349 } | |
| 350 } | |
| 351 }else{ | |
| 352 int tl; | |
| 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++){ | |
| 375 int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor | |
| 376 int i; | |
| 377 | |
| 378 if((nx&3)==0 && (ny&3)==0) continue; | |
| 379 | |
| 380 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
|
381 // if(nx&1) score-=32*c->penalty_factor; |
|
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
382 // if(ny&1) score-=32*c->penalty_factor; |
| 936 | 383 |
| 384 for(i=0; i<8; i++){ | |
| 385 if(score < best[i]){ | |
| 386 memmove(&best[i+1], &best[i], sizeof(int)*(7-i)); | |
| 387 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i)); | |
| 388 best[i]= score; | |
| 389 best_pos[i][0]= nx + 4*mx; | |
| 390 best_pos[i][1]= ny + 4*my; | |
| 391 break; | |
| 392 } | |
| 393 } | |
| 394 } | |
| 395 } | |
| 396 } | |
| 954 | 397 for(i=0; i<subpel_quality; i++){ |
| 936 | 398 nx= best_pos[i][0]; |
| 399 ny= best_pos[i][1]; | |
| 400 CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2) | |
| 401 } | |
| 954 | 402 |
| 936 | 403 #if 0 |
| 954 | 404 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; |
| 405 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)]; | |
| 406 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)]; | |
| 407 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)]; | |
| 408 // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){ | |
| 409 if(tl<br){ | |
| 410 | |
| 411 // nx= FFMAX(4*mx - bx, bx - 4*mx); | |
| 412 // ny= FFMAX(4*my - by, by - 4*my); | |
| 936 | 413 |
| 954 | 414 static int stats[7][7], count; |
| 415 count++; | |
| 416 stats[4*mx - bx + 3][4*my - by + 3]++; | |
| 417 if(256*256*256*64 % count ==0){ | |
| 418 for(i=0; i<49; i++){ | |
| 419 if((i%7)==0) printf("\n"); | |
| 936 | 420 printf("%6d ", stats[0][i]); |
| 421 } | |
| 422 printf("\n"); | |
| 423 } | |
| 954 | 424 } |
| 936 | 425 #endif |
| 426 #else | |
| 427 | |
| 428 CHECK_QUARTER_MV(2, 2, mx-1, my-1) | |
| 429 CHECK_QUARTER_MV(0, 2, mx , my-1) | |
| 430 CHECK_QUARTER_MV(2, 2, mx , my-1) | |
| 431 CHECK_QUARTER_MV(2, 0, mx , my ) | |
| 432 CHECK_QUARTER_MV(2, 2, mx , my ) | |
| 433 CHECK_QUARTER_MV(0, 2, mx , my ) | |
| 434 CHECK_QUARTER_MV(2, 2, mx-1, my ) | |
| 435 CHECK_QUARTER_MV(2, 0, mx-1, my ) | |
| 436 | |
| 437 nx= bx; | |
| 438 ny= by; | |
| 439 | |
| 440 for(i=0; i<8; i++){ | |
| 441 int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1}; | |
| 442 int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1}; | |
| 443 CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2) | |
| 444 } | |
| 445 #endif | |
| 446 #if 0 | |
| 447 //outer ring | |
| 448 CHECK_QUARTER_MV(1, 3, mx-1, my-1) | |
| 449 CHECK_QUARTER_MV(1, 2, mx-1, my-1) | |
| 450 CHECK_QUARTER_MV(1, 1, mx-1, my-1) | |
| 451 CHECK_QUARTER_MV(2, 1, mx-1, my-1) | |
| 452 CHECK_QUARTER_MV(3, 1, mx-1, my-1) | |
| 453 CHECK_QUARTER_MV(0, 1, mx , my-1) | |
| 454 CHECK_QUARTER_MV(1, 1, mx , my-1) | |
| 455 CHECK_QUARTER_MV(2, 1, mx , my-1) | |
| 456 CHECK_QUARTER_MV(3, 1, mx , my-1) | |
| 457 CHECK_QUARTER_MV(3, 2, mx , my-1) | |
| 458 CHECK_QUARTER_MV(3, 3, mx , my-1) | |
| 459 CHECK_QUARTER_MV(3, 0, mx , my ) | |
| 460 CHECK_QUARTER_MV(3, 1, mx , my ) | |
| 461 CHECK_QUARTER_MV(3, 2, mx , my ) | |
| 462 CHECK_QUARTER_MV(3, 3, mx , my ) | |
| 463 CHECK_QUARTER_MV(2, 3, mx , my ) | |
| 464 CHECK_QUARTER_MV(1, 3, mx , my ) | |
| 465 CHECK_QUARTER_MV(0, 3, mx , my ) | |
| 466 CHECK_QUARTER_MV(3, 3, mx-1, my ) | |
| 467 CHECK_QUARTER_MV(2, 3, mx-1, my ) | |
| 468 CHECK_QUARTER_MV(1, 3, mx-1, my ) | |
| 469 CHECK_QUARTER_MV(1, 2, mx-1, my ) | |
| 470 CHECK_QUARTER_MV(1, 1, mx-1, my ) | |
| 471 CHECK_QUARTER_MV(1, 0, mx-1, my ) | |
| 472 #endif | |
| 473 assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4); | |
| 474 | |
| 475 *mx_ptr = bx; | |
| 476 *my_ptr = by; | |
| 477 }else{ | |
| 478 *mx_ptr =4*mx; | |
| 479 *my_ptr =4*my; | |
| 480 } | |
| 481 | |
| 482 return dmin; | |
| 483 } | |
| 484 | |
| 485 | |
| 486 #define CHECK_MV(x,y)\ | |
| 487 {\ | |
| 488 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ | |
| 489 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ | |
| 948 | 490 /*printf("check_mv %d %d\n", x, y);*/\ |
| 936 | 491 if(map[index]!=key){\ |
| 1950 | 492 d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
| 936 | 493 map[index]= key;\ |
| 494 score_map[index]= d;\ | |
| 495 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\ | |
| 948 | 496 /*printf("score:%d\n", d);*/\ |
| 936 | 497 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\ |
| 498 }\ | |
| 499 } | |
| 500 | |
| 948 | 501 #define CHECK_CLIPED_MV(ax,ay)\ |
| 502 {\ | |
| 1799 | 503 const int x= ax;\ |
| 504 const int y= ay;\ | |
| 505 const int x2= FFMAX(xmin, FFMIN(x, xmax));\ | |
| 506 const int y2= FFMAX(ymin, FFMIN(y, ymax));\ | |
| 507 CHECK_MV(x2, y2)\ | |
| 948 | 508 } |
| 509 | |
| 936 | 510 #define CHECK_MV_DIR(x,y,new_dir)\ |
| 511 {\ | |
| 512 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ | |
| 513 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ | |
| 948 | 514 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\ |
| 936 | 515 if(map[index]!=key){\ |
| 1950 | 516 d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
| 936 | 517 map[index]= key;\ |
| 518 score_map[index]= d;\ | |
| 519 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\ | |
| 948 | 520 /*printf("score:%d\n", d);*/\ |
| 936 | 521 if(d<dmin){\ |
| 522 best[0]=x;\ | |
| 523 best[1]=y;\ | |
| 524 dmin=d;\ | |
| 525 next_dir= new_dir;\ | |
| 526 }\ | |
| 527 }\ | |
| 528 } | |
| 529 | |
| 530 #define check(x,y,S,v)\ | |
| 531 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\ | |
| 532 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\ | |
| 533 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\ | |
| 534 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\ | |
| 535 | |
| 1950 | 536 #define LOAD_COMMON2\ |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
537 uint32_t *map= c->map;\ |
| 1950 | 538 const int qpel= flags&FLAG_QPEL;\ |
| 539 const int shift= 1+qpel;\ | |
| 936 | 540 |
| 1950 | 541 static always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin, |
| 542 int src_index, int ref_index, int const penalty_factor, | |
| 543 int size, int h, int flags) | |
| 936 | 544 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
545 MotionEstContext * const c= &s->me; |
| 1950 | 546 me_cmp_func cmpf, chroma_cmpf; |
| 936 | 547 int next_dir=-1; |
| 1708 | 548 LOAD_COMMON |
| 1950 | 549 LOAD_COMMON2 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
550 int map_generation= c->map_generation; |
| 936 | 551 |
| 1950 | 552 cmpf= s->dsp.me_cmp[size]; |
| 553 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 936 | 554 |
| 948 | 555 { /* ensure that the best point is in the MAP as h/qpel refinement needs it */ |
| 556 const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation; | |
| 557 const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1); | |
| 558 if(map[index]!=key){ //this will be executed only very rarey | |
| 1950 | 559 score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags); |
| 948 | 560 map[index]= key; |
| 561 } | |
| 562 } | |
| 563 | |
| 936 | 564 for(;;){ |
| 565 int d; | |
| 566 const int dir= next_dir; | |
| 567 const int x= best[0]; | |
| 568 const int y= best[1]; | |
| 569 next_dir=-1; | |
| 570 | |
| 571 //printf("%d", dir); | |
| 572 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0) | |
| 573 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1) | |
| 574 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2) | |
| 575 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3) | |
| 576 | |
| 577 if(next_dir==-1){ | |
| 578 return dmin; | |
| 579 } | |
| 580 } | |
| 581 } | |
| 582 | |
| 1950 | 583 static int funny_diamond_search(MpegEncContext * s, int *best, int dmin, |
| 584 int src_index, int ref_index, int const penalty_factor, | |
| 585 int size, int h, int flags) | |
| 948 | 586 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
587 MotionEstContext * const c= &s->me; |
| 1950 | 588 me_cmp_func cmpf, chroma_cmpf; |
| 948 | 589 int dia_size; |
| 1708 | 590 LOAD_COMMON |
| 1950 | 591 LOAD_COMMON2 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
592 int map_generation= c->map_generation; |
| 948 | 593 |
| 1950 | 594 cmpf= s->dsp.me_cmp[size]; |
| 595 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 948 | 596 |
| 597 for(dia_size=1; dia_size<=4; dia_size++){ | |
| 598 int dir; | |
| 599 const int x= best[0]; | |
| 600 const int y= best[1]; | |
| 601 | |
| 602 if(dia_size&(dia_size-1)) continue; | |
| 603 | |
| 604 if( x + dia_size > xmax | |
| 605 || x - dia_size < xmin | |
| 606 || y + dia_size > ymax | |
| 607 || y - dia_size < ymin) | |
| 608 continue; | |
| 609 | |
| 610 for(dir= 0; dir<dia_size; dir+=2){ | |
| 611 int d; | |
| 612 | |
| 613 CHECK_MV(x + dir , y + dia_size - dir); | |
| 614 CHECK_MV(x + dia_size - dir, y - dir ); | |
| 615 CHECK_MV(x - dir , y - dia_size + dir); | |
| 616 CHECK_MV(x - dia_size + dir, y + dir ); | |
| 617 } | |
| 618 | |
| 619 if(x!=best[0] || y!=best[1]) | |
| 620 dia_size=0; | |
| 621 #if 0 | |
| 622 { | |
| 623 int dx, dy, i; | |
| 624 static int stats[8*8]; | |
| 625 dx= ABS(x-best[0]); | |
| 626 dy= ABS(y-best[1]); | |
| 627 if(dy>dx){ | |
| 628 dx^=dy; dy^=dx; dx^=dy; | |
| 629 } | |
| 630 stats[dy*8 + dx] ++; | |
| 631 if(256*256*256*64 % (stats[0]+1)==0){ | |
| 632 for(i=0; i<64; i++){ | |
| 633 if((i&7)==0) printf("\n"); | |
| 634 printf("%8d ", stats[i]); | |
| 635 } | |
| 636 printf("\n"); | |
| 637 } | |
| 638 } | |
| 639 #endif | |
| 640 } | |
| 641 return dmin; | |
| 642 } | |
| 643 | |
| 644 #define SAB_CHECK_MV(ax,ay)\ | |
| 645 {\ | |
| 646 const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\ | |
| 647 const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\ | |
| 648 /*printf("sab check %d %d\n", ax, ay);*/\ | |
| 649 if(map[index]!=key){\ | |
| 1950 | 650 d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ |
| 948 | 651 map[index]= key;\ |
| 652 score_map[index]= d;\ | |
| 653 d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\ | |
| 654 /*printf("score: %d\n", d);*/\ | |
| 655 if(d < minima[minima_count-1].height){\ | |
| 656 int j=0;\ | |
| 657 \ | |
| 658 while(d >= minima[j].height) j++;\ | |
| 659 \ | |
| 660 memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\ | |
| 661 \ | |
| 662 minima[j].checked= 0;\ | |
| 663 minima[j].height= d;\ | |
| 664 minima[j].x= ax;\ | |
| 665 minima[j].y= ay;\ | |
| 666 \ | |
| 667 i=-1;\ | |
| 668 continue;\ | |
| 669 }\ | |
| 670 }\ | |
| 671 } | |
| 672 | |
| 673 #define MAX_SAB_SIZE 16 | |
| 1950 | 674 static int sab_diamond_search(MpegEncContext * s, int *best, int dmin, |
| 675 int src_index, int ref_index, int const penalty_factor, | |
| 676 int size, int h, int flags) | |
| 948 | 677 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
678 MotionEstContext * const c= &s->me; |
| 1950 | 679 me_cmp_func cmpf, chroma_cmpf; |
| 948 | 680 Minima minima[MAX_SAB_SIZE]; |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
681 const int minima_count= ABS(c->dia_size); |
| 948 | 682 int i, j; |
| 1708 | 683 LOAD_COMMON |
| 1950 | 684 LOAD_COMMON2 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
685 int map_generation= c->map_generation; |
| 948 | 686 |
| 1950 | 687 cmpf= s->dsp.me_cmp[size]; |
| 688 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 948 | 689 |
| 690 for(j=i=0; i<ME_MAP_SIZE; i++){ | |
| 691 uint32_t key= map[i]; | |
| 692 | |
| 693 key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1)); | |
| 694 | |
| 695 if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue; | |
| 696 | |
| 697 assert(j<MAX_SAB_SIZE); //max j = number of predictors | |
| 698 | |
| 699 minima[j].height= score_map[i]; | |
| 700 minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS; | |
| 701 minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1); | |
| 702 minima[j].x-= (1<<(ME_MAP_MV_BITS-1)); | |
| 703 minima[j].y-= (1<<(ME_MAP_MV_BITS-1)); | |
| 704 minima[j].checked=0; | |
| 705 if(minima[j].x || minima[j].y) | |
| 706 minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor; | |
| 707 | |
| 708 j++; | |
| 709 } | |
| 710 | |
| 711 qsort(minima, j, sizeof(Minima), minima_cmp); | |
| 712 | |
| 713 for(; j<minima_count; j++){ | |
| 714 minima[j].height=256*256*256*64; | |
| 715 minima[j].checked=0; | |
| 716 minima[j].x= minima[j].y=0; | |
| 717 } | |
| 718 | |
| 719 for(i=0; i<minima_count; i++){ | |
| 720 const int x= minima[i].x; | |
| 721 const int y= minima[i].y; | |
| 722 int d; | |
| 723 | |
| 724 if(minima[i].checked) continue; | |
| 725 | |
| 726 if( x >= xmax || x <= xmin | |
| 727 || y >= ymax || y <= ymin) | |
| 728 continue; | |
| 729 | |
| 730 SAB_CHECK_MV(x-1, y) | |
| 731 SAB_CHECK_MV(x+1, y) | |
| 732 SAB_CHECK_MV(x , y-1) | |
| 733 SAB_CHECK_MV(x , y+1) | |
| 734 | |
| 735 minima[i].checked= 1; | |
| 736 } | |
| 737 | |
| 738 best[0]= minima[0].x; | |
| 739 best[1]= minima[0].y; | |
| 740 dmin= minima[0].height; | |
| 741 | |
| 742 if( best[0] < xmax && best[0] > xmin | |
| 743 && best[1] < ymax && best[1] > ymin){ | |
| 744 int d; | |
| 745 //ensure that the refernece samples for hpel refinement are in the map | |
| 746 CHECK_MV(best[0]-1, best[1]) | |
| 747 CHECK_MV(best[0]+1, best[1]) | |
| 748 CHECK_MV(best[0], best[1]-1) | |
| 749 CHECK_MV(best[0], best[1]+1) | |
| 750 } | |
| 751 return dmin; | |
| 752 } | |
| 753 | |
| 1950 | 754 static int var_diamond_search(MpegEncContext * s, int *best, int dmin, |
| 755 int src_index, int ref_index, int const penalty_factor, | |
| 756 int size, int h, int flags) | |
| 936 | 757 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
758 MotionEstContext * const c= &s->me; |
| 1950 | 759 me_cmp_func cmpf, chroma_cmpf; |
| 948 | 760 int dia_size; |
| 1708 | 761 LOAD_COMMON |
| 1950 | 762 LOAD_COMMON2 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
763 int map_generation= c->map_generation; |
| 936 | 764 |
| 1950 | 765 cmpf= s->dsp.me_cmp[size]; |
| 766 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 936 | 767 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
768 for(dia_size=1; dia_size<=c->dia_size; dia_size++){ |
| 936 | 769 int dir, start, end; |
| 770 const int x= best[0]; | |
| 771 const int y= best[1]; | |
| 772 | |
| 773 start= FFMAX(0, y + dia_size - ymax); | |
| 948 | 774 end = FFMIN(dia_size, xmax - x + 1); |
| 936 | 775 for(dir= start; dir<end; dir++){ |
| 776 int d; | |
| 777 | |
| 778 //check(x + dir,y + dia_size - dir,0, a0) | |
| 779 CHECK_MV(x + dir , y + dia_size - dir); | |
| 780 } | |
| 781 | |
| 782 start= FFMAX(0, x + dia_size - xmax); | |
| 948 | 783 end = FFMIN(dia_size, y - ymin + 1); |
| 936 | 784 for(dir= start; dir<end; dir++){ |
| 785 int d; | |
| 786 | |
| 787 //check(x + dia_size - dir, y - dir,0, a1) | |
| 788 CHECK_MV(x + dia_size - dir, y - dir ); | |
| 789 } | |
| 790 | |
| 791 start= FFMAX(0, -y + dia_size + ymin ); | |
| 948 | 792 end = FFMIN(dia_size, x - xmin + 1); |
| 936 | 793 for(dir= start; dir<end; dir++){ |
| 794 int d; | |
| 795 | |
| 796 //check(x - dir,y - dia_size + dir,0, a2) | |
| 797 CHECK_MV(x - dir , y - dia_size + dir); | |
| 798 } | |
| 799 | |
| 800 start= FFMAX(0, -x + dia_size + xmin ); | |
| 948 | 801 end = FFMIN(dia_size, ymax - y + 1); |
| 936 | 802 for(dir= start; dir<end; dir++){ |
| 803 int d; | |
| 804 | |
| 805 //check(x - dia_size + dir, y + dir,0, a3) | |
| 806 CHECK_MV(x - dia_size + dir, y + dir ); | |
| 807 } | |
| 808 | |
| 809 if(x!=best[0] || y!=best[1]) | |
| 810 dia_size=0; | |
| 948 | 811 #if 0 |
| 812 { | |
| 813 int dx, dy, i; | |
| 814 static int stats[8*8]; | |
| 815 dx= ABS(x-best[0]); | |
| 816 dy= ABS(y-best[1]); | |
| 817 stats[dy*8 + dx] ++; | |
| 818 if(256*256*256*64 % (stats[0]+1)==0){ | |
| 819 for(i=0; i<64; i++){ | |
| 820 if((i&7)==0) printf("\n"); | |
| 821 printf("%6d ", stats[i]); | |
| 822 } | |
| 823 printf("\n"); | |
| 824 } | |
| 825 } | |
| 826 #endif | |
| 936 | 827 } |
| 828 return dmin; | |
| 829 } | |
| 830 | |
| 1950 | 831 static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin, |
| 832 int src_index, int ref_index, int const penalty_factor, | |
| 833 int size, int h, int flags){ | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
834 MotionEstContext * const c= &s->me; |
|
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
835 if(c->dia_size==-1) |
| 1950 | 836 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
|
837 else if(c->dia_size<-1) |
| 1950 | 838 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
|
839 else if(c->dia_size<2) |
| 1950 | 840 return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
| 841 else | |
| 842 return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); | |
| 843 } | |
| 844 | |
| 845 static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr, | |
| 846 int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], | |
| 847 int ref_mv_scale, int flags) | |
| 936 | 848 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
849 MotionEstContext * const c= &s->me; |
| 936 | 850 int best[2]={0, 0}; |
| 1950 | 851 int d, dmin; |
| 936 | 852 int map_generation; |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
853 const int penalty_factor= c->penalty_factor; |
| 936 | 854 const int size=0; |
| 1708 | 855 const int h=16; |
| 856 const int ref_mv_stride= s->mb_stride; //pass as arg FIXME | |
| 857 const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME | |
| 1950 | 858 me_cmp_func cmpf, chroma_cmpf; |
| 936 | 859 |
| 1950 | 860 LOAD_COMMON |
| 861 LOAD_COMMON2 | |
| 862 | |
| 863 cmpf= s->dsp.me_cmp[size]; | |
| 864 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 936 | 865 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
866 map_generation= update_map_generation(c); |
| 936 | 867 |
| 1950 | 868 dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags); |
| 936 | 869 map[0]= map_generation; |
| 870 score_map[0]= dmin; | |
| 871 | |
| 872 /* first line */ | |
| 1799 | 873 if (s->first_slice_line) { |
| 936 | 874 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
| 948 | 875 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
| 876 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
| 936 | 877 }else{ |
| 878 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1] | |
| 879 |P_TOP[0] |P_TOP[1] | |
| 948 | 880 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){ |
| 936 | 881 *mx_ptr= 0; |
| 882 *my_ptr= 0; | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
883 c->skip=1; |
| 936 | 884 return dmin; |
| 885 } | |
| 886 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) | |
| 887 if(dmin>256*2){ | |
| 948 | 888 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
| 889 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
| 936 | 890 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift) |
| 891 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift) | |
| 892 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) | |
| 893 } | |
| 894 } | |
| 895 if(dmin>256*4){ | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
896 if(c->pre_pass){ |
| 952 | 897 CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, |
| 898 (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16) | |
| 1799 | 899 if(!s->first_slice_line) |
| 900 CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, | |
| 901 (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
| 952 | 902 }else{ |
| 903 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, | |
| 904 (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
|
905 if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line |
| 1799 | 906 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, |
| 907 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
| 952 | 908 } |
| 936 | 909 } |
| 948 | 910 |
|
2015
3ab8f3e2ae6a
moving motion estimation specific variables from MpegEncContext -> MotionEstContext
michael
parents:
2014
diff
changeset
|
911 if(c->avctx->last_predictor_count){ |
|
3ab8f3e2ae6a
moving motion estimation specific variables from MpegEncContext -> MotionEstContext
michael
parents:
2014
diff
changeset
|
912 const int count= c->avctx->last_predictor_count; |
| 948 | 913 const int xstart= FFMAX(0, s->mb_x - count); |
| 914 const int ystart= FFMAX(0, s->mb_y - count); | |
| 915 const int xend= FFMIN(s->mb_width , s->mb_x + count + 1); | |
| 916 const int yend= FFMIN(s->mb_height, s->mb_y + count + 1); | |
| 917 int mb_y; | |
| 936 | 918 |
| 948 | 919 for(mb_y=ystart; mb_y<yend; mb_y++){ |
| 920 int mb_x; | |
| 921 for(mb_x=xstart; mb_x<xend; mb_x++){ | |
| 922 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride; | |
| 923 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16; | |
| 924 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16; | |
| 925 | |
| 926 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue; | |
| 927 CHECK_MV(mx,my) | |
| 936 | 928 } |
| 929 } | |
| 930 } | |
| 948 | 931 |
| 936 | 932 //check(best[0],best[1],0, b0) |
| 1950 | 933 dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
| 936 | 934 |
| 935 //check(best[0],best[1],0, b1) | |
| 936 *mx_ptr= best[0]; | |
| 937 *my_ptr= best[1]; | |
| 938 | |
| 939 // printf("%d %d %d \n", best[0], best[1], dmin); | |
| 940 return dmin; | |
| 941 } | |
| 942 | |
| 1950 | 943 //this function is dedicated to the braindamaged gcc |
| 944 static inline int epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, | |
| 945 int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], | |
| 946 int ref_mv_scale) | |
| 947 { | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
948 MotionEstContext * const c= &s->me; |
| 1950 | 949 //FIXME convert other functions in the same way if faster |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
950 switch(c->flags){ |
| 1950 | 951 case 0: |
| 952 return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0); | |
| 953 // case FLAG_QPEL: | |
| 954 // return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL); | |
| 955 default: | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
956 return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags); |
| 1950 | 957 } |
| 958 } | |
| 959 | |
| 960 static int epzs_motion_search4(MpegEncContext * s, | |
| 961 int *mx_ptr, int *my_ptr, int P[10][2], | |
| 962 int src_index, int ref_index, int16_t (*last_mv)[2], | |
| 963 int ref_mv_scale) | |
| 936 | 964 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
965 MotionEstContext * const c= &s->me; |
| 936 | 966 int best[2]={0, 0}; |
| 967 int d, dmin; | |
| 968 int map_generation; | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
969 const int penalty_factor= c->penalty_factor; |
| 936 | 970 const int size=1; |
| 1708 | 971 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
|
972 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
|
973 const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; |
| 1950 | 974 me_cmp_func cmpf, chroma_cmpf; |
| 1708 | 975 LOAD_COMMON |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
976 int flags= c->flags; |
| 1950 | 977 LOAD_COMMON2 |
| 936 | 978 |
| 1950 | 979 cmpf= s->dsp.me_cmp[size]; |
| 980 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 936 | 981 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
982 map_generation= update_map_generation(c); |
| 936 | 983 |
| 984 dmin = 1000000; | |
| 985 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); | |
| 986 /* first line */ | |
| 1799 | 987 if (s->first_slice_line) { |
| 936 | 988 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
| 948 | 989 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
| 990 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
| 936 | 991 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) |
| 992 }else{ | |
| 993 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
| 994 //FIXME try some early stop | |
| 995 if(dmin>64*2){ | |
| 996 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) | |
| 997 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) | |
| 998 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) | |
| 999 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) | |
| 948 | 1000 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, |
| 1001 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
| 936 | 1002 } |
| 1003 } | |
| 1004 if(dmin>64*4){ | |
| 948 | 1005 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, |
| 1006 (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
|
1007 if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line |
| 1799 | 1008 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, |
| 1009 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
| 936 | 1010 } |
| 1011 | |
| 1950 | 1012 dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
| 1708 | 1013 |
| 1014 *mx_ptr= best[0]; | |
| 1015 *my_ptr= best[1]; | |
| 1016 | |
| 1017 // printf("%d %d %d \n", best[0], best[1], dmin); | |
| 1018 return dmin; | |
| 1019 } | |
| 1020 | |
| 1021 //try to merge with above FIXME (needs PSNR test) | |
| 1950 | 1022 static int epzs_motion_search2(MpegEncContext * s, |
| 1023 int *mx_ptr, int *my_ptr, int P[10][2], | |
| 1024 int src_index, int ref_index, int16_t (*last_mv)[2], | |
| 1025 int ref_mv_scale) | |
| 1708 | 1026 { |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
1027 MotionEstContext * const c= &s->me; |
| 1708 | 1028 int best[2]={0, 0}; |
| 1029 int d, dmin; | |
| 1030 int map_generation; | |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
1031 const int penalty_factor= c->penalty_factor; |
| 1708 | 1032 const int size=0; //FIXME pass as arg |
| 1033 const int h=8; | |
| 1034 const int ref_mv_stride= s->mb_stride; | |
| 1035 const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; | |
| 1950 | 1036 me_cmp_func cmpf, chroma_cmpf; |
| 1708 | 1037 LOAD_COMMON |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
1038 int flags= c->flags; |
| 1950 | 1039 LOAD_COMMON2 |
| 1708 | 1040 |
| 1950 | 1041 cmpf= s->dsp.me_cmp[size]; |
| 1042 chroma_cmpf= s->dsp.me_cmp[size+1]; | |
| 1708 | 1043 |
|
2014
15c885db82a8
reduce dependancy between motion estimation and MpegEncContext
michael
parents:
1950
diff
changeset
|
1044 map_generation= update_map_generation(c); |
| 1708 | 1045 |
| 1046 dmin = 1000000; | |
| 1047 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); | |
| 1048 /* first line */ | |
| 1799 | 1049 if (s->first_slice_line) { |
| 1708 | 1050 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
| 1051 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, | |
| 1052 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
| 1053 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
| 1054 }else{ | |
| 1055 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
| 1056 //FIXME try some early stop | |
| 1057 if(dmin>64*2){ | |
| 1058 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) | |
| 1059 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) | |
| 1060 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) | |
| 1061 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) | |
| 1062 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, | |
| 1063 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) | |
| 1064 } | |
| 1065 } | |
| 1066 if(dmin>64*4){ | |
| 1067 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, | |
| 1068 (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
|
1069 if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line |
| 1799 | 1070 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, |
| 1071 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) | |
| 1708 | 1072 } |
| 1073 | |
| 1950 | 1074 dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); |
| 948 | 1075 |
| 936 | 1076 *mx_ptr= best[0]; |
| 1077 *my_ptr= best[1]; | |
| 1078 | |
| 1079 // printf("%d %d %d \n", best[0], best[1], dmin); | |
| 1080 return dmin; | |
| 1081 } |
