1 /*****************************************************************************
2 * wall.c : Wall video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include "filter_common.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Destroy ( vlc_object_t * );
43 static int Init ( vout_thread_t * );
44 static void End ( vout_thread_t * );
45 static void Render ( vout_thread_t *, picture_t * );
47 static void RemoveAllVout ( vout_thread_t *p_vout );
49 static int SendEvents( vlc_object_t *, char const *,
50 vlc_value_t, vlc_value_t, void * );
52 /*****************************************************************************
54 *****************************************************************************/
55 #define COLS_TEXT N_("Number of columns")
56 #define COLS_LONGTEXT N_("Number of horizontal windows in " \
57 "which to split the video.")
59 #define ROWS_TEXT N_("Number of rows")
60 #define ROWS_LONGTEXT N_("Number of vertical windows in " \
61 "which to split the video.")
63 #define ACTIVE_TEXT N_("Active windows")
64 #define ACTIVE_LONGTEXT N_("Comma-separated list of active windows, " \
67 #define ASPECT_TEXT N_("Element aspect ratio")
68 #define ASPECT_LONGTEXT N_("Aspect ratio of the individual displays " \
71 #define CFG_PREFIX "wall-"
74 set_description( _("Wall video filter") );
75 set_shortname( _("Image wall" ));
76 set_capability( "video filter", 0 );
77 set_category( CAT_VIDEO );
78 set_subcategory( SUBCAT_VIDEO_VFILTER );
80 add_integer( CFG_PREFIX "cols", 3, NULL, COLS_TEXT, COLS_LONGTEXT, false );
81 add_integer( CFG_PREFIX "rows", 3, NULL, ROWS_TEXT, ROWS_LONGTEXT, false );
82 add_string( CFG_PREFIX "active", NULL, NULL, ACTIVE_TEXT, ACTIVE_LONGTEXT,
84 add_string( CFG_PREFIX "element-aspect", "4:3", NULL, ASPECT_TEXT, ASPECT_LONGTEXT, false );
86 add_shortcut( "wall" );
87 set_callbacks( Create, Destroy );
90 static const char *ppsz_filter_options[] = {
91 "cols", "rows", "active", "element-aspect", NULL
94 /*****************************************************************************
95 * vout_sys_t: Wall video output method descriptor
96 *****************************************************************************
97 * This structure is part of the video output thread descriptor.
98 * It describes the Wall specific properties of an output thread.
99 *****************************************************************************/
112 vout_thread_t *p_vout;
116 /*****************************************************************************
117 * Control: control facility for the vout (forwards to child vout)
118 *****************************************************************************/
119 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
121 int i_row, i_col, i_vout = 0;
123 for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
125 for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
127 vout_vaControl( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
135 /*****************************************************************************
136 * Create: allocates Wall video thread output method
137 *****************************************************************************
138 * This function allocates and initializes a Wall vout method.
139 *****************************************************************************/
140 static int Create( vlc_object_t *p_this )
142 vout_thread_t *p_vout = (vout_thread_t *)p_this;
143 char *psz_method, *psz_tmp, *psz_method_tmp;
146 /* Allocate structure */
147 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
148 if( p_vout->p_sys == NULL )
150 msg_Err( p_vout, "out of memory" );
154 p_vout->pf_init = Init;
155 p_vout->pf_end = End;
156 p_vout->pf_manage = NULL;
157 p_vout->pf_render = Render;
158 p_vout->pf_display = NULL;
159 p_vout->pf_control = Control;
161 config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
164 /* Look what method was requested */
165 p_vout->p_sys->i_col = var_CreateGetInteger( p_vout, CFG_PREFIX "cols" );
166 p_vout->p_sys->i_row = var_CreateGetInteger( p_vout, CFG_PREFIX "rows" );
168 p_vout->p_sys->i_col = __MAX( 1, __MIN( 15, p_vout->p_sys->i_col ) );
169 p_vout->p_sys->i_row = __MAX( 1, __MIN( 15, p_vout->p_sys->i_row ) );
171 msg_Dbg( p_vout, "opening a %i x %i wall",
172 p_vout->p_sys->i_col, p_vout->p_sys->i_row );
174 p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_row *
175 p_vout->p_sys->i_col *
176 sizeof(struct vout_list_t) );
177 if( p_vout->p_sys->pp_vout == NULL )
179 msg_Err( p_vout, "out of memory" );
180 free( p_vout->p_sys );
185 psz_method = var_CreateGetNonEmptyString( p_vout, CFG_PREFIX "active" );
187 /* If no trailing vout are specified, take them all */
188 if( psz_method == NULL )
190 for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
193 p_vout->p_sys->pp_vout[i_vout].b_active = 1;
196 /* If trailing vout are specified, activate only the requested ones */
199 for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
202 p_vout->p_sys->pp_vout[i_vout].b_active = 0;
207 psz_tmp = psz_method;
208 while( *psz_tmp && *psz_tmp != ',' )
216 i_vout = atoi( psz_method );
217 psz_method = psz_tmp + 1;
221 i_vout = atoi( psz_method );
222 psz_method = psz_tmp;
226 i_vout < p_vout->p_sys->i_row * p_vout->p_sys->i_col )
228 p_vout->p_sys->pp_vout[i_vout].b_active = 1;
233 free( psz_method_tmp );
238 /*****************************************************************************
239 * Init: initialize Wall video thread output method
240 *****************************************************************************/
241 static int Init( vout_thread_t *p_vout )
243 int i_index, i_row, i_col, i_width, i_height, i_left, i_top;
244 unsigned int i_target_width,i_target_height;
247 int i_aspect = 4*VOUT_ASPECT_FACTOR/3;
249 unsigned int i_hstart, i_hend, i_vstart, i_vend;
250 unsigned int w1,h1,w2,h2;
252 int i_vstart_rounded = 0, i_hstart_rounded = 0;
255 memset( &fmt, 0, sizeof(video_format_t) );
257 psz_aspect = var_CreateGetNonEmptyString( p_vout,
258 CFG_PREFIX "element-aspect" );
259 if( psz_aspect && *psz_aspect )
261 char *psz_parser = strchr( psz_aspect, ':' );
264 *psz_parser++ = '\0';
265 i_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR
266 / atoi( psz_parser );
270 msg_Warn( p_vout, "invalid aspect ratio specification" );
275 i_xpos = var_CreateGetInteger( p_vout, "video-x" );
276 i_ypos = var_CreateGetInteger( p_vout, "video-y" );
277 if( i_xpos < 0 ) i_xpos = 0;
278 if( i_ypos < 0 ) i_ypos = 0;
280 I_OUTPUTPICTURES = 0;
282 /* Initialize the output structure */
283 p_vout->output.i_chroma = p_vout->render.i_chroma;
284 p_vout->output.i_width = p_vout->render.i_width;
285 p_vout->output.i_height = p_vout->render.i_height;
286 p_vout->output.i_aspect = p_vout->render.i_aspect;
287 var_Create( p_vout, "align", VLC_VAR_INTEGER );
289 fmt.i_width = fmt.i_visible_width = p_vout->render.i_width;
290 fmt.i_height = fmt.i_visible_height = p_vout->render.i_height;
291 fmt.i_x_offset = fmt.i_y_offset = 0;
292 fmt.i_chroma = p_vout->render.i_chroma;
293 fmt.i_aspect = p_vout->render.i_aspect;
294 fmt.i_sar_num = p_vout->render.i_aspect * fmt.i_height / fmt.i_width;
295 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
297 w1 = p_vout->output.i_width / p_vout->p_sys->i_col;
299 h1 = w1 * VOUT_ASPECT_FACTOR / i_aspect&~1;
302 h2 = p_vout->output.i_height / p_vout->p_sys->i_row&~1;
304 w2 = h2 * i_aspect / VOUT_ASPECT_FACTOR&~1;
307 if ( h1 * p_vout->p_sys->i_row < p_vout->output.i_height )
311 i_target_height = h2;
313 i_vend = p_vout->output.i_height;
314 i_tmp = i_target_width * p_vout->p_sys->i_col;
315 while( i_tmp < p_vout->output.i_width ) i_tmp += p_vout->p_sys->i_col;
316 i_hstart = (( i_tmp - p_vout->output.i_width ) / 2)&~1;
317 i_hstart_rounded = ( ( i_tmp - p_vout->output.i_width ) % 2 ) ||
318 ( ( ( i_tmp - p_vout->output.i_width ) / 2 ) & 1 );
319 i_hend = i_hstart + p_vout->output.i_width;
324 i_target_height = h1;
327 i_hend = p_vout->output.i_width;
328 i_tmp = i_target_height * p_vout->p_sys->i_row;
329 while( i_tmp < p_vout->output.i_height ) i_tmp += p_vout->p_sys->i_row;
330 i_vstart = ( ( i_tmp - p_vout->output.i_height ) / 2 ) & ~1;
331 i_vstart_rounded = ( ( i_tmp - p_vout->output.i_height ) % 2 ) ||
332 ( ( ( i_tmp - p_vout->output.i_height ) / 2 ) & 1 );
333 i_vend = i_vstart + p_vout->output.i_height;
335 msg_Dbg( p_vout, "target resolution %dx%d", i_target_width, i_target_height );
337 /* Try to open the real video output */
338 msg_Dbg( p_vout, "spawning the real video outputs" );
340 p_vout->p_sys->i_vout = 0;
341 msg_Dbg( p_vout, "target window (%d,%d)-(%d,%d)", i_hstart,i_vstart,i_hend,i_vend );
346 for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
350 for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
354 if( i_col*i_target_width >= i_hstart &&
355 (i_col+1)*i_target_width <= i_hend )
357 i_width = i_target_width;
359 else if( ( i_col + 1 ) * i_target_width < i_hstart ||
360 ( i_col * i_target_width ) > i_hend )
366 i_width = ( i_target_width - i_hstart % i_target_width );
367 if( i_col >= ( p_vout->p_sys->i_col / 2 ) )
369 i_align |= VOUT_ALIGN_LEFT;
370 i_width -= i_hstart_rounded ? 2: 0;
374 i_align |= VOUT_ALIGN_RIGHT;
378 if( i_row * i_target_height >= i_vstart &&
379 ( i_row + 1 ) * i_target_height <= i_vend )
381 i_height = i_target_height;
383 else if( ( i_row + 1 ) * i_target_height < i_vstart ||
384 ( i_row * i_target_height ) > i_vend )
390 i_height = ( i_target_height -
391 i_vstart%i_target_height );
392 if( i_row >= ( p_vout->p_sys->i_row / 2 ) )
394 i_align |= VOUT_ALIGN_TOP;
395 i_height -= i_vstart_rounded ? 2: 0;
399 i_align |= VOUT_ALIGN_BOTTOM;
402 if( i_height == 0 || i_width == 0 )
404 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active = false;
407 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_width = i_width;
408 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_height = i_height;
409 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_left = i_left;
410 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_top = i_top;
413 if( !p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
415 p_vout->p_sys->i_vout++;
418 var_SetInteger( p_vout, "align", i_align );
419 var_SetInteger( p_vout, "video-x", i_left + i_xpos - i_width);
420 var_SetInteger( p_vout, "video-y", i_top + i_ypos );
422 fmt.i_width = fmt.i_visible_width = i_width;
423 fmt.i_height = fmt.i_visible_height = i_height;
424 fmt.i_aspect = i_aspect * i_target_height / i_height *
425 i_width / i_target_width;
427 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout =
428 vout_Create( p_vout, &fmt );
429 if( !p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout )
431 msg_Err( p_vout, "failed to get %ix%i vout threads",
432 p_vout->p_sys->i_col, p_vout->p_sys->i_row );
433 RemoveAllVout( p_vout );
437 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout,
439 p_vout->p_sys->i_vout++;
443 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
445 ADD_PARENT_CALLBACKS( SendEventsToChild );
450 /*****************************************************************************
451 * End: terminate Wall video thread output method
452 *****************************************************************************/
453 static void End( vout_thread_t *p_vout )
457 /* Free the fake output buffers we allocated */
458 for( i_index = I_OUTPUTPICTURES ; i_index ; )
461 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
465 /*****************************************************************************
466 * Destroy: destroy Wall video thread output method
467 *****************************************************************************
468 * Terminate an output method created by WallCreateOutputMethod
469 *****************************************************************************/
470 static void Destroy( vlc_object_t *p_this )
472 vout_thread_t *p_vout = (vout_thread_t *)p_this;
474 RemoveAllVout( p_vout );
476 DEL_PARENT_CALLBACKS( SendEventsToChild );
478 free( p_vout->p_sys->pp_vout );
479 free( p_vout->p_sys );
482 /*****************************************************************************
483 * Render: displays previously rendered output
484 *****************************************************************************
485 * This function send the currently rendered image to Wall image, waits
486 * until it is displayed and switch the two rendering buffers, preparing next
488 *****************************************************************************/
489 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
491 picture_t *p_outpic = NULL;
492 int i_col, i_row, i_vout, i_plane;
493 int pi_left_skip[VOUT_MAX_PLANES], pi_top_skip[VOUT_MAX_PLANES];
497 for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
499 for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
501 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
503 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;
504 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;
507 if( !p_vout->p_sys->pp_vout[ i_vout ].b_active )
514 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
518 if( p_vout->b_die || p_vout->b_error )
521 p_vout->p_sys->pp_vout[ i_vout ].p_vout, p_outpic );
525 msleep( VOUT_OUTMEM_SLEEP );
528 vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
529 p_outpic, p_pic->date );
530 vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
533 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
535 uint8_t *p_in, *p_in_end, *p_out;
536 int i_in_pitch = p_pic->p[i_plane].i_pitch;
537 int i_out_pitch = p_outpic->p[i_plane].i_pitch;
538 int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
540 p_in = p_pic->p[i_plane].p_pixels
541 + pi_top_skip[i_plane] + pi_left_skip[i_plane];
543 p_in_end = p_in + p_outpic->p[i_plane].i_visible_lines
544 * p_pic->p[i_plane].i_pitch;
546 p_out = p_outpic->p[i_plane].p_pixels;
548 while( p_in < p_in_end )
550 vlc_memcpy( p_out, p_in, i_copy_pitch );
552 p_out += i_out_pitch;
555 // pi_left_skip[i_plane] += i_copy_pitch;
558 vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
560 vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
566 /* for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ ) */
568 /* pi_top_skip[i_plane] += p_vout->p_sys->pp_vout[ i_vout ].i_height */
569 /* * p_pic->p[i_plane].i_visible_lines */
570 /* / p_vout->output.i_height */
571 /* * p_pic->p[i_plane].i_pitch; */
576 /*****************************************************************************
577 * RemoveAllVout: destroy all the child video output threads
578 *****************************************************************************/
579 static void RemoveAllVout( vout_thread_t *p_vout )
581 while( p_vout->p_sys->i_vout )
583 --p_vout->p_sys->i_vout;
584 if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
587 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout,
590 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout );
592 p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout );
597 /*****************************************************************************
598 * SendEvents: forward mouse and keyboard events to the parent p_vout
599 *****************************************************************************/
600 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
601 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
604 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
606 vlc_value_t sentval = newval;
608 /* Find the video output index */
609 for( i_vout = 0; i_vout < p_vout->p_sys->i_vout; i_vout++ )
611 if( p_this == (vlc_object_t *)p_vout->p_sys->pp_vout[ i_vout ].p_vout )
617 if( i_vout == p_vout->p_sys->i_vout )
622 /* Translate the mouse coordinates */
623 if( !strcmp( psz_var, "mouse-x" ) )
625 sentval.i_int += p_vout->output.i_width
626 * (i_vout % p_vout->p_sys->i_col)
627 / p_vout->p_sys->i_col;
629 else if( !strcmp( psz_var, "mouse-y" ) )
631 sentval.i_int += p_vout->output.i_height
632 * (i_vout / p_vout->p_sys->i_row)
633 / p_vout->p_sys->i_row;
636 var_Set( p_vout, psz_var, sentval );
641 /*****************************************************************************
642 * SendEventsToChild: forward events to the child/children vout
643 *****************************************************************************/
644 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
645 vlc_value_t oldval, vlc_value_t newval, void *p_data )
647 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
648 vout_thread_t *p_vout = (vout_thread_t *)p_this;
649 int i_row, i_col, i_vout = 0;
651 for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
653 for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
655 var_Set( p_vout->p_sys->pp_vout[ i_vout ].p_vout, psz_var, newval);
656 if( !strcmp( psz_var, "fullscreen" ) ) break;