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