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