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.

364 lines
15KB

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