Mercurial > mplayer.hg
annotate libmpcodecs/vf_delogo.c @ 11603:fbef9326c2ed
small fixes
author | diego |
---|---|
date | Tue, 09 Dec 2003 06:23:36 +0000 |
parents | bdf37ec99325 |
children | 6ff3379a0862 |
rev | line source |
---|---|
10809 | 1 /* |
2 Copyright (C) 2002 Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz> | |
3 | |
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. | |
8 | |
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. | |
13 | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 */ | |
18 | |
19 /* A very simple tv station logo remover */ | |
20 | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <string.h> | |
24 #include <inttypes.h> | |
25 #include <math.h> | |
26 | |
27 #include "../config.h" | |
28 #include "../mp_msg.h" | |
29 #include "../cpudetect.h" | |
30 | |
31 #ifdef HAVE_MALLOC_H | |
32 #include <malloc.h> | |
33 #endif | |
34 | |
35 #include "img_format.h" | |
36 #include "mp_image.h" | |
37 #include "vf.h" | |
38 #include "../libvo/fastmemcpy.h" | |
39 | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
40 #include "m_option.h" |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
41 #include "m_struct.h" |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
42 |
10809 | 43 //===========================================================================// |
44 | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
45 static struct vf_priv_s { |
10809 | 46 unsigned int outfmt; |
47 int xoff, yoff, lw, lh, band, show; | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
48 } vf_priv_dflt = { |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
49 0, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
50 0, 0, 0, 0, 0, 0 |
10809 | 51 }; |
52 | |
53 #define MIN(a,b) (((a) < (b)) ? (a) : (b)) | |
54 #define MAX(a,b) (((a) > (b)) ? (a) : (b)) | |
55 | |
56 static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, | |
10811 | 57 int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) { |
10809 | 58 int y, x; |
59 int interp, dist; | |
60 uint8_t *xdst, *xsrc; | |
61 | |
62 uint8_t *topleft, *botleft, *topright; | |
63 int xclipl, xclipr, yclipt, yclipb; | |
10811 | 64 int logo_x1, logo_x2, logo_y1, logo_y2; |
10809 | 65 |
66 xclipl = MAX(-logo_x, 0); | |
67 xclipr = MAX(logo_x+logo_w-width, 0); | |
68 yclipt = MAX(-logo_y, 0); | |
69 yclipb = MAX(logo_y+logo_h-height, 0); | |
70 | |
10811 | 71 logo_x1 = logo_x + xclipl; |
72 logo_x2 = logo_x + logo_w - xclipr; | |
73 logo_y1 = logo_y + yclipt; | |
74 logo_y2 = logo_y + logo_h - yclipb; | |
10809 | 75 |
10811 | 76 topleft = src+logo_y1*srcStride+logo_x1; |
11029 | 77 topright = src+logo_y1*srcStride+logo_x2-1; |
10811 | 78 botleft = src+(logo_y2-1)*srcStride+logo_x1; |
79 | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
80 if (!direct) memcpy_pic(dst, src, width, height, dstStride, srcStride); |
10811 | 81 |
82 dst += (logo_y1+1)*dstStride; | |
83 src += (logo_y1+1)*srcStride; | |
84 | |
85 for(y = logo_y1+1; y < logo_y2-1; y++) | |
10809 | 86 { |
10811 | 87 for (x = logo_x1+1, xdst = dst+logo_x1+1, xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) { |
88 interp = ((topleft[srcStride*(y-logo_y-yclipt)] | |
89 + topleft[srcStride*(y-logo_y-1-yclipt)] | |
90 + topleft[srcStride*(y-logo_y+1-yclipt)])*(logo_w-(x-logo_x))/logo_w | |
91 + (topright[srcStride*(y-logo_y-yclipt)] | |
92 + topright[srcStride*(y-logo_y-1-yclipt)] | |
93 + topright[srcStride*(y-logo_y+1-yclipt)])*(x-logo_x)/logo_w | |
94 + (topleft[x-logo_x-xclipl] | |
95 + topleft[x-logo_x-1-xclipl] | |
96 + topleft[x-logo_x+1-xclipl])*(logo_h-(y-logo_y))/logo_h | |
97 + (botleft[x-logo_x-xclipl] | |
98 + botleft[x-logo_x-1-xclipl] | |
99 + botleft[x-logo_x+1-xclipl])*(y-logo_y)/logo_h | |
100 )/6; | |
10809 | 101 /* interp = (topleft[srcStride*(y-logo_y)]*(logo_w-(x-logo_x))/logo_w |
102 + topright[srcStride*(y-logo_y)]*(x-logo_x)/logo_w | |
103 + topleft[x-logo_x]*(logo_h-(y-logo_y))/logo_h | |
104 + botleft[x-logo_x]*(y-logo_y)/logo_h | |
105 )/2;*/ | |
10811 | 106 if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) { |
10809 | 107 *xdst = interp; |
108 } else { | |
10811 | 109 dist = 0; |
110 if (x < logo_x+band) dist = MAX(dist, logo_x-x+band); | |
111 else if (x >= logo_x+logo_w-band) dist = MAX(dist, x-(logo_x+logo_w-1-band)); | |
112 if (y < logo_y+band) dist = MAX(dist, logo_y-y+band); | |
113 else if (y >= logo_y+logo_h-band) dist = MAX(dist, y-(logo_y+logo_h-1-band)); | |
114 *xdst = (*xsrc*dist + interp*(band-dist))/band; | |
115 if (show && (dist == band-1)) *xdst = 0; | |
10809 | 116 } |
117 } | |
118 | |
119 dst+= dstStride; | |
120 src+= srcStride; | |
121 } | |
122 } | |
123 | |
124 static int config(struct vf_instance_s* vf, | |
125 int width, int height, int d_width, int d_height, | |
126 unsigned int flags, unsigned int outfmt){ | |
127 | |
128 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
129 } | |
130 | |
131 | |
132 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
133 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change | |
134 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ | |
135 // ok, we can do pp in-place (or pp disabled): | |
136 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
137 mpi->type, mpi->flags, mpi->w, mpi->h); | |
138 mpi->planes[0]=vf->dmpi->planes[0]; | |
139 mpi->stride[0]=vf->dmpi->stride[0]; | |
140 mpi->width=vf->dmpi->width; | |
141 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
142 mpi->planes[1]=vf->dmpi->planes[1]; | |
143 mpi->planes[2]=vf->dmpi->planes[2]; | |
144 mpi->stride[1]=vf->dmpi->stride[1]; | |
145 mpi->stride[2]=vf->dmpi->stride[2]; | |
146 } | |
147 mpi->flags|=MP_IMGFLAG_DIRECT; | |
148 } | |
149 | |
150 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
151 mp_image_t *dmpi; | |
152 | |
153 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){ | |
154 // no DR, so get a new image! hope we'll get DR buffer: | |
155 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt, | |
156 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
157 mpi->w,mpi->h); | |
158 } | |
159 dmpi= vf->dmpi; | |
160 | |
161 delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, | |
10811 | 162 vf->priv->xoff, vf->priv->yoff, vf->priv->lw, vf->priv->lh, vf->priv->band, vf->priv->show, |
163 mpi->flags&MP_IMGFLAG_DIRECT); | |
10809 | 164 delogo(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, |
10811 | 165 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show, |
166 mpi->flags&MP_IMGFLAG_DIRECT); | |
10809 | 167 delogo(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, |
10811 | 168 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show, |
169 mpi->flags&MP_IMGFLAG_DIRECT); | |
10809 | 170 |
171 vf_clone_mpi_attributes(dmpi, mpi); | |
172 | |
173 return vf_next_put_image(vf,dmpi); | |
174 } | |
175 | |
176 static void uninit(struct vf_instance_s* vf){ | |
177 if(!vf->priv) return; | |
178 | |
179 free(vf->priv); | |
180 vf->priv=NULL; | |
181 } | |
182 | |
183 //===========================================================================// | |
184 | |
185 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
186 switch(fmt) | |
187 { | |
188 case IMGFMT_YV12: | |
189 case IMGFMT_I420: | |
190 case IMGFMT_IYUV: | |
191 return vf_next_query_format(vf,vf->priv->outfmt); | |
192 } | |
193 return 0; | |
194 } | |
195 | |
196 static unsigned int fmt_list[]={ | |
197 IMGFMT_YV12, | |
198 IMGFMT_I420, | |
199 IMGFMT_IYUV, | |
200 0 | |
201 }; | |
202 | |
203 static int open(vf_instance_t *vf, char* args){ | |
204 int res; | |
205 | |
206 vf->config=config; | |
207 vf->put_image=put_image; | |
208 vf->get_image=get_image; | |
209 vf->query_format=query_format; | |
210 vf->uninit=uninit; | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
211 if (!vf->priv) |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
212 { |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
213 vf->priv=malloc(sizeof(struct vf_priv_s)); |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
214 memset(vf->priv, 0, sizeof(struct vf_priv_s)); |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
215 } |
10809 | 216 |
217 if (args) res = sscanf(args, "%d:%d:%d:%d:%d", | |
218 &vf->priv->xoff, &vf->priv->yoff, | |
219 &vf->priv->lw, &vf->priv->lh, | |
220 &vf->priv->band); | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
221 if (args && (res != 5)) { |
10809 | 222 uninit(vf); |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
223 return 0; // bad syntax |
10809 | 224 } |
225 | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
226 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n", |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
227 vf->priv->xoff, vf->priv->yoff, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
228 vf->priv->lw, vf->priv->lh, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
229 vf->priv->band); |
10809 | 230 |
231 vf->priv->show = 0; | |
232 | |
233 if (vf->priv->band < 0) { | |
10811 | 234 vf->priv->band = 4; |
10809 | 235 vf->priv->show = 1; |
236 } | |
237 | |
238 | |
239 vf->priv->lw += vf->priv->band*2; | |
240 vf->priv->lh += vf->priv->band*2; | |
241 vf->priv->xoff -= vf->priv->band; | |
242 vf->priv->yoff -= vf->priv->band; | |
243 | |
244 // check csp: | |
245 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12); | |
246 if(!vf->priv->outfmt) | |
247 { | |
248 uninit(vf); | |
249 return 0; // no csp match :( | |
250 } | |
251 | |
252 return 1; | |
253 } | |
254 | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
255 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f) |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
256 static m_option_t vf_opts_fields[] = { |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
257 { "x", ST_OFF(xoff), CONF_TYPE_INT, 0, 0, 0, NULL }, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
258 { "y", ST_OFF(yoff), CONF_TYPE_INT, 0, 0, 0, NULL }, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
259 { "w", ST_OFF(lw), CONF_TYPE_INT, 0, 0, 0, NULL }, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
260 { "h", ST_OFF(lh), CONF_TYPE_INT, 0, 0, 0, NULL }, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
261 { "t", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
262 { "band", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, // alias |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
263 { NULL, NULL, 0, 0, 0, 0, NULL } |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
264 }; |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
265 |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
266 static m_struct_t vf_opts = { |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
267 "delogo", |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
268 sizeof(struct vf_priv_s), |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
269 &vf_priv_dflt, |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
270 vf_opts_fields |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
271 }; |
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
272 |
10809 | 273 vf_info_t vf_info_delogo = { |
274 "simple logo remover", | |
275 "delogo", | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
276 "Jindrich Makovicka, Alex Beregszaszi", |
10809 | 277 "", |
278 open, | |
10816
e9dc9b0b16fa
added new config system support and fixed some bugs (altought sanity checks are needed in delogo as it segfaults easily with w/h out of range)
alex
parents:
10811
diff
changeset
|
279 &vf_opts |
10809 | 280 }; |
281 | |
282 //===========================================================================// |