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