vf_tinterlace.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2003 Michael Zucchi <notzed@ximian.com>
  3. *
  4. * This file is part of MPlayer.
  5. *
  6. * MPlayer is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * MPlayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "config.h"
  24. #include "mp_msg.h"
  25. #include "img_format.h"
  26. #include "mp_image.h"
  27. #include "vf.h"
  28. #include "libvo/fastmemcpy.h"
  29. struct vf_priv_s {
  30. int mode;
  31. int frame;
  32. mp_image_t *dmpi;
  33. };
  34. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  35. {
  36. int ret = 0;
  37. mp_image_t *dmpi;
  38. switch (vf->priv->mode) {
  39. case 0:
  40. dmpi = vf->priv->dmpi;
  41. if (dmpi == NULL) {
  42. dmpi = vf_get_image(vf->next, mpi->imgfmt,
  43. MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
  44. MP_IMGFLAG_PRESERVE,
  45. mpi->width, mpi->height*2);
  46. vf->priv->dmpi = dmpi;
  47. memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
  48. dmpi->stride[0]*2, mpi->stride[0]);
  49. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  50. memcpy_pic(dmpi->planes[1], mpi->planes[1],
  51. mpi->chroma_width, mpi->chroma_height,
  52. dmpi->stride[1]*2, mpi->stride[1]);
  53. memcpy_pic(dmpi->planes[2], mpi->planes[2],
  54. mpi->chroma_width, mpi->chroma_height,
  55. dmpi->stride[2]*2, mpi->stride[2]);
  56. }
  57. } else {
  58. vf->priv->dmpi = NULL;
  59. memcpy_pic(dmpi->planes[0]+dmpi->stride[0], mpi->planes[0], mpi->w, mpi->h,
  60. dmpi->stride[0]*2, mpi->stride[0]);
  61. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  62. memcpy_pic(dmpi->planes[1]+dmpi->stride[1], mpi->planes[1],
  63. mpi->chroma_width, mpi->chroma_height,
  64. dmpi->stride[1]*2, mpi->stride[1]);
  65. memcpy_pic(dmpi->planes[2]+dmpi->stride[2], mpi->planes[2],
  66. mpi->chroma_width, mpi->chroma_height,
  67. dmpi->stride[2]*2, mpi->stride[2]);
  68. }
  69. ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  70. }
  71. break;
  72. case 1:
  73. if (vf->priv->frame & 1)
  74. ret = vf_next_put_image(vf, mpi, MP_NOPTS_VALUE);
  75. break;
  76. case 2:
  77. if ((vf->priv->frame & 1) == 0)
  78. ret = vf_next_put_image(vf, mpi, MP_NOPTS_VALUE);
  79. break;
  80. case 3:
  81. dmpi = vf_get_image(vf->next, mpi->imgfmt,
  82. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  83. mpi->width, mpi->height*2);
  84. /* fixme, just clear alternate lines */
  85. vf_mpi_clear(dmpi, 0, 0, dmpi->w, dmpi->h);
  86. if ((vf->priv->frame & 1) == 0) {
  87. memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
  88. dmpi->stride[0]*2, mpi->stride[0]);
  89. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  90. memcpy_pic(dmpi->planes[1], mpi->planes[1],
  91. mpi->chroma_width, mpi->chroma_height,
  92. dmpi->stride[1]*2, mpi->stride[1]);
  93. memcpy_pic(dmpi->planes[2], mpi->planes[2],
  94. mpi->chroma_width, mpi->chroma_height,
  95. dmpi->stride[2]*2, mpi->stride[2]);
  96. }
  97. } else {
  98. memcpy_pic(dmpi->planes[0]+dmpi->stride[0], mpi->planes[0], mpi->w, mpi->h,
  99. dmpi->stride[0]*2, mpi->stride[0]);
  100. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  101. memcpy_pic(dmpi->planes[1]+dmpi->stride[1], mpi->planes[1],
  102. mpi->chroma_width, mpi->chroma_height,
  103. dmpi->stride[1]*2, mpi->stride[1]);
  104. memcpy_pic(dmpi->planes[2]+dmpi->stride[2], mpi->planes[2],
  105. mpi->chroma_width, mpi->chroma_height,
  106. dmpi->stride[2]*2, mpi->stride[2]);
  107. }
  108. }
  109. ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  110. break;
  111. case 4:
  112. // Interleave even lines (only) from Frame 'i' with odd
  113. // lines (only) from Frame 'i+1', halving the Frame
  114. // rate and preserving image height.
  115. dmpi = vf->priv->dmpi;
  116. // @@ Need help: Should I set dmpi->fields to indicate
  117. // that the (new) frame will be interlaced!? E.g. ...
  118. // dmpi->fields |= MP_IMGFIELD_INTERLACED;
  119. // dmpi->fields |= MP_IMGFIELD_TOP_FIRST;
  120. // etc.
  121. if (dmpi == NULL) {
  122. dmpi = vf_get_image(vf->next, mpi->imgfmt,
  123. MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
  124. MP_IMGFLAG_PRESERVE,
  125. mpi->width, mpi->height);
  126. vf->priv->dmpi = dmpi;
  127. my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
  128. dmpi->stride[0]*2, mpi->stride[0]*2);
  129. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  130. my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
  131. mpi->chroma_width, mpi->chroma_height/2,
  132. dmpi->stride[1]*2, mpi->stride[1]*2);
  133. my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
  134. mpi->chroma_width, mpi->chroma_height/2,
  135. dmpi->stride[2]*2, mpi->stride[2]*2);
  136. }
  137. } else {
  138. vf->priv->dmpi = NULL;
  139. my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
  140. mpi->planes[0]+mpi->stride[0],
  141. mpi->w, mpi->h/2,
  142. dmpi->stride[0]*2, mpi->stride[0]*2);
  143. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  144. my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
  145. mpi->planes[1]+mpi->stride[1],
  146. mpi->chroma_width, mpi->chroma_height/2,
  147. dmpi->stride[1]*2, mpi->stride[1]*2);
  148. my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
  149. mpi->planes[2]+mpi->stride[2],
  150. mpi->chroma_width, mpi->chroma_height/2,
  151. dmpi->stride[2]*2, mpi->stride[2]*2);
  152. }
  153. ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  154. }
  155. break;
  156. }
  157. vf->priv->frame++;
  158. return ret;
  159. }
  160. static int query_format(struct vf_instance *vf, unsigned int fmt)
  161. {
  162. /* FIXME - figure out which other formats work */
  163. switch (fmt) {
  164. case IMGFMT_YV12:
  165. case IMGFMT_IYUV:
  166. case IMGFMT_I420:
  167. return vf_next_query_format(vf, fmt);
  168. }
  169. return 0;
  170. }
  171. static int config(struct vf_instance *vf,
  172. int width, int height, int d_width, int d_height,
  173. unsigned int flags, unsigned int outfmt)
  174. {
  175. switch (vf->priv->mode) {
  176. case 0:
  177. case 3:
  178. return vf_next_config(vf,width,height*2,d_width,d_height*2,flags,outfmt);
  179. case 1: /* odd frames */
  180. case 2: /* even frames */
  181. case 4: /* alternate frame (height-preserving) interlacing */
  182. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  183. }
  184. return 0;
  185. }
  186. static void uninit(struct vf_instance *vf)
  187. {
  188. free(vf->priv);
  189. }
  190. static int vf_open(vf_instance_t *vf, char *args)
  191. {
  192. struct vf_priv_s *p;
  193. vf->config = config;
  194. vf->put_image = put_image;
  195. vf->query_format = query_format;
  196. vf->uninit = uninit;
  197. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  198. vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
  199. vf->priv->mode = 0;
  200. if (args)
  201. sscanf(args, "%d", &vf->priv->mode);
  202. vf->priv->frame = 0;
  203. return 1;
  204. }
  205. const vf_info_t vf_info_tinterlace = {
  206. "temporal field interlacing",
  207. "tinterlace",
  208. "Michael Zucchi",
  209. "",
  210. vf_open,
  211. NULL
  212. };