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>
47 #include <vlc_image.h>
50 #include "vout_internal.h"
51 #include "interlacing.h"
52 #include "postprocessing.h"
55 /*****************************************************************************
57 *****************************************************************************/
58 static void *Thread(void *);
59 static void VoutDestructor(vlc_object_t *);
61 /* Maximum delay between 2 displayed pictures.
62 * XXX it is needed for now but should be removed in the long term.
64 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
67 * Late pictures having a delay higher than this value are thrashed.
69 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
71 /* Better be in advance when awakening than late... */
72 #define VOUT_MWAIT_TOLERANCE (INT64_C(4000))
75 static int VoutValidateFormat(video_format_t *dst,
76 const video_format_t *src)
78 if (src->i_width <= 0 || src->i_height <= 0)
80 if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
84 video_format_Copy(dst, src);
85 dst->i_chroma = vlc_fourcc_GetCodec(VIDEO_ES, src->i_chroma);
86 vlc_ureduce( &dst->i_sar_num, &dst->i_sar_den,
87 src->i_sar_num, src->i_sar_den, 50000 );
88 if (dst->i_sar_num <= 0 || dst->i_sar_den <= 0) {
92 video_format_FixRgb(dst);
95 static void VideoFormatCopyCropAr(video_format_t *dst,
96 const video_format_t *src)
98 video_format_CopyCrop(dst, src);
99 dst->i_sar_num = src->i_sar_num;
100 dst->i_sar_den = src->i_sar_den;
102 static bool VideoFormatIsCropArEqual(video_format_t *dst,
103 const video_format_t *src)
105 return dst->i_sar_num * src->i_sar_den == dst->i_sar_den * src->i_sar_num &&
106 dst->i_x_offset == src->i_x_offset &&
107 dst->i_y_offset == src->i_y_offset &&
108 dst->i_visible_width == src->i_visible_width &&
109 dst->i_visible_height == src->i_visible_height;
112 static vout_thread_t *VoutCreate(vlc_object_t *object,
113 const vout_configuration_t *cfg)
115 video_format_t original;
116 if (VoutValidateFormat(&original, cfg->fmt))
119 /* Allocate descriptor */
120 vout_thread_t *vout = vlc_custom_create(object,
121 sizeof(*vout) + sizeof(*vout->p),
122 VLC_OBJECT_VOUT, "video output");
124 video_format_Clean(&original);
129 vout->p = (vout_thread_sys_t*)&vout[1];
131 vout->p->original = original;
132 vout->p->dpb_size = cfg->dpb_size;
134 vout_control_Init(&vout->p->control);
135 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
137 vout_statistic_Init(&vout->p->statistic);
139 vout_snapshot_Init(&vout->p->snapshot);
141 /* Initialize locks */
142 vlc_mutex_init(&vout->p->picture_lock);
143 vlc_mutex_init(&vout->p->filter.lock);
144 vlc_mutex_init(&vout->p->spu_lock);
146 /* Attach the new object now so we can use var inheritance below */
147 vlc_object_attach(vout, object);
149 /* Initialize subpicture unit */
150 vout->p->spu = spu_Create(vout);
152 /* Take care of some "interface/control" related initialisations */
155 vout->p->title.show = var_GetBool(vout, "video-title-show");
156 vout->p->title.timeout = var_GetInteger(vout, "video-title-timeout");
157 vout->p->title.position = var_GetInteger(vout, "video-title-position");
159 /* Get splitter name if present */
160 char *splitter_name = var_InheritString(vout, "video-splitter");
161 if (splitter_name && *splitter_name) {
162 vout->p->splitter_name = splitter_name;
168 vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
171 vlc_object_set_destructor(vout, VoutDestructor);
174 if (vlc_clone(&vout->p->thread, Thread, vout,
175 VLC_THREAD_PRIORITY_OUTPUT)) {
176 spu_Destroy(vout->p->spu);
177 vlc_object_release(vout);
181 vout_control_WaitEmpty(&vout->p->control);
184 msg_Err(vout, "video output creation failed");
185 vout_CloseAndRelease(vout);
189 vout->p->input = cfg->input;
191 spu_Attach(vout->p->spu, vout->p->input, true);
196 vout_thread_t *(vout_Request)(vlc_object_t *object,
197 const vout_configuration_t *cfg)
199 vout_thread_t *vout = cfg->vout;
200 if (cfg->change_fmt && !cfg->fmt) {
202 vout_CloseAndRelease(vout);
206 /* If a vout is provided, try reusing it */
208 if (vout->p->input != cfg->input) {
210 spu_Attach(vout->p->spu, vout->p->input, false);
211 vout->p->input = cfg->input;
213 spu_Attach(vout->p->spu, vout->p->input, true);
216 if (cfg->change_fmt) {
217 vout_control_cmd_t cmd;
218 vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
221 vout_control_Push(&vout->p->control, &cmd);
222 vout_control_WaitEmpty(&vout->p->control);
225 if (!vout->p->dead) {
226 msg_Dbg(object, "reusing provided vout");
229 vout_CloseAndRelease(vout);
231 msg_Warn(object, "cannot reuse provided vout");
233 return VoutCreate(object, cfg);
236 void vout_Close(vout_thread_t *vout)
241 spu_Attach(vout->p->spu, vout->p->input, false);
243 vout_snapshot_End(&vout->p->snapshot);
245 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
246 vlc_join(vout->p->thread, NULL);
248 vlc_mutex_lock(&vout->p->spu_lock);
249 spu_Destroy(vout->p->spu);
251 vlc_mutex_unlock(&vout->p->spu_lock);
255 static void VoutDestructor(vlc_object_t *object)
257 vout_thread_t *vout = (vout_thread_t *)object;
259 /* Make sure the vout was stopped first */
260 //assert(!vout->p_module);
262 free(vout->p->splitter_name);
264 /* Destroy the locks */
265 vlc_mutex_destroy(&vout->p->spu_lock);
266 vlc_mutex_destroy(&vout->p->picture_lock);
267 vlc_mutex_destroy(&vout->p->filter.lock);
268 vout_control_Clean(&vout->p->control);
271 vout_statistic_Clean(&vout->p->statistic);
274 vout_snapshot_Clean(&vout->p->snapshot);
276 video_format_Clean(&vout->p->original);
280 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
282 vout_control_cmd_t cmd;
283 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
284 cmd.u.pause.is_on = is_paused;
285 cmd.u.pause.date = date;
286 vout_control_Push(&vout->p->control, &cmd);
288 vout_control_WaitEmpty(&vout->p->control);
291 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
293 vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
296 void vout_Flush(vout_thread_t *vout, mtime_t date)
298 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
299 vout_control_WaitEmpty(&vout->p->control);
302 void vout_Reset(vout_thread_t *vout)
304 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
305 vout_control_WaitEmpty(&vout->p->control);
308 bool vout_IsEmpty(vout_thread_t *vout)
310 vlc_mutex_lock(&vout->p->picture_lock);
312 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
314 picture_Release(picture);
316 vlc_mutex_unlock(&vout->p->picture_lock);
321 void vout_FixLeaks( vout_thread_t *vout )
323 vlc_mutex_lock(&vout->p->picture_lock);
325 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
327 picture = picture_pool_Get(vout->p->decoder_pool);
331 picture_Release(picture);
332 /* Not all pictures has been displayed yet or some are
334 vlc_mutex_unlock(&vout->p->picture_lock);
338 /* There is no reason that no pictures are available, force one
339 * from the pool, becarefull with it though */
340 msg_Err(vout, "pictures leaked, trying to workaround");
343 picture_pool_NonEmpty(vout->p->decoder_pool, false);
345 vlc_mutex_unlock(&vout->p->picture_lock);
347 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
349 vout_control_cmd_t cmd;
350 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
351 cmd.u.time_ptr = duration;
353 vout_control_Push(&vout->p->control, &cmd);
354 vout_control_WaitEmpty(&vout->p->control);
357 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
360 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
363 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
365 vout_control_cmd_t cmd;
366 vout_control_cmd_Init(&cmd, VOUT_CONTROL_SUBPICTURE);
367 cmd.u.subpicture = subpic;
369 vout_control_Push(&vout->p->control, &cmd);
371 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
373 int channel = SPU_DEFAULT_CHANNEL;
375 vlc_mutex_lock(&vout->p->spu_lock);
377 channel = spu_RegisterChannel(vout->p->spu);
378 vlc_mutex_unlock(&vout->p->spu_lock);
382 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
384 vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_FLUSH_SUBPICTURE,
389 * It retreives a picture from the vout or NULL if no pictures are
392 * You MUST call vout_PutPicture or vout_ReleasePicture on it.
394 * You may use vout_HoldPicture(paired with vout_ReleasePicture) to keep a
395 * read-only reference.
397 picture_t *vout_GetPicture(vout_thread_t *vout)
400 vlc_mutex_lock(&vout->p->picture_lock);
401 picture_t *picture = picture_pool_Get(vout->p->decoder_pool);
403 picture_Reset(picture);
404 VideoFormatCopyCropAr(&picture->format, &vout->p->original);
406 vlc_mutex_unlock(&vout->p->picture_lock);
412 * It gives to the vout a picture to be displayed.
414 * The given picture MUST comes from vout_GetPicture.
416 * Becareful, after vout_PutPicture is called, picture_t::p_next cannot be
419 void vout_PutPicture(vout_thread_t *vout, picture_t *picture)
421 vlc_mutex_lock(&vout->p->picture_lock);
423 picture->p_next = NULL;
424 picture_fifo_Push(vout->p->decoder_fifo, picture);
426 vlc_mutex_unlock(&vout->p->picture_lock);
428 vout_control_Wake(&vout->p->control);
432 * It releases a picture retreived by vout_GetPicture.
434 void vout_ReleasePicture(vout_thread_t *vout, picture_t *picture)
436 vlc_mutex_lock(&vout->p->picture_lock);
438 picture_Release(picture);
440 vlc_mutex_unlock(&vout->p->picture_lock);
442 vout_control_Wake(&vout->p->control);
446 * It increment the reference counter of a picture retreived by
449 void vout_HoldPicture(vout_thread_t *vout, picture_t *picture)
451 vlc_mutex_lock(&vout->p->picture_lock);
453 picture_Hold(picture);
455 vlc_mutex_unlock(&vout->p->picture_lock);
459 int vout_GetSnapshot(vout_thread_t *vout,
460 block_t **image_dst, picture_t **picture_dst,
462 const char *type, mtime_t timeout)
464 picture_t *picture = vout_snapshot_Get(&vout->p->snapshot, timeout);
466 msg_Err(vout, "Failed to grab a snapshot");
471 vlc_fourcc_t codec = VLC_CODEC_PNG;
472 if (type && image_Type2Fourcc(type))
473 codec = image_Type2Fourcc(type);
475 const int override_width = var_GetInteger(vout, "snapshot-width");
476 const int override_height = var_GetInteger(vout, "snapshot-height");
478 if (picture_Export(VLC_OBJECT(vout), image_dst, fmt,
479 picture, codec, override_width, override_height)) {
480 msg_Err(vout, "Failed to convert image for snapshot");
481 picture_Release(picture);
486 *picture_dst = picture;
488 picture_Release(picture);
492 /* vout_Control* are usable by anyone at anytime */
493 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
495 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
498 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
500 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
503 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
505 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
508 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
510 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
513 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
514 unsigned num, unsigned den)
516 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
519 void vout_ControlChangeCropRatio(vout_thread_t *vout,
520 unsigned num, unsigned den)
522 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
525 void vout_ControlChangeCropWindow(vout_thread_t *vout,
526 int x, int y, int width, int height)
528 vout_control_cmd_t cmd;
529 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
530 cmd.u.window.x = __MAX(x, 0);
531 cmd.u.window.y = __MAX(y, 0);
532 cmd.u.window.width = __MAX(width, 0);
533 cmd.u.window.height = __MAX(height, 0);
535 vout_control_Push(&vout->p->control, &cmd);
537 void vout_ControlChangeCropBorder(vout_thread_t *vout,
538 int left, int top, int right, int bottom)
540 vout_control_cmd_t cmd;
541 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
542 cmd.u.border.left = __MAX(left, 0);
543 cmd.u.border.top = __MAX(top, 0);
544 cmd.u.border.right = __MAX(right, 0);
545 cmd.u.border.bottom = __MAX(bottom, 0);
547 vout_control_Push(&vout->p->control, &cmd);
549 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
551 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
554 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
556 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
559 void vout_ControlChangeSubMargin(vout_thread_t *vout, int margin)
561 vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_MARGIN,
566 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
568 /* Load configuration */
569 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
570 cfg->display.title = title;
571 const int display_width = var_CreateGetInteger(vout, "width");
572 const int display_height = var_CreateGetInteger(vout, "height");
573 cfg->display.width = display_width > 0 ? display_width : 0;
574 cfg->display.height = display_height > 0 ? display_height : 0;
575 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
576 unsigned msar_num, msar_den;
577 if (var_InheritURational(vout, &msar_num, &msar_den, "monitor-par") ||
578 msar_num <= 0 || msar_den <= 0) {
582 cfg->display.sar.num = msar_num;
583 cfg->display.sar.den = msar_den;
584 unsigned zoom_den = 1000;
585 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
586 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
587 cfg->zoom.num = zoom_num;
588 cfg->zoom.den = zoom_den;
589 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
590 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
591 const int align_mask = var_CreateGetInteger(vout, "align");
592 if (align_mask & 0x1)
593 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
594 else if (align_mask & 0x2)
595 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
596 if (align_mask & 0x4)
597 cfg->align.vertical = VOUT_DISPLAY_ALIGN_TOP;
598 else if (align_mask & 0x8)
599 cfg->align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
602 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
603 const vout_window_cfg_t *cfg)
606 vout_window_cfg_t cfg_override = *cfg;
608 if (!var_InheritBool( vout, "embedded-video"))
609 cfg_override.is_standalone = true;
611 if (vout->p->window.is_unused && vout->p->window.object) {
612 assert(!vout->p->splitter_name);
613 if (!cfg_override.is_standalone == !vout->p->window.cfg.is_standalone &&
614 cfg_override.type == vout->p->window.cfg.type) {
615 /* Reuse the stored window */
616 msg_Dbg(vout, "Reusing previous vout window");
617 vout_window_t *window = vout->p->window.object;
618 if (cfg_override.width != vout->p->window.cfg.width ||
619 cfg_override.height != vout->p->window.cfg.height)
620 vout_window_SetSize(window,
621 cfg_override.width, cfg_override.height);
622 vout->p->window.is_unused = false;
623 vout->p->window.cfg = cfg_override;
627 vout_window_Delete(vout->p->window.object);
628 vout->p->window.is_unused = true;
629 vout->p->window.object = NULL;
632 vout_window_t *window = vout_window_New(VLC_OBJECT(vout), "$window",
636 if (!vout->p->splitter_name) {
637 vout->p->window.is_unused = false;
638 vout->p->window.cfg = cfg_override;
639 vout->p->window.object = window;
644 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
645 vout_window_t *window)
648 if (!vout->p->window.is_unused && vout->p->window.object == window) {
649 vout->p->window.is_unused = true;
650 } else if (vout->p->window.is_unused && vout->p->window.object && !window) {
651 vout_window_Delete(vout->p->window.object);
652 vout->p->window.is_unused = true;
653 vout->p->window.object = NULL;
655 vout_window_Delete(window);
660 static picture_t *VoutVideoFilterInteractiveNewPicture(filter_t *filter)
662 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
664 picture_t *picture = picture_pool_Get(vout->p->private_pool);
666 picture_Reset(picture);
667 VideoFormatCopyCropAr(&picture->format, &filter->fmt_out.video);
671 static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
673 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
675 vlc_assert_locked(&vout->p->filter.lock);
676 if (filter_chain_GetLength(vout->p->filter.chain_interactive) == 0)
677 return VoutVideoFilterInteractiveNewPicture(filter);
679 return picture_NewFromFormat(&filter->fmt_out.video);
681 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
684 picture_Release(picture);
686 static int VoutVideoFilterStaticAllocationSetup(filter_t *filter, void *data)
688 filter->pf_video_buffer_new = VoutVideoFilterStaticNewPicture;
689 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
690 filter->p_owner = data; /* vout */
693 static int VoutVideoFilterInteractiveAllocationSetup(filter_t *filter, void *data)
695 filter->pf_video_buffer_new = VoutVideoFilterInteractiveNewPicture;
696 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
697 filter->p_owner = data; /* vout */
700 static void ThreadFilterFlush(vout_thread_t *vout, bool is_locked)
702 if (vout->p->displayed.current)
703 picture_Release( vout->p->displayed.current );
704 vout->p->displayed.current = NULL;
706 if (vout->p->displayed.next)
707 picture_Release( vout->p->displayed.next );
708 vout->p->displayed.next = NULL;
711 vlc_mutex_lock(&vout->p->filter.lock);
712 filter_chain_VideoFlush(vout->p->filter.chain_static);
713 filter_chain_VideoFlush(vout->p->filter.chain_interactive);
715 vlc_mutex_unlock(&vout->p->filter.lock);
723 static void ThreadChangeFilters(vout_thread_t *vout,
724 const video_format_t *source,
728 ThreadFilterFlush(vout, is_locked);
730 vlc_array_t array_static;
731 vlc_array_t array_interactive;
733 vlc_array_init(&array_static);
734 vlc_array_init(&array_interactive);
735 char *current = filters ? strdup(filters) : NULL;
739 char *next = config_ChainCreate(&name, &cfg, current);
742 vout_filter_t *e = xmalloc(sizeof(*e));
745 if (!strcmp(e->name, "deinterlace") ||
746 !strcmp(e->name, "postproc")) {
747 vlc_array_append(&array_static, e);
749 vlc_array_append(&array_interactive, e);
753 config_ChainDestroy(cfg);
761 vlc_mutex_lock(&vout->p->filter.lock);
763 es_format_t fmt_target;
764 es_format_InitFromVideo(&fmt_target, source ? source : &vout->p->filter.format);
766 es_format_t fmt_current = fmt_target;
768 for (int a = 0; a < 2; a++) {
769 vlc_array_t *array = a == 0 ? &array_static :
771 filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
772 vout->p->filter.chain_interactive;
774 filter_chain_Reset(chain, &fmt_current, &fmt_current);
775 for (int i = 0; i < vlc_array_count(array); i++) {
776 vout_filter_t *e = vlc_array_item_at_index(array, i);
777 msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
778 if (!filter_chain_AppendFilter(chain, e->name, e->cfg, NULL, NULL)) {
779 msg_Err(vout, "Failed to add filter '%s'", e->name);
780 config_ChainDestroy(e->cfg);
785 fmt_current = *filter_chain_GetFmtOut(chain);
786 vlc_array_clear(array);
788 VideoFormatCopyCropAr(&fmt_target.video, &fmt_current.video);
789 if (!es_format_IsSimilar(&fmt_current, &fmt_target)) {
790 msg_Dbg(vout, "Adding a filter to compensate for format changes");
791 if (!filter_chain_AppendFilter(vout->p->filter.chain_interactive, NULL, NULL,
792 &fmt_current, &fmt_target)) {
793 msg_Err(vout, "Failed to compensate for the format changes, removing all filters");
794 filter_chain_Reset(vout->p->filter.chain_static, &fmt_target, &fmt_target);
795 filter_chain_Reset(vout->p->filter.chain_interactive, &fmt_target, &fmt_target);
799 if (vout->p->filter.configuration != filters) {
800 free(vout->p->filter.configuration);
801 vout->p->filter.configuration = filters ? strdup(filters) : NULL;
804 video_format_Clean(&vout->p->filter.format);
805 video_format_Copy(&vout->p->filter.format, source);
809 vlc_mutex_unlock(&vout->p->filter.lock);
814 static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_late_dropped)
818 vlc_mutex_lock(&vout->p->filter.lock);
820 picture_t *picture = filter_chain_VideoFilter(vout->p->filter.chain_static, NULL);
821 assert(!reuse || !picture);
825 if (reuse && vout->p->displayed.decoded) {
826 decoded = picture_Hold(vout->p->displayed.decoded);
828 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
829 if (is_late_dropped && decoded && !decoded->b_force) {
830 const mtime_t predicted = mdate() + 0; /* TODO improve */
831 const mtime_t late = predicted - decoded->date;
832 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
833 msg_Warn(vout, "picture is too late to be displayed (missing %d ms)", (int)(late/1000));
834 picture_Release(decoded);
837 } else if (late > 0) {
838 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
842 !VideoFormatIsCropArEqual(&decoded->format, &vout->p->filter.format))
843 ThreadChangeFilters(vout, &decoded->format, vout->p->filter.configuration, true);
849 if (vout->p->displayed.decoded)
850 picture_Release(vout->p->displayed.decoded);
852 vout->p->displayed.decoded = picture_Hold(decoded);
853 vout->p->displayed.timestamp = decoded->date;
854 vout->p->displayed.is_interlaced = !decoded->b_progressive;
855 vout->p->displayed.qtype = decoded->i_qtype;
857 picture = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
860 vlc_mutex_unlock(&vout->p->filter.lock);
862 vout_statistic_Update(&vout->p->statistic, 0, lost_count);
866 assert(!vout->p->displayed.next);
867 if (!vout->p->displayed.current)
868 vout->p->displayed.current = picture;
870 vout->p->displayed.next = picture;
874 static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
876 vout_display_t *vd = vout->p->display.vd;
878 picture_t *torender = picture_Hold(vout->p->displayed.current);
880 vout_chrono_Start(&vout->p->render);
882 vlc_mutex_lock(&vout->p->filter.lock);
883 picture_t *filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, torender);
884 vlc_mutex_unlock(&vout->p->filter.lock);
889 if (filtered->date != vout->p->displayed.current->date)
890 msg_Warn(vout, "Unsupported timestamp modifications done by chain_interactive");
893 * Get the subpicture to be displayed
895 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
896 mtime_t render_subtitle_date;
897 if (vout->p->pause.is_on)
898 render_subtitle_date = vout->p->pause.date;
900 render_subtitle_date = filtered->date > 1 ? filtered->date : mdate();
901 mtime_t render_osd_date = mdate(); /* FIXME wrong */
903 subpicture_t *subpic = spu_Render(vout->p->spu, NULL, &vd->source, &vd->source,
904 render_subtitle_date, render_osd_date,
911 * - be sure to end up with a direct buffer.
912 * - blend subtitles, and in a fast access buffer
914 picture_t *direct = NULL;
916 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
918 if (vout->p->is_decoder_pool_slow)
919 render = picture_NewFromFormat(&vd->source);
920 else if (vout->p->decoder_pool != vout->p->display_pool)
921 render = picture_pool_Get(vout->p->display_pool);
923 render = picture_pool_Get(vout->p->private_pool);
926 picture_Copy(render, filtered);
928 if (vout->p->spu_blend && subpic)
929 picture_BlendSubpicture(render, vout->p->spu_blend, subpic);
931 if (vout->p->is_decoder_pool_slow) {
932 direct = picture_pool_Get(vout->p->display_pool);
934 picture_Copy(direct, render);
935 picture_Release(render);
940 VideoFormatCopyCropAr(&direct->format, &filtered->format);
941 picture_Release(filtered);
947 subpicture_Delete(subpic);
953 * Take a snapshot if requested
956 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
958 /* Render the direct buffer returned by vout_RenderPicture */
959 vout_RenderWrapper(vout, direct, NULL);
961 vout_chrono_Stop(&vout->p->render);
966 msg_Info(vout, "render: avg %d ms var %d ms",
967 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
971 /* Wait the real date (for rendering jitter) */
973 mtime_t delay = direct->date - mdate();
975 msg_Warn(vout, "picture is late (%lld ms)", delay / 1000);
980 /* Display the direct buffer returned by vout_RenderPicture */
981 vout->p->displayed.date = mdate();
983 vout_DisplayWrapper(vout, direct, NULL);
985 vout_statistic_Update(&vout->p->statistic, 1, 0);
990 static int ThreadDisplayPicture(vout_thread_t *vout,
991 bool now, mtime_t *deadline)
993 bool is_late_dropped = vout->p->is_late_dropped && !vout->p->pause.is_on && !now;
994 bool first = !vout->p->displayed.current;
995 if (first && ThreadDisplayPreparePicture(vout, true, is_late_dropped)) /* FIXME not sure it is ok */
997 if (!vout->p->pause.is_on || now) {
998 while (!vout->p->displayed.next) {
999 if (ThreadDisplayPreparePicture(vout, false, is_late_dropped)) {
1005 const mtime_t date = mdate();
1006 const mtime_t render_delay = vout_chrono_GetHigh(&vout->p->render) + VOUT_MWAIT_TOLERANCE;
1008 mtime_t date_next = VLC_TS_INVALID;
1009 if (!vout->p->pause.is_on && vout->p->displayed.next)
1010 date_next = vout->p->displayed.next->date - render_delay;
1012 /* FIXME/XXX we must redisplay the last decoded picture (because
1013 * of potential vout updated, or filters update or SPU update)
1014 * For now a high update period is needed but it coulmd be removed
1016 * - vout module emits events from theselves.
1017 * - *and* SPU is modified to emit an event or a deadline when needed.
1019 * So it will be done latter.
1021 mtime_t date_refresh = VLC_TS_INVALID;
1022 if (vout->p->displayed.date > VLC_TS_INVALID)
1023 date_refresh = vout->p->displayed.date + VOUT_REDISPLAY_DELAY - render_delay;
1026 if (date_next != VLC_TS_INVALID)
1027 drop |= date_next + 0 <= date;
1029 bool refresh = false;
1030 if (date_refresh > VLC_TS_INVALID)
1031 refresh = date_refresh <= date;
1033 if (!first && !refresh && !drop) {
1034 if (date_next != VLC_TS_INVALID && date_refresh != VLC_TS_INVALID)
1035 *deadline = __MIN(date_next, date_refresh);
1036 else if (date_next != VLC_TS_INVALID)
1037 *deadline = date_next;
1038 else if (date_refresh != VLC_TS_INVALID)
1039 *deadline = date_refresh;
1040 return VLC_EGENERIC;
1044 picture_Release(vout->p->displayed.current);
1045 vout->p->displayed.current = vout->p->displayed.next;
1046 vout->p->displayed.next = NULL;
1048 if (!vout->p->displayed.current)
1049 return VLC_EGENERIC;
1051 bool is_forced = now || (!drop && refresh) || vout->p->displayed.current->b_force;
1052 return ThreadDisplayRenderPicture(vout, is_forced);
1055 static void ThreadManage(vout_thread_t *vout,
1057 vout_interlacing_support_t *interlacing,
1058 vout_postprocessing_support_t *postprocessing)
1060 vlc_mutex_lock(&vout->p->picture_lock);
1062 *deadline = VLC_TS_INVALID;
1064 if (ThreadDisplayPicture(vout, false, deadline))
1068 const int picture_qtype = vout->p->displayed.qtype;
1069 const bool picture_interlaced = vout->p->displayed.is_interlaced;
1071 vlc_mutex_unlock(&vout->p->picture_lock);
1073 /* Post processing */
1074 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
1077 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
1079 vout_ManageWrapper(vout);
1082 static void ThreadDisplaySubpicture(vout_thread_t *vout,
1083 subpicture_t *subpicture)
1085 spu_PutSubpicture(vout->p->spu, subpicture);
1088 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
1090 spu_ClearChannel(vout->p->spu, channel);
1093 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
1095 if (!vout->p->title.show)
1098 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
1099 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
1103 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
1105 spu_ChangeFilters(vout->p->spu, filters);
1107 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
1109 spu_ChangeMargin(vout->p->spu, margin);
1112 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
1114 assert(!vout->p->pause.is_on || !is_paused);
1116 if (vout->p->pause.is_on) {
1117 const mtime_t duration = date - vout->p->pause.date;
1119 if (vout->p->step.timestamp > VLC_TS_INVALID)
1120 vout->p->step.timestamp += duration;
1121 if (vout->p->step.last > VLC_TS_INVALID)
1122 vout->p->step.last += duration;
1123 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
1124 if (vout->p->displayed.decoded)
1125 vout->p->displayed.decoded->date += duration;
1126 spu_OffsetSubtitleDate(vout->p->spu, duration);
1128 ThreadFilterFlush(vout, false);
1130 vout->p->step.timestamp = VLC_TS_INVALID;
1131 vout->p->step.last = VLC_TS_INVALID;
1133 vout->p->pause.is_on = is_paused;
1134 vout->p->pause.date = date;
1137 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
1139 vout->p->step.timestamp = VLC_TS_INVALID;
1140 vout->p->step.last = VLC_TS_INVALID;
1142 ThreadFilterFlush(vout, false); /* FIXME too much */
1144 picture_t *last = vout->p->displayed.decoded;
1146 if (( below && last->date <= date) ||
1147 (!below && last->date >= date)) {
1148 picture_Release(last);
1150 vout->p->displayed.decoded = NULL;
1151 vout->p->displayed.date = VLC_TS_INVALID;
1152 vout->p->displayed.timestamp = VLC_TS_INVALID;
1156 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
1159 static void ThreadReset(vout_thread_t *vout)
1161 ThreadFlush(vout, true, INT64_MAX);
1162 if (vout->p->decoder_pool)
1163 picture_pool_NonEmpty(vout->p->decoder_pool, true);
1164 vout->p->pause.is_on = false;
1165 vout->p->pause.date = mdate();
1168 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
1172 if (vout->p->step.last <= VLC_TS_INVALID)
1173 vout->p->step.last = vout->p->displayed.timestamp;
1176 if (ThreadDisplayPicture(vout, true, &dummy))
1179 vout->p->step.timestamp = vout->p->displayed.timestamp;
1181 if (vout->p->step.last > VLC_TS_INVALID &&
1182 vout->p->step.timestamp > vout->p->step.last) {
1183 *duration = vout->p->step.timestamp - vout->p->step.last;
1184 vout->p->step.last = vout->p->step.timestamp;
1185 /* TODO advance subpicture by the duration ... */
1189 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
1191 /* FIXME not sure setting "fullscreen" is good ... */
1192 var_SetBool(vout, "fullscreen", fullscreen);
1193 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
1196 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
1198 vout_SetWindowState(vout->p->display.vd,
1199 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
1200 VOUT_WINDOW_STATE_NORMAL);
1203 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1205 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1208 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1210 if (num * 10 < den) {
1213 } else if (num > den * 10) {
1217 vout_SetDisplayZoom(vout->p->display.vd, num, den);
1220 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1221 unsigned num, unsigned den)
1223 vout_SetDisplayAspect(vout->p->display.vd, num, den);
1227 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1228 unsigned x, unsigned y,
1229 unsigned width, unsigned height)
1231 vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1232 x, y, width, height);
1234 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1235 unsigned left, unsigned top,
1236 unsigned right, unsigned bottom)
1238 msg_Err(vout, "ThreadExecuteCropBorder %d.%d %dx%d", left, top, right, bottom);
1239 vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1240 left, top, -(int)right, -(int)bottom);
1243 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1244 unsigned num, unsigned den)
1246 vout_SetDisplayCrop(vout->p->display.vd, num, den,
1250 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
1252 vlc_mouse_Init(&vout->p->mouse);
1253 vout->p->decoder_fifo = picture_fifo_New();
1254 vout->p->decoder_pool = NULL;
1255 vout->p->display_pool = NULL;
1256 vout->p->private_pool = NULL;
1258 vout->p->filter.configuration = NULL;
1259 video_format_Copy(&vout->p->filter.format, &vout->p->original);
1260 vout->p->filter.chain_static =
1261 filter_chain_New( vout, "video filter2", true,
1262 VoutVideoFilterStaticAllocationSetup, NULL, vout);
1263 vout->p->filter.chain_interactive =
1264 filter_chain_New( vout, "video filter2", true,
1265 VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
1267 vout_display_state_t state_default;
1269 VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1270 state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
1271 VOUT_WINDOW_STATE_NORMAL;
1272 state_default.sar.num = 0;
1273 state_default.sar.den = 0;
1275 state = &state_default;
1278 if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1279 return VLC_EGENERIC;
1280 if (vout_InitWrapper(vout))
1281 return VLC_EGENERIC;
1282 assert(vout->p->decoder_pool);
1284 vout->p->displayed.current = NULL;
1285 vout->p->displayed.next = NULL;
1286 vout->p->displayed.decoded = NULL;
1287 vout->p->displayed.date = VLC_TS_INVALID;
1288 vout->p->displayed.timestamp = VLC_TS_INVALID;
1289 vout->p->displayed.qtype = QTYPE_NONE;
1290 vout->p->displayed.is_interlaced = false;
1292 vout->p->step.last = VLC_TS_INVALID;
1293 vout->p->step.timestamp = VLC_TS_INVALID;
1296 vout->p->spu_blend = filter_NewBlend(VLC_OBJECT(vout), &vout->p->original);
1297 if (!vout->p->spu_blend)
1298 msg_Err(vout, "Failed to create blending filter, OSD/Subtitles will not work");
1300 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1304 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1306 if (vout->p->spu_blend)
1307 filter_DeleteBlend(vout->p->spu_blend);
1309 /* Destroy translation tables */
1310 if (vout->p->display.vd) {
1311 if (vout->p->decoder_pool) {
1312 ThreadFlush(vout, true, INT64_MAX);
1313 vout_EndWrapper(vout);
1315 vout_CloseWrapper(vout, state);
1318 /* Destroy the video filters2 */
1319 filter_chain_Delete(vout->p->filter.chain_interactive);
1320 filter_chain_Delete(vout->p->filter.chain_static);
1321 video_format_Clean(&vout->p->filter.format);
1322 free(vout->p->filter.configuration);
1324 if (vout->p->decoder_fifo)
1325 picture_fifo_Delete(vout->p->decoder_fifo);
1326 assert(!vout->p->decoder_pool);
1329 static void ThreadInit(vout_thread_t *vout)
1331 vout->p->window.is_unused = true;
1332 vout->p->window.object = NULL;
1333 vout->p->dead = false;
1334 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
1335 vout->p->pause.is_on = false;
1336 vout->p->pause.date = VLC_TS_INVALID;
1338 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1341 static void ThreadClean(vout_thread_t *vout)
1343 if (vout->p->window.object) {
1344 assert(vout->p->window.is_unused);
1345 vout_window_Delete(vout->p->window.object);
1347 vout_chrono_Clean(&vout->p->render);
1348 vout->p->dead = true;
1349 vout_control_Dead(&vout->p->control);
1352 static int ThreadReinit(vout_thread_t *vout,
1353 const vout_configuration_t *cfg)
1355 video_format_t original;
1356 if (VoutValidateFormat(&original, cfg->fmt)) {
1357 ThreadStop(vout, NULL);
1359 return VLC_EGENERIC;
1361 /* We ignore crop/ar changes at this point, they are dynamically supported */
1362 VideoFormatCopyCropAr(&vout->p->original, &original);
1363 if (video_format_IsSimilar(&original, &vout->p->original)) {
1364 if (cfg->dpb_size <= vout->p->dpb_size)
1366 msg_Warn(vout, "DPB need to be increased");
1369 vout_display_state_t state;
1370 memset(&state, 0, sizeof(state));
1372 ThreadStop(vout, &state);
1374 if (!state.cfg.is_fullscreen) {
1375 state.cfg.display.width = 0;
1376 state.cfg.display.height = 0;
1380 /* FIXME current vout "variables" are not in sync here anymore
1381 * and I am not sure what to do */
1383 vout->p->original = original;
1384 vout->p->dpb_size = cfg->dpb_size;
1385 if (ThreadStart(vout, &state)) {
1387 return VLC_EGENERIC;
1392 /*****************************************************************************
1393 * Thread: video output thread
1394 *****************************************************************************
1395 * Video output thread. This function does only returns when the thread is
1396 * terminated. It handles the pictures arriving in the video heap and the
1397 * display device events.
1398 *****************************************************************************/
1399 static void *Thread(void *object)
1401 vout_thread_t *vout = object;
1403 vout_interlacing_support_t interlacing = {
1404 .is_interlaced = false,
1407 vout_postprocessing_support_t postprocessing = {
1408 .qtype = QTYPE_NONE,
1411 mtime_t deadline = VLC_TS_INVALID;
1413 vout_control_cmd_t cmd;
1415 /* FIXME remove thoses ugly timeouts
1417 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1419 case VOUT_CONTROL_INIT:
1421 if (ThreadStart(vout, NULL)) {
1422 ThreadStop(vout, NULL);
1427 case VOUT_CONTROL_CLEAN:
1428 ThreadStop(vout, NULL);
1431 case VOUT_CONTROL_REINIT:
1432 if (ThreadReinit(vout, cmd.u.cfg))
1435 case VOUT_CONTROL_SUBPICTURE:
1436 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1437 cmd.u.subpicture = NULL;
1439 case VOUT_CONTROL_FLUSH_SUBPICTURE:
1440 ThreadFlushSubpicture(vout, cmd.u.integer);
1442 case VOUT_CONTROL_OSD_TITLE:
1443 ThreadDisplayOsdTitle(vout, cmd.u.string);
1445 case VOUT_CONTROL_CHANGE_FILTERS:
1446 ThreadChangeFilters(vout, NULL, cmd.u.string, false);
1448 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1449 ThreadChangeSubFilters(vout, cmd.u.string);
1451 case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1452 ThreadChangeSubMargin(vout, cmd.u.integer);
1454 case VOUT_CONTROL_PAUSE:
1455 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1457 case VOUT_CONTROL_FLUSH:
1458 ThreadFlush(vout, false, cmd.u.time);
1460 case VOUT_CONTROL_RESET:
1463 case VOUT_CONTROL_STEP:
1464 ThreadStep(vout, cmd.u.time_ptr);
1466 case VOUT_CONTROL_FULLSCREEN:
1467 ThreadChangeFullscreen(vout, cmd.u.boolean);
1469 case VOUT_CONTROL_ON_TOP:
1470 ThreadChangeOnTop(vout, cmd.u.boolean);
1472 case VOUT_CONTROL_DISPLAY_FILLED:
1473 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1475 case VOUT_CONTROL_ZOOM:
1476 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1478 case VOUT_CONTROL_ASPECT_RATIO:
1479 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1481 case VOUT_CONTROL_CROP_RATIO:
1482 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1484 case VOUT_CONTROL_CROP_WINDOW:
1485 ThreadExecuteCropWindow(vout,
1486 cmd.u.window.x, cmd.u.window.y,
1487 cmd.u.window.width, cmd.u.window.height);
1489 case VOUT_CONTROL_CROP_BORDER:
1490 ThreadExecuteCropBorder(vout,
1491 cmd.u.border.left, cmd.u.border.top,
1492 cmd.u.border.right, cmd.u.border.bottom);
1497 vout_control_cmd_Clean(&cmd);
1500 ThreadManage(vout, &deadline, &interlacing, &postprocessing);