vf_ilpack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * This file is part of MPlayer.
  3. *
  4. * MPlayer is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * MPlayer is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <inttypes.h>
  22. #include "config.h"
  23. #include "mp_msg.h"
  24. #include "cpudetect.h"
  25. #include "img_format.h"
  26. #include "mp_image.h"
  27. #include "vf.h"
  28. typedef void (pack_func_t)(unsigned char *dst, unsigned char *y,
  29. unsigned char *u, unsigned char *v, int w, int us, int vs);
  30. struct vf_priv_s {
  31. int mode;
  32. pack_func_t *pack[2];
  33. };
  34. static void pack_nn_C(unsigned char *dst, unsigned char *y,
  35. unsigned char *u, unsigned char *v, int w)
  36. {
  37. int j;
  38. for (j = w/2; j; j--) {
  39. *dst++ = *y++;
  40. *dst++ = *u++;
  41. *dst++ = *y++;
  42. *dst++ = *v++;
  43. }
  44. }
  45. static void pack_li_0_C(unsigned char *dst, unsigned char *y,
  46. unsigned char *u, unsigned char *v, int w, int us, int vs)
  47. {
  48. int j;
  49. for (j = w/2; j; j--) {
  50. *dst++ = *y++;
  51. *dst++ = (u[us+us] + 7*u[0])>>3;
  52. *dst++ = *y++;
  53. *dst++ = (v[vs+vs] + 7*v[0])>>3;
  54. u++; v++;
  55. }
  56. }
  57. static void pack_li_1_C(unsigned char *dst, unsigned char *y,
  58. unsigned char *u, unsigned char *v, int w, int us, int vs)
  59. {
  60. int j;
  61. for (j = w/2; j; j--) {
  62. *dst++ = *y++;
  63. *dst++ = (3*u[us+us] + 5*u[0])>>3;
  64. *dst++ = *y++;
  65. *dst++ = (3*v[vs+vs] + 5*v[0])>>3;
  66. u++; v++;
  67. }
  68. }
  69. #if HAVE_MMX
  70. static void pack_nn_MMX(unsigned char *dst, unsigned char *y,
  71. unsigned char *u, unsigned char *v, int w)
  72. {
  73. __asm__ volatile (""
  74. ASMALIGN(4)
  75. "1: \n\t"
  76. "movq (%0), %%mm1 \n\t"
  77. "movq (%0), %%mm2 \n\t"
  78. "movq (%1), %%mm4 \n\t"
  79. "movq (%2), %%mm6 \n\t"
  80. "punpcklbw %%mm6, %%mm4 \n\t"
  81. "punpcklbw %%mm4, %%mm1 \n\t"
  82. "punpckhbw %%mm4, %%mm2 \n\t"
  83. "add $8, %0 \n\t"
  84. "add $4, %1 \n\t"
  85. "add $4, %2 \n\t"
  86. "movq %%mm1, (%3) \n\t"
  87. "movq %%mm2, 8(%3) \n\t"
  88. "add $16, %3 \n\t"
  89. "decl %4 \n\t"
  90. "jnz 1b \n\t"
  91. "emms \n\t"
  92. :
  93. : "r" (y), "r" (u), "r" (v), "r" (dst), "r" (w/8)
  94. : "memory"
  95. );
  96. pack_nn_C(dst, y, u, v, (w&7));
  97. }
  98. #if HAVE_EBX_AVAILABLE
  99. static void pack_li_0_MMX(unsigned char *dst, unsigned char *y,
  100. unsigned char *u, unsigned char *v, int w, int us, int vs)
  101. {
  102. __asm__ volatile (""
  103. "push %%"REG_BP" \n\t"
  104. #if ARCH_X86_64
  105. "mov %6, %%"REG_BP" \n\t"
  106. #else
  107. "movl 4(%%"REG_d"), %%"REG_BP" \n\t"
  108. "movl (%%"REG_d"), %%"REG_d" \n\t"
  109. #endif
  110. "pxor %%mm0, %%mm0 \n\t"
  111. ASMALIGN(4)
  112. ".Lli0: \n\t"
  113. "movq (%%"REG_S"), %%mm1 \n\t"
  114. "movq (%%"REG_S"), %%mm2 \n\t"
  115. "movq (%%"REG_a",%%"REG_d",2), %%mm4 \n\t"
  116. "movq (%%"REG_b",%%"REG_BP",2), %%mm6 \n\t"
  117. "punpcklbw %%mm0, %%mm4 \n\t"
  118. "punpcklbw %%mm0, %%mm6 \n\t"
  119. "movq (%%"REG_a"), %%mm3 \n\t"
  120. "movq (%%"REG_b"), %%mm5 \n\t"
  121. "punpcklbw %%mm0, %%mm3 \n\t"
  122. "punpcklbw %%mm0, %%mm5 \n\t"
  123. "paddw %%mm3, %%mm4 \n\t"
  124. "paddw %%mm5, %%mm6 \n\t"
  125. "paddw %%mm3, %%mm4 \n\t"
  126. "paddw %%mm5, %%mm6 \n\t"
  127. "paddw %%mm3, %%mm4 \n\t"
  128. "paddw %%mm5, %%mm6 \n\t"
  129. "paddw %%mm3, %%mm4 \n\t"
  130. "paddw %%mm5, %%mm6 \n\t"
  131. "paddw %%mm3, %%mm4 \n\t"
  132. "paddw %%mm5, %%mm6 \n\t"
  133. "paddw %%mm3, %%mm4 \n\t"
  134. "paddw %%mm5, %%mm6 \n\t"
  135. "paddw %%mm3, %%mm4 \n\t"
  136. "paddw %%mm5, %%mm6 \n\t"
  137. "psrlw $3, %%mm4 \n\t"
  138. "psrlw $3, %%mm6 \n\t"
  139. "packuswb %%mm4, %%mm4 \n\t"
  140. "packuswb %%mm6, %%mm6 \n\t"
  141. "punpcklbw %%mm6, %%mm4 \n\t"
  142. "punpcklbw %%mm4, %%mm1 \n\t"
  143. "punpckhbw %%mm4, %%mm2 \n\t"
  144. "movq %%mm1, (%%"REG_D") \n\t"
  145. "movq %%mm2, 8(%%"REG_D") \n\t"
  146. "movq 8(%%"REG_S"), %%mm1 \n\t"
  147. "movq 8(%%"REG_S"), %%mm2 \n\t"
  148. "movq (%%"REG_a",%%"REG_d",2), %%mm4 \n\t"
  149. "movq (%%"REG_b",%%"REG_BP",2), %%mm6 \n\t"
  150. "punpckhbw %%mm0, %%mm4 \n\t"
  151. "punpckhbw %%mm0, %%mm6 \n\t"
  152. "movq (%%"REG_a"), %%mm3 \n\t"
  153. "movq (%%"REG_b"), %%mm5 \n\t"
  154. "punpckhbw %%mm0, %%mm3 \n\t"
  155. "punpckhbw %%mm0, %%mm5 \n\t"
  156. "paddw %%mm3, %%mm4 \n\t"
  157. "paddw %%mm5, %%mm6 \n\t"
  158. "paddw %%mm3, %%mm4 \n\t"
  159. "paddw %%mm5, %%mm6 \n\t"
  160. "paddw %%mm3, %%mm4 \n\t"
  161. "paddw %%mm5, %%mm6 \n\t"
  162. "paddw %%mm3, %%mm4 \n\t"
  163. "paddw %%mm5, %%mm6 \n\t"
  164. "paddw %%mm3, %%mm4 \n\t"
  165. "paddw %%mm5, %%mm6 \n\t"
  166. "paddw %%mm3, %%mm4 \n\t"
  167. "paddw %%mm5, %%mm6 \n\t"
  168. "paddw %%mm3, %%mm4 \n\t"
  169. "paddw %%mm5, %%mm6 \n\t"
  170. "psrlw $3, %%mm4 \n\t"
  171. "psrlw $3, %%mm6 \n\t"
  172. "packuswb %%mm4, %%mm4 \n\t"
  173. "packuswb %%mm6, %%mm6 \n\t"
  174. "punpcklbw %%mm6, %%mm4 \n\t"
  175. "punpcklbw %%mm4, %%mm1 \n\t"
  176. "punpckhbw %%mm4, %%mm2 \n\t"
  177. "add $16, %%"REG_S" \n\t"
  178. "add $8, %%"REG_a" \n\t"
  179. "add $8, %%"REG_b" \n\t"
  180. "movq %%mm1, 16(%%"REG_D") \n\t"
  181. "movq %%mm2, 24(%%"REG_D") \n\t"
  182. "add $32, %%"REG_D" \n\t"
  183. "decl %%ecx \n\t"
  184. "jnz .Lli0 \n\t"
  185. "emms \n\t"
  186. "pop %%"REG_BP" \n\t"
  187. :
  188. : "S" (y), "D" (dst), "a" (u), "b" (v), "c" (w/16),
  189. #if ARCH_X86_64
  190. "d" ((x86_reg)us), "r" ((x86_reg)vs)
  191. #else
  192. "d" (&us)
  193. #endif
  194. : "memory"
  195. );
  196. pack_li_0_C(dst, y, u, v, (w&15), us, vs);
  197. }
  198. static void pack_li_1_MMX(unsigned char *dst, unsigned char *y,
  199. unsigned char *u, unsigned char *v, int w, int us, int vs)
  200. {
  201. __asm__ volatile (""
  202. "push %%"REG_BP" \n\t"
  203. #if ARCH_X86_64
  204. "mov %6, %%"REG_BP" \n\t"
  205. #else
  206. "movl 4(%%"REG_d"), %%"REG_BP" \n\t"
  207. "movl (%%"REG_d"), %%"REG_d" \n\t"
  208. #endif
  209. "pxor %%mm0, %%mm0 \n\t"
  210. ASMALIGN(4)
  211. ".Lli1: \n\t"
  212. "movq (%%"REG_S"), %%mm1 \n\t"
  213. "movq (%%"REG_S"), %%mm2 \n\t"
  214. "movq (%%"REG_a",%%"REG_d",2), %%mm4 \n\t"
  215. "movq (%%"REG_b",%%"REG_BP",2), %%mm6 \n\t"
  216. "punpcklbw %%mm0, %%mm4 \n\t"
  217. "punpcklbw %%mm0, %%mm6 \n\t"
  218. "movq (%%"REG_a"), %%mm3 \n\t"
  219. "movq (%%"REG_b"), %%mm5 \n\t"
  220. "punpcklbw %%mm0, %%mm3 \n\t"
  221. "punpcklbw %%mm0, %%mm5 \n\t"
  222. "movq %%mm4, %%mm7 \n\t"
  223. "paddw %%mm4, %%mm4 \n\t"
  224. "paddw %%mm7, %%mm4 \n\t"
  225. "movq %%mm6, %%mm7 \n\t"
  226. "paddw %%mm6, %%mm6 \n\t"
  227. "paddw %%mm7, %%mm6 \n\t"
  228. "paddw %%mm3, %%mm4 \n\t"
  229. "paddw %%mm5, %%mm6 \n\t"
  230. "paddw %%mm3, %%mm4 \n\t"
  231. "paddw %%mm5, %%mm6 \n\t"
  232. "paddw %%mm3, %%mm4 \n\t"
  233. "paddw %%mm5, %%mm6 \n\t"
  234. "paddw %%mm3, %%mm4 \n\t"
  235. "paddw %%mm5, %%mm6 \n\t"
  236. "paddw %%mm3, %%mm4 \n\t"
  237. "paddw %%mm5, %%mm6 \n\t"
  238. "psrlw $3, %%mm4 \n\t"
  239. "psrlw $3, %%mm6 \n\t"
  240. "packuswb %%mm4, %%mm4 \n\t"
  241. "packuswb %%mm6, %%mm6 \n\t"
  242. "punpcklbw %%mm6, %%mm4 \n\t"
  243. "punpcklbw %%mm4, %%mm1 \n\t"
  244. "punpckhbw %%mm4, %%mm2 \n\t"
  245. "movq %%mm1, (%%"REG_D") \n\t"
  246. "movq %%mm2, 8(%%"REG_D") \n\t"
  247. "movq 8(%%"REG_S"), %%mm1 \n\t"
  248. "movq 8(%%"REG_S"), %%mm2 \n\t"
  249. "movq (%%"REG_a",%%"REG_d",2), %%mm4 \n\t"
  250. "movq (%%"REG_b",%%"REG_BP",2), %%mm6 \n\t"
  251. "punpckhbw %%mm0, %%mm4 \n\t"
  252. "punpckhbw %%mm0, %%mm6 \n\t"
  253. "movq (%%"REG_a"), %%mm3 \n\t"
  254. "movq (%%"REG_b"), %%mm5 \n\t"
  255. "punpckhbw %%mm0, %%mm3 \n\t"
  256. "punpckhbw %%mm0, %%mm5 \n\t"
  257. "movq %%mm4, %%mm7 \n\t"
  258. "paddw %%mm4, %%mm4 \n\t"
  259. "paddw %%mm7, %%mm4 \n\t"
  260. "movq %%mm6, %%mm7 \n\t"
  261. "paddw %%mm6, %%mm6 \n\t"
  262. "paddw %%mm7, %%mm6 \n\t"
  263. "paddw %%mm3, %%mm4 \n\t"
  264. "paddw %%mm5, %%mm6 \n\t"
  265. "paddw %%mm3, %%mm4 \n\t"
  266. "paddw %%mm5, %%mm6 \n\t"
  267. "paddw %%mm3, %%mm4 \n\t"
  268. "paddw %%mm5, %%mm6 \n\t"
  269. "paddw %%mm3, %%mm4 \n\t"
  270. "paddw %%mm5, %%mm6 \n\t"
  271. "paddw %%mm3, %%mm4 \n\t"
  272. "paddw %%mm5, %%mm6 \n\t"
  273. "psrlw $3, %%mm4 \n\t"
  274. "psrlw $3, %%mm6 \n\t"
  275. "packuswb %%mm4, %%mm4 \n\t"
  276. "packuswb %%mm6, %%mm6 \n\t"
  277. "punpcklbw %%mm6, %%mm4 \n\t"
  278. "punpcklbw %%mm4, %%mm1 \n\t"
  279. "punpckhbw %%mm4, %%mm2 \n\t"
  280. "add $16, %%"REG_S" \n\t"
  281. "add $8, %%"REG_a" \n\t"
  282. "add $8, %%"REG_b" \n\t"
  283. "movq %%mm1, 16(%%"REG_D") \n\t"
  284. "movq %%mm2, 24(%%"REG_D") \n\t"
  285. "add $32, %%"REG_D" \n\t"
  286. "decl %%ecx \n\t"
  287. "jnz .Lli1 \n\t"
  288. "emms \n\t"
  289. "pop %%"REG_BP" \n\t"
  290. :
  291. : "S" (y), "D" (dst), "a" (u), "b" (v), "c" (w/16),
  292. #if ARCH_X86_64
  293. "d" ((x86_reg)us), "r" ((x86_reg)vs)
  294. #else
  295. "d" (&us)
  296. #endif
  297. : "memory"
  298. );
  299. pack_li_1_C(dst, y, u, v, (w&15), us, vs);
  300. }
  301. #endif /* HAVE_EBX_AVAILABLE */
  302. #endif
  303. static pack_func_t *pack_nn;
  304. static pack_func_t *pack_li_0;
  305. static pack_func_t *pack_li_1;
  306. static void ilpack(unsigned char *dst, unsigned char *src[3],
  307. int dststride, int srcstride[3], int w, int h, pack_func_t *pack[2])
  308. {
  309. int i;
  310. unsigned char *y, *u, *v;
  311. int ys = srcstride[0], us = srcstride[1], vs = srcstride[2];
  312. int a, b;
  313. y = src[0];
  314. u = src[1];
  315. v = src[2];
  316. pack_nn(dst, y, u, v, w, 0, 0);
  317. y += ys; dst += dststride;
  318. pack_nn(dst, y, u+us, v+vs, w, 0, 0);
  319. y += ys; dst += dststride;
  320. for (i=2; i<h-2; i++) {
  321. a = (i&2) ? 1 : -1;
  322. b = (i&1) ^ ((i&2)>>1);
  323. pack[b](dst, y, u, v, w, us*a, vs*a);
  324. y += ys;
  325. if ((i&3) == 1) {
  326. u -= us;
  327. v -= vs;
  328. } else {
  329. u += us;
  330. v += vs;
  331. }
  332. dst += dststride;
  333. }
  334. pack_nn(dst, y, u, v, w, 0, 0);
  335. y += ys; dst += dststride; u += us; v += vs;
  336. pack_nn(dst, y, u, v, w, 0, 0);
  337. }
  338. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  339. {
  340. mp_image_t *dmpi;
  341. // hope we'll get DR buffer:
  342. dmpi=vf_get_image(vf->next, IMGFMT_YUY2,
  343. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  344. mpi->w, mpi->h);
  345. ilpack(dmpi->planes[0], mpi->planes, dmpi->stride[0], mpi->stride, mpi->w, mpi->h, vf->priv->pack);
  346. return vf_next_put_image(vf,dmpi, pts);
  347. }
  348. static int config(struct vf_instance *vf,
  349. int width, int height, int d_width, int d_height,
  350. unsigned int flags, unsigned int outfmt)
  351. {
  352. /* FIXME - also support UYVY output? */
  353. return vf_next_config(vf, width, height, d_width, d_height, flags, IMGFMT_YUY2);
  354. }
  355. static int query_format(struct vf_instance *vf, unsigned int fmt)
  356. {
  357. /* FIXME - really any YUV 4:2:0 input format should work */
  358. switch (fmt) {
  359. case IMGFMT_YV12:
  360. case IMGFMT_IYUV:
  361. case IMGFMT_I420:
  362. return vf_next_query_format(vf,IMGFMT_YUY2);
  363. }
  364. return 0;
  365. }
  366. static int vf_open(vf_instance_t *vf, char *args)
  367. {
  368. vf->config=config;
  369. vf->query_format=query_format;
  370. vf->put_image=put_image;
  371. vf->priv = calloc(1, sizeof(struct vf_priv_s));
  372. vf->priv->mode = 1;
  373. if (args) sscanf(args, "%d", &vf->priv->mode);
  374. pack_nn = (pack_func_t *)pack_nn_C;
  375. pack_li_0 = pack_li_0_C;
  376. pack_li_1 = pack_li_1_C;
  377. #if HAVE_MMX
  378. if(gCpuCaps.hasMMX) {
  379. pack_nn = (pack_func_t *)pack_nn_MMX;
  380. #if HAVE_EBX_AVAILABLE
  381. pack_li_0 = pack_li_0_MMX;
  382. pack_li_1 = pack_li_1_MMX;
  383. #endif
  384. }
  385. #endif
  386. switch(vf->priv->mode) {
  387. case 0:
  388. vf->priv->pack[0] = vf->priv->pack[1] = pack_nn;
  389. break;
  390. default:
  391. mp_msg(MSGT_VFILTER, MSGL_WARN,
  392. "ilpack: unknown mode %d (fallback to linear)\n",
  393. vf->priv->mode);
  394. case 1:
  395. vf->priv->pack[0] = pack_li_0;
  396. vf->priv->pack[1] = pack_li_1;
  397. break;
  398. }
  399. return 1;
  400. }
  401. const vf_info_t vf_info_ilpack = {
  402. "4:2:0 planar -> 4:2:2 packed reinterlacer",
  403. "ilpack",
  404. "Richard Felker",
  405. "",
  406. vf_open,
  407. NULL
  408. };