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.

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