]> git.sesse.net Git - vlc/blob - modules/video_filter/invert.c
Improvements to preferences
[vlc] / modules / video_filter / invert.c
1 /*****************************************************************************
2  * invert.c : Invert video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "filter_common.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static int  Init      ( vout_thread_t * );
42 static void End       ( vout_thread_t * );
43 static void Render    ( vout_thread_t *, picture_t * );
44
45 static int  SendEvents( vlc_object_t *, char const *,
46                         vlc_value_t, vlc_value_t, void * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();
52     set_description( _("Invert video filter") );
53     set_category( CAT_VIDEO );
54     set_subcategory( SUBCAT_VIDEO_VFILTER );
55     set_capability( "video filter", 0 );
56     add_shortcut( "invert" );
57     set_callbacks( Create, Destroy );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * vout_sys_t: Invert video output method descriptor
62  *****************************************************************************
63  * This structure is part of the video output thread descriptor.
64  * It describes the Invert specific properties of an output thread.
65  *****************************************************************************/
66 struct vout_sys_t
67 {
68     vout_thread_t *p_vout;
69 };
70
71 /*****************************************************************************
72  * Control: control facility for the vout (forwards to child vout)
73  *****************************************************************************/
74 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
75 {
76     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
77 }
78
79 /*****************************************************************************
80  * Create: allocates Invert video thread output method
81  *****************************************************************************
82  * This function allocates and initializes a Invert vout method.
83  *****************************************************************************/
84 static int Create( vlc_object_t *p_this )
85 {
86     vout_thread_t *p_vout = (vout_thread_t *)p_this;
87
88     /* Allocate structure */
89     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
90     if( p_vout->p_sys == NULL )
91     {
92         msg_Err( p_vout, "out of memory" );
93         return VLC_ENOMEM;
94     }
95
96     p_vout->pf_init = Init;
97     p_vout->pf_end = End;
98     p_vout->pf_manage = NULL;
99     p_vout->pf_render = Render;
100     p_vout->pf_display = NULL;
101     p_vout->pf_control = Control;
102
103     return VLC_SUCCESS;
104 }
105
106 /*****************************************************************************
107  * Init: initialize Invert video thread output method
108  *****************************************************************************/
109 static int Init( vout_thread_t *p_vout )
110 {
111     int i_index;
112     picture_t *p_pic;
113
114     I_OUTPUTPICTURES = 0;
115
116     /* Initialize the output structure */
117     p_vout->output.i_chroma = p_vout->render.i_chroma;
118     p_vout->output.i_width  = p_vout->render.i_width;
119     p_vout->output.i_height = p_vout->render.i_height;
120     p_vout->output.i_aspect = p_vout->render.i_aspect;
121
122     /* Try to open the real video output */
123     msg_Dbg( p_vout, "spawning the real video output" );
124
125     p_vout->p_sys->p_vout = vout_Create( p_vout,
126                            p_vout->render.i_width, p_vout->render.i_height,
127                            p_vout->render.i_chroma, p_vout->render.i_aspect );
128
129     /* Everything failed */
130     if( p_vout->p_sys->p_vout == NULL )
131     {
132         msg_Err( p_vout, "can't open vout, aborting" );
133
134         return VLC_EGENERIC;
135     }
136
137     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
138
139     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
140
141     ADD_PARENT_CALLBACKS( SendEventsToChild );
142
143     return VLC_SUCCESS;
144 }
145
146 /*****************************************************************************
147  * End: terminate Invert video thread output method
148  *****************************************************************************/
149 static void End( vout_thread_t *p_vout )
150 {
151     int i_index;
152
153     /* Free the fake output buffers we allocated */
154     for( i_index = I_OUTPUTPICTURES ; i_index ; )
155     {
156         i_index--;
157         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
158     }
159 }
160
161 /*****************************************************************************
162  * Destroy: destroy Invert video thread output method
163  *****************************************************************************
164  * Terminate an output method created by InvertCreateOutputMethod
165  *****************************************************************************/
166 static void Destroy( vlc_object_t *p_this )
167 {
168     vout_thread_t *p_vout = (vout_thread_t *)p_this;
169
170     if( p_vout->p_sys->p_vout )
171     {
172         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
173         vlc_object_detach( p_vout->p_sys->p_vout );
174         vout_Destroy( p_vout->p_sys->p_vout );
175     }
176
177     DEL_PARENT_CALLBACKS( SendEventsToChild );
178
179     free( p_vout->p_sys );
180 }
181
182 /*****************************************************************************
183  * Render: displays previously rendered output
184  *****************************************************************************
185  * This function send the currently rendered image to Invert image, waits
186  * until it is displayed and switch the two rendering buffers, preparing next
187  * frame.
188  *****************************************************************************/
189 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
190 {
191     picture_t *p_outpic;
192     int i_index;
193
194     /* This is a new frame. Get a structure from the video_output. */
195     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
196               == NULL )
197     {
198         if( p_vout->b_die || p_vout->b_error )
199         {
200             return;
201         }
202         msleep( VOUT_OUTMEM_SLEEP );
203     }
204
205     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
206     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
207
208     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
209     {
210         uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
211
212         p_in = p_pic->p[i_index].p_pixels;
213         p_in_end = p_in + p_pic->p[i_index].i_visible_lines
214                            * p_pic->p[i_index].i_pitch;
215
216         p_out = p_outpic->p[i_index].p_pixels;
217
218         for( ; p_in < p_in_end ; )
219         {
220             uint64_t *p_in64, *p_out64;
221
222             p_line_end = p_in + p_pic->p[i_index].i_visible_pitch - 64;
223
224             p_in64 = (uint64_t*)p_in;
225             p_out64 = (uint64_t*)p_out;
226
227             for( ; (ptrdiff_t)p_in64 < (ptrdiff_t)p_line_end ; )
228             {
229                 /* Do 64 pixels at a time */
230                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
231                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
232                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
233                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
234             }
235
236             p_in = (uint8_t*)p_in64;
237             p_out = (uint8_t*)p_out64;
238             p_line_end += 64;
239
240             for( ; p_in < p_line_end ; )
241             {
242                 *p_out++ = ~( *p_in++ );
243             }
244
245             p_in += p_pic->p[i_index].i_pitch
246                      - p_pic->p[i_index].i_visible_pitch;
247             p_out += p_outpic->p[i_index].i_pitch
248                      - p_outpic->p[i_index].i_visible_pitch;
249         }
250     }
251
252     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
253
254     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
255 }
256
257 /*****************************************************************************
258  * SendEvents: forward mouse and keyboard events to the parent p_vout
259  *****************************************************************************/
260 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
261                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
262 {
263     var_Set( (vlc_object_t *)p_data, psz_var, newval );
264
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * SendEventsToChild: forward events to the child/children vout
270  *****************************************************************************/
271 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
272                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
273 {
274     vout_thread_t *p_vout = (vout_thread_t *)p_this;
275     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
276     return VLC_SUCCESS;
277 }