vf_pullup.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "config.h"
  22. #include "mp_msg.h"
  23. #include "cpudetect.h"
  24. #include "img_format.h"
  25. #include "mp_image.h"
  26. #include "vf.h"
  27. #include "libvo/fastmemcpy.h"
  28. #include "pullup.h"
  29. #undef MAX
  30. #define MAX(a,b) ((a)>(b)?(a):(b))
  31. struct vf_priv_s {
  32. struct pullup_context *ctx;
  33. int init;
  34. int fakecount;
  35. char *qbuf;
  36. };
  37. static void init_pullup(struct vf_instance *vf, mp_image_t *mpi)
  38. {
  39. struct pullup_context *c = vf->priv->ctx;
  40. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  41. c->format = PULLUP_FMT_Y;
  42. c->nplanes = 4;
  43. pullup_preinit_context(c);
  44. c->bpp[0] = c->bpp[1] = c->bpp[2] = 8;
  45. c->w[0] = mpi->w;
  46. c->h[0] = mpi->h;
  47. c->w[1] = c->w[2] = mpi->chroma_width;
  48. c->h[1] = c->h[2] = mpi->chroma_height;
  49. c->w[3] = ((mpi->w+15)/16) * ((mpi->h+15)/16);
  50. c->h[3] = 2;
  51. c->stride[0] = mpi->width;
  52. c->stride[1] = c->stride[2] = mpi->chroma_width;
  53. c->stride[3] = c->w[3];
  54. c->background[1] = c->background[2] = 128;
  55. }
  56. if (gCpuCaps.hasMMX) c->cpu |= PULLUP_CPU_MMX;
  57. if (gCpuCaps.hasMMX2) c->cpu |= PULLUP_CPU_MMX2;
  58. if (gCpuCaps.has3DNow) c->cpu |= PULLUP_CPU_3DNOW;
  59. if (gCpuCaps.has3DNowExt) c->cpu |= PULLUP_CPU_3DNOWEXT;
  60. if (gCpuCaps.hasSSE) c->cpu |= PULLUP_CPU_SSE;
  61. if (gCpuCaps.hasSSE2) c->cpu |= PULLUP_CPU_SSE2;
  62. pullup_init_context(c);
  63. vf->priv->init = 1;
  64. vf->priv->qbuf = malloc(c->w[3]);
  65. }
  66. #if 0
  67. static void get_image(struct vf_instance *vf, mp_image_t *mpi)
  68. {
  69. struct pullup_context *c = vf->priv->ctx;
  70. struct pullup_buffer *b;
  71. if (mpi->type == MP_IMGTYPE_STATIC) return;
  72. if (!vf->priv->init) init_pullup(vf, mpi);
  73. b = pullup_get_buffer(c, 2);
  74. if (!b) return; /* shouldn't happen... */
  75. mpi->priv = b;
  76. mpi->planes[0] = b->planes[0];
  77. mpi->planes[1] = b->planes[1];
  78. mpi->planes[2] = b->planes[2];
  79. mpi->stride[0] = c->stride[0];
  80. mpi->stride[1] = c->stride[1];
  81. mpi->stride[2] = c->stride[2];
  82. mpi->flags |= MP_IMGFLAG_DIRECT;
  83. mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
  84. }
  85. #endif
  86. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  87. {
  88. struct pullup_context *c = vf->priv->ctx;
  89. struct pullup_buffer *b;
  90. struct pullup_frame *f;
  91. mp_image_t *dmpi;
  92. int ret;
  93. int p;
  94. int i;
  95. if (!vf->priv->init) init_pullup(vf, mpi);
  96. if (mpi->flags & MP_IMGFLAG_DIRECT) {
  97. b = mpi->priv;
  98. mpi->priv = 0;
  99. } else {
  100. b = pullup_get_buffer(c, 2);
  101. if (!b) {
  102. mp_msg(MSGT_VFILTER,MSGL_ERR,"Could not get buffer from pullup!\n");
  103. f = pullup_get_frame(c);
  104. pullup_release_frame(f);
  105. return 0;
  106. }
  107. memcpy_pic(b->planes[0], mpi->planes[0], mpi->w, mpi->h,
  108. c->stride[0], mpi->stride[0]);
  109. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  110. memcpy_pic(b->planes[1], mpi->planes[1],
  111. mpi->chroma_width, mpi->chroma_height,
  112. c->stride[1], mpi->stride[1]);
  113. memcpy_pic(b->planes[2], mpi->planes[2],
  114. mpi->chroma_width, mpi->chroma_height,
  115. c->stride[2], mpi->stride[2]);
  116. }
  117. }
  118. if (mpi->qscale) {
  119. fast_memcpy(b->planes[3], mpi->qscale, c->w[3]);
  120. fast_memcpy(b->planes[3]+c->w[3], mpi->qscale, c->w[3]);
  121. }
  122. p = mpi->fields & MP_IMGFIELD_TOP_FIRST ? 0 :
  123. (mpi->fields & MP_IMGFIELD_ORDERED ? 1 : 0);
  124. pullup_submit_field(c, b, p);
  125. pullup_submit_field(c, b, p^1);
  126. if (mpi->fields & MP_IMGFIELD_REPEAT_FIRST)
  127. pullup_submit_field(c, b, p);
  128. pullup_release_buffer(b, 2);
  129. f = pullup_get_frame(c);
  130. /* Fake yes for first few frames (buffer depth) to keep from
  131. * breaking A/V sync with G1's bad architecture... */
  132. if (!f) return vf->priv->fakecount ? (--vf->priv->fakecount,1) : 0;
  133. if (f->length < 2) {
  134. pullup_release_frame(f);
  135. f = pullup_get_frame(c);
  136. if (!f) return 0;
  137. if (f->length < 2) {
  138. pullup_release_frame(f);
  139. if (!(mpi->fields & MP_IMGFIELD_REPEAT_FIRST))
  140. return 0;
  141. f = pullup_get_frame(c);
  142. if (!f) return 0;
  143. if (f->length < 2) {
  144. pullup_release_frame(f);
  145. return 0;
  146. }
  147. }
  148. }
  149. #if 0
  150. /* Average qscale tables from both frames. */
  151. if (mpi->qscale) {
  152. for (i=0; i<c->w[3]; i++) {
  153. vf->priv->qbuf[i] = (f->ofields[0]->planes[3][i]
  154. + f->ofields[1]->planes[3][i+c->w[3]])>>1;
  155. }
  156. }
  157. #else
  158. /* Take worst of qscale tables from both frames. */
  159. if (mpi->qscale) {
  160. for (i=0; i<c->w[3]; i++) {
  161. vf->priv->qbuf[i] = MAX(f->ofields[0]->planes[3][i], f->ofields[1]->planes[3][i+c->w[3]]);
  162. }
  163. }
  164. #endif
  165. /* If the frame isn't already exportable... */
  166. while (!f->buffer) {
  167. dmpi = vf_get_image(vf->next, mpi->imgfmt,
  168. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  169. mpi->width, mpi->height);
  170. /* FIXME: Is it ok to discard dmpi if it's not direct? */
  171. if (!(dmpi->flags & MP_IMGFLAG_DIRECT)) {
  172. pullup_pack_frame(c, f);
  173. break;
  174. }
  175. /* Direct render fields into output buffer */
  176. my_memcpy_pic(dmpi->planes[0], f->ofields[0]->planes[0],
  177. mpi->w, mpi->h/2, dmpi->stride[0]*2, c->stride[0]*2);
  178. my_memcpy_pic(dmpi->planes[0] + dmpi->stride[0],
  179. f->ofields[1]->planes[0] + c->stride[0],
  180. mpi->w, mpi->h/2, dmpi->stride[0]*2, c->stride[0]*2);
  181. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  182. my_memcpy_pic(dmpi->planes[1], f->ofields[0]->planes[1],
  183. mpi->chroma_width, mpi->chroma_height/2,
  184. dmpi->stride[1]*2, c->stride[1]*2);
  185. my_memcpy_pic(dmpi->planes[1] + dmpi->stride[1],
  186. f->ofields[1]->planes[1] + c->stride[1],
  187. mpi->chroma_width, mpi->chroma_height/2,
  188. dmpi->stride[1]*2, c->stride[1]*2);
  189. my_memcpy_pic(dmpi->planes[2], f->ofields[0]->planes[2],
  190. mpi->chroma_width, mpi->chroma_height/2,
  191. dmpi->stride[2]*2, c->stride[2]*2);
  192. my_memcpy_pic(dmpi->planes[2] + dmpi->stride[2],
  193. f->ofields[1]->planes[2] + c->stride[2],
  194. mpi->chroma_width, mpi->chroma_height/2,
  195. dmpi->stride[2]*2, c->stride[2]*2);
  196. }
  197. pullup_release_frame(f);
  198. if (mpi->qscale) {
  199. dmpi->qscale = vf->priv->qbuf;
  200. dmpi->qstride = mpi->qstride;
  201. dmpi->qscale_type = mpi->qscale_type;
  202. }
  203. return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  204. }
  205. dmpi = vf_get_image(vf->next, mpi->imgfmt,
  206. MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
  207. mpi->width, mpi->height);
  208. dmpi->planes[0] = f->buffer->planes[0];
  209. dmpi->planes[1] = f->buffer->planes[1];
  210. dmpi->planes[2] = f->buffer->planes[2];
  211. dmpi->stride[0] = c->stride[0];
  212. dmpi->stride[1] = c->stride[1];
  213. dmpi->stride[2] = c->stride[2];
  214. if (mpi->qscale) {
  215. dmpi->qscale = vf->priv->qbuf;
  216. dmpi->qstride = mpi->qstride;
  217. dmpi->qscale_type = mpi->qscale_type;
  218. }
  219. ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  220. pullup_release_frame(f);
  221. return ret;
  222. }
  223. static int query_format(struct vf_instance *vf, unsigned int fmt)
  224. {
  225. /* FIXME - support more formats */
  226. switch (fmt) {
  227. case IMGFMT_YV12:
  228. case IMGFMT_IYUV:
  229. case IMGFMT_I420:
  230. return vf_next_query_format(vf, fmt);
  231. }
  232. return 0;
  233. }
  234. static int config(struct vf_instance *vf,
  235. int width, int height, int d_width, int d_height,
  236. unsigned int flags, unsigned int outfmt)
  237. {
  238. if (height&3) return 0;
  239. return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
  240. }
  241. static void uninit(struct vf_instance *vf)
  242. {
  243. pullup_free_context(vf->priv->ctx);
  244. free(vf->priv);
  245. }
  246. static int vf_open(vf_instance_t *vf, char *args)
  247. {
  248. struct vf_priv_s *p;
  249. struct pullup_context *c;
  250. //vf->get_image = get_image;
  251. vf->put_image = put_image;
  252. vf->config = config;
  253. vf->query_format = query_format;
  254. vf->uninit = uninit;
  255. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  256. vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
  257. p->ctx = c = pullup_alloc_context();
  258. p->fakecount = 1;
  259. c->verbose = 1;
  260. c->junk_left = c->junk_right = 1;
  261. c->junk_top = c->junk_bottom = 4;
  262. c->strict_breaks = 0;
  263. c->metric_plane = 0;
  264. if (args) {
  265. sscanf(args, "%d:%d:%d:%d:%d:%d", &c->junk_left, &c->junk_right, &c->junk_top, &c->junk_bottom, &c->strict_breaks, &c->metric_plane);
  266. }
  267. return 1;
  268. }
  269. const vf_info_t vf_info_pullup = {
  270. "pullup (from field sequence to frames)",
  271. "pullup",
  272. "Rich Felker",
  273. "",
  274. vf_open,
  275. NULL
  276. };