Add support for VDPAU video out, including hardware decoding.
[mplayer/glamo.git] / libmpcodecs / vf_geq.c
blob21da7e200048141a755b2f15128ee9b6f420aa90
1 /*
2 * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <inttypes.h>
27 #include "config.h"
29 #include "mp_msg.h"
30 #include "cpudetect.h"
32 #include "img_format.h"
33 #include "mp_image.h"
34 #include "vf.h"
36 #include "libavcodec/avcodec.h"
37 #include "libavcodec/eval.h"
39 struct vf_priv_s {
40 AVEvalExpr * e[3];
41 int framenum;
42 mp_image_t *mpi;
45 static int config(struct vf_instance_s* vf,
46 int width, int height, int d_width, int d_height,
47 unsigned int flags, unsigned int outfmt){
48 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
51 static inline double getpix(struct vf_instance_s* vf, double x, double y, int plane){
52 int xi, yi;
53 mp_image_t *mpi= vf->priv->mpi;
54 int stride= mpi->stride[plane];
55 uint8_t *src= mpi->planes[plane];
56 xi=x= FFMIN(FFMAX(x, 0), (mpi->w >> (plane ? mpi->chroma_x_shift : 0))-1);
57 yi=y= FFMIN(FFMAX(y, 0), (mpi->h >> (plane ? mpi->chroma_y_shift : 0))-1);
59 x-=xi;
60 y-=yi;
62 return
63 (1-y)*((1-x)*src[xi + yi * stride] + x*src[xi + 1 + yi * stride])
64 + y *((1-x)*src[xi + (yi+1) * stride] + x*src[xi + 1 + (yi+1) * stride]);
67 //FIXME cubic interpolate
68 //FIXME keep the last few frames
69 static double lum(struct vf_instance_s* vf, double x, double y){
70 return getpix(vf, x, y, 0);
73 static double cb(struct vf_instance_s* vf, double x, double y){
74 return getpix(vf, x, y, 1);
77 static double cr(struct vf_instance_s* vf, double x, double y){
78 return getpix(vf, x, y, 2);
81 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
82 mp_image_t *dmpi;
83 int x,y, plane;
85 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
86 // no DR, so get a new image! hope we'll get DR buffer:
87 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, MP_IMGTYPE_TEMP,
88 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
89 mpi->w,mpi->h);
92 dmpi= vf->dmpi;
93 vf->priv->mpi= mpi;
95 vf_clone_mpi_attributes(dmpi, mpi);
97 for(plane=0; plane<3; plane++){
98 int w= mpi->w >> (plane ? mpi->chroma_x_shift : 0);
99 int h= mpi->h >> (plane ? mpi->chroma_y_shift : 0);
100 uint8_t *dst = dmpi->planes[plane];
101 int dst_stride= dmpi->stride[plane];
102 double const_values[]={
103 M_PI,
104 M_E,
109 vf->priv->framenum,
110 w/(double)mpi->w,
111 h/(double)mpi->h,
114 if (!vf->priv->e[plane]) continue;
115 for(y=0; y<h; y++){
116 const_values[3]=y;
117 for(x=0; x<w; x++){
118 const_values[2]=x;
119 dst[x+y* dst_stride]= ff_parse_eval(vf->priv->e[plane], const_values, vf);
124 vf->priv->framenum++;
126 return vf_next_put_image(vf,dmpi, pts);
129 static void uninit(struct vf_instance_s* vf){
130 if(!vf->priv) return;
132 av_free(vf->priv);
133 vf->priv=NULL;
136 //===========================================================================//
137 static int open(vf_instance_t *vf, char* args){
138 char eq[3][2000] = { { 0 }, { 0 }, { 0 } };
139 int plane;
141 vf->config=config;
142 vf->put_image=put_image;
143 // vf->get_image=get_image;
144 vf->uninit=uninit;
145 vf->priv=av_malloc(sizeof(struct vf_priv_s));
146 memset(vf->priv, 0, sizeof(struct vf_priv_s));
148 if (args) sscanf(args, "%1999[^:]:%1999[^:]:%1999[^:]", eq[0], eq[1], eq[2]);
150 if (!eq[1][0]) strncpy(eq[1], eq[0], sizeof(eq[0])-1);
151 if (!eq[2][0]) strncpy(eq[2], eq[1], sizeof(eq[0])-1);
153 for(plane=0; plane<3; plane++){
154 static const char *const_names[]={
155 "PI",
156 "E",
157 "X",
158 "Y",
159 "W",
160 "H",
161 "N",
162 "SW",
163 "SH",
164 NULL
166 static const char *func2_names[]={
167 "lum",
168 "cb",
169 "cr",
170 "p",
171 NULL
173 double (*func2[])(void *, double, double)={
174 lum,
177 plane==0 ? lum : (plane==1 ? cb : cr),
178 NULL
180 char * a;
181 vf->priv->e[plane] = ff_parse(eq[plane], const_names, NULL, NULL, func2, func2_names, &a);
183 if (!vf->priv->e[plane]) {
184 mp_msg(MSGT_VFILTER, MSGL_ERR, "geq: error loading equation `%s': %s\n", eq[plane], a);
188 return 1;
191 const vf_info_t vf_info_geq = {
192 "generic equation filter",
193 "geq",
194 "Michael Niedermayer",
196 open,
197 NULL