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