]> git.sesse.net Git - vlc/blob - plugins/filter/wall.c
07f021cf95700fecd93e952e872667a3938c7ce1
[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.20 2002/05/30 08:17:04 gbazin 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 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
48 ADD_INTEGER ( "wall-cols", 3, NULL, N_("Number of columns"),
49               N_("Select the number of horizontal videowindows in which "
50                  "to split the video") )
51 ADD_INTEGER ( "wall-rows", 3, NULL, N_("Number of rows"),
52               N_("Select the number of vertical videowindows in which "
53                  "to split the video") )
54 ADD_STRING ( "wall-active", NULL, NULL, N_("Active windows"),
55              N_("comma separated list of active windows, defaults to all") )
56 MODULE_CONFIG_STOP
57
58 MODULE_INIT_START
59     SET_DESCRIPTION( _("image wall video module") )
60     /* Capability score set to 0 because we don't want to be spawned
61      * as a video output unless explicitly requested to */
62     ADD_CAPABILITY( VOUT, 0 )
63     ADD_SHORTCUT( "wall" )
64 MODULE_INIT_STOP
65
66 MODULE_ACTIVATE_START
67     vout_getfunctions( &p_module->p_functions->vout );
68 MODULE_ACTIVATE_STOP
69
70 MODULE_DEACTIVATE_START
71 MODULE_DEACTIVATE_STOP
72
73 /*****************************************************************************
74  * vout_sys_t: Wall video output method descriptor
75  *****************************************************************************
76  * This structure is part of the video output thread descriptor.
77  * It describes the Wall specific properties of an output thread.
78  *****************************************************************************/
79 typedef struct vout_sys_s
80 {
81     int    i_col;
82     int    i_row;
83     int    i_vout;
84     struct vout_list_s
85     {
86         boolean_t b_active;
87         int i_width;
88         int i_height;
89         struct vout_thread_s *p_vout;
90     } *pp_vout;
91
92 } vout_sys_t;
93
94 /*****************************************************************************
95  * Local prototypes
96  *****************************************************************************/
97 static int  vout_Create    ( vout_thread_t * );
98 static int  vout_Init      ( vout_thread_t * );
99 static void vout_End       ( vout_thread_t * );
100 static void vout_Destroy   ( vout_thread_t * );
101 static int  vout_Manage    ( vout_thread_t * );
102 static void vout_Render    ( vout_thread_t *, struct picture_s * );
103 static void vout_Display   ( vout_thread_t *, struct picture_s * );
104
105 static void RemoveAllVout  ( vout_thread_t *p_vout );
106
107 /*****************************************************************************
108  * Functions exported as capabilities. They are declared as static so that
109  * we don't pollute the namespace too much.
110  *****************************************************************************/
111 static void vout_getfunctions( function_list_t * p_function_list )
112 {
113     p_function_list->functions.vout.pf_create     = vout_Create;
114     p_function_list->functions.vout.pf_init       = vout_Init;
115     p_function_list->functions.vout.pf_end        = vout_End;
116     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
117     p_function_list->functions.vout.pf_manage     = vout_Manage;
118     p_function_list->functions.vout.pf_render     = vout_Render;
119     p_function_list->functions.vout.pf_display    = vout_Display;
120 }
121
122 /*****************************************************************************
123  * vout_Create: allocates Wall video thread output method
124  *****************************************************************************
125  * This function allocates and initializes a Wall vout method.
126  *****************************************************************************/
127 static int vout_Create( vout_thread_t *p_vout )
128 {
129     char *psz_method, *psz_tmp, *psz_method_tmp;
130     int i_vout;
131
132     /* Allocate structure */
133     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
134     if( p_vout->p_sys == NULL )
135     {
136         intf_ErrMsg("error: %s", strerror(ENOMEM) );
137         return( 1 );
138     }
139
140     /* Look what method was requested */
141     p_vout->p_sys->i_col = config_GetIntVariable( "wall-cols" );
142     p_vout->p_sys->i_row = config_GetIntVariable( "wall-rows" );
143
144     p_vout->p_sys->i_col = __MAX( 1, __MIN( 15, p_vout->p_sys->i_col ) );
145     p_vout->p_sys->i_row = __MAX( 1, __MIN( 15, p_vout->p_sys->i_row ) );
146
147     intf_WarnMsg( 3, "filter info: opening a %i x %i wall",
148                   p_vout->p_sys->i_col, p_vout->p_sys->i_row );
149
150     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_row *
151                                      p_vout->p_sys->i_col *
152                                      sizeof(struct vout_list_s) );
153     if( p_vout->p_sys->pp_vout == NULL )
154     {
155         intf_ErrMsg("error: %s", strerror(ENOMEM) );
156         free( p_vout->p_sys );
157         return( 1 );
158     }
159
160     psz_method_tmp = psz_method = config_GetPszVariable( "wall-active" );
161
162     /* If no trailing vout are specified, take them all */
163     if( psz_method == NULL )
164     {
165         for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
166              i_vout--; )
167         {
168             p_vout->p_sys->pp_vout[i_vout].b_active = 1;
169         }
170     }
171     /* If trailing vout are specified, activate only the requested ones */
172     else
173     {
174         for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
175              i_vout--; )
176         {
177             p_vout->p_sys->pp_vout[i_vout].b_active = 0;
178         }
179
180         while( *psz_method )
181         {
182             psz_tmp = psz_method;
183             while( *psz_tmp && *psz_tmp != ',' )
184             {
185                 psz_tmp++;
186             }
187
188             if( *psz_tmp )
189             {
190                 *psz_tmp = '\0';
191                 i_vout = atoi( psz_method );
192                 psz_method = psz_tmp + 1;
193             }
194             else
195             {
196                 i_vout = atoi( psz_method );
197                 psz_method = psz_tmp;
198             }
199
200             if( i_vout >= 0 &&
201                 i_vout < p_vout->p_sys->i_row * p_vout->p_sys->i_col )
202             {
203                 p_vout->p_sys->pp_vout[i_vout].b_active = 1;
204             }
205         }
206     }
207
208     free( psz_method_tmp );
209
210     return( 0 );
211 }
212
213 /*****************************************************************************
214  * vout_Init: initialize Wall video thread output method
215  *****************************************************************************/
216 static int vout_Init( vout_thread_t *p_vout )
217 {
218     int i_index, i_row, i_col, i_width, i_height;
219     char *psz_filter;
220     picture_t *p_pic;
221     
222     I_OUTPUTPICTURES = 0;
223
224     /* Initialize the output structure */
225     p_vout->output.i_chroma = p_vout->render.i_chroma;
226     p_vout->output.i_width  = p_vout->render.i_width;
227     p_vout->output.i_height = p_vout->render.i_height;
228     p_vout->output.i_aspect = p_vout->render.i_aspect;
229
230     /* Try to open the real video output */
231     psz_filter = config_GetPszVariable( "filter" );
232     config_PutPszVariable( "filter", NULL );
233
234     intf_WarnMsg( 1, "filter: spawning the real video outputs" );
235
236     p_vout->p_sys->i_vout = 0;
237
238     /* FIXME: use bresenham instead of those ugly divisions */
239     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
240     {
241         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
242         {
243             if( i_col + 1 < p_vout->p_sys->i_col )
244             {
245                 i_width = ( p_vout->render.i_width
246                              / p_vout->p_sys->i_col ) & ~0x1;
247             }
248             else
249             {
250                 i_width = p_vout->render.i_width
251                            - ( ( p_vout->render.i_width
252                                   / p_vout->p_sys->i_col ) & ~0x1 ) * i_col;
253             }
254
255             if( i_row + 1 < p_vout->p_sys->i_row )
256             {
257                 i_height = ( p_vout->render.i_height
258                               / p_vout->p_sys->i_row ) & ~0x3;
259             }
260             else
261             {
262                 i_height = p_vout->render.i_height
263                             - ( ( p_vout->render.i_height
264                                    / p_vout->p_sys->i_row ) & ~0x3 ) * i_row;
265             }
266
267             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_width = i_width;
268             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_height = i_height;
269
270             if( !p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
271             {
272                 p_vout->p_sys->i_vout++;
273                 continue;
274             }
275
276             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout =
277                 vout_CreateThread( NULL, i_width, i_height,
278                                    p_vout->render.i_chroma,
279                                    p_vout->render.i_aspect
280                                     * p_vout->render.i_height / i_height
281                                     * i_width / p_vout->render.i_width );
282             if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout == NULL )
283             {
284                 intf_ErrMsg( "vout error: failed to get %ix%i vout threads",
285                              p_vout->p_sys->i_col, p_vout->p_sys->i_row );
286                 RemoveAllVout( p_vout );
287                 config_PutPszVariable( "filter", psz_filter );
288                 if( psz_filter ) free( psz_filter );
289                 return 0;
290             }
291
292             p_vout->p_sys->i_vout++;
293         }
294     }
295
296     config_PutPszVariable( "filter", psz_filter );
297     if( psz_filter ) free( psz_filter );
298
299     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
300
301     return( 0 );
302 }
303
304 /*****************************************************************************
305  * vout_End: terminate Wall video thread output method
306  *****************************************************************************/
307 static void vout_End( vout_thread_t *p_vout )
308 {
309     int i_index;
310
311     /* Free the fake output buffers we allocated */
312     for( i_index = I_OUTPUTPICTURES ; i_index ; )
313     {
314         i_index--;
315         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
316     }
317 }
318
319 /*****************************************************************************
320  * vout_Destroy: destroy Wall video thread output method
321  *****************************************************************************
322  * Terminate an output method created by WallCreateOutputMethod
323  *****************************************************************************/
324 static void vout_Destroy( vout_thread_t *p_vout )
325 {
326     RemoveAllVout( p_vout );
327
328     free( p_vout->p_sys->pp_vout );
329     free( p_vout->p_sys );
330 }
331
332 /*****************************************************************************
333  * vout_Manage: handle Wall events
334  *****************************************************************************
335  * This function should be called regularly by video output thread. It manages
336  * console events. It returns a non null value on error.
337  *****************************************************************************/
338 static int vout_Manage( vout_thread_t *p_vout )
339 {
340     return( 0 );
341 }
342
343 /*****************************************************************************
344  * vout_Render: displays previously rendered output
345  *****************************************************************************
346  * This function send the currently rendered image to Wall image, waits
347  * until it is displayed and switch the two rendering buffers, preparing next
348  * frame.
349  *****************************************************************************/
350 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
351 {
352     picture_t *p_outpic = NULL;
353     int i_col, i_row, i_vout, i_plane;
354     int pi_left_skip[VOUT_MAX_PLANES], pi_top_skip[VOUT_MAX_PLANES];
355
356     i_vout = 0;
357
358     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
359     {
360         pi_top_skip[i_plane] = 0;
361     }
362
363     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
364     {
365         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
366         {
367             pi_left_skip[i_plane] = 0;
368         }
369
370         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
371         {
372             if( !p_vout->p_sys->pp_vout[ i_vout ].b_active )
373             {
374                 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
375                 {
376                     pi_left_skip[i_plane] +=
377                         p_vout->p_sys->pp_vout[ i_vout ].i_width
378                          * p_pic->p[i_plane].i_pitch / p_vout->output.i_width;
379                 }
380                 i_vout++;
381                 continue;
382             }
383
384             while( ( p_outpic =
385                 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
386                                     0, 0, 0 )
387                    ) == NULL )
388             {
389                 if( p_vout->b_die || p_vout->b_error )
390                 {
391                     vout_DestroyPicture(
392                         p_vout->p_sys->pp_vout[ i_vout ].p_vout, p_outpic );
393                     return;
394                 }
395
396                 msleep( VOUT_OUTMEM_SLEEP );
397             }
398
399             vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
400                               p_outpic, p_pic->date );
401             vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
402                               p_outpic );
403
404             for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
405             {
406                 u8 *p_in, *p_in_end, *p_out;
407                 int i_in_pitch = p_pic->p[i_plane].i_pitch;
408                 int i_out_pitch = p_outpic->p[i_plane].i_pitch;
409
410                 p_in = p_pic->p[i_plane].p_pixels
411                         + pi_top_skip[i_plane] + pi_left_skip[i_plane];
412
413                 p_in_end = p_in + p_outpic->p[i_plane].i_lines
414                                    * p_pic->p[i_plane].i_pitch;
415
416                 p_out = p_outpic->p[i_plane].p_pixels;
417
418                 while( p_in < p_in_end )
419                 {
420                     FAST_MEMCPY( p_out, p_in, i_out_pitch );
421                     p_in += i_in_pitch;
422                     p_out += i_out_pitch;
423                 }
424
425                 pi_left_skip[i_plane] += i_out_pitch;
426             }
427
428             vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
429                                 p_outpic );
430             vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
431                                  p_outpic );
432
433             i_vout++;
434         }
435
436         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
437         {
438             pi_top_skip[i_plane] += p_vout->p_sys->pp_vout[ i_vout ].i_height
439                                      * p_pic->p[i_plane].i_lines
440                                      / p_vout->output.i_height
441                                      * p_pic->p[i_plane].i_pitch;
442         }
443     }
444 }
445
446 /*****************************************************************************
447  * vout_Display: displays previously rendered output
448  *****************************************************************************
449  * This function send the currently rendered image to Invert image, waits
450  * until it is displayed and switch the two rendering buffers, preparing next
451  * frame.
452  *****************************************************************************/
453 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
454 {
455     ;
456 }
457
458 /*****************************************************************************
459  * RemoveAllVout: destroy all the child video output threads
460  *****************************************************************************/
461 static void RemoveAllVout( vout_thread_t *p_vout )
462 {
463     while( p_vout->p_sys->i_vout )
464     {
465          --p_vout->p_sys->i_vout;
466          if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
467          {
468              vout_DestroyThread(
469                p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout, NULL );
470          }
471     }
472 }
473