1 /*****************************************************************************
2 * wrapper.c: a "video filter2/splitter" with mouse to "video filter" wrapper.
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ 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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37 #include <vlc_video_splitter.h>
39 #include "filter_common.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t *, const char *psz_name, bool b_filter );
45 static void Close( vlc_object_t * );
47 #define DECLARE_OPEN(name,filter) \
48 static int Open##name ( vlc_object_t *p_this ) { return Open( p_this, #name, filter ); }
50 DECLARE_OPEN(magnify, true)
51 DECLARE_OPEN(puzzle, true)
52 DECLARE_OPEN(logo, true)
54 DECLARE_OPEN(clone, false)
55 DECLARE_OPEN(wall, false)
56 DECLARE_OPEN(panoramix, false)
60 #define DECLARE_MODULE(name) \
61 set_description( "Video filter "#name" wrapper" ) \
62 set_shortname( "Video filter"#name" wrapper" ) \
63 set_capability( "video filter", 0 ) \
64 set_callbacks( Open##name, Close ) \
68 set_category( CAT_VIDEO )
69 set_subcategory( SUBCAT_VIDEO_VFILTER )
71 DECLARE_MODULE(magnify)
74 DECLARE_MODULE(puzzle)
86 DECLARE_MODULE(panoramix)
92 /*****************************************************************************
94 *****************************************************************************/
95 static int Init ( vout_thread_t * );
96 static void End ( vout_thread_t * );
97 static void Render ( vout_thread_t *, picture_t * );
98 static int Control ( vout_thread_t *, int, va_list );
103 vout_thread_t **pp_vout;
108 filter_chain_t *p_chain;
109 video_splitter_t *p_splitter;
111 vlc_mouse_t *p_mouse_src;
116 static int FilterAllocationInit ( filter_t *, void * );
117 static void FilterAllocationClean( filter_t * );
120 static int FullscreenEventDown( vlc_object_t *, char const *,
121 vlc_value_t, vlc_value_t, void * );
122 static int SplitterPictureNew( video_splitter_t *, picture_t *pp_picture[] );
123 static void SplitterPictureDel( video_splitter_t *, picture_t *pp_picture[] );
126 static int MouseEvent( vlc_object_t *, char const *,
127 vlc_value_t, vlc_value_t, void * );
128 static void VoutsClean( vout_thread_t *p_vout, int i_count );
131 * Open our wrapper instance.
133 static int Open( vlc_object_t *p_this, const char *psz_name, bool b_filter )
135 vout_thread_t *p_vout = (vout_thread_t *)p_this;
138 msg_Err( p_vout, "Opening video %s wrapper for %s",
139 b_filter ? "filter" : "splitter", psz_name );
143 es_format_Init( &fmt, VIDEO_ES, p_vout->render.i_chroma );
144 video_format_Setup( &fmt.video, p_vout->render.i_chroma,
145 p_vout->render.i_width, p_vout->render.i_height,
146 p_vout->render.i_aspect * p_vout->render.i_height,
147 VOUT_ASPECT_FACTOR * p_vout->render.i_width );
149 /* Try to open our real module */
150 filter_chain_t *p_chain = NULL;
151 video_splitter_t *p_splitter = NULL;
154 p_chain = filter_chain_New( p_vout, "video filter2", false,
155 FilterAllocationInit, FilterAllocationClean, p_vout );
159 filter_chain_Reset( p_chain, &fmt, &fmt );
162 filter_chain_AppendFilter( p_chain, psz_name, p_vout->p_cfg, &fmt, &fmt );
166 msg_Err( p_vout, "Failed to open filter '%s'", psz_name );
167 filter_chain_Delete( p_chain );
173 p_splitter = video_splitter_New( VLC_OBJECT(p_vout), psz_name, &fmt.video );
176 msg_Err( p_vout, "Failed to open splitter '%s'", psz_name );
179 assert( p_splitter->i_output > 0 );
181 p_splitter->p_owner = (video_splitter_owner_t*)p_vout;
182 p_splitter->pf_picture_new = SplitterPictureNew;
183 p_splitter->pf_picture_del = SplitterPictureDel;
187 p_vout->p_sys = p_sys = malloc( sizeof(*p_sys) );
191 p_sys->i_vout = p_chain ? 1 : p_splitter->i_output;
192 p_sys->pp_vout = calloc( p_sys->i_vout, sizeof(*p_sys->pp_vout) );;
193 p_sys->p_mouse_src = calloc( p_sys->i_vout, sizeof(*p_sys->p_mouse_src) );
196 vlc_mutex_init( &p_sys->lock );
197 p_sys->p_chain = p_chain;
198 p_sys->p_splitter = p_splitter;
199 vlc_mouse_Init( &p_sys->mouse );
200 for( int i = 0; i < p_sys->i_vout; i++ )
201 vlc_mouse_Init( &p_sys->p_mouse_src[i] );
203 p_vout->pf_init = Init;
204 p_vout->pf_end = End;
205 p_vout->pf_manage = NULL;
206 p_vout->pf_render = Render;
207 p_vout->pf_display = NULL;
208 p_vout->pf_control = Control;
214 filter_chain_Delete( p_chain );
216 video_splitter_Delete( p_splitter );
221 * Close our wrapper instance
223 static void Close( vlc_object_t *p_this )
225 vout_thread_t *p_vout = (vout_thread_t *)p_this;
226 vout_sys_t *p_sys = p_vout->p_sys;
229 filter_chain_Delete( p_sys->p_chain );
230 if( p_sys->p_splitter )
231 video_splitter_Delete( p_sys->p_splitter );
233 vlc_mutex_destroy( &p_sys->lock );
234 es_format_Clean( &p_sys->fmt );
235 free( p_sys->p_mouse_src );
236 free( p_sys->pp_vout );
238 free( p_vout->p_sys );
242 * Initialise our wrapper
244 static int Init( vout_thread_t *p_vout )
246 vout_sys_t *p_sys = p_vout->p_sys;
248 assert( p_vout->render.i_chroma == p_sys->fmt.video.i_chroma &&
249 p_vout->render.i_width == p_sys->fmt.video.i_width &&
250 p_vout->render.i_height == p_sys->fmt.video.i_height );
252 /* Initialize the output structure */
253 I_OUTPUTPICTURES = 0;
254 p_vout->output.i_chroma = p_vout->render.i_chroma;
255 p_vout->output.i_width = p_vout->render.i_width;
256 p_vout->output.i_height = p_vout->render.i_height;
257 p_vout->output.i_aspect = p_vout->render.i_aspect;
259 p_vout->fmt_out = p_vout->fmt_in;
261 /* Try to open the real video output */
262 msg_Dbg( p_vout, "spawning the real video output(s)" );
264 video_format_t fmt = p_vout->fmt_out;
268 p_sys->pp_vout[0] = vout_Create( p_vout, &fmt );
269 if( !p_sys->pp_vout[0] )
271 msg_Err( p_vout, "cannot open vout, aborting" );
274 vout_filter_AddChild( p_vout, p_sys->pp_vout[0], MouseEvent );
278 video_splitter_t *p_splitter = p_sys->p_splitter;
281 const int i_org_align = var_CreateGetInteger( p_vout, "align" );
282 const int i_org_x = var_CreateGetInteger( p_vout, "video-x" );
283 const int i_org_y = var_CreateGetInteger( p_vout, "video-y" );
284 const char *psz_org_vout = var_CreateGetNonEmptyString( p_vout, "vout" );
287 for( int i = 0; i < p_splitter->i_output; i++ )
289 const video_splitter_output_t *p_cfg = &p_splitter->p_output[i];
292 var_SetInteger( p_vout, "align", p_cfg->window.i_align);
294 var_SetInteger( p_vout, "video-x", i_org_x + p_cfg->window.i_x );
295 var_SetInteger( p_vout, "video-y", i_org_y + p_cfg->window.i_y );
297 if( p_cfg->psz_module )
298 var_SetString( p_vout, "vout", p_cfg->psz_module );
301 video_format_t fmt = p_cfg->fmt;
302 p_sys->pp_vout[i] = vout_Create( p_vout, &fmt );
303 if( !p_sys->pp_vout[i] )
305 msg_Err( p_vout, "cannot open vout, aborting" );
306 VoutsClean( p_vout, i );
311 /* Attach once pp_vout is completly filed to avoid race conditions */
312 for( int i = 0; i < p_splitter->i_output; i++ )
313 vout_filter_SetupChild( p_vout, p_sys->pp_vout[i],
315 FullscreenEventDown, true );
316 /* Restore settings */
317 var_SetInteger( p_vout, "align", i_org_align );
318 var_SetInteger( p_vout, "video-x", i_org_x );
319 var_SetInteger( p_vout, "video-y", i_org_y );
320 var_SetString( p_vout, "vout", psz_org_vout ? psz_org_vout : "" );
323 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
329 * Clean up our wrapper
331 static void End( vout_thread_t *p_vout )
333 vout_sys_t *p_sys = p_vout->p_sys;
335 VoutsClean( p_vout, p_sys->i_vout );
337 vout_filter_ReleaseDirectBuffers( p_vout );
341 * Control the real vout
343 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
345 vout_sys_t *p_sys = p_vout->p_sys;
346 int i_ret = VLC_SUCCESS;
348 for( int i = 0; i < p_sys->i_vout; i++ )
349 i_ret = vout_vaControl( p_sys->pp_vout[i], i_query, args );
356 static void Render( vout_thread_t *p_vout, picture_t *p_src )
358 vout_sys_t *p_sys = p_vout->p_sys;
360 vlc_mutex_lock( &p_sys->lock );
362 picture_t *pp_dst[p_sys->i_vout];
366 pp_dst[0] = filter_chain_VideoFilter( p_sys->p_chain, p_src );
370 if( video_splitter_Filter( p_sys->p_splitter, pp_dst, p_src ) )
372 for( int i = 0; i < p_sys->i_vout; i++ )
376 for( int i = 0; i < p_sys->i_vout; i++ )
378 picture_t *p_dst = pp_dst[i];
380 vout_DisplayPicture( p_sys->pp_vout[i], p_dst );
383 vlc_mutex_unlock( &p_sys->lock );
387 static void VoutsClean( vout_thread_t *p_vout, int i_count )
389 vout_sys_t *p_sys = p_vout->p_sys;
391 /* Detach all vouts before destroying them */
392 for( int i = 0; i < i_count; i++ )
395 vout_filter_DelChild( p_vout, p_sys->pp_vout[i], MouseEvent );
397 vout_filter_SetupChild( p_vout, p_sys->pp_vout[i],
399 FullscreenEventDown, false );
402 for( int i = 0; i < i_count; i++ )
403 vout_CloseAndRelease( p_sys->pp_vout[i] );
405 static int VoutsNewPicture( vout_thread_t *p_vout, picture_t *pp_dst[] )
407 vout_sys_t *p_sys = p_vout->p_sys;
409 for( int i = 0; i < p_sys->i_vout; i++ )
411 picture_t *p_picture = NULL;
414 p_picture = vout_CreatePicture( p_sys->pp_vout[i], 0, 0, 0 );
418 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
420 msleep( VOUT_OUTMEM_SLEEP );
422 /* FIXME what to do with the allocated picture ? */
426 pp_dst[i] = p_picture;
432 * Callback for mouse events
434 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
435 vlc_value_t oldval, vlc_value_t newval, void *p_data )
437 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
438 vout_thread_t *p_vout = p_data;
439 vout_sys_t *p_sys = p_vout->p_sys;
442 for( i_index = 0; i_index < p_sys->i_vout; i_index++ )
444 if( p_this == VLC_OBJECT(p_sys->pp_vout[i_index]) )
447 if( i_index >= p_sys->i_vout )
449 msg_Err( p_vout, "Failed to find vout source in MouseEvent" );
453 vout_thread_t *p_vout_src = p_sys->pp_vout[i_index];
456 vlc_mouse_Init( &m );
457 var_GetCoords( p_vout_src, "mouse-moved", &m.i_x, &m.i_y );
458 m.i_pressed = var_GetInteger( p_vout_src, "mouse-button-down" );
460 vlc_mutex_lock( &p_sys->lock );
463 vlc_mouse_t omouse = p_sys->mouse;
468 i_ret = filter_chain_MouseFilter( p_sys->p_chain, &nmouse, &m );
472 vlc_mouse_t *p_mouse_src = &p_sys->p_mouse_src[i_index];
474 i_ret = video_splitter_Mouse( p_sys->p_splitter, &nmouse, i_index, p_mouse_src, &m );
479 p_sys->mouse = nmouse;
480 vlc_mutex_unlock( &p_sys->lock );
485 if( vlc_mouse_HasMoved( &omouse, &nmouse ) )
487 var_SetCoords( p_vout, "mouse-moved", nmouse.i_x, nmouse.i_y );
489 if( vlc_mouse_HasButton( &omouse, &nmouse ) )
491 var_SetInteger( p_vout, "mouse-button-down", nmouse.i_pressed );
492 if( vlc_mouse_HasPressed( &omouse, &nmouse, MOUSE_BUTTON_LEFT ) )
493 var_SetCoords( p_vout, "mouse-clicked", nmouse.i_x, nmouse.i_y );
495 if( m.b_double_click )
497 /* Nothing with current API */
498 msg_Warn( p_vout, "Ignoring double click" );
503 /* -- Filter callbacks -- */
505 static picture_t *VideoBufferNew( filter_t *p_filter )
507 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
509 picture_t *pp_picture[1];
510 if( VoutsNewPicture( p_vout, pp_picture ) )
512 return pp_picture[0];
514 static void VideoBufferDelete( filter_t *p_filter, picture_t *p_picture )
516 VLC_UNUSED(p_filter); VLC_UNUSED(p_picture);
517 /* FIXME is there anything to do ? */
520 static int FilterAllocationInit( filter_t *p_filter, void *p_data )
522 VLC_UNUSED( p_data );
524 p_filter->pf_video_buffer_new = VideoBufferNew;
525 p_filter->pf_video_buffer_del = VideoBufferDelete;
526 p_filter->p_owner = p_data;
530 static void FilterAllocationClean( filter_t *p_filter )
532 p_filter->pf_video_buffer_new = NULL;
533 p_filter->pf_video_buffer_del = NULL;
536 /* -- Splitter callbacks -- */
538 static int FullscreenEventDown( vlc_object_t *p_this, char const *psz_var,
539 vlc_value_t oldval, vlc_value_t newval, void *p_data )
541 vout_thread_t *p_vout = (vout_thread_t*)p_this;
542 vout_sys_t *p_sys = p_vout->p_sys;
543 VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(psz_var);
545 for( int i = 0; i < p_sys->i_vout; i++ )
547 vout_thread_t *p_child = p_sys->pp_vout[i];
548 var_SetBool( p_child, "fullscreen", newval.b_bool );
553 static int SplitterPictureNew( video_splitter_t *p_splitter, picture_t *pp_picture[] )
555 vout_thread_t *p_vout = (vout_thread_t*)p_splitter->p_owner;
557 return VoutsNewPicture( p_vout, pp_picture );
559 static void SplitterPictureDel( video_splitter_t *p_splitter, picture_t *pp_picture[] )
561 VLC_UNUSED(p_splitter); VLC_UNUSED(pp_picture);
562 /* FIXME is there anything to do ? */