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>
13 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
37 #include <vlc_common.h>
39 #include <stdlib.h> /* free() */
45 #include <vlc_filter.h>
49 #include <vlc_input.h>
50 #include "vout_pictures.h"
51 #include "vout_internal.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static void *Thread(void *);
57 static void vout_Destructor(vlc_object_t *);
59 /* Object variables callbacks */
60 static int FilterCallback( vlc_object_t *, char const *,
61 vlc_value_t, vlc_value_t, void * );
62 static int VideoFilter2Callback( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
66 static void PostProcessEnable( vout_thread_t * );
67 static void PostProcessDisable( vout_thread_t * );
68 static int PostProcessCallback( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static void DeinterlaceEnable( vout_thread_t *, bool is_interlaced );
72 static void DeinterlaceNeeded( vout_thread_t *, bool );
74 /* Display media title in OSD */
75 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
78 static void PrintVideoFormat(vout_thread_t *, const char *, const video_format_t *);
80 /* Maximum delay between 2 displayed pictures.
81 * XXX it is needed for now but should be removed in the long term.
83 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
86 * Late pictures having a delay higher than this value are thrashed.
88 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
90 /* Better be in advance when awakening than late... */
91 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
93 /*****************************************************************************
94 * Video Filter2 functions
95 *****************************************************************************/
96 static picture_t *video_new_buffer_filter( filter_t *p_filter )
98 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
99 return picture_pool_Get(p_vout->p->private_pool);
102 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
104 VLC_UNUSED(p_filter);
105 picture_Release(p_pic);
108 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
110 p_filter->pf_video_buffer_new = video_new_buffer_filter;
111 p_filter->pf_video_buffer_del = video_del_buffer_filter;
112 p_filter->p_owner = p_data; /* p_vout */
117 /*****************************************************************************
118 * vout_Request: find a video output thread, create one, or destroy one.
119 *****************************************************************************
120 * This function looks for a video output thread matching the current
121 * properties. If not found, it spawns a new one.
122 *****************************************************************************/
123 vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
124 video_format_t *p_fmt )
128 /* Video output is no longer used.
129 * TODO: support for reusing video outputs with proper _thread-safe_
130 * reference handling. */
132 vout_CloseAndRelease( p_vout );
136 /* If a video output was provided, lock it, otherwise look for one. */
139 vlc_object_hold( p_vout );
142 /* TODO: find a suitable unused video output */
144 /* If we now have a video output, check it has the right properties */
147 vlc_mutex_lock( &p_vout->p->change_lock );
149 /* We don't directly check for the "vout-filter" variable for obvious
150 * performance reasons. */
151 if( p_vout->p->b_filter_change )
153 char *psz_filter_chain = var_GetString( p_vout, "vout-filter" );
155 if( psz_filter_chain && !*psz_filter_chain )
157 free( psz_filter_chain );
158 psz_filter_chain = NULL;
160 if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
162 free( p_vout->p->psz_filter_chain );
163 p_vout->p->psz_filter_chain = NULL;
166 if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
168 p_vout->p->b_filter_change = false;
171 free( psz_filter_chain );
174 #warning "FIXME: Check RGB masks in vout_Request"
175 /* FIXME: check RGB masks */
176 if( p_vout->fmt_render.i_chroma != vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma ) ||
177 p_vout->fmt_render.i_width != p_fmt->i_width ||
178 p_vout->fmt_render.i_height != p_fmt->i_height ||
179 p_vout->p->b_filter_change )
181 vlc_mutex_unlock( &p_vout->p->change_lock );
183 /* We are not interested in this format, close this vout */
184 vout_CloseAndRelease( p_vout );
185 vlc_object_release( p_vout );
190 /* This video output is cool! Hijack it. */
191 /* Correct aspect ratio on change
192 * FIXME factorize this code with other aspect ration related code */
193 unsigned int i_sar_num;
194 unsigned int i_sar_den;
195 vlc_ureduce( &i_sar_num, &i_sar_den,
196 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
198 /* What's that, it does not seems to be used correcly everywhere */
199 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
201 i_sar_num *= p_vout->i_par_den;
202 i_sar_den *= p_vout->i_par_num;
206 if( i_sar_num > 0 && i_sar_den > 0 &&
207 ( i_sar_num != p_vout->fmt_render.i_sar_num ||
208 i_sar_den != p_vout->fmt_render.i_sar_den ) )
210 p_vout->fmt_in.i_sar_num = i_sar_num;
211 p_vout->fmt_in.i_sar_den = i_sar_den;
213 p_vout->fmt_render.i_sar_num = i_sar_num;
214 p_vout->fmt_render.i_sar_den = i_sar_den;
215 p_vout->p->i_changes |= VOUT_ASPECT_CHANGE;
217 vlc_mutex_unlock( &p_vout->p->change_lock );
219 vlc_object_release( p_vout );
224 msg_Dbg( p_this, "reusing provided vout" );
226 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
227 vlc_object_detach( p_vout );
229 vlc_object_attach( p_vout, p_this );
230 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
236 msg_Dbg( p_this, "no usable vout present, spawning one" );
238 p_vout = vout_Create( p_this, p_fmt );
244 /*****************************************************************************
245 * vout_Create: creates a new video output thread
246 *****************************************************************************
247 * This function creates a new video output thread, and returns a pointer
248 * to its description. On error, it returns NULL.
249 *****************************************************************************/
250 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
252 vout_thread_t *p_vout; /* thread descriptor */
256 config_chain_t *p_cfg;
260 if( p_fmt->i_width <= 0 || p_fmt->i_height <= 0 )
262 const vlc_fourcc_t i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma );
264 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
265 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
266 if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
269 /* Allocate descriptor */
270 static const char typename[] = "video output";
271 p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
277 p_vout->p = calloc( 1, sizeof(*p_vout->p) );
280 vlc_object_release( p_vout );
285 p_vout->fmt_render = *p_fmt; /* FIXME palette */
286 p_vout->fmt_in = *p_fmt; /* FIXME palette */
288 p_vout->fmt_render.i_chroma =
289 p_vout->fmt_in.i_chroma = i_chroma;
290 video_format_FixRgb( &p_vout->fmt_render );
291 video_format_FixRgb( &p_vout->fmt_in );
293 /* Initialize misc stuff */
294 p_vout->p->i_changes = 0;
295 p_vout->p->b_fullscreen = 0;
296 vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
297 vout_statistic_Init( &p_vout->p->statistic );
298 p_vout->p->b_filter_change = 0;
299 p_vout->p->i_par_num =
300 p_vout->p->i_par_den = 1;
301 p_vout->p->b_picture_empty = false;
302 p_vout->p->displayed.date = VLC_TS_INVALID;
303 p_vout->p->displayed.decoded = NULL;
304 p_vout->p->displayed.timestamp = VLC_TS_INVALID;
305 p_vout->p->displayed.qtype = QTYPE_NONE;
306 p_vout->p->displayed.is_interlaced = false;
308 p_vout->p->step.is_requested = false;
309 p_vout->p->step.last = VLC_TS_INVALID;
310 p_vout->p->step.timestamp = VLC_TS_INVALID;
312 p_vout->p->pause.is_on = false;
313 p_vout->p->pause.date = VLC_TS_INVALID;
315 p_vout->p->decoder_fifo = picture_fifo_New();
316 p_vout->p->decoder_pool = NULL;
318 vlc_mouse_Init( &p_vout->p->mouse );
320 vout_snapshot_Init( &p_vout->p->snapshot );
322 /* Initialize locks */
323 vlc_mutex_init( &p_vout->p->picture_lock );
324 vlc_cond_init( &p_vout->p->picture_wait );
325 vlc_mutex_init( &p_vout->p->change_lock );
326 vlc_mutex_init( &p_vout->p->vfilter_lock );
328 /* Mouse coordinates */
329 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
330 var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
331 var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
332 /* Mouse object (area of interest in a video filter) */
333 var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
335 /* Attach the new object now so we can use var inheritance below */
336 vlc_object_attach( p_vout, p_parent );
338 /* Initialize subpicture unit */
339 p_vout->p->p_spu = spu_Create( p_vout );
342 spu_Init( p_vout->p->p_spu );
344 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
346 p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
348 /* Take care of some "interface/control" related initialisations */
349 vout_IntfInit( p_vout );
351 /* Look for the default filter configuration */
352 p_vout->p->psz_filter_chain =
353 var_CreateGetStringCommand( p_vout, "vout-filter" );
355 /* Apply video filter2 objects on the first vout */
357 var_CreateGetStringCommand( p_vout, "video-filter" );
359 var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
360 p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
361 false, video_filter_buffer_allocation_init, NULL, p_vout );
363 /* Choose the video output module */
364 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
370 psz_parser = strdup( p_vout->p->psz_filter_chain );
371 p_vout->p->title.show = false;
374 /* Create the vout thread */
375 char *psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
378 p_vout->p->p_cfg = p_cfg;
380 /* Create a few object variables for interface interaction */
381 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
382 text.psz_string = _("Filters");
383 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
384 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
387 DeinterlaceEnable( p_vout, p_vout->p->displayed.is_interlaced );
389 if( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain )
392 if( asprintf( &psz_tmp, "%s,none", psz_name ) < 0 )
393 psz_tmp = strdup( "" );
397 p_vout->p->psz_module_name = psz_name;
400 vlc_object_set_destructor( p_vout, vout_Destructor );
403 vlc_cond_init( &p_vout->p->change_wait );
404 if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
405 VLC_THREAD_PRIORITY_OUTPUT ) )
407 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
408 spu_Destroy( p_vout->p->p_spu );
409 p_vout->p->p_spu = NULL;
410 vlc_object_release( p_vout );
414 vlc_mutex_lock( &p_vout->p->change_lock );
415 while( !p_vout->p->b_ready )
416 { /* We are (ab)using the same condition in opposite directions for
417 * b_ready and b_done. This works because of the strict ordering. */
418 assert( !p_vout->p->b_done );
419 vlc_cond_wait( &p_vout->p->change_wait, &p_vout->p->change_lock );
421 vlc_mutex_unlock( &p_vout->p->change_lock );
423 if( p_vout->p->b_error )
425 msg_Err( p_vout, "video output creation failed" );
426 vout_CloseAndRelease( p_vout );
433 /*****************************************************************************
434 * vout_Close: Close a vout created by vout_Create.
435 *****************************************************************************
436 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
437 * You should NEVER call it on vout not obtained through vout_Create
438 * (like with vout_Request or vlc_object_find.)
439 * You can use vout_CloseAndRelease() as a convenience method.
440 *****************************************************************************/
441 void vout_Close( vout_thread_t *p_vout )
445 vlc_mutex_lock( &p_vout->p->change_lock );
446 p_vout->p->b_done = true;
447 vlc_cond_signal( &p_vout->p->change_wait );
448 vlc_mutex_unlock( &p_vout->p->change_lock );
450 vout_snapshot_End( &p_vout->p->snapshot );
452 vlc_join( p_vout->p->thread, NULL );
456 static void vout_Destructor( vlc_object_t * p_this )
458 vout_thread_t *p_vout = (vout_thread_t *)p_this;
460 /* Make sure the vout was stopped first */
461 //assert( !p_vout->p_module );
463 free( p_vout->p->psz_module_name );
466 if( p_vout->p->p_spu )
467 spu_Destroy( p_vout->p->p_spu );
469 vout_chrono_Clean( &p_vout->p->render );
471 if( p_vout->p->decoder_fifo )
472 picture_fifo_Delete( p_vout->p->decoder_fifo );
473 assert( !p_vout->p->decoder_pool );
475 /* Destroy the locks */
476 vlc_cond_destroy( &p_vout->p->change_wait );
477 vlc_cond_destroy( &p_vout->p->picture_wait );
478 vlc_mutex_destroy( &p_vout->p->picture_lock );
479 vlc_mutex_destroy( &p_vout->p->change_lock );
480 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
483 vout_statistic_Clean( &p_vout->p->statistic );
486 vout_snapshot_Clean( &p_vout->p->snapshot );
489 free( p_vout->p->psz_filter_chain );
490 free( p_vout->p->title.value );
492 config_ChainDestroy( p_vout->p->p_cfg );
499 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
501 vlc_mutex_lock( &p_vout->p->change_lock );
503 assert( !p_vout->p->pause.is_on || !b_paused );
505 vlc_mutex_lock( &p_vout->p->picture_lock );
507 if( p_vout->p->pause.is_on )
509 const mtime_t i_duration = i_date - p_vout->p->pause.date;
511 if (p_vout->p->step.timestamp > VLC_TS_INVALID)
512 p_vout->p->step.timestamp += i_duration;
514 p_vout->p->step.last = p_vout->p->step.timestamp;
515 picture_fifo_OffsetDate( p_vout->p->decoder_fifo, i_duration );
516 if (p_vout->p->displayed.decoded)
517 p_vout->p->displayed.decoded->date += i_duration;
519 vlc_cond_signal( &p_vout->p->picture_wait );
520 vlc_mutex_unlock( &p_vout->p->picture_lock );
522 spu_OffsetSubtitleDate( p_vout->p->p_spu, i_duration );
527 p_vout->p->step.last = p_vout->p->step.timestamp;
528 vlc_mutex_unlock( &p_vout->p->picture_lock );
530 p_vout->p->pause.is_on = b_paused;
531 p_vout->p->pause.date = i_date;
533 vlc_mutex_unlock( &p_vout->p->change_lock );
536 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
538 vout_statistic_GetReset( &p_vout->p->statistic,
539 pi_displayed, pi_lost );
542 static void Flush(vout_thread_t *vout, mtime_t date, bool reset, bool below)
544 vlc_assert_locked(&vout->p->picture_lock);
545 vout->p->step.timestamp = VLC_TS_INVALID;
546 vout->p->step.last = VLC_TS_INVALID;
548 picture_t *last = vout->p->displayed.decoded;
551 picture_Release(last);
552 vout->p->displayed.decoded = NULL;
553 } else if (( below && last->date <= date) ||
554 (!below && last->date >= date)) {
555 vout->p->step.is_requested = true;
558 picture_fifo_Flush( vout->p->decoder_fifo, date, below );
561 void vout_Flush(vout_thread_t *vout, mtime_t date)
563 vlc_mutex_lock(&vout->p->picture_lock);
565 Flush(vout, date, false, false);
567 vlc_cond_signal(&vout->p->picture_wait);
568 vlc_mutex_unlock(&vout->p->picture_lock);
571 void vout_Reset(vout_thread_t *vout)
573 vlc_mutex_lock(&vout->p->picture_lock);
575 Flush(vout, INT64_MAX, true, true);
576 if (vout->p->decoder_pool)
577 picture_pool_NonEmpty(vout->p->decoder_pool, true);
578 vout->p->pause.is_on = false;
579 vout->p->pause.date = mdate();
581 vlc_cond_signal( &vout->p->picture_wait );
582 vlc_mutex_unlock(&vout->p->picture_lock);
585 void vout_FixLeaks( vout_thread_t *vout )
587 vlc_mutex_lock(&vout->p->picture_lock);
589 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
591 picture = picture_pool_Get(vout->p->decoder_pool);
595 picture_Release(picture);
596 /* Not all pictures has been displayed yet or some are
598 vlc_mutex_unlock(&vout->p->picture_lock);
602 /* There is no reason that no pictures are available, force one
603 * from the pool, becarefull with it though */
604 msg_Err(vout, "pictures leaked, trying to workaround");
607 picture_pool_NonEmpty(vout->p->decoder_pool, false);
609 vlc_cond_signal(&vout->p->picture_wait);
610 vlc_mutex_unlock(&vout->p->picture_lock);
612 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
614 vlc_mutex_lock(&vout->p->picture_lock);
616 vout->p->b_picture_empty = false;
617 vout->p->step.is_requested = true;
619 /* FIXME I highly doubt that it can works with only one cond_t FIXME */
620 vlc_cond_signal(&vout->p->picture_wait);
622 while (vout->p->step.is_requested && !vout->p->b_picture_empty)
623 vlc_cond_wait(&vout->p->picture_wait, &vout->p->picture_lock);
625 if (vout->p->step.last > VLC_TS_INVALID &&
626 vout->p->step.timestamp > vout->p->step.last) {
627 *duration = vout->p->step.timestamp - vout->p->step.last;
628 vout->p->step.last = vout->p->step.timestamp;
633 /* TODO advance subpicture by the duration ... */
634 vlc_mutex_unlock(&vout->p->picture_lock);
637 void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
641 if( !var_InheritBool( p_vout, "osd" ) )
644 vlc_mutex_lock( &p_vout->p->change_lock );
645 free( p_vout->p->title.value );
646 p_vout->p->title.value = strdup( psz_title );
647 vlc_mutex_unlock( &p_vout->p->change_lock );
650 spu_t *vout_GetSpu( vout_thread_t *p_vout )
652 return p_vout->p->p_spu;
655 /*****************************************************************************
656 * InitThread: initialize video output thread
657 *****************************************************************************
658 * This function is called from Thread and performs the second step of the
659 * initialization. It returns 0 on success. Note that the thread's flag are not
660 * modified inside this function.
661 * XXX You have to enter it with change_lock taken.
662 *****************************************************************************/
663 static int ThreadInit(vout_thread_t *vout)
665 /* Initialize output method, it allocates direct buffers for us */
666 if (vout_InitWrapper(vout))
668 assert(vout->p->decoder_pool);
670 vout->p->displayed.decoded = NULL;
672 /* print some usefull debug info about different vout formats
674 PrintVideoFormat(vout, "pic render", &vout->fmt_render);
675 PrintVideoFormat(vout, "pic in", &vout->fmt_in);
676 PrintVideoFormat(vout, "pic out", &vout->fmt_out);
678 assert(vout->fmt_out.i_width == vout->fmt_render.i_width &&
679 vout->fmt_out.i_height == vout->fmt_render.i_height &&
680 vout->fmt_out.i_chroma == vout->fmt_render.i_chroma);
684 /*****************************************************************************
685 * CleanThread: clean up after InitThread
686 *****************************************************************************
687 * This function is called after a sucessful
688 * initialization. It frees all resources allocated by InitThread.
689 * XXX You have to enter it with change_lock taken.
690 *****************************************************************************/
691 static void ThreadClean(vout_thread_t *vout)
693 /* Destroy translation tables */
694 if (!vout->p->b_error) {
695 picture_fifo_Flush(vout->p->decoder_fifo, INT64_MAX, false);
696 vout_EndWrapper(vout);
700 static int ThreadDisplayPicture(vout_thread_t *vout,
701 bool now, mtime_t *deadline)
703 int displayed_count = 0;
707 const mtime_t date = mdate();
708 const bool is_paused = vout->p->pause.is_on;
709 bool redisplay = is_paused && !now;
712 /* FIXME/XXX we must redisplay the last decoded picture (because
713 * of potential vout updated, or filters update or SPU update)
714 * For now a high update period is needed but it coulmd be removed
716 * - vout module emits events from theselves.
717 * - *and* SPU is modified to emit an event or a deadline when needed.
719 * So it will be done latter.
722 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
724 is_forced = peek->b_force || is_paused || now;
725 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
726 picture_Release(peek);
732 /* FIXME a better way for this delay is needed */
733 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
734 if (date_update > date || !vout->p->displayed.decoded) {
735 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
740 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
742 if (*deadline > VOUT_MWAIT_TOLERANCE)
743 *deadline -= VOUT_MWAIT_TOLERANCE;
745 /* If we are too early and can wait, do it */
746 if (date < *deadline && !now)
751 decoded = vout->p->displayed.decoded;
752 vout->p->displayed.decoded = NULL;
754 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
756 if (!is_forced && !vout->p->is_late_dropped) {
757 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
758 const mtime_t late = predicted - decoded->date;
760 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
761 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
762 msg_Warn(vout, "rejected picture because of render time");
764 picture_Release(decoded);
771 vout->p->displayed.is_interlaced = !decoded->b_progressive;
772 vout->p->displayed.qtype = decoded->i_qtype;
774 vout->p->displayed.timestamp = decoded->date;
777 if (vout->p->displayed.decoded)
778 picture_Release(vout->p->displayed.decoded);
779 picture_Hold(decoded);
780 vout->p->displayed.decoded = decoded;
783 vout_chrono_Start(&vout->p->render);
785 picture_t *filtered = NULL;
787 vlc_mutex_lock(&vout->p->vfilter_lock);
788 filtered = filter_chain_VideoFilter(vout->p->p_vf2_chain, decoded);
789 //assert(filtered == decoded); // TODO implement
790 vlc_mutex_unlock(&vout->p->vfilter_lock);
796 * Check for subpictures to display
798 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
799 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
800 if (vout->p->pause.is_on)
801 spu_render_time = vout->p->pause.date;
803 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
805 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
812 * - be sure to end up with a direct buffer.
813 * - blend subtitles, and in a fast access buffer
815 picture_t *direct = NULL;
817 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
819 if (vout->p->is_decoder_pool_slow)
820 render = picture_NewFromFormat(&vout->fmt_out);
821 else if (vout->p->decoder_pool != vout->p->display_pool)
822 render = picture_pool_Get(vout->p->display_pool);
824 render = picture_pool_Get(vout->p->private_pool);
827 picture_Copy(render, filtered);
829 spu_RenderSubpictures(vout->p->p_spu,
830 render, &vout->fmt_out,
831 subpic, &vout->fmt_in, spu_render_time);
833 if (vout->p->is_decoder_pool_slow) {
834 direct = picture_pool_Get(vout->p->display_pool);
836 picture_Copy(direct, render);
837 picture_Release(render);
842 picture_Release(filtered);
849 * Take a snapshot if requested
851 if (direct && do_snapshot)
852 vout_snapshot_Set(&vout->p->snapshot, &vout->fmt_out, direct);
854 /* Render the direct buffer returned by vout_RenderPicture */
856 vout_RenderWrapper(vout, direct);
858 vout_chrono_Stop(&vout->p->render);
863 msg_Info(vout, "render: avg %d ms var %d ms",
864 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
869 /* Wait the real date (for rendering jitter) */
871 mwait(decoded->date);
873 /* Display the direct buffer returned by vout_RenderPicture */
874 vout->p->displayed.date = mdate();
876 vout_DisplayWrapper(vout, direct);
882 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
883 if (displayed_count <= 0)
888 /*****************************************************************************
889 * Thread: video output thread
890 *****************************************************************************
891 * Video output thread. This function does only returns when the thread is
892 * terminated. It handles the pictures arriving in the video heap and the
893 * display device events.
894 *****************************************************************************/
895 static void *Thread(void *object)
897 vout_thread_t *vout = object;
903 has_wrapper = !vout_OpenWrapper(vout, vout->p->psz_module_name);
905 vlc_mutex_lock(&vout->p->change_lock);
908 vout->p->b_error = ThreadInit(vout);
910 vout->p->b_error = true;
912 /* signal the creation of the vout */
913 vout->p->b_ready = true;
914 vlc_cond_signal(&vout->p->change_wait);
916 if (vout->p->b_error)
920 bool last_picture_interlaced = false;
921 int last_picture_qtype = QTYPE_NONE;
922 mtime_t last_picture_interlaced_date = mdate();
925 * Main loop - it is not executed if an error occurred during
928 while (!vout->p->b_done && !vout->p->b_error) {
930 if(vout->p->title.show && vout->p->title.value)
931 DisplayTitleOnOSD(vout);
933 vlc_mutex_lock(&vout->p->picture_lock);
935 mtime_t deadline = VLC_TS_INVALID;
936 bool has_displayed = !ThreadDisplayPicture(vout, vout->p->step.is_requested, &deadline);
939 vout->p->step.timestamp = vout->p->displayed.timestamp;
940 if (vout->p->step.last <= VLC_TS_INVALID)
941 vout->p->step.last = vout->p->step.timestamp;
943 if (vout->p->step.is_requested) {
944 if (!has_displayed && !vout->p->b_picture_empty) {
945 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
947 picture_Release(peek);
949 vout->p->b_picture_empty = true;
950 vlc_cond_signal(&vout->p->picture_wait);
954 vout->p->step.is_requested = false;
955 vlc_cond_signal(&vout->p->picture_wait);
959 const int picture_qtype = vout->p->displayed.qtype;
960 const bool picture_interlaced = vout->p->displayed.is_interlaced;
962 vlc_mutex_unlock(&vout->p->picture_lock);
964 if (vout_ManageWrapper(vout)) {
965 /* A fatal error occurred, and the thread must terminate
966 * immediately, without displaying anything - setting b_error to 1
967 * causes the immediate end of the main while() loop. */
969 vout->p->b_error = true;
973 /* Post processing */
974 const int postproc_change = (picture_qtype != QTYPE_NONE) - (last_picture_qtype != QTYPE_NONE);
975 if (postproc_change == 1)
976 PostProcessEnable(vout);
977 else if (postproc_change == -1)
978 PostProcessDisable(vout);
980 last_picture_qtype = picture_qtype;
983 * Wait 30s before quiting interlacing mode */
984 const int interlacing_change = (!!picture_interlaced) - (!!last_picture_interlaced);
985 if ((interlacing_change == 1) ||
986 (interlacing_change == -1 && last_picture_interlaced_date + 30000000 < mdate())) {
987 DeinterlaceNeeded(vout, picture_interlaced);
988 last_picture_interlaced = picture_interlaced;
990 if (picture_interlaced)
991 last_picture_interlaced_date = mdate();
993 /* Check for "video filter2" changes */
994 vlc_mutex_lock(&vout->p->vfilter_lock);
995 if (vout->p->psz_vf2) {
998 es_format_Init(&fmt, VIDEO_ES, vout->fmt_render.i_chroma);
999 fmt.video = vout->fmt_render;
1000 filter_chain_Reset(vout->p->p_vf2_chain, &fmt, &fmt);
1002 if (filter_chain_AppendFromString(vout->p->p_vf2_chain,
1003 vout->p->psz_vf2) < 0)
1004 msg_Err(vout, "Video filter chain creation failed");
1006 free(vout->p->psz_vf2);
1007 vout->p->psz_vf2 = NULL;
1009 vlc_mutex_unlock(&vout->p->vfilter_lock);
1011 vlc_mutex_unlock(&vout->p->change_lock);
1013 if (deadline > VLC_TS_INVALID) {
1014 vlc_mutex_lock(&vout->p->picture_lock);
1015 vlc_cond_timedwait(&vout->p->picture_wait, &vout->p->picture_lock, deadline);
1016 vlc_mutex_unlock(&vout->p->picture_lock);
1019 vlc_mutex_lock(&vout->p->change_lock);
1023 * Error loop - wait until the thread destruction is requested
1025 * XXX I wonder if we should periodically clean the decoder_fifo
1026 * or have a way to prevent it filling up.
1028 while (vout->p->b_error && !vout->p->b_done)
1029 vlc_cond_wait(&vout->p->change_wait, &vout->p->change_lock);
1035 /* Detach subpicture unit from both input and vout */
1036 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
1037 vlc_object_detach(vout->p->p_spu);
1039 /* Destroy the video filters2 */
1040 filter_chain_Delete(vout->p->p_vf2_chain);
1042 vlc_mutex_unlock(&vout->p->change_lock);
1045 vout_CloseWrapper(vout);
1050 /*****************************************************************************
1051 * object variables callbacks: a bunch of object variables are used by the
1052 * interfaces to interact with the vout.
1053 *****************************************************************************/
1054 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1055 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1057 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1058 input_thread_t *p_input;
1059 (void)psz_cmd; (void)oldval; (void)p_data;
1061 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1065 msg_Err( p_vout, "Input not found" );
1066 return VLC_EGENERIC;
1069 var_SetBool( p_vout, "intf-change", true );
1071 /* Modify input as well because the vout might have to be restarted */
1072 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1073 var_SetString( p_input, "vout-filter", newval.psz_string );
1075 /* Now restart current video stream */
1076 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1077 vlc_object_release( p_input );
1082 /*****************************************************************************
1083 * Video Filter2 stuff
1084 *****************************************************************************/
1085 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1086 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1088 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1089 (void)psz_cmd; (void)oldval; (void)p_data;
1091 vlc_mutex_lock( &p_vout->p->vfilter_lock );
1092 p_vout->p->psz_vf2 = strdup( newval.psz_string );
1093 vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1098 /*****************************************************************************
1100 *****************************************************************************/
1101 static bool PostProcessIsPresent( const char *psz_filter )
1103 const char *psz_pp = "postproc";
1104 const size_t i_pp = strlen(psz_pp);
1105 return psz_filter &&
1106 !strncmp( psz_filter, psz_pp, strlen(psz_pp) ) &&
1107 ( psz_filter[i_pp] == '\0' || psz_filter[i_pp] == ':' );
1110 static int PostProcessCallback( vlc_object_t *p_this, char const *psz_cmd,
1111 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1113 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1114 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1116 static const char *psz_pp = "postproc";
1118 char *psz_vf2 = var_GetString( p_vout, "video-filter" );
1120 if( newval.i_int <= 0 )
1122 if( PostProcessIsPresent( psz_vf2 ) )
1124 strcpy( psz_vf2, &psz_vf2[strlen(psz_pp)] );
1125 if( *psz_vf2 == ':' )
1126 strcpy( psz_vf2, &psz_vf2[1] );
1131 if( !PostProcessIsPresent( psz_vf2 ) )
1135 char *psz_tmp = psz_vf2;
1136 if( asprintf( &psz_vf2, "%s:%s", psz_pp, psz_tmp ) < 0 )
1143 psz_vf2 = strdup( psz_pp );
1147 if( newval.i_int > 0 )
1148 var_SetInteger( p_vout, "postproc-q", newval.i_int );
1151 var_SetString( p_vout, "video-filter", psz_vf2 );
1154 else if( newval.i_int > 0 )
1156 var_TriggerCallback( p_vout, "video-filter" );
1160 static void PostProcessEnable( vout_thread_t *p_vout )
1163 msg_Dbg( p_vout, "Post-processing available" );
1164 var_Create( p_vout, "postprocess", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
1165 text.psz_string = _("Post processing");
1166 var_Change( p_vout, "postprocess", VLC_VAR_SETTEXT, &text, NULL );
1168 for( int i = 0; i <= 6; i++ )
1175 snprintf( psz_text, sizeof(psz_text), "%d", i );
1177 text.psz_string = _("Disable");
1179 text.psz_string = psz_text;
1180 var_Change( p_vout, "postprocess", VLC_VAR_ADDCHOICE, &val, &text );
1182 var_AddCallback( p_vout, "postprocess", PostProcessCallback, NULL );
1185 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1186 int i_postproc_q = 0;
1187 if( PostProcessIsPresent( psz_filter ) )
1188 i_postproc_q = var_CreateGetInteger( p_vout, "postproc-q" );
1190 var_SetInteger( p_vout, "postprocess", i_postproc_q );
1194 static void PostProcessDisable( vout_thread_t *p_vout )
1196 msg_Dbg( p_vout, "Post-processing no more available" );
1197 var_Destroy( p_vout, "postprocess" );
1200 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1202 const mtime_t i_start = mdate();
1203 const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->title.timeout;
1205 if( i_stop <= i_start )
1208 vlc_assert_locked( &p_vout->p->change_lock );
1210 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1211 p_vout->p->title.value, NULL,
1212 p_vout->p->title.position,
1213 30 + p_vout->fmt_in.i_width
1214 - p_vout->fmt_in.i_visible_width
1215 - p_vout->fmt_in.i_x_offset,
1216 20 + p_vout->fmt_in.i_y_offset,
1219 free( p_vout->p->title.value );
1221 p_vout->p->title.value = NULL;
1224 /*****************************************************************************
1226 *****************************************************************************/
1228 * You can use the non vout filter if and only if the video properties stay the
1229 * same (width/height/chroma/fps), at least for now.
1231 static const char *deinterlace_modes[] = {
1243 static bool DeinterlaceIsModeValid(const char *mode)
1245 for (unsigned i = 0; deinterlace_modes[i]; i++) {
1246 if( !strcmp(deinterlace_modes[i], mode))
1252 static char *FilterFind( char *psz_filter_base, const char *psz_module )
1254 const size_t i_module = strlen( psz_module );
1255 const char *psz_filter = psz_filter_base;
1257 if( !psz_filter || i_module <= 0 )
1262 char *psz_find = strstr( psz_filter, psz_module );
1265 if( psz_find[i_module] == '\0' || psz_find[i_module] == ':' )
1267 psz_filter = &psz_find[i_module];
1271 static bool DeinterlaceIsPresent( vout_thread_t *p_vout )
1273 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1275 bool b_found = FilterFind( psz_filter, "deinterlace" ) != NULL;
1282 static void DeinterlaceRemove( vout_thread_t *p_vout )
1284 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1286 char *psz = FilterFind( psz_filter, "deinterlace" );
1294 strcpy( &psz[0], &psz[strlen("deinterlace")] );
1296 strcpy( &psz[0], &psz[1] );
1298 var_SetString( p_vout, "video-filter", psz_filter );
1301 static void DeinterlaceAdd( vout_thread_t *p_vout )
1303 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1305 if( FilterFind( psz_filter, "deinterlace" ) )
1314 char *psz_tmp = psz_filter;
1315 if( asprintf( &psz_filter, "%s:%s", psz_tmp, "deinterlace" ) < 0 )
1316 psz_filter = psz_tmp;
1322 psz_filter = strdup( "deinterlace" );
1327 var_SetString( p_vout, "video-filter", psz_filter );
1332 static void DeinterlaceSave( vout_thread_t *p_vout, int i_deinterlace, const char *psz_mode )
1334 /* We have to set input variable to ensure restart support
1335 * FIXME to be removed when vout_Request does the right job.
1337 vlc_object_t *p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
1341 var_Create( p_input, "deinterlace", VLC_VAR_INTEGER );
1342 var_SetInteger( p_input, "deinterlace", i_deinterlace );
1344 static const char * const ppsz_variable[] = {
1346 "sout-deinterlace-mode",
1349 for( int i = 0; ppsz_variable[i]; i++ )
1351 var_Create( p_input, ppsz_variable[i], VLC_VAR_STRING );
1352 var_SetString( p_input, ppsz_variable[i], psz_mode );
1355 vlc_object_release( p_input );
1357 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1358 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1360 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1361 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1364 const int i_deinterlace = var_GetInteger( p_this, "deinterlace" );
1365 char *psz_mode = var_GetString( p_this, "deinterlace-mode" );
1366 const bool is_needed = var_GetBool( p_this, "deinterlace-needed" );
1367 if( !psz_mode || !DeinterlaceIsModeValid(psz_mode) )
1368 return VLC_EGENERIC;
1370 DeinterlaceSave( p_vout, i_deinterlace, psz_mode );
1373 char *psz_old = var_CreateGetString( p_vout, "sout-deinterlace-mode" );
1375 msg_Dbg( p_vout, "deinterlace %d, mode %s, is_needed %d", i_deinterlace, psz_mode, is_needed );
1376 if( i_deinterlace == 0 || ( i_deinterlace == -1 && !is_needed ) )
1378 DeinterlaceRemove( p_vout );
1380 else if( !DeinterlaceIsPresent( p_vout ) )
1382 DeinterlaceAdd( p_vout );
1384 else if( psz_old && strcmp( psz_old, psz_mode ) )
1386 var_TriggerCallback( p_vout, "video-filter" );
1395 static void DeinterlaceEnable( vout_thread_t *p_vout, bool is_interlaced )
1397 vlc_value_t val, text;
1399 msg_Dbg( p_vout, "Deinterlacing available" );
1401 /* Create the configuration variables */
1403 var_Create( p_vout, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_HASCHOICE );
1404 int i_deinterlace = var_GetInteger( p_vout, "deinterlace" );
1406 text.psz_string = _("Deinterlace");
1407 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
1409 const module_config_t *p_optd = config_FindConfig( VLC_OBJECT(p_vout), "deinterlace" );
1410 var_Change( p_vout, "deinterlace", VLC_VAR_CLEARCHOICES, NULL, NULL );
1411 for( int i = 0; p_optd && i < p_optd->i_list; i++ )
1413 val.i_int = p_optd->pi_list[i];
1414 text.psz_string = (char*)vlc_gettext(p_optd->ppsz_list_text[i]);
1415 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
1417 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
1419 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_HASCHOICE );
1420 char *psz_deinterlace = var_GetNonEmptyString( p_vout, "deinterlace-mode" );
1422 text.psz_string = _("Deinterlace mode");
1423 var_Change( p_vout, "deinterlace-mode", VLC_VAR_SETTEXT, &text, NULL );
1425 const module_config_t *p_optm = config_FindConfig( VLC_OBJECT(p_vout), "deinterlace-mode" );
1426 var_Change( p_vout, "deinterlace-mode", VLC_VAR_CLEARCHOICES, NULL, NULL );
1427 for( int i = 0; p_optm && i < p_optm->i_list; i++ )
1429 if( !DeinterlaceIsModeValid( p_optm->ppsz_list[i] ) )
1432 val.psz_string = p_optm->ppsz_list[i];
1433 text.psz_string = (char*)vlc_gettext(p_optm->ppsz_list_text[i]);
1434 var_Change( p_vout, "deinterlace-mode", VLC_VAR_ADDCHOICE, &val, &text );
1436 var_AddCallback( p_vout, "deinterlace-mode", DeinterlaceCallback, NULL );
1438 var_Create( p_vout, "deinterlace-needed", VLC_VAR_BOOL );
1439 var_AddCallback( p_vout, "deinterlace-needed", DeinterlaceCallback, NULL );
1441 /* Override the initial value from filters if present */
1442 char *psz_filter_mode = NULL;
1443 if( DeinterlaceIsPresent( p_vout ) )
1444 psz_filter_mode = var_CreateGetNonEmptyString( p_vout, "sout-deinterlace-mode" );
1445 if( psz_filter_mode )
1447 free( psz_deinterlace );
1448 if( i_deinterlace >= -1 )
1450 psz_deinterlace = psz_filter_mode;
1454 if( i_deinterlace < 0 )
1458 val.psz_string = psz_deinterlace ? psz_deinterlace : p_optm->orig.psz;
1459 var_Change( p_vout, "deinterlace-mode", VLC_VAR_SETVALUE, &val, NULL );
1460 val.b_bool = is_interlaced;
1461 var_Change( p_vout, "deinterlace-needed", VLC_VAR_SETVALUE, &val, NULL );
1463 var_SetInteger( p_vout, "deinterlace", i_deinterlace );
1464 free( psz_deinterlace );
1467 static void DeinterlaceNeeded( vout_thread_t *p_vout, bool is_interlaced )
1469 msg_Dbg( p_vout, "Detected %s video",
1470 is_interlaced ? "interlaced" : "progressive" );
1471 var_SetBool( p_vout, "deinterlace-needed", is_interlaced );
1475 static void PrintVideoFormat(vout_thread_t *vout,
1476 const char *description,
1477 const video_format_t *fmt)
1479 msg_Dbg(vout, "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
1481 fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
1482 fmt->i_visible_width, fmt->i_visible_height,
1483 (char*)&fmt->i_chroma,
1484 fmt->i_sar_num, fmt->i_sar_den,
1485 fmt->i_rmask, fmt->i_gmask, fmt->i_bmask);