Configure needs AS to be set for the Makefiles.
[mplayer/glamo.git] / libmpcodecs / vf_delogo.c
blob7654b627de6d6b149d34b9758fcab66ab1a3ed9e
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 "mp_msg.h"
30 #include "cpudetect.h"
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
34 #include "libvo/fastmemcpy.h"
36 #include "m_option.h"
37 #include "m_struct.h"
39 //===========================================================================//
41 static struct vf_priv_s {
42 unsigned int outfmt;
43 int xoff, yoff, lw, lh, band, show;
44 } const vf_priv_dflt = {
46 0, 0, 0, 0, 0, 0
49 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
50 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
52 static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height,
53 int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) {
54 int y, x;
55 int interp, dist;
56 uint8_t *xdst, *xsrc;
58 uint8_t *topleft, *botleft, *topright;
59 int xclipl, xclipr, yclipt, yclipb;
60 int logo_x1, logo_x2, logo_y1, logo_y2;
62 xclipl = MAX(-logo_x, 0);
63 xclipr = MAX(logo_x+logo_w-width, 0);
64 yclipt = MAX(-logo_y, 0);
65 yclipb = MAX(logo_y+logo_h-height, 0);
67 logo_x1 = logo_x + xclipl;
68 logo_x2 = logo_x + logo_w - xclipr;
69 logo_y1 = logo_y + yclipt;
70 logo_y2 = logo_y + logo_h - yclipb;
72 topleft = src+logo_y1*srcStride+logo_x1;
73 topright = src+logo_y1*srcStride+logo_x2-1;
74 botleft = src+(logo_y2-1)*srcStride+logo_x1;
76 if (!direct) memcpy_pic(dst, src, width, height, dstStride, srcStride);
78 dst += (logo_y1+1)*dstStride;
79 src += (logo_y1+1)*srcStride;
81 for(y = logo_y1+1; y < logo_y2-1; y++)
83 for (x = logo_x1+1, xdst = dst+logo_x1+1, xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
84 interp = ((topleft[srcStride*(y-logo_y-yclipt)]
85 + topleft[srcStride*(y-logo_y-1-yclipt)]
86 + topleft[srcStride*(y-logo_y+1-yclipt)])*(logo_w-(x-logo_x))/logo_w
87 + (topright[srcStride*(y-logo_y-yclipt)]
88 + topright[srcStride*(y-logo_y-1-yclipt)]
89 + topright[srcStride*(y-logo_y+1-yclipt)])*(x-logo_x)/logo_w
90 + (topleft[x-logo_x-xclipl]
91 + topleft[x-logo_x-1-xclipl]
92 + topleft[x-logo_x+1-xclipl])*(logo_h-(y-logo_y))/logo_h
93 + (botleft[x-logo_x-xclipl]
94 + botleft[x-logo_x-1-xclipl]
95 + botleft[x-logo_x+1-xclipl])*(y-logo_y)/logo_h
96 )/6;
97 /* interp = (topleft[srcStride*(y-logo_y)]*(logo_w-(x-logo_x))/logo_w
98 + topright[srcStride*(y-logo_y)]*(x-logo_x)/logo_w
99 + topleft[x-logo_x]*(logo_h-(y-logo_y))/logo_h
100 + botleft[x-logo_x]*(y-logo_y)/logo_h
101 )/2;*/
102 if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) {
103 *xdst = interp;
104 } else {
105 dist = 0;
106 if (x < logo_x+band) dist = MAX(dist, logo_x-x+band);
107 else if (x >= logo_x+logo_w-band) dist = MAX(dist, x-(logo_x+logo_w-1-band));
108 if (y < logo_y+band) dist = MAX(dist, logo_y-y+band);
109 else if (y >= logo_y+logo_h-band) dist = MAX(dist, y-(logo_y+logo_h-1-band));
110 *xdst = (*xsrc*dist + interp*(band-dist))/band;
111 if (show && (dist == band-1)) *xdst = 0;
115 dst+= dstStride;
116 src+= srcStride;
120 static int config(struct vf_instance_s* vf,
121 int width, int height, int d_width, int d_height,
122 unsigned int flags, unsigned int outfmt){
124 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
128 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
129 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
130 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
131 // ok, we can do pp in-place (or pp disabled):
132 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
133 mpi->type, mpi->flags, mpi->w, mpi->h);
134 mpi->planes[0]=vf->dmpi->planes[0];
135 mpi->stride[0]=vf->dmpi->stride[0];
136 mpi->width=vf->dmpi->width;
137 if(mpi->flags&MP_IMGFLAG_PLANAR){
138 mpi->planes[1]=vf->dmpi->planes[1];
139 mpi->planes[2]=vf->dmpi->planes[2];
140 mpi->stride[1]=vf->dmpi->stride[1];
141 mpi->stride[2]=vf->dmpi->stride[2];
143 mpi->flags|=MP_IMGFLAG_DIRECT;
146 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
147 mp_image_t *dmpi;
149 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
150 // no DR, so get a new image! hope we'll get DR buffer:
151 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
152 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
153 mpi->w,mpi->h);
155 dmpi= vf->dmpi;
157 delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h,
158 vf->priv->xoff, vf->priv->yoff, vf->priv->lw, vf->priv->lh, vf->priv->band, vf->priv->show,
159 mpi->flags&MP_IMGFLAG_DIRECT);
160 delogo(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2,
161 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
162 mpi->flags&MP_IMGFLAG_DIRECT);
163 delogo(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2,
164 vf->priv->xoff/2, vf->priv->yoff/2, vf->priv->lw/2, vf->priv->lh/2, vf->priv->band/2, vf->priv->show,
165 mpi->flags&MP_IMGFLAG_DIRECT);
167 vf_clone_mpi_attributes(dmpi, mpi);
169 return vf_next_put_image(vf,dmpi, pts);
172 static void uninit(struct vf_instance_s* vf){
173 if(!vf->priv) return;
175 free(vf->priv);
176 vf->priv=NULL;
179 //===========================================================================//
181 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
182 switch(fmt)
184 case IMGFMT_YV12:
185 case IMGFMT_I420:
186 case IMGFMT_IYUV:
187 return vf_next_query_format(vf,vf->priv->outfmt);
189 return 0;
192 static unsigned int fmt_list[]={
193 IMGFMT_YV12,
194 IMGFMT_I420,
195 IMGFMT_IYUV,
199 static int open(vf_instance_t *vf, char* args){
200 int res;
202 vf->config=config;
203 vf->put_image=put_image;
204 vf->get_image=get_image;
205 vf->query_format=query_format;
206 vf->uninit=uninit;
207 if (!vf->priv)
209 vf->priv=malloc(sizeof(struct vf_priv_s));
210 memset(vf->priv, 0, sizeof(struct vf_priv_s));
213 if (args) res = sscanf(args, "%d:%d:%d:%d:%d",
214 &vf->priv->xoff, &vf->priv->yoff,
215 &vf->priv->lw, &vf->priv->lh,
216 &vf->priv->band);
217 if (args && (res != 5)) {
218 uninit(vf);
219 return 0; // bad syntax
222 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
223 vf->priv->xoff, vf->priv->yoff,
224 vf->priv->lw, vf->priv->lh,
225 vf->priv->band);
227 vf->priv->show = 0;
229 if (vf->priv->band < 0) {
230 vf->priv->band = 4;
231 vf->priv->show = 1;
235 vf->priv->lw += vf->priv->band*2;
236 vf->priv->lh += vf->priv->band*2;
237 vf->priv->xoff -= vf->priv->band;
238 vf->priv->yoff -= vf->priv->band;
240 // check csp:
241 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
242 if(!vf->priv->outfmt)
244 uninit(vf);
245 return 0; // no csp match :(
248 return 1;
251 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
252 static m_option_t vf_opts_fields[] = {
253 { "x", ST_OFF(xoff), CONF_TYPE_INT, 0, 0, 0, NULL },
254 { "y", ST_OFF(yoff), CONF_TYPE_INT, 0, 0, 0, NULL },
255 { "w", ST_OFF(lw), CONF_TYPE_INT, 0, 0, 0, NULL },
256 { "h", ST_OFF(lh), CONF_TYPE_INT, 0, 0, 0, NULL },
257 { "t", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL },
258 { "band", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, // alias
259 { NULL, NULL, 0, 0, 0, 0, NULL }
262 static m_struct_t vf_opts = {
263 "delogo",
264 sizeof(struct vf_priv_s),
265 &vf_priv_dflt,
266 vf_opts_fields
269 const vf_info_t vf_info_delogo = {
270 "simple logo remover",
271 "delogo",
272 "Jindrich Makovicka, Alex Beregszaszi",
274 open,
275 &vf_opts
278 //===========================================================================//