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.

386 lines
18KB

  1. /*
  2. * Copyright (c) 2004-2005 Michael Niedermayer, Loren Merritt
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/x86/asm.h"
  22. #include "libavcodec/h264dsp.h"
  23. #include "dsputil_mmx.h"
  24. /***********************************/
  25. /* IDCT */
  26. #define IDCT_ADD_FUNC(NUM, DEPTH, OPT) \
  27. void ff_h264_idct ## NUM ## _add_ ## DEPTH ## _ ## OPT(uint8_t *dst, \
  28. int16_t *block, \
  29. int stride);
  30. IDCT_ADD_FUNC(, 8, mmx)
  31. IDCT_ADD_FUNC(, 10, sse2)
  32. IDCT_ADD_FUNC(_dc, 8, mmx2)
  33. IDCT_ADD_FUNC(_dc, 10, mmx2)
  34. IDCT_ADD_FUNC(8_dc, 8, mmx2)
  35. IDCT_ADD_FUNC(8_dc, 10, sse2)
  36. IDCT_ADD_FUNC(8, 8, mmx)
  37. IDCT_ADD_FUNC(8, 8, sse2)
  38. IDCT_ADD_FUNC(8, 10, sse2)
  39. #if HAVE_AVX
  40. IDCT_ADD_FUNC(, 10, avx)
  41. IDCT_ADD_FUNC(8_dc, 10, avx)
  42. IDCT_ADD_FUNC(8, 10, avx)
  43. #endif
  44. #define IDCT_ADD_REP_FUNC(NUM, REP, DEPTH, OPT) \
  45. void ff_h264_idct ## NUM ## _add ## REP ## _ ## DEPTH ## _ ## OPT \
  46. (uint8_t *dst, const int *block_offset, \
  47. DCTELEM *block, int stride, const uint8_t nnzc[6 * 8]);
  48. IDCT_ADD_REP_FUNC(8, 4, 8, mmx)
  49. IDCT_ADD_REP_FUNC(8, 4, 8, mmx2)
  50. IDCT_ADD_REP_FUNC(8, 4, 8, sse2)
  51. IDCT_ADD_REP_FUNC(8, 4, 10, sse2)
  52. IDCT_ADD_REP_FUNC(8, 4, 10, avx)
  53. IDCT_ADD_REP_FUNC(, 16, 8, mmx)
  54. IDCT_ADD_REP_FUNC(, 16, 8, mmx2)
  55. IDCT_ADD_REP_FUNC(, 16, 8, sse2)
  56. IDCT_ADD_REP_FUNC(, 16, 10, sse2)
  57. IDCT_ADD_REP_FUNC(, 16intra, 8, mmx)
  58. IDCT_ADD_REP_FUNC(, 16intra, 8, mmx2)
  59. IDCT_ADD_REP_FUNC(, 16intra, 8, sse2)
  60. IDCT_ADD_REP_FUNC(, 16intra, 10, sse2)
  61. #if HAVE_AVX
  62. IDCT_ADD_REP_FUNC(, 16, 10, avx)
  63. IDCT_ADD_REP_FUNC(, 16intra, 10, avx)
  64. #endif
  65. #define IDCT_ADD_REP_FUNC2(NUM, REP, DEPTH, OPT) \
  66. void ff_h264_idct ## NUM ## _add ## REP ## _ ## DEPTH ## _ ## OPT \
  67. (uint8_t **dst, const int *block_offset, \
  68. DCTELEM *block, int stride, const uint8_t nnzc[6 * 8]);
  69. IDCT_ADD_REP_FUNC2(, 8, 8, mmx)
  70. IDCT_ADD_REP_FUNC2(, 8, 8, mmx2)
  71. IDCT_ADD_REP_FUNC2(, 8, 8, sse2)
  72. IDCT_ADD_REP_FUNC2(, 8, 10, sse2)
  73. #if HAVE_AVX
  74. IDCT_ADD_REP_FUNC2(, 8, 10, avx)
  75. #endif
  76. void ff_h264_luma_dc_dequant_idct_mmx(DCTELEM *output, DCTELEM *input, int qmul);
  77. void ff_h264_luma_dc_dequant_idct_sse2(DCTELEM *output, DCTELEM *input, int qmul);
  78. /***********************************/
  79. /* deblocking */
  80. void ff_h264_loop_filter_strength_mmx2(int16_t bS[2][4][4], uint8_t nnz[40],
  81. int8_t ref[2][40], int16_t mv[2][40][2],
  82. int bidir, int edges, int step,
  83. int mask_mv0, int mask_mv1, int field);
  84. #define LF_FUNC(DIR, TYPE, DEPTH, OPT) \
  85. void ff_deblock_ ## DIR ## _ ## TYPE ## _ ## DEPTH ## _ ## OPT(uint8_t *pix, \
  86. int stride, \
  87. int alpha, \
  88. int beta, \
  89. int8_t *tc0);
  90. #define LF_IFUNC(DIR, TYPE, DEPTH, OPT) \
  91. void ff_deblock_ ## DIR ## _ ## TYPE ## _ ## DEPTH ## _ ## OPT(uint8_t *pix, \
  92. int stride, \
  93. int alpha, \
  94. int beta);
  95. #define LF_FUNCS(type, depth) \
  96. LF_FUNC(h, chroma, depth, mmx2) \
  97. LF_IFUNC(h, chroma_intra, depth, mmx2) \
  98. LF_FUNC(v, chroma, depth, mmx2) \
  99. LF_IFUNC(v, chroma_intra, depth, mmx2) \
  100. LF_FUNC(h, luma, depth, mmx2) \
  101. LF_IFUNC(h, luma_intra, depth, mmx2) \
  102. LF_FUNC(h, luma, depth, sse2) \
  103. LF_IFUNC(h, luma_intra, depth, sse2) \
  104. LF_FUNC(v, luma, depth, sse2) \
  105. LF_IFUNC(v, luma_intra, depth, sse2) \
  106. LF_FUNC(h, chroma, depth, sse2) \
  107. LF_IFUNC(h, chroma_intra, depth, sse2) \
  108. LF_FUNC(v, chroma, depth, sse2) \
  109. LF_IFUNC(v, chroma_intra, depth, sse2) \
  110. LF_FUNC(h, luma, depth, avx) \
  111. LF_IFUNC(h, luma_intra, depth, avx) \
  112. LF_FUNC(v, luma, depth, avx) \
  113. LF_IFUNC(v, luma_intra, depth, avx) \
  114. LF_FUNC(h, chroma, depth, avx) \
  115. LF_IFUNC(h, chroma_intra, depth, avx) \
  116. LF_FUNC(v, chroma, depth, avx) \
  117. LF_IFUNC(v, chroma_intra, depth, avx)
  118. LF_FUNCS(uint8_t, 8)
  119. LF_FUNCS(uint16_t, 10)
  120. #if ARCH_X86_32 && HAVE_YASM
  121. LF_FUNC(v8, luma, 8, mmx2)
  122. static void ff_deblock_v_luma_8_mmx2(uint8_t *pix, int stride, int alpha,
  123. int beta, int8_t *tc0)
  124. {
  125. if ((tc0[0] & tc0[1]) >= 0)
  126. ff_deblock_v8_luma_8_mmx2(pix + 0, stride, alpha, beta, tc0);
  127. if ((tc0[2] & tc0[3]) >= 0)
  128. ff_deblock_v8_luma_8_mmx2(pix + 8, stride, alpha, beta, tc0 + 2);
  129. }
  130. LF_IFUNC(v8, luma_intra, 8, mmx2)
  131. static void ff_deblock_v_luma_intra_8_mmx2(uint8_t *pix, int stride,
  132. int alpha, int beta)
  133. {
  134. ff_deblock_v8_luma_intra_8_mmx2(pix + 0, stride, alpha, beta);
  135. ff_deblock_v8_luma_intra_8_mmx2(pix + 8, stride, alpha, beta);
  136. }
  137. #endif /* ARCH_X86_32 */
  138. LF_FUNC(v, luma, 10, mmx2)
  139. LF_IFUNC(v, luma_intra, 10, mmx2)
  140. /***********************************/
  141. /* weighted prediction */
  142. #define H264_WEIGHT(W, OPT) \
  143. void ff_h264_weight_ ## W ## _ ## OPT(uint8_t *dst, int stride, \
  144. int height, int log2_denom, \
  145. int weight, int offset);
  146. #define H264_BIWEIGHT(W, OPT) \
  147. void ff_h264_biweight_ ## W ## _ ## OPT(uint8_t *dst, uint8_t *src, \
  148. int stride, int height, \
  149. int log2_denom, int weightd, \
  150. int weights, int offset);
  151. #define H264_BIWEIGHT_MMX(W) \
  152. H264_WEIGHT(W, mmx2) \
  153. H264_BIWEIGHT(W, mmx2)
  154. #define H264_BIWEIGHT_MMX_SSE(W) \
  155. H264_BIWEIGHT_MMX(W) \
  156. H264_WEIGHT(W, sse2) \
  157. H264_BIWEIGHT(W, sse2) \
  158. H264_BIWEIGHT(W, ssse3)
  159. H264_BIWEIGHT_MMX_SSE(16)
  160. H264_BIWEIGHT_MMX_SSE(8)
  161. H264_BIWEIGHT_MMX(4)
  162. #define H264_WEIGHT_10(W, DEPTH, OPT) \
  163. void ff_h264_weight_ ## W ## _ ## DEPTH ## _ ## OPT(uint8_t *dst, \
  164. int stride, \
  165. int height, \
  166. int log2_denom, \
  167. int weight, \
  168. int offset);
  169. #define H264_BIWEIGHT_10(W, DEPTH, OPT) \
  170. void ff_h264_biweight_ ## W ## _ ## DEPTH ## _ ## OPT(uint8_t *dst, \
  171. uint8_t *src, \
  172. int stride, \
  173. int height, \
  174. int log2_denom, \
  175. int weightd, \
  176. int weights, \
  177. int offset);
  178. #define H264_BIWEIGHT_10_SSE(W, DEPTH) \
  179. H264_WEIGHT_10(W, DEPTH, sse2) \
  180. H264_WEIGHT_10(W, DEPTH, sse4) \
  181. H264_BIWEIGHT_10(W, DEPTH, sse2) \
  182. H264_BIWEIGHT_10(W, DEPTH, sse4)
  183. H264_BIWEIGHT_10_SSE(16, 10)
  184. H264_BIWEIGHT_10_SSE(8, 10)
  185. H264_BIWEIGHT_10_SSE(4, 10)
  186. void ff_h264dsp_init_x86(H264DSPContext *c, const int bit_depth,
  187. const int chroma_format_idc)
  188. {
  189. #if HAVE_YASM
  190. int mm_flags = av_get_cpu_flags();
  191. if (chroma_format_idc == 1 && mm_flags & AV_CPU_FLAG_MMXEXT)
  192. c->h264_loop_filter_strength = ff_h264_loop_filter_strength_mmx2;
  193. if (bit_depth == 8) {
  194. if (mm_flags & AV_CPU_FLAG_MMX) {
  195. c->h264_idct_dc_add =
  196. c->h264_idct_add = ff_h264_idct_add_8_mmx;
  197. c->h264_idct8_dc_add =
  198. c->h264_idct8_add = ff_h264_idct8_add_8_mmx;
  199. c->h264_idct_add16 = ff_h264_idct_add16_8_mmx;
  200. c->h264_idct8_add4 = ff_h264_idct8_add4_8_mmx;
  201. if (chroma_format_idc == 1)
  202. c->h264_idct_add8 = ff_h264_idct_add8_8_mmx;
  203. c->h264_idct_add16intra = ff_h264_idct_add16intra_8_mmx;
  204. if (mm_flags & AV_CPU_FLAG_CMOV)
  205. c->h264_luma_dc_dequant_idct = ff_h264_luma_dc_dequant_idct_mmx;
  206. if (mm_flags & AV_CPU_FLAG_MMXEXT) {
  207. c->h264_idct_dc_add = ff_h264_idct_dc_add_8_mmx2;
  208. c->h264_idct8_dc_add = ff_h264_idct8_dc_add_8_mmx2;
  209. c->h264_idct_add16 = ff_h264_idct_add16_8_mmx2;
  210. c->h264_idct8_add4 = ff_h264_idct8_add4_8_mmx2;
  211. if (chroma_format_idc == 1)
  212. c->h264_idct_add8 = ff_h264_idct_add8_8_mmx2;
  213. c->h264_idct_add16intra = ff_h264_idct_add16intra_8_mmx2;
  214. c->h264_v_loop_filter_chroma = ff_deblock_v_chroma_8_mmx2;
  215. c->h264_v_loop_filter_chroma_intra = ff_deblock_v_chroma_intra_8_mmx2;
  216. if (chroma_format_idc == 1) {
  217. c->h264_h_loop_filter_chroma = ff_deblock_h_chroma_8_mmx2;
  218. c->h264_h_loop_filter_chroma_intra = ff_deblock_h_chroma_intra_8_mmx2;
  219. }
  220. #if ARCH_X86_32
  221. c->h264_v_loop_filter_luma = ff_deblock_v_luma_8_mmx2;
  222. c->h264_h_loop_filter_luma = ff_deblock_h_luma_8_mmx2;
  223. c->h264_v_loop_filter_luma_intra = ff_deblock_v_luma_intra_8_mmx2;
  224. c->h264_h_loop_filter_luma_intra = ff_deblock_h_luma_intra_8_mmx2;
  225. #endif /* ARCH_X86_32 */
  226. c->weight_h264_pixels_tab[0] = ff_h264_weight_16_mmx2;
  227. c->weight_h264_pixels_tab[1] = ff_h264_weight_8_mmx2;
  228. c->weight_h264_pixels_tab[2] = ff_h264_weight_4_mmx2;
  229. c->biweight_h264_pixels_tab[0] = ff_h264_biweight_16_mmx2;
  230. c->biweight_h264_pixels_tab[1] = ff_h264_biweight_8_mmx2;
  231. c->biweight_h264_pixels_tab[2] = ff_h264_biweight_4_mmx2;
  232. if (mm_flags & AV_CPU_FLAG_SSE2) {
  233. c->h264_idct8_add = ff_h264_idct8_add_8_sse2;
  234. c->h264_idct_add16 = ff_h264_idct_add16_8_sse2;
  235. c->h264_idct8_add4 = ff_h264_idct8_add4_8_sse2;
  236. if (chroma_format_idc == 1)
  237. c->h264_idct_add8 = ff_h264_idct_add8_8_sse2;
  238. c->h264_idct_add16intra = ff_h264_idct_add16intra_8_sse2;
  239. c->h264_luma_dc_dequant_idct = ff_h264_luma_dc_dequant_idct_sse2;
  240. c->weight_h264_pixels_tab[0] = ff_h264_weight_16_sse2;
  241. c->weight_h264_pixels_tab[1] = ff_h264_weight_8_sse2;
  242. c->biweight_h264_pixels_tab[0] = ff_h264_biweight_16_sse2;
  243. c->biweight_h264_pixels_tab[1] = ff_h264_biweight_8_sse2;
  244. #if HAVE_ALIGNED_STACK
  245. c->h264_v_loop_filter_luma = ff_deblock_v_luma_8_sse2;
  246. c->h264_h_loop_filter_luma = ff_deblock_h_luma_8_sse2;
  247. c->h264_v_loop_filter_luma_intra = ff_deblock_v_luma_intra_8_sse2;
  248. c->h264_h_loop_filter_luma_intra = ff_deblock_h_luma_intra_8_sse2;
  249. #endif /* HAVE_ALIGNED_STACK */
  250. }
  251. if (mm_flags & AV_CPU_FLAG_SSSE3) {
  252. c->biweight_h264_pixels_tab[0] = ff_h264_biweight_16_ssse3;
  253. c->biweight_h264_pixels_tab[1] = ff_h264_biweight_8_ssse3;
  254. }
  255. if (HAVE_AVX && mm_flags & AV_CPU_FLAG_AVX) {
  256. #if HAVE_ALIGNED_STACK
  257. c->h264_v_loop_filter_luma = ff_deblock_v_luma_8_avx;
  258. c->h264_h_loop_filter_luma = ff_deblock_h_luma_8_avx;
  259. c->h264_v_loop_filter_luma_intra = ff_deblock_v_luma_intra_8_avx;
  260. c->h264_h_loop_filter_luma_intra = ff_deblock_h_luma_intra_8_avx;
  261. #endif /* HAVE_ALIGNED_STACK */
  262. }
  263. }
  264. }
  265. } else if (bit_depth == 10) {
  266. if (mm_flags & AV_CPU_FLAG_MMX) {
  267. if (mm_flags & AV_CPU_FLAG_MMXEXT) {
  268. #if ARCH_X86_32
  269. c->h264_v_loop_filter_chroma = ff_deblock_v_chroma_10_mmx2;
  270. c->h264_v_loop_filter_chroma_intra = ff_deblock_v_chroma_intra_10_mmx2;
  271. c->h264_v_loop_filter_luma = ff_deblock_v_luma_10_mmx2;
  272. c->h264_h_loop_filter_luma = ff_deblock_h_luma_10_mmx2;
  273. c->h264_v_loop_filter_luma_intra = ff_deblock_v_luma_intra_10_mmx2;
  274. c->h264_h_loop_filter_luma_intra = ff_deblock_h_luma_intra_10_mmx2;
  275. #endif /* ARCH_X86_32 */
  276. c->h264_idct_dc_add = ff_h264_idct_dc_add_10_mmx2;
  277. if (mm_flags & AV_CPU_FLAG_SSE2) {
  278. c->h264_idct_add = ff_h264_idct_add_10_sse2;
  279. c->h264_idct8_dc_add = ff_h264_idct8_dc_add_10_sse2;
  280. c->h264_idct_add16 = ff_h264_idct_add16_10_sse2;
  281. if (chroma_format_idc == 1)
  282. c->h264_idct_add8 = ff_h264_idct_add8_10_sse2;
  283. c->h264_idct_add16intra = ff_h264_idct_add16intra_10_sse2;
  284. #if HAVE_ALIGNED_STACK
  285. c->h264_idct8_add = ff_h264_idct8_add_10_sse2;
  286. c->h264_idct8_add4 = ff_h264_idct8_add4_10_sse2;
  287. #endif /* HAVE_ALIGNED_STACK */
  288. c->weight_h264_pixels_tab[0] = ff_h264_weight_16_10_sse2;
  289. c->weight_h264_pixels_tab[1] = ff_h264_weight_8_10_sse2;
  290. c->weight_h264_pixels_tab[2] = ff_h264_weight_4_10_sse2;
  291. c->biweight_h264_pixels_tab[0] = ff_h264_biweight_16_10_sse2;
  292. c->biweight_h264_pixels_tab[1] = ff_h264_biweight_8_10_sse2;
  293. c->biweight_h264_pixels_tab[2] = ff_h264_biweight_4_10_sse2;
  294. c->h264_v_loop_filter_chroma = ff_deblock_v_chroma_10_sse2;
  295. c->h264_v_loop_filter_chroma_intra = ff_deblock_v_chroma_intra_10_sse2;
  296. #if HAVE_ALIGNED_STACK
  297. c->h264_v_loop_filter_luma = ff_deblock_v_luma_10_sse2;
  298. c->h264_h_loop_filter_luma = ff_deblock_h_luma_10_sse2;
  299. c->h264_v_loop_filter_luma_intra = ff_deblock_v_luma_intra_10_sse2;
  300. c->h264_h_loop_filter_luma_intra = ff_deblock_h_luma_intra_10_sse2;
  301. #endif /* HAVE_ALIGNED_STACK */
  302. }
  303. if (mm_flags & AV_CPU_FLAG_SSE4) {
  304. c->weight_h264_pixels_tab[0] = ff_h264_weight_16_10_sse4;
  305. c->weight_h264_pixels_tab[1] = ff_h264_weight_8_10_sse4;
  306. c->weight_h264_pixels_tab[2] = ff_h264_weight_4_10_sse4;
  307. c->biweight_h264_pixels_tab[0] = ff_h264_biweight_16_10_sse4;
  308. c->biweight_h264_pixels_tab[1] = ff_h264_biweight_8_10_sse4;
  309. c->biweight_h264_pixels_tab[2] = ff_h264_biweight_4_10_sse4;
  310. }
  311. #if HAVE_AVX
  312. if (mm_flags & AV_CPU_FLAG_AVX) {
  313. c->h264_idct_dc_add =
  314. c->h264_idct_add = ff_h264_idct_add_10_avx;
  315. c->h264_idct8_dc_add = ff_h264_idct8_dc_add_10_avx;
  316. c->h264_idct_add16 = ff_h264_idct_add16_10_avx;
  317. if (chroma_format_idc == 1)
  318. c->h264_idct_add8 = ff_h264_idct_add8_10_avx;
  319. c->h264_idct_add16intra = ff_h264_idct_add16intra_10_avx;
  320. #if HAVE_ALIGNED_STACK
  321. c->h264_idct8_add = ff_h264_idct8_add_10_avx;
  322. c->h264_idct8_add4 = ff_h264_idct8_add4_10_avx;
  323. #endif /* HAVE_ALIGNED_STACK */
  324. c->h264_v_loop_filter_chroma = ff_deblock_v_chroma_10_avx;
  325. c->h264_v_loop_filter_chroma_intra = ff_deblock_v_chroma_intra_10_avx;
  326. #if HAVE_ALIGNED_STACK
  327. c->h264_v_loop_filter_luma = ff_deblock_v_luma_10_avx;
  328. c->h264_h_loop_filter_luma = ff_deblock_h_luma_10_avx;
  329. c->h264_v_loop_filter_luma_intra = ff_deblock_v_luma_intra_10_avx;
  330. c->h264_h_loop_filter_luma_intra = ff_deblock_h_luma_intra_10_avx;
  331. #endif /* HAVE_ALIGNED_STACK */
  332. }
  333. #endif /* HAVE_AVX */
  334. }
  335. }
  336. }
  337. #endif /* HAVE_YASM */
  338. }