vpx_subpixel_8t_intrin_ssse3.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <tmmintrin.h> // SSSE3
  11. #include <string.h>
  12. #include "./vpx_config.h"
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "vpx_dsp/vpx_filter.h"
  15. #include "vpx_dsp/x86/convolve.h"
  16. #include "vpx_dsp/x86/convolve_sse2.h"
  17. #include "vpx_dsp/x86/convolve_ssse3.h"
  18. #include "vpx_dsp/x86/mem_sse2.h"
  19. #include "vpx_dsp/x86/transpose_sse2.h"
  20. #include "vpx_mem/vpx_mem.h"
  21. #include "vpx_ports/mem.h"
  22. static INLINE __m128i shuffle_filter_convolve8_8_ssse3(
  23. const __m128i *const s, const int16_t *const filter) {
  24. __m128i f[4];
  25. shuffle_filter_ssse3(filter, f);
  26. return convolve8_8_ssse3(s, f);
  27. }
  28. // Used by the avx2 implementation.
  29. #if ARCH_X86_64
  30. // Use the intrinsics below
  31. filter8_1dfunction vpx_filter_block1d4_h8_intrin_ssse3;
  32. filter8_1dfunction vpx_filter_block1d8_h8_intrin_ssse3;
  33. filter8_1dfunction vpx_filter_block1d8_v8_intrin_ssse3;
  34. #define vpx_filter_block1d4_h8_ssse3 vpx_filter_block1d4_h8_intrin_ssse3
  35. #define vpx_filter_block1d8_h8_ssse3 vpx_filter_block1d8_h8_intrin_ssse3
  36. #define vpx_filter_block1d8_v8_ssse3 vpx_filter_block1d8_v8_intrin_ssse3
  37. #else // ARCH_X86
  38. // Use the assembly in vpx_dsp/x86/vpx_subpixel_8t_ssse3.asm.
  39. filter8_1dfunction vpx_filter_block1d4_h8_ssse3;
  40. filter8_1dfunction vpx_filter_block1d8_h8_ssse3;
  41. filter8_1dfunction vpx_filter_block1d8_v8_ssse3;
  42. #endif
  43. #if ARCH_X86_64
  44. void vpx_filter_block1d4_h8_intrin_ssse3(
  45. const uint8_t *src_ptr, ptrdiff_t src_pitch, uint8_t *output_ptr,
  46. ptrdiff_t output_pitch, uint32_t output_height, const int16_t *filter) {
  47. __m128i firstFilters, secondFilters, shuffle1, shuffle2;
  48. __m128i srcRegFilt1, srcRegFilt2;
  49. __m128i addFilterReg64, filtersReg, srcReg;
  50. unsigned int i;
  51. // create a register with 0,64,0,64,0,64,0,64,0,64,0,64,0,64,0,64
  52. addFilterReg64 = _mm_set1_epi32((int)0x0400040u);
  53. filtersReg = _mm_loadu_si128((const __m128i *)filter);
  54. // converting the 16 bit (short) to 8 bit (byte) and have the same data
  55. // in both lanes of 128 bit register.
  56. filtersReg = _mm_packs_epi16(filtersReg, filtersReg);
  57. // duplicate only the first 16 bits in the filter into the first lane
  58. firstFilters = _mm_shufflelo_epi16(filtersReg, 0);
  59. // duplicate only the third 16 bit in the filter into the first lane
  60. secondFilters = _mm_shufflelo_epi16(filtersReg, 0xAAu);
  61. // duplicate only the seconds 16 bits in the filter into the second lane
  62. // firstFilters: k0 k1 k0 k1 k0 k1 k0 k1 k2 k3 k2 k3 k2 k3 k2 k3
  63. firstFilters = _mm_shufflehi_epi16(firstFilters, 0x55u);
  64. // duplicate only the forth 16 bits in the filter into the second lane
  65. // secondFilters: k4 k5 k4 k5 k4 k5 k4 k5 k6 k7 k6 k7 k6 k7 k6 k7
  66. secondFilters = _mm_shufflehi_epi16(secondFilters, 0xFFu);
  67. // loading the local filters
  68. shuffle1 = _mm_setr_epi8(0, 1, 1, 2, 2, 3, 3, 4, 2, 3, 3, 4, 4, 5, 5, 6);
  69. shuffle2 = _mm_setr_epi8(4, 5, 5, 6, 6, 7, 7, 8, 6, 7, 7, 8, 8, 9, 9, 10);
  70. for (i = 0; i < output_height; i++) {
  71. srcReg = _mm_loadu_si128((const __m128i *)(src_ptr - 3));
  72. // filter the source buffer
  73. srcRegFilt1 = _mm_shuffle_epi8(srcReg, shuffle1);
  74. srcRegFilt2 = _mm_shuffle_epi8(srcReg, shuffle2);
  75. // multiply 2 adjacent elements with the filter and add the result
  76. srcRegFilt1 = _mm_maddubs_epi16(srcRegFilt1, firstFilters);
  77. srcRegFilt2 = _mm_maddubs_epi16(srcRegFilt2, secondFilters);
  78. // sum the results together, saturating only on the final step
  79. // the specific order of the additions prevents outranges
  80. srcRegFilt1 = _mm_add_epi16(srcRegFilt1, srcRegFilt2);
  81. // extract the higher half of the register
  82. srcRegFilt2 = _mm_srli_si128(srcRegFilt1, 8);
  83. // add the rounding offset early to avoid another saturated add
  84. srcRegFilt1 = _mm_add_epi16(srcRegFilt1, addFilterReg64);
  85. srcRegFilt1 = _mm_adds_epi16(srcRegFilt1, srcRegFilt2);
  86. // shift by 7 bit each 16 bits
  87. srcRegFilt1 = _mm_srai_epi16(srcRegFilt1, 7);
  88. // shrink to 8 bit each 16 bits
  89. srcRegFilt1 = _mm_packus_epi16(srcRegFilt1, srcRegFilt1);
  90. src_ptr += src_pitch;
  91. // save only 4 bytes
  92. *((int *)&output_ptr[0]) = _mm_cvtsi128_si32(srcRegFilt1);
  93. output_ptr += output_pitch;
  94. }
  95. }
  96. void vpx_filter_block1d8_h8_intrin_ssse3(
  97. const uint8_t *src_ptr, ptrdiff_t src_pitch, uint8_t *output_ptr,
  98. ptrdiff_t output_pitch, uint32_t output_height, const int16_t *filter) {
  99. unsigned int i;
  100. __m128i f[4], filt[4], s[4];
  101. shuffle_filter_ssse3(filter, f);
  102. filt[0] = _mm_setr_epi8(0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8);
  103. filt[1] = _mm_setr_epi8(2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10);
  104. filt[2] = _mm_setr_epi8(4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12);
  105. filt[3] =
  106. _mm_setr_epi8(6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14);
  107. for (i = 0; i < output_height; i++) {
  108. const __m128i srcReg = _mm_loadu_si128((const __m128i *)(src_ptr - 3));
  109. // filter the source buffer
  110. s[0] = _mm_shuffle_epi8(srcReg, filt[0]);
  111. s[1] = _mm_shuffle_epi8(srcReg, filt[1]);
  112. s[2] = _mm_shuffle_epi8(srcReg, filt[2]);
  113. s[3] = _mm_shuffle_epi8(srcReg, filt[3]);
  114. s[0] = convolve8_8_ssse3(s, f);
  115. // shrink to 8 bit each 16 bits
  116. s[0] = _mm_packus_epi16(s[0], s[0]);
  117. src_ptr += src_pitch;
  118. // save only 8 bytes
  119. _mm_storel_epi64((__m128i *)&output_ptr[0], s[0]);
  120. output_ptr += output_pitch;
  121. }
  122. }
  123. void vpx_filter_block1d8_v8_intrin_ssse3(
  124. const uint8_t *src_ptr, ptrdiff_t src_pitch, uint8_t *output_ptr,
  125. ptrdiff_t out_pitch, uint32_t output_height, const int16_t *filter) {
  126. unsigned int i;
  127. __m128i f[4], s[8], ss[4];
  128. shuffle_filter_ssse3(filter, f);
  129. // load the first 7 rows of 8 bytes
  130. s[0] = _mm_loadl_epi64((const __m128i *)(src_ptr + 0 * src_pitch));
  131. s[1] = _mm_loadl_epi64((const __m128i *)(src_ptr + 1 * src_pitch));
  132. s[2] = _mm_loadl_epi64((const __m128i *)(src_ptr + 2 * src_pitch));
  133. s[3] = _mm_loadl_epi64((const __m128i *)(src_ptr + 3 * src_pitch));
  134. s[4] = _mm_loadl_epi64((const __m128i *)(src_ptr + 4 * src_pitch));
  135. s[5] = _mm_loadl_epi64((const __m128i *)(src_ptr + 5 * src_pitch));
  136. s[6] = _mm_loadl_epi64((const __m128i *)(src_ptr + 6 * src_pitch));
  137. for (i = 0; i < output_height; i++) {
  138. // load the last 8 bytes
  139. s[7] = _mm_loadl_epi64((const __m128i *)(src_ptr + 7 * src_pitch));
  140. // merge the result together
  141. ss[0] = _mm_unpacklo_epi8(s[0], s[1]);
  142. ss[1] = _mm_unpacklo_epi8(s[2], s[3]);
  143. // merge the result together
  144. ss[2] = _mm_unpacklo_epi8(s[4], s[5]);
  145. ss[3] = _mm_unpacklo_epi8(s[6], s[7]);
  146. ss[0] = convolve8_8_ssse3(ss, f);
  147. // shrink to 8 bit each 16 bits
  148. ss[0] = _mm_packus_epi16(ss[0], ss[0]);
  149. src_ptr += src_pitch;
  150. // shift down a row
  151. s[0] = s[1];
  152. s[1] = s[2];
  153. s[2] = s[3];
  154. s[3] = s[4];
  155. s[4] = s[5];
  156. s[5] = s[6];
  157. s[6] = s[7];
  158. // save only 8 bytes convolve result
  159. _mm_storel_epi64((__m128i *)&output_ptr[0], ss[0]);
  160. output_ptr += out_pitch;
  161. }
  162. }
  163. #endif // ARCH_X86_64
  164. static void vpx_filter_block1d16_h4_ssse3(const uint8_t *src_ptr,
  165. ptrdiff_t src_stride,
  166. uint8_t *dst_ptr,
  167. ptrdiff_t dst_stride, uint32_t height,
  168. const int16_t *kernel) {
  169. // We will cast the kernel from 16-bit words to 8-bit words, and then extract
  170. // the middle four elements of the kernel into two registers in the form
  171. // ... k[3] k[2] k[3] k[2]
  172. // ... k[5] k[4] k[5] k[4]
  173. // Then we shuffle the source into
  174. // ... s[1] s[0] s[0] s[-1]
  175. // ... s[3] s[2] s[2] s[1]
  176. // Calling multiply and add gives us half of the sum. Calling add gives us
  177. // first half of the output. Repeat again to get the second half of the
  178. // output. Finally we shuffle again to combine the two outputs.
  179. __m128i kernel_reg; // Kernel
  180. __m128i kernel_reg_23, kernel_reg_45; // Segments of the kernel used
  181. const __m128i reg_32 = _mm_set1_epi16(32); // Used for rounding
  182. int h;
  183. __m128i src_reg, src_reg_shift_0, src_reg_shift_2;
  184. __m128i dst_first, dst_second;
  185. __m128i tmp_0, tmp_1;
  186. __m128i idx_shift_0 =
  187. _mm_setr_epi8(0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8);
  188. __m128i idx_shift_2 =
  189. _mm_setr_epi8(2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10);
  190. // Start one pixel before as we need tap/2 - 1 = 1 sample from the past
  191. src_ptr -= 1;
  192. // Load Kernel
  193. kernel_reg = _mm_loadu_si128((const __m128i *)kernel);
  194. kernel_reg = _mm_srai_epi16(kernel_reg, 1);
  195. kernel_reg = _mm_packs_epi16(kernel_reg, kernel_reg);
  196. kernel_reg_23 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0302u));
  197. kernel_reg_45 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0504u));
  198. for (h = height; h > 0; --h) {
  199. // Load the source
  200. src_reg = _mm_loadu_si128((const __m128i *)src_ptr);
  201. src_reg_shift_0 = _mm_shuffle_epi8(src_reg, idx_shift_0);
  202. src_reg_shift_2 = _mm_shuffle_epi8(src_reg, idx_shift_2);
  203. // Partial result for first half
  204. tmp_0 = _mm_maddubs_epi16(src_reg_shift_0, kernel_reg_23);
  205. tmp_1 = _mm_maddubs_epi16(src_reg_shift_2, kernel_reg_45);
  206. dst_first = _mm_adds_epi16(tmp_0, tmp_1);
  207. // Do again to get the second half of dst
  208. // Load the source
  209. src_reg = _mm_loadu_si128((const __m128i *)(src_ptr + 8));
  210. src_reg_shift_0 = _mm_shuffle_epi8(src_reg, idx_shift_0);
  211. src_reg_shift_2 = _mm_shuffle_epi8(src_reg, idx_shift_2);
  212. // Partial result for first half
  213. tmp_0 = _mm_maddubs_epi16(src_reg_shift_0, kernel_reg_23);
  214. tmp_1 = _mm_maddubs_epi16(src_reg_shift_2, kernel_reg_45);
  215. dst_second = _mm_adds_epi16(tmp_0, tmp_1);
  216. // Round each result
  217. dst_first = mm_round_epi16_sse2(&dst_first, &reg_32, 6);
  218. dst_second = mm_round_epi16_sse2(&dst_second, &reg_32, 6);
  219. // Finally combine to get the final dst
  220. dst_first = _mm_packus_epi16(dst_first, dst_second);
  221. _mm_store_si128((__m128i *)dst_ptr, dst_first);
  222. src_ptr += src_stride;
  223. dst_ptr += dst_stride;
  224. }
  225. }
  226. static void vpx_filter_block1d16_v4_ssse3(const uint8_t *src_ptr,
  227. ptrdiff_t src_stride,
  228. uint8_t *dst_ptr,
  229. ptrdiff_t dst_stride, uint32_t height,
  230. const int16_t *kernel) {
  231. // We will load two rows of pixels as 8-bit words, rearrange them into the
  232. // form
  233. // ... s[0,1] s[-1,1] s[0,0] s[-1,0]
  234. // ... s[0,9] s[-1,9] s[0,8] s[-1,8]
  235. // so that we can call multiply and add with the kernel to get 16-bit words of
  236. // the form
  237. // ... s[0,1]k[3]+s[-1,1]k[2] s[0,0]k[3]+s[-1,0]k[2]
  238. // Finally, we can add multiple rows together to get the desired output.
  239. // Register for source s[-1:3, :]
  240. __m128i src_reg_m1, src_reg_0, src_reg_1, src_reg_2, src_reg_3;
  241. // Interleaved rows of the source. lo is first half, hi second
  242. __m128i src_reg_m10_lo, src_reg_m10_hi, src_reg_01_lo, src_reg_01_hi;
  243. __m128i src_reg_12_lo, src_reg_12_hi, src_reg_23_lo, src_reg_23_hi;
  244. __m128i kernel_reg; // Kernel
  245. __m128i kernel_reg_23, kernel_reg_45; // Segments of the kernel used
  246. // Result after multiply and add
  247. __m128i res_reg_m10_lo, res_reg_01_lo, res_reg_12_lo, res_reg_23_lo;
  248. __m128i res_reg_m10_hi, res_reg_01_hi, res_reg_12_hi, res_reg_23_hi;
  249. __m128i res_reg_m1012, res_reg_0123;
  250. __m128i res_reg_m1012_lo, res_reg_0123_lo, res_reg_m1012_hi, res_reg_0123_hi;
  251. const __m128i reg_32 = _mm_set1_epi16(32); // Used for rounding
  252. // We will compute the result two rows at a time
  253. const ptrdiff_t src_stride_unrolled = src_stride << 1;
  254. const ptrdiff_t dst_stride_unrolled = dst_stride << 1;
  255. int h;
  256. // Load Kernel
  257. kernel_reg = _mm_loadu_si128((const __m128i *)kernel);
  258. kernel_reg = _mm_srai_epi16(kernel_reg, 1);
  259. kernel_reg = _mm_packs_epi16(kernel_reg, kernel_reg);
  260. kernel_reg_23 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0302u));
  261. kernel_reg_45 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0504u));
  262. // First shuffle the data
  263. src_reg_m1 = _mm_loadu_si128((const __m128i *)src_ptr);
  264. src_reg_0 = _mm_loadu_si128((const __m128i *)(src_ptr + src_stride));
  265. src_reg_m10_lo = _mm_unpacklo_epi8(src_reg_m1, src_reg_0);
  266. src_reg_m10_hi = _mm_unpackhi_epi8(src_reg_m1, src_reg_0);
  267. // More shuffling
  268. src_reg_1 = _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 2));
  269. src_reg_01_lo = _mm_unpacklo_epi8(src_reg_0, src_reg_1);
  270. src_reg_01_hi = _mm_unpackhi_epi8(src_reg_0, src_reg_1);
  271. for (h = height; h > 1; h -= 2) {
  272. src_reg_2 = _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 3));
  273. src_reg_12_lo = _mm_unpacklo_epi8(src_reg_1, src_reg_2);
  274. src_reg_12_hi = _mm_unpackhi_epi8(src_reg_1, src_reg_2);
  275. src_reg_3 = _mm_loadu_si128((const __m128i *)(src_ptr + src_stride * 4));
  276. src_reg_23_lo = _mm_unpacklo_epi8(src_reg_2, src_reg_3);
  277. src_reg_23_hi = _mm_unpackhi_epi8(src_reg_2, src_reg_3);
  278. // Partial output from first half
  279. res_reg_m10_lo = _mm_maddubs_epi16(src_reg_m10_lo, kernel_reg_23);
  280. res_reg_01_lo = _mm_maddubs_epi16(src_reg_01_lo, kernel_reg_23);
  281. res_reg_12_lo = _mm_maddubs_epi16(src_reg_12_lo, kernel_reg_45);
  282. res_reg_23_lo = _mm_maddubs_epi16(src_reg_23_lo, kernel_reg_45);
  283. // Add to get first half of the results
  284. res_reg_m1012_lo = _mm_adds_epi16(res_reg_m10_lo, res_reg_12_lo);
  285. res_reg_0123_lo = _mm_adds_epi16(res_reg_01_lo, res_reg_23_lo);
  286. // Partial output for second half
  287. res_reg_m10_hi = _mm_maddubs_epi16(src_reg_m10_hi, kernel_reg_23);
  288. res_reg_01_hi = _mm_maddubs_epi16(src_reg_01_hi, kernel_reg_23);
  289. res_reg_12_hi = _mm_maddubs_epi16(src_reg_12_hi, kernel_reg_45);
  290. res_reg_23_hi = _mm_maddubs_epi16(src_reg_23_hi, kernel_reg_45);
  291. // Second half of the results
  292. res_reg_m1012_hi = _mm_adds_epi16(res_reg_m10_hi, res_reg_12_hi);
  293. res_reg_0123_hi = _mm_adds_epi16(res_reg_01_hi, res_reg_23_hi);
  294. // Round the words
  295. res_reg_m1012_lo = mm_round_epi16_sse2(&res_reg_m1012_lo, &reg_32, 6);
  296. res_reg_0123_lo = mm_round_epi16_sse2(&res_reg_0123_lo, &reg_32, 6);
  297. res_reg_m1012_hi = mm_round_epi16_sse2(&res_reg_m1012_hi, &reg_32, 6);
  298. res_reg_0123_hi = mm_round_epi16_sse2(&res_reg_0123_hi, &reg_32, 6);
  299. // Combine to get the result
  300. res_reg_m1012 = _mm_packus_epi16(res_reg_m1012_lo, res_reg_m1012_hi);
  301. res_reg_0123 = _mm_packus_epi16(res_reg_0123_lo, res_reg_0123_hi);
  302. _mm_store_si128((__m128i *)dst_ptr, res_reg_m1012);
  303. _mm_store_si128((__m128i *)(dst_ptr + dst_stride), res_reg_0123);
  304. // Update the source by two rows
  305. src_ptr += src_stride_unrolled;
  306. dst_ptr += dst_stride_unrolled;
  307. src_reg_m10_lo = src_reg_12_lo;
  308. src_reg_m10_hi = src_reg_12_hi;
  309. src_reg_01_lo = src_reg_23_lo;
  310. src_reg_01_hi = src_reg_23_hi;
  311. src_reg_1 = src_reg_3;
  312. }
  313. }
  314. static void vpx_filter_block1d8_h4_ssse3(const uint8_t *src_ptr,
  315. ptrdiff_t src_stride, uint8_t *dst_ptr,
  316. ptrdiff_t dst_stride, uint32_t height,
  317. const int16_t *kernel) {
  318. // We will cast the kernel from 16-bit words to 8-bit words, and then extract
  319. // the middle four elements of the kernel into two registers in the form
  320. // ... k[3] k[2] k[3] k[2]
  321. // ... k[5] k[4] k[5] k[4]
  322. // Then we shuffle the source into
  323. // ... s[1] s[0] s[0] s[-1]
  324. // ... s[3] s[2] s[2] s[1]
  325. // Calling multiply and add gives us half of the sum. Calling add gives us
  326. // first half of the output. Repeat again to get the second half of the
  327. // output. Finally we shuffle again to combine the two outputs.
  328. __m128i kernel_reg; // Kernel
  329. __m128i kernel_reg_23, kernel_reg_45; // Segments of the kernel used
  330. const __m128i reg_32 = _mm_set1_epi16(32); // Used for rounding
  331. int h;
  332. __m128i src_reg, src_reg_shift_0, src_reg_shift_2;
  333. __m128i dst_first;
  334. __m128i tmp_0, tmp_1;
  335. __m128i idx_shift_0 =
  336. _mm_setr_epi8(0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8);
  337. __m128i idx_shift_2 =
  338. _mm_setr_epi8(2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10);
  339. // Start one pixel before as we need tap/2 - 1 = 1 sample from the past
  340. src_ptr -= 1;
  341. // Load Kernel
  342. kernel_reg = _mm_loadu_si128((const __m128i *)kernel);
  343. kernel_reg = _mm_srai_epi16(kernel_reg, 1);
  344. kernel_reg = _mm_packs_epi16(kernel_reg, kernel_reg);
  345. kernel_reg_23 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0302u));
  346. kernel_reg_45 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0504u));
  347. for (h = height; h > 0; --h) {
  348. // Load the source
  349. src_reg = _mm_loadu_si128((const __m128i *)src_ptr);
  350. src_reg_shift_0 = _mm_shuffle_epi8(src_reg, idx_shift_0);
  351. src_reg_shift_2 = _mm_shuffle_epi8(src_reg, idx_shift_2);
  352. // Get the result
  353. tmp_0 = _mm_maddubs_epi16(src_reg_shift_0, kernel_reg_23);
  354. tmp_1 = _mm_maddubs_epi16(src_reg_shift_2, kernel_reg_45);
  355. dst_first = _mm_adds_epi16(tmp_0, tmp_1);
  356. // Round round result
  357. dst_first = mm_round_epi16_sse2(&dst_first, &reg_32, 6);
  358. // Pack to 8-bits
  359. dst_first = _mm_packus_epi16(dst_first, _mm_setzero_si128());
  360. _mm_storel_epi64((__m128i *)dst_ptr, dst_first);
  361. src_ptr += src_stride;
  362. dst_ptr += dst_stride;
  363. }
  364. }
  365. static void vpx_filter_block1d8_v4_ssse3(const uint8_t *src_ptr,
  366. ptrdiff_t src_stride, uint8_t *dst_ptr,
  367. ptrdiff_t dst_stride, uint32_t height,
  368. const int16_t *kernel) {
  369. // We will load two rows of pixels as 8-bit words, rearrange them into the
  370. // form
  371. // ... s[0,1] s[-1,1] s[0,0] s[-1,0]
  372. // so that we can call multiply and add with the kernel to get 16-bit words of
  373. // the form
  374. // ... s[0,1]k[3]+s[-1,1]k[2] s[0,0]k[3]+s[-1,0]k[2]
  375. // Finally, we can add multiple rows together to get the desired output.
  376. // Register for source s[-1:3, :]
  377. __m128i src_reg_m1, src_reg_0, src_reg_1, src_reg_2, src_reg_3;
  378. // Interleaved rows of the source. lo is first half, hi second
  379. __m128i src_reg_m10, src_reg_01;
  380. __m128i src_reg_12, src_reg_23;
  381. __m128i kernel_reg; // Kernel
  382. __m128i kernel_reg_23, kernel_reg_45; // Segments of the kernel used
  383. // Result after multiply and add
  384. __m128i res_reg_m10, res_reg_01, res_reg_12, res_reg_23;
  385. __m128i res_reg_m1012, res_reg_0123;
  386. const __m128i reg_32 = _mm_set1_epi16(32); // Used for rounding
  387. // We will compute the result two rows at a time
  388. const ptrdiff_t src_stride_unrolled = src_stride << 1;
  389. const ptrdiff_t dst_stride_unrolled = dst_stride << 1;
  390. int h;
  391. // Load Kernel
  392. kernel_reg = _mm_loadu_si128((const __m128i *)kernel);
  393. kernel_reg = _mm_srai_epi16(kernel_reg, 1);
  394. kernel_reg = _mm_packs_epi16(kernel_reg, kernel_reg);
  395. kernel_reg_23 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0302u));
  396. kernel_reg_45 = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi16(0x0504u));
  397. // First shuffle the data
  398. src_reg_m1 = _mm_loadl_epi64((const __m128i *)src_ptr);
  399. src_reg_0 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride));
  400. src_reg_m10 = _mm_unpacklo_epi8(src_reg_m1, src_reg_0);
  401. // More shuffling
  402. src_reg_1 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 2));
  403. src_reg_01 = _mm_unpacklo_epi8(src_reg_0, src_reg_1);
  404. for (h = height; h > 1; h -= 2) {
  405. src_reg_2 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 3));
  406. src_reg_12 = _mm_unpacklo_epi8(src_reg_1, src_reg_2);
  407. src_reg_3 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 4));
  408. src_reg_23 = _mm_unpacklo_epi8(src_reg_2, src_reg_3);
  409. // Partial output
  410. res_reg_m10 = _mm_maddubs_epi16(src_reg_m10, kernel_reg_23);
  411. res_reg_01 = _mm_maddubs_epi16(src_reg_01, kernel_reg_23);
  412. res_reg_12 = _mm_maddubs_epi16(src_reg_12, kernel_reg_45);
  413. res_reg_23 = _mm_maddubs_epi16(src_reg_23, kernel_reg_45);
  414. // Add to get entire output
  415. res_reg_m1012 = _mm_adds_epi16(res_reg_m10, res_reg_12);
  416. res_reg_0123 = _mm_adds_epi16(res_reg_01, res_reg_23);
  417. // Round the words
  418. res_reg_m1012 = mm_round_epi16_sse2(&res_reg_m1012, &reg_32, 6);
  419. res_reg_0123 = mm_round_epi16_sse2(&res_reg_0123, &reg_32, 6);
  420. // Pack from 16-bit to 8-bit
  421. res_reg_m1012 = _mm_packus_epi16(res_reg_m1012, _mm_setzero_si128());
  422. res_reg_0123 = _mm_packus_epi16(res_reg_0123, _mm_setzero_si128());
  423. _mm_storel_epi64((__m128i *)dst_ptr, res_reg_m1012);
  424. _mm_storel_epi64((__m128i *)(dst_ptr + dst_stride), res_reg_0123);
  425. // Update the source by two rows
  426. src_ptr += src_stride_unrolled;
  427. dst_ptr += dst_stride_unrolled;
  428. src_reg_m10 = src_reg_12;
  429. src_reg_01 = src_reg_23;
  430. src_reg_1 = src_reg_3;
  431. }
  432. }
  433. static void vpx_filter_block1d4_h4_ssse3(const uint8_t *src_ptr,
  434. ptrdiff_t src_stride, uint8_t *dst_ptr,
  435. ptrdiff_t dst_stride, uint32_t height,
  436. const int16_t *kernel) {
  437. // We will cast the kernel from 16-bit words to 8-bit words, and then extract
  438. // the middle four elements of the kernel into a single register in the form
  439. // k[5:2] k[5:2] k[5:2] k[5:2]
  440. // Then we shuffle the source into
  441. // s[5:2] s[4:1] s[3:0] s[2:-1]
  442. // Calling multiply and add gives us half of the sum next to each other.
  443. // Calling horizontal add then gives us the output.
  444. __m128i kernel_reg; // Kernel
  445. const __m128i reg_32 = _mm_set1_epi16(32); // Used for rounding
  446. int h;
  447. __m128i src_reg, src_reg_shuf;
  448. __m128i dst_first;
  449. __m128i shuf_idx =
  450. _mm_setr_epi8(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6);
  451. // Start one pixel before as we need tap/2 - 1 = 1 sample from the past
  452. src_ptr -= 1;
  453. // Load Kernel
  454. kernel_reg = _mm_loadu_si128((const __m128i *)kernel);
  455. kernel_reg = _mm_srai_epi16(kernel_reg, 1);
  456. kernel_reg = _mm_packs_epi16(kernel_reg, kernel_reg);
  457. kernel_reg = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi32(0x05040302u));
  458. for (h = height; h > 0; --h) {
  459. // Load the source
  460. src_reg = _mm_loadu_si128((const __m128i *)src_ptr);
  461. src_reg_shuf = _mm_shuffle_epi8(src_reg, shuf_idx);
  462. // Get the result
  463. dst_first = _mm_maddubs_epi16(src_reg_shuf, kernel_reg);
  464. dst_first = _mm_hadds_epi16(dst_first, _mm_setzero_si128());
  465. // Round result
  466. dst_first = mm_round_epi16_sse2(&dst_first, &reg_32, 6);
  467. // Pack to 8-bits
  468. dst_first = _mm_packus_epi16(dst_first, _mm_setzero_si128());
  469. *((uint32_t *)(dst_ptr)) = _mm_cvtsi128_si32(dst_first);
  470. src_ptr += src_stride;
  471. dst_ptr += dst_stride;
  472. }
  473. }
  474. static void vpx_filter_block1d4_v4_ssse3(const uint8_t *src_ptr,
  475. ptrdiff_t src_stride, uint8_t *dst_ptr,
  476. ptrdiff_t dst_stride, uint32_t height,
  477. const int16_t *kernel) {
  478. // We will load two rows of pixels as 8-bit words, rearrange them into the
  479. // form
  480. // ... s[2,0] s[1,0] s[0,0] s[-1,0]
  481. // so that we can call multiply and add with the kernel partial output. Then
  482. // we can call horizontal add to get the output.
  483. // Finally, we can add multiple rows together to get the desired output.
  484. // This is done two rows at a time
  485. // Register for source s[-1:3, :]
  486. __m128i src_reg_m1, src_reg_0, src_reg_1, src_reg_2, src_reg_3;
  487. // Interleaved rows of the source.
  488. __m128i src_reg_m10, src_reg_01;
  489. __m128i src_reg_12, src_reg_23;
  490. __m128i src_reg_m1001, src_reg_1223;
  491. __m128i src_reg_m1012_1023_lo, src_reg_m1012_1023_hi;
  492. __m128i kernel_reg; // Kernel
  493. // Result after multiply and add
  494. __m128i reg_0, reg_1;
  495. const __m128i reg_32 = _mm_set1_epi16(32); // Used for rounding
  496. // We will compute the result two rows at a time
  497. const ptrdiff_t src_stride_unrolled = src_stride << 1;
  498. const ptrdiff_t dst_stride_unrolled = dst_stride << 1;
  499. int h;
  500. // Load Kernel
  501. kernel_reg = _mm_loadu_si128((const __m128i *)kernel);
  502. kernel_reg = _mm_srai_epi16(kernel_reg, 1);
  503. kernel_reg = _mm_packs_epi16(kernel_reg, kernel_reg);
  504. kernel_reg = _mm_shuffle_epi8(kernel_reg, _mm_set1_epi32(0x05040302u));
  505. // First shuffle the data
  506. src_reg_m1 = _mm_loadl_epi64((const __m128i *)src_ptr);
  507. src_reg_0 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride));
  508. src_reg_m10 = _mm_unpacklo_epi32(src_reg_m1, src_reg_0);
  509. // More shuffling
  510. src_reg_1 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 2));
  511. src_reg_01 = _mm_unpacklo_epi32(src_reg_0, src_reg_1);
  512. // Put three rows next to each other
  513. src_reg_m1001 = _mm_unpacklo_epi8(src_reg_m10, src_reg_01);
  514. for (h = height; h > 1; h -= 2) {
  515. src_reg_2 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 3));
  516. src_reg_12 = _mm_unpacklo_epi32(src_reg_1, src_reg_2);
  517. src_reg_3 = _mm_loadl_epi64((const __m128i *)(src_ptr + src_stride * 4));
  518. src_reg_23 = _mm_unpacklo_epi32(src_reg_2, src_reg_3);
  519. // Put three rows next to each other
  520. src_reg_1223 = _mm_unpacklo_epi8(src_reg_12, src_reg_23);
  521. // Put all four rows next to each other
  522. src_reg_m1012_1023_lo = _mm_unpacklo_epi16(src_reg_m1001, src_reg_1223);
  523. src_reg_m1012_1023_hi = _mm_unpackhi_epi16(src_reg_m1001, src_reg_1223);
  524. // Get the results
  525. reg_0 = _mm_maddubs_epi16(src_reg_m1012_1023_lo, kernel_reg);
  526. reg_1 = _mm_maddubs_epi16(src_reg_m1012_1023_hi, kernel_reg);
  527. reg_0 = _mm_hadds_epi16(reg_0, _mm_setzero_si128());
  528. reg_1 = _mm_hadds_epi16(reg_1, _mm_setzero_si128());
  529. // Round the words
  530. reg_0 = mm_round_epi16_sse2(&reg_0, &reg_32, 6);
  531. reg_1 = mm_round_epi16_sse2(&reg_1, &reg_32, 6);
  532. // Pack from 16-bit to 8-bit and put them in the right order
  533. reg_0 = _mm_packus_epi16(reg_0, reg_0);
  534. reg_1 = _mm_packus_epi16(reg_1, reg_1);
  535. // Save the result
  536. *((uint32_t *)(dst_ptr)) = _mm_cvtsi128_si32(reg_0);
  537. *((uint32_t *)(dst_ptr + dst_stride)) = _mm_cvtsi128_si32(reg_1);
  538. // Update the source by two rows
  539. src_ptr += src_stride_unrolled;
  540. dst_ptr += dst_stride_unrolled;
  541. src_reg_m1001 = src_reg_1223;
  542. src_reg_1 = src_reg_3;
  543. }
  544. }
  545. // From vpx_dsp/x86/vpx_subpixel_8t_ssse3.asm
  546. filter8_1dfunction vpx_filter_block1d16_v8_ssse3;
  547. filter8_1dfunction vpx_filter_block1d16_h8_ssse3;
  548. filter8_1dfunction vpx_filter_block1d4_v8_ssse3;
  549. filter8_1dfunction vpx_filter_block1d16_v8_avg_ssse3;
  550. filter8_1dfunction vpx_filter_block1d16_h8_avg_ssse3;
  551. filter8_1dfunction vpx_filter_block1d8_v8_avg_ssse3;
  552. filter8_1dfunction vpx_filter_block1d8_h8_avg_ssse3;
  553. filter8_1dfunction vpx_filter_block1d4_v8_avg_ssse3;
  554. filter8_1dfunction vpx_filter_block1d4_h8_avg_ssse3;
  555. // Use the [vh]8 version because there is no [vh]4 implementation.
  556. #define vpx_filter_block1d16_v4_avg_ssse3 vpx_filter_block1d16_v8_avg_ssse3
  557. #define vpx_filter_block1d16_h4_avg_ssse3 vpx_filter_block1d16_h8_avg_ssse3
  558. #define vpx_filter_block1d8_v4_avg_ssse3 vpx_filter_block1d8_v8_avg_ssse3
  559. #define vpx_filter_block1d8_h4_avg_ssse3 vpx_filter_block1d8_h8_avg_ssse3
  560. #define vpx_filter_block1d4_v4_avg_ssse3 vpx_filter_block1d4_v8_avg_ssse3
  561. #define vpx_filter_block1d4_h4_avg_ssse3 vpx_filter_block1d4_h8_avg_ssse3
  562. // From vpx_dsp/x86/vpx_subpixel_bilinear_ssse3.asm
  563. filter8_1dfunction vpx_filter_block1d16_v2_ssse3;
  564. filter8_1dfunction vpx_filter_block1d16_h2_ssse3;
  565. filter8_1dfunction vpx_filter_block1d8_v2_ssse3;
  566. filter8_1dfunction vpx_filter_block1d8_h2_ssse3;
  567. filter8_1dfunction vpx_filter_block1d4_v2_ssse3;
  568. filter8_1dfunction vpx_filter_block1d4_h2_ssse3;
  569. filter8_1dfunction vpx_filter_block1d16_v2_avg_ssse3;
  570. filter8_1dfunction vpx_filter_block1d16_h2_avg_ssse3;
  571. filter8_1dfunction vpx_filter_block1d8_v2_avg_ssse3;
  572. filter8_1dfunction vpx_filter_block1d8_h2_avg_ssse3;
  573. filter8_1dfunction vpx_filter_block1d4_v2_avg_ssse3;
  574. filter8_1dfunction vpx_filter_block1d4_h2_avg_ssse3;
  575. // void vpx_convolve8_horiz_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  576. // uint8_t *dst, ptrdiff_t dst_stride,
  577. // const InterpKernel *filter, int x0_q4,
  578. // int32_t x_step_q4, int y0_q4, int y_step_q4,
  579. // int w, int h);
  580. // void vpx_convolve8_vert_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  581. // uint8_t *dst, ptrdiff_t dst_stride,
  582. // const InterpKernel *filter, int x0_q4,
  583. // int32_t x_step_q4, int y0_q4, int y_step_q4,
  584. // int w, int h);
  585. // void vpx_convolve8_avg_horiz_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  586. // uint8_t *dst, ptrdiff_t dst_stride,
  587. // const InterpKernel *filter, int x0_q4,
  588. // int32_t x_step_q4, int y0_q4,
  589. // int y_step_q4, int w, int h);
  590. // void vpx_convolve8_avg_vert_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  591. // uint8_t *dst, ptrdiff_t dst_stride,
  592. // const InterpKernel *filter, int x0_q4,
  593. // int32_t x_step_q4, int y0_q4,
  594. // int y_step_q4, int w, int h);
  595. FUN_CONV_1D(horiz, x0_q4, x_step_q4, h, src, , ssse3, 0);
  596. FUN_CONV_1D(vert, y0_q4, y_step_q4, v, src - src_stride * (num_taps / 2 - 1), ,
  597. ssse3, 0);
  598. FUN_CONV_1D(avg_horiz, x0_q4, x_step_q4, h, src, avg_, ssse3, 1);
  599. FUN_CONV_1D(avg_vert, y0_q4, y_step_q4, v,
  600. src - src_stride * (num_taps / 2 - 1), avg_, ssse3, 1);
  601. static void filter_horiz_w8_ssse3(const uint8_t *const src,
  602. const ptrdiff_t src_stride,
  603. uint8_t *const dst,
  604. const int16_t *const x_filter) {
  605. __m128i s[8], ss[4], temp;
  606. load_8bit_8x8(src, src_stride, s);
  607. // 00 01 10 11 20 21 30 31 40 41 50 51 60 61 70 71
  608. // 02 03 12 13 22 23 32 33 42 43 52 53 62 63 72 73
  609. // 04 05 14 15 24 25 34 35 44 45 54 55 64 65 74 75
  610. // 06 07 16 17 26 27 36 37 46 47 56 57 66 67 76 77
  611. transpose_16bit_4x8(s, ss);
  612. temp = shuffle_filter_convolve8_8_ssse3(ss, x_filter);
  613. // shrink to 8 bit each 16 bits
  614. temp = _mm_packus_epi16(temp, temp);
  615. // save only 8 bytes convolve result
  616. _mm_storel_epi64((__m128i *)dst, temp);
  617. }
  618. static void transpose8x8_to_dst(const uint8_t *const src,
  619. const ptrdiff_t src_stride, uint8_t *const dst,
  620. const ptrdiff_t dst_stride) {
  621. __m128i s[8];
  622. load_8bit_8x8(src, src_stride, s);
  623. transpose_8bit_8x8(s, s);
  624. store_8bit_8x8(s, dst, dst_stride);
  625. }
  626. static void scaledconvolve_horiz_w8(const uint8_t *src,
  627. const ptrdiff_t src_stride, uint8_t *dst,
  628. const ptrdiff_t dst_stride,
  629. const InterpKernel *const x_filters,
  630. const int x0_q4, const int x_step_q4,
  631. const int w, const int h) {
  632. DECLARE_ALIGNED(16, uint8_t, temp[8 * 8]);
  633. int x, y, z;
  634. src -= SUBPEL_TAPS / 2 - 1;
  635. // This function processes 8x8 areas. The intermediate height is not always
  636. // a multiple of 8, so force it to be a multiple of 8 here.
  637. y = h + (8 - (h & 0x7));
  638. do {
  639. int x_q4 = x0_q4;
  640. for (x = 0; x < w; x += 8) {
  641. // process 8 src_x steps
  642. for (z = 0; z < 8; ++z) {
  643. const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
  644. const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
  645. if (x_q4 & SUBPEL_MASK) {
  646. filter_horiz_w8_ssse3(src_x, src_stride, temp + (z * 8), x_filter);
  647. } else {
  648. int i;
  649. for (i = 0; i < 8; ++i) {
  650. temp[z * 8 + i] = src_x[i * src_stride + 3];
  651. }
  652. }
  653. x_q4 += x_step_q4;
  654. }
  655. // transpose the 8x8 filters values back to dst
  656. transpose8x8_to_dst(temp, 8, dst + x, dst_stride);
  657. }
  658. src += src_stride * 8;
  659. dst += dst_stride * 8;
  660. } while (y -= 8);
  661. }
  662. static void filter_horiz_w4_ssse3(const uint8_t *const src,
  663. const ptrdiff_t src_stride,
  664. uint8_t *const dst,
  665. const int16_t *const filter) {
  666. __m128i s[4], ss[2];
  667. __m128i temp;
  668. load_8bit_8x4(src, src_stride, s);
  669. transpose_16bit_4x4(s, ss);
  670. // 00 01 10 11 20 21 30 31
  671. s[0] = ss[0];
  672. // 02 03 12 13 22 23 32 33
  673. s[1] = _mm_srli_si128(ss[0], 8);
  674. // 04 05 14 15 24 25 34 35
  675. s[2] = ss[1];
  676. // 06 07 16 17 26 27 36 37
  677. s[3] = _mm_srli_si128(ss[1], 8);
  678. temp = shuffle_filter_convolve8_8_ssse3(s, filter);
  679. // shrink to 8 bit each 16 bits
  680. temp = _mm_packus_epi16(temp, temp);
  681. // save only 4 bytes
  682. *(int *)dst = _mm_cvtsi128_si32(temp);
  683. }
  684. static void transpose4x4_to_dst(const uint8_t *const src,
  685. const ptrdiff_t src_stride, uint8_t *const dst,
  686. const ptrdiff_t dst_stride) {
  687. __m128i s[4];
  688. load_8bit_4x4(src, src_stride, s);
  689. s[0] = transpose_8bit_4x4(s);
  690. s[1] = _mm_srli_si128(s[0], 4);
  691. s[2] = _mm_srli_si128(s[0], 8);
  692. s[3] = _mm_srli_si128(s[0], 12);
  693. store_8bit_4x4(s, dst, dst_stride);
  694. }
  695. static void scaledconvolve_horiz_w4(const uint8_t *src,
  696. const ptrdiff_t src_stride, uint8_t *dst,
  697. const ptrdiff_t dst_stride,
  698. const InterpKernel *const x_filters,
  699. const int x0_q4, const int x_step_q4,
  700. const int w, const int h) {
  701. DECLARE_ALIGNED(16, uint8_t, temp[4 * 4]);
  702. int x, y, z;
  703. src -= SUBPEL_TAPS / 2 - 1;
  704. for (y = 0; y < h; y += 4) {
  705. int x_q4 = x0_q4;
  706. for (x = 0; x < w; x += 4) {
  707. // process 4 src_x steps
  708. for (z = 0; z < 4; ++z) {
  709. const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
  710. const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
  711. if (x_q4 & SUBPEL_MASK) {
  712. filter_horiz_w4_ssse3(src_x, src_stride, temp + (z * 4), x_filter);
  713. } else {
  714. int i;
  715. for (i = 0; i < 4; ++i) {
  716. temp[z * 4 + i] = src_x[i * src_stride + 3];
  717. }
  718. }
  719. x_q4 += x_step_q4;
  720. }
  721. // transpose the 4x4 filters values back to dst
  722. transpose4x4_to_dst(temp, 4, dst + x, dst_stride);
  723. }
  724. src += src_stride * 4;
  725. dst += dst_stride * 4;
  726. }
  727. }
  728. static __m128i filter_vert_kernel(const __m128i *const s,
  729. const int16_t *const filter) {
  730. __m128i ss[4];
  731. __m128i temp;
  732. // 00 10 01 11 02 12 03 13
  733. ss[0] = _mm_unpacklo_epi8(s[0], s[1]);
  734. // 20 30 21 31 22 32 23 33
  735. ss[1] = _mm_unpacklo_epi8(s[2], s[3]);
  736. // 40 50 41 51 42 52 43 53
  737. ss[2] = _mm_unpacklo_epi8(s[4], s[5]);
  738. // 60 70 61 71 62 72 63 73
  739. ss[3] = _mm_unpacklo_epi8(s[6], s[7]);
  740. temp = shuffle_filter_convolve8_8_ssse3(ss, filter);
  741. // shrink to 8 bit each 16 bits
  742. return _mm_packus_epi16(temp, temp);
  743. }
  744. static void filter_vert_w4_ssse3(const uint8_t *const src,
  745. const ptrdiff_t src_stride, uint8_t *const dst,
  746. const int16_t *const filter) {
  747. __m128i s[8];
  748. __m128i temp;
  749. load_8bit_4x8(src, src_stride, s);
  750. temp = filter_vert_kernel(s, filter);
  751. // save only 4 bytes
  752. *(int *)dst = _mm_cvtsi128_si32(temp);
  753. }
  754. static void scaledconvolve_vert_w4(
  755. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *const dst,
  756. const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
  757. const int y0_q4, const int y_step_q4, const int w, const int h) {
  758. int y;
  759. int y_q4 = y0_q4;
  760. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  761. for (y = 0; y < h; ++y) {
  762. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  763. const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
  764. if (y_q4 & SUBPEL_MASK) {
  765. filter_vert_w4_ssse3(src_y, src_stride, &dst[y * dst_stride], y_filter);
  766. } else {
  767. memcpy(&dst[y * dst_stride], &src_y[3 * src_stride], w);
  768. }
  769. y_q4 += y_step_q4;
  770. }
  771. }
  772. static void filter_vert_w8_ssse3(const uint8_t *const src,
  773. const ptrdiff_t src_stride, uint8_t *const dst,
  774. const int16_t *const filter) {
  775. __m128i s[8], temp;
  776. load_8bit_8x8(src, src_stride, s);
  777. temp = filter_vert_kernel(s, filter);
  778. // save only 8 bytes convolve result
  779. _mm_storel_epi64((__m128i *)dst, temp);
  780. }
  781. static void scaledconvolve_vert_w8(
  782. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *const dst,
  783. const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
  784. const int y0_q4, const int y_step_q4, const int w, const int h) {
  785. int y;
  786. int y_q4 = y0_q4;
  787. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  788. for (y = 0; y < h; ++y) {
  789. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  790. const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
  791. if (y_q4 & SUBPEL_MASK) {
  792. filter_vert_w8_ssse3(src_y, src_stride, &dst[y * dst_stride], y_filter);
  793. } else {
  794. memcpy(&dst[y * dst_stride], &src_y[3 * src_stride], w);
  795. }
  796. y_q4 += y_step_q4;
  797. }
  798. }
  799. static void filter_vert_w16_ssse3(const uint8_t *src,
  800. const ptrdiff_t src_stride,
  801. uint8_t *const dst,
  802. const int16_t *const filter, const int w) {
  803. int i;
  804. __m128i f[4];
  805. shuffle_filter_ssse3(filter, f);
  806. for (i = 0; i < w; i += 16) {
  807. __m128i s[8], s_lo[4], s_hi[4], temp_lo, temp_hi;
  808. loadu_8bit_16x8(src, src_stride, s);
  809. // merge the result together
  810. s_lo[0] = _mm_unpacklo_epi8(s[0], s[1]);
  811. s_hi[0] = _mm_unpackhi_epi8(s[0], s[1]);
  812. s_lo[1] = _mm_unpacklo_epi8(s[2], s[3]);
  813. s_hi[1] = _mm_unpackhi_epi8(s[2], s[3]);
  814. s_lo[2] = _mm_unpacklo_epi8(s[4], s[5]);
  815. s_hi[2] = _mm_unpackhi_epi8(s[4], s[5]);
  816. s_lo[3] = _mm_unpacklo_epi8(s[6], s[7]);
  817. s_hi[3] = _mm_unpackhi_epi8(s[6], s[7]);
  818. temp_lo = convolve8_8_ssse3(s_lo, f);
  819. temp_hi = convolve8_8_ssse3(s_hi, f);
  820. // shrink to 8 bit each 16 bits, the first lane contain the first convolve
  821. // result and the second lane contain the second convolve result
  822. temp_hi = _mm_packus_epi16(temp_lo, temp_hi);
  823. src += 16;
  824. // save 16 bytes convolve result
  825. _mm_store_si128((__m128i *)&dst[i], temp_hi);
  826. }
  827. }
  828. static void scaledconvolve_vert_w16(
  829. const uint8_t *src, const ptrdiff_t src_stride, uint8_t *const dst,
  830. const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
  831. const int y0_q4, const int y_step_q4, const int w, const int h) {
  832. int y;
  833. int y_q4 = y0_q4;
  834. src -= src_stride * (SUBPEL_TAPS / 2 - 1);
  835. for (y = 0; y < h; ++y) {
  836. const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
  837. const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
  838. if (y_q4 & SUBPEL_MASK) {
  839. filter_vert_w16_ssse3(src_y, src_stride, &dst[y * dst_stride], y_filter,
  840. w);
  841. } else {
  842. memcpy(&dst[y * dst_stride], &src_y[3 * src_stride], w);
  843. }
  844. y_q4 += y_step_q4;
  845. }
  846. }
  847. void vpx_scaled_2d_ssse3(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
  848. ptrdiff_t dst_stride, const InterpKernel *filter,
  849. int x0_q4, int x_step_q4, int y0_q4, int y_step_q4,
  850. int w, int h) {
  851. // Note: Fixed size intermediate buffer, temp, places limits on parameters.
  852. // 2d filtering proceeds in 2 steps:
  853. // (1) Interpolate horizontally into an intermediate buffer, temp.
  854. // (2) Interpolate temp vertically to derive the sub-pixel result.
  855. // Deriving the maximum number of rows in the temp buffer (135):
  856. // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
  857. // --Largest block size is 64x64 pixels.
  858. // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
  859. // original frame (in 1/16th pixel units).
  860. // --Must round-up because block may be located at sub-pixel position.
  861. // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
  862. // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
  863. // --Require an additional 8 rows for the horiz_w8 transpose tail.
  864. // When calling in frame scaling function, the smallest scaling factor is x1/4
  865. // ==> y_step_q4 = 64. Since w and h are at most 16, the temp buffer is still
  866. // big enough.
  867. DECLARE_ALIGNED(16, uint8_t, temp[(135 + 8) * 64]);
  868. const int intermediate_height =
  869. (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
  870. assert(w <= 64);
  871. assert(h <= 64);
  872. assert(y_step_q4 <= 32 || (y_step_q4 <= 64 && h <= 32));
  873. assert(x_step_q4 <= 64);
  874. if (w >= 8) {
  875. scaledconvolve_horiz_w8(src - src_stride * (SUBPEL_TAPS / 2 - 1),
  876. src_stride, temp, 64, filter, x0_q4, x_step_q4, w,
  877. intermediate_height);
  878. } else {
  879. scaledconvolve_horiz_w4(src - src_stride * (SUBPEL_TAPS / 2 - 1),
  880. src_stride, temp, 64, filter, x0_q4, x_step_q4, w,
  881. intermediate_height);
  882. }
  883. if (w >= 16) {
  884. scaledconvolve_vert_w16(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  885. dst_stride, filter, y0_q4, y_step_q4, w, h);
  886. } else if (w == 8) {
  887. scaledconvolve_vert_w8(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  888. dst_stride, filter, y0_q4, y_step_q4, w, h);
  889. } else {
  890. scaledconvolve_vert_w4(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
  891. dst_stride, filter, y0_q4, y_step_q4, w, h);
  892. }
  893. }
  894. // void vpx_convolve8_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  895. // uint8_t *dst, ptrdiff_t dst_stride,
  896. // const InterpKernel *filter, int x0_q4,
  897. // int32_t x_step_q4, int y0_q4, int y_step_q4,
  898. // int w, int h);
  899. // void vpx_convolve8_avg_ssse3(const uint8_t *src, ptrdiff_t src_stride,
  900. // uint8_t *dst, ptrdiff_t dst_stride,
  901. // const InterpKernel *filter, int x0_q4,
  902. // int32_t x_step_q4, int y0_q4, int y_step_q4,
  903. // int w, int h);
  904. FUN_CONV_2D(, ssse3, 0);
  905. FUN_CONV_2D(avg_, ssse3, 1);