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.

408 lines
15KB

  1. /*
  2. * XVID MPEG-4 VIDEO CODEC
  3. * - SSE2 inverse discrete cosine transform -
  4. *
  5. * Copyright(C) 2003 Pascal Massimino <skal@planet-d.net>
  6. *
  7. * Conversion to gcc syntax with modifications
  8. * by Alexander Strange <astrange@ithinksw.com>
  9. *
  10. * Originally from dct/x86_asm/fdct_sse2_skal.asm in Xvid.
  11. *
  12. * This file is part of FFmpeg.
  13. *
  14. * Vertical pass is an implementation of the scheme:
  15. * Loeffler C., Ligtenberg A., and Moschytz C.S.:
  16. * Practical Fast 1D DCT Algorithm with Eleven Multiplications,
  17. * Proc. ICASSP 1989, 988-991.
  18. *
  19. * Horizontal pass is a double 4x4 vector/matrix multiplication,
  20. * (see also Intel's Application Note 922:
  21. * http://developer.intel.com/vtune/cbts/strmsimd/922down.htm
  22. * Copyright (C) 1999 Intel Corporation)
  23. *
  24. * More details at http://skal.planet-d.net/coding/dct.html
  25. *
  26. * FFmpeg is free software; you can redistribute it and/or
  27. * modify it under the terms of the GNU Lesser General Public
  28. * License as published by the Free Software Foundation; either
  29. * version 2.1 of the License, or (at your option) any later version.
  30. *
  31. * FFmpeg is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. * Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public License
  37. * along with FFmpeg; if not, write to the Free Software Foundation,
  38. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  39. */
  40. #include "libavcodec/dsputil.h"
  41. #include "libavutil/x86_cpu.h"
  42. #include "idct_xvid.h"
  43. #include "dsputil_mmx.h"
  44. #if HAVE_INLINE_ASM
  45. /**
  46. * @file
  47. * @brief SSE2 idct compatible with xvidmmx
  48. */
  49. #define X8(x) x,x,x,x,x,x,x,x
  50. #define ROW_SHIFT 11
  51. #define COL_SHIFT 6
  52. DECLARE_ASM_CONST(16, int16_t, tan1)[] = {X8(13036)}; // tan( pi/16)
  53. DECLARE_ASM_CONST(16, int16_t, tan2)[] = {X8(27146)}; // tan(2pi/16) = sqrt(2)-1
  54. DECLARE_ASM_CONST(16, int16_t, tan3)[] = {X8(43790)}; // tan(3pi/16)-1
  55. DECLARE_ASM_CONST(16, int16_t, sqrt2)[]= {X8(23170)}; // 0.5/sqrt(2)
  56. DECLARE_ASM_CONST(8, uint8_t, m127)[] = {X8(127)};
  57. DECLARE_ASM_CONST(16, int16_t, iTab1)[] = {
  58. 0x4000, 0x539f, 0xc000, 0xac61, 0x4000, 0xdd5d, 0x4000, 0xdd5d,
  59. 0x4000, 0x22a3, 0x4000, 0x22a3, 0xc000, 0x539f, 0x4000, 0xac61,
  60. 0x3249, 0x11a8, 0x4b42, 0xee58, 0x11a8, 0x4b42, 0x11a8, 0xcdb7,
  61. 0x58c5, 0x4b42, 0xa73b, 0xcdb7, 0x3249, 0xa73b, 0x4b42, 0xa73b
  62. };
  63. DECLARE_ASM_CONST(16, int16_t, iTab2)[] = {
  64. 0x58c5, 0x73fc, 0xa73b, 0x8c04, 0x58c5, 0xcff5, 0x58c5, 0xcff5,
  65. 0x58c5, 0x300b, 0x58c5, 0x300b, 0xa73b, 0x73fc, 0x58c5, 0x8c04,
  66. 0x45bf, 0x187e, 0x6862, 0xe782, 0x187e, 0x6862, 0x187e, 0xba41,
  67. 0x7b21, 0x6862, 0x84df, 0xba41, 0x45bf, 0x84df, 0x6862, 0x84df
  68. };
  69. DECLARE_ASM_CONST(16, int16_t, iTab3)[] = {
  70. 0x539f, 0x6d41, 0xac61, 0x92bf, 0x539f, 0xd2bf, 0x539f, 0xd2bf,
  71. 0x539f, 0x2d41, 0x539f, 0x2d41, 0xac61, 0x6d41, 0x539f, 0x92bf,
  72. 0x41b3, 0x1712, 0x6254, 0xe8ee, 0x1712, 0x6254, 0x1712, 0xbe4d,
  73. 0x73fc, 0x6254, 0x8c04, 0xbe4d, 0x41b3, 0x8c04, 0x6254, 0x8c04
  74. };
  75. DECLARE_ASM_CONST(16, int16_t, iTab4)[] = {
  76. 0x4b42, 0x6254, 0xb4be, 0x9dac, 0x4b42, 0xd746, 0x4b42, 0xd746,
  77. 0x4b42, 0x28ba, 0x4b42, 0x28ba, 0xb4be, 0x6254, 0x4b42, 0x9dac,
  78. 0x3b21, 0x14c3, 0x587e, 0xeb3d, 0x14c3, 0x587e, 0x14c3, 0xc4df,
  79. 0x6862, 0x587e, 0x979e, 0xc4df, 0x3b21, 0x979e, 0x587e, 0x979e
  80. };
  81. DECLARE_ASM_CONST(16, int32_t, walkenIdctRounders)[] = {
  82. 65536, 65536, 65536, 65536,
  83. 3597, 3597, 3597, 3597,
  84. 2260, 2260, 2260, 2260,
  85. 1203, 1203, 1203, 1203,
  86. 120, 120, 120, 120,
  87. 512, 512, 512, 512
  88. };
  89. // Temporary storage before the column pass
  90. #define ROW1 "%%xmm6"
  91. #define ROW3 "%%xmm4"
  92. #define ROW5 "%%xmm5"
  93. #define ROW7 "%%xmm7"
  94. #define CLEAR_ODD(r) "pxor "r","r" \n\t"
  95. #define PUT_ODD(dst) "pshufhw $0x1B, %%xmm2, "dst" \n\t"
  96. #if ARCH_X86_64
  97. # define ROW0 "%%xmm8"
  98. # define REG0 ROW0
  99. # define ROW2 "%%xmm9"
  100. # define REG2 ROW2
  101. # define ROW4 "%%xmm10"
  102. # define REG4 ROW4
  103. # define ROW6 "%%xmm11"
  104. # define REG6 ROW6
  105. # define CLEAR_EVEN(r) CLEAR_ODD(r)
  106. # define PUT_EVEN(dst) PUT_ODD(dst)
  107. # define XMMS "%%xmm12"
  108. # define MOV_32_ONLY "#"
  109. # define SREG2 REG2
  110. # define TAN3 "%%xmm13"
  111. # define TAN1 "%%xmm14"
  112. #else
  113. # define ROW0 "(%0)"
  114. # define REG0 "%%xmm4"
  115. # define ROW2 "2*16(%0)"
  116. # define REG2 "%%xmm4"
  117. # define ROW4 "4*16(%0)"
  118. # define REG4 "%%xmm6"
  119. # define ROW6 "6*16(%0)"
  120. # define REG6 "%%xmm6"
  121. # define CLEAR_EVEN(r)
  122. # define PUT_EVEN(dst) \
  123. "pshufhw $0x1B, %%xmm2, %%xmm2 \n\t" \
  124. "movdqa %%xmm2, "dst" \n\t"
  125. # define XMMS "%%xmm2"
  126. # define MOV_32_ONLY "movdqa "
  127. # define SREG2 "%%xmm7"
  128. # define TAN3 "%%xmm0"
  129. # define TAN1 "%%xmm2"
  130. #endif
  131. #define ROUND(x) "paddd "MANGLE(x)
  132. #define JZ(reg, to) \
  133. "testl "reg","reg" \n\t" \
  134. "jz "to" \n\t"
  135. #define JNZ(reg, to) \
  136. "testl "reg","reg" \n\t" \
  137. "jnz "to" \n\t"
  138. #define TEST_ONE_ROW(src, reg, clear) \
  139. clear \
  140. "movq "src", %%mm1 \n\t" \
  141. "por 8+"src", %%mm1 \n\t" \
  142. "paddusb %%mm0, %%mm1 \n\t" \
  143. "pmovmskb %%mm1, "reg" \n\t"
  144. #define TEST_TWO_ROWS(row1, row2, reg1, reg2, clear1, clear2) \
  145. clear1 \
  146. clear2 \
  147. "movq "row1", %%mm1 \n\t" \
  148. "por 8+"row1", %%mm1 \n\t" \
  149. "movq "row2", %%mm2 \n\t" \
  150. "por 8+"row2", %%mm2 \n\t" \
  151. "paddusb %%mm0, %%mm1 \n\t" \
  152. "paddusb %%mm0, %%mm2 \n\t" \
  153. "pmovmskb %%mm1, "reg1" \n\t" \
  154. "pmovmskb %%mm2, "reg2" \n\t"
  155. ///IDCT pass on rows.
  156. #define iMTX_MULT(src, table, rounder, put) \
  157. "movdqa "src", %%xmm3 \n\t" \
  158. "movdqa %%xmm3, %%xmm0 \n\t" \
  159. "pshufd $0x11, %%xmm3, %%xmm1 \n\t" /* 4602 */ \
  160. "punpcklqdq %%xmm0, %%xmm0 \n\t" /* 0246 */ \
  161. "pmaddwd "table", %%xmm0 \n\t" \
  162. "pmaddwd 16+"table", %%xmm1 \n\t" \
  163. "pshufd $0xBB, %%xmm3, %%xmm2 \n\t" /* 5713 */ \
  164. "punpckhqdq %%xmm3, %%xmm3 \n\t" /* 1357 */ \
  165. "pmaddwd 32+"table", %%xmm2 \n\t" \
  166. "pmaddwd 48+"table", %%xmm3 \n\t" \
  167. "paddd %%xmm1, %%xmm0 \n\t" \
  168. "paddd %%xmm3, %%xmm2 \n\t" \
  169. rounder", %%xmm0 \n\t" \
  170. "movdqa %%xmm2, %%xmm3 \n\t" \
  171. "paddd %%xmm0, %%xmm2 \n\t" \
  172. "psubd %%xmm3, %%xmm0 \n\t" \
  173. "psrad $11, %%xmm2 \n\t" \
  174. "psrad $11, %%xmm0 \n\t" \
  175. "packssdw %%xmm0, %%xmm2 \n\t" \
  176. put \
  177. "1: \n\t"
  178. #define iLLM_HEAD \
  179. "movdqa "MANGLE(tan3)", "TAN3" \n\t" \
  180. "movdqa "MANGLE(tan1)", "TAN1" \n\t" \
  181. ///IDCT pass on columns.
  182. #define iLLM_PASS(dct) \
  183. "movdqa "TAN3", %%xmm1 \n\t" \
  184. "movdqa "TAN1", %%xmm3 \n\t" \
  185. "pmulhw %%xmm4, "TAN3" \n\t" \
  186. "pmulhw %%xmm5, %%xmm1 \n\t" \
  187. "paddsw %%xmm4, "TAN3" \n\t" \
  188. "paddsw %%xmm5, %%xmm1 \n\t" \
  189. "psubsw %%xmm5, "TAN3" \n\t" \
  190. "paddsw %%xmm4, %%xmm1 \n\t" \
  191. "pmulhw %%xmm7, %%xmm3 \n\t" \
  192. "pmulhw %%xmm6, "TAN1" \n\t" \
  193. "paddsw %%xmm6, %%xmm3 \n\t" \
  194. "psubsw %%xmm7, "TAN1" \n\t" \
  195. "movdqa %%xmm3, %%xmm7 \n\t" \
  196. "movdqa "TAN1", %%xmm6 \n\t" \
  197. "psubsw %%xmm1, %%xmm3 \n\t" \
  198. "psubsw "TAN3", "TAN1" \n\t" \
  199. "paddsw %%xmm7, %%xmm1 \n\t" \
  200. "paddsw %%xmm6, "TAN3" \n\t" \
  201. "movdqa %%xmm3, %%xmm6 \n\t" \
  202. "psubsw "TAN3", %%xmm3 \n\t" \
  203. "paddsw %%xmm6, "TAN3" \n\t" \
  204. "movdqa "MANGLE(sqrt2)", %%xmm4 \n\t" \
  205. "pmulhw %%xmm4, %%xmm3 \n\t" \
  206. "pmulhw %%xmm4, "TAN3" \n\t" \
  207. "paddsw "TAN3", "TAN3" \n\t" \
  208. "paddsw %%xmm3, %%xmm3 \n\t" \
  209. "movdqa "MANGLE(tan2)", %%xmm7 \n\t" \
  210. MOV_32_ONLY ROW2", "REG2" \n\t" \
  211. MOV_32_ONLY ROW6", "REG6" \n\t" \
  212. "movdqa %%xmm7, %%xmm5 \n\t" \
  213. "pmulhw "REG6", %%xmm7 \n\t" \
  214. "pmulhw "REG2", %%xmm5 \n\t" \
  215. "paddsw "REG2", %%xmm7 \n\t" \
  216. "psubsw "REG6", %%xmm5 \n\t" \
  217. MOV_32_ONLY ROW0", "REG0" \n\t" \
  218. MOV_32_ONLY ROW4", "REG4" \n\t" \
  219. MOV_32_ONLY" "TAN1", (%0) \n\t" \
  220. "movdqa "REG0", "XMMS" \n\t" \
  221. "psubsw "REG4", "REG0" \n\t" \
  222. "paddsw "XMMS", "REG4" \n\t" \
  223. "movdqa "REG4", "XMMS" \n\t" \
  224. "psubsw %%xmm7, "REG4" \n\t" \
  225. "paddsw "XMMS", %%xmm7 \n\t" \
  226. "movdqa "REG0", "XMMS" \n\t" \
  227. "psubsw %%xmm5, "REG0" \n\t" \
  228. "paddsw "XMMS", %%xmm5 \n\t" \
  229. "movdqa %%xmm5, "XMMS" \n\t" \
  230. "psubsw "TAN3", %%xmm5 \n\t" \
  231. "paddsw "XMMS", "TAN3" \n\t" \
  232. "movdqa "REG0", "XMMS" \n\t" \
  233. "psubsw %%xmm3, "REG0" \n\t" \
  234. "paddsw "XMMS", %%xmm3 \n\t" \
  235. MOV_32_ONLY" (%0), "TAN1" \n\t" \
  236. "psraw $6, %%xmm5 \n\t" \
  237. "psraw $6, "REG0" \n\t" \
  238. "psraw $6, "TAN3" \n\t" \
  239. "psraw $6, %%xmm3 \n\t" \
  240. "movdqa "TAN3", 1*16("dct") \n\t" \
  241. "movdqa %%xmm3, 2*16("dct") \n\t" \
  242. "movdqa "REG0", 5*16("dct") \n\t" \
  243. "movdqa %%xmm5, 6*16("dct") \n\t" \
  244. "movdqa %%xmm7, %%xmm0 \n\t" \
  245. "movdqa "REG4", %%xmm4 \n\t" \
  246. "psubsw %%xmm1, %%xmm7 \n\t" \
  247. "psubsw "TAN1", "REG4" \n\t" \
  248. "paddsw %%xmm0, %%xmm1 \n\t" \
  249. "paddsw %%xmm4, "TAN1" \n\t" \
  250. "psraw $6, %%xmm1 \n\t" \
  251. "psraw $6, %%xmm7 \n\t" \
  252. "psraw $6, "TAN1" \n\t" \
  253. "psraw $6, "REG4" \n\t" \
  254. "movdqa %%xmm1, ("dct") \n\t" \
  255. "movdqa "TAN1", 3*16("dct") \n\t" \
  256. "movdqa "REG4", 4*16("dct") \n\t" \
  257. "movdqa %%xmm7, 7*16("dct") \n\t"
  258. ///IDCT pass on columns, assuming rows 4-7 are zero.
  259. #define iLLM_PASS_SPARSE(dct) \
  260. "pmulhw %%xmm4, "TAN3" \n\t" \
  261. "paddsw %%xmm4, "TAN3" \n\t" \
  262. "movdqa %%xmm6, %%xmm3 \n\t" \
  263. "pmulhw %%xmm6, "TAN1" \n\t" \
  264. "movdqa %%xmm4, %%xmm1 \n\t" \
  265. "psubsw %%xmm1, %%xmm3 \n\t" \
  266. "paddsw %%xmm6, %%xmm1 \n\t" \
  267. "movdqa "TAN1", %%xmm6 \n\t" \
  268. "psubsw "TAN3", "TAN1" \n\t" \
  269. "paddsw %%xmm6, "TAN3" \n\t" \
  270. "movdqa %%xmm3, %%xmm6 \n\t" \
  271. "psubsw "TAN3", %%xmm3 \n\t" \
  272. "paddsw %%xmm6, "TAN3" \n\t" \
  273. "movdqa "MANGLE(sqrt2)", %%xmm4 \n\t" \
  274. "pmulhw %%xmm4, %%xmm3 \n\t" \
  275. "pmulhw %%xmm4, "TAN3" \n\t" \
  276. "paddsw "TAN3", "TAN3" \n\t" \
  277. "paddsw %%xmm3, %%xmm3 \n\t" \
  278. "movdqa "MANGLE(tan2)", %%xmm5 \n\t" \
  279. MOV_32_ONLY ROW2", "SREG2" \n\t" \
  280. "pmulhw "SREG2", %%xmm5 \n\t" \
  281. MOV_32_ONLY ROW0", "REG0" \n\t" \
  282. "movdqa "REG0", %%xmm6 \n\t" \
  283. "psubsw "SREG2", %%xmm6 \n\t" \
  284. "paddsw "REG0", "SREG2" \n\t" \
  285. MOV_32_ONLY" "TAN1", (%0) \n\t" \
  286. "movdqa "REG0", "XMMS" \n\t" \
  287. "psubsw %%xmm5, "REG0" \n\t" \
  288. "paddsw "XMMS", %%xmm5 \n\t" \
  289. "movdqa %%xmm5, "XMMS" \n\t" \
  290. "psubsw "TAN3", %%xmm5 \n\t" \
  291. "paddsw "XMMS", "TAN3" \n\t" \
  292. "movdqa "REG0", "XMMS" \n\t" \
  293. "psubsw %%xmm3, "REG0" \n\t" \
  294. "paddsw "XMMS", %%xmm3 \n\t" \
  295. MOV_32_ONLY" (%0), "TAN1" \n\t" \
  296. "psraw $6, %%xmm5 \n\t" \
  297. "psraw $6, "REG0" \n\t" \
  298. "psraw $6, "TAN3" \n\t" \
  299. "psraw $6, %%xmm3 \n\t" \
  300. "movdqa "TAN3", 1*16("dct") \n\t" \
  301. "movdqa %%xmm3, 2*16("dct") \n\t" \
  302. "movdqa "REG0", 5*16("dct") \n\t" \
  303. "movdqa %%xmm5, 6*16("dct") \n\t" \
  304. "movdqa "SREG2", %%xmm0 \n\t" \
  305. "movdqa %%xmm6, %%xmm4 \n\t" \
  306. "psubsw %%xmm1, "SREG2" \n\t" \
  307. "psubsw "TAN1", %%xmm6 \n\t" \
  308. "paddsw %%xmm0, %%xmm1 \n\t" \
  309. "paddsw %%xmm4, "TAN1" \n\t" \
  310. "psraw $6, %%xmm1 \n\t" \
  311. "psraw $6, "SREG2" \n\t" \
  312. "psraw $6, "TAN1" \n\t" \
  313. "psraw $6, %%xmm6 \n\t" \
  314. "movdqa %%xmm1, ("dct") \n\t" \
  315. "movdqa "TAN1", 3*16("dct") \n\t" \
  316. "movdqa %%xmm6, 4*16("dct") \n\t" \
  317. "movdqa "SREG2", 7*16("dct") \n\t"
  318. inline void ff_idct_xvid_sse2(short *block)
  319. {
  320. __asm__ volatile(
  321. "movq "MANGLE(m127)", %%mm0 \n\t"
  322. iMTX_MULT("(%0)", MANGLE(iTab1), ROUND(walkenIdctRounders), PUT_EVEN(ROW0))
  323. iMTX_MULT("1*16(%0)", MANGLE(iTab2), ROUND(walkenIdctRounders+1*16), PUT_ODD(ROW1))
  324. iMTX_MULT("2*16(%0)", MANGLE(iTab3), ROUND(walkenIdctRounders+2*16), PUT_EVEN(ROW2))
  325. TEST_TWO_ROWS("3*16(%0)", "4*16(%0)", "%%eax", "%%ecx", CLEAR_ODD(ROW3), CLEAR_EVEN(ROW4))
  326. JZ("%%eax", "1f")
  327. iMTX_MULT("3*16(%0)", MANGLE(iTab4), ROUND(walkenIdctRounders+3*16), PUT_ODD(ROW3))
  328. TEST_TWO_ROWS("5*16(%0)", "6*16(%0)", "%%eax", "%%edx", CLEAR_ODD(ROW5), CLEAR_EVEN(ROW6))
  329. TEST_ONE_ROW("7*16(%0)", "%%esi", CLEAR_ODD(ROW7))
  330. iLLM_HEAD
  331. ".p2align 4 \n\t"
  332. JNZ("%%ecx", "2f")
  333. JNZ("%%eax", "3f")
  334. JNZ("%%edx", "4f")
  335. JNZ("%%esi", "5f")
  336. iLLM_PASS_SPARSE("%0")
  337. "jmp 6f \n\t"
  338. "2: \n\t"
  339. iMTX_MULT("4*16(%0)", MANGLE(iTab1), "#", PUT_EVEN(ROW4))
  340. "3: \n\t"
  341. iMTX_MULT("5*16(%0)", MANGLE(iTab4), ROUND(walkenIdctRounders+4*16), PUT_ODD(ROW5))
  342. JZ("%%edx", "1f")
  343. "4: \n\t"
  344. iMTX_MULT("6*16(%0)", MANGLE(iTab3), ROUND(walkenIdctRounders+5*16), PUT_EVEN(ROW6))
  345. JZ("%%esi", "1f")
  346. "5: \n\t"
  347. iMTX_MULT("7*16(%0)", MANGLE(iTab2), ROUND(walkenIdctRounders+5*16), PUT_ODD(ROW7))
  348. #if !ARCH_X86_64
  349. iLLM_HEAD
  350. #endif
  351. iLLM_PASS("%0")
  352. "6: \n\t"
  353. : "+r"(block)
  354. :
  355. : XMM_CLOBBERS("%xmm0" , "%xmm1" , "%xmm2" , "%xmm3" ,
  356. "%xmm4" , "%xmm5" , "%xmm6" , "%xmm7" ,)
  357. #if ARCH_X86_64
  358. XMM_CLOBBERS("%xmm8" , "%xmm9" , "%xmm10", "%xmm11",
  359. "%xmm12", "%xmm13", "%xmm14",)
  360. #endif
  361. "%eax", "%ecx", "%edx", "%esi", "memory"
  362. );
  363. }
  364. void ff_idct_xvid_sse2_put(uint8_t *dest, int line_size, short *block)
  365. {
  366. ff_idct_xvid_sse2(block);
  367. ff_put_pixels_clamped_mmx(block, dest, line_size);
  368. }
  369. void ff_idct_xvid_sse2_add(uint8_t *dest, int line_size, short *block)
  370. {
  371. ff_idct_xvid_sse2(block);
  372. ff_add_pixels_clamped_mmx(block, dest, line_size);
  373. }
  374. #endif /* HAVE_INLINE_ASM */