]> git.sesse.net Git - vlc/blob - plugins/filter/clone.c
f12cd0003f0835afe0987341934d29bff67bd8f4
[vlc] / plugins / filter / clone.c
1 /*****************************************************************************
2  * clone.c : Clone video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: clone.c,v 1.1 2002/05/13 19:30:40 sam 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 <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <videolan/vlc.h>
32
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "filter_common.h"
37
38 /*****************************************************************************
39  * Capabilities defined in the other files.
40  *****************************************************************************/
41 static void vout_getfunctions( function_list_t * p_function_list );
42
43 /*****************************************************************************
44  * Build configuration tree.
45  *****************************************************************************/
46 MODULE_CONFIG_START
47 MODULE_CONFIG_STOP
48
49 MODULE_INIT_START
50     SET_DESCRIPTION( _("image clone video module") )
51     /* Capability score set to 0 because we don't want to be spawned
52      * as a video output unless explicitly requested to */
53     ADD_CAPABILITY( VOUT, 0 )
54     ADD_SHORTCUT( "clone" )
55 MODULE_INIT_STOP
56
57 MODULE_ACTIVATE_START
58     vout_getfunctions( &p_module->p_functions->vout );
59 MODULE_ACTIVATE_STOP
60
61 MODULE_DEACTIVATE_START
62 MODULE_DEACTIVATE_STOP
63
64 /*****************************************************************************
65  * vout_sys_t: Clone video output method descriptor
66  *****************************************************************************
67  * This structure is part of the video output thread descriptor.
68  * It describes the Clone specific properties of an output thread.
69  *****************************************************************************/
70 typedef struct vout_sys_s
71 {
72     int    i_clones;
73     vout_thread_t **pp_vout;
74
75 } vout_sys_t;
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int  vout_Create    ( vout_thread_t * );
81 static int  vout_Init      ( vout_thread_t * );
82 static void vout_End       ( vout_thread_t * );
83 static void vout_Destroy   ( vout_thread_t * );
84 static int  vout_Manage    ( vout_thread_t * );
85 static void vout_Render    ( vout_thread_t *, picture_t * );
86 static void vout_Display   ( vout_thread_t *, picture_t * );
87
88 static void RemoveAllVout  ( vout_thread_t *p_vout );
89
90 /*****************************************************************************
91  * Functions exported as capabilities. They are declared as static so that
92  * we don't pollute the namespace too much.
93  *****************************************************************************/
94 static void vout_getfunctions( function_list_t * p_function_list )
95 {
96     p_function_list->functions.vout.pf_create     = vout_Create;
97     p_function_list->functions.vout.pf_init       = vout_Init;
98     p_function_list->functions.vout.pf_end        = vout_End;
99     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
100     p_function_list->functions.vout.pf_manage     = vout_Manage;
101     p_function_list->functions.vout.pf_render     = vout_Render;
102     p_function_list->functions.vout.pf_display    = vout_Display;
103 }
104
105 /*****************************************************************************
106  * vout_Create: allocates Clone video thread output method
107  *****************************************************************************
108  * This function allocates and initializes a Clone vout method.
109  *****************************************************************************/
110 static int vout_Create( vout_thread_t *p_vout )
111 {
112     char *psz_method, *psz_method_orig;
113
114     /* Allocate structure */
115     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
116     if( p_vout->p_sys == NULL )
117     {
118         intf_ErrMsg( "vout error: out of memory" );
119         return( 1 );
120     }
121
122     /* Look what method was requested */
123     if( !(psz_method = psz_method_orig = config_GetPszVariable( "filter" )) )
124     {
125         intf_ErrMsg( "vout error: configuration variable %s empty", "filter" );
126         return( 1 );
127     }
128
129     while( *psz_method && *psz_method != ':' )
130     {
131         psz_method++;
132     }
133
134     if( *psz_method )
135     {
136         p_vout->p_sys->i_clones = atoi( psz_method + 1 );
137     }
138     else
139     {
140         intf_ErrMsg( "vout error: "
141                      "no valid clone count provided, using clone:2" );
142         p_vout->p_sys->i_clones = 2;
143     }
144
145     p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );
146
147     intf_WarnMsg( 3, "vout info: spawning %i clone(s)",
148                   p_vout->p_sys->i_clones );
149
150     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
151                                      sizeof(vout_thread_t *) );
152     if( p_vout->p_sys->pp_vout == NULL )
153     {
154         intf_ErrMsg( "vout error: out of memory" );
155         free( psz_method_orig );
156         free( p_vout->p_sys );
157         return( 1 );
158     }
159
160     free( psz_method_orig );
161
162     return( 0 );
163 }
164
165 /*****************************************************************************
166  * vout_Init: initialize Clone video thread output method
167  *****************************************************************************/
168 static int vout_Init( vout_thread_t *p_vout )
169 {
170     int   i_index, i_vout;
171     char *psz_filter;
172     picture_t *p_pic;
173     
174     I_OUTPUTPICTURES = 0;
175
176     /* Initialize the output structure */
177     p_vout->output.i_chroma = p_vout->render.i_chroma;
178     p_vout->output.i_width  = p_vout->render.i_width;
179     p_vout->output.i_height = p_vout->render.i_height;
180     p_vout->output.i_aspect = p_vout->render.i_aspect;
181
182     /* Try to open the real video output */
183     psz_filter = config_GetPszVariable( "filter" );
184     config_PutPszVariable( "filter", NULL );
185
186     intf_WarnMsg( 3, "vout info: spawning the real video outputs" );
187
188     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
189     {
190         p_vout->p_sys->pp_vout[ i_vout ] =
191                 vout_CreateThread( NULL,
192                             p_vout->render.i_width, p_vout->render.i_height,
193                             p_vout->render.i_chroma, p_vout->render.i_aspect );
194         if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
195         {
196             intf_ErrMsg( "vout error: failed to clone %i vout threads",
197                          p_vout->p_sys->i_clones );
198             p_vout->p_sys->i_clones = i_vout;
199             RemoveAllVout( p_vout );
200             config_PutPszVariable( "filter", psz_filter );
201             if( psz_filter ) free( psz_filter );
202             return 0;
203         }
204     }
205
206     config_PutPszVariable( "filter", psz_filter );
207     if( psz_filter ) free( psz_filter );
208
209     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
210
211     return( 0 );
212 }
213
214 /*****************************************************************************
215  * vout_End: terminate Clone video thread output method
216  *****************************************************************************/
217 static void vout_End( vout_thread_t *p_vout )
218 {
219     int i_index;
220
221     /* Free the fake output buffers we allocated */
222     for( i_index = I_OUTPUTPICTURES ; i_index ; )
223     {
224         i_index--;
225         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
226     }
227 }
228
229 /*****************************************************************************
230  * vout_Destroy: destroy Clone video thread output method
231  *****************************************************************************
232  * Terminate an output method created by CloneCreateOutputMethod
233  *****************************************************************************/
234 static void vout_Destroy( vout_thread_t *p_vout )
235 {
236     RemoveAllVout( p_vout );
237
238     free( p_vout->p_sys->pp_vout );
239     free( p_vout->p_sys );
240 }
241
242 /*****************************************************************************
243  * vout_Manage: handle Clone events
244  *****************************************************************************
245  * This function should be called regularly by video output thread. It manages
246  * console events. It returns a non null value on error.
247  *****************************************************************************/
248 static int vout_Manage( vout_thread_t *p_vout )
249 {
250     return( 0 );
251 }
252
253 /*****************************************************************************
254  * vout_Render: displays previously rendered output
255  *****************************************************************************
256  * This function send the currently rendered image to Clone image, waits
257  * until it is displayed and switch the two rendering buffers, preparing next
258  * frame.
259  *****************************************************************************/
260 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
261 {
262     picture_t *p_outpic = NULL;
263     int i_vout, i_plane;
264
265     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
266     {
267         while( ( p_outpic =
268             vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
269                ) == NULL )
270         {
271             if( p_vout->b_die || p_vout->b_error )
272             {
273                 vout_DestroyPicture(
274                     p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
275                 return;
276             }
277
278             msleep( VOUT_OUTMEM_SLEEP );
279         }
280
281         vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
282                           p_outpic, p_pic->date );
283         vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
284
285         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
286         {
287             u8 *p_in, *p_in_end, *p_out;
288             int i_in_pitch = p_pic->p[i_plane].i_pitch;
289             const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
290
291             p_in = p_pic->p[i_plane].p_pixels;
292
293             p_in_end = p_in + p_outpic->p[i_plane].i_lines
294                                * p_pic->p[i_plane].i_pitch;
295
296             p_out = p_outpic->p[i_plane].p_pixels;
297
298             while( p_in < p_in_end )
299             {
300                 FAST_MEMCPY( p_out, p_in, i_out_pitch );
301                 p_in += i_in_pitch;
302                 p_out += i_out_pitch;
303             }
304         }
305
306         vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
307         vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
308     }
309 }
310
311 /*****************************************************************************
312  * vout_Display: displays previously rendered output
313  *****************************************************************************
314  * This function send the currently rendered image to Invert image, waits
315  * until it is displayed and switch the two rendering buffers, preparing next
316  * frame.
317  *****************************************************************************/
318 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
319 {
320     ;
321 }
322
323 /*****************************************************************************
324  * RemoveAllVout: destroy all the child video output threads
325  *****************************************************************************/
326 static void RemoveAllVout( vout_thread_t *p_vout )
327 {
328     while( p_vout->p_sys->i_clones )
329     {
330          --p_vout->p_sys->i_clones;
331          vout_DestroyThread(
332                    p_vout->p_sys->pp_vout[ p_vout->p_sys->i_clones ], NULL );
333     }
334 }
335