Improvements to preferences
[vlc.git] / 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     set_category( CAT_VIDEO );
69     set_subcategory( SUBCAT_VIDEO_VFILTER );
70
71     add_integer( "wall-cols", 3, NULL, COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
72     add_integer( "wall-rows", 3, NULL, ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
73     add_string( "wall-active", NULL, NULL, ACTIVE_TEXT, ACTIVE_LONGTEXT, VLC_FALSE );
74
75     add_shortcut( "wall" );
76     set_callbacks( Create, Destroy );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * vout_sys_t: Wall video output method descriptor
81  *****************************************************************************
82  * This structure is part of the video output thread descriptor.
83  * It describes the Wall specific properties of an output thread.
84  *****************************************************************************/
85 struct vout_sys_t
86 {
87     int    i_col;
88     int    i_row;
89     int    i_vout;
90     struct vout_list_t
91     {
92         vlc_bool_t b_active;
93         int i_width;
94         int i_height;
95         vout_thread_t *p_vout;
96     } *pp_vout;
97 };
98
99 /*****************************************************************************
100  * Control: control facility for the vout (forwards to child vout)
101  *****************************************************************************/
102 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
103 {
104     int i_row, i_col, i_vout = 0;
105
106     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
107     {
108         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
109         {
110             vout_vaControl( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
111                             i_query, args );
112             i_vout++;
113         }
114     }
115     return VLC_SUCCESS;
116 }
117
118 /*****************************************************************************
119  * Create: allocates Wall video thread output method
120  *****************************************************************************
121  * This function allocates and initializes a Wall vout method.
122  *****************************************************************************/
123 static int Create( vlc_object_t *p_this )
124 {
125     vout_thread_t *p_vout = (vout_thread_t *)p_this;
126     char *psz_method, *psz_tmp, *psz_method_tmp;
127     int i_vout;
128
129     /* Allocate structure */
130     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
131     if( p_vout->p_sys == NULL )
132     {
133         msg_Err( p_vout, "out of memory" );
134         return VLC_ENOMEM;
135     }
136
137     p_vout->pf_init = Init;
138     p_vout->pf_end = End;
139     p_vout->pf_manage = NULL;
140     p_vout->pf_render = Render;
141     p_vout->pf_display = NULL;
142     p_vout->pf_control = Control;
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 VLC_ENOMEM;
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 VLC_SUCCESS;
215 }
216
217 /*****************************************************************************
218  * Init: initialize Wall video thread output method
219  *****************************************************************************/
220 static int 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_Create( 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 VLC_EGENERIC;
288             }
289             ADD_CALLBACKS(
290                 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout,
291                 SendEvents );
292
293             p_vout->p_sys->i_vout++;
294         }
295     }
296
297     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
298
299     ADD_PARENT_CALLBACKS( SendEventsToChild );
300
301     return VLC_SUCCESS;
302 }
303
304 /*****************************************************************************
305  * End: terminate Wall video thread output method
306  *****************************************************************************/
307 static void 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  * Destroy: destroy Wall video thread output method
321  *****************************************************************************
322  * Terminate an output method created by WallCreateOutputMethod
323  *****************************************************************************/
324 static void Destroy( vlc_object_t *p_this )
325 {
326     vout_thread_t *p_vout = (vout_thread_t *)p_this;
327
328     RemoveAllVout( p_vout );
329
330     DEL_PARENT_CALLBACKS( SendEventsToChild );
331
332     free( p_vout->p_sys->pp_vout );
333     free( p_vout->p_sys );
334 }
335
336 /*****************************************************************************
337  * Render: displays previously rendered output
338  *****************************************************************************
339  * This function send the currently rendered image to Wall image, waits
340  * until it is displayed and switch the two rendering buffers, preparing next
341  * frame.
342  *****************************************************************************/
343 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
344 {
345     picture_t *p_outpic = NULL;
346     int i_col, i_row, i_vout, i_plane;
347     int pi_left_skip[VOUT_MAX_PLANES], pi_top_skip[VOUT_MAX_PLANES];
348
349     i_vout = 0;
350
351     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
352     {
353         pi_top_skip[i_plane] = 0;
354     }
355
356     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
357     {
358         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
359         {
360             pi_left_skip[i_plane] = 0;
361         }
362
363         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
364         {
365             if( !p_vout->p_sys->pp_vout[ i_vout ].b_active )
366             {
367                 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
368                 {
369                     pi_left_skip[i_plane] +=
370                         p_vout->p_sys->pp_vout[ i_vout ].i_width
371                          * p_pic->p[i_plane].i_pitch / p_vout->output.i_width;
372                 }
373                 i_vout++;
374                 continue;
375             }
376
377             while( ( p_outpic =
378                 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
379                                     0, 0, 0 )
380                    ) == NULL )
381             {
382                 if( p_vout->b_die || p_vout->b_error )
383                 {
384                     vout_DestroyPicture(
385                         p_vout->p_sys->pp_vout[ i_vout ].p_vout, p_outpic );
386                     return;
387                 }
388
389                 msleep( VOUT_OUTMEM_SLEEP );
390             }
391
392             vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
393                               p_outpic, p_pic->date );
394             vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
395                               p_outpic );
396
397             for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
398             {
399                 uint8_t *p_in, *p_in_end, *p_out;
400                 int i_in_pitch = p_pic->p[i_plane].i_pitch;
401                 int i_out_pitch = p_outpic->p[i_plane].i_pitch;
402                 int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
403
404                 p_in = p_pic->p[i_plane].p_pixels
405                         + pi_top_skip[i_plane] + pi_left_skip[i_plane];
406
407                 p_in_end = p_in + p_outpic->p[i_plane].i_visible_lines
408                                    * p_pic->p[i_plane].i_pitch;
409
410                 p_out = p_outpic->p[i_plane].p_pixels;
411
412                 while( p_in < p_in_end )
413                 {
414                     p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
415                     p_in += i_in_pitch;
416                     p_out += i_out_pitch;
417                 }
418
419                 pi_left_skip[i_plane] += i_out_pitch;
420             }
421
422             vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
423                                 p_outpic );
424             vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
425                                  p_outpic );
426
427             i_vout++;
428         }
429
430         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
431         {
432             pi_top_skip[i_plane] += p_vout->p_sys->pp_vout[ i_vout ].i_height
433                                      * p_pic->p[i_plane].i_visible_lines
434                                      / p_vout->output.i_height
435                                      * p_pic->p[i_plane].i_pitch;
436         }
437     }
438 }
439
440 /*****************************************************************************
441  * RemoveAllVout: destroy all the child video output threads
442  *****************************************************************************/
443 static void RemoveAllVout( vout_thread_t *p_vout )
444 {
445     while( p_vout->p_sys->i_vout )
446     {
447          --p_vout->p_sys->i_vout;
448          if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
449          {
450              DEL_CALLBACKS(
451                  p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout,
452                  SendEvents );
453              vlc_object_detach(
454                  p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout );
455              vout_Destroy(
456                  p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout );
457          }
458     }
459 }
460
461 /*****************************************************************************
462  * SendEvents: forward mouse and keyboard events to the parent p_vout
463  *****************************************************************************/
464 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
465                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
466 {
467     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
468     int i_vout;
469     vlc_value_t sentval = newval;
470
471     /* Find the video output index */
472     for( i_vout = 0; i_vout < p_vout->p_sys->i_vout; i_vout++ )
473     {
474         if( p_this == (vlc_object_t *)p_vout->p_sys->pp_vout[ i_vout ].p_vout )
475         {
476             break;
477         }
478     }
479
480     if( i_vout == p_vout->p_sys->i_vout )
481     {
482         return VLC_EGENERIC;
483     }
484
485     /* Translate the mouse coordinates */
486     if( !strcmp( psz_var, "mouse-x" ) )
487     {
488         sentval.i_int += p_vout->output.i_width
489                           * (i_vout % p_vout->p_sys->i_col)
490                           / p_vout->p_sys->i_col;
491     }
492     else if( !strcmp( psz_var, "mouse-y" ) )
493     {
494         sentval.i_int += p_vout->output.i_height
495                           * (i_vout / p_vout->p_sys->i_row)
496                           / p_vout->p_sys->i_row;
497     }
498
499     var_Set( p_vout, psz_var, sentval );
500
501     return VLC_SUCCESS;
502 }
503
504 /*****************************************************************************
505  * SendEventsToChild: forward events to the child/children vout
506  *****************************************************************************/
507 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
508                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
509 {
510     vout_thread_t *p_vout = (vout_thread_t *)p_this;
511     int i_row, i_col, i_vout = 0;
512
513     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
514     {
515         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
516         {
517             var_Set( p_vout->p_sys->pp_vout[ i_vout ].p_vout, psz_var, newval);
518             if( !strcmp( psz_var, "fullscreen" ) ) break;
519             i_vout++;
520         }
521     }
522
523     return VLC_SUCCESS;
524 }