mvpred.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*****************************************************************************
  2. * mvpred.c: motion vector prediction
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Loren Merritt <lorenm@u.washington.edu>
  7. * Fiona Glaser <fiona@x264.com>
  8. * Laurent Aimar <fenrir@via.ecp.fr>
  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.h"
  28. void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
  29. {
  30. const int i8 = x264_scan8[idx];
  31. const int i_ref= h->mb.cache.ref[i_list][i8];
  32. int i_refa = h->mb.cache.ref[i_list][i8 - 1];
  33. int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
  34. int i_refb = h->mb.cache.ref[i_list][i8 - 8];
  35. int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
  36. int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width];
  37. int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
  38. // Partitions not yet reached in scan order are unavailable.
  39. if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
  40. {
  41. i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
  42. mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
  43. if( SLICE_MBAFF
  44. && h->mb.cache.ref[i_list][x264_scan8[0]-1] != -2
  45. && MB_INTERLACED != h->mb.field[h->mb.i_mb_left_xy[0]] )
  46. {
  47. if( idx == 2 )
  48. {
  49. mv_c = h->mb.cache.topright_mv[i_list][0];
  50. i_refc = h->mb.cache.topright_ref[i_list][0];
  51. }
  52. else if( idx == 8 )
  53. {
  54. mv_c = h->mb.cache.topright_mv[i_list][1];
  55. i_refc = h->mb.cache.topright_ref[i_list][1];
  56. }
  57. else if( idx == 10 )
  58. {
  59. mv_c = h->mb.cache.topright_mv[i_list][2];
  60. i_refc = h->mb.cache.topright_ref[i_list][2];
  61. }
  62. }
  63. }
  64. if( h->mb.i_partition == D_16x8 )
  65. {
  66. if( idx == 0 )
  67. {
  68. if( i_refb == i_ref )
  69. {
  70. CP32( mvp, mv_b );
  71. return;
  72. }
  73. }
  74. else
  75. {
  76. if( i_refa == i_ref )
  77. {
  78. CP32( mvp, mv_a );
  79. return;
  80. }
  81. }
  82. }
  83. else if( h->mb.i_partition == D_8x16 )
  84. {
  85. if( idx == 0 )
  86. {
  87. if( i_refa == i_ref )
  88. {
  89. CP32( mvp, mv_a );
  90. return;
  91. }
  92. }
  93. else
  94. {
  95. if( i_refc == i_ref )
  96. {
  97. CP32( mvp, mv_c );
  98. return;
  99. }
  100. }
  101. }
  102. int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
  103. if( i_count > 1 )
  104. {
  105. median:
  106. x264_median_mv( mvp, mv_a, mv_b, mv_c );
  107. }
  108. else if( i_count == 1 )
  109. {
  110. if( i_refa == i_ref )
  111. CP32( mvp, mv_a );
  112. else if( i_refb == i_ref )
  113. CP32( mvp, mv_b );
  114. else
  115. CP32( mvp, mv_c );
  116. }
  117. else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
  118. CP32( mvp, mv_a );
  119. else
  120. goto median;
  121. }
  122. void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
  123. {
  124. int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
  125. int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
  126. int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
  127. int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
  128. int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
  129. int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
  130. if( i_refc == -2 )
  131. {
  132. i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
  133. mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
  134. }
  135. int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
  136. if( i_count > 1 )
  137. {
  138. median:
  139. x264_median_mv( mvp, mv_a, mv_b, mv_c );
  140. }
  141. else if( i_count == 1 )
  142. {
  143. if( i_refa == i_ref )
  144. CP32( mvp, mv_a );
  145. else if( i_refb == i_ref )
  146. CP32( mvp, mv_b );
  147. else
  148. CP32( mvp, mv_c );
  149. }
  150. else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
  151. CP32( mvp, mv_a );
  152. else
  153. goto median;
  154. }
  155. void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
  156. {
  157. int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
  158. int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
  159. int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
  160. int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
  161. if( i_refa == -2 || i_refb == -2 ||
  162. !( i_refa | M32( mv_a ) ) ||
  163. !( i_refb | M32( mv_b ) ) )
  164. {
  165. M32( mv ) = 0;
  166. }
  167. else
  168. x264_mb_predict_mv_16x16( h, 0, 0, mv );
  169. }
  170. static int mb_predict_mv_direct16x16_temporal( x264_t *h )
  171. {
  172. int mb_x = h->mb.i_mb_x;
  173. int mb_y = h->mb.i_mb_y;
  174. int mb_xy = h->mb.i_mb_xy;
  175. int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] };
  176. int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] };
  177. int preshift = MB_INTERLACED;
  178. int postshift = MB_INTERLACED;
  179. int offset = 1;
  180. int yshift = 1;
  181. h->mb.i_partition = partition_col[0];
  182. if( PARAM_INTERLACED && h->fref[1][0]->field[mb_xy] != MB_INTERLACED )
  183. {
  184. if( MB_INTERLACED )
  185. {
  186. mb_y = h->mb.i_mb_y&~1;
  187. mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
  188. type_col[0] = h->fref[1][0]->mb_type[mb_xy];
  189. type_col[1] = h->fref[1][0]->mb_type[mb_xy + h->mb.i_mb_stride];
  190. partition_col[0] = h->fref[1][0]->mb_partition[mb_xy];
  191. partition_col[1] = h->fref[1][0]->mb_partition[mb_xy + h->mb.i_mb_stride];
  192. preshift = 0;
  193. yshift = 0;
  194. if( (IS_INTRA(type_col[0]) || partition_col[0] == D_16x16) &&
  195. (IS_INTRA(type_col[1]) || partition_col[1] == D_16x16) &&
  196. partition_col[0] != D_8x8 )
  197. h->mb.i_partition = D_16x8;
  198. else
  199. h->mb.i_partition = D_8x8;
  200. }
  201. else
  202. {
  203. int cur_poc = h->fdec->i_poc + h->fdec->i_delta_poc[MB_INTERLACED&h->mb.i_mb_y&1];
  204. int col_parity = abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[0] - cur_poc)
  205. >= abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[1] - cur_poc);
  206. mb_y = (h->mb.i_mb_y&~1) + col_parity;
  207. mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
  208. type_col[0] = type_col[1] = h->fref[1][0]->mb_type[mb_xy];
  209. partition_col[0] = partition_col[1] = h->fref[1][0]->mb_partition[mb_xy];
  210. preshift = 1;
  211. yshift = 2;
  212. h->mb.i_partition = partition_col[0];
  213. }
  214. offset = 0;
  215. }
  216. int i_mb_4x4 = 16 * h->mb.i_mb_stride * mb_y + 4 * mb_x;
  217. int i_mb_8x8 = 4 * h->mb.i_mb_stride * mb_y + 2 * mb_x;
  218. x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
  219. /* Don't do any checks other than the ones we have to, based
  220. * on the size of the colocated partitions.
  221. * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
  222. int max_i8 = (D_16x16 - h->mb.i_partition) + 1;
  223. int step = (h->mb.i_partition == D_16x8) + 1;
  224. int width = 4 >> ((D_16x16 - h->mb.i_partition)&1);
  225. int height = 4 >> ((D_16x16 - h->mb.i_partition)>>1);
  226. for( int i8 = 0; i8 < max_i8; i8 += step )
  227. {
  228. int x8 = i8&1;
  229. int y8 = i8>>1;
  230. int ypart = (SLICE_MBAFF && h->fref[1][0]->field[mb_xy] != MB_INTERLACED) ?
  231. MB_INTERLACED ? y8*6 : 2*(h->mb.i_mb_y&1) + y8 :
  232. 3*y8;
  233. if( IS_INTRA( type_col[y8] ) )
  234. {
  235. x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, 0 );
  236. x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 );
  237. x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 );
  238. continue;
  239. }
  240. int i_part_8x8 = i_mb_8x8 + x8 + (ypart>>1) * h->mb.i_b8_stride;
  241. int i_ref1_ref = h->fref[1][0]->ref[0][i_part_8x8];
  242. int i_ref = (map_col_to_list0(i_ref1_ref>>preshift) << postshift) + (offset&i_ref1_ref&MB_INTERLACED);
  243. if( i_ref >= 0 )
  244. {
  245. int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
  246. int16_t *mv_col = h->fref[1][0]->mv[0][i_mb_4x4 + 3*x8 + ypart * h->mb.i_b4_stride];
  247. int16_t mv_y = (mv_col[1]<<yshift)/2;
  248. int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
  249. int l0y = ( dist_scale_factor * mv_y + 128 ) >> 8;
  250. if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_y > h->mb.mv_max_spel[1]) )
  251. return 0;
  252. x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, i_ref );
  253. x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, pack16to32_mask(l0x, l0y) );
  254. x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_y) );
  255. }
  256. else
  257. {
  258. /* the collocated ref isn't in the current list0 */
  259. /* FIXME: we might still be able to use direct_8x8 on some partitions */
  260. /* FIXME: with B-pyramid + extensive ref list reordering
  261. * (not currently used), we would also have to check
  262. * l1mv1 like in spatial mode */
  263. return 0;
  264. }
  265. }
  266. return 1;
  267. }
  268. static ALWAYS_INLINE int mb_predict_mv_direct16x16_spatial( x264_t *h, int b_interlaced )
  269. {
  270. int8_t ref[2];
  271. ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
  272. for( int i_list = 0; i_list < 2; i_list++ )
  273. {
  274. int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
  275. int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
  276. int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
  277. int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
  278. int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
  279. int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
  280. if( i_refc == -2 )
  281. {
  282. i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
  283. mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
  284. }
  285. int i_ref = X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc );
  286. if( i_ref < 0 )
  287. {
  288. i_ref = -1;
  289. M32( mv[i_list] ) = 0;
  290. }
  291. else
  292. {
  293. /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases
  294. * not relevant to spatial direct. */
  295. int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
  296. if( i_count > 1 )
  297. x264_median_mv( mv[i_list], mv_a, mv_b, mv_c );
  298. else
  299. {
  300. if( i_refa == i_ref )
  301. CP32( mv[i_list], mv_a );
  302. else if( i_refb == i_ref )
  303. CP32( mv[i_list], mv_b );
  304. else
  305. CP32( mv[i_list], mv_c );
  306. }
  307. }
  308. x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref );
  309. x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] );
  310. ref[i_list] = i_ref;
  311. }
  312. int mb_x = h->mb.i_mb_x;
  313. int mb_y = h->mb.i_mb_y;
  314. int mb_xy = h->mb.i_mb_xy;
  315. int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] };
  316. int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] };
  317. h->mb.i_partition = partition_col[0];
  318. if( b_interlaced && h->fref[1][0]->field[mb_xy] != MB_INTERLACED )
  319. {
  320. if( MB_INTERLACED )
  321. {
  322. mb_y = h->mb.i_mb_y&~1;
  323. mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
  324. type_col[0] = h->fref[1][0]->mb_type[mb_xy];
  325. type_col[1] = h->fref[1][0]->mb_type[mb_xy + h->mb.i_mb_stride];
  326. partition_col[0] = h->fref[1][0]->mb_partition[mb_xy];
  327. partition_col[1] = h->fref[1][0]->mb_partition[mb_xy + h->mb.i_mb_stride];
  328. if( (IS_INTRA(type_col[0]) || partition_col[0] == D_16x16) &&
  329. (IS_INTRA(type_col[1]) || partition_col[1] == D_16x16) &&
  330. partition_col[0] != D_8x8 )
  331. h->mb.i_partition = D_16x8;
  332. else
  333. h->mb.i_partition = D_8x8;
  334. }
  335. else
  336. {
  337. int cur_poc = h->fdec->i_poc + h->fdec->i_delta_poc[MB_INTERLACED&h->mb.i_mb_y&1];
  338. int col_parity = abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[0] - cur_poc)
  339. >= abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[1] - cur_poc);
  340. mb_y = (h->mb.i_mb_y&~1) + col_parity;
  341. mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
  342. type_col[0] = type_col[1] = h->fref[1][0]->mb_type[mb_xy];
  343. partition_col[0] = partition_col[1] = h->fref[1][0]->mb_partition[mb_xy];
  344. h->mb.i_partition = partition_col[0];
  345. }
  346. }
  347. int i_mb_4x4 = b_interlaced ? 4 * (h->mb.i_b4_stride*mb_y + mb_x) : h->mb.i_b4_xy;
  348. int i_mb_8x8 = b_interlaced ? 2 * (h->mb.i_b8_stride*mb_y + mb_x) : h->mb.i_b8_xy;
  349. int8_t *l1ref0 = &h->fref[1][0]->ref[0][i_mb_8x8];
  350. int8_t *l1ref1 = &h->fref[1][0]->ref[1][i_mb_8x8];
  351. int16_t (*l1mv[2])[2] = { (int16_t (*)[2]) &h->fref[1][0]->mv[0][i_mb_4x4],
  352. (int16_t (*)[2]) &h->fref[1][0]->mv[1][i_mb_4x4] };
  353. if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */
  354. {
  355. x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
  356. x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
  357. return 1;
  358. }
  359. if( h->param.i_threads > 1
  360. && ( mv[0][1] > h->mb.mv_max_spel[1]
  361. || mv[1][1] > h->mb.mv_max_spel[1] ) )
  362. {
  363. #if 0
  364. fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
  365. mv[0][0], mv[0][1], mv[1][0], mv[1][1],
  366. h->mb.mv_max_spel[1]);
  367. #endif
  368. return 0;
  369. }
  370. if( !M64( mv ) || (!b_interlaced && IS_INTRA( type_col[0] )) || (ref[0]&&ref[1]) )
  371. return 1;
  372. /* Don't do any checks other than the ones we have to, based
  373. * on the size of the colocated partitions.
  374. * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
  375. int max_i8 = (D_16x16 - h->mb.i_partition) + 1;
  376. int step = (h->mb.i_partition == D_16x8) + 1;
  377. int width = 4 >> ((D_16x16 - h->mb.i_partition)&1);
  378. int height = 4 >> ((D_16x16 - h->mb.i_partition)>>1);
  379. /* col_zero_flag */
  380. for( int i8 = 0; i8 < max_i8; i8 += step )
  381. {
  382. const int x8 = i8&1;
  383. const int y8 = i8>>1;
  384. int ypart = (b_interlaced && h->fref[1][0]->field[mb_xy] != MB_INTERLACED) ?
  385. MB_INTERLACED ? y8*6 : 2*(h->mb.i_mb_y&1) + y8 :
  386. 3*y8;
  387. int o8 = x8 + (ypart>>1) * h->mb.i_b8_stride;
  388. int o4 = 3*x8 + ypart * h->mb.i_b4_stride;
  389. if( b_interlaced && IS_INTRA( type_col[y8] ) )
  390. continue;
  391. int idx;
  392. if( l1ref0[o8] == 0 )
  393. idx = 0;
  394. else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
  395. idx = 1;
  396. else
  397. continue;
  398. if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 )
  399. {
  400. if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 );
  401. if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 );
  402. }
  403. }
  404. return 1;
  405. }
  406. static int mb_predict_mv_direct16x16_spatial_interlaced( x264_t *h )
  407. {
  408. return mb_predict_mv_direct16x16_spatial( h, 1 );
  409. }
  410. static int mb_predict_mv_direct16x16_spatial_progressive( x264_t *h )
  411. {
  412. return mb_predict_mv_direct16x16_spatial( h, 0 );
  413. }
  414. int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
  415. {
  416. int b_available;
  417. if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
  418. return 0;
  419. else if( h->sh.b_direct_spatial_mv_pred )
  420. {
  421. if( SLICE_MBAFF )
  422. b_available = mb_predict_mv_direct16x16_spatial_interlaced( h );
  423. else
  424. b_available = mb_predict_mv_direct16x16_spatial_progressive( h );
  425. }
  426. else
  427. b_available = mb_predict_mv_direct16x16_temporal( h );
  428. if( b_changed != NULL && b_available )
  429. {
  430. int changed;
  431. changed = M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][x264_scan8[0]] );
  432. changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][x264_scan8[0]] );
  433. changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][x264_scan8[0]];
  434. changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][x264_scan8[0]];
  435. if( !changed && h->mb.i_partition != D_16x16 )
  436. {
  437. changed |= M32( h->mb.cache.direct_mv[0][3] ) ^ M32( h->mb.cache.mv[0][x264_scan8[12]] );
  438. changed |= M32( h->mb.cache.direct_mv[1][3] ) ^ M32( h->mb.cache.mv[1][x264_scan8[12]] );
  439. changed |= h->mb.cache.direct_ref[0][3] ^ h->mb.cache.ref[0][x264_scan8[12]];
  440. changed |= h->mb.cache.direct_ref[1][3] ^ h->mb.cache.ref[1][x264_scan8[12]];
  441. }
  442. if( !changed && h->mb.i_partition == D_8x8 )
  443. {
  444. changed |= M32( h->mb.cache.direct_mv[0][1] ) ^ M32( h->mb.cache.mv[0][x264_scan8[4]] );
  445. changed |= M32( h->mb.cache.direct_mv[1][1] ) ^ M32( h->mb.cache.mv[1][x264_scan8[4]] );
  446. changed |= M32( h->mb.cache.direct_mv[0][2] ) ^ M32( h->mb.cache.mv[0][x264_scan8[8]] );
  447. changed |= M32( h->mb.cache.direct_mv[1][2] ) ^ M32( h->mb.cache.mv[1][x264_scan8[8]] );
  448. changed |= h->mb.cache.direct_ref[0][1] ^ h->mb.cache.ref[0][x264_scan8[4]];
  449. changed |= h->mb.cache.direct_ref[1][1] ^ h->mb.cache.ref[1][x264_scan8[4]];
  450. changed |= h->mb.cache.direct_ref[0][2] ^ h->mb.cache.ref[0][x264_scan8[8]];
  451. changed |= h->mb.cache.direct_ref[1][2] ^ h->mb.cache.ref[1][x264_scan8[8]];
  452. }
  453. *b_changed = changed;
  454. if( !changed )
  455. return b_available;
  456. }
  457. /* cache ref & mv */
  458. if( b_available )
  459. for( int l = 0; l < 2; l++ )
  460. {
  461. CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
  462. CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
  463. CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
  464. CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
  465. h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
  466. h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
  467. h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
  468. h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
  469. h->mb.cache.direct_partition = h->mb.i_partition;
  470. }
  471. return b_available;
  472. }
  473. /* This just improves encoder performance, it's not part of the spec */
  474. void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
  475. {
  476. int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
  477. int i = 0;
  478. #define SET_MVP(mvp) \
  479. { \
  480. CP32( mvc[i], mvp ); \
  481. i++; \
  482. }
  483. #define SET_IMVP(xy) \
  484. if( xy >= 0 ) \
  485. { \
  486. int shift = 1 + MB_INTERLACED - h->mb.field[xy]; \
  487. int16_t *mvp = h->mb.mvr[i_list][i_ref<<1>>shift][xy]; \
  488. mvc[i][0] = mvp[0]; \
  489. mvc[i][1] = mvp[1]<<1>>shift; \
  490. i++; \
  491. }
  492. /* b_direct */
  493. if( h->sh.i_type == SLICE_TYPE_B
  494. && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
  495. {
  496. SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
  497. }
  498. if( i_ref == 0 && h->frames.b_have_lowres )
  499. {
  500. int idx = i_list ? h->fref[1][0]->i_frame-h->fenc->i_frame-1
  501. : h->fenc->i_frame-h->fref[0][0]->i_frame-1;
  502. if( idx <= h->param.i_bframe )
  503. {
  504. int16_t (*lowres_mv)[2] = h->fenc->lowres_mvs[i_list][idx];
  505. if( lowres_mv[0][0] != 0x7fff )
  506. {
  507. M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
  508. i++;
  509. }
  510. }
  511. }
  512. /* spatial predictors */
  513. if( SLICE_MBAFF )
  514. {
  515. SET_IMVP( h->mb.i_mb_left_xy[0] );
  516. SET_IMVP( h->mb.i_mb_top_xy );
  517. SET_IMVP( h->mb.i_mb_topleft_xy );
  518. SET_IMVP( h->mb.i_mb_topright_xy );
  519. }
  520. else
  521. {
  522. SET_MVP( mvr[h->mb.i_mb_left_xy[0]] );
  523. SET_MVP( mvr[h->mb.i_mb_top_xy] );
  524. SET_MVP( mvr[h->mb.i_mb_topleft_xy] );
  525. SET_MVP( mvr[h->mb.i_mb_topright_xy] );
  526. }
  527. #undef SET_IMVP
  528. #undef SET_MVP
  529. /* temporal predictors */
  530. if( h->fref[0][0]->i_ref[0] > 0 )
  531. {
  532. x264_frame_t *l0 = h->fref[0][0];
  533. int field = h->mb.i_mb_y&1;
  534. int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
  535. int refpoc = h->fref[i_list][i_ref>>SLICE_MBAFF]->i_poc;
  536. refpoc += l0->i_delta_poc[field^(i_ref&1)];
  537. #define SET_TMVP( dx, dy ) \
  538. { \
  539. int mb_index = h->mb.i_mb_xy + dx + dy*h->mb.i_mb_stride; \
  540. int scale = (curpoc - refpoc) * l0->inv_ref_poc[MB_INTERLACED&field]; \
  541. mvc[i][0] = (l0->mv16x16[mb_index][0]*scale + 128) >> 8; \
  542. mvc[i][1] = (l0->mv16x16[mb_index][1]*scale + 128) >> 8; \
  543. i++; \
  544. }
  545. SET_TMVP(0,0);
  546. if( h->mb.i_mb_x < h->mb.i_mb_width-1 )
  547. SET_TMVP(1,0);
  548. if( h->mb.i_mb_y < h->mb.i_mb_height-1 )
  549. SET_TMVP(0,1);
  550. #undef SET_TMVP
  551. }
  552. *i_mvc = i;
  553. }