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.

609 lines
27KB

  1. /*
  2. * Optimized for ia32 CPUs by Nick Kurshev <nickols_k@mail.ru>
  3. * h263, mpeg1, mpeg2 dequantizer & draw_edges by Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/x86/asm.h"
  23. #include "libavcodec/avcodec.h"
  24. #include "libavcodec/dsputil.h"
  25. #include "libavcodec/mpegvideo.h"
  26. #include "dsputil_mmx.h"
  27. #if HAVE_INLINE_ASM
  28. static void dct_unquantize_h263_intra_mmx(MpegEncContext *s,
  29. DCTELEM *block, int n, int qscale)
  30. {
  31. x86_reg level, qmul, qadd, nCoeffs;
  32. qmul = qscale << 1;
  33. assert(s->block_last_index[n]>=0 || s->h263_aic);
  34. if (!s->h263_aic) {
  35. if (n < 4)
  36. level = block[0] * s->y_dc_scale;
  37. else
  38. level = block[0] * s->c_dc_scale;
  39. qadd = (qscale - 1) | 1;
  40. }else{
  41. qadd = 0;
  42. level= block[0];
  43. }
  44. if(s->ac_pred)
  45. nCoeffs=63;
  46. else
  47. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  48. __asm__ volatile(
  49. "movd %1, %%mm6 \n\t" //qmul
  50. "packssdw %%mm6, %%mm6 \n\t"
  51. "packssdw %%mm6, %%mm6 \n\t"
  52. "movd %2, %%mm5 \n\t" //qadd
  53. "pxor %%mm7, %%mm7 \n\t"
  54. "packssdw %%mm5, %%mm5 \n\t"
  55. "packssdw %%mm5, %%mm5 \n\t"
  56. "psubw %%mm5, %%mm7 \n\t"
  57. "pxor %%mm4, %%mm4 \n\t"
  58. ".p2align 4 \n\t"
  59. "1: \n\t"
  60. "movq (%0, %3), %%mm0 \n\t"
  61. "movq 8(%0, %3), %%mm1 \n\t"
  62. "pmullw %%mm6, %%mm0 \n\t"
  63. "pmullw %%mm6, %%mm1 \n\t"
  64. "movq (%0, %3), %%mm2 \n\t"
  65. "movq 8(%0, %3), %%mm3 \n\t"
  66. "pcmpgtw %%mm4, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
  67. "pcmpgtw %%mm4, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
  68. "pxor %%mm2, %%mm0 \n\t"
  69. "pxor %%mm3, %%mm1 \n\t"
  70. "paddw %%mm7, %%mm0 \n\t"
  71. "paddw %%mm7, %%mm1 \n\t"
  72. "pxor %%mm0, %%mm2 \n\t"
  73. "pxor %%mm1, %%mm3 \n\t"
  74. "pcmpeqw %%mm7, %%mm0 \n\t" // block[i] == 0 ? -1 : 0
  75. "pcmpeqw %%mm7, %%mm1 \n\t" // block[i] == 0 ? -1 : 0
  76. "pandn %%mm2, %%mm0 \n\t"
  77. "pandn %%mm3, %%mm1 \n\t"
  78. "movq %%mm0, (%0, %3) \n\t"
  79. "movq %%mm1, 8(%0, %3) \n\t"
  80. "add $16, %3 \n\t"
  81. "jng 1b \n\t"
  82. ::"r" (block+nCoeffs), "rm"(qmul), "rm" (qadd), "r" (2*(-nCoeffs))
  83. : "memory"
  84. );
  85. block[0]= level;
  86. }
  87. static void dct_unquantize_h263_inter_mmx(MpegEncContext *s,
  88. DCTELEM *block, int n, int qscale)
  89. {
  90. x86_reg qmul, qadd, nCoeffs;
  91. qmul = qscale << 1;
  92. qadd = (qscale - 1) | 1;
  93. assert(s->block_last_index[n]>=0 || s->h263_aic);
  94. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  95. __asm__ volatile(
  96. "movd %1, %%mm6 \n\t" //qmul
  97. "packssdw %%mm6, %%mm6 \n\t"
  98. "packssdw %%mm6, %%mm6 \n\t"
  99. "movd %2, %%mm5 \n\t" //qadd
  100. "pxor %%mm7, %%mm7 \n\t"
  101. "packssdw %%mm5, %%mm5 \n\t"
  102. "packssdw %%mm5, %%mm5 \n\t"
  103. "psubw %%mm5, %%mm7 \n\t"
  104. "pxor %%mm4, %%mm4 \n\t"
  105. ".p2align 4 \n\t"
  106. "1: \n\t"
  107. "movq (%0, %3), %%mm0 \n\t"
  108. "movq 8(%0, %3), %%mm1 \n\t"
  109. "pmullw %%mm6, %%mm0 \n\t"
  110. "pmullw %%mm6, %%mm1 \n\t"
  111. "movq (%0, %3), %%mm2 \n\t"
  112. "movq 8(%0, %3), %%mm3 \n\t"
  113. "pcmpgtw %%mm4, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
  114. "pcmpgtw %%mm4, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
  115. "pxor %%mm2, %%mm0 \n\t"
  116. "pxor %%mm3, %%mm1 \n\t"
  117. "paddw %%mm7, %%mm0 \n\t"
  118. "paddw %%mm7, %%mm1 \n\t"
  119. "pxor %%mm0, %%mm2 \n\t"
  120. "pxor %%mm1, %%mm3 \n\t"
  121. "pcmpeqw %%mm7, %%mm0 \n\t" // block[i] == 0 ? -1 : 0
  122. "pcmpeqw %%mm7, %%mm1 \n\t" // block[i] == 0 ? -1 : 0
  123. "pandn %%mm2, %%mm0 \n\t"
  124. "pandn %%mm3, %%mm1 \n\t"
  125. "movq %%mm0, (%0, %3) \n\t"
  126. "movq %%mm1, 8(%0, %3) \n\t"
  127. "add $16, %3 \n\t"
  128. "jng 1b \n\t"
  129. ::"r" (block+nCoeffs), "rm"(qmul), "rm" (qadd), "r" (2*(-nCoeffs))
  130. : "memory"
  131. );
  132. }
  133. /*
  134. NK:
  135. Note: looking at PARANOID:
  136. "enable all paranoid tests for rounding, overflows, etc..."
  137. #ifdef PARANOID
  138. if (level < -2048 || level > 2047)
  139. fprintf(stderr, "unquant error %d %d\n", i, level);
  140. #endif
  141. We can suppose that result of two multiplications can't be greater than 0xFFFF
  142. i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
  143. a complex multiplication.
  144. =====================================================
  145. Full formula for multiplication of 2 integer numbers
  146. which are represent as high:low words:
  147. input: value1 = high1:low1
  148. value2 = high2:low2
  149. output: value3 = value1*value2
  150. value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
  151. this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
  152. but this algorithm will compute only 0x66cb0ce4
  153. this limited by 16-bit size of operands
  154. ---------------------------------
  155. tlow1 = high1*low2
  156. tlow2 = high2*low1
  157. tlow1 = tlow1 + tlow2
  158. high3:low3 = low1*low2
  159. high3 += tlow1
  160. */
  161. static void dct_unquantize_mpeg1_intra_mmx(MpegEncContext *s,
  162. DCTELEM *block, int n, int qscale)
  163. {
  164. x86_reg nCoeffs;
  165. const uint16_t *quant_matrix;
  166. int block0;
  167. assert(s->block_last_index[n]>=0);
  168. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
  169. if (n < 4)
  170. block0 = block[0] * s->y_dc_scale;
  171. else
  172. block0 = block[0] * s->c_dc_scale;
  173. /* XXX: only mpeg1 */
  174. quant_matrix = s->intra_matrix;
  175. __asm__ volatile(
  176. "pcmpeqw %%mm7, %%mm7 \n\t"
  177. "psrlw $15, %%mm7 \n\t"
  178. "movd %2, %%mm6 \n\t"
  179. "packssdw %%mm6, %%mm6 \n\t"
  180. "packssdw %%mm6, %%mm6 \n\t"
  181. "mov %3, %%"REG_a" \n\t"
  182. ".p2align 4 \n\t"
  183. "1: \n\t"
  184. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  185. "movq 8(%0, %%"REG_a"), %%mm1 \n\t"
  186. "movq (%1, %%"REG_a"), %%mm4 \n\t"
  187. "movq 8(%1, %%"REG_a"), %%mm5 \n\t"
  188. "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
  189. "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
  190. "pxor %%mm2, %%mm2 \n\t"
  191. "pxor %%mm3, %%mm3 \n\t"
  192. "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
  193. "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
  194. "pxor %%mm2, %%mm0 \n\t"
  195. "pxor %%mm3, %%mm1 \n\t"
  196. "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
  197. "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
  198. "pmullw %%mm4, %%mm0 \n\t" // abs(block[i])*q
  199. "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*q
  200. "pxor %%mm4, %%mm4 \n\t"
  201. "pxor %%mm5, %%mm5 \n\t" // FIXME slow
  202. "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
  203. "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
  204. "psraw $3, %%mm0 \n\t"
  205. "psraw $3, %%mm1 \n\t"
  206. "psubw %%mm7, %%mm0 \n\t"
  207. "psubw %%mm7, %%mm1 \n\t"
  208. "por %%mm7, %%mm0 \n\t"
  209. "por %%mm7, %%mm1 \n\t"
  210. "pxor %%mm2, %%mm0 \n\t"
  211. "pxor %%mm3, %%mm1 \n\t"
  212. "psubw %%mm2, %%mm0 \n\t"
  213. "psubw %%mm3, %%mm1 \n\t"
  214. "pandn %%mm0, %%mm4 \n\t"
  215. "pandn %%mm1, %%mm5 \n\t"
  216. "movq %%mm4, (%0, %%"REG_a") \n\t"
  217. "movq %%mm5, 8(%0, %%"REG_a") \n\t"
  218. "add $16, %%"REG_a" \n\t"
  219. "js 1b \n\t"
  220. ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
  221. : "%"REG_a, "memory"
  222. );
  223. block[0]= block0;
  224. }
  225. static void dct_unquantize_mpeg1_inter_mmx(MpegEncContext *s,
  226. DCTELEM *block, int n, int qscale)
  227. {
  228. x86_reg nCoeffs;
  229. const uint16_t *quant_matrix;
  230. assert(s->block_last_index[n]>=0);
  231. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
  232. quant_matrix = s->inter_matrix;
  233. __asm__ volatile(
  234. "pcmpeqw %%mm7, %%mm7 \n\t"
  235. "psrlw $15, %%mm7 \n\t"
  236. "movd %2, %%mm6 \n\t"
  237. "packssdw %%mm6, %%mm6 \n\t"
  238. "packssdw %%mm6, %%mm6 \n\t"
  239. "mov %3, %%"REG_a" \n\t"
  240. ".p2align 4 \n\t"
  241. "1: \n\t"
  242. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  243. "movq 8(%0, %%"REG_a"), %%mm1 \n\t"
  244. "movq (%1, %%"REG_a"), %%mm4 \n\t"
  245. "movq 8(%1, %%"REG_a"), %%mm5 \n\t"
  246. "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
  247. "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
  248. "pxor %%mm2, %%mm2 \n\t"
  249. "pxor %%mm3, %%mm3 \n\t"
  250. "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
  251. "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
  252. "pxor %%mm2, %%mm0 \n\t"
  253. "pxor %%mm3, %%mm1 \n\t"
  254. "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
  255. "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
  256. "paddw %%mm0, %%mm0 \n\t" // abs(block[i])*2
  257. "paddw %%mm1, %%mm1 \n\t" // abs(block[i])*2
  258. "paddw %%mm7, %%mm0 \n\t" // abs(block[i])*2 + 1
  259. "paddw %%mm7, %%mm1 \n\t" // abs(block[i])*2 + 1
  260. "pmullw %%mm4, %%mm0 \n\t" // (abs(block[i])*2 + 1)*q
  261. "pmullw %%mm5, %%mm1 \n\t" // (abs(block[i])*2 + 1)*q
  262. "pxor %%mm4, %%mm4 \n\t"
  263. "pxor %%mm5, %%mm5 \n\t" // FIXME slow
  264. "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
  265. "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
  266. "psraw $4, %%mm0 \n\t"
  267. "psraw $4, %%mm1 \n\t"
  268. "psubw %%mm7, %%mm0 \n\t"
  269. "psubw %%mm7, %%mm1 \n\t"
  270. "por %%mm7, %%mm0 \n\t"
  271. "por %%mm7, %%mm1 \n\t"
  272. "pxor %%mm2, %%mm0 \n\t"
  273. "pxor %%mm3, %%mm1 \n\t"
  274. "psubw %%mm2, %%mm0 \n\t"
  275. "psubw %%mm3, %%mm1 \n\t"
  276. "pandn %%mm0, %%mm4 \n\t"
  277. "pandn %%mm1, %%mm5 \n\t"
  278. "movq %%mm4, (%0, %%"REG_a") \n\t"
  279. "movq %%mm5, 8(%0, %%"REG_a") \n\t"
  280. "add $16, %%"REG_a" \n\t"
  281. "js 1b \n\t"
  282. ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
  283. : "%"REG_a, "memory"
  284. );
  285. }
  286. static void dct_unquantize_mpeg2_intra_mmx(MpegEncContext *s,
  287. DCTELEM *block, int n, int qscale)
  288. {
  289. x86_reg nCoeffs;
  290. const uint16_t *quant_matrix;
  291. int block0;
  292. assert(s->block_last_index[n]>=0);
  293. if(s->alternate_scan) nCoeffs= 63; //FIXME
  294. else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  295. if (n < 4)
  296. block0 = block[0] * s->y_dc_scale;
  297. else
  298. block0 = block[0] * s->c_dc_scale;
  299. quant_matrix = s->intra_matrix;
  300. __asm__ volatile(
  301. "pcmpeqw %%mm7, %%mm7 \n\t"
  302. "psrlw $15, %%mm7 \n\t"
  303. "movd %2, %%mm6 \n\t"
  304. "packssdw %%mm6, %%mm6 \n\t"
  305. "packssdw %%mm6, %%mm6 \n\t"
  306. "mov %3, %%"REG_a" \n\t"
  307. ".p2align 4 \n\t"
  308. "1: \n\t"
  309. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  310. "movq 8(%0, %%"REG_a"), %%mm1 \n\t"
  311. "movq (%1, %%"REG_a"), %%mm4 \n\t"
  312. "movq 8(%1, %%"REG_a"), %%mm5 \n\t"
  313. "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
  314. "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
  315. "pxor %%mm2, %%mm2 \n\t"
  316. "pxor %%mm3, %%mm3 \n\t"
  317. "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
  318. "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
  319. "pxor %%mm2, %%mm0 \n\t"
  320. "pxor %%mm3, %%mm1 \n\t"
  321. "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
  322. "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
  323. "pmullw %%mm4, %%mm0 \n\t" // abs(block[i])*q
  324. "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*q
  325. "pxor %%mm4, %%mm4 \n\t"
  326. "pxor %%mm5, %%mm5 \n\t" // FIXME slow
  327. "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
  328. "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
  329. "psraw $3, %%mm0 \n\t"
  330. "psraw $3, %%mm1 \n\t"
  331. "pxor %%mm2, %%mm0 \n\t"
  332. "pxor %%mm3, %%mm1 \n\t"
  333. "psubw %%mm2, %%mm0 \n\t"
  334. "psubw %%mm3, %%mm1 \n\t"
  335. "pandn %%mm0, %%mm4 \n\t"
  336. "pandn %%mm1, %%mm5 \n\t"
  337. "movq %%mm4, (%0, %%"REG_a") \n\t"
  338. "movq %%mm5, 8(%0, %%"REG_a") \n\t"
  339. "add $16, %%"REG_a" \n\t"
  340. "jng 1b \n\t"
  341. ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
  342. : "%"REG_a, "memory"
  343. );
  344. block[0]= block0;
  345. //Note, we do not do mismatch control for intra as errors cannot accumulate
  346. }
  347. static void dct_unquantize_mpeg2_inter_mmx(MpegEncContext *s,
  348. DCTELEM *block, int n, int qscale)
  349. {
  350. x86_reg nCoeffs;
  351. const uint16_t *quant_matrix;
  352. assert(s->block_last_index[n]>=0);
  353. if(s->alternate_scan) nCoeffs= 63; //FIXME
  354. else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  355. quant_matrix = s->inter_matrix;
  356. __asm__ volatile(
  357. "pcmpeqw %%mm7, %%mm7 \n\t"
  358. "psrlq $48, %%mm7 \n\t"
  359. "movd %2, %%mm6 \n\t"
  360. "packssdw %%mm6, %%mm6 \n\t"
  361. "packssdw %%mm6, %%mm6 \n\t"
  362. "mov %3, %%"REG_a" \n\t"
  363. ".p2align 4 \n\t"
  364. "1: \n\t"
  365. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  366. "movq 8(%0, %%"REG_a"), %%mm1 \n\t"
  367. "movq (%1, %%"REG_a"), %%mm4 \n\t"
  368. "movq 8(%1, %%"REG_a"), %%mm5 \n\t"
  369. "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
  370. "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
  371. "pxor %%mm2, %%mm2 \n\t"
  372. "pxor %%mm3, %%mm3 \n\t"
  373. "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
  374. "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
  375. "pxor %%mm2, %%mm0 \n\t"
  376. "pxor %%mm3, %%mm1 \n\t"
  377. "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
  378. "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
  379. "paddw %%mm0, %%mm0 \n\t" // abs(block[i])*2
  380. "paddw %%mm1, %%mm1 \n\t" // abs(block[i])*2
  381. "pmullw %%mm4, %%mm0 \n\t" // abs(block[i])*2*q
  382. "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*2*q
  383. "paddw %%mm4, %%mm0 \n\t" // (abs(block[i])*2 + 1)*q
  384. "paddw %%mm5, %%mm1 \n\t" // (abs(block[i])*2 + 1)*q
  385. "pxor %%mm4, %%mm4 \n\t"
  386. "pxor %%mm5, %%mm5 \n\t" // FIXME slow
  387. "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
  388. "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
  389. "psrlw $4, %%mm0 \n\t"
  390. "psrlw $4, %%mm1 \n\t"
  391. "pxor %%mm2, %%mm0 \n\t"
  392. "pxor %%mm3, %%mm1 \n\t"
  393. "psubw %%mm2, %%mm0 \n\t"
  394. "psubw %%mm3, %%mm1 \n\t"
  395. "pandn %%mm0, %%mm4 \n\t"
  396. "pandn %%mm1, %%mm5 \n\t"
  397. "pxor %%mm4, %%mm7 \n\t"
  398. "pxor %%mm5, %%mm7 \n\t"
  399. "movq %%mm4, (%0, %%"REG_a") \n\t"
  400. "movq %%mm5, 8(%0, %%"REG_a") \n\t"
  401. "add $16, %%"REG_a" \n\t"
  402. "jng 1b \n\t"
  403. "movd 124(%0, %3), %%mm0 \n\t"
  404. "movq %%mm7, %%mm6 \n\t"
  405. "psrlq $32, %%mm7 \n\t"
  406. "pxor %%mm6, %%mm7 \n\t"
  407. "movq %%mm7, %%mm6 \n\t"
  408. "psrlq $16, %%mm7 \n\t"
  409. "pxor %%mm6, %%mm7 \n\t"
  410. "pslld $31, %%mm7 \n\t"
  411. "psrlq $15, %%mm7 \n\t"
  412. "pxor %%mm7, %%mm0 \n\t"
  413. "movd %%mm0, 124(%0, %3) \n\t"
  414. ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "r" (-2*nCoeffs)
  415. : "%"REG_a, "memory"
  416. );
  417. }
  418. static void denoise_dct_mmx(MpegEncContext *s, DCTELEM *block){
  419. const int intra= s->mb_intra;
  420. int *sum= s->dct_error_sum[intra];
  421. uint16_t *offset= s->dct_offset[intra];
  422. s->dct_count[intra]++;
  423. __asm__ volatile(
  424. "pxor %%mm7, %%mm7 \n\t"
  425. "1: \n\t"
  426. "pxor %%mm0, %%mm0 \n\t"
  427. "pxor %%mm1, %%mm1 \n\t"
  428. "movq (%0), %%mm2 \n\t"
  429. "movq 8(%0), %%mm3 \n\t"
  430. "pcmpgtw %%mm2, %%mm0 \n\t"
  431. "pcmpgtw %%mm3, %%mm1 \n\t"
  432. "pxor %%mm0, %%mm2 \n\t"
  433. "pxor %%mm1, %%mm3 \n\t"
  434. "psubw %%mm0, %%mm2 \n\t"
  435. "psubw %%mm1, %%mm3 \n\t"
  436. "movq %%mm2, %%mm4 \n\t"
  437. "movq %%mm3, %%mm5 \n\t"
  438. "psubusw (%2), %%mm2 \n\t"
  439. "psubusw 8(%2), %%mm3 \n\t"
  440. "pxor %%mm0, %%mm2 \n\t"
  441. "pxor %%mm1, %%mm3 \n\t"
  442. "psubw %%mm0, %%mm2 \n\t"
  443. "psubw %%mm1, %%mm3 \n\t"
  444. "movq %%mm2, (%0) \n\t"
  445. "movq %%mm3, 8(%0) \n\t"
  446. "movq %%mm4, %%mm2 \n\t"
  447. "movq %%mm5, %%mm3 \n\t"
  448. "punpcklwd %%mm7, %%mm4 \n\t"
  449. "punpckhwd %%mm7, %%mm2 \n\t"
  450. "punpcklwd %%mm7, %%mm5 \n\t"
  451. "punpckhwd %%mm7, %%mm3 \n\t"
  452. "paddd (%1), %%mm4 \n\t"
  453. "paddd 8(%1), %%mm2 \n\t"
  454. "paddd 16(%1), %%mm5 \n\t"
  455. "paddd 24(%1), %%mm3 \n\t"
  456. "movq %%mm4, (%1) \n\t"
  457. "movq %%mm2, 8(%1) \n\t"
  458. "movq %%mm5, 16(%1) \n\t"
  459. "movq %%mm3, 24(%1) \n\t"
  460. "add $16, %0 \n\t"
  461. "add $32, %1 \n\t"
  462. "add $16, %2 \n\t"
  463. "cmp %3, %0 \n\t"
  464. " jb 1b \n\t"
  465. : "+r" (block), "+r" (sum), "+r" (offset)
  466. : "r"(block+64)
  467. );
  468. }
  469. static void denoise_dct_sse2(MpegEncContext *s, DCTELEM *block){
  470. const int intra= s->mb_intra;
  471. int *sum= s->dct_error_sum[intra];
  472. uint16_t *offset= s->dct_offset[intra];
  473. s->dct_count[intra]++;
  474. __asm__ volatile(
  475. "pxor %%xmm7, %%xmm7 \n\t"
  476. "1: \n\t"
  477. "pxor %%xmm0, %%xmm0 \n\t"
  478. "pxor %%xmm1, %%xmm1 \n\t"
  479. "movdqa (%0), %%xmm2 \n\t"
  480. "movdqa 16(%0), %%xmm3 \n\t"
  481. "pcmpgtw %%xmm2, %%xmm0 \n\t"
  482. "pcmpgtw %%xmm3, %%xmm1 \n\t"
  483. "pxor %%xmm0, %%xmm2 \n\t"
  484. "pxor %%xmm1, %%xmm3 \n\t"
  485. "psubw %%xmm0, %%xmm2 \n\t"
  486. "psubw %%xmm1, %%xmm3 \n\t"
  487. "movdqa %%xmm2, %%xmm4 \n\t"
  488. "movdqa %%xmm3, %%xmm5 \n\t"
  489. "psubusw (%2), %%xmm2 \n\t"
  490. "psubusw 16(%2), %%xmm3 \n\t"
  491. "pxor %%xmm0, %%xmm2 \n\t"
  492. "pxor %%xmm1, %%xmm3 \n\t"
  493. "psubw %%xmm0, %%xmm2 \n\t"
  494. "psubw %%xmm1, %%xmm3 \n\t"
  495. "movdqa %%xmm2, (%0) \n\t"
  496. "movdqa %%xmm3, 16(%0) \n\t"
  497. "movdqa %%xmm4, %%xmm6 \n\t"
  498. "movdqa %%xmm5, %%xmm0 \n\t"
  499. "punpcklwd %%xmm7, %%xmm4 \n\t"
  500. "punpckhwd %%xmm7, %%xmm6 \n\t"
  501. "punpcklwd %%xmm7, %%xmm5 \n\t"
  502. "punpckhwd %%xmm7, %%xmm0 \n\t"
  503. "paddd (%1), %%xmm4 \n\t"
  504. "paddd 16(%1), %%xmm6 \n\t"
  505. "paddd 32(%1), %%xmm5 \n\t"
  506. "paddd 48(%1), %%xmm0 \n\t"
  507. "movdqa %%xmm4, (%1) \n\t"
  508. "movdqa %%xmm6, 16(%1) \n\t"
  509. "movdqa %%xmm5, 32(%1) \n\t"
  510. "movdqa %%xmm0, 48(%1) \n\t"
  511. "add $32, %0 \n\t"
  512. "add $64, %1 \n\t"
  513. "add $32, %2 \n\t"
  514. "cmp %3, %0 \n\t"
  515. " jb 1b \n\t"
  516. : "+r" (block), "+r" (sum), "+r" (offset)
  517. : "r"(block+64)
  518. XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
  519. "%xmm4", "%xmm5", "%xmm6", "%xmm7")
  520. );
  521. }
  522. #endif /* HAVE_INLINE_ASM */
  523. void ff_MPV_common_init_x86(MpegEncContext *s)
  524. {
  525. #if HAVE_INLINE_ASM
  526. int mm_flags = av_get_cpu_flags();
  527. if (mm_flags & AV_CPU_FLAG_MMX) {
  528. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_mmx;
  529. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_mmx;
  530. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_mmx;
  531. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_mmx;
  532. if(!(s->flags & CODEC_FLAG_BITEXACT))
  533. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_mmx;
  534. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_mmx;
  535. if (mm_flags & AV_CPU_FLAG_SSE2) {
  536. s->denoise_dct= denoise_dct_sse2;
  537. } else {
  538. s->denoise_dct= denoise_dct_mmx;
  539. }
  540. }
  541. #endif /* HAVE_INLINE_ASM */
  542. }