]> git.sesse.net Git - vlc/blob - modules/video_filter/clone.c
Improvements to preferences
[vlc] / modules / video_filter / clone.c
1 /*****************************************************************************
2  * clone.c : Clone video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 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 #define VOUTSEPARATOR ','
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static int  Init      ( vout_thread_t * );
44 static void End       ( vout_thread_t * );
45 static void Render    ( vout_thread_t *, picture_t * );
46
47 static void RemoveAllVout  ( vout_thread_t *p_vout );
48
49 static int  SendEvents( vlc_object_t *, char const *,
50                         vlc_value_t, vlc_value_t, void * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #define COUNT_TEXT N_("Number of clones")
56 #define COUNT_LONGTEXT N_("Select the number of video windows in which to "\
57     "clone the video.")
58
59 #define VOUTLIST_TEXT N_("List of video output modules")
60 #define VOUTLIST_LONGTEXT N_("Select the specific video output modules that you want to activate.")
61
62 vlc_module_begin();
63     set_description( _("Clone video filter") );
64     set_capability( "video filter", 0 );
65     set_category( CAT_VIDEO );
66     set_subcategory( SUBCAT_VIDEO_VFILTER );
67
68     add_integer( "clone-count", 2, NULL, COUNT_TEXT, COUNT_LONGTEXT, VLC_FALSE );
69     add_string ( "clone-vout-list", NULL, NULL, VOUTLIST_TEXT, VOUTLIST_LONGTEXT, VLC_FALSE );
70     
71     add_shortcut( "clone" );
72     set_callbacks( Create, Destroy );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * vout_sys_t: Clone video output method descriptor
77  *****************************************************************************
78  * This structure is part of the video output thread descriptor.
79  * It describes the Clone specific properties of an output thread.
80  *****************************************************************************/
81 struct vout_sys_t
82 {
83     int    i_clones;
84
85     /* list of vout modules to use. "default" will launch a default
86      * module. If specified, overrides the setting in i_clones (which it
87      * sets to the list length) */
88     char **ppsz_vout_list;
89
90     vout_thread_t **pp_vout;
91 };
92
93 /*****************************************************************************
94  * Control: control facility for the vout (forwards to child vout)
95  *****************************************************************************/
96 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
97 {
98     int i_vout;
99     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
100     {
101         vout_vaControl( p_vout->p_sys->pp_vout[ i_vout ], i_query, args );
102     }
103     return VLC_SUCCESS;
104 }
105
106 /*****************************************************************************
107  * Create: allocates Clone video thread output method
108  *****************************************************************************
109  * This function allocates and initializes a Clone vout method.
110  *****************************************************************************/
111 static int Create( vlc_object_t *p_this )
112 {
113     vout_thread_t *p_vout = (vout_thread_t *)p_this;
114     char *psz_clonelist;
115
116     /* Allocate structure */
117     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
118     if( p_vout->p_sys == NULL )
119     {
120         msg_Err( p_vout, "out of memory" );
121         return VLC_ENOMEM;
122     }
123
124     p_vout->pf_init = Init;
125     p_vout->pf_end = End;
126     p_vout->pf_manage = NULL;
127     p_vout->pf_render = Render;
128     p_vout->pf_display = NULL;
129     p_vout->pf_control = Control;
130
131     psz_clonelist = config_GetPsz( p_vout, "clone-vout-list" );
132     if( psz_clonelist )
133     {
134         int i_dummy;
135         char *psz_token;
136
137         /* Count the number of defined vout */
138         p_vout->p_sys->i_clones = 1;
139         i_dummy = 0;
140         while( psz_clonelist[i_dummy] != 0 )
141         {
142             if( psz_clonelist[i_dummy] == VOUTSEPARATOR )
143                 p_vout->p_sys->i_clones++;
144             i_dummy++;
145         }
146
147         p_vout->p_sys->ppsz_vout_list = malloc( p_vout->p_sys->i_clones
148                                                 * sizeof(char *) );
149         if( !p_vout->p_sys->ppsz_vout_list )
150         {
151             msg_Err( p_vout, "out of memory" );
152             free( p_vout->p_sys );
153             return VLC_ENOMEM;
154         }
155
156         /* Tokenize the list */
157         i_dummy = 0;
158         psz_token = psz_clonelist;
159         while( psz_token && *psz_token )
160         {
161            char *psz_module;
162            psz_module = psz_token;
163            psz_token = strchr( psz_module, VOUTSEPARATOR );
164            if( psz_token )
165            {
166                *psz_token = '\0';
167                psz_token++;
168            }
169            p_vout->p_sys->ppsz_vout_list[i_dummy] = strdup( psz_module );
170            i_dummy++;
171         }
172
173         free( psz_clonelist );
174     }
175     else
176     {
177         /* No list was specified. We will use the default vout, and get
178          * the number of clones from clone-count */
179         p_vout->p_sys->i_clones = config_GetInt( p_vout, "clone-count" );
180         p_vout->p_sys->ppsz_vout_list = NULL;
181     }
182
183     p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );
184
185     msg_Dbg( p_vout, "spawning %i clone(s)", p_vout->p_sys->i_clones );
186
187     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
188                                      sizeof(vout_thread_t *) );
189     if( p_vout->p_sys->pp_vout == NULL )
190     {
191         msg_Err( p_vout, "out of memory" );
192         free( p_vout->p_sys );
193         return VLC_ENOMEM;
194     }
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Init: initialize Clone video thread output method
201  *****************************************************************************/
202 static int Init( vout_thread_t *p_vout )
203 {
204     int   i_index, i_vout;
205     picture_t *p_pic;
206     char *psz_default_vout;
207
208     I_OUTPUTPICTURES = 0;
209
210     /* Initialize the output structure */
211     p_vout->output.i_chroma = p_vout->render.i_chroma;
212     p_vout->output.i_width  = p_vout->render.i_width;
213     p_vout->output.i_height = p_vout->render.i_height;
214     p_vout->output.i_aspect = p_vout->render.i_aspect;
215
216     /* Try to open the real video output */
217     msg_Dbg( p_vout, "spawning the real video outputs" );
218
219     /* Save the default vout */
220     psz_default_vout = config_GetPsz( p_vout, "vout" );
221
222     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
223     {
224         if( p_vout->p_sys->ppsz_vout_list == NULL 
225             || ( !strncmp( p_vout->p_sys->ppsz_vout_list[i_vout],
226                            "default", 8 ) ) )
227         {
228             p_vout->p_sys->pp_vout[i_vout] =
229                 vout_Create( p_vout, p_vout->render.i_width,
230                              p_vout->render.i_height, p_vout->render.i_chroma, 
231                              p_vout->render.i_aspect );
232         }
233         else
234         {
235             /* create the appropriate vout instead of the default one */
236             config_PutPsz( p_vout, "vout",
237                            p_vout->p_sys->ppsz_vout_list[i_vout] );
238             p_vout->p_sys->pp_vout[i_vout] =
239                 vout_Create( p_vout, p_vout->render.i_width,
240                              p_vout->render.i_height, p_vout->render.i_chroma, 
241                              p_vout->render.i_aspect );
242
243             /* Reset the default value */
244             config_PutPsz( p_vout, "vout", psz_default_vout );
245         }
246
247         if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
248         {
249             msg_Err( p_vout, "failed to clone %i vout threads",
250                      p_vout->p_sys->i_clones );
251             p_vout->p_sys->i_clones = i_vout;
252             if( psz_default_vout ) free( psz_default_vout );
253             RemoveAllVout( p_vout );
254             return VLC_EGENERIC;
255         }
256
257         ADD_CALLBACKS( p_vout->p_sys->pp_vout[ i_vout ], SendEvents );
258     }
259
260     if( psz_default_vout ) free( psz_default_vout );
261     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
262
263     ADD_PARENT_CALLBACKS( SendEventsToChild );
264
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * End: terminate Clone video thread output method
270  *****************************************************************************/
271 static void End( vout_thread_t *p_vout )
272 {
273     int i_index;
274
275     /* Free the fake output buffers we allocated */
276     for( i_index = I_OUTPUTPICTURES ; i_index ; )
277     {
278         i_index--;
279         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
280     }
281 }
282
283 /*****************************************************************************
284  * Destroy: destroy Clone video thread output method
285  *****************************************************************************
286  * Terminate an output method created by CloneCreateOutputMethod
287  *****************************************************************************/
288 static void Destroy( vlc_object_t *p_this )
289 {
290     vout_thread_t *p_vout = (vout_thread_t *)p_this;
291
292     RemoveAllVout( p_vout );
293
294     DEL_PARENT_CALLBACKS( SendEventsToChild );
295
296     free( p_vout->p_sys->pp_vout );
297     free( p_vout->p_sys );
298 }
299
300 /*****************************************************************************
301  * Render: displays previously rendered output
302  *****************************************************************************
303  * This function send the currently rendered image to Clone image, waits
304  * until it is displayed and switch the two rendering buffers, preparing next
305  * frame.
306  *****************************************************************************/
307 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
308 {
309     picture_t *p_outpic = NULL;
310     int i_vout, i_plane;
311
312     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
313     {
314         while( ( p_outpic =
315             vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
316                ) == NULL )
317         {
318             if( p_vout->b_die || p_vout->b_error )
319             {
320                 vout_DestroyPicture(
321                     p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
322                 return;
323             }
324
325             msleep( VOUT_OUTMEM_SLEEP );
326         }
327
328         vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
329                           p_outpic, p_pic->date );
330         vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
331
332         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
333         {
334             uint8_t *p_in, *p_in_end, *p_out;
335             int i_in_pitch = p_pic->p[i_plane].i_pitch;
336             const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
337             const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
338
339             p_in = p_pic->p[i_plane].p_pixels;
340             p_out = p_outpic->p[i_plane].p_pixels;
341
342             if( i_in_pitch == i_copy_pitch
343                  && i_out_pitch == i_copy_pitch )
344             {
345                 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_in_pitch
346                                      * p_outpic->p[i_plane].i_visible_lines );
347             }
348             else
349             {
350                 p_in_end = p_in + i_in_pitch *
351                     p_outpic->p[i_plane].i_visible_lines;
352
353                 while( p_in < p_in_end )
354                 {
355                     p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
356                     p_in += i_in_pitch;
357                     p_out += i_out_pitch;
358                 }
359             }
360         }
361
362         vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
363         vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
364     }
365 }
366
367 /*****************************************************************************
368  * RemoveAllVout: destroy all the child video output threads
369  *****************************************************************************/
370 static void RemoveAllVout( vout_thread_t *p_vout )
371 {
372     while( p_vout->p_sys->i_clones )
373     {
374          --p_vout->p_sys->i_clones;
375          DEL_CALLBACKS( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones],
376                         SendEvents );
377          vlc_object_detach( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
378          vout_Destroy( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
379     }
380 }
381
382 /*****************************************************************************
383  * SendEvents: forward mouse and keyboard events to the parent p_vout
384  *****************************************************************************/
385 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
386                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
387 {
388     var_Set( (vlc_object_t *)p_data, psz_var, newval );
389
390     return VLC_SUCCESS;
391 }
392
393 /*****************************************************************************
394  * SendEventsToChild: forward events to the child/children vout
395  *****************************************************************************/
396 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
397                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
398 {
399     vout_thread_t *p_vout = (vout_thread_t *)p_this;
400     int i_vout;
401
402     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
403     {
404         var_Set( p_vout->p_sys->pp_vout[ i_vout ], psz_var, newval );
405
406         if( !strcmp( psz_var, "fullscreen" ) ) break;
407     }
408
409     return VLC_SUCCESS;
410 }