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