Avoid passing test invalid arguments when string is empty.
[mplayer/glamo.git] / libmpcodecs / vf_tinterlace.c
blobeb02872b0ede3195e827702a00baf321168b3ea8
1 /*
2 Copyright (C) 2003 Michael Zucchi <notzed@ximian.com>
4 This program 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.
9 This program 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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #include "mp_msg.h"
26 #include "img_format.h"
27 #include "mp_image.h"
28 #include "vf.h"
30 #include "libvo/fastmemcpy.h"
32 struct vf_priv_s {
33 int mode;
34 int frame;
35 mp_image_t *dmpi;
38 // Copied verbatim from vf_telecine.c:
39 static inline void *my_memcpy_pic(void * dst, void * src, int bytesPerLine, int height, int dstStride, int srcStride)
41 int i;
42 void *retval=dst;
44 for(i=0; i<height; i++)
46 memcpy(dst, src, bytesPerLine);
47 src+= srcStride;
48 dst+= dstStride;
51 return retval;
54 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
56 int ret = 0;
57 mp_image_t *dmpi;
59 switch (vf->priv->mode) {
60 case 0:
61 dmpi = vf->priv->dmpi;
62 if (dmpi == NULL) {
63 dmpi = vf_get_image(vf->next, mpi->imgfmt,
64 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
65 MP_IMGFLAG_PRESERVE,
66 mpi->width, mpi->height*2);
68 vf->priv->dmpi = dmpi;
70 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
71 dmpi->stride[0]*2, mpi->stride[0]);
72 if (mpi->flags & MP_IMGFLAG_PLANAR) {
73 memcpy_pic(dmpi->planes[1], mpi->planes[1],
74 mpi->chroma_width, mpi->chroma_height,
75 dmpi->stride[1]*2, mpi->stride[1]);
76 memcpy_pic(dmpi->planes[2], mpi->planes[2],
77 mpi->chroma_width, mpi->chroma_height,
78 dmpi->stride[2]*2, mpi->stride[2]);
80 } else {
81 vf->priv->dmpi = NULL;
83 memcpy_pic(dmpi->planes[0]+dmpi->stride[0], mpi->planes[0], mpi->w, mpi->h,
84 dmpi->stride[0]*2, mpi->stride[0]);
85 if (mpi->flags & MP_IMGFLAG_PLANAR) {
86 memcpy_pic(dmpi->planes[1]+dmpi->stride[1], mpi->planes[1],
87 mpi->chroma_width, mpi->chroma_height,
88 dmpi->stride[1]*2, mpi->stride[1]);
89 memcpy_pic(dmpi->planes[2]+dmpi->stride[2], mpi->planes[2],
90 mpi->chroma_width, mpi->chroma_height,
91 dmpi->stride[2]*2, mpi->stride[2]);
93 ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
95 break;
96 case 1:
97 if (vf->priv->frame & 1)
98 ret = vf_next_put_image(vf, mpi, MP_NOPTS_VALUE);
99 break;
100 case 2:
101 if ((vf->priv->frame & 1) == 0)
102 ret = vf_next_put_image(vf, mpi, MP_NOPTS_VALUE);
103 break;
104 case 3:
105 dmpi = vf_get_image(vf->next, mpi->imgfmt,
106 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
107 mpi->width, mpi->height*2);
108 /* fixme, just clear alternate lines */
109 vf_mpi_clear(dmpi, 0, 0, dmpi->w, dmpi->h);
110 if ((vf->priv->frame & 1) == 0) {
111 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
112 dmpi->stride[0]*2, mpi->stride[0]);
113 if (mpi->flags & MP_IMGFLAG_PLANAR) {
114 memcpy_pic(dmpi->planes[1], mpi->planes[1],
115 mpi->chroma_width, mpi->chroma_height,
116 dmpi->stride[1]*2, mpi->stride[1]);
117 memcpy_pic(dmpi->planes[2], mpi->planes[2],
118 mpi->chroma_width, mpi->chroma_height,
119 dmpi->stride[2]*2, mpi->stride[2]);
121 } else {
122 memcpy_pic(dmpi->planes[0]+dmpi->stride[0], mpi->planes[0], mpi->w, mpi->h,
123 dmpi->stride[0]*2, mpi->stride[0]);
124 if (mpi->flags & MP_IMGFLAG_PLANAR) {
125 memcpy_pic(dmpi->planes[1]+dmpi->stride[1], mpi->planes[1],
126 mpi->chroma_width, mpi->chroma_height,
127 dmpi->stride[1]*2, mpi->stride[1]);
128 memcpy_pic(dmpi->planes[2]+dmpi->stride[2], mpi->planes[2],
129 mpi->chroma_width, mpi->chroma_height,
130 dmpi->stride[2]*2, mpi->stride[2]);
133 ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
134 break;
135 case 4:
136 // Interleave even lines (only) from Frame 'i' with odd
137 // lines (only) from Frame 'i+1', halving the Frame
138 // rate and preserving image height.
140 dmpi = vf->priv->dmpi;
142 // @@ Need help: Should I set dmpi->fields to indicate
143 // that the (new) frame will be interlaced!? E.g. ...
144 // dmpi->fields |= MP_IMGFIELD_INTERLACED;
145 // dmpi->fields |= MP_IMGFIELD_TOP_FIRST;
146 // etc.
148 if (dmpi == NULL) {
149 dmpi = vf_get_image(vf->next, mpi->imgfmt,
150 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
151 MP_IMGFLAG_PRESERVE,
152 mpi->width, mpi->height);
154 vf->priv->dmpi = dmpi;
156 my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
157 dmpi->stride[0]*2, mpi->stride[0]*2);
158 if (mpi->flags & MP_IMGFLAG_PLANAR) {
159 my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
160 mpi->chroma_width, mpi->chroma_height/2,
161 dmpi->stride[1]*2, mpi->stride[1]*2);
162 my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
163 mpi->chroma_width, mpi->chroma_height/2,
164 dmpi->stride[2]*2, mpi->stride[2]*2);
166 } else {
167 vf->priv->dmpi = NULL;
169 my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
170 mpi->planes[0]+mpi->stride[0],
171 mpi->w, mpi->h/2,
172 dmpi->stride[0]*2, mpi->stride[0]*2);
173 if (mpi->flags & MP_IMGFLAG_PLANAR) {
174 my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
175 mpi->planes[1]+mpi->stride[1],
176 mpi->chroma_width, mpi->chroma_height/2,
177 dmpi->stride[1]*2, mpi->stride[1]*2);
178 my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
179 mpi->planes[2]+mpi->stride[2],
180 mpi->chroma_width, mpi->chroma_height/2,
181 dmpi->stride[2]*2, mpi->stride[2]*2);
183 ret = vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
185 break;
188 vf->priv->frame++;
190 return ret;
193 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
195 /* FIXME - figure out which other formats work */
196 switch (fmt) {
197 case IMGFMT_YV12:
198 case IMGFMT_IYUV:
199 case IMGFMT_I420:
200 return vf_next_query_format(vf, fmt);
202 return 0;
205 static int config(struct vf_instance_s* vf,
206 int width, int height, int d_width, int d_height,
207 unsigned int flags, unsigned int outfmt)
209 switch (vf->priv->mode) {
210 case 0:
211 case 3:
212 return vf_next_config(vf,width,height*2,d_width,d_height*2,flags,outfmt);
213 case 1: /* odd frames */
214 case 2: /* even frames */
215 case 4: /* alternate frame (height-preserving) interlacing */
216 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
218 return 0;
221 static void uninit(struct vf_instance_s* vf)
223 free(vf->priv);
226 static int open(vf_instance_t *vf, char* args)
228 struct vf_priv_s *p;
229 vf->config = config;
230 vf->put_image = put_image;
231 vf->query_format = query_format;
232 vf->uninit = uninit;
233 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
234 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
235 vf->priv->mode = 0;
236 if (args)
237 sscanf(args, "%d", &vf->priv->mode);
238 vf->priv->frame = 0;
239 return 1;
242 vf_info_t vf_info_tinterlace = {
243 "temporal field interlacing",
244 "tinterlace",
245 "Michael Zucchi",
247 open,
248 NULL