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