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