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