Define RDCOST only once
[libvpx.git] / vp8 / encoder / mcomp.c
blob716f514afc7bebf431db03c0a6572c5d3045b391
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
12 #include "mcomp.h"
13 #include "vpx_mem/vpx_mem.h"
15 #include <stdio.h>
16 #include <limits.h>
17 #include <math.h>
19 #ifdef ENTROPY_STATS
20 static int mv_ref_ct [31] [4] [2];
21 static int mv_mode_cts [4] [2];
22 #endif
24 static int mv_bits_sadcost[256];
26 void vp8cx_init_mv_bits_sadcost()
28 int i;
30 for (i = 0; i < 256; i++)
32 mv_bits_sadcost[i] = (int)sqrt(i * 16);
37 int vp8_mv_bit_cost(MV *mv, MV *ref, int *mvcost[2], int Weight)
39 // MV costing is based on the distribution of vectors in the previous frame and as such will tend to
40 // over state the cost of vectors. In addition coding a new vector can have a knock on effect on the
41 // cost of subsequent vectors and the quality of prediction from NEAR and NEAREST for subsequent blocks.
42 // The "Weight" parameter allows, to a limited extent, for some account to be taken of these factors.
43 return ((mvcost[0][(mv->row - ref->row) >> 1] + mvcost[1][(mv->col - ref->col) >> 1]) * Weight) >> 7;
46 static int mv_err_cost(MV *mv, MV *ref, int *mvcost[2], int error_per_bit)
48 //int i;
49 //return ((mvcost[0][(mv->row - ref->row)>>1] + mvcost[1][(mv->col - ref->col)>>1] + 128) * error_per_bit) >> 8;
50 //return ( (vp8_mv_bit_cost(mv, ref, mvcost, 100) + 128) * error_per_bit) >> 8;
52 //i = (vp8_mv_bit_cost(mv, ref, mvcost, 100) * error_per_bit + 128) >> 8;
53 return ((mvcost[0][(mv->row - ref->row) >> 1] + mvcost[1][(mv->col - ref->col) >> 1]) * error_per_bit + 128) >> 8;
54 //return (vp8_mv_bit_cost(mv, ref, mvcost, 128) * error_per_bit + 128) >> 8;
57 static int mvsad_err_cost(MV *mv, MV *ref, int *mvsadcost[2], int error_per_bit)
59 /* Calculate sad error cost on full pixel basis. */
60 return ((mvsadcost[0][(mv->row - ref->row)] + mvsadcost[1][(mv->col - ref->col)]) * error_per_bit + 128) >> 8;
63 static int mv_bits(MV *mv, MV *ref, int *mvcost[2])
65 // get the estimated number of bits for a motion vector, to be used for costing in SAD based
66 // motion estimation
67 return ((mvcost[0][(mv->row - ref->row) >> 1] + mvcost[1][(mv->col - ref->col)>> 1]) + 128) >> 8;
70 void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
72 int Len;
73 int search_site_count = 0;
76 // Generate offsets for 4 search sites per step.
77 Len = MAX_FIRST_STEP;
78 x->ss[search_site_count].mv.col = 0;
79 x->ss[search_site_count].mv.row = 0;
80 x->ss[search_site_count].offset = 0;
81 search_site_count++;
83 while (Len > 0)
86 // Compute offsets for search sites.
87 x->ss[search_site_count].mv.col = 0;
88 x->ss[search_site_count].mv.row = -Len;
89 x->ss[search_site_count].offset = -Len * stride;
90 search_site_count++;
92 // Compute offsets for search sites.
93 x->ss[search_site_count].mv.col = 0;
94 x->ss[search_site_count].mv.row = Len;
95 x->ss[search_site_count].offset = Len * stride;
96 search_site_count++;
98 // Compute offsets for search sites.
99 x->ss[search_site_count].mv.col = -Len;
100 x->ss[search_site_count].mv.row = 0;
101 x->ss[search_site_count].offset = -Len;
102 search_site_count++;
104 // Compute offsets for search sites.
105 x->ss[search_site_count].mv.col = Len;
106 x->ss[search_site_count].mv.row = 0;
107 x->ss[search_site_count].offset = Len;
108 search_site_count++;
110 // Contract.
111 Len /= 2;
114 x->ss_count = search_site_count;
115 x->searches_per_step = 4;
118 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
120 int Len;
121 int search_site_count = 0;
123 // Generate offsets for 8 search sites per step.
124 Len = MAX_FIRST_STEP;
125 x->ss[search_site_count].mv.col = 0;
126 x->ss[search_site_count].mv.row = 0;
127 x->ss[search_site_count].offset = 0;
128 search_site_count++;
130 while (Len > 0)
133 // Compute offsets for search sites.
134 x->ss[search_site_count].mv.col = 0;
135 x->ss[search_site_count].mv.row = -Len;
136 x->ss[search_site_count].offset = -Len * stride;
137 search_site_count++;
139 // Compute offsets for search sites.
140 x->ss[search_site_count].mv.col = 0;
141 x->ss[search_site_count].mv.row = Len;
142 x->ss[search_site_count].offset = Len * stride;
143 search_site_count++;
145 // Compute offsets for search sites.
146 x->ss[search_site_count].mv.col = -Len;
147 x->ss[search_site_count].mv.row = 0;
148 x->ss[search_site_count].offset = -Len;
149 search_site_count++;
151 // Compute offsets for search sites.
152 x->ss[search_site_count].mv.col = Len;
153 x->ss[search_site_count].mv.row = 0;
154 x->ss[search_site_count].offset = Len;
155 search_site_count++;
157 // Compute offsets for search sites.
158 x->ss[search_site_count].mv.col = -Len;
159 x->ss[search_site_count].mv.row = -Len;
160 x->ss[search_site_count].offset = -Len * stride - Len;
161 search_site_count++;
163 // Compute offsets for search sites.
164 x->ss[search_site_count].mv.col = Len;
165 x->ss[search_site_count].mv.row = -Len;
166 x->ss[search_site_count].offset = -Len * stride + Len;
167 search_site_count++;
169 // Compute offsets for search sites.
170 x->ss[search_site_count].mv.col = -Len;
171 x->ss[search_site_count].mv.row = Len;
172 x->ss[search_site_count].offset = Len * stride - Len;
173 search_site_count++;
175 // Compute offsets for search sites.
176 x->ss[search_site_count].mv.col = Len;
177 x->ss[search_site_count].mv.row = Len;
178 x->ss[search_site_count].offset = Len * stride + Len;
179 search_site_count++;
182 // Contract.
183 Len /= 2;
186 x->ss_count = search_site_count;
187 x->searches_per_step = 8;
191 #define MVC(r,c) (((mvcost[0][(r)-rr] + mvcost[1][(c) - rc]) * error_per_bit + 128 )>>8 ) // estimated cost of a motion vector (r,c)
192 #define PRE(r,c) (*(d->base_pre) + d->pre + ((r)>>2) * d->pre_stride + ((c)>>2)) // pointer to predictor base of a motionvector
193 #define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
194 #define DIST(r,c) vfp->svf( PRE(r,c), d->pre_stride, SP(c),SP(r), z,b->src_stride,&sse) // returns subpixel variance error function.
195 #define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
196 #define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
197 #define CHECK_BETTER(v,r,c) IFMVCV(r,c,{if((v = ERR(r,c)) < besterr) { besterr = v; br=r; bc=c; }}, v=INT_MAX;)// checks if (r,c) has better score than previous best
198 #define MIN(x,y) (((x)<(y))?(x):(y))
199 #define MAX(x,y) (((x)>(y))?(x):(y))
201 //#define CHECK_BETTER(v,r,c) if((v = ERR(r,c)) < besterr) { besterr = v; br=r; bc=c; }
203 int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d, MV *bestmv, MV *ref_mv, int error_per_bit, const vp8_variance_fn_ptr_t *vfp, int *mvcost[2])
205 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->row) * d->pre_stride + bestmv->col;
206 unsigned char *z = (*(b->base_src) + b->src);
208 int rr = ref_mv->row >> 1, rc = ref_mv->col >> 1;
209 int br = bestmv->row << 2, bc = bestmv->col << 2;
210 int tr = br, tc = bc;
211 unsigned int besterr = INT_MAX;
212 unsigned int left, right, up, down, diag;
213 unsigned int sse;
214 unsigned int whichdir;
215 unsigned int halfiters = 4;
216 unsigned int quarteriters = 4;
218 int minc = MAX(x->mv_col_min << 2, (ref_mv->col >> 1) - ((1 << mvlong_width) - 1));
219 int maxc = MIN(x->mv_col_max << 2, (ref_mv->col >> 1) + ((1 << mvlong_width) - 1));
220 int minr = MAX(x->mv_row_min << 2, (ref_mv->row >> 1) - ((1 << mvlong_width) - 1));
221 int maxr = MIN(x->mv_row_max << 2, (ref_mv->row >> 1) + ((1 << mvlong_width) - 1));
223 // central mv
224 bestmv->row <<= 3;
225 bestmv->col <<= 3;
227 // calculate central point error
228 besterr = vfp->vf(y, d->pre_stride, z, b->src_stride, &sse);
229 besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
231 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
232 while (--halfiters)
234 // 1/2 pel
235 CHECK_BETTER(left, tr, tc - 2);
236 CHECK_BETTER(right, tr, tc + 2);
237 CHECK_BETTER(up, tr - 2, tc);
238 CHECK_BETTER(down, tr + 2, tc);
240 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
242 switch (whichdir)
244 case 0:
245 CHECK_BETTER(diag, tr - 2, tc - 2);
246 break;
247 case 1:
248 CHECK_BETTER(diag, tr - 2, tc + 2);
249 break;
250 case 2:
251 CHECK_BETTER(diag, tr + 2, tc - 2);
252 break;
253 case 3:
254 CHECK_BETTER(diag, tr + 2, tc + 2);
255 break;
258 // no reason to check the same one again.
259 if (tr == br && tc == bc)
260 break;
262 tr = br;
263 tc = bc;
266 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
267 // 1/4 pel
268 while (--quarteriters)
270 CHECK_BETTER(left, tr, tc - 1);
271 CHECK_BETTER(right, tr, tc + 1);
272 CHECK_BETTER(up, tr - 1, tc);
273 CHECK_BETTER(down, tr + 1, tc);
275 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
277 switch (whichdir)
279 case 0:
280 CHECK_BETTER(diag, tr - 1, tc - 1);
281 break;
282 case 1:
283 CHECK_BETTER(diag, tr - 1, tc + 1);
284 break;
285 case 2:
286 CHECK_BETTER(diag, tr + 1, tc - 1);
287 break;
288 case 3:
289 CHECK_BETTER(diag, tr + 1, tc + 1);
290 break;
293 // no reason to check the same one again.
294 if (tr == br && tc == bc)
295 break;
297 tr = br;
298 tc = bc;
301 bestmv->row = br << 1;
302 bestmv->col = bc << 1;
304 if ((abs(bestmv->col - ref_mv->col) > MAX_FULL_PEL_VAL) || (abs(bestmv->row - ref_mv->row) > MAX_FULL_PEL_VAL))
305 return INT_MAX;
307 return besterr;
309 #undef MVC
310 #undef PRE
311 #undef SP
312 #undef DIST
313 #undef ERR
314 #undef CHECK_BETTER
315 #undef MIN
316 #undef MAX
317 int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d, MV *bestmv, MV *ref_mv, int error_per_bit, const vp8_variance_fn_ptr_t *vfp, int *mvcost[2])
319 int bestmse = INT_MAX;
320 MV startmv;
321 //MV this_mv;
322 MV this_mv;
323 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->row) * d->pre_stride + bestmv->col;
324 unsigned char *z = (*(b->base_src) + b->src);
325 int left, right, up, down, diag;
326 unsigned int sse;
327 int whichdir ;
330 // Trap uncodable vectors
331 if ((abs((bestmv->col << 3) - ref_mv->col) > MAX_FULL_PEL_VAL) || (abs((bestmv->row << 3) - ref_mv->row) > MAX_FULL_PEL_VAL))
333 bestmv->row <<= 3;
334 bestmv->col <<= 3;
335 return INT_MAX;
338 // central mv
339 bestmv->row <<= 3;
340 bestmv->col <<= 3;
341 startmv = *bestmv;
343 // calculate central point error
344 bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, &sse);
345 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
347 // go left then right and check error
348 this_mv.row = startmv.row;
349 this_mv.col = ((startmv.col - 8) | 4);
350 left = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
351 left += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
353 if (left < bestmse)
355 *bestmv = this_mv;
356 bestmse = left;
359 this_mv.col += 8;
360 right = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
361 right += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
363 if (right < bestmse)
365 *bestmv = this_mv;
366 bestmse = right;
369 // go up then down and check error
370 this_mv.col = startmv.col;
371 this_mv.row = ((startmv.row - 8) | 4);
372 up = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
373 up += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
375 if (up < bestmse)
377 *bestmv = this_mv;
378 bestmse = up;
381 this_mv.row += 8;
382 down = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
383 down += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
385 if (down < bestmse)
387 *bestmv = this_mv;
388 bestmse = down;
392 // now check 1 more diagonal
393 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
394 //for(whichdir =0;whichdir<4;whichdir++)
396 this_mv = startmv;
398 switch (whichdir)
400 case 0:
401 this_mv.col = (this_mv.col - 8) | 4;
402 this_mv.row = (this_mv.row - 8) | 4;
403 diag = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
404 break;
405 case 1:
406 this_mv.col += 4;
407 this_mv.row = (this_mv.row - 8) | 4;
408 diag = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
409 break;
410 case 2:
411 this_mv.col = (this_mv.col - 8) | 4;
412 this_mv.row += 4;
413 diag = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
414 break;
415 case 3:
416 default:
417 this_mv.col += 4;
418 this_mv.row += 4;
419 diag = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
420 break;
423 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
425 if (diag < bestmse)
427 *bestmv = this_mv;
428 bestmse = diag;
431 // }
434 // time to check quarter pels.
435 if (bestmv->row < startmv.row)
436 y -= d->pre_stride;
438 if (bestmv->col < startmv.col)
439 y--;
441 startmv = *bestmv;
445 // go left then right and check error
446 this_mv.row = startmv.row;
448 if (startmv.col & 7)
450 this_mv.col = startmv.col - 2;
451 left = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
453 else
455 this_mv.col = (startmv.col - 8) | 6;
456 left = vfp->svf(y - 1, d->pre_stride, 6, this_mv.row & 7, z, b->src_stride, &sse);
459 left += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
461 if (left < bestmse)
463 *bestmv = this_mv;
464 bestmse = left;
467 this_mv.col += 4;
468 right = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
469 right += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
471 if (right < bestmse)
473 *bestmv = this_mv;
474 bestmse = right;
477 // go up then down and check error
478 this_mv.col = startmv.col;
480 if (startmv.row & 7)
482 this_mv.row = startmv.row - 2;
483 up = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
485 else
487 this_mv.row = (startmv.row - 8) | 6;
488 up = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.col & 7, 6, z, b->src_stride, &sse);
491 up += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
493 if (up < bestmse)
495 *bestmv = this_mv;
496 bestmse = up;
499 this_mv.row += 4;
500 down = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
501 down += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
503 if (down < bestmse)
505 *bestmv = this_mv;
506 bestmse = down;
510 // now check 1 more diagonal
511 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
513 // for(whichdir=0;whichdir<4;whichdir++)
514 // {
515 this_mv = startmv;
517 switch (whichdir)
519 case 0:
521 if (startmv.row & 7)
523 this_mv.row -= 2;
525 if (startmv.col & 7)
527 this_mv.col -= 2;
528 diag = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
530 else
532 this_mv.col = (startmv.col - 8) | 6;
533 diag = vfp->svf(y - 1, d->pre_stride, 6, this_mv.row & 7, z, b->src_stride, &sse);;
536 else
538 this_mv.row = (startmv.row - 8) | 6;
540 if (startmv.col & 7)
542 this_mv.col -= 2;
543 diag = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.col & 7, 6, z, b->src_stride, &sse);
545 else
547 this_mv.col = (startmv.col - 8) | 6;
548 diag = vfp->svf(y - d->pre_stride - 1, d->pre_stride, 6, 6, z, b->src_stride, &sse);
552 break;
553 case 1:
554 this_mv.col += 2;
556 if (startmv.row & 7)
558 this_mv.row -= 2;
559 diag = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
561 else
563 this_mv.row = (startmv.row - 8) | 6;
564 diag = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.col & 7, 6, z, b->src_stride, &sse);
567 break;
568 case 2:
569 this_mv.row += 2;
571 if (startmv.col & 7)
573 this_mv.col -= 2;
574 diag = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
576 else
578 this_mv.col = (startmv.col - 8) | 6;
579 diag = vfp->svf(y - 1, d->pre_stride, 6, this_mv.row & 7, z, b->src_stride, &sse);;
582 break;
583 case 3:
584 this_mv.col += 2;
585 this_mv.row += 2;
586 diag = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
587 break;
590 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
592 if (diag < bestmse)
594 *bestmv = this_mv;
595 bestmse = diag;
598 // }
600 return bestmse;
603 int vp8_find_best_half_pixel_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d, MV *bestmv, MV *ref_mv, int error_per_bit, const vp8_variance_fn_ptr_t *vfp, int *mvcost[2])
605 int bestmse = INT_MAX;
606 MV startmv;
607 //MV this_mv;
608 MV this_mv;
609 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->row) * d->pre_stride + bestmv->col;
610 unsigned char *z = (*(b->base_src) + b->src);
611 int left, right, up, down, diag;
612 unsigned int sse;
614 // Trap uncodable vectors
615 if ((abs((bestmv->col << 3) - ref_mv->col) > MAX_FULL_PEL_VAL) || (abs((bestmv->row << 3) - ref_mv->row) > MAX_FULL_PEL_VAL))
617 bestmv->row <<= 3;
618 bestmv->col <<= 3;
619 return INT_MAX;
622 // central mv
623 bestmv->row <<= 3;
624 bestmv->col <<= 3;
625 startmv = *bestmv;
627 // calculate central point error
628 bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, &sse);
629 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
631 // go left then right and check error
632 this_mv.row = startmv.row;
633 this_mv.col = ((startmv.col - 8) | 4);
634 left = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
635 left += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
637 if (left < bestmse)
639 *bestmv = this_mv;
640 bestmse = left;
643 this_mv.col += 8;
644 right = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
645 right += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
647 if (right < bestmse)
649 *bestmv = this_mv;
650 bestmse = right;
653 // go up then down and check error
654 this_mv.col = startmv.col;
655 this_mv.row = ((startmv.row - 8) | 4);
656 up = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
657 up += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
659 if (up < bestmse)
661 *bestmv = this_mv;
662 bestmse = up;
665 this_mv.row += 8;
666 down = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
667 down += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
669 if (down < bestmse)
671 *bestmv = this_mv;
672 bestmse = down;
675 // somewhat strangely not doing all the diagonals for half pel is slower than doing them.
676 #if 0
677 // now check 1 more diagonal -
678 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
679 this_mv = startmv;
681 switch (whichdir)
683 case 0:
684 this_mv.col = (this_mv.col - 8) | 4;
685 this_mv.row = (this_mv.row - 8) | 4;
686 diag = vfp->svf(y - 1 - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
687 break;
688 case 1:
689 this_mv.col += 4;
690 this_mv.row = (this_mv.row - 8) | 4;
691 diag = vfp->svf(y - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
692 break;
693 case 2:
694 this_mv.col = (this_mv.col - 8) | 4;
695 this_mv.row += 4;
696 diag = vfp->svf(y - 1, d->pre_stride, 4, 4, z, b->src_stride, &sse);
697 break;
698 case 3:
699 this_mv.col += 4;
700 this_mv.row += 4;
701 diag = vfp->svf(y, d->pre_stride, 4, 4, z, b->src_stride, &sse);
702 break;
705 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
707 if (diag < bestmse)
709 *bestmv = this_mv;
710 bestmse = diag;
713 #else
714 this_mv.col = (this_mv.col - 8) | 4;
715 this_mv.row = (this_mv.row - 8) | 4;
716 diag = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
717 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
719 if (diag < bestmse)
721 *bestmv = this_mv;
722 bestmse = diag;
725 this_mv.col += 8;
726 diag = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
727 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
729 if (diag < bestmse)
731 *bestmv = this_mv;
732 bestmse = diag;
735 this_mv.col = (this_mv.col - 8) | 4;
736 this_mv.row = startmv.row + 4;
737 diag = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
738 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
740 if (diag < bestmse)
742 *bestmv = this_mv;
743 bestmse = diag;
746 this_mv.col += 8;
747 diag = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
748 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
750 if (diag < bestmse)
752 *bestmv = this_mv;
753 bestmse = diag;
756 #endif
757 return bestmse;
761 #define MVC(r,c) (((mvsadcost[0][r-rr] + mvsadcost[1][c-rc]) * error_per_bit + 128 )>>8 ) // estimated cost of a motion vector (r,c)
762 #define PRE(r,c) (*(d->base_pre) + d->pre + (r) * d->pre_stride + (c)) // pointer to predictor base of a motionvector
763 #define DIST(r,c,v) vfp->sdf( src,src_stride,PRE(r,c),d->pre_stride, v) // returns sad error score.
764 #define ERR(r,c,v) (MVC(r,c)+DIST(r,c,v)) // returns distortion + motion vector cost
765 #define CHECK_BETTER(v,r,c) if ((v = ERR(r,c,besterr)) < besterr) { besterr = v; br=r; bc=c; } // checks if (r,c) has better score than previous best
766 static const MV next_chkpts[6][3] =
768 {{ -2, 0}, { -1, -2}, {1, -2}},
769 {{ -1, -2}, {1, -2}, {2, 0}},
770 {{1, -2}, {2, 0}, {1, 2}},
771 {{2, 0}, {1, 2}, { -1, 2}},
772 {{1, 2}, { -1, 2}, { -2, 0}},
773 {{ -1, 2}, { -2, 0}, { -1, -2}}
775 int vp8_hex_search
777 MACROBLOCK *x,
778 BLOCK *b,
779 BLOCKD *d,
780 MV *ref_mv,
781 MV *best_mv,
782 int search_param,
783 int error_per_bit,
784 int *num00,
785 const vp8_variance_fn_ptr_t *vfp,
786 int *mvsadcost[2],
787 int *mvcost[2],
788 MV *center_mv
791 MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
792 MV neighbors[8] = { { -1, -1}, {0, -1}, {1, -1}, { -1, 0}, {1, 0}, { -1, 1}, {0, 1}, {1, 1} } ;
793 int i, j;
794 unsigned char *src = (*(b->base_src) + b->src);
795 int src_stride = b->src_stride;
796 int rr = center_mv->row, rc = center_mv->col;
797 int br = ref_mv->row >> 3, bc = ref_mv->col >> 3, tr, tc;
798 unsigned int besterr, thiserr = 0x7fffffff;
799 int k = -1, tk;
801 if (bc < x->mv_col_min) bc = x->mv_col_min;
803 if (bc > x->mv_col_max) bc = x->mv_col_max;
805 if (br < x->mv_row_min) br = x->mv_row_min;
807 if (br > x->mv_row_max) br = x->mv_row_max;
809 rr >>= 3;
810 rc >>= 3;
812 besterr = ERR(br, bc, thiserr);
814 // hex search
815 //j=0
816 tr = br;
817 tc = bc;
819 for (i = 0; i < 6; i++)
821 int nr = tr + hex[i].row, nc = tc + hex[i].col;
823 if (nc < x->mv_col_min) continue;
825 if (nc > x->mv_col_max) continue;
827 if (nr < x->mv_row_min) continue;
829 if (nr > x->mv_row_max) continue;
831 //CHECK_BETTER(thiserr,nr,nc);
832 if ((thiserr = ERR(nr, nc, besterr)) < besterr)
834 besterr = thiserr;
835 br = nr;
836 bc = nc;
837 k = i;
841 if (tr == br && tc == bc)
842 goto cal_neighbors;
844 for (j = 1; j < 127; j++)
846 tr = br;
847 tc = bc;
848 tk = k;
850 for (i = 0; i < 3; i++)
852 int nr = tr + next_chkpts[tk][i].row, nc = tc + next_chkpts[tk][i].col;
854 if (nc < x->mv_col_min) continue;
856 if (nc > x->mv_col_max) continue;
858 if (nr < x->mv_row_min) continue;
860 if (nr > x->mv_row_max) continue;
862 //CHECK_BETTER(thiserr,nr,nc);
863 if ((thiserr = ERR(nr, nc, besterr)) < besterr)
865 besterr = thiserr;
866 br = nr;
867 bc = nc; //k=(tk+5+i)%6;}
868 k = tk + 5 + i;
870 if (k >= 12) k -= 12;
871 else if (k >= 6) k -= 6;
875 if (tr == br && tc == bc)
876 break;
879 // check 8 1 away neighbors
880 cal_neighbors:
881 tr = br;
882 tc = bc;
884 for (i = 0; i < 8; i++)
886 int nr = tr + neighbors[i].row, nc = tc + neighbors[i].col;
888 if (nc < x->mv_col_min) continue;
890 if (nc > x->mv_col_max) continue;
892 if (nr < x->mv_row_min) continue;
894 if (nr > x->mv_row_max) continue;
896 CHECK_BETTER(thiserr, nr, nc);
899 best_mv->row = br;
900 best_mv->col = bc;
902 return vfp->vf(src, src_stride, PRE(br, bc), d->pre_stride, &thiserr) + mv_err_cost(best_mv, center_mv, mvcost, error_per_bit) ;
904 #undef MVC
905 #undef PRE
906 #undef SP
907 #undef DIST
908 #undef ERR
909 #undef CHECK_BETTER
912 int vp8_diamond_search_sad
914 MACROBLOCK *x,
915 BLOCK *b,
916 BLOCKD *d,
917 MV *ref_mv,
918 MV *best_mv,
919 int search_param,
920 int error_per_bit,
921 int *num00,
922 vp8_variance_fn_ptr_t *fn_ptr,
923 int *mvcost[2],
924 MV *center_mv
927 int i, j, step;
929 unsigned char *what = (*(b->base_src) + b->src);
930 int what_stride = b->src_stride;
931 unsigned char *in_what;
932 int in_what_stride = d->pre_stride;
933 unsigned char *best_address;
935 int tot_steps;
936 MV this_mv;
938 int bestsad = INT_MAX;
939 int best_site = 0;
940 int last_site = 0;
942 int ref_row = ref_mv->row >> 3;
943 int ref_col = ref_mv->col >> 3;
944 int this_row_offset;
945 int this_col_offset;
946 search_site *ss;
948 unsigned char *check_here;
949 int thissad;
951 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
952 MV fcenter_mv;
953 fcenter_mv.row = center_mv->row >> 3;
954 fcenter_mv.col = center_mv->col >> 3;
956 *num00 = 0;
958 best_mv->row = ref_row;
959 best_mv->col = ref_col;
961 // Work out the start point for the search
962 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
963 best_address = in_what;
965 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
966 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
967 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
969 // Check the starting position
970 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
973 // search_param determines the length of the initial step and hence the number of iterations
974 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
975 ss = &x->ss[search_param * x->searches_per_step];
976 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
978 i = 1;
980 for (step = 0; step < tot_steps ; step++)
982 for (j = 0 ; j < x->searches_per_step ; j++)
984 // Trap illegal vectors
985 this_row_offset = best_mv->row + ss[i].mv.row;
986 this_col_offset = best_mv->col + ss[i].mv.col;
988 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
989 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
992 check_here = ss[i].offset + best_address;
993 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
995 if (thissad < bestsad)
997 this_mv.row = this_row_offset;
998 this_mv.col = this_col_offset;
999 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1001 if (thissad < bestsad)
1003 bestsad = thissad;
1004 best_site = i;
1009 i++;
1012 if (best_site != last_site)
1014 best_mv->row += ss[best_site].mv.row;
1015 best_mv->col += ss[best_site].mv.col;
1016 best_address += ss[best_site].offset;
1017 last_site = best_site;
1019 else if (best_address == in_what)
1020 (*num00)++;
1023 this_mv.row = best_mv->row << 3;
1024 this_mv.col = best_mv->col << 3;
1026 if (bestsad == INT_MAX)
1027 return INT_MAX;
1029 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1030 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1033 int vp8_diamond_search_sadx4
1035 MACROBLOCK *x,
1036 BLOCK *b,
1037 BLOCKD *d,
1038 MV *ref_mv,
1039 MV *best_mv,
1040 int search_param,
1041 int error_per_bit,
1042 int *num00,
1043 vp8_variance_fn_ptr_t *fn_ptr,
1044 int *mvcost[2],
1045 MV *center_mv
1048 int i, j, step;
1050 unsigned char *what = (*(b->base_src) + b->src);
1051 int what_stride = b->src_stride;
1052 unsigned char *in_what;
1053 int in_what_stride = d->pre_stride;
1054 unsigned char *best_address;
1056 int tot_steps;
1057 MV this_mv;
1059 int bestsad = INT_MAX;
1060 int best_site = 0;
1061 int last_site = 0;
1063 int ref_row = ref_mv->row >> 3;
1064 int ref_col = ref_mv->col >> 3;
1065 int this_row_offset;
1066 int this_col_offset;
1067 search_site *ss;
1069 unsigned char *check_here;
1070 unsigned int thissad;
1072 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1073 MV fcenter_mv;
1074 fcenter_mv.row = center_mv->row >> 3;
1075 fcenter_mv.col = center_mv->col >> 3;
1077 *num00 = 0;
1078 best_mv->row = ref_row;
1079 best_mv->col = ref_col;
1081 // Work out the start point for the search
1082 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1083 best_address = in_what;
1085 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1086 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1087 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1089 // Check the starting position
1090 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1093 // search_param determines the length of the initial step and hence the number of iterations
1094 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1095 ss = &x->ss[search_param * x->searches_per_step];
1096 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1098 i = 1;
1100 for (step = 0; step < tot_steps ; step++)
1102 int all_in = 1, t;
1104 // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1105 // checking 4 bounds for each points.
1106 all_in &= ((best_mv->row + ss[i].mv.row)> x->mv_row_min);
1107 all_in &= ((best_mv->row + ss[i+1].mv.row) < x->mv_row_max);
1108 all_in &= ((best_mv->col + ss[i+2].mv.col) > x->mv_col_min);
1109 all_in &= ((best_mv->col + ss[i+3].mv.col) < x->mv_col_max);
1111 if (all_in)
1113 unsigned int sad_array[4];
1115 for (j = 0 ; j < x->searches_per_step ; j += 4)
1117 unsigned char *block_offset[4];
1119 for (t = 0; t < 4; t++)
1120 block_offset[t] = ss[i+t].offset + best_address;
1122 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1124 for (t = 0; t < 4; t++, i++)
1126 if (sad_array[t] < bestsad)
1128 this_mv.row = best_mv->row + ss[i].mv.row;
1129 this_mv.col = best_mv->col + ss[i].mv.col;
1130 sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1132 if (sad_array[t] < bestsad)
1134 bestsad = sad_array[t];
1135 best_site = i;
1141 else
1143 for (j = 0 ; j < x->searches_per_step ; j++)
1145 // Trap illegal vectors
1146 this_row_offset = best_mv->row + ss[i].mv.row;
1147 this_col_offset = best_mv->col + ss[i].mv.col;
1149 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1150 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1152 check_here = ss[i].offset + best_address;
1153 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1155 if (thissad < bestsad)
1157 this_mv.row = this_row_offset;
1158 this_mv.col = this_col_offset;
1159 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1161 if (thissad < bestsad)
1163 bestsad = thissad;
1164 best_site = i;
1168 i++;
1172 if (best_site != last_site)
1174 best_mv->row += ss[best_site].mv.row;
1175 best_mv->col += ss[best_site].mv.col;
1176 best_address += ss[best_site].offset;
1177 last_site = best_site;
1179 else if (best_address == in_what)
1180 (*num00)++;
1183 this_mv.row = best_mv->row << 3;
1184 this_mv.col = best_mv->col << 3;
1186 if (bestsad == INT_MAX)
1187 return INT_MAX;
1189 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1190 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1194 #if !(CONFIG_REALTIME_ONLY)
1195 int vp8_full_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, MV *ref_mv, int error_per_bit, int distance, vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], MV *center_mv)
1197 unsigned char *what = (*(b->base_src) + b->src);
1198 int what_stride = b->src_stride;
1199 unsigned char *in_what;
1200 int in_what_stride = d->pre_stride;
1201 int mv_stride = d->pre_stride;
1202 unsigned char *bestaddress;
1203 MV *best_mv = &d->bmi.mv.as_mv;
1204 MV this_mv;
1205 int bestsad = INT_MAX;
1206 int r, c;
1208 unsigned char *check_here;
1209 int thissad;
1211 int ref_row = ref_mv->row;
1212 int ref_col = ref_mv->col;
1214 int row_min = ref_row - distance;
1215 int row_max = ref_row + distance;
1216 int col_min = ref_col - distance;
1217 int col_max = ref_col + distance;
1219 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1220 MV fcenter_mv;
1221 fcenter_mv.row = center_mv->row >> 3;
1222 fcenter_mv.col = center_mv->col >> 3;
1224 // Work out the mid point for the search
1225 in_what = *(d->base_pre) + d->pre;
1226 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1228 best_mv->row = ref_row;
1229 best_mv->col = ref_col;
1231 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1232 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1233 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1235 // Baseline value at the centre
1237 //bestsad = fn_ptr->sf( what,what_stride,bestaddress,in_what_stride) + (int)sqrt(mv_err_cost(ref_mv,ref_mv, mvcost,error_per_bit*14));
1238 bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1241 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1242 if (col_min < x->mv_col_min)
1243 col_min = x->mv_col_min;
1245 if (col_max > x->mv_col_max)
1246 col_max = x->mv_col_max;
1248 if (row_min < x->mv_row_min)
1249 row_min = x->mv_row_min;
1251 if (row_max > x->mv_row_max)
1252 row_max = x->mv_row_max;
1254 for (r = row_min; r < row_max ; r++)
1256 this_mv.row = r;
1257 check_here = r * mv_stride + in_what + col_min;
1259 for (c = col_min; c < col_max; c++)
1261 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1263 this_mv.col = c;
1264 //thissad += (int)sqrt(mv_err_cost(&this_mv,ref_mv, mvcost,error_per_bit*14));
1265 //thissad += error_per_bit * mv_bits_sadcost[mv_bits(&this_mv, ref_mv, mvcost)];
1266 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit); //mv_bits(error_per_bit, &this_mv, ref_mv, mvsadcost);
1268 if (thissad < bestsad)
1270 bestsad = thissad;
1271 best_mv->row = r;
1272 best_mv->col = c;
1273 bestaddress = check_here;
1276 check_here++;
1280 this_mv.row = best_mv->row << 3;
1281 this_mv.col = best_mv->col << 3;
1283 if (bestsad < INT_MAX)
1284 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1285 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1286 else
1287 return INT_MAX;
1290 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, MV *ref_mv, int error_per_bit, int distance, vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], MV *center_mv)
1292 unsigned char *what = (*(b->base_src) + b->src);
1293 int what_stride = b->src_stride;
1294 unsigned char *in_what;
1295 int in_what_stride = d->pre_stride;
1296 int mv_stride = d->pre_stride;
1297 unsigned char *bestaddress;
1298 MV *best_mv = &d->bmi.mv.as_mv;
1299 MV this_mv;
1300 int bestsad = INT_MAX;
1301 int r, c;
1303 unsigned char *check_here;
1304 unsigned int thissad;
1306 int ref_row = ref_mv->row;
1307 int ref_col = ref_mv->col;
1309 int row_min = ref_row - distance;
1310 int row_max = ref_row + distance;
1311 int col_min = ref_col - distance;
1312 int col_max = ref_col + distance;
1314 unsigned int sad_array[3];
1316 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1317 MV fcenter_mv;
1318 fcenter_mv.row = center_mv->row >> 3;
1319 fcenter_mv.col = center_mv->col >> 3;
1321 // Work out the mid point for the search
1322 in_what = *(d->base_pre) + d->pre;
1323 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1325 best_mv->row = ref_row;
1326 best_mv->col = ref_col;
1328 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1329 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1330 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1332 // Baseline value at the centre
1333 bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1336 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1337 if (col_min < x->mv_col_min)
1338 col_min = x->mv_col_min;
1340 if (col_max > x->mv_col_max)
1341 col_max = x->mv_col_max;
1343 if (row_min < x->mv_row_min)
1344 row_min = x->mv_row_min;
1346 if (row_max > x->mv_row_max)
1347 row_max = x->mv_row_max;
1349 for (r = row_min; r < row_max ; r++)
1351 this_mv.row = r;
1352 check_here = r * mv_stride + in_what + col_min;
1353 c = col_min;
1355 while ((c + 2) < col_max)
1357 int i;
1359 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1361 for (i = 0; i < 3; i++)
1363 thissad = sad_array[i];
1365 if (thissad < bestsad)
1367 this_mv.col = c;
1368 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1370 if (thissad < bestsad)
1372 bestsad = thissad;
1373 best_mv->row = r;
1374 best_mv->col = c;
1375 bestaddress = check_here;
1379 check_here++;
1380 c++;
1384 while (c < col_max)
1386 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1388 if (thissad < bestsad)
1390 this_mv.col = c;
1391 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1393 if (thissad < bestsad)
1395 bestsad = thissad;
1396 best_mv->row = r;
1397 best_mv->col = c;
1398 bestaddress = check_here;
1402 check_here ++;
1403 c ++;
1408 this_mv.row = best_mv->row << 3;
1409 this_mv.col = best_mv->col << 3;
1411 if (bestsad < INT_MAX)
1412 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1413 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1414 else
1415 return INT_MAX;
1418 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, MV *ref_mv, int error_per_bit, int distance, vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], MV *center_mv)
1420 unsigned char *what = (*(b->base_src) + b->src);
1421 int what_stride = b->src_stride;
1422 unsigned char *in_what;
1423 int in_what_stride = d->pre_stride;
1424 int mv_stride = d->pre_stride;
1425 unsigned char *bestaddress;
1426 MV *best_mv = &d->bmi.mv.as_mv;
1427 MV this_mv;
1428 int bestsad = INT_MAX;
1429 int r, c;
1431 unsigned char *check_here;
1432 unsigned int thissad;
1434 int ref_row = ref_mv->row;
1435 int ref_col = ref_mv->col;
1437 int row_min = ref_row - distance;
1438 int row_max = ref_row + distance;
1439 int col_min = ref_col - distance;
1440 int col_max = ref_col + distance;
1442 DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1443 unsigned int sad_array[3];
1445 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1446 MV fcenter_mv;
1447 fcenter_mv.row = center_mv->row >> 3;
1448 fcenter_mv.col = center_mv->col >> 3;
1450 // Work out the mid point for the search
1451 in_what = *(d->base_pre) + d->pre;
1452 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1454 best_mv->row = ref_row;
1455 best_mv->col = ref_col;
1457 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1458 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1459 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1461 // Baseline value at the centre
1462 bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1465 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1466 if (col_min < x->mv_col_min)
1467 col_min = x->mv_col_min;
1469 if (col_max > x->mv_col_max)
1470 col_max = x->mv_col_max;
1472 if (row_min < x->mv_row_min)
1473 row_min = x->mv_row_min;
1475 if (row_max > x->mv_row_max)
1476 row_max = x->mv_row_max;
1478 for (r = row_min; r < row_max ; r++)
1480 this_mv.row = r;
1481 check_here = r * mv_stride + in_what + col_min;
1482 c = col_min;
1484 while ((c + 7) < col_max)
1486 int i;
1488 fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1490 for (i = 0; i < 8; i++)
1492 thissad = (unsigned int)sad_array8[i];
1494 if (thissad < bestsad)
1496 this_mv.col = c;
1497 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1499 if (thissad < bestsad)
1501 bestsad = thissad;
1502 best_mv->row = r;
1503 best_mv->col = c;
1504 bestaddress = check_here;
1508 check_here++;
1509 c++;
1513 while ((c + 2) < col_max)
1515 int i;
1517 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1519 for (i = 0; i < 3; i++)
1521 thissad = sad_array[i];
1523 if (thissad < bestsad)
1525 this_mv.col = c;
1526 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1528 if (thissad < bestsad)
1530 bestsad = thissad;
1531 best_mv->row = r;
1532 best_mv->col = c;
1533 bestaddress = check_here;
1537 check_here++;
1538 c++;
1542 while (c < col_max)
1544 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1546 if (thissad < bestsad)
1548 this_mv.col = c;
1549 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1551 if (thissad < bestsad)
1553 bestsad = thissad;
1554 best_mv->row = r;
1555 best_mv->col = c;
1556 bestaddress = check_here;
1560 check_here ++;
1561 c ++;
1565 this_mv.row = best_mv->row << 3;
1566 this_mv.col = best_mv->col << 3;
1568 if (bestsad < INT_MAX)
1569 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1570 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1571 else
1572 return INT_MAX;
1574 #endif /* !(CONFIG_REALTIME_ONLY) */
1576 #ifdef ENTROPY_STATS
1577 void print_mode_context(void)
1579 FILE *f = fopen("modecont.c", "w");
1580 int i, j;
1582 fprintf(f, "#include \"entropy.h\"\n");
1583 fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1584 fprintf(f, "{\n");
1586 for (j = 0; j < 6; j++)
1588 fprintf(f, " { // %d \n", j);
1589 fprintf(f, " ");
1591 for (i = 0; i < 4; i++)
1593 int overal_prob;
1594 int this_prob;
1595 int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1597 // Overall probs
1598 count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1600 if (count)
1601 overal_prob = 256 * mv_mode_cts[i][0] / count;
1602 else
1603 overal_prob = 128;
1605 if (overal_prob == 0)
1606 overal_prob = 1;
1608 // context probs
1609 count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1611 if (count)
1612 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1613 else
1614 this_prob = 128;
1616 if (this_prob == 0)
1617 this_prob = 1;
1619 fprintf(f, "%5d, ", this_prob);
1620 //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1621 //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1624 fprintf(f, " },\n");
1627 fprintf(f, "};\n");
1628 fclose(f);
1631 /* MV ref count ENTROPY_STATS stats code */
1632 #ifdef ENTROPY_STATS
1633 void init_mv_ref_counts()
1635 vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1636 vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1639 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1641 if (m == ZEROMV)
1643 ++mv_ref_ct [ct[0]] [0] [0];
1644 ++mv_mode_cts[0][0];
1646 else
1648 ++mv_ref_ct [ct[0]] [0] [1];
1649 ++mv_mode_cts[0][1];
1651 if (m == NEARESTMV)
1653 ++mv_ref_ct [ct[1]] [1] [0];
1654 ++mv_mode_cts[1][0];
1656 else
1658 ++mv_ref_ct [ct[1]] [1] [1];
1659 ++mv_mode_cts[1][1];
1661 if (m == NEARMV)
1663 ++mv_ref_ct [ct[2]] [2] [0];
1664 ++mv_mode_cts[2][0];
1666 else
1668 ++mv_ref_ct [ct[2]] [2] [1];
1669 ++mv_mode_cts[2][1];
1671 if (m == NEWMV)
1673 ++mv_ref_ct [ct[3]] [3] [0];
1674 ++mv_mode_cts[3][0];
1676 else
1678 ++mv_ref_ct [ct[3]] [3] [1];
1679 ++mv_mode_cts[3][1];
1686 #endif/* END MV ref count ENTROPY_STATS stats code */
1688 #endif