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