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