Mercurial > libavcodec.hg
annotate motion_est.c @ 951:ad264a7d4f94 libavcodec
motion estimation pre pass
| author | michaelni |
|---|---|
| date | Thu, 02 Jan 2003 01:29:35 +0000 |
| parents | d4d714493faa |
| children | f348d302a51e |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Motion estimation | |
| 429 | 3 * Copyright (c) 2000,2001 Fabrice Bellard. |
| 455 | 4 * Copyright (c) 2002 Michael Niedermayer |
| 0 | 5 * |
| 6 * | |
| 429 | 7 * This library is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Lesser General Public | |
| 9 * License as published by the Free Software Foundation; either | |
| 10 * version 2 of the License, or (at your option) any later version. | |
| 0 | 11 * |
| 429 | 12 * This library is distributed in the hope that it will be useful, |
| 0 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 429 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 * Lesser General Public License for more details. | |
| 0 | 16 * |
| 429 | 17 * You should have received a copy of the GNU Lesser General Public |
| 18 * License along with this library; if not, write to the Free Software | |
| 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
20 * |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
21 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at> |
| 0 | 22 */ |
| 23 #include <stdlib.h> | |
| 24 #include <stdio.h> | |
| 25 #include "avcodec.h" | |
| 26 #include "dsputil.h" | |
| 27 #include "mpegvideo.h" | |
| 28 | |
| 936 | 29 //#undef NDEBUG |
| 30 //#include <assert.h> | |
| 31 | |
| 455 | 32 #define SQ(a) ((a)*(a)) |
| 284 | 33 |
| 455 | 34 #define P_LEFT P[1] |
| 35 #define P_TOP P[2] | |
| 36 #define P_TOPRIGHT P[3] | |
| 37 #define P_MEDIAN P[4] | |
| 38 #define P_MV1 P[9] | |
| 39 | |
| 936 | 40 static inline int sad_hpel_motion_search(MpegEncContext * s, |
| 41 int *mx_ptr, int *my_ptr, int dmin, | |
| 42 int xmin, int ymin, int xmax, int ymax, | |
| 43 int pred_x, int pred_y, Picture *picture, | |
| 44 int n, int size, uint16_t * const mv_penalty); | |
| 0 | 45 |
| 936 | 46 static inline int update_map_generation(MpegEncContext * s) |
| 47 { | |
| 48 s->me.map_generation+= 1<<(ME_MAP_MV_BITS*2); | |
| 49 if(s->me.map_generation==0){ | |
| 50 s->me.map_generation= 1<<(ME_MAP_MV_BITS*2); | |
| 51 memset(s->me.map, 0, sizeof(uint32_t)*ME_MAP_SIZE); | |
| 52 } | |
| 53 return s->me.map_generation; | |
| 54 } | |
| 55 | |
| 948 | 56 /* shape adaptive search stuff */ |
| 57 typedef struct Minima{ | |
| 58 int height; | |
| 59 int x, y; | |
| 60 int checked; | |
| 61 }Minima; | |
| 936 | 62 |
| 948 | 63 static int minima_cmp(const void *a, const void *b){ |
| 64 Minima *da = (Minima *) a; | |
| 65 Minima *db = (Minima *) b; | |
| 66 | |
| 67 return da->height - db->height; | |
| 68 } | |
| 936 | 69 |
| 70 /* SIMPLE */ | |
| 71 #define RENAME(a) simple_ ## a | |
| 72 | |
| 73 #define CMP(d, x, y, size)\ | |
| 74 d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride); | |
| 75 | |
| 76 #define CMP_HPEL(d, dx, dy, x, y, size)\ | |
| 77 {\ | |
| 78 const int dxy= (dx) + 2*(dy);\ | |
| 79 hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\ | |
| 80 d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ | |
| 81 } | |
| 82 | |
| 83 #define CMP_QPEL(d, dx, dy, x, y, size)\ | |
| 84 {\ | |
| 85 const int dxy= (dx) + 4*(dy);\ | |
| 86 qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\ | |
| 87 d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ | |
| 88 } | |
| 89 | |
| 90 #include "motion_est_template.c" | |
| 91 #undef RENAME | |
| 92 #undef CMP | |
| 93 #undef CMP_HPEL | |
| 94 #undef CMP_QPEL | |
| 95 #undef INIT | |
| 96 | |
| 97 /* SIMPLE CHROMA */ | |
| 98 #define RENAME(a) simple_chroma_ ## a | |
| 99 | |
| 100 #define CMP(d, x, y, size)\ | |
| 101 d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride);\ | |
| 102 if(chroma_cmp){\ | |
| 103 int dxy= ((x)&1) + 2*((y)&1);\ | |
| 104 int c= ((x)>>1) + ((y)>>1)*uvstride;\ | |
| 105 \ | |
| 106 chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, 8);\ | |
| 107 d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride);\ | |
| 108 chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, 8);\ | |
| 109 d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride);\ | |
| 110 } | |
| 111 | |
| 112 #define CMP_HPEL(d, dx, dy, x, y, size)\ | |
| 113 {\ | |
| 114 const int dxy= (dx) + 2*(dy);\ | |
| 115 hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\ | |
| 116 d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ | |
| 117 if(chroma_cmp_sub){\ | |
| 118 int cxy= (dxy) | ((x)&1) | (2*((y)&1));\ | |
| 119 int c= ((x)>>1) + ((y)>>1)*uvstride;\ | |
| 120 chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\ | |
| 121 d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\ | |
| 122 chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\ | |
| 123 d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\ | |
| 124 }\ | |
| 125 } | |
| 126 | |
| 127 #define CMP_QPEL(d, dx, dy, x, y, size)\ | |
| 128 {\ | |
| 129 const int dxy= (dx) + 4*(dy);\ | |
| 130 qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\ | |
| 131 d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ | |
| 132 if(chroma_cmp_sub){\ | |
| 133 int cxy, c;\ | |
| 134 int cx= (4*(x) + (dx))/2;\ | |
| 135 int cy= (4*(y) + (dy))/2;\ | |
| 136 cx= (cx>>1)|(cx&1);\ | |
| 137 cy= (cy>>1)|(cy&1);\ | |
| 138 cxy= (cx&1) + 2*(cy&1);\ | |
| 139 c= ((cx)>>1) + ((cy)>>1)*uvstride;\ | |
| 140 chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\ | |
| 141 d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\ | |
| 142 chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\ | |
| 143 d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\ | |
| 144 }\ | |
| 145 } | |
| 146 | |
| 147 #include "motion_est_template.c" | |
| 148 #undef RENAME | |
| 149 #undef CMP | |
| 150 #undef CMP_HPEL | |
| 151 #undef CMP_QPEL | |
| 152 #undef INIT | |
| 153 | |
| 154 /* SIMPLE DIRECT HPEL */ | |
| 155 #define RENAME(a) simple_direct_hpel_ ## a | |
| 156 //FIXME precalc divisions stuff | |
| 157 | |
| 158 #define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\ | |
| 159 if((x) >= xmin && 2*(x) + (dx) <= 2*xmax && (y) >= ymin && 2*(y) + (dy) <= 2*ymax){\ | |
| 160 const int hx= 2*(x) + (dx);\ | |
| 161 const int hy= 2*(y) + (dy);\ | |
| 162 if(s->mv_type==MV_TYPE_8X8){\ | |
| 163 int i;\ | |
| 164 for(i=0; i<4; i++){\ | |
| 165 int fx = s->me.direct_basis_mv[i][0] + hx;\ | |
| 166 int fy = s->me.direct_basis_mv[i][1] + hy;\ | |
| 167 int bx = hx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\ | |
| 168 int by = hy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\ | |
| 169 int fxy= (fx&1) + 2*(fy&1);\ | |
| 170 int bxy= (bx&1) + 2*(by&1);\ | |
| 171 \ | |
| 172 uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\ | |
| 173 hpel_put[1][fxy](dst, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 8);\ | |
| 174 hpel_avg[1][bxy](dst, (ref2_y) + (bx>>1) + (by>>1)*(stride), stride, 8);\ | |
| 175 }\ | |
| 176 }else{\ | |
| 177 int fx = s->me.direct_basis_mv[0][0] + hx;\ | |
| 178 int fy = s->me.direct_basis_mv[0][1] + hy;\ | |
| 179 int bx = hx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\ | |
| 180 int by = hy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\ | |
| 181 int fxy= (fx&1) + 2*(fy&1);\ | |
| 182 int bxy= (bx&1) + 2*(by&1);\ | |
| 183 \ | |
| 184 hpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 16);\ | |
| 185 hpel_avg[0][bxy](s->me.scratchpad, (ref2_y) + (bx>>1) + (by>>1)*(stride), stride, 16);\ | |
| 186 }\ | |
| 187 d = cmp_func(s, s->me.scratchpad, src_y, stride);\ | |
| 188 }else\ | |
| 189 d= 256*256*256*32; | |
| 190 | |
| 191 | |
| 192 #define CMP_HPEL(d, dx, dy, x, y, size)\ | |
| 193 CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub) | |
| 194 | |
| 195 #define CMP(d, x, y, size)\ | |
| 196 CMP_DIRECT(d, 0, 0, x, y, size, cmp) | |
| 197 | |
| 198 #include "motion_est_template.c" | |
| 199 #undef RENAME | |
| 200 #undef CMP | |
| 201 #undef CMP_HPEL | |
| 202 #undef CMP_QPEL | |
| 203 #undef INIT | |
| 204 #undef CMP_DIRECT | |
| 205 | |
| 206 /* SIMPLE DIRECT QPEL */ | |
| 207 #define RENAME(a) simple_direct_qpel_ ## a | |
| 208 | |
| 209 #define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\ | |
| 210 if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*ymax){\ | |
| 211 const int qx= 4*(x) + (dx);\ | |
| 212 const int qy= 4*(y) + (dy);\ | |
| 213 if(s->mv_type==MV_TYPE_8X8){\ | |
| 214 int i;\ | |
| 215 for(i=0; i<4; i++){\ | |
| 216 int fx = s->me.direct_basis_mv[i][0] + qx;\ | |
| 217 int fy = s->me.direct_basis_mv[i][1] + qy;\ | |
| 218 int bx = qx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\ | |
| 219 int by = qy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\ | |
| 220 int fxy= (fx&3) + 4*(fy&3);\ | |
| 221 int bxy= (bx&3) + 4*(by&3);\ | |
| 222 \ | |
| 223 uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\ | |
| 224 qpel_put[1][fxy](dst, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\ | |
| 225 qpel_avg[1][bxy](dst, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\ | |
| 226 }\ | |
| 227 }else{\ | |
| 228 int fx = s->me.direct_basis_mv[0][0] + qx;\ | |
| 229 int fy = s->me.direct_basis_mv[0][1] + qy;\ | |
| 230 int bx = qx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\ | |
| 231 int by = qy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\ | |
| 232 int fxy= (fx&3) + 4*(fy&3);\ | |
| 233 int bxy= (bx&3) + 4*(by&3);\ | |
| 234 \ | |
| 235 qpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\ | |
| 236 qpel_avg[0][bxy](s->me.scratchpad, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\ | |
| 237 }\ | |
| 238 d = cmp_func(s, s->me.scratchpad, src_y, stride);\ | |
| 239 }else\ | |
| 240 d= 256*256*256*32; | |
| 241 | |
| 242 | |
| 243 #define CMP_QPEL(d, dx, dy, x, y, size)\ | |
| 244 CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub) | |
| 245 | |
| 246 #define CMP(d, x, y, size)\ | |
| 247 CMP_DIRECT(d, 0, 0, x, y, size, cmp) | |
| 248 | |
| 249 #include "motion_est_template.c" | |
| 250 #undef RENAME | |
| 251 #undef CMP | |
| 252 #undef CMP_HPEL | |
| 253 #undef CMP_QPEL | |
| 254 #undef INIT | |
| 255 #undef CMP__DIRECT | |
| 256 | |
| 257 | |
| 258 static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride){ | |
| 259 return 0; | |
| 260 } | |
| 261 | |
| 262 static void set_cmp(MpegEncContext *s, me_cmp_func *cmp, int type){ | |
| 263 DSPContext* c= &s->dsp; | |
| 264 int i; | |
| 265 | |
| 266 memset(cmp, 0, sizeof(void*)*11); | |
| 267 | |
| 268 switch(type&0xFF){ | |
| 269 case FF_CMP_SAD: | |
| 270 cmp[0]= c->sad[0]; | |
| 271 cmp[1]= c->sad[1]; | |
| 272 break; | |
| 273 case FF_CMP_SATD: | |
| 274 cmp[0]= c->hadamard8_diff[0]; | |
| 275 cmp[1]= c->hadamard8_diff[1]; | |
| 276 break; | |
| 277 case FF_CMP_SSE: | |
| 278 cmp[0]= c->sse[0]; | |
| 279 cmp[1]= c->sse[1]; | |
| 280 break; | |
| 281 case FF_CMP_DCT: | |
| 282 cmp[0]= c->dct_sad[0]; | |
| 283 cmp[1]= c->dct_sad[1]; | |
| 284 break; | |
| 285 case FF_CMP_PSNR: | |
| 286 cmp[0]= c->quant_psnr[0]; | |
| 287 cmp[1]= c->quant_psnr[1]; | |
| 288 break; | |
| 289 case FF_CMP_ZERO: | |
| 290 for(i=0; i<7; i++){ | |
| 291 cmp[i]= zero_cmp; | |
| 292 } | |
| 293 break; | |
| 294 default: | |
| 295 fprintf(stderr,"internal error in cmp function selection\n"); | |
| 296 } | |
| 297 }; | |
| 298 | |
| 299 static inline int get_penalty_factor(MpegEncContext *s, int type){ | |
| 300 | |
| 301 switch(type){ | |
| 302 default: | |
| 303 case FF_CMP_SAD: | |
| 304 return s->qscale; | |
| 305 case FF_CMP_SSE: | |
| 306 // return s->qscale*8; | |
| 307 case FF_CMP_DCT: | |
| 308 case FF_CMP_SATD: | |
| 309 return s->qscale*8; | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 void ff_init_me(MpegEncContext *s){ | |
| 314 set_cmp(s, s->dsp.me_cmp, s->avctx->me_cmp); | |
| 315 set_cmp(s, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp); | |
| 316 set_cmp(s, s->dsp.mb_cmp, s->avctx->mb_cmp); | |
| 317 | |
| 318 if(s->flags&CODEC_FLAG_QPEL){ | |
| 319 if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) | |
| 320 s->me.sub_motion_search= simple_chroma_qpel_motion_search; | |
| 321 else | |
| 322 s->me.sub_motion_search= simple_qpel_motion_search; | |
| 323 }else{ | |
| 324 if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) | |
| 325 s->me.sub_motion_search= simple_chroma_hpel_motion_search; | |
| 326 else if(s->avctx->me_sub_cmp == FF_CMP_SAD && s->avctx->me_cmp == FF_CMP_SAD) | |
| 327 s->me.sub_motion_search= sad_hpel_motion_search; | |
| 328 else | |
| 329 s->me.sub_motion_search= simple_hpel_motion_search; | |
| 330 } | |
| 331 | |
| 332 if(s->avctx->me_cmp&FF_CMP_CHROMA){ | |
| 333 s->me.motion_search[0]= simple_chroma_epzs_motion_search; | |
| 334 s->me.motion_search[1]= simple_chroma_epzs_motion_search4; | |
| 335 }else{ | |
| 336 s->me.motion_search[0]= simple_epzs_motion_search; | |
| 337 s->me.motion_search[1]= simple_epzs_motion_search4; | |
| 338 } | |
| 339 } | |
| 340 | |
| 284 | 341 static int pix_dev(UINT8 * pix, int line_size, int mean) |
| 342 { | |
| 343 int s, i, j; | |
| 344 | |
| 345 s = 0; | |
| 346 for (i = 0; i < 16; i++) { | |
| 347 for (j = 0; j < 16; j += 8) { | |
| 348 s += ABS(pix[0]-mean); | |
| 349 s += ABS(pix[1]-mean); | |
| 350 s += ABS(pix[2]-mean); | |
| 351 s += ABS(pix[3]-mean); | |
| 352 s += ABS(pix[4]-mean); | |
| 353 s += ABS(pix[5]-mean); | |
| 354 s += ABS(pix[6]-mean); | |
| 355 s += ABS(pix[7]-mean); | |
| 356 pix += 8; | |
| 357 } | |
| 358 pix += line_size - 16; | |
| 359 } | |
| 360 return s; | |
| 361 } | |
| 362 | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
363 static inline void no_motion_search(MpegEncContext * s, |
|
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
364 int *mx_ptr, int *my_ptr) |
| 0 | 365 { |
| 366 *mx_ptr = 16 * s->mb_x; | |
| 367 *my_ptr = 16 * s->mb_y; | |
| 368 } | |
| 369 | |
| 370 static int full_motion_search(MpegEncContext * s, | |
| 371 int *mx_ptr, int *my_ptr, int range, | |
| 324 | 372 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
| 0 | 373 { |
| 374 int x1, y1, x2, y2, xx, yy, x, y; | |
| 375 int mx, my, dmin, d; | |
| 376 UINT8 *pix; | |
| 377 | |
| 378 xx = 16 * s->mb_x; | |
| 379 yy = 16 * s->mb_y; | |
| 380 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */ | |
| 381 if (x1 < xmin) | |
| 382 x1 = xmin; | |
| 383 x2 = xx + range - 1; | |
| 384 if (x2 > xmax) | |
| 385 x2 = xmax; | |
| 386 y1 = yy - range + 1; | |
| 387 if (y1 < ymin) | |
| 388 y1 = ymin; | |
| 389 y2 = yy + range - 1; | |
| 390 if (y2 > ymax) | |
| 391 y2 = ymax; | |
| 903 | 392 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 0 | 393 dmin = 0x7fffffff; |
| 394 mx = 0; | |
| 395 my = 0; | |
| 396 for (y = y1; y <= y2; y++) { | |
| 397 for (x = x1; x <= x2; x++) { | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
398 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, |
| 294 | 399 s->linesize); |
| 0 | 400 if (d < dmin || |
| 401 (d == dmin && | |
| 402 (abs(x - xx) + abs(y - yy)) < | |
| 403 (abs(mx - xx) + abs(my - yy)))) { | |
| 404 dmin = d; | |
| 405 mx = x; | |
| 406 my = y; | |
| 407 } | |
| 408 } | |
| 409 } | |
| 410 | |
| 411 *mx_ptr = mx; | |
| 412 *my_ptr = my; | |
| 413 | |
| 414 #if 0 | |
| 415 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) || | |
| 416 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) { | |
| 417 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr); | |
| 418 } | |
| 419 #endif | |
| 420 return dmin; | |
| 421 } | |
| 422 | |
| 423 | |
| 424 static int log_motion_search(MpegEncContext * s, | |
| 425 int *mx_ptr, int *my_ptr, int range, | |
| 324 | 426 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
| 0 | 427 { |
| 428 int x1, y1, x2, y2, xx, yy, x, y; | |
| 429 int mx, my, dmin, d; | |
| 430 UINT8 *pix; | |
| 431 | |
| 432 xx = s->mb_x << 4; | |
| 433 yy = s->mb_y << 4; | |
| 434 | |
| 435 /* Left limit */ | |
| 436 x1 = xx - range; | |
| 437 if (x1 < xmin) | |
| 438 x1 = xmin; | |
| 439 | |
| 440 /* Right limit */ | |
| 441 x2 = xx + range; | |
| 442 if (x2 > xmax) | |
| 443 x2 = xmax; | |
| 444 | |
| 445 /* Upper limit */ | |
| 446 y1 = yy - range; | |
| 447 if (y1 < ymin) | |
| 448 y1 = ymin; | |
| 449 | |
| 450 /* Lower limit */ | |
| 451 y2 = yy + range; | |
| 452 if (y2 > ymax) | |
| 453 y2 = ymax; | |
| 454 | |
| 903 | 455 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 0 | 456 dmin = 0x7fffffff; |
| 457 mx = 0; | |
| 458 my = 0; | |
| 459 | |
| 460 do { | |
| 461 for (y = y1; y <= y2; y += range) { | |
| 462 for (x = x1; x <= x2; x += range) { | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
463 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
| 0 | 464 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
| 465 dmin = d; | |
| 466 mx = x; | |
| 467 my = y; | |
| 468 } | |
| 469 } | |
| 470 } | |
| 471 | |
| 472 range = range >> 1; | |
| 473 | |
| 474 x1 = mx - range; | |
| 475 if (x1 < xmin) | |
| 476 x1 = xmin; | |
| 477 | |
| 478 x2 = mx + range; | |
| 479 if (x2 > xmax) | |
| 480 x2 = xmax; | |
| 481 | |
| 482 y1 = my - range; | |
| 483 if (y1 < ymin) | |
| 484 y1 = ymin; | |
| 485 | |
| 486 y2 = my + range; | |
| 487 if (y2 > ymax) | |
| 488 y2 = ymax; | |
| 489 | |
| 490 } while (range >= 1); | |
| 491 | |
| 492 #ifdef DEBUG | |
| 493 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my); | |
| 494 #endif | |
| 495 *mx_ptr = mx; | |
| 496 *my_ptr = my; | |
| 497 return dmin; | |
| 498 } | |
| 499 | |
| 500 static int phods_motion_search(MpegEncContext * s, | |
| 501 int *mx_ptr, int *my_ptr, int range, | |
| 324 | 502 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
| 0 | 503 { |
| 504 int x1, y1, x2, y2, xx, yy, x, y, lastx, d; | |
| 505 int mx, my, dminx, dminy; | |
| 506 UINT8 *pix; | |
| 507 | |
| 508 xx = s->mb_x << 4; | |
| 509 yy = s->mb_y << 4; | |
| 510 | |
| 511 /* Left limit */ | |
| 512 x1 = xx - range; | |
| 513 if (x1 < xmin) | |
| 514 x1 = xmin; | |
| 515 | |
| 516 /* Right limit */ | |
| 517 x2 = xx + range; | |
| 518 if (x2 > xmax) | |
| 519 x2 = xmax; | |
| 520 | |
| 521 /* Upper limit */ | |
| 522 y1 = yy - range; | |
| 523 if (y1 < ymin) | |
| 524 y1 = ymin; | |
| 525 | |
| 526 /* Lower limit */ | |
| 527 y2 = yy + range; | |
| 528 if (y2 > ymax) | |
| 529 y2 = ymax; | |
| 530 | |
| 903 | 531 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 0 | 532 mx = 0; |
| 533 my = 0; | |
| 534 | |
| 535 x = xx; | |
| 536 y = yy; | |
| 537 do { | |
| 538 dminx = 0x7fffffff; | |
| 539 dminy = 0x7fffffff; | |
| 540 | |
| 541 lastx = x; | |
| 542 for (x = x1; x <= x2; x += range) { | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
543 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
| 0 | 544 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
| 545 dminx = d; | |
| 546 mx = x; | |
| 547 } | |
| 548 } | |
| 549 | |
| 550 x = lastx; | |
| 551 for (y = y1; y <= y2; y += range) { | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
552 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
| 0 | 553 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
| 554 dminy = d; | |
| 555 my = y; | |
| 556 } | |
| 557 } | |
| 558 | |
| 559 range = range >> 1; | |
| 560 | |
| 561 x = mx; | |
| 562 y = my; | |
| 563 x1 = mx - range; | |
| 564 if (x1 < xmin) | |
| 565 x1 = xmin; | |
| 566 | |
| 567 x2 = mx + range; | |
| 568 if (x2 > xmax) | |
| 569 x2 = xmax; | |
| 570 | |
| 571 y1 = my - range; | |
| 572 if (y1 < ymin) | |
| 573 y1 = ymin; | |
| 574 | |
| 575 y2 = my + range; | |
| 576 if (y2 > ymax) | |
| 577 y2 = ymax; | |
| 578 | |
| 579 } while (range >= 1); | |
| 580 | |
| 581 #ifdef DEBUG | |
| 582 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my); | |
| 583 #endif | |
| 584 | |
| 585 /* half pixel search */ | |
| 586 *mx_ptr = mx; | |
| 587 *my_ptr = my; | |
| 588 return dminy; | |
| 589 } | |
| 590 | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
591 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
592 #define Z_THRESHOLD 256 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
593 |
| 936 | 594 #define CHECK_SAD_HALF_MV(suffix, x, y) \ |
| 455 | 595 {\ |
| 596 d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ | |
| 936 | 597 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\ |
| 455 | 598 COPY3_IF_LT(dminh, d, dx, x, dy, y)\ |
| 599 } | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
600 |
| 936 | 601 static inline int sad_hpel_motion_search(MpegEncContext * s, |
| 0 | 602 int *mx_ptr, int *my_ptr, int dmin, |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
603 int xmin, int ymin, int xmax, int ymax, |
| 936 | 604 int pred_x, int pred_y, Picture *picture, |
| 605 int n, int size, uint16_t * const mv_penalty) | |
| 0 | 606 { |
| 936 | 607 uint8_t *ref_picture= picture->data[0]; |
| 608 uint32_t *score_map= s->me.score_map; | |
| 609 const int penalty_factor= s->me.sub_penalty_factor; | |
| 455 | 610 int mx, my, xx, yy, dminh; |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
611 UINT8 *pix, *ptr; |
| 936 | 612 op_pixels_abs_func pix_abs_x2; |
| 613 op_pixels_abs_func pix_abs_y2; | |
| 614 op_pixels_abs_func pix_abs_xy2; | |
| 615 | |
| 616 if(size==0){ | |
| 617 pix_abs_x2 = s->dsp.pix_abs16x16_x2; | |
| 618 pix_abs_y2 = s->dsp.pix_abs16x16_y2; | |
| 619 pix_abs_xy2= s->dsp.pix_abs16x16_xy2; | |
| 620 }else{ | |
| 621 pix_abs_x2 = s->dsp.pix_abs8x8_x2; | |
| 622 pix_abs_y2 = s->dsp.pix_abs8x8_y2; | |
| 623 pix_abs_xy2= s->dsp.pix_abs8x8_xy2; | |
| 863 | 624 } |
| 455 | 625 |
| 936 | 626 if(s->me.skip){ |
| 455 | 627 // printf("S"); |
| 628 *mx_ptr = 0; | |
| 629 *my_ptr = 0; | |
| 630 return dmin; | |
| 631 } | |
| 632 // printf("N"); | |
| 633 | |
| 634 xx = 16 * s->mb_x + 8*(n&1); | |
| 635 yy = 16 * s->mb_y + 8*(n>>1); | |
| 903 | 636 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 455 | 637 |
| 294 | 638 mx = *mx_ptr; |
| 639 my = *my_ptr; | |
| 455 | 640 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx); |
| 641 | |
| 294 | 642 dminh = dmin; |
| 643 | |
| 644 if (mx > xmin && mx < xmax && | |
| 645 my > ymin && my < ymax) { | |
| 455 | 646 int dx=0, dy=0; |
| 647 int d, pen_x, pen_y; | |
| 648 const int index= (my<<ME_MAP_SHIFT) + mx; | |
| 649 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; | |
| 650 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]; | |
| 651 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]; | |
| 652 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; | |
| 653 mx<<=1; | |
| 654 my<<=1; | |
| 294 | 655 |
| 656 | |
| 657 pen_x= pred_x + mx; | |
| 658 pen_y= pred_y + my; | |
| 659 | |
| 660 ptr-= s->linesize; | |
| 455 | 661 if(t<=b){ |
| 936 | 662 CHECK_SAD_HALF_MV(y2 , 0, -1) |
| 455 | 663 if(l<=r){ |
| 936 | 664 CHECK_SAD_HALF_MV(xy2, -1, -1) |
| 455 | 665 if(t+r<=b+l){ |
| 936 | 666 CHECK_SAD_HALF_MV(xy2, +1, -1) |
| 455 | 667 ptr+= s->linesize; |
| 668 }else{ | |
| 669 ptr+= s->linesize; | |
| 936 | 670 CHECK_SAD_HALF_MV(xy2, -1, +1) |
| 455 | 671 } |
| 936 | 672 CHECK_SAD_HALF_MV(x2 , -1, 0) |
| 455 | 673 }else{ |
| 936 | 674 CHECK_SAD_HALF_MV(xy2, +1, -1) |
| 455 | 675 if(t+l<=b+r){ |
| 936 | 676 CHECK_SAD_HALF_MV(xy2, -1, -1) |
| 455 | 677 ptr+= s->linesize; |
| 678 }else{ | |
| 679 ptr+= s->linesize; | |
| 936 | 680 CHECK_SAD_HALF_MV(xy2, +1, +1) |
| 455 | 681 } |
| 936 | 682 CHECK_SAD_HALF_MV(x2 , +1, 0) |
| 455 | 683 } |
| 684 }else{ | |
| 685 if(l<=r){ | |
| 686 if(t+l<=b+r){ | |
| 936 | 687 CHECK_SAD_HALF_MV(xy2, -1, -1) |
| 455 | 688 ptr+= s->linesize; |
| 689 }else{ | |
| 690 ptr+= s->linesize; | |
| 936 | 691 CHECK_SAD_HALF_MV(xy2, +1, +1) |
| 455 | 692 } |
| 936 | 693 CHECK_SAD_HALF_MV(x2 , -1, 0) |
| 694 CHECK_SAD_HALF_MV(xy2, -1, +1) | |
| 455 | 695 }else{ |
| 696 if(t+r<=b+l){ | |
| 936 | 697 CHECK_SAD_HALF_MV(xy2, +1, -1) |
| 455 | 698 ptr+= s->linesize; |
| 699 }else{ | |
| 700 ptr+= s->linesize; | |
| 936 | 701 CHECK_SAD_HALF_MV(xy2, -1, +1) |
| 455 | 702 } |
| 936 | 703 CHECK_SAD_HALF_MV(x2 , +1, 0) |
| 704 CHECK_SAD_HALF_MV(xy2, +1, +1) | |
| 455 | 705 } |
| 936 | 706 CHECK_SAD_HALF_MV(y2 , 0, +1) |
| 455 | 707 } |
| 708 mx+=dx; | |
| 709 my+=dy; | |
| 294 | 710 |
| 711 }else{ | |
| 455 | 712 mx<<=1; |
| 713 my<<=1; | |
| 294 | 714 } |
| 715 | |
| 716 *mx_ptr = mx; | |
| 717 *my_ptr = my; | |
| 455 | 718 return dminh; |
| 294 | 719 } |
| 720 | |
| 455 | 721 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) |
| 294 | 722 { |
| 324 | 723 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2); |
| 294 | 724 |
| 324 | 725 s->p_mv_table[xy][0] = mx; |
| 726 s->p_mv_table[xy][1] = my; | |
| 294 | 727 |
| 728 /* has allready been set to the 4 MV if 4MV is done */ | |
| 455 | 729 if(mv4){ |
| 294 | 730 int mot_xy= s->block_index[0]; |
| 731 | |
| 732 s->motion_val[mot_xy ][0]= mx; | |
| 733 s->motion_val[mot_xy ][1]= my; | |
| 734 s->motion_val[mot_xy+1][0]= mx; | |
| 735 s->motion_val[mot_xy+1][1]= my; | |
| 736 | |
| 737 mot_xy += s->block_wrap[0]; | |
| 738 s->motion_val[mot_xy ][0]= mx; | |
| 739 s->motion_val[mot_xy ][1]= my; | |
| 740 s->motion_val[mot_xy+1][0]= mx; | |
| 741 s->motion_val[mot_xy+1][1]= my; | |
| 742 } | |
| 743 } | |
| 744 | |
| 324 | 745 static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code) |
| 746 { | |
| 747 *range = 8 * (1 << (f_code - 1)); | |
| 748 /* XXX: temporary kludge to avoid overflow for msmpeg4 */ | |
| 749 if (s->out_format == FMT_H263 && !s->h263_msmpeg4) | |
| 750 *range *= 2; | |
| 0 | 751 |
| 324 | 752 if (s->unrestricted_mv) { |
| 753 *xmin = -16; | |
| 754 *ymin = -16; | |
| 755 if (s->h263_plus) | |
| 756 *range *= 2; | |
| 951 | 757 if(s->avctx->codec->id!=CODEC_ID_MPEG4){ |
| 324 | 758 *xmax = s->mb_width*16; |
| 759 *ymax = s->mb_height*16; | |
| 760 }else { | |
| 761 *xmax = s->width; | |
| 762 *ymax = s->height; | |
| 763 } | |
| 764 } else { | |
| 765 *xmin = 0; | |
| 766 *ymin = 0; | |
| 767 *xmax = s->mb_width*16 - 16; | |
| 768 *ymax = s->mb_height*16 - 16; | |
| 769 } | |
| 770 } | |
| 771 | |
| 455 | 772 static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift) |
| 773 { | |
| 774 int block; | |
| 775 int P[10][2]; | |
| 903 | 776 uint8_t *ref_picture= s->last_picture.data[0]; |
| 455 | 777 int dmin_sum=0; |
| 936 | 778 uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; |
| 455 | 779 |
| 780 for(block=0; block<4; block++){ | |
| 781 int mx4, my4; | |
| 782 int pred_x4, pred_y4; | |
| 783 int dmin4; | |
| 784 static const int off[4]= {2, 1, 1, -1}; | |
| 785 const int mot_stride = s->block_wrap[0]; | |
| 786 const int mot_xy = s->block_index[block]; | |
| 787 // const int block_x= (block&1); | |
| 788 // const int block_y= (block>>1); | |
| 789 #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way | |
| 790 const int rel_xmin4= xmin; | |
| 791 const int rel_xmax4= xmax; | |
| 792 const int rel_ymin4= ymin; | |
| 793 const int rel_ymax4= ymax; | |
| 794 #else | |
| 795 const int rel_xmin4= xmin - block_x*8; | |
| 796 const int rel_xmax4= xmax - block_x*8 + 8; | |
| 797 const int rel_ymin4= ymin - block_y*8; | |
| 798 const int rel_ymax4= ymax - block_y*8 + 8; | |
| 799 #endif | |
| 800 P_LEFT[0] = s->motion_val[mot_xy - 1][0]; | |
| 801 P_LEFT[1] = s->motion_val[mot_xy - 1][1]; | |
| 802 | |
| 803 if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift); | |
| 804 | |
| 805 /* special case for first line */ | |
| 806 if ((s->mb_y == 0 || s->first_slice_line) && block<2) { | |
| 807 pred_x4= P_LEFT[0]; | |
| 808 pred_y4= P_LEFT[1]; | |
| 809 } else { | |
| 810 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0]; | |
| 811 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1]; | |
| 812 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0]; | |
| 813 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1]; | |
| 814 if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift); | |
| 815 if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift); | |
| 816 if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift); | |
| 817 if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift); | |
| 818 | |
| 819 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
| 820 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 821 | |
| 822 if(s->out_format == FMT_H263){ | |
| 823 pred_x4 = P_MEDIAN[0]; | |
| 824 pred_y4 = P_MEDIAN[1]; | |
| 825 }else { /* mpeg1 at least */ | |
| 826 pred_x4= P_LEFT[0]; | |
| 827 pred_y4= P_LEFT[1]; | |
| 828 } | |
| 829 } | |
| 830 P_MV1[0]= mx; | |
| 831 P_MV1[1]= my; | |
| 832 | |
| 936 | 833 dmin4 = s->me.motion_search[1](s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, |
| 948 | 834 &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); |
| 455 | 835 |
| 936 | 836 dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, |
| 837 pred_x4, pred_y4, &s->last_picture, block, 1, mv_penalty); | |
| 455 | 838 |
| 839 s->motion_val[ s->block_index[block] ][0]= mx4; | |
| 840 s->motion_val[ s->block_index[block] ][1]= my4; | |
| 841 dmin_sum+= dmin4; | |
| 842 } | |
| 843 return dmin_sum; | |
| 844 } | |
| 845 | |
| 324 | 846 void ff_estimate_p_frame_motion(MpegEncContext * s, |
| 847 int mb_x, int mb_y) | |
| 0 | 848 { |
| 849 UINT8 *pix, *ppix; | |
| 850 int sum, varc, vard, mx, my, range, dmin, xx, yy; | |
| 851 int xmin, ymin, xmax, ymax; | |
| 280 | 852 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
853 int pred_x=0, pred_y=0; |
| 455 | 854 int P[10][2]; |
| 280 | 855 const int shift= 1+s->quarter_sample; |
| 294 | 856 int mb_type=0; |
| 903 | 857 uint8_t *ref_picture= s->last_picture.data[0]; |
| 858 Picture * const pic= &s->current_picture; | |
| 936 | 859 uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; |
| 860 | |
| 861 assert(s->quarter_sample==0 || s->quarter_sample==1); | |
| 862 | |
| 863 s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); | |
| 864 s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); | |
| 0 | 865 |
| 324 | 866 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); |
| 455 | 867 rel_xmin= xmin - mb_x*16; |
| 868 rel_xmax= xmax - mb_x*16; | |
| 869 rel_ymin= ymin - mb_y*16; | |
| 870 rel_ymax= ymax - mb_y*16; | |
| 936 | 871 s->me.skip=0; |
| 324 | 872 |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
873 switch(s->me_method) { |
| 0 | 874 case ME_ZERO: |
| 875 default: | |
| 876 no_motion_search(s, &mx, &my); | |
| 455 | 877 mx-= mb_x*16; |
| 878 my-= mb_y*16; | |
| 0 | 879 dmin = 0; |
| 880 break; | |
| 881 case ME_FULL: | |
| 324 | 882 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); |
| 455 | 883 mx-= mb_x*16; |
| 884 my-= mb_y*16; | |
| 0 | 885 break; |
| 886 case ME_LOG: | |
| 324 | 887 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
| 455 | 888 mx-= mb_x*16; |
| 889 my-= mb_y*16; | |
| 0 | 890 break; |
| 891 case ME_PHODS: | |
| 324 | 892 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
| 455 | 893 mx-= mb_x*16; |
| 894 my-= mb_y*16; | |
| 0 | 895 break; |
| 288 | 896 case ME_X1: |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
897 case ME_EPZS: |
| 288 | 898 { |
| 294 | 899 const int mot_stride = s->block_wrap[0]; |
| 900 const int mot_xy = s->block_index[0]; | |
| 288 | 901 |
| 455 | 902 P_LEFT[0] = s->motion_val[mot_xy - 1][0]; |
| 903 P_LEFT[1] = s->motion_val[mot_xy - 1][1]; | |
| 288 | 904 |
| 455 | 905 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); |
| 280 | 906 |
| 907 /* special case for first line */ | |
| 455 | 908 if ((mb_y == 0 || s->first_slice_line)) { |
| 909 pred_x= P_LEFT[0]; | |
| 910 pred_y= P_LEFT[1]; | |
| 280 | 911 } else { |
| 455 | 912 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0]; |
| 913 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1]; | |
| 914 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0]; | |
| 915 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1]; | |
| 916 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift); | |
| 917 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift); | |
| 918 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift); | |
| 280 | 919 |
| 455 | 920 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
| 921 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 922 | |
| 923 if(s->out_format == FMT_H263){ | |
| 924 pred_x = P_MEDIAN[0]; | |
| 925 pred_y = P_MEDIAN[1]; | |
| 926 }else { /* mpeg1 at least */ | |
| 927 pred_x= P_LEFT[0]; | |
| 928 pred_y= P_LEFT[1]; | |
| 929 } | |
| 288 | 930 } |
| 280 | 931 } |
| 936 | 932 dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 948 | 933 &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); |
|
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
934 |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
935 break; |
| 0 | 936 } |
| 937 | |
| 938 /* intra / predictive decision */ | |
| 939 xx = mb_x * 16; | |
| 940 yy = mb_y * 16; | |
| 941 | |
| 903 | 942 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 455 | 943 /* At this point (mx,my) are full-pell and the relative displacement */ |
| 944 ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx); | |
|
289
648e9245546d
seems the old intra/inter decission is slightly better with a threshold, than the new one
michaelni
parents:
288
diff
changeset
|
945 |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
946 sum = s->dsp.pix_sum(pix, s->linesize); |
| 455 | 947 |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
948 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; |
| 936 | 949 vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize)+128)>>8; |
| 608 | 950 |
| 455 | 951 //printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); |
| 903 | 952 pic->mb_var [s->mb_width * mb_y + mb_x] = varc; |
| 953 pic->mc_mb_var[s->mb_width * mb_y + mb_x] = vard; | |
| 954 pic->mb_mean [s->mb_width * mb_y + mb_x] = (sum+128)>>8; | |
| 955 pic->mb_var_sum += varc; | |
| 956 pic->mc_mb_var_sum += vard; | |
| 455 | 957 //printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
958 |
| 0 | 959 #if 0 |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
960 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n", |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
961 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); |
| 0 | 962 #endif |
| 294 | 963 if(s->flags&CODEC_FLAG_HQ){ |
| 608 | 964 if (vard <= 64 || vard < varc) |
| 965 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); | |
| 966 else | |
| 913 | 967 s->scene_change_score+= s->qscale; |
| 608 | 968 |
| 294 | 969 if (vard*2 + 200 > varc) |
| 970 mb_type|= MB_TYPE_INTRA; | |
| 971 if (varc*2 + 200 > vard){ | |
| 972 mb_type|= MB_TYPE_INTER; | |
| 936 | 973 s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 974 pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty); | |
| 304 | 975 }else{ |
| 936 | 976 mx <<=shift; |
| 977 my <<=shift; | |
| 0 | 978 } |
| 455 | 979 if((s->flags&CODEC_FLAG_4MV) |
| 936 | 980 && !s->me.skip && varc>50 && vard>10){ |
| 455 | 981 mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); |
| 982 mb_type|=MB_TYPE_INTER4V; | |
| 983 | |
| 984 set_p_mv_tables(s, mx, my, 0); | |
| 985 }else | |
| 986 set_p_mv_tables(s, mx, my, 1); | |
| 294 | 987 }else{ |
| 988 if (vard <= 64 || vard < varc) { | |
| 936 | 989 // if (sadP <= 32 || sadP < sadI + 500) { |
| 608 | 990 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); |
| 294 | 991 mb_type|= MB_TYPE_INTER; |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
992 if (s->me_method != ME_ZERO) { |
| 936 | 993 dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 994 pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty); | |
| 455 | 995 if((s->flags&CODEC_FLAG_4MV) |
| 936 | 996 && !s->me.skip && varc>50 && vard>10){ |
| 455 | 997 int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); |
| 998 if(dmin4 + 128 <dmin) | |
| 999 mb_type= MB_TYPE_INTER4V; | |
| 1000 } | |
| 1001 set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V); | |
| 1002 | |
| 294 | 1003 } else { |
| 936 | 1004 mx <<=shift; |
| 1005 my <<=shift; | |
| 294 | 1006 } |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1007 #if 0 |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1008 if (vard < 10) { |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1009 skip++; |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1010 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d", |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1011 skip, vard, varc, dmin); |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1012 } |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1013 #endif |
| 294 | 1014 }else{ |
| 608 | 1015 s->scene_change_score+= 20; |
| 294 | 1016 mb_type|= MB_TYPE_INTRA; |
| 455 | 1017 mx = 0; |
| 1018 my = 0; | |
| 294 | 1019 } |
| 1020 } | |
| 284 | 1021 |
| 294 | 1022 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
| 0 | 1023 } |
| 1024 | |
| 951 | 1025 int ff_pre_estimate_p_frame_motion(MpegEncContext * s, |
| 1026 int mb_x, int mb_y) | |
| 1027 { | |
| 1028 int mx, my, range, dmin; | |
| 1029 int xmin, ymin, xmax, ymax; | |
| 1030 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | |
| 1031 int pred_x=0, pred_y=0; | |
| 1032 int P[10][2]; | |
| 1033 const int shift= 1+s->quarter_sample; | |
| 1034 uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; | |
| 1035 const int mv_stride= s->mb_width + 2; | |
| 1036 const int xy= mb_x + 1 + (mb_y + 1)*mv_stride; | |
| 1037 | |
| 1038 assert(s->quarter_sample==0 || s->quarter_sample==1); | |
| 1039 | |
| 1040 s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); | |
| 1041 | |
| 1042 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); | |
| 1043 rel_xmin= xmin - mb_x*16; | |
| 1044 rel_xmax= xmax - mb_x*16; | |
| 1045 rel_ymin= ymin - mb_y*16; | |
| 1046 rel_ymax= ymax - mb_y*16; | |
| 1047 s->me.skip=0; | |
| 1048 | |
| 1049 P_LEFT[0] = s->p_mv_table[xy + 1][0]; | |
| 1050 P_LEFT[1] = s->p_mv_table[xy + 1][1]; | |
| 1051 | |
| 1052 if(P_LEFT[0] < (rel_xmin<<shift)) P_LEFT[0] = (rel_xmin<<shift); | |
| 1053 | |
| 1054 /* special case for first line */ | |
| 1055 if (mb_y == s->mb_height-1) { | |
| 1056 pred_x= P_LEFT[0]; | |
| 1057 pred_y= P_LEFT[1]; | |
| 1058 } else { | |
| 1059 P_TOP[0] = s->p_mv_table[xy + mv_stride ][0]; | |
| 1060 P_TOP[1] = s->p_mv_table[xy + mv_stride ][1]; | |
| 1061 P_TOPRIGHT[0] = s->p_mv_table[xy + mv_stride - 1][0]; | |
| 1062 P_TOPRIGHT[1] = s->p_mv_table[xy + mv_stride - 1][1]; | |
| 1063 if(P_TOP[1] < (rel_ymin<<shift)) P_TOP[1] = (rel_ymin<<shift); | |
| 1064 if(P_TOPRIGHT[0] > (rel_xmax<<shift)) P_TOPRIGHT[0]= (rel_xmax<<shift); | |
| 1065 if(P_TOPRIGHT[1] < (rel_ymin<<shift)) P_TOPRIGHT[1]= (rel_ymin<<shift); | |
| 1066 | |
| 1067 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
| 1068 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 1069 | |
| 1070 if(s->out_format == FMT_H263){ | |
| 1071 pred_x = P_MEDIAN[0]; | |
| 1072 pred_y = P_MEDIAN[1]; | |
| 1073 }else { /* mpeg1 at least */ | |
| 1074 pred_x= P_LEFT[0]; | |
| 1075 pred_y= P_LEFT[1]; | |
| 1076 } | |
| 1077 } | |
| 1078 dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, | |
| 1079 &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); | |
| 1080 | |
| 1081 s->p_mv_table[xy][0] = mx<<shift; | |
| 1082 s->p_mv_table[xy][1] = my<<shift; | |
| 1083 | |
| 1084 return dmin; | |
| 1085 } | |
| 1086 | |
| 327 | 1087 int ff_estimate_motion_b(MpegEncContext * s, |
| 936 | 1088 int mb_x, int mb_y, int16_t (*mv_table)[2], Picture *picture, int f_code) |
| 0 | 1089 { |
| 327 | 1090 int mx, my, range, dmin; |
| 324 | 1091 int xmin, ymin, xmax, ymax; |
| 1092 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | |
| 1093 int pred_x=0, pred_y=0; | |
| 455 | 1094 int P[10][2]; |
| 324 | 1095 const int shift= 1+s->quarter_sample; |
| 1096 const int mot_stride = s->mb_width + 2; | |
| 1097 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
| 936 | 1098 uint8_t * const ref_picture= picture->data[0]; |
| 1099 uint16_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV; | |
| 948 | 1100 int mv_scale; |
| 936 | 1101 |
| 1102 s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); | |
| 1103 s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); | |
| 1104 | |
| 324 | 1105 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code); |
| 455 | 1106 rel_xmin= xmin - mb_x*16; |
| 1107 rel_xmax= xmax - mb_x*16; | |
| 1108 rel_ymin= ymin - mb_y*16; | |
| 1109 rel_ymax= ymax - mb_y*16; | |
| 324 | 1110 |
| 1111 switch(s->me_method) { | |
| 1112 case ME_ZERO: | |
| 1113 default: | |
| 1114 no_motion_search(s, &mx, &my); | |
| 1115 dmin = 0; | |
| 455 | 1116 mx-= mb_x*16; |
| 1117 my-= mb_y*16; | |
| 324 | 1118 break; |
| 1119 case ME_FULL: | |
| 1120 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); | |
| 455 | 1121 mx-= mb_x*16; |
| 1122 my-= mb_y*16; | |
| 324 | 1123 break; |
| 1124 case ME_LOG: | |
| 1125 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | |
| 455 | 1126 mx-= mb_x*16; |
| 1127 my-= mb_y*16; | |
| 324 | 1128 break; |
| 1129 case ME_PHODS: | |
| 1130 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | |
| 455 | 1131 mx-= mb_x*16; |
| 1132 my-= mb_y*16; | |
| 324 | 1133 break; |
| 1134 case ME_X1: | |
| 1135 case ME_EPZS: | |
| 1136 { | |
| 455 | 1137 P_LEFT[0] = mv_table[mot_xy - 1][0]; |
| 1138 P_LEFT[1] = mv_table[mot_xy - 1][1]; | |
| 324 | 1139 |
| 455 | 1140 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); |
| 324 | 1141 |
| 1142 /* special case for first line */ | |
| 455 | 1143 if ((mb_y == 0 || s->first_slice_line)) { |
| 324 | 1144 } else { |
| 455 | 1145 P_TOP[0] = mv_table[mot_xy - mot_stride ][0]; |
| 1146 P_TOP[1] = mv_table[mot_xy - mot_stride ][1]; | |
| 1147 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0]; | |
| 1148 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1]; | |
| 1149 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift); | |
| 1150 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift); | |
| 1151 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift); | |
| 324 | 1152 |
| 455 | 1153 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
| 1154 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 324 | 1155 } |
| 455 | 1156 pred_x= P_LEFT[0]; |
| 1157 pred_y= P_LEFT[1]; | |
| 324 | 1158 } |
| 948 | 1159 |
| 1160 if(mv_table == s->b_forw_mv_table){ | |
| 1161 mv_scale= (s->pb_time<<16) / (s->pp_time<<shift); | |
| 1162 }else{ | |
| 1163 mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift); | |
| 1164 } | |
| 1165 | |
| 936 | 1166 dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 948 | 1167 picture, s->p_mv_table, mv_scale, mv_penalty); |
| 324 | 1168 |
| 1169 break; | |
| 1170 } | |
| 1171 | |
| 936 | 1172 dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 1173 pred_x, pred_y, picture, 0, 0, mv_penalty); | |
| 455 | 1174 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my); |
| 324 | 1175 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
| 1176 mv_table[mot_xy][0]= mx; | |
| 1177 mv_table[mot_xy][1]= my; | |
| 936 | 1178 |
| 327 | 1179 return dmin; |
| 324 | 1180 } |
| 1181 | |
| 327 | 1182 static inline int check_bidir_mv(MpegEncContext * s, |
| 1183 int mb_x, int mb_y, | |
| 1184 int motion_fx, int motion_fy, | |
| 1185 int motion_bx, int motion_by, | |
| 1186 int pred_fx, int pred_fy, | |
| 1187 int pred_bx, int pred_by) | |
| 1188 { | |
| 1189 //FIXME optimize? | |
| 936 | 1190 //FIXME move into template? |
| 1191 //FIXME better f_code prediction (max mv & distance) | |
| 1192 UINT16 *mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | |
| 1193 uint8_t *dest_y = s->me.scratchpad; | |
| 327 | 1194 uint8_t *ptr; |
| 1195 int dxy; | |
| 1196 int src_x, src_y; | |
| 1197 int fbmin; | |
| 1198 | |
| 936 | 1199 if(s->quarter_sample){ |
| 1200 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3); | |
| 1201 src_x = mb_x * 16 + (motion_fx >> 2); | |
| 1202 src_y = mb_y * 16 + (motion_fy >> 2); | |
| 1203 assert(src_x >=-16 && src_x<=s->width); | |
| 1204 assert(src_y >=-16 && src_y<=s->height); | |
| 327 | 1205 |
| 936 | 1206 ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x; |
| 1207 s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize); | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1208 |
| 936 | 1209 dxy = ((motion_by & 3) << 2) | (motion_bx & 3); |
| 1210 src_x = mb_x * 16 + (motion_bx >> 2); | |
| 1211 src_y = mb_y * 16 + (motion_by >> 2); | |
| 1212 assert(src_x >=-16 && src_x<=s->width); | |
| 1213 assert(src_y >=-16 && src_y<=s->height); | |
| 1214 | |
| 1215 ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x; | |
| 1216 s->dsp.avg_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize); | |
| 1217 }else{ | |
| 1218 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1); | |
| 1219 src_x = mb_x * 16 + (motion_fx >> 1); | |
| 1220 src_y = mb_y * 16 + (motion_fy >> 1); | |
| 1221 assert(src_x >=-16 && src_x<=s->width); | |
| 1222 assert(src_y >=-16 && src_y<=s->height); | |
| 327 | 1223 |
| 936 | 1224 ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x; |
| 1225 s->dsp.put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16); | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1226 |
| 936 | 1227 dxy = ((motion_by & 1) << 1) | (motion_bx & 1); |
| 1228 src_x = mb_x * 16 + (motion_bx >> 1); | |
| 1229 src_y = mb_y * 16 + (motion_by >> 1); | |
| 1230 assert(src_x >=-16 && src_x<=s->width); | |
| 1231 assert(src_y >=-16 && src_y<=s->height); | |
| 1232 | |
| 1233 ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x; | |
| 1234 s->dsp.avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16); | |
| 1235 } | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1236 |
| 936 | 1237 fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.sub_penalty_factor |
| 1238 +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.sub_penalty_factor; | |
| 1239 + s->dsp.me_sub_cmp[0](s, s->new_picture.data[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize); | |
| 1240 | |
| 327 | 1241 return fbmin; |
| 1242 } | |
| 1243 | |
| 1244 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/ | |
| 1245 static inline int bidir_refine(MpegEncContext * s, | |
| 1246 int mb_x, int mb_y) | |
| 1247 { | |
| 1248 const int mot_stride = s->mb_width + 2; | |
| 1249 const int xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
| 1250 int fbmin; | |
| 1251 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0]; | |
| 1252 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1]; | |
| 1253 int pred_bx= s->b_bidir_back_mv_table[xy-1][0]; | |
| 1254 int pred_by= s->b_bidir_back_mv_table[xy-1][1]; | |
| 1255 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0]; | |
| 1256 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1]; | |
| 1257 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0]; | |
| 1258 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1]; | |
| 1259 | |
| 1260 //FIXME do refinement and add flag | |
| 1261 | |
| 1262 fbmin= check_bidir_mv(s, mb_x, mb_y, | |
| 1263 motion_fx, motion_fy, | |
| 1264 motion_bx, motion_by, | |
| 1265 pred_fx, pred_fy, | |
| 1266 pred_bx, pred_by); | |
| 1267 | |
| 1268 return fbmin; | |
| 1269 } | |
| 1270 | |
| 1271 static inline int direct_search(MpegEncContext * s, | |
| 1272 int mb_x, int mb_y) | |
| 324 | 1273 { |
| 455 | 1274 int P[10][2]; |
| 327 | 1275 const int mot_stride = s->mb_width + 2; |
| 1276 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
| 936 | 1277 const int shift= 1+s->quarter_sample; |
| 1278 int dmin, i; | |
| 327 | 1279 const int time_pp= s->pp_time; |
| 664 | 1280 const int time_pb= s->pb_time; |
| 936 | 1281 int mx, my, xmin, xmax, ymin, ymax; |
| 327 | 1282 int16_t (*mv_table)[2]= s->b_direct_mv_table; |
| 936 | 1283 uint16_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV; |
| 1284 | |
| 1285 ymin= xmin=(-32)>>shift; | |
| 1286 ymax= xmax= 31>>shift; | |
| 1287 | |
| 1288 if(s->co_located_type_table[mb_x + mb_y*s->mb_width]==CO_LOCATED_TYPE_4MV){ | |
| 1289 s->mv_type= MV_TYPE_8X8; | |
| 1290 }else{ | |
| 1291 s->mv_type= MV_TYPE_16X16; | |
| 327 | 1292 } |
| 936 | 1293 |
| 1294 for(i=0; i<4; i++){ | |
| 1295 int index= s->block_index[i]; | |
| 1296 int min, max; | |
| 1297 | |
| 1298 s->me.co_located_mv[i][0]= s->motion_val[index][0]; | |
| 1299 s->me.co_located_mv[i][1]= s->motion_val[index][1]; | |
| 1300 s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3)); | |
| 1301 s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3)); | |
| 1302 // s->me.direct_basis_mv[1][i][0]= s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3); | |
| 1303 // s->me.direct_basis_mv[1][i][1]= s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3); | |
| 1304 | |
| 1305 max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; | |
| 1306 min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; | |
| 1307 max+= (2*mb_x + (i& 1))*8 - 1; // +-1 is for the simpler rounding | |
| 1308 min+= (2*mb_x + (i& 1))*8 + 1; | |
| 1309 if(max >= s->width) xmax= s->width - max - 1; | |
| 1310 if(min < -16 ) xmin= - 32 - min; | |
| 1311 | |
| 1312 max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; | |
| 1313 min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; | |
| 1314 max+= (2*mb_y + (i>>1))*8 - 1; // +-1 is for the simpler rounding | |
| 1315 min+= (2*mb_y + (i>>1))*8 + 1; | |
| 1316 if(max >= s->height) ymax= s->height - max - 1; | |
| 1317 if(min < -16 ) ymin= - 32 - min; | |
| 1318 | |
| 1319 if(s->mv_type == MV_TYPE_16X16) break; | |
| 327 | 1320 } |
| 936 | 1321 |
| 1322 assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16); | |
| 1323 | |
| 1324 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){ | |
| 1325 s->b_direct_mv_table[mot_xy][0]= 0; | |
| 1326 s->b_direct_mv_table[mot_xy][1]= 0; | |
| 1327 | |
| 1328 return 256*256*256*64; | |
| 1329 } | |
| 1330 | |
| 950 | 1331 P_LEFT[0] = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift); |
| 1332 P_LEFT[1] = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift); | |
| 1333 | |
| 1334 /* special case for first line */ | |
| 1335 if ((mb_y == 0 || s->first_slice_line)) { | |
| 1336 } else { | |
| 1337 P_TOP[0] = clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift); | |
| 1338 P_TOP[1] = clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift); | |
| 1339 P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift); | |
| 1340 P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift); | |
| 1341 | |
| 1342 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
| 1343 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 1344 } | |
| 1345 | |
| 936 | 1346 if(s->flags&CODEC_FLAG_QPEL){ |
| 1347 dmin = simple_direct_qpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, | |
| 948 | 1348 &s->last_picture, mv_table, 1<<14, mv_penalty); |
| 936 | 1349 dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, |
| 1350 0, 0, &s->last_picture, 0, 0, mv_penalty); | |
| 1351 }else{ | |
| 1352 dmin = simple_direct_hpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, | |
| 948 | 1353 &s->last_picture, mv_table, 1<<15, mv_penalty); |
| 936 | 1354 dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, |
| 1355 0, 0, &s->last_picture, 0, 0, mv_penalty); | |
| 327 | 1356 } |
| 1357 | |
| 1358 s->b_direct_mv_table[mot_xy][0]= mx; | |
| 1359 s->b_direct_mv_table[mot_xy][1]= my; | |
| 1360 return dmin; | |
| 324 | 1361 } |
| 1362 | |
| 1363 void ff_estimate_b_frame_motion(MpegEncContext * s, | |
| 1364 int mb_x, int mb_y) | |
| 1365 { | |
| 936 | 1366 const int penalty_factor= s->me.penalty_factor; |
| 327 | 1367 int fmin, bmin, dmin, fbmin; |
| 1368 int type=0; | |
| 1369 | |
| 1370 dmin= direct_search(s, mb_x, mb_y); | |
| 324 | 1371 |
| 936 | 1372 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, &s->last_picture, s->f_code); |
| 1373 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, &s->next_picture, s->b_code) - penalty_factor; | |
| 324 | 1374 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); |
| 327 | 1375 |
| 1376 fbmin= bidir_refine(s, mb_x, mb_y); | |
| 1377 | |
| 612 | 1378 { |
| 327 | 1379 int score= dmin; |
| 1380 type=MB_TYPE_DIRECT; | |
| 1381 | |
| 1382 if(fmin<score){ | |
| 1383 score=fmin; | |
| 1384 type= MB_TYPE_FORWARD; | |
| 1385 } | |
| 1386 if(bmin<score){ | |
| 1387 score=bmin; | |
| 1388 type= MB_TYPE_BACKWARD; | |
| 1389 } | |
| 1390 if(fbmin<score){ | |
| 1391 score=fbmin; | |
| 1392 type= MB_TYPE_BIDIR; | |
| 1393 } | |
|
807
0e1d375c537f
fixing q>0.0 assert failure caused by overflow of variance for b frames
michaelni
parents:
765
diff
changeset
|
1394 score= ((unsigned)(score*score + 128*256))>>16; |
| 903 | 1395 s->current_picture.mc_mb_var_sum += score; |
| 1396 s->current_picture.mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSD | |
| 327 | 1397 } |
| 612 | 1398 |
| 1399 if(s->flags&CODEC_FLAG_HQ){ | |
| 1400 type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter | |
| 936 | 1401 if(dmin>256*256*16) type&= ~MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB |
| 612 | 1402 } |
| 1403 | |
| 327 | 1404 s->mb_type[mb_y*s->mb_width + mb_x]= type; |
| 324 | 1405 } |
| 0 | 1406 |
| 324 | 1407 /* find best f_code for ME which do unlimited searches */ |
| 1408 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) | |
| 1409 { | |
| 1410 if(s->me_method>=ME_EPZS){ | |
| 455 | 1411 int score[8]; |
| 324 | 1412 int i, y; |
| 1413 UINT8 * fcode_tab= s->fcode_tab; | |
| 455 | 1414 int best_fcode=-1; |
| 1415 int best_score=-10000000; | |
| 324 | 1416 |
| 936 | 1417 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i); |
| 324 | 1418 |
| 1419 for(y=0; y<s->mb_height; y++){ | |
| 1420 int x; | |
| 1421 int xy= (y+1)* (s->mb_width+2) + 1; | |
| 1422 i= y*s->mb_width; | |
| 1423 for(x=0; x<s->mb_width; x++){ | |
| 1424 if(s->mb_type[i] & type){ | |
| 847 | 1425 int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV], |
| 1426 fcode_tab[mv_table[xy][1] + MAX_MV]); | |
| 455 | 1427 int j; |
| 1428 | |
| 1429 for(j=0; j<fcode && j<8; j++){ | |
| 903 | 1430 if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[i] < s->current_picture.mb_var[i]) |
| 455 | 1431 score[j]-= 170; |
| 1432 } | |
| 324 | 1433 } |
| 1434 i++; | |
| 1435 xy++; | |
| 1436 } | |
| 1437 } | |
| 455 | 1438 |
| 1439 for(i=1; i<8; i++){ | |
| 1440 if(score[i] > best_score){ | |
| 1441 best_score= score[i]; | |
| 1442 best_fcode= i; | |
| 1443 } | |
| 1444 // printf("%d %d\n", i, score[i]); | |
| 1445 } | |
| 327 | 1446 |
| 324 | 1447 // printf("fcode: %d type: %d\n", i, s->pict_type); |
| 455 | 1448 return best_fcode; |
| 324 | 1449 /* for(i=0; i<=MAX_FCODE; i++){ |
| 1450 printf("%d ", mv_num[i]); | |
| 1451 } | |
| 1452 printf("\n");*/ | |
| 1453 }else{ | |
| 1454 return 1; | |
| 0 | 1455 } |
| 1456 } | |
| 1457 | |
| 324 | 1458 void ff_fix_long_p_mvs(MpegEncContext * s) |
| 1459 { | |
| 1460 const int f_code= s->f_code; | |
| 1461 int y; | |
| 1462 UINT8 * fcode_tab= s->fcode_tab; | |
| 455 | 1463 //int clip=0; |
| 1464 //int noclip=0; | |
| 324 | 1465 /* clip / convert to intra 16x16 type MVs */ |
| 1466 for(y=0; y<s->mb_height; y++){ | |
| 1467 int x; | |
| 1468 int xy= (y+1)* (s->mb_width+2)+1; | |
| 1469 int i= y*s->mb_width; | |
| 1470 for(x=0; x<s->mb_width; x++){ | |
| 1471 if(s->mb_type[i]&MB_TYPE_INTER){ | |
| 1472 if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code | |
| 1473 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0 | |
| 1474 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code | |
| 1475 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){ | |
| 1476 s->mb_type[i] &= ~MB_TYPE_INTER; | |
| 1477 s->mb_type[i] |= MB_TYPE_INTRA; | |
| 1478 s->p_mv_table[xy][0] = 0; | |
| 1479 s->p_mv_table[xy][1] = 0; | |
| 455 | 1480 //clip++; |
| 324 | 1481 } |
| 455 | 1482 //else |
| 1483 // noclip++; | |
| 324 | 1484 } |
| 1485 xy++; | |
| 1486 i++; | |
| 1487 } | |
| 1488 } | |
| 455 | 1489 //printf("%d no:%d %d//\n", clip, noclip, f_code); |
| 324 | 1490 if(s->flags&CODEC_FLAG_4MV){ |
| 1491 const int wrap= 2+ s->mb_width*2; | |
| 1492 | |
| 1493 /* clip / convert to intra 8x8 type MVs */ | |
| 1494 for(y=0; y<s->mb_height; y++){ | |
| 1495 int xy= (y*2 + 1)*wrap + 1; | |
| 1496 int i= y*s->mb_width; | |
| 1497 int x; | |
| 1498 | |
| 1499 for(x=0; x<s->mb_width; x++){ | |
| 1500 if(s->mb_type[i]&MB_TYPE_INTER4V){ | |
| 1501 int block; | |
| 1502 for(block=0; block<4; block++){ | |
| 1503 int off= (block& 1) + (block>>1)*wrap; | |
| 1504 int mx= s->motion_val[ xy + off ][0]; | |
| 1505 int my= s->motion_val[ xy + off ][1]; | |
| 1506 | |
| 1507 if( fcode_tab[mx + MAX_MV] > f_code | |
| 1508 || fcode_tab[mx + MAX_MV] == 0 | |
| 1509 || fcode_tab[my + MAX_MV] > f_code | |
| 1510 || fcode_tab[my + MAX_MV] == 0 ){ | |
| 1511 s->mb_type[i] &= ~MB_TYPE_INTER4V; | |
| 1512 s->mb_type[i] |= MB_TYPE_INTRA; | |
| 1513 } | |
| 1514 } | |
| 1515 } | |
| 502 | 1516 xy+=2; |
| 1517 i++; | |
| 324 | 1518 } |
| 1519 } | |
| 1520 } | |
| 1521 } | |
| 1522 | |
| 1523 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type) | |
| 1524 { | |
| 1525 int y; | |
| 1526 UINT8 * fcode_tab= s->fcode_tab; | |
| 1527 | |
| 1528 /* clip / convert to intra 16x16 type MVs */ | |
| 1529 for(y=0; y<s->mb_height; y++){ | |
| 1530 int x; | |
| 1531 int xy= (y+1)* (s->mb_width+2)+1; | |
| 1532 int i= y*s->mb_width; | |
| 1533 for(x=0; x<s->mb_width; x++){ | |
|
691
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1534 if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1535 || fcode_tab[mv_table[xy][0] + MAX_MV] == 0){ |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1536 if(mv_table[xy][0]>0) mv_table[xy][0]= (16<<f_code)-1; |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1537 else mv_table[xy][0]= -(16<<f_code); |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1538 } |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1539 if( fcode_tab[mv_table[xy][1] + MAX_MV] > f_code |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1540 || fcode_tab[mv_table[xy][1] + MAX_MV] == 0){ |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1541 if(mv_table[xy][1]>0) mv_table[xy][1]= (16<<f_code)-1; |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1542 else mv_table[xy][1]= -(16<<f_code); |
| 324 | 1543 } |
| 1544 xy++; | |
| 1545 i++; | |
| 1546 } | |
| 1547 } | |
| 1548 } |
