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.
13 #include "vpx_mem/vpx_mem.h"
20 static int mv_ref_ct [31] [4] [2];
21 static int mv_mode_cts [4] [2];
24 static int mv_bits_sadcost[256];
26 void vp8cx_init_mv_bits_sadcost()
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)
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
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)
73 int search_site_count = 0;
76 // Generate offsets for 4 search sites per 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;
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;
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;
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;
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;
114 x->ss_count = search_site_count;
115 x->searches_per_step = 4;
118 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
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;
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;
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;
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;
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;
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;
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;
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;
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;
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,{thismse = DIST(r,c); if((v = (MVC(r,c)+thismse)) < besterr) { besterr = v; br=r; bc=c; *distortion = thismse; *sse1 = sse; }}, 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], int *distortion, unsigned int *sse1)
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;
214 unsigned int whichdir;
215 unsigned int halfiters = 4;
216 unsigned int quarteriters = 4;
219 int minc = MAX(x->mv_col_min << 2, (ref_mv->col >> 1) - ((1 << mvlong_width) - 1));
220 int maxc = MIN(x->mv_col_max << 2, (ref_mv->col >> 1) + ((1 << mvlong_width) - 1));
221 int minr = MAX(x->mv_row_min << 2, (ref_mv->row >> 1) - ((1 << mvlong_width) - 1));
222 int maxr = MIN(x->mv_row_max << 2, (ref_mv->row >> 1) + ((1 << mvlong_width) - 1));
228 // calculate central point error
229 besterr = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
230 *distortion = besterr;
231 besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
233 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
237 CHECK_BETTER(left, tr, tc - 2);
238 CHECK_BETTER(right, tr, tc + 2);
239 CHECK_BETTER(up, tr - 2, tc);
240 CHECK_BETTER(down, tr + 2, tc);
242 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
247 CHECK_BETTER(diag, tr - 2, tc - 2);
250 CHECK_BETTER(diag, tr - 2, tc + 2);
253 CHECK_BETTER(diag, tr + 2, tc - 2);
256 CHECK_BETTER(diag, tr + 2, tc + 2);
260 // no reason to check the same one again.
261 if (tr == br && tc == bc)
268 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
270 while (--quarteriters)
272 CHECK_BETTER(left, tr, tc - 1);
273 CHECK_BETTER(right, tr, tc + 1);
274 CHECK_BETTER(up, tr - 1, tc);
275 CHECK_BETTER(down, tr + 1, tc);
277 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
282 CHECK_BETTER(diag, tr - 1, tc - 1);
285 CHECK_BETTER(diag, tr - 1, tc + 1);
288 CHECK_BETTER(diag, tr + 1, tc - 1);
291 CHECK_BETTER(diag, tr + 1, tc + 1);
295 // no reason to check the same one again.
296 if (tr == br && tc == bc)
303 bestmv->row = br << 1;
304 bestmv->col = bc << 1;
306 if ((abs(bestmv->col - ref_mv->col) > MAX_FULL_PEL_VAL) || (abs(bestmv->row - ref_mv->row) > MAX_FULL_PEL_VAL))
319 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], int *distortion, unsigned int *sse1)
321 int bestmse = INT_MAX;
325 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->row) * d->pre_stride + bestmv->col;
326 unsigned char *z = (*(b->base_src) + b->src);
327 int left, right, up, down, diag;
333 // Trap uncodable vectors
334 if ((abs((bestmv->col << 3) - ref_mv->col) > MAX_FULL_PEL_VAL) || (abs((bestmv->row << 3) - ref_mv->row) > MAX_FULL_PEL_VAL))
338 *distortion = INT_MAX;
347 // calculate central point error
348 bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
349 *distortion = bestmse;
350 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
352 // go left then right and check error
353 this_mv.row = startmv.row;
354 this_mv.col = ((startmv.col - 8) | 4);
355 thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
356 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
362 *distortion = thismse;
367 thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
368 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
374 *distortion = thismse;
378 // go up then down and check error
379 this_mv.col = startmv.col;
380 this_mv.row = ((startmv.row - 8) | 4);
381 thismse = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
382 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
388 *distortion = thismse;
393 thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
394 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
400 *distortion = thismse;
405 // now check 1 more diagonal
406 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
407 //for(whichdir =0;whichdir<4;whichdir++)
414 this_mv.col = (this_mv.col - 8) | 4;
415 this_mv.row = (this_mv.row - 8) | 4;
416 thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
420 this_mv.row = (this_mv.row - 8) | 4;
421 thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
424 this_mv.col = (this_mv.col - 8) | 4;
426 thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
432 thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
436 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
442 *distortion = thismse;
449 // time to check quarter pels.
450 if (bestmv->row < startmv.row)
453 if (bestmv->col < startmv.col)
460 // go left then right and check error
461 this_mv.row = startmv.row;
465 this_mv.col = startmv.col - 2;
466 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
470 this_mv.col = (startmv.col - 8) | 6;
471 thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.row & 7, z, b->src_stride, &sse);
474 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
480 *distortion = thismse;
485 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
486 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
492 *distortion = thismse;
496 // go up then down and check error
497 this_mv.col = startmv.col;
501 this_mv.row = startmv.row - 2;
502 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
506 this_mv.row = (startmv.row - 8) | 6;
507 thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.col & 7, 6, z, b->src_stride, &sse);
510 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
516 *distortion = thismse;
521 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
522 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
528 *distortion = thismse;
533 // now check 1 more diagonal
534 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
536 // for(whichdir=0;whichdir<4;whichdir++)
551 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
555 this_mv.col = (startmv.col - 8) | 6;
556 thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.row & 7, z, b->src_stride, &sse);;
561 this_mv.row = (startmv.row - 8) | 6;
566 thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.col & 7, 6, z, b->src_stride, &sse);
570 this_mv.col = (startmv.col - 8) | 6;
571 thismse = vfp->svf(y - d->pre_stride - 1, d->pre_stride, 6, 6, z, b->src_stride, &sse);
582 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
586 this_mv.row = (startmv.row - 8) | 6;
587 thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.col & 7, 6, z, b->src_stride, &sse);
597 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
601 this_mv.col = (startmv.col - 8) | 6;
602 thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.row & 7, z, b->src_stride, &sse);;
609 thismse = vfp->svf(y, d->pre_stride, this_mv.col & 7, this_mv.row & 7, z, b->src_stride, &sse);
613 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
619 *distortion = thismse;
626 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], int *distortion, unsigned int *sse1)
628 int bestmse = INT_MAX;
632 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->row) * d->pre_stride + bestmv->col;
633 unsigned char *z = (*(b->base_src) + b->src);
634 int left, right, up, down, diag;
638 // Trap uncodable vectors
639 if ((abs((bestmv->col << 3) - ref_mv->col) > MAX_FULL_PEL_VAL) || (abs((bestmv->row << 3) - ref_mv->row) > MAX_FULL_PEL_VAL))
643 *distortion = INT_MAX;
652 // calculate central point error
653 bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
654 *distortion = bestmse;
655 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
657 // go left then right and check error
658 this_mv.row = startmv.row;
659 this_mv.col = ((startmv.col - 8) | 4);
660 thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
661 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
667 *distortion = thismse;
672 thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
673 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
679 *distortion = thismse;
683 // go up then down and check error
684 this_mv.col = startmv.col;
685 this_mv.row = ((startmv.row - 8) | 4);
686 thismse = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
687 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
693 *distortion = thismse;
698 thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
699 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
705 *distortion = thismse;
709 // somewhat strangely not doing all the diagonals for half pel is slower than doing them.
711 // now check 1 more diagonal -
712 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
718 this_mv.col = (this_mv.col - 8) | 4;
719 this_mv.row = (this_mv.row - 8) | 4;
720 diag = vfp->svf(y - 1 - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
724 this_mv.row = (this_mv.row - 8) | 4;
725 diag = vfp->svf(y - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
728 this_mv.col = (this_mv.col - 8) | 4;
730 diag = vfp->svf(y - 1, d->pre_stride, 4, 4, z, b->src_stride, &sse);
735 diag = vfp->svf(y, d->pre_stride, 4, 4, z, b->src_stride, &sse);
739 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
748 this_mv.col = (this_mv.col - 8) | 4;
749 this_mv.row = (this_mv.row - 8) | 4;
750 thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
751 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
757 *distortion = thismse;
762 thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
763 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
769 *distortion = thismse;
773 this_mv.col = (this_mv.col - 8) | 4;
774 this_mv.row = startmv.row + 4;
775 thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
776 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
782 *distortion = thismse;
787 thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
788 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
794 *distortion = thismse;
803 #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)
804 #define PRE(r,c) (*(d->base_pre) + d->pre + (r) * d->pre_stride + (c)) // pointer to predictor base of a motionvector
805 #define DIST(r,c,v) vfp->sdf( src,src_stride,PRE(r,c),d->pre_stride, v) // returns sad error score.
806 #define ERR(r,c,v) (MVC(r,c)+DIST(r,c,v)) // returns distortion + motion vector cost
807 #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
808 static const MV next_chkpts[6][3] =
810 {{ -2, 0}, { -1, -2}, {1, -2}},
811 {{ -1, -2}, {1, -2}, {2, 0}},
812 {{1, -2}, {2, 0}, {1, 2}},
813 {{2, 0}, {1, 2}, { -1, 2}},
814 {{1, 2}, { -1, 2}, { -2, 0}},
815 {{ -1, 2}, { -2, 0}, { -1, -2}}
827 const vp8_variance_fn_ptr_t *vfp,
833 MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
834 //MV neighbors[8] = { { -1, -1}, {0, -1}, {1, -1}, { -1, 0}, {1, 0}, { -1, 1}, {0, 1}, {1, 1} } ;
835 MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
838 unsigned char *src = (*(b->base_src) + b->src);
839 int src_stride = b->src_stride;
840 int rr = center_mv->row, rc = center_mv->col;
841 int br = ref_mv->row >> 3, bc = ref_mv->col >> 3, tr, tc;
842 unsigned int besterr, thiserr = 0x7fffffff;
845 if (bc < x->mv_col_min) bc = x->mv_col_min;
847 if (bc > x->mv_col_max) bc = x->mv_col_max;
849 if (br < x->mv_row_min) br = x->mv_row_min;
851 if (br > x->mv_row_max) br = x->mv_row_max;
856 besterr = ERR(br, bc, thiserr);
863 for (i = 0; i < 6; i++)
865 int nr = tr + hex[i].row, nc = tc + hex[i].col;
867 if (nc < x->mv_col_min) continue;
869 if (nc > x->mv_col_max) continue;
871 if (nr < x->mv_row_min) continue;
873 if (nr > x->mv_row_max) continue;
875 //CHECK_BETTER(thiserr,nr,nc);
876 if ((thiserr = ERR(nr, nc, besterr)) < besterr)
885 if (tr == br && tc == bc)
888 for (j = 1; j < 127; j++)
894 for (i = 0; i < 3; i++)
896 int nr = tr + next_chkpts[tk][i].row, nc = tc + next_chkpts[tk][i].col;
898 if (nc < x->mv_col_min) continue;
900 if (nc > x->mv_col_max) continue;
902 if (nr < x->mv_row_min) continue;
904 if (nr > x->mv_row_max) continue;
906 //CHECK_BETTER(thiserr,nr,nc);
907 if ((thiserr = ERR(nr, nc, besterr)) < besterr)
911 bc = nc; //k=(tk+5+i)%6;}
914 if (k >= 12) k -= 12;
915 else if (k >= 6) k -= 6;
919 if (tr == br && tc == bc)
923 // check 4 1-away neighbors
926 for (j = 0; j < 32; j++)
931 for (i = 0; i < 4; i++)
933 int nr = tr + neighbors[i].row, nc = tc + neighbors[i].col;
935 if (nc < x->mv_col_min) continue;
937 if (nc > x->mv_col_max) continue;
939 if (nr < x->mv_row_min) continue;
941 if (nr > x->mv_row_max) continue;
943 CHECK_BETTER(thiserr, nr, nc);
946 if (tr == br && tc == bc)
953 return vfp->vf(src, src_stride, PRE(br, bc), d->pre_stride, &thiserr) + mv_err_cost(best_mv, center_mv, mvcost, error_per_bit) ;
963 int vp8_diamond_search_sad
973 vp8_variance_fn_ptr_t *fn_ptr,
980 unsigned char *what = (*(b->base_src) + b->src);
981 int what_stride = b->src_stride;
982 unsigned char *in_what;
983 int in_what_stride = d->pre_stride;
984 unsigned char *best_address;
989 int bestsad = INT_MAX;
993 int ref_row = ref_mv->row >> 3;
994 int ref_col = ref_mv->col >> 3;
999 unsigned char *check_here;
1002 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1004 fcenter_mv.row = center_mv->row >> 3;
1005 fcenter_mv.col = center_mv->col >> 3;
1009 best_mv->row = ref_row;
1010 best_mv->col = ref_col;
1012 // Work out the start point for the search
1013 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1014 best_address = in_what;
1016 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1017 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1018 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1020 // Check the starting position
1021 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1024 // search_param determines the length of the initial step and hence the number of iterations
1025 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1026 ss = &x->ss[search_param * x->searches_per_step];
1027 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1031 for (step = 0; step < tot_steps ; step++)
1033 for (j = 0 ; j < x->searches_per_step ; j++)
1035 // Trap illegal vectors
1036 this_row_offset = best_mv->row + ss[i].mv.row;
1037 this_col_offset = best_mv->col + ss[i].mv.col;
1039 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1040 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1043 check_here = ss[i].offset + best_address;
1044 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1046 if (thissad < bestsad)
1048 this_mv.row = this_row_offset;
1049 this_mv.col = this_col_offset;
1050 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1052 if (thissad < bestsad)
1063 if (best_site != last_site)
1065 best_mv->row += ss[best_site].mv.row;
1066 best_mv->col += ss[best_site].mv.col;
1067 best_address += ss[best_site].offset;
1068 last_site = best_site;
1070 else if (best_address == in_what)
1074 this_mv.row = best_mv->row << 3;
1075 this_mv.col = best_mv->col << 3;
1077 if (bestsad == INT_MAX)
1080 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1081 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1084 int vp8_diamond_search_sadx4
1094 vp8_variance_fn_ptr_t *fn_ptr,
1101 unsigned char *what = (*(b->base_src) + b->src);
1102 int what_stride = b->src_stride;
1103 unsigned char *in_what;
1104 int in_what_stride = d->pre_stride;
1105 unsigned char *best_address;
1110 int bestsad = INT_MAX;
1114 int ref_row = ref_mv->row >> 3;
1115 int ref_col = ref_mv->col >> 3;
1116 int this_row_offset;
1117 int this_col_offset;
1120 unsigned char *check_here;
1121 unsigned int thissad;
1123 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1125 fcenter_mv.row = center_mv->row >> 3;
1126 fcenter_mv.col = center_mv->col >> 3;
1129 best_mv->row = ref_row;
1130 best_mv->col = ref_col;
1132 // Work out the start point for the search
1133 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1134 best_address = in_what;
1136 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1137 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1138 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1140 // Check the starting position
1141 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1144 // search_param determines the length of the initial step and hence the number of iterations
1145 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1146 ss = &x->ss[search_param * x->searches_per_step];
1147 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1151 for (step = 0; step < tot_steps ; step++)
1155 // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1156 // checking 4 bounds for each points.
1157 all_in &= ((best_mv->row + ss[i].mv.row)> x->mv_row_min);
1158 all_in &= ((best_mv->row + ss[i+1].mv.row) < x->mv_row_max);
1159 all_in &= ((best_mv->col + ss[i+2].mv.col) > x->mv_col_min);
1160 all_in &= ((best_mv->col + ss[i+3].mv.col) < x->mv_col_max);
1164 unsigned int sad_array[4];
1166 for (j = 0 ; j < x->searches_per_step ; j += 4)
1168 unsigned char *block_offset[4];
1170 for (t = 0; t < 4; t++)
1171 block_offset[t] = ss[i+t].offset + best_address;
1173 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1175 for (t = 0; t < 4; t++, i++)
1177 if (sad_array[t] < bestsad)
1179 this_mv.row = best_mv->row + ss[i].mv.row;
1180 this_mv.col = best_mv->col + ss[i].mv.col;
1181 sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1183 if (sad_array[t] < bestsad)
1185 bestsad = sad_array[t];
1194 for (j = 0 ; j < x->searches_per_step ; j++)
1196 // Trap illegal vectors
1197 this_row_offset = best_mv->row + ss[i].mv.row;
1198 this_col_offset = best_mv->col + ss[i].mv.col;
1200 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1201 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1203 check_here = ss[i].offset + best_address;
1204 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1206 if (thissad < bestsad)
1208 this_mv.row = this_row_offset;
1209 this_mv.col = this_col_offset;
1210 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1212 if (thissad < bestsad)
1223 if (best_site != last_site)
1225 best_mv->row += ss[best_site].mv.row;
1226 best_mv->col += ss[best_site].mv.col;
1227 best_address += ss[best_site].offset;
1228 last_site = best_site;
1230 else if (best_address == in_what)
1234 this_mv.row = best_mv->row << 3;
1235 this_mv.col = best_mv->col << 3;
1237 if (bestsad == INT_MAX)
1240 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1241 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1244 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)
1246 unsigned char *what = (*(b->base_src) + b->src);
1247 int what_stride = b->src_stride;
1248 unsigned char *in_what;
1249 int in_what_stride = d->pre_stride;
1250 int mv_stride = d->pre_stride;
1251 unsigned char *bestaddress;
1252 MV *best_mv = &d->bmi.mv.as_mv;
1254 int bestsad = INT_MAX;
1257 unsigned char *check_here;
1260 int ref_row = ref_mv->row;
1261 int ref_col = ref_mv->col;
1263 int row_min = ref_row - distance;
1264 int row_max = ref_row + distance;
1265 int col_min = ref_col - distance;
1266 int col_max = ref_col + distance;
1268 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1270 fcenter_mv.row = center_mv->row >> 3;
1271 fcenter_mv.col = center_mv->col >> 3;
1273 // Work out the mid point for the search
1274 in_what = *(d->base_pre) + d->pre;
1275 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1277 best_mv->row = ref_row;
1278 best_mv->col = ref_col;
1280 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1281 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1282 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1284 // Baseline value at the centre
1286 //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));
1287 bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1290 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1291 if (col_min < x->mv_col_min)
1292 col_min = x->mv_col_min;
1294 if (col_max > x->mv_col_max)
1295 col_max = x->mv_col_max;
1297 if (row_min < x->mv_row_min)
1298 row_min = x->mv_row_min;
1300 if (row_max > x->mv_row_max)
1301 row_max = x->mv_row_max;
1303 for (r = row_min; r < row_max ; r++)
1306 check_here = r * mv_stride + in_what + col_min;
1308 for (c = col_min; c < col_max; c++)
1310 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1313 //thissad += (int)sqrt(mv_err_cost(&this_mv,ref_mv, mvcost,error_per_bit*14));
1314 //thissad += error_per_bit * mv_bits_sadcost[mv_bits(&this_mv, ref_mv, mvcost)];
1315 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit); //mv_bits(error_per_bit, &this_mv, ref_mv, mvsadcost);
1317 if (thissad < bestsad)
1322 bestaddress = check_here;
1329 this_mv.row = best_mv->row << 3;
1330 this_mv.col = best_mv->col << 3;
1332 if (bestsad < INT_MAX)
1333 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1334 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1339 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)
1341 unsigned char *what = (*(b->base_src) + b->src);
1342 int what_stride = b->src_stride;
1343 unsigned char *in_what;
1344 int in_what_stride = d->pre_stride;
1345 int mv_stride = d->pre_stride;
1346 unsigned char *bestaddress;
1347 MV *best_mv = &d->bmi.mv.as_mv;
1349 int bestsad = INT_MAX;
1352 unsigned char *check_here;
1353 unsigned int thissad;
1355 int ref_row = ref_mv->row;
1356 int ref_col = ref_mv->col;
1358 int row_min = ref_row - distance;
1359 int row_max = ref_row + distance;
1360 int col_min = ref_col - distance;
1361 int col_max = ref_col + distance;
1363 unsigned int sad_array[3];
1365 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1367 fcenter_mv.row = center_mv->row >> 3;
1368 fcenter_mv.col = center_mv->col >> 3;
1370 // Work out the mid point for the search
1371 in_what = *(d->base_pre) + d->pre;
1372 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1374 best_mv->row = ref_row;
1375 best_mv->col = ref_col;
1377 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1378 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1379 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1381 // Baseline value at the centre
1382 bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1385 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1386 if (col_min < x->mv_col_min)
1387 col_min = x->mv_col_min;
1389 if (col_max > x->mv_col_max)
1390 col_max = x->mv_col_max;
1392 if (row_min < x->mv_row_min)
1393 row_min = x->mv_row_min;
1395 if (row_max > x->mv_row_max)
1396 row_max = x->mv_row_max;
1398 for (r = row_min; r < row_max ; r++)
1401 check_here = r * mv_stride + in_what + col_min;
1404 while ((c + 2) < col_max)
1408 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1410 for (i = 0; i < 3; i++)
1412 thissad = sad_array[i];
1414 if (thissad < bestsad)
1417 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1419 if (thissad < bestsad)
1424 bestaddress = check_here;
1435 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1437 if (thissad < bestsad)
1440 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1442 if (thissad < bestsad)
1447 bestaddress = check_here;
1457 this_mv.row = best_mv->row << 3;
1458 this_mv.col = best_mv->col << 3;
1460 if (bestsad < INT_MAX)
1461 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1462 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1467 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)
1469 unsigned char *what = (*(b->base_src) + b->src);
1470 int what_stride = b->src_stride;
1471 unsigned char *in_what;
1472 int in_what_stride = d->pre_stride;
1473 int mv_stride = d->pre_stride;
1474 unsigned char *bestaddress;
1475 MV *best_mv = &d->bmi.mv.as_mv;
1477 int bestsad = INT_MAX;
1480 unsigned char *check_here;
1481 unsigned int thissad;
1483 int ref_row = ref_mv->row;
1484 int ref_col = ref_mv->col;
1486 int row_min = ref_row - distance;
1487 int row_max = ref_row + distance;
1488 int col_min = ref_col - distance;
1489 int col_max = ref_col + distance;
1491 DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1492 unsigned int sad_array[3];
1494 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1496 fcenter_mv.row = center_mv->row >> 3;
1497 fcenter_mv.col = center_mv->col >> 3;
1499 // Work out the mid point for the search
1500 in_what = *(d->base_pre) + d->pre;
1501 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1503 best_mv->row = ref_row;
1504 best_mv->col = ref_col;
1506 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1507 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1508 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1510 // Baseline value at the centre
1511 bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride, 0x7fffffff) + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, error_per_bit);
1514 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1515 if (col_min < x->mv_col_min)
1516 col_min = x->mv_col_min;
1518 if (col_max > x->mv_col_max)
1519 col_max = x->mv_col_max;
1521 if (row_min < x->mv_row_min)
1522 row_min = x->mv_row_min;
1524 if (row_max > x->mv_row_max)
1525 row_max = x->mv_row_max;
1527 for (r = row_min; r < row_max ; r++)
1530 check_here = r * mv_stride + in_what + col_min;
1533 while ((c + 7) < col_max)
1537 fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1539 for (i = 0; i < 8; i++)
1541 thissad = (unsigned int)sad_array8[i];
1543 if (thissad < bestsad)
1546 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1548 if (thissad < bestsad)
1553 bestaddress = check_here;
1562 while ((c + 2) < col_max)
1566 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1568 for (i = 0; i < 3; i++)
1570 thissad = sad_array[i];
1572 if (thissad < bestsad)
1575 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1577 if (thissad < bestsad)
1582 bestaddress = check_here;
1593 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1595 if (thissad < bestsad)
1598 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1600 if (thissad < bestsad)
1605 bestaddress = check_here;
1614 this_mv.row = best_mv->row << 3;
1615 this_mv.col = best_mv->col << 3;
1617 if (bestsad < INT_MAX)
1618 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1619 + mv_err_cost(&this_mv, center_mv, mvcost, error_per_bit);
1624 #ifdef ENTROPY_STATS
1625 void print_mode_context(void)
1627 FILE *f = fopen("modecont.c", "w");
1630 fprintf(f, "#include \"entropy.h\"\n");
1631 fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1634 for (j = 0; j < 6; j++)
1636 fprintf(f, " { // %d \n", j);
1639 for (i = 0; i < 4; i++)
1643 int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1646 count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1649 overal_prob = 256 * mv_mode_cts[i][0] / count;
1653 if (overal_prob == 0)
1657 count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1660 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1667 fprintf(f, "%5d, ", this_prob);
1668 //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1669 //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1672 fprintf(f, " },\n");
1679 /* MV ref count ENTROPY_STATS stats code */
1680 #ifdef ENTROPY_STATS
1681 void init_mv_ref_counts()
1683 vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1684 vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1687 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1691 ++mv_ref_ct [ct[0]] [0] [0];
1692 ++mv_mode_cts[0][0];
1696 ++mv_ref_ct [ct[0]] [0] [1];
1697 ++mv_mode_cts[0][1];
1701 ++mv_ref_ct [ct[1]] [1] [0];
1702 ++mv_mode_cts[1][0];
1706 ++mv_ref_ct [ct[1]] [1] [1];
1707 ++mv_mode_cts[1][1];
1711 ++mv_ref_ct [ct[2]] [2] [0];
1712 ++mv_mode_cts[2][0];
1716 ++mv_ref_ct [ct[2]] [2] [1];
1717 ++mv_mode_cts[2][1];
1721 ++mv_ref_ct [ct[3]] [3] [0];
1722 ++mv_mode_cts[3][0];
1726 ++mv_ref_ct [ct[3]] [3] [1];
1727 ++mv_mode_cts[3][1];
1734 #endif/* END MV ref count ENTROPY_STATS stats code */