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 ( void * );
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 * );
88 static void PostProcessEnable( vout_thread_t * );
89 static void PostProcessDisable( vout_thread_t * );
90 static void PostProcessSetFilterQuality( vout_thread_t *p_vout );
91 static int PostProcessCallback( vlc_object_t *, char const *,
92 vlc_value_t, vlc_value_t, void * );
94 /* From vout_intf.c */
95 int vout_Snapshot( vout_thread_t *, picture_t * );
97 /* Display media title in OSD */
98 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
100 /* Time during which the thread will sleep if it has nothing to
101 * display (in micro-seconds) */
102 #define VOUT_IDLE_SLEEP ((int)(0.020*CLOCK_FREQ))
104 /* Maximum lap of time allowed between the beginning of rendering and
105 * display. If, compared to the current date, the next image is too
106 * late, the thread will perform an idle loop. This time should be
107 * at least VOUT_IDLE_SLEEP plus the time required to render a few
108 * images, to avoid trashing of decoded images */
109 #define VOUT_DISPLAY_DELAY ((int)(0.200*CLOCK_FREQ))
111 /* Better be in advance when awakening than late... */
112 #define VOUT_MWAIT_TOLERANCE ((mtime_t)(0.020*CLOCK_FREQ))
114 /* Minimum number of direct pictures the video output will accept without
115 * creating additional pictures in system memory */
116 #ifdef OPTIMIZE_MEMORY
117 # define VOUT_MIN_DIRECT_PICTURES (VOUT_MAX_PICTURES/2)
119 # define VOUT_MIN_DIRECT_PICTURES (3*VOUT_MAX_PICTURES/4)
122 /*****************************************************************************
123 * Video Filter2 functions
124 *****************************************************************************/
125 static picture_t *video_new_buffer_filter( filter_t *p_filter )
127 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
128 picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
130 p_picture->i_status = READY_PICTURE;
135 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
137 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
139 vlc_mutex_lock( &p_vout->picture_lock );
140 vout_UsePictureLocked( p_vout, p_pic );
141 vlc_mutex_unlock( &p_vout->picture_lock );
144 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
146 p_filter->pf_vout_buffer_new = video_new_buffer_filter;
147 p_filter->pf_vout_buffer_del = video_del_buffer_filter;
148 p_filter->p_owner = p_data; /* p_vout */
152 /*****************************************************************************
153 * vout_Request: find a video output thread, create one, or destroy one.
154 *****************************************************************************
155 * This function looks for a video output thread matching the current
156 * properties. If not found, it spawns a new one.
157 *****************************************************************************/
158 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
159 video_format_t *p_fmt )
163 /* Video output is no longer used.
164 * TODO: support for reusing video outputs with proper _thread-safe_
165 * reference handling. */
167 vout_CloseAndRelease( p_vout );
171 /* If a video output was provided, lock it, otherwise look for one. */
174 vlc_object_hold( p_vout );
177 /* TODO: find a suitable unused video output */
179 /* If we now have a video output, check it has the right properties */
182 char *psz_filter_chain;
185 vlc_mutex_lock( &p_vout->change_lock );
187 /* We don't directly check for the "vout-filter" variable for obvious
188 * performance reasons. */
189 if( p_vout->p->b_filter_change )
191 var_Get( p_vout, "vout-filter", &val );
192 psz_filter_chain = val.psz_string;
194 if( psz_filter_chain && !*psz_filter_chain )
196 free( psz_filter_chain );
197 psz_filter_chain = NULL;
199 if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
201 free( p_vout->p->psz_filter_chain );
202 p_vout->p->psz_filter_chain = NULL;
205 if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
207 p_vout->p->b_filter_change = false;
210 free( psz_filter_chain );
213 if( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ||
214 p_vout->fmt_render.i_width != p_fmt->i_width ||
215 p_vout->fmt_render.i_height != p_fmt->i_height ||
216 p_vout->p->b_filter_change )
218 vlc_mutex_unlock( &p_vout->change_lock );
220 /* We are not interested in this format, close this vout */
221 vout_CloseAndRelease( p_vout );
222 vlc_object_release( p_vout );
227 /* This video output is cool! Hijack it. */
228 if( p_vout->fmt_render.i_aspect != p_fmt->i_aspect )
230 /* Correct aspect ratio on change
231 * FIXME factorize this code with other aspect ration related code */
232 unsigned int i_sar_num;
233 unsigned int i_sar_den;
234 unsigned int i_aspect;
236 i_aspect = p_fmt->i_aspect;
237 vlc_ureduce( &i_sar_num, &i_sar_den,
238 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
240 /* What's that, it does not seems to be used correcly everywhere
241 * beside the previous p_vout->fmt_render.i_aspect != p_fmt->i_aspect
242 * should be fixed to use it too then */
243 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
245 i_sar_num *= p_vout->i_par_den;
246 i_sar_den *= p_vout->i_par_num;
247 i_aspect = i_aspect * p_vout->i_par_den / p_vout->i_par_num;
251 if( i_sar_num > 0 && i_sar_den > 0 && i_aspect > 0 )
253 p_vout->fmt_in.i_sar_num = i_sar_num;
254 p_vout->fmt_in.i_sar_den = i_sar_den;
255 p_vout->fmt_in.i_aspect = i_aspect;
257 p_vout->fmt_render.i_sar_num = i_sar_num;
258 p_vout->fmt_render.i_sar_den = i_sar_den;
259 p_vout->fmt_render.i_aspect = i_aspect;
261 p_vout->render.i_aspect = i_aspect;
263 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
267 vlc_mutex_unlock( &p_vout->change_lock );
269 vlc_object_release( p_vout );
274 msg_Dbg( p_this, "reusing provided vout" );
276 spu_Attach( p_vout->p_spu, p_this, true );
278 vlc_object_detach( p_vout );
279 vlc_object_attach( p_vout, p_this );
285 msg_Dbg( p_this, "no usable vout present, spawning one" );
287 p_vout = vout_Create( p_this, p_fmt );
293 /*****************************************************************************
294 * vout_Create: creates a new video output thread
295 *****************************************************************************
296 * This function creates a new video output thread, and returns a pointer
297 * to its description. On error, it returns NULL.
298 *****************************************************************************/
299 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
301 vout_thread_t * p_vout; /* thread descriptor */
302 int i_index; /* loop variable */
303 vlc_value_t val, text;
305 unsigned int i_width = p_fmt->i_width;
306 unsigned int i_height = p_fmt->i_height;
307 vlc_fourcc_t i_chroma = p_fmt->i_chroma;
308 unsigned int i_aspect = p_fmt->i_aspect;
310 config_chain_t *p_cfg;
314 if( i_width <= 0 || i_height <= 0 || i_aspect <= 0 )
317 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
318 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
319 if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
322 /* Allocate descriptor */
323 static const char typename[] = "video output";
324 p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
330 p_vout->p = calloc( 1, sizeof(*p_vout->p) );
333 vlc_object_release( p_vout );
337 /* Initialize pictures - translation tables and functions
338 * will be initialized later in InitThread */
339 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
341 p_vout->p_picture[i_index].pf_lock = NULL;
342 p_vout->p_picture[i_index].pf_unlock = NULL;
343 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
344 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
345 p_vout->p_picture[i_index].b_slow = 0;
348 /* No images in the heap */
349 p_vout->i_heap_size = 0;
351 /* Initialize the rendering heap */
352 I_RENDERPICTURES = 0;
354 p_vout->fmt_render = *p_fmt; /* FIXME palette */
355 p_vout->fmt_in = *p_fmt; /* FIXME palette */
357 p_vout->render.i_width = i_width;
358 p_vout->render.i_height = i_height;
359 p_vout->render.i_chroma = i_chroma;
360 p_vout->render.i_aspect = i_aspect;
362 p_vout->render.i_rmask = p_fmt->i_rmask;
363 p_vout->render.i_gmask = p_fmt->i_gmask;
364 p_vout->render.i_bmask = p_fmt->i_bmask;
366 p_vout->render.i_last_used_pic = -1;
367 p_vout->render.b_allow_modify_pics = 1;
369 /* Zero the output heap */
370 I_OUTPUTPICTURES = 0;
371 p_vout->output.i_width = 0;
372 p_vout->output.i_height = 0;
373 p_vout->output.i_chroma = 0;
374 p_vout->output.i_aspect = 0;
376 p_vout->output.i_rmask = 0;
377 p_vout->output.i_gmask = 0;
378 p_vout->output.i_bmask = 0;
380 /* Initialize misc stuff */
381 p_vout->i_changes = 0;
382 p_vout->b_autoscale = 1;
383 p_vout->i_zoom = ZOOM_FP_FACTOR;
384 p_vout->b_fullscreen = 0;
385 p_vout->i_alignment = 0;
386 p_vout->p->render_time = 10;
387 p_vout->p->c_fps_samples = 0;
388 p_vout->p->i_picture_lost = 0;
389 p_vout->p->i_picture_displayed = 0;
390 p_vout->p->b_filter_change = 0;
391 p_vout->p->b_paused = false;
392 p_vout->p->i_pause_date = 0;
393 p_vout->pf_control = NULL;
394 p_vout->p->i_par_num =
395 p_vout->p->i_par_den = 1;
396 p_vout->p->p_picture_displayed = NULL;
397 p_vout->p->i_picture_displayed_date = 0;
398 p_vout->p->b_picture_displayed = false;
399 p_vout->p->b_picture_empty = false;
400 p_vout->p->i_picture_qtype = QTYPE_NONE;
402 /* Initialize locks */
403 vlc_mutex_init( &p_vout->picture_lock );
404 vlc_cond_init( &p_vout->p->picture_wait );
405 vlc_mutex_init( &p_vout->change_lock );
406 vlc_mutex_init( &p_vout->p->vfilter_lock );
408 /* Mouse coordinates */
409 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
410 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
411 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
412 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
413 var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
415 /* Initialize subpicture unit */
416 p_vout->p_spu = spu_Create( p_vout );
417 spu_Attach( p_vout->p_spu, p_parent, true );
419 /* Attach the new object now so we can use var inheritance below */
420 vlc_object_attach( p_vout, p_parent );
422 spu_Init( p_vout->p_spu );
424 /* Take care of some "interface/control" related initialisations */
425 vout_IntfInit( p_vout );
427 /* If the parent is not a VOUT object, that means we are at the start of
428 * the video output pipe */
429 if( vlc_internals( p_parent )->i_object_type != VLC_OBJECT_VOUT )
431 /* Look for the default filter configuration */
432 p_vout->p->psz_filter_chain =
433 var_CreateGetStringCommand( p_vout, "vout-filter" );
435 /* Apply video filter2 objects on the first vout */
437 var_CreateGetStringCommand( p_vout, "video-filter" );
441 /* continue the parent's filter chain */
444 /* Ugly hack to jump to our configuration chain */
445 p_vout->p->psz_filter_chain
446 = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
447 p_vout->p->psz_filter_chain
448 = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
449 config_ChainDestroy( p_cfg );
452 /* Create a video filter2 var ... but don't inherit values */
453 var_Create( p_vout, "video-filter",
454 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
455 p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
458 var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
459 p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
460 false, video_filter_buffer_allocation_init, NULL, p_vout );
462 /* Choose the video output module */
463 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
465 var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
466 var_Get( p_vout, "vout", &val );
467 psz_parser = val.psz_string;
471 psz_parser = strdup( p_vout->p->psz_filter_chain );
472 p_vout->p->b_title_show = false;
475 /* Create the vout thread */
476 char* psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
479 p_vout->p_cfg = p_cfg;
480 p_vout->p_module = module_need( p_vout,
481 ( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain ) ?
482 "video filter" : "video output", psz_name, p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain );
485 vlc_object_set_destructor( p_vout, vout_Destructor );
487 if( p_vout->p_module == NULL )
489 msg_Err( p_vout, "no suitable vout module" );
490 spu_Attach( p_vout->p_spu, p_vout, false );
491 spu_Destroy( p_vout->p_spu );
492 p_vout->p_spu = NULL;
493 vlc_object_release( p_vout );
497 /* Create a few object variables for interface interaction */
499 var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
500 text.psz_string = _("Deinterlace");
501 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
502 val.psz_string = (char *)""; text.psz_string = _("Disable");
503 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
504 val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
505 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
506 val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
507 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
508 val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
509 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
510 val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
511 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
512 val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
513 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
514 val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
515 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
517 if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
519 var_Set( p_vout, "deinterlace", val );
520 free( val.psz_string );
522 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
525 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
526 text.psz_string = _("Filters");
527 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
528 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
530 vlc_cond_init( &p_vout->p->change_wait );
531 if( vlc_clone( &p_vout->p->thread, RunThread, p_vout,
532 VLC_THREAD_PRIORITY_OUTPUT ) )
534 module_unneed( p_vout, p_vout->p_module );
535 p_vout->p_module = NULL;
536 vlc_object_release( p_vout );
540 vlc_mutex_lock( &p_vout->change_lock );
541 while( !p_vout->p->b_ready )
542 { /* We are (ab)using the same condition in opposite directions for
543 * b_ready and b_done. This works because of the strict ordering. */
544 assert( !p_vout->p->b_done );
545 vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
547 vlc_mutex_unlock( &p_vout->change_lock );
549 if( p_vout->b_error )
551 msg_Err( p_vout, "video output creation failed" );
552 vout_CloseAndRelease( p_vout );
559 /*****************************************************************************
560 * vout_Close: Close a vout created by vout_Create.
561 *****************************************************************************
562 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
563 * You should NEVER call it on vout not obtained through vout_Create
564 * (like with vout_Request or vlc_object_find.)
565 * You can use vout_CloseAndRelease() as a convenience method.
566 *****************************************************************************/
567 void vout_Close( vout_thread_t *p_vout )
571 vlc_mutex_lock( &p_vout->change_lock );
572 p_vout->p->b_done = true;
573 vlc_cond_signal( &p_vout->p->change_wait );
574 vlc_mutex_unlock( &p_vout->change_lock );
575 vlc_join( p_vout->p->thread, NULL );
576 module_unneed( p_vout, p_vout->p_module );
577 p_vout->p_module = NULL;
581 static void vout_Destructor( vlc_object_t * p_this )
583 vout_thread_t *p_vout = (vout_thread_t *)p_this;
585 /* Make sure the vout was stopped first */
586 assert( !p_vout->p_module );
590 spu_Destroy( p_vout->p_spu );
592 /* Destroy the locks */
593 vlc_cond_destroy( &p_vout->p->change_wait );
594 vlc_cond_destroy( &p_vout->p->picture_wait );
595 vlc_mutex_destroy( &p_vout->picture_lock );
596 vlc_mutex_destroy( &p_vout->change_lock );
597 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
599 free( p_vout->p->psz_filter_chain );
600 free( p_vout->p->psz_title );
602 config_ChainDestroy( p_vout->p_cfg );
607 vout_thread_t *p_another_vout;
609 /* This is a dirty hack mostly for Linux, where there is no way to get the
610 * GUI back if you closed it while playing video. This is solved in
611 * Mac OS X, where we have this novelty called menubar, that will always
612 * allow you access to the applications main functionality. They should try
613 * that on linux sometime. */
614 p_another_vout = vlc_object_find( p_this->p_libvlc,
615 VLC_OBJECT_VOUT, FIND_ANYWHERE );
616 if( p_another_vout == NULL )
617 var_SetBool( p_this->p_libvlc, "intf-show", true );
619 vlc_object_release( p_another_vout );
624 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
626 vlc_mutex_lock( &p_vout->change_lock );
628 assert( !p_vout->p->b_paused || !b_paused );
630 vlc_mutex_lock( &p_vout->picture_lock );
632 p_vout->p->i_picture_displayed_date = 0;
634 if( p_vout->p->b_paused )
636 const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
638 for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
640 picture_t *p_pic = PP_RENDERPICTURE[i_index];
642 if( p_pic->i_status == READY_PICTURE )
643 p_pic->date += i_duration;
645 vlc_cond_signal( &p_vout->p->picture_wait );
646 vlc_mutex_unlock( &p_vout->picture_lock );
648 spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
652 vlc_mutex_unlock( &p_vout->picture_lock );
654 p_vout->p->b_paused = b_paused;
655 p_vout->p->i_pause_date = i_date;
657 vlc_mutex_unlock( &p_vout->change_lock );
660 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
662 vlc_mutex_lock( &p_vout->change_lock );
664 *pi_displayed = p_vout->p->i_picture_displayed;
665 *pi_lost = p_vout->p->i_picture_lost;
667 p_vout->p->i_picture_displayed = 0;
668 p_vout->p->i_picture_lost = 0;
670 vlc_mutex_unlock( &p_vout->change_lock );
673 void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
675 vlc_mutex_lock( &p_vout->picture_lock );
676 p_vout->p->i_picture_displayed_date = 0;
677 for( int i = 0; i < p_vout->render.i_pictures; i++ )
679 picture_t *p_pic = p_vout->render.pp_picture[i];
681 if( p_pic->i_status == READY_PICTURE ||
682 p_pic->i_status == DISPLAYED_PICTURE )
684 /* We cannot change picture status if it is in READY_PICTURE state,
685 * Just make sure they won't be displayed */
686 if( p_pic->date > i_date )
687 p_pic->date = i_date;
690 vlc_cond_signal( &p_vout->p->picture_wait );
691 vlc_mutex_unlock( &p_vout->picture_lock );
694 void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
696 int i_pic, i_ready_pic;
698 vlc_mutex_lock( &p_vout->picture_lock );
700 for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
702 const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
704 if( p_pic->i_status == READY_PICTURE )
707 /* If we have at least 2 ready pictures, wait for the vout thread to
709 if( i_ready_pic >= 2 )
715 if( p_pic->i_status == DISPLAYED_PICTURE )
717 /* If at least one displayed picture is not referenced
718 * let vout free it */
719 if( p_pic->i_refcount == 0 )
723 if( i_pic < p_vout->render.i_pictures && !b_forced )
725 vlc_mutex_unlock( &p_vout->picture_lock );
729 /* Too many pictures are still referenced, there is probably a bug
730 * with the decoder */
732 msg_Err( p_vout, "pictures leaked, resetting the heap" );
734 /* Just free all the pictures */
735 for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
737 picture_t *p_pic = p_vout->render.pp_picture[i_pic];
739 msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
740 p_pic->i_refcount = 0;
742 switch( p_pic->i_status )
745 case DISPLAYED_PICTURE:
746 case RESERVED_PICTURE:
747 if( p_pic != p_vout->p->p_picture_displayed )
748 vout_UsePictureLocked( p_vout, p_pic );
752 vlc_cond_signal( &p_vout->p->picture_wait );
753 vlc_mutex_unlock( &p_vout->picture_lock );
755 void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
757 vlc_mutex_lock( &p_vout->picture_lock );
759 const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
761 p_vout->p->b_picture_displayed = false;
762 p_vout->p->b_picture_empty = false;
763 if( p_vout->p->p_picture_displayed )
765 p_vout->p->p_picture_displayed->date = 1;
766 vlc_cond_signal( &p_vout->p->picture_wait );
769 while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
770 vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->picture_lock );
772 *pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
774 /* TODO advance subpicture by the duration ... */
776 vlc_mutex_unlock( &p_vout->picture_lock );
779 void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
783 if( !config_GetInt( p_vout, "osd" ) )
786 vlc_mutex_lock( &p_vout->change_lock );
787 free( p_vout->p->psz_title );
788 p_vout->p->psz_title = strdup( psz_title );
789 vlc_mutex_unlock( &p_vout->change_lock );
792 /*****************************************************************************
793 * InitThread: initialize video output thread
794 *****************************************************************************
795 * This function is called from RunThread and performs the second step of the
796 * initialization. It returns 0 on success. Note that the thread's flag are not
797 * modified inside this function.
798 * XXX You have to enter it with change_lock taken.
799 *****************************************************************************/
800 static int ChromaCreate( vout_thread_t *p_vout );
801 static void ChromaDestroy( vout_thread_t *p_vout );
803 static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
805 if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
808 if( p_output->i_chroma != FOURCC_RV15 &&
809 p_output->i_chroma != FOURCC_RV16 &&
810 p_output->i_chroma != FOURCC_RV24 &&
811 p_output->i_chroma != FOURCC_RV32 )
814 return p_output->i_rmask == p_render->i_rmask &&
815 p_output->i_gmask == p_render->i_gmask &&
816 p_output->i_bmask == p_render->i_bmask;
819 static int InitThread( vout_thread_t *p_vout )
821 int i, i_aspect_x, i_aspect_y;
823 /* Initialize output method, it allocates direct buffers for us */
824 if( p_vout->pf_init( p_vout ) )
827 p_vout->p->p_picture_displayed = NULL;
829 if( !I_OUTPUTPICTURES )
831 msg_Err( p_vout, "plugin was unable to allocate at least "
832 "one direct buffer" );
833 p_vout->pf_end( p_vout );
837 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
839 msg_Err( p_vout, "plugin allocated too many direct buffers, "
840 "our internal buffers must have overflown." );
841 p_vout->pf_end( p_vout );
845 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
847 AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
849 AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
851 if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
853 p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
854 p_vout->output.i_width;
855 p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
856 p_vout->output.i_height;
857 p_vout->fmt_out.i_x_offset = p_vout->fmt_out.i_y_offset = 0;
859 p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
860 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
862 if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
864 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
865 p_vout->fmt_out.i_height;
866 p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
867 p_vout->fmt_out.i_width;
870 vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
871 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
873 AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
875 /* FIXME removed the need of both fmt_* and heap infos */
876 /* Calculate shifts from system-updated masks */
877 PictureHeapFixRgb( &p_vout->render );
878 VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
880 PictureHeapFixRgb( &p_vout->output );
881 VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
883 /* print some usefull debug info about different vout formats
885 msg_Dbg( p_vout, "pic render sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
886 p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
887 p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
888 p_vout->fmt_render.i_visible_width,
889 p_vout->fmt_render.i_visible_height,
890 (char*)&p_vout->fmt_render.i_chroma,
891 i_aspect_x, i_aspect_y,
892 p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den,
893 p_vout->fmt_render.i_rmask, p_vout->fmt_render.i_gmask, p_vout->fmt_render.i_bmask );
895 msg_Dbg( p_vout, "pic in sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
896 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
897 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
898 p_vout->fmt_in.i_visible_width,
899 p_vout->fmt_in.i_visible_height,
900 (char*)&p_vout->fmt_in.i_chroma,
901 i_aspect_x, i_aspect_y,
902 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den,
903 p_vout->fmt_in.i_rmask, p_vout->fmt_in.i_gmask, p_vout->fmt_in.i_bmask );
905 msg_Dbg( p_vout, "pic out sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
906 p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
907 p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
908 p_vout->fmt_out.i_visible_width,
909 p_vout->fmt_out.i_visible_height,
910 (char*)&p_vout->fmt_out.i_chroma,
911 i_aspect_x, i_aspect_y,
912 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den,
913 p_vout->fmt_out.i_rmask, p_vout->fmt_out.i_gmask, p_vout->fmt_out.i_bmask );
915 /* Check whether we managed to create direct buffers similar to
916 * the render buffers, ie same size and chroma */
917 if( ( p_vout->output.i_width == p_vout->render.i_width )
918 && ( p_vout->output.i_height == p_vout->render.i_height )
919 && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
921 /* Cool ! We have direct buffers, we can ask the decoder to
922 * directly decode into them ! Map the first render buffers to
923 * the first direct buffers, but keep the first direct buffer
924 * for memcpy operations */
925 p_vout->p->b_direct = true;
927 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
929 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
930 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
931 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
933 /* We have enough direct buffers so there's no need to
934 * try to use system memory buffers. */
937 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
941 msg_Dbg( p_vout, "direct render, mapping "
942 "render pictures 0-%i to system pictures 1-%i",
943 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
947 /* Rats... Something is wrong here, we could not find an output
948 * plugin able to directly render what we decode. See if we can
949 * find a chroma plugin to do the conversion */
950 p_vout->p->b_direct = false;
952 if( ChromaCreate( p_vout ) )
954 p_vout->pf_end( p_vout );
958 msg_Dbg( p_vout, "indirect render, mapping "
959 "render pictures 0-%i to system pictures %i-%i",
960 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
961 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
963 /* Append render buffers after the direct buffers */
964 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
966 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
969 /* Check if we have enough render pictures */
970 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
975 /* Link pictures back to their heap */
976 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
978 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
981 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
983 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
989 /*****************************************************************************
990 * RunThread: video output thread
991 *****************************************************************************
992 * Video output thread. This function does only returns when the thread is
993 * terminated. It handles the pictures arriving in the video heap and the
994 * display device events.
995 *****************************************************************************/
996 static void* RunThread( void *p_this )
998 vout_thread_t *p_vout = p_this;
999 int i_idle_loops = 0; /* loops without displaying a picture */
1000 int i_picture_qtype_last = QTYPE_NONE;
1007 vlc_mutex_lock( &p_vout->change_lock );
1008 p_vout->b_error = InitThread( p_vout );
1010 b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
1012 /* signal the creation of the vout */
1013 p_vout->p->b_ready = true;
1014 vlc_cond_signal( &p_vout->p->change_wait );
1016 if( p_vout->b_error )
1018 EndThread( p_vout );
1019 vlc_mutex_unlock( &p_vout->change_lock );
1024 * Main loop - it is not executed if an error occurred during
1027 while( !p_vout->p->b_done && !p_vout->b_error )
1029 /* Initialize loop variables */
1030 const mtime_t current_date = mdate();
1031 picture_t *p_picture;
1032 picture_t *p_filtered_picture;
1033 mtime_t display_date;
1034 picture_t *p_directbuffer;
1037 if( p_vout->p->b_title_show && p_vout->p->psz_title )
1038 DisplayTitleOnOSD( p_vout );
1040 vlc_mutex_lock( &p_vout->picture_lock );
1042 /* Look for the earliest picture but after the last displayed one */
1043 picture_t *p_last = p_vout->p->p_picture_displayed;;
1046 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
1048 picture_t *p_pic = PP_RENDERPICTURE[i_index];
1050 if( p_pic->i_status != READY_PICTURE )
1053 if( p_vout->p->b_paused && p_last && p_last->date > 1 )
1056 if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
1058 /* Drop old picture */
1059 vout_UsePictureLocked( p_vout, p_pic );
1061 else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
1062 p_pic->date < current_date + p_vout->p->render_time &&
1065 /* Picture is late: it will be destroyed and the thread
1066 * will directly choose the next picture */
1067 vout_UsePictureLocked( p_vout, p_pic );
1068 p_vout->p->i_picture_lost++;
1070 msg_Warn( p_vout, "late picture skipped (%"PRId64" > %d)",
1071 current_date - p_pic->date, - p_vout->p->render_time );
1073 else if( ( !p_last || p_last->date < p_pic->date ) &&
1074 ( p_picture == NULL || p_pic->date < p_picture->date ) )
1083 if( !p_vout->p->b_picture_empty )
1085 p_vout->p->b_picture_empty = true;
1086 vlc_cond_signal( &p_vout->p->picture_wait );
1093 display_date = p_picture->date;
1095 /* If we found better than the last picture, destroy it */
1096 if( p_last && p_picture != p_last )
1098 vout_UsePictureLocked( p_vout, p_last );
1099 p_vout->p->p_picture_displayed = p_last = NULL;
1102 /* Compute FPS rate */
1103 p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1105 if( !p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1107 /* A picture is ready to be rendered, but its rendering date
1108 * is far from the current one so the thread will perform an
1109 * empty loop as if no picture were found. The picture state
1114 else if( p_picture == p_last )
1116 /* We are asked to repeat the previous picture, but we first
1117 * wait for a couple of idle loops */
1118 if( i_idle_loops < 4 )
1125 /* We set the display date to something high, otherwise
1126 * we'll have lots of problems with late pictures */
1127 display_date = current_date + p_vout->p->render_time;
1130 else if( p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1132 display_date = current_date + VOUT_DISPLAY_DELAY;
1137 if( p_picture->date > 1 )
1139 p_vout->p->i_picture_displayed_date = p_picture->date;
1140 if( p_picture != p_last && !p_vout->p->b_picture_displayed )
1142 p_vout->p->b_picture_displayed = true;
1143 vlc_cond_signal( &p_vout->p->picture_wait );
1146 p_vout->p->p_picture_displayed = p_picture;
1151 const int i_postproc_type = p_vout->p->i_picture_qtype;
1152 const int i_postproc_state = (p_vout->p->i_picture_qtype != QTYPE_NONE) - (i_picture_qtype_last != QTYPE_NONE);
1154 vlc_mutex_unlock( &p_vout->picture_lock );
1156 if( p_picture == NULL )
1159 p_filtered_picture = NULL;
1161 p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
1164 /* FIXME it is ugly that b_snapshot is not locked but I do not
1165 * know which lock to use (here and in the snapshot callback) */
1166 const bool b_snapshot = p_vout->p->b_snapshot && p_picture != NULL;
1169 * Check for subpictures to display
1171 subpicture_t *p_subpic = NULL;
1172 if( display_date > 0 )
1173 p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
1174 p_vout->p->b_paused, b_snapshot );
1179 p_vout->p->i_picture_displayed++;
1180 p_directbuffer = vout_RenderPicture( p_vout, p_filtered_picture,
1181 p_subpic, p_vout->p->b_paused );
1184 * Take a snapshot if requested
1186 if( p_directbuffer && b_snapshot )
1188 /* FIXME lock (see b_snapshot) */
1189 p_vout->p->b_snapshot = false;
1191 vout_Snapshot( p_vout, p_directbuffer );
1195 * Call the plugin-specific rendering method if there is one
1197 if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
1199 /* Render the direct buffer returned by vout_RenderPicture */
1200 p_vout->pf_render( p_vout, p_directbuffer );
1206 if( display_date != 0 && p_directbuffer != NULL )
1208 mtime_t current_render_time = mdate() - current_date;
1209 /* if render time is very large we don't include it in the mean */
1210 if( current_render_time < p_vout->p->render_time +
1211 VOUT_DISPLAY_DELAY )
1213 /* Store render time using a sliding mean weighting to
1214 * current value in a 3 to 1 ratio*/
1215 p_vout->p->render_time *= 3;
1216 p_vout->p->render_time += current_render_time;
1217 p_vout->p->render_time >>= 2;
1220 msg_Dbg( p_vout, "skipped big render time %d > %d", (int) current_render_time,
1221 (int) (p_vout->p->render_time +VOUT_DISPLAY_DELAY ) ) ;
1224 /* Give back change lock */
1225 vlc_mutex_unlock( &p_vout->change_lock );
1227 /* Sleep a while or until a given date */
1228 if( display_date != 0 )
1230 /* If there are *vout* filters in the chain, better give them the picture
1232 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
1234 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1239 /* Wait until a frame is being sent or a spurious wakeup (not a problem here) */
1240 vlc_mutex_lock( &p_vout->picture_lock );
1241 vlc_cond_timedwait( &p_vout->p->picture_wait, &p_vout->picture_lock, current_date + VOUT_IDLE_SLEEP );
1242 vlc_mutex_unlock( &p_vout->picture_lock );
1245 /* On awakening, take back lock and send immediately picture
1247 /* Note: p_vout->p->b_done could be true here and now */
1248 vlc_mutex_lock( &p_vout->change_lock );
1251 * Display the previously rendered picture
1253 if( p_filtered_picture != NULL && p_directbuffer != NULL )
1255 /* Display the direct buffer returned by vout_RenderPicture */
1256 if( p_vout->pf_display )
1257 p_vout->pf_display( p_vout, p_directbuffer );
1259 /* Tell the vout this was the last picture and that it does not
1260 * need to be forced anymore. */
1261 p_picture->b_force = false;
1264 /* Drop the filtered picture if created by video filters */
1265 if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
1267 vlc_mutex_lock( &p_vout->picture_lock );
1268 vout_UsePictureLocked( p_vout, p_filtered_picture );
1269 vlc_mutex_unlock( &p_vout->picture_lock );
1272 if( p_picture != NULL )
1274 /* Reinitialize idle loop count */
1279 * Check events and manage thread
1281 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1283 /* A fatal error occurred, and the thread must terminate
1284 * immediately, without displaying anything - setting b_error to 1
1285 * causes the immediate end of the main while() loop. */
1287 p_vout->b_error = 1;
1291 while( p_vout->i_changes & VOUT_ON_TOP_CHANGE )
1293 p_vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
1294 vlc_mutex_unlock( &p_vout->change_lock );
1295 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, p_vout->b_on_top );
1296 vlc_mutex_lock( &p_vout->change_lock );
1299 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1301 /* this must only happen when the vout plugin is incapable of
1302 * rescaling the picture itself. In this case we need to destroy
1303 * the current picture buffers and recreate new ones with the right
1307 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1309 assert( !p_vout->p->b_direct );
1311 ChromaDestroy( p_vout );
1313 vlc_mutex_lock( &p_vout->picture_lock );
1315 p_vout->pf_end( p_vout );
1317 p_vout->p->p_picture_displayed = NULL;
1318 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1319 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1320 vlc_cond_signal( &p_vout->p->picture_wait );
1322 I_OUTPUTPICTURES = 0;
1324 if( p_vout->pf_init( p_vout ) )
1326 msg_Err( p_vout, "cannot resize display" );
1327 /* FIXME: pf_end will be called again in EndThread() */
1328 p_vout->b_error = 1;
1331 vlc_mutex_unlock( &p_vout->picture_lock );
1333 /* Need to reinitialise the chroma plugin. Since we might need
1334 * resizing too and it's not sure that we already had it,
1335 * recreate the chroma plugin chain from scratch. */
1337 if( ChromaCreate( p_vout ) )
1339 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1340 p_vout->b_error = 1;
1342 if( p_vout->b_error )
1346 if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1348 /* This happens when the picture buffers need to be recreated.
1349 * This is useful on multimonitor displays for instance.
1351 * Warning: This only works when the vout creates only 1 picture
1353 p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1355 if( !p_vout->p->b_direct )
1356 ChromaDestroy( p_vout );
1358 vlc_mutex_lock( &p_vout->picture_lock );
1360 p_vout->pf_end( p_vout );
1362 I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1364 p_vout->b_error = InitThread( p_vout );
1365 if( p_vout->b_error )
1366 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed" );
1368 vlc_cond_signal( &p_vout->p->picture_wait );
1369 vlc_mutex_unlock( &p_vout->picture_lock );
1371 if( p_vout->b_error )
1375 /* Post processing */
1376 if( i_postproc_state == 1 )
1377 PostProcessEnable( p_vout );
1378 else if( i_postproc_state == -1 )
1379 PostProcessDisable( p_vout );
1380 if( i_postproc_state != 0 )
1381 i_picture_qtype_last = i_postproc_type;
1383 /* Check for "video filter2" changes */
1384 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1385 if( p_vout->p->psz_vf2 )
1389 es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1390 fmt.video = p_vout->fmt_render;
1391 filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
1393 if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
1394 p_vout->p->psz_vf2 ) < 0 )
1395 msg_Err( p_vout, "Video filter chain creation failed" );
1397 free( p_vout->p->psz_vf2 );
1398 p_vout->p->psz_vf2 = NULL;
1400 if( i_picture_qtype_last != QTYPE_NONE )
1401 PostProcessSetFilterQuality( p_vout );
1403 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1407 * Error loop - wait until the thread destruction is requested
1409 if( p_vout->b_error )
1410 ErrorThread( p_vout );
1413 CleanThread( p_vout );
1414 EndThread( p_vout );
1415 vlc_mutex_unlock( &p_vout->change_lock );
1420 /*****************************************************************************
1421 * ErrorThread: RunThread() error loop
1422 *****************************************************************************
1423 * This function is called when an error occurred during thread main's loop.
1424 * The thread can still receive feed, but must be ready to terminate as soon
1426 *****************************************************************************/
1427 static void ErrorThread( vout_thread_t *p_vout )
1429 /* Wait until a `close' order */
1430 while( !p_vout->p->b_done )
1431 vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
1434 /*****************************************************************************
1435 * CleanThread: clean up after InitThread
1436 *****************************************************************************
1437 * This function is called after a sucessful
1438 * initialization. It frees all resources allocated by InitThread.
1439 * XXX You have to enter it with change_lock taken.
1440 *****************************************************************************/
1441 static void CleanThread( vout_thread_t *p_vout )
1443 int i_index; /* index in heap */
1445 if( !p_vout->p->b_direct )
1446 ChromaDestroy( p_vout );
1448 /* Destroy all remaining pictures */
1449 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1451 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1453 free( p_vout->p_picture[i_index].p_data_orig );
1457 /* Destroy translation tables */
1458 if( !p_vout->b_error )
1459 p_vout->pf_end( p_vout );
1462 /*****************************************************************************
1463 * EndThread: thread destruction
1464 *****************************************************************************
1465 * This function is called when the thread ends.
1466 * It frees all resources not allocated by InitThread.
1467 * XXX You have to enter it with change_lock taken.
1468 *****************************************************************************/
1469 static void EndThread( vout_thread_t *p_vout )
1473 struct tms cpu_usage;
1474 times( &cpu_usage );
1476 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1477 cpu_usage.tms_utime, cpu_usage.tms_stime );
1481 /* FIXME does that function *really* need to be called inside the thread ? */
1483 /* Detach subpicture unit from both input and vout */
1484 spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1485 vlc_object_detach( p_vout->p_spu );
1487 /* Destroy the video filters2 */
1488 filter_chain_Delete( p_vout->p->p_vf2_chain );
1491 /* Thread helpers */
1492 static picture_t *ChromaGetPicture( filter_t *p_filter )
1494 picture_t *p_pic = (picture_t *)p_filter->p_owner;
1495 p_filter->p_owner = NULL;
1499 static int ChromaCreate( vout_thread_t *p_vout )
1501 static const char typename[] = "chroma";
1504 /* Choose the best module */
1505 p_chroma = p_vout->p->p_chroma =
1506 vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
1509 vlc_object_attach( p_chroma, p_vout );
1511 /* TODO: Set the fmt_in and fmt_out stuff here */
1512 p_chroma->fmt_in.video = p_vout->fmt_render;
1513 p_chroma->fmt_out.video = p_vout->fmt_out;
1514 VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
1515 VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
1517 p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, false );
1519 if( p_chroma->p_module == NULL )
1521 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1522 (char*)&p_vout->render.i_chroma,
1523 (char*)&p_vout->output.i_chroma,
1524 p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1525 p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1528 vlc_object_release( p_vout->p->p_chroma );
1529 p_vout->p->p_chroma = NULL;
1531 return VLC_EGENERIC;
1533 p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1537 static void ChromaDestroy( vout_thread_t *p_vout )
1539 assert( !p_vout->p->b_direct );
1541 if( !p_vout->p->p_chroma )
1544 module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
1545 vlc_object_release( p_vout->p->p_chroma );
1546 p_vout->p->p_chroma = NULL;
1549 /* following functions are local */
1550 static int ReduceHeight( int i_ratio )
1552 int i_dummy = VOUT_ASPECT_FACTOR;
1560 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1561 while( !(i_ratio & 1) && !(i_dummy & 1) )
1568 while( !(i_ratio % 3) && !(i_dummy % 3) )
1575 while( !(i_ratio % 5) && !(i_dummy % 5) )
1585 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1587 unsigned int i_pgcd = ReduceHeight( i_aspect );
1588 *i_aspect_x = i_aspect / i_pgcd;
1589 *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1593 * This function copies all RGB informations from a picture_heap_t into
1596 static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
1598 p_fmt->i_rmask = p_heap->i_rmask;
1599 p_fmt->i_gmask = p_heap->i_gmask;
1600 p_fmt->i_bmask = p_heap->i_bmask;
1601 p_fmt->i_rrshift = p_heap->i_rrshift;
1602 p_fmt->i_lrshift = p_heap->i_lrshift;
1603 p_fmt->i_rgshift = p_heap->i_rgshift;
1604 p_fmt->i_lgshift = p_heap->i_lgshift;
1605 p_fmt->i_rbshift = p_heap->i_rbshift;
1606 p_fmt->i_lbshift = p_heap->i_lbshift;
1610 * This funtion copes all RGB informations from a video_format_t into
1613 static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
1615 p_heap->i_rmask = p_fmt->i_rmask;
1616 p_heap->i_gmask = p_fmt->i_gmask;
1617 p_heap->i_bmask = p_fmt->i_bmask;
1618 p_heap->i_rrshift = p_fmt->i_rrshift;
1619 p_heap->i_lrshift = p_fmt->i_lrshift;
1620 p_heap->i_rgshift = p_fmt->i_rgshift;
1621 p_heap->i_lgshift = p_fmt->i_lgshift;
1622 p_heap->i_rbshift = p_fmt->i_rbshift;
1623 p_heap->i_lbshift = p_fmt->i_lbshift;
1627 * This function computes rgb shifts from masks
1629 static void PictureHeapFixRgb( picture_heap_t *p_heap )
1634 fmt.i_chroma = p_heap->i_chroma;
1635 VideoFormatImportRgb( &fmt, p_heap );
1638 video_format_FixRgb( &fmt );
1640 VideoFormatExportRgb( &fmt, p_heap );
1643 /*****************************************************************************
1644 * object variables callbacks: a bunch of object variables are used by the
1645 * interfaces to interact with the vout.
1646 *****************************************************************************/
1647 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1648 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1650 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1651 input_thread_t *p_input;
1654 char *psz_mode = newval.psz_string;
1655 char *psz_filter, *psz_deinterlace = NULL;
1656 (void)psz_cmd; (void)oldval; (void)p_data;
1658 var_Get( p_vout, "vout-filter", &val );
1659 psz_filter = val.psz_string;
1660 if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1662 if( !psz_mode || !*psz_mode )
1664 if( psz_deinterlace )
1666 char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1667 if( psz_src[0] == ':' ) psz_src++;
1668 memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1671 else if( !psz_deinterlace )
1673 psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1674 sizeof(":deinterlace") );
1678 strcat( psz_filter, ":" );
1679 strcat( psz_filter, "deinterlace" );
1683 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1688 return VLC_EGENERIC;
1691 if( psz_mode && *psz_mode )
1693 /* Modify input as well because the vout might have to be restarted */
1694 val.psz_string = psz_mode;
1695 var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1696 var_Set( p_input, "deinterlace-mode", val );
1698 vlc_object_release( p_input );
1701 var_Set( p_vout, "intf-change", val );
1703 val.psz_string = psz_filter;
1704 var_Set( p_vout, "vout-filter", val );
1710 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1711 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1713 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1714 input_thread_t *p_input;
1716 (void)psz_cmd; (void)oldval; (void)p_data;
1718 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1722 msg_Err( p_vout, "Input not found" );
1723 return( VLC_EGENERIC );
1727 var_Set( p_vout, "intf-change", val );
1729 /* Modify input as well because the vout might have to be restarted */
1730 val.psz_string = newval.psz_string;
1731 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1733 var_Set( p_input, "vout-filter", val );
1735 /* Now restart current video stream */
1736 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1737 vlc_object_release( p_input );
1742 /*****************************************************************************
1743 * Video Filter2 stuff
1744 *****************************************************************************/
1745 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1746 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1748 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1749 (void)psz_cmd; (void)oldval; (void)p_data;
1751 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1752 p_vout->p->psz_vf2 = strdup( newval.psz_string );
1753 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1758 /*****************************************************************************
1760 *****************************************************************************/
1761 static bool PostProcessIsPresent( const char *psz_filter )
1763 const char *psz_pp = "postproc";
1764 const size_t i_pp = strlen(psz_pp);
1765 return psz_filter &&
1766 !strncmp( psz_filter, psz_pp, strlen(psz_pp) ) &&
1767 ( psz_filter[i_pp] == '\0' || psz_filter[i_pp] == ':' );
1770 static int PostProcessCallback( vlc_object_t *p_this, char const *psz_cmd,
1771 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1773 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1774 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1776 static const char *psz_pp = "postproc";
1778 char *psz_vf2 = var_GetString( p_vout, "video-filter" );
1780 if( newval.i_int <= 0 )
1782 if( PostProcessIsPresent( psz_vf2 ) )
1784 strcpy( psz_vf2, &psz_vf2[strlen(psz_pp)] );
1785 if( *psz_vf2 == ':' )
1786 strcpy( psz_vf2, &psz_vf2[1] );
1791 if( !PostProcessIsPresent( psz_vf2 ) )
1795 char *psz_tmp = psz_vf2;
1796 if( asprintf( &psz_vf2, "%s:%s", psz_pp, psz_tmp ) < 0 )
1803 psz_vf2 = strdup( psz_pp );
1808 var_SetString( p_vout, "video-filter", psz_vf2 );
1812 static void PostProcessEnable( vout_thread_t *p_vout )
1814 msg_Dbg( p_vout, "Post-processing available" );
1815 var_Create( p_vout, "postprocess", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
1816 for( int i = 0; i <= 6; i++ )
1823 snprintf( psz_text, sizeof(psz_text), "%d", i );
1825 text.psz_string = _("Disable");
1827 text.psz_string = psz_text;
1828 var_Change( p_vout, "postprocess", VLC_VAR_ADDCHOICE, &val, &text );
1830 var_AddCallback( p_vout, "postprocess", PostProcessCallback, NULL );
1833 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1834 int i_postproc_q = 0;
1835 if( PostProcessIsPresent( psz_filter ) )
1836 i_postproc_q = var_CreateGetInteger( p_vout, "postproc-q" );
1838 var_SetInteger( p_vout, "postprocess", i_postproc_q );
1842 static void PostProcessDisable( vout_thread_t *p_vout )
1844 msg_Dbg( p_vout, "Post-processing no more available" );
1845 var_Destroy( p_vout, "postprocess" );
1847 static void PostProcessSetFilterQuality( vout_thread_t *p_vout )
1849 vlc_object_t *p_pp = vlc_object_find_name( p_vout, "postproc", FIND_CHILD );
1853 var_SetInteger( p_pp, "postproc-q", var_GetInteger( p_vout, "postprocess" ) );
1854 vlc_object_release( p_pp );
1858 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1860 const mtime_t i_start = mdate();
1861 const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->i_title_timeout;
1863 vlc_assert_locked( &p_vout->change_lock );
1865 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1866 p_vout->p->psz_title, NULL,
1867 p_vout->p->i_title_position,
1868 30 + p_vout->fmt_in.i_width
1869 - p_vout->fmt_in.i_visible_width
1870 - p_vout->fmt_in.i_x_offset,
1871 20 + p_vout->fmt_in.i_y_offset,
1874 free( p_vout->p->psz_title );
1876 p_vout->p->psz_title = NULL;