You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
11KB

  1. /*
  2. * VP9 SIMD optimizations
  3. *
  4. * Copyright (c) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/cpu.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/x86/asm.h"
  26. #include "libavutil/x86/cpu.h"
  27. #include "libavcodec/vp9dsp.h"
  28. #if HAVE_YASM
  29. #define fpel_func(avg, sz, opt) \
  30. void ff_##avg##sz##_##opt(uint8_t *dst, ptrdiff_t dst_stride, \
  31. const uint8_t *src, ptrdiff_t src_stride, \
  32. int h, int mx, int my)
  33. fpel_func(put, 4, mmx);
  34. fpel_func(put, 8, mmx);
  35. fpel_func(put, 16, sse);
  36. fpel_func(put, 32, sse);
  37. fpel_func(put, 64, sse);
  38. fpel_func(avg, 4, mmxext);
  39. fpel_func(avg, 8, mmxext);
  40. fpel_func(avg, 16, sse2);
  41. fpel_func(avg, 32, sse2);
  42. fpel_func(avg, 64, sse2);
  43. #undef fpel_func
  44. #define mc_func(avg, sz, dir, opt) \
  45. void ff_##avg##_8tap_1d_##dir##_##sz##_##opt(uint8_t *dst, ptrdiff_t dst_stride, \
  46. const uint8_t *src, ptrdiff_t src_stride, \
  47. int h, const int8_t (*filter)[16])
  48. #define mc_funcs(sz) \
  49. mc_func(put, sz, h, ssse3); \
  50. mc_func(avg, sz, h, ssse3); \
  51. mc_func(put, sz, v, ssse3); \
  52. mc_func(avg, sz, v, ssse3)
  53. mc_funcs(4);
  54. mc_funcs(8);
  55. #if ARCH_X86_64
  56. mc_funcs(16);
  57. #endif
  58. #undef mc_funcs
  59. #undef mc_func
  60. #define mc_rep_func(avg, sz, hsz, dir, opt) \
  61. static av_always_inline void \
  62. ff_##avg##_8tap_1d_##dir##_##sz##_##opt(uint8_t *dst, ptrdiff_t dst_stride, \
  63. const uint8_t *src, ptrdiff_t src_stride, \
  64. int h, const int8_t (*filter)[16]) \
  65. { \
  66. ff_##avg##_8tap_1d_##dir##_##hsz##_##opt(dst, dst_stride, src, \
  67. src_stride, h, filter); \
  68. ff_##avg##_8tap_1d_##dir##_##hsz##_##opt(dst + hsz, dst_stride, src + hsz, \
  69. src_stride, h, filter); \
  70. }
  71. #define mc_rep_funcs(sz, hsz) \
  72. mc_rep_func(put, sz, hsz, h, ssse3); \
  73. mc_rep_func(avg, sz, hsz, h, ssse3); \
  74. mc_rep_func(put, sz, hsz, v, ssse3); \
  75. mc_rep_func(avg, sz, hsz, v, ssse3)
  76. #if ARCH_X86_32
  77. mc_rep_funcs(16, 8);
  78. #endif
  79. mc_rep_funcs(32, 16);
  80. mc_rep_funcs(64, 32);
  81. #undef mc_rep_funcs
  82. #undef mc_rep_func
  83. extern const int8_t ff_filters_ssse3[3][15][4][16];
  84. #define filter_8tap_2d_fn(op, sz, f, fname) \
  85. static void op##_8tap_##fname##_##sz##hv_ssse3(uint8_t *dst, ptrdiff_t dst_stride, \
  86. const uint8_t *src, ptrdiff_t src_stride, \
  87. int h, int mx, int my) \
  88. { \
  89. LOCAL_ALIGNED_16(uint8_t, temp, [71 * 64]); \
  90. ff_put_8tap_1d_h_##sz##_ssse3(temp, 64, src - 3 * src_stride, src_stride, \
  91. h + 7, ff_filters_ssse3[f][mx - 1]); \
  92. ff_##op##_8tap_1d_v_##sz##_ssse3(dst, dst_stride, temp + 3 * 64, 64, \
  93. h, ff_filters_ssse3[f][my - 1]); \
  94. }
  95. #define filters_8tap_2d_fn(op, sz) \
  96. filter_8tap_2d_fn(op, sz, FILTER_8TAP_REGULAR, regular) \
  97. filter_8tap_2d_fn(op, sz, FILTER_8TAP_SHARP, sharp) \
  98. filter_8tap_2d_fn(op, sz, FILTER_8TAP_SMOOTH, smooth)
  99. #define filters_8tap_2d_fn2(op) \
  100. filters_8tap_2d_fn(op, 64) \
  101. filters_8tap_2d_fn(op, 32) \
  102. filters_8tap_2d_fn(op, 16) \
  103. filters_8tap_2d_fn(op, 8) \
  104. filters_8tap_2d_fn(op, 4)
  105. filters_8tap_2d_fn2(put)
  106. filters_8tap_2d_fn2(avg)
  107. #undef filters_8tap_2d_fn2
  108. #undef filters_8tap_2d_fn
  109. #undef filter_8tap_2d_fn
  110. #define filter_8tap_1d_fn(op, sz, f, fname, dir, dvar) \
  111. static void op##_8tap_##fname##_##sz##dir##_ssse3(uint8_t *dst, ptrdiff_t dst_stride, \
  112. const uint8_t *src, ptrdiff_t src_stride, \
  113. int h, int mx, int my) \
  114. { \
  115. ff_##op##_8tap_1d_##dir##_##sz##_ssse3(dst, dst_stride, src, src_stride, \
  116. h, ff_filters_ssse3[f][dvar - 1]); \
  117. }
  118. #define filters_8tap_1d_fn(op, sz, dir, dvar) \
  119. filter_8tap_1d_fn(op, sz, FILTER_8TAP_REGULAR, regular, dir, dvar) \
  120. filter_8tap_1d_fn(op, sz, FILTER_8TAP_SHARP, sharp, dir, dvar) \
  121. filter_8tap_1d_fn(op, sz, FILTER_8TAP_SMOOTH, smooth, dir, dvar)
  122. #define filters_8tap_1d_fn2(op, sz) \
  123. filters_8tap_1d_fn(op, sz, h, mx) \
  124. filters_8tap_1d_fn(op, sz, v, my)
  125. #define filters_8tap_1d_fn3(op) \
  126. filters_8tap_1d_fn2(op, 64) \
  127. filters_8tap_1d_fn2(op, 32) \
  128. filters_8tap_1d_fn2(op, 16) \
  129. filters_8tap_1d_fn2(op, 8) \
  130. filters_8tap_1d_fn2(op, 4)
  131. filters_8tap_1d_fn3(put)
  132. filters_8tap_1d_fn3(avg)
  133. #undef filters_8tap_1d_fn
  134. #undef filters_8tap_1d_fn2
  135. #undef filters_8tap_1d_fn3
  136. #undef filter_8tap_1d_fn
  137. #define itxfm_func(typea, typeb, size, opt) \
  138. void ff_vp9_##typea##_##typeb##_##size##x##size##_add_##opt(uint8_t *dst, ptrdiff_t stride, \
  139. int16_t *block, int eob)
  140. #define itxfm_funcs(size, opt) \
  141. itxfm_func(idct, idct, size, opt); \
  142. itxfm_func(iadst, idct, size, opt); \
  143. itxfm_func(idct, iadst, size, opt); \
  144. itxfm_func(iadst, iadst, size, opt)
  145. itxfm_funcs(4, ssse3);
  146. itxfm_funcs(8, ssse3);
  147. itxfm_funcs(8, avx);
  148. itxfm_funcs(16, ssse3);
  149. itxfm_funcs(16, avx);
  150. itxfm_func(idct, idct, 32, ssse3);
  151. itxfm_func(idct, idct, 32, avx);
  152. #undef itxfm_func
  153. #undef itxfm_funcs
  154. #define lpf_funcs(size1, size2, opt) \
  155. void ff_vp9_loop_filter_v_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t stride, \
  156. int E, int I, int H); \
  157. void ff_vp9_loop_filter_h_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t stride, \
  158. int E, int I, int H)
  159. lpf_funcs(16, 16, sse2);
  160. lpf_funcs(16, 16, ssse3);
  161. lpf_funcs(16, 16, avx);
  162. #undef lpf_funcs
  163. #endif /* HAVE_YASM */
  164. av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp)
  165. {
  166. #if HAVE_YASM
  167. int cpu_flags = av_get_cpu_flags();
  168. #define init_fpel(idx1, idx2, sz, type, opt) \
  169. dsp->mc[idx1][FILTER_8TAP_SMOOTH ][idx2][0][0] = \
  170. dsp->mc[idx1][FILTER_8TAP_REGULAR][idx2][0][0] = \
  171. dsp->mc[idx1][FILTER_8TAP_SHARP ][idx2][0][0] = \
  172. dsp->mc[idx1][FILTER_BILINEAR ][idx2][0][0] = ff_##type##sz##_##opt
  173. #define init_subpel1(idx1, idx2, idxh, idxv, sz, dir, type, opt) \
  174. dsp->mc[idx1][FILTER_8TAP_SMOOTH ][idx2][idxh][idxv] = type##_8tap_smooth_##sz##dir##_##opt; \
  175. dsp->mc[idx1][FILTER_8TAP_REGULAR][idx2][idxh][idxv] = type##_8tap_regular_##sz##dir##_##opt; \
  176. dsp->mc[idx1][FILTER_8TAP_SHARP ][idx2][idxh][idxv] = type##_8tap_sharp_##sz##dir##_##opt
  177. #define init_subpel2(idx, idxh, idxv, dir, type, opt) \
  178. init_subpel1(0, idx, idxh, idxv, 64, dir, type, opt); \
  179. init_subpel1(1, idx, idxh, idxv, 32, dir, type, opt); \
  180. init_subpel1(2, idx, idxh, idxv, 16, dir, type, opt); \
  181. init_subpel1(3, idx, idxh, idxv, 8, dir, type, opt); \
  182. init_subpel1(4, idx, idxh, idxv, 4, dir, type, opt)
  183. #define init_subpel3(idx, type, opt) \
  184. init_subpel2(idx, 1, 1, hv, type, opt); \
  185. init_subpel2(idx, 0, 1, v, type, opt); \
  186. init_subpel2(idx, 1, 0, h, type, opt)
  187. if (EXTERNAL_MMX(cpu_flags)) {
  188. init_fpel(4, 0, 4, put, mmx);
  189. init_fpel(3, 0, 8, put, mmx);
  190. }
  191. if (EXTERNAL_MMXEXT(cpu_flags)) {
  192. init_fpel(4, 1, 4, avg, mmxext);
  193. init_fpel(3, 1, 8, avg, mmxext);
  194. }
  195. if (EXTERNAL_SSE(cpu_flags)) {
  196. init_fpel(2, 0, 16, put, sse);
  197. init_fpel(1, 0, 32, put, sse);
  198. init_fpel(0, 0, 64, put, sse);
  199. }
  200. if (EXTERNAL_SSE2(cpu_flags)) {
  201. init_fpel(2, 1, 16, avg, sse2);
  202. init_fpel(1, 1, 32, avg, sse2);
  203. init_fpel(0, 1, 64, avg, sse2);
  204. if (ARCH_X86_64) {
  205. dsp->loop_filter_16[0] = ff_vp9_loop_filter_h_16_16_sse2;
  206. dsp->loop_filter_16[1] = ff_vp9_loop_filter_v_16_16_sse2;
  207. }
  208. }
  209. if (EXTERNAL_SSSE3(cpu_flags)) {
  210. init_subpel3(0, put, ssse3);
  211. init_subpel3(1, avg, ssse3);
  212. dsp->itxfm_add[TX_4X4][DCT_DCT] = ff_vp9_idct_idct_4x4_add_ssse3;
  213. dsp->itxfm_add[TX_4X4][ADST_DCT] = ff_vp9_idct_iadst_4x4_add_ssse3;
  214. dsp->itxfm_add[TX_4X4][DCT_ADST] = ff_vp9_iadst_idct_4x4_add_ssse3;
  215. dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_ssse3;
  216. if (ARCH_X86_64) {
  217. dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_ssse3;
  218. dsp->itxfm_add[TX_8X8][ADST_DCT] = ff_vp9_idct_iadst_8x8_add_ssse3;
  219. dsp->itxfm_add[TX_8X8][DCT_ADST] = ff_vp9_iadst_idct_8x8_add_ssse3;
  220. dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_ssse3;
  221. dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_ssse3;
  222. dsp->itxfm_add[TX_16X16][ADST_DCT] = ff_vp9_idct_iadst_16x16_add_ssse3;
  223. dsp->itxfm_add[TX_16X16][DCT_ADST] = ff_vp9_iadst_idct_16x16_add_ssse3;
  224. dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_ssse3;
  225. dsp->itxfm_add[TX_32X32][ADST_ADST] =
  226. dsp->itxfm_add[TX_32X32][ADST_DCT] =
  227. dsp->itxfm_add[TX_32X32][DCT_ADST] =
  228. dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_ssse3;
  229. dsp->loop_filter_16[0] = ff_vp9_loop_filter_h_16_16_ssse3;
  230. dsp->loop_filter_16[1] = ff_vp9_loop_filter_v_16_16_ssse3;
  231. }
  232. }
  233. if (EXTERNAL_AVX(cpu_flags)) {
  234. if (ARCH_X86_64) {
  235. dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_avx;
  236. dsp->itxfm_add[TX_8X8][ADST_DCT] = ff_vp9_idct_iadst_8x8_add_avx;
  237. dsp->itxfm_add[TX_8X8][DCT_ADST] = ff_vp9_iadst_idct_8x8_add_avx;
  238. dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_avx;
  239. dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_avx;
  240. dsp->itxfm_add[TX_16X16][ADST_DCT] = ff_vp9_idct_iadst_16x16_add_avx;
  241. dsp->itxfm_add[TX_16X16][DCT_ADST] = ff_vp9_iadst_idct_16x16_add_avx;
  242. dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_avx;
  243. dsp->itxfm_add[TX_32X32][ADST_ADST] =
  244. dsp->itxfm_add[TX_32X32][ADST_DCT] =
  245. dsp->itxfm_add[TX_32X32][DCT_ADST] =
  246. dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_avx;
  247. dsp->loop_filter_16[0] = ff_vp9_loop_filter_h_16_16_avx;
  248. dsp->loop_filter_16[1] = ff_vp9_loop_filter_v_16_16_avx;
  249. }
  250. }
  251. #undef init_fpel
  252. #undef init_subpel1
  253. #undef init_subpel2
  254. #undef init_subpel3
  255. #endif /* HAVE_YASM */
  256. }