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 "vout_internal.h"
50 #include "interlacing.h"
51 #include "postprocessing.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static void *Thread(void *);
58 static void VoutDestructor(vlc_object_t *);
60 /* Maximum delay between 2 displayed pictures.
61 * XXX it is needed for now but should be removed in the long term.
63 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
66 * Late pictures having a delay higher than this value are thrashed.
68 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
70 /* Better be in advance when awakening than late... */
71 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
74 static int VoutValidateFormat(video_format_t *dst,
75 const video_format_t *src)
77 if (src->i_width <= 0 || src->i_height <= 0)
79 if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
83 video_format_Copy(dst, src);
84 dst->i_chroma = vlc_fourcc_GetCodec(VIDEO_ES, src->i_chroma);
85 vlc_ureduce( &dst->i_sar_num, &dst->i_sar_den,
86 src->i_sar_num, src->i_sar_den, 50000 );
87 if (dst->i_sar_num <= 0 || dst->i_sar_den <= 0) {
91 video_format_FixRgb(dst);
96 /*****************************************************************************
97 * vout_Request: find a video output thread, create one, or destroy one.
98 *****************************************************************************
99 * This function looks for a video output thread matching the current
100 * properties. If not found, it spawns a new one.
101 *****************************************************************************/
102 vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
103 video_format_t *p_fmt )
107 /* Video output is no longer used.
108 * TODO: support for reusing video outputs with proper _thread-safe_
109 * reference handling. */
111 vout_CloseAndRelease( p_vout );
115 /* If a video output was provided, lock it, otherwise look for one. */
118 vlc_object_hold( p_vout );
121 /* TODO: find a suitable unused video output */
123 /* If we now have a video output, check it has the right properties */
126 if( !video_format_IsSimilar( &p_vout->p->original, p_fmt ) )
128 /* We are not interested in this format, close this vout */
129 vout_CloseAndRelease( p_vout );
130 vlc_object_release( p_vout );
135 /* This video output is cool! Hijack it. */
136 vlc_object_release( p_vout );
141 msg_Dbg( p_this, "reusing provided vout" );
143 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
144 vlc_object_detach( p_vout );
146 vlc_object_attach( p_vout, p_this );
147 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
153 msg_Dbg( p_this, "no usable vout present, spawning one" );
155 p_vout = vout_Create( p_this, p_fmt );
161 /*****************************************************************************
162 * vout_Create: creates a new video output thread
163 *****************************************************************************
164 * This function creates a new video output thread, and returns a pointer
165 * to its description. On error, it returns NULL.
166 *****************************************************************************/
167 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
169 video_format_t original;
170 if (VoutValidateFormat(&original, p_fmt))
173 /* Allocate descriptor */
174 vout_thread_t *p_vout = vlc_custom_create(p_parent,
175 sizeof(*p_vout) + sizeof(*p_vout->p),
176 VLC_OBJECT_VOUT, "video output");
178 video_format_Clean(&original);
183 p_vout->p = (vout_thread_sys_t*)&p_vout[1];
185 p_vout->p->original = original;
187 vout_control_Init( &p_vout->p->control );
188 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_INIT );
190 vout_statistic_Init( &p_vout->p->statistic );
191 p_vout->p->i_par_num =
192 p_vout->p->i_par_den = 1;
194 vout_snapshot_Init( &p_vout->p->snapshot );
196 /* Initialize locks */
197 vlc_mutex_init( &p_vout->p->picture_lock );
198 vlc_mutex_init( &p_vout->p->vfilter_lock );
200 /* Attach the new object now so we can use var inheritance below */
201 vlc_object_attach( p_vout, p_parent );
203 /* Initialize subpicture unit */
204 p_vout->p->p_spu = spu_Create( p_vout );
207 spu_Init( p_vout->p->p_spu );
209 /* Take care of some "interface/control" related initialisations */
210 vout_IntfInit( p_vout );
212 /* Get splitter name if present */
213 char *splitter_name = var_GetNonEmptyString(p_vout, "vout-filter");
215 if (asprintf(&p_vout->p->splitter_name, "%s,none", splitter_name) < 0)
216 p_vout->p->splitter_name = NULL;
219 p_vout->p->splitter_name = NULL;
223 vout_InitInterlacingSupport( p_vout, p_vout->p->displayed.is_interlaced );
226 vlc_object_set_destructor( p_vout, VoutDestructor );
229 if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
230 VLC_THREAD_PRIORITY_OUTPUT ) )
232 vlc_object_release( p_vout );
235 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
237 vout_control_WaitEmpty( &p_vout->p->control );
239 if (p_vout->p->dead )
241 msg_Err( p_vout, "video output creation failed" );
242 vout_CloseAndRelease( p_vout );
249 /*****************************************************************************
250 * vout_Close: Close a vout created by vout_Create.
251 *****************************************************************************
252 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
253 * You should NEVER call it on vout not obtained through vout_Create
254 * (like with vout_Request or vlc_object_find.)
255 * You can use vout_CloseAndRelease() as a convenience method.
256 *****************************************************************************/
257 void vout_Close( vout_thread_t *p_vout )
261 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
263 vout_snapshot_End( &p_vout->p->snapshot );
265 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_CLEAN );
266 vlc_join( p_vout->p->thread, NULL );
270 static void VoutDestructor( vlc_object_t * p_this )
272 vout_thread_t *p_vout = (vout_thread_t *)p_this;
274 /* Make sure the vout was stopped first */
275 //assert( !p_vout->p_module );
277 free( p_vout->p->splitter_name );
280 spu_Destroy( p_vout->p->p_spu );
282 /* Destroy the locks */
283 vlc_mutex_destroy( &p_vout->p->picture_lock );
284 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
285 vout_control_Clean( &p_vout->p->control );
288 vout_statistic_Clean( &p_vout->p->statistic );
291 vout_snapshot_Clean( &p_vout->p->snapshot );
293 video_format_Clean( &p_vout->p->original );
297 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
299 vout_control_cmd_t cmd;
300 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
301 cmd.u.pause.is_on = is_paused;
302 cmd.u.pause.date = date;
303 vout_control_Push(&vout->p->control, &cmd);
305 vout_control_WaitEmpty(&vout->p->control);
308 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
310 vout_statistic_GetReset( &p_vout->p->statistic,
311 pi_displayed, pi_lost );
314 void vout_Flush(vout_thread_t *vout, mtime_t date)
316 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
317 vout_control_WaitEmpty(&vout->p->control);
320 void vout_Reset(vout_thread_t *vout)
322 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
323 vout_control_WaitEmpty(&vout->p->control);
326 bool vout_IsEmpty(vout_thread_t *vout)
328 vlc_mutex_lock(&vout->p->picture_lock);
330 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
332 picture_Release(picture);
334 vlc_mutex_unlock(&vout->p->picture_lock);
339 void vout_FixLeaks( vout_thread_t *vout )
341 vlc_mutex_lock(&vout->p->picture_lock);
343 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
345 picture = picture_pool_Get(vout->p->decoder_pool);
349 picture_Release(picture);
350 /* Not all pictures has been displayed yet or some are
352 vlc_mutex_unlock(&vout->p->picture_lock);
356 /* There is no reason that no pictures are available, force one
357 * from the pool, becarefull with it though */
358 msg_Err(vout, "pictures leaked, trying to workaround");
361 picture_pool_NonEmpty(vout->p->decoder_pool, false);
363 vlc_mutex_unlock(&vout->p->picture_lock);
365 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
367 vout_control_cmd_t cmd;
368 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
369 cmd.u.time_ptr = duration;
371 vout_control_Push(&vout->p->control, &cmd);
372 vout_control_WaitEmpty(&vout->p->control);
375 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
378 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
381 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
383 spu_DisplaySubpicture(vout->p->p_spu, subpic);
385 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
387 return spu_RegisterChannel(vout->p->p_spu);
389 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
391 spu_ClearChannel(vout->p->p_spu, channel);
394 spu_t *vout_GetSpu( vout_thread_t *p_vout )
396 return p_vout->p->p_spu;
399 /* vout_Control* are usable by anyone at anytime */
400 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
402 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
405 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
407 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
410 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
412 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
415 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
417 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
420 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
421 unsigned num, unsigned den)
423 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
426 void vout_ControlChangeCropRatio(vout_thread_t *vout,
427 unsigned num, unsigned den)
429 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
432 void vout_ControlChangeCropWindow(vout_thread_t *vout,
433 int x, int y, int width, int height)
435 vout_control_cmd_t cmd;
436 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
439 cmd.u.window.width = width;
440 cmd.u.window.height = height;
442 vout_control_Push(&vout->p->control, &cmd);
444 void vout_ControlChangeCropBorder(vout_thread_t *vout,
445 int left, int top, int right, int bottom)
447 vout_control_cmd_t cmd;
448 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
449 cmd.u.border.left = left;
450 cmd.u.border.top = top;
451 cmd.u.border.right = right;
452 cmd.u.border.bottom = bottom;
454 vout_control_Push(&vout->p->control, &cmd);
456 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
458 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
463 static picture_t *VoutVideoFilterNewPicture(filter_t *filter)
465 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
466 return picture_pool_Get(vout->p->private_pool);
468 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
471 picture_Release(picture);
473 static int VoutVideoFilterAllocationSetup(filter_t *filter, void *data)
475 filter->pf_video_buffer_new = VoutVideoFilterNewPicture;
476 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
477 filter->p_owner = data; /* vout */
482 static int ThreadDisplayPicture(vout_thread_t *vout,
483 bool now, mtime_t *deadline)
485 vout_display_t *vd = vout->p->display.vd;
486 int displayed_count = 0;
490 const mtime_t date = mdate();
491 const bool is_paused = vout->p->pause.is_on;
492 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
495 /* FIXME/XXX we must redisplay the last decoded picture (because
496 * of potential vout updated, or filters update or SPU update)
497 * For now a high update period is needed but it coulmd be removed
499 * - vout module emits events from theselves.
500 * - *and* SPU is modified to emit an event or a deadline when needed.
502 * So it will be done latter.
505 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
507 is_forced = peek->b_force || is_paused || now;
508 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
509 picture_Release(peek);
515 /* FIXME a better way for this delay is needed */
516 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
517 if (date_update > date || !vout->p->displayed.decoded) {
518 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
523 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
525 if (*deadline > VOUT_MWAIT_TOLERANCE)
526 *deadline -= VOUT_MWAIT_TOLERANCE;
528 /* If we are too early and can wait, do it */
529 if (date < *deadline && !now)
534 decoded = vout->p->displayed.decoded;
535 vout->p->displayed.decoded = NULL;
537 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
539 if (!is_forced && !vout->p->is_late_dropped) {
540 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
541 const mtime_t late = predicted - decoded->date;
543 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
544 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
545 msg_Warn(vout, "rejected picture because of render time");
547 picture_Release(decoded);
554 vout->p->displayed.is_interlaced = !decoded->b_progressive;
555 vout->p->displayed.qtype = decoded->i_qtype;
557 vout->p->displayed.timestamp = decoded->date;
560 if (vout->p->displayed.decoded)
561 picture_Release(vout->p->displayed.decoded);
562 picture_Hold(decoded);
563 vout->p->displayed.decoded = decoded;
566 vout_chrono_Start(&vout->p->render);
568 picture_t *filtered = NULL;
570 vlc_mutex_lock(&vout->p->vfilter_lock);
571 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
572 //assert(filtered == decoded); // TODO implement
573 vlc_mutex_unlock(&vout->p->vfilter_lock);
579 * Check for subpictures to display
581 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
582 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
583 if (vout->p->pause.is_on)
584 spu_render_time = vout->p->pause.date;
586 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
588 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
595 * - be sure to end up with a direct buffer.
596 * - blend subtitles, and in a fast access buffer
598 picture_t *direct = NULL;
600 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
602 if (vout->p->is_decoder_pool_slow)
603 render = picture_NewFromFormat(&vd->source);
604 else if (vout->p->decoder_pool != vout->p->display_pool)
605 render = picture_pool_Get(vout->p->display_pool);
607 render = picture_pool_Get(vout->p->private_pool);
610 picture_Copy(render, filtered);
612 spu_RenderSubpictures(vout->p->p_spu,
614 subpic, &vd->source, spu_render_time);
616 if (vout->p->is_decoder_pool_slow) {
617 direct = picture_pool_Get(vout->p->display_pool);
619 picture_Copy(direct, render);
620 picture_Release(render);
625 picture_Release(filtered);
632 * Take a snapshot if requested
634 if (direct && do_snapshot)
635 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
637 /* Render the direct buffer returned by vout_RenderPicture */
639 vout_RenderWrapper(vout, direct);
641 vout_chrono_Stop(&vout->p->render);
646 msg_Info(vout, "render: avg %d ms var %d ms",
647 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
652 /* Wait the real date (for rendering jitter) */
654 mwait(decoded->date);
656 /* Display the direct buffer returned by vout_RenderPicture */
657 vout->p->displayed.date = mdate();
659 vout_DisplayWrapper(vout, direct);
665 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
666 if (displayed_count <= 0)
671 static void ThreadManage(vout_thread_t *vout,
673 vout_interlacing_support_t *interlacing,
674 vout_postprocessing_support_t *postprocessing)
676 vlc_mutex_lock(&vout->p->picture_lock);
678 *deadline = VLC_TS_INVALID;
679 ThreadDisplayPicture(vout, false, deadline);
681 const int picture_qtype = vout->p->displayed.qtype;
682 const bool picture_interlaced = vout->p->displayed.is_interlaced;
684 vlc_mutex_unlock(&vout->p->picture_lock);
686 /* Post processing */
687 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
690 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
692 vout_ManageWrapper(vout);
695 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
697 if (!vout->p->title.show)
700 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
701 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
705 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
708 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
709 fmt.video = vout->p->original;
711 vlc_mutex_lock(&vout->p->vfilter_lock);
713 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
714 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
716 msg_Err(vout, "Video filter chain creation failed");
718 vlc_mutex_unlock(&vout->p->vfilter_lock);
721 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
723 assert(!vout->p->pause.is_on || !is_paused);
725 if (vout->p->pause.is_on) {
726 const mtime_t duration = date - vout->p->pause.date;
728 if (vout->p->step.timestamp > VLC_TS_INVALID)
729 vout->p->step.timestamp += duration;
730 if (vout->p->step.last > VLC_TS_INVALID)
731 vout->p->step.last += duration;
732 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
733 if (vout->p->displayed.decoded)
734 vout->p->displayed.decoded->date += duration;
736 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
738 vout->p->step.timestamp = VLC_TS_INVALID;
739 vout->p->step.last = VLC_TS_INVALID;
741 vout->p->pause.is_on = is_paused;
742 vout->p->pause.date = date;
745 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
747 vout->p->step.timestamp = VLC_TS_INVALID;
748 vout->p->step.last = VLC_TS_INVALID;
750 picture_t *last = vout->p->displayed.decoded;
752 if (( below && last->date <= date) ||
753 (!below && last->date >= date)) {
754 picture_Release(last);
756 vout->p->displayed.decoded = NULL;
757 vout->p->displayed.date = VLC_TS_INVALID;
758 vout->p->displayed.timestamp = VLC_TS_INVALID;
761 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
764 static void ThreadReset(vout_thread_t *vout)
766 ThreadFlush(vout, true, INT64_MAX);
767 if (vout->p->decoder_pool)
768 picture_pool_NonEmpty(vout->p->decoder_pool, true);
769 vout->p->pause.is_on = false;
770 vout->p->pause.date = mdate();
773 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
777 if (vout->p->step.last <= VLC_TS_INVALID)
778 vout->p->step.last = vout->p->displayed.timestamp;
781 if (ThreadDisplayPicture(vout, true, &dummy))
784 vout->p->step.timestamp = vout->p->displayed.timestamp;
786 if (vout->p->step.last > VLC_TS_INVALID &&
787 vout->p->step.timestamp > vout->p->step.last) {
788 *duration = vout->p->step.timestamp - vout->p->step.last;
789 vout->p->step.last = vout->p->step.timestamp;
790 /* TODO advance subpicture by the duration ... */
794 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
796 /* FIXME not sure setting "fullscreen" is good ... */
797 var_SetBool(vout, "fullscreen", fullscreen);
798 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
801 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
803 vout_SetWindowState(vout->p->display.vd,
804 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
805 VOUT_WINDOW_STATE_NORMAL);
808 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
810 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
813 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
815 if (num * 10 < den) {
818 } else if (num > den * 10) {
822 vout_SetDisplayZoom(vout->p->display.vd, num, den);
825 static void ThreadChangeAspectRatio(vout_thread_t *vout,
826 unsigned num, unsigned den)
828 const video_format_t *source = &vout->p->original;
830 if (num > 0 && den > 0) {
831 num *= source->i_visible_height;
832 den *= source->i_visible_width;
833 vlc_ureduce(&num, &den, num, den, 0);
835 vout_SetDisplayAspect(vout->p->display.vd, num, den);
839 static void ThreadExecuteCropWindow(vout_thread_t *vout,
840 unsigned crop_num, unsigned crop_den,
841 unsigned x, unsigned y,
842 unsigned width, unsigned height)
844 const video_format_t *source = &vout->p->original;
846 vout_SetDisplayCrop(vout->p->display.vd,
848 source->i_x_offset + x,
849 source->i_y_offset + y,
852 static void ThreadExecuteCropBorder(vout_thread_t *vout,
853 unsigned left, unsigned top,
854 unsigned right, unsigned bottom)
856 const video_format_t *source = &vout->p->original;
857 ThreadExecuteCropWindow(vout, 0, 0,
860 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
861 source->i_visible_width - (left + right),
862 source->i_visible_height - (top + bottom));
865 static void ThreadExecuteCropRatio(vout_thread_t *vout,
866 unsigned num, unsigned den)
868 const video_format_t *source = &vout->p->original;
872 if (num <= 0 || den <= 0) {
877 width = source->i_visible_width;
878 height = source->i_visible_height;
880 unsigned scaled_width = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
881 unsigned scaled_height = (uint64_t)source->i_visible_width * den * source->i_sar_num / num / source->i_sar_den;
883 if (scaled_width < source->i_visible_width) {
884 x = (source->i_visible_width - scaled_width) / 2;
886 width = scaled_width;
887 height = source->i_visible_height;
890 y = (source->i_visible_height - scaled_height) / 2;
891 width = source->i_visible_width;
892 height = scaled_height;
895 ThreadExecuteCropWindow(vout, num, den, x, y, width, height);
898 static int ThreadInit(vout_thread_t *vout)
900 vout->p->dead = false;
901 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
902 vlc_mouse_Init(&vout->p->mouse);
903 vout->p->decoder_fifo = picture_fifo_New();
904 vout->p->decoder_pool = NULL;
905 vout->p->display_pool = NULL;
906 vout->p->private_pool = NULL;
908 vout->p->vfilter_chain =
909 filter_chain_New( vout, "video filter2", false,
910 VoutVideoFilterAllocationSetup, NULL, vout);
912 if (vout_OpenWrapper(vout, vout->p->splitter_name))
914 if (vout_InitWrapper(vout))
916 assert(vout->p->decoder_pool);
918 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
920 vout->p->displayed.decoded = NULL;
921 vout->p->displayed.date = VLC_TS_INVALID;
922 vout->p->displayed.decoded = NULL;
923 vout->p->displayed.timestamp = VLC_TS_INVALID;
924 vout->p->displayed.qtype = QTYPE_NONE;
925 vout->p->displayed.is_interlaced = false;
927 vout->p->step.last = VLC_TS_INVALID;
928 vout->p->step.timestamp = VLC_TS_INVALID;
930 vout->p->pause.is_on = false;
931 vout->p->pause.date = VLC_TS_INVALID;
933 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
937 static void ThreadClean(vout_thread_t *vout)
939 /* Destroy the video filters2 */
940 filter_chain_Delete(vout->p->vfilter_chain);
942 /* Destroy translation tables */
943 if (vout->p->display.vd) {
944 if (vout->p->decoder_pool) {
945 ThreadFlush(vout, true, INT64_MAX);
946 vout_EndWrapper(vout);
948 vout_CloseWrapper(vout);
951 /* Detach subpicture unit from vout */
952 vlc_object_detach(vout->p->p_spu);
954 if (vout->p->decoder_fifo)
955 picture_fifo_Delete(vout->p->decoder_fifo);
956 assert(!vout->p->decoder_pool);
957 vout_chrono_Clean(&vout->p->render);
959 vout->p->dead = true;
960 vout_control_Dead(&vout->p->control);
963 /*****************************************************************************
964 * Thread: video output thread
965 *****************************************************************************
966 * Video output thread. This function does only returns when the thread is
967 * terminated. It handles the pictures arriving in the video heap and the
968 * display device events.
969 *****************************************************************************/
970 static void *Thread(void *object)
972 vout_thread_t *vout = object;
974 vout_interlacing_support_t interlacing = {
975 .is_interlaced = false,
978 vout_postprocessing_support_t postprocessing = {
982 mtime_t deadline = VLC_TS_INVALID;
984 vout_control_cmd_t cmd;
986 /* FIXME remove thoses ugly timeouts
988 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
990 case VOUT_CONTROL_INIT:
991 if (ThreadInit(vout)) {
996 case VOUT_CONTROL_CLEAN:
999 case VOUT_CONTROL_OSD_TITLE:
1000 ThreadDisplayOsdTitle(vout, cmd.u.string);
1002 case VOUT_CONTROL_CHANGE_FILTERS:
1003 ThreadChangeFilters(vout, cmd.u.string);
1005 case VOUT_CONTROL_PAUSE:
1006 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1008 case VOUT_CONTROL_FLUSH:
1009 ThreadFlush(vout, false, cmd.u.time);
1011 case VOUT_CONTROL_RESET:
1014 case VOUT_CONTROL_STEP:
1015 ThreadStep(vout, cmd.u.time_ptr);
1017 case VOUT_CONTROL_FULLSCREEN:
1018 ThreadChangeFullscreen(vout, cmd.u.boolean);
1020 case VOUT_CONTROL_ON_TOP:
1021 ThreadChangeOnTop(vout, cmd.u.boolean);
1023 case VOUT_CONTROL_DISPLAY_FILLED:
1024 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1026 case VOUT_CONTROL_ZOOM:
1027 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1029 case VOUT_CONTROL_ASPECT_RATIO:
1030 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1032 case VOUT_CONTROL_CROP_RATIO:
1033 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1035 case VOUT_CONTROL_CROP_WINDOW:
1036 ThreadExecuteCropWindow(vout, 0, 0,
1037 cmd.u.window.x, cmd.u.window.y,
1038 cmd.u.window.width, cmd.u.window.height);
1040 case VOUT_CONTROL_CROP_BORDER:
1041 ThreadExecuteCropBorder(vout,
1042 cmd.u.border.left, cmd.u.border.top,
1043 cmd.u.border.right, cmd.u.border.bottom);
1048 vout_control_cmd_Clean(&cmd);
1051 ThreadManage(vout, &deadline, &interlacing, &postprocessing);