Mercurial > libavcodec.hg
annotate motion_est.c @ 1007:b2cf2a1d9a51 libavcodec
more compare functions (rd & bit)
| author | michaelni |
|---|---|
| date | Wed, 15 Jan 2003 18:05:23 +0000 |
| parents | dd421045b4ce |
| children | 3b7fcfb9c551 |
| 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; | |
| 1007 | 289 case FF_CMP_BIT: |
| 290 cmp[0]= c->bit[0]; | |
| 291 cmp[1]= c->bit[1]; | |
| 292 break; | |
| 293 case FF_CMP_RD: | |
| 294 cmp[0]= c->rd[0]; | |
| 295 cmp[1]= c->rd[1]; | |
| 296 break; | |
| 936 | 297 case FF_CMP_ZERO: |
| 298 for(i=0; i<7; i++){ | |
| 299 cmp[i]= zero_cmp; | |
| 300 } | |
| 301 break; | |
| 302 default: | |
| 303 fprintf(stderr,"internal error in cmp function selection\n"); | |
| 304 } | |
| 305 }; | |
| 306 | |
| 307 static inline int get_penalty_factor(MpegEncContext *s, int type){ | |
| 308 | |
| 309 switch(type){ | |
| 310 default: | |
| 311 case FF_CMP_SAD: | |
| 312 return s->qscale; | |
| 313 case FF_CMP_DCT: | |
| 314 case FF_CMP_SATD: | |
| 1007 | 315 case FF_CMP_SSE: |
| 936 | 316 return s->qscale*8; |
| 1007 | 317 case FF_CMP_BIT: |
| 318 return 1; | |
| 319 case FF_CMP_RD: | |
| 320 return (s->qscale*s->qscale*105 + 64)>>7; | |
| 936 | 321 } |
| 322 } | |
| 323 | |
| 324 void ff_init_me(MpegEncContext *s){ | |
| 954 | 325 set_cmp(s, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp); |
| 936 | 326 set_cmp(s, s->dsp.me_cmp, s->avctx->me_cmp); |
| 327 set_cmp(s, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp); | |
| 328 set_cmp(s, s->dsp.mb_cmp, s->avctx->mb_cmp); | |
| 329 | |
| 330 if(s->flags&CODEC_FLAG_QPEL){ | |
| 331 if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) | |
| 332 s->me.sub_motion_search= simple_chroma_qpel_motion_search; | |
| 333 else | |
| 334 s->me.sub_motion_search= simple_qpel_motion_search; | |
| 335 }else{ | |
| 336 if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) | |
| 337 s->me.sub_motion_search= simple_chroma_hpel_motion_search; | |
| 338 else if(s->avctx->me_sub_cmp == FF_CMP_SAD && s->avctx->me_cmp == FF_CMP_SAD) | |
| 339 s->me.sub_motion_search= sad_hpel_motion_search; | |
| 340 else | |
| 341 s->me.sub_motion_search= simple_hpel_motion_search; | |
| 342 } | |
| 343 | |
| 344 if(s->avctx->me_cmp&FF_CMP_CHROMA){ | |
| 345 s->me.motion_search[0]= simple_chroma_epzs_motion_search; | |
| 346 s->me.motion_search[1]= simple_chroma_epzs_motion_search4; | |
| 347 }else{ | |
| 348 s->me.motion_search[0]= simple_epzs_motion_search; | |
| 349 s->me.motion_search[1]= simple_epzs_motion_search4; | |
| 350 } | |
| 954 | 351 |
| 352 if(s->avctx->me_pre_cmp&FF_CMP_CHROMA){ | |
| 353 s->me.pre_motion_search= simple_chroma_epzs_motion_search; | |
| 354 }else{ | |
| 355 s->me.pre_motion_search= simple_epzs_motion_search; | |
| 356 } | |
| 936 | 357 } |
| 358 | |
| 284 | 359 static int pix_dev(UINT8 * pix, int line_size, int mean) |
| 360 { | |
| 361 int s, i, j; | |
| 362 | |
| 363 s = 0; | |
| 364 for (i = 0; i < 16; i++) { | |
| 365 for (j = 0; j < 16; j += 8) { | |
| 366 s += ABS(pix[0]-mean); | |
| 367 s += ABS(pix[1]-mean); | |
| 368 s += ABS(pix[2]-mean); | |
| 369 s += ABS(pix[3]-mean); | |
| 370 s += ABS(pix[4]-mean); | |
| 371 s += ABS(pix[5]-mean); | |
| 372 s += ABS(pix[6]-mean); | |
| 373 s += ABS(pix[7]-mean); | |
| 374 pix += 8; | |
| 375 } | |
| 376 pix += line_size - 16; | |
| 377 } | |
| 378 return s; | |
| 379 } | |
| 380 | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
381 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
|
382 int *mx_ptr, int *my_ptr) |
| 0 | 383 { |
| 384 *mx_ptr = 16 * s->mb_x; | |
| 385 *my_ptr = 16 * s->mb_y; | |
| 386 } | |
| 387 | |
| 388 static int full_motion_search(MpegEncContext * s, | |
| 389 int *mx_ptr, int *my_ptr, int range, | |
| 324 | 390 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
| 0 | 391 { |
| 392 int x1, y1, x2, y2, xx, yy, x, y; | |
| 393 int mx, my, dmin, d; | |
| 394 UINT8 *pix; | |
| 395 | |
| 396 xx = 16 * s->mb_x; | |
| 397 yy = 16 * s->mb_y; | |
| 398 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */ | |
| 399 if (x1 < xmin) | |
| 400 x1 = xmin; | |
| 401 x2 = xx + range - 1; | |
| 402 if (x2 > xmax) | |
| 403 x2 = xmax; | |
| 404 y1 = yy - range + 1; | |
| 405 if (y1 < ymin) | |
| 406 y1 = ymin; | |
| 407 y2 = yy + range - 1; | |
| 408 if (y2 > ymax) | |
| 409 y2 = ymax; | |
| 903 | 410 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 0 | 411 dmin = 0x7fffffff; |
| 412 mx = 0; | |
| 413 my = 0; | |
| 414 for (y = y1; y <= y2; y++) { | |
| 415 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
|
416 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, |
| 294 | 417 s->linesize); |
| 0 | 418 if (d < dmin || |
| 419 (d == dmin && | |
| 420 (abs(x - xx) + abs(y - yy)) < | |
| 421 (abs(mx - xx) + abs(my - yy)))) { | |
| 422 dmin = d; | |
| 423 mx = x; | |
| 424 my = y; | |
| 425 } | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 *mx_ptr = mx; | |
| 430 *my_ptr = my; | |
| 431 | |
| 432 #if 0 | |
| 433 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) || | |
| 434 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) { | |
| 435 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr); | |
| 436 } | |
| 437 #endif | |
| 438 return dmin; | |
| 439 } | |
| 440 | |
| 441 | |
| 442 static int log_motion_search(MpegEncContext * s, | |
| 443 int *mx_ptr, int *my_ptr, int range, | |
| 324 | 444 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
| 0 | 445 { |
| 446 int x1, y1, x2, y2, xx, yy, x, y; | |
| 447 int mx, my, dmin, d; | |
| 448 UINT8 *pix; | |
| 449 | |
| 450 xx = s->mb_x << 4; | |
| 451 yy = s->mb_y << 4; | |
| 452 | |
| 453 /* Left limit */ | |
| 454 x1 = xx - range; | |
| 455 if (x1 < xmin) | |
| 456 x1 = xmin; | |
| 457 | |
| 458 /* Right limit */ | |
| 459 x2 = xx + range; | |
| 460 if (x2 > xmax) | |
| 461 x2 = xmax; | |
| 462 | |
| 463 /* Upper limit */ | |
| 464 y1 = yy - range; | |
| 465 if (y1 < ymin) | |
| 466 y1 = ymin; | |
| 467 | |
| 468 /* Lower limit */ | |
| 469 y2 = yy + range; | |
| 470 if (y2 > ymax) | |
| 471 y2 = ymax; | |
| 472 | |
| 903 | 473 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 0 | 474 dmin = 0x7fffffff; |
| 475 mx = 0; | |
| 476 my = 0; | |
| 477 | |
| 478 do { | |
| 479 for (y = y1; y <= y2; y += range) { | |
| 480 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
|
481 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
| 0 | 482 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
| 483 dmin = d; | |
| 484 mx = x; | |
| 485 my = y; | |
| 486 } | |
| 487 } | |
| 488 } | |
| 489 | |
| 490 range = range >> 1; | |
| 491 | |
| 492 x1 = mx - range; | |
| 493 if (x1 < xmin) | |
| 494 x1 = xmin; | |
| 495 | |
| 496 x2 = mx + range; | |
| 497 if (x2 > xmax) | |
| 498 x2 = xmax; | |
| 499 | |
| 500 y1 = my - range; | |
| 501 if (y1 < ymin) | |
| 502 y1 = ymin; | |
| 503 | |
| 504 y2 = my + range; | |
| 505 if (y2 > ymax) | |
| 506 y2 = ymax; | |
| 507 | |
| 508 } while (range >= 1); | |
| 509 | |
| 510 #ifdef DEBUG | |
| 511 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my); | |
| 512 #endif | |
| 513 *mx_ptr = mx; | |
| 514 *my_ptr = my; | |
| 515 return dmin; | |
| 516 } | |
| 517 | |
| 518 static int phods_motion_search(MpegEncContext * s, | |
| 519 int *mx_ptr, int *my_ptr, int range, | |
| 324 | 520 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
| 0 | 521 { |
| 522 int x1, y1, x2, y2, xx, yy, x, y, lastx, d; | |
| 523 int mx, my, dminx, dminy; | |
| 524 UINT8 *pix; | |
| 525 | |
| 526 xx = s->mb_x << 4; | |
| 527 yy = s->mb_y << 4; | |
| 528 | |
| 529 /* Left limit */ | |
| 530 x1 = xx - range; | |
| 531 if (x1 < xmin) | |
| 532 x1 = xmin; | |
| 533 | |
| 534 /* Right limit */ | |
| 535 x2 = xx + range; | |
| 536 if (x2 > xmax) | |
| 537 x2 = xmax; | |
| 538 | |
| 539 /* Upper limit */ | |
| 540 y1 = yy - range; | |
| 541 if (y1 < ymin) | |
| 542 y1 = ymin; | |
| 543 | |
| 544 /* Lower limit */ | |
| 545 y2 = yy + range; | |
| 546 if (y2 > ymax) | |
| 547 y2 = ymax; | |
| 548 | |
| 903 | 549 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 0 | 550 mx = 0; |
| 551 my = 0; | |
| 552 | |
| 553 x = xx; | |
| 554 y = yy; | |
| 555 do { | |
| 556 dminx = 0x7fffffff; | |
| 557 dminy = 0x7fffffff; | |
| 558 | |
| 559 lastx = x; | |
| 560 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
|
561 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
| 0 | 562 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
| 563 dminx = d; | |
| 564 mx = x; | |
| 565 } | |
| 566 } | |
| 567 | |
| 568 x = lastx; | |
| 569 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
|
570 d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); |
| 0 | 571 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { |
| 572 dminy = d; | |
| 573 my = y; | |
| 574 } | |
| 575 } | |
| 576 | |
| 577 range = range >> 1; | |
| 578 | |
| 579 x = mx; | |
| 580 y = my; | |
| 581 x1 = mx - range; | |
| 582 if (x1 < xmin) | |
| 583 x1 = xmin; | |
| 584 | |
| 585 x2 = mx + range; | |
| 586 if (x2 > xmax) | |
| 587 x2 = xmax; | |
| 588 | |
| 589 y1 = my - range; | |
| 590 if (y1 < ymin) | |
| 591 y1 = ymin; | |
| 592 | |
| 593 y2 = my + range; | |
| 594 if (y2 > ymax) | |
| 595 y2 = ymax; | |
| 596 | |
| 597 } while (range >= 1); | |
| 598 | |
| 599 #ifdef DEBUG | |
| 600 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my); | |
| 601 #endif | |
| 602 | |
| 603 /* half pixel search */ | |
| 604 *mx_ptr = mx; | |
| 605 *my_ptr = my; | |
| 606 return dminy; | |
| 607 } | |
| 608 | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
609 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
610 #define Z_THRESHOLD 256 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
611 |
| 936 | 612 #define CHECK_SAD_HALF_MV(suffix, x, y) \ |
| 455 | 613 {\ |
| 614 d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ | |
| 936 | 615 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\ |
| 455 | 616 COPY3_IF_LT(dminh, d, dx, x, dy, y)\ |
| 617 } | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
618 |
| 936 | 619 static inline int sad_hpel_motion_search(MpegEncContext * s, |
| 0 | 620 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
|
621 int xmin, int ymin, int xmax, int ymax, |
| 936 | 622 int pred_x, int pred_y, Picture *picture, |
| 623 int n, int size, uint16_t * const mv_penalty) | |
| 0 | 624 { |
| 936 | 625 uint8_t *ref_picture= picture->data[0]; |
| 626 uint32_t *score_map= s->me.score_map; | |
| 627 const int penalty_factor= s->me.sub_penalty_factor; | |
| 455 | 628 int mx, my, xx, yy, dminh; |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
629 UINT8 *pix, *ptr; |
| 936 | 630 op_pixels_abs_func pix_abs_x2; |
| 631 op_pixels_abs_func pix_abs_y2; | |
| 632 op_pixels_abs_func pix_abs_xy2; | |
| 633 | |
| 634 if(size==0){ | |
| 635 pix_abs_x2 = s->dsp.pix_abs16x16_x2; | |
| 636 pix_abs_y2 = s->dsp.pix_abs16x16_y2; | |
| 637 pix_abs_xy2= s->dsp.pix_abs16x16_xy2; | |
| 638 }else{ | |
| 639 pix_abs_x2 = s->dsp.pix_abs8x8_x2; | |
| 640 pix_abs_y2 = s->dsp.pix_abs8x8_y2; | |
| 641 pix_abs_xy2= s->dsp.pix_abs8x8_xy2; | |
| 863 | 642 } |
| 455 | 643 |
| 936 | 644 if(s->me.skip){ |
| 455 | 645 // printf("S"); |
| 646 *mx_ptr = 0; | |
| 647 *my_ptr = 0; | |
| 648 return dmin; | |
| 649 } | |
| 650 // printf("N"); | |
| 651 | |
| 652 xx = 16 * s->mb_x + 8*(n&1); | |
| 653 yy = 16 * s->mb_y + 8*(n>>1); | |
| 903 | 654 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 455 | 655 |
| 294 | 656 mx = *mx_ptr; |
| 657 my = *my_ptr; | |
| 455 | 658 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx); |
| 659 | |
| 294 | 660 dminh = dmin; |
| 661 | |
| 662 if (mx > xmin && mx < xmax && | |
| 663 my > ymin && my < ymax) { | |
| 455 | 664 int dx=0, dy=0; |
| 665 int d, pen_x, pen_y; | |
| 666 const int index= (my<<ME_MAP_SHIFT) + mx; | |
| 667 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; | |
| 668 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]; | |
| 669 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]; | |
| 670 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; | |
| 671 mx<<=1; | |
| 672 my<<=1; | |
| 294 | 673 |
| 674 | |
| 675 pen_x= pred_x + mx; | |
| 676 pen_y= pred_y + my; | |
| 677 | |
| 678 ptr-= s->linesize; | |
| 455 | 679 if(t<=b){ |
| 936 | 680 CHECK_SAD_HALF_MV(y2 , 0, -1) |
| 455 | 681 if(l<=r){ |
| 936 | 682 CHECK_SAD_HALF_MV(xy2, -1, -1) |
| 455 | 683 if(t+r<=b+l){ |
| 936 | 684 CHECK_SAD_HALF_MV(xy2, +1, -1) |
| 455 | 685 ptr+= s->linesize; |
| 686 }else{ | |
| 687 ptr+= s->linesize; | |
| 936 | 688 CHECK_SAD_HALF_MV(xy2, -1, +1) |
| 455 | 689 } |
| 936 | 690 CHECK_SAD_HALF_MV(x2 , -1, 0) |
| 455 | 691 }else{ |
| 936 | 692 CHECK_SAD_HALF_MV(xy2, +1, -1) |
| 455 | 693 if(t+l<=b+r){ |
| 936 | 694 CHECK_SAD_HALF_MV(xy2, -1, -1) |
| 455 | 695 ptr+= s->linesize; |
| 696 }else{ | |
| 697 ptr+= s->linesize; | |
| 936 | 698 CHECK_SAD_HALF_MV(xy2, +1, +1) |
| 455 | 699 } |
| 936 | 700 CHECK_SAD_HALF_MV(x2 , +1, 0) |
| 455 | 701 } |
| 702 }else{ | |
| 703 if(l<=r){ | |
| 704 if(t+l<=b+r){ | |
| 936 | 705 CHECK_SAD_HALF_MV(xy2, -1, -1) |
| 455 | 706 ptr+= s->linesize; |
| 707 }else{ | |
| 708 ptr+= s->linesize; | |
| 936 | 709 CHECK_SAD_HALF_MV(xy2, +1, +1) |
| 455 | 710 } |
| 936 | 711 CHECK_SAD_HALF_MV(x2 , -1, 0) |
| 712 CHECK_SAD_HALF_MV(xy2, -1, +1) | |
| 455 | 713 }else{ |
| 714 if(t+r<=b+l){ | |
| 936 | 715 CHECK_SAD_HALF_MV(xy2, +1, -1) |
| 455 | 716 ptr+= s->linesize; |
| 717 }else{ | |
| 718 ptr+= s->linesize; | |
| 936 | 719 CHECK_SAD_HALF_MV(xy2, -1, +1) |
| 455 | 720 } |
| 936 | 721 CHECK_SAD_HALF_MV(x2 , +1, 0) |
| 722 CHECK_SAD_HALF_MV(xy2, +1, +1) | |
| 455 | 723 } |
| 936 | 724 CHECK_SAD_HALF_MV(y2 , 0, +1) |
| 455 | 725 } |
| 726 mx+=dx; | |
| 727 my+=dy; | |
| 294 | 728 |
| 729 }else{ | |
| 455 | 730 mx<<=1; |
| 731 my<<=1; | |
| 294 | 732 } |
| 733 | |
| 734 *mx_ptr = mx; | |
| 735 *my_ptr = my; | |
| 455 | 736 return dminh; |
| 294 | 737 } |
| 738 | |
| 455 | 739 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) |
| 294 | 740 { |
| 324 | 741 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2); |
| 294 | 742 |
| 324 | 743 s->p_mv_table[xy][0] = mx; |
| 744 s->p_mv_table[xy][1] = my; | |
| 294 | 745 |
| 746 /* has allready been set to the 4 MV if 4MV is done */ | |
| 455 | 747 if(mv4){ |
| 294 | 748 int mot_xy= s->block_index[0]; |
| 749 | |
| 750 s->motion_val[mot_xy ][0]= mx; | |
| 751 s->motion_val[mot_xy ][1]= my; | |
| 752 s->motion_val[mot_xy+1][0]= mx; | |
| 753 s->motion_val[mot_xy+1][1]= my; | |
| 754 | |
| 755 mot_xy += s->block_wrap[0]; | |
| 756 s->motion_val[mot_xy ][0]= mx; | |
| 757 s->motion_val[mot_xy ][1]= my; | |
| 758 s->motion_val[mot_xy+1][0]= mx; | |
| 759 s->motion_val[mot_xy+1][1]= my; | |
| 760 } | |
| 761 } | |
| 762 | |
| 324 | 763 static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code) |
| 764 { | |
| 765 *range = 8 * (1 << (f_code - 1)); | |
| 766 /* XXX: temporary kludge to avoid overflow for msmpeg4 */ | |
| 767 if (s->out_format == FMT_H263 && !s->h263_msmpeg4) | |
| 768 *range *= 2; | |
| 0 | 769 |
| 324 | 770 if (s->unrestricted_mv) { |
| 771 *xmin = -16; | |
| 772 *ymin = -16; | |
| 773 if (s->h263_plus) | |
| 774 *range *= 2; | |
| 951 | 775 if(s->avctx->codec->id!=CODEC_ID_MPEG4){ |
| 324 | 776 *xmax = s->mb_width*16; |
| 777 *ymax = s->mb_height*16; | |
| 778 }else { | |
| 779 *xmax = s->width; | |
| 780 *ymax = s->height; | |
| 781 } | |
| 782 } else { | |
| 783 *xmin = 0; | |
| 784 *ymin = 0; | |
| 785 *xmax = s->mb_width*16 - 16; | |
| 786 *ymax = s->mb_height*16 - 16; | |
| 787 } | |
| 788 } | |
| 789 | |
| 455 | 790 static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift) |
| 791 { | |
| 792 int block; | |
| 793 int P[10][2]; | |
| 903 | 794 uint8_t *ref_picture= s->last_picture.data[0]; |
| 455 | 795 int dmin_sum=0; |
| 936 | 796 uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; |
| 455 | 797 |
| 798 for(block=0; block<4; block++){ | |
| 799 int mx4, my4; | |
| 800 int pred_x4, pred_y4; | |
| 801 int dmin4; | |
| 802 static const int off[4]= {2, 1, 1, -1}; | |
| 803 const int mot_stride = s->block_wrap[0]; | |
| 804 const int mot_xy = s->block_index[block]; | |
| 805 // const int block_x= (block&1); | |
| 806 // const int block_y= (block>>1); | |
| 807 #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way | |
| 808 const int rel_xmin4= xmin; | |
| 809 const int rel_xmax4= xmax; | |
| 810 const int rel_ymin4= ymin; | |
| 811 const int rel_ymax4= ymax; | |
| 812 #else | |
| 813 const int rel_xmin4= xmin - block_x*8; | |
| 814 const int rel_xmax4= xmax - block_x*8 + 8; | |
| 815 const int rel_ymin4= ymin - block_y*8; | |
| 816 const int rel_ymax4= ymax - block_y*8 + 8; | |
| 817 #endif | |
| 818 P_LEFT[0] = s->motion_val[mot_xy - 1][0]; | |
| 819 P_LEFT[1] = s->motion_val[mot_xy - 1][1]; | |
| 820 | |
| 821 if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift); | |
| 822 | |
| 823 /* special case for first line */ | |
| 952 | 824 if (s->mb_y == 0 && block<2) { |
| 455 | 825 pred_x4= P_LEFT[0]; |
| 826 pred_y4= P_LEFT[1]; | |
| 827 } else { | |
| 828 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0]; | |
| 829 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1]; | |
| 830 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0]; | |
| 831 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1]; | |
| 832 if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift); | |
| 833 if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift); | |
| 834 if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift); | |
| 835 if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift); | |
| 836 | |
| 837 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
| 838 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 839 | |
| 840 if(s->out_format == FMT_H263){ | |
| 841 pred_x4 = P_MEDIAN[0]; | |
| 842 pred_y4 = P_MEDIAN[1]; | |
| 843 }else { /* mpeg1 at least */ | |
| 844 pred_x4= P_LEFT[0]; | |
| 845 pred_y4= P_LEFT[1]; | |
| 846 } | |
| 847 } | |
| 848 P_MV1[0]= mx; | |
| 849 P_MV1[1]= my; | |
| 850 | |
| 936 | 851 dmin4 = s->me.motion_search[1](s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, |
| 948 | 852 &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); |
| 455 | 853 |
| 936 | 854 dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, |
| 855 pred_x4, pred_y4, &s->last_picture, block, 1, mv_penalty); | |
| 455 | 856 |
| 857 s->motion_val[ s->block_index[block] ][0]= mx4; | |
| 858 s->motion_val[ s->block_index[block] ][1]= my4; | |
| 859 dmin_sum+= dmin4; | |
| 860 } | |
| 861 return dmin_sum; | |
| 862 } | |
| 863 | |
| 324 | 864 void ff_estimate_p_frame_motion(MpegEncContext * s, |
| 865 int mb_x, int mb_y) | |
| 0 | 866 { |
| 867 UINT8 *pix, *ppix; | |
| 868 int sum, varc, vard, mx, my, range, dmin, xx, yy; | |
| 869 int xmin, ymin, xmax, ymax; | |
| 280 | 870 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
|
871 int pred_x=0, pred_y=0; |
| 455 | 872 int P[10][2]; |
| 280 | 873 const int shift= 1+s->quarter_sample; |
| 294 | 874 int mb_type=0; |
| 903 | 875 uint8_t *ref_picture= s->last_picture.data[0]; |
| 876 Picture * const pic= &s->current_picture; | |
| 936 | 877 uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; |
| 878 | |
| 879 assert(s->quarter_sample==0 || s->quarter_sample==1); | |
| 880 | |
| 881 s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); | |
| 882 s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); | |
| 0 | 883 |
| 324 | 884 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); |
| 455 | 885 rel_xmin= xmin - mb_x*16; |
| 886 rel_xmax= xmax - mb_x*16; | |
| 887 rel_ymin= ymin - mb_y*16; | |
| 888 rel_ymax= ymax - mb_y*16; | |
| 936 | 889 s->me.skip=0; |
| 324 | 890 |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
891 switch(s->me_method) { |
| 0 | 892 case ME_ZERO: |
| 893 default: | |
| 894 no_motion_search(s, &mx, &my); | |
| 455 | 895 mx-= mb_x*16; |
| 896 my-= mb_y*16; | |
| 0 | 897 dmin = 0; |
| 898 break; | |
| 899 case ME_FULL: | |
| 324 | 900 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); |
| 455 | 901 mx-= mb_x*16; |
| 902 my-= mb_y*16; | |
| 0 | 903 break; |
| 904 case ME_LOG: | |
| 324 | 905 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
| 455 | 906 mx-= mb_x*16; |
| 907 my-= mb_y*16; | |
| 0 | 908 break; |
| 909 case ME_PHODS: | |
| 324 | 910 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
| 455 | 911 mx-= mb_x*16; |
| 912 my-= mb_y*16; | |
| 0 | 913 break; |
| 288 | 914 case ME_X1: |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
915 case ME_EPZS: |
| 288 | 916 { |
| 294 | 917 const int mot_stride = s->block_wrap[0]; |
| 918 const int mot_xy = s->block_index[0]; | |
| 288 | 919 |
| 455 | 920 P_LEFT[0] = s->motion_val[mot_xy - 1][0]; |
| 921 P_LEFT[1] = s->motion_val[mot_xy - 1][1]; | |
| 288 | 922 |
| 455 | 923 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); |
| 280 | 924 |
| 952 | 925 if(mb_y) { |
| 455 | 926 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0]; |
| 927 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1]; | |
| 928 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0]; | |
| 929 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1]; | |
| 930 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift); | |
| 931 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift); | |
| 932 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift); | |
| 280 | 933 |
| 455 | 934 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
| 935 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 936 | |
| 937 if(s->out_format == FMT_H263){ | |
| 938 pred_x = P_MEDIAN[0]; | |
| 939 pred_y = P_MEDIAN[1]; | |
| 940 }else { /* mpeg1 at least */ | |
| 941 pred_x= P_LEFT[0]; | |
| 942 pred_y= P_LEFT[1]; | |
| 943 } | |
| 952 | 944 }else{ |
| 945 pred_x= P_LEFT[0]; | |
| 946 pred_y= P_LEFT[1]; | |
| 288 | 947 } |
| 952 | 948 |
| 280 | 949 } |
| 936 | 950 dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 948 | 951 &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
|
952 |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
275
diff
changeset
|
953 break; |
| 0 | 954 } |
| 955 | |
| 956 /* intra / predictive decision */ | |
| 957 xx = mb_x * 16; | |
| 958 yy = mb_y * 16; | |
| 959 | |
| 903 | 960 pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
| 455 | 961 /* At this point (mx,my) are full-pell and the relative displacement */ |
| 962 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
|
963 |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
964 sum = s->dsp.pix_sum(pix, s->linesize); |
| 455 | 965 |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
966 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; |
| 936 | 967 vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize)+128)>>8; |
| 608 | 968 |
| 455 | 969 //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 | 970 pic->mb_var [s->mb_width * mb_y + mb_x] = varc; |
| 971 pic->mc_mb_var[s->mb_width * mb_y + mb_x] = vard; | |
| 972 pic->mb_mean [s->mb_width * mb_y + mb_x] = (sum+128)>>8; | |
| 973 pic->mb_var_sum += varc; | |
| 974 pic->mc_mb_var_sum += vard; | |
| 455 | 975 //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
|
976 |
| 0 | 977 #if 0 |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
978 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
|
979 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); |
| 0 | 980 #endif |
| 294 | 981 if(s->flags&CODEC_FLAG_HQ){ |
| 608 | 982 if (vard <= 64 || vard < varc) |
| 983 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); | |
| 984 else | |
| 913 | 985 s->scene_change_score+= s->qscale; |
| 608 | 986 |
| 294 | 987 if (vard*2 + 200 > varc) |
| 988 mb_type|= MB_TYPE_INTRA; | |
| 989 if (varc*2 + 200 > vard){ | |
| 990 mb_type|= MB_TYPE_INTER; | |
| 936 | 991 s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 992 pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty); | |
| 304 | 993 }else{ |
| 936 | 994 mx <<=shift; |
| 995 my <<=shift; | |
| 0 | 996 } |
| 455 | 997 if((s->flags&CODEC_FLAG_4MV) |
| 936 | 998 && !s->me.skip && varc>50 && vard>10){ |
| 455 | 999 mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); |
| 1000 mb_type|=MB_TYPE_INTER4V; | |
| 1001 | |
| 1002 set_p_mv_tables(s, mx, my, 0); | |
| 1003 }else | |
| 1004 set_p_mv_tables(s, mx, my, 1); | |
| 294 | 1005 }else{ |
| 1006 if (vard <= 64 || vard < varc) { | |
| 936 | 1007 // if (sadP <= 32 || sadP < sadI + 500) { |
| 608 | 1008 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); |
| 294 | 1009 mb_type|= MB_TYPE_INTER; |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1010 if (s->me_method != ME_ZERO) { |
| 936 | 1011 dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 1012 pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty); | |
| 455 | 1013 if((s->flags&CODEC_FLAG_4MV) |
| 936 | 1014 && !s->me.skip && varc>50 && vard>10){ |
| 455 | 1015 int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); |
| 1016 if(dmin4 + 128 <dmin) | |
| 1017 mb_type= MB_TYPE_INTER4V; | |
| 1018 } | |
| 1019 set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V); | |
| 1020 | |
| 294 | 1021 } else { |
| 936 | 1022 mx <<=shift; |
| 1023 my <<=shift; | |
| 294 | 1024 } |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1025 #if 0 |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1026 if (vard < 10) { |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1027 skip++; |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1028 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
|
1029 skip, vard, varc, dmin); |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1030 } |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
304
diff
changeset
|
1031 #endif |
| 294 | 1032 }else{ |
| 608 | 1033 s->scene_change_score+= 20; |
| 294 | 1034 mb_type|= MB_TYPE_INTRA; |
| 455 | 1035 mx = 0; |
| 1036 my = 0; | |
| 294 | 1037 } |
| 1038 } | |
| 284 | 1039 |
| 294 | 1040 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
| 0 | 1041 } |
| 1042 | |
| 951 | 1043 int ff_pre_estimate_p_frame_motion(MpegEncContext * s, |
| 1044 int mb_x, int mb_y) | |
| 1045 { | |
| 1046 int mx, my, range, dmin; | |
| 1047 int xmin, ymin, xmax, ymax; | |
| 1048 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | |
| 1049 int pred_x=0, pred_y=0; | |
| 1050 int P[10][2]; | |
| 1051 const int shift= 1+s->quarter_sample; | |
| 1052 uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; | |
| 1053 const int mv_stride= s->mb_width + 2; | |
| 1054 const int xy= mb_x + 1 + (mb_y + 1)*mv_stride; | |
| 1055 | |
| 1056 assert(s->quarter_sample==0 || s->quarter_sample==1); | |
| 1057 | |
| 954 | 1058 s->me.pre_penalty_factor = get_penalty_factor(s, s->avctx->me_pre_cmp); |
| 951 | 1059 |
| 1060 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); | |
| 1061 rel_xmin= xmin - mb_x*16; | |
| 1062 rel_xmax= xmax - mb_x*16; | |
| 1063 rel_ymin= ymin - mb_y*16; | |
| 1064 rel_ymax= ymax - mb_y*16; | |
| 1065 s->me.skip=0; | |
| 1066 | |
| 1067 P_LEFT[0] = s->p_mv_table[xy + 1][0]; | |
| 1068 P_LEFT[1] = s->p_mv_table[xy + 1][1]; | |
| 1069 | |
| 1070 if(P_LEFT[0] < (rel_xmin<<shift)) P_LEFT[0] = (rel_xmin<<shift); | |
| 1071 | |
| 1072 /* special case for first line */ | |
| 1073 if (mb_y == s->mb_height-1) { | |
| 1074 pred_x= P_LEFT[0]; | |
| 1075 pred_y= P_LEFT[1]; | |
| 952 | 1076 P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]= |
| 1077 P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME | |
| 951 | 1078 } else { |
| 1079 P_TOP[0] = s->p_mv_table[xy + mv_stride ][0]; | |
| 1080 P_TOP[1] = s->p_mv_table[xy + mv_stride ][1]; | |
| 1081 P_TOPRIGHT[0] = s->p_mv_table[xy + mv_stride - 1][0]; | |
| 1082 P_TOPRIGHT[1] = s->p_mv_table[xy + mv_stride - 1][1]; | |
| 1083 if(P_TOP[1] < (rel_ymin<<shift)) P_TOP[1] = (rel_ymin<<shift); | |
| 1084 if(P_TOPRIGHT[0] > (rel_xmax<<shift)) P_TOPRIGHT[0]= (rel_xmax<<shift); | |
| 1085 if(P_TOPRIGHT[1] < (rel_ymin<<shift)) P_TOPRIGHT[1]= (rel_ymin<<shift); | |
| 1086 | |
| 1087 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
| 1088 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 1089 | |
| 952 | 1090 pred_x = P_MEDIAN[0]; |
| 1091 pred_y = P_MEDIAN[1]; | |
| 951 | 1092 } |
| 954 | 1093 dmin = s->me.pre_motion_search(s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 1094 &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); | |
| 952 | 1095 |
| 951 | 1096 s->p_mv_table[xy][0] = mx<<shift; |
| 1097 s->p_mv_table[xy][1] = my<<shift; | |
| 1098 | |
| 1099 return dmin; | |
| 1100 } | |
| 1101 | |
| 327 | 1102 int ff_estimate_motion_b(MpegEncContext * s, |
| 936 | 1103 int mb_x, int mb_y, int16_t (*mv_table)[2], Picture *picture, int f_code) |
| 0 | 1104 { |
| 327 | 1105 int mx, my, range, dmin; |
| 324 | 1106 int xmin, ymin, xmax, ymax; |
| 1107 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | |
| 1108 int pred_x=0, pred_y=0; | |
| 455 | 1109 int P[10][2]; |
| 324 | 1110 const int shift= 1+s->quarter_sample; |
| 1111 const int mot_stride = s->mb_width + 2; | |
| 1112 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
| 936 | 1113 uint8_t * const ref_picture= picture->data[0]; |
| 1114 uint16_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV; | |
| 948 | 1115 int mv_scale; |
| 936 | 1116 |
| 1117 s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); | |
| 1118 s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); | |
| 1119 | |
| 324 | 1120 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code); |
| 455 | 1121 rel_xmin= xmin - mb_x*16; |
| 1122 rel_xmax= xmax - mb_x*16; | |
| 1123 rel_ymin= ymin - mb_y*16; | |
| 1124 rel_ymax= ymax - mb_y*16; | |
| 324 | 1125 |
| 1126 switch(s->me_method) { | |
| 1127 case ME_ZERO: | |
| 1128 default: | |
| 1129 no_motion_search(s, &mx, &my); | |
| 1130 dmin = 0; | |
| 455 | 1131 mx-= mb_x*16; |
| 1132 my-= mb_y*16; | |
| 324 | 1133 break; |
| 1134 case ME_FULL: | |
| 1135 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); | |
| 455 | 1136 mx-= mb_x*16; |
| 1137 my-= mb_y*16; | |
| 324 | 1138 break; |
| 1139 case ME_LOG: | |
| 1140 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | |
| 455 | 1141 mx-= mb_x*16; |
| 1142 my-= mb_y*16; | |
| 324 | 1143 break; |
| 1144 case ME_PHODS: | |
| 1145 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | |
| 455 | 1146 mx-= mb_x*16; |
| 1147 my-= mb_y*16; | |
| 324 | 1148 break; |
| 1149 case ME_X1: | |
| 1150 case ME_EPZS: | |
| 1151 { | |
| 455 | 1152 P_LEFT[0] = mv_table[mot_xy - 1][0]; |
| 1153 P_LEFT[1] = mv_table[mot_xy - 1][1]; | |
| 324 | 1154 |
| 455 | 1155 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); |
| 324 | 1156 |
| 1157 /* special case for first line */ | |
| 952 | 1158 if (mb_y) { |
| 455 | 1159 P_TOP[0] = mv_table[mot_xy - mot_stride ][0]; |
| 1160 P_TOP[1] = mv_table[mot_xy - mot_stride ][1]; | |
| 1161 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0]; | |
| 1162 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1]; | |
| 1163 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift); | |
| 1164 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift); | |
| 1165 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift); | |
| 324 | 1166 |
| 455 | 1167 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
| 1168 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 324 | 1169 } |
| 455 | 1170 pred_x= P_LEFT[0]; |
| 1171 pred_y= P_LEFT[1]; | |
| 324 | 1172 } |
| 948 | 1173 |
| 1174 if(mv_table == s->b_forw_mv_table){ | |
| 1175 mv_scale= (s->pb_time<<16) / (s->pp_time<<shift); | |
| 1176 }else{ | |
| 1177 mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift); | |
| 1178 } | |
| 1179 | |
| 936 | 1180 dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 948 | 1181 picture, s->p_mv_table, mv_scale, mv_penalty); |
| 324 | 1182 |
| 1183 break; | |
| 1184 } | |
| 1185 | |
| 936 | 1186 dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
| 1187 pred_x, pred_y, picture, 0, 0, mv_penalty); | |
| 455 | 1188 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my); |
| 324 | 1189 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
| 1190 mv_table[mot_xy][0]= mx; | |
| 1191 mv_table[mot_xy][1]= my; | |
| 936 | 1192 |
| 327 | 1193 return dmin; |
| 324 | 1194 } |
| 1195 | |
| 327 | 1196 static inline int check_bidir_mv(MpegEncContext * s, |
| 1197 int mb_x, int mb_y, | |
| 1198 int motion_fx, int motion_fy, | |
| 1199 int motion_bx, int motion_by, | |
| 1200 int pred_fx, int pred_fy, | |
| 1201 int pred_bx, int pred_by) | |
| 1202 { | |
| 1203 //FIXME optimize? | |
| 936 | 1204 //FIXME move into template? |
| 1205 //FIXME better f_code prediction (max mv & distance) | |
| 1206 UINT16 *mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | |
| 1207 uint8_t *dest_y = s->me.scratchpad; | |
| 327 | 1208 uint8_t *ptr; |
| 1209 int dxy; | |
| 1210 int src_x, src_y; | |
| 1211 int fbmin; | |
| 1212 | |
| 936 | 1213 if(s->quarter_sample){ |
| 1214 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3); | |
| 1215 src_x = mb_x * 16 + (motion_fx >> 2); | |
| 1216 src_y = mb_y * 16 + (motion_fy >> 2); | |
| 1217 assert(src_x >=-16 && src_x<=s->width); | |
| 1218 assert(src_y >=-16 && src_y<=s->height); | |
| 327 | 1219 |
| 936 | 1220 ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x; |
| 1221 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
|
1222 |
| 936 | 1223 dxy = ((motion_by & 3) << 2) | (motion_bx & 3); |
| 1224 src_x = mb_x * 16 + (motion_bx >> 2); | |
| 1225 src_y = mb_y * 16 + (motion_by >> 2); | |
| 1226 assert(src_x >=-16 && src_x<=s->width); | |
| 1227 assert(src_y >=-16 && src_y<=s->height); | |
| 1228 | |
| 1229 ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x; | |
| 1230 s->dsp.avg_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize); | |
| 1231 }else{ | |
| 1232 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1); | |
| 1233 src_x = mb_x * 16 + (motion_fx >> 1); | |
| 1234 src_y = mb_y * 16 + (motion_fy >> 1); | |
| 1235 assert(src_x >=-16 && src_x<=s->width); | |
| 1236 assert(src_y >=-16 && src_y<=s->height); | |
| 327 | 1237 |
| 936 | 1238 ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x; |
| 1239 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
|
1240 |
| 936 | 1241 dxy = ((motion_by & 1) << 1) | (motion_bx & 1); |
| 1242 src_x = mb_x * 16 + (motion_bx >> 1); | |
| 1243 src_y = mb_y * 16 + (motion_by >> 1); | |
| 1244 assert(src_x >=-16 && src_x<=s->width); | |
| 1245 assert(src_y >=-16 && src_y<=s->height); | |
| 1246 | |
| 1247 ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x; | |
| 1248 s->dsp.avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16); | |
| 1249 } | |
|
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1250 |
| 936 | 1251 fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.sub_penalty_factor |
| 1252 +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.sub_penalty_factor; | |
| 1253 + s->dsp.me_sub_cmp[0](s, s->new_picture.data[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize); | |
| 1254 | |
| 327 | 1255 return fbmin; |
| 1256 } | |
| 1257 | |
| 1258 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/ | |
| 1259 static inline int bidir_refine(MpegEncContext * s, | |
| 1260 int mb_x, int mb_y) | |
| 1261 { | |
| 1262 const int mot_stride = s->mb_width + 2; | |
| 1263 const int xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
| 1264 int fbmin; | |
| 1265 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0]; | |
| 1266 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1]; | |
| 1267 int pred_bx= s->b_bidir_back_mv_table[xy-1][0]; | |
| 1268 int pred_by= s->b_bidir_back_mv_table[xy-1][1]; | |
| 1269 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0]; | |
| 1270 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1]; | |
| 1271 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0]; | |
| 1272 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1]; | |
| 1273 | |
| 1274 //FIXME do refinement and add flag | |
| 1275 | |
| 1276 fbmin= check_bidir_mv(s, mb_x, mb_y, | |
| 1277 motion_fx, motion_fy, | |
| 1278 motion_bx, motion_by, | |
| 1279 pred_fx, pred_fy, | |
| 1280 pred_bx, pred_by); | |
| 1281 | |
| 1282 return fbmin; | |
| 1283 } | |
| 1284 | |
| 1285 static inline int direct_search(MpegEncContext * s, | |
| 1286 int mb_x, int mb_y) | |
| 324 | 1287 { |
| 455 | 1288 int P[10][2]; |
| 327 | 1289 const int mot_stride = s->mb_width + 2; |
| 1290 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
| 936 | 1291 const int shift= 1+s->quarter_sample; |
| 1292 int dmin, i; | |
| 327 | 1293 const int time_pp= s->pp_time; |
| 664 | 1294 const int time_pb= s->pb_time; |
| 936 | 1295 int mx, my, xmin, xmax, ymin, ymax; |
| 327 | 1296 int16_t (*mv_table)[2]= s->b_direct_mv_table; |
| 936 | 1297 uint16_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV; |
| 1298 | |
| 1299 ymin= xmin=(-32)>>shift; | |
| 1300 ymax= xmax= 31>>shift; | |
| 1301 | |
| 1302 if(s->co_located_type_table[mb_x + mb_y*s->mb_width]==CO_LOCATED_TYPE_4MV){ | |
| 1303 s->mv_type= MV_TYPE_8X8; | |
| 1304 }else{ | |
| 1305 s->mv_type= MV_TYPE_16X16; | |
| 327 | 1306 } |
| 936 | 1307 |
| 1308 for(i=0; i<4; i++){ | |
| 1309 int index= s->block_index[i]; | |
| 1310 int min, max; | |
| 1311 | |
| 1312 s->me.co_located_mv[i][0]= s->motion_val[index][0]; | |
| 1313 s->me.co_located_mv[i][1]= s->motion_val[index][1]; | |
| 1314 s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3)); | |
| 1315 s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3)); | |
| 1316 // 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); | |
| 1317 // 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); | |
| 1318 | |
| 1319 max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; | |
| 1320 min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; | |
| 1321 max+= (2*mb_x + (i& 1))*8 - 1; // +-1 is for the simpler rounding | |
| 1322 min+= (2*mb_x + (i& 1))*8 + 1; | |
| 960 | 1323 xmax= FFMIN(xmax, s->width - max); |
| 1324 xmin= FFMAX(xmin, - 16 - min); | |
| 936 | 1325 |
| 1326 max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; | |
| 1327 min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; | |
| 1328 max+= (2*mb_y + (i>>1))*8 - 1; // +-1 is for the simpler rounding | |
| 1329 min+= (2*mb_y + (i>>1))*8 + 1; | |
| 960 | 1330 ymax= FFMIN(ymax, s->height - max); |
| 1331 ymin= FFMAX(ymin, - 16 - min); | |
| 936 | 1332 |
| 1333 if(s->mv_type == MV_TYPE_16X16) break; | |
| 327 | 1334 } |
| 936 | 1335 |
| 1336 assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16); | |
| 1337 | |
| 1338 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){ | |
| 1339 s->b_direct_mv_table[mot_xy][0]= 0; | |
| 1340 s->b_direct_mv_table[mot_xy][1]= 0; | |
| 1341 | |
| 1342 return 256*256*256*64; | |
| 1343 } | |
| 1344 | |
| 950 | 1345 P_LEFT[0] = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift); |
| 1346 P_LEFT[1] = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift); | |
| 1347 | |
| 1348 /* special case for first line */ | |
| 952 | 1349 if (mb_y) { |
| 950 | 1350 P_TOP[0] = clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift); |
| 1351 P_TOP[1] = clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift); | |
| 1352 P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift); | |
| 1353 P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift); | |
| 1354 | |
| 1355 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
| 1356 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
| 1357 } | |
| 1358 | |
| 936 | 1359 if(s->flags&CODEC_FLAG_QPEL){ |
| 1360 dmin = simple_direct_qpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, | |
| 948 | 1361 &s->last_picture, mv_table, 1<<14, mv_penalty); |
| 936 | 1362 dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, |
| 1363 0, 0, &s->last_picture, 0, 0, mv_penalty); | |
| 1364 }else{ | |
| 1365 dmin = simple_direct_hpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, | |
| 948 | 1366 &s->last_picture, mv_table, 1<<15, mv_penalty); |
| 936 | 1367 dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, |
| 1368 0, 0, &s->last_picture, 0, 0, mv_penalty); | |
| 327 | 1369 } |
| 1370 | |
| 1371 s->b_direct_mv_table[mot_xy][0]= mx; | |
| 1372 s->b_direct_mv_table[mot_xy][1]= my; | |
| 1373 return dmin; | |
| 324 | 1374 } |
| 1375 | |
| 1376 void ff_estimate_b_frame_motion(MpegEncContext * s, | |
| 1377 int mb_x, int mb_y) | |
| 1378 { | |
| 936 | 1379 const int penalty_factor= s->me.penalty_factor; |
| 327 | 1380 int fmin, bmin, dmin, fbmin; |
| 1381 int type=0; | |
| 1382 | |
| 1383 dmin= direct_search(s, mb_x, mb_y); | |
| 324 | 1384 |
| 936 | 1385 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, &s->last_picture, s->f_code); |
| 1386 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, &s->next_picture, s->b_code) - penalty_factor; | |
| 324 | 1387 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); |
| 327 | 1388 |
| 1389 fbmin= bidir_refine(s, mb_x, mb_y); | |
| 1390 | |
| 612 | 1391 { |
| 327 | 1392 int score= dmin; |
| 1393 type=MB_TYPE_DIRECT; | |
| 1394 | |
| 1395 if(fmin<score){ | |
| 1396 score=fmin; | |
| 1397 type= MB_TYPE_FORWARD; | |
| 1398 } | |
| 1399 if(bmin<score){ | |
| 1400 score=bmin; | |
| 1401 type= MB_TYPE_BACKWARD; | |
| 1402 } | |
| 1403 if(fbmin<score){ | |
| 1404 score=fbmin; | |
| 1405 type= MB_TYPE_BIDIR; | |
| 1406 } | |
|
807
0e1d375c537f
fixing q>0.0 assert failure caused by overflow of variance for b frames
michaelni
parents:
765
diff
changeset
|
1407 score= ((unsigned)(score*score + 128*256))>>16; |
| 903 | 1408 s->current_picture.mc_mb_var_sum += score; |
| 1409 s->current_picture.mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSD | |
| 327 | 1410 } |
| 612 | 1411 |
| 1412 if(s->flags&CODEC_FLAG_HQ){ | |
| 1413 type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter | |
| 936 | 1414 if(dmin>256*256*16) type&= ~MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB |
| 612 | 1415 } |
| 1416 | |
| 327 | 1417 s->mb_type[mb_y*s->mb_width + mb_x]= type; |
| 324 | 1418 } |
| 0 | 1419 |
| 324 | 1420 /* find best f_code for ME which do unlimited searches */ |
| 1421 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) | |
| 1422 { | |
| 1423 if(s->me_method>=ME_EPZS){ | |
| 455 | 1424 int score[8]; |
| 324 | 1425 int i, y; |
| 1426 UINT8 * fcode_tab= s->fcode_tab; | |
| 455 | 1427 int best_fcode=-1; |
| 1428 int best_score=-10000000; | |
| 324 | 1429 |
| 936 | 1430 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i); |
| 324 | 1431 |
| 1432 for(y=0; y<s->mb_height; y++){ | |
| 1433 int x; | |
| 1434 int xy= (y+1)* (s->mb_width+2) + 1; | |
| 1435 i= y*s->mb_width; | |
| 1436 for(x=0; x<s->mb_width; x++){ | |
| 1437 if(s->mb_type[i] & type){ | |
| 847 | 1438 int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV], |
| 1439 fcode_tab[mv_table[xy][1] + MAX_MV]); | |
| 455 | 1440 int j; |
| 1441 | |
| 1442 for(j=0; j<fcode && j<8; j++){ | |
| 903 | 1443 if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[i] < s->current_picture.mb_var[i]) |
| 455 | 1444 score[j]-= 170; |
| 1445 } | |
| 324 | 1446 } |
| 1447 i++; | |
| 1448 xy++; | |
| 1449 } | |
| 1450 } | |
| 455 | 1451 |
| 1452 for(i=1; i<8; i++){ | |
| 1453 if(score[i] > best_score){ | |
| 1454 best_score= score[i]; | |
| 1455 best_fcode= i; | |
| 1456 } | |
| 1457 // printf("%d %d\n", i, score[i]); | |
| 1458 } | |
| 327 | 1459 |
| 324 | 1460 // printf("fcode: %d type: %d\n", i, s->pict_type); |
| 455 | 1461 return best_fcode; |
| 324 | 1462 /* for(i=0; i<=MAX_FCODE; i++){ |
| 1463 printf("%d ", mv_num[i]); | |
| 1464 } | |
| 1465 printf("\n");*/ | |
| 1466 }else{ | |
| 1467 return 1; | |
| 0 | 1468 } |
| 1469 } | |
| 1470 | |
| 324 | 1471 void ff_fix_long_p_mvs(MpegEncContext * s) |
| 1472 { | |
| 1473 const int f_code= s->f_code; | |
| 1474 int y; | |
| 1475 UINT8 * fcode_tab= s->fcode_tab; | |
| 455 | 1476 //int clip=0; |
| 1477 //int noclip=0; | |
| 324 | 1478 /* clip / convert to intra 16x16 type MVs */ |
| 1479 for(y=0; y<s->mb_height; y++){ | |
| 1480 int x; | |
| 1481 int xy= (y+1)* (s->mb_width+2)+1; | |
| 1482 int i= y*s->mb_width; | |
| 1483 for(x=0; x<s->mb_width; x++){ | |
| 1484 if(s->mb_type[i]&MB_TYPE_INTER){ | |
| 1485 if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code | |
| 1486 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0 | |
| 1487 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code | |
| 1488 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){ | |
| 1489 s->mb_type[i] &= ~MB_TYPE_INTER; | |
| 1490 s->mb_type[i] |= MB_TYPE_INTRA; | |
| 1491 s->p_mv_table[xy][0] = 0; | |
| 1492 s->p_mv_table[xy][1] = 0; | |
| 455 | 1493 //clip++; |
| 324 | 1494 } |
| 455 | 1495 //else |
| 1496 // noclip++; | |
| 324 | 1497 } |
| 1498 xy++; | |
| 1499 i++; | |
| 1500 } | |
| 1501 } | |
| 455 | 1502 //printf("%d no:%d %d//\n", clip, noclip, f_code); |
| 324 | 1503 if(s->flags&CODEC_FLAG_4MV){ |
| 1504 const int wrap= 2+ s->mb_width*2; | |
| 1505 | |
| 1506 /* clip / convert to intra 8x8 type MVs */ | |
| 1507 for(y=0; y<s->mb_height; y++){ | |
| 1508 int xy= (y*2 + 1)*wrap + 1; | |
| 1509 int i= y*s->mb_width; | |
| 1510 int x; | |
| 1511 | |
| 1512 for(x=0; x<s->mb_width; x++){ | |
| 1513 if(s->mb_type[i]&MB_TYPE_INTER4V){ | |
| 1514 int block; | |
| 1515 for(block=0; block<4; block++){ | |
| 1516 int off= (block& 1) + (block>>1)*wrap; | |
| 1517 int mx= s->motion_val[ xy + off ][0]; | |
| 1518 int my= s->motion_val[ xy + off ][1]; | |
| 1519 | |
| 1520 if( fcode_tab[mx + MAX_MV] > f_code | |
| 1521 || fcode_tab[mx + MAX_MV] == 0 | |
| 1522 || fcode_tab[my + MAX_MV] > f_code | |
| 1523 || fcode_tab[my + MAX_MV] == 0 ){ | |
| 1524 s->mb_type[i] &= ~MB_TYPE_INTER4V; | |
| 1525 s->mb_type[i] |= MB_TYPE_INTRA; | |
| 1526 } | |
| 1527 } | |
| 1528 } | |
| 502 | 1529 xy+=2; |
| 1530 i++; | |
| 324 | 1531 } |
| 1532 } | |
| 1533 } | |
| 1534 } | |
| 1535 | |
| 1536 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type) | |
| 1537 { | |
| 1538 int y; | |
| 1539 UINT8 * fcode_tab= s->fcode_tab; | |
| 1540 | |
| 1541 /* clip / convert to intra 16x16 type MVs */ | |
| 1542 for(y=0; y<s->mb_height; y++){ | |
| 1543 int x; | |
| 1544 int xy= (y+1)* (s->mb_width+2)+1; | |
| 1545 int i= y*s->mb_width; | |
| 1546 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
|
1547 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
|
1548 || 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
|
1549 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
|
1550 else mv_table[xy][0]= -(16<<f_code); |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1551 } |
|
199b324b2693
fixing variance scaling for b frames (messed adaptive quants up)
michaelni
parents:
690
diff
changeset
|
1552 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
|
1553 || 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
|
1554 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
|
1555 else mv_table[xy][1]= -(16<<f_code); |
| 324 | 1556 } |
| 1557 xy++; | |
| 1558 i++; | |
| 1559 } | |
| 1560 } | |
| 1561 } |
