]> git.sesse.net Git - vlc/blob - modules/video_filter/psychedelic.c
Don't include config.h from the headers - refs #297.
[vlc] / modules / video_filter / psychedelic.c
1 /*****************************************************************************
2  * Psychedelic.c : Psychedelic video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
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
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.
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 General Public License for more details.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <math.h>                                            /* sin(), cos() */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc/vlc.h>
36
37 #include "vlc_filter.h"
38 #include "vlc_image.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static picture_t *Filter( filter_t *, picture_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();
52     set_description( _("Psychedelic video filter") );
53     set_shortname( _( "Psychedelic" ));
54     set_capability( "video filter2", 0 );
55     set_category( CAT_VIDEO );
56     set_subcategory( SUBCAT_VIDEO_VFILTER );
57
58     add_shortcut( "psychedelic" );
59     set_callbacks( Create, Destroy );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: Distort video output method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the Distort specific properties of an output thread.
67  *****************************************************************************/
68 struct filter_sys_t
69 {
70     image_handler_t *p_image;
71     unsigned int x, y, scale;
72     int xinc, yinc, scaleinc;
73     uint8_t u,v;
74 };
75
76 /*****************************************************************************
77  * Create: allocates Distort video thread output method
78  *****************************************************************************
79  * This function allocates and initializes a Distort vout method.
80  *****************************************************************************/
81 static int Create( vlc_object_t *p_this )
82 {
83     filter_t *p_filter = (filter_t *)p_this;
84
85     /* Allocate structure */
86     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
87     if( p_filter->p_sys == NULL )
88     {
89         msg_Err( p_filter, "out of memory" );
90         return VLC_ENOMEM;
91     }
92
93     p_filter->pf_video_filter = Filter;
94
95     p_filter->p_sys->x = 10;
96     p_filter->p_sys->y = 10;
97     p_filter->p_sys->scale = 1;
98     p_filter->p_sys->xinc = 1;
99     p_filter->p_sys->yinc = 1;
100     p_filter->p_sys->scaleinc = 1;
101     p_filter->p_sys->u = 0;
102     p_filter->p_sys->v = 0;
103     p_filter->p_sys->p_image = NULL;
104
105     return VLC_SUCCESS;
106 }
107
108 /*****************************************************************************
109  * Destroy: destroy Distort video thread output method
110  *****************************************************************************
111  * Terminate an output method created by DistortCreateOutputMethod
112  *****************************************************************************/
113 static void Destroy( vlc_object_t *p_this )
114 {
115     filter_t *p_filter = (filter_t *)p_this;
116
117     if( p_filter->p_sys->p_image )
118         image_HandlerDelete( p_filter->p_sys->p_image );
119     p_filter->p_sys->p_image = NULL;
120
121     free( p_filter->p_sys );
122 }
123
124 /*****************************************************************************
125  * Render: displays previously rendered output
126  *****************************************************************************
127  * This function send the currently rendered image to Distort image, waits
128  * until it is displayed and switch the two rendering buffers, preparing next
129  * frame.
130  *****************************************************************************/
131 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
132 {
133     picture_t *p_outpic;
134
135     unsigned int w, h;
136     int x,y;
137     uint8_t u,v;
138
139     picture_t *p_converted;
140     video_format_t fmt_out;
141     memset( &fmt_out, 0, sizeof(video_format_t) );
142     fmt_out.p_palette = NULL;
143
144     if( !p_pic ) return NULL;
145
146     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
147     if( !p_outpic )
148     {
149         msg_Warn( p_filter, "can't get output picture" );
150         if( p_pic->pf_release )
151             p_pic->pf_release( p_pic );
152         return NULL;
153     }
154
155     if( !p_filter->p_sys->p_image )
156         p_filter->p_sys->p_image = image_HandlerCreate( p_filter );
157
158     /* chrominance */
159     u = p_filter->p_sys->u;
160     v = p_filter->p_sys->v;
161     for( y = 0; y<p_outpic->p[U_PLANE].i_lines; y++)
162     {
163         p_filter->p_libvlc->pf_memset(
164                 p_outpic->p[U_PLANE].p_pixels+y*p_outpic->p[U_PLANE].i_pitch,
165                 u, p_outpic->p[U_PLANE].i_pitch );
166         p_filter->p_libvlc->pf_memset(
167                 p_outpic->p[V_PLANE].p_pixels+y*p_outpic->p[V_PLANE].i_pitch,
168                 v, p_outpic->p[V_PLANE].i_pitch );
169         if( v == 0 && u != 0 )
170             u --;
171         else if( u == 0xff )
172             v --;
173         else if( v == 0xff )
174             u ++;
175         else if( u == 0 )
176             v ++;
177     }
178
179     /* luminance */
180     p_filter->p_libvlc->pf_memcpy(
181                 p_outpic->p[Y_PLANE].p_pixels, p_pic->p[Y_PLANE].p_pixels,
182                 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
183
184     /* image visualization */
185     fmt_out = p_filter->fmt_out.video;
186     fmt_out.i_width = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
187     fmt_out.i_height = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
188     p_converted = image_Convert( p_filter->p_sys->p_image, p_pic,
189                                  &(p_pic->format), &fmt_out );
190
191     if( p_converted )
192     {
193 #define copyimage( plane, b ) \
194     for( y=0; y<p_converted->p[plane].i_visible_lines; y++) { \
195     for( x=0; x<p_converted->p[plane].i_visible_pitch; x++) { \
196         int nx, ny; \
197         if( p_filter->p_sys->yinc == 1 ) \
198             ny= y; \
199         else \
200             ny = p_converted->p[plane].i_visible_lines-y; \
201         if( p_filter->p_sys->xinc == 1 ) \
202             nx = x; \
203         else \
204             nx = p_converted->p[plane].i_visible_pitch-x; \
205         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]; \
206     } }
207     copyimage( Y_PLANE, 2 );
208     copyimage( U_PLANE, 1 );
209     copyimage( V_PLANE, 1 );
210 #undef copyimage
211
212     p_converted->pf_release( p_converted );
213     }
214     else
215     {
216         msg_Err( p_filter, "Image scaling failed miserably." );
217     }
218
219     p_filter->p_sys->x += p_filter->p_sys->xinc;
220     p_filter->p_sys->y += p_filter->p_sys->yinc;
221
222     p_filter->p_sys->scale += p_filter->p_sys->scaleinc;
223     if( p_filter->p_sys->scale >= 50 ) p_filter->p_sys->scaleinc = -1;
224     if( p_filter->p_sys->scale <= 1 ) p_filter->p_sys->scaleinc = 1;
225
226     w = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
227     h = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
228     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
229         p_filter->p_sys->xinc = -1;
230     if( p_filter->p_sys->x <= 0 )
231         p_filter->p_sys->xinc = 1;
232
233     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
234         p_filter->p_sys->x = (p_filter->fmt_out.video.i_width-w)/2;
235     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
236         p_filter->p_sys->y = (p_filter->fmt_out.video.i_height-h)/2;
237
238     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
239         p_filter->p_sys->yinc = -1;
240     if( p_filter->p_sys->y <= 0 )
241         p_filter->p_sys->yinc = 1;
242
243     for( y = 0; y< 16; y++ )
244     {
245         if( p_filter->p_sys->v == 0 && p_filter->p_sys->u != 0 )
246             p_filter->p_sys->u -= 1;
247         else if( p_filter->p_sys->u == 0xff )
248             p_filter->p_sys->v -= 1;
249         else if( p_filter->p_sys->v == 0xff )
250             p_filter->p_sys->u += 1;
251         else if( p_filter->p_sys->u == 0 )
252             p_filter->p_sys->v += 1;
253     }
254
255     p_outpic->date = p_pic->date;
256     p_outpic->b_force = p_pic->b_force;
257     p_outpic->i_nb_fields = p_pic->i_nb_fields;
258     p_outpic->b_progressive = p_pic->b_progressive;
259     p_outpic->b_top_field_first = p_pic->b_top_field_first;
260
261     if( p_pic->pf_release )
262         p_pic->pf_release( p_pic );
263
264     return p_outpic;
265 }