]> git.sesse.net Git - vlc/blob - plugins/filter/wall.c
1999db16c2ad50b1ac69f2759cdbf7d06f1ba23d
[vlc] / plugins / filter / wall.c
1 /*****************************************************************************
2  * wall.c : Wall video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: wall.c,v 1.5 2002/01/02 14:37:42 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 wall 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( "wall" )
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: Wall video output method descriptor
66  *****************************************************************************
67  * This structure is part of the video output thread descriptor.
68  * It describes the Wall specific properties of an output thread.
69  *****************************************************************************/
70 typedef struct vout_sys_s
71 {
72     int    i_col;
73     int    i_row;
74     int    i_vout;
75     struct vout_list_s
76     {
77         int i_width;
78         int i_height;
79         struct vout_thread_s *p_vout;
80     } *pp_vout;
81
82 } vout_sys_t;
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 static int  vout_Probe     ( probedata_t *p_data );
88 static int  vout_Create    ( vout_thread_t * );
89 static int  vout_Init      ( vout_thread_t * );
90 static void vout_End       ( vout_thread_t * );
91 static void vout_Destroy   ( vout_thread_t * );
92 static int  vout_Manage    ( vout_thread_t * );
93 static void vout_Display   ( vout_thread_t *, struct picture_s * );
94
95 static void RemoveAllVout  ( vout_thread_t *p_vout );
96
97 /*****************************************************************************
98  * Functions exported as capabilities. They are declared as static so that
99  * we don't pollute the namespace too much.
100  *****************************************************************************/
101 static void vout_getfunctions( function_list_t * p_function_list )
102 {
103     p_function_list->pf_probe = vout_Probe;
104     p_function_list->functions.vout.pf_create     = vout_Create;
105     p_function_list->functions.vout.pf_init       = vout_Init;
106     p_function_list->functions.vout.pf_end        = vout_End;
107     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
108     p_function_list->functions.vout.pf_manage     = vout_Manage;
109     p_function_list->functions.vout.pf_display    = vout_Display;
110     p_function_list->functions.vout.pf_setpalette = NULL;
111 }
112
113 /*****************************************************************************
114  * intf_Probe: return a score
115  *****************************************************************************/
116 static int vout_Probe( probedata_t *p_data )
117 {
118     return( 0 );
119 }
120
121 /*****************************************************************************
122  * vout_Create: allocates Wall video thread output method
123  *****************************************************************************
124  * This function allocates and initializes a Wall vout method.
125  *****************************************************************************/
126 static int vout_Create( vout_thread_t *p_vout )
127 {
128     /* Allocate structure */
129     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
130     if( p_vout->p_sys == NULL )
131     {
132         intf_ErrMsg("error: %s", strerror(ENOMEM) );
133         return( 1 );
134     }
135
136     p_vout->p_sys->i_col = 3;
137     p_vout->p_sys->i_row = 2;
138
139     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_row *
140                                      p_vout->p_sys->i_col *
141                                      sizeof(struct vout_list_s) );
142     if( p_vout->p_sys->pp_vout == NULL )
143     {
144         intf_ErrMsg("error: %s", strerror(ENOMEM) );
145         free( p_vout->p_sys );
146         return( 1 );
147     }
148
149
150     return( 0 );
151 }
152
153 /*****************************************************************************
154  * vout_Init: initialize Wall video thread output method
155  *****************************************************************************/
156 static int vout_Init( vout_thread_t *p_vout )
157 {
158     int i_index, i_row, i_col, i_width, i_height;
159     char *psz_filter;
160     picture_t *p_pic;
161     
162     I_OUTPUTPICTURES = 0;
163
164     /* Initialize the output structure */
165     switch( p_vout->render.i_chroma )
166     {
167         case FOURCC_I420:
168         case FOURCC_IYUV:
169         case FOURCC_YV12:
170         case FOURCC_I422:
171         case FOURCC_I444:
172             p_vout->output.i_chroma = p_vout->render.i_chroma;
173             p_vout->output.i_width  = p_vout->render.i_width;
174             p_vout->output.i_height = p_vout->render.i_height;
175             p_vout->output.i_aspect = p_vout->render.i_aspect;
176             break;
177
178         default:
179             return( 0 ); /* unknown chroma */
180             break;
181     }
182
183     /* Try to open the real video output */
184     psz_filter = main_GetPszVariable( VOUT_FILTER_VAR, NULL );
185     main_PutPszVariable( VOUT_FILTER_VAR, "" );
186
187     intf_WarnMsg( 1, "filter: spawning the real video outputs" );
188
189     p_vout->p_sys->i_vout = 0;
190
191     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
192     {
193         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
194         {
195             if( i_col + 1 < p_vout->p_sys->i_col )
196             {
197                 i_width = ( p_vout->render.i_width
198                              / p_vout->p_sys->i_col ) & ~0xf;
199             }
200             else
201             {
202                 i_width = p_vout->render.i_width
203                            - ( ( p_vout->render.i_width
204                                   / p_vout->p_sys->i_col ) & ~0xf ) * i_col;
205             }
206
207             if( i_row + 1 < p_vout->p_sys->i_row )
208             {
209                 i_height = p_vout->render.i_height / p_vout->p_sys->i_row;
210             }
211             else
212             {
213                 i_height = p_vout->render.i_height
214                             - p_vout->render.i_height
215                                / p_vout->p_sys->i_row * i_row;
216             }
217
218             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout =
219                 vout_CreateThread( NULL, i_width, i_height,
220                                    p_vout->render.i_chroma,
221                                    p_vout->render.i_aspect
222                                     * p_vout->render.i_height / i_height
223                                     * i_width / p_vout->render.i_width );
224             if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout == NULL )
225             {
226                 intf_ErrMsg( "vout error: failed to get %ix%i vout threads",
227                              p_vout->p_sys->i_col, p_vout->p_sys->i_row );
228                 RemoveAllVout( p_vout );
229                 return 0;
230             }
231
232             p_vout->p_sys->i_vout++;
233         }
234     }
235
236     main_PutPszVariable( VOUT_FILTER_VAR, psz_filter );
237
238     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
239
240     return( 0 );
241 }
242
243 /*****************************************************************************
244  * vout_End: terminate Wall video thread output method
245  *****************************************************************************/
246 static void vout_End( vout_thread_t *p_vout )
247 {
248     int i_index;
249
250     /* Free the fake output buffers we allocated */
251     for( i_index = I_OUTPUTPICTURES ; i_index ; )
252     {
253         i_index--;
254         free( PP_OUTPUTPICTURE[ i_index ]->planes[ 0 ].p_data );
255     }
256 }
257
258 /*****************************************************************************
259  * vout_Destroy: destroy Wall video thread output method
260  *****************************************************************************
261  * Terminate an output method created by WallCreateOutputMethod
262  *****************************************************************************/
263 static void vout_Destroy( vout_thread_t *p_vout )
264 {
265     RemoveAllVout( p_vout );
266
267     free( p_vout->p_sys->pp_vout );
268     free( p_vout->p_sys );
269 }
270
271 /*****************************************************************************
272  * vout_Manage: handle Wall events
273  *****************************************************************************
274  * This function should be called regularly by video output thread. It manages
275  * console events. It returns a non null value on error.
276  *****************************************************************************/
277 static int vout_Manage( vout_thread_t *p_vout )
278 {
279     return( 0 );
280 }
281
282 /*****************************************************************************
283  * vout_Display: displays previously rendered output
284  *****************************************************************************
285  * This function send the currently rendered image to Wall image, waits
286  * until it is displayed and switch the two rendering buffers, preparing next
287  * frame.
288  *****************************************************************************/
289 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
290 {
291     picture_t *p_outpic;
292     int i_col, i_row, i_vout, i_index;
293     mtime_t i_date = mdate() + 50000;
294
295     i_vout = 0;
296
297     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
298     {
299         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
300         {
301             while( ( p_outpic =
302                 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
303                                     0, 0, 0 )
304                    ) == NULL )
305             {
306                 if( p_vout->b_die || p_vout->b_error )
307                 {
308                     vout_DestroyPicture(
309                         p_vout->p_sys->pp_vout[ i_vout ].p_vout, p_outpic );
310                     return;
311                 }
312
313                 msleep( VOUT_OUTMEM_SLEEP );
314             }
315
316             vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
317                               p_outpic, i_date );
318             vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
319                               p_outpic );
320
321             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
322             {
323                 yuv_data_t *p_in, *p_in_end, *p_out;
324                 int i_out_bytes, i_offset;
325
326                 /* XXX: beware, it's p_outpic ! */
327                 i_out_bytes = p_outpic->planes[ i_index ].i_line_bytes;
328
329                 if( i_col + 1 < p_vout->p_sys->i_col )
330                 {
331                     i_offset = i_out_bytes * i_col;
332                 }
333                 else
334                 {
335                     i_offset = p_pic->planes[ i_index ].i_line_bytes
336                                 - i_out_bytes;
337                 }
338
339                 p_in = p_pic->planes[ i_index ].p_data
340                         + p_pic->planes[ i_index ].i_bytes
341                            / p_vout->p_sys->i_row * i_row
342                         + i_offset;
343
344                 if( i_row + 1 < p_vout->p_sys->i_row )
345                 {
346                     p_in_end = p_in
347                                 + p_pic->planes[ i_index ].i_bytes
348                                    / p_vout->p_sys->i_row;
349                 }
350                 else
351                 {
352                     p_in_end = p_pic->planes[ i_index ].p_data
353                                 + p_pic->planes[ i_index ].i_bytes
354                                 + i_offset;
355                 }
356
357                 p_out = p_outpic->planes[ i_index ].p_data;
358
359                 while( p_in < p_in_end )
360                 {
361                     FAST_MEMCPY( p_out, p_in, i_out_bytes );
362                     p_in += p_pic->planes[ i_index ].i_line_bytes;
363                     p_out += i_out_bytes;
364                 }
365             }
366
367             vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
368                                 p_outpic );
369             vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
370                                  p_outpic );
371
372             i_vout++;
373         }
374     }
375 }
376
377 static void RemoveAllVout( vout_thread_t *p_vout )
378 {
379     while( p_vout->p_sys->i_vout )
380     {
381          --p_vout->p_sys->i_vout;
382          vout_DestroyThread(
383              p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout, NULL );
384     }
385 }
386