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