20266
|
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 // Needed to bring in lrintf.
|
|
31 #define HAVE_AV_CONFIG_H
|
|
32
|
|
33 #include "libavcodec/avcodec.h"
|
|
34 #include "libavcodec/dsputil.h"
|
20455
|
35 #include "libavcodec/eval.h"
|
20266
|
36 #include "libavutil/common.h"
|
|
37
|
|
38 /* FIXME: common.h defines printf away when HAVE_AV_CONFIG
|
|
39 * is defined, but mp_image.h needs printf.
|
|
40 */
|
|
41 #undef printf
|
|
42
|
|
43 #ifdef HAVE_MALLOC_H
|
|
44 #include <malloc.h>
|
|
45 #endif
|
|
46
|
|
47 #include "img_format.h"
|
|
48 #include "mp_image.h"
|
|
49 #include "vf.h"
|
|
50 #include "libvo/fastmemcpy.h"
|
|
51
|
|
52
|
|
53 struct vf_priv_s {
|
20456
|
54 AVEvalExpr * e[3];
|
|
55 int framenum;
|
|
56 mp_image_t *mpi;
|
20266
|
57 };
|
|
58
|
|
59 static int config(struct vf_instance_s* vf,
|
|
60 int width, int height, int d_width, int d_height,
|
20456
|
61 unsigned int flags, unsigned int outfmt){
|
|
62 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
20266
|
63 }
|
|
64
|
|
65 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
66 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
|
|
67 // ok, we can do pp in-place (or pp disabled):
|
|
68 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
69 mpi->type, mpi->flags, mpi->w, mpi->h);
|
|
70 mpi->planes[0]=vf->dmpi->planes[0];
|
|
71 mpi->stride[0]=vf->dmpi->stride[0];
|
|
72 mpi->width=vf->dmpi->width;
|
|
73 if(mpi->flags&MP_IMGFLAG_PLANAR){
|
|
74 mpi->planes[1]=vf->dmpi->planes[1];
|
|
75 mpi->planes[2]=vf->dmpi->planes[2];
|
20456
|
76 mpi->stride[1]=vf->dmpi->stride[1];
|
|
77 mpi->stride[2]=vf->dmpi->stride[2];
|
20266
|
78 }
|
|
79 mpi->flags|=MP_IMGFLAG_DIRECT;
|
|
80 }
|
|
81
|
20270
|
82 static double inline getpix(struct vf_instance_s* vf, double x, double y, int plane){
|
|
83 int xi, yi;
|
|
84 mp_image_t *mpi= vf->priv->mpi;
|
|
85 int stride= mpi->stride[plane];
|
|
86 uint8_t *src= mpi->planes[plane];
|
|
87 xi=x= FFMIN(FFMAX(x, 0), (mpi->w >> (plane ? mpi->chroma_x_shift : 0))-1);
|
|
88 yi=y= FFMIN(FFMAX(y, 0), (mpi->h >> (plane ? mpi->chroma_y_shift : 0))-1);
|
|
89
|
|
90 x-=xi;
|
|
91 y-=yi;
|
|
92
|
|
93 return
|
|
94 (1-y)*((1-x)*src[xi + yi * stride] + x*src[xi + 1 + yi * stride])
|
|
95 + y *((1-x)*src[xi + (yi+1) * stride] + x*src[xi + 1 + (yi+1) * stride]);
|
|
96 }
|
|
97
|
|
98 //FIXME cubic interpolate
|
20266
|
99 //FIXME keep the last few frames
|
|
100 static double lum(struct vf_instance_s* vf, double x, double y){
|
20270
|
101 return getpix(vf, x, y, 0);
|
20266
|
102 }
|
|
103
|
|
104 static double cb(struct vf_instance_s* vf, double x, double y){
|
20270
|
105 return getpix(vf, x, y, 1);
|
20266
|
106 }
|
|
107
|
|
108 static double cr(struct vf_instance_s* vf, double x, double y){
|
20270
|
109 return getpix(vf, x, y, 2);
|
20266
|
110 }
|
|
111
|
|
112 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
|
20456
|
113 mp_image_t *dmpi;
|
|
114 int x,y, plane;
|
20455
|
115
|
20456
|
116 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
|
|
117 // no DR, so get a new image! hope we'll get DR buffer:
|
|
118 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, MP_IMGTYPE_TEMP,
|
|
119 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
|
|
120 mpi->w,mpi->h);
|
|
121 }
|
20455
|
122
|
20456
|
123 dmpi= vf->dmpi;
|
|
124 vf->priv->mpi= mpi;
|
20455
|
125
|
20456
|
126 vf_clone_mpi_attributes(dmpi, mpi);
|
20455
|
127
|
20456
|
128 for(plane=0; plane<3; plane++){
|
|
129 int w= mpi->w >> (plane ? mpi->chroma_x_shift : 0);
|
|
130 int h= mpi->h >> (plane ? mpi->chroma_y_shift : 0);
|
|
131 uint8_t *dst = dmpi->planes[plane];
|
|
132 int dst_stride= dmpi->stride[plane];
|
|
133 double const_values[]={
|
|
134 M_PI,
|
|
135 M_E,
|
|
136 0,
|
|
137 0,
|
|
138 w,
|
|
139 h,
|
|
140 vf->priv->framenum,
|
|
141 w/(double)mpi->w,
|
|
142 h/(double)mpi->h,
|
|
143 0
|
|
144 };
|
|
145 if (!vf->priv->e[plane]) continue;
|
|
146 for(y=0; y<h; y++){
|
|
147 const_values[3]=y;
|
|
148 for(x=0; x<w; x++){
|
|
149 const_values[2]=x;
|
|
150 dst[x+y* dst_stride]= ff_parse_eval(vf->priv->e[plane], const_values, vf);
|
20455
|
151 }
|
|
152 }
|
20456
|
153 }
|
20455
|
154
|
20456
|
155 vf->priv->framenum++;
|
20455
|
156
|
20456
|
157 return vf_next_put_image(vf,dmpi, pts);
|
20455
|
158 }
|
|
159
|
|
160 static void uninit(struct vf_instance_s* vf){
|
20456
|
161 if(!vf->priv) return;
|
20455
|
162
|
20456
|
163 av_free(vf->priv);
|
|
164 vf->priv=NULL;
|
20455
|
165 }
|
|
166
|
|
167 //===========================================================================//
|
|
168 static int open(vf_instance_t *vf, char* args){
|
|
169 char eq[3][2000] = { { 0 }, { 0 }, { 0 } };
|
|
170 int plane;
|
|
171
|
|
172 vf->config=config;
|
|
173 vf->put_image=put_image;
|
|
174 // vf->get_image=get_image;
|
|
175 vf->uninit=uninit;
|
|
176 vf->priv=av_malloc(sizeof(struct vf_priv_s));
|
|
177 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
178
|
20466
|
179 if (args) sscanf(args, "%1999[^:]:%1999[^:]:%1999[^:]", eq[0], eq[1], eq[2]);
|
20455
|
180
|
|
181 if (!eq[1][0]) strncpy(eq[1], eq[0], sizeof(eq[0])-1);
|
|
182 if (!eq[2][0]) strncpy(eq[2], eq[1], sizeof(eq[0])-1);
|
|
183
|
|
184 for(plane=0; plane<3; plane++){
|
20266
|
185 static const char *const_names[]={
|
|
186 "PI",
|
|
187 "E",
|
|
188 "X",
|
|
189 "Y",
|
|
190 "W",
|
|
191 "H",
|
|
192 "N",
|
|
193 "SW",
|
|
194 "SH",
|
|
195 NULL
|
|
196 };
|
|
197 static const char *func2_names[]={
|
|
198 "lum",
|
|
199 "cb",
|
|
200 "cr",
|
|
201 "p",
|
|
202 NULL
|
|
203 };
|
20455
|
204 double (*func2[])(void *, double, double)={
|
|
205 lum,
|
|
206 cb,
|
|
207 cr,
|
|
208 plane==0 ? lum : (plane==1 ? cb : cr),
|
|
209 NULL
|
|
210 };
|
|
211 char * a;
|
|
212 vf->priv->e[plane] = ff_parse(eq[plane], const_names, NULL, NULL, func2, func2_names, &a);
|
20266
|
213
|
20455
|
214 if (!vf->priv->e[plane]) {
|
|
215 mp_msg(MSGT_VFILTER, MSGL_ERR, "geq: error loading equation `%s': %s\n", eq[plane], a);
|
20266
|
216 }
|
20455
|
217 }
|
20266
|
218
|
|
219 return 1;
|
|
220 }
|
|
221
|
|
222 vf_info_t vf_info_geq = {
|
|
223 "generic equation filter",
|
|
224 "geq",
|
|
225 "Michael Niedermayer",
|
|
226 "",
|
|
227 open,
|
|
228 NULL
|
|
229 };
|