]> git.sesse.net Git - vlc/blob - modules/video_filter/clone.c
* ./modules/video_filter/*.c: all filters now properly use i_visible_pitch
[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.4 2003/01/09 17:47:05 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 ] = vout_Create( p_vout,
139                             p_vout->render.i_width, p_vout->render.i_height,
140                             p_vout->render.i_chroma, p_vout->render.i_aspect );
141         if( p_vout->p_sys->pp_vout[ i_vout ] == NULL )
142         {
143             msg_Err( p_vout, "failed to clone %i vout threads",
144                              p_vout->p_sys->i_clones );
145             p_vout->p_sys->i_clones = i_vout;
146             RemoveAllVout( p_vout );
147             return 0;
148         }
149     }
150
151     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
152
153     return( 0 );
154 }
155
156 /*****************************************************************************
157  * End: terminate Clone video thread output method
158  *****************************************************************************/
159 static void End( vout_thread_t *p_vout )
160 {
161     int i_index;
162
163     /* Free the fake output buffers we allocated */
164     for( i_index = I_OUTPUTPICTURES ; i_index ; )
165     {
166         i_index--;
167         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
168     }
169 }
170
171 /*****************************************************************************
172  * Destroy: destroy Clone video thread output method
173  *****************************************************************************
174  * Terminate an output method created by CloneCreateOutputMethod
175  *****************************************************************************/
176 static void Destroy( vlc_object_t *p_this )
177 {
178     vout_thread_t *p_vout = (vout_thread_t *)p_this;
179
180     RemoveAllVout( p_vout );
181
182     free( p_vout->p_sys->pp_vout );
183     free( p_vout->p_sys );
184 }
185
186 /*****************************************************************************
187  * Render: displays previously rendered output
188  *****************************************************************************
189  * This function send the currently rendered image to Clone image, waits
190  * until it is displayed and switch the two rendering buffers, preparing next
191  * frame.
192  *****************************************************************************/
193 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
194 {
195     picture_t *p_outpic = NULL;
196     int i_vout, i_plane;
197
198     for( i_vout = 0; i_vout < p_vout->p_sys->i_clones; i_vout++ )
199     {
200         while( ( p_outpic =
201             vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ], 0, 0, 0 )
202                ) == NULL )
203         {
204             if( p_vout->b_die || p_vout->b_error )
205             {
206                 vout_DestroyPicture(
207                     p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
208                 return;
209             }
210
211             msleep( VOUT_OUTMEM_SLEEP );
212         }
213
214         vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ],
215                           p_outpic, p_pic->date );
216         vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
217
218         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
219         {
220             uint8_t *p_in, *p_in_end, *p_out;
221             int i_in_pitch = p_pic->p[i_plane].i_pitch;
222             const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
223             const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
224
225             p_in = p_pic->p[i_plane].p_pixels;
226             p_out = p_outpic->p[i_plane].p_pixels;
227
228             if( i_in_pitch == i_copy_pitch
229                  && i_out_pitch == i_copy_pitch )
230             {
231                 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_in_pitch
232                                            * p_outpic->p[i_plane].i_lines );
233             }
234             else
235             {
236                 p_in_end = p_in + i_in_pitch * p_outpic->p[i_plane].i_lines;
237
238                 while( p_in < p_in_end )
239                 {
240                     p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
241                     p_in += i_in_pitch;
242                     p_out += i_out_pitch;
243                 }
244             }
245         }
246
247         vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
248         vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ], p_outpic );
249     }
250 }
251
252 /*****************************************************************************
253  * RemoveAllVout: destroy all the child video output threads
254  *****************************************************************************/
255 static void RemoveAllVout( vout_thread_t *p_vout )
256 {
257     while( p_vout->p_sys->i_clones )
258     {
259          --p_vout->p_sys->i_clones;
260          vout_Destroy( p_vout->p_sys->pp_vout[p_vout->p_sys->i_clones] );
261     }
262 }
263