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