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