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.

602 lines
26KB

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