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