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.

375 lines
17KB

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