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 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->p->i_par_den = 1;
124 vout_snapshot_Init(&vout->p->snapshot);
126 /* Initialize locks */
127 vlc_mutex_init(&vout->p->picture_lock);
128 vlc_mutex_init(&vout->p->vfilter_lock);
130 /* Attach the new object now so we can use var inheritance below */
131 vlc_object_attach(vout, object);
133 /* Initialize subpicture unit */
134 vout->p->p_spu = spu_Create(vout);
136 /* Take care of some "interface/control" related initialisations */
139 /* Get splitter name if present */
140 char *splitter_name = var_GetNonEmptyString(vout, "vout-filter");
142 if (asprintf(&vout->p->splitter_name, "%s,none", splitter_name) < 0)
143 vout->p->splitter_name = NULL;
146 vout->p->splitter_name = NULL;
150 vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
153 vlc_object_set_destructor(vout, VoutDestructor);
156 if (vlc_clone(&vout->p->thread, Thread, vout,
157 VLC_THREAD_PRIORITY_OUTPUT)) {
158 vlc_object_release(vout);
162 vout_control_WaitEmpty(&vout->p->control);
165 msg_Err(vout, "video output creation failed");
166 vout_CloseAndRelease(vout);
170 vout->p->input = cfg->input;
172 spu_Attach(vout->p->p_spu, vout->p->input, true);
177 vout_thread_t *(vout_Request)(vlc_object_t *object,
178 const vout_configuration_t *cfg)
180 vout_thread_t *vout = cfg->vout;
181 if (cfg->change_fmt && !cfg->fmt) {
183 vout_CloseAndRelease(vout);
187 /* If a vout is provided, try reusing it */
189 if (vout->p->input != cfg->input) {
191 spu_Attach(vout->p->p_spu, vout->p->input, false);
192 vout->p->input = cfg->input;
194 spu_Attach(vout->p->p_spu, vout->p->input, true);
197 if (cfg->change_fmt) {
198 vout_control_cmd_t cmd;
199 vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
202 vout_control_Push(&vout->p->control, &cmd);
203 vout_control_WaitEmpty(&vout->p->control);
206 if (!vout->p->dead) {
207 msg_Dbg(object, "reusing provided vout");
210 vout_CloseAndRelease(vout);
212 msg_Warn(object, "cannot reuse provided vout");
214 return VoutCreate(object, cfg);
217 /*****************************************************************************
218 * vout_Close: Close a vout created by VoutCreate.
219 *****************************************************************************
220 * You HAVE to call it on vout created by VoutCreate before vlc_object_release.
221 * You should NEVER call it on vout not obtained through VoutCreate
222 * (like with vout_Request or vlc_object_find.)
223 * You can use vout_CloseAndRelease() as a convenience method.
224 *****************************************************************************/
225 void vout_Close(vout_thread_t *vout)
230 spu_Attach(vout->p->p_spu, vout->p->input, false);
231 vlc_object_detach(vout->p->p_spu);
233 vout_snapshot_End(&vout->p->snapshot);
235 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
236 vlc_join(vout->p->thread, NULL);
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);
250 spu_Destroy(vout->p->p_spu);
252 /* Destroy the locks */
253 vlc_mutex_destroy(&vout->p->picture_lock);
254 vlc_mutex_destroy(&vout->p->vfilter_lock);
255 vout_control_Clean(&vout->p->control);
258 vout_statistic_Clean(&vout->p->statistic);
261 vout_snapshot_Clean(&vout->p->snapshot);
263 video_format_Clean(&vout->p->original);
267 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
269 vout_control_cmd_t cmd;
270 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
271 cmd.u.pause.is_on = is_paused;
272 cmd.u.pause.date = date;
273 vout_control_Push(&vout->p->control, &cmd);
275 vout_control_WaitEmpty(&vout->p->control);
278 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
280 vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
283 void vout_Flush(vout_thread_t *vout, mtime_t date)
285 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
286 vout_control_WaitEmpty(&vout->p->control);
289 void vout_Reset(vout_thread_t *vout)
291 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
292 vout_control_WaitEmpty(&vout->p->control);
295 bool vout_IsEmpty(vout_thread_t *vout)
297 vlc_mutex_lock(&vout->p->picture_lock);
299 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
301 picture_Release(picture);
303 vlc_mutex_unlock(&vout->p->picture_lock);
308 void vout_FixLeaks( 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 = picture_pool_Get(vout->p->decoder_pool);
318 picture_Release(picture);
319 /* Not all pictures has been displayed yet or some are
321 vlc_mutex_unlock(&vout->p->picture_lock);
325 /* There is no reason that no pictures are available, force one
326 * from the pool, becarefull with it though */
327 msg_Err(vout, "pictures leaked, trying to workaround");
330 picture_pool_NonEmpty(vout->p->decoder_pool, false);
332 vlc_mutex_unlock(&vout->p->picture_lock);
334 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
336 vout_control_cmd_t cmd;
337 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
338 cmd.u.time_ptr = duration;
340 vout_control_Push(&vout->p->control, &cmd);
341 vout_control_WaitEmpty(&vout->p->control);
344 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
347 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
350 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
352 vout_control_cmd_t cmd;
353 vout_control_cmd_Init(&cmd, VOUT_CONTROL_SUBPICTURE);
354 cmd.u.subpicture = subpic;
356 vout_control_Push(&vout->p->control, &cmd);
358 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
360 return spu_RegisterChannel(vout->p->p_spu);
362 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
364 spu_ClearChannel(vout->p->p_spu, channel);
367 /* vout_Control* are usable by anyone at anytime */
368 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
370 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
373 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
375 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
378 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
380 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
383 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
385 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
388 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
389 unsigned num, unsigned den)
391 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
394 void vout_ControlChangeCropRatio(vout_thread_t *vout,
395 unsigned num, unsigned den)
397 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
400 void vout_ControlChangeCropWindow(vout_thread_t *vout,
401 int x, int y, int width, int height)
403 vout_control_cmd_t cmd;
404 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
407 cmd.u.window.width = width;
408 cmd.u.window.height = height;
410 vout_control_Push(&vout->p->control, &cmd);
412 void vout_ControlChangeCropBorder(vout_thread_t *vout,
413 int left, int top, int right, int bottom)
415 vout_control_cmd_t cmd;
416 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
417 cmd.u.border.left = left;
418 cmd.u.border.top = top;
419 cmd.u.border.right = right;
420 cmd.u.border.bottom = bottom;
422 vout_control_Push(&vout->p->control, &cmd);
424 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
426 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
429 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
431 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
436 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
438 /* Load configuration */
439 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
440 cfg->display.title = title;
441 const int display_width = var_CreateGetInteger(vout, "width");
442 const int display_height = var_CreateGetInteger(vout, "height");
443 cfg->display.width = display_width > 0 ? display_width : 0;
444 cfg->display.height = display_height > 0 ? display_height : 0;
445 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
446 cfg->display.sar.num = 1; /* TODO monitor AR */
447 cfg->display.sar.den = 1;
448 unsigned zoom_den = 1000;
449 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
450 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
451 cfg->zoom.num = zoom_num;
452 cfg->zoom.den = zoom_den;
453 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
454 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
455 const int align_mask = var_CreateGetInteger(vout, "align");
456 if (align_mask & 0x1)
457 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
458 else if (align_mask & 0x2)
459 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
460 if (align_mask & 0x4)
461 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
462 else if (align_mask & 0x8)
463 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
466 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
467 const vout_window_cfg_t *cfg)
470 vout_window_cfg_t cfg_override = *cfg;
472 if (!var_InheritBool( vout, "embedded-video"))
473 cfg_override.is_standalone = true;
475 if (vout->p->window.is_unused && vout->p->window.object) {
476 assert(!vout->p->splitter_name);
477 if (!cfg_override.is_standalone == !vout->p->window.cfg.is_standalone &&
478 cfg_override.type == vout->p->window.cfg.type) {
479 /* Reuse the stored window */
480 msg_Dbg(vout, "Reusing previous vout window");
481 vout_window_t *window = vout->p->window.object;
482 if (cfg_override.width != vout->p->window.cfg.width ||
483 cfg_override.height != vout->p->window.cfg.height)
484 vout_window_SetSize(window,
485 cfg_override.width, cfg_override.height);
486 vout->p->window.is_unused = false;
487 vout->p->window.cfg = cfg_override;
491 vout_window_Delete(vout->p->window.object);
492 vout->p->window.is_unused = true;
493 vout->p->window.object = NULL;
496 vout_window_t *window = vout_window_New(VLC_OBJECT(vout), NULL,
500 if (!vout->p->splitter_name) {
501 vout->p->window.is_unused = false;
502 vout->p->window.cfg = cfg_override;
503 vout->p->window.object = window;
508 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
509 vout_window_t *window)
512 if (!vout->p->window.is_unused && vout->p->window.object == window) {
513 vout->p->window.is_unused = true;
514 } else if (vout->p->window.is_unused && vout->p->window.object && !window) {
515 vout_window_Delete(vout->p->window.object);
516 vout->p->window.is_unused = true;
517 vout->p->window.object = NULL;
519 vout_window_Delete(window);
524 static picture_t *VoutVideoFilterNewPicture(filter_t *filter)
526 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
527 return picture_pool_Get(vout->p->private_pool);
529 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
532 picture_Release(picture);
534 static int VoutVideoFilterAllocationSetup(filter_t *filter, void *data)
536 filter->pf_video_buffer_new = VoutVideoFilterNewPicture;
537 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
538 filter->p_owner = data; /* vout */
543 static int ThreadDisplayPicture(vout_thread_t *vout,
544 bool now, mtime_t *deadline)
546 vout_display_t *vd = vout->p->display.vd;
547 int displayed_count = 0;
551 const mtime_t date = mdate();
552 const bool is_paused = vout->p->pause.is_on;
553 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
556 /* FIXME/XXX we must redisplay the last decoded picture (because
557 * of potential vout updated, or filters update or SPU update)
558 * For now a high update period is needed but it coulmd be removed
560 * - vout module emits events from theselves.
561 * - *and* SPU is modified to emit an event or a deadline when needed.
563 * So it will be done latter.
566 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
568 is_forced = peek->b_force || is_paused || now;
569 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
570 picture_Release(peek);
576 /* FIXME a better way for this delay is needed */
577 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
578 if (date_update > date || !vout->p->displayed.decoded) {
579 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
584 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
586 if (*deadline > VOUT_MWAIT_TOLERANCE)
587 *deadline -= VOUT_MWAIT_TOLERANCE;
589 /* If we are too early and can wait, do it */
590 if (date < *deadline && !now)
595 decoded = vout->p->displayed.decoded;
596 vout->p->displayed.decoded = NULL;
598 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
600 if (!is_forced && !vout->p->is_late_dropped) {
601 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
602 const mtime_t late = predicted - decoded->date;
604 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
605 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
606 msg_Warn(vout, "rejected picture because of render time");
608 picture_Release(decoded);
615 vout->p->displayed.is_interlaced = !decoded->b_progressive;
616 vout->p->displayed.qtype = decoded->i_qtype;
618 vout->p->displayed.timestamp = decoded->date;
621 if (vout->p->displayed.decoded)
622 picture_Release(vout->p->displayed.decoded);
623 picture_Hold(decoded);
624 vout->p->displayed.decoded = decoded;
627 vout_chrono_Start(&vout->p->render);
629 picture_t *filtered = NULL;
631 vlc_mutex_lock(&vout->p->vfilter_lock);
632 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
633 //assert(filtered == decoded); // TODO implement
634 vlc_mutex_unlock(&vout->p->vfilter_lock);
640 * Check for subpictures to display
642 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
643 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
644 if (vout->p->pause.is_on)
645 spu_render_time = vout->p->pause.date;
647 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
649 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
656 * - be sure to end up with a direct buffer.
657 * - blend subtitles, and in a fast access buffer
659 picture_t *direct = NULL;
661 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
663 if (vout->p->is_decoder_pool_slow)
664 render = picture_NewFromFormat(&vd->source);
665 else if (vout->p->decoder_pool != vout->p->display_pool)
666 render = picture_pool_Get(vout->p->display_pool);
668 render = picture_pool_Get(vout->p->private_pool);
671 picture_Copy(render, filtered);
673 spu_RenderSubpictures(vout->p->p_spu,
675 subpic, &vd->source, spu_render_time);
677 if (vout->p->is_decoder_pool_slow) {
678 direct = picture_pool_Get(vout->p->display_pool);
680 picture_Copy(direct, render);
681 picture_Release(render);
686 picture_Release(filtered);
693 * Take a snapshot if requested
695 if (direct && do_snapshot)
696 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
698 /* Render the direct buffer returned by vout_RenderPicture */
700 vout_RenderWrapper(vout, direct);
702 vout_chrono_Stop(&vout->p->render);
707 msg_Info(vout, "render: avg %d ms var %d ms",
708 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
713 /* Wait the real date (for rendering jitter) */
715 mwait(decoded->date);
717 /* Display the direct buffer returned by vout_RenderPicture */
718 vout->p->displayed.date = mdate();
720 vout_DisplayWrapper(vout, direct);
726 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
727 if (displayed_count <= 0)
732 static void ThreadManage(vout_thread_t *vout,
734 vout_interlacing_support_t *interlacing,
735 vout_postprocessing_support_t *postprocessing)
737 vlc_mutex_lock(&vout->p->picture_lock);
739 *deadline = VLC_TS_INVALID;
740 ThreadDisplayPicture(vout, false, deadline);
742 const int picture_qtype = vout->p->displayed.qtype;
743 const bool picture_interlaced = vout->p->displayed.is_interlaced;
745 vlc_mutex_unlock(&vout->p->picture_lock);
747 /* Post processing */
748 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
751 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
753 vout_ManageWrapper(vout);
756 static void ThreadDisplaySubpicture(vout_thread_t *vout,
757 subpicture_t *subpicture)
759 spu_DisplaySubpicture(vout->p->p_spu, subpicture);
761 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
763 if (!vout->p->title.show)
766 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
767 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
771 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
774 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
775 fmt.video = vout->p->original;
777 vlc_mutex_lock(&vout->p->vfilter_lock);
779 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
780 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
782 msg_Err(vout, "Video filter chain creation failed");
784 vlc_mutex_unlock(&vout->p->vfilter_lock);
787 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
789 spu_ChangeFilters(vout->p->p_spu, filters);
792 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
794 assert(!vout->p->pause.is_on || !is_paused);
796 if (vout->p->pause.is_on) {
797 const mtime_t duration = date - vout->p->pause.date;
799 if (vout->p->step.timestamp > VLC_TS_INVALID)
800 vout->p->step.timestamp += duration;
801 if (vout->p->step.last > VLC_TS_INVALID)
802 vout->p->step.last += duration;
803 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
804 if (vout->p->displayed.decoded)
805 vout->p->displayed.decoded->date += duration;
807 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
809 vout->p->step.timestamp = VLC_TS_INVALID;
810 vout->p->step.last = VLC_TS_INVALID;
812 vout->p->pause.is_on = is_paused;
813 vout->p->pause.date = date;
816 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
818 vout->p->step.timestamp = VLC_TS_INVALID;
819 vout->p->step.last = VLC_TS_INVALID;
821 picture_t *last = vout->p->displayed.decoded;
823 if (( below && last->date <= date) ||
824 (!below && last->date >= date)) {
825 picture_Release(last);
827 vout->p->displayed.decoded = NULL;
828 vout->p->displayed.date = VLC_TS_INVALID;
829 vout->p->displayed.timestamp = VLC_TS_INVALID;
832 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
835 static void ThreadReset(vout_thread_t *vout)
837 ThreadFlush(vout, true, INT64_MAX);
838 if (vout->p->decoder_pool)
839 picture_pool_NonEmpty(vout->p->decoder_pool, true);
840 vout->p->pause.is_on = false;
841 vout->p->pause.date = mdate();
844 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
848 if (vout->p->step.last <= VLC_TS_INVALID)
849 vout->p->step.last = vout->p->displayed.timestamp;
852 if (ThreadDisplayPicture(vout, true, &dummy))
855 vout->p->step.timestamp = vout->p->displayed.timestamp;
857 if (vout->p->step.last > VLC_TS_INVALID &&
858 vout->p->step.timestamp > vout->p->step.last) {
859 *duration = vout->p->step.timestamp - vout->p->step.last;
860 vout->p->step.last = vout->p->step.timestamp;
861 /* TODO advance subpicture by the duration ... */
865 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
867 /* FIXME not sure setting "fullscreen" is good ... */
868 var_SetBool(vout, "fullscreen", fullscreen);
869 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
872 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
874 vout_SetWindowState(vout->p->display.vd,
875 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
876 VOUT_WINDOW_STATE_NORMAL);
879 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
881 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
884 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
886 if (num * 10 < den) {
889 } else if (num > den * 10) {
893 vout_SetDisplayZoom(vout->p->display.vd, num, den);
896 static void ThreadChangeAspectRatio(vout_thread_t *vout,
897 unsigned num, unsigned den)
899 const video_format_t *source = &vout->p->original;
901 if (num > 0 && den > 0) {
902 num *= source->i_visible_height;
903 den *= source->i_visible_width;
904 vlc_ureduce(&num, &den, num, den, 0);
906 vout_SetDisplayAspect(vout->p->display.vd, num, den);
910 static void ThreadExecuteCropWindow(vout_thread_t *vout,
911 unsigned crop_num, unsigned crop_den,
912 unsigned x, unsigned y,
913 unsigned width, unsigned height)
915 const video_format_t *source = &vout->p->original;
917 vout_SetDisplayCrop(vout->p->display.vd,
919 source->i_x_offset + x,
920 source->i_y_offset + y,
923 static void ThreadExecuteCropBorder(vout_thread_t *vout,
924 unsigned left, unsigned top,
925 unsigned right, unsigned bottom)
927 const video_format_t *source = &vout->p->original;
928 ThreadExecuteCropWindow(vout, 0, 0,
931 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
932 source->i_visible_width - (left + right),
933 source->i_visible_height - (top + bottom));
936 static void ThreadExecuteCropRatio(vout_thread_t *vout,
937 unsigned num, unsigned den)
939 const video_format_t *source = &vout->p->original;
940 ThreadExecuteCropWindow(vout, num, den,
942 source->i_visible_width,
943 source->i_visible_height);
946 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
948 vlc_mouse_Init(&vout->p->mouse);
949 vout->p->decoder_fifo = picture_fifo_New();
950 vout->p->decoder_pool = NULL;
951 vout->p->display_pool = NULL;
952 vout->p->private_pool = NULL;
954 vout->p->vfilter_chain =
955 filter_chain_New( vout, "video filter2", false,
956 VoutVideoFilterAllocationSetup, NULL, vout);
958 vout_display_state_t state_default;
960 VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
961 state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
962 VOUT_WINDOW_STATE_NORMAL;
963 state_default.sar.num = 0;
964 state_default.sar.den = 0;
966 state = &state_default;
969 if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
971 if (vout_InitWrapper(vout))
973 assert(vout->p->decoder_pool);
975 vout->p->displayed.decoded = NULL;
976 vout->p->displayed.date = VLC_TS_INVALID;
977 vout->p->displayed.decoded = NULL;
978 vout->p->displayed.timestamp = VLC_TS_INVALID;
979 vout->p->displayed.qtype = QTYPE_NONE;
980 vout->p->displayed.is_interlaced = false;
982 vout->p->step.last = VLC_TS_INVALID;
983 vout->p->step.timestamp = VLC_TS_INVALID;
985 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
989 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
991 /* Destroy the video filters2 */
992 filter_chain_Delete(vout->p->vfilter_chain);
994 /* Destroy translation tables */
995 if (vout->p->display.vd) {
996 if (vout->p->decoder_pool) {
997 ThreadFlush(vout, true, INT64_MAX);
998 vout_EndWrapper(vout);
1000 vout_CloseWrapper(vout, state);
1003 if (vout->p->decoder_fifo)
1004 picture_fifo_Delete(vout->p->decoder_fifo);
1005 assert(!vout->p->decoder_pool);
1008 static void ThreadInit(vout_thread_t *vout)
1010 vout->p->window.is_unused = true;
1011 vout->p->window.object = NULL;
1012 vout->p->dead = false;
1013 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
1014 vout->p->pause.is_on = false;
1015 vout->p->pause.date = VLC_TS_INVALID;
1017 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1020 static void ThreadClean(vout_thread_t *vout)
1022 if (vout->p->window.object) {
1023 assert(vout->p->window.is_unused);
1024 vout_window_Delete(vout->p->window.object);
1026 vout_chrono_Clean(&vout->p->render);
1027 vout->p->dead = true;
1028 vout_control_Dead(&vout->p->control);
1031 static int ThreadReinit(vout_thread_t *vout,
1032 const vout_configuration_t *cfg)
1034 video_format_t original;
1035 if (VoutValidateFormat(&original, cfg->fmt)) {
1036 ThreadStop(vout, NULL);
1038 return VLC_EGENERIC;
1040 if (video_format_IsSimilar(&original, &vout->p->original)) {
1041 if (cfg->dpb_size <= vout->p->dpb_size)
1043 msg_Warn(vout, "DPB need to be increased");
1046 vout_display_state_t state;
1047 memset(&state, 0, sizeof(state));
1049 ThreadStop(vout, &state);
1051 if (!state.cfg.is_fullscreen) {
1052 state.cfg.display.width = 0;
1053 state.cfg.display.height = 0;
1057 /* FIXME current vout "variables" are not in sync here anymore
1058 * and I am not sure what to do */
1060 vout->p->original = original;
1061 vout->p->dpb_size = cfg->dpb_size;
1062 if (ThreadStart(vout, &state)) {
1064 return VLC_EGENERIC;
1069 /*****************************************************************************
1070 * Thread: video output thread
1071 *****************************************************************************
1072 * Video output thread. This function does only returns when the thread is
1073 * terminated. It handles the pictures arriving in the video heap and the
1074 * display device events.
1075 *****************************************************************************/
1076 static void *Thread(void *object)
1078 vout_thread_t *vout = object;
1080 vout_interlacing_support_t interlacing = {
1081 .is_interlaced = false,
1084 vout_postprocessing_support_t postprocessing = {
1085 .qtype = QTYPE_NONE,
1088 mtime_t deadline = VLC_TS_INVALID;
1090 vout_control_cmd_t cmd;
1092 /* FIXME remove thoses ugly timeouts
1094 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1096 case VOUT_CONTROL_INIT:
1098 if (ThreadStart(vout, NULL)) {
1099 ThreadStop(vout, NULL);
1104 case VOUT_CONTROL_CLEAN:
1105 ThreadStop(vout, NULL);
1108 case VOUT_CONTROL_REINIT:
1109 if (ThreadReinit(vout, cmd.u.cfg))
1112 case VOUT_CONTROL_SUBPICTURE:
1113 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1114 cmd.u.subpicture = NULL;
1116 case VOUT_CONTROL_OSD_TITLE:
1117 ThreadDisplayOsdTitle(vout, cmd.u.string);
1119 case VOUT_CONTROL_CHANGE_FILTERS:
1120 ThreadChangeFilters(vout, cmd.u.string);
1122 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1123 ThreadChangeSubFilters(vout, cmd.u.string);
1125 case VOUT_CONTROL_PAUSE:
1126 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1128 case VOUT_CONTROL_FLUSH:
1129 ThreadFlush(vout, false, cmd.u.time);
1131 case VOUT_CONTROL_RESET:
1134 case VOUT_CONTROL_STEP:
1135 ThreadStep(vout, cmd.u.time_ptr);
1137 case VOUT_CONTROL_FULLSCREEN:
1138 ThreadChangeFullscreen(vout, cmd.u.boolean);
1140 case VOUT_CONTROL_ON_TOP:
1141 ThreadChangeOnTop(vout, cmd.u.boolean);
1143 case VOUT_CONTROL_DISPLAY_FILLED:
1144 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1146 case VOUT_CONTROL_ZOOM:
1147 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1149 case VOUT_CONTROL_ASPECT_RATIO:
1150 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1152 case VOUT_CONTROL_CROP_RATIO:
1153 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1155 case VOUT_CONTROL_CROP_WINDOW:
1156 ThreadExecuteCropWindow(vout, 0, 0,
1157 cmd.u.window.x, cmd.u.window.y,
1158 cmd.u.window.width, cmd.u.window.height);
1160 case VOUT_CONTROL_CROP_BORDER:
1161 ThreadExecuteCropBorder(vout,
1162 cmd.u.border.left, cmd.u.border.top,
1163 cmd.u.border.right, cmd.u.border.bottom);
1168 vout_control_cmd_Clean(&cmd);
1171 ThreadManage(vout, &deadline, &interlacing, &postprocessing);