]> git.sesse.net Git - vlc/blob - modules/video_filter/psychedelic.c
Use picture helpers (Yield,Release,CopyProperties).
[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 #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  * vout_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     /* Allocate structure */
88     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
89     if( p_filter->p_sys == NULL )
90         return VLC_ENOMEM;
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;
140     memset( &fmt_out, 0, sizeof(video_format_t) );
141     fmt_out.p_palette = NULL;
142
143     if( !p_pic ) return NULL;
144
145     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
146     if( !p_outpic )
147     {
148         msg_Warn( p_filter, "can't get output picture" );
149         picture_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         vlc_memset(
162                 p_outpic->p[U_PLANE].p_pixels+y*p_outpic->p[U_PLANE].i_pitch,
163                 u, p_outpic->p[U_PLANE].i_pitch );
164         vlc_memset(
165                 p_outpic->p[V_PLANE].p_pixels+y*p_outpic->p[V_PLANE].i_pitch,
166                 v, p_outpic->p[V_PLANE].i_pitch );
167         if( v == 0 && u != 0 )
168             u --;
169         else if( u == 0xff )
170             v --;
171         else if( v == 0xff )
172             u ++;
173         else if( u == 0 )
174             v ++;
175     }
176
177     /* luminance */
178     vlc_memcpy( 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     if( p_converted )
189     {
190 #define copyimage( plane, b ) \
191         for( y=0; y<p_converted->p[plane].i_visible_lines; y++) { \
192         for( x=0; x<p_converted->p[plane].i_visible_pitch; x++) { \
193             int nx, ny; \
194             if( p_filter->p_sys->yinc == 1 ) \
195                 ny= y; \
196             else \
197                 ny = p_converted->p[plane].i_visible_lines-y; \
198             if( p_filter->p_sys->xinc == 1 ) \
199                 nx = x; \
200             else \
201                 nx = p_converted->p[plane].i_visible_pitch-x; \
202             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]; \
203         } }
204         copyimage( Y_PLANE, 2 );
205         copyimage( U_PLANE, 1 );
206         copyimage( V_PLANE, 1 );
207 #undef copyimage
208
209         picture_Release( p_converted );
210     }
211     else
212     {
213         msg_Err( p_filter, "Image scaling failed miserably." );
214     }
215
216     p_filter->p_sys->x += p_filter->p_sys->xinc;
217     p_filter->p_sys->y += p_filter->p_sys->yinc;
218
219     p_filter->p_sys->scale += p_filter->p_sys->scaleinc;
220     if( p_filter->p_sys->scale >= 50 ) p_filter->p_sys->scaleinc = -1;
221     if( p_filter->p_sys->scale <= 1 ) p_filter->p_sys->scaleinc = 1;
222
223     w = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
224     h = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
225     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
226         p_filter->p_sys->xinc = -1;
227     if( p_filter->p_sys->x <= 0 )
228         p_filter->p_sys->xinc = 1;
229
230     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
231         p_filter->p_sys->x = (p_filter->fmt_out.video.i_width-w)/2;
232     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
233         p_filter->p_sys->y = (p_filter->fmt_out.video.i_height-h)/2;
234
235     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
236         p_filter->p_sys->yinc = -1;
237     if( p_filter->p_sys->y <= 0 )
238         p_filter->p_sys->yinc = 1;
239
240     for( y = 0; y< 16; y++ )
241     {
242         if( p_filter->p_sys->v == 0 && p_filter->p_sys->u != 0 )
243             p_filter->p_sys->u -= 1;
244         else if( p_filter->p_sys->u == 0xff )
245             p_filter->p_sys->v -= 1;
246         else if( p_filter->p_sys->v == 0xff )
247             p_filter->p_sys->u += 1;
248         else if( p_filter->p_sys->u == 0 )
249             p_filter->p_sys->v += 1;
250     }
251
252     return CopyInfoAndRelease( p_outpic, p_pic );
253 }