PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/media/libvpx/vp8/encoder/mcomp.c

http://github.com/zpao/v8monkey
C | 2017 lines | 1587 code | 328 blank | 102 comment | 193 complexity | 9d17ed43bd5fc633103567d6787ee8aa MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  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. */
  10. #include "mcomp.h"
  11. #include "vpx_mem/vpx_mem.h"
  12. #include "vpx_ports/config.h"
  13. #include <stdio.h>
  14. #include <limits.h>
  15. #include <math.h>
  16. #include "vp8/common/findnearmv.h"
  17. #ifdef ENTROPY_STATS
  18. static int mv_ref_ct [31] [4] [2];
  19. static int mv_mode_cts [4] [2];
  20. #endif
  21. int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
  22. {
  23. // MV costing is based on the distribution of vectors in the previous frame and as such will tend to
  24. // over state the cost of vectors. In addition coding a new vector can have a knock on effect on the
  25. // cost of subsequent vectors and the quality of prediction from NEAR and NEAREST for subsequent blocks.
  26. // The "Weight" parameter allows, to a limited extent, for some account to be taken of these factors.
  27. return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] + mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1]) * Weight) >> 7;
  28. }
  29. static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int error_per_bit)
  30. {
  31. return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] +
  32. mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1])
  33. * error_per_bit + 128) >> 8;
  34. }
  35. static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
  36. {
  37. /* Calculate sad error cost on full pixel basis. */
  38. return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] +
  39. mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)])
  40. * error_per_bit + 128) >> 8;
  41. }
  42. void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
  43. {
  44. int Len;
  45. int search_site_count = 0;
  46. // Generate offsets for 4 search sites per step.
  47. Len = MAX_FIRST_STEP;
  48. x->ss[search_site_count].mv.col = 0;
  49. x->ss[search_site_count].mv.row = 0;
  50. x->ss[search_site_count].offset = 0;
  51. search_site_count++;
  52. while (Len > 0)
  53. {
  54. // Compute offsets for search sites.
  55. x->ss[search_site_count].mv.col = 0;
  56. x->ss[search_site_count].mv.row = -Len;
  57. x->ss[search_site_count].offset = -Len * stride;
  58. search_site_count++;
  59. // Compute offsets for search sites.
  60. x->ss[search_site_count].mv.col = 0;
  61. x->ss[search_site_count].mv.row = Len;
  62. x->ss[search_site_count].offset = Len * stride;
  63. search_site_count++;
  64. // Compute offsets for search sites.
  65. x->ss[search_site_count].mv.col = -Len;
  66. x->ss[search_site_count].mv.row = 0;
  67. x->ss[search_site_count].offset = -Len;
  68. search_site_count++;
  69. // Compute offsets for search sites.
  70. x->ss[search_site_count].mv.col = Len;
  71. x->ss[search_site_count].mv.row = 0;
  72. x->ss[search_site_count].offset = Len;
  73. search_site_count++;
  74. // Contract.
  75. Len /= 2;
  76. }
  77. x->ss_count = search_site_count;
  78. x->searches_per_step = 4;
  79. }
  80. void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
  81. {
  82. int Len;
  83. int search_site_count = 0;
  84. // Generate offsets for 8 search sites per step.
  85. Len = MAX_FIRST_STEP;
  86. x->ss[search_site_count].mv.col = 0;
  87. x->ss[search_site_count].mv.row = 0;
  88. x->ss[search_site_count].offset = 0;
  89. search_site_count++;
  90. while (Len > 0)
  91. {
  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++;
  97. // Compute offsets for search sites.
  98. x->ss[search_site_count].mv.col = 0;
  99. x->ss[search_site_count].mv.row = Len;
  100. x->ss[search_site_count].offset = Len * stride;
  101. search_site_count++;
  102. // Compute offsets for search sites.
  103. x->ss[search_site_count].mv.col = -Len;
  104. x->ss[search_site_count].mv.row = 0;
  105. x->ss[search_site_count].offset = -Len;
  106. search_site_count++;
  107. // Compute offsets for search sites.
  108. x->ss[search_site_count].mv.col = Len;
  109. x->ss[search_site_count].mv.row = 0;
  110. x->ss[search_site_count].offset = Len;
  111. search_site_count++;
  112. // Compute offsets for search sites.
  113. x->ss[search_site_count].mv.col = -Len;
  114. x->ss[search_site_count].mv.row = -Len;
  115. x->ss[search_site_count].offset = -Len * stride - Len;
  116. search_site_count++;
  117. // Compute offsets for search sites.
  118. x->ss[search_site_count].mv.col = Len;
  119. x->ss[search_site_count].mv.row = -Len;
  120. x->ss[search_site_count].offset = -Len * stride + Len;
  121. search_site_count++;
  122. // Compute offsets for search sites.
  123. x->ss[search_site_count].mv.col = -Len;
  124. x->ss[search_site_count].mv.row = Len;
  125. x->ss[search_site_count].offset = Len * stride - Len;
  126. search_site_count++;
  127. // Compute offsets for search sites.
  128. x->ss[search_site_count].mv.col = Len;
  129. x->ss[search_site_count].mv.row = Len;
  130. x->ss[search_site_count].offset = Len * stride + Len;
  131. search_site_count++;
  132. // Contract.
  133. Len /= 2;
  134. }
  135. x->ss_count = search_site_count;
  136. x->searches_per_step = 8;
  137. }
  138. /*
  139. * To avoid the penalty for crossing cache-line read, preload the reference
  140. * area in a small buffer, which is aligned to make sure there won't be crossing
  141. * cache-line read while reading from this buffer. This reduced the cpu
  142. * cycles spent on reading ref data in sub-pixel filter functions.
  143. * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
  144. * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
  145. * could reduce the area.
  146. */
  147. #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)
  148. #define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset))) // pointer to predictor base of a motionvector
  149. #define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
  150. #define DIST(r,c) vfp->svf( PRE(r,c), y_stride, SP(c),SP(r), z,b->src_stride,&sse) // returns subpixel variance error function.
  151. #define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
  152. #define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
  153. #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
  154. #define MIN(x,y) (((x)<(y))?(x):(y))
  155. #define MAX(x,y) (((x)>(y))?(x):(y))
  156. int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
  157. int_mv *bestmv, int_mv *ref_mv,
  158. int error_per_bit,
  159. const vp8_variance_fn_ptr_t *vfp,
  160. int *mvcost[2], int *distortion,
  161. unsigned int *sse1)
  162. {
  163. unsigned char *z = (*(b->base_src) + b->src);
  164. int rr = ref_mv->as_mv.row >> 1, rc = ref_mv->as_mv.col >> 1;
  165. int br = bestmv->as_mv.row << 2, bc = bestmv->as_mv.col << 2;
  166. int tr = br, tc = bc;
  167. unsigned int besterr = INT_MAX;
  168. unsigned int left, right, up, down, diag;
  169. unsigned int sse;
  170. unsigned int whichdir;
  171. unsigned int halfiters = 4;
  172. unsigned int quarteriters = 4;
  173. int thismse;
  174. int minc = MAX(x->mv_col_min << 2, (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1));
  175. int maxc = MIN(x->mv_col_max << 2, (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1));
  176. int minr = MAX(x->mv_row_min << 2, (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1));
  177. int maxr = MIN(x->mv_row_max << 2, (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1));
  178. int y_stride;
  179. int offset;
  180. #if ARCH_X86 || ARCH_X86_64
  181. MACROBLOCKD *xd = &x->e_mbd;
  182. unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  183. unsigned char *y;
  184. int buf_r1, buf_r2, buf_c1, buf_c2;
  185. // Clamping to avoid out-of-range data access
  186. buf_r1 = ((bestmv->as_mv.row - 3) < x->mv_row_min)?(bestmv->as_mv.row - x->mv_row_min):3;
  187. buf_r2 = ((bestmv->as_mv.row + 3) > x->mv_row_max)?(x->mv_row_max - bestmv->as_mv.row):3;
  188. buf_c1 = ((bestmv->as_mv.col - 3) < x->mv_col_min)?(bestmv->as_mv.col - x->mv_col_min):3;
  189. buf_c2 = ((bestmv->as_mv.col + 3) > x->mv_col_max)?(x->mv_col_max - bestmv->as_mv.col):3;
  190. y_stride = 32;
  191. /* Copy to intermediate buffer before searching. */
  192. vfp->copymem(y0 - buf_c1 - d->pre_stride*buf_r1, d->pre_stride, xd->y_buf, y_stride, 16+buf_r1+buf_r2);
  193. y = xd->y_buf + y_stride*buf_r1 +buf_c1;
  194. #else
  195. unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  196. y_stride = d->pre_stride;
  197. #endif
  198. offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col;
  199. // central mv
  200. bestmv->as_mv.row <<= 3;
  201. bestmv->as_mv.col <<= 3;
  202. // calculate central point error
  203. besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
  204. *distortion = besterr;
  205. besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
  206. // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
  207. while (--halfiters)
  208. {
  209. // 1/2 pel
  210. CHECK_BETTER(left, tr, tc - 2);
  211. CHECK_BETTER(right, tr, tc + 2);
  212. CHECK_BETTER(up, tr - 2, tc);
  213. CHECK_BETTER(down, tr + 2, tc);
  214. whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
  215. switch (whichdir)
  216. {
  217. case 0:
  218. CHECK_BETTER(diag, tr - 2, tc - 2);
  219. break;
  220. case 1:
  221. CHECK_BETTER(diag, tr - 2, tc + 2);
  222. break;
  223. case 2:
  224. CHECK_BETTER(diag, tr + 2, tc - 2);
  225. break;
  226. case 3:
  227. CHECK_BETTER(diag, tr + 2, tc + 2);
  228. break;
  229. }
  230. // no reason to check the same one again.
  231. if (tr == br && tc == bc)
  232. break;
  233. tr = br;
  234. tc = bc;
  235. }
  236. // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
  237. // 1/4 pel
  238. while (--quarteriters)
  239. {
  240. CHECK_BETTER(left, tr, tc - 1);
  241. CHECK_BETTER(right, tr, tc + 1);
  242. CHECK_BETTER(up, tr - 1, tc);
  243. CHECK_BETTER(down, tr + 1, tc);
  244. whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
  245. switch (whichdir)
  246. {
  247. case 0:
  248. CHECK_BETTER(diag, tr - 1, tc - 1);
  249. break;
  250. case 1:
  251. CHECK_BETTER(diag, tr - 1, tc + 1);
  252. break;
  253. case 2:
  254. CHECK_BETTER(diag, tr + 1, tc - 1);
  255. break;
  256. case 3:
  257. CHECK_BETTER(diag, tr + 1, tc + 1);
  258. break;
  259. }
  260. // no reason to check the same one again.
  261. if (tr == br && tc == bc)
  262. break;
  263. tr = br;
  264. tc = bc;
  265. }
  266. bestmv->as_mv.row = br << 1;
  267. bestmv->as_mv.col = bc << 1;
  268. if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL<<3)) ||
  269. (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL<<3)))
  270. return INT_MAX;
  271. return besterr;
  272. }
  273. #undef MVC
  274. #undef PRE
  275. #undef SP
  276. #undef DIST
  277. #undef IFMVCV
  278. #undef ERR
  279. #undef CHECK_BETTER
  280. #undef MIN
  281. #undef MAX
  282. int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
  283. int_mv *bestmv, int_mv *ref_mv,
  284. int error_per_bit,
  285. const vp8_variance_fn_ptr_t *vfp,
  286. int *mvcost[2], int *distortion,
  287. unsigned int *sse1)
  288. {
  289. int bestmse = INT_MAX;
  290. int_mv startmv;
  291. int_mv this_mv;
  292. unsigned char *z = (*(b->base_src) + b->src);
  293. int left, right, up, down, diag;
  294. unsigned int sse;
  295. int whichdir ;
  296. int thismse;
  297. int y_stride;
  298. #if ARCH_X86 || ARCH_X86_64
  299. MACROBLOCKD *xd = &x->e_mbd;
  300. unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  301. unsigned char *y;
  302. y_stride = 32;
  303. /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
  304. vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
  305. y = xd->y_buf + y_stride + 1;
  306. #else
  307. unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  308. y_stride = d->pre_stride;
  309. #endif
  310. // central mv
  311. bestmv->as_mv.row <<= 3;
  312. bestmv->as_mv.col <<= 3;
  313. startmv = *bestmv;
  314. // calculate central point error
  315. bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
  316. *distortion = bestmse;
  317. bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
  318. // go left then right and check error
  319. this_mv.as_mv.row = startmv.as_mv.row;
  320. this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
  321. thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
  322. left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  323. if (left < bestmse)
  324. {
  325. *bestmv = this_mv;
  326. bestmse = left;
  327. *distortion = thismse;
  328. *sse1 = sse;
  329. }
  330. this_mv.as_mv.col += 8;
  331. thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
  332. right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  333. if (right < bestmse)
  334. {
  335. *bestmv = this_mv;
  336. bestmse = right;
  337. *distortion = thismse;
  338. *sse1 = sse;
  339. }
  340. // go up then down and check error
  341. this_mv.as_mv.col = startmv.as_mv.col;
  342. this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
  343. thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
  344. up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  345. if (up < bestmse)
  346. {
  347. *bestmv = this_mv;
  348. bestmse = up;
  349. *distortion = thismse;
  350. *sse1 = sse;
  351. }
  352. this_mv.as_mv.row += 8;
  353. thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
  354. down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  355. if (down < bestmse)
  356. {
  357. *bestmv = this_mv;
  358. bestmse = down;
  359. *distortion = thismse;
  360. *sse1 = sse;
  361. }
  362. // now check 1 more diagonal
  363. whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
  364. //for(whichdir =0;whichdir<4;whichdir++)
  365. //{
  366. this_mv = startmv;
  367. switch (whichdir)
  368. {
  369. case 0:
  370. this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
  371. this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
  372. thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
  373. break;
  374. case 1:
  375. this_mv.as_mv.col += 4;
  376. this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
  377. thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
  378. break;
  379. case 2:
  380. this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
  381. this_mv.as_mv.row += 4;
  382. thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
  383. break;
  384. case 3:
  385. default:
  386. this_mv.as_mv.col += 4;
  387. this_mv.as_mv.row += 4;
  388. thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
  389. break;
  390. }
  391. diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  392. if (diag < bestmse)
  393. {
  394. *bestmv = this_mv;
  395. bestmse = diag;
  396. *distortion = thismse;
  397. *sse1 = sse;
  398. }
  399. // }
  400. // time to check quarter pels.
  401. if (bestmv->as_mv.row < startmv.as_mv.row)
  402. y -= y_stride;
  403. if (bestmv->as_mv.col < startmv.as_mv.col)
  404. y--;
  405. startmv = *bestmv;
  406. // go left then right and check error
  407. this_mv.as_mv.row = startmv.as_mv.row;
  408. if (startmv.as_mv.col & 7)
  409. {
  410. this_mv.as_mv.col = startmv.as_mv.col - 2;
  411. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  412. }
  413. else
  414. {
  415. this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
  416. thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  417. }
  418. left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  419. if (left < bestmse)
  420. {
  421. *bestmv = this_mv;
  422. bestmse = left;
  423. *distortion = thismse;
  424. *sse1 = sse;
  425. }
  426. this_mv.as_mv.col += 4;
  427. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  428. right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  429. if (right < bestmse)
  430. {
  431. *bestmv = this_mv;
  432. bestmse = right;
  433. *distortion = thismse;
  434. *sse1 = sse;
  435. }
  436. // go up then down and check error
  437. this_mv.as_mv.col = startmv.as_mv.col;
  438. if (startmv.as_mv.row & 7)
  439. {
  440. this_mv.as_mv.row = startmv.as_mv.row - 2;
  441. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  442. }
  443. else
  444. {
  445. this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
  446. thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
  447. }
  448. up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  449. if (up < bestmse)
  450. {
  451. *bestmv = this_mv;
  452. bestmse = up;
  453. *distortion = thismse;
  454. *sse1 = sse;
  455. }
  456. this_mv.as_mv.row += 4;
  457. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  458. down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  459. if (down < bestmse)
  460. {
  461. *bestmv = this_mv;
  462. bestmse = down;
  463. *distortion = thismse;
  464. *sse1 = sse;
  465. }
  466. // now check 1 more diagonal
  467. whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
  468. // for(whichdir=0;whichdir<4;whichdir++)
  469. // {
  470. this_mv = startmv;
  471. switch (whichdir)
  472. {
  473. case 0:
  474. if (startmv.as_mv.row & 7)
  475. {
  476. this_mv.as_mv.row -= 2;
  477. if (startmv.as_mv.col & 7)
  478. {
  479. this_mv.as_mv.col -= 2;
  480. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  481. }
  482. else
  483. {
  484. this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
  485. thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
  486. }
  487. }
  488. else
  489. {
  490. this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
  491. if (startmv.as_mv.col & 7)
  492. {
  493. this_mv.as_mv.col -= 2;
  494. thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
  495. }
  496. else
  497. {
  498. this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
  499. thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse);
  500. }
  501. }
  502. break;
  503. case 1:
  504. this_mv.as_mv.col += 2;
  505. if (startmv.as_mv.row & 7)
  506. {
  507. this_mv.as_mv.row -= 2;
  508. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  509. }
  510. else
  511. {
  512. this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
  513. thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
  514. }
  515. break;
  516. case 2:
  517. this_mv.as_mv.row += 2;
  518. if (startmv.as_mv.col & 7)
  519. {
  520. this_mv.as_mv.col -= 2;
  521. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  522. }
  523. else
  524. {
  525. this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
  526. thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  527. }
  528. break;
  529. case 3:
  530. this_mv.as_mv.col += 2;
  531. this_mv.as_mv.row += 2;
  532. thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
  533. break;
  534. }
  535. diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  536. if (diag < bestmse)
  537. {
  538. *bestmv = this_mv;
  539. bestmse = diag;
  540. *distortion = thismse;
  541. *sse1 = sse;
  542. }
  543. return bestmse;
  544. }
  545. int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
  546. int_mv *bestmv, int_mv *ref_mv,
  547. int error_per_bit,
  548. const vp8_variance_fn_ptr_t *vfp,
  549. int *mvcost[2], int *distortion,
  550. unsigned int *sse1)
  551. {
  552. int bestmse = INT_MAX;
  553. int_mv startmv;
  554. int_mv this_mv;
  555. unsigned char *z = (*(b->base_src) + b->src);
  556. int left, right, up, down, diag;
  557. unsigned int sse;
  558. int thismse;
  559. int y_stride;
  560. #if ARCH_X86 || ARCH_X86_64
  561. MACROBLOCKD *xd = &x->e_mbd;
  562. unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  563. unsigned char *y;
  564. y_stride = 32;
  565. /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
  566. vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
  567. y = xd->y_buf + y_stride + 1;
  568. #else
  569. unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
  570. y_stride = d->pre_stride;
  571. #endif
  572. // central mv
  573. bestmv->as_mv.row <<= 3;
  574. bestmv->as_mv.col <<= 3;
  575. startmv = *bestmv;
  576. // calculate central point error
  577. bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
  578. *distortion = bestmse;
  579. bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
  580. // go left then right and check error
  581. this_mv.as_mv.row = startmv.as_mv.row;
  582. this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
  583. thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
  584. left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  585. if (left < bestmse)
  586. {
  587. *bestmv = this_mv;
  588. bestmse = left;
  589. *distortion = thismse;
  590. *sse1 = sse;
  591. }
  592. this_mv.as_mv.col += 8;
  593. thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
  594. right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  595. if (right < bestmse)
  596. {
  597. *bestmv = this_mv;
  598. bestmse = right;
  599. *distortion = thismse;
  600. *sse1 = sse;
  601. }
  602. // go up then down and check error
  603. this_mv.as_mv.col = startmv.as_mv.col;
  604. this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
  605. thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
  606. up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  607. if (up < bestmse)
  608. {
  609. *bestmv = this_mv;
  610. bestmse = up;
  611. *distortion = thismse;
  612. *sse1 = sse;
  613. }
  614. this_mv.as_mv.row += 8;
  615. thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
  616. down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  617. if (down < bestmse)
  618. {
  619. *bestmv = this_mv;
  620. bestmse = down;
  621. *distortion = thismse;
  622. *sse1 = sse;
  623. }
  624. // somewhat strangely not doing all the diagonals for half pel is slower than doing them.
  625. #if 0
  626. // now check 1 more diagonal -
  627. whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
  628. this_mv = startmv;
  629. switch (whichdir)
  630. {
  631. case 0:
  632. this_mv.col = (this_mv.col - 8) | 4;
  633. this_mv.row = (this_mv.row - 8) | 4;
  634. diag = vfp->svf(y - 1 - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
  635. break;
  636. case 1:
  637. this_mv.col += 4;
  638. this_mv.row = (this_mv.row - 8) | 4;
  639. diag = vfp->svf(y - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
  640. break;
  641. case 2:
  642. this_mv.col = (this_mv.col - 8) | 4;
  643. this_mv.row += 4;
  644. diag = vfp->svf(y - 1, y_stride, 4, 4, z, b->src_stride, &sse);
  645. break;
  646. case 3:
  647. this_mv.col += 4;
  648. this_mv.row += 4;
  649. diag = vfp->svf(y, y_stride, 4, 4, z, b->src_stride, &sse);
  650. break;
  651. }
  652. diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  653. if (diag < bestmse)
  654. {
  655. *bestmv = this_mv;
  656. bestmse = diag;
  657. }
  658. #else
  659. this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
  660. this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
  661. thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
  662. diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  663. if (diag < bestmse)
  664. {
  665. *bestmv = this_mv;
  666. bestmse = diag;
  667. *distortion = thismse;
  668. *sse1 = sse;
  669. }
  670. this_mv.as_mv.col += 8;
  671. thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
  672. diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  673. if (diag < bestmse)
  674. {
  675. *bestmv = this_mv;
  676. bestmse = diag;
  677. *distortion = thismse;
  678. *sse1 = sse;
  679. }
  680. this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
  681. this_mv.as_mv.row = startmv.as_mv.row + 4;
  682. thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
  683. diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  684. if (diag < bestmse)
  685. {
  686. *bestmv = this_mv;
  687. bestmse = diag;
  688. *distortion = thismse;
  689. *sse1 = sse;
  690. }
  691. this_mv.as_mv.col += 8;
  692. thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
  693. diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
  694. if (diag < bestmse)
  695. {
  696. *bestmv = this_mv;
  697. bestmse = diag;
  698. *distortion = thismse;
  699. *sse1 = sse;
  700. }
  701. #endif
  702. return bestmse;
  703. }
  704. #define CHECK_BOUNDS(range) \
  705. {\
  706. all_in = 1;\
  707. all_in &= ((br-range) >= x->mv_row_min);\
  708. all_in &= ((br+range) <= x->mv_row_max);\
  709. all_in &= ((bc-range) >= x->mv_col_min);\
  710. all_in &= ((bc+range) <= x->mv_col_max);\
  711. }
  712. #define CHECK_POINT \
  713. {\
  714. if (this_mv.as_mv.col < x->mv_col_min) continue;\
  715. if (this_mv.as_mv.col > x->mv_col_max) continue;\
  716. if (this_mv.as_mv.row < x->mv_row_min) continue;\
  717. if (this_mv.as_mv.row > x->mv_row_max) continue;\
  718. }
  719. #define CHECK_BETTER \
  720. {\
  721. if (thissad < bestsad)\
  722. {\
  723. thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
  724. if (thissad < bestsad)\
  725. {\
  726. bestsad = thissad;\
  727. best_site = i;\
  728. }\
  729. }\
  730. }
  731. static const MV next_chkpts[6][3] =
  732. {
  733. {{ -2, 0}, { -1, -2}, {1, -2}},
  734. {{ -1, -2}, {1, -2}, {2, 0}},
  735. {{1, -2}, {2, 0}, {1, 2}},
  736. {{2, 0}, {1, 2}, { -1, 2}},
  737. {{1, 2}, { -1, 2}, { -2, 0}},
  738. {{ -1, 2}, { -2, 0}, { -1, -2}}
  739. };
  740. int vp8_hex_search
  741. (
  742. MACROBLOCK *x,
  743. BLOCK *b,
  744. BLOCKD *d,
  745. int_mv *ref_mv,
  746. int_mv *best_mv,
  747. int search_param,
  748. int sad_per_bit,
  749. const vp8_variance_fn_ptr_t *vfp,
  750. int *mvsadcost[2],
  751. int *mvcost[2],
  752. int_mv *center_mv
  753. )
  754. {
  755. MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
  756. MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
  757. int i, j;
  758. unsigned char *what = (*(b->base_src) + b->src);
  759. int what_stride = b->src_stride;
  760. int in_what_stride = d->pre_stride;
  761. int br, bc;
  762. int_mv this_mv;
  763. unsigned int bestsad = 0x7fffffff;
  764. unsigned int thissad;
  765. unsigned char *base_offset;
  766. unsigned char *this_offset;
  767. int k = -1;
  768. int all_in;
  769. int best_site = -1;
  770. int_mv fcenter_mv;
  771. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  772. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  773. // adjust ref_mv to make sure it is within MV range
  774. vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
  775. br = ref_mv->as_mv.row;
  776. bc = ref_mv->as_mv.col;
  777. // Work out the start point for the search
  778. base_offset = (unsigned char *)(*(d->base_pre) + d->pre);
  779. this_offset = base_offset + (br * (d->pre_stride)) + bc;
  780. this_mv.as_mv.row = br;
  781. this_mv.as_mv.col = bc;
  782. bestsad = vfp->sdf( what, what_stride, this_offset,
  783. in_what_stride, 0x7fffffff)
  784. + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
  785. // hex search
  786. //j=0
  787. CHECK_BOUNDS(2)
  788. if(all_in)
  789. {
  790. for (i = 0; i < 6; i++)
  791. {
  792. this_mv.as_mv.row = br + hex[i].row;
  793. this_mv.as_mv.col = bc + hex[i].col;
  794. this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
  795. thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
  796. CHECK_BETTER
  797. }
  798. }else
  799. {
  800. for (i = 0; i < 6; i++)
  801. {
  802. this_mv.as_mv.row = br + hex[i].row;
  803. this_mv.as_mv.col = bc + hex[i].col;
  804. CHECK_POINT
  805. this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
  806. thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
  807. CHECK_BETTER
  808. }
  809. }
  810. if (best_site == -1)
  811. goto cal_neighbors;
  812. else
  813. {
  814. br += hex[best_site].row;
  815. bc += hex[best_site].col;
  816. k = best_site;
  817. }
  818. for (j = 1; j < 127; j++)
  819. {
  820. best_site = -1;
  821. CHECK_BOUNDS(2)
  822. if(all_in)
  823. {
  824. for (i = 0; i < 3; i++)
  825. {
  826. this_mv.as_mv.row = br + next_chkpts[k][i].row;
  827. this_mv.as_mv.col = bc + next_chkpts[k][i].col;
  828. this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
  829. thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
  830. CHECK_BETTER
  831. }
  832. }else
  833. {
  834. for (i = 0; i < 3; i++)
  835. {
  836. this_mv.as_mv.row = br + next_chkpts[k][i].row;
  837. this_mv.as_mv.col = bc + next_chkpts[k][i].col;
  838. CHECK_POINT
  839. this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
  840. thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
  841. CHECK_BETTER
  842. }
  843. }
  844. if (best_site == -1)
  845. break;
  846. else
  847. {
  848. br += next_chkpts[k][best_site].row;
  849. bc += next_chkpts[k][best_site].col;
  850. k += 5 + best_site;
  851. if (k >= 12) k -= 12;
  852. else if (k >= 6) k -= 6;
  853. }
  854. }
  855. // check 4 1-away neighbors
  856. cal_neighbors:
  857. for (j = 0; j < 32; j++)
  858. {
  859. best_site = -1;
  860. CHECK_BOUNDS(1)
  861. if(all_in)
  862. {
  863. for (i = 0; i < 4; i++)
  864. {
  865. this_mv.as_mv.row = br + neighbors[i].row;
  866. this_mv.as_mv.col = bc + neighbors[i].col;
  867. this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
  868. thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
  869. CHECK_BETTER
  870. }
  871. }else
  872. {
  873. for (i = 0; i < 4; i++)
  874. {
  875. this_mv.as_mv.row = br + neighbors[i].row;
  876. this_mv.as_mv.col = bc + neighbors[i].col;
  877. CHECK_POINT
  878. this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
  879. thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
  880. CHECK_BETTER
  881. }
  882. }
  883. if (best_site == -1)
  884. break;
  885. else
  886. {
  887. br += neighbors[best_site].row;
  888. bc += neighbors[best_site].col;
  889. }
  890. }
  891. best_mv->as_mv.row = br;
  892. best_mv->as_mv.col = bc;
  893. return bestsad;
  894. }
  895. #undef CHECK_BOUNDS
  896. #undef CHECK_POINT
  897. #undef CHECK_BETTER
  898. int vp8_diamond_search_sad
  899. (
  900. MACROBLOCK *x,
  901. BLOCK *b,
  902. BLOCKD *d,
  903. int_mv *ref_mv,
  904. int_mv *best_mv,
  905. int search_param,
  906. int sad_per_bit,
  907. int *num00,
  908. vp8_variance_fn_ptr_t *fn_ptr,
  909. int *mvcost[2],
  910. int_mv *center_mv
  911. )
  912. {
  913. int i, j, step;
  914. unsigned char *what = (*(b->base_src) + b->src);
  915. int what_stride = b->src_stride;
  916. unsigned char *in_what;
  917. int in_what_stride = d->pre_stride;
  918. unsigned char *best_address;
  919. int tot_steps;
  920. int_mv this_mv;
  921. int bestsad = INT_MAX;
  922. int best_site = 0;
  923. int last_site = 0;
  924. int ref_row;
  925. int ref_col;
  926. int this_row_offset;
  927. int this_col_offset;
  928. search_site *ss;
  929. unsigned char *check_here;
  930. int thissad;
  931. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  932. int_mv fcenter_mv;
  933. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  934. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  935. vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
  936. ref_row = ref_mv->as_mv.row;
  937. ref_col = ref_mv->as_mv.col;
  938. *num00 = 0;
  939. best_mv->as_mv.row = ref_row;
  940. best_mv->as_mv.col = ref_col;
  941. // Work out the start point for the search
  942. in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
  943. best_address = in_what;
  944. // Check the starting position
  945. bestsad = fn_ptr->sdf(what, what_stride, in_what,
  946. in_what_stride, 0x7fffffff)
  947. + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
  948. // search_param determines the length of the initial step and hence the number of iterations
  949. // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
  950. ss = &x->ss[search_param * x->searches_per_step];
  951. tot_steps = (x->ss_count / x->searches_per_step) - search_param;
  952. i = 1;
  953. for (step = 0; step < tot_steps ; step++)
  954. {
  955. for (j = 0 ; j < x->searches_per_step ; j++)
  956. {
  957. // Trap illegal vectors
  958. this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
  959. this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
  960. if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
  961. (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
  962. {
  963. check_here = ss[i].offset + best_address;
  964. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  965. if (thissad < bestsad)
  966. {
  967. this_mv.as_mv.row = this_row_offset;
  968. this_mv.as_mv.col = this_col_offset;
  969. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  970. mvsadcost, sad_per_bit);
  971. if (thissad < bestsad)
  972. {
  973. bestsad = thissad;
  974. best_site = i;
  975. }
  976. }
  977. }
  978. i++;
  979. }
  980. if (best_site != last_site)
  981. {
  982. best_mv->as_mv.row += ss[best_site].mv.row;
  983. best_mv->as_mv.col += ss[best_site].mv.col;
  984. best_address += ss[best_site].offset;
  985. last_site = best_site;
  986. }
  987. else if (best_address == in_what)
  988. (*num00)++;
  989. }
  990. this_mv.as_mv.row = best_mv->as_mv.row << 3;
  991. this_mv.as_mv.col = best_mv->as_mv.col << 3;
  992. if (bestsad == INT_MAX)
  993. return INT_MAX;
  994. return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
  995. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  996. }
  997. int vp8_diamond_search_sadx4
  998. (
  999. MACROBLOCK *x,
  1000. BLOCK *b,
  1001. BLOCKD *d,
  1002. int_mv *ref_mv,
  1003. int_mv *best_mv,
  1004. int search_param,
  1005. int sad_per_bit,
  1006. int *num00,
  1007. vp8_variance_fn_ptr_t *fn_ptr,
  1008. int *mvcost[2],
  1009. int_mv *center_mv
  1010. )
  1011. {
  1012. int i, j, step;
  1013. unsigned char *what = (*(b->base_src) + b->src);
  1014. int what_stride = b->src_stride;
  1015. unsigned char *in_what;
  1016. int in_what_stride = d->pre_stride;
  1017. unsigned char *best_address;
  1018. int tot_steps;
  1019. int_mv this_mv;
  1020. int bestsad = INT_MAX;
  1021. int best_site = 0;
  1022. int last_site = 0;
  1023. int ref_row;
  1024. int ref_col;
  1025. int this_row_offset;
  1026. int this_col_offset;
  1027. search_site *ss;
  1028. unsigned char *check_here;
  1029. unsigned int thissad;
  1030. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  1031. int_mv fcenter_mv;
  1032. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  1033. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  1034. vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
  1035. ref_row = ref_mv->as_mv.row;
  1036. ref_col = ref_mv->as_mv.col;
  1037. *num00 = 0;
  1038. best_mv->as_mv.row = ref_row;
  1039. best_mv->as_mv.col = ref_col;
  1040. // Work out the start point for the search
  1041. in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
  1042. best_address = in_what;
  1043. // Check the starting position
  1044. bestsad = fn_ptr->sdf(what, what_stride,
  1045. in_what, in_what_stride, 0x7fffffff)
  1046. + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
  1047. // search_param determines the length of the initial step and hence the number of iterations
  1048. // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
  1049. ss = &x->ss[search_param * x->searches_per_step];
  1050. tot_steps = (x->ss_count / x->searches_per_step) - search_param;
  1051. i = 1;
  1052. for (step = 0; step < tot_steps ; step++)
  1053. {
  1054. int all_in = 1, t;
  1055. // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
  1056. // checking 4 bounds for each points.
  1057. all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min);
  1058. all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max);
  1059. all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min);
  1060. all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max);
  1061. if (all_in)
  1062. {
  1063. unsigned int sad_array[4];
  1064. for (j = 0 ; j < x->searches_per_step ; j += 4)
  1065. {
  1066. unsigned char *block_offset[4];
  1067. for (t = 0; t < 4; t++)
  1068. block_offset[t] = ss[i+t].offset + best_address;
  1069. fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
  1070. for (t = 0; t < 4; t++, i++)
  1071. {
  1072. if (sad_array[t] < bestsad)
  1073. {
  1074. this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
  1075. this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
  1076. sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
  1077. mvsadcost, sad_per_bit);
  1078. if (sad_array[t] < bestsad)
  1079. {
  1080. bestsad = sad_array[t];
  1081. best_site = i;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. }
  1087. else
  1088. {
  1089. for (j = 0 ; j < x->searches_per_step ; j++)
  1090. {
  1091. // Trap illegal vectors
  1092. this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
  1093. this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
  1094. if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
  1095. (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
  1096. {
  1097. check_here = ss[i].offset + best_address;
  1098. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  1099. if (thissad < bestsad)
  1100. {
  1101. this_mv.as_mv.row = this_row_offset;
  1102. this_mv.as_mv.col = this_col_offset;
  1103. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1104. mvsadcost, sad_per_bit);
  1105. if (thissad < bestsad)
  1106. {
  1107. bestsad = thissad;
  1108. best_site = i;
  1109. }
  1110. }
  1111. }
  1112. i++;
  1113. }
  1114. }
  1115. if (best_site != last_site)
  1116. {
  1117. best_mv->as_mv.row += ss[best_site].mv.row;
  1118. best_mv->as_mv.col += ss[best_site].mv.col;
  1119. best_address += ss[best_site].offset;
  1120. last_site = best_site;
  1121. }
  1122. else if (best_address == in_what)
  1123. (*num00)++;
  1124. }
  1125. this_mv.as_mv.row = best_mv->as_mv.row << 3;
  1126. this_mv.as_mv.col = best_mv->as_mv.col << 3;
  1127. if (bestsad == INT_MAX)
  1128. return INT_MAX;
  1129. return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
  1130. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  1131. }
  1132. int vp8_full_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
  1133. int sad_per_bit, int distance,
  1134. vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
  1135. int_mv *center_mv)
  1136. {
  1137. unsigned char *what = (*(b->base_src) + b->src);
  1138. int what_stride = b->src_stride;
  1139. unsigned char *in_what;
  1140. int in_what_stride = d->pre_stride;
  1141. int mv_stride = d->pre_stride;
  1142. unsigned char *bestaddress;
  1143. int_mv *best_mv = &d->bmi.mv;
  1144. int_mv this_mv;
  1145. int bestsad = INT_MAX;
  1146. int r, c;
  1147. unsigned char *check_here;
  1148. int thissad;
  1149. int ref_row = ref_mv->as_mv.row;
  1150. int ref_col = ref_mv->as_mv.col;
  1151. int row_min = ref_row - distance;
  1152. int row_max = ref_row + distance;
  1153. int col_min = ref_col - distance;
  1154. int col_max = ref_col + distance;
  1155. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  1156. int_mv fcenter_mv;
  1157. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  1158. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  1159. // Work out the mid point for the search
  1160. in_what = *(d->base_pre) + d->pre;
  1161. bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
  1162. best_mv->as_mv.row = ref_row;
  1163. best_mv->as_mv.col = ref_col;
  1164. // Baseline value at the centre
  1165. bestsad = fn_ptr->sdf(what, what_stride, bestaddress,
  1166. in_what_stride, 0x7fffffff)
  1167. + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
  1168. // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
  1169. if (col_min < x->mv_col_min)
  1170. col_min = x->mv_col_min;
  1171. if (col_max > x->mv_col_max)
  1172. col_max = x->mv_col_max;
  1173. if (row_min < x->mv_row_min)
  1174. row_min = x->mv_row_min;
  1175. if (row_max > x->mv_row_max)
  1176. row_max = x->mv_row_max;
  1177. for (r = row_min; r < row_max ; r++)
  1178. {
  1179. this_mv.as_mv.row = r;
  1180. check_here = r * mv_stride + in_what + col_min;
  1181. for (c = col_min; c < col_max; c++)
  1182. {
  1183. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  1184. this_mv.as_mv.col = c;
  1185. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1186. mvsadcost, sad_per_bit);
  1187. if (thissad < bestsad)
  1188. {
  1189. bestsad = thissad;
  1190. best_mv->as_mv.row = r;
  1191. best_mv->as_mv.col = c;
  1192. bestaddress = check_here;
  1193. }
  1194. check_here++;
  1195. }
  1196. }
  1197. this_mv.as_mv.row = best_mv->as_mv.row << 3;
  1198. this_mv.as_mv.col = best_mv->as_mv.col << 3;
  1199. if (bestsad < INT_MAX)
  1200. return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
  1201. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  1202. else
  1203. return INT_MAX;
  1204. }
  1205. int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
  1206. int sad_per_bit, int distance,
  1207. vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
  1208. int_mv *center_mv)
  1209. {
  1210. unsigned char *what = (*(b->base_src) + b->src);
  1211. int what_stride = b->src_stride;
  1212. unsigned char *in_what;
  1213. int in_what_stride = d->pre_stride;
  1214. int mv_stride = d->pre_stride;
  1215. unsigned char *bestaddress;
  1216. int_mv *best_mv = &d->bmi.mv;
  1217. int_mv this_mv;
  1218. int bestsad = INT_MAX;
  1219. int r, c;
  1220. unsigned char *check_here;
  1221. unsigned int thissad;
  1222. int ref_row = ref_mv->as_mv.row;
  1223. int ref_col = ref_mv->as_mv.col;
  1224. int row_min = ref_row - distance;
  1225. int row_max = ref_row + distance;
  1226. int col_min = ref_col - distance;
  1227. int col_max = ref_col + distance;
  1228. unsigned int sad_array[3];
  1229. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  1230. int_mv fcenter_mv;
  1231. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  1232. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  1233. // Work out the mid point for the search
  1234. in_what = *(d->base_pre) + d->pre;
  1235. bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
  1236. best_mv->as_mv.row = ref_row;
  1237. best_mv->as_mv.col = ref_col;
  1238. // Baseline value at the centre
  1239. bestsad = fn_ptr->sdf(what, what_stride,
  1240. bestaddress, in_what_stride, 0x7fffffff)
  1241. + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
  1242. // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
  1243. if (col_min < x->mv_col_min)
  1244. col_min = x->mv_col_min;
  1245. if (col_max > x->mv_col_max)
  1246. col_max = x->mv_col_max;
  1247. if (row_min < x->mv_row_min)
  1248. row_min = x->mv_row_min;
  1249. if (row_max > x->mv_row_max)
  1250. row_max = x->mv_row_max;
  1251. for (r = row_min; r < row_max ; r++)
  1252. {
  1253. this_mv.as_mv.row = r;
  1254. check_here = r * mv_stride + in_what + col_min;
  1255. c = col_min;
  1256. while ((c + 2) < col_max)
  1257. {
  1258. int i;
  1259. fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
  1260. for (i = 0; i < 3; i++)
  1261. {
  1262. thissad = sad_array[i];
  1263. if (thissad < bestsad)
  1264. {
  1265. this_mv.as_mv.col = c;
  1266. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1267. mvsadcost, sad_per_bit);
  1268. if (thissad < bestsad)
  1269. {
  1270. bestsad = thissad;
  1271. best_mv->as_mv.row = r;
  1272. best_mv->as_mv.col = c;
  1273. bestaddress = check_here;
  1274. }
  1275. }
  1276. check_here++;
  1277. c++;
  1278. }
  1279. }
  1280. while (c < col_max)
  1281. {
  1282. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  1283. if (thissad < bestsad)
  1284. {
  1285. this_mv.as_mv.col = c;
  1286. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1287. mvsadcost, sad_per_bit);
  1288. if (thissad < bestsad)
  1289. {
  1290. bestsad = thissad;
  1291. best_mv->as_mv.row = r;
  1292. best_mv->as_mv.col = c;
  1293. bestaddress = check_here;
  1294. }
  1295. }
  1296. check_here ++;
  1297. c ++;
  1298. }
  1299. }
  1300. this_mv.as_mv.row = best_mv->as_mv.row << 3;
  1301. this_mv.as_mv.col = best_mv->as_mv.col << 3;
  1302. if (bestsad < INT_MAX)
  1303. return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
  1304. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  1305. else
  1306. return INT_MAX;
  1307. }
  1308. int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
  1309. int sad_per_bit, int distance,
  1310. vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
  1311. int_mv *center_mv)
  1312. {
  1313. unsigned char *what = (*(b->base_src) + b->src);
  1314. int what_stride = b->src_stride;
  1315. unsigned char *in_what;
  1316. int in_what_stride = d->pre_stride;
  1317. int mv_stride = d->pre_stride;
  1318. unsigned char *bestaddress;
  1319. int_mv *best_mv = &d->bmi.mv;
  1320. int_mv this_mv;
  1321. int bestsad = INT_MAX;
  1322. int r, c;
  1323. unsigned char *check_here;
  1324. unsigned int thissad;
  1325. int ref_row = ref_mv->as_mv.row;
  1326. int ref_col = ref_mv->as_mv.col;
  1327. int row_min = ref_row - distance;
  1328. int row_max = ref_row + distance;
  1329. int col_min = ref_col - distance;
  1330. int col_max = ref_col + distance;
  1331. DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
  1332. unsigned int sad_array[3];
  1333. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  1334. int_mv fcenter_mv;
  1335. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  1336. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  1337. // Work out the mid point for the search
  1338. in_what = *(d->base_pre) + d->pre;
  1339. bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
  1340. best_mv->as_mv.row = ref_row;
  1341. best_mv->as_mv.col = ref_col;
  1342. // Baseline value at the centre
  1343. bestsad = fn_ptr->sdf(what, what_stride,
  1344. bestaddress, in_what_stride, 0x7fffffff)
  1345. + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
  1346. // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
  1347. if (col_min < x->mv_col_min)
  1348. col_min = x->mv_col_min;
  1349. if (col_max > x->mv_col_max)
  1350. col_max = x->mv_col_max;
  1351. if (row_min < x->mv_row_min)
  1352. row_min = x->mv_row_min;
  1353. if (row_max > x->mv_row_max)
  1354. row_max = x->mv_row_max;
  1355. for (r = row_min; r < row_max ; r++)
  1356. {
  1357. this_mv.as_mv.row = r;
  1358. check_here = r * mv_stride + in_what + col_min;
  1359. c = col_min;
  1360. while ((c + 7) < col_max)
  1361. {
  1362. int i;
  1363. fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
  1364. for (i = 0; i < 8; i++)
  1365. {
  1366. thissad = (unsigned int)sad_array8[i];
  1367. if (thissad < bestsad)
  1368. {
  1369. this_mv.as_mv.col = c;
  1370. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1371. mvsadcost, sad_per_bit);
  1372. if (thissad < bestsad)
  1373. {
  1374. bestsad = thissad;
  1375. best_mv->as_mv.row = r;
  1376. best_mv->as_mv.col = c;
  1377. bestaddress = check_here;
  1378. }
  1379. }
  1380. check_here++;
  1381. c++;
  1382. }
  1383. }
  1384. while ((c + 2) < col_max)
  1385. {
  1386. int i;
  1387. fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
  1388. for (i = 0; i < 3; i++)
  1389. {
  1390. thissad = sad_array[i];
  1391. if (thissad < bestsad)
  1392. {
  1393. this_mv.as_mv.col = c;
  1394. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1395. mvsadcost, sad_per_bit);
  1396. if (thissad < bestsad)
  1397. {
  1398. bestsad = thissad;
  1399. best_mv->as_mv.row = r;
  1400. best_mv->as_mv.col = c;
  1401. bestaddress = check_here;
  1402. }
  1403. }
  1404. check_here++;
  1405. c++;
  1406. }
  1407. }
  1408. while (c < col_max)
  1409. {
  1410. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  1411. if (thissad < bestsad)
  1412. {
  1413. this_mv.as_mv.col = c;
  1414. thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
  1415. mvsadcost, sad_per_bit);
  1416. if (thissad < bestsad)
  1417. {
  1418. bestsad = thissad;
  1419. best_mv->as_mv.row = r;
  1420. best_mv->as_mv.col = c;
  1421. bestaddress = check_here;
  1422. }
  1423. }
  1424. check_here ++;
  1425. c ++;
  1426. }
  1427. }
  1428. this_mv.as_mv.row = best_mv->as_mv.row << 3;
  1429. this_mv.as_mv.col = best_mv->as_mv.col << 3;
  1430. if (bestsad < INT_MAX)
  1431. return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
  1432. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  1433. else
  1434. return INT_MAX;
  1435. }
  1436. int vp8_refining_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
  1437. int error_per_bit, int search_range,
  1438. vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
  1439. int_mv *center_mv)
  1440. {
  1441. MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
  1442. int i, j;
  1443. short this_row_offset, this_col_offset;
  1444. int what_stride = b->src_stride;
  1445. int in_what_stride = d->pre_stride;
  1446. unsigned char *what = (*(b->base_src) + b->src);
  1447. unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
  1448. (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
  1449. unsigned char *check_here;
  1450. unsigned int thissad;
  1451. int_mv this_mv;
  1452. unsigned int bestsad = INT_MAX;
  1453. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  1454. int_mv fcenter_mv;
  1455. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  1456. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  1457. bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
  1458. for (i=0; i<search_range; i++)
  1459. {
  1460. int best_site = -1;
  1461. for (j = 0 ; j < 4 ; j++)
  1462. {
  1463. this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
  1464. this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
  1465. if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
  1466. (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
  1467. {
  1468. check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
  1469. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  1470. if (thissad < bestsad)
  1471. {
  1472. this_mv.as_mv.row = this_row_offset;
  1473. this_mv.as_mv.col = this_col_offset;
  1474. thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
  1475. if (thissad < bestsad)
  1476. {
  1477. bestsad = thissad;
  1478. best_site = j;
  1479. }
  1480. }
  1481. }
  1482. }
  1483. if (best_site == -1)
  1484. break;
  1485. else
  1486. {
  1487. ref_mv->as_mv.row += neighbors[best_site].row;
  1488. ref_mv->as_mv.col += neighbors[best_site].col;
  1489. best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
  1490. }
  1491. }
  1492. this_mv.as_mv.row = ref_mv->as_mv.row << 3;
  1493. this_mv.as_mv.col = ref_mv->as_mv.col << 3;
  1494. if (bestsad < INT_MAX)
  1495. return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
  1496. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  1497. else
  1498. return INT_MAX;
  1499. }
  1500. int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
  1501. int_mv *ref_mv, int error_per_bit,
  1502. int search_range, vp8_variance_fn_ptr_t *fn_ptr,
  1503. int *mvcost[2], int_mv *center_mv)
  1504. {
  1505. MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
  1506. int i, j;
  1507. short this_row_offset, this_col_offset;
  1508. int what_stride = b->src_stride;
  1509. int in_what_stride = d->pre_stride;
  1510. unsigned char *what = (*(b->base_src) + b->src);
  1511. unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
  1512. (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
  1513. unsigned char *check_here;
  1514. unsigned int thissad;
  1515. int_mv this_mv;
  1516. unsigned int bestsad = INT_MAX;
  1517. int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
  1518. int_mv fcenter_mv;
  1519. fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
  1520. fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
  1521. bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
  1522. for (i=0; i<search_range; i++)
  1523. {
  1524. int best_site = -1;
  1525. int all_in = 1;
  1526. all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
  1527. all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
  1528. all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
  1529. all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
  1530. if(all_in)
  1531. {
  1532. unsigned int sad_array[4];
  1533. unsigned char *block_offset[4];
  1534. block_offset[0] = best_address - in_what_stride;
  1535. block_offset[1] = best_address - 1;
  1536. block_offset[2] = best_address + 1;
  1537. block_offset[3] = best_address + in_what_stride;
  1538. fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
  1539. for (j = 0; j < 4; j++)
  1540. {
  1541. if (sad_array[j] < bestsad)
  1542. {
  1543. this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
  1544. this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
  1545. sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
  1546. if (sad_array[j] < bestsad)
  1547. {
  1548. bestsad = sad_array[j];
  1549. best_site = j;
  1550. }
  1551. }
  1552. }
  1553. }
  1554. else
  1555. {
  1556. for (j = 0 ; j < 4 ; j++)
  1557. {
  1558. this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
  1559. this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
  1560. if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
  1561. (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
  1562. {
  1563. check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
  1564. thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
  1565. if (thissad < bestsad)
  1566. {
  1567. this_mv.as_mv.row = this_row_offset;
  1568. this_mv.as_mv.col = this_col_offset;
  1569. thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
  1570. if (thissad < bestsad)
  1571. {
  1572. bestsad = thissad;
  1573. best_site = j;
  1574. }
  1575. }
  1576. }
  1577. }
  1578. }
  1579. if (best_site == -1)
  1580. break;
  1581. else
  1582. {
  1583. ref_mv->as_mv.row += neighbors[best_site].row;
  1584. ref_mv->as_mv.col += neighbors[best_site].col;
  1585. best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
  1586. }
  1587. }
  1588. this_mv.as_mv.row = ref_mv->as_mv.row << 3;
  1589. this_mv.as_mv.col = ref_mv->as_mv.col << 3;
  1590. if (bestsad < INT_MAX)
  1591. return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
  1592. + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
  1593. else
  1594. return INT_MAX;
  1595. }
  1596. #ifdef ENTROPY_STATS
  1597. void print_mode_context(void)
  1598. {
  1599. FILE *f = fopen("modecont.c", "w");
  1600. int i, j;
  1601. fprintf(f, "#include \"entropy.h\"\n");
  1602. fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
  1603. fprintf(f, "{\n");
  1604. for (j = 0; j < 6; j++)
  1605. {
  1606. fprintf(f, " { // %d \n", j);
  1607. fprintf(f, " ");
  1608. for (i = 0; i < 4; i++)
  1609. {
  1610. int overal_prob;
  1611. int this_prob;
  1612. int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
  1613. // Overall probs
  1614. count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
  1615. if (count)
  1616. overal_prob = 256 * mv_mode_cts[i][0] / count;
  1617. else
  1618. overal_prob = 128;
  1619. if (overal_prob == 0)
  1620. overal_prob = 1;
  1621. // context probs
  1622. count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
  1623. if (count)
  1624. this_prob = 256 * mv_ref_ct[j][i][0] / count;
  1625. else
  1626. this_prob = 128;
  1627. if (this_prob == 0)
  1628. this_prob = 1;
  1629. fprintf(f, "%5d, ", this_prob);
  1630. //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
  1631. //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
  1632. }
  1633. fprintf(f, " },\n");
  1634. }
  1635. fprintf(f, "};\n");
  1636. fclose(f);
  1637. }
  1638. /* MV ref count ENTROPY_STATS stats code */
  1639. #ifdef ENTROPY_STATS
  1640. void init_mv_ref_counts()
  1641. {
  1642. vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
  1643. vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
  1644. }
  1645. void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
  1646. {
  1647. if (m == ZEROMV)
  1648. {
  1649. ++mv_ref_ct [ct[0]] [0] [0];
  1650. ++mv_mode_cts[0][0];
  1651. }
  1652. else
  1653. {
  1654. ++mv_ref_ct [ct[0]] [0] [1];
  1655. ++mv_mode_cts[0][1];
  1656. if (m == NEARESTMV)
  1657. {
  1658. ++mv_ref_ct [ct[1]] [1] [0];
  1659. ++mv_mode_cts[1][0];
  1660. }
  1661. else
  1662. {
  1663. ++mv_ref_ct [ct[1]] [1] [1];
  1664. ++mv_mode_cts[1][1];
  1665. if (m == NEARMV)
  1666. {
  1667. ++mv_ref_ct [ct[2]] [2] [0];
  1668. ++mv_mode_cts[2][0];
  1669. }
  1670. else
  1671. {
  1672. ++mv_ref_ct [ct[2]] [2] [1];
  1673. ++mv_mode_cts[2][1];
  1674. if (m == NEWMV)
  1675. {
  1676. ++mv_ref_ct [ct[3]] [3] [0];
  1677. ++mv_mode_cts[3][0];
  1678. }
  1679. else
  1680. {
  1681. ++mv_ref_ct [ct[3]] [3] [1];
  1682. ++mv_mode_cts[3][1];
  1683. }
  1684. }
  1685. }
  1686. }
  1687. }
  1688. #endif/* END MV ref count ENTROPY_STATS stats code */
  1689. #endif