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