Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_perspective.c
blob2ced1f73053f8d17422ddee03edba42053337634
1 /*
2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <assert.h>
24 #include <math.h>
26 #include "config.h"
27 #include "mp_msg.h"
29 #ifdef HAVE_MALLOC_H
30 #include <malloc.h>
31 #endif
33 #include "img_format.h"
34 #include "mp_image.h"
35 #include "vf.h"
37 #define SUB_PIXEL_BITS 8
38 #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
39 #define COEFF_BITS 11
41 //===========================================================================//
43 struct vf_priv_s {
44 double ref[4][2];
45 int32_t coeff[1<<SUB_PIXEL_BITS][4];
46 int32_t (*pv)[2];
47 int pvStride;
48 int cubic;
52 /***************************************************************************/
54 static void initPv(struct vf_priv_s *priv, int W, int H){
55 double a,b,c,d,e,f,g,h,D;
56 double (*ref)[2]= priv->ref;
57 int x,y;
59 g= ( (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1])
60 - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H;
61 h= ( (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0])
62 - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W;
63 D= (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1])
64 - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]);
66 a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0];
67 b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0];
68 c= D*ref[0][0]*W*H;
69 d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1];
70 e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1];
71 f= D*ref[0][1]*W*H;
73 for(y=0; y<H; y++){
74 for(x=0; x<W; x++){
75 int u, v;
77 u= (int)floor( SUB_PIXELS*(a*x + b*y + c)/(g*x + h*y + D*W*H) + 0.5);
78 v= (int)floor( SUB_PIXELS*(d*x + e*y + f)/(g*x + h*y + D*W*H) + 0.5);
80 priv->pv[x + y*W][0]= u;
81 priv->pv[x + y*W][1]= v;
86 static double getCoeff(double d){
87 double A= -0.60;
88 double coeff;
90 d= fabs(d);
92 // Equation is from VirtualDub
93 if(d<1.0)
94 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
95 else if(d<2.0)
96 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
97 else
98 coeff=0.0;
100 return coeff;
103 static int config(struct vf_instance_s* vf,
104 int width, int height, int d_width, int d_height,
105 unsigned int flags, unsigned int outfmt){
106 int i, j;
108 vf->priv->pvStride= width;
109 vf->priv->pv= (void*)memalign(8, width*height*2*sizeof(int32_t));
110 initPv(vf->priv, width, height);
112 for(i=0; i<SUB_PIXELS; i++){
113 double d= i/(double)SUB_PIXELS;
114 double temp[4];
115 double sum=0;
117 for(j=0; j<4; j++)
118 temp[j]= getCoeff(j - d - 1);
120 for(j=0; j<4; j++)
121 sum+= temp[j];
123 for(j=0; j<4; j++)
124 vf->priv->coeff[i][j]= (int)floor((1<<COEFF_BITS)*temp[j]/sum + 0.5);
127 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
130 static void uninit(struct vf_instance_s* vf){
131 if(!vf->priv) return;
133 if(vf->priv->pv) free(vf->priv->pv);
134 vf->priv->pv= NULL;
136 free(vf->priv);
137 vf->priv=NULL;
140 static inline void resampleCubic(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, struct vf_priv_s *privParam, int xShift, int yShift){
141 int x, y;
142 struct vf_priv_s priv= *privParam;
144 for(y=0; y<h; y++){
145 for(x=0; x<w; x++){
146 int u, v, subU, subV, sum, sx, sy;
148 sx= x << xShift;
149 sy= y << yShift;
150 u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
151 v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
152 subU= u & (SUB_PIXELS-1);
153 subV= v & (SUB_PIXELS-1);
154 u >>= SUB_PIXEL_BITS;
155 v >>= SUB_PIXEL_BITS;
157 if(u>0 && v>0 && u<w-2 && v<h-2){
158 const int index= u + v*srcStride;
159 const int a= priv.coeff[subU][0];
160 const int b= priv.coeff[subU][1];
161 const int c= priv.coeff[subU][2];
162 const int d= priv.coeff[subU][3];
164 sum=
165 priv.coeff[subV][0]*( a*src[index - 1 - srcStride] + b*src[index - 0 - srcStride]
166 + c*src[index + 1 - srcStride] + d*src[index + 2 - srcStride])
167 +priv.coeff[subV][1]*( a*src[index - 1 ] + b*src[index - 0 ]
168 + c*src[index + 1 ] + d*src[index + 2 ])
169 +priv.coeff[subV][2]*( a*src[index - 1 + srcStride] + b*src[index - 0 + srcStride]
170 + c*src[index + 1 + srcStride] + d*src[index + 2 + srcStride])
171 +priv.coeff[subV][3]*( a*src[index - 1+2*srcStride] + b*src[index - 0+2*srcStride]
172 + c*src[index + 1+2*srcStride] + d*src[index + 2+2*srcStride]);
173 }else{
174 int dx, dy;
175 sum=0;
177 for(dy=0; dy<4; dy++){
178 int iy= v + dy - 1;
179 if (iy< 0) iy=0;
180 else if(iy>=h) iy=h-1;
181 for(dx=0; dx<4; dx++){
182 int ix= u + dx - 1;
183 if (ix< 0) ix=0;
184 else if(ix>=w) ix=w-1;
186 sum+= priv.coeff[subU][dx]*priv.coeff[subV][dy]
187 *src[ ix + iy*srcStride];
191 sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2);
192 if(sum&~255){
193 if(sum<0) sum=0;
194 else sum=255;
196 dst[ x + y*dstStride]= sum;
201 static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride,
202 struct vf_priv_s *privParam, int xShift, int yShift){
203 int x, y;
204 struct vf_priv_s priv= *privParam;
206 for(y=0; y<h; y++){
207 for(x=0; x<w; x++){
208 int u, v, subU, subV, sum, sx, sy, index, subUI, subVI;
210 sx= x << xShift;
211 sy= y << yShift;
212 u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
213 v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
214 subU= u & (SUB_PIXELS-1);
215 subV= v & (SUB_PIXELS-1);
216 u >>= SUB_PIXEL_BITS;
217 v >>= SUB_PIXEL_BITS;
218 index= u + v*srcStride;
219 subUI= SUB_PIXELS - subU;
220 subVI= SUB_PIXELS - subV;
222 if((unsigned)u < (unsigned)(w - 1)){
223 if((unsigned)v < (unsigned)(h - 1)){
224 sum= subVI*(subUI*src[index ] + subU*src[index +1])
225 +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]);
226 sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2);
227 }else{
228 if(v<0) v= 0;
229 else v= h-1;
230 index= u + v*srcStride;
231 sum= subUI*src[index] + subU*src[index+1];
232 sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
234 }else{
235 if((unsigned)v < (unsigned)(h - 1)){
236 if(u<0) u= 0;
237 else u= w-1;
238 index= u + v*srcStride;
239 sum= subVI*src[index] + subV*src[index+srcStride];
240 sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
241 }else{
242 if(u<0) u= 0;
243 else u= w-1;
244 if(v<0) v= 0;
245 else v= h-1;
246 index= u + v*srcStride;
247 sum= src[index];
250 if(sum&~255){
251 if(sum<0) sum=0;
252 else sum=255;
254 dst[ x + y*dstStride]= sum;
259 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
260 int cw= mpi->w >> mpi->chroma_x_shift;
261 int ch= mpi->h >> mpi->chroma_y_shift;
263 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
264 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
265 mpi->w,mpi->h);
267 assert(mpi->flags&MP_IMGFLAG_PLANAR);
269 if(vf->priv->cubic){
270 resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
271 vf->priv, 0, 0);
272 resampleCubic(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
273 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
274 resampleCubic(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
275 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
276 }else{
277 resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
278 vf->priv, 0, 0);
279 resampleLinear(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
280 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
281 resampleLinear(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
282 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
285 return vf_next_put_image(vf,dmpi, pts);
288 //===========================================================================//
290 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
291 switch(fmt)
293 case IMGFMT_YV12:
294 case IMGFMT_I420:
295 case IMGFMT_IYUV:
296 case IMGFMT_YVU9:
297 case IMGFMT_444P:
298 case IMGFMT_422P:
299 case IMGFMT_411P:
300 return vf_next_query_format(vf, fmt);
302 return 0;
305 static int open(vf_instance_t *vf, char* args){
306 int e;
308 vf->config=config;
309 vf->put_image=put_image;
310 // vf->get_image=get_image;
311 vf->query_format=query_format;
312 vf->uninit=uninit;
313 vf->priv=malloc(sizeof(struct vf_priv_s));
314 memset(vf->priv, 0, sizeof(struct vf_priv_s));
316 if(args==NULL) return 0;
318 e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d",
319 &vf->priv->ref[0][0], &vf->priv->ref[0][1],
320 &vf->priv->ref[1][0], &vf->priv->ref[1][1],
321 &vf->priv->ref[2][0], &vf->priv->ref[2][1],
322 &vf->priv->ref[3][0], &vf->priv->ref[3][1],
323 &vf->priv->cubic
326 if(e!=9)
327 return 0;
329 return 1;
332 const vf_info_t vf_info_perspective = {
333 "perspective correcture",
334 "perspective",
335 "Michael Niedermayer",
337 open,
338 NULL
341 //===========================================================================//