comparison libmpcodecs/vf_geq.c @ 20266:5f216140e72b

generic equation filter example: -vf 'geq=(p(X\,Y)+p(mod(Y*2-X*2\,W)\,mod(Y*2+X*2+sin((X-Y)/10/SW+N)*SW*20\,H)))/2'
author michael
date Mon, 16 Oct 2006 15:47:32 +0000
parents
children 9f7284368fa1
comparison
equal deleted inserted replaced
20265:3edea8a7f4e8 20266:5f216140e72b
1 /*
2 Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <inttypes.h>
24
25 #include "config.h"
26
27 #include "mp_msg.h"
28 #include "cpudetect.h"
29
30 #if 1
31 double ff_eval(char *s, double *const_value, const char **const_name,
32 double (**func1)(void *, double), const char **func1_name,
33 double (**func2)(void *, double, double), char **func2_name,
34 void *opaque);
35 #endif
36
37 // Needed to bring in lrintf.
38 #define HAVE_AV_CONFIG_H
39
40 #include "libavcodec/avcodec.h"
41 #include "libavcodec/dsputil.h"
42 #include "libavutil/common.h"
43
44 /* FIXME: common.h defines printf away when HAVE_AV_CONFIG
45 * is defined, but mp_image.h needs printf.
46 */
47 #undef printf
48
49 #ifdef HAVE_MALLOC_H
50 #include <malloc.h>
51 #endif
52
53 #include "img_format.h"
54 #include "mp_image.h"
55 #include "vf.h"
56 #include "libvo/fastmemcpy.h"
57
58
59 struct vf_priv_s {
60 char eq[3][200];
61 int framenum;
62 mp_image_t *mpi;
63 };
64
65 static int config(struct vf_instance_s* vf,
66 int width, int height, int d_width, int d_height,
67 unsigned int flags, unsigned int outfmt){
68 int i;
69
70 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
71 }
72
73 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
74 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
75 // ok, we can do pp in-place (or pp disabled):
76 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
77 mpi->type, mpi->flags, mpi->w, mpi->h);
78 mpi->planes[0]=vf->dmpi->planes[0];
79 mpi->stride[0]=vf->dmpi->stride[0];
80 mpi->width=vf->dmpi->width;
81 if(mpi->flags&MP_IMGFLAG_PLANAR){
82 mpi->planes[1]=vf->dmpi->planes[1];
83 mpi->planes[2]=vf->dmpi->planes[2];
84 mpi->stride[1]=vf->dmpi->stride[1];
85 mpi->stride[2]=vf->dmpi->stride[2];
86 }
87 mpi->flags|=MP_IMGFLAG_DIRECT;
88 }
89
90 //FIXME spatial interpolate
91 //FIXME keep the last few frames
92 static double lum(struct vf_instance_s* vf, double x, double y){
93 mp_image_t *mpi= vf->priv->mpi;
94 x= clip(x, 0, vf->priv->mpi->w-1);
95 y= clip(y, 0, vf->priv->mpi->h-1);
96 return mpi->planes[0][(int)x + (int)y * mpi->stride[0]];
97 }
98
99 static double cb(struct vf_instance_s* vf, double x, double y){
100 mp_image_t *mpi= vf->priv->mpi;
101 x= clip(x, 0, (vf->priv->mpi->w >> mpi->chroma_x_shift)-1);
102 y= clip(y, 0, (vf->priv->mpi->h >> mpi->chroma_y_shift)-1);
103 return mpi->planes[1][(int)x + (int)y * mpi->stride[1]];
104 }
105
106 static double cr(struct vf_instance_s* vf, double x, double y){
107 mp_image_t *mpi= vf->priv->mpi;
108 x= clip(x, 0, (vf->priv->mpi->w >> mpi->chroma_x_shift)-1);
109 y= clip(y, 0, (vf->priv->mpi->h >> mpi->chroma_y_shift)-1);
110 return mpi->planes[2][(int)x + (int)y * mpi->stride[2]];
111 }
112
113 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
114 mp_image_t *dmpi;
115 int x,y, plane;
116 static const char *const_names[]={
117 "PI",
118 "E",
119 "X",
120 "Y",
121 "W",
122 "H",
123 "N",
124 "SW",
125 "SH",
126 NULL
127 };
128 static const char *func2_names[]={
129 "lum",
130 "cb",
131 "cr",
132 "p",
133 NULL
134 };
135
136 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
137 // no DR, so get a new image! hope we'll get DR buffer:
138 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
139 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
140 mpi->w,mpi->h);
141 }
142
143 dmpi= vf->dmpi;
144 vf->priv->mpi= mpi;
145
146 vf_clone_mpi_attributes(dmpi, mpi);
147
148 for(plane=0; plane<3; plane++){
149 int w= mpi->w >> (plane ? mpi->chroma_x_shift : 0);
150 int h= mpi->h >> (plane ? mpi->chroma_y_shift : 0);
151 uint8_t *dst = dmpi->planes[plane];
152 int dst_stride= dmpi->stride[plane];
153 double (*func2[])(void *, double, double)={
154 lum,
155 cb,
156 cr,
157 plane==0 ? lum : (plane==1 ? cb : cr),
158 NULL
159 };
160 double const_values[]={
161 M_PI,
162 M_E,
163 0,
164 0,
165 w,
166 h,
167 vf->priv->framenum,
168 w/(double)mpi->w,
169 h/(double)mpi->h,
170 0
171 };
172 for(y=0; y<h; y++){
173 const_values[3]=y;
174 for(x=0; x<w; x++){
175 const_values[2]=x;
176 dst[x+y* dst_stride]= ff_eval(vf->priv->eq[plane], const_values, const_names, NULL, NULL, func2, func2_names, vf);
177 }
178 }
179 }
180
181 vf->priv->framenum++;
182
183 return vf_next_put_image(vf,dmpi, pts);
184 }
185
186 static void uninit(struct vf_instance_s* vf){
187 if(!vf->priv) return;
188
189 av_free(vf->priv);
190 vf->priv=NULL;
191 }
192
193 //===========================================================================//
194 static int open(vf_instance_t *vf, char* args){
195 vf->config=config;
196 vf->put_image=put_image;
197 // vf->get_image=get_image;
198 vf->uninit=uninit;
199 vf->priv=av_malloc(sizeof(struct vf_priv_s));
200 memset(vf->priv, 0, sizeof(struct vf_priv_s));
201
202 if (args) sscanf(args, "%199s:%199s:%199s", vf->priv->eq[0], vf->priv->eq[1], vf->priv->eq[2]);
203
204 if(!vf->priv->eq[1][0]) strncpy(vf->priv->eq[1], vf->priv->eq[0], 199);
205 if(!vf->priv->eq[2][0]) strncpy(vf->priv->eq[2], vf->priv->eq[1], 199);
206
207 return 1;
208 }
209
210 vf_info_t vf_info_geq = {
211 "generic equation filter",
212 "geq",
213 "Michael Niedermayer",
214 "",
215 open,
216 NULL
217 };