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