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.

826 lines
36KB

  1. /*
  2. * VC-1 and WMV3 - DSP functions MMX-optimized
  3. * Copyright (c) 2007 Christophe GISQUET <christophe.gisquet@free.fr>
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use,
  9. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following
  12. * conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "libavutil/cpu.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/mem.h"
  29. #include "libavutil/x86/asm.h"
  30. #include "libavcodec/dsputil.h"
  31. #include "dsputil_mmx.h"
  32. #include "libavcodec/vc1dsp.h"
  33. #if HAVE_INLINE_ASM
  34. #define OP_PUT(S,D)
  35. #define OP_AVG(S,D) "pavgb " #S ", " #D " \n\t"
  36. /** Add rounder from mm7 to mm3 and pack result at destination */
  37. #define NORMALIZE_MMX(SHIFT) \
  38. "paddw %%mm7, %%mm3 \n\t" /* +bias-r */ \
  39. "paddw %%mm7, %%mm4 \n\t" /* +bias-r */ \
  40. "psraw "SHIFT", %%mm3 \n\t" \
  41. "psraw "SHIFT", %%mm4 \n\t"
  42. #define TRANSFER_DO_PACK(OP) \
  43. "packuswb %%mm4, %%mm3 \n\t" \
  44. OP((%2), %%mm3) \
  45. "movq %%mm3, (%2) \n\t"
  46. #define TRANSFER_DONT_PACK(OP) \
  47. OP(0(%2), %%mm3) \
  48. OP(8(%2), %%mm4) \
  49. "movq %%mm3, 0(%2) \n\t" \
  50. "movq %%mm4, 8(%2) \n\t"
  51. /** @see MSPEL_FILTER13_CORE for use as UNPACK macro */
  52. #define DO_UNPACK(reg) "punpcklbw %%mm0, " reg "\n\t"
  53. #define DONT_UNPACK(reg)
  54. /** Compute the rounder 32-r or 8-r and unpacks it to mm7 */
  55. #define LOAD_ROUNDER_MMX(ROUND) \
  56. "movd "ROUND", %%mm7 \n\t" \
  57. "punpcklwd %%mm7, %%mm7 \n\t" \
  58. "punpckldq %%mm7, %%mm7 \n\t"
  59. #define SHIFT2_LINE(OFF, R0,R1,R2,R3) \
  60. "paddw %%mm"#R2", %%mm"#R1" \n\t" \
  61. "movd (%0,%3), %%mm"#R0" \n\t" \
  62. "pmullw %%mm6, %%mm"#R1" \n\t" \
  63. "punpcklbw %%mm0, %%mm"#R0" \n\t" \
  64. "movd (%0,%2), %%mm"#R3" \n\t" \
  65. "psubw %%mm"#R0", %%mm"#R1" \n\t" \
  66. "punpcklbw %%mm0, %%mm"#R3" \n\t" \
  67. "paddw %%mm7, %%mm"#R1" \n\t" \
  68. "psubw %%mm"#R3", %%mm"#R1" \n\t" \
  69. "psraw %4, %%mm"#R1" \n\t" \
  70. "movq %%mm"#R1", "#OFF"(%1) \n\t" \
  71. "add %2, %0 \n\t"
  72. /** Sacrifying mm6 allows to pipeline loads from src */
  73. static void vc1_put_ver_16b_shift2_mmx(int16_t *dst,
  74. const uint8_t *src, x86_reg stride,
  75. int rnd, int64_t shift)
  76. {
  77. __asm__ volatile(
  78. "mov $3, %%"REG_c" \n\t"
  79. LOAD_ROUNDER_MMX("%5")
  80. "movq "MANGLE(ff_pw_9)", %%mm6 \n\t"
  81. "1: \n\t"
  82. "movd (%0), %%mm2 \n\t"
  83. "add %2, %0 \n\t"
  84. "movd (%0), %%mm3 \n\t"
  85. "punpcklbw %%mm0, %%mm2 \n\t"
  86. "punpcklbw %%mm0, %%mm3 \n\t"
  87. SHIFT2_LINE( 0, 1, 2, 3, 4)
  88. SHIFT2_LINE( 24, 2, 3, 4, 1)
  89. SHIFT2_LINE( 48, 3, 4, 1, 2)
  90. SHIFT2_LINE( 72, 4, 1, 2, 3)
  91. SHIFT2_LINE( 96, 1, 2, 3, 4)
  92. SHIFT2_LINE(120, 2, 3, 4, 1)
  93. SHIFT2_LINE(144, 3, 4, 1, 2)
  94. SHIFT2_LINE(168, 4, 1, 2, 3)
  95. "sub %6, %0 \n\t"
  96. "add $8, %1 \n\t"
  97. "dec %%"REG_c" \n\t"
  98. "jnz 1b \n\t"
  99. : "+r"(src), "+r"(dst)
  100. : "r"(stride), "r"(-2*stride),
  101. "m"(shift), "m"(rnd), "r"(9*stride-4)
  102. : "%"REG_c, "memory"
  103. );
  104. }
  105. /**
  106. * Data is already unpacked, so some operations can directly be made from
  107. * memory.
  108. */
  109. #define VC1_HOR_16b_SHIFT2(OP, OPNAME)\
  110. static void OPNAME ## vc1_hor_16b_shift2_mmx(uint8_t *dst, x86_reg stride,\
  111. const int16_t *src, int rnd)\
  112. {\
  113. int h = 8;\
  114. \
  115. src -= 1;\
  116. rnd -= (-1+9+9-1)*1024; /* Add -1024 bias */\
  117. __asm__ volatile(\
  118. LOAD_ROUNDER_MMX("%4")\
  119. "movq "MANGLE(ff_pw_128)", %%mm6\n\t"\
  120. "movq "MANGLE(ff_pw_9)", %%mm5 \n\t"\
  121. "1: \n\t"\
  122. "movq 2*0+0(%1), %%mm1 \n\t"\
  123. "movq 2*0+8(%1), %%mm2 \n\t"\
  124. "movq 2*1+0(%1), %%mm3 \n\t"\
  125. "movq 2*1+8(%1), %%mm4 \n\t"\
  126. "paddw 2*3+0(%1), %%mm1 \n\t"\
  127. "paddw 2*3+8(%1), %%mm2 \n\t"\
  128. "paddw 2*2+0(%1), %%mm3 \n\t"\
  129. "paddw 2*2+8(%1), %%mm4 \n\t"\
  130. "pmullw %%mm5, %%mm3 \n\t"\
  131. "pmullw %%mm5, %%mm4 \n\t"\
  132. "psubw %%mm1, %%mm3 \n\t"\
  133. "psubw %%mm2, %%mm4 \n\t"\
  134. NORMALIZE_MMX("$7")\
  135. /* Remove bias */\
  136. "paddw %%mm6, %%mm3 \n\t"\
  137. "paddw %%mm6, %%mm4 \n\t"\
  138. TRANSFER_DO_PACK(OP)\
  139. "add $24, %1 \n\t"\
  140. "add %3, %2 \n\t"\
  141. "decl %0 \n\t"\
  142. "jnz 1b \n\t"\
  143. : "+r"(h), "+r" (src), "+r" (dst)\
  144. : "r"(stride), "m"(rnd)\
  145. : "memory"\
  146. );\
  147. }
  148. VC1_HOR_16b_SHIFT2(OP_PUT, put_)
  149. VC1_HOR_16b_SHIFT2(OP_AVG, avg_)
  150. /**
  151. * Purely vertical or horizontal 1/2 shift interpolation.
  152. * Sacrify mm6 for *9 factor.
  153. */
  154. #define VC1_SHIFT2(OP, OPNAME)\
  155. static void OPNAME ## vc1_shift2_mmx(uint8_t *dst, const uint8_t *src,\
  156. x86_reg stride, int rnd, x86_reg offset)\
  157. {\
  158. rnd = 8-rnd;\
  159. __asm__ volatile(\
  160. "mov $8, %%"REG_c" \n\t"\
  161. LOAD_ROUNDER_MMX("%5")\
  162. "movq "MANGLE(ff_pw_9)", %%mm6\n\t"\
  163. "1: \n\t"\
  164. "movd 0(%0 ), %%mm3 \n\t"\
  165. "movd 4(%0 ), %%mm4 \n\t"\
  166. "movd 0(%0,%2), %%mm1 \n\t"\
  167. "movd 4(%0,%2), %%mm2 \n\t"\
  168. "add %2, %0 \n\t"\
  169. "punpcklbw %%mm0, %%mm3 \n\t"\
  170. "punpcklbw %%mm0, %%mm4 \n\t"\
  171. "punpcklbw %%mm0, %%mm1 \n\t"\
  172. "punpcklbw %%mm0, %%mm2 \n\t"\
  173. "paddw %%mm1, %%mm3 \n\t"\
  174. "paddw %%mm2, %%mm4 \n\t"\
  175. "movd 0(%0,%3), %%mm1 \n\t"\
  176. "movd 4(%0,%3), %%mm2 \n\t"\
  177. "pmullw %%mm6, %%mm3 \n\t" /* 0,9,9,0*/\
  178. "pmullw %%mm6, %%mm4 \n\t" /* 0,9,9,0*/\
  179. "punpcklbw %%mm0, %%mm1 \n\t"\
  180. "punpcklbw %%mm0, %%mm2 \n\t"\
  181. "psubw %%mm1, %%mm3 \n\t" /*-1,9,9,0*/\
  182. "psubw %%mm2, %%mm4 \n\t" /*-1,9,9,0*/\
  183. "movd 0(%0,%2), %%mm1 \n\t"\
  184. "movd 4(%0,%2), %%mm2 \n\t"\
  185. "punpcklbw %%mm0, %%mm1 \n\t"\
  186. "punpcklbw %%mm0, %%mm2 \n\t"\
  187. "psubw %%mm1, %%mm3 \n\t" /*-1,9,9,-1*/\
  188. "psubw %%mm2, %%mm4 \n\t" /*-1,9,9,-1*/\
  189. NORMALIZE_MMX("$4")\
  190. "packuswb %%mm4, %%mm3 \n\t"\
  191. OP((%1), %%mm3)\
  192. "movq %%mm3, (%1) \n\t"\
  193. "add %6, %0 \n\t"\
  194. "add %4, %1 \n\t"\
  195. "dec %%"REG_c" \n\t"\
  196. "jnz 1b \n\t"\
  197. : "+r"(src), "+r"(dst)\
  198. : "r"(offset), "r"(-2*offset), "g"(stride), "m"(rnd),\
  199. "g"(stride-offset)\
  200. : "%"REG_c, "memory"\
  201. );\
  202. }
  203. VC1_SHIFT2(OP_PUT, put_)
  204. VC1_SHIFT2(OP_AVG, avg_)
  205. /**
  206. * Core of the 1/4 and 3/4 shift bicubic interpolation.
  207. *
  208. * @param UNPACK Macro unpacking arguments from 8 to 16bits (can be empty).
  209. * @param MOVQ "movd 1" or "movq 2", if data read is already unpacked.
  210. * @param A1 Address of 1st tap (beware of unpacked/packed).
  211. * @param A2 Address of 2nd tap
  212. * @param A3 Address of 3rd tap
  213. * @param A4 Address of 4th tap
  214. */
  215. #define MSPEL_FILTER13_CORE(UNPACK, MOVQ, A1, A2, A3, A4) \
  216. MOVQ "*0+"A1", %%mm1 \n\t" \
  217. MOVQ "*4+"A1", %%mm2 \n\t" \
  218. UNPACK("%%mm1") \
  219. UNPACK("%%mm2") \
  220. "pmullw "MANGLE(ff_pw_3)", %%mm1\n\t" \
  221. "pmullw "MANGLE(ff_pw_3)", %%mm2\n\t" \
  222. MOVQ "*0+"A2", %%mm3 \n\t" \
  223. MOVQ "*4+"A2", %%mm4 \n\t" \
  224. UNPACK("%%mm3") \
  225. UNPACK("%%mm4") \
  226. "pmullw %%mm6, %%mm3 \n\t" /* *18 */ \
  227. "pmullw %%mm6, %%mm4 \n\t" /* *18 */ \
  228. "psubw %%mm1, %%mm3 \n\t" /* 18,-3 */ \
  229. "psubw %%mm2, %%mm4 \n\t" /* 18,-3 */ \
  230. MOVQ "*0+"A4", %%mm1 \n\t" \
  231. MOVQ "*4+"A4", %%mm2 \n\t" \
  232. UNPACK("%%mm1") \
  233. UNPACK("%%mm2") \
  234. "psllw $2, %%mm1 \n\t" /* 4* */ \
  235. "psllw $2, %%mm2 \n\t" /* 4* */ \
  236. "psubw %%mm1, %%mm3 \n\t" /* -4,18,-3 */ \
  237. "psubw %%mm2, %%mm4 \n\t" /* -4,18,-3 */ \
  238. MOVQ "*0+"A3", %%mm1 \n\t" \
  239. MOVQ "*4+"A3", %%mm2 \n\t" \
  240. UNPACK("%%mm1") \
  241. UNPACK("%%mm2") \
  242. "pmullw %%mm5, %%mm1 \n\t" /* *53 */ \
  243. "pmullw %%mm5, %%mm2 \n\t" /* *53 */ \
  244. "paddw %%mm1, %%mm3 \n\t" /* 4,53,18,-3 */ \
  245. "paddw %%mm2, %%mm4 \n\t" /* 4,53,18,-3 */
  246. /**
  247. * Macro to build the vertical 16bits version of vc1_put_shift[13].
  248. * Here, offset=src_stride. Parameters passed A1 to A4 must use
  249. * %3 (src_stride) and %4 (3*src_stride).
  250. *
  251. * @param NAME Either 1 or 3
  252. * @see MSPEL_FILTER13_CORE for information on A1->A4
  253. */
  254. #define MSPEL_FILTER13_VER_16B(NAME, A1, A2, A3, A4) \
  255. static void \
  256. vc1_put_ver_16b_ ## NAME ## _mmx(int16_t *dst, const uint8_t *src, \
  257. x86_reg src_stride, \
  258. int rnd, int64_t shift) \
  259. { \
  260. int h = 8; \
  261. src -= src_stride; \
  262. __asm__ volatile( \
  263. LOAD_ROUNDER_MMX("%5") \
  264. "movq "MANGLE(ff_pw_53)", %%mm5\n\t" \
  265. "movq "MANGLE(ff_pw_18)", %%mm6\n\t" \
  266. ".p2align 3 \n\t" \
  267. "1: \n\t" \
  268. MSPEL_FILTER13_CORE(DO_UNPACK, "movd 1", A1, A2, A3, A4) \
  269. NORMALIZE_MMX("%6") \
  270. TRANSFER_DONT_PACK(OP_PUT) \
  271. /* Last 3 (in fact 4) bytes on the line */ \
  272. "movd 8+"A1", %%mm1 \n\t" \
  273. DO_UNPACK("%%mm1") \
  274. "movq %%mm1, %%mm3 \n\t" \
  275. "paddw %%mm1, %%mm1 \n\t" \
  276. "paddw %%mm3, %%mm1 \n\t" /* 3* */ \
  277. "movd 8+"A2", %%mm3 \n\t" \
  278. DO_UNPACK("%%mm3") \
  279. "pmullw %%mm6, %%mm3 \n\t" /* *18 */ \
  280. "psubw %%mm1, %%mm3 \n\t" /*18,-3 */ \
  281. "movd 8+"A3", %%mm1 \n\t" \
  282. DO_UNPACK("%%mm1") \
  283. "pmullw %%mm5, %%mm1 \n\t" /* *53 */ \
  284. "paddw %%mm1, %%mm3 \n\t" /*53,18,-3 */ \
  285. "movd 8+"A4", %%mm1 \n\t" \
  286. DO_UNPACK("%%mm1") \
  287. "psllw $2, %%mm1 \n\t" /* 4* */ \
  288. "psubw %%mm1, %%mm3 \n\t" \
  289. "paddw %%mm7, %%mm3 \n\t" \
  290. "psraw %6, %%mm3 \n\t" \
  291. "movq %%mm3, 16(%2) \n\t" \
  292. "add %3, %1 \n\t" \
  293. "add $24, %2 \n\t" \
  294. "decl %0 \n\t" \
  295. "jnz 1b \n\t" \
  296. : "+r"(h), "+r" (src), "+r" (dst) \
  297. : "r"(src_stride), "r"(3*src_stride), \
  298. "m"(rnd), "m"(shift) \
  299. : "memory" \
  300. ); \
  301. }
  302. /**
  303. * Macro to build the horizontal 16bits version of vc1_put_shift[13].
  304. * Here, offset=16bits, so parameters passed A1 to A4 should be simple.
  305. *
  306. * @param NAME Either 1 or 3
  307. * @see MSPEL_FILTER13_CORE for information on A1->A4
  308. */
  309. #define MSPEL_FILTER13_HOR_16B(NAME, A1, A2, A3, A4, OP, OPNAME) \
  310. static void \
  311. OPNAME ## vc1_hor_16b_ ## NAME ## _mmx(uint8_t *dst, x86_reg stride, \
  312. const int16_t *src, int rnd) \
  313. { \
  314. int h = 8; \
  315. src -= 1; \
  316. rnd -= (-4+58+13-3)*256; /* Add -256 bias */ \
  317. __asm__ volatile( \
  318. LOAD_ROUNDER_MMX("%4") \
  319. "movq "MANGLE(ff_pw_18)", %%mm6 \n\t" \
  320. "movq "MANGLE(ff_pw_53)", %%mm5 \n\t" \
  321. ".p2align 3 \n\t" \
  322. "1: \n\t" \
  323. MSPEL_FILTER13_CORE(DONT_UNPACK, "movq 2", A1, A2, A3, A4) \
  324. NORMALIZE_MMX("$7") \
  325. /* Remove bias */ \
  326. "paddw "MANGLE(ff_pw_128)", %%mm3 \n\t" \
  327. "paddw "MANGLE(ff_pw_128)", %%mm4 \n\t" \
  328. TRANSFER_DO_PACK(OP) \
  329. "add $24, %1 \n\t" \
  330. "add %3, %2 \n\t" \
  331. "decl %0 \n\t" \
  332. "jnz 1b \n\t" \
  333. : "+r"(h), "+r" (src), "+r" (dst) \
  334. : "r"(stride), "m"(rnd) \
  335. : "memory" \
  336. ); \
  337. }
  338. /**
  339. * Macro to build the 8bits, any direction, version of vc1_put_shift[13].
  340. * Here, offset=src_stride. Parameters passed A1 to A4 must use
  341. * %3 (offset) and %4 (3*offset).
  342. *
  343. * @param NAME Either 1 or 3
  344. * @see MSPEL_FILTER13_CORE for information on A1->A4
  345. */
  346. #define MSPEL_FILTER13_8B(NAME, A1, A2, A3, A4, OP, OPNAME) \
  347. static void \
  348. OPNAME ## vc1_## NAME ## _mmx(uint8_t *dst, const uint8_t *src, \
  349. x86_reg stride, int rnd, x86_reg offset) \
  350. { \
  351. int h = 8; \
  352. src -= offset; \
  353. rnd = 32-rnd; \
  354. __asm__ volatile ( \
  355. LOAD_ROUNDER_MMX("%6") \
  356. "movq "MANGLE(ff_pw_53)", %%mm5 \n\t" \
  357. "movq "MANGLE(ff_pw_18)", %%mm6 \n\t" \
  358. ".p2align 3 \n\t" \
  359. "1: \n\t" \
  360. MSPEL_FILTER13_CORE(DO_UNPACK, "movd 1", A1, A2, A3, A4) \
  361. NORMALIZE_MMX("$6") \
  362. TRANSFER_DO_PACK(OP) \
  363. "add %5, %1 \n\t" \
  364. "add %5, %2 \n\t" \
  365. "decl %0 \n\t" \
  366. "jnz 1b \n\t" \
  367. : "+r"(h), "+r" (src), "+r" (dst) \
  368. : "r"(offset), "r"(3*offset), "g"(stride), "m"(rnd) \
  369. : "memory" \
  370. ); \
  371. }
  372. /** 1/4 shift bicubic interpolation */
  373. MSPEL_FILTER13_8B (shift1, "0(%1,%4 )", "0(%1,%3,2)", "0(%1,%3 )", "0(%1 )", OP_PUT, put_)
  374. MSPEL_FILTER13_8B (shift1, "0(%1,%4 )", "0(%1,%3,2)", "0(%1,%3 )", "0(%1 )", OP_AVG, avg_)
  375. MSPEL_FILTER13_VER_16B(shift1, "0(%1,%4 )", "0(%1,%3,2)", "0(%1,%3 )", "0(%1 )")
  376. MSPEL_FILTER13_HOR_16B(shift1, "2*3(%1)", "2*2(%1)", "2*1(%1)", "2*0(%1)", OP_PUT, put_)
  377. MSPEL_FILTER13_HOR_16B(shift1, "2*3(%1)", "2*2(%1)", "2*1(%1)", "2*0(%1)", OP_AVG, avg_)
  378. /** 3/4 shift bicubic interpolation */
  379. MSPEL_FILTER13_8B (shift3, "0(%1 )", "0(%1,%3 )", "0(%1,%3,2)", "0(%1,%4 )", OP_PUT, put_)
  380. MSPEL_FILTER13_8B (shift3, "0(%1 )", "0(%1,%3 )", "0(%1,%3,2)", "0(%1,%4 )", OP_AVG, avg_)
  381. MSPEL_FILTER13_VER_16B(shift3, "0(%1 )", "0(%1,%3 )", "0(%1,%3,2)", "0(%1,%4 )")
  382. MSPEL_FILTER13_HOR_16B(shift3, "2*0(%1)", "2*1(%1)", "2*2(%1)", "2*3(%1)", OP_PUT, put_)
  383. MSPEL_FILTER13_HOR_16B(shift3, "2*0(%1)", "2*1(%1)", "2*2(%1)", "2*3(%1)", OP_AVG, avg_)
  384. typedef void (*vc1_mspel_mc_filter_ver_16bits)(int16_t *dst, const uint8_t *src, x86_reg src_stride, int rnd, int64_t shift);
  385. typedef void (*vc1_mspel_mc_filter_hor_16bits)(uint8_t *dst, x86_reg dst_stride, const int16_t *src, int rnd);
  386. typedef void (*vc1_mspel_mc_filter_8bits)(uint8_t *dst, const uint8_t *src, x86_reg stride, int rnd, x86_reg offset);
  387. /**
  388. * Interpolate fractional pel values by applying proper vertical then
  389. * horizontal filter.
  390. *
  391. * @param dst Destination buffer for interpolated pels.
  392. * @param src Source buffer.
  393. * @param stride Stride for both src and dst buffers.
  394. * @param hmode Horizontal filter (expressed in quarter pixels shift).
  395. * @param hmode Vertical filter.
  396. * @param rnd Rounding bias.
  397. */
  398. #define VC1_MSPEL_MC(OP)\
  399. static void OP ## vc1_mspel_mc(uint8_t *dst, const uint8_t *src, int stride,\
  400. int hmode, int vmode, int rnd)\
  401. {\
  402. static const vc1_mspel_mc_filter_ver_16bits vc1_put_shift_ver_16bits[] =\
  403. { NULL, vc1_put_ver_16b_shift1_mmx, vc1_put_ver_16b_shift2_mmx, vc1_put_ver_16b_shift3_mmx };\
  404. static const vc1_mspel_mc_filter_hor_16bits vc1_put_shift_hor_16bits[] =\
  405. { NULL, OP ## vc1_hor_16b_shift1_mmx, OP ## vc1_hor_16b_shift2_mmx, OP ## vc1_hor_16b_shift3_mmx };\
  406. static const vc1_mspel_mc_filter_8bits vc1_put_shift_8bits[] =\
  407. { NULL, OP ## vc1_shift1_mmx, OP ## vc1_shift2_mmx, OP ## vc1_shift3_mmx };\
  408. \
  409. __asm__ volatile(\
  410. "pxor %%mm0, %%mm0 \n\t"\
  411. ::: "memory"\
  412. );\
  413. \
  414. if (vmode) { /* Vertical filter to apply */\
  415. if (hmode) { /* Horizontal filter to apply, output to tmp */\
  416. static const int shift_value[] = { 0, 5, 1, 5 };\
  417. int shift = (shift_value[hmode]+shift_value[vmode])>>1;\
  418. int r;\
  419. DECLARE_ALIGNED(16, int16_t, tmp)[12*8];\
  420. \
  421. r = (1<<(shift-1)) + rnd-1;\
  422. vc1_put_shift_ver_16bits[vmode](tmp, src-1, stride, r, shift);\
  423. \
  424. vc1_put_shift_hor_16bits[hmode](dst, stride, tmp+1, 64-rnd);\
  425. return;\
  426. }\
  427. else { /* No horizontal filter, output 8 lines to dst */\
  428. vc1_put_shift_8bits[vmode](dst, src, stride, 1-rnd, stride);\
  429. return;\
  430. }\
  431. }\
  432. \
  433. /* Horizontal mode with no vertical mode */\
  434. vc1_put_shift_8bits[hmode](dst, src, stride, rnd, 1);\
  435. }
  436. VC1_MSPEL_MC(put_)
  437. VC1_MSPEL_MC(avg_)
  438. /** Macro to ease bicubic filter interpolation functions declarations */
  439. #define DECLARE_FUNCTION(a, b) \
  440. static void put_vc1_mspel_mc ## a ## b ## _mmx(uint8_t *dst, const uint8_t *src, int stride, int rnd) { \
  441. put_vc1_mspel_mc(dst, src, stride, a, b, rnd); \
  442. }\
  443. static void avg_vc1_mspel_mc ## a ## b ## _mmx2(uint8_t *dst, const uint8_t *src, int stride, int rnd) { \
  444. avg_vc1_mspel_mc(dst, src, stride, a, b, rnd); \
  445. }
  446. DECLARE_FUNCTION(0, 1)
  447. DECLARE_FUNCTION(0, 2)
  448. DECLARE_FUNCTION(0, 3)
  449. DECLARE_FUNCTION(1, 0)
  450. DECLARE_FUNCTION(1, 1)
  451. DECLARE_FUNCTION(1, 2)
  452. DECLARE_FUNCTION(1, 3)
  453. DECLARE_FUNCTION(2, 0)
  454. DECLARE_FUNCTION(2, 1)
  455. DECLARE_FUNCTION(2, 2)
  456. DECLARE_FUNCTION(2, 3)
  457. DECLARE_FUNCTION(3, 0)
  458. DECLARE_FUNCTION(3, 1)
  459. DECLARE_FUNCTION(3, 2)
  460. DECLARE_FUNCTION(3, 3)
  461. static void vc1_inv_trans_4x4_dc_mmx2(uint8_t *dest, int linesize, DCTELEM *block)
  462. {
  463. int dc = block[0];
  464. dc = (17 * dc + 4) >> 3;
  465. dc = (17 * dc + 64) >> 7;
  466. __asm__ volatile(
  467. "movd %0, %%mm0 \n\t"
  468. "pshufw $0, %%mm0, %%mm0 \n\t"
  469. "pxor %%mm1, %%mm1 \n\t"
  470. "psubw %%mm0, %%mm1 \n\t"
  471. "packuswb %%mm0, %%mm0 \n\t"
  472. "packuswb %%mm1, %%mm1 \n\t"
  473. ::"r"(dc)
  474. );
  475. __asm__ volatile(
  476. "movd %0, %%mm2 \n\t"
  477. "movd %1, %%mm3 \n\t"
  478. "movd %2, %%mm4 \n\t"
  479. "movd %3, %%mm5 \n\t"
  480. "paddusb %%mm0, %%mm2 \n\t"
  481. "paddusb %%mm0, %%mm3 \n\t"
  482. "paddusb %%mm0, %%mm4 \n\t"
  483. "paddusb %%mm0, %%mm5 \n\t"
  484. "psubusb %%mm1, %%mm2 \n\t"
  485. "psubusb %%mm1, %%mm3 \n\t"
  486. "psubusb %%mm1, %%mm4 \n\t"
  487. "psubusb %%mm1, %%mm5 \n\t"
  488. "movd %%mm2, %0 \n\t"
  489. "movd %%mm3, %1 \n\t"
  490. "movd %%mm4, %2 \n\t"
  491. "movd %%mm5, %3 \n\t"
  492. :"+m"(*(uint32_t*)(dest+0*linesize)),
  493. "+m"(*(uint32_t*)(dest+1*linesize)),
  494. "+m"(*(uint32_t*)(dest+2*linesize)),
  495. "+m"(*(uint32_t*)(dest+3*linesize))
  496. );
  497. }
  498. static void vc1_inv_trans_4x8_dc_mmx2(uint8_t *dest, int linesize, DCTELEM *block)
  499. {
  500. int dc = block[0];
  501. dc = (17 * dc + 4) >> 3;
  502. dc = (12 * dc + 64) >> 7;
  503. __asm__ volatile(
  504. "movd %0, %%mm0 \n\t"
  505. "pshufw $0, %%mm0, %%mm0 \n\t"
  506. "pxor %%mm1, %%mm1 \n\t"
  507. "psubw %%mm0, %%mm1 \n\t"
  508. "packuswb %%mm0, %%mm0 \n\t"
  509. "packuswb %%mm1, %%mm1 \n\t"
  510. ::"r"(dc)
  511. );
  512. __asm__ volatile(
  513. "movd %0, %%mm2 \n\t"
  514. "movd %1, %%mm3 \n\t"
  515. "movd %2, %%mm4 \n\t"
  516. "movd %3, %%mm5 \n\t"
  517. "paddusb %%mm0, %%mm2 \n\t"
  518. "paddusb %%mm0, %%mm3 \n\t"
  519. "paddusb %%mm0, %%mm4 \n\t"
  520. "paddusb %%mm0, %%mm5 \n\t"
  521. "psubusb %%mm1, %%mm2 \n\t"
  522. "psubusb %%mm1, %%mm3 \n\t"
  523. "psubusb %%mm1, %%mm4 \n\t"
  524. "psubusb %%mm1, %%mm5 \n\t"
  525. "movd %%mm2, %0 \n\t"
  526. "movd %%mm3, %1 \n\t"
  527. "movd %%mm4, %2 \n\t"
  528. "movd %%mm5, %3 \n\t"
  529. :"+m"(*(uint32_t*)(dest+0*linesize)),
  530. "+m"(*(uint32_t*)(dest+1*linesize)),
  531. "+m"(*(uint32_t*)(dest+2*linesize)),
  532. "+m"(*(uint32_t*)(dest+3*linesize))
  533. );
  534. dest += 4*linesize;
  535. __asm__ volatile(
  536. "movd %0, %%mm2 \n\t"
  537. "movd %1, %%mm3 \n\t"
  538. "movd %2, %%mm4 \n\t"
  539. "movd %3, %%mm5 \n\t"
  540. "paddusb %%mm0, %%mm2 \n\t"
  541. "paddusb %%mm0, %%mm3 \n\t"
  542. "paddusb %%mm0, %%mm4 \n\t"
  543. "paddusb %%mm0, %%mm5 \n\t"
  544. "psubusb %%mm1, %%mm2 \n\t"
  545. "psubusb %%mm1, %%mm3 \n\t"
  546. "psubusb %%mm1, %%mm4 \n\t"
  547. "psubusb %%mm1, %%mm5 \n\t"
  548. "movd %%mm2, %0 \n\t"
  549. "movd %%mm3, %1 \n\t"
  550. "movd %%mm4, %2 \n\t"
  551. "movd %%mm5, %3 \n\t"
  552. :"+m"(*(uint32_t*)(dest+0*linesize)),
  553. "+m"(*(uint32_t*)(dest+1*linesize)),
  554. "+m"(*(uint32_t*)(dest+2*linesize)),
  555. "+m"(*(uint32_t*)(dest+3*linesize))
  556. );
  557. }
  558. static void vc1_inv_trans_8x4_dc_mmx2(uint8_t *dest, int linesize, DCTELEM *block)
  559. {
  560. int dc = block[0];
  561. dc = ( 3 * dc + 1) >> 1;
  562. dc = (17 * dc + 64) >> 7;
  563. __asm__ volatile(
  564. "movd %0, %%mm0 \n\t"
  565. "pshufw $0, %%mm0, %%mm0 \n\t"
  566. "pxor %%mm1, %%mm1 \n\t"
  567. "psubw %%mm0, %%mm1 \n\t"
  568. "packuswb %%mm0, %%mm0 \n\t"
  569. "packuswb %%mm1, %%mm1 \n\t"
  570. ::"r"(dc)
  571. );
  572. __asm__ volatile(
  573. "movq %0, %%mm2 \n\t"
  574. "movq %1, %%mm3 \n\t"
  575. "movq %2, %%mm4 \n\t"
  576. "movq %3, %%mm5 \n\t"
  577. "paddusb %%mm0, %%mm2 \n\t"
  578. "paddusb %%mm0, %%mm3 \n\t"
  579. "paddusb %%mm0, %%mm4 \n\t"
  580. "paddusb %%mm0, %%mm5 \n\t"
  581. "psubusb %%mm1, %%mm2 \n\t"
  582. "psubusb %%mm1, %%mm3 \n\t"
  583. "psubusb %%mm1, %%mm4 \n\t"
  584. "psubusb %%mm1, %%mm5 \n\t"
  585. "movq %%mm2, %0 \n\t"
  586. "movq %%mm3, %1 \n\t"
  587. "movq %%mm4, %2 \n\t"
  588. "movq %%mm5, %3 \n\t"
  589. :"+m"(*(uint32_t*)(dest+0*linesize)),
  590. "+m"(*(uint32_t*)(dest+1*linesize)),
  591. "+m"(*(uint32_t*)(dest+2*linesize)),
  592. "+m"(*(uint32_t*)(dest+3*linesize))
  593. );
  594. }
  595. static void vc1_inv_trans_8x8_dc_mmx2(uint8_t *dest, int linesize, DCTELEM *block)
  596. {
  597. int dc = block[0];
  598. dc = (3 * dc + 1) >> 1;
  599. dc = (3 * dc + 16) >> 5;
  600. __asm__ volatile(
  601. "movd %0, %%mm0 \n\t"
  602. "pshufw $0, %%mm0, %%mm0 \n\t"
  603. "pxor %%mm1, %%mm1 \n\t"
  604. "psubw %%mm0, %%mm1 \n\t"
  605. "packuswb %%mm0, %%mm0 \n\t"
  606. "packuswb %%mm1, %%mm1 \n\t"
  607. ::"r"(dc)
  608. );
  609. __asm__ volatile(
  610. "movq %0, %%mm2 \n\t"
  611. "movq %1, %%mm3 \n\t"
  612. "movq %2, %%mm4 \n\t"
  613. "movq %3, %%mm5 \n\t"
  614. "paddusb %%mm0, %%mm2 \n\t"
  615. "paddusb %%mm0, %%mm3 \n\t"
  616. "paddusb %%mm0, %%mm4 \n\t"
  617. "paddusb %%mm0, %%mm5 \n\t"
  618. "psubusb %%mm1, %%mm2 \n\t"
  619. "psubusb %%mm1, %%mm3 \n\t"
  620. "psubusb %%mm1, %%mm4 \n\t"
  621. "psubusb %%mm1, %%mm5 \n\t"
  622. "movq %%mm2, %0 \n\t"
  623. "movq %%mm3, %1 \n\t"
  624. "movq %%mm4, %2 \n\t"
  625. "movq %%mm5, %3 \n\t"
  626. :"+m"(*(uint32_t*)(dest+0*linesize)),
  627. "+m"(*(uint32_t*)(dest+1*linesize)),
  628. "+m"(*(uint32_t*)(dest+2*linesize)),
  629. "+m"(*(uint32_t*)(dest+3*linesize))
  630. );
  631. dest += 4*linesize;
  632. __asm__ volatile(
  633. "movq %0, %%mm2 \n\t"
  634. "movq %1, %%mm3 \n\t"
  635. "movq %2, %%mm4 \n\t"
  636. "movq %3, %%mm5 \n\t"
  637. "paddusb %%mm0, %%mm2 \n\t"
  638. "paddusb %%mm0, %%mm3 \n\t"
  639. "paddusb %%mm0, %%mm4 \n\t"
  640. "paddusb %%mm0, %%mm5 \n\t"
  641. "psubusb %%mm1, %%mm2 \n\t"
  642. "psubusb %%mm1, %%mm3 \n\t"
  643. "psubusb %%mm1, %%mm4 \n\t"
  644. "psubusb %%mm1, %%mm5 \n\t"
  645. "movq %%mm2, %0 \n\t"
  646. "movq %%mm3, %1 \n\t"
  647. "movq %%mm4, %2 \n\t"
  648. "movq %%mm5, %3 \n\t"
  649. :"+m"(*(uint32_t*)(dest+0*linesize)),
  650. "+m"(*(uint32_t*)(dest+1*linesize)),
  651. "+m"(*(uint32_t*)(dest+2*linesize)),
  652. "+m"(*(uint32_t*)(dest+3*linesize))
  653. );
  654. }
  655. #endif /* HAVE_INLINE_ASM */
  656. #define LOOP_FILTER(EXT) \
  657. void ff_vc1_v_loop_filter4_ ## EXT(uint8_t *src, int stride, int pq); \
  658. void ff_vc1_h_loop_filter4_ ## EXT(uint8_t *src, int stride, int pq); \
  659. void ff_vc1_v_loop_filter8_ ## EXT(uint8_t *src, int stride, int pq); \
  660. void ff_vc1_h_loop_filter8_ ## EXT(uint8_t *src, int stride, int pq); \
  661. \
  662. static void vc1_v_loop_filter16_ ## EXT(uint8_t *src, int stride, int pq) \
  663. { \
  664. ff_vc1_v_loop_filter8_ ## EXT(src, stride, pq); \
  665. ff_vc1_v_loop_filter8_ ## EXT(src+8, stride, pq); \
  666. } \
  667. \
  668. static void vc1_h_loop_filter16_ ## EXT(uint8_t *src, int stride, int pq) \
  669. { \
  670. ff_vc1_h_loop_filter8_ ## EXT(src, stride, pq); \
  671. ff_vc1_h_loop_filter8_ ## EXT(src+8*stride, stride, pq); \
  672. }
  673. #if HAVE_YASM
  674. LOOP_FILTER(mmx2)
  675. LOOP_FILTER(sse2)
  676. LOOP_FILTER(ssse3)
  677. void ff_vc1_h_loop_filter8_sse4(uint8_t *src, int stride, int pq);
  678. static void vc1_h_loop_filter16_sse4(uint8_t *src, int stride, int pq)
  679. {
  680. ff_vc1_h_loop_filter8_sse4(src, stride, pq);
  681. ff_vc1_h_loop_filter8_sse4(src+8*stride, stride, pq);
  682. }
  683. #endif /* HAVE_YASM */
  684. void ff_put_vc1_chroma_mc8_mmx_nornd (uint8_t *dst, uint8_t *src,
  685. int stride, int h, int x, int y);
  686. void ff_avg_vc1_chroma_mc8_mmx2_nornd (uint8_t *dst, uint8_t *src,
  687. int stride, int h, int x, int y);
  688. void ff_avg_vc1_chroma_mc8_3dnow_nornd(uint8_t *dst, uint8_t *src,
  689. int stride, int h, int x, int y);
  690. void ff_put_vc1_chroma_mc8_ssse3_nornd(uint8_t *dst, uint8_t *src,
  691. int stride, int h, int x, int y);
  692. void ff_avg_vc1_chroma_mc8_ssse3_nornd(uint8_t *dst, uint8_t *src,
  693. int stride, int h, int x, int y);
  694. void ff_vc1dsp_init_mmx(VC1DSPContext *dsp)
  695. {
  696. int mm_flags = av_get_cpu_flags();
  697. #if HAVE_INLINE_ASM
  698. if (mm_flags & AV_CPU_FLAG_MMX) {
  699. dsp->put_vc1_mspel_pixels_tab[ 0] = ff_put_vc1_mspel_mc00_mmx;
  700. dsp->put_vc1_mspel_pixels_tab[ 4] = put_vc1_mspel_mc01_mmx;
  701. dsp->put_vc1_mspel_pixels_tab[ 8] = put_vc1_mspel_mc02_mmx;
  702. dsp->put_vc1_mspel_pixels_tab[12] = put_vc1_mspel_mc03_mmx;
  703. dsp->put_vc1_mspel_pixels_tab[ 1] = put_vc1_mspel_mc10_mmx;
  704. dsp->put_vc1_mspel_pixels_tab[ 5] = put_vc1_mspel_mc11_mmx;
  705. dsp->put_vc1_mspel_pixels_tab[ 9] = put_vc1_mspel_mc12_mmx;
  706. dsp->put_vc1_mspel_pixels_tab[13] = put_vc1_mspel_mc13_mmx;
  707. dsp->put_vc1_mspel_pixels_tab[ 2] = put_vc1_mspel_mc20_mmx;
  708. dsp->put_vc1_mspel_pixels_tab[ 6] = put_vc1_mspel_mc21_mmx;
  709. dsp->put_vc1_mspel_pixels_tab[10] = put_vc1_mspel_mc22_mmx;
  710. dsp->put_vc1_mspel_pixels_tab[14] = put_vc1_mspel_mc23_mmx;
  711. dsp->put_vc1_mspel_pixels_tab[ 3] = put_vc1_mspel_mc30_mmx;
  712. dsp->put_vc1_mspel_pixels_tab[ 7] = put_vc1_mspel_mc31_mmx;
  713. dsp->put_vc1_mspel_pixels_tab[11] = put_vc1_mspel_mc32_mmx;
  714. dsp->put_vc1_mspel_pixels_tab[15] = put_vc1_mspel_mc33_mmx;
  715. }
  716. if (mm_flags & AV_CPU_FLAG_MMXEXT) {
  717. dsp->avg_vc1_mspel_pixels_tab[ 0] = ff_avg_vc1_mspel_mc00_mmx2;
  718. dsp->avg_vc1_mspel_pixels_tab[ 4] = avg_vc1_mspel_mc01_mmx2;
  719. dsp->avg_vc1_mspel_pixels_tab[ 8] = avg_vc1_mspel_mc02_mmx2;
  720. dsp->avg_vc1_mspel_pixels_tab[12] = avg_vc1_mspel_mc03_mmx2;
  721. dsp->avg_vc1_mspel_pixels_tab[ 1] = avg_vc1_mspel_mc10_mmx2;
  722. dsp->avg_vc1_mspel_pixels_tab[ 5] = avg_vc1_mspel_mc11_mmx2;
  723. dsp->avg_vc1_mspel_pixels_tab[ 9] = avg_vc1_mspel_mc12_mmx2;
  724. dsp->avg_vc1_mspel_pixels_tab[13] = avg_vc1_mspel_mc13_mmx2;
  725. dsp->avg_vc1_mspel_pixels_tab[ 2] = avg_vc1_mspel_mc20_mmx2;
  726. dsp->avg_vc1_mspel_pixels_tab[ 6] = avg_vc1_mspel_mc21_mmx2;
  727. dsp->avg_vc1_mspel_pixels_tab[10] = avg_vc1_mspel_mc22_mmx2;
  728. dsp->avg_vc1_mspel_pixels_tab[14] = avg_vc1_mspel_mc23_mmx2;
  729. dsp->avg_vc1_mspel_pixels_tab[ 3] = avg_vc1_mspel_mc30_mmx2;
  730. dsp->avg_vc1_mspel_pixels_tab[ 7] = avg_vc1_mspel_mc31_mmx2;
  731. dsp->avg_vc1_mspel_pixels_tab[11] = avg_vc1_mspel_mc32_mmx2;
  732. dsp->avg_vc1_mspel_pixels_tab[15] = avg_vc1_mspel_mc33_mmx2;
  733. dsp->vc1_inv_trans_8x8_dc = vc1_inv_trans_8x8_dc_mmx2;
  734. dsp->vc1_inv_trans_4x8_dc = vc1_inv_trans_4x8_dc_mmx2;
  735. dsp->vc1_inv_trans_8x4_dc = vc1_inv_trans_8x4_dc_mmx2;
  736. dsp->vc1_inv_trans_4x4_dc = vc1_inv_trans_4x4_dc_mmx2;
  737. }
  738. #endif /* HAVE_INLINE_ASM */
  739. #define ASSIGN_LF(EXT) \
  740. dsp->vc1_v_loop_filter4 = ff_vc1_v_loop_filter4_ ## EXT; \
  741. dsp->vc1_h_loop_filter4 = ff_vc1_h_loop_filter4_ ## EXT; \
  742. dsp->vc1_v_loop_filter8 = ff_vc1_v_loop_filter8_ ## EXT; \
  743. dsp->vc1_h_loop_filter8 = ff_vc1_h_loop_filter8_ ## EXT; \
  744. dsp->vc1_v_loop_filter16 = vc1_v_loop_filter16_ ## EXT; \
  745. dsp->vc1_h_loop_filter16 = vc1_h_loop_filter16_ ## EXT
  746. #if HAVE_YASM
  747. if (mm_flags & AV_CPU_FLAG_MMX) {
  748. dsp->put_no_rnd_vc1_chroma_pixels_tab[0]= ff_put_vc1_chroma_mc8_mmx_nornd;
  749. }
  750. if (mm_flags & AV_CPU_FLAG_MMXEXT) {
  751. ASSIGN_LF(mmx2);
  752. dsp->avg_no_rnd_vc1_chroma_pixels_tab[0]= ff_avg_vc1_chroma_mc8_mmx2_nornd;
  753. } else if (mm_flags & AV_CPU_FLAG_3DNOW) {
  754. dsp->avg_no_rnd_vc1_chroma_pixels_tab[0]= ff_avg_vc1_chroma_mc8_3dnow_nornd;
  755. }
  756. if (mm_flags & AV_CPU_FLAG_SSE2) {
  757. dsp->vc1_v_loop_filter8 = ff_vc1_v_loop_filter8_sse2;
  758. dsp->vc1_h_loop_filter8 = ff_vc1_h_loop_filter8_sse2;
  759. dsp->vc1_v_loop_filter16 = vc1_v_loop_filter16_sse2;
  760. dsp->vc1_h_loop_filter16 = vc1_h_loop_filter16_sse2;
  761. }
  762. if (mm_flags & AV_CPU_FLAG_SSSE3) {
  763. ASSIGN_LF(ssse3);
  764. dsp->put_no_rnd_vc1_chroma_pixels_tab[0]= ff_put_vc1_chroma_mc8_ssse3_nornd;
  765. dsp->avg_no_rnd_vc1_chroma_pixels_tab[0]= ff_avg_vc1_chroma_mc8_ssse3_nornd;
  766. }
  767. if (mm_flags & AV_CPU_FLAG_SSE4) {
  768. dsp->vc1_h_loop_filter8 = ff_vc1_h_loop_filter8_sse4;
  769. dsp->vc1_h_loop_filter16 = vc1_h_loop_filter16_sse4;
  770. }
  771. #endif /* HAVE_YASM */
  772. }