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