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>
46 #include <vlc_vout_osd.h>
49 #include <vlc_input.h>
50 #include "vout_internal.h"
51 #include "interlacing.h"
52 #include "postprocessing.h"
55 /*****************************************************************************
57 *****************************************************************************/
58 static void *Thread(void *);
59 static void vout_Destructor(vlc_object_t *);
61 /* Object variables callbacks */
62 static int FilterCallback( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
64 static int VideoFilter2Callback( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
67 /* Maximum delay between 2 displayed pictures.
68 * XXX it is needed for now but should be removed in the long term.
70 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
73 * Late pictures having a delay higher than this value are thrashed.
75 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
77 /* Better be in advance when awakening than late... */
78 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
80 /*****************************************************************************
81 * Video Filter2 functions
82 *****************************************************************************/
83 static picture_t *video_new_buffer_filter( filter_t *p_filter )
85 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
86 return picture_pool_Get(p_vout->p->private_pool);
89 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
92 picture_Release(p_pic);
95 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
97 p_filter->pf_video_buffer_new = video_new_buffer_filter;
98 p_filter->pf_video_buffer_del = video_del_buffer_filter;
99 p_filter->p_owner = p_data; /* p_vout */
104 /*****************************************************************************
105 * vout_Request: find a video output thread, create one, or destroy one.
106 *****************************************************************************
107 * This function looks for a video output thread matching the current
108 * properties. If not found, it spawns a new one.
109 *****************************************************************************/
110 vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
111 video_format_t *p_fmt )
115 /* Video output is no longer used.
116 * TODO: support for reusing video outputs with proper _thread-safe_
117 * reference handling. */
119 vout_CloseAndRelease( p_vout );
123 /* If a video output was provided, lock it, otherwise look for one. */
126 vlc_object_hold( p_vout );
129 /* TODO: find a suitable unused video output */
131 /* If we now have a video output, check it has the right properties */
134 if( !video_format_IsSimilar( &p_vout->p->original, p_fmt ) )
136 /* We are not interested in this format, close this vout */
137 vout_CloseAndRelease( p_vout );
138 vlc_object_release( p_vout );
143 /* This video output is cool! Hijack it. */
144 vlc_object_release( p_vout );
149 msg_Dbg( p_this, "reusing provided vout" );
151 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
152 vlc_object_detach( p_vout );
154 vlc_object_attach( p_vout, p_this );
155 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
161 msg_Dbg( p_this, "no usable vout present, spawning one" );
163 p_vout = vout_Create( p_this, p_fmt );
169 /*****************************************************************************
170 * vout_Create: creates a new video output thread
171 *****************************************************************************
172 * This function creates a new video output thread, and returns a pointer
173 * to its description. On error, it returns NULL.
174 *****************************************************************************/
175 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
177 vout_thread_t *p_vout; /* thread descriptor */
181 config_chain_t *p_cfg;
185 if( p_fmt->i_width <= 0 || p_fmt->i_height <= 0 )
187 const vlc_fourcc_t i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma );
189 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
190 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
191 if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
194 /* Allocate descriptor */
195 static const char typename[] = "video output";
196 p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
202 p_vout->p = calloc( 1, sizeof(*p_vout->p) );
205 vlc_object_release( p_vout );
210 p_vout->p->original = *p_fmt; /* FIXME palette */
211 p_vout->p->original.i_chroma = i_chroma;
212 video_format_FixRgb( &p_vout->p->original );
214 /* Initialize misc stuff */
215 vout_control_Init( &p_vout->p->control );
216 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_INIT );
217 vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
218 vout_statistic_Init( &p_vout->p->statistic );
219 p_vout->p->i_par_num =
220 p_vout->p->i_par_den = 1;
221 p_vout->p->displayed.date = VLC_TS_INVALID;
222 p_vout->p->displayed.decoded = NULL;
223 p_vout->p->displayed.timestamp = VLC_TS_INVALID;
224 p_vout->p->displayed.qtype = QTYPE_NONE;
225 p_vout->p->displayed.is_interlaced = false;
227 p_vout->p->step.last = VLC_TS_INVALID;
228 p_vout->p->step.timestamp = VLC_TS_INVALID;
230 p_vout->p->pause.is_on = false;
231 p_vout->p->pause.date = VLC_TS_INVALID;
233 p_vout->p->decoder_fifo = picture_fifo_New();
234 p_vout->p->decoder_pool = NULL;
236 vlc_mouse_Init( &p_vout->p->mouse );
238 vout_snapshot_Init( &p_vout->p->snapshot );
240 p_vout->p->vfilter_chain =
241 filter_chain_New( p_vout, "video filter2", false,
242 video_filter_buffer_allocation_init, NULL, p_vout );
244 /* Initialize locks */
245 vlc_mutex_init( &p_vout->p->picture_lock );
246 vlc_mutex_init( &p_vout->p->vfilter_lock );
248 /* Mouse coordinates */
249 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
250 var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
251 var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
252 /* Mouse object (area of interest in a video filter) */
253 var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
255 /* Attach the new object now so we can use var inheritance below */
256 vlc_object_attach( p_vout, p_parent );
258 /* Initialize subpicture unit */
259 p_vout->p->p_spu = spu_Create( p_vout );
262 spu_Init( p_vout->p->p_spu );
264 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
266 p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
268 /* Take care of some "interface/control" related initialisations */
269 vout_IntfInit( p_vout );
271 /* Look for the default filter configuration */
272 p_vout->p->psz_filter_chain =
273 var_CreateGetStringCommand( p_vout, "vout-filter" );
275 /* Apply video filter2 objects on the first vout */
276 var_Create( p_vout, "video-filter",
277 VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
278 var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
279 var_TriggerCallback( p_vout, "video-filter" );
281 /* Choose the video output module */
282 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
288 psz_parser = strdup( p_vout->p->psz_filter_chain );
289 p_vout->p->title.show = false;
292 /* Create the vout thread */
293 char *psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
296 p_vout->p->p_cfg = p_cfg;
298 /* Create a few object variables for interface interaction */
299 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
300 text.psz_string = _("Filters");
301 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
302 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
305 vout_InitInterlacingSupport( p_vout, p_vout->p->displayed.is_interlaced );
307 if( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain )
310 if( asprintf( &psz_tmp, "%s,none", psz_name ) < 0 )
311 psz_tmp = strdup( "" );
315 p_vout->p->psz_module_name = psz_name;
318 vlc_object_set_destructor( p_vout, vout_Destructor );
321 if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
322 VLC_THREAD_PRIORITY_OUTPUT ) )
324 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
325 spu_Destroy( p_vout->p->p_spu );
326 p_vout->p->p_spu = NULL;
327 vlc_object_release( p_vout );
331 vout_control_WaitEmpty( &p_vout->p->control );
333 if (p_vout->p->dead )
335 msg_Err( p_vout, "video output creation failed" );
336 vout_CloseAndRelease( p_vout );
343 /*****************************************************************************
344 * vout_Close: Close a vout created by vout_Create.
345 *****************************************************************************
346 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
347 * You should NEVER call it on vout not obtained through vout_Create
348 * (like with vout_Request or vlc_object_find.)
349 * You can use vout_CloseAndRelease() as a convenience method.
350 *****************************************************************************/
351 void vout_Close( vout_thread_t *p_vout )
355 vout_snapshot_End( &p_vout->p->snapshot );
357 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_CLEAN );
358 vlc_join( p_vout->p->thread, NULL );
362 static void vout_Destructor( vlc_object_t * p_this )
364 vout_thread_t *p_vout = (vout_thread_t *)p_this;
366 /* Make sure the vout was stopped first */
367 //assert( !p_vout->p_module );
369 free( p_vout->p->psz_module_name );
372 if( p_vout->p->p_spu )
373 spu_Destroy( p_vout->p->p_spu );
375 vout_chrono_Clean( &p_vout->p->render );
377 if( p_vout->p->decoder_fifo )
378 picture_fifo_Delete( p_vout->p->decoder_fifo );
379 assert( !p_vout->p->decoder_pool );
381 /* Destroy the locks */
382 vlc_mutex_destroy( &p_vout->p->picture_lock );
383 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
384 vout_control_Clean( &p_vout->p->control );
387 vout_statistic_Clean( &p_vout->p->statistic );
390 vout_snapshot_Clean( &p_vout->p->snapshot );
393 free( p_vout->p->psz_filter_chain );
395 config_ChainDestroy( p_vout->p->p_cfg );
402 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
404 vout_control_cmd_t cmd;
405 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
406 cmd.u.pause.is_on = is_paused;
407 cmd.u.pause.date = date;
408 vout_control_Push(&vout->p->control, &cmd);
410 vout_control_WaitEmpty(&vout->p->control);
413 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
415 vout_statistic_GetReset( &p_vout->p->statistic,
416 pi_displayed, pi_lost );
419 void vout_Flush(vout_thread_t *vout, mtime_t date)
421 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
422 vout_control_WaitEmpty(&vout->p->control);
425 void vout_Reset(vout_thread_t *vout)
427 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
428 vout_control_WaitEmpty(&vout->p->control);
431 bool vout_IsEmpty(vout_thread_t *vout)
433 vlc_mutex_lock(&vout->p->picture_lock);
435 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
437 picture_Release(picture);
439 vlc_mutex_unlock(&vout->p->picture_lock);
444 void vout_FixLeaks( vout_thread_t *vout )
446 vlc_mutex_lock(&vout->p->picture_lock);
448 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
450 picture = picture_pool_Get(vout->p->decoder_pool);
454 picture_Release(picture);
455 /* Not all pictures has been displayed yet or some are
457 vlc_mutex_unlock(&vout->p->picture_lock);
461 /* There is no reason that no pictures are available, force one
462 * from the pool, becarefull with it though */
463 msg_Err(vout, "pictures leaked, trying to workaround");
466 picture_pool_NonEmpty(vout->p->decoder_pool, false);
468 vlc_mutex_unlock(&vout->p->picture_lock);
470 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
472 vout_control_cmd_t cmd;
473 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
474 cmd.u.time_ptr = duration;
476 vout_control_Push(&vout->p->control, &cmd);
477 vout_control_WaitEmpty(&vout->p->control);
480 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
483 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
486 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
488 spu_DisplaySubpicture(vout->p->p_spu, subpic);
490 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
492 return spu_RegisterChannel(vout->p->p_spu);
494 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
496 spu_ClearChannel(vout->p->p_spu, channel);
499 spu_t *vout_GetSpu( vout_thread_t *p_vout )
501 return p_vout->p->p_spu;
504 /* vout_Control* are usable by anyone at anytime */
505 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
507 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
510 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
512 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
515 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
517 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
520 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
522 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
525 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
526 unsigned num, unsigned den)
528 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
531 void vout_ControlChangeCropRatio(vout_thread_t *vout,
532 unsigned num, unsigned den)
534 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
537 void vout_ControlChangeCropWindow(vout_thread_t *vout,
538 int x, int y, int width, int height)
540 vout_control_cmd_t cmd;
541 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
544 cmd.u.window.width = width;
545 cmd.u.window.height = height;
547 vout_control_Push(&vout->p->control, &cmd);
549 void vout_ControlChangeCropBorder(vout_thread_t *vout,
550 int left, int top, int right, int bottom)
552 vout_control_cmd_t cmd;
553 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
554 cmd.u.border.left = left;
555 cmd.u.border.top = top;
556 cmd.u.border.right = right;
557 cmd.u.border.bottom = bottom;
559 vout_control_Push(&vout->p->control, &cmd);
563 static int ThreadDisplayPicture(vout_thread_t *vout,
564 bool now, mtime_t *deadline)
566 vout_display_t *vd = vout->p->display.vd;
567 int displayed_count = 0;
571 const mtime_t date = mdate();
572 const bool is_paused = vout->p->pause.is_on;
573 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
576 /* FIXME/XXX we must redisplay the last decoded picture (because
577 * of potential vout updated, or filters update or SPU update)
578 * For now a high update period is needed but it coulmd be removed
580 * - vout module emits events from theselves.
581 * - *and* SPU is modified to emit an event or a deadline when needed.
583 * So it will be done latter.
586 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
588 is_forced = peek->b_force || is_paused || now;
589 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
590 picture_Release(peek);
596 /* FIXME a better way for this delay is needed */
597 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
598 if (date_update > date || !vout->p->displayed.decoded) {
599 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
604 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
606 if (*deadline > VOUT_MWAIT_TOLERANCE)
607 *deadline -= VOUT_MWAIT_TOLERANCE;
609 /* If we are too early and can wait, do it */
610 if (date < *deadline && !now)
615 decoded = vout->p->displayed.decoded;
616 vout->p->displayed.decoded = NULL;
618 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
620 if (!is_forced && !vout->p->is_late_dropped) {
621 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
622 const mtime_t late = predicted - decoded->date;
624 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
625 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
626 msg_Warn(vout, "rejected picture because of render time");
628 picture_Release(decoded);
635 vout->p->displayed.is_interlaced = !decoded->b_progressive;
636 vout->p->displayed.qtype = decoded->i_qtype;
638 vout->p->displayed.timestamp = decoded->date;
641 if (vout->p->displayed.decoded)
642 picture_Release(vout->p->displayed.decoded);
643 picture_Hold(decoded);
644 vout->p->displayed.decoded = decoded;
647 vout_chrono_Start(&vout->p->render);
649 picture_t *filtered = NULL;
651 vlc_mutex_lock(&vout->p->vfilter_lock);
652 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
653 //assert(filtered == decoded); // TODO implement
654 vlc_mutex_unlock(&vout->p->vfilter_lock);
660 * Check for subpictures to display
662 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
663 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
664 if (vout->p->pause.is_on)
665 spu_render_time = vout->p->pause.date;
667 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
669 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
676 * - be sure to end up with a direct buffer.
677 * - blend subtitles, and in a fast access buffer
679 picture_t *direct = NULL;
681 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
683 if (vout->p->is_decoder_pool_slow)
684 render = picture_NewFromFormat(&vd->source);
685 else if (vout->p->decoder_pool != vout->p->display_pool)
686 render = picture_pool_Get(vout->p->display_pool);
688 render = picture_pool_Get(vout->p->private_pool);
691 picture_Copy(render, filtered);
693 spu_RenderSubpictures(vout->p->p_spu,
695 subpic, &vd->source, spu_render_time);
697 if (vout->p->is_decoder_pool_slow) {
698 direct = picture_pool_Get(vout->p->display_pool);
700 picture_Copy(direct, render);
701 picture_Release(render);
706 picture_Release(filtered);
713 * Take a snapshot if requested
715 if (direct && do_snapshot)
716 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
718 /* Render the direct buffer returned by vout_RenderPicture */
720 vout_RenderWrapper(vout, direct);
722 vout_chrono_Stop(&vout->p->render);
727 msg_Info(vout, "render: avg %d ms var %d ms",
728 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
733 /* Wait the real date (for rendering jitter) */
735 mwait(decoded->date);
737 /* Display the direct buffer returned by vout_RenderPicture */
738 vout->p->displayed.date = mdate();
740 vout_DisplayWrapper(vout, direct);
746 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
747 if (displayed_count <= 0)
752 static void ThreadManage(vout_thread_t *vout,
754 vout_interlacing_support_t *interlacing,
755 vout_postprocessing_support_t *postprocessing)
757 vlc_mutex_lock(&vout->p->picture_lock);
759 *deadline = VLC_TS_INVALID;
760 ThreadDisplayPicture(vout, false, deadline);
762 const int picture_qtype = vout->p->displayed.qtype;
763 const bool picture_interlaced = vout->p->displayed.is_interlaced;
765 vlc_mutex_unlock(&vout->p->picture_lock);
767 /* Post processing */
768 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
771 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
773 vout_ManageWrapper(vout);
776 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
778 if (!vout->p->title.show)
781 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
782 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
786 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
789 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
790 fmt.video = vout->p->original;
792 vlc_mutex_lock(&vout->p->vfilter_lock);
794 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
795 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
797 msg_Err(vout, "Video filter chain creation failed");
799 vlc_mutex_unlock(&vout->p->vfilter_lock);
802 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
804 assert(!vout->p->pause.is_on || !is_paused);
806 if (vout->p->pause.is_on) {
807 const mtime_t duration = date - vout->p->pause.date;
809 if (vout->p->step.timestamp > VLC_TS_INVALID)
810 vout->p->step.timestamp += duration;
811 if (vout->p->step.last > VLC_TS_INVALID)
812 vout->p->step.last += duration;
813 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
814 if (vout->p->displayed.decoded)
815 vout->p->displayed.decoded->date += duration;
817 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
819 vout->p->step.timestamp = VLC_TS_INVALID;
820 vout->p->step.last = VLC_TS_INVALID;
822 vout->p->pause.is_on = is_paused;
823 vout->p->pause.date = date;
826 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
828 vout->p->step.timestamp = VLC_TS_INVALID;
829 vout->p->step.last = VLC_TS_INVALID;
831 picture_t *last = vout->p->displayed.decoded;
833 if (( below && last->date <= date) ||
834 (!below && last->date >= date)) {
835 picture_Release(last);
837 vout->p->displayed.decoded = NULL;
838 vout->p->displayed.date = VLC_TS_INVALID;
839 vout->p->displayed.timestamp = VLC_TS_INVALID;
842 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
845 static void ThreadReset(vout_thread_t *vout)
847 ThreadFlush(vout, true, INT64_MAX);
848 if (vout->p->decoder_pool)
849 picture_pool_NonEmpty(vout->p->decoder_pool, true);
850 vout->p->pause.is_on = false;
851 vout->p->pause.date = mdate();
854 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
858 if (vout->p->step.last <= VLC_TS_INVALID)
859 vout->p->step.last = vout->p->displayed.timestamp;
862 if (ThreadDisplayPicture(vout, true, &dummy))
865 vout->p->step.timestamp = vout->p->displayed.timestamp;
867 if (vout->p->step.last > VLC_TS_INVALID &&
868 vout->p->step.timestamp > vout->p->step.last) {
869 *duration = vout->p->step.timestamp - vout->p->step.last;
870 vout->p->step.last = vout->p->step.timestamp;
871 /* TODO advance subpicture by the duration ... */
875 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
877 /* FIXME not sure setting "fullscreen" is good ... */
878 var_SetBool(vout, "fullscreen", fullscreen);
879 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
882 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
884 vout_SetWindowState(vout->p->display.vd,
885 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
886 VOUT_WINDOW_STATE_NORMAL);
889 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
891 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
894 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
896 if (num * 10 < den) {
899 } else if (num > den * 10) {
903 vout_SetDisplayZoom(vout->p->display.vd, num, den);
906 static void ThreadChangeAspectRatio(vout_thread_t *vout,
907 unsigned num, unsigned den)
909 const video_format_t *source = &vout->p->original;
911 if (num > 0 && den > 0) {
912 num *= source->i_visible_height;
913 den *= source->i_visible_width;
914 vlc_ureduce(&num, &den, num, den, 0);
916 vout_SetDisplayAspect(vout->p->display.vd, num, den);
920 static void ThreadExecuteCropWindow(vout_thread_t *vout,
921 unsigned crop_num, unsigned crop_den,
922 unsigned x, unsigned y,
923 unsigned width, unsigned height)
925 const video_format_t *source = &vout->p->original;
927 vout_SetDisplayCrop(vout->p->display.vd,
929 source->i_x_offset + x,
930 source->i_y_offset + y,
933 static void ThreadExecuteCropBorder(vout_thread_t *vout,
934 unsigned left, unsigned top,
935 unsigned right, unsigned bottom)
937 const video_format_t *source = &vout->p->original;
938 ThreadExecuteCropWindow(vout, 0, 0,
941 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
942 source->i_visible_width - (left + right),
943 source->i_visible_height - (top + bottom));
946 static void ThreadExecuteCropRatio(vout_thread_t *vout,
947 unsigned num, unsigned den)
949 const video_format_t *source = &vout->p->original;
953 if (num <= 0 || den <= 0) {
958 width = source->i_visible_width;
959 height = source->i_visible_height;
961 unsigned scaled_width = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
962 unsigned scaled_height = (uint64_t)source->i_visible_width * den * source->i_sar_num / num / source->i_sar_den;
964 if (scaled_width < source->i_visible_width) {
965 x = (source->i_visible_width - scaled_width) / 2;
967 width = scaled_width;
968 height = source->i_visible_height;
971 y = (source->i_visible_height - scaled_height) / 2;
972 width = source->i_visible_width;
973 height = scaled_height;
976 ThreadExecuteCropWindow(vout, num, den, x, y, width, height);
979 static int ThreadInit(vout_thread_t *vout)
981 vout->p->dead = false;
983 if (vout_OpenWrapper(vout, vout->p->psz_module_name))
985 if (vout_InitWrapper(vout))
987 assert(vout->p->decoder_pool);
989 vout->p->displayed.decoded = NULL;
991 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
995 static void ThreadClean(vout_thread_t *vout)
997 /* Destroy the video filters2 */
998 filter_chain_Delete(vout->p->vfilter_chain);
1000 /* Destroy translation tables */
1001 if (vout->p->display.vd) {
1002 if (vout->p->decoder_pool) {
1003 ThreadFlush(vout, true, INT64_MAX);
1004 vout_EndWrapper(vout);
1006 vout_CloseWrapper(vout);
1009 /* Detach subpicture unit from both input and vout */
1010 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
1011 vlc_object_detach(vout->p->p_spu);
1013 vout->p->dead = true;
1014 vout_control_Dead(&vout->p->control);
1017 /*****************************************************************************
1018 * Thread: video output thread
1019 *****************************************************************************
1020 * Video output thread. This function does only returns when the thread is
1021 * terminated. It handles the pictures arriving in the video heap and the
1022 * display device events.
1023 *****************************************************************************/
1024 static void *Thread(void *object)
1026 vout_thread_t *vout = object;
1028 vout_interlacing_support_t interlacing = {
1029 .is_interlaced = false,
1032 vout_postprocessing_support_t postprocessing = {
1033 .qtype = QTYPE_NONE,
1036 mtime_t deadline = VLC_TS_INVALID;
1038 vout_control_cmd_t cmd;
1040 /* FIXME remove thoses ugly timeouts
1042 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1044 case VOUT_CONTROL_INIT:
1045 if (ThreadInit(vout)) {
1050 case VOUT_CONTROL_CLEAN:
1053 case VOUT_CONTROL_OSD_TITLE:
1054 ThreadDisplayOsdTitle(vout, cmd.u.string);
1056 case VOUT_CONTROL_CHANGE_FILTERS:
1057 ThreadChangeFilters(vout, cmd.u.string);
1059 case VOUT_CONTROL_PAUSE:
1060 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1062 case VOUT_CONTROL_FLUSH:
1063 ThreadFlush(vout, false, cmd.u.time);
1065 case VOUT_CONTROL_RESET:
1068 case VOUT_CONTROL_STEP:
1069 ThreadStep(vout, cmd.u.time_ptr);
1071 case VOUT_CONTROL_FULLSCREEN:
1072 ThreadChangeFullscreen(vout, cmd.u.boolean);
1074 case VOUT_CONTROL_ON_TOP:
1075 ThreadChangeOnTop(vout, cmd.u.boolean);
1077 case VOUT_CONTROL_DISPLAY_FILLED:
1078 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1080 case VOUT_CONTROL_ZOOM:
1081 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1083 case VOUT_CONTROL_ASPECT_RATIO:
1084 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1086 case VOUT_CONTROL_CROP_RATIO:
1087 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1089 case VOUT_CONTROL_CROP_WINDOW:
1090 ThreadExecuteCropWindow(vout, 0, 0,
1091 cmd.u.window.x, cmd.u.window.y,
1092 cmd.u.window.width, cmd.u.window.height);
1094 case VOUT_CONTROL_CROP_BORDER:
1095 ThreadExecuteCropBorder(vout,
1096 cmd.u.border.left, cmd.u.border.top,
1097 cmd.u.border.right, cmd.u.border.bottom);
1102 vout_control_cmd_Clean(&cmd);
1105 ThreadManage(vout, &deadline, &interlacing, &postprocessing);
1109 /*****************************************************************************
1110 * object variables callbacks: a bunch of object variables are used by the
1111 * interfaces to interact with the vout.
1112 *****************************************************************************/
1113 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1114 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1116 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1117 input_thread_t *p_input;
1118 (void)psz_cmd; (void)oldval; (void)p_data;
1120 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1124 msg_Err( p_vout, "Input not found" );
1125 return VLC_EGENERIC;
1128 /* Modify input as well because the vout might have to be restarted */
1129 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1130 var_SetString( p_input, "vout-filter", newval.psz_string );
1132 /* Now restart current video stream */
1133 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1134 vlc_object_release( p_input );
1139 /*****************************************************************************
1140 * Video Filter2 stuff
1141 *****************************************************************************/
1142 static int VideoFilter2Callback(vlc_object_t *object, char const *cmd,
1143 vlc_value_t oldval, vlc_value_t newval,
1146 vout_thread_t *vout = (vout_thread_t *)object;
1147 (void)cmd; (void)oldval; (void)data;
1149 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,