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.

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