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.

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