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.

672 lines
28KB

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