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.

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