lib: media_player: fix libvlc_MediaPlayerMediaChanged event
[vlc.git] / modules / video_filter / psychedelic.c
blob94c57641227e47d964c565728773f0715b6cc3dd
1 /*****************************************************************************
2 * psychedelic.c : Psychedelic video effect plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <math.h> /* sin(), cos() */
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37 #include <vlc_picture.h>
38 #include <vlc_image.h>
39 #include "filter_picture.h"
41 /*****************************************************************************
42 * Local prototypes
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static picture_t *Filter( filter_t *, picture_t * );
49 /*****************************************************************************
50 * Module descriptor
51 *****************************************************************************/
52 vlc_module_begin ()
53 set_description( N_("Psychedelic video filter") )
54 set_shortname( N_( "Psychedelic" ))
55 set_capability( "video filter", 0 )
56 set_category( CAT_VIDEO )
57 set_subcategory( SUBCAT_VIDEO_VFILTER )
59 add_shortcut( "psychedelic" )
60 set_callbacks( Create, Destroy )
61 vlc_module_end ()
63 /*****************************************************************************
64 * filter_sys_t: Distort video output method descriptor
65 *****************************************************************************
66 * This structure is part of the video output thread descriptor.
67 * It describes the Distort specific properties of an output thread.
68 *****************************************************************************/
69 typedef struct
71 image_handler_t *p_image;
72 unsigned int x, y, scale;
73 int xinc, yinc, scaleinc;
74 uint8_t u,v;
75 } filter_sys_t;
77 /*****************************************************************************
78 * Create: allocates Distort video thread output method
79 *****************************************************************************
80 * This function allocates and initializes a Distort vout method.
81 *****************************************************************************/
82 static int Create( vlc_object_t *p_this )
84 filter_t *p_filter = (filter_t *)p_this;
86 const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
87 const vlc_chroma_description_t *p_chroma = vlc_fourcc_GetChromaDescription( fourcc );
88 if( !p_chroma || p_chroma->plane_count != 3 || p_chroma->pixel_size != 1 ) {
89 msg_Err( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
90 return VLC_EGENERIC;
93 /* Allocate structure */
94 filter_sys_t *p_sys = malloc( sizeof( filter_sys_t ) );
95 if( p_sys == NULL )
96 return VLC_ENOMEM;
97 p_filter->p_sys = p_sys;
99 p_filter->pf_video_filter = Filter;
101 p_sys->x = 10;
102 p_sys->y = 10;
103 p_sys->scale = 1;
104 p_sys->xinc = 1;
105 p_sys->yinc = 1;
106 p_sys->scaleinc = 1;
107 p_sys->u = 0;
108 p_sys->v = 0;
109 p_sys->p_image = NULL;
111 return VLC_SUCCESS;
114 /*****************************************************************************
115 * Destroy: destroy Distort video thread output method
116 *****************************************************************************
117 * Terminate an output method created by DistortCreateOutputMethod
118 *****************************************************************************/
119 static void Destroy( vlc_object_t *p_this )
121 filter_t *p_filter = (filter_t *)p_this;
122 filter_sys_t *p_sys = p_filter->p_sys;
124 if( p_sys->p_image )
125 image_HandlerDelete( p_sys->p_image );
126 p_sys->p_image = NULL;
128 free( p_sys );
131 /*****************************************************************************
132 * Render: displays previously rendered output
133 *****************************************************************************
134 * This function send the currently rendered image to Distort image, waits
135 * until it is displayed and switch the two rendering buffers, preparing next
136 * frame.
137 *****************************************************************************/
138 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
140 picture_t *p_outpic;
142 unsigned int w, h;
143 uint8_t u,v;
145 picture_t *p_converted;
146 video_format_t fmt_out;
148 if( !p_pic ) return NULL;
150 p_outpic = filter_NewPicture( p_filter );
151 if( !p_outpic )
153 picture_Release( p_pic );
154 return NULL;
157 filter_sys_t *p_sys = p_filter->p_sys;
159 if( !p_sys->p_image )
160 p_sys->p_image = image_HandlerCreate( p_filter );
162 /* chrominance */
163 u = p_sys->u;
164 v = p_sys->v;
165 for( int y = 0; y < p_outpic->p[U_PLANE].i_lines; y++ )
167 memset(
168 p_outpic->p[U_PLANE].p_pixels+y*p_outpic->p[U_PLANE].i_pitch,
169 u, p_outpic->p[U_PLANE].i_pitch );
170 memset(
171 p_outpic->p[V_PLANE].p_pixels+y*p_outpic->p[V_PLANE].i_pitch,
172 v, p_outpic->p[V_PLANE].i_pitch );
173 if( v == 0 && u != 0 )
174 u --;
175 else if( u == 0xff )
176 v --;
177 else if( v == 0xff )
178 u ++;
179 else if( u == 0 )
180 v ++;
183 /* luminance */
184 plane_CopyPixels( &p_outpic->p[Y_PLANE], &p_pic->p[Y_PLANE] );
186 /* image visualization */
187 fmt_out = p_filter->fmt_out.video;
188 fmt_out.i_width = p_filter->fmt_out.video.i_width*p_sys->scale/150;
189 fmt_out.i_height = p_filter->fmt_out.video.i_height*p_sys->scale/150;
190 fmt_out.i_visible_width = fmt_out.i_width;
191 fmt_out.i_visible_height = fmt_out.i_height;
192 p_converted = image_Convert( p_sys->p_image, p_pic,
193 &(p_pic->format), &fmt_out );
195 if( p_converted )
197 #define copyimage( plane, b ) \
198 for( int y = 0; y<p_converted->p[plane].i_visible_lines; y++ ) { \
199 for( int x = 0; x<p_converted->p[plane].i_visible_pitch; x++ ) { \
200 int nx, ny; \
201 if( p_sys->yinc == 1 ) \
202 ny= y; \
203 else \
204 ny = p_converted->p[plane].i_visible_lines-y -1; \
205 if( p_sys->xinc == 1 ) \
206 nx = x; \
207 else \
208 nx = p_converted->p[plane].i_visible_pitch-x -1; \
209 p_outpic->p[plane].p_pixels[(p_sys->x*b+nx)+(ny+p_sys->y*b)*p_outpic->p[plane].i_pitch ] = p_converted->p[plane].p_pixels[y*p_converted->p[plane].i_pitch+x]; \
211 copyimage( Y_PLANE, 2 );
212 copyimage( U_PLANE, 1 );
213 copyimage( V_PLANE, 1 );
214 #undef copyimage
216 picture_Release( p_converted );
218 else
220 msg_Err( p_filter, "Image scaling failed miserably." );
223 p_sys->x += p_sys->xinc;
224 p_sys->y += p_sys->yinc;
226 p_sys->scale += p_sys->scaleinc;
227 if( p_sys->scale >= 50 ) p_sys->scaleinc = -1;
228 if( p_sys->scale <= 1 ) p_sys->scaleinc = 1;
230 w = p_filter->fmt_out.video.i_width*p_sys->scale/150;
231 h = p_filter->fmt_out.video.i_height*p_sys->scale/150;
232 if( p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
233 p_sys->xinc = -1;
234 if( p_sys->x <= 0 )
235 p_sys->xinc = 1;
237 if( p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
238 p_sys->x = (p_filter->fmt_out.video.i_width-w)/2;
239 if( p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
240 p_sys->y = (p_filter->fmt_out.video.i_height-h)/2;
242 if( p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
243 p_sys->yinc = -1;
244 if( p_sys->y <= 0 )
245 p_sys->yinc = 1;
247 for( int y = 0; y < 16; y++ )
249 if( p_sys->v == 0 && p_sys->u != 0 )
250 p_sys->u -= 1;
251 else if( p_sys->u == 0xff )
252 p_sys->v -= 1;
253 else if( p_sys->v == 0xff )
254 p_sys->u += 1;
255 else if( p_sys->u == 0 )
256 p_sys->v += 1;
259 return CopyInfoAndRelease( p_outpic, p_pic );