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