1 /*****************************************************************************
2 * video_output.c : video output thread
4 * This module describes the programming interface for video output threads.
5 * It includes functions allowing to open a new thread, send pictures to a
6 * thread, and destroy a previously oppened video output thread.
7 *****************************************************************************
8 * Copyright (C) 2000-2007 the VideoLAN team
11 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12 * Gildas Bazin <gbazin@videolan.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
36 #include <vlc_common.h>
38 #include <stdlib.h> /* free() */
42 #ifdef HAVE_SYS_TIMES_H
43 # include <sys/times.h>
48 #include <vlc_filter.h>
52 #if defined( __APPLE__ )
53 /* Include darwin_specific.h here if needed */
56 /** FIXME This is quite ugly but needed while we don't have counters
58 //#include "input/input_internal.h"
61 #include <vlc_input.h>
62 #include "vout_pictures.h"
63 #include "vout_internal.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int InitThread ( vout_thread_t * );
69 static void* RunThread ( vlc_object_t * );
70 static void ErrorThread ( vout_thread_t * );
71 static void CleanThread ( vout_thread_t * );
72 static void EndThread ( vout_thread_t * );
74 static void AspectRatio ( int, int *, int * );
76 static void VideoFormatImportRgb( video_format_t *, const picture_heap_t * );
77 static void PictureHeapFixRgb( picture_heap_t * );
79 static void vout_Destructor ( vlc_object_t * p_this );
81 /* Object variables callbacks */
82 static int DeinterlaceCallback( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
84 static int FilterCallback( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
86 static int VideoFilter2Callback( vlc_object_t *, char const *,
87 vlc_value_t, vlc_value_t, void * );
89 /* From vout_intf.c */
90 int vout_Snapshot( vout_thread_t *, picture_t * );
92 /* Display media title in OSD */
93 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
95 /* Time during which the thread will sleep if it has nothing to
96 * display (in micro-seconds) */
97 #define VOUT_IDLE_SLEEP ((int)(0.020*CLOCK_FREQ))
99 /* Maximum lap of time allowed between the beginning of rendering and
100 * display. If, compared to the current date, the next image is too
101 * late, the thread will perform an idle loop. This time should be
102 * at least VOUT_IDLE_SLEEP plus the time required to render a few
103 * images, to avoid trashing of decoded images */
104 #define VOUT_DISPLAY_DELAY ((int)(0.200*CLOCK_FREQ))
106 /* Better be in advance when awakening than late... */
107 #define VOUT_MWAIT_TOLERANCE ((mtime_t)(0.020*CLOCK_FREQ))
109 /*****************************************************************************
110 * Video Filter2 functions
111 *****************************************************************************/
112 static picture_t *video_new_buffer_filter( filter_t *p_filter )
114 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
115 picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
117 p_picture->i_status = READY_PICTURE;
122 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
124 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
126 vlc_mutex_lock( &p_vout->picture_lock );
127 vout_UsePictureLocked( p_vout, p_pic );
128 vlc_mutex_unlock( &p_vout->picture_lock );
131 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
133 p_filter->pf_vout_buffer_new = video_new_buffer_filter;
134 p_filter->pf_vout_buffer_del = video_del_buffer_filter;
135 p_filter->p_owner = p_data; /* p_vout */
139 /*****************************************************************************
140 * vout_Request: find a video output thread, create one, or destroy one.
141 *****************************************************************************
142 * This function looks for a video output thread matching the current
143 * properties. If not found, it spawns a new one.
144 *****************************************************************************/
145 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
146 video_format_t *p_fmt )
148 const bool b_vout_provided = p_vout != NULL;
151 /* Video output is no longer used.
152 * TODO: support for reusing video outputs with proper _thread-safe_
153 * reference handling. */
155 vout_CloseAndRelease( p_vout );
159 /* If a video output was provided, lock it, otherwise look for one. */
162 vlc_object_hold( p_vout );
165 /* TODO: find a suitable unused video output */
167 /* If we now have a video output, check it has the right properties */
170 char *psz_filter_chain;
173 vlc_mutex_lock( &p_vout->change_lock );
175 /* We don't directly check for the "vout-filter" variable for obvious
176 * performance reasons. */
177 if( p_vout->p->b_filter_change )
179 var_Get( p_vout, "vout-filter", &val );
180 psz_filter_chain = val.psz_string;
182 if( psz_filter_chain && !*psz_filter_chain )
184 free( psz_filter_chain );
185 psz_filter_chain = NULL;
187 if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
189 free( p_vout->p->psz_filter_chain );
190 p_vout->p->psz_filter_chain = NULL;
193 if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
195 p_vout->p->b_filter_change = false;
198 free( psz_filter_chain );
201 if( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ||
202 p_vout->fmt_render.i_width != p_fmt->i_width ||
203 p_vout->fmt_render.i_height != p_fmt->i_height ||
204 p_vout->p->b_filter_change )
206 vlc_mutex_unlock( &p_vout->change_lock );
208 /* We are not interested in this format, close this vout */
209 vout_CloseAndRelease( p_vout );
210 vlc_object_release( p_vout );
215 /* This video output is cool! Hijack it. */
216 if( p_vout->fmt_render.i_aspect != p_fmt->i_aspect )
218 /* Correct aspect ratio on change
219 * FIXME factorize this code with other aspect ration related code */
220 unsigned int i_sar_num;
221 unsigned int i_sar_den;
222 unsigned int i_aspect;
224 i_aspect = p_fmt->i_aspect;
225 vlc_ureduce( &i_sar_num, &i_sar_den,
226 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
228 /* What's that, it does not seems to be used correcly everywhere
229 * beside the previous p_vout->fmt_render.i_aspect != p_fmt->i_aspect
230 * should be fixed to use it too then */
231 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
233 i_sar_num *= p_vout->i_par_den;
234 i_sar_den *= p_vout->i_par_num;
235 i_aspect = i_aspect * p_vout->i_par_den / p_vout->i_par_num;
239 if( i_sar_num > 0 && i_sar_den > 0 && i_aspect > 0 )
241 p_vout->fmt_in.i_sar_num = i_sar_num;
242 p_vout->fmt_in.i_sar_den = i_sar_den;
243 p_vout->fmt_in.i_aspect = i_aspect;
245 p_vout->fmt_render.i_sar_num = i_sar_num;
246 p_vout->fmt_render.i_sar_den = i_sar_den;
247 p_vout->fmt_render.i_aspect = i_aspect;
249 p_vout->render.i_aspect = i_aspect;
251 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
255 vlc_mutex_unlock( &p_vout->change_lock );
257 vlc_object_release( p_vout );
262 msg_Dbg( p_this, "reusing provided vout" );
264 spu_Attach( p_vout->p_spu, p_this, true );
266 vlc_object_detach( p_vout );
267 vlc_object_attach( p_vout, p_this );
269 /* Display title if we are not using the vout given to vout_Request.
270 * XXX for now b_vout_provided is always true at this stage */
271 if( p_vout->p->b_title_show && !b_vout_provided )
272 DisplayTitleOnOSD( p_vout );
278 msg_Dbg( p_this, "no usable vout present, spawning one" );
280 p_vout = vout_Create( p_this, p_fmt );
286 /*****************************************************************************
287 * vout_Create: creates a new video output thread
288 *****************************************************************************
289 * This function creates a new video output thread, and returns a pointer
290 * to its description. On error, it returns NULL.
291 *****************************************************************************/
292 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
294 vout_thread_t * p_vout; /* thread descriptor */
295 int i_index; /* loop variable */
296 vlc_value_t val, text;
298 unsigned int i_width = p_fmt->i_width;
299 unsigned int i_height = p_fmt->i_height;
300 vlc_fourcc_t i_chroma = p_fmt->i_chroma;
301 unsigned int i_aspect = p_fmt->i_aspect;
303 config_chain_t *p_cfg;
307 if( i_width <= 0 || i_height <= 0 || i_aspect <= 0 )
310 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
311 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
312 if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
315 /* Allocate descriptor */
316 static const char typename[] = "video output";
317 p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
323 p_vout->p = calloc( 1, sizeof(*p_vout->p) );
326 vlc_object_release( p_vout );
330 /* Initialize pictures - translation tables and functions
331 * will be initialized later in InitThread */
332 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
334 p_vout->p_picture[i_index].pf_lock = NULL;
335 p_vout->p_picture[i_index].pf_unlock = NULL;
336 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
337 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
338 p_vout->p_picture[i_index].b_slow = 0;
341 /* No images in the heap */
342 p_vout->i_heap_size = 0;
344 /* Initialize the rendering heap */
345 I_RENDERPICTURES = 0;
347 p_vout->fmt_render = *p_fmt; /* FIXME palette */
348 p_vout->fmt_in = *p_fmt; /* FIXME palette */
350 p_vout->render.i_width = i_width;
351 p_vout->render.i_height = i_height;
352 p_vout->render.i_chroma = i_chroma;
353 p_vout->render.i_aspect = i_aspect;
355 p_vout->render.i_rmask = p_fmt->i_rmask;
356 p_vout->render.i_gmask = p_fmt->i_gmask;
357 p_vout->render.i_bmask = p_fmt->i_bmask;
359 p_vout->render.i_last_used_pic = -1;
360 p_vout->render.b_allow_modify_pics = 1;
362 /* Zero the output heap */
363 I_OUTPUTPICTURES = 0;
364 p_vout->output.i_width = 0;
365 p_vout->output.i_height = 0;
366 p_vout->output.i_chroma = 0;
367 p_vout->output.i_aspect = 0;
369 p_vout->output.i_rmask = 0;
370 p_vout->output.i_gmask = 0;
371 p_vout->output.i_bmask = 0;
373 /* Initialize misc stuff */
374 p_vout->i_changes = 0;
376 p_vout->b_fullscreen = 0;
377 p_vout->i_alignment = 0;
378 p_vout->p->render_time = 10;
379 p_vout->p->c_fps_samples = 0;
380 p_vout->p->i_picture_lost = 0;
381 p_vout->p->i_picture_displayed = 0;
382 p_vout->p->b_filter_change = 0;
383 p_vout->p->b_paused = false;
384 p_vout->p->i_pause_date = 0;
385 p_vout->pf_control = NULL;
386 p_vout->p_window = NULL;
387 p_vout->p->i_par_num =
388 p_vout->p->i_par_den = 1;
389 p_vout->p->p_picture_displayed = NULL;
390 p_vout->p->i_picture_displayed_date = 0;
391 p_vout->p->b_picture_displayed = false;
392 p_vout->p->b_picture_empty = false;
394 /* Initialize locks */
395 vlc_mutex_init( &p_vout->picture_lock );
396 vlc_cond_init( &p_vout->p->picture_wait );
397 vlc_mutex_init( &p_vout->change_lock );
398 vlc_mutex_init( &p_vout->p->vfilter_lock );
400 /* Mouse coordinates */
401 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
402 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
403 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
404 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
405 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
407 /* Initialize subpicture unit */
408 p_vout->p_spu = spu_Create( p_vout );
409 spu_Attach( p_vout->p_spu, p_parent, true );
411 /* Attach the new object now so we can use var inheritance below */
412 vlc_object_attach( p_vout, p_parent );
414 spu_Init( p_vout->p_spu );
416 /* Take care of some "interface/control" related initialisations */
417 vout_IntfInit( p_vout );
419 /* If the parent is not a VOUT object, that means we are at the start of
420 * the video output pipe */
421 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
423 /* Look for the default filter configuration */
424 p_vout->p->psz_filter_chain =
425 var_CreateGetStringCommand( p_vout, "vout-filter" );
427 /* Apply video filter2 objects on the first vout */
429 var_CreateGetStringCommand( p_vout, "video-filter" );
433 /* continue the parent's filter chain */
436 /* Ugly hack to jump to our configuration chain */
437 p_vout->p->psz_filter_chain
438 = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
439 p_vout->p->psz_filter_chain
440 = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
441 config_ChainDestroy( p_cfg );
444 /* Create a video filter2 var ... but don't inherit values */
445 var_Create( p_vout, "video-filter",
446 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
447 p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
450 var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
451 p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
452 false, video_filter_buffer_allocation_init, NULL, p_vout );
454 /* Choose the video output module */
455 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
457 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
458 var_Get( p_vout, "vout", &val );
459 psz_parser = val.psz_string;
463 psz_parser = strdup( p_vout->p->psz_filter_chain );
464 p_vout->p->b_title_show = false;
467 /* Create the vout thread */
468 char* psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
471 p_vout->p_cfg = p_cfg;
472 p_vout->p_module = module_need( p_vout,
473 ( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain ) ?
474 "video filter" : "video output", psz_name, p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain );
477 if( p_vout->p_module == NULL )
479 msg_Err( p_vout, "no suitable vout module" );
480 vlc_object_set_destructor( p_vout, vout_Destructor );
481 vlc_object_release( p_vout );
485 /* Create a few object variables for interface interaction */
486 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
487 text.psz_string = _("Deinterlace");
488 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
489 val.psz_string = (char *)""; text.psz_string = _("Disable");
490 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
491 val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
492 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
493 val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
494 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
495 val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
496 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
497 val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
498 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
499 val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
500 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
501 val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
502 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
504 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
506 var_Set( p_vout, "deinterlace", val );
507 free( val.psz_string );
509 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
511 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
512 text.psz_string = _("Filters");
513 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
514 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
516 if( vlc_thread_create( p_vout, "video output", RunThread,
517 VLC_THREAD_PRIORITY_OUTPUT, true ) )
519 module_unneed( p_vout, p_vout->p_module );
520 p_vout->p_module = NULL;
521 vlc_object_set_destructor( p_vout, vout_Destructor );
522 vlc_object_release( p_vout );
526 vlc_object_set_destructor( p_vout, vout_Destructor );
528 if( p_vout->b_error )
530 msg_Err( p_vout, "video output creation failed" );
531 vout_CloseAndRelease( p_vout );
538 /*****************************************************************************
539 * vout_Close: Close a vout created by vout_Create.
540 *****************************************************************************
541 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
542 * You should NEVER call it on vout not obtained through vout_Create
543 * (like with vout_Request or vlc_object_find.)
544 * You can use vout_CloseAndRelease() as a convenient method.
545 *****************************************************************************/
546 void vout_Close( vout_thread_t *p_vout )
550 vlc_object_kill( p_vout );
551 vlc_thread_join( p_vout );
552 module_unneed( p_vout, p_vout->p_module );
553 p_vout->p_module = NULL;
557 static void vout_Destructor( vlc_object_t * p_this )
559 vout_thread_t *p_vout = (vout_thread_t *)p_this;
561 /* Make sure the vout was stopped first */
562 assert( !p_vout->p_module );
565 spu_Destroy( p_vout->p_spu );
567 /* Destroy the locks */
568 vlc_cond_destroy( &p_vout->p->picture_wait );
569 vlc_mutex_destroy( &p_vout->picture_lock );
570 vlc_mutex_destroy( &p_vout->change_lock );
571 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
573 free( p_vout->p->psz_filter_chain );
575 config_ChainDestroy( p_vout->p_cfg );
580 vout_thread_t *p_another_vout;
582 /* This is a dirty hack mostly for Linux, where there is no way to get the
583 * GUI back if you closed it while playing video. This is solved in
584 * Mac OS X, where we have this novelty called menubar, that will always
585 * allow you access to the applications main functionality. They should try
586 * that on linux sometime. */
587 p_another_vout = vlc_object_find( p_this->p_libvlc,
588 VLC_OBJECT_VOUT, FIND_ANYWHERE );
589 if( p_another_vout == NULL )
590 var_SetBool( p_this->p_libvlc, "intf-show", true );
592 vlc_object_release( p_another_vout );
597 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
599 vlc_object_lock( p_vout );
601 assert( !p_vout->p->b_paused || !b_paused );
603 vlc_mutex_lock( &p_vout->picture_lock );
605 p_vout->p->i_picture_displayed_date = 0;
607 if( p_vout->p->b_paused )
609 const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
611 for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
613 picture_t *p_pic = PP_RENDERPICTURE[i_index];
615 if( p_pic->i_status == READY_PICTURE )
616 p_pic->date += i_duration;
618 vlc_cond_signal( &p_vout->p->picture_wait );
619 vlc_mutex_unlock( &p_vout->picture_lock );
621 spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
625 vlc_mutex_unlock( &p_vout->picture_lock );
627 p_vout->p->b_paused = b_paused;
628 p_vout->p->i_pause_date = i_date;
630 vlc_object_unlock( p_vout );
632 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
634 vlc_object_lock( p_vout );
636 *pi_displayed = p_vout->p->i_picture_displayed;
637 *pi_lost = p_vout->p->i_picture_lost;
639 p_vout->p->i_picture_displayed = 0;
640 p_vout->p->i_picture_lost = 0;
642 vlc_object_unlock( p_vout );
644 void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
646 vlc_mutex_lock( &p_vout->picture_lock );
647 p_vout->p->i_picture_displayed_date = 0;
648 for( int i = 0; i < p_vout->render.i_pictures; i++ )
650 picture_t *p_pic = p_vout->render.pp_picture[i];
652 if( p_pic->i_status == READY_PICTURE ||
653 p_pic->i_status == DISPLAYED_PICTURE )
655 /* We cannot change picture status if it is in READY_PICTURE state,
656 * Just make sure they won't be displayed */
657 if( p_pic->date > i_date )
658 p_pic->date = i_date;
661 vlc_cond_signal( &p_vout->p->picture_wait );
662 vlc_mutex_unlock( &p_vout->picture_lock );
664 void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
666 int i_pic, i_ready_pic;
668 vlc_mutex_lock( &p_vout->picture_lock );
670 for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
672 const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
674 if( p_pic->i_status == READY_PICTURE )
677 /* If we have at least 2 ready pictures, wait for the vout thread to
679 if( i_ready_pic >= 2 )
685 if( p_pic->i_status == DISPLAYED_PICTURE )
687 /* If at least one displayed picture is not referenced
688 * let vout free it */
689 if( p_pic->i_refcount == 0 )
693 if( i_pic < p_vout->render.i_pictures && !b_forced )
695 vlc_mutex_unlock( &p_vout->picture_lock );
699 /* Too many pictures are still referenced, there is probably a bug
700 * with the decoder */
702 msg_Err( p_vout, "pictures leaked, resetting the heap" );
704 /* Just free all the pictures */
705 for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
707 picture_t *p_pic = p_vout->render.pp_picture[i_pic];
709 msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
710 p_pic->i_refcount = 0;
711 vout_UsePictureLocked( p_vout, p_pic );
713 vlc_cond_signal( &p_vout->p->picture_wait );
714 vlc_mutex_unlock( &p_vout->picture_lock );
716 void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
718 vlc_mutex_lock( &p_vout->picture_lock );
720 const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
722 p_vout->p->b_picture_displayed = false;
723 p_vout->p->b_picture_empty = false;
724 if( p_vout->p->p_picture_displayed )
726 p_vout->p->p_picture_displayed->date = 1;
727 vlc_cond_signal( &p_vout->p->picture_wait );
730 while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
731 vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->picture_lock );
733 *pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
735 /* TODO advance subpicture by the duration ... */
737 vlc_mutex_unlock( &p_vout->picture_lock );
740 /*****************************************************************************
741 * InitThread: initialize video output thread
742 *****************************************************************************
743 * This function is called from RunThread and performs the second step of the
744 * initialization. It returns 0 on success. Note that the thread's flag are not
745 * modified inside this function.
746 * XXX You have to enter it with change_lock taken.
747 *****************************************************************************/
748 static int ChromaCreate( vout_thread_t *p_vout );
749 static void ChromaDestroy( vout_thread_t *p_vout );
751 static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
753 if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
756 if( p_output->i_chroma != FOURCC_RV15 &&
757 p_output->i_chroma != FOURCC_RV16 &&
758 p_output->i_chroma != FOURCC_RV24 &&
759 p_output->i_chroma != FOURCC_RV32 )
762 return p_output->i_rmask == p_render->i_rmask &&
763 p_output->i_gmask == p_render->i_gmask &&
764 p_output->i_bmask == p_render->i_bmask;
767 static int InitThread( vout_thread_t *p_vout )
769 int i, i_aspect_x, i_aspect_y;
771 /* Initialize output method, it allocates direct buffers for us */
772 if( p_vout->pf_init( p_vout ) )
775 p_vout->p->p_picture_displayed = NULL;
777 if( !I_OUTPUTPICTURES )
779 msg_Err( p_vout, "plugin was unable to allocate at least "
780 "one direct buffer" );
781 p_vout->pf_end( p_vout );
785 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
787 msg_Err( p_vout, "plugin allocated too many direct buffers, "
788 "our internal buffers must have overflown." );
789 p_vout->pf_end( p_vout );
793 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
795 AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
797 msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
798 "chroma %4.4s, ar %i:%i, sar %i:%i",
799 p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
800 p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
801 p_vout->fmt_render.i_visible_width,
802 p_vout->fmt_render.i_visible_height,
803 (char*)&p_vout->fmt_render.i_chroma,
804 i_aspect_x, i_aspect_y,
805 p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
807 AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
809 msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
810 "chroma %4.4s, ar %i:%i, sar %i:%i",
811 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
812 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
813 p_vout->fmt_in.i_visible_width,
814 p_vout->fmt_in.i_visible_height,
815 (char*)&p_vout->fmt_in.i_chroma,
816 i_aspect_x, i_aspect_y,
817 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
819 if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
821 p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
822 p_vout->output.i_width;
823 p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
824 p_vout->output.i_height;
825 p_vout->fmt_out.i_x_offset = p_vout->fmt_out.i_y_offset = 0;
827 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
828 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
830 if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
832 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
833 p_vout->fmt_out.i_height;
834 p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
835 p_vout->fmt_out.i_width;
838 vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
839 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
841 AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
843 msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
844 "chroma %4.4s, ar %i:%i, sar %i:%i",
845 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
846 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
847 p_vout->fmt_out.i_visible_width,
848 p_vout->fmt_out.i_visible_height,
849 (char*)&p_vout->fmt_out.i_chroma,
850 i_aspect_x, i_aspect_y,
851 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
853 /* FIXME removed the need of both fmt_* and heap infos */
854 /* Calculate shifts from system-updated masks */
855 PictureHeapFixRgb( &p_vout->render );
856 VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
858 PictureHeapFixRgb( &p_vout->output );
859 VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
861 /* Check whether we managed to create direct buffers similar to
862 * the render buffers, ie same size and chroma */
863 if( ( p_vout->output.i_width == p_vout->render.i_width )
864 && ( p_vout->output.i_height == p_vout->render.i_height )
865 && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
867 /* Cool ! We have direct buffers, we can ask the decoder to
868 * directly decode into them ! Map the first render buffers to
869 * the first direct buffers, but keep the first direct buffer
870 * for memcpy operations */
871 p_vout->p->b_direct = true;
873 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
875 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
876 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
877 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
879 /* We have enough direct buffers so there's no need to
880 * try to use system memory buffers. */
883 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
887 msg_Dbg( p_vout, "direct render, mapping "
888 "render pictures 0-%i to system pictures 1-%i",
889 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
893 /* Rats... Something is wrong here, we could not find an output
894 * plugin able to directly render what we decode. See if we can
895 * find a chroma plugin to do the conversion */
896 p_vout->p->b_direct = false;
898 if( ChromaCreate( p_vout ) )
900 p_vout->pf_end( p_vout );
904 msg_Dbg( p_vout, "indirect render, mapping "
905 "render pictures 0-%i to system pictures %i-%i",
906 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
907 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
909 /* Append render buffers after the direct buffers */
910 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
912 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
915 /* Check if we have enough render pictures */
916 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
921 /* Link pictures back to their heap */
922 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
924 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
927 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
929 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
935 /*****************************************************************************
936 * RunThread: video output thread
937 *****************************************************************************
938 * Video output thread. This function does only returns when the thread is
939 * terminated. It handles the pictures arriving in the video heap and the
940 * display device events.
941 *****************************************************************************/
942 static void* RunThread( vlc_object_t *p_this )
944 vout_thread_t *p_vout = (vout_thread_t *)p_this;
945 int i_idle_loops = 0; /* loops without displaying a picture */
949 int canc = vlc_savecancel();
954 vlc_mutex_lock( &p_vout->change_lock );
955 p_vout->b_error = InitThread( p_vout );
957 b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
959 /* signal the creation of the vout */
960 vlc_thread_ready( p_vout );
962 if( p_vout->b_error )
965 vlc_mutex_unlock( &p_vout->change_lock );
966 vlc_restorecancel( canc );
970 vlc_object_lock( p_vout );
972 if( p_vout->p->b_title_show )
973 DisplayTitleOnOSD( p_vout );
976 * Main loop - it is not executed if an error occurred during
979 while( vlc_object_alive( p_vout ) && !p_vout->b_error )
981 /* Initialize loop variables */
982 const mtime_t current_date = mdate();
983 picture_t *p_picture;
984 picture_t *p_filtered_picture;
985 mtime_t display_date;
986 picture_t *p_directbuffer;
989 vlc_mutex_lock( &p_vout->picture_lock );
991 /* Look for the earliest picture but after the last displayed one */
992 picture_t *p_last = p_vout->p->p_picture_displayed;;
995 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
997 picture_t *p_pic = PP_RENDERPICTURE[i_index];
999 if( p_pic->i_status != READY_PICTURE )
1002 if( p_vout->p->b_paused && p_last && p_last->date > 1 )
1005 if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
1007 /* Drop old picture */
1008 vout_UsePictureLocked( p_vout, p_pic );
1010 else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
1011 p_pic->date < current_date + p_vout->p->render_time &&
1014 /* Picture is late: it will be destroyed and the thread
1015 * will directly choose the next picture */
1016 vout_UsePictureLocked( p_vout, p_pic );
1017 p_vout->p->i_picture_lost++;
1019 msg_Warn( p_vout, "late picture skipped (%"PRId64")",
1020 current_date - p_pic->date );
1022 else if( ( !p_last || p_last->date < p_pic->date ) &&
1023 ( p_picture == NULL || p_pic->date < p_picture->date ) )
1032 if( !p_vout->p->b_picture_empty )
1034 p_vout->p->b_picture_empty = true;
1035 vlc_cond_signal( &p_vout->p->picture_wait );
1042 display_date = p_picture->date;
1044 /* If we found better than the last picture, destroy it */
1045 if( p_last && p_picture != p_last )
1047 vout_UsePictureLocked( p_vout, p_last );
1048 p_vout->p->p_picture_displayed = p_last = NULL;
1051 /* Compute FPS rate */
1052 p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1054 if( display_date > current_date + VOUT_DISPLAY_DELAY && !p_vout->p->b_paused )
1056 /* A picture is ready to be rendered, but its rendering date
1057 * is far from the current one so the thread will perform an
1058 * empty loop as if no picture were found. The picture state
1063 else if( p_picture == p_last )
1065 /* We are asked to repeat the previous picture, but we first
1066 * wait for a couple of idle loops */
1067 if( i_idle_loops < 4 )
1074 /* We set the display date to something high, otherwise
1075 * we'll have lots of problems with late pictures */
1076 display_date = current_date + p_vout->p->render_time;
1081 if( p_picture->date > 1 )
1083 p_vout->p->i_picture_displayed_date = p_picture->date;
1084 if( p_picture != p_last && !p_vout->p->b_picture_displayed )
1086 p_vout->p->b_picture_displayed = true;
1087 vlc_cond_signal( &p_vout->p->picture_wait );
1090 p_vout->p->p_picture_displayed = p_picture;
1093 vlc_mutex_unlock( &p_vout->picture_lock );
1095 if( p_picture == NULL )
1098 p_filtered_picture = NULL;
1100 p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
1103 /* FIXME it is a bit ugly that b_snapshot is not locked but I do not
1104 * know which lock to use (here and in the snapshot callback) */
1105 const bool b_snapshot = p_vout->p->b_snapshot;
1107 p_vout->p->b_snapshot = false;
1110 * Check for subpictures to display
1112 subpicture_t *p_subpic = NULL;
1113 if( display_date > 0 )
1114 p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
1115 p_vout->p->b_paused, b_snapshot );
1120 p_vout->p->i_picture_displayed++;
1121 p_directbuffer = vout_RenderPicture( p_vout, p_filtered_picture,
1122 p_subpic, p_vout->p->b_paused );
1125 * Take a snapshot if requested
1127 if( p_directbuffer && b_snapshot )
1128 vout_Snapshot( p_vout, p_directbuffer );
1131 * Call the plugin-specific rendering method if there is one
1133 if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
1135 /* Render the direct buffer returned by vout_RenderPicture */
1136 p_vout->pf_render( p_vout, p_directbuffer );
1142 if( display_date != 0 && p_directbuffer != NULL )
1144 mtime_t current_render_time = mdate() - current_date;
1145 /* if render time is very large we don't include it in the mean */
1146 if( current_render_time < p_vout->p->render_time +
1147 VOUT_DISPLAY_DELAY )
1149 /* Store render time using a sliding mean weighting to
1150 * current value in a 3 to 1 ratio*/
1151 p_vout->p->render_time *= 3;
1152 p_vout->p->render_time += current_render_time;
1153 p_vout->p->render_time >>= 2;
1157 /* Give back change lock */
1158 vlc_mutex_unlock( &p_vout->change_lock );
1160 vlc_object_unlock( p_vout );
1162 /* Sleep a while or until a given date */
1163 if( display_date != 0 )
1165 /* If there are *vout* filters in the chain, better give them the picture
1167 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
1169 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1174 /* Wait until a frame is being sent or a supurious wakeup (not a problem here) */
1175 vlc_mutex_lock( &p_vout->picture_lock );
1176 vlc_cond_timedwait( &p_vout->p->picture_wait, &p_vout->picture_lock, current_date + VOUT_IDLE_SLEEP );
1177 vlc_mutex_unlock( &p_vout->picture_lock );
1180 /* On awakening, take back lock and send immediately picture
1182 vlc_object_lock( p_vout );
1183 /* Note: vlc_object_alive() could be false here, and we
1185 vlc_mutex_lock( &p_vout->change_lock );
1188 * Display the previously rendered picture
1190 if( p_filtered_picture != NULL && p_directbuffer != NULL )
1192 /* Display the direct buffer returned by vout_RenderPicture */
1193 if( p_vout->pf_display )
1194 p_vout->pf_display( p_vout, p_directbuffer );
1196 /* Tell the vout this was the last picture and that it does not
1197 * need to be forced anymore. */
1198 p_picture->b_force = false;
1201 /* Drop the filtered picture if created by video filters */
1202 if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
1204 vlc_mutex_lock( &p_vout->picture_lock );
1205 vout_UsePictureLocked( p_vout, p_filtered_picture );
1206 vlc_mutex_unlock( &p_vout->picture_lock );
1209 if( p_picture != NULL )
1211 /* Reinitialize idle loop count */
1216 * Check events and manage thread
1218 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1220 /* A fatal error occurred, and the thread must terminate
1221 * immediately, without displaying anything - setting b_error to 1
1222 * causes the immediate end of the main while() loop. */
1224 p_vout->b_error = 1;
1228 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1230 /* this must only happen when the vout plugin is incapable of
1231 * rescaling the picture itself. In this case we need to destroy
1232 * the current picture buffers and recreate new ones with the right
1236 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1238 assert( !p_vout->p->b_direct );
1240 ChromaDestroy( p_vout );
1242 vlc_mutex_lock( &p_vout->picture_lock );
1244 p_vout->pf_end( p_vout );
1246 p_vout->p->p_picture_displayed = NULL;
1247 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1248 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1249 vlc_cond_signal( &p_vout->p->picture_wait );
1251 I_OUTPUTPICTURES = 0;
1253 if( p_vout->pf_init( p_vout ) )
1255 msg_Err( p_vout, "cannot resize display" );
1256 /* FIXME: pf_end will be called again in EndThread() */
1257 p_vout->b_error = 1;
1260 vlc_mutex_unlock( &p_vout->picture_lock );
1262 /* Need to reinitialise the chroma plugin. Since we might need
1263 * resizing too and it's not sure that we already had it,
1264 * recreate the chroma plugin chain from scratch. */
1266 if( ChromaCreate( p_vout ) )
1268 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1269 p_vout->b_error = 1;
1271 if( p_vout->b_error )
1275 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1277 /* This happens when the picture buffers need to be recreated.
1278 * This is useful on multimonitor displays for instance.
1280 * Warning: This only works when the vout creates only 1 picture
1282 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1284 if( !p_vout->p->b_direct )
1285 ChromaDestroy( p_vout );
1287 vlc_mutex_lock( &p_vout->picture_lock );
1289 p_vout->pf_end( p_vout );
1291 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1293 p_vout->b_error = InitThread( p_vout );
1294 if( p_vout->b_error )
1295 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed\n" );
1297 vlc_cond_signal( &p_vout->p->picture_wait );
1298 vlc_mutex_unlock( &p_vout->picture_lock );
1300 if( p_vout->b_error )
1304 /* Check for "video filter2" changes */
1305 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1306 if( p_vout->p->psz_vf2 )
1310 es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1311 fmt.video = p_vout->fmt_render;
1312 filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
1314 if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
1315 p_vout->p->psz_vf2 ) < 0 )
1316 msg_Err( p_vout, "Video filter chain creation failed" );
1318 free( p_vout->p->psz_vf2 );
1319 p_vout->p->psz_vf2 = NULL;
1321 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1325 * Error loop - wait until the thread destruction is requested
1327 if( p_vout->b_error )
1328 ErrorThread( p_vout );
1331 CleanThread( p_vout );
1332 EndThread( p_vout );
1333 vlc_mutex_unlock( &p_vout->change_lock );
1335 vlc_object_unlock( p_vout );
1336 vlc_restorecancel( canc );
1340 /*****************************************************************************
1341 * ErrorThread: RunThread() error loop
1342 *****************************************************************************
1343 * This function is called when an error occurred during thread main's loop.
1344 * The thread can still receive feed, but must be ready to terminate as soon
1346 *****************************************************************************/
1347 static void ErrorThread( vout_thread_t *p_vout )
1349 /* Wait until a `die' order */
1350 while( vlc_object_alive( p_vout ) )
1351 vlc_object_wait( p_vout );
1354 /*****************************************************************************
1355 * CleanThread: clean up after InitThread
1356 *****************************************************************************
1357 * This function is called after a sucessful
1358 * initialization. It frees all resources allocated by InitThread.
1359 * XXX You have to enter it with change_lock taken.
1360 *****************************************************************************/
1361 static void CleanThread( vout_thread_t *p_vout )
1363 int i_index; /* index in heap */
1365 if( !p_vout->p->b_direct )
1366 ChromaDestroy( p_vout );
1368 /* Destroy all remaining pictures */
1369 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1371 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1373 free( p_vout->p_picture[i_index].p_data_orig );
1377 /* Destroy translation tables */
1378 if( !p_vout->b_error )
1379 p_vout->pf_end( p_vout );
1382 /*****************************************************************************
1383 * EndThread: thread destruction
1384 *****************************************************************************
1385 * This function is called when the thread ends.
1386 * It frees all resources not allocated by InitThread.
1387 * XXX You have to enter it with change_lock taken.
1388 *****************************************************************************/
1389 static void EndThread( vout_thread_t *p_vout )
1393 struct tms cpu_usage;
1394 times( &cpu_usage );
1396 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1397 cpu_usage.tms_utime, cpu_usage.tms_stime );
1401 /* FIXME does that function *really* need to be called inside the thread ? */
1403 /* Destroy subpicture unit */
1404 spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1406 /* Destroy the video filters2 */
1407 filter_chain_Delete( p_vout->p->p_vf2_chain );
1410 /* Thread helpers */
1411 static picture_t *ChromaGetPicture( filter_t *p_filter )
1413 picture_t *p_pic = (picture_t *)p_filter->p_owner;
1414 p_filter->p_owner = NULL;
1418 static int ChromaCreate( vout_thread_t *p_vout )
1420 static const char typename[] = "chroma";
1423 /* Choose the best module */
1424 p_chroma = p_vout->p->p_chroma =
1425 vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
1428 vlc_object_attach( p_chroma, p_vout );
1430 /* TODO: Set the fmt_in and fmt_out stuff here */
1431 p_chroma->fmt_in.video = p_vout->fmt_render;
1432 p_chroma->fmt_out.video = p_vout->fmt_out;
1433 VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
1434 VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
1436 p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, 0 );
1438 if( p_chroma->p_module == NULL )
1440 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1441 (char*)&p_vout->render.i_chroma,
1442 (char*)&p_vout->output.i_chroma,
1443 p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1444 p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1447 vlc_object_release( p_vout->p->p_chroma );
1448 p_vout->p->p_chroma = NULL;
1450 return VLC_EGENERIC;
1452 p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1456 static void ChromaDestroy( vout_thread_t *p_vout )
1458 assert( !p_vout->p->b_direct );
1460 if( !p_vout->p->p_chroma )
1463 module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
1464 vlc_object_release( p_vout->p->p_chroma );
1465 p_vout->p->p_chroma = NULL;
1468 /* following functions are local */
1469 static int ReduceHeight( int i_ratio )
1471 int i_dummy = VOUT_ASPECT_FACTOR;
1479 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1480 while( !(i_ratio & 1) && !(i_dummy & 1) )
1487 while( !(i_ratio % 3) && !(i_dummy % 3) )
1494 while( !(i_ratio % 5) && !(i_dummy % 5) )
1504 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1506 unsigned int i_pgcd = ReduceHeight( i_aspect );
1507 *i_aspect_x = i_aspect / i_pgcd;
1508 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1512 * This function copies all RGB informations from a picture_heap_t into
1515 static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
1517 p_fmt->i_rmask = p_heap->i_rmask;
1518 p_fmt->i_gmask = p_heap->i_gmask;
1519 p_fmt->i_bmask = p_heap->i_bmask;
1520 p_fmt->i_rrshift = p_heap->i_rrshift;
1521 p_fmt->i_lrshift = p_heap->i_lrshift;
1522 p_fmt->i_rgshift = p_heap->i_rgshift;
1523 p_fmt->i_lgshift = p_heap->i_lgshift;
1524 p_fmt->i_rbshift = p_heap->i_rbshift;
1525 p_fmt->i_lbshift = p_heap->i_lbshift;
1529 * This funtion copes all RGB informations from a video_format_t into
1532 static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
1534 p_heap->i_rmask = p_fmt->i_rmask;
1535 p_heap->i_gmask = p_fmt->i_gmask;
1536 p_heap->i_bmask = p_fmt->i_bmask;
1537 p_heap->i_rrshift = p_fmt->i_rrshift;
1538 p_heap->i_lrshift = p_fmt->i_lrshift;
1539 p_heap->i_rgshift = p_fmt->i_rgshift;
1540 p_heap->i_lgshift = p_fmt->i_lgshift;
1541 p_heap->i_rbshift = p_fmt->i_rbshift;
1542 p_heap->i_lbshift = p_fmt->i_lbshift;
1546 * This function computes rgb shifts from masks
1548 static void PictureHeapFixRgb( picture_heap_t *p_heap )
1553 fmt.i_chroma = p_heap->i_chroma;
1554 VideoFormatImportRgb( &fmt, p_heap );
1557 video_format_FixRgb( &fmt );
1559 VideoFormatExportRgb( &fmt, p_heap );
1562 /*****************************************************************************
1563 * object variables callbacks: a bunch of object variables are used by the
1564 * interfaces to interact with the vout.
1565 *****************************************************************************/
1566 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1567 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1569 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1570 input_thread_t *p_input;
1573 char *psz_mode = newval.psz_string;
1574 char *psz_filter, *psz_deinterlace = NULL;
1575 (void)psz_cmd; (void)oldval; (void)p_data;
1577 var_Get( p_vout, "vout-filter", &val );
1578 psz_filter = val.psz_string;
1579 if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1581 if( !psz_mode || !*psz_mode )
1583 if( psz_deinterlace )
1585 char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1586 if( psz_src[0] == ':' ) psz_src++;
1587 memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1590 else if( !psz_deinterlace )
1592 psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1593 sizeof(":deinterlace") );
1597 strcat( psz_filter, ":" );
1598 strcat( psz_filter, "deinterlace" );
1602 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1607 return VLC_EGENERIC;
1610 if( psz_mode && *psz_mode )
1612 /* Modify input as well because the vout might have to be restarted */
1613 val.psz_string = psz_mode;
1614 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1615 var_Set( p_input, "deinterlace-mode", val );
1617 vlc_object_release( p_input );
1620 var_Set( p_vout, "intf-change", val );
1622 val.psz_string = psz_filter;
1623 var_Set( p_vout, "vout-filter", val );
1629 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1630 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1632 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1633 input_thread_t *p_input;
1635 (void)psz_cmd; (void)oldval; (void)p_data;
1637 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1641 msg_Err( p_vout, "Input not found" );
1642 return( VLC_EGENERIC );
1646 var_Set( p_vout, "intf-change", val );
1648 /* Modify input as well because the vout might have to be restarted */
1649 val.psz_string = newval.psz_string;
1650 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1652 var_Set( p_input, "vout-filter", val );
1654 /* Now restart current video stream */
1655 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1656 vlc_object_release( p_input );
1661 /*****************************************************************************
1662 * Video Filter2 stuff
1663 *****************************************************************************/
1664 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1665 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1667 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1668 (void)psz_cmd; (void)oldval; (void)p_data;
1670 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1671 p_vout->p->psz_vf2 = strdup( newval.psz_string );
1672 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1677 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1679 input_thread_t *p_input;
1680 mtime_t i_now, i_stop;
1682 if( !config_GetInt( p_vout, "osd" ) ) return;
1684 p_input = (input_thread_t *)vlc_object_find( p_vout,
1685 VLC_OBJECT_INPUT, FIND_ANYWHERE );
1689 i_stop = i_now + (mtime_t)(p_vout->p->i_title_timeout * 1000);
1690 char *psz_nowplaying =
1691 input_item_GetNowPlaying( input_GetItem( p_input ) );
1692 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
1693 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
1694 if( EMPTY_STR( psz_name ) )
1697 psz_name = input_item_GetName( input_GetItem( p_input ) );
1699 if( !EMPTY_STR( psz_nowplaying ) )
1701 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1702 psz_nowplaying, NULL,
1703 p_vout->p->i_title_position,
1704 30 + p_vout->fmt_in.i_width
1705 - p_vout->fmt_in.i_visible_width
1706 - p_vout->fmt_in.i_x_offset,
1707 20 + p_vout->fmt_in.i_y_offset,
1710 else if( !EMPTY_STR( psz_artist ) )
1712 char *psz_string = NULL;
1713 if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
1715 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1717 p_vout->p->i_title_position,
1718 30 + p_vout->fmt_in.i_width
1719 - p_vout->fmt_in.i_visible_width
1720 - p_vout->fmt_in.i_x_offset,
1721 20 + p_vout->fmt_in.i_y_offset,
1728 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1730 p_vout->p->i_title_position,
1731 30 + p_vout->fmt_in.i_width
1732 - p_vout->fmt_in.i_visible_width
1733 - p_vout->fmt_in.i_x_offset,
1734 20 + p_vout->fmt_in.i_y_offset,
1737 vlc_object_release( p_input );
1740 free( psz_nowplaying );