]> git.sesse.net Git - vlc/blob - modules/video_filter/psychedelic.c
decoder: fix data race in input_DecoderChangePause()
[vlc] / modules / video_filter / psychedelic.c
1 /*****************************************************************************
2  * psychedelic.c : Psychedelic video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Antoine Cellerier <dionoea -at- videolan -dot- org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <math.h>                                            /* sin(), cos() */
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37
38 #include <vlc_filter.h>
39 #include <vlc_image.h>
40 #include "filter_picture.h"
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static picture_t *Filter( filter_t *, picture_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin ()
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 )
59
60     add_shortcut( "psychedelic" )
61     set_callbacks( Create, Destroy )
62 vlc_module_end ()
63
64 /*****************************************************************************
65  * filter_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  *****************************************************************************/
70 struct filter_sys_t
71 {
72     image_handler_t *p_image;
73     unsigned int x, y, scale;
74     int xinc, yinc, scaleinc;
75     uint8_t u,v;
76 };
77
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 )
84 {
85     filter_t *p_filter = (filter_t *)p_this;
86
87     const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
88     const vlc_chroma_description_t *p_chroma = vlc_fourcc_GetChromaDescription( fourcc );
89     if( !p_chroma || p_chroma->plane_count != 3 || p_chroma->pixel_size != 1 ) {
90         msg_Err( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
91         return VLC_EGENERIC;
92     }
93
94     /* Allocate structure */
95     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
96     if( p_filter->p_sys == NULL )
97         return VLC_ENOMEM;
98
99     p_filter->pf_video_filter = Filter;
100
101     p_filter->p_sys->x = 10;
102     p_filter->p_sys->y = 10;
103     p_filter->p_sys->scale = 1;
104     p_filter->p_sys->xinc = 1;
105     p_filter->p_sys->yinc = 1;
106     p_filter->p_sys->scaleinc = 1;
107     p_filter->p_sys->u = 0;
108     p_filter->p_sys->v = 0;
109     p_filter->p_sys->p_image = NULL;
110
111     return VLC_SUCCESS;
112 }
113
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 )
120 {
121     filter_t *p_filter = (filter_t *)p_this;
122
123     if( p_filter->p_sys->p_image )
124         image_HandlerDelete( p_filter->p_sys->p_image );
125     p_filter->p_sys->p_image = NULL;
126
127     free( p_filter->p_sys );
128 }
129
130 /*****************************************************************************
131  * Render: displays previously rendered output
132  *****************************************************************************
133  * This function send the currently rendered image to Distort image, waits
134  * until it is displayed and switch the two rendering buffers, preparing next
135  * frame.
136  *****************************************************************************/
137 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
138 {
139     picture_t *p_outpic;
140
141     unsigned int w, h;
142     int x,y;
143     uint8_t u,v;
144
145     picture_t *p_converted;
146     video_format_t fmt_out;
147     memset( &fmt_out, 0, sizeof(video_format_t) );
148     fmt_out.p_palette = NULL;
149
150     if( !p_pic ) return NULL;
151
152     p_outpic = filter_NewPicture( p_filter );
153     if( !p_outpic )
154     {
155         picture_Release( p_pic );
156         return NULL;
157     }
158
159     if( !p_filter->p_sys->p_image )
160         p_filter->p_sys->p_image = image_HandlerCreate( p_filter );
161
162     /* chrominance */
163     u = p_filter->p_sys->u;
164     v = p_filter->p_sys->v;
165     for( y = 0; y<p_outpic->p[U_PLANE].i_lines; y++)
166     {
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 ++;
181     }
182
183     /* luminance */
184     plane_CopyPixels( &p_outpic->p[Y_PLANE], &p_pic->p[Y_PLANE] );
185
186     /* image visualization */
187     fmt_out = p_filter->fmt_out.video;
188     fmt_out.i_width = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
189     fmt_out.i_height = p_filter->fmt_out.video.i_height*p_filter->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_filter->p_sys->p_image, p_pic,
193                                  &(p_pic->format), &fmt_out );
194
195     if( p_converted )
196     {
197 #define copyimage( plane, b ) \
198         for( y=0; y<p_converted->p[plane].i_visible_lines; y++) { \
199         for( x=0; x<p_converted->p[plane].i_visible_pitch; x++) { \
200             int nx, ny; \
201             if( p_filter->p_sys->yinc == 1 ) \
202                 ny= y; \
203             else \
204                 ny = p_converted->p[plane].i_visible_lines-y; \
205             if( p_filter->p_sys->xinc == 1 ) \
206                 nx = x; \
207             else \
208                 nx = p_converted->p[plane].i_visible_pitch-x; \
209             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]; \
210         } }
211         copyimage( Y_PLANE, 2 );
212         copyimage( U_PLANE, 1 );
213         copyimage( V_PLANE, 1 );
214 #undef copyimage
215
216         picture_Release( p_converted );
217     }
218     else
219     {
220         msg_Err( p_filter, "Image scaling failed miserably." );
221     }
222
223     p_filter->p_sys->x += p_filter->p_sys->xinc;
224     p_filter->p_sys->y += p_filter->p_sys->yinc;
225
226     p_filter->p_sys->scale += p_filter->p_sys->scaleinc;
227     if( p_filter->p_sys->scale >= 50 ) p_filter->p_sys->scaleinc = -1;
228     if( p_filter->p_sys->scale <= 1 ) p_filter->p_sys->scaleinc = 1;
229
230     w = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
231     h = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
232     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
233         p_filter->p_sys->xinc = -1;
234     if( p_filter->p_sys->x <= 0 )
235         p_filter->p_sys->xinc = 1;
236
237     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
238         p_filter->p_sys->x = (p_filter->fmt_out.video.i_width-w)/2;
239     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
240         p_filter->p_sys->y = (p_filter->fmt_out.video.i_height-h)/2;
241
242     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
243         p_filter->p_sys->yinc = -1;
244     if( p_filter->p_sys->y <= 0 )
245         p_filter->p_sys->yinc = 1;
246
247     for( y = 0; y< 16; y++ )
248     {
249         if( p_filter->p_sys->v == 0 && p_filter->p_sys->u != 0 )
250             p_filter->p_sys->u -= 1;
251         else if( p_filter->p_sys->u == 0xff )
252             p_filter->p_sys->v -= 1;
253         else if( p_filter->p_sys->v == 0xff )
254             p_filter->p_sys->u += 1;
255         else if( p_filter->p_sys->u == 0 )
256             p_filter->p_sys->v += 1;
257     }
258
259     return CopyInfoAndRelease( p_outpic, p_pic );
260 }