]> git.sesse.net Git - vlc/blob - modules/video_filter/clone.c
* ./modules/video_output/wingdi.c: the GDI video output now properly sets
[vlc] / modules / video_filter / clone.c
1 /*****************************************************************************
2  * clone.c : Clone video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: clone.c,v 1.2 2002/11/23 02:40:30 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 <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 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static int  Init      ( vout_thread_t * );
42 static void End       ( vout_thread_t * );
43 static void Render    ( vout_thread_t *, picture_t * );
44
45 static void RemoveAllVout  ( vout_thread_t *p_vout );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 #define COUNT_TEXT N_("Number of clones")
51 #define COUNT_LONGTEXT N_("Select the number of video windows in which to "\
52     "clone the video")
53
54 vlc_module_begin();
55     add_category_hint( N_("Miscellaneous"), NULL );
56     add_integer( "clone-count", 2, NULL, COUNT_TEXT, COUNT_LONGTEXT );
57     set_description( _("image clone video module") );
58     set_capability( "video filter", 0 );
59     add_shortcut( "clone" );
60     set_callbacks( Create, Destroy );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * vout_sys_t: Clone video output method descriptor
65  *****************************************************************************
66  * This structure is part of the video output thread descriptor.
67  * It describes the Clone specific properties of an output thread.
68  *****************************************************************************/
69 struct vout_sys_t
70 {
71     int    i_clones;
72     vout_thread_t **pp_vout;
73 };
74
75 /*****************************************************************************
76  * Create: allocates Clone video thread output method
77  *****************************************************************************
78  * This function allocates and initializes a Clone vout method.
79  *****************************************************************************/
80 static int Create( vlc_object_t *p_this )
81 {
82     vout_thread_t *p_vout = (vout_thread_t *)p_this;
83
84     /* Allocate structure */
85     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
86     if( p_vout->p_sys == NULL )
87     {
88         msg_Err( p_vout, "out of memory" );
89         return( 1 );
90     }
91
92     p_vout->pf_init = Init;
93     p_vout->pf_end = End;
94     p_vout->pf_manage = NULL;
95     p_vout->pf_render = Render;
96     p_vout->pf_display = NULL;
97
98     /* Look what method was requested */
99     p_vout->p_sys->i_clones = config_GetInt( p_vout, "clone-count" );
100
101     p_vout->p_sys->i_clones = __MAX( 1, __MIN( 99, p_vout->p_sys->i_clones ) );
102
103     msg_Dbg( p_vout, "spawning %i clone(s)", p_vout->p_sys->i_clones );
104
105     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_clones *
106                                      sizeof(vout_thread_t *) );
107     if( p_vout->p_sys->pp_vout == NULL )
108     {
109         msg_Err( p_vout, "out of memory" );
110         free( p_vout->p_sys );
111         return( 1 );
112     }
113
114     return( 0 );
115 }
116
117 /*****************************************************************************
118  * Init: initialize Clone video thread output method
119  *****************************************************************************/
120 static int Init( vout_thread_t *p_vout )
121 {
122     int   i_index, i_vout;
123     picture_t *p_pic;
124     
125     I_OUTPUTPICTURES = 0;
126
127     /* Initialize the output structure */
128     p_vout->output.i_chroma = p_vout->render.i_chroma;
129     p_vout->output.i_width  = p_vout->render.i_width;
130     p_vout->output.i_height = p_vout->render.i_height;
131     p_vout->output.i_aspect = p_vout->render.i_aspect;
132
133     /* Try to open the real video output */
134     msg_Dbg( p_vout, "spawning the real video outputs" );
135
136     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
137     {
138         p_vout->p_sys->pp_vout[ i_vout ] =
139                 vout_CreateThread( p_vout,
140                             p_vout->render.i_width, p_vout->render.i_height,
141                             p_vout->render.i_chroma, p_vout->render.i_aspect );
142         if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
143         {
144             msg_Err( p_vout, "failed to clone %i vout threads",
145                              p_vout->p_sys->i_clones );
146             p_vout->p_sys->i_clones = i_vout;
147             RemoveAllVout( p_vout );
148             return 0;
149         }
150     }
151
152     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
153
154     return( 0 );
155 }
156
157 /*****************************************************************************
158  * End: terminate Clone video thread output method
159  *****************************************************************************/
160 static void End( vout_thread_t *p_vout )
161 {
162     int i_index;
163
164     /* Free the fake output buffers we allocated */
165     for( i_index = I_OUTPUTPICTURES ; i_index ; )
166     {
167         i_index--;
168         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
169     }
170 }
171
172 /*****************************************************************************
173  * Destroy: destroy Clone video thread output method
174  *****************************************************************************
175  * Terminate an output method created by CloneCreateOutputMethod
176  *****************************************************************************/
177 static void Destroy( vlc_object_t *p_this )
178 {
179     vout_thread_t *p_vout = (vout_thread_t *)p_this;
180
181     RemoveAllVout( p_vout );
182
183     free( p_vout->p_sys->pp_vout );
184     free( p_vout->p_sys );
185 }
186
187 /*****************************************************************************
188  * Render: displays previously rendered output
189  *****************************************************************************
190  * This function send the currently rendered image to Clone image, waits
191  * until it is displayed and switch the two rendering buffers, preparing next
192  * frame.
193  *****************************************************************************/
194 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
195 {
196     picture_t *p_outpic = NULL;
197     int i_vout, i_plane;
198
199     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
200     {
201         while( ( p_outpic =
202             vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
203                ) == NULL )
204         {
205             if( p_vout->b_die || p_vout->b_error )
206             {
207                 vout_DestroyPicture(
208                     p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
209                 return;
210             }
211
212             msleep( VOUT_OUTMEM_SLEEP );
213         }
214
215         vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
216                           p_outpic, p_pic->date );
217         vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
218
219         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
220         {
221             u8 *p_in, *p_in_end, *p_out;
222             int i_in_pitch = p_pic->p[i_plane].i_pitch;
223             const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
224
225             p_in = p_pic->p[i_plane].p_pixels;
226
227             p_in_end = p_in + p_outpic->p[i_plane].i_lines
228                                * p_pic->p[i_plane].i_pitch;
229
230             p_out = p_outpic->p[i_plane].p_pixels;
231
232             while( p_in < p_in_end )
233             {
234                 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_out_pitch );
235                 p_in += i_in_pitch;
236                 p_out += i_out_pitch;
237             }
238         }
239
240         vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
241         vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
242     }
243 }
244
245 /*****************************************************************************
246  * RemoveAllVout: destroy all the child video output threads
247  *****************************************************************************/
248 static void RemoveAllVout( vout_thread_t *p_vout )
249 {
250     while( p_vout->p_sys->i_clones )
251     {
252          --p_vout->p_sys->i_clones;
253          vout_DestroyThread( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
254     }
255 }
256