]> git.sesse.net Git - vlc/blob - modules/video_filter/invert.c
5c10f778db22a11ac2a933483768ae711fb60801
[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_capability( "video filter", 0 );
54     add_shortcut( "invert" );
55     set_callbacks( Create, Destroy );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * vout_sys_t: Invert video output method descriptor
60  *****************************************************************************
61  * This structure is part of the video output thread descriptor.
62  * It describes the Invert specific properties of an output thread.
63  *****************************************************************************/
64 struct vout_sys_t
65 {
66     vout_thread_t *p_vout;
67 };
68
69 /*****************************************************************************
70  * Control: control facility for the vout (forwards to child vout)
71  *****************************************************************************/
72 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
73 {
74     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
75 }
76
77 /*****************************************************************************
78  * Create: allocates Invert video thread output method
79  *****************************************************************************
80  * This function allocates and initializes a Invert vout method.
81  *****************************************************************************/
82 static int Create( vlc_object_t *p_this )
83 {
84     vout_thread_t *p_vout = (vout_thread_t *)p_this;
85
86     /* Allocate structure */
87     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
88     if( p_vout->p_sys == NULL )
89     {
90         msg_Err( p_vout, "out of memory" );
91         return VLC_ENOMEM;
92     }
93
94     p_vout->pf_init = Init;
95     p_vout->pf_end = End;
96     p_vout->pf_manage = NULL;
97     p_vout->pf_render = Render;
98     p_vout->pf_display = NULL;
99     p_vout->pf_control = Control;
100
101     return VLC_SUCCESS;
102 }
103
104 /*****************************************************************************
105  * Init: initialize Invert video thread output method
106  *****************************************************************************/
107 static int Init( vout_thread_t *p_vout )
108 {
109     int i_index;
110     picture_t *p_pic;
111
112     I_OUTPUTPICTURES = 0;
113
114     /* Initialize the output structure */
115     p_vout->output.i_chroma = p_vout->render.i_chroma;
116     p_vout->output.i_width  = p_vout->render.i_width;
117     p_vout->output.i_height = p_vout->render.i_height;
118     p_vout->output.i_aspect = p_vout->render.i_aspect;
119
120     /* Try to open the real video output */
121     msg_Dbg( p_vout, "spawning the real video output" );
122
123     p_vout->p_sys->p_vout = vout_Create( p_vout,
124                            p_vout->render.i_width, p_vout->render.i_height,
125                            p_vout->render.i_chroma, p_vout->render.i_aspect );
126
127     /* Everything failed */
128     if( p_vout->p_sys->p_vout == NULL )
129     {
130         msg_Err( p_vout, "can't open vout, aborting" );
131
132         return VLC_EGENERIC;
133     }
134
135     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
136
137     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
138
139     ADD_PARENT_CALLBACKS( SendEventsToChild );
140
141     return VLC_SUCCESS;
142 }
143
144 /*****************************************************************************
145  * End: terminate Invert video thread output method
146  *****************************************************************************/
147 static void End( vout_thread_t *p_vout )
148 {
149     int i_index;
150
151     /* Free the fake output buffers we allocated */
152     for( i_index = I_OUTPUTPICTURES ; i_index ; )
153     {
154         i_index--;
155         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
156     }
157 }
158
159 /*****************************************************************************
160  * Destroy: destroy Invert video thread output method
161  *****************************************************************************
162  * Terminate an output method created by InvertCreateOutputMethod
163  *****************************************************************************/
164 static void Destroy( vlc_object_t *p_this )
165 {
166     vout_thread_t *p_vout = (vout_thread_t *)p_this;
167
168     if( p_vout->p_sys->p_vout )
169     {
170         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
171         vlc_object_detach( p_vout->p_sys->p_vout );
172         vout_Destroy( p_vout->p_sys->p_vout );
173     }
174
175     DEL_PARENT_CALLBACKS( SendEventsToChild );
176
177     free( p_vout->p_sys );
178 }
179
180 /*****************************************************************************
181  * Render: displays previously rendered output
182  *****************************************************************************
183  * This function send the currently rendered image to Invert image, waits
184  * until it is displayed and switch the two rendering buffers, preparing next
185  * frame.
186  *****************************************************************************/
187 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
188 {
189     picture_t *p_outpic;
190     int i_index;
191
192     /* This is a new frame. Get a structure from the video_output. */
193     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
194               == NULL )
195     {
196         if( p_vout->b_die || p_vout->b_error )
197         {
198             return;
199         }
200         msleep( VOUT_OUTMEM_SLEEP );
201     }
202
203     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
204     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
205
206     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
207     {
208         uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
209
210         p_in = p_pic->p[i_index].p_pixels;
211         p_in_end = p_in + p_pic->p[i_index].i_visible_lines
212                            * p_pic->p[i_index].i_pitch;
213
214         p_out = p_outpic->p[i_index].p_pixels;
215
216         for( ; p_in < p_in_end ; )
217         {
218             uint64_t *p_in64, *p_out64;
219
220             p_line_end = p_in + p_pic->p[i_index].i_visible_pitch - 64;
221
222             p_in64 = (uint64_t*)p_in;
223             p_out64 = (uint64_t*)p_out;
224
225             for( ; (ptrdiff_t)p_in64 < (ptrdiff_t)p_line_end ; )
226             {
227                 /* Do 64 pixels at a time */
228                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
229                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
230                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
231                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
232             }
233
234             p_in = (uint8_t*)p_in64;
235             p_out = (uint8_t*)p_out64;
236             p_line_end += 64;
237
238             for( ; p_in < p_line_end ; )
239             {
240                 *p_out++ = ~( *p_in++ );
241             }
242
243             p_in += p_pic->p[i_index].i_pitch
244                      - p_pic->p[i_index].i_visible_pitch;
245             p_out += p_outpic->p[i_index].i_pitch
246                      - p_outpic->p[i_index].i_visible_pitch;
247         }
248     }
249
250     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
251
252     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
253 }
254
255 /*****************************************************************************
256  * SendEvents: forward mouse and keyboard events to the parent p_vout
257  *****************************************************************************/
258 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
259                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
260 {
261     var_Set( (vlc_object_t *)p_data, psz_var, newval );
262
263     return VLC_SUCCESS;
264 }
265
266 /*****************************************************************************
267  * SendEventsToChild: forward events to the child/children vout
268  *****************************************************************************/
269 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
270                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
271 {
272     vout_thread_t *p_vout = (vout_thread_t *)p_this;
273     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
274     return VLC_SUCCESS;
275 }