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.

724 lines
22KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Optimized for ia32 cpus by Nick Kurshev <nickols_k@mail.ru>
  20. * h263, mpeg1, mpeg2 dequantizer & draw_edges by Michael Niedermayer <michaelni@gmx.at>
  21. */
  22. #include "../dsputil.h"
  23. #include "../mpegvideo.h"
  24. #include "../avcodec.h"
  25. #include "mmx.h"
  26. extern uint8_t zigzag_direct_noperm[64];
  27. extern uint16_t inv_zigzag_direct16[64];
  28. static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
  29. static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
  30. static void dct_unquantize_h263_intra_mmx(MpegEncContext *s,
  31. DCTELEM *block, int n, int qscale)
  32. {
  33. long 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. ".balign 16\n\t"
  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), "g"(qmul), "g" (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. long 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. ".balign 16\n\t"
  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), "g"(qmul), "g" (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 greate of 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. long 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. ".balign 16\n\t"
  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), "g" (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. long 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. ".balign 16\n\t"
  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), "g" (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. long 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. ".balign 16\n\t"
  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), "g" (qscale), "g" (-2*nCoeffs)
  346. : "%"REG_a, "memory"
  347. );
  348. block[0]= block0;
  349. //Note, we dont 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. long 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. ".balign 16\n\t"
  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), "g" (qscale), "r" (-2*nCoeffs)
  419. : "%"REG_a, "memory"
  420. );
  421. }
  422. /* draw the edges of width 'w' of an image of size width, height
  423. this mmx version can only handle w==8 || w==16 */
  424. static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w)
  425. {
  426. uint8_t *ptr, *last_line;
  427. int i;
  428. last_line = buf + (height - 1) * wrap;
  429. /* left and right */
  430. ptr = buf;
  431. if(w==8)
  432. {
  433. asm volatile(
  434. "1: \n\t"
  435. "movd (%0), %%mm0 \n\t"
  436. "punpcklbw %%mm0, %%mm0 \n\t"
  437. "punpcklwd %%mm0, %%mm0 \n\t"
  438. "punpckldq %%mm0, %%mm0 \n\t"
  439. "movq %%mm0, -8(%0) \n\t"
  440. "movq -8(%0, %2), %%mm1 \n\t"
  441. "punpckhbw %%mm1, %%mm1 \n\t"
  442. "punpckhwd %%mm1, %%mm1 \n\t"
  443. "punpckhdq %%mm1, %%mm1 \n\t"
  444. "movq %%mm1, (%0, %2) \n\t"
  445. "add %1, %0 \n\t"
  446. "cmp %3, %0 \n\t"
  447. " jb 1b \n\t"
  448. : "+r" (ptr)
  449. : "r" ((long)wrap), "r" ((long)width), "r" (ptr + wrap*height)
  450. );
  451. }
  452. else
  453. {
  454. asm volatile(
  455. "1: \n\t"
  456. "movd (%0), %%mm0 \n\t"
  457. "punpcklbw %%mm0, %%mm0 \n\t"
  458. "punpcklwd %%mm0, %%mm0 \n\t"
  459. "punpckldq %%mm0, %%mm0 \n\t"
  460. "movq %%mm0, -8(%0) \n\t"
  461. "movq %%mm0, -16(%0) \n\t"
  462. "movq -8(%0, %2), %%mm1 \n\t"
  463. "punpckhbw %%mm1, %%mm1 \n\t"
  464. "punpckhwd %%mm1, %%mm1 \n\t"
  465. "punpckhdq %%mm1, %%mm1 \n\t"
  466. "movq %%mm1, (%0, %2) \n\t"
  467. "movq %%mm1, 8(%0, %2) \n\t"
  468. "add %1, %0 \n\t"
  469. "cmp %3, %0 \n\t"
  470. " jb 1b \n\t"
  471. : "+r" (ptr)
  472. : "r" ((long)wrap), "r" ((long)width), "r" (ptr + wrap*height)
  473. );
  474. }
  475. for(i=0;i<w;i+=4) {
  476. /* top and bottom (and hopefully also the corners) */
  477. ptr= buf - (i + 1) * wrap - w;
  478. asm volatile(
  479. "1: \n\t"
  480. "movq (%1, %0), %%mm0 \n\t"
  481. "movq %%mm0, (%0) \n\t"
  482. "movq %%mm0, (%0, %2) \n\t"
  483. "movq %%mm0, (%0, %2, 2) \n\t"
  484. "movq %%mm0, (%0, %3) \n\t"
  485. "add $8, %0 \n\t"
  486. "cmp %4, %0 \n\t"
  487. " jb 1b \n\t"
  488. : "+r" (ptr)
  489. : "r" ((long)buf - (long)ptr - w), "r" ((long)-wrap), "r" ((long)-wrap*3), "r" (ptr+width+2*w)
  490. );
  491. ptr= last_line + (i + 1) * wrap - w;
  492. asm volatile(
  493. "1: \n\t"
  494. "movq (%1, %0), %%mm0 \n\t"
  495. "movq %%mm0, (%0) \n\t"
  496. "movq %%mm0, (%0, %2) \n\t"
  497. "movq %%mm0, (%0, %2, 2) \n\t"
  498. "movq %%mm0, (%0, %3) \n\t"
  499. "add $8, %0 \n\t"
  500. "cmp %4, %0 \n\t"
  501. " jb 1b \n\t"
  502. : "+r" (ptr)
  503. : "r" ((long)last_line - (long)ptr - w), "r" ((long)wrap), "r" ((long)wrap*3), "r" (ptr+width+2*w)
  504. );
  505. }
  506. }
  507. static void denoise_dct_mmx(MpegEncContext *s, DCTELEM *block){
  508. const int intra= s->mb_intra;
  509. int *sum= s->dct_error_sum[intra];
  510. uint16_t *offset= s->dct_offset[intra];
  511. s->dct_count[intra]++;
  512. asm volatile(
  513. "pxor %%mm7, %%mm7 \n\t"
  514. "1: \n\t"
  515. "pxor %%mm0, %%mm0 \n\t"
  516. "pxor %%mm1, %%mm1 \n\t"
  517. "movq (%0), %%mm2 \n\t"
  518. "movq 8(%0), %%mm3 \n\t"
  519. "pcmpgtw %%mm2, %%mm0 \n\t"
  520. "pcmpgtw %%mm3, %%mm1 \n\t"
  521. "pxor %%mm0, %%mm2 \n\t"
  522. "pxor %%mm1, %%mm3 \n\t"
  523. "psubw %%mm0, %%mm2 \n\t"
  524. "psubw %%mm1, %%mm3 \n\t"
  525. "movq %%mm2, %%mm4 \n\t"
  526. "movq %%mm3, %%mm5 \n\t"
  527. "psubusw (%2), %%mm2 \n\t"
  528. "psubusw 8(%2), %%mm3 \n\t"
  529. "pxor %%mm0, %%mm2 \n\t"
  530. "pxor %%mm1, %%mm3 \n\t"
  531. "psubw %%mm0, %%mm2 \n\t"
  532. "psubw %%mm1, %%mm3 \n\t"
  533. "movq %%mm2, (%0) \n\t"
  534. "movq %%mm3, 8(%0) \n\t"
  535. "movq %%mm4, %%mm2 \n\t"
  536. "movq %%mm5, %%mm3 \n\t"
  537. "punpcklwd %%mm7, %%mm4 \n\t"
  538. "punpckhwd %%mm7, %%mm2 \n\t"
  539. "punpcklwd %%mm7, %%mm5 \n\t"
  540. "punpckhwd %%mm7, %%mm3 \n\t"
  541. "paddd (%1), %%mm4 \n\t"
  542. "paddd 8(%1), %%mm2 \n\t"
  543. "paddd 16(%1), %%mm5 \n\t"
  544. "paddd 24(%1), %%mm3 \n\t"
  545. "movq %%mm4, (%1) \n\t"
  546. "movq %%mm2, 8(%1) \n\t"
  547. "movq %%mm5, 16(%1) \n\t"
  548. "movq %%mm3, 24(%1) \n\t"
  549. "add $16, %0 \n\t"
  550. "add $32, %1 \n\t"
  551. "add $16, %2 \n\t"
  552. "cmp %3, %0 \n\t"
  553. " jb 1b \n\t"
  554. : "+r" (block), "+r" (sum), "+r" (offset)
  555. : "r"(block+64)
  556. );
  557. }
  558. static void denoise_dct_sse2(MpegEncContext *s, DCTELEM *block){
  559. const int intra= s->mb_intra;
  560. int *sum= s->dct_error_sum[intra];
  561. uint16_t *offset= s->dct_offset[intra];
  562. s->dct_count[intra]++;
  563. asm volatile(
  564. "pxor %%xmm7, %%xmm7 \n\t"
  565. "1: \n\t"
  566. "pxor %%xmm0, %%xmm0 \n\t"
  567. "pxor %%xmm1, %%xmm1 \n\t"
  568. "movdqa (%0), %%xmm2 \n\t"
  569. "movdqa 16(%0), %%xmm3 \n\t"
  570. "pcmpgtw %%xmm2, %%xmm0 \n\t"
  571. "pcmpgtw %%xmm3, %%xmm1 \n\t"
  572. "pxor %%xmm0, %%xmm2 \n\t"
  573. "pxor %%xmm1, %%xmm3 \n\t"
  574. "psubw %%xmm0, %%xmm2 \n\t"
  575. "psubw %%xmm1, %%xmm3 \n\t"
  576. "movdqa %%xmm2, %%xmm4 \n\t"
  577. "movdqa %%xmm3, %%xmm5 \n\t"
  578. "psubusw (%2), %%xmm2 \n\t"
  579. "psubusw 16(%2), %%xmm3 \n\t"
  580. "pxor %%xmm0, %%xmm2 \n\t"
  581. "pxor %%xmm1, %%xmm3 \n\t"
  582. "psubw %%xmm0, %%xmm2 \n\t"
  583. "psubw %%xmm1, %%xmm3 \n\t"
  584. "movdqa %%xmm2, (%0) \n\t"
  585. "movdqa %%xmm3, 16(%0) \n\t"
  586. "movdqa %%xmm4, %%xmm6 \n\t"
  587. "movdqa %%xmm5, %%xmm0 \n\t"
  588. "punpcklwd %%xmm7, %%xmm4 \n\t"
  589. "punpckhwd %%xmm7, %%xmm6 \n\t"
  590. "punpcklwd %%xmm7, %%xmm5 \n\t"
  591. "punpckhwd %%xmm7, %%xmm0 \n\t"
  592. "paddd (%1), %%xmm4 \n\t"
  593. "paddd 16(%1), %%xmm6 \n\t"
  594. "paddd 32(%1), %%xmm5 \n\t"
  595. "paddd 48(%1), %%xmm0 \n\t"
  596. "movdqa %%xmm4, (%1) \n\t"
  597. "movdqa %%xmm6, 16(%1) \n\t"
  598. "movdqa %%xmm5, 32(%1) \n\t"
  599. "movdqa %%xmm0, 48(%1) \n\t"
  600. "add $32, %0 \n\t"
  601. "add $64, %1 \n\t"
  602. "add $32, %2 \n\t"
  603. "cmp %3, %0 \n\t"
  604. " jb 1b \n\t"
  605. : "+r" (block), "+r" (sum), "+r" (offset)
  606. : "r"(block+64)
  607. );
  608. }
  609. #undef HAVE_MMX2
  610. #define RENAME(a) a ## _MMX
  611. #define RENAMEl(a) a ## _mmx
  612. #include "mpegvideo_mmx_template.c"
  613. #define HAVE_MMX2
  614. #undef RENAME
  615. #undef RENAMEl
  616. #define RENAME(a) a ## _MMX2
  617. #define RENAMEl(a) a ## _mmx2
  618. #include "mpegvideo_mmx_template.c"
  619. #undef RENAME
  620. #undef RENAMEl
  621. #define RENAME(a) a ## _SSE2
  622. #define RENAMEl(a) a ## _sse2
  623. #include "mpegvideo_mmx_template.c"
  624. void MPV_common_init_mmx(MpegEncContext *s)
  625. {
  626. if (mm_flags & MM_MMX) {
  627. const int dct_algo = s->avctx->dct_algo;
  628. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_mmx;
  629. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_mmx;
  630. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_mmx;
  631. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_mmx;
  632. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_mmx;
  633. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_mmx;
  634. draw_edges = draw_edges_mmx;
  635. if (mm_flags & MM_SSE2) {
  636. s->denoise_dct= denoise_dct_sse2;
  637. } else {
  638. s->denoise_dct= denoise_dct_mmx;
  639. }
  640. if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
  641. if(mm_flags & MM_SSE2){
  642. s->dct_quantize= dct_quantize_SSE2;
  643. } else if(mm_flags & MM_MMXEXT){
  644. s->dct_quantize= dct_quantize_MMX2;
  645. } else {
  646. s->dct_quantize= dct_quantize_MMX;
  647. }
  648. }
  649. }
  650. }