vpx_subpixel_8t_intrin_ssse3.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // Due to a header conflict between math.h and intrinsics includes with ceil()
  11. // in certain configurations under vs9 this include needs to precede
  12. // tmmintrin.h.
  13. #include <tmmintrin.h>
  14. #include "./vpx_dsp_rtcd.h"
  15. #include "vpx_dsp/vpx_filter.h"
  16. #include "vpx_dsp/x86/convolve.h"
  17. #include "vpx_mem/vpx_mem.h"
  18. #include "vpx_ports/mem.h"
  19. #include "vpx_ports/emmintrin_compat.h"
  20. // filters only for the 4_h8 convolution
  21. DECLARE_ALIGNED(16, static const uint8_t, filt1_4_h8[16]) = {
  22. 0, 1, 1, 2, 2, 3, 3, 4, 2, 3, 3, 4, 4, 5, 5, 6
  23. };
  24. DECLARE_ALIGNED(16, static const uint8_t, filt2_4_h8[16]) = {
  25. 4, 5, 5, 6, 6, 7, 7, 8, 6, 7, 7, 8, 8, 9, 9, 10
  26. };
  27. // filters for 8_h8 and 16_h8
  28. DECLARE_ALIGNED(16, static const uint8_t, filt1_global[16]) = {
  29. 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8
  30. };
  31. DECLARE_ALIGNED(16, static const uint8_t, filt2_global[16]) = {
  32. 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10
  33. };
  34. DECLARE_ALIGNED(16, static const uint8_t, filt3_global[16]) = {
  35. 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12
  36. };
  37. DECLARE_ALIGNED(16, static const uint8_t, filt4_global[16]) = {
  38. 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14
  39. };
  40. // These are reused by the avx2 intrinsics.
  41. filter8_1dfunction vpx_filter_block1d8_v8_intrin_ssse3;
  42. filter8_1dfunction vpx_filter_block1d8_h8_intrin_ssse3;
  43. filter8_1dfunction vpx_filter_block1d4_h8_intrin_ssse3;
  44. void vpx_filter_block1d4_h8_intrin_ssse3(const uint8_t *src_ptr,
  45. ptrdiff_t src_pixels_per_line,
  46. uint8_t *output_ptr,
  47. ptrdiff_t output_pitch,
  48. uint32_t output_height,
  49. const int16_t *filter) {
  50. __m128i firstFilters, secondFilters, shuffle1, shuffle2;
  51. __m128i srcRegFilt1, srcRegFilt2, srcRegFilt3, srcRegFilt4;
  52. __m128i addFilterReg64, filtersReg, srcReg, minReg;
  53. unsigned int i;
  54. // create a register with 0,64,0,64,0,64,0,64,0,64,0,64,0,64,0,64
  55. addFilterReg64 =_mm_set1_epi32((int)0x0400040u);
  56. filtersReg = _mm_loadu_si128((const __m128i *)filter);
  57. // converting the 16 bit (short) to 8 bit (byte) and have the same data
  58. // in both lanes of 128 bit register.
  59. filtersReg =_mm_packs_epi16(filtersReg, filtersReg);
  60. // duplicate only the first 16 bits in the filter into the first lane
  61. firstFilters = _mm_shufflelo_epi16(filtersReg, 0);
  62. // duplicate only the third 16 bit in the filter into the first lane
  63. secondFilters = _mm_shufflelo_epi16(filtersReg, 0xAAu);
  64. // duplicate only the seconds 16 bits in the filter into the second lane
  65. // firstFilters: k0 k1 k0 k1 k0 k1 k0 k1 k2 k3 k2 k3 k2 k3 k2 k3
  66. firstFilters = _mm_shufflehi_epi16(firstFilters, 0x55u);
  67. // duplicate only the forth 16 bits in the filter into the second lane
  68. // secondFilters: k4 k5 k4 k5 k4 k5 k4 k5 k6 k7 k6 k7 k6 k7 k6 k7
  69. secondFilters = _mm_shufflehi_epi16(secondFilters, 0xFFu);
  70. // loading the local filters
  71. shuffle1 =_mm_load_si128((__m128i const *)filt1_4_h8);
  72. shuffle2 = _mm_load_si128((__m128i const *)filt2_4_h8);
  73. for (i = 0; i < output_height; i++) {
  74. srcReg = _mm_loadu_si128((const __m128i *)(src_ptr - 3));
  75. // filter the source buffer
  76. srcRegFilt1= _mm_shuffle_epi8(srcReg, shuffle1);
  77. srcRegFilt2= _mm_shuffle_epi8(srcReg, shuffle2);
  78. // multiply 2 adjacent elements with the filter and add the result
  79. srcRegFilt1 = _mm_maddubs_epi16(srcRegFilt1, firstFilters);
  80. srcRegFilt2 = _mm_maddubs_epi16(srcRegFilt2, secondFilters);
  81. // extract the higher half of the lane
  82. srcRegFilt3 = _mm_srli_si128(srcRegFilt1, 8);
  83. srcRegFilt4 = _mm_srli_si128(srcRegFilt2, 8);
  84. minReg = _mm_min_epi16(srcRegFilt3, srcRegFilt2);
  85. // add and saturate all the results together
  86. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt4);
  87. srcRegFilt3 = _mm_max_epi16(srcRegFilt3, srcRegFilt2);
  88. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, minReg);
  89. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt3);
  90. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, addFilterReg64);
  91. // shift by 7 bit each 16 bits
  92. srcRegFilt1 = _mm_srai_epi16(srcRegFilt1, 7);
  93. // shrink to 8 bit each 16 bits
  94. srcRegFilt1 = _mm_packus_epi16(srcRegFilt1, srcRegFilt1);
  95. src_ptr+=src_pixels_per_line;
  96. // save only 4 bytes
  97. *((int*)&output_ptr[0])= _mm_cvtsi128_si32(srcRegFilt1);
  98. output_ptr+=output_pitch;
  99. }
  100. }
  101. void vpx_filter_block1d8_h8_intrin_ssse3(const uint8_t *src_ptr,
  102. ptrdiff_t src_pixels_per_line,
  103. uint8_t *output_ptr,
  104. ptrdiff_t output_pitch,
  105. uint32_t output_height,
  106. const int16_t *filter) {
  107. __m128i firstFilters, secondFilters, thirdFilters, forthFilters, srcReg;
  108. __m128i filt1Reg, filt2Reg, filt3Reg, filt4Reg;
  109. __m128i srcRegFilt1, srcRegFilt2, srcRegFilt3, srcRegFilt4;
  110. __m128i addFilterReg64, filtersReg, minReg;
  111. unsigned int i;
  112. // create a register with 0,64,0,64,0,64,0,64,0,64,0,64,0,64,0,64
  113. addFilterReg64 = _mm_set1_epi32((int)0x0400040u);
  114. filtersReg = _mm_loadu_si128((const __m128i *)filter);
  115. // converting the 16 bit (short) to 8 bit (byte) and have the same data
  116. // in both lanes of 128 bit register.
  117. filtersReg =_mm_packs_epi16(filtersReg, filtersReg);
  118. // duplicate only the first 16 bits (first and second byte)
  119. // across 128 bit register
  120. firstFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x100u));
  121. // duplicate only the second 16 bits (third and forth byte)
  122. // across 128 bit register
  123. secondFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x302u));
  124. // duplicate only the third 16 bits (fifth and sixth byte)
  125. // across 128 bit register
  126. thirdFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x504u));
  127. // duplicate only the forth 16 bits (seventh and eighth byte)
  128. // across 128 bit register
  129. forthFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x706u));
  130. filt1Reg = _mm_load_si128((__m128i const *)filt1_global);
  131. filt2Reg = _mm_load_si128((__m128i const *)filt2_global);
  132. filt3Reg = _mm_load_si128((__m128i const *)filt3_global);
  133. filt4Reg = _mm_load_si128((__m128i const *)filt4_global);
  134. for (i = 0; i < output_height; i++) {
  135. srcReg = _mm_loadu_si128((const __m128i *)(src_ptr - 3));
  136. // filter the source buffer
  137. srcRegFilt1= _mm_shuffle_epi8(srcReg, filt1Reg);
  138. srcRegFilt2= _mm_shuffle_epi8(srcReg, filt2Reg);
  139. // multiply 2 adjacent elements with the filter and add the result
  140. srcRegFilt1 = _mm_maddubs_epi16(srcRegFilt1, firstFilters);
  141. srcRegFilt2 = _mm_maddubs_epi16(srcRegFilt2, secondFilters);
  142. // filter the source buffer
  143. srcRegFilt3= _mm_shuffle_epi8(srcReg, filt3Reg);
  144. srcRegFilt4= _mm_shuffle_epi8(srcReg, filt4Reg);
  145. // multiply 2 adjacent elements with the filter and add the result
  146. srcRegFilt3 = _mm_maddubs_epi16(srcRegFilt3, thirdFilters);
  147. srcRegFilt4 = _mm_maddubs_epi16(srcRegFilt4, forthFilters);
  148. // add and saturate all the results together
  149. minReg = _mm_min_epi16(srcRegFilt2, srcRegFilt3);
  150. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt4);
  151. srcRegFilt2= _mm_max_epi16(srcRegFilt2, srcRegFilt3);
  152. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, minReg);
  153. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt2);
  154. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, addFilterReg64);
  155. // shift by 7 bit each 16 bits
  156. srcRegFilt1 = _mm_srai_epi16(srcRegFilt1, 7);
  157. // shrink to 8 bit each 16 bits
  158. srcRegFilt1 = _mm_packus_epi16(srcRegFilt1, srcRegFilt1);
  159. src_ptr+=src_pixels_per_line;
  160. // save only 8 bytes
  161. _mm_storel_epi64((__m128i*)&output_ptr[0], srcRegFilt1);
  162. output_ptr+=output_pitch;
  163. }
  164. }
  165. void vpx_filter_block1d8_v8_intrin_ssse3(const uint8_t *src_ptr,
  166. ptrdiff_t src_pitch,
  167. uint8_t *output_ptr,
  168. ptrdiff_t out_pitch,
  169. uint32_t output_height,
  170. const int16_t *filter) {
  171. __m128i addFilterReg64, filtersReg, minReg;
  172. __m128i firstFilters, secondFilters, thirdFilters, forthFilters;
  173. __m128i srcRegFilt1, srcRegFilt2, srcRegFilt3, srcRegFilt5;
  174. __m128i srcReg1, srcReg2, srcReg3, srcReg4, srcReg5, srcReg6, srcReg7;
  175. __m128i srcReg8;
  176. unsigned int i;
  177. // create a register with 0,64,0,64,0,64,0,64,0,64,0,64,0,64,0,64
  178. addFilterReg64 = _mm_set1_epi32((int)0x0400040u);
  179. filtersReg = _mm_loadu_si128((const __m128i *)filter);
  180. // converting the 16 bit (short) to 8 bit (byte) and have the same data
  181. // in both lanes of 128 bit register.
  182. filtersReg =_mm_packs_epi16(filtersReg, filtersReg);
  183. // duplicate only the first 16 bits in the filter
  184. firstFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x100u));
  185. // duplicate only the second 16 bits in the filter
  186. secondFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x302u));
  187. // duplicate only the third 16 bits in the filter
  188. thirdFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x504u));
  189. // duplicate only the forth 16 bits in the filter
  190. forthFilters = _mm_shuffle_epi8(filtersReg, _mm_set1_epi16(0x706u));
  191. // load the first 7 rows of 8 bytes
  192. srcReg1 = _mm_loadl_epi64((const __m128i *)src_ptr);
  193. srcReg2 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch));
  194. srcReg3 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 2));
  195. srcReg4 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 3));
  196. srcReg5 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 4));
  197. srcReg6 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 5));
  198. srcReg7 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 6));
  199. for (i = 0; i < output_height; i++) {
  200. // load the last 8 bytes
  201. srcReg8 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 7));
  202. // merge the result together
  203. srcRegFilt1 = _mm_unpacklo_epi8(srcReg1, srcReg2);
  204. srcRegFilt3 = _mm_unpacklo_epi8(srcReg3, srcReg4);
  205. // merge the result together
  206. srcRegFilt2 = _mm_unpacklo_epi8(srcReg5, srcReg6);
  207. srcRegFilt5 = _mm_unpacklo_epi8(srcReg7, srcReg8);
  208. // multiply 2 adjacent elements with the filter and add the result
  209. srcRegFilt1 = _mm_maddubs_epi16(srcRegFilt1, firstFilters);
  210. srcRegFilt3 = _mm_maddubs_epi16(srcRegFilt3, secondFilters);
  211. srcRegFilt2 = _mm_maddubs_epi16(srcRegFilt2, thirdFilters);
  212. srcRegFilt5 = _mm_maddubs_epi16(srcRegFilt5, forthFilters);
  213. // add and saturate the results together
  214. minReg = _mm_min_epi16(srcRegFilt2, srcRegFilt3);
  215. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt5);
  216. srcRegFilt2 = _mm_max_epi16(srcRegFilt2, srcRegFilt3);
  217. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, minReg);
  218. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt2);
  219. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, addFilterReg64);
  220. // shift by 7 bit each 16 bit
  221. srcRegFilt1 = _mm_srai_epi16(srcRegFilt1, 7);
  222. // shrink to 8 bit each 16 bits
  223. srcRegFilt1 = _mm_packus_epi16(srcRegFilt1, srcRegFilt1);
  224. src_ptr+=src_pitch;
  225. // shift down a row
  226. srcReg1 = srcReg2;
  227. srcReg2 = srcReg3;
  228. srcReg3 = srcReg4;
  229. srcReg4 = srcReg5;
  230. srcReg5 = srcReg6;
  231. srcReg6 = srcReg7;
  232. srcReg7 = srcReg8;
  233. // save only 8 bytes convolve result
  234. _mm_storel_epi64((__m128i*)&output_ptr[0], srcRegFilt1);
  235. output_ptr+=out_pitch;
  236. }
  237. }
  238. filter8_1dfunction vpx_filter_block1d16_v8_ssse3;
  239. filter8_1dfunction vpx_filter_block1d16_h8_ssse3;
  240. filter8_1dfunction vpx_filter_block1d8_v8_ssse3;
  241. filter8_1dfunction vpx_filter_block1d8_h8_ssse3;
  242. filter8_1dfunction vpx_filter_block1d4_v8_ssse3;
  243. filter8_1dfunction vpx_filter_block1d4_h8_ssse3;
  244. filter8_1dfunction vpx_filter_block1d16_v8_avg_ssse3;
  245. filter8_1dfunction vpx_filter_block1d16_h8_avg_ssse3;
  246. filter8_1dfunction vpx_filter_block1d8_v8_avg_ssse3;
  247. filter8_1dfunction vpx_filter_block1d8_h8_avg_ssse3;
  248. filter8_1dfunction vpx_filter_block1d4_v8_avg_ssse3;
  249. filter8_1dfunction vpx_filter_block1d4_h8_avg_ssse3;
  250. filter8_1dfunction vpx_filter_block1d16_v2_ssse3;
  251. filter8_1dfunction vpx_filter_block1d16_h2_ssse3;
  252. filter8_1dfunction vpx_filter_block1d8_v2_ssse3;
  253. filter8_1dfunction vpx_filter_block1d8_h2_ssse3;
  254. filter8_1dfunction vpx_filter_block1d4_v2_ssse3;
  255. filter8_1dfunction vpx_filter_block1d4_h2_ssse3;
  256. filter8_1dfunction vpx_filter_block1d16_v2_avg_ssse3;
  257. filter8_1dfunction vpx_filter_block1d16_h2_avg_ssse3;
  258. filter8_1dfunction vpx_filter_block1d8_v2_avg_ssse3;
  259. filter8_1dfunction vpx_filter_block1d8_h2_avg_ssse3;
  260. filter8_1dfunction vpx_filter_block1d4_v2_avg_ssse3;
  261. filter8_1dfunction vpx_filter_block1d4_h2_avg_ssse3;
  262. // void vpx_convolve8_horiz_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  263. // uint8_t *dst, ptrdiff_t dst_stride,
  264. // const int16_t *filter_x, int x_step_q4,
  265. // const int16_t *filter_y, int y_step_q4,
  266. // int w, int h);
  267. // void vpx_convolve8_vert_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  268. // uint8_t *dst, ptrdiff_t dst_stride,
  269. // const int16_t *filter_x, int x_step_q4,
  270. // const int16_t *filter_y, int y_step_q4,
  271. // int w, int h);
  272. // void vpx_convolve8_avg_horiz_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  273. // uint8_t *dst, ptrdiff_t dst_stride,
  274. // const int16_t *filter_x, int x_step_q4,
  275. // const int16_t *filter_y, int y_step_q4,
  276. // int w, int h);
  277. // void vpx_convolve8_avg_vert_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  278. // uint8_t *dst, ptrdiff_t dst_stride,
  279. // const int16_t *filter_x, int x_step_q4,
  280. // const int16_t *filter_y, int y_step_q4,
  281. // int w, int h);
  282. FUN_CONV_1D(horiz, x_step_q4, filter_x, h, src, , ssse3);
  283. FUN_CONV_1D(vert, y_step_q4, filter_y, v, src - src_stride * 3, , ssse3);
  284. FUN_CONV_1D(avg_horiz, x_step_q4, filter_x, h, src, avg_, ssse3);
  285. FUN_CONV_1D(avg_vert, y_step_q4, filter_y, v, src - src_stride * 3, avg_,
  286. ssse3);
  287. #define TRANSPOSE_8X8(in0, in1, in2, in3, in4, in5, in6, in7, \
  288. out0, out1, out2, out3, out4, out5, out6, out7) { \
  289. const __m128i tr0_0 = _mm_unpacklo_epi8(in0, in1); \
  290. const __m128i tr0_1 = _mm_unpacklo_epi8(in2, in3); \
  291. const __m128i tr0_2 = _mm_unpacklo_epi8(in4, in5); \
  292. const __m128i tr0_3 = _mm_unpacklo_epi8(in6, in7); \
  293. \
  294. const __m128i tr1_0 = _mm_unpacklo_epi16(tr0_0, tr0_1); \
  295. const __m128i tr1_1 = _mm_unpackhi_epi16(tr0_0, tr0_1); \
  296. const __m128i tr1_2 = _mm_unpacklo_epi16(tr0_2, tr0_3); \
  297. const __m128i tr1_3 = _mm_unpackhi_epi16(tr0_2, tr0_3); \
  298. \
  299. const __m128i tr2_0 = _mm_unpacklo_epi32(tr1_0, tr1_2); \
  300. const __m128i tr2_1 = _mm_unpackhi_epi32(tr1_0, tr1_2); \
  301. const __m128i tr2_2 = _mm_unpacklo_epi32(tr1_1, tr1_3); \
  302. const __m128i tr2_3 = _mm_unpackhi_epi32(tr1_1, tr1_3); \
  303. \
  304. out0 = _mm_unpacklo_epi64(tr2_0, tr2_0); \
  305. out1 = _mm_unpackhi_epi64(tr2_0, tr2_0); \
  306. out2 = _mm_unpacklo_epi64(tr2_1, tr2_1); \
  307. out3 = _mm_unpackhi_epi64(tr2_1, tr2_1); \
  308. out4 = _mm_unpacklo_epi64(tr2_2, tr2_2); \
  309. out5 = _mm_unpackhi_epi64(tr2_2, tr2_2); \
  310. out6 = _mm_unpacklo_epi64(tr2_3, tr2_3); \
  311. out7 = _mm_unpackhi_epi64(tr2_3, tr2_3); \
  312. }
  313. static void filter_horiz_w8_ssse3(const uint8_t *src_x, ptrdiff_t src_pitch,
  314. uint8_t *dst, const int16_t *x_filter) {
  315. const __m128i k_256 = _mm_set1_epi16(1 << 8);
  316. const __m128i f_values = _mm_load_si128((const __m128i *)x_filter);
  317. // pack and duplicate the filter values
  318. const __m128i f1f0 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0200u));
  319. const __m128i f3f2 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0604u));
  320. const __m128i f5f4 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0a08u));
  321. const __m128i f7f6 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0e0cu));
  322. const __m128i A = _mm_loadl_epi64((const __m128i *)src_x);
  323. const __m128i B = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch));
  324. const __m128i C = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch * 2));
  325. const __m128i D = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch * 3));
  326. const __m128i E = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch * 4));
  327. const __m128i F = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch * 5));
  328. const __m128i G = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch * 6));
  329. const __m128i H = _mm_loadl_epi64((const __m128i *)(src_x + src_pitch * 7));
  330. // 00 01 10 11 02 03 12 13 04 05 14 15 06 07 16 17
  331. const __m128i tr0_0 = _mm_unpacklo_epi16(A, B);
  332. // 20 21 30 31 22 23 32 33 24 25 34 35 26 27 36 37
  333. const __m128i tr0_1 = _mm_unpacklo_epi16(C, D);
  334. // 40 41 50 51 42 43 52 53 44 45 54 55 46 47 56 57
  335. const __m128i tr0_2 = _mm_unpacklo_epi16(E, F);
  336. // 60 61 70 71 62 63 72 73 64 65 74 75 66 67 76 77
  337. const __m128i tr0_3 = _mm_unpacklo_epi16(G, H);
  338. // 00 01 10 11 20 21 30 31 02 03 12 13 22 23 32 33
  339. const __m128i tr1_0 = _mm_unpacklo_epi32(tr0_0, tr0_1);
  340. // 04 05 14 15 24 25 34 35 06 07 16 17 26 27 36 37
  341. const __m128i tr1_1 = _mm_unpackhi_epi32(tr0_0, tr0_1);
  342. // 40 41 50 51 60 61 70 71 42 43 52 53 62 63 72 73
  343. const __m128i tr1_2 = _mm_unpacklo_epi32(tr0_2, tr0_3);
  344. // 44 45 54 55 64 65 74 75 46 47 56 57 66 67 76 77
  345. const __m128i tr1_3 = _mm_unpackhi_epi32(tr0_2, tr0_3);
  346. // 00 01 10 11 20 21 30 31 40 41 50 51 60 61 70 71
  347. const __m128i s1s0 = _mm_unpacklo_epi64(tr1_0, tr1_2);
  348. const __m128i s3s2 = _mm_unpackhi_epi64(tr1_0, tr1_2);
  349. const __m128i s5s4 = _mm_unpacklo_epi64(tr1_1, tr1_3);
  350. const __m128i s7s6 = _mm_unpackhi_epi64(tr1_1, tr1_3);
  351. // multiply 2 adjacent elements with the filter and add the result
  352. const __m128i x0 = _mm_maddubs_epi16(s1s0, f1f0);
  353. const __m128i x1 = _mm_maddubs_epi16(s3s2, f3f2);
  354. const __m128i x2 = _mm_maddubs_epi16(s5s4, f5f4);
  355. const __m128i x3 = _mm_maddubs_epi16(s7s6, f7f6);
  356. // add and saturate the results together
  357. const __m128i min_x2x1 = _mm_min_epi16(x2, x1);
  358. const __m128i max_x2x1 = _mm_max_epi16(x2, x1);
  359. __m128i temp = _mm_adds_epi16(x0, x3);
  360. temp = _mm_adds_epi16(temp, min_x2x1);
  361. temp = _mm_adds_epi16(temp, max_x2x1);
  362. // round and shift by 7 bit each 16 bit
  363. temp = _mm_mulhrs_epi16(temp, k_256);
  364. // shrink to 8 bit each 16 bits
  365. temp = _mm_packus_epi16(temp, temp);
  366. // save only 8 bytes convolve result
  367. _mm_storel_epi64((__m128i*)dst, temp);
  368. }
  369. static void transpose8x8_to_dst(const uint8_t *src, ptrdiff_t src_stride,
  370. uint8_t *dst, ptrdiff_t dst_stride) {
  371. __m128i A, B, C, D, E, F, G, H;
  372. A = _mm_loadl_epi64((const __m128i *)src);
  373. B = _mm_loadl_epi64((const __m128i *)(src + src_stride));
  374. C = _mm_loadl_epi64((const __m128i *)(src + src_stride * 2));
  375. D = _mm_loadl_epi64((const __m128i *)(src + src_stride * 3));
  376. E = _mm_loadl_epi64((const __m128i *)(src + src_stride * 4));
  377. F = _mm_loadl_epi64((const __m128i *)(src + src_stride * 5));
  378. G = _mm_loadl_epi64((const __m128i *)(src + src_stride * 6));
  379. H = _mm_loadl_epi64((const __m128i *)(src + src_stride * 7));
  380. TRANSPOSE_8X8(A, B, C, D, E, F, G, H,
  381. A, B, C, D, E, F, G, H);
  382. _mm_storel_epi64((__m128i*)dst, A);
  383. _mm_storel_epi64((__m128i*)(dst + dst_stride * 1), B);
  384. _mm_storel_epi64((__m128i*)(dst + dst_stride * 2), C);
  385. _mm_storel_epi64((__m128i*)(dst + dst_stride * 3), D);
  386. _mm_storel_epi64((__m128i*)(dst + dst_stride * 4), E);
  387. _mm_storel_epi64((__m128i*)(dst + dst_stride * 5), F);
  388. _mm_storel_epi64((__m128i*)(dst + dst_stride * 6), G);
  389. _mm_storel_epi64((__m128i*)(dst + dst_stride * 7), H);
  390. }
  391. static void scaledconvolve_horiz_w8(const uint8_t *src, ptrdiff_t src_stride,
  392. uint8_t *dst, ptrdiff_t dst_stride,
  393. const InterpKernel *x_filters,
  394. int x0_q4, int x_step_q4, int w, int h) {
  395. DECLARE_ALIGNED(16, uint8_t, temp[8 * 8]);
  396. int x, y, z;
  397. src -= SUBPEL_TAPS / 2 - 1;
  398. // This function processes 8x8 areas. The intermediate height is not always
  399. // a multiple of 8, so force it to be a multiple of 8 here.
  400. y = h + (8 - (h & 0x7));
  401. do {
  402. int x_q4 = x0_q4;
  403. for (x = 0; x < w; x += 8) {
  404. // process 8 src_x steps
  405. for (z = 0; z < 8; ++z) {
  406. const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
  407. const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
  408. if (x_q4 & SUBPEL_MASK) {
  409. filter_horiz_w8_ssse3(src_x, src_stride, temp + (z * 8), x_filter);
  410. } else {
  411. int i;
  412. for (i = 0; i < 8; ++i) {
  413. temp[z * 8 + i] = src_x[i * src_stride + 3];
  414. }
  415. }
  416. x_q4 += x_step_q4;
  417. }
  418. // transpose the 8x8 filters values back to dst
  419. transpose8x8_to_dst(temp, 8, dst + x, dst_stride);
  420. }
  421. src += src_stride * 8;
  422. dst += dst_stride * 8;
  423. } while (y -= 8);
  424. }
  425. static void filter_horiz_w4_ssse3(const uint8_t *src_ptr, ptrdiff_t src_pitch,
  426. uint8_t *dst, const int16_t *filter) {
  427. const __m128i k_256 = _mm_set1_epi16(1 << 8);
  428. const __m128i f_values = _mm_load_si128((const __m128i *)filter);
  429. // pack and duplicate the filter values
  430. const __m128i f1f0 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0200u));
  431. const __m128i f3f2 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0604u));
  432. const __m128i f5f4 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0a08u));
  433. const __m128i f7f6 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0e0cu));
  434. const __m128i A = _mm_loadl_epi64((const __m128i *)src_ptr);
  435. const __m128i B = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch));
  436. const __m128i C = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 2));
  437. const __m128i D = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 3));
  438. // TRANSPOSE...
  439. // 00 01 02 03 04 05 06 07
  440. // 10 11 12 13 14 15 16 17
  441. // 20 21 22 23 24 25 26 27
  442. // 30 31 32 33 34 35 36 37
  443. //
  444. // TO
  445. //
  446. // 00 10 20 30
  447. // 01 11 21 31
  448. // 02 12 22 32
  449. // 03 13 23 33
  450. // 04 14 24 34
  451. // 05 15 25 35
  452. // 06 16 26 36
  453. // 07 17 27 37
  454. //
  455. // 00 01 10 11 02 03 12 13 04 05 14 15 06 07 16 17
  456. const __m128i tr0_0 = _mm_unpacklo_epi16(A, B);
  457. // 20 21 30 31 22 23 32 33 24 25 34 35 26 27 36 37
  458. const __m128i tr0_1 = _mm_unpacklo_epi16(C, D);
  459. // 00 01 10 11 20 21 30 31 02 03 12 13 22 23 32 33
  460. const __m128i s1s0 = _mm_unpacklo_epi32(tr0_0, tr0_1);
  461. // 04 05 14 15 24 25 34 35 06 07 16 17 26 27 36 37
  462. const __m128i s5s4 = _mm_unpackhi_epi32(tr0_0, tr0_1);
  463. // 02 03 12 13 22 23 32 33
  464. const __m128i s3s2 = _mm_srli_si128(s1s0, 8);
  465. // 06 07 16 17 26 27 36 37
  466. const __m128i s7s6 = _mm_srli_si128(s5s4, 8);
  467. // multiply 2 adjacent elements with the filter and add the result
  468. const __m128i x0 = _mm_maddubs_epi16(s1s0, f1f0);
  469. const __m128i x1 = _mm_maddubs_epi16(s3s2, f3f2);
  470. const __m128i x2 = _mm_maddubs_epi16(s5s4, f5f4);
  471. const __m128i x3 = _mm_maddubs_epi16(s7s6, f7f6);
  472. // add and saturate the results together
  473. const __m128i min_x2x1 = _mm_min_epi16(x2, x1);
  474. const __m128i max_x2x1 = _mm_max_epi16(x2, x1);
  475. __m128i temp = _mm_adds_epi16(x0, x3);
  476. temp = _mm_adds_epi16(temp, min_x2x1);
  477. temp = _mm_adds_epi16(temp, max_x2x1);
  478. // round and shift by 7 bit each 16 bit
  479. temp = _mm_mulhrs_epi16(temp, k_256);
  480. // shrink to 8 bit each 16 bits
  481. temp = _mm_packus_epi16(temp, temp);
  482. // save only 4 bytes
  483. *(int *)dst = _mm_cvtsi128_si32(temp);
  484. }
  485. static void transpose4x4_to_dst(const uint8_t *src, ptrdiff_t src_stride,
  486. uint8_t *dst, ptrdiff_t dst_stride) {
  487. __m128i A = _mm_cvtsi32_si128(*(const int *)src);
  488. __m128i B = _mm_cvtsi32_si128(*(const int *)(src + src_stride));
  489. __m128i C = _mm_cvtsi32_si128(*(const int *)(src + src_stride * 2));
  490. __m128i D = _mm_cvtsi32_si128(*(const int *)(src + src_stride * 3));
  491. // 00 10 01 11 02 12 03 13
  492. const __m128i tr0_0 = _mm_unpacklo_epi8(A, B);
  493. // 20 30 21 31 22 32 23 33
  494. const __m128i tr0_1 = _mm_unpacklo_epi8(C, D);
  495. // 00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
  496. A = _mm_unpacklo_epi16(tr0_0, tr0_1);
  497. B = _mm_srli_si128(A, 4);
  498. C = _mm_srli_si128(A, 8);
  499. D = _mm_srli_si128(A, 12);
  500. *(int *)(dst) = _mm_cvtsi128_si32(A);
  501. *(int *)(dst + dst_stride) = _mm_cvtsi128_si32(B);
  502. *(int *)(dst + dst_stride * 2) = _mm_cvtsi128_si32(C);
  503. *(int *)(dst + dst_stride * 3) = _mm_cvtsi128_si32(D);
  504. }
  505. static void scaledconvolve_horiz_w4(const uint8_t *src, ptrdiff_t src_stride,
  506. uint8_t *dst, ptrdiff_t dst_stride,
  507. const InterpKernel *x_filters,
  508. int x0_q4, int x_step_q4, int w, int h) {
  509. DECLARE_ALIGNED(16, uint8_t, temp[4 * 4]);
  510. int x, y, z;
  511. src -= SUBPEL_TAPS / 2 - 1;
  512. for (y = 0; y < h; y += 4) {
  513. int x_q4 = x0_q4;
  514. for (x = 0; x < w; x += 4) {
  515. // process 4 src_x steps
  516. for (z = 0; z < 4; ++z) {
  517. const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
  518. const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
  519. if (x_q4 & SUBPEL_MASK) {
  520. filter_horiz_w4_ssse3(src_x, src_stride, temp + (z * 4), x_filter);
  521. } else {
  522. int i;
  523. for (i = 0; i < 4; ++i) {
  524. temp[z * 4 + i] = src_x[i * src_stride + 3];
  525. }
  526. }
  527. x_q4 += x_step_q4;
  528. }
  529. // transpose the 4x4 filters values back to dst
  530. transpose4x4_to_dst(temp, 4, dst + x, dst_stride);
  531. }
  532. src += src_stride * 4;
  533. dst += dst_stride * 4;
  534. }
  535. }
  536. static void filter_vert_w4_ssse3(const uint8_t *src_ptr, ptrdiff_t src_pitch,
  537. uint8_t *dst, const int16_t *filter) {
  538. const __m128i k_256 = _mm_set1_epi16(1 << 8);
  539. const __m128i f_values = _mm_load_si128((const __m128i *)filter);
  540. // pack and duplicate the filter values
  541. const __m128i f1f0 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0200u));
  542. const __m128i f3f2 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0604u));
  543. const __m128i f5f4 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0a08u));
  544. const __m128i f7f6 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0e0cu));
  545. const __m128i A = _mm_cvtsi32_si128(*(const int *)src_ptr);
  546. const __m128i B = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch));
  547. const __m128i C = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch * 2));
  548. const __m128i D = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch * 3));
  549. const __m128i E = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch * 4));
  550. const __m128i F = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch * 5));
  551. const __m128i G = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch * 6));
  552. const __m128i H = _mm_cvtsi32_si128(*(const int *)(src_ptr + src_pitch * 7));
  553. const __m128i s1s0 = _mm_unpacklo_epi8(A, B);
  554. const __m128i s3s2 = _mm_unpacklo_epi8(C, D);
  555. const __m128i s5s4 = _mm_unpacklo_epi8(E, F);
  556. const __m128i s7s6 = _mm_unpacklo_epi8(G, H);
  557. // multiply 2 adjacent elements with the filter and add the result
  558. const __m128i x0 = _mm_maddubs_epi16(s1s0, f1f0);
  559. const __m128i x1 = _mm_maddubs_epi16(s3s2, f3f2);
  560. const __m128i x2 = _mm_maddubs_epi16(s5s4, f5f4);
  561. const __m128i x3 = _mm_maddubs_epi16(s7s6, f7f6);
  562. // add and saturate the results together
  563. const __m128i min_x2x1 = _mm_min_epi16(x2, x1);
  564. const __m128i max_x2x1 = _mm_max_epi16(x2, x1);
  565. __m128i temp = _mm_adds_epi16(x0, x3);
  566. temp = _mm_adds_epi16(temp, min_x2x1);
  567. temp = _mm_adds_epi16(temp, max_x2x1);
  568. // round and shift by 7 bit each 16 bit
  569. temp = _mm_mulhrs_epi16(temp, k_256);
  570. // shrink to 8 bit each 16 bits
  571. temp = _mm_packus_epi16(temp, temp);
  572. // save only 4 bytes
  573. *(int *)dst = _mm_cvtsi128_si32(temp);
  574. }
  575. static void scaledconvolve_vert_w4(const uint8_t *src, ptrdiff_t src_stride,
  576. uint8_t *dst, ptrdiff_t dst_stride,
  577. const InterpKernel *y_filters,
  578. int y0_q4, int y_step_q4, int w, int h) {
  579. int y;
  580. int y_q4 = y0_q4;
  581. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  582. for (y = 0; y < h; ++y) {
  583. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  584. const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
  585. if (y_q4 & SUBPEL_MASK) {
  586. filter_vert_w4_ssse3(src_y, src_stride, &dst[y * dst_stride], y_filter);
  587. } else {
  588. memcpy(&dst[y * dst_stride], &src_y[3 * src_stride], w);
  589. }
  590. y_q4 += y_step_q4;
  591. }
  592. }
  593. static void filter_vert_w8_ssse3(const uint8_t *src_ptr, ptrdiff_t src_pitch,
  594. uint8_t *dst, const int16_t *filter) {
  595. const __m128i k_256 = _mm_set1_epi16(1 << 8);
  596. const __m128i f_values = _mm_load_si128((const __m128i *)filter);
  597. // pack and duplicate the filter values
  598. const __m128i f1f0 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0200u));
  599. const __m128i f3f2 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0604u));
  600. const __m128i f5f4 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0a08u));
  601. const __m128i f7f6 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0e0cu));
  602. const __m128i A = _mm_loadl_epi64((const __m128i *)src_ptr);
  603. const __m128i B = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch));
  604. const __m128i C = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 2));
  605. const __m128i D = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 3));
  606. const __m128i E = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 4));
  607. const __m128i F = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 5));
  608. const __m128i G = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 6));
  609. const __m128i H = _mm_loadl_epi64((const __m128i *)(src_ptr + src_pitch * 7));
  610. const __m128i s1s0 = _mm_unpacklo_epi8(A, B);
  611. const __m128i s3s2 = _mm_unpacklo_epi8(C, D);
  612. const __m128i s5s4 = _mm_unpacklo_epi8(E, F);
  613. const __m128i s7s6 = _mm_unpacklo_epi8(G, H);
  614. // multiply 2 adjacent elements with the filter and add the result
  615. const __m128i x0 = _mm_maddubs_epi16(s1s0, f1f0);
  616. const __m128i x1 = _mm_maddubs_epi16(s3s2, f3f2);
  617. const __m128i x2 = _mm_maddubs_epi16(s5s4, f5f4);
  618. const __m128i x3 = _mm_maddubs_epi16(s7s6, f7f6);
  619. // add and saturate the results together
  620. const __m128i min_x2x1 = _mm_min_epi16(x2, x1);
  621. const __m128i max_x2x1 = _mm_max_epi16(x2, x1);
  622. __m128i temp = _mm_adds_epi16(x0, x3);
  623. temp = _mm_adds_epi16(temp, min_x2x1);
  624. temp = _mm_adds_epi16(temp, max_x2x1);
  625. // round and shift by 7 bit each 16 bit
  626. temp = _mm_mulhrs_epi16(temp, k_256);
  627. // shrink to 8 bit each 16 bits
  628. temp = _mm_packus_epi16(temp, temp);
  629. // save only 8 bytes convolve result
  630. _mm_storel_epi64((__m128i*)dst, temp);
  631. }
  632. static void scaledconvolve_vert_w8(const uint8_t *src, ptrdiff_t src_stride,
  633. uint8_t *dst, ptrdiff_t dst_stride,
  634. const InterpKernel *y_filters,
  635. int y0_q4, int y_step_q4, int w, int h) {
  636. int y;
  637. int y_q4 = y0_q4;
  638. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  639. for (y = 0; y < h; ++y) {
  640. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  641. const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
  642. if (y_q4 & SUBPEL_MASK) {
  643. filter_vert_w8_ssse3(src_y, src_stride, &dst[y * dst_stride], y_filter);
  644. } else {
  645. memcpy(&dst[y * dst_stride], &src_y[3 * src_stride], w);
  646. }
  647. y_q4 += y_step_q4;
  648. }
  649. }
  650. static void filter_vert_w16_ssse3(const uint8_t *src_ptr, ptrdiff_t src_pitch,
  651. uint8_t *dst, const int16_t *filter, int w) {
  652. const __m128i k_256 = _mm_set1_epi16(1 << 8);
  653. const __m128i f_values = _mm_load_si128((const __m128i *)filter);
  654. // pack and duplicate the filter values
  655. const __m128i f1f0 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0200u));
  656. const __m128i f3f2 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0604u));
  657. const __m128i f5f4 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0a08u));
  658. const __m128i f7f6 = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0e0cu));
  659. int i;
  660. for (i = 0; i < w; i += 16) {
  661. const __m128i A = _mm_loadu_si128((const __m128i *)src_ptr);
  662. const __m128i B = _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch));
  663. const __m128i C =
  664. _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 2));
  665. const __m128i D =
  666. _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 3));
  667. const __m128i E =
  668. _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 4));
  669. const __m128i F =
  670. _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 5));
  671. const __m128i G =
  672. _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 6));
  673. const __m128i H =
  674. _mm_loadu_si128((const __m128i *)(src_ptr + src_pitch * 7));
  675. // merge the result together
  676. const __m128i s1s0_lo = _mm_unpacklo_epi8(A, B);
  677. const __m128i s7s6_lo = _mm_unpacklo_epi8(G, H);
  678. const __m128i s1s0_hi = _mm_unpackhi_epi8(A, B);
  679. const __m128i s7s6_hi = _mm_unpackhi_epi8(G, H);
  680. // multiply 2 adjacent elements with the filter and add the result
  681. const __m128i x0_lo = _mm_maddubs_epi16(s1s0_lo, f1f0);
  682. const __m128i x3_lo = _mm_maddubs_epi16(s7s6_lo, f7f6);
  683. const __m128i x0_hi = _mm_maddubs_epi16(s1s0_hi, f1f0);
  684. const __m128i x3_hi = _mm_maddubs_epi16(s7s6_hi, f7f6);
  685. // add and saturate the results together
  686. const __m128i x3x0_lo = _mm_adds_epi16(x0_lo, x3_lo);
  687. const __m128i x3x0_hi = _mm_adds_epi16(x0_hi, x3_hi);
  688. // merge the result together
  689. const __m128i s3s2_lo = _mm_unpacklo_epi8(C, D);
  690. const __m128i s3s2_hi = _mm_unpackhi_epi8(C, D);
  691. // multiply 2 adjacent elements with the filter and add the result
  692. const __m128i x1_lo = _mm_maddubs_epi16(s3s2_lo, f3f2);
  693. const __m128i x1_hi = _mm_maddubs_epi16(s3s2_hi, f3f2);
  694. // merge the result together
  695. const __m128i s5s4_lo = _mm_unpacklo_epi8(E, F);
  696. const __m128i s5s4_hi = _mm_unpackhi_epi8(E, F);
  697. // multiply 2 adjacent elements with the filter and add the result
  698. const __m128i x2_lo = _mm_maddubs_epi16(s5s4_lo, f5f4);
  699. const __m128i x2_hi = _mm_maddubs_epi16(s5s4_hi, f5f4);
  700. // add and saturate the results together
  701. __m128i temp_lo = _mm_adds_epi16(x3x0_lo, _mm_min_epi16(x1_lo, x2_lo));
  702. __m128i temp_hi = _mm_adds_epi16(x3x0_hi, _mm_min_epi16(x1_hi, x2_hi));
  703. // add and saturate the results together
  704. temp_lo = _mm_adds_epi16(temp_lo, _mm_max_epi16(x1_lo, x2_lo));
  705. temp_hi = _mm_adds_epi16(temp_hi, _mm_max_epi16(x1_hi, x2_hi));
  706. // round and shift by 7 bit each 16 bit
  707. temp_lo = _mm_mulhrs_epi16(temp_lo, k_256);
  708. temp_hi = _mm_mulhrs_epi16(temp_hi, k_256);
  709. // shrink to 8 bit each 16 bits, the first lane contain the first
  710. // convolve result and the second lane contain the second convolve
  711. // result
  712. temp_hi = _mm_packus_epi16(temp_lo, temp_hi);
  713. src_ptr += 16;
  714. // save 16 bytes convolve result
  715. _mm_store_si128((__m128i*)&dst[i], temp_hi);
  716. }
  717. }
  718. static void scaledconvolve_vert_w16(const uint8_t *src, ptrdiff_t src_stride,
  719. uint8_t *dst, ptrdiff_t dst_stride,
  720. const InterpKernel *y_filters,
  721. int y0_q4, int y_step_q4, int w, int h) {
  722. int y;
  723. int y_q4 = y0_q4;
  724. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  725. for (y = 0; y < h; ++y) {
  726. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  727. const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
  728. if (y_q4 & SUBPEL_MASK) {
  729. filter_vert_w16_ssse3(src_y, src_stride, &dst[y * dst_stride], y_filter,
  730. w);
  731. } else {
  732. memcpy(&dst[y * dst_stride], &src_y[3 * src_stride], w);
  733. }
  734. y_q4 += y_step_q4;
  735. }
  736. }
  737. static void scaledconvolve2d(const uint8_t *src, ptrdiff_t src_stride,
  738. uint8_t *dst, ptrdiff_t dst_stride,
  739. const InterpKernel *const x_filters,
  740. int x0_q4, int x_step_q4,
  741. const InterpKernel *const y_filters,
  742. int y0_q4, int y_step_q4,
  743. int w, int h) {
  744. // Note: Fixed size intermediate buffer, temp, places limits on parameters.
  745. // 2d filtering proceeds in 2 steps:
  746. // (1) Interpolate horizontally into an intermediate buffer, temp.
  747. // (2) Interpolate temp vertically to derive the sub-pixel result.
  748. // Deriving the maximum number of rows in the temp buffer (135):
  749. // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
  750. // --Largest block size is 64x64 pixels.
  751. // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
  752. // original frame (in 1/16th pixel units).
  753. // --Must round-up because block may be located at sub-pixel position.
  754. // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
  755. // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
  756. // --Require an additional 8 rows for the horiz_w8 transpose tail.
  757. DECLARE_ALIGNED(16, uint8_t, temp[(135 + 8) * 64]);
  758. const int intermediate_height =
  759. (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
  760. assert(w <= 64);
  761. assert(h <= 64);
  762. assert(y_step_q4 <= 32);
  763. assert(x_step_q4 <= 32);
  764. if (w >= 8) {
  765. scaledconvolve_horiz_w8(src - src_stride * (SUBPEL_TAPS / 2 - 1),
  766. src_stride, temp, 64, x_filters, x0_q4, x_step_q4,
  767. w, intermediate_height);
  768. } else {
  769. scaledconvolve_horiz_w4(src - src_stride * (SUBPEL_TAPS / 2 - 1),
  770. src_stride, temp, 64, x_filters, x0_q4, x_step_q4,
  771. w, intermediate_height);
  772. }
  773. if (w >= 16) {
  774. scaledconvolve_vert_w16(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  775. dst_stride, y_filters, y0_q4, y_step_q4, w, h);
  776. } else if (w == 8) {
  777. scaledconvolve_vert_w8(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  778. dst_stride, y_filters, y0_q4, y_step_q4, w, h);
  779. } else {
  780. scaledconvolve_vert_w4(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  781. dst_stride, y_filters, y0_q4, y_step_q4, w, h);
  782. }
  783. }
  784. static const InterpKernel *get_filter_base(const int16_t *filter) {
  785. // NOTE: This assumes that the filter table is 256-byte aligned.
  786. // TODO(agrange) Modify to make independent of table alignment.
  787. return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
  788. }
  789. static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
  790. return (int)((const InterpKernel *)(intptr_t)f - base);
  791. }
  792. void vpx_scaled_2d_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  793. uint8_t *dst, ptrdiff_t dst_stride,
  794. const int16_t *filter_x, int x_step_q4,
  795. const int16_t *filter_y, int y_step_q4,
  796. int w, int h) {
  797. const InterpKernel *const filters_x = get_filter_base(filter_x);
  798. const int x0_q4 = get_filter_offset(filter_x, filters_x);
  799. const InterpKernel *const filters_y = get_filter_base(filter_y);
  800. const int y0_q4 = get_filter_offset(filter_y, filters_y);
  801. scaledconvolve2d(src, src_stride, dst, dst_stride,
  802. filters_x, x0_q4, x_step_q4,
  803. filters_y, y0_q4, y_step_q4, w, h);
  804. }
  805. // void vp9_convolve8_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  806. // uint8_t *dst, ptrdiff_t dst_stride,
  807. // const int16_t *filter_x, int x_step_q4,
  808. // const int16_t *filter_y, int y_step_q4,
  809. // int w, int h);
  810. // void vpx_convolve8_avg_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  811. // uint8_t *dst, ptrdiff_t dst_stride,
  812. // const int16_t *filter_x, int x_step_q4,
  813. // const int16_t *filter_y, int y_step_q4,
  814. // int w, int h);
  815. FUN_CONV_2D(, ssse3);
  816. FUN_CONV_2D(avg_ , ssse3);