2
0

videodsp_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2002-2012 Michael Niedermayer
  3. * Copyright (C) 2012 Ronald S. Bultje
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/x86/asm.h"
  28. #include "libavutil/x86/cpu.h"
  29. #include "libavcodec/videodsp.h"
  30. #if HAVE_X86ASM
  31. typedef void emu_edge_vfix_func(uint8_t *dst, x86_reg dst_stride,
  32. const uint8_t *src, x86_reg src_stride,
  33. x86_reg start_y, x86_reg end_y, x86_reg bh);
  34. typedef void emu_edge_vvar_func(uint8_t *dst, x86_reg dst_stride,
  35. const uint8_t *src, x86_reg src_stride,
  36. x86_reg start_y, x86_reg end_y, x86_reg bh,
  37. x86_reg w);
  38. extern emu_edge_vfix_func ff_emu_edge_vfix1_mmx;
  39. extern emu_edge_vfix_func ff_emu_edge_vfix2_mmx;
  40. extern emu_edge_vfix_func ff_emu_edge_vfix3_mmx;
  41. extern emu_edge_vfix_func ff_emu_edge_vfix4_mmx;
  42. extern emu_edge_vfix_func ff_emu_edge_vfix5_mmx;
  43. extern emu_edge_vfix_func ff_emu_edge_vfix6_mmx;
  44. extern emu_edge_vfix_func ff_emu_edge_vfix7_mmx;
  45. extern emu_edge_vfix_func ff_emu_edge_vfix8_mmx;
  46. extern emu_edge_vfix_func ff_emu_edge_vfix9_mmx;
  47. extern emu_edge_vfix_func ff_emu_edge_vfix10_mmx;
  48. extern emu_edge_vfix_func ff_emu_edge_vfix11_mmx;
  49. extern emu_edge_vfix_func ff_emu_edge_vfix12_mmx;
  50. extern emu_edge_vfix_func ff_emu_edge_vfix13_mmx;
  51. extern emu_edge_vfix_func ff_emu_edge_vfix14_mmx;
  52. extern emu_edge_vfix_func ff_emu_edge_vfix15_mmx;
  53. extern emu_edge_vfix_func ff_emu_edge_vfix16_mmx;
  54. extern emu_edge_vfix_func ff_emu_edge_vfix17_mmx;
  55. extern emu_edge_vfix_func ff_emu_edge_vfix18_mmx;
  56. extern emu_edge_vfix_func ff_emu_edge_vfix19_mmx;
  57. extern emu_edge_vfix_func ff_emu_edge_vfix20_mmx;
  58. extern emu_edge_vfix_func ff_emu_edge_vfix21_mmx;
  59. extern emu_edge_vfix_func ff_emu_edge_vfix22_mmx;
  60. #if ARCH_X86_32
  61. static emu_edge_vfix_func * const vfixtbl_mmx[22] = {
  62. &ff_emu_edge_vfix1_mmx, &ff_emu_edge_vfix2_mmx, &ff_emu_edge_vfix3_mmx,
  63. &ff_emu_edge_vfix4_mmx, &ff_emu_edge_vfix5_mmx, &ff_emu_edge_vfix6_mmx,
  64. &ff_emu_edge_vfix7_mmx, &ff_emu_edge_vfix8_mmx, &ff_emu_edge_vfix9_mmx,
  65. &ff_emu_edge_vfix10_mmx, &ff_emu_edge_vfix11_mmx, &ff_emu_edge_vfix12_mmx,
  66. &ff_emu_edge_vfix13_mmx, &ff_emu_edge_vfix14_mmx, &ff_emu_edge_vfix15_mmx,
  67. &ff_emu_edge_vfix16_mmx, &ff_emu_edge_vfix17_mmx, &ff_emu_edge_vfix18_mmx,
  68. &ff_emu_edge_vfix19_mmx, &ff_emu_edge_vfix20_mmx, &ff_emu_edge_vfix21_mmx,
  69. &ff_emu_edge_vfix22_mmx
  70. };
  71. #endif
  72. extern emu_edge_vvar_func ff_emu_edge_vvar_mmx;
  73. extern emu_edge_vfix_func ff_emu_edge_vfix16_sse;
  74. extern emu_edge_vfix_func ff_emu_edge_vfix17_sse;
  75. extern emu_edge_vfix_func ff_emu_edge_vfix18_sse;
  76. extern emu_edge_vfix_func ff_emu_edge_vfix19_sse;
  77. extern emu_edge_vfix_func ff_emu_edge_vfix20_sse;
  78. extern emu_edge_vfix_func ff_emu_edge_vfix21_sse;
  79. extern emu_edge_vfix_func ff_emu_edge_vfix22_sse;
  80. static emu_edge_vfix_func * const vfixtbl_sse[22] = {
  81. ff_emu_edge_vfix1_mmx, ff_emu_edge_vfix2_mmx, ff_emu_edge_vfix3_mmx,
  82. ff_emu_edge_vfix4_mmx, ff_emu_edge_vfix5_mmx, ff_emu_edge_vfix6_mmx,
  83. ff_emu_edge_vfix7_mmx, ff_emu_edge_vfix8_mmx, ff_emu_edge_vfix9_mmx,
  84. ff_emu_edge_vfix10_mmx, ff_emu_edge_vfix11_mmx, ff_emu_edge_vfix12_mmx,
  85. ff_emu_edge_vfix13_mmx, ff_emu_edge_vfix14_mmx, ff_emu_edge_vfix15_mmx,
  86. ff_emu_edge_vfix16_sse, ff_emu_edge_vfix17_sse, ff_emu_edge_vfix18_sse,
  87. ff_emu_edge_vfix19_sse, ff_emu_edge_vfix20_sse, ff_emu_edge_vfix21_sse,
  88. ff_emu_edge_vfix22_sse
  89. };
  90. extern emu_edge_vvar_func ff_emu_edge_vvar_sse;
  91. typedef void emu_edge_hfix_func(uint8_t *dst, x86_reg dst_stride,
  92. x86_reg start_x, x86_reg bh);
  93. typedef void emu_edge_hvar_func(uint8_t *dst, x86_reg dst_stride,
  94. x86_reg start_x, x86_reg n_words, x86_reg bh);
  95. extern emu_edge_hfix_func ff_emu_edge_hfix2_mmx;
  96. extern emu_edge_hfix_func ff_emu_edge_hfix4_mmx;
  97. extern emu_edge_hfix_func ff_emu_edge_hfix6_mmx;
  98. extern emu_edge_hfix_func ff_emu_edge_hfix8_mmx;
  99. extern emu_edge_hfix_func ff_emu_edge_hfix10_mmx;
  100. extern emu_edge_hfix_func ff_emu_edge_hfix12_mmx;
  101. extern emu_edge_hfix_func ff_emu_edge_hfix14_mmx;
  102. extern emu_edge_hfix_func ff_emu_edge_hfix16_mmx;
  103. extern emu_edge_hfix_func ff_emu_edge_hfix18_mmx;
  104. extern emu_edge_hfix_func ff_emu_edge_hfix20_mmx;
  105. extern emu_edge_hfix_func ff_emu_edge_hfix22_mmx;
  106. #if ARCH_X86_32
  107. static emu_edge_hfix_func * const hfixtbl_mmx[11] = {
  108. ff_emu_edge_hfix2_mmx, ff_emu_edge_hfix4_mmx, ff_emu_edge_hfix6_mmx,
  109. ff_emu_edge_hfix8_mmx, ff_emu_edge_hfix10_mmx, ff_emu_edge_hfix12_mmx,
  110. ff_emu_edge_hfix14_mmx, ff_emu_edge_hfix16_mmx, ff_emu_edge_hfix18_mmx,
  111. ff_emu_edge_hfix20_mmx, ff_emu_edge_hfix22_mmx
  112. };
  113. #endif
  114. extern emu_edge_hvar_func ff_emu_edge_hvar_mmx;
  115. extern emu_edge_hfix_func ff_emu_edge_hfix16_sse2;
  116. extern emu_edge_hfix_func ff_emu_edge_hfix18_sse2;
  117. extern emu_edge_hfix_func ff_emu_edge_hfix20_sse2;
  118. extern emu_edge_hfix_func ff_emu_edge_hfix22_sse2;
  119. static emu_edge_hfix_func * const hfixtbl_sse2[11] = {
  120. ff_emu_edge_hfix2_mmx, ff_emu_edge_hfix4_mmx, ff_emu_edge_hfix6_mmx,
  121. ff_emu_edge_hfix8_mmx, ff_emu_edge_hfix10_mmx, ff_emu_edge_hfix12_mmx,
  122. ff_emu_edge_hfix14_mmx, ff_emu_edge_hfix16_sse2, ff_emu_edge_hfix18_sse2,
  123. ff_emu_edge_hfix20_sse2, ff_emu_edge_hfix22_sse2
  124. };
  125. extern emu_edge_hvar_func ff_emu_edge_hvar_sse2;
  126. #if HAVE_AVX2_EXTERNAL
  127. extern emu_edge_hfix_func ff_emu_edge_hfix8_avx2;
  128. extern emu_edge_hfix_func ff_emu_edge_hfix10_avx2;
  129. extern emu_edge_hfix_func ff_emu_edge_hfix12_avx2;
  130. extern emu_edge_hfix_func ff_emu_edge_hfix14_avx2;
  131. extern emu_edge_hfix_func ff_emu_edge_hfix16_avx2;
  132. extern emu_edge_hfix_func ff_emu_edge_hfix18_avx2;
  133. extern emu_edge_hfix_func ff_emu_edge_hfix20_avx2;
  134. extern emu_edge_hfix_func ff_emu_edge_hfix22_avx2;
  135. static emu_edge_hfix_func * const hfixtbl_avx2[11] = {
  136. ff_emu_edge_hfix2_mmx, ff_emu_edge_hfix4_mmx, ff_emu_edge_hfix6_mmx,
  137. ff_emu_edge_hfix8_avx2, ff_emu_edge_hfix10_avx2, ff_emu_edge_hfix12_avx2,
  138. ff_emu_edge_hfix14_avx2, ff_emu_edge_hfix16_avx2, ff_emu_edge_hfix18_avx2,
  139. ff_emu_edge_hfix20_avx2, ff_emu_edge_hfix22_avx2
  140. };
  141. extern emu_edge_hvar_func ff_emu_edge_hvar_avx2;
  142. #endif
  143. static av_always_inline void emulated_edge_mc(uint8_t *dst, const uint8_t *src,
  144. ptrdiff_t dst_stride,
  145. ptrdiff_t src_stride,
  146. x86_reg block_w, x86_reg block_h,
  147. x86_reg src_x, x86_reg src_y,
  148. x86_reg w, x86_reg h,
  149. emu_edge_vfix_func * const *vfix_tbl,
  150. emu_edge_vvar_func *v_extend_var,
  151. emu_edge_hfix_func * const *hfix_tbl,
  152. emu_edge_hvar_func *h_extend_var)
  153. {
  154. x86_reg start_y, start_x, end_y, end_x, src_y_add = 0, p;
  155. if (!w || !h)
  156. return;
  157. av_assert2(block_w <= FFABS(dst_stride));
  158. if (src_y >= h) {
  159. src -= src_y*src_stride;
  160. src_y_add = h - 1;
  161. src_y = h - 1;
  162. } else if (src_y <= -block_h) {
  163. src -= src_y*src_stride;
  164. src_y_add = 1 - block_h;
  165. src_y = 1 - block_h;
  166. }
  167. if (src_x >= w) {
  168. src += w - 1 - src_x;
  169. src_x = w - 1;
  170. } else if (src_x <= -block_w) {
  171. src += 1 - block_w - src_x;
  172. src_x = 1 - block_w;
  173. }
  174. start_y = FFMAX(0, -src_y);
  175. start_x = FFMAX(0, -src_x);
  176. end_y = FFMIN(block_h, h-src_y);
  177. end_x = FFMIN(block_w, w-src_x);
  178. av_assert2(start_x < end_x && block_w > 0);
  179. av_assert2(start_y < end_y && block_h > 0);
  180. // fill in the to-be-copied part plus all above/below
  181. src += (src_y_add + start_y) * src_stride + start_x;
  182. w = end_x - start_x;
  183. if (w <= 22) {
  184. vfix_tbl[w - 1](dst + start_x, dst_stride, src, src_stride,
  185. start_y, end_y, block_h);
  186. } else {
  187. v_extend_var(dst + start_x, dst_stride, src, src_stride,
  188. start_y, end_y, block_h, w);
  189. }
  190. // fill left
  191. if (start_x) {
  192. if (start_x <= 22) {
  193. hfix_tbl[(start_x - 1) >> 1](dst, dst_stride, start_x, block_h);
  194. } else {
  195. h_extend_var(dst, dst_stride,
  196. start_x, (start_x + 1) >> 1, block_h);
  197. }
  198. }
  199. // fill right
  200. p = block_w - end_x;
  201. if (p) {
  202. if (p <= 22) {
  203. hfix_tbl[(p - 1) >> 1](dst + end_x - (p & 1), dst_stride,
  204. -!(p & 1), block_h);
  205. } else {
  206. h_extend_var(dst + end_x - (p & 1), dst_stride,
  207. -!(p & 1), (p + 1) >> 1, block_h);
  208. }
  209. }
  210. }
  211. #if ARCH_X86_32
  212. static av_noinline void emulated_edge_mc_mmx(uint8_t *buf, const uint8_t *src,
  213. ptrdiff_t buf_stride,
  214. ptrdiff_t src_stride,
  215. int block_w, int block_h,
  216. int src_x, int src_y, int w, int h)
  217. {
  218. emulated_edge_mc(buf, src, buf_stride, src_stride, block_w, block_h,
  219. src_x, src_y, w, h, vfixtbl_mmx, &ff_emu_edge_vvar_mmx,
  220. hfixtbl_mmx, &ff_emu_edge_hvar_mmx);
  221. }
  222. static av_noinline void emulated_edge_mc_sse(uint8_t *buf, const uint8_t *src,
  223. ptrdiff_t buf_stride,
  224. ptrdiff_t src_stride,
  225. int block_w, int block_h,
  226. int src_x, int src_y, int w, int h)
  227. {
  228. emulated_edge_mc(buf, src, buf_stride, src_stride, block_w, block_h,
  229. src_x, src_y, w, h, vfixtbl_sse, &ff_emu_edge_vvar_sse,
  230. hfixtbl_mmx, &ff_emu_edge_hvar_mmx);
  231. }
  232. #endif
  233. static av_noinline void emulated_edge_mc_sse2(uint8_t *buf, const uint8_t *src,
  234. ptrdiff_t buf_stride,
  235. ptrdiff_t src_stride,
  236. int block_w, int block_h,
  237. int src_x, int src_y, int w,
  238. int h)
  239. {
  240. emulated_edge_mc(buf, src, buf_stride, src_stride, block_w, block_h,
  241. src_x, src_y, w, h, vfixtbl_sse, &ff_emu_edge_vvar_sse,
  242. hfixtbl_sse2, &ff_emu_edge_hvar_sse2);
  243. }
  244. #if HAVE_AVX2_EXTERNAL
  245. static av_noinline void emulated_edge_mc_avx2(uint8_t *buf, const uint8_t *src,
  246. ptrdiff_t buf_stride,
  247. ptrdiff_t src_stride,
  248. int block_w, int block_h,
  249. int src_x, int src_y, int w,
  250. int h)
  251. {
  252. emulated_edge_mc(buf, src, buf_stride, src_stride, block_w, block_h,
  253. src_x, src_y, w, h, vfixtbl_sse, &ff_emu_edge_vvar_sse,
  254. hfixtbl_avx2, &ff_emu_edge_hvar_avx2);
  255. }
  256. #endif /* HAVE_AVX2_EXTERNAL */
  257. #endif /* HAVE_X86ASM */
  258. void ff_prefetch_mmxext(uint8_t *buf, ptrdiff_t stride, int h);
  259. void ff_prefetch_3dnow(uint8_t *buf, ptrdiff_t stride, int h);
  260. av_cold void ff_videodsp_init_x86(VideoDSPContext *ctx, int bpc)
  261. {
  262. #if HAVE_X86ASM
  263. int cpu_flags = av_get_cpu_flags();
  264. #if ARCH_X86_32
  265. if (EXTERNAL_MMX(cpu_flags) && bpc <= 8) {
  266. ctx->emulated_edge_mc = emulated_edge_mc_mmx;
  267. }
  268. if (EXTERNAL_AMD3DNOW(cpu_flags)) {
  269. ctx->prefetch = ff_prefetch_3dnow;
  270. }
  271. #endif /* ARCH_X86_32 */
  272. if (EXTERNAL_MMXEXT(cpu_flags)) {
  273. ctx->prefetch = ff_prefetch_mmxext;
  274. }
  275. #if ARCH_X86_32
  276. if (EXTERNAL_SSE(cpu_flags) && bpc <= 8) {
  277. ctx->emulated_edge_mc = emulated_edge_mc_sse;
  278. }
  279. #endif /* ARCH_X86_32 */
  280. if (EXTERNAL_SSE2(cpu_flags) && bpc <= 8) {
  281. ctx->emulated_edge_mc = emulated_edge_mc_sse2;
  282. }
  283. #if HAVE_AVX2_EXTERNAL
  284. if (EXTERNAL_AVX2(cpu_flags) && bpc <= 8) {
  285. ctx->emulated_edge_mc = emulated_edge_mc_avx2;
  286. }
  287. #endif
  288. #endif /* HAVE_X86ASM */
  289. }