sync with en/mplayer.1 r28576
[mplayer/glamo.git] / libmpcodecs / vf_delogo.c
blob3ab2ab5f8f20496c4a8eface7ff88c24cc196efb
1 /*
2 * Copyright (C) 2002 Jindrich Makovicka <makovick@gmail.com>
4 * This file is part of MPlayer.
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.
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.
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.
21 /* A very simple tv station logo remover */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <math.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "cpudetect.h"
33 #if HAVE_MALLOC_H
34 #include <malloc.h>
35 #endif
37 #include "img_format.h"
38 #include "mp_image.h"
39 #include "vf.h"
40 #include "libvo/fastmemcpy.h"
42 #include "m_option.h"
43 #include "m_struct.h"
45 //===========================================================================//
47 static struct vf_priv_s {
48 unsigned int outfmt;
49 int xoff, yoff, lw, lh, band, show;
50 } const vf_priv_dflt = {
52 0, 0, 0, 0, 0, 0
55 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
56 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
58 static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height,
59 int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) {
60 int y, x;
61 int interp, dist;
62 uint8_t *xdst, *xsrc;
64 uint8_t *topleft, *botleft, *topright;
65 int xclipl, xclipr, yclipt, yclipb;
66 int logo_x1, logo_x2, logo_y1, logo_y2;
68 xclipl = MAX(-logo_x, 0);
69 xclipr = MAX(logo_x+logo_w-width, 0);
70 yclipt = MAX(-logo_y, 0);
71 yclipb = MAX(logo_y+logo_h-height, 0);
73 logo_x1 = logo_x + xclipl;
74 logo_x2 = logo_x + logo_w - xclipr;
75 logo_y1 = logo_y + yclipt;
76 logo_y2 = logo_y + logo_h - yclipb;
78 topleft = src+logo_y1*srcStride+logo_x1;
79 topright = src+logo_y1*srcStride+logo_x2-1;
80 botleft = src+(logo_y2-1)*srcStride+logo_x1;
82 if (!direct) memcpy_pic(dst, src, width, height, dstStride, srcStride);
84 dst += (logo_y1+1)*dstStride;
85 src += (logo_y1+1)*srcStride;
87 for(y = logo_y1+1; y < logo_y2-1; y++)
89 for (x = logo_x1+1, xdst = dst+logo_x1+1, xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
90 interp = ((topleft[srcStride*(y-logo_y-yclipt)]
91 + topleft[srcStride*(y-logo_y-1-yclipt)]
92 + topleft[srcStride*(y-logo_y+1-yclipt)])*(logo_w-(x-logo_x))/logo_w
93 + (topright[srcStride*(y-logo_y-yclipt)]
94 + topright[srcStride*(y-logo_y-1-yclipt)]
95 + topright[srcStride*(y-logo_y+1-yclipt)])*(x-logo_x)/logo_w
96 + (topleft[x-logo_x-xclipl]
97 + topleft[x-logo_x-1-xclipl]
98 + topleft[x-logo_x+1-xclipl])*(logo_h-(y-logo_y))/logo_h
99 + (botleft[x-logo_x-xclipl]
100 + botleft[x-logo_x-1-xclipl]
101 + botleft[x-logo_x+1-xclipl])*(y-logo_y)/logo_h
102 )/6;
103 /* interp = (topleft[srcStride*(y-logo_y)]*(logo_w-(x-logo_x))/logo_w
104 + topright[srcStride*(y-logo_y)]*(x-logo_x)/logo_w
105 + topleft[x-logo_x]*(logo_h-(y-logo_y))/logo_h
106 + botleft[x-logo_x]*(y-logo_y)/logo_h
107 )/2;*/
108 if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) {
109 *xdst = interp;
110 } else {
111 dist = 0;
112 if (x < logo_x+band) dist = MAX(dist, logo_x-x+band);
113 else if (x >= logo_x+logo_w-band) dist = MAX(dist, x-(logo_x+logo_w-1-band));
114 if (y < logo_y+band) dist = MAX(dist, logo_y-y+band);
115 else if (y >= logo_y+logo_h-band) dist = MAX(dist, y-(logo_y+logo_h-1-band));
116 *xdst = (*xsrc*dist + interp*(band-dist))/band;
117 if (show && (dist == band-1)) *xdst = 0;
121 dst+= dstStride;
122 src+= srcStride;
126 static int config(struct vf_instance_s* vf,
127 int width, int height, int d_width, int d_height,
128 unsigned int flags, unsigned int outfmt){
130 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
134 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
135 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
136 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
137 // ok, we can do pp in-place (or pp disabled):
138 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
139 mpi->type, mpi->flags, mpi->w, mpi->h);
140 mpi->planes[0]=vf->dmpi->planes[0];
141 mpi->stride[0]=vf->dmpi->stride[0];
142 mpi->width=vf->dmpi->width;
143 if(mpi->flags&MP_IMGFLAG_PLANAR){
144 mpi->planes[1]=vf->dmpi->planes[1];
145 mpi->planes[2]=vf->dmpi->planes[2];
146 mpi->stride[1]=vf->dmpi->stride[1];
147 mpi->stride[2]=vf->dmpi->stride[2];
149 mpi->flags|=MP_IMGFLAG_DIRECT;
152 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
153 mp_image_t *dmpi;
155 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
156 // no DR, so get a new image! hope we'll get DR buffer:
157 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
158 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
159 mpi->w,mpi->h);
161 dmpi= vf->dmpi;
163 delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h,
164 vf->priv->xoff, vf->priv->yoff, vf->priv->lw, vf->priv->lh, vf->priv->band, vf->priv->show,
165 mpi->flags&MP_IMGFLAG_DIRECT);
166 delogo(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2,
167 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
168 mpi->flags&MP_IMGFLAG_DIRECT);
169 delogo(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2,
170 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
171 mpi->flags&MP_IMGFLAG_DIRECT);
173 vf_clone_mpi_attributes(dmpi, mpi);
175 return vf_next_put_image(vf,dmpi, pts);
178 static void uninit(struct vf_instance_s* vf){
179 if(!vf->priv) return;
181 free(vf->priv);
182 vf->priv=NULL;
185 //===========================================================================//
187 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
188 switch(fmt)
190 case IMGFMT_YV12:
191 case IMGFMT_I420:
192 case IMGFMT_IYUV:
193 return vf_next_query_format(vf,vf->priv->outfmt);
195 return 0;
198 static unsigned int fmt_list[]={
199 IMGFMT_YV12,
200 IMGFMT_I420,
201 IMGFMT_IYUV,
205 static int open(vf_instance_t *vf, char* args){
206 int res;
208 vf->config=config;
209 vf->put_image=put_image;
210 vf->get_image=get_image;
211 vf->query_format=query_format;
212 vf->uninit=uninit;
213 if (!vf->priv)
215 vf->priv=malloc(sizeof(struct vf_priv_s));
216 memset(vf->priv, 0, sizeof(struct vf_priv_s));
219 if (args) res = sscanf(args, "%d:%d:%d:%d:%d",
220 &vf->priv->xoff, &vf->priv->yoff,
221 &vf->priv->lw, &vf->priv->lh,
222 &vf->priv->band);
223 if (args && (res != 5)) {
224 uninit(vf);
225 return 0; // bad syntax
228 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
229 vf->priv->xoff, vf->priv->yoff,
230 vf->priv->lw, vf->priv->lh,
231 vf->priv->band);
233 vf->priv->show = 0;
235 if (vf->priv->band < 0) {
236 vf->priv->band = 4;
237 vf->priv->show = 1;
241 vf->priv->lw += vf->priv->band*2;
242 vf->priv->lh += vf->priv->band*2;
243 vf->priv->xoff -= vf->priv->band;
244 vf->priv->yoff -= vf->priv->band;
246 // check csp:
247 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
248 if(!vf->priv->outfmt)
250 uninit(vf);
251 return 0; // no csp match :(
254 return 1;
257 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
258 static m_option_t vf_opts_fields[] = {
259 { "x", ST_OFF(xoff), CONF_TYPE_INT, 0, 0, 0, NULL },
260 { "y", ST_OFF(yoff), CONF_TYPE_INT, 0, 0, 0, NULL },
261 { "w", ST_OFF(lw), CONF_TYPE_INT, 0, 0, 0, NULL },
262 { "h", ST_OFF(lh), CONF_TYPE_INT, 0, 0, 0, NULL },
263 { "t", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL },
264 { "band", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, // alias
265 { NULL, NULL, 0, 0, 0, 0, NULL }
268 static m_struct_t vf_opts = {
269 "delogo",
270 sizeof(struct vf_priv_s),
271 &vf_priv_dflt,
272 vf_opts_fields
275 const vf_info_t vf_info_delogo = {
276 "simple logo remover",
277 "delogo",
278 "Jindrich Makovicka, Alex Beregszaszi",
280 open,
281 &vf_opts
284 //===========================================================================//