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