1 /*
2 * Copyright (C) 2002 Jindrich Makovicka <makovick@gmail.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
21 /* A very simple tv station logo remover */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <errno.h>
29 #include <math.h>
30
31 #include "mp_msg.h"
32 #include "cpudetect.h"
33 #include "img_format.h"
34 #include "mp_image.h"
35 #include "vf.h"
36 #include "libvo/fastmemcpy.h"
37
38 #include "m_option.h"
39 #include "m_struct.h"
40
41 //===========================================================================//
42
43 static struct vf_priv_s {
44 unsigned int outfmt;
45 int xoff, yoff, lw, lh, band, show;
46 const char *file;
47 struct timed_rectangle {
48 int ts, x, y, w, h, b;
49 } *timed_rect;
50 int n_timed_rect;
51 int cur_timed_rect;
52 } const vf_priv_dflt = {
53 0,
54 0, 0, 0, 0, 0, 0,
55 NULL, NULL, 0, 0,
56 };
57
58 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
59 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
60
61 /**
62 * Adjust the coordinates to suit the band width
63 * Also print a notice in verbose mode
64 */
fix_band(struct vf_priv_s * p)65 static void fix_band(struct vf_priv_s *p)
66 {
67 p->show = 0;
68 if (p->band < 0) {
69 p->band = 4;
70 p->show = 1;
71 }
72 p->lw += p->band*2;
73 p->lh += p->band*2;
74 p->xoff -= p->band;
75 p->yoff -= p->band;
76 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
77 p->xoff, p->yoff, p->lw, p->lh, p->band);
78 }
79
update_sub(struct vf_priv_s * p,double pts)80 static void update_sub(struct vf_priv_s *p, double pts)
81 {
82 int ipts = pts * 1000;
83 int tr = p->cur_timed_rect;
84 while (tr < p->n_timed_rect - 1 && ipts >= p->timed_rect[tr + 1].ts)
85 tr++;
86 while (tr >= 0 && ipts < p->timed_rect[tr].ts)
87 tr--;
88 if (tr == p->cur_timed_rect)
89 return;
90 p->cur_timed_rect = tr;
91 if (tr >= 0) {
92 p->xoff = p->timed_rect[tr].x;
93 p->yoff = p->timed_rect[tr].y;
94 p->lw = p->timed_rect[tr].w;
95 p->lh = p->timed_rect[tr].h;
96 p->band = p->timed_rect[tr].b;
97 } else {
98 p->xoff = p->yoff = p->lw = p->lh = p->band = 0;
99 }
100 fix_band(p);
101 }
102
delogo(uint8_t * dst,uint8_t * src,int dstStride,int srcStride,int width,int height,int logo_x,int logo_y,int logo_w,int logo_h,int band,int show,int direct)103 static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height,
104 int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) {
105 int y, x;
106 int interp, dist;
107 uint8_t *xdst, *xsrc;
108
109 uint8_t *topleft, *botleft, *topright;
110 int xclipl, xclipr, yclipt, yclipb;
111 int logo_x1, logo_x2, logo_y1, logo_y2;
112
113 xclipl = MAX(-logo_x, 0);
114 xclipr = MAX(logo_x+logo_w-width, 0);
115 yclipt = MAX(-logo_y, 0);
116 yclipb = MAX(logo_y+logo_h-height, 0);
117
118 logo_x1 = logo_x + xclipl;
119 logo_x2 = logo_x + logo_w - xclipr;
120 logo_y1 = logo_y + yclipt;
121 logo_y2 = logo_y + logo_h - yclipb;
122
123 topleft = src+logo_y1*srcStride+logo_x1;
124 topright = src+logo_y1*srcStride+logo_x2-1;
125 botleft = src+(logo_y2-1)*srcStride+logo_x1;
126
127 if (!direct) memcpy_pic(dst, src, width, height, dstStride, srcStride);
128
129 dst += (logo_y1+1)*dstStride;
130 src += (logo_y1+1)*srcStride;
131
132 for(y = logo_y1+1; y < logo_y2-1; y++)
133 {
134 for (x = logo_x1+1, xdst = dst+logo_x1+1, xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
135 interp = ((topleft[srcStride*(y-logo_y-yclipt)]
136 + topleft[srcStride*(y-logo_y-1-yclipt)]
137 + topleft[srcStride*(y-logo_y+1-yclipt)])*(logo_w-(x-logo_x))/logo_w
138 + (topright[srcStride*(y-logo_y-yclipt)]
139 + topright[srcStride*(y-logo_y-1-yclipt)]
140 + topright[srcStride*(y-logo_y+1-yclipt)])*(x-logo_x)/logo_w
141 + (topleft[x-logo_x-xclipl]
142 + topleft[x-logo_x-1-xclipl]
143 + topleft[x-logo_x+1-xclipl])*(logo_h-(y-logo_y))/logo_h
144 + (botleft[x-logo_x-xclipl]
145 + botleft[x-logo_x-1-xclipl]
146 + botleft[x-logo_x+1-xclipl])*(y-logo_y)/logo_h
147 )/6;
148 /* interp = (topleft[srcStride*(y-logo_y)]*(logo_w-(x-logo_x))/logo_w
149 + topright[srcStride*(y-logo_y)]*(x-logo_x)/logo_w
150 + topleft[x-logo_x]*(logo_h-(y-logo_y))/logo_h
151 + botleft[x-logo_x]*(y-logo_y)/logo_h
152 )/2;*/
153 if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) {
154 *xdst = interp;
155 } else {
156 dist = 0;
157 if (x < logo_x+band) dist = MAX(dist, logo_x-x+band);
158 else if (x >= logo_x+logo_w-band) dist = MAX(dist, x-(logo_x+logo_w-1-band));
159 if (y < logo_y+band) dist = MAX(dist, logo_y-y+band);
160 else if (y >= logo_y+logo_h-band) dist = MAX(dist, y-(logo_y+logo_h-1-band));
161 *xdst = (*xsrc*dist + interp*(band-dist))/band;
162 if (show && (dist == band-1)) *xdst = 0;
163 }
164 }
165
166 dst+= dstStride;
167 src+= srcStride;
168 }
169 }
170
config(struct vf_instance * vf,int width,int height,int d_width,int d_height,unsigned int flags,unsigned int outfmt)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 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
176 }
177
178
get_image(struct vf_instance * vf,mp_image_t * mpi)179 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
180 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
181 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
182 // ok, we can do pp in-place (or pp disabled):
183 mpi->priv =
184 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
185 mpi->type, mpi->flags, mpi->w, mpi->h);
186 mpi->planes[0]=vf->dmpi->planes[0];
187 mpi->stride[0]=vf->dmpi->stride[0];
188 mpi->width=vf->dmpi->width;
189 if(mpi->flags&MP_IMGFLAG_PLANAR){
190 mpi->planes[1]=vf->dmpi->planes[1];
191 mpi->planes[2]=vf->dmpi->planes[2];
192 mpi->stride[1]=vf->dmpi->stride[1];
193 mpi->stride[2]=vf->dmpi->stride[2];
194 }
195 mpi->flags|=MP_IMGFLAG_DIRECT;
196 }
197
put_image(struct vf_instance * vf,mp_image_t * mpi,double pts,double endpts)198 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts, double endpts){
199 mp_image_t *dmpi;
200
201 if(mpi->flags&MP_IMGFLAG_DIRECT) {
202 vf->dmpi = mpi->priv;
203 mpi->priv = NULL;
204 } else {
205 // no DR, so get a new image! hope we'll get DR buffer:
206 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
207 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
208 mpi->w,mpi->h);
209 }
210 dmpi= vf->dmpi;
211
212 if (vf->priv->timed_rect)
213 update_sub(vf->priv, pts);
214 delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h,
215 vf->priv->xoff, vf->priv->yoff, vf->priv->lw, vf->priv->lh, vf->priv->band, vf->priv->show,
216 mpi->flags&MP_IMGFLAG_DIRECT);
217 delogo(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2,
218 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
219 mpi->flags&MP_IMGFLAG_DIRECT);
220 delogo(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2,
221 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
222 mpi->flags&MP_IMGFLAG_DIRECT);
223
224 vf_clone_mpi_attributes(dmpi, mpi);
225
226 return vf_next_put_image(vf, dmpi, pts, endpts);
227 }
228
uninit(struct vf_instance * vf)229 static void uninit(struct vf_instance *vf){
230 if(!vf->priv) return;
231
232 free(vf->priv);
233 vf->priv=NULL;
234 }
235
236 //===========================================================================//
237
query_format(struct vf_instance * vf,unsigned int fmt)238 static int query_format(struct vf_instance *vf, unsigned int fmt){
239 switch(fmt)
240 {
241 case IMGFMT_YV12:
242 case IMGFMT_I420:
243 case IMGFMT_IYUV:
244 return vf_next_query_format(vf,vf->priv->outfmt);
245 }
246 return 0;
247 }
248
249 static const unsigned int fmt_list[]={
250 IMGFMT_YV12,
251 IMGFMT_I420,
252 IMGFMT_IYUV,
253 0
254 };
255
load_timed_rectangles(struct vf_priv_s * delogo)256 static int load_timed_rectangles(struct vf_priv_s *delogo)
257 {
258 FILE *f;
259 char line[2048];
260 int lineno = 0, p;
261 double ts, last_ts = 0;
262 struct timed_rectangle *rect = NULL, *nr;
263 int n_rect = 0, alloc_rect = 0;
264
265 f = fopen(delogo->file, "r");
266 if (!f) {
267 mp_msg(MSGT_VFILTER, MSGL_ERR, "delogo: unable to load %s: %s\n",
268 delogo->file, strerror(errno));
269 return -1;
270 }
271 while (fgets(line, sizeof(line), f)) {
272 lineno++;
273 if (*line == '#' || *line == '\n')
274 continue;
275 if (n_rect == alloc_rect) {
276 if (alloc_rect > INT_MAX / 2 / (int)sizeof(*rect)) {
277 mp_msg(MSGT_VFILTER, MSGL_WARN,
278 "delogo: too many rectangles\n");
279 goto load_error;
280 }
281 alloc_rect = alloc_rect ? 2 * alloc_rect : 256;
282 nr = realloc(rect, alloc_rect * sizeof(*rect));
283 if (!nr) {
284 mp_msg(MSGT_VFILTER, MSGL_WARN, "delogo: out of memory\n");
285 goto load_error;
286 }
287 rect = nr;
288 }
289 nr = rect + n_rect;
290 memset(nr, 0, sizeof(*nr));
291 p = sscanf(line, "%lf %d:%d:%d:%d:%d",
292 &ts, &nr->x, &nr->y, &nr->w, &nr->h, &nr->b);
293 if ((p == 2 && !nr->x) || p == 5 || p == 6) {
294 if (ts <= last_ts)
295 mp_msg(MSGT_VFILTER, MSGL_WARN, "delogo: %s:%d: wrong time\n",
296 delogo->file, lineno);
297 nr->ts = 1000 * ts + 0.5;
298 n_rect++;
299 } else {
300 mp_msg(MSGT_VFILTER, MSGL_WARN, "delogo: %s:%d: syntax error\n",
301 delogo->file, lineno);
302 }
303 }
304 fclose(f);
305 if (!n_rect) {
306 mp_msg(MSGT_VFILTER, MSGL_ERR, "delogo: %s: no rectangles found\n",
307 delogo->file);
308 free(rect);
309 return -1;
310 }
311 nr = realloc(rect, n_rect * sizeof(*rect));
312 if (nr)
313 rect = nr;
314 delogo->timed_rect = rect;
315 delogo->n_timed_rect = n_rect;
316 return 0;
317
318 load_error:
319 free(rect);
320 fclose(f);
321 return -1;
322 }
323
vf_open(vf_instance_t * vf,char * args)324 static int vf_open(vf_instance_t *vf, char *args){
325 vf->config=config;
326 vf->put_image=put_image;
327 vf->get_image=get_image;
328 vf->query_format=query_format;
329 vf->uninit=uninit;
330
331 if (vf->priv->file) {
332 if (load_timed_rectangles(vf->priv))
333 return 0;
334 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d from %s\n",
335 vf->priv->n_timed_rect, vf->priv->file);
336 vf->priv->cur_timed_rect = -1;
337 }
338 fix_band(vf->priv);
339
340 // check csp:
341 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
342 if(!vf->priv->outfmt)
343 {
344 uninit(vf);
345 return 0; // no csp match :(
346 }
347
348 return 1;
349 }
350
351 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
352 static const m_option_t vf_opts_fields[] = {
353 { "x", ST_OFF(xoff), CONF_TYPE_INT, 0, 0, 0, NULL },
354 { "y", ST_OFF(yoff), CONF_TYPE_INT, 0, 0, 0, NULL },
355 { "w", ST_OFF(lw), CONF_TYPE_INT, 0, 0, 0, NULL },
356 { "h", ST_OFF(lh), CONF_TYPE_INT, 0, 0, 0, NULL },
357 { "t", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL },
358 { "band", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, // alias
359 { "file", ST_OFF(file), CONF_TYPE_STRING, 0, 0, 0, NULL },
360 { NULL, NULL, 0, 0, 0, 0, NULL }
361 };
362
363 static const m_struct_t vf_opts = {
364 "delogo",
365 sizeof(struct vf_priv_s),
366 &vf_priv_dflt,
367 vf_opts_fields
368 };
369
370 const vf_info_t vf_info_delogo = {
371 "simple logo remover",
372 "delogo",
373 "Jindrich Makovicka, Alex Beregszaszi",
374 "",
375 vf_open,
376 &vf_opts
377 };
378
379 //===========================================================================//
380