1 /*****************************************************************************
2 * Psychedelic.c : Psychedelic video effect plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Antoine Cellerier <dionoea -at- videolan -dot- org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <math.h> /* sin(), cos() */
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include "vlc_filter.h"
39 #include "vlc_image.h"
40 #include "filter_picture.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
48 static picture_t *Filter( filter_t *, picture_t * );
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( N_("Psychedelic video filter") );
55 set_shortname( N_( "Psychedelic" ));
56 set_capability( "video filter2", 0 );
57 set_category( CAT_VIDEO );
58 set_subcategory( SUBCAT_VIDEO_VFILTER );
60 add_shortcut( "psychedelic" );
61 set_callbacks( Create, Destroy );
64 /*****************************************************************************
65 * vout_sys_t: Distort video output method descriptor
66 *****************************************************************************
67 * This structure is part of the video output thread descriptor.
68 * It describes the Distort specific properties of an output thread.
69 *****************************************************************************/
72 image_handler_t *p_image;
73 unsigned int x, y, scale;
74 int xinc, yinc, scaleinc;
78 /*****************************************************************************
79 * Create: allocates Distort video thread output method
80 *****************************************************************************
81 * This function allocates and initializes a Distort vout method.
82 *****************************************************************************/
83 static int Create( vlc_object_t *p_this )
85 filter_t *p_filter = (filter_t *)p_this;
87 /* Allocate structure */
88 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
89 if( p_filter->p_sys == NULL )
91 msg_Err( p_filter, "out of memory" );
95 p_filter->pf_video_filter = Filter;
97 p_filter->p_sys->x = 10;
98 p_filter->p_sys->y = 10;
99 p_filter->p_sys->scale = 1;
100 p_filter->p_sys->xinc = 1;
101 p_filter->p_sys->yinc = 1;
102 p_filter->p_sys->scaleinc = 1;
103 p_filter->p_sys->u = 0;
104 p_filter->p_sys->v = 0;
105 p_filter->p_sys->p_image = NULL;
110 /*****************************************************************************
111 * Destroy: destroy Distort video thread output method
112 *****************************************************************************
113 * Terminate an output method created by DistortCreateOutputMethod
114 *****************************************************************************/
115 static void Destroy( vlc_object_t *p_this )
117 filter_t *p_filter = (filter_t *)p_this;
119 if( p_filter->p_sys->p_image )
120 image_HandlerDelete( p_filter->p_sys->p_image );
121 p_filter->p_sys->p_image = NULL;
123 free( p_filter->p_sys );
126 /*****************************************************************************
127 * Render: displays previously rendered output
128 *****************************************************************************
129 * This function send the currently rendered image to Distort image, waits
130 * until it is displayed and switch the two rendering buffers, preparing next
132 *****************************************************************************/
133 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
141 picture_t *p_converted;
142 video_format_t fmt_out;
143 memset( &fmt_out, 0, sizeof(video_format_t) );
144 fmt_out.p_palette = NULL;
146 if( !p_pic ) return NULL;
148 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
151 msg_Warn( p_filter, "can't get output picture" );
152 if( p_pic->pf_release )
153 p_pic->pf_release( p_pic );
157 if( !p_filter->p_sys->p_image )
158 p_filter->p_sys->p_image = image_HandlerCreate( p_filter );
161 u = p_filter->p_sys->u;
162 v = p_filter->p_sys->v;
163 for( y = 0; y<p_outpic->p[U_PLANE].i_lines; y++)
166 p_outpic->p[U_PLANE].p_pixels+y*p_outpic->p[U_PLANE].i_pitch,
167 u, p_outpic->p[U_PLANE].i_pitch );
169 p_outpic->p[V_PLANE].p_pixels+y*p_outpic->p[V_PLANE].i_pitch,
170 v, p_outpic->p[V_PLANE].i_pitch );
171 if( v == 0 && u != 0 )
182 vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_pic->p[Y_PLANE].p_pixels,
183 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
185 /* image visualization */
186 fmt_out = p_filter->fmt_out.video;
187 fmt_out.i_width = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
188 fmt_out.i_height = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
189 p_converted = image_Convert( p_filter->p_sys->p_image, p_pic,
190 &(p_pic->format), &fmt_out );
194 #define copyimage( plane, b ) \
195 for( y=0; y<p_converted->p[plane].i_visible_lines; y++) { \
196 for( x=0; x<p_converted->p[plane].i_visible_pitch; x++) { \
198 if( p_filter->p_sys->yinc == 1 ) \
201 ny = p_converted->p[plane].i_visible_lines-y; \
202 if( p_filter->p_sys->xinc == 1 ) \
205 nx = p_converted->p[plane].i_visible_pitch-x; \
206 p_outpic->p[plane].p_pixels[(p_filter->p_sys->x*b+nx)+(ny+p_filter->p_sys->y*b)*p_outpic->p[plane].i_pitch ] = p_converted->p[plane].p_pixels[y*p_converted->p[plane].i_pitch+x]; \
208 copyimage( Y_PLANE, 2 );
209 copyimage( U_PLANE, 1 );
210 copyimage( V_PLANE, 1 );
213 p_converted->pf_release( p_converted );
217 msg_Err( p_filter, "Image scaling failed miserably." );
220 p_filter->p_sys->x += p_filter->p_sys->xinc;
221 p_filter->p_sys->y += p_filter->p_sys->yinc;
223 p_filter->p_sys->scale += p_filter->p_sys->scaleinc;
224 if( p_filter->p_sys->scale >= 50 ) p_filter->p_sys->scaleinc = -1;
225 if( p_filter->p_sys->scale <= 1 ) p_filter->p_sys->scaleinc = 1;
227 w = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
228 h = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
229 if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
230 p_filter->p_sys->xinc = -1;
231 if( p_filter->p_sys->x <= 0 )
232 p_filter->p_sys->xinc = 1;
234 if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
235 p_filter->p_sys->x = (p_filter->fmt_out.video.i_width-w)/2;
236 if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
237 p_filter->p_sys->y = (p_filter->fmt_out.video.i_height-h)/2;
239 if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
240 p_filter->p_sys->yinc = -1;
241 if( p_filter->p_sys->y <= 0 )
242 p_filter->p_sys->yinc = 1;
244 for( y = 0; y< 16; y++ )
246 if( p_filter->p_sys->v == 0 && p_filter->p_sys->u != 0 )
247 p_filter->p_sys->u -= 1;
248 else if( p_filter->p_sys->u == 0xff )
249 p_filter->p_sys->v -= 1;
250 else if( p_filter->p_sys->v == 0xff )
251 p_filter->p_sys->u += 1;
252 else if( p_filter->p_sys->u == 0 )
253 p_filter->p_sys->v += 1;
256 return CopyInfoAndRelease( p_outpic, p_pic );