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