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