]> git.sesse.net Git - vlc/blob - modules/video_filter/clone.c
* ./modules/video_filter/**/*.c: mouse clicks and keyboard events are now
[vlc] / modules / video_filter / clone.c
1 /*****************************************************************************
2  * clone.c : Clone video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 VideoLAN
5  * $Id: clone.c,v 1.5 2003/01/17 16:18:03 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 <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 void RemoveAllVout  ( vout_thread_t *p_vout );
46
47 static int  SendEvents( vlc_object_t *, char const *,
48                         vlc_value_t, vlc_value_t, void * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define COUNT_TEXT N_("Number of clones")
54 #define COUNT_LONGTEXT N_("Select the number of video windows in which to "\
55     "clone the video")
56
57 vlc_module_begin();
58     add_category_hint( N_("Miscellaneous"), NULL );
59     add_integer( "clone-count", 2, NULL, COUNT_TEXT, COUNT_LONGTEXT );
60     set_description( _("image clone video module") );
61     set_capability( "video filter", 0 );
62     add_shortcut( "clone" );
63     set_callbacks( Create, Destroy );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * vout_sys_t: Clone video output method descriptor
68  *****************************************************************************
69  * This structure is part of the video output thread descriptor.
70  * It describes the Clone specific properties of an output thread.
71  *****************************************************************************/
72 struct vout_sys_t
73 {
74     int    i_clones;
75     vout_thread_t **pp_vout;
76 };
77
78 /*****************************************************************************
79  * Create: allocates Clone video thread output method
80  *****************************************************************************
81  * This function allocates and initializes a Clone vout method.
82  *****************************************************************************/
83 static int Create( vlc_object_t *p_this )
84 {
85     vout_thread_t *p_vout = (vout_thread_t *)p_this;
86
87     /* Allocate structure */
88     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
89     if( p_vout->p_sys == NULL )
90     {
91         msg_Err( p_vout, "out of memory" );
92         return VLC_ENOMEM;
93     }
94
95     p_vout->pf_init = Init;
96     p_vout->pf_end = End;
97     p_vout->pf_manage = NULL;
98     p_vout->pf_render = Render;
99     p_vout->pf_display = NULL;
100
101     /* Look what method was requested */
102     p_vout->p_sys->i_clones = config_GetInt( p_vout, "clone-count" );
103
104     p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );
105
106     msg_Dbg( p_vout, "spawning %i clone(s)", p_vout->p_sys->i_clones );
107
108     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
109                                      sizeof(vout_thread_t *) );
110     if( p_vout->p_sys->pp_vout == NULL )
111     {
112         msg_Err( p_vout, "out of memory" );
113         free( p_vout->p_sys );
114         return VLC_ENOMEM;
115     }
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Init: initialize Clone video thread output method
122  *****************************************************************************/
123 static int Init( vout_thread_t *p_vout )
124 {
125     int   i_index, i_vout;
126     picture_t *p_pic;
127
128     I_OUTPUTPICTURES = 0;
129
130     /* Initialize the output structure */
131     p_vout->output.i_chroma = p_vout->render.i_chroma;
132     p_vout->output.i_width  = p_vout->render.i_width;
133     p_vout->output.i_height = p_vout->render.i_height;
134     p_vout->output.i_aspect = p_vout->render.i_aspect;
135
136     /* Try to open the real video output */
137     msg_Dbg( p_vout, "spawning the real video outputs" );
138
139     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
140     {
141         p_vout->p_sys->pp_vout[ i_vout ] = vout_Create( p_vout,
142                             p_vout->render.i_width, p_vout->render.i_height,
143                             p_vout->render.i_chroma, p_vout->render.i_aspect );
144         if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
145         {
146             msg_Err( p_vout, "failed to clone %i vout threads",
147                              p_vout->p_sys->i_clones );
148             p_vout->p_sys->i_clones = i_vout;
149             RemoveAllVout( p_vout );
150             return VLC_EGENERIC;
151         }
152         ADD_CALLBACKS( p_vout->p_sys->pp_vout[ i_vout ], SendEvents );
153     }
154
155     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * End: terminate Clone video thread output method
162  *****************************************************************************/
163 static void End( vout_thread_t *p_vout )
164 {
165     int i_index;
166
167     /* Free the fake output buffers we allocated */
168     for( i_index = I_OUTPUTPICTURES ; i_index ; )
169     {
170         i_index--;
171         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
172     }
173 }
174
175 /*****************************************************************************
176  * Destroy: destroy Clone video thread output method
177  *****************************************************************************
178  * Terminate an output method created by CloneCreateOutputMethod
179  *****************************************************************************/
180 static void Destroy( vlc_object_t *p_this )
181 {
182     vout_thread_t *p_vout = (vout_thread_t *)p_this;
183
184     RemoveAllVout( p_vout );
185
186     free( p_vout->p_sys->pp_vout );
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 Clone 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 = NULL;
200     int i_vout, i_plane;
201
202     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
203     {
204         while( ( p_outpic =
205             vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
206                ) == NULL )
207         {
208             if( p_vout->b_die || p_vout->b_error )
209             {
210                 vout_DestroyPicture(
211                     p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
212                 return;
213             }
214
215             msleep( VOUT_OUTMEM_SLEEP );
216         }
217
218         vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
219                           p_outpic, p_pic->date );
220         vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
221
222         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
223         {
224             uint8_t *p_in, *p_in_end, *p_out;
225             int i_in_pitch = p_pic->p[i_plane].i_pitch;
226             const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
227             const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
228
229             p_in = p_pic->p[i_plane].p_pixels;
230             p_out = p_outpic->p[i_plane].p_pixels;
231
232             if( i_in_pitch == i_copy_pitch
233                  && i_out_pitch == i_copy_pitch )
234             {
235                 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_in_pitch
236                                            * p_outpic->p[i_plane].i_lines );
237             }
238             else
239             {
240                 p_in_end = p_in + i_in_pitch * p_outpic->p[i_plane].i_lines;
241
242                 while( p_in < p_in_end )
243                 {
244                     p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
245                     p_in += i_in_pitch;
246                     p_out += i_out_pitch;
247                 }
248             }
249         }
250
251         vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
252         vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
253     }
254 }
255
256 /*****************************************************************************
257  * RemoveAllVout: destroy all the child video output threads
258  *****************************************************************************/
259 static void RemoveAllVout( vout_thread_t *p_vout )
260 {
261     while( p_vout->p_sys->i_clones )
262     {
263          --p_vout->p_sys->i_clones;
264          DEL_CALLBACKS( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones],
265                         SendEvents );
266          vout_Destroy( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
267     }
268 }
269
270 /*****************************************************************************
271  * SendEvents: forward mouse and keyboard events to the parent p_vout
272  *****************************************************************************/
273 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
274                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
275 {
276     var_Set( (vlc_object_t *)p_data, psz_var, newval );
277
278     return VLC_SUCCESS;
279 }
280