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