vf_ilpack.c 13 KB

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