1 /*****************************************************************************
2 * clone.c : Clone video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
36 #include "filter_common.h"
38 #define VOUTSEPARATOR ','
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
46 static int Init ( vout_thread_t * );
47 static void End ( vout_thread_t * );
48 static void Render ( vout_thread_t *, picture_t * );
50 static void RemoveAllVout ( vout_thread_t *p_vout );
52 static int SendEvents( vlc_object_t *, char const *,
53 vlc_value_t, vlc_value_t, void * );
55 /*****************************************************************************
57 *****************************************************************************/
58 #define COUNT_TEXT N_("Number of clones")
59 #define COUNT_LONGTEXT N_("Number of video windows in which to "\
62 #define VOUTLIST_TEXT N_("Video output modules")
63 #define VOUTLIST_LONGTEXT N_("You can use specific video output modules " \
64 "for the clones. Use a comma-separated list of modules." )
66 #define CFG_PREFIX "clone-"
69 set_description( N_("Clone video filter") );
70 set_capability( "video filter", 0 );
71 set_shortname( N_("Clone" ));
72 set_category( CAT_VIDEO );
73 set_subcategory( SUBCAT_VIDEO_VFILTER );
75 add_integer( CFG_PREFIX "count", 2, NULL, COUNT_TEXT, COUNT_LONGTEXT, false );
76 add_string ( CFG_PREFIX "vout-list", NULL, NULL, VOUTLIST_TEXT, VOUTLIST_LONGTEXT, true );
78 add_shortcut( "clone" );
79 set_callbacks( Create, Destroy );
82 static const char *const ppsz_filter_options[] = {
83 "count", "vout-list", NULL
86 /*****************************************************************************
87 * vout_sys_t: Clone video output method descriptor
88 *****************************************************************************
89 * This structure is part of the video output thread descriptor.
90 * It describes the Clone specific properties of an output thread.
91 *****************************************************************************/
96 /* list of vout modules to use. "default" will launch a default
97 * module. If specified, overrides the setting in i_clones (which it
98 * sets to the list length) */
99 char **ppsz_vout_list;
101 vout_thread_t **pp_vout;
104 /*****************************************************************************
105 * Control: control facility for the vout (forwards to child vout)
106 *****************************************************************************/
107 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
110 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
112 vout_vaControl( p_vout->p_sys->pp_vout[ i_vout ], i_query, args );
117 /*****************************************************************************
118 * Create: allocates Clone video thread output method
119 *****************************************************************************
120 * This function allocates and initializes a Clone vout method.
121 *****************************************************************************/
122 static int Create( vlc_object_t *p_this )
124 vout_thread_t *p_vout = (vout_thread_t *)p_this;
127 /* Allocate structure */
128 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
129 if( p_vout->p_sys == NULL )
132 p_vout->pf_init = Init;
133 p_vout->pf_end = End;
134 p_vout->pf_manage = NULL;
135 p_vout->pf_render = Render;
136 p_vout->pf_display = NULL;
137 p_vout->pf_control = Control;
139 config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
142 psz_clonelist = var_CreateGetNonEmptyString( p_vout,
143 CFG_PREFIX "vout-list" );
149 /* Count the number of defined vout */
150 p_vout->p_sys->i_clones = 1;
152 while( psz_clonelist[i_dummy] != 0 )
154 if( psz_clonelist[i_dummy] == VOUTSEPARATOR )
155 p_vout->p_sys->i_clones++;
159 p_vout->p_sys->ppsz_vout_list = malloc( p_vout->p_sys->i_clones
161 if( !p_vout->p_sys->ppsz_vout_list )
163 free( psz_clonelist );
164 free( p_vout->p_sys );
168 /* Tokenize the list */
170 psz_token = psz_clonelist;
171 while( psz_token && *psz_token )
174 psz_module = psz_token;
175 psz_token = strchr( psz_module, VOUTSEPARATOR );
181 p_vout->p_sys->ppsz_vout_list[i_dummy] = strdup( psz_module );
185 free( psz_clonelist );
189 /* No list was specified. We will use the default vout, and get
190 * the number of clones from clone-count */
191 p_vout->p_sys->i_clones =
192 var_CreateGetInteger( p_vout, CFG_PREFIX "count" );
193 p_vout->p_sys->ppsz_vout_list = NULL;
196 p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );
198 msg_Dbg( p_vout, "spawning %i clone(s)", p_vout->p_sys->i_clones );
200 p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
201 sizeof(vout_thread_t *) );
202 if( p_vout->p_sys->pp_vout == NULL )
204 msg_Err( p_vout, "out of memory" );
205 free( p_vout->p_sys );
212 /*****************************************************************************
213 * Init: initialize Clone video thread output method
214 *****************************************************************************/
215 static int Init( vout_thread_t *p_vout )
219 char *psz_default_vout;
222 I_OUTPUTPICTURES = 0;
223 memset( &fmt, 0, sizeof(video_format_t) );
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;
233 /* Try to open the real video output */
234 msg_Dbg( p_vout, "spawning the real video outputs" );
236 /* Save the default vout */
237 psz_default_vout = config_GetPsz( p_vout, "vout" );
239 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
241 if( p_vout->p_sys->ppsz_vout_list == NULL
242 || ( !strncmp( p_vout->p_sys->ppsz_vout_list[i_vout],
245 p_vout->p_sys->pp_vout[i_vout] =
246 vout_Create( p_vout, &fmt );
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 );
256 /* Reset the default value */
257 config_PutPsz( p_vout, "vout", psz_default_vout );
260 if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
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 );
270 ADD_CALLBACKS( p_vout->p_sys->pp_vout[ i_vout ], SendEvents );
273 free( psz_default_vout );
274 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
276 ADD_PARENT_CALLBACKS( SendEventsToChild );
281 /*****************************************************************************
282 * End: terminate Clone video thread output method
283 *****************************************************************************/
284 static void End( vout_thread_t *p_vout )
288 DEL_PARENT_CALLBACKS( SendEventsToChild );
290 /* Free the fake output buffers we allocated */
291 for( i_index = I_OUTPUTPICTURES ; i_index ; )
294 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
297 RemoveAllVout( p_vout );
300 /*****************************************************************************
301 * Destroy: destroy Clone video thread output method
302 *****************************************************************************
303 * Terminate an output method created by CloneCreateOutputMethod
304 *****************************************************************************/
305 static void Destroy( vlc_object_t *p_this )
307 vout_thread_t *p_vout = (vout_thread_t *)p_this;
309 free( p_vout->p_sys->pp_vout );
310 free( p_vout->p_sys );
313 /*****************************************************************************
314 * Render: displays previously rendered output
315 *****************************************************************************
316 * This function send the currently rendered image to Clone image, waits
317 * until it is displayed and switch the two rendering buffers, preparing next
319 *****************************************************************************/
320 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
322 picture_t *p_outpic = NULL;
325 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
328 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
331 if( !vlc_object_alive (p_vout) || p_vout->b_error )
334 p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
338 msleep( VOUT_OUTMEM_SLEEP );
341 vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
342 p_outpic, p_pic->date );
343 vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
345 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
347 uint8_t *p_in, *p_in_end, *p_out;
348 int i_in_pitch = p_pic->p[i_plane].i_pitch;
349 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
350 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
352 p_in = p_pic->p[i_plane].p_pixels;
353 p_out = p_outpic->p[i_plane].p_pixels;
355 if( i_in_pitch == i_copy_pitch
356 && i_out_pitch == i_copy_pitch )
358 vlc_memcpy( p_out, p_in, i_in_pitch
359 * p_outpic->p[i_plane].i_visible_lines );
363 p_in_end = p_in + i_in_pitch *
364 p_outpic->p[i_plane].i_visible_lines;
366 while( p_in < p_in_end )
368 vlc_memcpy( p_out, p_in, i_copy_pitch );
370 p_out += i_out_pitch;
375 vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
376 vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
380 /*****************************************************************************
381 * RemoveAllVout: destroy all the child video output threads
382 *****************************************************************************/
383 static void RemoveAllVout( vout_thread_t *p_vout )
385 while( p_vout->p_sys->i_clones )
387 --p_vout->p_sys->i_clones;
388 DEL_CALLBACKS( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones],
390 vout_CloseAndRelease( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
394 /*****************************************************************************
395 * SendEvents: forward mouse and keyboard events to the parent p_vout
396 *****************************************************************************/
397 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
398 vlc_value_t oldval, vlc_value_t newval, void *p_data )
400 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
401 var_Set( (vlc_object_t *)p_data, psz_var, newval );
406 /*****************************************************************************
407 * SendEventsToChild: forward events to the child/children vout
408 *****************************************************************************/
409 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
410 vlc_value_t oldval, vlc_value_t newval, void *p_data )
412 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
413 vout_thread_t *p_vout = (vout_thread_t *)p_this;
416 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
418 var_Set( p_vout->p_sys->pp_vout[ i_vout ], psz_var, newval );
420 if( !strcmp( psz_var, "fullscreen" ) ) break;