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.

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