1 /*****************************************************************************
2 * clone.c : Clone video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: clone.c,v 1.12 2004/01/25 20:05:28 hartman Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
33 #include "filter_common.h"
35 #define VOUTSEPARATOR ','
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Destroy ( vlc_object_t * );
43 static int Init ( vout_thread_t * );
44 static void End ( vout_thread_t * );
45 static void Render ( vout_thread_t *, picture_t * );
47 static void RemoveAllVout ( vout_thread_t *p_vout );
49 static int SendEvents( vlc_object_t *, char const *,
50 vlc_value_t, vlc_value_t, void * );
52 /*****************************************************************************
54 *****************************************************************************/
55 #define COUNT_TEXT N_("Number of clones")
56 #define COUNT_LONGTEXT N_("Select the number of video windows in which to "\
59 #define VOUTLIST_TEXT N_("List of vout modules")
60 #define VOUTLIST_LONGTEXT N_("Select the specific vout modules that you want to activate")
63 set_description( _("Clone video filter") );
64 set_capability( "video filter", 0 );
66 add_integer( "clone-count", 2, NULL, COUNT_TEXT, COUNT_LONGTEXT, VLC_FALSE );
67 add_string ( "clone-vout-list", NULL, NULL, VOUTLIST_TEXT, VOUTLIST_LONGTEXT, VLC_FALSE );
69 add_shortcut( "clone" );
70 set_callbacks( Create, Destroy );
73 /*****************************************************************************
74 * vout_sys_t: Clone video output method descriptor
75 *****************************************************************************
76 * This structure is part of the video output thread descriptor.
77 * It describes the Clone specific properties of an output thread.
78 *****************************************************************************/
83 /* list of vout modules to use. "default" will launch a default
84 * module. If specified, overrides the setting in i_clones (which it
85 * sets to the list length) */
86 char **ppsz_vout_list;
88 vout_thread_t **pp_vout;
91 /*****************************************************************************
92 * Create: allocates Clone video thread output method
93 *****************************************************************************
94 * This function allocates and initializes a Clone vout method.
95 *****************************************************************************/
96 static int Create( vlc_object_t *p_this )
98 vout_thread_t *p_vout = (vout_thread_t *)p_this;
101 /* Allocate structure */
102 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
103 if( p_vout->p_sys == NULL )
105 msg_Err( p_vout, "out of memory" );
109 p_vout->pf_init = Init;
110 p_vout->pf_end = End;
111 p_vout->pf_manage = NULL;
112 p_vout->pf_render = Render;
113 p_vout->pf_display = NULL;
115 psz_clonelist = config_GetPsz( p_vout, "clone-vout-list" );
121 /* Count the number of defined vout */
122 p_vout->p_sys->i_clones = 1;
124 while( psz_clonelist[i_dummy] != 0 )
126 if( psz_clonelist[i_dummy] == VOUTSEPARATOR )
127 p_vout->p_sys->i_clones++;
131 p_vout->p_sys->ppsz_vout_list = malloc( p_vout->p_sys->i_clones
133 if( !p_vout->p_sys->ppsz_vout_list )
135 msg_Err( p_vout, "out of memory" );
136 free( p_vout->p_sys );
140 /* Tokenize the list */
142 psz_token = psz_clonelist;
143 while( psz_token && *psz_token )
146 psz_module = psz_token;
147 psz_token = strchr( psz_module, VOUTSEPARATOR );
153 p_vout->p_sys->ppsz_vout_list[i_dummy] = strdup( psz_module );
157 free( psz_clonelist );
161 /* No list was specified. We will use the default vout, and get
162 * the number of clones from clone-count */
163 p_vout->p_sys->i_clones = config_GetInt( p_vout, "clone-count" );
164 p_vout->p_sys->ppsz_vout_list = NULL;
167 p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );
169 msg_Dbg( p_vout, "spawning %i clone(s)", p_vout->p_sys->i_clones );
171 p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
172 sizeof(vout_thread_t *) );
173 if( p_vout->p_sys->pp_vout == NULL )
175 msg_Err( p_vout, "out of memory" );
176 free( p_vout->p_sys );
183 /*****************************************************************************
184 * Init: initialize Clone video thread output method
185 *****************************************************************************/
186 static int Init( vout_thread_t *p_vout )
190 char *psz_default_vout;
192 I_OUTPUTPICTURES = 0;
194 /* Initialize the output structure */
195 p_vout->output.i_chroma = p_vout->render.i_chroma;
196 p_vout->output.i_width = p_vout->render.i_width;
197 p_vout->output.i_height = p_vout->render.i_height;
198 p_vout->output.i_aspect = p_vout->render.i_aspect;
200 /* Try to open the real video output */
201 msg_Dbg( p_vout, "spawning the real video outputs" );
203 /* Save the default vout */
204 psz_default_vout = config_GetPsz( p_vout, "vout" );
206 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
208 if( p_vout->p_sys->ppsz_vout_list == NULL
209 || ( !strncmp( p_vout->p_sys->ppsz_vout_list[i_vout],
212 p_vout->p_sys->pp_vout[i_vout] =
213 vout_Create( p_vout, p_vout->render.i_width,
214 p_vout->render.i_height, p_vout->render.i_chroma,
215 p_vout->render.i_aspect );
219 /* create the appropriate vout instead of the default one */
220 config_PutPsz( p_vout, "vout",
221 p_vout->p_sys->ppsz_vout_list[i_vout] );
222 p_vout->p_sys->pp_vout[i_vout] =
223 vout_Create( p_vout, p_vout->render.i_width,
224 p_vout->render.i_height, p_vout->render.i_chroma,
225 p_vout->render.i_aspect );
227 /* Reset the default value */
228 config_PutPsz( p_vout, "vout", psz_default_vout );
231 if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
233 msg_Err( p_vout, "failed to clone %i vout threads",
234 p_vout->p_sys->i_clones );
235 p_vout->p_sys->i_clones = i_vout;
236 if( psz_default_vout ) free( psz_default_vout );
237 RemoveAllVout( p_vout );
241 ADD_CALLBACKS( p_vout->p_sys->pp_vout[ i_vout ], SendEvents );
244 if( psz_default_vout ) free( psz_default_vout );
245 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
247 ADD_PARENT_CALLBACKS( SendEventsToChild );
252 /*****************************************************************************
253 * End: terminate Clone video thread output method
254 *****************************************************************************/
255 static void End( vout_thread_t *p_vout )
259 /* Free the fake output buffers we allocated */
260 for( i_index = I_OUTPUTPICTURES ; i_index ; )
263 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
267 /*****************************************************************************
268 * Destroy: destroy Clone video thread output method
269 *****************************************************************************
270 * Terminate an output method created by CloneCreateOutputMethod
271 *****************************************************************************/
272 static void Destroy( vlc_object_t *p_this )
274 vout_thread_t *p_vout = (vout_thread_t *)p_this;
276 RemoveAllVout( p_vout );
278 DEL_PARENT_CALLBACKS( SendEventsToChild );
280 free( p_vout->p_sys->pp_vout );
281 free( p_vout->p_sys );
284 /*****************************************************************************
285 * Render: displays previously rendered output
286 *****************************************************************************
287 * This function send the currently rendered image to Clone image, waits
288 * until it is displayed and switch the two rendering buffers, preparing next
290 *****************************************************************************/
291 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
293 picture_t *p_outpic = NULL;
296 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
299 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
302 if( p_vout->b_die || p_vout->b_error )
305 p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
309 msleep( VOUT_OUTMEM_SLEEP );
312 vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
313 p_outpic, p_pic->date );
314 vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
316 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
318 uint8_t *p_in, *p_in_end, *p_out;
319 int i_in_pitch = p_pic->p[i_plane].i_pitch;
320 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
321 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
323 p_in = p_pic->p[i_plane].p_pixels;
324 p_out = p_outpic->p[i_plane].p_pixels;
326 if( i_in_pitch == i_copy_pitch
327 && i_out_pitch == i_copy_pitch )
329 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_in_pitch
330 * p_outpic->p[i_plane].i_lines );
334 p_in_end = p_in + i_in_pitch * p_outpic->p[i_plane].i_lines;
336 while( p_in < p_in_end )
338 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
340 p_out += i_out_pitch;
345 vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
346 vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
350 /*****************************************************************************
351 * RemoveAllVout: destroy all the child video output threads
352 *****************************************************************************/
353 static void RemoveAllVout( vout_thread_t *p_vout )
355 while( p_vout->p_sys->i_clones )
357 --p_vout->p_sys->i_clones;
358 DEL_CALLBACKS( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones],
360 vlc_object_detach( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
361 vout_Destroy( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
365 /*****************************************************************************
366 * SendEvents: forward mouse and keyboard events to the parent p_vout
367 *****************************************************************************/
368 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
369 vlc_value_t oldval, vlc_value_t newval, void *p_data )
371 var_Set( (vlc_object_t *)p_data, psz_var, newval );
376 /*****************************************************************************
377 * SendEventsToChild: forward events to the child/children vout
378 *****************************************************************************/
379 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
380 vlc_value_t oldval, vlc_value_t newval, void *p_data )
382 vout_thread_t *p_vout = (vout_thread_t *)p_this;
385 for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
387 var_Set( p_vout->p_sys->pp_vout[ i_vout ], psz_var, newval );
389 if( !strcmp( psz_var, "fullscreen" ) ) break;