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 * );
68 static void PrintVideoFormat(vout_thread_t *, const char *, const video_format_t *);
70 /* Maximum delay between 2 displayed pictures.
71 * XXX it is needed for now but should be removed in the long term.
73 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
76 * Late pictures having a delay higher than this value are thrashed.
78 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
80 /* Better be in advance when awakening than late... */
81 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
83 /*****************************************************************************
84 * Video Filter2 functions
85 *****************************************************************************/
86 static picture_t *video_new_buffer_filter( filter_t *p_filter )
88 vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
89 return picture_pool_Get(p_vout->p->private_pool);
92 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
95 picture_Release(p_pic);
98 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
100 p_filter->pf_video_buffer_new = video_new_buffer_filter;
101 p_filter->pf_video_buffer_del = video_del_buffer_filter;
102 p_filter->p_owner = p_data; /* p_vout */
107 /*****************************************************************************
108 * vout_Request: find a video output thread, create one, or destroy one.
109 *****************************************************************************
110 * This function looks for a video output thread matching the current
111 * properties. If not found, it spawns a new one.
112 *****************************************************************************/
113 vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
114 video_format_t *p_fmt )
118 /* Video output is no longer used.
119 * TODO: support for reusing video outputs with proper _thread-safe_
120 * reference handling. */
122 vout_CloseAndRelease( p_vout );
126 /* If a video output was provided, lock it, otherwise look for one. */
129 vlc_object_hold( p_vout );
132 /* TODO: find a suitable unused video output */
134 /* If we now have a video output, check it has the right properties */
137 if( !video_format_IsSimilar( &p_vout->p->original, p_fmt ) )
139 /* We are not interested in this format, close this vout */
140 vout_CloseAndRelease( p_vout );
141 vlc_object_release( p_vout );
146 /* This video output is cool! Hijack it. */
147 vlc_object_release( p_vout );
152 msg_Dbg( p_this, "reusing provided vout" );
154 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
155 vlc_object_detach( p_vout );
157 vlc_object_attach( p_vout, p_this );
158 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
164 msg_Dbg( p_this, "no usable vout present, spawning one" );
166 p_vout = vout_Create( p_this, p_fmt );
172 /*****************************************************************************
173 * vout_Create: creates a new video output thread
174 *****************************************************************************
175 * This function creates a new video output thread, and returns a pointer
176 * to its description. On error, it returns NULL.
177 *****************************************************************************/
178 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
180 vout_thread_t *p_vout; /* thread descriptor */
184 config_chain_t *p_cfg;
188 if( p_fmt->i_width <= 0 || p_fmt->i_height <= 0 )
190 const vlc_fourcc_t i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma );
192 vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
193 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
194 if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
197 /* Allocate descriptor */
198 static const char typename[] = "video output";
199 p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
205 p_vout->p = calloc( 1, sizeof(*p_vout->p) );
208 vlc_object_release( p_vout );
213 p_vout->p->original = *p_fmt; /* FIXME palette */
214 p_vout->p->original.i_chroma = i_chroma;
215 video_format_FixRgb( &p_vout->p->original );
217 /* Initialize misc stuff */
218 vout_control_Init( &p_vout->p->control );
219 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_INIT );
220 vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
221 vout_statistic_Init( &p_vout->p->statistic );
222 p_vout->p->i_par_num =
223 p_vout->p->i_par_den = 1;
224 p_vout->p->displayed.date = VLC_TS_INVALID;
225 p_vout->p->displayed.decoded = NULL;
226 p_vout->p->displayed.timestamp = VLC_TS_INVALID;
227 p_vout->p->displayed.qtype = QTYPE_NONE;
228 p_vout->p->displayed.is_interlaced = false;
230 p_vout->p->step.last = VLC_TS_INVALID;
231 p_vout->p->step.timestamp = VLC_TS_INVALID;
233 p_vout->p->pause.is_on = false;
234 p_vout->p->pause.date = VLC_TS_INVALID;
236 p_vout->p->decoder_fifo = picture_fifo_New();
237 p_vout->p->decoder_pool = NULL;
239 vlc_mouse_Init( &p_vout->p->mouse );
241 vout_snapshot_Init( &p_vout->p->snapshot );
243 p_vout->p->vfilter_chain =
244 filter_chain_New( p_vout, "video filter2", false,
245 video_filter_buffer_allocation_init, NULL, p_vout );
247 /* Initialize locks */
248 vlc_mutex_init( &p_vout->p->picture_lock );
249 vlc_mutex_init( &p_vout->p->vfilter_lock );
251 /* Mouse coordinates */
252 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
253 var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
254 var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
255 /* Mouse object (area of interest in a video filter) */
256 var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
258 /* Attach the new object now so we can use var inheritance below */
259 vlc_object_attach( p_vout, p_parent );
261 /* Initialize subpicture unit */
262 p_vout->p->p_spu = spu_Create( p_vout );
265 spu_Init( p_vout->p->p_spu );
267 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
269 p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
271 /* Take care of some "interface/control" related initialisations */
272 vout_IntfInit( p_vout );
274 /* Look for the default filter configuration */
275 p_vout->p->psz_filter_chain =
276 var_CreateGetStringCommand( p_vout, "vout-filter" );
278 /* Apply video filter2 objects on the first vout */
279 var_Create( p_vout, "video-filter",
280 VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
281 var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
282 var_TriggerCallback( p_vout, "video-filter" );
284 /* Choose the video output module */
285 if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
291 psz_parser = strdup( p_vout->p->psz_filter_chain );
292 p_vout->p->title.show = false;
295 /* Create the vout thread */
296 char *psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
299 p_vout->p->p_cfg = p_cfg;
301 /* Create a few object variables for interface interaction */
302 var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
303 text.psz_string = _("Filters");
304 var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
305 var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
308 vout_InitInterlacingSupport( p_vout, p_vout->p->displayed.is_interlaced );
310 if( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain )
313 if( asprintf( &psz_tmp, "%s,none", psz_name ) < 0 )
314 psz_tmp = strdup( "" );
318 p_vout->p->psz_module_name = psz_name;
321 vlc_object_set_destructor( p_vout, vout_Destructor );
324 if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
325 VLC_THREAD_PRIORITY_OUTPUT ) )
327 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
328 spu_Destroy( p_vout->p->p_spu );
329 p_vout->p->p_spu = NULL;
330 vlc_object_release( p_vout );
334 vout_control_WaitEmpty( &p_vout->p->control );
336 if (p_vout->p->dead )
338 msg_Err( p_vout, "video output creation failed" );
339 vout_CloseAndRelease( p_vout );
346 /*****************************************************************************
347 * vout_Close: Close a vout created by vout_Create.
348 *****************************************************************************
349 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
350 * You should NEVER call it on vout not obtained through vout_Create
351 * (like with vout_Request or vlc_object_find.)
352 * You can use vout_CloseAndRelease() as a convenience method.
353 *****************************************************************************/
354 void vout_Close( vout_thread_t *p_vout )
358 vout_snapshot_End( &p_vout->p->snapshot );
360 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_CLEAN );
361 vlc_join( p_vout->p->thread, NULL );
365 static void vout_Destructor( vlc_object_t * p_this )
367 vout_thread_t *p_vout = (vout_thread_t *)p_this;
369 /* Make sure the vout was stopped first */
370 //assert( !p_vout->p_module );
372 free( p_vout->p->psz_module_name );
375 if( p_vout->p->p_spu )
376 spu_Destroy( p_vout->p->p_spu );
378 vout_chrono_Clean( &p_vout->p->render );
380 if( p_vout->p->decoder_fifo )
381 picture_fifo_Delete( p_vout->p->decoder_fifo );
382 assert( !p_vout->p->decoder_pool );
384 /* Destroy the locks */
385 vlc_mutex_destroy( &p_vout->p->picture_lock );
386 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
387 vout_control_Clean( &p_vout->p->control );
390 vout_statistic_Clean( &p_vout->p->statistic );
393 vout_snapshot_Clean( &p_vout->p->snapshot );
396 free( p_vout->p->psz_filter_chain );
398 config_ChainDestroy( p_vout->p->p_cfg );
405 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
407 vout_control_cmd_t cmd;
408 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
409 cmd.u.pause.is_on = is_paused;
410 cmd.u.pause.date = date;
411 vout_control_Push(&vout->p->control, &cmd);
413 vout_control_WaitEmpty(&vout->p->control);
416 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
418 vout_statistic_GetReset( &p_vout->p->statistic,
419 pi_displayed, pi_lost );
422 void vout_Flush(vout_thread_t *vout, mtime_t date)
424 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
425 vout_control_WaitEmpty(&vout->p->control);
428 void vout_Reset(vout_thread_t *vout)
430 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
431 vout_control_WaitEmpty(&vout->p->control);
434 bool vout_IsEmpty(vout_thread_t *vout)
436 vlc_mutex_lock(&vout->p->picture_lock);
438 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
440 picture_Release(picture);
442 vlc_mutex_unlock(&vout->p->picture_lock);
447 void vout_FixLeaks( vout_thread_t *vout )
449 vlc_mutex_lock(&vout->p->picture_lock);
451 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
453 picture = picture_pool_Get(vout->p->decoder_pool);
457 picture_Release(picture);
458 /* Not all pictures has been displayed yet or some are
460 vlc_mutex_unlock(&vout->p->picture_lock);
464 /* There is no reason that no pictures are available, force one
465 * from the pool, becarefull with it though */
466 msg_Err(vout, "pictures leaked, trying to workaround");
469 picture_pool_NonEmpty(vout->p->decoder_pool, false);
471 vlc_mutex_unlock(&vout->p->picture_lock);
473 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
475 vout_control_cmd_t cmd;
476 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
477 cmd.u.time_ptr = duration;
479 vout_control_Push(&vout->p->control, &cmd);
480 vout_control_WaitEmpty(&vout->p->control);
483 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
486 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
489 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
491 spu_DisplaySubpicture(vout->p->p_spu, subpic);
493 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
495 return spu_RegisterChannel(vout->p->p_spu);
497 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
499 spu_ClearChannel(vout->p->p_spu, channel);
502 spu_t *vout_GetSpu( vout_thread_t *p_vout )
504 return p_vout->p->p_spu;
507 /* vout_Control* are usable by anyone at anytime */
508 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
510 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
513 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
515 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
518 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
520 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
523 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
525 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
528 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
529 unsigned num, unsigned den)
531 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
534 void vout_ControlChangeCropRatio(vout_thread_t *vout,
535 unsigned num, unsigned den)
537 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
540 void vout_ControlChangeCropWindow(vout_thread_t *vout,
541 int x, int y, int width, int height)
543 vout_control_cmd_t cmd;
544 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
547 cmd.u.window.width = width;
548 cmd.u.window.height = height;
550 vout_control_Push(&vout->p->control, &cmd);
552 void vout_ControlChangeCropBorder(vout_thread_t *vout,
553 int left, int top, int right, int bottom)
555 vout_control_cmd_t cmd;
556 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
557 cmd.u.border.left = left;
558 cmd.u.border.top = top;
559 cmd.u.border.right = right;
560 cmd.u.border.bottom = bottom;
562 vout_control_Push(&vout->p->control, &cmd);
566 static int ThreadDisplayPicture(vout_thread_t *vout,
567 bool now, mtime_t *deadline)
569 vout_display_t *vd = vout->p->display.vd;
570 int displayed_count = 0;
574 const mtime_t date = mdate();
575 const bool is_paused = vout->p->pause.is_on;
576 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
579 /* FIXME/XXX we must redisplay the last decoded picture (because
580 * of potential vout updated, or filters update or SPU update)
581 * For now a high update period is needed but it coulmd be removed
583 * - vout module emits events from theselves.
584 * - *and* SPU is modified to emit an event or a deadline when needed.
586 * So it will be done latter.
589 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
591 is_forced = peek->b_force || is_paused || now;
592 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
593 picture_Release(peek);
599 /* FIXME a better way for this delay is needed */
600 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
601 if (date_update > date || !vout->p->displayed.decoded) {
602 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
607 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
609 if (*deadline > VOUT_MWAIT_TOLERANCE)
610 *deadline -= VOUT_MWAIT_TOLERANCE;
612 /* If we are too early and can wait, do it */
613 if (date < *deadline && !now)
618 decoded = vout->p->displayed.decoded;
619 vout->p->displayed.decoded = NULL;
621 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
623 if (!is_forced && !vout->p->is_late_dropped) {
624 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
625 const mtime_t late = predicted - decoded->date;
627 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
628 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
629 msg_Warn(vout, "rejected picture because of render time");
631 picture_Release(decoded);
638 vout->p->displayed.is_interlaced = !decoded->b_progressive;
639 vout->p->displayed.qtype = decoded->i_qtype;
641 vout->p->displayed.timestamp = decoded->date;
644 if (vout->p->displayed.decoded)
645 picture_Release(vout->p->displayed.decoded);
646 picture_Hold(decoded);
647 vout->p->displayed.decoded = decoded;
650 vout_chrono_Start(&vout->p->render);
652 picture_t *filtered = NULL;
654 vlc_mutex_lock(&vout->p->vfilter_lock);
655 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
656 //assert(filtered == decoded); // TODO implement
657 vlc_mutex_unlock(&vout->p->vfilter_lock);
663 * Check for subpictures to display
665 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
666 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
667 if (vout->p->pause.is_on)
668 spu_render_time = vout->p->pause.date;
670 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
672 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
679 * - be sure to end up with a direct buffer.
680 * - blend subtitles, and in a fast access buffer
682 picture_t *direct = NULL;
684 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
686 if (vout->p->is_decoder_pool_slow)
687 render = picture_NewFromFormat(&vd->source);
688 else if (vout->p->decoder_pool != vout->p->display_pool)
689 render = picture_pool_Get(vout->p->display_pool);
691 render = picture_pool_Get(vout->p->private_pool);
694 picture_Copy(render, filtered);
696 spu_RenderSubpictures(vout->p->p_spu,
698 subpic, &vd->source, spu_render_time);
700 if (vout->p->is_decoder_pool_slow) {
701 direct = picture_pool_Get(vout->p->display_pool);
703 picture_Copy(direct, render);
704 picture_Release(render);
709 picture_Release(filtered);
716 * Take a snapshot if requested
718 if (direct && do_snapshot)
719 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
721 /* Render the direct buffer returned by vout_RenderPicture */
723 vout_RenderWrapper(vout, direct);
725 vout_chrono_Stop(&vout->p->render);
730 msg_Info(vout, "render: avg %d ms var %d ms",
731 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
736 /* Wait the real date (for rendering jitter) */
738 mwait(decoded->date);
740 /* Display the direct buffer returned by vout_RenderPicture */
741 vout->p->displayed.date = mdate();
743 vout_DisplayWrapper(vout, direct);
749 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
750 if (displayed_count <= 0)
755 static void ThreadManage(vout_thread_t *vout,
757 vout_interlacing_support_t *interlacing,
758 vout_postprocessing_support_t *postprocessing)
760 vlc_mutex_lock(&vout->p->picture_lock);
762 *deadline = VLC_TS_INVALID;
763 ThreadDisplayPicture(vout, false, deadline);
765 const int picture_qtype = vout->p->displayed.qtype;
766 const bool picture_interlaced = vout->p->displayed.is_interlaced;
768 vlc_mutex_unlock(&vout->p->picture_lock);
770 /* Post processing */
771 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
774 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
776 vout_ManageWrapper(vout);
779 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
781 if (!vout->p->title.show)
784 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
785 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
789 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
792 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
793 fmt.video = vout->p->original;
795 vlc_mutex_lock(&vout->p->vfilter_lock);
797 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
798 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
800 msg_Err(vout, "Video filter chain creation failed");
802 vlc_mutex_unlock(&vout->p->vfilter_lock);
805 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
807 assert(!vout->p->pause.is_on || !is_paused);
809 if (vout->p->pause.is_on) {
810 const mtime_t duration = date - vout->p->pause.date;
812 if (vout->p->step.timestamp > VLC_TS_INVALID)
813 vout->p->step.timestamp += duration;
814 if (vout->p->step.last > VLC_TS_INVALID)
815 vout->p->step.last += duration;
816 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
817 if (vout->p->displayed.decoded)
818 vout->p->displayed.decoded->date += duration;
820 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
822 vout->p->step.timestamp = VLC_TS_INVALID;
823 vout->p->step.last = VLC_TS_INVALID;
825 vout->p->pause.is_on = is_paused;
826 vout->p->pause.date = date;
829 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
831 vout->p->step.timestamp = VLC_TS_INVALID;
832 vout->p->step.last = VLC_TS_INVALID;
834 picture_t *last = vout->p->displayed.decoded;
836 if (( below && last->date <= date) ||
837 (!below && last->date >= date)) {
838 picture_Release(last);
840 vout->p->displayed.decoded = NULL;
841 vout->p->displayed.date = VLC_TS_INVALID;
842 vout->p->displayed.timestamp = VLC_TS_INVALID;
845 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
848 static void ThreadReset(vout_thread_t *vout)
850 ThreadFlush(vout, true, INT64_MAX);
851 if (vout->p->decoder_pool)
852 picture_pool_NonEmpty(vout->p->decoder_pool, true);
853 vout->p->pause.is_on = false;
854 vout->p->pause.date = mdate();
857 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
861 if (vout->p->step.last <= VLC_TS_INVALID)
862 vout->p->step.last = vout->p->displayed.timestamp;
865 if (ThreadDisplayPicture(vout, true, &dummy))
868 vout->p->step.timestamp = vout->p->displayed.timestamp;
870 if (vout->p->step.last > VLC_TS_INVALID &&
871 vout->p->step.timestamp > vout->p->step.last) {
872 *duration = vout->p->step.timestamp - vout->p->step.last;
873 vout->p->step.last = vout->p->step.timestamp;
874 /* TODO advance subpicture by the duration ... */
878 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
880 /* FIXME not sure setting "fullscreen" is good ... */
881 var_SetBool(vout, "fullscreen", fullscreen);
882 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
885 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
887 vout_SetWindowState(vout->p->display.vd,
888 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
889 VOUT_WINDOW_STATE_NORMAL);
892 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
894 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
897 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
899 if (num * 10 < den) {
902 } else if (num > den * 10) {
906 vout_SetDisplayZoom(vout->p->display.vd, num, den);
909 static void ThreadChangeAspectRatio(vout_thread_t *vout,
910 unsigned num, unsigned den)
912 const video_format_t *source = &vout->p->original;
914 if (num > 0 && den > 0) {
915 num *= source->i_visible_height;
916 den *= source->i_visible_width;
917 vlc_ureduce(&num, &den, num, den, 0);
919 vout_SetDisplayAspect(vout->p->display.vd, num, den);
923 static void ThreadExecuteCropWindow(vout_thread_t *vout,
924 unsigned crop_num, unsigned crop_den,
925 unsigned x, unsigned y,
926 unsigned width, unsigned height)
928 const video_format_t *source = &vout->p->original;
930 vout_SetDisplayCrop(vout->p->display.vd,
932 source->i_x_offset + x,
933 source->i_y_offset + y,
936 static void ThreadExecuteCropBorder(vout_thread_t *vout,
937 unsigned left, unsigned top,
938 unsigned right, unsigned bottom)
940 const video_format_t *source = &vout->p->original;
941 ThreadExecuteCropWindow(vout, 0, 0,
944 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
945 source->i_visible_width - (left + right),
946 source->i_visible_height - (top + bottom));
949 static void ThreadExecuteCropRatio(vout_thread_t *vout,
950 unsigned num, unsigned den)
952 const video_format_t *source = &vout->p->original;
956 if (num <= 0 || den <= 0) {
961 width = source->i_visible_width;
962 height = source->i_visible_height;
964 unsigned scaled_width = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
965 unsigned scaled_height = (uint64_t)source->i_visible_width * den * source->i_sar_num / num / source->i_sar_den;
967 if (scaled_width < source->i_visible_width) {
968 x = (source->i_visible_width - scaled_width) / 2;
970 width = scaled_width;
971 height = source->i_visible_height;
974 y = (source->i_visible_height - scaled_height) / 2;
975 width = source->i_visible_width;
976 height = scaled_height;
979 ThreadExecuteCropWindow(vout, num, den, x, y, width, height);
982 static int ThreadInit(vout_thread_t *vout)
984 vout->p->dead = false;
986 if (vout_OpenWrapper(vout, vout->p->psz_module_name))
988 if (vout_InitWrapper(vout))
990 assert(vout->p->decoder_pool);
992 vout->p->displayed.decoded = NULL;
994 PrintVideoFormat(vout, "original format", &vout->p->original);
998 static void ThreadClean(vout_thread_t *vout)
1000 /* Destroy the video filters2 */
1001 filter_chain_Delete(vout->p->vfilter_chain);
1003 /* Destroy translation tables */
1004 if (vout->p->display.vd) {
1005 if (vout->p->decoder_pool) {
1006 ThreadFlush(vout, true, INT64_MAX);
1007 vout_EndWrapper(vout);
1009 vout_CloseWrapper(vout);
1012 /* Detach subpicture unit from both input and vout */
1013 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
1014 vlc_object_detach(vout->p->p_spu);
1016 vout->p->dead = true;
1017 vout_control_Dead(&vout->p->control);
1020 /*****************************************************************************
1021 * Thread: video output thread
1022 *****************************************************************************
1023 * Video output thread. This function does only returns when the thread is
1024 * terminated. It handles the pictures arriving in the video heap and the
1025 * display device events.
1026 *****************************************************************************/
1027 static void *Thread(void *object)
1029 vout_thread_t *vout = object;
1031 vout_interlacing_support_t interlacing = {
1032 .is_interlaced = false,
1035 vout_postprocessing_support_t postprocessing = {
1036 .qtype = QTYPE_NONE,
1039 mtime_t deadline = VLC_TS_INVALID;
1041 vout_control_cmd_t cmd;
1043 /* FIXME remove thoses ugly timeouts
1045 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1047 case VOUT_CONTROL_INIT:
1048 if (ThreadInit(vout)) {
1053 case VOUT_CONTROL_CLEAN:
1056 case VOUT_CONTROL_OSD_TITLE:
1057 ThreadDisplayOsdTitle(vout, cmd.u.string);
1059 case VOUT_CONTROL_CHANGE_FILTERS:
1060 ThreadChangeFilters(vout, cmd.u.string);
1062 case VOUT_CONTROL_PAUSE:
1063 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1065 case VOUT_CONTROL_FLUSH:
1066 ThreadFlush(vout, false, cmd.u.time);
1068 case VOUT_CONTROL_RESET:
1071 case VOUT_CONTROL_STEP:
1072 ThreadStep(vout, cmd.u.time_ptr);
1074 case VOUT_CONTROL_FULLSCREEN:
1075 ThreadChangeFullscreen(vout, cmd.u.boolean);
1077 case VOUT_CONTROL_ON_TOP:
1078 ThreadChangeOnTop(vout, cmd.u.boolean);
1080 case VOUT_CONTROL_DISPLAY_FILLED:
1081 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1083 case VOUT_CONTROL_ZOOM:
1084 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1086 case VOUT_CONTROL_ASPECT_RATIO:
1087 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1089 case VOUT_CONTROL_CROP_RATIO:
1090 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1092 case VOUT_CONTROL_CROP_WINDOW:
1093 ThreadExecuteCropWindow(vout, 0, 0,
1094 cmd.u.window.x, cmd.u.window.y,
1095 cmd.u.window.width, cmd.u.window.height);
1097 case VOUT_CONTROL_CROP_BORDER:
1098 ThreadExecuteCropBorder(vout,
1099 cmd.u.border.left, cmd.u.border.top,
1100 cmd.u.border.right, cmd.u.border.bottom);
1105 vout_control_cmd_Clean(&cmd);
1108 ThreadManage(vout, &deadline, &interlacing, &postprocessing);
1112 /*****************************************************************************
1113 * object variables callbacks: a bunch of object variables are used by the
1114 * interfaces to interact with the vout.
1115 *****************************************************************************/
1116 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1117 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1119 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1120 input_thread_t *p_input;
1121 (void)psz_cmd; (void)oldval; (void)p_data;
1123 p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1127 msg_Err( p_vout, "Input not found" );
1128 return VLC_EGENERIC;
1131 /* Modify input as well because the vout might have to be restarted */
1132 var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1133 var_SetString( p_input, "vout-filter", newval.psz_string );
1135 /* Now restart current video stream */
1136 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1137 vlc_object_release( p_input );
1142 /*****************************************************************************
1143 * Video Filter2 stuff
1144 *****************************************************************************/
1145 static int VideoFilter2Callback(vlc_object_t *object, char const *cmd,
1146 vlc_value_t oldval, vlc_value_t newval,
1149 vout_thread_t *vout = (vout_thread_t *)object;
1150 (void)cmd; (void)oldval; (void)data;
1152 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
1158 static void PrintVideoFormat(vout_thread_t *vout,
1159 const char *description,
1160 const video_format_t *fmt)
1162 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",
1164 fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
1165 fmt->i_visible_width, fmt->i_visible_height,
1166 (char*)&fmt->i_chroma,
1167 fmt->i_sar_num, fmt->i_sar_den,
1168 fmt->i_rmask, fmt->i_gmask, fmt->i_bmask);