vp9dsp_init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/cpu.h"
  26. #include "libavcodec/vp9dsp.h"
  27. #include "libavcodec/x86/vp9dsp_init.h"
  28. #if HAVE_YASM
  29. decl_fpel_func(put, 4, , mmx);
  30. decl_fpel_func(put, 8, , mmx);
  31. decl_fpel_func(put, 16, , sse);
  32. decl_fpel_func(put, 32, , sse);
  33. decl_fpel_func(put, 64, , sse);
  34. decl_fpel_func(avg, 4, _8, mmxext);
  35. decl_fpel_func(avg, 8, _8, mmxext);
  36. decl_fpel_func(avg, 16, _8, sse2);
  37. decl_fpel_func(avg, 32, _8, sse2);
  38. decl_fpel_func(avg, 64, _8, sse2);
  39. decl_fpel_func(put, 32, , avx);
  40. decl_fpel_func(put, 64, , avx);
  41. decl_fpel_func(avg, 32, _8, avx2);
  42. decl_fpel_func(avg, 64, _8, avx2);
  43. decl_mc_funcs(4, mmxext, int16_t, 8, 8);
  44. decl_mc_funcs(8, sse2, int16_t, 8, 8);
  45. decl_mc_funcs(4, ssse3, int8_t, 32, 8);
  46. decl_mc_funcs(8, ssse3, int8_t, 32, 8);
  47. #if ARCH_X86_64
  48. decl_mc_funcs(16, ssse3, int8_t, 32, 8);
  49. decl_mc_funcs(32, avx2, int8_t, 32, 8);
  50. #endif
  51. mc_rep_funcs(16, 8, 8, sse2, int16_t, 8, 8)
  52. #if ARCH_X86_32
  53. mc_rep_funcs(16, 8, 8, ssse3, int8_t, 32, 8)
  54. #endif
  55. mc_rep_funcs(32, 16, 16, sse2, int16_t, 8, 8)
  56. mc_rep_funcs(32, 16, 16, ssse3, int8_t, 32, 8)
  57. mc_rep_funcs(64, 32, 32, sse2, int16_t, 8, 8)
  58. mc_rep_funcs(64, 32, 32, ssse3, int8_t, 32, 8)
  59. #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
  60. mc_rep_funcs(64, 32, 32, avx2, int8_t, 32, 8)
  61. #endif
  62. extern const int8_t ff_filters_ssse3[3][15][4][32];
  63. extern const int16_t ff_filters_sse2[3][15][8][8];
  64. filters_8tap_2d_fn2(put, 16, 8, 1, mmxext, sse2, sse2)
  65. filters_8tap_2d_fn2(avg, 16, 8, 1, mmxext, sse2, sse2)
  66. filters_8tap_2d_fn2(put, 16, 8, 1, ssse3, ssse3, ssse3)
  67. filters_8tap_2d_fn2(avg, 16, 8, 1, ssse3, ssse3, ssse3)
  68. #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
  69. filters_8tap_2d_fn(put, 64, 32, 8, 1, avx2, ssse3)
  70. filters_8tap_2d_fn(put, 32, 32, 8, 1, avx2, ssse3)
  71. filters_8tap_2d_fn(avg, 64, 32, 8, 1, avx2, ssse3)
  72. filters_8tap_2d_fn(avg, 32, 32, 8, 1, avx2, ssse3)
  73. #endif
  74. filters_8tap_1d_fn3(put, 8, mmxext, sse2, sse2)
  75. filters_8tap_1d_fn3(avg, 8, mmxext, sse2, sse2)
  76. filters_8tap_1d_fn3(put, 8, ssse3, ssse3, ssse3)
  77. filters_8tap_1d_fn3(avg, 8, ssse3, ssse3, ssse3)
  78. #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
  79. filters_8tap_1d_fn2(put, 64, 8, avx2, ssse3)
  80. filters_8tap_1d_fn2(put, 32, 8, avx2, ssse3)
  81. filters_8tap_1d_fn2(avg, 64, 8, avx2, ssse3)
  82. filters_8tap_1d_fn2(avg, 32, 8, avx2, ssse3)
  83. #endif
  84. #define itxfm_func(typea, typeb, size, opt) \
  85. void ff_vp9_##typea##_##typeb##_##size##x##size##_add_##opt(uint8_t *dst, ptrdiff_t stride, \
  86. int16_t *block, int eob)
  87. #define itxfm_funcs(size, opt) \
  88. itxfm_func(idct, idct, size, opt); \
  89. itxfm_func(iadst, idct, size, opt); \
  90. itxfm_func(idct, iadst, size, opt); \
  91. itxfm_func(iadst, iadst, size, opt)
  92. itxfm_func(idct, idct, 4, mmxext);
  93. itxfm_func(idct, iadst, 4, sse2);
  94. itxfm_func(iadst, idct, 4, sse2);
  95. itxfm_func(iadst, iadst, 4, sse2);
  96. itxfm_funcs(4, ssse3);
  97. itxfm_funcs(8, sse2);
  98. itxfm_funcs(8, ssse3);
  99. itxfm_funcs(8, avx);
  100. itxfm_funcs(16, sse2);
  101. itxfm_funcs(16, ssse3);
  102. itxfm_funcs(16, avx);
  103. itxfm_func(idct, idct, 32, sse2);
  104. itxfm_func(idct, idct, 32, ssse3);
  105. itxfm_func(idct, idct, 32, avx);
  106. itxfm_func(iwht, iwht, 4, mmx);
  107. #undef itxfm_func
  108. #undef itxfm_funcs
  109. #define lpf_funcs(size1, size2, opt) \
  110. void ff_vp9_loop_filter_v_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t stride, \
  111. int E, int I, int H); \
  112. void ff_vp9_loop_filter_h_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t stride, \
  113. int E, int I, int H)
  114. lpf_funcs(16, 16, sse2);
  115. lpf_funcs(16, 16, ssse3);
  116. lpf_funcs(16, 16, avx);
  117. lpf_funcs(44, 16, sse2);
  118. lpf_funcs(44, 16, ssse3);
  119. lpf_funcs(44, 16, avx);
  120. lpf_funcs(84, 16, sse2);
  121. lpf_funcs(84, 16, ssse3);
  122. lpf_funcs(84, 16, avx);
  123. lpf_funcs(48, 16, sse2);
  124. lpf_funcs(48, 16, ssse3);
  125. lpf_funcs(48, 16, avx);
  126. lpf_funcs(88, 16, sse2);
  127. lpf_funcs(88, 16, ssse3);
  128. lpf_funcs(88, 16, avx);
  129. #undef lpf_funcs
  130. #define ipred_func(size, type, opt) \
  131. void ff_vp9_ipred_##type##_##size##x##size##_##opt(uint8_t *dst, ptrdiff_t stride, \
  132. const uint8_t *l, const uint8_t *a)
  133. ipred_func(8, v, mmx);
  134. #define ipred_dc_funcs(size, opt) \
  135. ipred_func(size, dc, opt); \
  136. ipred_func(size, dc_left, opt); \
  137. ipred_func(size, dc_top, opt)
  138. ipred_dc_funcs(4, mmxext);
  139. ipred_dc_funcs(8, mmxext);
  140. #define ipred_dir_tm_funcs(size, opt) \
  141. ipred_func(size, tm, opt); \
  142. ipred_func(size, dl, opt); \
  143. ipred_func(size, dr, opt); \
  144. ipred_func(size, hd, opt); \
  145. ipred_func(size, hu, opt); \
  146. ipred_func(size, vl, opt); \
  147. ipred_func(size, vr, opt)
  148. ipred_dir_tm_funcs(4, mmxext);
  149. ipred_func(16, v, sse);
  150. ipred_func(32, v, sse);
  151. ipred_dc_funcs(16, sse2);
  152. ipred_dc_funcs(32, sse2);
  153. #define ipred_dir_tm_h_funcs(size, opt) \
  154. ipred_dir_tm_funcs(size, opt); \
  155. ipred_func(size, h, opt)
  156. ipred_dir_tm_h_funcs(8, sse2);
  157. ipred_dir_tm_h_funcs(16, sse2);
  158. ipred_dir_tm_h_funcs(32, sse2);
  159. ipred_func(4, h, sse2);
  160. #define ipred_all_funcs(size, opt) \
  161. ipred_dc_funcs(size, opt); \
  162. ipred_dir_tm_h_funcs(size, opt)
  163. // FIXME hd/vl_4x4_ssse3 does not exist
  164. ipred_all_funcs(4, ssse3);
  165. ipred_all_funcs(8, ssse3);
  166. ipred_all_funcs(16, ssse3);
  167. ipred_all_funcs(32, ssse3);
  168. ipred_dir_tm_h_funcs(8, avx);
  169. ipred_dir_tm_h_funcs(16, avx);
  170. ipred_dir_tm_h_funcs(32, avx);
  171. ipred_func(32, v, avx);
  172. ipred_dc_funcs(32, avx2);
  173. ipred_func(32, h, avx2);
  174. ipred_func(32, tm, avx2);
  175. #undef ipred_func
  176. #undef ipred_dir_tm_h_funcs
  177. #undef ipred_dir_tm_funcs
  178. #undef ipred_dc_funcs
  179. #endif /* HAVE_YASM */
  180. av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int bpp, int bitexact)
  181. {
  182. #if HAVE_YASM
  183. int cpu_flags;
  184. if (bpp == 10) {
  185. ff_vp9dsp_init_10bpp_x86(dsp, bitexact);
  186. return;
  187. } else if (bpp == 12) {
  188. ff_vp9dsp_init_12bpp_x86(dsp, bitexact);
  189. return;
  190. }
  191. cpu_flags = av_get_cpu_flags();
  192. #define init_lpf(opt) do { \
  193. dsp->loop_filter_16[0] = ff_vp9_loop_filter_h_16_16_##opt; \
  194. dsp->loop_filter_16[1] = ff_vp9_loop_filter_v_16_16_##opt; \
  195. dsp->loop_filter_mix2[0][0][0] = ff_vp9_loop_filter_h_44_16_##opt; \
  196. dsp->loop_filter_mix2[0][0][1] = ff_vp9_loop_filter_v_44_16_##opt; \
  197. dsp->loop_filter_mix2[0][1][0] = ff_vp9_loop_filter_h_48_16_##opt; \
  198. dsp->loop_filter_mix2[0][1][1] = ff_vp9_loop_filter_v_48_16_##opt; \
  199. dsp->loop_filter_mix2[1][0][0] = ff_vp9_loop_filter_h_84_16_##opt; \
  200. dsp->loop_filter_mix2[1][0][1] = ff_vp9_loop_filter_v_84_16_##opt; \
  201. dsp->loop_filter_mix2[1][1][0] = ff_vp9_loop_filter_h_88_16_##opt; \
  202. dsp->loop_filter_mix2[1][1][1] = ff_vp9_loop_filter_v_88_16_##opt; \
  203. } while (0)
  204. #define init_ipred(sz, opt, t, e) \
  205. dsp->intra_pred[TX_##sz##X##sz][e##_PRED] = ff_vp9_ipred_##t##_##sz##x##sz##_##opt
  206. #define ff_vp9_ipred_hd_4x4_ssse3 ff_vp9_ipred_hd_4x4_mmxext
  207. #define ff_vp9_ipred_vl_4x4_ssse3 ff_vp9_ipred_vl_4x4_mmxext
  208. #define init_dir_tm_ipred(sz, opt) do { \
  209. init_ipred(sz, opt, dl, DIAG_DOWN_LEFT); \
  210. init_ipred(sz, opt, dr, DIAG_DOWN_RIGHT); \
  211. init_ipred(sz, opt, hd, HOR_DOWN); \
  212. init_ipred(sz, opt, vl, VERT_LEFT); \
  213. init_ipred(sz, opt, hu, HOR_UP); \
  214. init_ipred(sz, opt, tm, TM_VP8); \
  215. init_ipred(sz, opt, vr, VERT_RIGHT); \
  216. } while (0)
  217. #define init_dir_tm_h_ipred(sz, opt) do { \
  218. init_dir_tm_ipred(sz, opt); \
  219. init_ipred(sz, opt, h, HOR); \
  220. } while (0)
  221. #define init_dc_ipred(sz, opt) do { \
  222. init_ipred(sz, opt, dc, DC); \
  223. init_ipred(sz, opt, dc_left, LEFT_DC); \
  224. init_ipred(sz, opt, dc_top, TOP_DC); \
  225. } while (0)
  226. #define init_all_ipred(sz, opt) do { \
  227. init_dc_ipred(sz, opt); \
  228. init_dir_tm_h_ipred(sz, opt); \
  229. } while (0)
  230. if (EXTERNAL_MMX(cpu_flags)) {
  231. init_fpel_func(4, 0, 4, put, , mmx);
  232. init_fpel_func(3, 0, 8, put, , mmx);
  233. if (!bitexact) {
  234. dsp->itxfm_add[4 /* lossless */][DCT_DCT] =
  235. dsp->itxfm_add[4 /* lossless */][ADST_DCT] =
  236. dsp->itxfm_add[4 /* lossless */][DCT_ADST] =
  237. dsp->itxfm_add[4 /* lossless */][ADST_ADST] = ff_vp9_iwht_iwht_4x4_add_mmx;
  238. }
  239. init_ipred(8, mmx, v, VERT);
  240. }
  241. if (EXTERNAL_MMXEXT(cpu_flags)) {
  242. init_subpel2(4, 0, 4, put, 8, mmxext);
  243. init_subpel2(4, 1, 4, avg, 8, mmxext);
  244. init_fpel_func(4, 1, 4, avg, _8, mmxext);
  245. init_fpel_func(3, 1, 8, avg, _8, mmxext);
  246. dsp->itxfm_add[TX_4X4][DCT_DCT] = ff_vp9_idct_idct_4x4_add_mmxext;
  247. init_dc_ipred(4, mmxext);
  248. init_dc_ipred(8, mmxext);
  249. init_dir_tm_ipred(4, mmxext);
  250. }
  251. if (EXTERNAL_SSE(cpu_flags)) {
  252. init_fpel_func(2, 0, 16, put, , sse);
  253. init_fpel_func(1, 0, 32, put, , sse);
  254. init_fpel_func(0, 0, 64, put, , sse);
  255. init_ipred(16, sse, v, VERT);
  256. init_ipred(32, sse, v, VERT);
  257. }
  258. if (EXTERNAL_SSE2(cpu_flags)) {
  259. init_subpel3_8to64(0, put, 8, sse2);
  260. init_subpel3_8to64(1, avg, 8, sse2);
  261. init_fpel_func(2, 1, 16, avg, _8, sse2);
  262. init_fpel_func(1, 1, 32, avg, _8, sse2);
  263. init_fpel_func(0, 1, 64, avg, _8, sse2);
  264. init_lpf(sse2);
  265. dsp->itxfm_add[TX_4X4][ADST_DCT] = ff_vp9_idct_iadst_4x4_add_sse2;
  266. dsp->itxfm_add[TX_4X4][DCT_ADST] = ff_vp9_iadst_idct_4x4_add_sse2;
  267. dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_sse2;
  268. dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_sse2;
  269. dsp->itxfm_add[TX_8X8][ADST_DCT] = ff_vp9_idct_iadst_8x8_add_sse2;
  270. dsp->itxfm_add[TX_8X8][DCT_ADST] = ff_vp9_iadst_idct_8x8_add_sse2;
  271. dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_sse2;
  272. dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_sse2;
  273. dsp->itxfm_add[TX_16X16][ADST_DCT] = ff_vp9_idct_iadst_16x16_add_sse2;
  274. dsp->itxfm_add[TX_16X16][DCT_ADST] = ff_vp9_iadst_idct_16x16_add_sse2;
  275. dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_sse2;
  276. dsp->itxfm_add[TX_32X32][ADST_ADST] =
  277. dsp->itxfm_add[TX_32X32][ADST_DCT] =
  278. dsp->itxfm_add[TX_32X32][DCT_ADST] =
  279. dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_sse2;
  280. init_dc_ipred(16, sse2);
  281. init_dc_ipred(32, sse2);
  282. init_dir_tm_h_ipred(8, sse2);
  283. init_dir_tm_h_ipred(16, sse2);
  284. init_dir_tm_h_ipred(32, sse2);
  285. init_ipred(4, sse2, h, HOR);
  286. }
  287. if (EXTERNAL_SSSE3(cpu_flags)) {
  288. init_subpel3(0, put, 8, ssse3);
  289. init_subpel3(1, avg, 8, ssse3);
  290. dsp->itxfm_add[TX_4X4][DCT_DCT] = ff_vp9_idct_idct_4x4_add_ssse3;
  291. dsp->itxfm_add[TX_4X4][ADST_DCT] = ff_vp9_idct_iadst_4x4_add_ssse3;
  292. dsp->itxfm_add[TX_4X4][DCT_ADST] = ff_vp9_iadst_idct_4x4_add_ssse3;
  293. dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_ssse3;
  294. dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_ssse3;
  295. dsp->itxfm_add[TX_8X8][ADST_DCT] = ff_vp9_idct_iadst_8x8_add_ssse3;
  296. dsp->itxfm_add[TX_8X8][DCT_ADST] = ff_vp9_iadst_idct_8x8_add_ssse3;
  297. dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_ssse3;
  298. dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_ssse3;
  299. dsp->itxfm_add[TX_16X16][ADST_DCT] = ff_vp9_idct_iadst_16x16_add_ssse3;
  300. dsp->itxfm_add[TX_16X16][DCT_ADST] = ff_vp9_iadst_idct_16x16_add_ssse3;
  301. dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_ssse3;
  302. dsp->itxfm_add[TX_32X32][ADST_ADST] =
  303. dsp->itxfm_add[TX_32X32][ADST_DCT] =
  304. dsp->itxfm_add[TX_32X32][DCT_ADST] =
  305. dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_ssse3;
  306. init_lpf(ssse3);
  307. init_all_ipred(4, ssse3);
  308. init_all_ipred(8, ssse3);
  309. init_all_ipred(16, ssse3);
  310. init_all_ipred(32, ssse3);
  311. }
  312. if (EXTERNAL_AVX(cpu_flags)) {
  313. dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_avx;
  314. dsp->itxfm_add[TX_8X8][ADST_DCT] = ff_vp9_idct_iadst_8x8_add_avx;
  315. dsp->itxfm_add[TX_8X8][DCT_ADST] = ff_vp9_iadst_idct_8x8_add_avx;
  316. dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_avx;
  317. dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_avx;
  318. dsp->itxfm_add[TX_16X16][ADST_DCT] = ff_vp9_idct_iadst_16x16_add_avx;
  319. dsp->itxfm_add[TX_16X16][DCT_ADST] = ff_vp9_iadst_idct_16x16_add_avx;
  320. dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_avx;
  321. dsp->itxfm_add[TX_32X32][ADST_ADST] =
  322. dsp->itxfm_add[TX_32X32][ADST_DCT] =
  323. dsp->itxfm_add[TX_32X32][DCT_ADST] =
  324. dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_avx;
  325. init_lpf(avx);
  326. init_dir_tm_h_ipred(8, avx);
  327. init_dir_tm_h_ipred(16, avx);
  328. init_dir_tm_h_ipred(32, avx);
  329. }
  330. if (EXTERNAL_AVX_FAST(cpu_flags)) {
  331. init_fpel_func(1, 0, 32, put, , avx);
  332. init_fpel_func(0, 0, 64, put, , avx);
  333. init_ipred(32, avx, v, VERT);
  334. }
  335. if (EXTERNAL_AVX2_FAST(cpu_flags)) {
  336. init_fpel_func(1, 1, 32, avg, _8, avx2);
  337. init_fpel_func(0, 1, 64, avg, _8, avx2);
  338. if (ARCH_X86_64) {
  339. #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
  340. init_subpel3_32_64(0, put, 8, avx2);
  341. init_subpel3_32_64(1, avg, 8, avx2);
  342. #endif
  343. }
  344. init_dc_ipred(32, avx2);
  345. init_ipred(32, avx2, h, HOR);
  346. init_ipred(32, avx2, tm, TM_VP8);
  347. }
  348. #undef init_fpel
  349. #undef init_subpel1
  350. #undef init_subpel2
  351. #undef init_subpel3
  352. #endif /* HAVE_YASM */
  353. }