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.

727 lines
31KB

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