* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc.git] / modules / video_filter / invert.c
1 /*****************************************************************************
2  * invert.c : Invert video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: invert.c,v 1.1 2002/08/04 17:23:43 sam Exp $
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 <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #include "filter_common.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static int  Init      ( vout_thread_t * );
43 static void End       ( vout_thread_t * );
44 static void Render    ( vout_thread_t *, picture_t * );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50     set_description( _("invert video module") );
51     set_capability( "video filter", 0 );
52     add_shortcut( "invert" );
53     set_callbacks( Create, Destroy );
54 vlc_module_end();
55
56 /*****************************************************************************
57  * vout_sys_t: Invert video output method descriptor
58  *****************************************************************************
59  * This structure is part of the video output thread descriptor.
60  * It describes the Invert specific properties of an output thread.
61  *****************************************************************************/
62 struct vout_sys_t
63 {
64     vout_thread_t *p_vout;
65 };
66
67 /*****************************************************************************
68  * Create: allocates Invert video thread output method
69  *****************************************************************************
70  * This function allocates and initializes a Invert vout method.
71  *****************************************************************************/
72 static int Create( vlc_object_t *p_this )
73 {
74     vout_thread_t *p_vout = (vout_thread_t *)p_this;
75
76     /* Allocate structure */
77     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
78     if( p_vout->p_sys == NULL )
79     {
80         msg_Err( p_vout, "out of memory" );
81         return( 1 );
82     }
83
84     p_vout->pf_init = Init;
85     p_vout->pf_end = End;
86     p_vout->pf_manage = NULL;
87     p_vout->pf_render = Render;
88     p_vout->pf_display = NULL;
89
90     return( 0 );
91 }
92
93 /*****************************************************************************
94  * Init: initialize Invert video thread output method
95  *****************************************************************************/
96 static int Init( vout_thread_t *p_vout )
97 {
98     int i_index;
99     picture_t *p_pic;
100
101     I_OUTPUTPICTURES = 0;
102
103     /* Initialize the output structure */
104     p_vout->output.i_chroma = p_vout->render.i_chroma;
105     p_vout->output.i_width  = p_vout->render.i_width;
106     p_vout->output.i_height = p_vout->render.i_height;
107     p_vout->output.i_aspect = p_vout->render.i_aspect;
108
109     /* Try to open the real video output */
110     msg_Dbg( p_vout, "spawning the real video output" );
111
112     p_vout->p_sys->p_vout =
113         vout_CreateThread( p_vout,
114                            p_vout->render.i_width, p_vout->render.i_height,
115                            p_vout->render.i_chroma, p_vout->render.i_aspect );
116
117     /* Everything failed */
118     if( p_vout->p_sys->p_vout == NULL )
119     {
120         msg_Err( p_vout, "can't open vout, aborting" );
121
122         return( 0 );
123     }
124  
125     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
126
127     return( 0 );
128 }
129
130 /*****************************************************************************
131  * End: terminate Invert video thread output method
132  *****************************************************************************/
133 static void End( vout_thread_t *p_vout )
134 {
135     int i_index;
136
137     /* Free the fake output buffers we allocated */
138     for( i_index = I_OUTPUTPICTURES ; i_index ; )
139     {
140         i_index--;
141         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
142     }
143 }
144
145 /*****************************************************************************
146  * Destroy: destroy Invert video thread output method
147  *****************************************************************************
148  * Terminate an output method created by InvertCreateOutputMethod
149  *****************************************************************************/
150 static void Destroy( vlc_object_t *p_this )
151 {   
152     vout_thread_t *p_vout = (vout_thread_t *)p_this;
153
154     vout_DestroyThread( p_vout->p_sys->p_vout );
155
156     free( p_vout->p_sys );
157 }
158
159 /*****************************************************************************
160  * Render: displays previously rendered output
161  *****************************************************************************
162  * This function send the currently rendered image to Invert image, waits
163  * until it is displayed and switch the two rendering buffers, preparing next
164  * frame.
165  *****************************************************************************/
166 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
167 {
168     picture_t *p_outpic;
169     int i_index;
170
171     /* This is a new frame. Get a structure from the video_output. */
172     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
173               == NULL )
174     {
175         if( p_vout->b_die || p_vout->b_error )
176         {
177             return;
178         }
179         msleep( VOUT_OUTMEM_SLEEP );
180     }   
181
182     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
183     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
184
185     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
186     {
187         u8 *p_in, *p_in_end, *p_out;
188
189         p_in = p_pic->p[i_index].p_pixels;
190         p_in_end = p_in - 64 + p_pic->p[i_index].i_lines
191                                 * p_pic->p[i_index].i_pitch;
192
193         p_out = p_outpic->p[i_index].p_pixels;
194
195         for( ; p_in < p_in_end ; )
196         {
197             /* Do 64 pixels at a time */
198             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
199             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
200             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
201             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
202             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
203             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
204             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
205             *((u64*)p_out)++ = ~( *((u64*)p_in)++ );
206         }
207
208         p_in_end += 64;
209
210         for( ; p_in < p_in_end ; )
211         {
212             /* Do 1 pixel at a time */
213             *p_out++ = ~( *p_in++ );
214         }
215     }
216
217     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
218
219     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
220 }
221