me.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*****************************************************************************
  2. * me.c: motion estimation
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Loren Merritt <lorenm@u.washington.edu>
  7. * Laurent Aimar <fenrir@via.ecp.fr>
  8. * Fiona Glaser <fiona@x264.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  23. *
  24. * This program is also available under a commercial proprietary license.
  25. * For more information, contact us at licensing@x264.com.
  26. *****************************************************************************/
  27. #include "common/common.h"
  28. #include "macroblock.h"
  29. #include "me.h"
  30. /* presets selected from good points on the speed-vs-quality curve of several test videos
  31. * subpel_iters[i_subpel_refine] = { refine_hpel, refine_qpel, me_hpel, me_qpel }
  32. * where me_* are the number of EPZS iterations run on all candidate block types,
  33. * and refine_* are run only on the winner.
  34. * the subme=8,9 values are much higher because any amount of satd search makes
  35. * up its time by reducing the number of qpel-rd iterations. */
  36. static const uint8_t subpel_iterations[][4] =
  37. {{0,0,0,0},
  38. {1,1,0,0},
  39. {0,1,1,0},
  40. {0,2,1,0},
  41. {0,2,1,1},
  42. {0,2,1,2},
  43. {0,0,2,2},
  44. {0,0,2,2},
  45. {0,0,4,10},
  46. {0,0,4,10},
  47. {0,0,4,10},
  48. {0,0,4,10}};
  49. /* (x-1)%6 */
  50. static const uint8_t mod6m1[8] = {5,0,1,2,3,4,5,0};
  51. /* radius 2 hexagon. repeated entries are to avoid having to compute mod6 every time. */
  52. static const int8_t hex2[8][2] = {{-1,-2}, {-2,0}, {-1,2}, {1,2}, {2,0}, {1,-2}, {-1,-2}, {-2,0}};
  53. static const int8_t square1[9][2] = {{0,0}, {0,-1}, {0,1}, {-1,0}, {1,0}, {-1,-1}, {-1,1}, {1,-1}, {1,1}};
  54. static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel );
  55. #define BITS_MVD( mx, my )\
  56. (p_cost_mvx[(mx)<<2] + p_cost_mvy[(my)<<2])
  57. #define COST_MV( mx, my )\
  58. do\
  59. {\
  60. int cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE,\
  61. &p_fref_w[(my)*stride+(mx)], stride )\
  62. + BITS_MVD(mx,my);\
  63. COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );\
  64. } while( 0 )
  65. #define COST_MV_HPEL( mx, my, cost )\
  66. do\
  67. {\
  68. intptr_t stride2 = 16;\
  69. pixel *src = h->mc.get_ref( pix, &stride2, m->p_fref, stride, mx, my, bw, bh, &m->weight[0] );\
  70. cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, src, stride2 )\
  71. + p_cost_mvx[ mx ] + p_cost_mvy[ my ];\
  72. } while( 0 )
  73. #define COST_MV_X3_DIR( m0x, m0y, m1x, m1y, m2x, m2y, costs )\
  74. {\
  75. pixel *pix_base = p_fref_w + bmx + bmy*stride;\
  76. h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\
  77. pix_base + (m0x) + (m0y)*stride,\
  78. pix_base + (m1x) + (m1y)*stride,\
  79. pix_base + (m2x) + (m2y)*stride,\
  80. stride, costs );\
  81. (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\
  82. (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\
  83. (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\
  84. }
  85. #define COST_MV_X4_DIR( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y, costs )\
  86. {\
  87. pixel *pix_base = p_fref_w + bmx + bmy*stride;\
  88. h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
  89. pix_base + (m0x) + (m0y)*stride,\
  90. pix_base + (m1x) + (m1y)*stride,\
  91. pix_base + (m2x) + (m2y)*stride,\
  92. pix_base + (m3x) + (m3y)*stride,\
  93. stride, costs );\
  94. (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\
  95. (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\
  96. (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\
  97. (costs)[3] += BITS_MVD( bmx+(m3x), bmy+(m3y) );\
  98. }
  99. #define COST_MV_X4( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\
  100. {\
  101. pixel *pix_base = p_fref_w + omx + omy*stride;\
  102. h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
  103. pix_base + (m0x) + (m0y)*stride,\
  104. pix_base + (m1x) + (m1y)*stride,\
  105. pix_base + (m2x) + (m2y)*stride,\
  106. pix_base + (m3x) + (m3y)*stride,\
  107. stride, costs );\
  108. costs[0] += BITS_MVD( omx+(m0x), omy+(m0y) );\
  109. costs[1] += BITS_MVD( omx+(m1x), omy+(m1y) );\
  110. costs[2] += BITS_MVD( omx+(m2x), omy+(m2y) );\
  111. costs[3] += BITS_MVD( omx+(m3x), omy+(m3y) );\
  112. COPY3_IF_LT( bcost, costs[0], bmx, omx+(m0x), bmy, omy+(m0y) );\
  113. COPY3_IF_LT( bcost, costs[1], bmx, omx+(m1x), bmy, omy+(m1y) );\
  114. COPY3_IF_LT( bcost, costs[2], bmx, omx+(m2x), bmy, omy+(m2y) );\
  115. COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );\
  116. }
  117. #define COST_MV_X3_ABS( m0x, m0y, m1x, m1y, m2x, m2y )\
  118. {\
  119. h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\
  120. p_fref_w + (m0x) + (m0y)*stride,\
  121. p_fref_w + (m1x) + (m1y)*stride,\
  122. p_fref_w + (m2x) + (m2y)*stride,\
  123. stride, costs );\
  124. costs[0] += p_cost_mvx[(m0x)<<2]; /* no cost_mvy */\
  125. costs[1] += p_cost_mvx[(m1x)<<2];\
  126. costs[2] += p_cost_mvx[(m2x)<<2];\
  127. COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );\
  128. COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );\
  129. COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );\
  130. }
  131. /* 1 */
  132. /* 101 */
  133. /* 1 */
  134. #define DIA1_ITER( mx, my )\
  135. {\
  136. omx = mx; omy = my;\
  137. COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 );\
  138. }
  139. #define CROSS( start, x_max, y_max )\
  140. {\
  141. int i = start;\
  142. if( (x_max) <= X264_MIN(mv_x_max-omx, omx-mv_x_min) )\
  143. for( ; i < (x_max)-2; i+=4 )\
  144. COST_MV_X4( i,0, -i,0, i+2,0, -i-2,0 );\
  145. for( ; i < (x_max); i+=2 )\
  146. {\
  147. if( omx+i <= mv_x_max )\
  148. COST_MV( omx+i, omy );\
  149. if( omx-i >= mv_x_min )\
  150. COST_MV( omx-i, omy );\
  151. }\
  152. i = start;\
  153. if( (y_max) <= X264_MIN(mv_y_max-omy, omy-mv_y_min) )\
  154. for( ; i < (y_max)-2; i+=4 )\
  155. COST_MV_X4( 0,i, 0,-i, 0,i+2, 0,-i-2 );\
  156. for( ; i < (y_max); i+=2 )\
  157. {\
  158. if( omy+i <= mv_y_max )\
  159. COST_MV( omx, omy+i );\
  160. if( omy-i >= mv_y_min )\
  161. COST_MV( omx, omy-i );\
  162. }\
  163. }
  164. #define FPEL(mv) (((mv)+2)>>2) /* Convert subpel MV to fullpel with rounding... */
  165. #define SPEL(mv) ((mv)<<2) /* ... and the reverse. */
  166. #define SPELx2(mv) (SPEL(mv)&0xFFFCFFFC) /* for two packed MVs */
  167. void x264_me_search_ref( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], int i_mvc, int *p_halfpel_thresh )
  168. {
  169. const int bw = x264_pixel_size[m->i_pixel].w;
  170. const int bh = x264_pixel_size[m->i_pixel].h;
  171. const int i_pixel = m->i_pixel;
  172. const int stride = m->i_stride[0];
  173. int i_me_range = h->param.analyse.i_me_range;
  174. int bmx, bmy, bcost = COST_MAX;
  175. int bpred_cost = COST_MAX;
  176. int omx, omy, pmx, pmy;
  177. pixel *p_fenc = m->p_fenc[0];
  178. pixel *p_fref_w = m->p_fref_w;
  179. ALIGNED_ARRAY_32( pixel, pix,[16*16] );
  180. ALIGNED_ARRAY_8( int16_t, mvc_temp,[16],[2] );
  181. ALIGNED_ARRAY_16( int, costs,[16] );
  182. int mv_x_min = h->mb.mv_limit_fpel[0][0];
  183. int mv_y_min = h->mb.mv_limit_fpel[0][1];
  184. int mv_x_max = h->mb.mv_limit_fpel[1][0];
  185. int mv_y_max = h->mb.mv_limit_fpel[1][1];
  186. /* Special version of pack to allow shortcuts in CHECK_MVRANGE */
  187. #define pack16to32_mask2(mx,my) ((mx<<16)|(my&0x7FFF))
  188. uint32_t mv_min = pack16to32_mask2( -mv_x_min, -mv_y_min );
  189. uint32_t mv_max = pack16to32_mask2( mv_x_max, mv_y_max )|0x8000;
  190. uint32_t pmv, bpred_mv = 0;
  191. #define CHECK_MVRANGE(mx,my) (!(((pack16to32_mask2(mx,my) + mv_min) | (mv_max - pack16to32_mask2(mx,my))) & 0x80004000))
  192. const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
  193. const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
  194. /* Try extra predictors if provided. If subme >= 3, check subpel predictors,
  195. * otherwise round them to fullpel. */
  196. if( h->mb.i_subpel_refine >= 3 )
  197. {
  198. /* Calculate and check the MVP first */
  199. int bpred_mx = x264_clip3( m->mvp[0], SPEL(mv_x_min), SPEL(mv_x_max) );
  200. int bpred_my = x264_clip3( m->mvp[1], SPEL(mv_y_min), SPEL(mv_y_max) );
  201. pmv = pack16to32_mask( bpred_mx, bpred_my );
  202. pmx = FPEL( bpred_mx );
  203. pmy = FPEL( bpred_my );
  204. COST_MV_HPEL( bpred_mx, bpred_my, bpred_cost );
  205. int pmv_cost = bpred_cost;
  206. if( i_mvc > 0 )
  207. {
  208. /* Clip MV candidates and eliminate those equal to zero and pmv. */
  209. int valid_mvcs = x264_predictor_clip( mvc_temp+2, mvc, i_mvc, h->mb.mv_limit_fpel, pmv );
  210. if( valid_mvcs > 0 )
  211. {
  212. int i = 1, cost;
  213. /* We stuff pmv here to branchlessly pick between pmv and the various
  214. * MV candidates. [0] gets skipped in order to maintain alignment for
  215. * x264_predictor_clip. */
  216. M32( mvc_temp[1] ) = pmv;
  217. bpred_cost <<= 4;
  218. do
  219. {
  220. int mx = mvc_temp[i+1][0];
  221. int my = mvc_temp[i+1][1];
  222. COST_MV_HPEL( mx, my, cost );
  223. COPY1_IF_LT( bpred_cost, (cost << 4) + i );
  224. } while( ++i <= valid_mvcs );
  225. bpred_mx = mvc_temp[(bpred_cost&15)+1][0];
  226. bpred_my = mvc_temp[(bpred_cost&15)+1][1];
  227. bpred_cost >>= 4;
  228. }
  229. }
  230. /* Round the best predictor back to fullpel and get the cost, since this is where
  231. * we'll be starting the fullpel motion search. */
  232. bmx = FPEL( bpred_mx );
  233. bmy = FPEL( bpred_my );
  234. bpred_mv = pack16to32_mask(bpred_mx, bpred_my);
  235. if( bpred_mv&0x00030003 ) /* Only test if the tested predictor is actually subpel... */
  236. COST_MV( bmx, bmy );
  237. else /* Otherwise just copy the cost (we already know it) */
  238. bcost = bpred_cost;
  239. /* Test the zero vector if it hasn't been tested yet. */
  240. if( pmv )
  241. {
  242. if( bmx|bmy ) COST_MV( 0, 0 );
  243. }
  244. /* If a subpel mv candidate was better than the zero vector, the previous
  245. * fullpel check won't have gotten it even if the pmv was zero. So handle
  246. * that possibility here. */
  247. else
  248. {
  249. COPY3_IF_LT( bcost, pmv_cost, bmx, 0, bmy, 0 );
  250. }
  251. }
  252. else
  253. {
  254. /* Calculate and check the fullpel MVP first */
  255. bmx = pmx = x264_clip3( FPEL(m->mvp[0]), mv_x_min, mv_x_max );
  256. bmy = pmy = x264_clip3( FPEL(m->mvp[1]), mv_y_min, mv_y_max );
  257. pmv = pack16to32_mask( bmx, bmy );
  258. /* Because we are rounding the predicted motion vector to fullpel, there will be
  259. * an extra MV cost in 15 out of 16 cases. However, when the predicted MV is
  260. * chosen as the best predictor, it is often the case that the subpel search will
  261. * result in a vector at or next to the predicted motion vector. Therefore, we omit
  262. * the cost of the MV from the rounded MVP to avoid unfairly biasing against use of
  263. * the predicted motion vector.
  264. *
  265. * Disclaimer: this is a post-hoc rationalization for why this hack works. */
  266. bcost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, &p_fref_w[bmy*stride+bmx], stride );
  267. if( i_mvc > 0 )
  268. {
  269. /* Like in subme>=3, except we also round the candidates to fullpel. */
  270. int valid_mvcs = x264_predictor_roundclip( mvc_temp+2, mvc, i_mvc, h->mb.mv_limit_fpel, pmv );
  271. if( valid_mvcs > 0 )
  272. {
  273. int i = 1, cost;
  274. M32( mvc_temp[1] ) = pmv;
  275. bcost <<= 4;
  276. do
  277. {
  278. int mx = mvc_temp[i+1][0];
  279. int my = mvc_temp[i+1][1];
  280. cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, &p_fref_w[my*stride+mx], stride ) + BITS_MVD( mx, my );
  281. COPY1_IF_LT( bcost, (cost << 4) + i );
  282. } while( ++i <= valid_mvcs );
  283. bmx = mvc_temp[(bcost&15)+1][0];
  284. bmy = mvc_temp[(bcost&15)+1][1];
  285. bcost >>= 4;
  286. }
  287. }
  288. /* Same as above, except the condition is simpler. */
  289. if( pmv )
  290. COST_MV( 0, 0 );
  291. }
  292. switch( h->mb.i_me_method )
  293. {
  294. case X264_ME_DIA:
  295. {
  296. /* diamond search, radius 1 */
  297. bcost <<= 4;
  298. int i = i_me_range;
  299. do
  300. {
  301. COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs );
  302. COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
  303. COPY1_IF_LT( bcost, (costs[1]<<4)+3 );
  304. COPY1_IF_LT( bcost, (costs[2]<<4)+4 );
  305. COPY1_IF_LT( bcost, (costs[3]<<4)+12 );
  306. if( !(bcost&15) )
  307. break;
  308. bmx -= (bcost<<28)>>30;
  309. bmy -= (bcost<<30)>>30;
  310. bcost &= ~15;
  311. } while( --i && CHECK_MVRANGE(bmx, bmy) );
  312. bcost >>= 4;
  313. break;
  314. }
  315. case X264_ME_HEX:
  316. {
  317. me_hex2:
  318. /* hexagon search, radius 2 */
  319. #if 0
  320. for( int i = 0; i < i_me_range/2; i++ )
  321. {
  322. omx = bmx; omy = bmy;
  323. COST_MV( omx-2, omy );
  324. COST_MV( omx-1, omy+2 );
  325. COST_MV( omx+1, omy+2 );
  326. COST_MV( omx+2, omy );
  327. COST_MV( omx+1, omy-2 );
  328. COST_MV( omx-1, omy-2 );
  329. if( bmx == omx && bmy == omy )
  330. break;
  331. if( !CHECK_MVRANGE(bmx, bmy) )
  332. break;
  333. }
  334. #else
  335. /* equivalent to the above, but eliminates duplicate candidates */
  336. /* hexagon */
  337. COST_MV_X3_DIR( -2,0, -1, 2, 1, 2, costs );
  338. COST_MV_X3_DIR( 2,0, 1,-2, -1,-2, costs+4 ); /* +4 for 16-byte alignment */
  339. bcost <<= 3;
  340. COPY1_IF_LT( bcost, (costs[0]<<3)+2 );
  341. COPY1_IF_LT( bcost, (costs[1]<<3)+3 );
  342. COPY1_IF_LT( bcost, (costs[2]<<3)+4 );
  343. COPY1_IF_LT( bcost, (costs[4]<<3)+5 );
  344. COPY1_IF_LT( bcost, (costs[5]<<3)+6 );
  345. COPY1_IF_LT( bcost, (costs[6]<<3)+7 );
  346. if( bcost&7 )
  347. {
  348. int dir = (bcost&7)-2;
  349. bmx += hex2[dir+1][0];
  350. bmy += hex2[dir+1][1];
  351. /* half hexagon, not overlapping the previous iteration */
  352. for( int i = (i_me_range>>1) - 1; i > 0 && CHECK_MVRANGE(bmx, bmy); i-- )
  353. {
  354. COST_MV_X3_DIR( hex2[dir+0][0], hex2[dir+0][1],
  355. hex2[dir+1][0], hex2[dir+1][1],
  356. hex2[dir+2][0], hex2[dir+2][1],
  357. costs );
  358. bcost &= ~7;
  359. COPY1_IF_LT( bcost, (costs[0]<<3)+1 );
  360. COPY1_IF_LT( bcost, (costs[1]<<3)+2 );
  361. COPY1_IF_LT( bcost, (costs[2]<<3)+3 );
  362. if( !(bcost&7) )
  363. break;
  364. dir += (bcost&7)-2;
  365. dir = mod6m1[dir+1];
  366. bmx += hex2[dir+1][0];
  367. bmy += hex2[dir+1][1];
  368. }
  369. }
  370. bcost >>= 3;
  371. #endif
  372. /* square refine */
  373. bcost <<= 4;
  374. COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs );
  375. COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
  376. COPY1_IF_LT( bcost, (costs[1]<<4)+2 );
  377. COPY1_IF_LT( bcost, (costs[2]<<4)+3 );
  378. COPY1_IF_LT( bcost, (costs[3]<<4)+4 );
  379. COST_MV_X4_DIR( -1,-1, -1,1, 1,-1, 1,1, costs );
  380. COPY1_IF_LT( bcost, (costs[0]<<4)+5 );
  381. COPY1_IF_LT( bcost, (costs[1]<<4)+6 );
  382. COPY1_IF_LT( bcost, (costs[2]<<4)+7 );
  383. COPY1_IF_LT( bcost, (costs[3]<<4)+8 );
  384. bmx += square1[bcost&15][0];
  385. bmy += square1[bcost&15][1];
  386. bcost >>= 4;
  387. break;
  388. }
  389. case X264_ME_UMH:
  390. {
  391. /* Uneven-cross Multi-Hexagon-grid Search
  392. * as in JM, except with different early termination */
  393. static const uint8_t pixel_size_shift[7] = { 0, 1, 1, 2, 3, 3, 4 };
  394. int ucost1, ucost2;
  395. int cross_start = 1;
  396. /* refine predictors */
  397. ucost1 = bcost;
  398. DIA1_ITER( pmx, pmy );
  399. if( pmx | pmy )
  400. DIA1_ITER( 0, 0 );
  401. if( i_pixel == PIXEL_4x4 )
  402. goto me_hex2;
  403. ucost2 = bcost;
  404. if( (bmx | bmy) && ((bmx-pmx) | (bmy-pmy)) )
  405. DIA1_ITER( bmx, bmy );
  406. if( bcost == ucost2 )
  407. cross_start = 3;
  408. omx = bmx; omy = bmy;
  409. /* early termination */
  410. #define SAD_THRESH(v) ( bcost < ( v >> pixel_size_shift[i_pixel] ) )
  411. if( bcost == ucost2 && SAD_THRESH(2000) )
  412. {
  413. COST_MV_X4( 0,-2, -1,-1, 1,-1, -2,0 );
  414. COST_MV_X4( 2, 0, -1, 1, 1, 1, 0,2 );
  415. if( bcost == ucost1 && SAD_THRESH(500) )
  416. break;
  417. if( bcost == ucost2 )
  418. {
  419. int range = (i_me_range>>1) | 1;
  420. CROSS( 3, range, range );
  421. COST_MV_X4( -1,-2, 1,-2, -2,-1, 2,-1 );
  422. COST_MV_X4( -2, 1, 2, 1, -1, 2, 1, 2 );
  423. if( bcost == ucost2 )
  424. break;
  425. cross_start = range + 2;
  426. }
  427. }
  428. /* adaptive search range */
  429. if( i_mvc )
  430. {
  431. /* range multipliers based on casual inspection of some statistics of
  432. * average distance between current predictor and final mv found by ESA.
  433. * these have not been tuned much by actual encoding. */
  434. static const uint8_t range_mul[4][4] =
  435. {
  436. { 3, 3, 4, 4 },
  437. { 3, 4, 4, 4 },
  438. { 4, 4, 4, 5 },
  439. { 4, 4, 5, 6 },
  440. };
  441. int mvd;
  442. int sad_ctx, mvd_ctx;
  443. int denom = 1;
  444. if( i_mvc == 1 )
  445. {
  446. if( i_pixel == PIXEL_16x16 )
  447. /* mvc is probably the same as mvp, so the difference isn't meaningful.
  448. * but prediction usually isn't too bad, so just use medium range */
  449. mvd = 25;
  450. else
  451. mvd = abs( m->mvp[0] - mvc[0][0] )
  452. + abs( m->mvp[1] - mvc[0][1] );
  453. }
  454. else
  455. {
  456. /* calculate the degree of agreement between predictors. */
  457. /* in 16x16, mvc includes all the neighbors used to make mvp,
  458. * so don't count mvp separately. */
  459. denom = i_mvc - 1;
  460. mvd = 0;
  461. if( i_pixel != PIXEL_16x16 )
  462. {
  463. mvd = abs( m->mvp[0] - mvc[0][0] )
  464. + abs( m->mvp[1] - mvc[0][1] );
  465. denom++;
  466. }
  467. mvd += x264_predictor_difference( mvc, i_mvc );
  468. }
  469. sad_ctx = SAD_THRESH(1000) ? 0
  470. : SAD_THRESH(2000) ? 1
  471. : SAD_THRESH(4000) ? 2 : 3;
  472. mvd_ctx = mvd < 10*denom ? 0
  473. : mvd < 20*denom ? 1
  474. : mvd < 40*denom ? 2 : 3;
  475. i_me_range = i_me_range * range_mul[mvd_ctx][sad_ctx] >> 2;
  476. }
  477. /* FIXME if the above DIA2/OCT2/CROSS found a new mv, it has not updated omx/omy.
  478. * we are still centered on the same place as the DIA2. is this desirable? */
  479. CROSS( cross_start, i_me_range, i_me_range>>1 );
  480. COST_MV_X4( -2,-2, -2,2, 2,-2, 2,2 );
  481. /* hexagon grid */
  482. omx = bmx; omy = bmy;
  483. const uint16_t *p_cost_omvx = p_cost_mvx + omx*4;
  484. const uint16_t *p_cost_omvy = p_cost_mvy + omy*4;
  485. int i = 1;
  486. do
  487. {
  488. static const int8_t hex4[16][2] = {
  489. { 0,-4}, { 0, 4}, {-2,-3}, { 2,-3},
  490. {-4,-2}, { 4,-2}, {-4,-1}, { 4,-1},
  491. {-4, 0}, { 4, 0}, {-4, 1}, { 4, 1},
  492. {-4, 2}, { 4, 2}, {-2, 3}, { 2, 3},
  493. };
  494. if( 4*i > X264_MIN4( mv_x_max-omx, omx-mv_x_min,
  495. mv_y_max-omy, omy-mv_y_min ) )
  496. {
  497. for( int j = 0; j < 16; j++ )
  498. {
  499. int mx = omx + hex4[j][0]*i;
  500. int my = omy + hex4[j][1]*i;
  501. if( CHECK_MVRANGE(mx, my) )
  502. COST_MV( mx, my );
  503. }
  504. }
  505. else
  506. {
  507. int dir = 0;
  508. pixel *pix_base = p_fref_w + omx + (omy-4*i)*stride;
  509. int dy = i*stride;
  510. #define SADS(k,x0,y0,x1,y1,x2,y2,x3,y3)\
  511. h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
  512. pix_base x0*i+(y0-2*k+4)*dy,\
  513. pix_base x1*i+(y1-2*k+4)*dy,\
  514. pix_base x2*i+(y2-2*k+4)*dy,\
  515. pix_base x3*i+(y3-2*k+4)*dy,\
  516. stride, costs+4*k );\
  517. pix_base += 2*dy;
  518. #define ADD_MVCOST(k,x,y) costs[k] += p_cost_omvx[x*4*i] + p_cost_omvy[y*4*i]
  519. #define MIN_MV(k,x,y) COPY2_IF_LT( bcost, costs[k], dir, x*16+(y&15) )
  520. SADS( 0, +0,-4, +0,+4, -2,-3, +2,-3 );
  521. SADS( 1, -4,-2, +4,-2, -4,-1, +4,-1 );
  522. SADS( 2, -4,+0, +4,+0, -4,+1, +4,+1 );
  523. SADS( 3, -4,+2, +4,+2, -2,+3, +2,+3 );
  524. ADD_MVCOST( 0, 0,-4 );
  525. ADD_MVCOST( 1, 0, 4 );
  526. ADD_MVCOST( 2,-2,-3 );
  527. ADD_MVCOST( 3, 2,-3 );
  528. ADD_MVCOST( 4,-4,-2 );
  529. ADD_MVCOST( 5, 4,-2 );
  530. ADD_MVCOST( 6,-4,-1 );
  531. ADD_MVCOST( 7, 4,-1 );
  532. ADD_MVCOST( 8,-4, 0 );
  533. ADD_MVCOST( 9, 4, 0 );
  534. ADD_MVCOST( 10,-4, 1 );
  535. ADD_MVCOST( 11, 4, 1 );
  536. ADD_MVCOST( 12,-4, 2 );
  537. ADD_MVCOST( 13, 4, 2 );
  538. ADD_MVCOST( 14,-2, 3 );
  539. ADD_MVCOST( 15, 2, 3 );
  540. MIN_MV( 0, 0,-4 );
  541. MIN_MV( 1, 0, 4 );
  542. MIN_MV( 2,-2,-3 );
  543. MIN_MV( 3, 2,-3 );
  544. MIN_MV( 4,-4,-2 );
  545. MIN_MV( 5, 4,-2 );
  546. MIN_MV( 6,-4,-1 );
  547. MIN_MV( 7, 4,-1 );
  548. MIN_MV( 8,-4, 0 );
  549. MIN_MV( 9, 4, 0 );
  550. MIN_MV( 10,-4, 1 );
  551. MIN_MV( 11, 4, 1 );
  552. MIN_MV( 12,-4, 2 );
  553. MIN_MV( 13, 4, 2 );
  554. MIN_MV( 14,-2, 3 );
  555. MIN_MV( 15, 2, 3 );
  556. #undef SADS
  557. #undef ADD_MVCOST
  558. #undef MIN_MV
  559. if( dir )
  560. {
  561. bmx = omx + i*(dir>>4);
  562. bmy = omy + i*((dir<<28)>>28);
  563. }
  564. }
  565. } while( ++i <= i_me_range>>2 );
  566. if( bmy <= mv_y_max && bmy >= mv_y_min && bmx <= mv_x_max && bmx >= mv_x_min )
  567. goto me_hex2;
  568. break;
  569. }
  570. case X264_ME_ESA:
  571. case X264_ME_TESA:
  572. {
  573. const int min_x = X264_MAX( bmx - i_me_range, mv_x_min );
  574. const int min_y = X264_MAX( bmy - i_me_range, mv_y_min );
  575. const int max_x = X264_MIN( bmx + i_me_range, mv_x_max );
  576. const int max_y = X264_MIN( bmy + i_me_range, mv_y_max );
  577. /* SEA is fastest in multiples of 4 */
  578. const int width = (max_x - min_x + 3) & ~3;
  579. #if 0
  580. /* plain old exhaustive search */
  581. for( int my = min_y; my <= max_y; my++ )
  582. for( int mx = min_x; mx < min_x + width; mx++ )
  583. COST_MV( mx, my );
  584. #else
  585. /* successive elimination by comparing DC before a full SAD,
  586. * because sum(abs(diff)) >= abs(diff(sum)). */
  587. uint16_t *sums_base = m->integral;
  588. ALIGNED_ARRAY_16( int, enc_dc,[4] );
  589. int sad_size = i_pixel <= PIXEL_8x8 ? PIXEL_8x8 : PIXEL_4x4;
  590. int delta = x264_pixel_size[sad_size].w;
  591. int16_t *xs = h->scratch_buffer;
  592. int xn;
  593. uint16_t *cost_fpel_mvx = h->cost_mv_fpel[h->mb.i_qp][-m->mvp[0]&3] + (-m->mvp[0]>>2);
  594. h->pixf.sad_x4[sad_size]( (pixel*)x264_zero, p_fenc, p_fenc+delta,
  595. p_fenc+delta*FENC_STRIDE, p_fenc+delta+delta*FENC_STRIDE,
  596. FENC_STRIDE, enc_dc );
  597. if( delta == 4 )
  598. sums_base += stride * (h->fenc->i_lines[0] + PADV*2);
  599. if( i_pixel == PIXEL_16x16 || i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
  600. delta *= stride;
  601. if( i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
  602. enc_dc[1] = enc_dc[2];
  603. if( h->mb.i_me_method == X264_ME_TESA )
  604. {
  605. // ADS threshold, then SAD threshold, then keep the best few SADs, then SATD
  606. mvsad_t *mvsads = (mvsad_t *)(xs + ((width+31)&~31) + 4);
  607. int nmvsad = 0, limit;
  608. int sad_thresh = i_me_range <= 16 ? 10 : i_me_range <= 24 ? 11 : 12;
  609. int bsad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+bmy*stride+bmx, stride )
  610. + BITS_MVD( bmx, bmy );
  611. for( int my = min_y; my <= max_y; my++ )
  612. {
  613. int i;
  614. int ycost = p_cost_mvy[my<<2];
  615. if( bsad <= ycost )
  616. continue;
  617. bsad -= ycost;
  618. xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
  619. cost_fpel_mvx+min_x, xs, width, bsad * 17 >> 4 );
  620. for( i = 0; i < xn-2; i += 3 )
  621. {
  622. pixel *ref = p_fref_w+min_x+my*stride;
  623. ALIGNED_ARRAY_16( int, sads,[4] ); /* padded to [4] for asm */
  624. h->pixf.sad_x3[i_pixel]( p_fenc, ref+xs[i], ref+xs[i+1], ref+xs[i+2], stride, sads );
  625. for( int j = 0; j < 3; j++ )
  626. {
  627. int sad = sads[j] + cost_fpel_mvx[xs[i+j]];
  628. if( sad < bsad*sad_thresh>>3 )
  629. {
  630. COPY1_IF_LT( bsad, sad );
  631. mvsads[nmvsad].sad = sad + ycost;
  632. mvsads[nmvsad].mv[0] = min_x+xs[i+j];
  633. mvsads[nmvsad].mv[1] = my;
  634. nmvsad++;
  635. }
  636. }
  637. }
  638. for( ; i < xn; i++ )
  639. {
  640. int mx = min_x+xs[i];
  641. int sad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+mx+my*stride, stride )
  642. + cost_fpel_mvx[xs[i]];
  643. if( sad < bsad*sad_thresh>>3 )
  644. {
  645. COPY1_IF_LT( bsad, sad );
  646. mvsads[nmvsad].sad = sad + ycost;
  647. mvsads[nmvsad].mv[0] = mx;
  648. mvsads[nmvsad].mv[1] = my;
  649. nmvsad++;
  650. }
  651. }
  652. bsad += ycost;
  653. }
  654. limit = i_me_range >> 1;
  655. sad_thresh = bsad*sad_thresh>>3;
  656. while( nmvsad > limit*2 && sad_thresh > bsad )
  657. {
  658. int i = 0;
  659. // halve the range if the domain is too large... eh, close enough
  660. sad_thresh = (sad_thresh + bsad) >> 1;
  661. while( i < nmvsad && mvsads[i].sad <= sad_thresh )
  662. i++;
  663. for( int j = i; j < nmvsad; j++ )
  664. {
  665. uint32_t sad;
  666. if( WORD_SIZE == 8 && sizeof(mvsad_t) == 8 )
  667. {
  668. uint64_t mvsad = M64( &mvsads[i] ) = M64( &mvsads[j] );
  669. #if WORDS_BIGENDIAN
  670. mvsad >>= 32;
  671. #endif
  672. sad = mvsad;
  673. }
  674. else
  675. {
  676. sad = mvsads[j].sad;
  677. CP32( mvsads[i].mv, mvsads[j].mv );
  678. mvsads[i].sad = sad;
  679. }
  680. i += (sad - (sad_thresh+1)) >> 31;
  681. }
  682. nmvsad = i;
  683. }
  684. while( nmvsad > limit )
  685. {
  686. int bi = 0;
  687. for( int i = 1; i < nmvsad; i++ )
  688. if( mvsads[i].sad > mvsads[bi].sad )
  689. bi = i;
  690. nmvsad--;
  691. if( sizeof( mvsad_t ) == sizeof( uint64_t ) )
  692. CP64( &mvsads[bi], &mvsads[nmvsad] );
  693. else
  694. mvsads[bi] = mvsads[nmvsad];
  695. }
  696. for( int i = 0; i < nmvsad; i++ )
  697. COST_MV( mvsads[i].mv[0], mvsads[i].mv[1] );
  698. }
  699. else
  700. {
  701. // just ADS and SAD
  702. for( int my = min_y; my <= max_y; my++ )
  703. {
  704. int i;
  705. int ycost = p_cost_mvy[my<<2];
  706. if( bcost <= ycost )
  707. continue;
  708. bcost -= ycost;
  709. xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
  710. cost_fpel_mvx+min_x, xs, width, bcost );
  711. for( i = 0; i < xn-2; i += 3 )
  712. COST_MV_X3_ABS( min_x+xs[i],my, min_x+xs[i+1],my, min_x+xs[i+2],my );
  713. bcost += ycost;
  714. for( ; i < xn; i++ )
  715. COST_MV( min_x+xs[i], my );
  716. }
  717. }
  718. #endif
  719. }
  720. break;
  721. }
  722. /* -> qpel mv */
  723. uint32_t bmv = pack16to32_mask(bmx,bmy);
  724. uint32_t bmv_spel = SPELx2(bmv);
  725. if( h->mb.i_subpel_refine < 3 )
  726. {
  727. m->cost_mv = p_cost_mvx[bmx<<2] + p_cost_mvy[bmy<<2];
  728. m->cost = bcost;
  729. /* compute the real cost */
  730. if( bmv == pmv ) m->cost += m->cost_mv;
  731. M32( m->mv ) = bmv_spel;
  732. }
  733. else
  734. {
  735. M32(m->mv) = bpred_cost < bcost ? bpred_mv : bmv_spel;
  736. m->cost = X264_MIN( bpred_cost, bcost );
  737. }
  738. /* subpel refine */
  739. if( h->mb.i_subpel_refine >= 2 )
  740. {
  741. int hpel = subpel_iterations[h->mb.i_subpel_refine][2];
  742. int qpel = subpel_iterations[h->mb.i_subpel_refine][3];
  743. refine_subpel( h, m, hpel, qpel, p_halfpel_thresh, 0 );
  744. }
  745. }
  746. #undef COST_MV
  747. void x264_me_refine_qpel( x264_t *h, x264_me_t *m )
  748. {
  749. int hpel = subpel_iterations[h->mb.i_subpel_refine][0];
  750. int qpel = subpel_iterations[h->mb.i_subpel_refine][1];
  751. if( m->i_pixel <= PIXEL_8x8 )
  752. m->cost -= m->i_ref_cost;
  753. refine_subpel( h, m, hpel, qpel, NULL, 1 );
  754. }
  755. void x264_me_refine_qpel_refdupe( x264_t *h, x264_me_t *m, int *p_halfpel_thresh )
  756. {
  757. refine_subpel( h, m, 0, X264_MIN( 2, subpel_iterations[h->mb.i_subpel_refine][3] ), p_halfpel_thresh, 0 );
  758. }
  759. #define COST_MV_SAD( mx, my ) \
  760. { \
  761. intptr_t stride = 16; \
  762. pixel *src = h->mc.get_ref( pix, &stride, m->p_fref, m->i_stride[0], mx, my, bw, bh, &m->weight[0] ); \
  763. int cost = h->pixf.fpelcmp[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
  764. + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
  765. COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my ); \
  766. }
  767. #define COST_MV_SATD( mx, my, dir ) \
  768. if( b_refine_qpel || (dir^1) != odir ) \
  769. { \
  770. intptr_t stride = 16; \
  771. pixel *src = h->mc.get_ref( pix, &stride, &m->p_fref[0], m->i_stride[0], mx, my, bw, bh, &m->weight[0] ); \
  772. int cost = h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
  773. + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
  774. if( b_chroma_me && cost < bcost ) \
  775. { \
  776. if( CHROMA444 ) \
  777. { \
  778. stride = 16; \
  779. src = h->mc.get_ref( pix, &stride, &m->p_fref[4], m->i_stride[1], mx, my, bw, bh, &m->weight[1] ); \
  780. cost += h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[1], FENC_STRIDE, src, stride ); \
  781. if( cost < bcost ) \
  782. { \
  783. stride = 16; \
  784. src = h->mc.get_ref( pix, &stride, &m->p_fref[8], m->i_stride[2], mx, my, bw, bh, &m->weight[2] ); \
  785. cost += h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[2], FENC_STRIDE, src, stride ); \
  786. } \
  787. } \
  788. else \
  789. { \
  790. h->mc.mc_chroma( pix, pix+8, 16, m->p_fref[4], m->i_stride[1], \
  791. mx, 2*(my+mvy_offset)>>chroma_v_shift, bw>>1, bh>>chroma_v_shift ); \
  792. if( m->weight[1].weightfn ) \
  793. m->weight[1].weightfn[bw>>3]( pix, 16, pix, 16, &m->weight[1], bh>>chroma_v_shift ); \
  794. cost += h->pixf.mbcmp[chromapix]( m->p_fenc[1], FENC_STRIDE, pix, 16 ); \
  795. if( cost < bcost ) \
  796. { \
  797. if( m->weight[2].weightfn ) \
  798. m->weight[2].weightfn[bw>>3]( pix+8, 16, pix+8, 16, &m->weight[2], bh>>chroma_v_shift ); \
  799. cost += h->pixf.mbcmp[chromapix]( m->p_fenc[2], FENC_STRIDE, pix+8, 16 ); \
  800. } \
  801. } \
  802. } \
  803. COPY4_IF_LT( bcost, cost, bmx, mx, bmy, my, bdir, dir ); \
  804. }
  805. static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel )
  806. {
  807. const int bw = x264_pixel_size[m->i_pixel].w;
  808. const int bh = x264_pixel_size[m->i_pixel].h;
  809. const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
  810. const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
  811. const int i_pixel = m->i_pixel;
  812. const int b_chroma_me = h->mb.b_chroma_me && (i_pixel <= PIXEL_8x8 || CHROMA444);
  813. int chromapix = h->luma2chroma_pixel[i_pixel];
  814. int chroma_v_shift = CHROMA_V_SHIFT;
  815. int mvy_offset = chroma_v_shift & MB_INTERLACED & m->i_ref ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
  816. ALIGNED_ARRAY_32( pixel, pix,[64*18] ); // really 17x17x2, but round up for alignment
  817. ALIGNED_ARRAY_16( int, costs,[4] );
  818. int bmx = m->mv[0];
  819. int bmy = m->mv[1];
  820. int bcost = m->cost;
  821. int odir = -1, bdir;
  822. /* halfpel diamond search */
  823. if( hpel_iters )
  824. {
  825. /* try the subpel component of the predicted mv */
  826. if( h->mb.i_subpel_refine < 3 )
  827. {
  828. int mx = x264_clip3( m->mvp[0], h->mb.mv_min_spel[0]+2, h->mb.mv_max_spel[0]-2 );
  829. int my = x264_clip3( m->mvp[1], h->mb.mv_min_spel[1]+2, h->mb.mv_max_spel[1]-2 );
  830. if( (mx-bmx)|(my-bmy) )
  831. COST_MV_SAD( mx, my );
  832. }
  833. bcost <<= 6;
  834. for( int i = hpel_iters; i > 0; i-- )
  835. {
  836. int omx = bmx, omy = bmy;
  837. intptr_t stride = 64; // candidates are either all hpel or all qpel, so one stride is enough
  838. pixel *src0, *src1, *src2, *src3;
  839. src0 = h->mc.get_ref( pix, &stride, m->p_fref, m->i_stride[0], omx, omy-2, bw, bh+1, &m->weight[0] );
  840. src2 = h->mc.get_ref( pix+32, &stride, m->p_fref, m->i_stride[0], omx-2, omy, bw+4, bh, &m->weight[0] );
  841. src1 = src0 + stride;
  842. src3 = src2 + 1;
  843. h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], src0, src1, src2, src3, stride, costs );
  844. costs[0] += p_cost_mvx[omx ] + p_cost_mvy[omy-2];
  845. costs[1] += p_cost_mvx[omx ] + p_cost_mvy[omy+2];
  846. costs[2] += p_cost_mvx[omx-2] + p_cost_mvy[omy ];
  847. costs[3] += p_cost_mvx[omx+2] + p_cost_mvy[omy ];
  848. COPY1_IF_LT( bcost, (costs[0]<<6)+2 );
  849. COPY1_IF_LT( bcost, (costs[1]<<6)+6 );
  850. COPY1_IF_LT( bcost, (costs[2]<<6)+16 );
  851. COPY1_IF_LT( bcost, (costs[3]<<6)+48 );
  852. if( !(bcost&63) )
  853. break;
  854. bmx -= (bcost<<26)>>29;
  855. bmy -= (bcost<<29)>>29;
  856. bcost &= ~63;
  857. }
  858. bcost >>= 6;
  859. }
  860. if( !b_refine_qpel && (h->pixf.mbcmp_unaligned[0] != h->pixf.fpelcmp[0] || b_chroma_me) )
  861. {
  862. bcost = COST_MAX;
  863. COST_MV_SATD( bmx, bmy, -1 );
  864. }
  865. /* early termination when examining multiple reference frames */
  866. if( p_halfpel_thresh )
  867. {
  868. if( (bcost*7)>>3 > *p_halfpel_thresh )
  869. {
  870. m->cost = bcost;
  871. m->mv[0] = bmx;
  872. m->mv[1] = bmy;
  873. // don't need cost_mv
  874. return;
  875. }
  876. else if( bcost < *p_halfpel_thresh )
  877. *p_halfpel_thresh = bcost;
  878. }
  879. /* quarterpel diamond search */
  880. if( h->mb.i_subpel_refine != 1 )
  881. {
  882. bdir = -1;
  883. for( int i = qpel_iters; i > 0; i-- )
  884. {
  885. if( bmy <= h->mb.mv_min_spel[1] || bmy >= h->mb.mv_max_spel[1] || bmx <= h->mb.mv_min_spel[0] || bmx >= h->mb.mv_max_spel[0] )
  886. break;
  887. odir = bdir;
  888. int omx = bmx, omy = bmy;
  889. COST_MV_SATD( omx, omy - 1, 0 );
  890. COST_MV_SATD( omx, omy + 1, 1 );
  891. COST_MV_SATD( omx - 1, omy, 2 );
  892. COST_MV_SATD( omx + 1, omy, 3 );
  893. if( (bmx == omx) & (bmy == omy) )
  894. break;
  895. }
  896. }
  897. /* Special simplified case for subme=1 */
  898. else if( bmy > h->mb.mv_min_spel[1] && bmy < h->mb.mv_max_spel[1] && bmx > h->mb.mv_min_spel[0] && bmx < h->mb.mv_max_spel[0] )
  899. {
  900. int omx = bmx, omy = bmy;
  901. /* We have to use mc_luma because all strides must be the same to use fpelcmp_x4 */
  902. h->mc.mc_luma( pix , 64, m->p_fref, m->i_stride[0], omx, omy-1, bw, bh, &m->weight[0] );
  903. h->mc.mc_luma( pix+16, 64, m->p_fref, m->i_stride[0], omx, omy+1, bw, bh, &m->weight[0] );
  904. h->mc.mc_luma( pix+32, 64, m->p_fref, m->i_stride[0], omx-1, omy, bw, bh, &m->weight[0] );
  905. h->mc.mc_luma( pix+48, 64, m->p_fref, m->i_stride[0], omx+1, omy, bw, bh, &m->weight[0] );
  906. h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], pix, pix+16, pix+32, pix+48, 64, costs );
  907. costs[0] += p_cost_mvx[omx ] + p_cost_mvy[omy-1];
  908. costs[1] += p_cost_mvx[omx ] + p_cost_mvy[omy+1];
  909. costs[2] += p_cost_mvx[omx-1] + p_cost_mvy[omy ];
  910. costs[3] += p_cost_mvx[omx+1] + p_cost_mvy[omy ];
  911. bcost <<= 4;
  912. COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
  913. COPY1_IF_LT( bcost, (costs[1]<<4)+3 );
  914. COPY1_IF_LT( bcost, (costs[2]<<4)+4 );
  915. COPY1_IF_LT( bcost, (costs[3]<<4)+12 );
  916. bmx -= (bcost<<28)>>30;
  917. bmy -= (bcost<<30)>>30;
  918. bcost >>= 4;
  919. }
  920. m->cost = bcost;
  921. m->mv[0] = bmx;
  922. m->mv[1] = bmy;
  923. m->cost_mv = p_cost_mvx[bmx] + p_cost_mvy[bmy];
  924. }
  925. #define BIME_CACHE( dx, dy, list )\
  926. {\
  927. x264_me_t *m = m##list;\
  928. int i = 4 + 3*dx + dy;\
  929. int mvx = bm##list##x+dx;\
  930. int mvy = bm##list##y+dy;\
  931. stride[0][list][i] = bw;\
  932. src[0][list][i] = h->mc.get_ref( pixy_buf[list][i], &stride[0][list][i], &m->p_fref[0],\
  933. m->i_stride[0], mvx, mvy, bw, bh, x264_weight_none );\
  934. if( rd )\
  935. {\
  936. if( CHROMA444 )\
  937. {\
  938. stride[1][list][i] = bw;\
  939. src[1][list][i] = h->mc.get_ref( pixu_buf[list][i], &stride[1][list][i], &m->p_fref[4],\
  940. m->i_stride[1], mvx, mvy, bw, bh, x264_weight_none );\
  941. stride[2][list][i] = bw;\
  942. src[2][list][i] = h->mc.get_ref( pixv_buf[list][i], &stride[2][list][i], &m->p_fref[8],\
  943. m->i_stride[2], mvx, mvy, bw, bh, x264_weight_none );\
  944. }\
  945. else if( CHROMA_FORMAT )\
  946. h->mc.mc_chroma( pixu_buf[list][i], pixv_buf[list][i], 8, m->p_fref[4], m->i_stride[1],\
  947. mvx, 2*(mvy+mv##list##y_offset)>>chroma_v_shift, bw>>1, bh>>chroma_v_shift );\
  948. }\
  949. }
  950. #define SATD_THRESH(cost) (cost+(cost>>4))
  951. /* Don't unroll the BIME_CACHE loop. I couldn't find any way to force this
  952. * other than making its iteration count not a compile-time constant. */
  953. #define x264_iter_kludge x264_template(iter_kludge)
  954. int x264_iter_kludge = 0;
  955. static ALWAYS_INLINE void me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2, int rd )
  956. {
  957. int x = i8&1;
  958. int y = i8>>1;
  959. int s8 = X264_SCAN8_0 + 2*x + 16*y;
  960. int16_t *cache0_mv = h->mb.cache.mv[0][s8];
  961. int16_t *cache1_mv = h->mb.cache.mv[1][s8];
  962. const int i_pixel = m0->i_pixel;
  963. const int bw = x264_pixel_size[i_pixel].w;
  964. const int bh = x264_pixel_size[i_pixel].h;
  965. ALIGNED_ARRAY_32( pixel, pixy_buf,[2],[9][16*16] );
  966. ALIGNED_ARRAY_32( pixel, pixu_buf,[2],[9][16*16] );
  967. ALIGNED_ARRAY_32( pixel, pixv_buf,[2],[9][16*16] );
  968. pixel *src[3][2][9];
  969. int chromapix = h->luma2chroma_pixel[i_pixel];
  970. int chroma_v_shift = CHROMA_V_SHIFT;
  971. int chroma_x = (8 >> CHROMA_H_SHIFT) * x;
  972. int chroma_y = (8 >> chroma_v_shift) * y;
  973. pixel *pix = &h->mb.pic.p_fdec[0][8*x + 8*y*FDEC_STRIDE];
  974. pixel *pixu = &h->mb.pic.p_fdec[1][chroma_x + chroma_y*FDEC_STRIDE];
  975. pixel *pixv = &h->mb.pic.p_fdec[2][chroma_x + chroma_y*FDEC_STRIDE];
  976. int ref0 = h->mb.cache.ref[0][s8];
  977. int ref1 = h->mb.cache.ref[1][s8];
  978. const int mv0y_offset = chroma_v_shift & MB_INTERLACED & ref0 ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
  979. const int mv1y_offset = chroma_v_shift & MB_INTERLACED & ref1 ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
  980. intptr_t stride[3][2][9];
  981. int bm0x = m0->mv[0];
  982. int bm0y = m0->mv[1];
  983. int bm1x = m1->mv[0];
  984. int bm1y = m1->mv[1];
  985. int bcost = COST_MAX;
  986. int mc_list0 = 1, mc_list1 = 1;
  987. uint64_t bcostrd = COST_MAX64;
  988. uint16_t amvd;
  989. /* each byte of visited represents 8 possible m1y positions, so a 4D array isn't needed */
  990. ALIGNED_ARRAY_64( uint8_t, visited,[8],[8][8] );
  991. /* all permutations of an offset in up to 2 of the dimensions */
  992. ALIGNED_4( static const int8_t dia4d[33][4] ) =
  993. {
  994. {0,0,0,0},
  995. {0,0,0,1}, {0,0,0,-1}, {0,0,1,0}, {0,0,-1,0},
  996. {0,1,0,0}, {0,-1,0,0}, {1,0,0,0}, {-1,0,0,0},
  997. {0,0,1,1}, {0,0,-1,-1},{0,1,1,0}, {0,-1,-1,0},
  998. {1,1,0,0}, {-1,-1,0,0},{1,0,0,1}, {-1,0,0,-1},
  999. {0,1,0,1}, {0,-1,0,-1},{1,0,1,0}, {-1,0,-1,0},
  1000. {0,0,-1,1},{0,0,1,-1}, {0,-1,1,0},{0,1,-1,0},
  1001. {-1,1,0,0},{1,-1,0,0}, {1,0,0,-1},{-1,0,0,1},
  1002. {0,-1,0,1},{0,1,0,-1}, {-1,0,1,0},{1,0,-1,0},
  1003. };
  1004. if( bm0y < h->mb.mv_min_spel[1] + 8 || bm1y < h->mb.mv_min_spel[1] + 8 ||
  1005. bm0y > h->mb.mv_max_spel[1] - 8 || bm1y > h->mb.mv_max_spel[1] - 8 ||
  1006. bm0x < h->mb.mv_min_spel[0] + 8 || bm1x < h->mb.mv_min_spel[0] + 8 ||
  1007. bm0x > h->mb.mv_max_spel[0] - 8 || bm1x > h->mb.mv_max_spel[0] - 8 )
  1008. return;
  1009. if( rd && m0->i_pixel != PIXEL_16x16 && i8 != 0 )
  1010. {
  1011. x264_mb_predict_mv( h, 0, i8<<2, bw>>2, m0->mvp );
  1012. x264_mb_predict_mv( h, 1, i8<<2, bw>>2, m1->mvp );
  1013. }
  1014. const uint16_t *p_cost_m0x = m0->p_cost_mv - m0->mvp[0];
  1015. const uint16_t *p_cost_m0y = m0->p_cost_mv - m0->mvp[1];
  1016. const uint16_t *p_cost_m1x = m1->p_cost_mv - m1->mvp[0];
  1017. const uint16_t *p_cost_m1y = m1->p_cost_mv - m1->mvp[1];
  1018. h->mc.memzero_aligned( visited, sizeof(uint8_t[8][8][8]) );
  1019. for( int pass = 0; pass < 8; pass++ )
  1020. {
  1021. int bestj = 0;
  1022. /* check all mv pairs that differ in at most 2 components from the current mvs. */
  1023. /* doesn't do chroma ME. this probably doesn't matter, as the gains
  1024. * from bidir ME are the same with and without chroma ME. */
  1025. if( mc_list0 )
  1026. for( int j = x264_iter_kludge; j < 9; j++ )
  1027. BIME_CACHE( square1[j][0], square1[j][1], 0 );
  1028. if( mc_list1 )
  1029. for( int j = x264_iter_kludge; j < 9; j++ )
  1030. BIME_CACHE( square1[j][0], square1[j][1], 1 );
  1031. for( int j = !!pass; j < 33; j++ )
  1032. {
  1033. int m0x = dia4d[j][0] + bm0x;
  1034. int m0y = dia4d[j][1] + bm0y;
  1035. int m1x = dia4d[j][2] + bm1x;
  1036. int m1y = dia4d[j][3] + bm1y;
  1037. if( !pass || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) )
  1038. {
  1039. int i0 = 4 + 3*dia4d[j][0] + dia4d[j][1];
  1040. int i1 = 4 + 3*dia4d[j][2] + dia4d[j][3];
  1041. visited[(m0x)&7][(m0y)&7][(m1x)&7] |= (1<<((m1y)&7));
  1042. h->mc.avg[i_pixel]( pix, FDEC_STRIDE, src[0][0][i0], stride[0][0][i0], src[0][1][i1], stride[0][1][i1], i_weight );
  1043. int cost = h->pixf.mbcmp[i_pixel]( m0->p_fenc[0], FENC_STRIDE, pix, FDEC_STRIDE )
  1044. + p_cost_m0x[m0x] + p_cost_m0y[m0y] + p_cost_m1x[m1x] + p_cost_m1y[m1y];
  1045. if( rd )
  1046. {
  1047. if( cost < SATD_THRESH(bcost) )
  1048. {
  1049. bcost = X264_MIN( cost, bcost );
  1050. M32( cache0_mv ) = pack16to32_mask(m0x,m0y);
  1051. M32( cache1_mv ) = pack16to32_mask(m1x,m1y);
  1052. if( CHROMA444 )
  1053. {
  1054. h->mc.avg[i_pixel]( pixu, FDEC_STRIDE, src[1][0][i0], stride[1][0][i0], src[1][1][i1], stride[1][1][i1], i_weight );
  1055. h->mc.avg[i_pixel]( pixv, FDEC_STRIDE, src[2][0][i0], stride[2][0][i0], src[2][1][i1], stride[2][1][i1], i_weight );
  1056. }
  1057. else if( CHROMA_FORMAT )
  1058. {
  1059. h->mc.avg[chromapix]( pixu, FDEC_STRIDE, pixu_buf[0][i0], 8, pixu_buf[1][i1], 8, i_weight );
  1060. h->mc.avg[chromapix]( pixv, FDEC_STRIDE, pixv_buf[0][i0], 8, pixv_buf[1][i1], 8, i_weight );
  1061. }
  1062. uint64_t costrd = x264_rd_cost_part( h, i_lambda2, i8*4, m0->i_pixel );
  1063. COPY2_IF_LT( bcostrd, costrd, bestj, j );
  1064. }
  1065. }
  1066. else
  1067. COPY2_IF_LT( bcost, cost, bestj, j );
  1068. }
  1069. }
  1070. if( !bestj )
  1071. break;
  1072. bm0x += dia4d[bestj][0];
  1073. bm0y += dia4d[bestj][1];
  1074. bm1x += dia4d[bestj][2];
  1075. bm1y += dia4d[bestj][3];
  1076. mc_list0 = M16( &dia4d[bestj][0] );
  1077. mc_list1 = M16( &dia4d[bestj][2] );
  1078. }
  1079. if( rd )
  1080. {
  1081. x264_macroblock_cache_mv ( h, 2*x, 2*y, bw>>2, bh>>2, 0, pack16to32_mask(bm0x, bm0y) );
  1082. amvd = pack8to16( X264_MIN(abs(bm0x - m0->mvp[0]),33), X264_MIN(abs(bm0y - m0->mvp[1]),33) );
  1083. x264_macroblock_cache_mvd( h, 2*x, 2*y, bw>>2, bh>>2, 0, amvd );
  1084. x264_macroblock_cache_mv ( h, 2*x, 2*y, bw>>2, bh>>2, 1, pack16to32_mask(bm1x, bm1y) );
  1085. amvd = pack8to16( X264_MIN(abs(bm1x - m1->mvp[0]),33), X264_MIN(abs(bm1y - m1->mvp[1]),33) );
  1086. x264_macroblock_cache_mvd( h, 2*x, 2*y, bw>>2, bh>>2, 1, amvd );
  1087. }
  1088. m0->mv[0] = bm0x;
  1089. m0->mv[1] = bm0y;
  1090. m1->mv[0] = bm1x;
  1091. m1->mv[1] = bm1y;
  1092. }
  1093. void x264_me_refine_bidir_satd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight )
  1094. {
  1095. me_refine_bidir( h, m0, m1, i_weight, 0, 0, 0 );
  1096. }
  1097. void x264_me_refine_bidir_rd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2 )
  1098. {
  1099. /* Motion compensation is done as part of bidir_rd; don't repeat
  1100. * it in encoding. */
  1101. h->mb.b_skip_mc = 1;
  1102. me_refine_bidir( h, m0, m1, i_weight, i8, i_lambda2, 1 );
  1103. h->mb.b_skip_mc = 0;
  1104. }
  1105. #undef COST_MV_SATD
  1106. #define COST_MV_SATD( mx, my, dst, avoid_mvp ) \
  1107. { \
  1108. if( !avoid_mvp || !(mx == pmx && my == pmy) ) \
  1109. { \
  1110. h->mc.mc_luma( pix, FDEC_STRIDE, m->p_fref, m->i_stride[0], mx, my, bw, bh, &m->weight[0] ); \
  1111. dst = h->pixf.mbcmp[i_pixel]( m->p_fenc[0], FENC_STRIDE, pix, FDEC_STRIDE ) \
  1112. + p_cost_mvx[mx] + p_cost_mvy[my]; \
  1113. COPY1_IF_LT( bsatd, dst ); \
  1114. } \
  1115. else \
  1116. dst = COST_MAX; \
  1117. }
  1118. #define COST_MV_RD( mx, my, satd, do_dir, mdir ) \
  1119. { \
  1120. if( satd <= SATD_THRESH(bsatd) ) \
  1121. { \
  1122. uint64_t cost; \
  1123. M32( cache_mv ) = pack16to32_mask(mx,my); \
  1124. if( CHROMA444 ) \
  1125. { \
  1126. h->mc.mc_luma( pixu, FDEC_STRIDE, &m->p_fref[4], m->i_stride[1], mx, my, bw, bh, &m->weight[1] ); \
  1127. h->mc.mc_luma( pixv, FDEC_STRIDE, &m->p_fref[8], m->i_stride[2], mx, my, bw, bh, &m->weight[2] ); \
  1128. } \
  1129. else if( CHROMA_FORMAT && m->i_pixel <= PIXEL_8x8 ) \
  1130. { \
  1131. h->mc.mc_chroma( pixu, pixv, FDEC_STRIDE, m->p_fref[4], m->i_stride[1], \
  1132. mx, 2*(my+mvy_offset)>>chroma_v_shift, bw>>1, bh>>chroma_v_shift ); \
  1133. if( m->weight[1].weightfn ) \
  1134. m->weight[1].weightfn[bw>>3]( pixu, FDEC_STRIDE, pixu, FDEC_STRIDE, &m->weight[1], bh>>chroma_v_shift ); \
  1135. if( m->weight[2].weightfn ) \
  1136. m->weight[2].weightfn[bw>>3]( pixv, FDEC_STRIDE, pixv, FDEC_STRIDE, &m->weight[2], bh>>chroma_v_shift ); \
  1137. } \
  1138. cost = x264_rd_cost_part( h, i_lambda2, i4, m->i_pixel ); \
  1139. COPY4_IF_LT( bcost, cost, bmx, mx, bmy, my, dir, do_dir?mdir:dir ); \
  1140. } \
  1141. }
  1142. void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i4, int i_list )
  1143. {
  1144. int16_t *cache_mv = h->mb.cache.mv[i_list][x264_scan8[i4]];
  1145. const uint16_t *p_cost_mvx, *p_cost_mvy;
  1146. const int bw = x264_pixel_size[m->i_pixel].w;
  1147. const int bh = x264_pixel_size[m->i_pixel].h;
  1148. const int i_pixel = m->i_pixel;
  1149. int chroma_v_shift = CHROMA_V_SHIFT;
  1150. int mvy_offset = chroma_v_shift & MB_INTERLACED & m->i_ref ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
  1151. uint64_t bcost = COST_MAX64;
  1152. int bmx = m->mv[0];
  1153. int bmy = m->mv[1];
  1154. int omx, omy, pmx, pmy;
  1155. int satd, bsatd;
  1156. int dir = -2;
  1157. int i8 = i4>>2;
  1158. uint16_t amvd;
  1159. pixel *pix = &h->mb.pic.p_fdec[0][block_idx_xy_fdec[i4]];
  1160. pixel *pixu, *pixv;
  1161. if( CHROMA444 )
  1162. {
  1163. pixu = &h->mb.pic.p_fdec[1][block_idx_xy_fdec[i4]];
  1164. pixv = &h->mb.pic.p_fdec[2][block_idx_xy_fdec[i4]];
  1165. }
  1166. else
  1167. {
  1168. pixu = &h->mb.pic.p_fdec[1][(i8>>1)*(8*FDEC_STRIDE>>chroma_v_shift)+(i8&1)*4];
  1169. pixv = &h->mb.pic.p_fdec[2][(i8>>1)*(8*FDEC_STRIDE>>chroma_v_shift)+(i8&1)*4];
  1170. }
  1171. h->mb.b_skip_mc = 1;
  1172. if( m->i_pixel != PIXEL_16x16 && i4 != 0 )
  1173. x264_mb_predict_mv( h, i_list, i4, bw>>2, m->mvp );
  1174. pmx = m->mvp[0];
  1175. pmy = m->mvp[1];
  1176. p_cost_mvx = m->p_cost_mv - pmx;
  1177. p_cost_mvy = m->p_cost_mv - pmy;
  1178. COST_MV_SATD( bmx, bmy, bsatd, 0 );
  1179. if( m->i_pixel != PIXEL_16x16 )
  1180. COST_MV_RD( bmx, bmy, 0, 0, 0 )
  1181. else
  1182. bcost = m->cost;
  1183. /* check the predicted mv */
  1184. if( (bmx != pmx || bmy != pmy)
  1185. && pmx >= h->mb.mv_min_spel[0] && pmx <= h->mb.mv_max_spel[0]
  1186. && pmy >= h->mb.mv_min_spel[1] && pmy <= h->mb.mv_max_spel[1] )
  1187. {
  1188. COST_MV_SATD( pmx, pmy, satd, 0 );
  1189. COST_MV_RD ( pmx, pmy, satd, 0, 0 );
  1190. /* The hex motion search is guaranteed to not repeat the center candidate,
  1191. * so if pmv is chosen, set the "MV to avoid checking" to bmv instead. */
  1192. if( bmx == pmx && bmy == pmy )
  1193. {
  1194. pmx = m->mv[0];
  1195. pmy = m->mv[1];
  1196. }
  1197. }
  1198. if( bmy < h->mb.mv_min_spel[1] + 3 || bmy > h->mb.mv_max_spel[1] - 3 ||
  1199. bmx < h->mb.mv_min_spel[0] + 3 || bmx > h->mb.mv_max_spel[0] - 3 )
  1200. {
  1201. h->mb.b_skip_mc = 0;
  1202. return;
  1203. }
  1204. /* subpel hex search, same pattern as ME HEX. */
  1205. dir = -2;
  1206. omx = bmx;
  1207. omy = bmy;
  1208. for( int j = 0; j < 6; j++ )
  1209. {
  1210. COST_MV_SATD( omx + hex2[j+1][0], omy + hex2[j+1][1], satd, 1 );
  1211. COST_MV_RD ( omx + hex2[j+1][0], omy + hex2[j+1][1], satd, 1, j );
  1212. }
  1213. if( dir != -2 )
  1214. {
  1215. /* half hexagon, not overlapping the previous iteration */
  1216. for( int i = 1; i < 10; i++ )
  1217. {
  1218. const int odir = mod6m1[dir+1];
  1219. if( bmy < h->mb.mv_min_spel[1] + 3 ||
  1220. bmy > h->mb.mv_max_spel[1] - 3 )
  1221. break;
  1222. dir = -2;
  1223. omx = bmx;
  1224. omy = bmy;
  1225. for( int j = 0; j < 3; j++ )
  1226. {
  1227. COST_MV_SATD( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satd, 1 );
  1228. COST_MV_RD ( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satd, 1, odir-1+j );
  1229. }
  1230. if( dir == -2 )
  1231. break;
  1232. }
  1233. }
  1234. /* square refine, same pattern as ME HEX. */
  1235. omx = bmx;
  1236. omy = bmy;
  1237. for( int i = 0; i < 8; i++ )
  1238. {
  1239. COST_MV_SATD( omx + square1[i+1][0], omy + square1[i+1][1], satd, 1 );
  1240. COST_MV_RD ( omx + square1[i+1][0], omy + square1[i+1][1], satd, 0, 0 );
  1241. }
  1242. m->cost = bcost;
  1243. m->mv[0] = bmx;
  1244. m->mv[1] = bmy;
  1245. x264_macroblock_cache_mv ( h, block_idx_x[i4], block_idx_y[i4], bw>>2, bh>>2, i_list, pack16to32_mask(bmx, bmy) );
  1246. amvd = pack8to16( X264_MIN(abs(bmx - m->mvp[0]),66), X264_MIN(abs(bmy - m->mvp[1]),66) );
  1247. x264_macroblock_cache_mvd( h, block_idx_x[i4], block_idx_y[i4], bw>>2, bh>>2, i_list, amvd );
  1248. h->mb.b_skip_mc = 0;
  1249. }