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