]> git.sesse.net Git - vlc/blob - modules/video_filter/wall.c
28ec64ecbdd1cd7ef19020e751c29fea3b11acf1
[vlc] / modules / video_filter / wall.c
1 /*****************************************************************************
2  * wall.c : Wall video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id$
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 static int  SendEvents( vlc_object_t *, char const *,
48                         vlc_value_t, vlc_value_t, void * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define COLS_TEXT N_("Number of columns")
54 #define COLS_LONGTEXT N_("Select the number of horizontal video windows in " \
55     "which to split the video.")
56
57 #define ROWS_TEXT N_("Number of rows")
58 #define ROWS_LONGTEXT N_("Select the number of vertical video windows in " \
59     "which to split the video.")
60
61 #define ACTIVE_TEXT N_("Active windows")
62 #define ACTIVE_LONGTEXT N_("Comma separated list of active windows, " \
63     "defaults to all")
64
65 vlc_module_begin();
66     set_description( _("wall video filter") );
67     set_capability( "video filter", 0 );
68
69     add_integer( "wall-cols", 3, NULL, COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
70     add_integer( "wall-rows", 3, NULL, ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
71     add_string( "wall-active", NULL, NULL, ACTIVE_TEXT, ACTIVE_LONGTEXT, VLC_FALSE );
72
73     add_shortcut( "wall" );
74     set_callbacks( Create, Destroy );
75 vlc_module_end();
76
77 /*****************************************************************************
78  * vout_sys_t: Wall video output method descriptor
79  *****************************************************************************
80  * This structure is part of the video output thread descriptor.
81  * It describes the Wall specific properties of an output thread.
82  *****************************************************************************/
83 struct vout_sys_t
84 {
85     int    i_col;
86     int    i_row;
87     int    i_vout;
88     struct vout_list_t
89     {
90         vlc_bool_t b_active;
91         int i_width;
92         int i_height;
93         vout_thread_t *p_vout;
94     } *pp_vout;
95 };
96
97 /*****************************************************************************
98  * Control: control facility for the vout (forwards to child vout)
99  *****************************************************************************/
100 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
101 {
102     int i_row, i_col, i_vout = 0;
103
104     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
105     {
106         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
107         {
108             vout_vaControl( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
109                             i_query, args );
110             i_vout++;
111         }
112     }
113     return VLC_SUCCESS;
114 }
115
116 /*****************************************************************************
117  * Create: allocates Wall video thread output method
118  *****************************************************************************
119  * This function allocates and initializes a Wall vout method.
120  *****************************************************************************/
121 static int Create( vlc_object_t *p_this )
122 {
123     vout_thread_t *p_vout = (vout_thread_t *)p_this;
124     char *psz_method, *psz_tmp, *psz_method_tmp;
125     int i_vout;
126
127     /* Allocate structure */
128     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
129     if( p_vout->p_sys == NULL )
130     {
131         msg_Err( p_vout, "out of memory" );
132         return VLC_ENOMEM;
133     }
134
135     p_vout->pf_init = Init;
136     p_vout->pf_end = End;
137     p_vout->pf_manage = NULL;
138     p_vout->pf_render = Render;
139     p_vout->pf_display = NULL;
140     p_vout->pf_control = Control;
141
142     /* Look what method was requested */
143     p_vout->p_sys->i_col = config_GetInt( p_vout, "wall-cols" );
144     p_vout->p_sys->i_row = config_GetInt( p_vout, "wall-rows" );
145
146     p_vout->p_sys->i_col = __MAX( 1, __MIN( 15, p_vout->p_sys->i_col ) );
147     p_vout->p_sys->i_row = __MAX( 1, __MIN( 15, p_vout->p_sys->i_row ) );
148
149     msg_Dbg( p_vout, "opening a %i x %i wall",
150              p_vout->p_sys->i_col, p_vout->p_sys->i_row );
151
152     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_row *
153                                      p_vout->p_sys->i_col *
154                                      sizeof(struct vout_list_t) );
155     if( p_vout->p_sys->pp_vout == NULL )
156     {
157         msg_Err( p_vout, "out of memory" );
158         free( p_vout->p_sys );
159         return VLC_ENOMEM;
160     }
161
162     psz_method_tmp = psz_method = config_GetPsz( p_vout, "wall-active" );
163
164     /* If no trailing vout are specified, take them all */
165     if( psz_method == NULL )
166     {
167         for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
168              i_vout--; )
169         {
170             p_vout->p_sys->pp_vout[i_vout].b_active = 1;
171         }
172     }
173     /* If trailing vout are specified, activate only the requested ones */
174     else
175     {
176         for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
177              i_vout--; )
178         {
179             p_vout->p_sys->pp_vout[i_vout].b_active = 0;
180         }
181
182         while( *psz_method )
183         {
184             psz_tmp = psz_method;
185             while( *psz_tmp && *psz_tmp != ',' )
186             {
187                 psz_tmp++;
188             }
189
190             if( *psz_tmp )
191             {
192                 *psz_tmp = '\0';
193                 i_vout = atoi( psz_method );
194                 psz_method = psz_tmp + 1;
195             }
196             else
197             {
198                 i_vout = atoi( psz_method );
199                 psz_method = psz_tmp;
200             }
201
202             if( i_vout >= 0 &&
203                 i_vout < p_vout->p_sys->i_row * p_vout->p_sys->i_col )
204             {
205                 p_vout->p_sys->pp_vout[i_vout].b_active = 1;
206             }
207         }
208     }
209
210     free( psz_method_tmp );
211
212     return VLC_SUCCESS;
213 }
214
215 /*****************************************************************************
216  * Init: initialize Wall video thread output method
217  *****************************************************************************/
218 static int Init( vout_thread_t *p_vout )
219 {
220     int i_index, i_row, i_col, i_width, i_height;
221     picture_t *p_pic;
222
223     I_OUTPUTPICTURES = 0;
224
225     /* Initialize the output structure */
226     p_vout->output.i_chroma = p_vout->render.i_chroma;
227     p_vout->output.i_width  = p_vout->render.i_width;
228     p_vout->output.i_height = p_vout->render.i_height;
229     p_vout->output.i_aspect = p_vout->render.i_aspect;
230
231     /* Try to open the real video output */
232     msg_Dbg( p_vout, "spawning the real video outputs" );
233
234     p_vout->p_sys->i_vout = 0;
235
236     /* FIXME: use bresenham instead of those ugly divisions */
237     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
238     {
239         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
240         {
241             if( i_col + 1 < p_vout->p_sys->i_col )
242             {
243                 i_width = ( p_vout->render.i_width
244                              / p_vout->p_sys->i_col ) & ~0x1;
245             }
246             else
247             {
248                 i_width = p_vout->render.i_width
249                            - ( ( p_vout->render.i_width
250                                   / p_vout->p_sys->i_col ) & ~0x1 ) * i_col;
251             }
252
253             if( i_row + 1 < p_vout->p_sys->i_row )
254             {
255                 i_height = ( p_vout->render.i_height
256                               / p_vout->p_sys->i_row ) & ~0x3;
257             }
258             else
259             {
260                 i_height = p_vout->render.i_height
261                             - ( ( p_vout->render.i_height
262                                    / p_vout->p_sys->i_row ) & ~0x3 ) * i_row;
263             }
264
265             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_width = i_width;
266             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_height = i_height;
267
268             if( !p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
269             {
270                 p_vout->p_sys->i_vout++;
271                 continue;
272             }
273
274             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout =
275                 vout_Create( p_vout, i_width, i_height,
276                              p_vout->render.i_chroma,
277                              p_vout->render.i_aspect
278                               * p_vout->render.i_height / i_height
279                               * i_width / p_vout->render.i_width );
280             if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout == NULL )
281             {
282                 msg_Err( p_vout, "failed to get %ix%i vout threads",
283                                  p_vout->p_sys->i_col, p_vout->p_sys->i_row );
284                 RemoveAllVout( p_vout );
285                 return VLC_EGENERIC;
286             }
287             ADD_CALLBACKS(
288                 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout,
289                 SendEvents );
290
291             p_vout->p_sys->i_vout++;
292         }
293     }
294
295     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
296
297     ADD_PARENT_CALLBACKS( SendEventsToChild );
298
299     return VLC_SUCCESS;
300 }
301
302 /*****************************************************************************
303  * End: terminate Wall video thread output method
304  *****************************************************************************/
305 static void End( vout_thread_t *p_vout )
306 {
307     int i_index;
308
309     /* Free the fake output buffers we allocated */
310     for( i_index = I_OUTPUTPICTURES ; i_index ; )
311     {
312         i_index--;
313         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
314     }
315 }
316
317 /*****************************************************************************
318  * Destroy: destroy Wall video thread output method
319  *****************************************************************************
320  * Terminate an output method created by WallCreateOutputMethod
321  *****************************************************************************/
322 static void Destroy( vlc_object_t *p_this )
323 {
324     vout_thread_t *p_vout = (vout_thread_t *)p_this;
325
326     RemoveAllVout( p_vout );
327
328     DEL_PARENT_CALLBACKS( SendEventsToChild );
329
330     free( p_vout->p_sys->pp_vout );
331     free( p_vout->p_sys );
332 }
333
334 /*****************************************************************************
335  * Render: displays previously rendered output
336  *****************************************************************************
337  * This function send the currently rendered image to Wall image, waits
338  * until it is displayed and switch the two rendering buffers, preparing next
339  * frame.
340  *****************************************************************************/
341 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
342 {
343     picture_t *p_outpic = NULL;
344     int i_col, i_row, i_vout, i_plane;
345     int pi_left_skip[VOUT_MAX_PLANES], pi_top_skip[VOUT_MAX_PLANES];
346
347     i_vout = 0;
348
349     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
350     {
351         pi_top_skip[i_plane] = 0;
352     }
353
354     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
355     {
356         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
357         {
358             pi_left_skip[i_plane] = 0;
359         }
360
361         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
362         {
363             if( !p_vout->p_sys->pp_vout[ i_vout ].b_active )
364             {
365                 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
366                 {
367                     pi_left_skip[i_plane] +=
368                         p_vout->p_sys->pp_vout[ i_vout ].i_width
369                          * p_pic->p[i_plane].i_pitch / p_vout->output.i_width;
370                 }
371                 i_vout++;
372                 continue;
373             }
374
375             while( ( p_outpic =
376                 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
377                                     0, 0, 0 )
378                    ) == NULL )
379             {
380                 if( p_vout->b_die || p_vout->b_error )
381                 {
382                     vout_DestroyPicture(
383                         p_vout->p_sys->pp_vout[ i_vout ].p_vout, p_outpic );
384                     return;
385                 }
386
387                 msleep( VOUT_OUTMEM_SLEEP );
388             }
389
390             vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
391                               p_outpic, p_pic->date );
392             vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
393                               p_outpic );
394
395             for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
396             {
397                 uint8_t *p_in, *p_in_end, *p_out;
398                 int i_in_pitch = p_pic->p[i_plane].i_pitch;
399                 int i_out_pitch = p_outpic->p[i_plane].i_pitch;
400                 int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
401
402                 p_in = p_pic->p[i_plane].p_pixels
403                         + pi_top_skip[i_plane] + pi_left_skip[i_plane];
404
405                 p_in_end = p_in + p_outpic->p[i_plane].i_visible_lines
406                                    * p_pic->p[i_plane].i_pitch;
407
408                 p_out = p_outpic->p[i_plane].p_pixels;
409
410                 while( p_in < p_in_end )
411                 {
412                     p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
413                     p_in += i_in_pitch;
414                     p_out += i_out_pitch;
415                 }
416
417                 pi_left_skip[i_plane] += i_out_pitch;
418             }
419
420             vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
421                                 p_outpic );
422             vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
423                                  p_outpic );
424
425             i_vout++;
426         }
427
428         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
429         {
430             pi_top_skip[i_plane] += p_vout->p_sys->pp_vout[ i_vout ].i_height
431                                      * p_pic->p[i_plane].i_visible_lines
432                                      / p_vout->output.i_height
433                                      * p_pic->p[i_plane].i_pitch;
434         }
435     }
436 }
437
438 /*****************************************************************************
439  * RemoveAllVout: destroy all the child video output threads
440  *****************************************************************************/
441 static void RemoveAllVout( vout_thread_t *p_vout )
442 {
443     while( p_vout->p_sys->i_vout )
444     {
445          --p_vout->p_sys->i_vout;
446          if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
447          {
448              DEL_CALLBACKS(
449                  p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout,
450                  SendEvents );
451              vlc_object_detach(
452                  p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout );
453              vout_Destroy(
454                  p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout );
455          }
456     }
457 }
458
459 /*****************************************************************************
460  * SendEvents: forward mouse and keyboard events to the parent p_vout
461  *****************************************************************************/
462 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
463                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
464 {
465     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
466     int i_vout;
467     vlc_value_t sentval = newval;
468
469     /* Find the video output index */
470     for( i_vout = 0; i_vout < p_vout->p_sys->i_vout; i_vout++ )
471     {
472         if( p_this == (vlc_object_t *)p_vout->p_sys->pp_vout[ i_vout ].p_vout )
473         {
474             break;
475         }
476     }
477
478     if( i_vout == p_vout->p_sys->i_vout )
479     {
480         return VLC_EGENERIC;
481     }
482
483     /* Translate the mouse coordinates */
484     if( !strcmp( psz_var, "mouse-x" ) )
485     {
486         sentval.i_int += p_vout->output.i_width
487                           * (i_vout % p_vout->p_sys->i_col)
488                           / p_vout->p_sys->i_col;
489     }
490     else if( !strcmp( psz_var, "mouse-y" ) )
491     {
492         sentval.i_int += p_vout->output.i_height
493                           * (i_vout / p_vout->p_sys->i_row)
494                           / p_vout->p_sys->i_row;
495     }
496
497     var_Set( p_vout, psz_var, sentval );
498
499     return VLC_SUCCESS;
500 }
501
502 /*****************************************************************************
503  * SendEventsToChild: forward events to the child/children vout
504  *****************************************************************************/
505 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
506                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
507 {
508     vout_thread_t *p_vout = (vout_thread_t *)p_this;
509     int i_row, i_col, i_vout = 0;
510
511     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
512     {
513         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
514         {
515             var_Set( p_vout->p_sys->pp_vout[ i_vout ].p_vout, psz_var, newval);
516             if( !strcmp( psz_var, "fullscreen" ) ) break;
517             i_vout++;
518         }
519     }
520
521     return VLC_SUCCESS;
522 }