macroblock.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /*****************************************************************************
  2. * macroblock.c: macroblock common functions
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Fiona Glaser <fiona@x264.com>
  7. * Laurent Aimar <fenrir@via.ecp.fr>
  8. * Loren Merritt <lorenm@u.washington.edu>
  9. * Henrik Gramner <henrik@gramner.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  24. *
  25. * This program is also available under a commercial proprietary license.
  26. * For more information, contact us at licensing@x264.com.
  27. *****************************************************************************/
  28. #include "common.h"
  29. #define MC_LUMA(list,p) \
  30. h->mc.mc_luma( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
  31. &h->mb.pic.p_fref[list][i_ref][p*4], h->mb.pic.i_stride[p], \
  32. mvx, mvy, 4*width, 4*height, \
  33. list ? x264_weight_none : &h->sh.weight[i_ref][p] );
  34. static NOINLINE void mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
  35. {
  36. int i8 = x264_scan8[0]+x+8*y;
  37. int i_ref = h->mb.cache.ref[0][i8];
  38. int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
  39. int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
  40. MC_LUMA( 0, 0 );
  41. if( CHROMA444 )
  42. {
  43. MC_LUMA( 0, 1 );
  44. MC_LUMA( 0, 2 );
  45. }
  46. else if( CHROMA_FORMAT )
  47. {
  48. int v_shift = CHROMA_V_SHIFT;
  49. // Chroma in 4:2:0 is offset if MCing from a field of opposite parity
  50. if( v_shift & MB_INTERLACED & i_ref )
  51. mvy += (h->mb.i_mb_y & 1)*4 - 2;
  52. int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
  53. height = 4*height >> v_shift;
  54. h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
  55. &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
  56. h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
  57. mvx, 2*mvy>>v_shift, 2*width, height );
  58. if( h->sh.weight[i_ref][1].weightfn )
  59. h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
  60. &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
  61. &h->sh.weight[i_ref][1], height );
  62. if( h->sh.weight[i_ref][2].weightfn )
  63. h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
  64. &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
  65. &h->sh.weight[i_ref][2], height );
  66. }
  67. }
  68. static NOINLINE void mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
  69. {
  70. int i8 = x264_scan8[0]+x+8*y;
  71. int i_ref = h->mb.cache.ref[1][i8];
  72. int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
  73. int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
  74. MC_LUMA( 1, 0 );
  75. if( CHROMA444 )
  76. {
  77. MC_LUMA( 1, 1 );
  78. MC_LUMA( 1, 2 );
  79. }
  80. else if( CHROMA_FORMAT )
  81. {
  82. int v_shift = CHROMA_V_SHIFT;
  83. if( v_shift & MB_INTERLACED & i_ref )
  84. mvy += (h->mb.i_mb_y & 1)*4 - 2;
  85. int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
  86. h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
  87. &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
  88. h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
  89. mvx, 2*mvy>>v_shift, 2*width, 4*height>>v_shift );
  90. }
  91. }
  92. #define MC_LUMA_BI(p) \
  93. src0 = h->mc.get_ref( tmp0, &i_stride0, &h->mb.pic.p_fref[0][i_ref0][p*4], h->mb.pic.i_stride[p], \
  94. mvx0, mvy0, 4*width, 4*height, x264_weight_none ); \
  95. src1 = h->mc.get_ref( tmp1, &i_stride1, &h->mb.pic.p_fref[1][i_ref1][p*4], h->mb.pic.i_stride[p], \
  96. mvx1, mvy1, 4*width, 4*height, x264_weight_none ); \
  97. h->mc.avg[i_mode]( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
  98. src0, i_stride0, src1, i_stride1, weight );
  99. static NOINLINE void mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
  100. {
  101. int i8 = x264_scan8[0]+x+8*y;
  102. int i_ref0 = h->mb.cache.ref[0][i8];
  103. int i_ref1 = h->mb.cache.ref[1][i8];
  104. int weight = h->mb.bipred_weight[i_ref0][i_ref1];
  105. int mvx0 = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
  106. int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
  107. int mvy0 = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
  108. int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
  109. int i_mode = x264_size2pixel[height][width];
  110. intptr_t i_stride0 = 16, i_stride1 = 16;
  111. ALIGNED_ARRAY_32( pixel, tmp0,[16*16] );
  112. ALIGNED_ARRAY_32( pixel, tmp1,[16*16] );
  113. pixel *src0, *src1;
  114. MC_LUMA_BI( 0 );
  115. if( CHROMA444 )
  116. {
  117. MC_LUMA_BI( 1 );
  118. MC_LUMA_BI( 2 );
  119. }
  120. else if( CHROMA_FORMAT )
  121. {
  122. int v_shift = CHROMA_V_SHIFT;
  123. if( v_shift & MB_INTERLACED & i_ref0 )
  124. mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
  125. if( v_shift & MB_INTERLACED & i_ref1 )
  126. mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
  127. h->mc.mc_chroma( tmp0, tmp0+8, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
  128. mvx0, 2*mvy0>>v_shift, 2*width, 4*height>>v_shift );
  129. h->mc.mc_chroma( tmp1, tmp1+8, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
  130. mvx1, 2*mvy1>>v_shift, 2*width, 4*height>>v_shift );
  131. int chromapix = h->luma2chroma_pixel[i_mode];
  132. int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
  133. h->mc.avg[chromapix]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
  134. h->mc.avg[chromapix]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE, tmp0+8, 16, tmp1+8, 16, weight );
  135. }
  136. }
  137. #undef MC_LUMA
  138. #undef MC_LUMA_BI
  139. void x264_mb_mc_8x8( x264_t *h, int i8 )
  140. {
  141. int x = 2*(i8&1);
  142. int y = 2*(i8>>1);
  143. if( h->sh.i_type == SLICE_TYPE_P )
  144. {
  145. switch( h->mb.i_sub_partition[i8] )
  146. {
  147. case D_L0_8x8:
  148. mb_mc_0xywh( h, x, y, 2, 2 );
  149. break;
  150. case D_L0_8x4:
  151. mb_mc_0xywh( h, x, y+0, 2, 1 );
  152. mb_mc_0xywh( h, x, y+1, 2, 1 );
  153. break;
  154. case D_L0_4x8:
  155. mb_mc_0xywh( h, x+0, y, 1, 2 );
  156. mb_mc_0xywh( h, x+1, y, 1, 2 );
  157. break;
  158. case D_L0_4x4:
  159. mb_mc_0xywh( h, x+0, y+0, 1, 1 );
  160. mb_mc_0xywh( h, x+1, y+0, 1, 1 );
  161. mb_mc_0xywh( h, x+0, y+1, 1, 1 );
  162. mb_mc_0xywh( h, x+1, y+1, 1, 1 );
  163. break;
  164. }
  165. }
  166. else
  167. {
  168. int scan8 = x264_scan8[0] + x + 8*y;
  169. if( h->mb.cache.ref[0][scan8] >= 0 )
  170. if( h->mb.cache.ref[1][scan8] >= 0 )
  171. mb_mc_01xywh( h, x, y, 2, 2 );
  172. else
  173. mb_mc_0xywh( h, x, y, 2, 2 );
  174. else
  175. mb_mc_1xywh( h, x, y, 2, 2 );
  176. }
  177. }
  178. void x264_mb_mc( x264_t *h )
  179. {
  180. if( h->mb.i_partition == D_8x8 )
  181. {
  182. for( int i = 0; i < 4; i++ )
  183. x264_mb_mc_8x8( h, i );
  184. }
  185. else
  186. {
  187. int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
  188. int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
  189. int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
  190. int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
  191. if( h->mb.i_partition == D_16x16 )
  192. {
  193. if( ref0a >= 0 )
  194. if( ref1a >= 0 ) mb_mc_01xywh( h, 0, 0, 4, 4 );
  195. else mb_mc_0xywh ( h, 0, 0, 4, 4 );
  196. else mb_mc_1xywh ( h, 0, 0, 4, 4 );
  197. }
  198. else if( h->mb.i_partition == D_16x8 )
  199. {
  200. if( ref0a >= 0 )
  201. if( ref1a >= 0 ) mb_mc_01xywh( h, 0, 0, 4, 2 );
  202. else mb_mc_0xywh ( h, 0, 0, 4, 2 );
  203. else mb_mc_1xywh ( h, 0, 0, 4, 2 );
  204. if( ref0b >= 0 )
  205. if( ref1b >= 0 ) mb_mc_01xywh( h, 0, 2, 4, 2 );
  206. else mb_mc_0xywh ( h, 0, 2, 4, 2 );
  207. else mb_mc_1xywh ( h, 0, 2, 4, 2 );
  208. }
  209. else if( h->mb.i_partition == D_8x16 )
  210. {
  211. if( ref0a >= 0 )
  212. if( ref1a >= 0 ) mb_mc_01xywh( h, 0, 0, 2, 4 );
  213. else mb_mc_0xywh ( h, 0, 0, 2, 4 );
  214. else mb_mc_1xywh ( h, 0, 0, 2, 4 );
  215. if( ref0b >= 0 )
  216. if( ref1b >= 0 ) mb_mc_01xywh( h, 2, 0, 2, 4 );
  217. else mb_mc_0xywh ( h, 2, 0, 2, 4 );
  218. else mb_mc_1xywh ( h, 2, 0, 2, 4 );
  219. }
  220. }
  221. }
  222. int x264_macroblock_cache_allocate( x264_t *h )
  223. {
  224. int i_mb_count = h->mb.i_mb_count;
  225. h->mb.i_mb_stride = h->mb.i_mb_width;
  226. h->mb.i_b8_stride = h->mb.i_mb_width * 2;
  227. h->mb.i_b4_stride = h->mb.i_mb_width * 4;
  228. h->mb.b_interlaced = PARAM_INTERLACED;
  229. PREALLOC_INIT
  230. PREALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
  231. PREALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
  232. PREALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
  233. PREALLOC( h->mb.slice_table, i_mb_count * sizeof(uint32_t) );
  234. /* 0 -> 3 top(4), 4 -> 6 : left(3) */
  235. PREALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
  236. /* all coeffs */
  237. PREALLOC( h->mb.non_zero_count, i_mb_count * 48 * sizeof(uint8_t) );
  238. if( h->param.b_cabac )
  239. {
  240. PREALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
  241. PREALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
  242. PREALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
  243. if( h->param.i_bframe )
  244. PREALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
  245. }
  246. for( int i = 0; i < 2; i++ )
  247. {
  248. int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
  249. if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
  250. i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
  251. for( int j = !i; j < i_refs; j++ )
  252. PREALLOC( h->mb.mvr[i][j], 2 * (i_mb_count + 1) * sizeof(int16_t) );
  253. }
  254. if( h->param.analyse.i_weighted_pred )
  255. {
  256. int i_padv = PADV << PARAM_INTERLACED;
  257. int luma_plane_size = 0;
  258. int numweightbuf;
  259. if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
  260. {
  261. // only need buffer for lookahead
  262. if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
  263. {
  264. // Fake analysis only works on lowres
  265. luma_plane_size = h->fdec->i_stride_lowres * (h->mb.i_mb_height*8+2*i_padv);
  266. // Only need 1 buffer for analysis
  267. numweightbuf = 1;
  268. }
  269. else
  270. numweightbuf = 0;
  271. }
  272. else
  273. {
  274. /* Both ref and fenc is stored for 4:2:0 and 4:2:2 which means that 4:2:0 and 4:4:4
  275. * needs the same amount of space and 4:2:2 needs twice that much */
  276. luma_plane_size = h->fdec->i_stride[0] * (h->mb.i_mb_height*(16<<(CHROMA_FORMAT==CHROMA_422))+2*i_padv);
  277. if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
  278. //smart can weight one ref and one offset -1 in 8-bit
  279. numweightbuf = 1 + (BIT_DEPTH == 8);
  280. else
  281. //simple only has one weighted ref
  282. numweightbuf = 1;
  283. }
  284. for( int i = 0; i < numweightbuf; i++ )
  285. PREALLOC( h->mb.p_weight_buf[i], luma_plane_size * sizeof(pixel) );
  286. }
  287. PREALLOC_END( h->mb.base );
  288. memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint32_t) );
  289. for( int i = 0; i < 2; i++ )
  290. {
  291. int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
  292. if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
  293. i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
  294. for( int j = !i; j < i_refs; j++ )
  295. {
  296. M32( h->mb.mvr[i][j][0] ) = 0;
  297. h->mb.mvr[i][j]++;
  298. }
  299. }
  300. return 0;
  301. fail:
  302. return -1;
  303. }
  304. void x264_macroblock_cache_free( x264_t *h )
  305. {
  306. x264_free( h->mb.base );
  307. }
  308. int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
  309. {
  310. if( !b_lookahead )
  311. {
  312. for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
  313. for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
  314. {
  315. CHECKED_MALLOC( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32) * sizeof(pixel) );
  316. h->intra_border_backup[i][j] += 16;
  317. }
  318. for( int i = 0; i <= PARAM_INTERLACED; i++ )
  319. {
  320. if( h->param.b_sliced_threads )
  321. {
  322. /* Only allocate the first one, and allocate it for the whole frame, because we
  323. * won't be deblocking until after the frame is fully encoded. */
  324. if( h == h->thread[0] && !i )
  325. CHECKED_MALLOC( h->deblock_strength[0], sizeof(**h->deblock_strength) * h->mb.i_mb_count );
  326. else
  327. h->deblock_strength[i] = h->thread[0]->deblock_strength[0];
  328. }
  329. else
  330. CHECKED_MALLOC( h->deblock_strength[i], sizeof(**h->deblock_strength) * h->mb.i_mb_width );
  331. h->deblock_strength[1] = h->deblock_strength[i];
  332. }
  333. }
  334. /* Allocate scratch buffer */
  335. int scratch_size = 0;
  336. if( !b_lookahead )
  337. {
  338. int buf_hpel = (h->thread[0]->fdec->i_width[0]+48+32) * sizeof(int16_t);
  339. int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
  340. int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
  341. int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
  342. ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
  343. scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
  344. }
  345. int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+15)&~15) * sizeof(int16_t);
  346. scratch_size = X264_MAX( scratch_size, buf_mbtree );
  347. if( scratch_size )
  348. CHECKED_MALLOC( h->scratch_buffer, scratch_size );
  349. else
  350. h->scratch_buffer = NULL;
  351. int buf_lookahead_threads = (h->mb.i_mb_height + (4 + 32) * h->param.i_lookahead_threads) * sizeof(int) * 2;
  352. int buf_mbtree2 = buf_mbtree * 12; /* size of the internal propagate_list asm buffer */
  353. scratch_size = X264_MAX( buf_lookahead_threads, buf_mbtree2 );
  354. CHECKED_MALLOC( h->scratch_buffer2, scratch_size );
  355. return 0;
  356. fail:
  357. return -1;
  358. }
  359. void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
  360. {
  361. if( !b_lookahead )
  362. {
  363. for( int i = 0; i <= PARAM_INTERLACED; i++ )
  364. if( !h->param.b_sliced_threads || (h == h->thread[0] && !i) )
  365. x264_free( h->deblock_strength[i] );
  366. for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
  367. for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
  368. x264_free( h->intra_border_backup[i][j] - 16 );
  369. }
  370. x264_free( h->scratch_buffer );
  371. x264_free( h->scratch_buffer2 );
  372. }
  373. void x264_macroblock_slice_init( x264_t *h )
  374. {
  375. h->mb.mv[0] = h->fdec->mv[0];
  376. h->mb.mv[1] = h->fdec->mv[1];
  377. h->mb.mvr[0][0] = h->fdec->mv16x16;
  378. h->mb.ref[0] = h->fdec->ref[0];
  379. h->mb.ref[1] = h->fdec->ref[1];
  380. h->mb.type = h->fdec->mb_type;
  381. h->mb.partition = h->fdec->mb_partition;
  382. h->mb.field = h->fdec->field;
  383. h->fdec->i_ref[0] = h->i_ref[0];
  384. h->fdec->i_ref[1] = h->i_ref[1];
  385. for( int i = 0; i < h->i_ref[0]; i++ )
  386. h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
  387. if( h->sh.i_type == SLICE_TYPE_B )
  388. {
  389. for( int i = 0; i < h->i_ref[1]; i++ )
  390. h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
  391. map_col_to_list0(-1) = -1;
  392. map_col_to_list0(-2) = -2;
  393. for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
  394. {
  395. int poc = h->fref[1][0]->ref_poc[0][i];
  396. map_col_to_list0(i) = -2;
  397. for( int j = 0; j < h->i_ref[0]; j++ )
  398. if( h->fref[0][j]->i_poc == poc )
  399. {
  400. map_col_to_list0(i) = j;
  401. break;
  402. }
  403. }
  404. }
  405. else if( h->sh.i_type == SLICE_TYPE_P )
  406. {
  407. if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
  408. {
  409. deblock_ref_table(-2) = -2;
  410. deblock_ref_table(-1) = -1;
  411. for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
  412. {
  413. /* Mask off high bits to avoid frame num collisions with -1/-2.
  414. * In current x264 frame num values don't cover a range of more
  415. * than 32, so 6 bits is enough for uniqueness. */
  416. if( !MB_INTERLACED )
  417. deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
  418. else
  419. deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
  420. }
  421. }
  422. }
  423. /* init with not available (for top right idx=7,15) */
  424. memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
  425. if( h->i_ref[0] > 0 )
  426. for( int field = 0; field <= SLICE_MBAFF; field++ )
  427. {
  428. int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
  429. int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
  430. int delta = curpoc - refpoc;
  431. h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
  432. }
  433. h->mb.i_neighbour4[6] =
  434. h->mb.i_neighbour4[9] =
  435. h->mb.i_neighbour4[12] =
  436. h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
  437. h->mb.i_neighbour4[3] =
  438. h->mb.i_neighbour4[7] =
  439. h->mb.i_neighbour4[11] =
  440. h->mb.i_neighbour4[13] =
  441. h->mb.i_neighbour4[15] =
  442. h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
  443. }
  444. void x264_macroblock_thread_init( x264_t *h )
  445. {
  446. h->mb.i_me_method = h->param.analyse.i_me_method;
  447. h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
  448. if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
  449. h->mb.i_subpel_refine--;
  450. h->mb.b_chroma_me = h->param.analyse.b_chroma_me &&
  451. ((h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5) ||
  452. (h->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
  453. h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
  454. (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
  455. h->mb.i_mb_prev_xy = -1;
  456. /* 4:2:0 4:2:2 4:4:4
  457. * fdec fenc fdec fenc fdec fenc
  458. * y y y y y y y Y Y Y Y y y y y y y y Y Y Y Y y y y y y y y Y Y Y Y
  459. * y Y Y Y Y Y Y Y Y y Y Y Y Y Y Y Y Y y Y Y Y Y Y Y Y Y
  460. * y Y Y Y Y Y Y Y Y y Y Y Y Y Y Y Y Y y Y Y Y Y Y Y Y Y
  461. * y Y Y Y Y Y Y Y Y y Y Y Y Y Y Y Y Y y Y Y Y Y Y Y Y Y
  462. * y Y Y Y Y U U V V y Y Y Y Y U U V V y Y Y Y Y U U U U
  463. * u u u v v v U U V V u u u v v v U U V V u u u u u u u U U U U
  464. * u U U v V V u U U v V V U U V V u U U U U U U U U
  465. * u U U v V V u U U v V V U U V V u U U U U U U U U
  466. * u U U v V V u U U U U V V V V
  467. * u U U v V V u U U U U V V V V
  468. * v v v v v v v V V V V
  469. * v V V V V V V V V
  470. * v V V V V
  471. * v V V V V
  472. * v V V V V
  473. */
  474. h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
  475. h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
  476. if( CHROMA_FORMAT )
  477. {
  478. h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
  479. h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 20*FDEC_STRIDE;
  480. if( CHROMA444 )
  481. {
  482. h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 32*FENC_STRIDE;
  483. h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 38*FDEC_STRIDE;
  484. }
  485. else
  486. {
  487. h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
  488. h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 20*FDEC_STRIDE + 16;
  489. }
  490. }
  491. }
  492. void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
  493. {
  494. int stride_y = fenc->i_stride[0];
  495. int stride_uv = fenc->i_stride[1];
  496. int off_y = 16 * i_mb_x + 16 * i_mb_y * stride_y;
  497. int off_uv = 16 * i_mb_x + (16 * i_mb_y * stride_uv >> CHROMA_V_SHIFT);
  498. h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
  499. fenc->plane[1]+off_uv, stride_uv, i_mb_x );
  500. }
  501. NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
  502. {
  503. // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
  504. for( int i = -4; i < 4; i++ )
  505. dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
  506. }
  507. static ALWAYS_INLINE void macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
  508. {
  509. int mb_interlaced = b_mbaff && MB_INTERLACED;
  510. int height = b_chroma ? 16 >> CHROMA_V_SHIFT : 16;
  511. int i_stride = h->fdec->i_stride[i];
  512. int i_stride2 = i_stride << mb_interlaced;
  513. int i_pix_offset = mb_interlaced
  514. ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
  515. : 16 * mb_x + height * mb_y * i_stride;
  516. pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
  517. int fdec_idx = b_mbaff ? (mb_interlaced ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : !(mb_y&1);
  518. pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
  519. int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
  520. /* ref_pix_offset[0] references the current field and [1] the opposite field. */
  521. if( mb_interlaced )
  522. ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
  523. h->mb.pic.i_stride[i] = i_stride2;
  524. h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
  525. if( b_chroma )
  526. {
  527. h->mc.load_deinterleave_chroma_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2, height );
  528. memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
  529. memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
  530. h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
  531. h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
  532. }
  533. else
  534. {
  535. h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, h->mb.pic.p_fenc_plane[i], i_stride2, 16 );
  536. memcpy( h->mb.pic.p_fdec[i]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
  537. h->mb.pic.p_fdec[i][-FDEC_STRIDE-1] = intra_fdec[-1];
  538. }
  539. if( b_mbaff || h->mb.b_reencode_mb )
  540. {
  541. for( int j = 0; j < height; j++ )
  542. if( b_chroma )
  543. {
  544. h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
  545. h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
  546. }
  547. else
  548. h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
  549. }
  550. pixel *plane_src, **filtered_src;
  551. for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
  552. {
  553. // Interpolate between pixels in same field.
  554. if( mb_interlaced )
  555. {
  556. plane_src = h->fref[0][j>>1]->plane_fld[i];
  557. filtered_src = h->fref[0][j>>1]->filtered_fld[i];
  558. }
  559. else
  560. {
  561. plane_src = h->fref[0][j]->plane[i];
  562. filtered_src = h->fref[0][j]->filtered[i];
  563. }
  564. h->mb.pic.p_fref[0][j][i*4] = plane_src + ref_pix_offset[j&1];
  565. if( !b_chroma )
  566. {
  567. for( int k = 1; k < 4; k++ )
  568. h->mb.pic.p_fref[0][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
  569. if( !i )
  570. {
  571. if( h->sh.weight[j][0].weightfn )
  572. h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> mb_interlaced][ref_pix_offset[j&1]];
  573. else
  574. h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
  575. }
  576. }
  577. }
  578. if( h->sh.i_type == SLICE_TYPE_B )
  579. for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
  580. {
  581. if( mb_interlaced )
  582. {
  583. plane_src = h->fref[1][j>>1]->plane_fld[i];
  584. filtered_src = h->fref[1][j>>1]->filtered_fld[i];
  585. }
  586. else
  587. {
  588. plane_src = h->fref[1][j]->plane[i];
  589. filtered_src = h->fref[1][j]->filtered[i];
  590. }
  591. h->mb.pic.p_fref[1][j][i*4] = plane_src + ref_pix_offset[j&1];
  592. if( !b_chroma )
  593. for( int k = 1; k < 4; k++ )
  594. h->mb.pic.p_fref[1][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
  595. }
  596. }
  597. static const x264_left_table_t left_indices[4] =
  598. {
  599. /* Current is progressive */
  600. {{ 4, 4, 5, 5}, { 3, 3, 7, 7}, {16+1, 16+1, 32+1, 32+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
  601. {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+5, 16+5, 32+5, 32+5}, {2, 2, 3, 3}, {1, 1, 1, 1}},
  602. /* Current is interlaced */
  603. {{ 4, 6, 4, 6}, { 3, 11, 3, 11}, {16+1, 16+1, 32+1, 32+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
  604. /* Both same */
  605. {{ 4, 5, 6, 3}, { 3, 7, 11, 15}, {16+1, 16+5, 32+1, 32+5}, {0, 1, 2, 3}, {0, 0, 1, 1}}
  606. };
  607. static ALWAYS_INLINE void macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
  608. {
  609. const int mb_interlaced = b_interlaced && MB_INTERLACED;
  610. int top_y = mb_y - (1 << mb_interlaced);
  611. int top = top_y * h->mb.i_mb_stride + mb_x;
  612. h->mb.i_mb_x = mb_x;
  613. h->mb.i_mb_y = mb_y;
  614. h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
  615. h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
  616. h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
  617. h->mb.left_b8[0] =
  618. h->mb.left_b8[1] = -1;
  619. h->mb.left_b4[0] =
  620. h->mb.left_b4[1] = -1;
  621. h->mb.i_neighbour = 0;
  622. h->mb.i_neighbour_intra = 0;
  623. h->mb.i_neighbour_frame = 0;
  624. h->mb.i_mb_top_xy = -1;
  625. h->mb.i_mb_top_y = -1;
  626. h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
  627. h->mb.i_mb_topleft_xy = -1;
  628. h->mb.i_mb_topright_xy = -1;
  629. h->mb.i_mb_type_top = -1;
  630. h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
  631. h->mb.i_mb_type_topleft = -1;
  632. h->mb.i_mb_type_topright = -1;
  633. h->mb.left_index_table = &left_indices[3];
  634. h->mb.topleft_partition = 0;
  635. int topleft_y = top_y;
  636. int topright_y = top_y;
  637. int left[2];
  638. left[0] = left[1] = h->mb.i_mb_xy - 1;
  639. h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
  640. h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;
  641. if( b_interlaced )
  642. {
  643. h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
  644. h->mb.i_mb_topleft_y = -1;
  645. h->mb.i_mb_topright_y = -1;
  646. if( mb_y&1 )
  647. {
  648. if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
  649. {
  650. left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
  651. h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
  652. h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;
  653. if( mb_interlaced )
  654. {
  655. h->mb.left_index_table = &left_indices[2];
  656. left[1] += h->mb.i_mb_stride;
  657. h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
  658. h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
  659. }
  660. else
  661. {
  662. h->mb.left_index_table = &left_indices[1];
  663. topleft_y++;
  664. h->mb.topleft_partition = 1;
  665. }
  666. }
  667. if( !mb_interlaced )
  668. topright_y = -1;
  669. }
  670. else
  671. {
  672. if( mb_interlaced && top >= 0 )
  673. {
  674. if( !h->mb.field[top] )
  675. {
  676. top += h->mb.i_mb_stride;
  677. top_y++;
  678. }
  679. if( mb_x )
  680. topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
  681. if( mb_x < h->mb.i_mb_width-1 )
  682. topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
  683. }
  684. if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
  685. {
  686. if( mb_interlaced )
  687. {
  688. h->mb.left_index_table = &left_indices[2];
  689. left[1] += h->mb.i_mb_stride;
  690. h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
  691. h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
  692. }
  693. else
  694. h->mb.left_index_table = &left_indices[0];
  695. }
  696. }
  697. }
  698. if( mb_x > 0 )
  699. {
  700. h->mb.i_neighbour_frame |= MB_LEFT;
  701. h->mb.i_mb_left_xy[0] = left[0];
  702. h->mb.i_mb_left_xy[1] = left[1];
  703. h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
  704. h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
  705. if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
  706. {
  707. h->mb.i_neighbour |= MB_LEFT;
  708. // FIXME: We don't currently support constrained intra + mbaff.
  709. if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
  710. h->mb.i_neighbour_intra |= MB_LEFT;
  711. }
  712. }
  713. /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
  714. if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
  715. {
  716. if( top >= 0 )
  717. {
  718. h->mb.i_neighbour_frame |= MB_TOP;
  719. h->mb.i_mb_top_xy = top;
  720. h->mb.i_mb_top_y = top_y;
  721. h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
  722. if( h->mb.slice_table[top] == h->sh.i_first_mb )
  723. {
  724. h->mb.i_neighbour |= MB_TOP;
  725. if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
  726. h->mb.i_neighbour_intra |= MB_TOP;
  727. /* We only need to prefetch the top blocks because the left was just written
  728. * to as part of the previous cache_save. Since most target CPUs use write-allocate
  729. * caches, left blocks are near-guaranteed to be in L1 cache. Top--not so much. */
  730. x264_prefetch( &h->mb.cbp[top] );
  731. x264_prefetch( h->mb.intra4x4_pred_mode[top] );
  732. x264_prefetch( &h->mb.non_zero_count[top][12] );
  733. /* These aren't always allocated, but prefetching an invalid address can't hurt. */
  734. x264_prefetch( &h->mb.mb_transform_size[top] );
  735. x264_prefetch( &h->mb.skipbp[top] );
  736. }
  737. }
  738. if( mb_x > 0 && topleft_y >= 0 )
  739. {
  740. h->mb.i_neighbour_frame |= MB_TOPLEFT;
  741. h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
  742. h->mb.i_mb_topleft_y = topleft_y;
  743. h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
  744. if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
  745. {
  746. h->mb.i_neighbour |= MB_TOPLEFT;
  747. if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
  748. h->mb.i_neighbour_intra |= MB_TOPLEFT;
  749. }
  750. }
  751. if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
  752. {
  753. h->mb.i_neighbour_frame |= MB_TOPRIGHT;
  754. h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
  755. h->mb.i_mb_topright_y = topright_y;
  756. h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
  757. if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
  758. {
  759. h->mb.i_neighbour |= MB_TOPRIGHT;
  760. if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
  761. h->mb.i_neighbour_intra |= MB_TOPRIGHT;
  762. }
  763. }
  764. }
  765. }
  766. #define LTOP 0
  767. #if HAVE_INTERLACED
  768. # define LBOT 1
  769. #else
  770. # define LBOT 0
  771. #endif
  772. static ALWAYS_INLINE void macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
  773. {
  774. macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
  775. int *left = h->mb.i_mb_left_xy;
  776. int top = h->mb.i_mb_top_xy;
  777. int top_y = h->mb.i_mb_top_y;
  778. int s8x8 = h->mb.i_b8_stride;
  779. int s4x4 = h->mb.i_b4_stride;
  780. int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
  781. int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
  782. int lists = (1 << h->sh.i_type) & 3;
  783. /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
  784. /* By only dereferencing them once, we avoid this issue. */
  785. int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
  786. uint8_t (*nnz)[48] = h->mb.non_zero_count;
  787. int16_t *cbp = h->mb.cbp;
  788. const x264_left_table_t *left_index_table = h->mb.left_index_table;
  789. h->mb.cache.deblock_strength = h->deblock_strength[mb_y&1][h->param.b_sliced_threads?h->mb.i_mb_xy:mb_x];
  790. /* load cache */
  791. if( h->mb.i_neighbour & MB_TOP )
  792. {
  793. h->mb.cache.i_cbp_top = cbp[top];
  794. /* load intra4x4 */
  795. CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
  796. /* load non_zero_count */
  797. CP32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8], &nnz[top][12] );
  798. CP32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8], &nnz[top][16-4 + (16>>CHROMA_V_SHIFT)] );
  799. CP32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8], &nnz[top][32-4 + (16>>CHROMA_V_SHIFT)] );
  800. /* Finish the prefetching */
  801. for( int l = 0; l < lists; l++ )
  802. {
  803. x264_prefetch( &h->mb.mv[l][top_4x4-1] );
  804. /* Top right being not in the same cacheline as top left will happen
  805. * once every 4 MBs, so one extra prefetch is worthwhile */
  806. x264_prefetch( &h->mb.mv[l][top_4x4+4] );
  807. x264_prefetch( &h->mb.ref[l][top_8x8-1] );
  808. x264_prefetch( &h->mb.mvd[l][top] );
  809. }
  810. }
  811. else
  812. {
  813. h->mb.cache.i_cbp_top = -1;
  814. /* load intra4x4 */
  815. M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
  816. /* load non_zero_count */
  817. M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
  818. M32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8] ) = 0x80808080U;
  819. M32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8] ) = 0x80808080U;
  820. }
  821. if( h->mb.i_neighbour & MB_LEFT )
  822. {
  823. int ltop = left[LTOP];
  824. int lbot = b_mbaff ? left[LBOT] : ltop;
  825. if( b_mbaff )
  826. {
  827. const int16_t top_luma = (cbp[ltop] >> (left_index_table->mv[0]&(~1))) & 2;
  828. const int16_t bot_luma = (cbp[lbot] >> (left_index_table->mv[2]&(~1))) & 2;
  829. h->mb.cache.i_cbp_left = (cbp[ltop] & 0xfff0) | (bot_luma<<2) | top_luma;
  830. }
  831. else
  832. h->mb.cache.i_cbp_left = cbp[ltop];
  833. /* load intra4x4 */
  834. h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] = i4x4[ltop][left_index_table->intra[0]];
  835. h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] = i4x4[ltop][left_index_table->intra[1]];
  836. h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] = i4x4[lbot][left_index_table->intra[2]];
  837. h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[lbot][left_index_table->intra[3]];
  838. /* load non_zero_count */
  839. h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] = nnz[ltop][left_index_table->nnz[0]];
  840. h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] = nnz[ltop][left_index_table->nnz[1]];
  841. h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] = nnz[lbot][left_index_table->nnz[2]];
  842. h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[lbot][left_index_table->nnz[3]];
  843. if( CHROMA_FORMAT >= CHROMA_422 )
  844. {
  845. int offset = (4>>CHROMA_H_SHIFT) - 4;
  846. h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+16+offset];
  847. h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+16+offset];
  848. h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+16+offset];
  849. h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] = nnz[lbot][left_index_table->nnz[3]+16+offset];
  850. h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+32+offset];
  851. h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+32+offset];
  852. h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+32+offset];
  853. h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = nnz[lbot][left_index_table->nnz[3]+32+offset];
  854. }
  855. else
  856. {
  857. h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[0]];
  858. h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[1]];
  859. h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[2]];
  860. h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[3]];
  861. }
  862. }
  863. else
  864. {
  865. h->mb.cache.i_cbp_left = -1;
  866. h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] =
  867. h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] =
  868. h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] =
  869. h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
  870. /* load non_zero_count */
  871. h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] =
  872. h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] =
  873. h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] =
  874. h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
  875. h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] =
  876. h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] =
  877. h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] =
  878. h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = 0x80;
  879. if( CHROMA_FORMAT >= CHROMA_422 )
  880. {
  881. h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] =
  882. h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] =
  883. h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] =
  884. h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = 0x80;
  885. }
  886. }
  887. if( h->pps->b_transform_8x8_mode )
  888. {
  889. h->mb.cache.i_neighbour_transform_size =
  890. ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
  891. + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] );
  892. }
  893. if( b_mbaff )
  894. {
  895. h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
  896. h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
  897. }
  898. if( !b_mbaff )
  899. {
  900. x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
  901. x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
  902. macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 0 );
  903. if( CHROMA444 )
  904. {
  905. x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+ 4*FDEC_STRIDE );
  906. x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+12*FDEC_STRIDE );
  907. x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+ 4*FDEC_STRIDE );
  908. x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+12*FDEC_STRIDE );
  909. macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 0 );
  910. macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 0 );
  911. }
  912. else if( CHROMA_FORMAT )
  913. {
  914. x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
  915. x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
  916. if( CHROMA_FORMAT == CHROMA_422 )
  917. {
  918. x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+12*FDEC_STRIDE );
  919. x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+12*FDEC_STRIDE );
  920. }
  921. macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 0 );
  922. }
  923. }
  924. else
  925. {
  926. macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 1 );
  927. if( CHROMA444 )
  928. {
  929. macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 1 );
  930. macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 1 );
  931. }
  932. else if( CHROMA_FORMAT )
  933. macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 1 );
  934. }
  935. if( h->fdec->integral )
  936. {
  937. int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
  938. for( int list = 0; list < 2; list++ )
  939. for( int i = 0; i < h->mb.pic.i_fref[list]; i++ )
  940. h->mb.pic.p_integral[list][i] = &h->fref[list][i]->integral[offset];
  941. }
  942. x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
  943. /* load ref/mv/mvd */
  944. for( int l = 0; l < lists; l++ )
  945. {
  946. int16_t (*mv)[2] = h->mb.mv[l];
  947. int8_t *ref = h->mb.ref[l];
  948. int i8 = x264_scan8[0] - 1 - 1*8;
  949. if( h->mb.i_neighbour & MB_TOPLEFT )
  950. {
  951. int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topleft_y + mb_x-1)+1+s8x8 : top_8x8 - 1;
  952. int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topleft_y + mb_x-1)+3+3*s4x4 : top_4x4 - 1;
  953. if( b_mbaff && h->mb.topleft_partition )
  954. {
  955. /* Take motion vector from the middle of macroblock instead of
  956. * the bottom right as usual. */
  957. iv -= 2*s4x4;
  958. ir -= s8x8;
  959. }
  960. h->mb.cache.ref[l][i8] = ref[ir];
  961. CP32( h->mb.cache.mv[l][i8], mv[iv] );
  962. }
  963. else
  964. {
  965. h->mb.cache.ref[l][i8] = -2;
  966. M32( h->mb.cache.mv[l][i8] ) = 0;
  967. }
  968. i8 = x264_scan8[0] - 8;
  969. if( h->mb.i_neighbour & MB_TOP )
  970. {
  971. h->mb.cache.ref[l][i8+0] =
  972. h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
  973. h->mb.cache.ref[l][i8+2] =
  974. h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
  975. CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
  976. }
  977. else
  978. {
  979. M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
  980. M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
  981. }
  982. i8 = x264_scan8[0] + 4 - 1*8;
  983. if( h->mb.i_neighbour & MB_TOPRIGHT )
  984. {
  985. int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topright_y + (mb_x+1))+s8x8 : top_8x8 + 2;
  986. int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topright_y + (mb_x+1))+3*s4x4 : top_4x4 + 4;
  987. h->mb.cache.ref[l][i8] = ref[ir];
  988. CP32( h->mb.cache.mv[l][i8], mv[iv] );
  989. }
  990. else
  991. h->mb.cache.ref[l][i8] = -2;
  992. i8 = x264_scan8[0] - 1;
  993. if( h->mb.i_neighbour & MB_LEFT )
  994. {
  995. if( b_mbaff )
  996. {
  997. h->mb.cache.ref[l][i8+0*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[0]];
  998. h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[1]];
  999. h->mb.cache.ref[l][i8+2*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[2]];
  1000. h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[3]];
  1001. CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[0]] );
  1002. CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[1]] );
  1003. CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[2]] );
  1004. CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[3]] );
  1005. }
  1006. else
  1007. {
  1008. const int ir = h->mb.i_b8_xy - 1;
  1009. const int iv = h->mb.i_b4_xy - 1;
  1010. h->mb.cache.ref[l][i8+0*8] =
  1011. h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
  1012. h->mb.cache.ref[l][i8+2*8] =
  1013. h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
  1014. CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
  1015. CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
  1016. CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
  1017. CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
  1018. }
  1019. }
  1020. else
  1021. {
  1022. for( int i = 0; i < 4; i++ )
  1023. {
  1024. h->mb.cache.ref[l][i8+i*8] = -2;
  1025. M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
  1026. }
  1027. }
  1028. /* Extra logic for top right mv in mbaff.
  1029. * . . . d . . a .
  1030. * . . . e . . . .
  1031. * . . . f b . c .
  1032. * . . . . . . . .
  1033. *
  1034. * If the top right of the 4x4 partitions labeled a, b and c in the
  1035. * above diagram do not exist, but the entries d, e and f exist (in
  1036. * the macroblock to the left) then use those instead.
  1037. */
  1038. if( b_mbaff && (h->mb.i_neighbour & MB_LEFT) )
  1039. {
  1040. if( MB_INTERLACED && !h->mb.field[h->mb.i_mb_xy-1] )
  1041. {
  1042. h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*0];
  1043. h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*1];
  1044. h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[1] + 1 + s8x8*0];
  1045. CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[0]+1)] );
  1046. CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[1]+1)] );
  1047. CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[1] + 3 + s4x4*(left_index_table->mv[2]+1)] );
  1048. }
  1049. else if( !MB_INTERLACED && h->mb.field[h->mb.i_mb_xy-1] )
  1050. {
  1051. // Looking at the bottom field so always take the bottom macroblock of the pair.
  1052. h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
  1053. h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[1]];
  1054. h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[2]];
  1055. CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[0]] );
  1056. CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[1]] );
  1057. CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[2]] );
  1058. }
  1059. }
  1060. if( h->param.b_cabac )
  1061. {
  1062. uint8_t (*mvd)[8][2] = h->mb.mvd[l];
  1063. if( h->mb.i_neighbour & MB_TOP )
  1064. CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
  1065. else
  1066. M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
  1067. if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1] >= 0) )
  1068. {
  1069. CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left[LTOP]][left_index_table->intra[0]] );
  1070. CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left[LTOP]][left_index_table->intra[1]] );
  1071. }
  1072. else
  1073. {
  1074. M16( h->mb.cache.mvd[l][x264_scan8[0]-1+0*8] ) = 0;
  1075. M16( h->mb.cache.mvd[l][x264_scan8[0]-1+1*8] ) = 0;
  1076. }
  1077. if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1+2*8] >= 0) )
  1078. {
  1079. CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left[LBOT]][left_index_table->intra[2]] );
  1080. CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left[LBOT]][left_index_table->intra[3]] );
  1081. }
  1082. else
  1083. {
  1084. M16( h->mb.cache.mvd[l][x264_scan8[0]-1+2*8] ) = 0;
  1085. M16( h->mb.cache.mvd[l][x264_scan8[0]-1+3*8] ) = 0;
  1086. }
  1087. }
  1088. /* If motion vectors are cached from frame macroblocks but this
  1089. * macroblock is a field macroblock then the motion vector must be
  1090. * halved. Similarly, motion vectors from field macroblocks are doubled. */
  1091. if( b_mbaff )
  1092. {
  1093. #define MAP_MVS\
  1094. if( FIELD_DIFFERENT(h->mb.i_mb_topleft_xy) )\
  1095. MAP_F2F(mv, ref, x264_scan8[0] - 1 - 1*8)\
  1096. if( FIELD_DIFFERENT(top) )\
  1097. {\
  1098. MAP_F2F(mv, ref, x264_scan8[0] + 0 - 1*8)\
  1099. MAP_F2F(mv, ref, x264_scan8[0] + 1 - 1*8)\
  1100. MAP_F2F(mv, ref, x264_scan8[0] + 2 - 1*8)\
  1101. MAP_F2F(mv, ref, x264_scan8[0] + 3 - 1*8)\
  1102. }\
  1103. if( FIELD_DIFFERENT(h->mb.i_mb_topright_xy) )\
  1104. MAP_F2F(mv, ref, x264_scan8[0] + 4 - 1*8)\
  1105. if( FIELD_DIFFERENT(left[0]) )\
  1106. {\
  1107. MAP_F2F(mv, ref, x264_scan8[0] - 1 + 0*8)\
  1108. MAP_F2F(mv, ref, x264_scan8[0] - 1 + 1*8)\
  1109. MAP_F2F(mv, ref, x264_scan8[0] - 1 + 2*8)\
  1110. MAP_F2F(mv, ref, x264_scan8[0] - 1 + 3*8)\
  1111. MAP_F2F(topright_mv, topright_ref, 0)\
  1112. MAP_F2F(topright_mv, topright_ref, 1)\
  1113. MAP_F2F(topright_mv, topright_ref, 2)\
  1114. }
  1115. if( MB_INTERLACED )
  1116. {
  1117. #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && !h->mb.field[macroblock])
  1118. #define MAP_F2F(varmv, varref, index)\
  1119. if( h->mb.cache.varref[l][index] >= 0 )\
  1120. {\
  1121. h->mb.cache.varref[l][index] <<= 1;\
  1122. h->mb.cache.varmv[l][index][1] /= 2;\
  1123. h->mb.cache.mvd[l][index][1] >>= 1;\
  1124. }
  1125. MAP_MVS
  1126. #undef MAP_F2F
  1127. #undef FIELD_DIFFERENT
  1128. }
  1129. else
  1130. {
  1131. #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && h->mb.field[macroblock])
  1132. #define MAP_F2F(varmv, varref, index)\
  1133. if( h->mb.cache.varref[l][index] >= 0 )\
  1134. {\
  1135. h->mb.cache.varref[l][index] >>= 1;\
  1136. h->mb.cache.varmv[l][index][1] <<= 1;\
  1137. h->mb.cache.mvd[l][index][1] <<= 1;\
  1138. }
  1139. MAP_MVS
  1140. #undef MAP_F2F
  1141. #undef FIELD_DIFFERENT
  1142. }
  1143. }
  1144. }
  1145. if( b_mbaff && mb_x == 0 && !(mb_y&1) )
  1146. {
  1147. if( h->mb.i_mb_top_xy >= h->sh.i_first_mb )
  1148. h->mb.field_decoding_flag = h->mb.field[h->mb.i_mb_top_xy];
  1149. else
  1150. h->mb.field_decoding_flag = 0;
  1151. }
  1152. /* Check whether skip here would cause decoder to predict interlace mode incorrectly.
  1153. * FIXME: It might be better to change the interlace type rather than forcing a skip to be non-skip. */
  1154. h->mb.b_allow_skip = 1;
  1155. if( b_mbaff )
  1156. {
  1157. if( MB_INTERLACED != h->mb.field_decoding_flag &&
  1158. (mb_y&1) && IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride]) )
  1159. h->mb.b_allow_skip = 0;
  1160. }
  1161. if( h->param.b_cabac )
  1162. {
  1163. if( b_mbaff )
  1164. {
  1165. int left_xy, top_xy;
  1166. /* Neighbours here are calculated based on field_decoding_flag */
  1167. int mb_xy = mb_x + (mb_y&~1)*h->mb.i_mb_stride;
  1168. left_xy = mb_xy - 1;
  1169. if( (mb_y&1) && mb_x > 0 && h->mb.field_decoding_flag == h->mb.field[left_xy] )
  1170. left_xy += h->mb.i_mb_stride;
  1171. if( h->mb.field_decoding_flag )
  1172. {
  1173. top_xy = mb_xy - h->mb.i_mb_stride;
  1174. if( !(mb_y&1) && top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && h->mb.field[top_xy] )
  1175. top_xy -= h->mb.i_mb_stride;
  1176. }
  1177. else
  1178. top_xy = mb_x + (mb_y-1)*h->mb.i_mb_stride;
  1179. h->mb.cache.i_neighbour_skip = (mb_x > 0 && h->mb.slice_table[left_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[left_xy] ))
  1180. + (top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[top_xy] ));
  1181. }
  1182. else
  1183. {
  1184. h->mb.cache.i_neighbour_skip = ((h->mb.i_neighbour & MB_LEFT) && !IS_SKIP( h->mb.i_mb_type_left[0] ))
  1185. + ((h->mb.i_neighbour & MB_TOP) && !IS_SKIP( h->mb.i_mb_type_top ));
  1186. }
  1187. }
  1188. /* load skip */
  1189. if( h->sh.i_type == SLICE_TYPE_B )
  1190. {
  1191. h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
  1192. h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
  1193. if( h->param.b_cabac )
  1194. {
  1195. uint8_t skipbp;
  1196. x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
  1197. if( b_mbaff )
  1198. {
  1199. skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LTOP]] : 0;
  1200. h->mb.cache.skip[x264_scan8[0] - 1] = (skipbp >> (1+(left_index_table->mv[0]&~1))) & 1;
  1201. skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LBOT]] : 0;
  1202. h->mb.cache.skip[x264_scan8[8] - 1] = (skipbp >> (1+(left_index_table->mv[2]&~1))) & 1;
  1203. }
  1204. else
  1205. {
  1206. skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[0]] : 0;
  1207. h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
  1208. h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
  1209. }
  1210. skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
  1211. h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
  1212. h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
  1213. }
  1214. }
  1215. if( h->sh.i_type == SLICE_TYPE_P )
  1216. x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
  1217. h->mb.i_neighbour4[0] =
  1218. h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
  1219. | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
  1220. h->mb.i_neighbour4[4] =
  1221. h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
  1222. h->mb.i_neighbour4[2] =
  1223. h->mb.i_neighbour4[8] =
  1224. h->mb.i_neighbour4[10] =
  1225. h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
  1226. h->mb.i_neighbour4[5] =
  1227. h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
  1228. | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
  1229. }
  1230. void x264_macroblock_cache_load_progressive( x264_t *h, int mb_x, int mb_y )
  1231. {
  1232. macroblock_cache_load( h, mb_x, mb_y, 0 );
  1233. }
  1234. void x264_macroblock_cache_load_interlaced( x264_t *h, int mb_x, int mb_y )
  1235. {
  1236. macroblock_cache_load( h, mb_x, mb_y, 1 );
  1237. }
  1238. static void macroblock_deblock_strength_mbaff( x264_t *h, uint8_t (*bs)[8][4] )
  1239. {
  1240. if( (h->mb.i_neighbour & MB_LEFT) && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
  1241. {
  1242. static const uint8_t offset[2][2][8] =
  1243. { { { 0, 0, 0, 0, 1, 1, 1, 1 },
  1244. { 2, 2, 2, 2, 3, 3, 3, 3 }, },
  1245. { { 0, 1, 2, 3, 0, 1, 2, 3 },
  1246. { 0, 1, 2, 3, 0, 1, 2, 3 }, }
  1247. };
  1248. ALIGNED_ARRAY_8( uint8_t, tmpbs, [8] );
  1249. const uint8_t *off = offset[MB_INTERLACED][h->mb.i_mb_y&1];
  1250. uint8_t (*nnz)[48] = h->mb.non_zero_count;
  1251. for( int i = 0; i < 8; i++ )
  1252. {
  1253. int left = h->mb.i_mb_left_xy[MB_INTERLACED ? i>>2 : i&1];
  1254. int nnz_this = h->mb.cache.non_zero_count[x264_scan8[0]+8*(i>>1)];
  1255. int nnz_left = nnz[left][3 + 4*off[i]];
  1256. if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
  1257. {
  1258. int j = off[i]&~1;
  1259. if( h->mb.mb_transform_size[left] )
  1260. nnz_left = !!(M16( &nnz[left][2+4*j] ) | M16( &nnz[left][2+4*(1+j)] ));
  1261. }
  1262. tmpbs[i] = (nnz_left || nnz_this) ? 2 : 1;
  1263. }
  1264. if( MB_INTERLACED )
  1265. {
  1266. CP32( bs[0][0], &tmpbs[0] );
  1267. CP32( bs[0][4], &tmpbs[4] );
  1268. }
  1269. else
  1270. {
  1271. for( int i = 0; i < 4; i++ ) bs[0][0][i] = tmpbs[2*i];
  1272. for( int i = 0; i < 4; i++ ) bs[0][4][i] = tmpbs[1+2*i];
  1273. }
  1274. }
  1275. if( (h->mb.i_neighbour & MB_TOP) && MB_INTERLACED != h->mb.field[h->mb.i_mb_top_xy] )
  1276. {
  1277. if( !(h->mb.i_mb_y&1) && !MB_INTERLACED )
  1278. {
  1279. /* Need to filter both fields (even for frame macroblocks).
  1280. * Filter top two rows using the top macroblock of the above
  1281. * pair and then the bottom one. */
  1282. int mbn_xy = h->mb.i_mb_xy - 2 * h->mb.i_mb_stride;
  1283. uint8_t *nnz_cur = &h->mb.cache.non_zero_count[x264_scan8[0]];
  1284. for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
  1285. {
  1286. uint8_t (*nnz)[48] = h->mb.non_zero_count;
  1287. ALIGNED_4( uint8_t nnz_top[4] );
  1288. CP32( nnz_top, &nnz[mbn_xy][3*4] );
  1289. if( !h->param.b_cabac && h->pps->b_transform_8x8_mode && h->mb.mb_transform_size[mbn_xy] )
  1290. {
  1291. nnz_top[0] = nnz_top[1] = M16( &nnz[mbn_xy][ 8] ) || M16( &nnz[mbn_xy][12] );
  1292. nnz_top[2] = nnz_top[3] = M16( &nnz[mbn_xy][10] ) || M16( &nnz[mbn_xy][14] );
  1293. }
  1294. for( int i = 0; i < 4; i++ )
  1295. bs[1][4*j][i] = (nnz_cur[i] || nnz_top[i]) ? 2 : 1;
  1296. }
  1297. }
  1298. else
  1299. for( int i = 0; i < 4; i++ )
  1300. bs[1][0][i] = X264_MAX( bs[1][0][i], 1 );
  1301. }
  1302. }
  1303. void x264_macroblock_deblock_strength( x264_t *h )
  1304. {
  1305. uint8_t (*bs)[8][4] = h->mb.cache.deblock_strength;
  1306. if( IS_INTRA( h->mb.i_type ) )
  1307. {
  1308. M32( bs[0][1] ) = 0x03030303;
  1309. M64( bs[0][2] ) = 0x0303030303030303ULL;
  1310. M32( bs[1][1] ) = 0x03030303;
  1311. M64( bs[1][2] ) = 0x0303030303030303ULL;
  1312. return;
  1313. }
  1314. /* Early termination: in this case, nnz guarantees all edges use strength 2.*/
  1315. if( h->mb.b_transform_8x8 && !CHROMA444 )
  1316. {
  1317. int cbp_mask = 0xf >> CHROMA_V_SHIFT;
  1318. if( (h->mb.i_cbp_luma&cbp_mask) == cbp_mask )
  1319. {
  1320. M32( bs[0][0] ) = 0x02020202;
  1321. M32( bs[0][2] ) = 0x02020202;
  1322. M32( bs[0][4] ) = 0x02020202;
  1323. M64( bs[1][0] ) = 0x0202020202020202ULL; /* [1][1] and [1][3] has to be set for 4:2:2 */
  1324. M64( bs[1][2] ) = 0x0202020202020202ULL;
  1325. M32( bs[1][4] ) = 0x02020202;
  1326. return;
  1327. }
  1328. }
  1329. int neighbour_changed = 0;
  1330. if( h->sh.i_disable_deblocking_filter_idc != 2 )
  1331. {
  1332. neighbour_changed = h->mb.i_neighbour_frame&~h->mb.i_neighbour;
  1333. h->mb.i_neighbour = h->mb.i_neighbour_frame;
  1334. }
  1335. /* MBAFF deblock uses different left neighbors from encoding */
  1336. if( SLICE_MBAFF && (h->mb.i_neighbour & MB_LEFT) && (h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED) )
  1337. {
  1338. h->mb.i_mb_left_xy[1] =
  1339. h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
  1340. if( h->mb.i_mb_y&1 )
  1341. h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
  1342. else
  1343. h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
  1344. }
  1345. /* If we have multiple slices and we're deblocking on slice edges, we
  1346. * have to reload neighbour data. */
  1347. if( neighbour_changed )
  1348. {
  1349. int top_y = h->mb.i_mb_top_y;
  1350. int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*h->mb.i_mb_x;
  1351. int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*h->mb.i_mb_x;
  1352. int s8x8 = h->mb.i_b8_stride;
  1353. int s4x4 = h->mb.i_b4_stride;
  1354. uint8_t (*nnz)[48] = h->mb.non_zero_count;
  1355. const x264_left_table_t *left_index_table = SLICE_MBAFF ? h->mb.left_index_table : &left_indices[3];
  1356. if( neighbour_changed & MB_TOP )
  1357. CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[h->mb.i_mb_top_xy][12] );
  1358. if( neighbour_changed & MB_LEFT )
  1359. {
  1360. int *left = h->mb.i_mb_left_xy;
  1361. h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[0]][left_index_table->nnz[0]];
  1362. h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[0]][left_index_table->nnz[1]];
  1363. h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[1]][left_index_table->nnz[2]];
  1364. h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[1]][left_index_table->nnz[3]];
  1365. }
  1366. for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
  1367. {
  1368. int16_t (*mv)[2] = h->mb.mv[l];
  1369. int8_t *ref = h->mb.ref[l];
  1370. int i8 = x264_scan8[0] - 8;
  1371. if( neighbour_changed & MB_TOP )
  1372. {
  1373. h->mb.cache.ref[l][i8+0] =
  1374. h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
  1375. h->mb.cache.ref[l][i8+2] =
  1376. h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
  1377. CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
  1378. }
  1379. i8 = x264_scan8[0] - 1;
  1380. if( neighbour_changed & MB_LEFT )
  1381. {
  1382. h->mb.cache.ref[l][i8+0*8] =
  1383. h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[0] + 1 + s8x8*left_index_table->ref[0]];
  1384. h->mb.cache.ref[l][i8+2*8] =
  1385. h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[1] + 1 + s8x8*left_index_table->ref[2]];
  1386. CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[0]] );
  1387. CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[1]] );
  1388. CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[2]] );
  1389. CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[3]] );
  1390. }
  1391. }
  1392. }
  1393. if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART && h->sh.i_type == SLICE_TYPE_P )
  1394. {
  1395. /* Handle reference frame duplicates */
  1396. int i8 = x264_scan8[0] - 8;
  1397. h->mb.cache.ref[0][i8+0] =
  1398. h->mb.cache.ref[0][i8+1] = deblock_ref_table(h->mb.cache.ref[0][i8+0]);
  1399. h->mb.cache.ref[0][i8+2] =
  1400. h->mb.cache.ref[0][i8+3] = deblock_ref_table(h->mb.cache.ref[0][i8+2]);
  1401. i8 = x264_scan8[0] - 1;
  1402. h->mb.cache.ref[0][i8+0*8] =
  1403. h->mb.cache.ref[0][i8+1*8] = deblock_ref_table(h->mb.cache.ref[0][i8+0*8]);
  1404. h->mb.cache.ref[0][i8+2*8] =
  1405. h->mb.cache.ref[0][i8+3*8] = deblock_ref_table(h->mb.cache.ref[0][i8+2*8]);
  1406. int ref0 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 0]]);
  1407. int ref1 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 4]]);
  1408. int ref2 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 8]]);
  1409. int ref3 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[12]]);
  1410. uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
  1411. uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
  1412. M32( &h->mb.cache.ref[0][x264_scan8[0]+8*0] ) = reftop;
  1413. M32( &h->mb.cache.ref[0][x264_scan8[0]+8*1] ) = reftop;
  1414. M32( &h->mb.cache.ref[0][x264_scan8[0]+8*2] ) = refbot;
  1415. M32( &h->mb.cache.ref[0][x264_scan8[0]+8*3] ) = refbot;
  1416. }
  1417. /* Munge NNZ for cavlc + 8x8dct */
  1418. if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
  1419. {
  1420. uint8_t (*nnz)[48] = h->mb.non_zero_count;
  1421. int top = h->mb.i_mb_top_xy;
  1422. int *left = h->mb.i_mb_left_xy;
  1423. if( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] )
  1424. {
  1425. int i8 = x264_scan8[0] - 8;
  1426. int nnz_top0 = M16( &nnz[top][8] ) | M16( &nnz[top][12] );
  1427. int nnz_top1 = M16( &nnz[top][10] ) | M16( &nnz[top][14] );
  1428. M16( &h->mb.cache.non_zero_count[i8+0] ) = nnz_top0 ? 0x0101 : 0;
  1429. M16( &h->mb.cache.non_zero_count[i8+2] ) = nnz_top1 ? 0x0101 : 0;
  1430. }
  1431. if( h->mb.i_neighbour & MB_LEFT )
  1432. {
  1433. int i8 = x264_scan8[0] - 1;
  1434. if( h->mb.mb_transform_size[left[0]] )
  1435. {
  1436. int nnz_left0 = M16( &nnz[left[0]][2] ) | M16( &nnz[left[0]][6] );
  1437. h->mb.cache.non_zero_count[i8+8*0] = !!nnz_left0;
  1438. h->mb.cache.non_zero_count[i8+8*1] = !!nnz_left0;
  1439. }
  1440. if( h->mb.mb_transform_size[left[1]] )
  1441. {
  1442. int nnz_left1 = M16( &nnz[left[1]][10] ) | M16( &nnz[left[1]][14] );
  1443. h->mb.cache.non_zero_count[i8+8*2] = !!nnz_left1;
  1444. h->mb.cache.non_zero_count[i8+8*3] = !!nnz_left1;
  1445. }
  1446. }
  1447. if( h->mb.b_transform_8x8 )
  1448. {
  1449. int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
  1450. int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
  1451. int nnz2 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[10]] );
  1452. int nnz3 = M16( &h->mb.cache.non_zero_count[x264_scan8[12]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[14]] );
  1453. uint32_t nnztop = pack16to32( !!nnz0, !!nnz1 ) * 0x0101;
  1454. uint32_t nnzbot = pack16to32( !!nnz2, !!nnz3 ) * 0x0101;
  1455. M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*0] ) = nnztop;
  1456. M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*1] ) = nnztop;
  1457. M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*2] ) = nnzbot;
  1458. M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*3] ) = nnzbot;
  1459. }
  1460. }
  1461. h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
  1462. bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
  1463. if( SLICE_MBAFF )
  1464. macroblock_deblock_strength_mbaff( h, bs );
  1465. }
  1466. static ALWAYS_INLINE void macroblock_store_pic( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
  1467. {
  1468. int height = b_chroma ? 16>>CHROMA_V_SHIFT : 16;
  1469. int i_stride = h->fdec->i_stride[i];
  1470. int i_stride2 = i_stride << (b_mbaff && MB_INTERLACED);
  1471. int i_pix_offset = (b_mbaff && MB_INTERLACED)
  1472. ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
  1473. : 16 * mb_x + height * mb_y * i_stride;
  1474. if( b_chroma )
  1475. h->mc.store_interleave_chroma( &h->fdec->plane[1][i_pix_offset], i_stride2, h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], height );
  1476. else
  1477. h->mc.copy[PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2, h->mb.pic.p_fdec[i], FDEC_STRIDE, 16 );
  1478. }
  1479. static ALWAYS_INLINE void macroblock_backup_intra( x264_t *h, int mb_x, int mb_y, int b_mbaff )
  1480. {
  1481. /* In MBAFF we store the last two rows in intra_border_backup[0] and [1].
  1482. * For progressive mbs this is the bottom two rows, and for interlaced the
  1483. * bottom row of each field. We also store samples needed for the next
  1484. * mbpair in intra_border_backup[2]. */
  1485. int backup_dst = !b_mbaff ? (mb_y&1) : (mb_y&1) ? 1 : MB_INTERLACED ? 0 : 2;
  1486. memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16 ], h->mb.pic.p_fdec[0]+FDEC_STRIDE*15, 16*sizeof(pixel) );
  1487. if( CHROMA444 )
  1488. {
  1489. memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+FDEC_STRIDE*15, 16*sizeof(pixel) );
  1490. memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16 ], h->mb.pic.p_fdec[2]+FDEC_STRIDE*15, 16*sizeof(pixel) );
  1491. }
  1492. else if( CHROMA_FORMAT )
  1493. {
  1494. int backup_src = (15>>CHROMA_V_SHIFT) * FDEC_STRIDE;
  1495. memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
  1496. memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
  1497. }
  1498. if( b_mbaff )
  1499. {
  1500. if( mb_y&1 )
  1501. {
  1502. int backup_src = (MB_INTERLACED ? 7 : 14) * FDEC_STRIDE;
  1503. backup_dst = MB_INTERLACED ? 2 : 0;
  1504. memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16 ], h->mb.pic.p_fdec[0]+backup_src, 16*sizeof(pixel) );
  1505. if( CHROMA444 )
  1506. {
  1507. memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 16*sizeof(pixel) );
  1508. memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16 ], h->mb.pic.p_fdec[2]+backup_src, 16*sizeof(pixel) );
  1509. }
  1510. else if( CHROMA_FORMAT )
  1511. {
  1512. if( CHROMA_FORMAT == CHROMA_420 )
  1513. backup_src = (MB_INTERLACED ? 3 : 6) * FDEC_STRIDE;
  1514. memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
  1515. memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
  1516. }
  1517. }
  1518. }
  1519. }
  1520. void x264_macroblock_cache_save( x264_t *h )
  1521. {
  1522. const int i_mb_xy = h->mb.i_mb_xy;
  1523. const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
  1524. const int s8x8 = h->mb.i_b8_stride;
  1525. const int s4x4 = h->mb.i_b4_stride;
  1526. const int i_mb_4x4 = h->mb.i_b4_xy;
  1527. const int i_mb_8x8 = h->mb.i_b8_xy;
  1528. /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
  1529. /* By only dereferencing them once, we avoid this issue. */
  1530. int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
  1531. uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
  1532. if( SLICE_MBAFF )
  1533. {
  1534. macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 1 );
  1535. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 1 );
  1536. if( CHROMA444 )
  1537. {
  1538. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 1 );
  1539. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 1 );
  1540. }
  1541. else if( CHROMA_FORMAT )
  1542. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 1 );
  1543. }
  1544. else
  1545. {
  1546. macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 0 );
  1547. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 0 );
  1548. if( CHROMA444 )
  1549. {
  1550. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 0 );
  1551. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 0 );
  1552. }
  1553. else if( CHROMA_FORMAT )
  1554. macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 0 );
  1555. }
  1556. x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
  1557. h->mb.type[i_mb_xy] = i_mb_type;
  1558. h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
  1559. h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
  1560. h->mb.i_mb_prev_xy = i_mb_xy;
  1561. /* save intra4x4 */
  1562. if( i_mb_type == I_4x4 )
  1563. {
  1564. CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
  1565. M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
  1566. h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
  1567. h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
  1568. }
  1569. else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
  1570. M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
  1571. else
  1572. M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
  1573. if( i_mb_type == I_PCM )
  1574. {
  1575. h->mb.qp[i_mb_xy] = 0;
  1576. h->mb.i_last_dqp = 0;
  1577. h->mb.i_cbp_chroma = CHROMA444 ? 0 : 2;
  1578. h->mb.i_cbp_luma = 0xf;
  1579. h->mb.cbp[i_mb_xy] = (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma | 0x1700;
  1580. h->mb.b_transform_8x8 = 0;
  1581. for( int i = 0; i < 48; i++ )
  1582. h->mb.cache.non_zero_count[x264_scan8[i]] = h->param.b_cabac ? 1 : 16;
  1583. }
  1584. else
  1585. {
  1586. if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
  1587. h->mb.i_qp = h->mb.i_last_qp;
  1588. h->mb.qp[i_mb_xy] = h->mb.i_qp;
  1589. h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
  1590. h->mb.i_last_qp = h->mb.i_qp;
  1591. }
  1592. /* save non zero count */
  1593. CP32( &nnz[ 0+0*4], &h->mb.cache.non_zero_count[x264_scan8[ 0]] );
  1594. CP32( &nnz[ 0+1*4], &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
  1595. CP32( &nnz[ 0+2*4], &h->mb.cache.non_zero_count[x264_scan8[ 8]] );
  1596. CP32( &nnz[ 0+3*4], &h->mb.cache.non_zero_count[x264_scan8[10]] );
  1597. CP32( &nnz[16+0*4], &h->mb.cache.non_zero_count[x264_scan8[16+0]] );
  1598. CP32( &nnz[16+1*4], &h->mb.cache.non_zero_count[x264_scan8[16+2]] );
  1599. CP32( &nnz[32+0*4], &h->mb.cache.non_zero_count[x264_scan8[32+0]] );
  1600. CP32( &nnz[32+1*4], &h->mb.cache.non_zero_count[x264_scan8[32+2]] );
  1601. if( CHROMA_FORMAT >= CHROMA_422 )
  1602. {
  1603. CP32( &nnz[16+2*4], &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] );
  1604. CP32( &nnz[16+3*4], &h->mb.cache.non_zero_count[x264_scan8[16+10]] );
  1605. CP32( &nnz[32+2*4], &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] );
  1606. CP32( &nnz[32+3*4], &h->mb.cache.non_zero_count[x264_scan8[32+10]] );
  1607. }
  1608. if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
  1609. h->mb.b_transform_8x8 = 0;
  1610. h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
  1611. if( h->sh.i_type != SLICE_TYPE_I )
  1612. {
  1613. int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
  1614. int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
  1615. int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
  1616. int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
  1617. if( !IS_INTRA( i_mb_type ) )
  1618. {
  1619. ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
  1620. ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
  1621. ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
  1622. ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
  1623. CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
  1624. CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
  1625. CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
  1626. CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
  1627. if( h->sh.i_type == SLICE_TYPE_B )
  1628. {
  1629. ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
  1630. ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
  1631. ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
  1632. ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
  1633. CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
  1634. CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
  1635. CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
  1636. CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
  1637. }
  1638. }
  1639. else
  1640. {
  1641. M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
  1642. M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
  1643. M128( &mv0[0*s4x4] ) = M128_ZERO;
  1644. M128( &mv0[1*s4x4] ) = M128_ZERO;
  1645. M128( &mv0[2*s4x4] ) = M128_ZERO;
  1646. M128( &mv0[3*s4x4] ) = M128_ZERO;
  1647. if( h->sh.i_type == SLICE_TYPE_B )
  1648. {
  1649. M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
  1650. M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
  1651. M128( &mv1[0*s4x4] ) = M128_ZERO;
  1652. M128( &mv1[1*s4x4] ) = M128_ZERO;
  1653. M128( &mv1[2*s4x4] ) = M128_ZERO;
  1654. M128( &mv1[3*s4x4] ) = M128_ZERO;
  1655. }
  1656. }
  1657. }
  1658. if( h->param.b_cabac )
  1659. {
  1660. uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
  1661. uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
  1662. if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
  1663. h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode];
  1664. else
  1665. h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
  1666. if( (0x3FF30 >> i_mb_type) & 1 ) /* !INTRA && !SKIP && !DIRECT */
  1667. {
  1668. CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
  1669. CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
  1670. CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
  1671. CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
  1672. if( h->sh.i_type == SLICE_TYPE_B )
  1673. {
  1674. CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
  1675. CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
  1676. CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
  1677. CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
  1678. }
  1679. }
  1680. else
  1681. {
  1682. M128( mvd0[0] ) = M128_ZERO;
  1683. if( h->sh.i_type == SLICE_TYPE_B )
  1684. M128( mvd1[0] ) = M128_ZERO;
  1685. }
  1686. if( h->sh.i_type == SLICE_TYPE_B )
  1687. {
  1688. if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
  1689. h->mb.skipbp[i_mb_xy] = 0xf;
  1690. else if( i_mb_type == B_8x8 )
  1691. {
  1692. int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
  1693. skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
  1694. skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
  1695. skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
  1696. h->mb.skipbp[i_mb_xy] = skipbp;
  1697. }
  1698. else
  1699. h->mb.skipbp[i_mb_xy] = 0;
  1700. }
  1701. }
  1702. }
  1703. void x264_macroblock_bipred_init( x264_t *h )
  1704. {
  1705. for( int mbfield = 0; mbfield <= SLICE_MBAFF; mbfield++ )
  1706. for( int field = 0; field <= SLICE_MBAFF; field++ )
  1707. for( int i_ref0 = 0; i_ref0 < (h->i_ref[0]<<mbfield); i_ref0++ )
  1708. {
  1709. x264_frame_t *l0 = h->fref[0][i_ref0>>mbfield];
  1710. int poc0 = l0->i_poc + mbfield*l0->i_delta_poc[field^(i_ref0&1)];
  1711. for( int i_ref1 = 0; i_ref1 < (h->i_ref[1]<<mbfield); i_ref1++ )
  1712. {
  1713. int dist_scale_factor;
  1714. x264_frame_t *l1 = h->fref[1][i_ref1>>mbfield];
  1715. int cur_poc = h->fdec->i_poc + mbfield*h->fdec->i_delta_poc[field];
  1716. int poc1 = l1->i_poc + mbfield*l1->i_delta_poc[field^(i_ref1&1)];
  1717. int td = x264_clip3( poc1 - poc0, -128, 127 );
  1718. if( td == 0 /* || pic0 is a long-term ref */ )
  1719. dist_scale_factor = 256;
  1720. else
  1721. {
  1722. int tb = x264_clip3( cur_poc - poc0, -128, 127 );
  1723. int tx = (16384 + (abs(td) >> 1)) / td;
  1724. dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
  1725. }
  1726. h->mb.dist_scale_factor_buf[mbfield][field][i_ref0][i_ref1] = dist_scale_factor;
  1727. dist_scale_factor >>= 2;
  1728. if( h->param.analyse.b_weighted_bipred
  1729. && dist_scale_factor >= -64
  1730. && dist_scale_factor <= 128 )
  1731. {
  1732. h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 64 - dist_scale_factor;
  1733. // ssse3 implementation of biweight doesn't support the extrema.
  1734. // if we ever generate them, we'll have to drop that optimization.
  1735. assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
  1736. }
  1737. else
  1738. h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 32;
  1739. }
  1740. }
  1741. }