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(4000))
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 static vout_thread_t *VoutCreate(vlc_object_t *object,
96 const vout_configuration_t *cfg)
98 video_format_t original;
99 if (VoutValidateFormat(&original, cfg->fmt))
102 /* Allocate descriptor */
103 vout_thread_t *vout = vlc_custom_create(object,
104 sizeof(*vout) + sizeof(*vout->p),
105 VLC_OBJECT_VOUT, "video output");
107 video_format_Clean(&original);
112 vout->p = (vout_thread_sys_t*)&vout[1];
114 vout->p->original = original;
115 vout->p->dpb_size = cfg->dpb_size;
117 vout_control_Init(&vout->p->control);
118 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
120 vout_statistic_Init(&vout->p->statistic);
122 vout_snapshot_Init(&vout->p->snapshot);
124 /* Initialize locks */
125 vlc_mutex_init(&vout->p->picture_lock);
126 vlc_mutex_init(&vout->p->filter.lock);
127 vlc_mutex_init(&vout->p->spu_lock);
129 /* Attach the new object now so we can use var inheritance below */
130 vlc_object_attach(vout, object);
132 /* Initialize subpicture unit */
133 vout->p->p_spu = spu_Create(vout);
135 /* Take care of some "interface/control" related initialisations */
138 vout->p->title.show = var_GetBool(vout, "video-title-show");
139 vout->p->title.timeout = var_GetInteger(vout, "video-title-timeout");
140 vout->p->title.position = var_GetInteger(vout, "video-title-position");
142 /* Get splitter name if present */
143 char *splitter_name = var_GetNonEmptyString(vout, "vout-filter");
145 if (asprintf(&vout->p->splitter_name, "%s,none", splitter_name) < 0)
146 vout->p->splitter_name = NULL;
149 vout->p->splitter_name = NULL;
153 vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
156 vlc_object_set_destructor(vout, VoutDestructor);
159 if (vlc_clone(&vout->p->thread, Thread, vout,
160 VLC_THREAD_PRIORITY_OUTPUT)) {
161 spu_Destroy(vout->p->p_spu);
162 vlc_object_release(vout);
166 vout_control_WaitEmpty(&vout->p->control);
169 msg_Err(vout, "video output creation failed");
170 vout_CloseAndRelease(vout);
174 vout->p->input = cfg->input;
176 spu_Attach(vout->p->p_spu, vout->p->input, true);
181 vout_thread_t *(vout_Request)(vlc_object_t *object,
182 const vout_configuration_t *cfg)
184 vout_thread_t *vout = cfg->vout;
185 if (cfg->change_fmt && !cfg->fmt) {
187 vout_CloseAndRelease(vout);
191 /* If a vout is provided, try reusing it */
193 if (vout->p->input != cfg->input) {
195 spu_Attach(vout->p->p_spu, vout->p->input, false);
196 vout->p->input = cfg->input;
198 spu_Attach(vout->p->p_spu, vout->p->input, true);
201 if (cfg->change_fmt) {
202 vout_control_cmd_t cmd;
203 vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
206 vout_control_Push(&vout->p->control, &cmd);
207 vout_control_WaitEmpty(&vout->p->control);
210 if (!vout->p->dead) {
211 msg_Dbg(object, "reusing provided vout");
214 vout_CloseAndRelease(vout);
216 msg_Warn(object, "cannot reuse provided vout");
218 return VoutCreate(object, cfg);
221 void vout_Close(vout_thread_t *vout)
226 spu_Attach(vout->p->p_spu, vout->p->input, false);
228 vout_snapshot_End(&vout->p->snapshot);
230 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
231 vlc_join(vout->p->thread, NULL);
233 vlc_mutex_lock(&vout->p->spu_lock);
234 spu_Destroy(vout->p->p_spu);
235 vout->p->p_spu = NULL;
236 vlc_mutex_unlock(&vout->p->spu_lock);
240 static void VoutDestructor(vlc_object_t *object)
242 vout_thread_t *vout = (vout_thread_t *)object;
244 /* Make sure the vout was stopped first */
245 //assert(!vout->p_module);
247 free(vout->p->splitter_name);
249 /* Destroy the locks */
250 vlc_mutex_destroy(&vout->p->spu_lock);
251 vlc_mutex_destroy(&vout->p->picture_lock);
252 vlc_mutex_destroy(&vout->p->filter.lock);
253 vout_control_Clean(&vout->p->control);
256 vout_statistic_Clean(&vout->p->statistic);
259 vout_snapshot_Clean(&vout->p->snapshot);
261 video_format_Clean(&vout->p->original);
265 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
267 vout_control_cmd_t cmd;
268 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
269 cmd.u.pause.is_on = is_paused;
270 cmd.u.pause.date = date;
271 vout_control_Push(&vout->p->control, &cmd);
273 vout_control_WaitEmpty(&vout->p->control);
276 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
278 vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
281 void vout_Flush(vout_thread_t *vout, mtime_t date)
283 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
284 vout_control_WaitEmpty(&vout->p->control);
287 void vout_Reset(vout_thread_t *vout)
289 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
290 vout_control_WaitEmpty(&vout->p->control);
293 bool vout_IsEmpty(vout_thread_t *vout)
295 vlc_mutex_lock(&vout->p->picture_lock);
297 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
299 picture_Release(picture);
301 vlc_mutex_unlock(&vout->p->picture_lock);
306 void vout_FixLeaks( vout_thread_t *vout )
308 vlc_mutex_lock(&vout->p->picture_lock);
310 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
312 picture = picture_pool_Get(vout->p->decoder_pool);
316 picture_Release(picture);
317 /* Not all pictures has been displayed yet or some are
319 vlc_mutex_unlock(&vout->p->picture_lock);
323 /* There is no reason that no pictures are available, force one
324 * from the pool, becarefull with it though */
325 msg_Err(vout, "pictures leaked, trying to workaround");
328 picture_pool_NonEmpty(vout->p->decoder_pool, false);
330 vlc_mutex_unlock(&vout->p->picture_lock);
332 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
334 vout_control_cmd_t cmd;
335 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
336 cmd.u.time_ptr = duration;
338 vout_control_Push(&vout->p->control, &cmd);
339 vout_control_WaitEmpty(&vout->p->control);
342 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
345 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
348 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
350 vout_control_cmd_t cmd;
351 vout_control_cmd_Init(&cmd, VOUT_CONTROL_SUBPICTURE);
352 cmd.u.subpicture = subpic;
354 vout_control_Push(&vout->p->control, &cmd);
356 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
358 int channel = SPU_DEFAULT_CHANNEL;
360 vlc_mutex_lock(&vout->p->spu_lock);
362 channel = spu_RegisterChannel(vout->p->p_spu);
363 vlc_mutex_unlock(&vout->p->spu_lock);
367 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
369 vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_FLUSH_SUBPICTURE,
374 * It retreives a picture from the vout or NULL if no pictures are
377 * You MUST call vout_PutPicture or vout_ReleasePicture on it.
379 * You may use vout_HoldPicture(paired with vout_ReleasePicture) to keep a
380 * read-only reference.
382 picture_t *vout_GetPicture(vout_thread_t *vout)
385 vlc_mutex_lock(&vout->p->picture_lock);
386 picture_t *picture = picture_pool_Get(vout->p->decoder_pool);
388 picture_Reset(picture);
389 picture->p_next = NULL;
391 vlc_mutex_unlock(&vout->p->picture_lock);
397 * It gives to the vout a picture to be displayed.
399 * The given picture MUST comes from vout_GetPicture.
401 * Becareful, after vout_PutPicture is called, picture_t::p_next cannot be
404 void vout_PutPicture(vout_thread_t *vout, picture_t *picture)
406 vlc_mutex_lock(&vout->p->picture_lock);
408 picture->p_next = NULL;
409 picture_fifo_Push(vout->p->decoder_fifo, picture);
411 vlc_mutex_unlock(&vout->p->picture_lock);
413 vout_control_Wake(&vout->p->control);
417 * It releases a picture retreived by vout_GetPicture.
419 void vout_ReleasePicture(vout_thread_t *vout, picture_t *picture)
421 vlc_mutex_lock(&vout->p->picture_lock);
423 picture_Release(picture);
425 vlc_mutex_unlock(&vout->p->picture_lock);
427 vout_control_Wake(&vout->p->control);
431 * It increment the reference counter of a picture retreived by
434 void vout_HoldPicture(vout_thread_t *vout, picture_t *picture)
436 vlc_mutex_lock(&vout->p->picture_lock);
438 picture_Hold(picture);
440 vlc_mutex_unlock(&vout->p->picture_lock);
443 /* vout_Control* are usable by anyone at anytime */
444 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
446 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
449 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
451 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
454 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
456 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
459 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
461 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
464 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
465 unsigned num, unsigned den)
467 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
470 void vout_ControlChangeCropRatio(vout_thread_t *vout,
471 unsigned num, unsigned den)
473 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
476 void vout_ControlChangeCropWindow(vout_thread_t *vout,
477 int x, int y, int width, int height)
479 vout_control_cmd_t cmd;
480 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
483 cmd.u.window.width = width;
484 cmd.u.window.height = height;
486 vout_control_Push(&vout->p->control, &cmd);
488 void vout_ControlChangeCropBorder(vout_thread_t *vout,
489 int left, int top, int right, int bottom)
491 vout_control_cmd_t cmd;
492 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
493 cmd.u.border.left = left;
494 cmd.u.border.top = top;
495 cmd.u.border.right = right;
496 cmd.u.border.bottom = bottom;
498 vout_control_Push(&vout->p->control, &cmd);
500 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
502 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
505 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
507 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
510 void vout_ControlChangeSubMargin(vout_thread_t *vout, int margin)
512 vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_MARGIN,
517 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
519 /* Load configuration */
520 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
521 cfg->display.title = title;
522 const int display_width = var_CreateGetInteger(vout, "width");
523 const int display_height = var_CreateGetInteger(vout, "height");
524 cfg->display.width = display_width > 0 ? display_width : 0;
525 cfg->display.height = display_height > 0 ? display_height : 0;
526 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
527 unsigned msar_num, msar_den;
528 if (var_InheritURational(vout, &msar_num, &msar_den, "monitor-par") ||
529 msar_num <= 0 || msar_den <= 0) {
533 cfg->display.sar.num = msar_num;
534 cfg->display.sar.den = msar_den;
535 unsigned zoom_den = 1000;
536 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
537 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
538 cfg->zoom.num = zoom_num;
539 cfg->zoom.den = zoom_den;
540 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
541 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
542 const int align_mask = var_CreateGetInteger(vout, "align");
543 if (align_mask & 0x1)
544 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
545 else if (align_mask & 0x2)
546 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
547 if (align_mask & 0x4)
548 cfg->align.vertical = VOUT_DISPLAY_ALIGN_TOP;
549 else if (align_mask & 0x8)
550 cfg->align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
553 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
554 const vout_window_cfg_t *cfg)
557 vout_window_cfg_t cfg_override = *cfg;
559 if (!var_InheritBool( vout, "embedded-video"))
560 cfg_override.is_standalone = true;
562 if (vout->p->window.is_unused && vout->p->window.object) {
563 assert(!vout->p->splitter_name);
564 if (!cfg_override.is_standalone == !vout->p->window.cfg.is_standalone &&
565 cfg_override.type == vout->p->window.cfg.type) {
566 /* Reuse the stored window */
567 msg_Dbg(vout, "Reusing previous vout window");
568 vout_window_t *window = vout->p->window.object;
569 if (cfg_override.width != vout->p->window.cfg.width ||
570 cfg_override.height != vout->p->window.cfg.height)
571 vout_window_SetSize(window,
572 cfg_override.width, cfg_override.height);
573 vout->p->window.is_unused = false;
574 vout->p->window.cfg = cfg_override;
578 vout_window_Delete(vout->p->window.object);
579 vout->p->window.is_unused = true;
580 vout->p->window.object = NULL;
583 vout_window_t *window = vout_window_New(VLC_OBJECT(vout), "$window",
587 if (!vout->p->splitter_name) {
588 vout->p->window.is_unused = false;
589 vout->p->window.cfg = cfg_override;
590 vout->p->window.object = window;
595 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
596 vout_window_t *window)
599 if (!vout->p->window.is_unused && vout->p->window.object == window) {
600 vout->p->window.is_unused = true;
601 } else if (vout->p->window.is_unused && vout->p->window.object && !window) {
602 vout_window_Delete(vout->p->window.object);
603 vout->p->window.is_unused = true;
604 vout->p->window.object = NULL;
606 vout_window_Delete(window);
612 static picture_t *VoutVideoFilterInteractiveNewPicture(filter_t *filter)
614 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
616 return picture_pool_Get(vout->p->private_pool);
618 static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
620 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
622 vlc_assert_locked(&vout->p->filter.lock);
623 if (filter_chain_GetLength(vout->p->filter.chain_interactive) == 0)
624 return VoutVideoFilterInteractiveNewPicture(filter);
625 return picture_NewFromFormat(&filter->fmt_out.video);
627 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
630 picture_Release(picture);
632 static int VoutVideoFilterStaticAllocationSetup(filter_t *filter, void *data)
634 filter->pf_video_buffer_new = VoutVideoFilterStaticNewPicture;
635 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
636 filter->p_owner = data; /* vout */
639 static int VoutVideoFilterInteractiveAllocationSetup(filter_t *filter, void *data)
641 filter->pf_video_buffer_new = VoutVideoFilterInteractiveNewPicture;
642 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
643 filter->p_owner = data; /* vout */
648 static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_late_dropped)
652 vlc_mutex_lock(&vout->p->filter.lock);
654 picture_t *picture = filter_chain_VideoFilter(vout->p->filter.chain_static, NULL);
655 assert(!reuse || !picture);
659 if (reuse && vout->p->displayed.decoded) {
660 decoded = picture_Hold(vout->p->displayed.decoded);
662 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
663 if (is_late_dropped && decoded && !decoded->b_force) {
664 const mtime_t predicted = mdate() + 0; /* TODO improve */
665 const mtime_t late = predicted - decoded->date;
666 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
667 msg_Warn(vout, "picture is too late to be displayed (missing %d ms)", (int)(late/1000));
668 picture_Release(decoded);
671 } else if (late > 0) {
672 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
680 if (vout->p->displayed.decoded)
681 picture_Release(vout->p->displayed.decoded);
683 vout->p->displayed.decoded = picture_Hold(decoded);
684 vout->p->displayed.timestamp = decoded->date;
685 vout->p->displayed.is_interlaced = !decoded->b_progressive;
686 vout->p->displayed.qtype = decoded->i_qtype;
688 picture = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
691 vlc_mutex_unlock(&vout->p->filter.lock);
693 vout_statistic_Update(&vout->p->statistic, 0, lost_count);
697 assert(!vout->p->displayed.next);
698 if (!vout->p->displayed.current)
699 vout->p->displayed.current = picture;
701 vout->p->displayed.next = picture;
705 static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
707 vout_display_t *vd = vout->p->display.vd;
709 picture_t *torender = picture_Hold(vout->p->displayed.current);
711 vout_chrono_Start(&vout->p->render);
713 vlc_mutex_lock(&vout->p->filter.lock);
714 picture_t *filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, torender);
715 vlc_mutex_unlock(&vout->p->filter.lock);
720 if (filtered->date != vout->p->displayed.current->date)
721 msg_Warn(vout, "Unsupported timestamp modifications done by chain_interactive");
724 * Check for subpictures to display
726 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
727 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
728 if (vout->p->pause.is_on)
729 spu_render_time = vout->p->pause.date;
731 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
733 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
740 * - be sure to end up with a direct buffer.
741 * - blend subtitles, and in a fast access buffer
743 picture_t *direct = NULL;
745 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
747 if (vout->p->is_decoder_pool_slow)
748 render = picture_NewFromFormat(&vd->source);
749 else if (vout->p->decoder_pool != vout->p->display_pool)
750 render = picture_pool_Get(vout->p->display_pool);
752 render = picture_pool_Get(vout->p->private_pool);
755 picture_Copy(render, filtered);
757 spu_RenderSubpictures(vout->p->p_spu,
759 subpic, &vd->source, spu_render_time);
761 if (vout->p->is_decoder_pool_slow) {
762 direct = picture_pool_Get(vout->p->display_pool);
764 picture_Copy(direct, render);
765 picture_Release(render);
770 picture_Release(filtered);
780 * Take a snapshot if requested
783 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
785 /* Render the direct buffer returned by vout_RenderPicture */
786 vout_RenderWrapper(vout, direct);
788 vout_chrono_Stop(&vout->p->render);
793 msg_Info(vout, "render: avg %d ms var %d ms",
794 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
798 /* Wait the real date (for rendering jitter) */
800 mtime_t delay = direct->date - mdate();
802 msg_Warn(vout, "picture is late (%lld ms)", delay / 1000);
807 /* Display the direct buffer returned by vout_RenderPicture */
808 vout->p->displayed.date = mdate();
810 vout_DisplayWrapper(vout, direct);
812 vout_statistic_Update(&vout->p->statistic, 1, 0);
817 static int ThreadDisplayPicture(vout_thread_t *vout,
818 bool now, mtime_t *deadline)
820 bool is_late_dropped = vout->p->is_late_dropped && !vout->p->pause.is_on && !now;
821 bool first = !vout->p->displayed.current;
822 if (first && ThreadDisplayPreparePicture(vout, true, is_late_dropped)) /* FIXME not sure it is ok */
824 if (!vout->p->pause.is_on || now) {
825 while (!vout->p->displayed.next) {
826 if (ThreadDisplayPreparePicture(vout, false, is_late_dropped)) {
832 const mtime_t date = mdate();
833 const mtime_t render_delay = vout_chrono_GetHigh(&vout->p->render) + VOUT_MWAIT_TOLERANCE;
835 mtime_t date_next = VLC_TS_INVALID;
836 if (!vout->p->pause.is_on && vout->p->displayed.next)
837 date_next = vout->p->displayed.next->date - render_delay;
839 /* FIXME/XXX we must redisplay the last decoded picture (because
840 * of potential vout updated, or filters update or SPU update)
841 * For now a high update period is needed but it coulmd be removed
843 * - vout module emits events from theselves.
844 * - *and* SPU is modified to emit an event or a deadline when needed.
846 * So it will be done latter.
848 mtime_t date_refresh = VLC_TS_INVALID;
849 if (vout->p->displayed.date > VLC_TS_INVALID)
850 date_refresh = vout->p->displayed.date + VOUT_REDISPLAY_DELAY - render_delay;
853 if (date_next != VLC_TS_INVALID)
854 drop |= date_next + 0 <= date;
856 bool refresh = false;
857 if (date_refresh > VLC_TS_INVALID)
858 refresh = date_refresh <= date;
860 if (!first && !refresh && !drop) {
861 if (date_next != VLC_TS_INVALID && date_refresh != VLC_TS_INVALID)
862 *deadline = __MIN(date_next, date_refresh);
863 else if (date_next != VLC_TS_INVALID)
864 *deadline = date_next;
865 else if (date_refresh != VLC_TS_INVALID)
866 *deadline = date_refresh;
871 picture_Release(vout->p->displayed.current);
872 vout->p->displayed.current = vout->p->displayed.next;
873 vout->p->displayed.next = NULL;
875 if (!vout->p->displayed.current)
878 bool is_forced = now || (!drop && refresh) || vout->p->displayed.current->b_force;
879 return ThreadDisplayRenderPicture(vout, is_forced);
882 static void ThreadManage(vout_thread_t *vout,
884 vout_interlacing_support_t *interlacing,
885 vout_postprocessing_support_t *postprocessing)
887 vlc_mutex_lock(&vout->p->picture_lock);
889 *deadline = VLC_TS_INVALID;
891 if (ThreadDisplayPicture(vout, false, deadline))
895 const int picture_qtype = vout->p->displayed.qtype;
896 const bool picture_interlaced = vout->p->displayed.is_interlaced;
898 vlc_mutex_unlock(&vout->p->picture_lock);
900 /* Post processing */
901 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
904 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
906 vout_ManageWrapper(vout);
909 static void ThreadDisplaySubpicture(vout_thread_t *vout,
910 subpicture_t *subpicture)
912 spu_DisplaySubpicture(vout->p->p_spu, subpicture);
915 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
917 spu_ClearChannel(vout->p->p_spu, channel);
920 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
922 if (!vout->p->title.show)
925 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
926 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
930 static void ThreadFilterFlush(vout_thread_t *vout)
932 if (vout->p->displayed.current)
933 picture_Release( vout->p->displayed.current );
934 vout->p->displayed.current = NULL;
936 if (vout->p->displayed.next)
937 picture_Release( vout->p->displayed.next );
938 vout->p->displayed.next = NULL;
940 vlc_mutex_lock(&vout->p->filter.lock);
941 filter_chain_VideoFlush(vout->p->filter.chain_static);
942 filter_chain_VideoFlush(vout->p->filter.chain_interactive);
943 vlc_mutex_unlock(&vout->p->filter.lock);
951 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
953 ThreadFilterFlush(vout);
955 vlc_array_t array_static;
956 vlc_array_t array_interactive;
958 vlc_array_init(&array_static);
959 vlc_array_init(&array_interactive);
960 char *current = filters ? strdup(filters) : NULL;
964 char *next = config_ChainCreate(&name, &cfg, current);
967 vout_filter_t *e = xmalloc(sizeof(*e));
970 if (!strcmp(e->name, "deinterlace") ||
971 !strcmp(e->name, "postproc")) {
972 vlc_array_append(&array_static, e);
974 vlc_array_append(&array_interactive, e);
978 config_ChainDestroy(cfg);
986 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
987 fmt.video = vout->p->original;
989 vlc_mutex_lock(&vout->p->filter.lock);
991 filter_chain_Reset(vout->p->filter.chain_static, &fmt, &fmt);
992 filter_chain_Reset(vout->p->filter.chain_interactive, &fmt, &fmt);
994 for (int a = 0; a < 2; a++) {
995 vlc_array_t *array = a == 0 ? &array_static :
997 filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
998 vout->p->filter.chain_interactive;
1000 for (int i = 0; i < vlc_array_count(array); i++) {
1001 vout_filter_t *e = vlc_array_item_at_index(array, i);
1002 msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
1003 if (!filter_chain_AppendFilter(chain, e->name, e->cfg, NULL, NULL)) {
1004 msg_Err(vout, "Failed to add filter '%s'", e->name);
1005 config_ChainDestroy(e->cfg);
1010 vlc_array_clear(array);
1013 vlc_mutex_unlock(&vout->p->filter.lock);
1016 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
1018 spu_ChangeFilters(vout->p->p_spu, filters);
1020 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
1022 spu_ChangeMargin(vout->p->p_spu, margin);
1025 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
1027 assert(!vout->p->pause.is_on || !is_paused);
1029 if (vout->p->pause.is_on) {
1030 const mtime_t duration = date - vout->p->pause.date;
1032 if (vout->p->step.timestamp > VLC_TS_INVALID)
1033 vout->p->step.timestamp += duration;
1034 if (vout->p->step.last > VLC_TS_INVALID)
1035 vout->p->step.last += duration;
1036 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
1037 if (vout->p->displayed.decoded)
1038 vout->p->displayed.decoded->date += duration;
1039 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
1041 ThreadFilterFlush(vout);
1043 vout->p->step.timestamp = VLC_TS_INVALID;
1044 vout->p->step.last = VLC_TS_INVALID;
1046 vout->p->pause.is_on = is_paused;
1047 vout->p->pause.date = date;
1050 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
1052 vout->p->step.timestamp = VLC_TS_INVALID;
1053 vout->p->step.last = VLC_TS_INVALID;
1055 ThreadFilterFlush(vout); /* FIXME too much */
1057 picture_t *last = vout->p->displayed.decoded;
1059 if (( below && last->date <= date) ||
1060 (!below && last->date >= date)) {
1061 picture_Release(last);
1063 vout->p->displayed.decoded = NULL;
1064 vout->p->displayed.date = VLC_TS_INVALID;
1065 vout->p->displayed.timestamp = VLC_TS_INVALID;
1069 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
1072 static void ThreadReset(vout_thread_t *vout)
1074 ThreadFlush(vout, true, INT64_MAX);
1075 if (vout->p->decoder_pool)
1076 picture_pool_NonEmpty(vout->p->decoder_pool, true);
1077 vout->p->pause.is_on = false;
1078 vout->p->pause.date = mdate();
1081 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
1085 if (vout->p->step.last <= VLC_TS_INVALID)
1086 vout->p->step.last = vout->p->displayed.timestamp;
1089 if (ThreadDisplayPicture(vout, true, &dummy))
1092 vout->p->step.timestamp = vout->p->displayed.timestamp;
1094 if (vout->p->step.last > VLC_TS_INVALID &&
1095 vout->p->step.timestamp > vout->p->step.last) {
1096 *duration = vout->p->step.timestamp - vout->p->step.last;
1097 vout->p->step.last = vout->p->step.timestamp;
1098 /* TODO advance subpicture by the duration ... */
1102 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
1104 /* FIXME not sure setting "fullscreen" is good ... */
1105 var_SetBool(vout, "fullscreen", fullscreen);
1106 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
1109 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
1111 vout_SetWindowState(vout->p->display.vd,
1112 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
1113 VOUT_WINDOW_STATE_NORMAL);
1116 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1118 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1121 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1123 if (num * 10 < den) {
1126 } else if (num > den * 10) {
1130 vout_SetDisplayZoom(vout->p->display.vd, num, den);
1133 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1134 unsigned num, unsigned den)
1136 const video_format_t *source = &vout->p->original;
1138 if (num > 0 && den > 0) {
1139 num *= source->i_visible_height;
1140 den *= source->i_visible_width;
1141 vlc_ureduce(&num, &den, num, den, 0);
1143 vout_SetDisplayAspect(vout->p->display.vd, num, den);
1147 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1148 unsigned crop_num, unsigned crop_den,
1149 unsigned x, unsigned y,
1150 unsigned width, unsigned height)
1152 const video_format_t *source = &vout->p->original;
1154 vout_SetDisplayCrop(vout->p->display.vd,
1156 source->i_x_offset + x,
1157 source->i_y_offset + y,
1160 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1161 unsigned left, unsigned top,
1162 unsigned right, unsigned bottom)
1164 const video_format_t *source = &vout->p->original;
1165 ThreadExecuteCropWindow(vout, 0, 0,
1168 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
1169 source->i_visible_width - (left + right),
1170 source->i_visible_height - (top + bottom));
1173 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1174 unsigned num, unsigned den)
1176 const video_format_t *source = &vout->p->original;
1177 ThreadExecuteCropWindow(vout, num, den,
1179 source->i_visible_width,
1180 source->i_visible_height);
1183 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
1185 vlc_mouse_Init(&vout->p->mouse);
1186 vout->p->decoder_fifo = picture_fifo_New();
1187 vout->p->decoder_pool = NULL;
1188 vout->p->display_pool = NULL;
1189 vout->p->private_pool = NULL;
1191 vout->p->filter.chain_static =
1192 filter_chain_New( vout, "video filter2", false,
1193 VoutVideoFilterStaticAllocationSetup, NULL, vout);
1194 vout->p->filter.chain_interactive =
1195 filter_chain_New( vout, "video filter2", false,
1196 VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
1198 vout_display_state_t state_default;
1200 VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1201 state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
1202 VOUT_WINDOW_STATE_NORMAL;
1203 state_default.sar.num = 0;
1204 state_default.sar.den = 0;
1206 state = &state_default;
1209 if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1210 return VLC_EGENERIC;
1211 if (vout_InitWrapper(vout))
1212 return VLC_EGENERIC;
1213 assert(vout->p->decoder_pool);
1215 vout->p->displayed.current = NULL;
1216 vout->p->displayed.next = NULL;
1217 vout->p->displayed.decoded = NULL;
1218 vout->p->displayed.date = VLC_TS_INVALID;
1219 vout->p->displayed.timestamp = VLC_TS_INVALID;
1220 vout->p->displayed.qtype = QTYPE_NONE;
1221 vout->p->displayed.is_interlaced = false;
1223 vout->p->step.last = VLC_TS_INVALID;
1224 vout->p->step.timestamp = VLC_TS_INVALID;
1226 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1230 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1232 /* Destroy translation tables */
1233 if (vout->p->display.vd) {
1234 if (vout->p->decoder_pool) {
1235 ThreadFlush(vout, true, INT64_MAX);
1236 vout_EndWrapper(vout);
1238 vout_CloseWrapper(vout, state);
1241 /* Destroy the video filters2 */
1242 filter_chain_Delete(vout->p->filter.chain_interactive);
1243 filter_chain_Delete(vout->p->filter.chain_static);
1245 if (vout->p->decoder_fifo)
1246 picture_fifo_Delete(vout->p->decoder_fifo);
1247 assert(!vout->p->decoder_pool);
1250 static void ThreadInit(vout_thread_t *vout)
1252 vout->p->window.is_unused = true;
1253 vout->p->window.object = NULL;
1254 vout->p->dead = false;
1255 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
1256 vout->p->pause.is_on = false;
1257 vout->p->pause.date = VLC_TS_INVALID;
1259 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1262 static void ThreadClean(vout_thread_t *vout)
1264 if (vout->p->window.object) {
1265 assert(vout->p->window.is_unused);
1266 vout_window_Delete(vout->p->window.object);
1268 vout_chrono_Clean(&vout->p->render);
1269 vout->p->dead = true;
1270 vout_control_Dead(&vout->p->control);
1273 static int ThreadReinit(vout_thread_t *vout,
1274 const vout_configuration_t *cfg)
1276 video_format_t original;
1277 if (VoutValidateFormat(&original, cfg->fmt)) {
1278 ThreadStop(vout, NULL);
1280 return VLC_EGENERIC;
1282 if (video_format_IsSimilar(&original, &vout->p->original)) {
1283 if (cfg->dpb_size <= vout->p->dpb_size)
1285 msg_Warn(vout, "DPB need to be increased");
1288 vout_display_state_t state;
1289 memset(&state, 0, sizeof(state));
1291 ThreadStop(vout, &state);
1293 if (!state.cfg.is_fullscreen) {
1294 state.cfg.display.width = 0;
1295 state.cfg.display.height = 0;
1299 /* FIXME current vout "variables" are not in sync here anymore
1300 * and I am not sure what to do */
1302 vout->p->original = original;
1303 vout->p->dpb_size = cfg->dpb_size;
1304 if (ThreadStart(vout, &state)) {
1306 return VLC_EGENERIC;
1311 /*****************************************************************************
1312 * Thread: video output thread
1313 *****************************************************************************
1314 * Video output thread. This function does only returns when the thread is
1315 * terminated. It handles the pictures arriving in the video heap and the
1316 * display device events.
1317 *****************************************************************************/
1318 static void *Thread(void *object)
1320 vout_thread_t *vout = object;
1322 vout_interlacing_support_t interlacing = {
1323 .is_interlaced = false,
1326 vout_postprocessing_support_t postprocessing = {
1327 .qtype = QTYPE_NONE,
1330 mtime_t deadline = VLC_TS_INVALID;
1332 vout_control_cmd_t cmd;
1334 /* FIXME remove thoses ugly timeouts
1336 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1338 case VOUT_CONTROL_INIT:
1340 if (ThreadStart(vout, NULL)) {
1341 ThreadStop(vout, NULL);
1346 case VOUT_CONTROL_CLEAN:
1347 ThreadStop(vout, NULL);
1350 case VOUT_CONTROL_REINIT:
1351 if (ThreadReinit(vout, cmd.u.cfg))
1354 case VOUT_CONTROL_SUBPICTURE:
1355 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1356 cmd.u.subpicture = NULL;
1358 case VOUT_CONTROL_FLUSH_SUBPICTURE:
1359 ThreadFlushSubpicture(vout, cmd.u.integer);
1361 case VOUT_CONTROL_OSD_TITLE:
1362 ThreadDisplayOsdTitle(vout, cmd.u.string);
1364 case VOUT_CONTROL_CHANGE_FILTERS:
1365 ThreadChangeFilters(vout, cmd.u.string);
1367 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1368 ThreadChangeSubFilters(vout, cmd.u.string);
1370 case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1371 ThreadChangeSubMargin(vout, cmd.u.integer);
1373 case VOUT_CONTROL_PAUSE:
1374 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1376 case VOUT_CONTROL_FLUSH:
1377 ThreadFlush(vout, false, cmd.u.time);
1379 case VOUT_CONTROL_RESET:
1382 case VOUT_CONTROL_STEP:
1383 ThreadStep(vout, cmd.u.time_ptr);
1385 case VOUT_CONTROL_FULLSCREEN:
1386 ThreadChangeFullscreen(vout, cmd.u.boolean);
1388 case VOUT_CONTROL_ON_TOP:
1389 ThreadChangeOnTop(vout, cmd.u.boolean);
1391 case VOUT_CONTROL_DISPLAY_FILLED:
1392 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1394 case VOUT_CONTROL_ZOOM:
1395 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1397 case VOUT_CONTROL_ASPECT_RATIO:
1398 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1400 case VOUT_CONTROL_CROP_RATIO:
1401 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1403 case VOUT_CONTROL_CROP_WINDOW:
1404 ThreadExecuteCropWindow(vout, 0, 0,
1405 cmd.u.window.x, cmd.u.window.y,
1406 cmd.u.window.width, cmd.u.window.height);
1408 case VOUT_CONTROL_CROP_BORDER:
1409 ThreadExecuteCropBorder(vout,
1410 cmd.u.border.left, cmd.u.border.top,
1411 cmd.u.border.right, cmd.u.border.bottom);
1416 vout_control_cmd_Clean(&cmd);
1419 ThreadManage(vout, &deadline, &interlacing, &postprocessing);