Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_delogo.c
blob926d0dd56df87732c6d950869b324ab47fcbae0f
1 /*
2 Copyright (C) 2002 Jindrich Makovicka <makovick@gmail.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 /* A very simple tv station logo remover */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <math.h>
27 #include "config.h"
28 #include "mp_msg.h"
29 #include "cpudetect.h"
31 #ifdef HAVE_MALLOC_H
32 #include <malloc.h>
33 #endif
35 #include "img_format.h"
36 #include "mp_image.h"
37 #include "vf.h"
38 #include "libvo/fastmemcpy.h"
40 #include "m_option.h"
41 #include "m_struct.h"
43 //===========================================================================//
45 static struct vf_priv_s {
46 unsigned int outfmt;
47 int xoff, yoff, lw, lh, band, show;
48 } const vf_priv_dflt = {
50 0, 0, 0, 0, 0, 0
53 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
54 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
56 static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height,
57 int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) {
58 int y, x;
59 int interp, dist;
60 uint8_t *xdst, *xsrc;
62 uint8_t *topleft, *botleft, *topright;
63 int xclipl, xclipr, yclipt, yclipb;
64 int logo_x1, logo_x2, logo_y1, logo_y2;
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);
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;
76 topleft = src+logo_y1*srcStride+logo_x1;
77 topright = src+logo_y1*srcStride+logo_x2-1;
78 botleft = src+(logo_y2-1)*srcStride+logo_x1;
80 if (!direct) memcpy_pic(dst, src, width, height, dstStride, srcStride);
82 dst += (logo_y1+1)*dstStride;
83 src += (logo_y1+1)*srcStride;
85 for(y = logo_y1+1; y < logo_y2-1; y++)
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;
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;*/
106 if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) {
107 *xdst = interp;
108 } else {
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;
119 dst+= dstStride;
120 src+= srcStride;
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){
128 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
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];
147 mpi->flags|=MP_IMGFLAG_DIRECT;
150 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
151 mp_image_t *dmpi;
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);
159 dmpi= vf->dmpi;
161 delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h,
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);
164 delogo(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2,
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);
167 delogo(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2,
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);
171 vf_clone_mpi_attributes(dmpi, mpi);
173 return vf_next_put_image(vf,dmpi, pts);
176 static void uninit(struct vf_instance_s* vf){
177 if(!vf->priv) return;
179 free(vf->priv);
180 vf->priv=NULL;
183 //===========================================================================//
185 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
186 switch(fmt)
188 case IMGFMT_YV12:
189 case IMGFMT_I420:
190 case IMGFMT_IYUV:
191 return vf_next_query_format(vf,vf->priv->outfmt);
193 return 0;
196 static unsigned int fmt_list[]={
197 IMGFMT_YV12,
198 IMGFMT_I420,
199 IMGFMT_IYUV,
203 static int open(vf_instance_t *vf, char* args){
204 int res;
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;
211 if (!vf->priv)
213 vf->priv=malloc(sizeof(struct vf_priv_s));
214 memset(vf->priv, 0, sizeof(struct vf_priv_s));
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);
221 if (args && (res != 5)) {
222 uninit(vf);
223 return 0; // bad syntax
226 mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
227 vf->priv->xoff, vf->priv->yoff,
228 vf->priv->lw, vf->priv->lh,
229 vf->priv->band);
231 vf->priv->show = 0;
233 if (vf->priv->band < 0) {
234 vf->priv->band = 4;
235 vf->priv->show = 1;
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;
244 // check csp:
245 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
246 if(!vf->priv->outfmt)
248 uninit(vf);
249 return 0; // no csp match :(
252 return 1;
255 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
256 static m_option_t vf_opts_fields[] = {
257 { "x", ST_OFF(xoff), CONF_TYPE_INT, 0, 0, 0, NULL },
258 { "y", ST_OFF(yoff), CONF_TYPE_INT, 0, 0, 0, NULL },
259 { "w", ST_OFF(lw), CONF_TYPE_INT, 0, 0, 0, NULL },
260 { "h", ST_OFF(lh), CONF_TYPE_INT, 0, 0, 0, NULL },
261 { "t", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL },
262 { "band", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, // alias
263 { NULL, NULL, 0, 0, 0, 0, NULL }
266 static m_struct_t vf_opts = {
267 "delogo",
268 sizeof(struct vf_priv_s),
269 &vf_priv_dflt,
270 vf_opts_fields
273 const vf_info_t vf_info_delogo = {
274 "simple logo remover",
275 "delogo",
276 "Jindrich Makovicka, Alex Beregszaszi",
278 open,
279 &vf_opts
282 //===========================================================================//