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.

1059 lines
30KB

  1. /*
  2. * MMX optimized DSP utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/x86/asm.h"
  27. #include "libavutil/x86/cpu.h"
  28. #include "libavcodec/dsputil.h"
  29. #include "libavcodec/mpegvideo.h"
  30. #include "libavcodec/mathops.h"
  31. #include "dsputil_mmx.h"
  32. void ff_get_pixels_mmx(int16_t *block, const uint8_t *pixels, int line_size);
  33. void ff_get_pixels_sse2(int16_t *block, const uint8_t *pixels, int line_size);
  34. void ff_diff_pixels_mmx(int16_t *block, const uint8_t *s1, const uint8_t *s2, int stride);
  35. int ff_pix_sum16_mmx(uint8_t * pix, int line_size);
  36. int ff_pix_norm1_mmx(uint8_t *pix, int line_size);
  37. #if HAVE_INLINE_ASM
  38. static int sse8_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
  39. int tmp;
  40. __asm__ volatile (
  41. "movl %4,%%ecx\n"
  42. "shr $1,%%ecx\n"
  43. "pxor %%mm0,%%mm0\n" /* mm0 = 0 */
  44. "pxor %%mm7,%%mm7\n" /* mm7 holds the sum */
  45. "1:\n"
  46. "movq (%0),%%mm1\n" /* mm1 = pix1[0][0-7] */
  47. "movq (%1),%%mm2\n" /* mm2 = pix2[0][0-7] */
  48. "movq (%0,%3),%%mm3\n" /* mm3 = pix1[1][0-7] */
  49. "movq (%1,%3),%%mm4\n" /* mm4 = pix2[1][0-7] */
  50. /* todo: mm1-mm2, mm3-mm4 */
  51. /* algo: subtract mm1 from mm2 with saturation and vice versa */
  52. /* OR the results to get absolute difference */
  53. "movq %%mm1,%%mm5\n"
  54. "movq %%mm3,%%mm6\n"
  55. "psubusb %%mm2,%%mm1\n"
  56. "psubusb %%mm4,%%mm3\n"
  57. "psubusb %%mm5,%%mm2\n"
  58. "psubusb %%mm6,%%mm4\n"
  59. "por %%mm1,%%mm2\n"
  60. "por %%mm3,%%mm4\n"
  61. /* now convert to 16-bit vectors so we can square them */
  62. "movq %%mm2,%%mm1\n"
  63. "movq %%mm4,%%mm3\n"
  64. "punpckhbw %%mm0,%%mm2\n"
  65. "punpckhbw %%mm0,%%mm4\n"
  66. "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
  67. "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
  68. "pmaddwd %%mm2,%%mm2\n"
  69. "pmaddwd %%mm4,%%mm4\n"
  70. "pmaddwd %%mm1,%%mm1\n"
  71. "pmaddwd %%mm3,%%mm3\n"
  72. "lea (%0,%3,2), %0\n" /* pix1 += 2*line_size */
  73. "lea (%1,%3,2), %1\n" /* pix2 += 2*line_size */
  74. "paddd %%mm2,%%mm1\n"
  75. "paddd %%mm4,%%mm3\n"
  76. "paddd %%mm1,%%mm7\n"
  77. "paddd %%mm3,%%mm7\n"
  78. "decl %%ecx\n"
  79. "jnz 1b\n"
  80. "movq %%mm7,%%mm1\n"
  81. "psrlq $32, %%mm7\n" /* shift hi dword to lo */
  82. "paddd %%mm7,%%mm1\n"
  83. "movd %%mm1,%2\n"
  84. : "+r" (pix1), "+r" (pix2), "=r"(tmp)
  85. : "r" ((x86_reg)line_size) , "m" (h)
  86. : "%ecx");
  87. return tmp;
  88. }
  89. static int sse16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
  90. int tmp;
  91. __asm__ volatile (
  92. "movl %4,%%ecx\n"
  93. "pxor %%mm0,%%mm0\n" /* mm0 = 0 */
  94. "pxor %%mm7,%%mm7\n" /* mm7 holds the sum */
  95. "1:\n"
  96. "movq (%0),%%mm1\n" /* mm1 = pix1[0-7] */
  97. "movq (%1),%%mm2\n" /* mm2 = pix2[0-7] */
  98. "movq 8(%0),%%mm3\n" /* mm3 = pix1[8-15] */
  99. "movq 8(%1),%%mm4\n" /* mm4 = pix2[8-15] */
  100. /* todo: mm1-mm2, mm3-mm4 */
  101. /* algo: subtract mm1 from mm2 with saturation and vice versa */
  102. /* OR the results to get absolute difference */
  103. "movq %%mm1,%%mm5\n"
  104. "movq %%mm3,%%mm6\n"
  105. "psubusb %%mm2,%%mm1\n"
  106. "psubusb %%mm4,%%mm3\n"
  107. "psubusb %%mm5,%%mm2\n"
  108. "psubusb %%mm6,%%mm4\n"
  109. "por %%mm1,%%mm2\n"
  110. "por %%mm3,%%mm4\n"
  111. /* now convert to 16-bit vectors so we can square them */
  112. "movq %%mm2,%%mm1\n"
  113. "movq %%mm4,%%mm3\n"
  114. "punpckhbw %%mm0,%%mm2\n"
  115. "punpckhbw %%mm0,%%mm4\n"
  116. "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
  117. "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
  118. "pmaddwd %%mm2,%%mm2\n"
  119. "pmaddwd %%mm4,%%mm4\n"
  120. "pmaddwd %%mm1,%%mm1\n"
  121. "pmaddwd %%mm3,%%mm3\n"
  122. "add %3,%0\n"
  123. "add %3,%1\n"
  124. "paddd %%mm2,%%mm1\n"
  125. "paddd %%mm4,%%mm3\n"
  126. "paddd %%mm1,%%mm7\n"
  127. "paddd %%mm3,%%mm7\n"
  128. "decl %%ecx\n"
  129. "jnz 1b\n"
  130. "movq %%mm7,%%mm1\n"
  131. "psrlq $32, %%mm7\n" /* shift hi dword to lo */
  132. "paddd %%mm7,%%mm1\n"
  133. "movd %%mm1,%2\n"
  134. : "+r" (pix1), "+r" (pix2), "=r"(tmp)
  135. : "r" ((x86_reg)line_size) , "m" (h)
  136. : "%ecx");
  137. return tmp;
  138. }
  139. static int hf_noise8_mmx(uint8_t * pix1, int line_size, int h) {
  140. int tmp;
  141. __asm__ volatile (
  142. "movl %3,%%ecx\n"
  143. "pxor %%mm7,%%mm7\n"
  144. "pxor %%mm6,%%mm6\n"
  145. "movq (%0),%%mm0\n"
  146. "movq %%mm0, %%mm1\n"
  147. "psllq $8, %%mm0\n"
  148. "psrlq $8, %%mm1\n"
  149. "psrlq $8, %%mm0\n"
  150. "movq %%mm0, %%mm2\n"
  151. "movq %%mm1, %%mm3\n"
  152. "punpcklbw %%mm7,%%mm0\n"
  153. "punpcklbw %%mm7,%%mm1\n"
  154. "punpckhbw %%mm7,%%mm2\n"
  155. "punpckhbw %%mm7,%%mm3\n"
  156. "psubw %%mm1, %%mm0\n"
  157. "psubw %%mm3, %%mm2\n"
  158. "add %2,%0\n"
  159. "movq (%0),%%mm4\n"
  160. "movq %%mm4, %%mm1\n"
  161. "psllq $8, %%mm4\n"
  162. "psrlq $8, %%mm1\n"
  163. "psrlq $8, %%mm4\n"
  164. "movq %%mm4, %%mm5\n"
  165. "movq %%mm1, %%mm3\n"
  166. "punpcklbw %%mm7,%%mm4\n"
  167. "punpcklbw %%mm7,%%mm1\n"
  168. "punpckhbw %%mm7,%%mm5\n"
  169. "punpckhbw %%mm7,%%mm3\n"
  170. "psubw %%mm1, %%mm4\n"
  171. "psubw %%mm3, %%mm5\n"
  172. "psubw %%mm4, %%mm0\n"
  173. "psubw %%mm5, %%mm2\n"
  174. "pxor %%mm3, %%mm3\n"
  175. "pxor %%mm1, %%mm1\n"
  176. "pcmpgtw %%mm0, %%mm3\n\t"
  177. "pcmpgtw %%mm2, %%mm1\n\t"
  178. "pxor %%mm3, %%mm0\n"
  179. "pxor %%mm1, %%mm2\n"
  180. "psubw %%mm3, %%mm0\n"
  181. "psubw %%mm1, %%mm2\n"
  182. "paddw %%mm0, %%mm2\n"
  183. "paddw %%mm2, %%mm6\n"
  184. "add %2,%0\n"
  185. "1:\n"
  186. "movq (%0),%%mm0\n"
  187. "movq %%mm0, %%mm1\n"
  188. "psllq $8, %%mm0\n"
  189. "psrlq $8, %%mm1\n"
  190. "psrlq $8, %%mm0\n"
  191. "movq %%mm0, %%mm2\n"
  192. "movq %%mm1, %%mm3\n"
  193. "punpcklbw %%mm7,%%mm0\n"
  194. "punpcklbw %%mm7,%%mm1\n"
  195. "punpckhbw %%mm7,%%mm2\n"
  196. "punpckhbw %%mm7,%%mm3\n"
  197. "psubw %%mm1, %%mm0\n"
  198. "psubw %%mm3, %%mm2\n"
  199. "psubw %%mm0, %%mm4\n"
  200. "psubw %%mm2, %%mm5\n"
  201. "pxor %%mm3, %%mm3\n"
  202. "pxor %%mm1, %%mm1\n"
  203. "pcmpgtw %%mm4, %%mm3\n\t"
  204. "pcmpgtw %%mm5, %%mm1\n\t"
  205. "pxor %%mm3, %%mm4\n"
  206. "pxor %%mm1, %%mm5\n"
  207. "psubw %%mm3, %%mm4\n"
  208. "psubw %%mm1, %%mm5\n"
  209. "paddw %%mm4, %%mm5\n"
  210. "paddw %%mm5, %%mm6\n"
  211. "add %2,%0\n"
  212. "movq (%0),%%mm4\n"
  213. "movq %%mm4, %%mm1\n"
  214. "psllq $8, %%mm4\n"
  215. "psrlq $8, %%mm1\n"
  216. "psrlq $8, %%mm4\n"
  217. "movq %%mm4, %%mm5\n"
  218. "movq %%mm1, %%mm3\n"
  219. "punpcklbw %%mm7,%%mm4\n"
  220. "punpcklbw %%mm7,%%mm1\n"
  221. "punpckhbw %%mm7,%%mm5\n"
  222. "punpckhbw %%mm7,%%mm3\n"
  223. "psubw %%mm1, %%mm4\n"
  224. "psubw %%mm3, %%mm5\n"
  225. "psubw %%mm4, %%mm0\n"
  226. "psubw %%mm5, %%mm2\n"
  227. "pxor %%mm3, %%mm3\n"
  228. "pxor %%mm1, %%mm1\n"
  229. "pcmpgtw %%mm0, %%mm3\n\t"
  230. "pcmpgtw %%mm2, %%mm1\n\t"
  231. "pxor %%mm3, %%mm0\n"
  232. "pxor %%mm1, %%mm2\n"
  233. "psubw %%mm3, %%mm0\n"
  234. "psubw %%mm1, %%mm2\n"
  235. "paddw %%mm0, %%mm2\n"
  236. "paddw %%mm2, %%mm6\n"
  237. "add %2,%0\n"
  238. "subl $2, %%ecx\n"
  239. " jnz 1b\n"
  240. "movq %%mm6, %%mm0\n"
  241. "punpcklwd %%mm7,%%mm0\n"
  242. "punpckhwd %%mm7,%%mm6\n"
  243. "paddd %%mm0, %%mm6\n"
  244. "movq %%mm6,%%mm0\n"
  245. "psrlq $32, %%mm6\n"
  246. "paddd %%mm6,%%mm0\n"
  247. "movd %%mm0,%1\n"
  248. : "+r" (pix1), "=r"(tmp)
  249. : "r" ((x86_reg)line_size) , "g" (h-2)
  250. : "%ecx");
  251. return tmp;
  252. }
  253. static int hf_noise16_mmx(uint8_t * pix1, int line_size, int h) {
  254. int tmp;
  255. uint8_t * pix= pix1;
  256. __asm__ volatile (
  257. "movl %3,%%ecx\n"
  258. "pxor %%mm7,%%mm7\n"
  259. "pxor %%mm6,%%mm6\n"
  260. "movq (%0),%%mm0\n"
  261. "movq 1(%0),%%mm1\n"
  262. "movq %%mm0, %%mm2\n"
  263. "movq %%mm1, %%mm3\n"
  264. "punpcklbw %%mm7,%%mm0\n"
  265. "punpcklbw %%mm7,%%mm1\n"
  266. "punpckhbw %%mm7,%%mm2\n"
  267. "punpckhbw %%mm7,%%mm3\n"
  268. "psubw %%mm1, %%mm0\n"
  269. "psubw %%mm3, %%mm2\n"
  270. "add %2,%0\n"
  271. "movq (%0),%%mm4\n"
  272. "movq 1(%0),%%mm1\n"
  273. "movq %%mm4, %%mm5\n"
  274. "movq %%mm1, %%mm3\n"
  275. "punpcklbw %%mm7,%%mm4\n"
  276. "punpcklbw %%mm7,%%mm1\n"
  277. "punpckhbw %%mm7,%%mm5\n"
  278. "punpckhbw %%mm7,%%mm3\n"
  279. "psubw %%mm1, %%mm4\n"
  280. "psubw %%mm3, %%mm5\n"
  281. "psubw %%mm4, %%mm0\n"
  282. "psubw %%mm5, %%mm2\n"
  283. "pxor %%mm3, %%mm3\n"
  284. "pxor %%mm1, %%mm1\n"
  285. "pcmpgtw %%mm0, %%mm3\n\t"
  286. "pcmpgtw %%mm2, %%mm1\n\t"
  287. "pxor %%mm3, %%mm0\n"
  288. "pxor %%mm1, %%mm2\n"
  289. "psubw %%mm3, %%mm0\n"
  290. "psubw %%mm1, %%mm2\n"
  291. "paddw %%mm0, %%mm2\n"
  292. "paddw %%mm2, %%mm6\n"
  293. "add %2,%0\n"
  294. "1:\n"
  295. "movq (%0),%%mm0\n"
  296. "movq 1(%0),%%mm1\n"
  297. "movq %%mm0, %%mm2\n"
  298. "movq %%mm1, %%mm3\n"
  299. "punpcklbw %%mm7,%%mm0\n"
  300. "punpcklbw %%mm7,%%mm1\n"
  301. "punpckhbw %%mm7,%%mm2\n"
  302. "punpckhbw %%mm7,%%mm3\n"
  303. "psubw %%mm1, %%mm0\n"
  304. "psubw %%mm3, %%mm2\n"
  305. "psubw %%mm0, %%mm4\n"
  306. "psubw %%mm2, %%mm5\n"
  307. "pxor %%mm3, %%mm3\n"
  308. "pxor %%mm1, %%mm1\n"
  309. "pcmpgtw %%mm4, %%mm3\n\t"
  310. "pcmpgtw %%mm5, %%mm1\n\t"
  311. "pxor %%mm3, %%mm4\n"
  312. "pxor %%mm1, %%mm5\n"
  313. "psubw %%mm3, %%mm4\n"
  314. "psubw %%mm1, %%mm5\n"
  315. "paddw %%mm4, %%mm5\n"
  316. "paddw %%mm5, %%mm6\n"
  317. "add %2,%0\n"
  318. "movq (%0),%%mm4\n"
  319. "movq 1(%0),%%mm1\n"
  320. "movq %%mm4, %%mm5\n"
  321. "movq %%mm1, %%mm3\n"
  322. "punpcklbw %%mm7,%%mm4\n"
  323. "punpcklbw %%mm7,%%mm1\n"
  324. "punpckhbw %%mm7,%%mm5\n"
  325. "punpckhbw %%mm7,%%mm3\n"
  326. "psubw %%mm1, %%mm4\n"
  327. "psubw %%mm3, %%mm5\n"
  328. "psubw %%mm4, %%mm0\n"
  329. "psubw %%mm5, %%mm2\n"
  330. "pxor %%mm3, %%mm3\n"
  331. "pxor %%mm1, %%mm1\n"
  332. "pcmpgtw %%mm0, %%mm3\n\t"
  333. "pcmpgtw %%mm2, %%mm1\n\t"
  334. "pxor %%mm3, %%mm0\n"
  335. "pxor %%mm1, %%mm2\n"
  336. "psubw %%mm3, %%mm0\n"
  337. "psubw %%mm1, %%mm2\n"
  338. "paddw %%mm0, %%mm2\n"
  339. "paddw %%mm2, %%mm6\n"
  340. "add %2,%0\n"
  341. "subl $2, %%ecx\n"
  342. " jnz 1b\n"
  343. "movq %%mm6, %%mm0\n"
  344. "punpcklwd %%mm7,%%mm0\n"
  345. "punpckhwd %%mm7,%%mm6\n"
  346. "paddd %%mm0, %%mm6\n"
  347. "movq %%mm6,%%mm0\n"
  348. "psrlq $32, %%mm6\n"
  349. "paddd %%mm6,%%mm0\n"
  350. "movd %%mm0,%1\n"
  351. : "+r" (pix1), "=r"(tmp)
  352. : "r" ((x86_reg)line_size) , "g" (h-2)
  353. : "%ecx");
  354. return tmp + hf_noise8_mmx(pix+8, line_size, h);
  355. }
  356. static int nsse16_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
  357. MpegEncContext *c = p;
  358. int score1, score2;
  359. if(c) score1 = c->dsp.sse[0](c, pix1, pix2, line_size, h);
  360. else score1 = sse16_mmx(c, pix1, pix2, line_size, h);
  361. score2= hf_noise16_mmx(pix1, line_size, h) - hf_noise16_mmx(pix2, line_size, h);
  362. if(c) return score1 + FFABS(score2)*c->avctx->nsse_weight;
  363. else return score1 + FFABS(score2)*8;
  364. }
  365. static int nsse8_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
  366. MpegEncContext *c = p;
  367. int score1= sse8_mmx(c, pix1, pix2, line_size, h);
  368. int score2= hf_noise8_mmx(pix1, line_size, h) - hf_noise8_mmx(pix2, line_size, h);
  369. if(c) return score1 + FFABS(score2)*c->avctx->nsse_weight;
  370. else return score1 + FFABS(score2)*8;
  371. }
  372. static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) {
  373. int tmp;
  374. assert( (((int)pix) & 7) == 0);
  375. assert((line_size &7) ==0);
  376. #define SUM(in0, in1, out0, out1) \
  377. "movq (%0), %%mm2\n"\
  378. "movq 8(%0), %%mm3\n"\
  379. "add %2,%0\n"\
  380. "movq %%mm2, " #out0 "\n"\
  381. "movq %%mm3, " #out1 "\n"\
  382. "psubusb " #in0 ", %%mm2\n"\
  383. "psubusb " #in1 ", %%mm3\n"\
  384. "psubusb " #out0 ", " #in0 "\n"\
  385. "psubusb " #out1 ", " #in1 "\n"\
  386. "por %%mm2, " #in0 "\n"\
  387. "por %%mm3, " #in1 "\n"\
  388. "movq " #in0 ", %%mm2\n"\
  389. "movq " #in1 ", %%mm3\n"\
  390. "punpcklbw %%mm7, " #in0 "\n"\
  391. "punpcklbw %%mm7, " #in1 "\n"\
  392. "punpckhbw %%mm7, %%mm2\n"\
  393. "punpckhbw %%mm7, %%mm3\n"\
  394. "paddw " #in1 ", " #in0 "\n"\
  395. "paddw %%mm3, %%mm2\n"\
  396. "paddw %%mm2, " #in0 "\n"\
  397. "paddw " #in0 ", %%mm6\n"
  398. __asm__ volatile (
  399. "movl %3,%%ecx\n"
  400. "pxor %%mm6,%%mm6\n"
  401. "pxor %%mm7,%%mm7\n"
  402. "movq (%0),%%mm0\n"
  403. "movq 8(%0),%%mm1\n"
  404. "add %2,%0\n"
  405. "jmp 2f\n"
  406. "1:\n"
  407. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  408. "2:\n"
  409. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  410. "subl $2, %%ecx\n"
  411. "jnz 1b\n"
  412. "movq %%mm6,%%mm0\n"
  413. "psrlq $32, %%mm6\n"
  414. "paddw %%mm6,%%mm0\n"
  415. "movq %%mm0,%%mm6\n"
  416. "psrlq $16, %%mm0\n"
  417. "paddw %%mm6,%%mm0\n"
  418. "movd %%mm0,%1\n"
  419. : "+r" (pix), "=r"(tmp)
  420. : "r" ((x86_reg)line_size) , "m" (h)
  421. : "%ecx");
  422. return tmp & 0xFFFF;
  423. }
  424. #undef SUM
  425. static int vsad_intra16_mmxext(void *v, uint8_t *pix, uint8_t *dummy,
  426. int line_size, int h)
  427. {
  428. int tmp;
  429. assert( (((int)pix) & 7) == 0);
  430. assert((line_size &7) ==0);
  431. #define SUM(in0, in1, out0, out1) \
  432. "movq (%0), " #out0 "\n"\
  433. "movq 8(%0), " #out1 "\n"\
  434. "add %2,%0\n"\
  435. "psadbw " #out0 ", " #in0 "\n"\
  436. "psadbw " #out1 ", " #in1 "\n"\
  437. "paddw " #in1 ", " #in0 "\n"\
  438. "paddw " #in0 ", %%mm6\n"
  439. __asm__ volatile (
  440. "movl %3,%%ecx\n"
  441. "pxor %%mm6,%%mm6\n"
  442. "pxor %%mm7,%%mm7\n"
  443. "movq (%0),%%mm0\n"
  444. "movq 8(%0),%%mm1\n"
  445. "add %2,%0\n"
  446. "jmp 2f\n"
  447. "1:\n"
  448. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  449. "2:\n"
  450. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  451. "subl $2, %%ecx\n"
  452. "jnz 1b\n"
  453. "movd %%mm6,%1\n"
  454. : "+r" (pix), "=r"(tmp)
  455. : "r" ((x86_reg)line_size) , "m" (h)
  456. : "%ecx");
  457. return tmp;
  458. }
  459. #undef SUM
  460. static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
  461. int tmp;
  462. assert( (((int)pix1) & 7) == 0);
  463. assert( (((int)pix2) & 7) == 0);
  464. assert((line_size &7) ==0);
  465. #define SUM(in0, in1, out0, out1) \
  466. "movq (%0),%%mm2\n"\
  467. "movq (%1)," #out0 "\n"\
  468. "movq 8(%0),%%mm3\n"\
  469. "movq 8(%1)," #out1 "\n"\
  470. "add %3,%0\n"\
  471. "add %3,%1\n"\
  472. "psubb " #out0 ", %%mm2\n"\
  473. "psubb " #out1 ", %%mm3\n"\
  474. "pxor %%mm7, %%mm2\n"\
  475. "pxor %%mm7, %%mm3\n"\
  476. "movq %%mm2, " #out0 "\n"\
  477. "movq %%mm3, " #out1 "\n"\
  478. "psubusb " #in0 ", %%mm2\n"\
  479. "psubusb " #in1 ", %%mm3\n"\
  480. "psubusb " #out0 ", " #in0 "\n"\
  481. "psubusb " #out1 ", " #in1 "\n"\
  482. "por %%mm2, " #in0 "\n"\
  483. "por %%mm3, " #in1 "\n"\
  484. "movq " #in0 ", %%mm2\n"\
  485. "movq " #in1 ", %%mm3\n"\
  486. "punpcklbw %%mm7, " #in0 "\n"\
  487. "punpcklbw %%mm7, " #in1 "\n"\
  488. "punpckhbw %%mm7, %%mm2\n"\
  489. "punpckhbw %%mm7, %%mm3\n"\
  490. "paddw " #in1 ", " #in0 "\n"\
  491. "paddw %%mm3, %%mm2\n"\
  492. "paddw %%mm2, " #in0 "\n"\
  493. "paddw " #in0 ", %%mm6\n"
  494. __asm__ volatile (
  495. "movl %4,%%ecx\n"
  496. "pxor %%mm6,%%mm6\n"
  497. "pcmpeqw %%mm7,%%mm7\n"
  498. "psllw $15, %%mm7\n"
  499. "packsswb %%mm7, %%mm7\n"
  500. "movq (%0),%%mm0\n"
  501. "movq (%1),%%mm2\n"
  502. "movq 8(%0),%%mm1\n"
  503. "movq 8(%1),%%mm3\n"
  504. "add %3,%0\n"
  505. "add %3,%1\n"
  506. "psubb %%mm2, %%mm0\n"
  507. "psubb %%mm3, %%mm1\n"
  508. "pxor %%mm7, %%mm0\n"
  509. "pxor %%mm7, %%mm1\n"
  510. "jmp 2f\n"
  511. "1:\n"
  512. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  513. "2:\n"
  514. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  515. "subl $2, %%ecx\n"
  516. "jnz 1b\n"
  517. "movq %%mm6,%%mm0\n"
  518. "psrlq $32, %%mm6\n"
  519. "paddw %%mm6,%%mm0\n"
  520. "movq %%mm0,%%mm6\n"
  521. "psrlq $16, %%mm0\n"
  522. "paddw %%mm6,%%mm0\n"
  523. "movd %%mm0,%2\n"
  524. : "+r" (pix1), "+r" (pix2), "=r"(tmp)
  525. : "r" ((x86_reg)line_size) , "m" (h)
  526. : "%ecx");
  527. return tmp & 0x7FFF;
  528. }
  529. #undef SUM
  530. static int vsad16_mmxext(void *v, uint8_t *pix1, uint8_t *pix2,
  531. int line_size, int h)
  532. {
  533. int tmp;
  534. assert( (((int)pix1) & 7) == 0);
  535. assert( (((int)pix2) & 7) == 0);
  536. assert((line_size &7) ==0);
  537. #define SUM(in0, in1, out0, out1) \
  538. "movq (%0)," #out0 "\n"\
  539. "movq (%1),%%mm2\n"\
  540. "movq 8(%0)," #out1 "\n"\
  541. "movq 8(%1),%%mm3\n"\
  542. "add %3,%0\n"\
  543. "add %3,%1\n"\
  544. "psubb %%mm2, " #out0 "\n"\
  545. "psubb %%mm3, " #out1 "\n"\
  546. "pxor %%mm7, " #out0 "\n"\
  547. "pxor %%mm7, " #out1 "\n"\
  548. "psadbw " #out0 ", " #in0 "\n"\
  549. "psadbw " #out1 ", " #in1 "\n"\
  550. "paddw " #in1 ", " #in0 "\n"\
  551. "paddw " #in0 ", %%mm6\n"
  552. __asm__ volatile (
  553. "movl %4,%%ecx\n"
  554. "pxor %%mm6,%%mm6\n"
  555. "pcmpeqw %%mm7,%%mm7\n"
  556. "psllw $15, %%mm7\n"
  557. "packsswb %%mm7, %%mm7\n"
  558. "movq (%0),%%mm0\n"
  559. "movq (%1),%%mm2\n"
  560. "movq 8(%0),%%mm1\n"
  561. "movq 8(%1),%%mm3\n"
  562. "add %3,%0\n"
  563. "add %3,%1\n"
  564. "psubb %%mm2, %%mm0\n"
  565. "psubb %%mm3, %%mm1\n"
  566. "pxor %%mm7, %%mm0\n"
  567. "pxor %%mm7, %%mm1\n"
  568. "jmp 2f\n"
  569. "1:\n"
  570. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  571. "2:\n"
  572. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  573. "subl $2, %%ecx\n"
  574. "jnz 1b\n"
  575. "movd %%mm6,%2\n"
  576. : "+r" (pix1), "+r" (pix2), "=r"(tmp)
  577. : "r" ((x86_reg)line_size) , "m" (h)
  578. : "%ecx");
  579. return tmp;
  580. }
  581. #undef SUM
  582. static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
  583. x86_reg i=0;
  584. __asm__ volatile(
  585. "1: \n\t"
  586. "movq (%2, %0), %%mm0 \n\t"
  587. "movq (%1, %0), %%mm1 \n\t"
  588. "psubb %%mm0, %%mm1 \n\t"
  589. "movq %%mm1, (%3, %0) \n\t"
  590. "movq 8(%2, %0), %%mm0 \n\t"
  591. "movq 8(%1, %0), %%mm1 \n\t"
  592. "psubb %%mm0, %%mm1 \n\t"
  593. "movq %%mm1, 8(%3, %0) \n\t"
  594. "add $16, %0 \n\t"
  595. "cmp %4, %0 \n\t"
  596. " jb 1b \n\t"
  597. : "+r" (i)
  598. : "r"(src1), "r"(src2), "r"(dst), "r"((x86_reg)w-15)
  599. );
  600. for(; i<w; i++)
  601. dst[i+0] = src1[i+0]-src2[i+0];
  602. }
  603. static void sub_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *src1,
  604. const uint8_t *src2, int w,
  605. int *left, int *left_top)
  606. {
  607. x86_reg i=0;
  608. uint8_t l, lt;
  609. __asm__ volatile(
  610. "movq (%1, %0), %%mm0 \n\t" // LT
  611. "psllq $8, %%mm0 \n\t"
  612. "1: \n\t"
  613. "movq (%1, %0), %%mm1 \n\t" // T
  614. "movq -1(%2, %0), %%mm2 \n\t" // L
  615. "movq (%2, %0), %%mm3 \n\t" // X
  616. "movq %%mm2, %%mm4 \n\t" // L
  617. "psubb %%mm0, %%mm2 \n\t"
  618. "paddb %%mm1, %%mm2 \n\t" // L + T - LT
  619. "movq %%mm4, %%mm5 \n\t" // L
  620. "pmaxub %%mm1, %%mm4 \n\t" // max(T, L)
  621. "pminub %%mm5, %%mm1 \n\t" // min(T, L)
  622. "pminub %%mm2, %%mm4 \n\t"
  623. "pmaxub %%mm1, %%mm4 \n\t"
  624. "psubb %%mm4, %%mm3 \n\t" // dst - pred
  625. "movq %%mm3, (%3, %0) \n\t"
  626. "add $8, %0 \n\t"
  627. "movq -1(%1, %0), %%mm0 \n\t" // LT
  628. "cmp %4, %0 \n\t"
  629. " jb 1b \n\t"
  630. : "+r" (i)
  631. : "r"(src1), "r"(src2), "r"(dst), "r"((x86_reg)w)
  632. );
  633. l= *left;
  634. lt= *left_top;
  635. dst[0]= src2[0] - mid_pred(l, src1[0], (l + src1[0] - lt)&0xFF);
  636. *left_top= src1[w-1];
  637. *left = src2[w-1];
  638. }
  639. #define MMABS_MMX(a,z)\
  640. "pxor " #z ", " #z " \n\t"\
  641. "pcmpgtw " #a ", " #z " \n\t"\
  642. "pxor " #z ", " #a " \n\t"\
  643. "psubw " #z ", " #a " \n\t"
  644. #define MMABS_MMXEXT(a, z) \
  645. "pxor " #z ", " #z " \n\t"\
  646. "psubw " #a ", " #z " \n\t"\
  647. "pmaxsw " #z ", " #a " \n\t"
  648. #define MMABS_SSSE3(a,z)\
  649. "pabsw " #a ", " #a " \n\t"
  650. #define MMABS_SUM(a,z, sum)\
  651. MMABS(a,z)\
  652. "paddusw " #a ", " #sum " \n\t"
  653. /* FIXME: HSUM_* saturates at 64k, while an 8x8 hadamard or dct block can get up to
  654. * about 100k on extreme inputs. But that's very unlikely to occur in natural video,
  655. * and it's even more unlikely to not have any alternative mvs/modes with lower cost. */
  656. #define HSUM_MMX(a, t, dst)\
  657. "movq "#a", "#t" \n\t"\
  658. "psrlq $32, "#a" \n\t"\
  659. "paddusw "#t", "#a" \n\t"\
  660. "movq "#a", "#t" \n\t"\
  661. "psrlq $16, "#a" \n\t"\
  662. "paddusw "#t", "#a" \n\t"\
  663. "movd "#a", "#dst" \n\t"\
  664. #define HSUM_MMXEXT(a, t, dst) \
  665. "pshufw $0x0E, "#a", "#t" \n\t"\
  666. "paddusw "#t", "#a" \n\t"\
  667. "pshufw $0x01, "#a", "#t" \n\t"\
  668. "paddusw "#t", "#a" \n\t"\
  669. "movd "#a", "#dst" \n\t"\
  670. #define HSUM_SSE2(a, t, dst)\
  671. "movhlps "#a", "#t" \n\t"\
  672. "paddusw "#t", "#a" \n\t"\
  673. "pshuflw $0x0E, "#a", "#t" \n\t"\
  674. "paddusw "#t", "#a" \n\t"\
  675. "pshuflw $0x01, "#a", "#t" \n\t"\
  676. "paddusw "#t", "#a" \n\t"\
  677. "movd "#a", "#dst" \n\t"\
  678. #define DCT_SAD4(m,mm,o)\
  679. "mov"#m" "#o"+ 0(%1), "#mm"2 \n\t"\
  680. "mov"#m" "#o"+16(%1), "#mm"3 \n\t"\
  681. "mov"#m" "#o"+32(%1), "#mm"4 \n\t"\
  682. "mov"#m" "#o"+48(%1), "#mm"5 \n\t"\
  683. MMABS_SUM(mm##2, mm##6, mm##0)\
  684. MMABS_SUM(mm##3, mm##7, mm##1)\
  685. MMABS_SUM(mm##4, mm##6, mm##0)\
  686. MMABS_SUM(mm##5, mm##7, mm##1)\
  687. #define DCT_SAD_MMX\
  688. "pxor %%mm0, %%mm0 \n\t"\
  689. "pxor %%mm1, %%mm1 \n\t"\
  690. DCT_SAD4(q, %%mm, 0)\
  691. DCT_SAD4(q, %%mm, 8)\
  692. DCT_SAD4(q, %%mm, 64)\
  693. DCT_SAD4(q, %%mm, 72)\
  694. "paddusw %%mm1, %%mm0 \n\t"\
  695. HSUM(%%mm0, %%mm1, %0)
  696. #define DCT_SAD_SSE2\
  697. "pxor %%xmm0, %%xmm0 \n\t"\
  698. "pxor %%xmm1, %%xmm1 \n\t"\
  699. DCT_SAD4(dqa, %%xmm, 0)\
  700. DCT_SAD4(dqa, %%xmm, 64)\
  701. "paddusw %%xmm1, %%xmm0 \n\t"\
  702. HSUM(%%xmm0, %%xmm1, %0)
  703. #define DCT_SAD_FUNC(cpu) \
  704. static int sum_abs_dctelem_##cpu(int16_t *block){\
  705. int sum;\
  706. __asm__ volatile(\
  707. DCT_SAD\
  708. :"=r"(sum)\
  709. :"r"(block)\
  710. );\
  711. return sum&0xFFFF;\
  712. }
  713. #define DCT_SAD DCT_SAD_MMX
  714. #define HSUM(a,t,dst) HSUM_MMX(a,t,dst)
  715. #define MMABS(a,z) MMABS_MMX(a,z)
  716. DCT_SAD_FUNC(mmx)
  717. #undef MMABS
  718. #undef HSUM
  719. #define HSUM(a,t,dst) HSUM_MMXEXT(a,t,dst)
  720. #define MMABS(a,z) MMABS_MMXEXT(a,z)
  721. DCT_SAD_FUNC(mmxext)
  722. #undef HSUM
  723. #undef DCT_SAD
  724. #define DCT_SAD DCT_SAD_SSE2
  725. #define HSUM(a,t,dst) HSUM_SSE2(a,t,dst)
  726. DCT_SAD_FUNC(sse2)
  727. #undef MMABS
  728. #if HAVE_SSSE3_INLINE
  729. #define MMABS(a,z) MMABS_SSSE3(a,z)
  730. DCT_SAD_FUNC(ssse3)
  731. #undef MMABS
  732. #endif
  733. #undef HSUM
  734. #undef DCT_SAD
  735. static int ssd_int8_vs_int16_mmx(const int8_t *pix1, const int16_t *pix2, int size){
  736. int sum;
  737. x86_reg i=size;
  738. __asm__ volatile(
  739. "pxor %%mm4, %%mm4 \n"
  740. "1: \n"
  741. "sub $8, %0 \n"
  742. "movq (%2,%0), %%mm2 \n"
  743. "movq (%3,%0,2), %%mm0 \n"
  744. "movq 8(%3,%0,2), %%mm1 \n"
  745. "punpckhbw %%mm2, %%mm3 \n"
  746. "punpcklbw %%mm2, %%mm2 \n"
  747. "psraw $8, %%mm3 \n"
  748. "psraw $8, %%mm2 \n"
  749. "psubw %%mm3, %%mm1 \n"
  750. "psubw %%mm2, %%mm0 \n"
  751. "pmaddwd %%mm1, %%mm1 \n"
  752. "pmaddwd %%mm0, %%mm0 \n"
  753. "paddd %%mm1, %%mm4 \n"
  754. "paddd %%mm0, %%mm4 \n"
  755. "jg 1b \n"
  756. "movq %%mm4, %%mm3 \n"
  757. "psrlq $32, %%mm3 \n"
  758. "paddd %%mm3, %%mm4 \n"
  759. "movd %%mm4, %1 \n"
  760. :"+r"(i), "=r"(sum)
  761. :"r"(pix1), "r"(pix2)
  762. );
  763. return sum;
  764. }
  765. #define PHADDD(a, t)\
  766. "movq "#a", "#t" \n\t"\
  767. "psrlq $32, "#a" \n\t"\
  768. "paddd "#t", "#a" \n\t"
  769. /*
  770. pmulhw: dst[0-15]=(src[0-15]*dst[0-15])[16-31]
  771. pmulhrw: dst[0-15]=(src[0-15]*dst[0-15] + 0x8000)[16-31]
  772. pmulhrsw: dst[0-15]=(src[0-15]*dst[0-15] + 0x4000)[15-30]
  773. */
  774. #define PMULHRW(x, y, s, o)\
  775. "pmulhw " #s ", "#x " \n\t"\
  776. "pmulhw " #s ", "#y " \n\t"\
  777. "paddw " #o ", "#x " \n\t"\
  778. "paddw " #o ", "#y " \n\t"\
  779. "psraw $1, "#x " \n\t"\
  780. "psraw $1, "#y " \n\t"
  781. #define DEF(x) x ## _mmx
  782. #define SET_RND MOVQ_WONE
  783. #define SCALE_OFFSET 1
  784. #include "dsputil_qns_template.c"
  785. #undef DEF
  786. #undef SET_RND
  787. #undef SCALE_OFFSET
  788. #undef PMULHRW
  789. #define DEF(x) x ## _3dnow
  790. #define SET_RND(x)
  791. #define SCALE_OFFSET 0
  792. #define PMULHRW(x, y, s, o)\
  793. "pmulhrw " #s ", "#x " \n\t"\
  794. "pmulhrw " #s ", "#y " \n\t"
  795. #include "dsputil_qns_template.c"
  796. #undef DEF
  797. #undef SET_RND
  798. #undef SCALE_OFFSET
  799. #undef PMULHRW
  800. #if HAVE_SSSE3_INLINE
  801. #undef PHADDD
  802. #define DEF(x) x ## _ssse3
  803. #define SET_RND(x)
  804. #define SCALE_OFFSET -1
  805. #define PHADDD(a, t)\
  806. "pshufw $0x0E, "#a", "#t" \n\t"\
  807. "paddd "#t", "#a" \n\t" /* faster than phaddd on core2 */
  808. #define PMULHRW(x, y, s, o)\
  809. "pmulhrsw " #s ", "#x " \n\t"\
  810. "pmulhrsw " #s ", "#y " \n\t"
  811. #include "dsputil_qns_template.c"
  812. #undef DEF
  813. #undef SET_RND
  814. #undef SCALE_OFFSET
  815. #undef PMULHRW
  816. #undef PHADDD
  817. #endif /* HAVE_SSSE3_INLINE */
  818. #endif /* HAVE_INLINE_ASM */
  819. int ff_sse16_sse2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
  820. #define hadamard_func(cpu) \
  821. int ff_hadamard8_diff_##cpu (void *s, uint8_t *src1, uint8_t *src2, \
  822. int stride, int h); \
  823. int ff_hadamard8_diff16_##cpu(void *s, uint8_t *src1, uint8_t *src2, \
  824. int stride, int h);
  825. hadamard_func(mmx)
  826. hadamard_func(mmxext)
  827. hadamard_func(sse2)
  828. hadamard_func(ssse3)
  829. av_cold void ff_dsputilenc_init_mmx(DSPContext *c, AVCodecContext *avctx)
  830. {
  831. int mm_flags = av_get_cpu_flags();
  832. int bit_depth = avctx->bits_per_raw_sample;
  833. #if HAVE_YASM
  834. if (EXTERNAL_MMX(mm_flags)) {
  835. if (bit_depth <= 8)
  836. c->get_pixels = ff_get_pixels_mmx;
  837. c->diff_pixels = ff_diff_pixels_mmx;
  838. c->pix_sum = ff_pix_sum16_mmx;
  839. c->pix_norm1 = ff_pix_norm1_mmx;
  840. }
  841. if (EXTERNAL_SSE2(mm_flags))
  842. if (bit_depth <= 8)
  843. c->get_pixels = ff_get_pixels_sse2;
  844. #endif /* HAVE_YASM */
  845. #if HAVE_INLINE_ASM
  846. if (mm_flags & AV_CPU_FLAG_MMX) {
  847. const int dct_algo = avctx->dct_algo;
  848. if (avctx->bits_per_raw_sample <= 8 &&
  849. (dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX)) {
  850. if(mm_flags & AV_CPU_FLAG_SSE2){
  851. c->fdct = ff_fdct_sse2;
  852. } else if (mm_flags & AV_CPU_FLAG_MMXEXT) {
  853. c->fdct = ff_fdct_mmxext;
  854. }else{
  855. c->fdct = ff_fdct_mmx;
  856. }
  857. }
  858. c->diff_bytes= diff_bytes_mmx;
  859. c->sum_abs_dctelem= sum_abs_dctelem_mmx;
  860. c->sse[0] = sse16_mmx;
  861. c->sse[1] = sse8_mmx;
  862. c->vsad[4]= vsad_intra16_mmx;
  863. c->nsse[0] = nsse16_mmx;
  864. c->nsse[1] = nsse8_mmx;
  865. if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
  866. c->vsad[0] = vsad16_mmx;
  867. }
  868. if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
  869. c->try_8x8basis= try_8x8basis_mmx;
  870. }
  871. c->add_8x8basis= add_8x8basis_mmx;
  872. c->ssd_int8_vs_int16 = ssd_int8_vs_int16_mmx;
  873. if (mm_flags & AV_CPU_FLAG_MMXEXT) {
  874. c->sum_abs_dctelem = sum_abs_dctelem_mmxext;
  875. c->vsad[4] = vsad_intra16_mmxext;
  876. if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
  877. c->vsad[0] = vsad16_mmxext;
  878. }
  879. c->sub_hfyu_median_prediction = sub_hfyu_median_prediction_mmxext;
  880. }
  881. if(mm_flags & AV_CPU_FLAG_SSE2){
  882. c->sum_abs_dctelem= sum_abs_dctelem_sse2;
  883. }
  884. #if HAVE_SSSE3_INLINE
  885. if(mm_flags & AV_CPU_FLAG_SSSE3){
  886. if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
  887. c->try_8x8basis= try_8x8basis_ssse3;
  888. }
  889. c->add_8x8basis= add_8x8basis_ssse3;
  890. c->sum_abs_dctelem= sum_abs_dctelem_ssse3;
  891. }
  892. #endif
  893. if(mm_flags & AV_CPU_FLAG_3DNOW){
  894. if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
  895. c->try_8x8basis= try_8x8basis_3dnow;
  896. }
  897. c->add_8x8basis= add_8x8basis_3dnow;
  898. }
  899. }
  900. #endif /* HAVE_INLINE_ASM */
  901. if (EXTERNAL_MMX(mm_flags)) {
  902. c->hadamard8_diff[0] = ff_hadamard8_diff16_mmx;
  903. c->hadamard8_diff[1] = ff_hadamard8_diff_mmx;
  904. if (EXTERNAL_MMXEXT(mm_flags)) {
  905. c->hadamard8_diff[0] = ff_hadamard8_diff16_mmxext;
  906. c->hadamard8_diff[1] = ff_hadamard8_diff_mmxext;
  907. }
  908. if (EXTERNAL_SSE2(mm_flags)) {
  909. c->sse[0] = ff_sse16_sse2;
  910. #if HAVE_ALIGNED_STACK
  911. c->hadamard8_diff[0] = ff_hadamard8_diff16_sse2;
  912. c->hadamard8_diff[1] = ff_hadamard8_diff_sse2;
  913. #endif
  914. }
  915. if (EXTERNAL_SSSE3(mm_flags) && HAVE_ALIGNED_STACK) {
  916. c->hadamard8_diff[0] = ff_hadamard8_diff16_ssse3;
  917. c->hadamard8_diff[1] = ff_hadamard8_diff_ssse3;
  918. }
  919. }
  920. ff_dsputil_init_pix_mmx(c, avctx);
  921. }