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);
95 /*****************************************************************************
96 * vout_Request: find a video output thread, create one, or destroy one.
97 *****************************************************************************
98 * This function looks for a video output thread matching the current
99 * properties. If not found, it spawns a new one.
100 *****************************************************************************/
101 vout_thread_t *(vout_Request)( vlc_object_t *p_this, vout_thread_t *p_vout,
102 const video_format_t *p_fmt )
106 /* Video output is no longer used.
107 * TODO: support for reusing video outputs with proper _thread-safe_
108 * reference handling. */
110 vout_CloseAndRelease( p_vout );
114 /* If a video output was provided, lock it, otherwise look for one. */
117 vlc_object_hold( p_vout );
120 /* TODO: find a suitable unused video output */
122 /* If we now have a video output, check it has the right properties */
125 if( !video_format_IsSimilar( &p_vout->p->original, p_fmt ) )
127 /* We are not interested in this format, close this vout */
128 vout_CloseAndRelease( p_vout );
129 vlc_object_release( p_vout );
134 /* This video output is cool! Hijack it. */
135 vlc_object_release( p_vout );
140 msg_Dbg( p_this, "reusing provided vout" );
142 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
143 vlc_object_detach( p_vout );
145 vlc_object_attach( p_vout, p_this );
146 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
152 msg_Dbg( p_this, "no usable vout present, spawning one" );
154 p_vout = vout_Create( p_this, p_fmt );
160 /*****************************************************************************
161 * vout_Create: creates a new video output thread
162 *****************************************************************************
163 * This function creates a new video output thread, and returns a pointer
164 * to its description. On error, it returns NULL.
165 *****************************************************************************/
166 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, const video_format_t *p_fmt )
168 video_format_t original;
169 if (VoutValidateFormat(&original, p_fmt))
172 /* Allocate descriptor */
173 vout_thread_t *p_vout = vlc_custom_create(p_parent,
174 sizeof(*p_vout) + sizeof(*p_vout->p),
175 VLC_OBJECT_VOUT, "video output");
177 video_format_Clean(&original);
182 p_vout->p = (vout_thread_sys_t*)&p_vout[1];
184 p_vout->p->original = original;
186 vout_control_Init( &p_vout->p->control );
187 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_INIT );
189 vout_statistic_Init( &p_vout->p->statistic );
190 p_vout->p->i_par_num =
191 p_vout->p->i_par_den = 1;
193 vout_snapshot_Init( &p_vout->p->snapshot );
195 /* Initialize locks */
196 vlc_mutex_init( &p_vout->p->picture_lock );
197 vlc_mutex_init( &p_vout->p->vfilter_lock );
199 /* Attach the new object now so we can use var inheritance below */
200 vlc_object_attach( p_vout, p_parent );
202 /* Initialize subpicture unit */
203 p_vout->p->p_spu = spu_Create( p_vout );
206 spu_Init( p_vout->p->p_spu );
208 /* Take care of some "interface/control" related initialisations */
209 vout_IntfInit( p_vout );
211 /* Get splitter name if present */
212 char *splitter_name = var_GetNonEmptyString(p_vout, "vout-filter");
214 if (asprintf(&p_vout->p->splitter_name, "%s,none", splitter_name) < 0)
215 p_vout->p->splitter_name = NULL;
218 p_vout->p->splitter_name = NULL;
222 vout_InitInterlacingSupport( p_vout, p_vout->p->displayed.is_interlaced );
225 vlc_object_set_destructor( p_vout, VoutDestructor );
228 if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
229 VLC_THREAD_PRIORITY_OUTPUT ) )
231 vlc_object_release( p_vout );
234 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
236 vout_control_WaitEmpty( &p_vout->p->control );
238 if (p_vout->p->dead )
240 msg_Err( p_vout, "video output creation failed" );
241 vout_CloseAndRelease( p_vout );
248 /*****************************************************************************
249 * vout_Close: Close a vout created by vout_Create.
250 *****************************************************************************
251 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
252 * You should NEVER call it on vout not obtained through vout_Create
253 * (like with vout_Request or vlc_object_find.)
254 * You can use vout_CloseAndRelease() as a convenience method.
255 *****************************************************************************/
256 void vout_Close( vout_thread_t *p_vout )
260 spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
262 vout_snapshot_End( &p_vout->p->snapshot );
264 vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_CLEAN );
265 vlc_join( p_vout->p->thread, NULL );
269 static void VoutDestructor( vlc_object_t * p_this )
271 vout_thread_t *p_vout = (vout_thread_t *)p_this;
273 /* Make sure the vout was stopped first */
274 //assert( !p_vout->p_module );
276 free( p_vout->p->splitter_name );
279 spu_Destroy( p_vout->p->p_spu );
281 /* Destroy the locks */
282 vlc_mutex_destroy( &p_vout->p->picture_lock );
283 vlc_mutex_destroy( &p_vout->p->vfilter_lock );
284 vout_control_Clean( &p_vout->p->control );
287 vout_statistic_Clean( &p_vout->p->statistic );
290 vout_snapshot_Clean( &p_vout->p->snapshot );
292 video_format_Clean( &p_vout->p->original );
296 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
298 vout_control_cmd_t cmd;
299 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
300 cmd.u.pause.is_on = is_paused;
301 cmd.u.pause.date = date;
302 vout_control_Push(&vout->p->control, &cmd);
304 vout_control_WaitEmpty(&vout->p->control);
307 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
309 vout_statistic_GetReset( &p_vout->p->statistic,
310 pi_displayed, pi_lost );
313 void vout_Flush(vout_thread_t *vout, mtime_t date)
315 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
316 vout_control_WaitEmpty(&vout->p->control);
319 void vout_Reset(vout_thread_t *vout)
321 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
322 vout_control_WaitEmpty(&vout->p->control);
325 bool vout_IsEmpty(vout_thread_t *vout)
327 vlc_mutex_lock(&vout->p->picture_lock);
329 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
331 picture_Release(picture);
333 vlc_mutex_unlock(&vout->p->picture_lock);
338 void vout_FixLeaks( vout_thread_t *vout )
340 vlc_mutex_lock(&vout->p->picture_lock);
342 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
344 picture = picture_pool_Get(vout->p->decoder_pool);
348 picture_Release(picture);
349 /* Not all pictures has been displayed yet or some are
351 vlc_mutex_unlock(&vout->p->picture_lock);
355 /* There is no reason that no pictures are available, force one
356 * from the pool, becarefull with it though */
357 msg_Err(vout, "pictures leaked, trying to workaround");
360 picture_pool_NonEmpty(vout->p->decoder_pool, false);
362 vlc_mutex_unlock(&vout->p->picture_lock);
364 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
366 vout_control_cmd_t cmd;
367 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
368 cmd.u.time_ptr = duration;
370 vout_control_Push(&vout->p->control, &cmd);
371 vout_control_WaitEmpty(&vout->p->control);
374 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
377 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
380 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
382 spu_DisplaySubpicture(vout->p->p_spu, subpic);
384 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
386 return spu_RegisterChannel(vout->p->p_spu);
388 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
390 spu_ClearChannel(vout->p->p_spu, channel);
393 spu_t *vout_GetSpu( vout_thread_t *p_vout )
395 return p_vout->p->p_spu;
398 /* vout_Control* are usable by anyone at anytime */
399 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
401 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
404 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
406 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
409 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
411 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
414 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
416 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
419 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
420 unsigned num, unsigned den)
422 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
425 void vout_ControlChangeCropRatio(vout_thread_t *vout,
426 unsigned num, unsigned den)
428 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
431 void vout_ControlChangeCropWindow(vout_thread_t *vout,
432 int x, int y, int width, int height)
434 vout_control_cmd_t cmd;
435 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
438 cmd.u.window.width = width;
439 cmd.u.window.height = height;
441 vout_control_Push(&vout->p->control, &cmd);
443 void vout_ControlChangeCropBorder(vout_thread_t *vout,
444 int left, int top, int right, int bottom)
446 vout_control_cmd_t cmd;
447 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
448 cmd.u.border.left = left;
449 cmd.u.border.top = top;
450 cmd.u.border.right = right;
451 cmd.u.border.bottom = bottom;
453 vout_control_Push(&vout->p->control, &cmd);
455 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
457 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
462 static picture_t *VoutVideoFilterNewPicture(filter_t *filter)
464 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
465 return picture_pool_Get(vout->p->private_pool);
467 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
470 picture_Release(picture);
472 static int VoutVideoFilterAllocationSetup(filter_t *filter, void *data)
474 filter->pf_video_buffer_new = VoutVideoFilterNewPicture;
475 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
476 filter->p_owner = data; /* vout */
481 static int ThreadDisplayPicture(vout_thread_t *vout,
482 bool now, mtime_t *deadline)
484 vout_display_t *vd = vout->p->display.vd;
485 int displayed_count = 0;
489 const mtime_t date = mdate();
490 const bool is_paused = vout->p->pause.is_on;
491 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
494 /* FIXME/XXX we must redisplay the last decoded picture (because
495 * of potential vout updated, or filters update or SPU update)
496 * For now a high update period is needed but it coulmd be removed
498 * - vout module emits events from theselves.
499 * - *and* SPU is modified to emit an event or a deadline when needed.
501 * So it will be done latter.
504 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
506 is_forced = peek->b_force || is_paused || now;
507 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
508 picture_Release(peek);
514 /* FIXME a better way for this delay is needed */
515 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
516 if (date_update > date || !vout->p->displayed.decoded) {
517 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
522 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
524 if (*deadline > VOUT_MWAIT_TOLERANCE)
525 *deadline -= VOUT_MWAIT_TOLERANCE;
527 /* If we are too early and can wait, do it */
528 if (date < *deadline && !now)
533 decoded = vout->p->displayed.decoded;
534 vout->p->displayed.decoded = NULL;
536 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
538 if (!is_forced && !vout->p->is_late_dropped) {
539 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
540 const mtime_t late = predicted - decoded->date;
542 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
543 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
544 msg_Warn(vout, "rejected picture because of render time");
546 picture_Release(decoded);
553 vout->p->displayed.is_interlaced = !decoded->b_progressive;
554 vout->p->displayed.qtype = decoded->i_qtype;
556 vout->p->displayed.timestamp = decoded->date;
559 if (vout->p->displayed.decoded)
560 picture_Release(vout->p->displayed.decoded);
561 picture_Hold(decoded);
562 vout->p->displayed.decoded = decoded;
565 vout_chrono_Start(&vout->p->render);
567 picture_t *filtered = NULL;
569 vlc_mutex_lock(&vout->p->vfilter_lock);
570 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
571 //assert(filtered == decoded); // TODO implement
572 vlc_mutex_unlock(&vout->p->vfilter_lock);
578 * Check for subpictures to display
580 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
581 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
582 if (vout->p->pause.is_on)
583 spu_render_time = vout->p->pause.date;
585 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
587 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
594 * - be sure to end up with a direct buffer.
595 * - blend subtitles, and in a fast access buffer
597 picture_t *direct = NULL;
599 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
601 if (vout->p->is_decoder_pool_slow)
602 render = picture_NewFromFormat(&vd->source);
603 else if (vout->p->decoder_pool != vout->p->display_pool)
604 render = picture_pool_Get(vout->p->display_pool);
606 render = picture_pool_Get(vout->p->private_pool);
609 picture_Copy(render, filtered);
611 spu_RenderSubpictures(vout->p->p_spu,
613 subpic, &vd->source, spu_render_time);
615 if (vout->p->is_decoder_pool_slow) {
616 direct = picture_pool_Get(vout->p->display_pool);
618 picture_Copy(direct, render);
619 picture_Release(render);
624 picture_Release(filtered);
631 * Take a snapshot if requested
633 if (direct && do_snapshot)
634 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
636 /* Render the direct buffer returned by vout_RenderPicture */
638 vout_RenderWrapper(vout, direct);
640 vout_chrono_Stop(&vout->p->render);
645 msg_Info(vout, "render: avg %d ms var %d ms",
646 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
651 /* Wait the real date (for rendering jitter) */
653 mwait(decoded->date);
655 /* Display the direct buffer returned by vout_RenderPicture */
656 vout->p->displayed.date = mdate();
658 vout_DisplayWrapper(vout, direct);
664 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
665 if (displayed_count <= 0)
670 static void ThreadManage(vout_thread_t *vout,
672 vout_interlacing_support_t *interlacing,
673 vout_postprocessing_support_t *postprocessing)
675 vlc_mutex_lock(&vout->p->picture_lock);
677 *deadline = VLC_TS_INVALID;
678 ThreadDisplayPicture(vout, false, deadline);
680 const int picture_qtype = vout->p->displayed.qtype;
681 const bool picture_interlaced = vout->p->displayed.is_interlaced;
683 vlc_mutex_unlock(&vout->p->picture_lock);
685 /* Post processing */
686 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
689 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
691 vout_ManageWrapper(vout);
694 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
696 if (!vout->p->title.show)
699 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
700 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
704 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
707 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
708 fmt.video = vout->p->original;
710 vlc_mutex_lock(&vout->p->vfilter_lock);
712 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
713 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
715 msg_Err(vout, "Video filter chain creation failed");
717 vlc_mutex_unlock(&vout->p->vfilter_lock);
720 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
722 assert(!vout->p->pause.is_on || !is_paused);
724 if (vout->p->pause.is_on) {
725 const mtime_t duration = date - vout->p->pause.date;
727 if (vout->p->step.timestamp > VLC_TS_INVALID)
728 vout->p->step.timestamp += duration;
729 if (vout->p->step.last > VLC_TS_INVALID)
730 vout->p->step.last += duration;
731 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
732 if (vout->p->displayed.decoded)
733 vout->p->displayed.decoded->date += duration;
735 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
737 vout->p->step.timestamp = VLC_TS_INVALID;
738 vout->p->step.last = VLC_TS_INVALID;
740 vout->p->pause.is_on = is_paused;
741 vout->p->pause.date = date;
744 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
746 vout->p->step.timestamp = VLC_TS_INVALID;
747 vout->p->step.last = VLC_TS_INVALID;
749 picture_t *last = vout->p->displayed.decoded;
751 if (( below && last->date <= date) ||
752 (!below && last->date >= date)) {
753 picture_Release(last);
755 vout->p->displayed.decoded = NULL;
756 vout->p->displayed.date = VLC_TS_INVALID;
757 vout->p->displayed.timestamp = VLC_TS_INVALID;
760 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
763 static void ThreadReset(vout_thread_t *vout)
765 ThreadFlush(vout, true, INT64_MAX);
766 if (vout->p->decoder_pool)
767 picture_pool_NonEmpty(vout->p->decoder_pool, true);
768 vout->p->pause.is_on = false;
769 vout->p->pause.date = mdate();
772 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
776 if (vout->p->step.last <= VLC_TS_INVALID)
777 vout->p->step.last = vout->p->displayed.timestamp;
780 if (ThreadDisplayPicture(vout, true, &dummy))
783 vout->p->step.timestamp = vout->p->displayed.timestamp;
785 if (vout->p->step.last > VLC_TS_INVALID &&
786 vout->p->step.timestamp > vout->p->step.last) {
787 *duration = vout->p->step.timestamp - vout->p->step.last;
788 vout->p->step.last = vout->p->step.timestamp;
789 /* TODO advance subpicture by the duration ... */
793 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
795 /* FIXME not sure setting "fullscreen" is good ... */
796 var_SetBool(vout, "fullscreen", fullscreen);
797 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
800 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
802 vout_SetWindowState(vout->p->display.vd,
803 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
804 VOUT_WINDOW_STATE_NORMAL);
807 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
809 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
812 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
814 if (num * 10 < den) {
817 } else if (num > den * 10) {
821 vout_SetDisplayZoom(vout->p->display.vd, num, den);
824 static void ThreadChangeAspectRatio(vout_thread_t *vout,
825 unsigned num, unsigned den)
827 const video_format_t *source = &vout->p->original;
829 if (num > 0 && den > 0) {
830 num *= source->i_visible_height;
831 den *= source->i_visible_width;
832 vlc_ureduce(&num, &den, num, den, 0);
834 vout_SetDisplayAspect(vout->p->display.vd, num, den);
838 static void ThreadExecuteCropWindow(vout_thread_t *vout,
839 unsigned crop_num, unsigned crop_den,
840 unsigned x, unsigned y,
841 unsigned width, unsigned height)
843 const video_format_t *source = &vout->p->original;
845 vout_SetDisplayCrop(vout->p->display.vd,
847 source->i_x_offset + x,
848 source->i_y_offset + y,
851 static void ThreadExecuteCropBorder(vout_thread_t *vout,
852 unsigned left, unsigned top,
853 unsigned right, unsigned bottom)
855 const video_format_t *source = &vout->p->original;
856 ThreadExecuteCropWindow(vout, 0, 0,
859 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
860 source->i_visible_width - (left + right),
861 source->i_visible_height - (top + bottom));
864 static void ThreadExecuteCropRatio(vout_thread_t *vout,
865 unsigned num, unsigned den)
867 const video_format_t *source = &vout->p->original;
871 if (num <= 0 || den <= 0) {
876 width = source->i_visible_width;
877 height = source->i_visible_height;
879 unsigned scaled_width = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
880 unsigned scaled_height = (uint64_t)source->i_visible_width * den * source->i_sar_num / num / source->i_sar_den;
882 if (scaled_width < source->i_visible_width) {
883 x = (source->i_visible_width - scaled_width) / 2;
885 width = scaled_width;
886 height = source->i_visible_height;
889 y = (source->i_visible_height - scaled_height) / 2;
890 width = source->i_visible_width;
891 height = scaled_height;
894 ThreadExecuteCropWindow(vout, num, den, x, y, width, height);
897 static int ThreadInit(vout_thread_t *vout)
899 vout->p->dead = false;
900 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
901 vlc_mouse_Init(&vout->p->mouse);
902 vout->p->decoder_fifo = picture_fifo_New();
903 vout->p->decoder_pool = NULL;
904 vout->p->display_pool = NULL;
905 vout->p->private_pool = NULL;
907 vout->p->vfilter_chain =
908 filter_chain_New( vout, "video filter2", false,
909 VoutVideoFilterAllocationSetup, NULL, vout);
911 if (vout_OpenWrapper(vout, vout->p->splitter_name))
913 if (vout_InitWrapper(vout))
915 assert(vout->p->decoder_pool);
917 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
919 vout->p->displayed.decoded = NULL;
920 vout->p->displayed.date = VLC_TS_INVALID;
921 vout->p->displayed.decoded = NULL;
922 vout->p->displayed.timestamp = VLC_TS_INVALID;
923 vout->p->displayed.qtype = QTYPE_NONE;
924 vout->p->displayed.is_interlaced = false;
926 vout->p->step.last = VLC_TS_INVALID;
927 vout->p->step.timestamp = VLC_TS_INVALID;
929 vout->p->pause.is_on = false;
930 vout->p->pause.date = VLC_TS_INVALID;
932 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
936 static void ThreadClean(vout_thread_t *vout)
938 /* Destroy the video filters2 */
939 filter_chain_Delete(vout->p->vfilter_chain);
941 /* Destroy translation tables */
942 if (vout->p->display.vd) {
943 if (vout->p->decoder_pool) {
944 ThreadFlush(vout, true, INT64_MAX);
945 vout_EndWrapper(vout);
947 vout_CloseWrapper(vout);
950 /* Detach subpicture unit from vout */
951 vlc_object_detach(vout->p->p_spu);
953 if (vout->p->decoder_fifo)
954 picture_fifo_Delete(vout->p->decoder_fifo);
955 assert(!vout->p->decoder_pool);
956 vout_chrono_Clean(&vout->p->render);
958 vout->p->dead = true;
959 vout_control_Dead(&vout->p->control);
962 /*****************************************************************************
963 * Thread: video output thread
964 *****************************************************************************
965 * Video output thread. This function does only returns when the thread is
966 * terminated. It handles the pictures arriving in the video heap and the
967 * display device events.
968 *****************************************************************************/
969 static void *Thread(void *object)
971 vout_thread_t *vout = object;
973 vout_interlacing_support_t interlacing = {
974 .is_interlaced = false,
977 vout_postprocessing_support_t postprocessing = {
981 mtime_t deadline = VLC_TS_INVALID;
983 vout_control_cmd_t cmd;
985 /* FIXME remove thoses ugly timeouts
987 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
989 case VOUT_CONTROL_INIT:
990 if (ThreadInit(vout)) {
995 case VOUT_CONTROL_CLEAN:
998 case VOUT_CONTROL_OSD_TITLE:
999 ThreadDisplayOsdTitle(vout, cmd.u.string);
1001 case VOUT_CONTROL_CHANGE_FILTERS:
1002 ThreadChangeFilters(vout, cmd.u.string);
1004 case VOUT_CONTROL_PAUSE:
1005 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1007 case VOUT_CONTROL_FLUSH:
1008 ThreadFlush(vout, false, cmd.u.time);
1010 case VOUT_CONTROL_RESET:
1013 case VOUT_CONTROL_STEP:
1014 ThreadStep(vout, cmd.u.time_ptr);
1016 case VOUT_CONTROL_FULLSCREEN:
1017 ThreadChangeFullscreen(vout, cmd.u.boolean);
1019 case VOUT_CONTROL_ON_TOP:
1020 ThreadChangeOnTop(vout, cmd.u.boolean);
1022 case VOUT_CONTROL_DISPLAY_FILLED:
1023 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1025 case VOUT_CONTROL_ZOOM:
1026 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1028 case VOUT_CONTROL_ASPECT_RATIO:
1029 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1031 case VOUT_CONTROL_CROP_RATIO:
1032 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1034 case VOUT_CONTROL_CROP_WINDOW:
1035 ThreadExecuteCropWindow(vout, 0, 0,
1036 cmd.u.window.x, cmd.u.window.y,
1037 cmd.u.window.width, cmd.u.window.height);
1039 case VOUT_CONTROL_CROP_BORDER:
1040 ThreadExecuteCropBorder(vout,
1041 cmd.u.border.left, cmd.u.border.top,
1042 cmd.u.border.right, cmd.u.border.bottom);
1047 vout_control_cmd_Clean(&cmd);
1050 ThreadManage(vout, &deadline, &interlacing, &postprocessing);