]> git.sesse.net Git - vlc/blob - modules/video_filter/wall.c
Converted wall to "video splitter".
[vlc] / modules / video_filter / wall.c
1 /*****************************************************************************
2  * wall.c : Wall video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2009 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include "vlc_video_splitter.h"
36
37 /* FIXME it is needed for VOUT_ALIGN_* only */
38 #include <vlc_vout.h>
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 #define COLS_TEXT N_("Number of columns")
44 #define COLS_LONGTEXT N_("Number of horizontal windows in " \
45     "which to split the video.")
46
47 #define ROWS_TEXT N_("Number of rows")
48 #define ROWS_LONGTEXT N_("Number of vertical windows in " \
49     "which to split the video.")
50
51 #define ACTIVE_TEXT N_("Active windows")
52 #define ACTIVE_LONGTEXT N_("Comma-separated list of active windows, " \
53     "defaults to all")
54
55 #define ASPECT_TEXT N_("Element aspect ratio")
56 #define ASPECT_LONGTEXT N_("Aspect ratio of the individual displays " \
57    "building the wall.")
58
59 #define CFG_PREFIX "wall-"
60
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 vlc_module_begin()
65     set_description( N_("Wall video filter") )
66     set_shortname( N_("Image wall" ))
67     set_capability( "video splitter", 0 )
68     set_category( CAT_VIDEO )
69     set_subcategory( SUBCAT_VIDEO_VFILTER )
70
71     add_integer( CFG_PREFIX "cols", 3, NULL, COLS_TEXT, COLS_LONGTEXT, false )
72     add_integer( CFG_PREFIX "rows", 3, NULL, ROWS_TEXT, ROWS_LONGTEXT, false )
73     add_string( CFG_PREFIX "active", NULL, NULL, ACTIVE_TEXT, ACTIVE_LONGTEXT,
74                  true )
75     add_string( CFG_PREFIX "element-aspect", "4:3", NULL, ASPECT_TEXT, ASPECT_LONGTEXT, false )
76
77     add_shortcut( "wall" )
78     set_callbacks( Open, Close )
79 vlc_module_end()
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static const char *const ppsz_filter_options[] = {
85     "cols", "rows", "active", "element-aspect", NULL
86 };
87
88 /* */
89 typedef struct
90 {
91     bool b_active;
92     int  i_output;
93     int  i_width;
94     int  i_height;
95     int  i_align;
96     int  i_left;
97     int  i_top;
98 } wall_output_t;
99
100 #define ROW_MAX (15)
101 #define COL_MAX (15)
102 struct video_splitter_sys_t
103 {
104     int           i_col;
105     int           i_row;
106     int           i_output;
107     wall_output_t pp_output[ROW_MAX][COL_MAX]; /* [x][y] */
108 };
109
110 static int Filter( video_splitter_t *, picture_t *pp_dst[], picture_t * );
111 static int Mouse( video_splitter_t *, vlc_mouse_t *,
112                   int i_index,
113                   const vlc_mouse_t *p_old, const vlc_mouse_t *p_new );
114
115 /**
116  * This function allocates and initializes a Wall splitter module.
117  */
118 static int Open( vlc_object_t *p_this )
119 {
120     video_splitter_t *p_splitter = (video_splitter_t*)p_this;
121     video_splitter_sys_t *p_sys;
122
123     p_splitter->p_sys = p_sys = malloc( sizeof(*p_sys) );
124     if( !p_sys )
125         return VLC_ENOMEM;
126
127     config_ChainParse( p_splitter, CFG_PREFIX, ppsz_filter_options,
128                        p_splitter->p_cfg );
129
130     /* */
131     p_sys->i_col = var_CreateGetInteger( p_splitter, CFG_PREFIX "cols" );
132     p_sys->i_col = __MAX( 1, __MIN( COL_MAX, p_sys->i_col ) );
133
134     p_sys->i_row = var_CreateGetInteger( p_splitter, CFG_PREFIX "rows" );
135     p_sys->i_row = __MAX( 1, __MIN( ROW_MAX, p_sys->i_row ) );
136
137     msg_Dbg( p_splitter, "opening a %i x %i wall",
138              p_sys->i_col, p_sys->i_row );
139
140     /* */
141     char *psz_state = var_CreateGetNonEmptyString( p_splitter, CFG_PREFIX "active" );
142
143     /* */
144     bool pb_active[COL_MAX*ROW_MAX];
145     for( int i = 0; i < COL_MAX*ROW_MAX; i++ )
146         pb_active[i] = psz_state == NULL;
147
148     /* Parse active list if provided */
149     char *psz_tmp = psz_state;
150     while( psz_tmp && *psz_tmp )
151     {
152         char *psz_next = strchr( psz_tmp, ',' );
153         if( psz_next )
154             *psz_next++ = '\0';
155
156         const int i_index = atoi( psz_tmp );
157         if( i_index >= 0 && i_index < COL_MAX*ROW_MAX )
158             pb_active[i_index] = true;
159     }
160     free( psz_state );
161
162     /* Parse aspect ratio if provided */
163     int i_aspect = 0;
164     char *psz_aspect = var_CreateGetNonEmptyString( p_splitter,
165                                                     CFG_PREFIX "element-aspect" );
166     if( psz_aspect )
167     {
168         int i_ar_num, i_ar_den;
169         if( sscanf( psz_aspect, "%d:%d", &i_ar_num, &i_ar_den ) == 2 &&
170             i_ar_num > 0 && i_ar_den > 0 )
171         {
172             i_aspect = i_ar_num * VOUT_ASPECT_FACTOR / i_ar_den;
173         }
174         else
175         {
176             msg_Warn( p_splitter, "invalid aspect ratio specification" );
177         }
178         free( psz_aspect );
179     }
180     if( i_aspect <= 0 )
181         i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
182
183     /* Compute placements/size of the windows */
184     const unsigned w1 = ( p_splitter->fmt.i_width / p_sys->i_col ) & ~1;
185     const unsigned h1 = ( w1 * VOUT_ASPECT_FACTOR / i_aspect ) & ~1;
186
187     const unsigned h2 = ( p_splitter->fmt.i_height / p_sys->i_row ) & ~1;
188     const unsigned w2 = ( h2 * i_aspect / VOUT_ASPECT_FACTOR ) & ~1;
189
190     unsigned i_target_width;
191     unsigned i_target_height;
192     unsigned i_hstart, i_hend;
193     unsigned i_vstart, i_vend;
194     bool b_vstart_rounded;
195     bool b_hstart_rounded;
196
197     if( h1 * p_sys->i_row < p_splitter->fmt.i_height )
198     {
199         i_target_width = w2;
200         i_target_height = h2;
201
202         i_vstart = 0;
203         b_vstart_rounded = false;
204         i_vend = p_splitter->fmt.i_height;
205
206         unsigned i_tmp = i_target_width * p_sys->i_col;
207         while( i_tmp < p_splitter->fmt.i_width )
208             i_tmp += p_sys->i_col;
209
210         i_hstart = (( i_tmp - p_splitter->fmt.i_width ) / 2)&~1;
211         b_hstart_rounded  = ( ( i_tmp - p_splitter->fmt.i_width ) % 2 ) ||
212             ( ( ( i_tmp - p_splitter->fmt.i_width ) / 2 ) & 1 );
213         i_hend = i_hstart + p_splitter->fmt.i_width;
214     }
215     else
216     {
217         i_target_height = h1;
218         i_target_width = w1;
219
220         i_hstart = 0;
221         b_hstart_rounded = false;
222         i_hend = p_splitter->fmt.i_width;
223
224         unsigned i_tmp = i_target_height * p_sys->i_row;
225         while( i_tmp < p_splitter->fmt.i_height )
226             i_tmp += p_sys->i_row;
227
228         i_vstart = ( ( i_tmp - p_splitter->fmt.i_height ) / 2 ) & ~1;
229         b_vstart_rounded  = ( ( i_tmp - p_splitter->fmt.i_height ) % 2 ) ||
230             ( ( ( i_tmp - p_splitter->fmt.i_height ) / 2 ) & 1 );
231         i_vend = i_vstart + p_splitter->fmt.i_height;
232     }
233     msg_Dbg( p_splitter, "target resolution %dx%d", i_target_width, i_target_height );
234     msg_Dbg( p_splitter, "target window (%d,%d)-(%d,%d)", i_hstart,i_vstart,i_hend,i_vend );
235
236     int i_active = 0;
237     for( int y = 0, i_top = 0; y < p_sys->i_row; y++ )
238     {
239         /* */
240         int i_height = 0;
241         int i_halign = 0;
242         if( y * i_target_height >= i_vstart &&
243             ( y + 1 ) * i_target_height <= i_vend )
244         {
245             i_height = i_target_height;
246         }
247         else if( ( y + 1 ) * i_target_height < i_vstart ||
248                  ( y * i_target_height ) > i_vend )
249         {
250             i_height = 0;
251         }
252         else
253         {
254             i_height = ( i_target_height -
255                          i_vstart%i_target_height );
256             if(  y >= ( p_sys->i_row / 2 ) )
257             {
258                 i_halign = VOUT_ALIGN_TOP;
259                 i_height -= b_vstart_rounded ? 2: 0;
260             }
261             else
262             {
263                 i_halign = VOUT_ALIGN_BOTTOM;
264             }
265         }
266
267         /* */
268         for( int x = 0, i_left = 0; x < p_sys->i_col; x++ )
269         {
270             wall_output_t *p_output = &p_sys->pp_output[x][y];
271
272             /* */
273             int i_width;
274             int i_valign = 0;
275             if( x*i_target_width >= i_hstart &&
276                 (x+1)*i_target_width <= i_hend )
277             {
278                 i_width = i_target_width;
279             }
280             else if( ( x + 1 ) * i_target_width < i_hstart ||
281                      ( x * i_target_width ) > i_hend )
282             {
283                 i_width = 0;
284             }
285             else
286             {
287                 i_width = ( i_target_width - i_hstart % i_target_width );
288                 if( x >= ( p_sys->i_col / 2 ) )
289                 {
290                     i_valign = VOUT_ALIGN_LEFT;
291                     i_width -= b_hstart_rounded ? 2: 0;
292                 }
293                 else
294                 {
295                     i_valign = VOUT_ALIGN_RIGHT;
296                 }
297             }
298
299             /* */
300             p_output->b_active = pb_active[y * p_sys->i_col + x] &&
301                                  i_height > 0 && i_width > 0;
302             p_output->i_output = -1;
303             p_output->i_align = i_valign | i_halign;
304             p_output->i_width = i_width;
305             p_output->i_height = i_height;
306             p_output->i_left = i_left;
307             p_output->i_top = i_top;
308
309             msg_Dbg( p_splitter, "window %dx%d at %d:%d size %dx%d", 
310                      x, y, i_left, i_top, i_width, i_height );
311
312             if( p_output->b_active )
313                 i_active++;
314
315             i_left += i_width;
316         }
317         i_top += i_height;
318     }
319     if( i_active <= 0 )
320     {
321         msg_Err( p_splitter, "No active video output" );
322         free( p_sys );
323         return VLC_EGENERIC;
324     }
325
326     /* Setup output configuration */
327     p_splitter->i_output = i_active;
328     p_splitter->p_output = calloc( p_splitter->i_output,
329                                    sizeof(*p_splitter->p_output) );
330     if( !p_splitter->p_output )
331     {
332         free( p_sys );
333         return VLC_ENOMEM;
334     }
335     for( int y = 0, i_output = 0; y < p_sys->i_row; y++ )
336     {
337         for( int x = 0; x < p_sys->i_col; x++ )
338         {
339             wall_output_t *p_output = &p_sys->pp_output[x][y];
340             if( !p_output->b_active )
341                 continue;
342
343             p_output->i_output = i_output++;
344
345             video_splitter_output_t *p_cfg = &p_splitter->p_output[p_output->i_output];
346
347             video_format_Copy( &p_cfg->fmt, &p_splitter->fmt );
348             p_cfg->fmt.i_visible_width  =
349             p_cfg->fmt.i_width          = p_output->i_width;
350             p_cfg->fmt.i_visible_height =
351             p_cfg->fmt.i_height         = p_output->i_height;
352             p_cfg->fmt.i_aspect         = (int64_t)i_aspect * i_target_height * p_output->i_width / i_target_width / p_output->i_height;
353
354             p_cfg->window.i_x     = p_output->i_left; /* FIXME relative to video-x/y (TODO in wrapper.c) ? */
355             p_cfg->window.i_y     = p_output->i_top;
356             p_cfg->window.i_align = p_output->i_align;
357             p_cfg->psz_module = NULL;
358         }
359     }
360
361     /* */
362     p_splitter->pf_filter = Filter;
363     p_splitter->pf_mouse = Mouse;
364
365     return VLC_SUCCESS;
366 }
367
368 /**
369  * Terminate a splitter module.
370  */
371 static void Close( vlc_object_t *p_this )
372 {
373     video_splitter_t *p_splitter = (video_splitter_t*)p_this;
374     video_splitter_sys_t *p_sys = p_splitter->p_sys;
375
376     free( p_splitter->p_output );
377     free( p_sys );
378 }
379
380 static int Filter( video_splitter_t *p_splitter, picture_t *pp_dst[], picture_t *p_src )
381 {
382     video_splitter_sys_t *p_sys = p_splitter->p_sys;
383
384     if( video_splitter_NewPicture( p_splitter, pp_dst ) )
385         return VLC_EGENERIC;
386
387     for( int y = 0; y < p_sys->i_row; y++ )
388     {
389         for( int x = 0; x < p_sys->i_col; x++ )
390         {
391             wall_output_t *p_output = &p_sys->pp_output[x][y];
392             if( !p_output->b_active )
393                 continue;
394
395             video_splitter_output_t *p_cfg = &p_splitter->p_output[p_output->i_output];
396             picture_t *p_dst = pp_dst[p_output->i_output];
397
398             /* */
399             picture_t tmp = *p_src;
400             for( int i = 0; i < tmp.i_planes; i++ )
401             {
402                 plane_t *p0 = &tmp.p[0];
403                 plane_t *p = &tmp.p[i];
404                 const int i_y = p_output->i_top  * p->i_visible_pitch / p0->i_visible_pitch;
405                 const int i_x = p_output->i_left * p->i_visible_lines / p0->i_visible_lines;
406
407                 p->p_pixels += i_y * p->i_pitch + ( i_x - (i_x % p->i_pixel_pitch));
408             }
409             picture_Copy( p_dst, &tmp );
410         }
411     }
412
413     picture_Release( p_src );
414     return VLC_SUCCESS;
415 }
416 static int Mouse( video_splitter_t *p_splitter, vlc_mouse_t *p_mouse,
417                   int i_index,
418                   const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
419 {
420     video_splitter_sys_t *p_sys = p_splitter->p_sys;
421
422     for( int y = 0; y < p_sys->i_row; y++ )
423     {
424         for( int x = 0; x < p_sys->i_col; x++ )
425         {
426             wall_output_t *p_output = p_output = &p_sys->pp_output[x][y];
427             if( p_output->b_active && p_output->i_output == i_index )
428             {
429                 *p_mouse = *p_new;
430                 p_mouse->i_x += p_output->i_left;
431                 p_mouse->i_y += p_output->i_top;
432                 return VLC_SUCCESS;
433             }
434         }
435     }
436     assert(0);
437     return VLC_EGENERIC;
438 }
439