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