1 /*****************************************************************************
2 * video_output.c : video output thread
4 * This module describes the programming interface for video output threads.
5 * It includes functions allowing to open a new thread, send pictures to a
6 * thread, and destroy a previously oppened video output thread.
7 *****************************************************************************
8 * Copyright (C) 2000-2007 the VideoLAN team
11 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12 * Gildas Bazin <gbazin@videolan.org>
13 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
37 #include <vlc_common.h>
39 #include <stdlib.h> /* free() */
45 #include <vlc_filter.h>
46 #include <vlc_vout_osd.h>
49 #include "vout_internal.h"
50 #include "interlacing.h"
51 #include "postprocessing.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static void *Thread(void *);
58 static void VoutDestructor(vlc_object_t *);
60 /* Maximum delay between 2 displayed pictures.
61 * XXX it is needed for now but should be removed in the long term.
63 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
66 * Late pictures having a delay higher than this value are thrashed.
68 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
70 /* Better be in advance when awakening than late... */
71 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
74 static int VoutValidateFormat(video_format_t *dst,
75 const video_format_t *src)
77 if (src->i_width <= 0 || src->i_height <= 0)
79 if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
83 video_format_Copy(dst, src);
84 dst->i_chroma = vlc_fourcc_GetCodec(VIDEO_ES, src->i_chroma);
85 vlc_ureduce( &dst->i_sar_num, &dst->i_sar_den,
86 src->i_sar_num, src->i_sar_den, 50000 );
87 if (dst->i_sar_num <= 0 || dst->i_sar_den <= 0) {
91 video_format_FixRgb(dst);
95 /*****************************************************************************
96 * vout_Request: find a video output thread, create one, or destroy one.
97 *****************************************************************************
98 * This function looks for a video output thread matching the current
99 * properties. If not found, it spawns a new one.
100 *****************************************************************************/
101 vout_thread_t *(vout_Request)(vlc_object_t *object, vout_thread_t *vout,
102 const video_format_t *fmt)
106 vout_CloseAndRelease(vout);
110 /* If a vout is provided, try reusing it */
112 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
113 vlc_object_detach(vout);
114 vlc_object_attach(vout, object);
115 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), true);
117 vout_control_cmd_t cmd;
118 vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
119 cmd.u.reinit.fmt = fmt;
121 vout_control_Push(&vout->p->control, &cmd);
122 vout_control_WaitEmpty(&vout->p->control);
123 if (!vout->p->dead) {
124 msg_Dbg(object, "reusing provided vout");
127 vout_CloseAndRelease(vout);
129 msg_Warn(object, "cannot reuse provided vout");
131 return vout_Create(object, fmt);
134 /*****************************************************************************
135 * vout_Create: creates a new video output thread
136 *****************************************************************************
137 * This function creates a new video output thread, and returns a pointer
138 * to its description. On error, it returns NULL.
139 *****************************************************************************/
140 vout_thread_t *(vout_Create)(vlc_object_t *object, const video_format_t *fmt)
142 video_format_t original;
143 if (VoutValidateFormat(&original, fmt))
146 /* Allocate descriptor */
147 vout_thread_t *vout = vlc_custom_create(object,
148 sizeof(*vout) + sizeof(*vout->p),
149 VLC_OBJECT_VOUT, "video output");
151 video_format_Clean(&original);
156 vout->p = (vout_thread_sys_t*)&vout[1];
158 vout->p->original = original;
160 vout_control_Init(&vout->p->control);
161 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
163 vout_statistic_Init(&vout->p->statistic);
165 vout->p->i_par_den = 1;
167 vout_snapshot_Init(&vout->p->snapshot);
169 /* Initialize locks */
170 vlc_mutex_init(&vout->p->picture_lock);
171 vlc_mutex_init(&vout->p->vfilter_lock);
173 /* Attach the new object now so we can use var inheritance below */
174 vlc_object_attach(vout, object);
176 /* Initialize subpicture unit */
177 vout->p->p_spu = spu_Create(vout);
179 /* Take care of some "interface/control" related initialisations */
182 /* Get splitter name if present */
183 char *splitter_name = var_GetNonEmptyString(vout, "vout-filter");
185 if (asprintf(&vout->p->splitter_name, "%s,none", splitter_name) < 0)
186 vout->p->splitter_name = NULL;
189 vout->p->splitter_name = NULL;
193 vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
196 vlc_object_set_destructor(vout, VoutDestructor);
199 if (vlc_clone(&vout->p->thread, Thread, vout,
200 VLC_THREAD_PRIORITY_OUTPUT)) {
201 vlc_object_release(vout);
204 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), true);
206 vout_control_WaitEmpty(&vout->p->control);
209 msg_Err(vout, "video output creation failed");
210 vout_CloseAndRelease(vout);
217 /*****************************************************************************
218 * vout_Close: Close a vout created by vout_Create.
219 *****************************************************************************
220 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
221 * You should NEVER call it on vout not obtained through vout_Create
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)
229 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
230 vlc_object_detach(vout->p->p_spu);
232 vout_snapshot_End(&vout->p->snapshot);
234 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
235 vlc_join(vout->p->thread, NULL);
239 static void VoutDestructor(vlc_object_t *object)
241 vout_thread_t *vout = (vout_thread_t *)object;
243 /* Make sure the vout was stopped first */
244 //assert(!vout->p_module);
246 free(vout->p->splitter_name);
249 spu_Destroy(vout->p->p_spu);
251 /* Destroy the locks */
252 vlc_mutex_destroy(&vout->p->picture_lock);
253 vlc_mutex_destroy(&vout->p->vfilter_lock);
254 vout_control_Clean(&vout->p->control);
257 vout_statistic_Clean(&vout->p->statistic);
260 vout_snapshot_Clean(&vout->p->snapshot);
262 video_format_Clean(&vout->p->original);
266 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
268 vout_control_cmd_t cmd;
269 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
270 cmd.u.pause.is_on = is_paused;
271 cmd.u.pause.date = date;
272 vout_control_Push(&vout->p->control, &cmd);
274 vout_control_WaitEmpty(&vout->p->control);
277 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
279 vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
282 void vout_Flush(vout_thread_t *vout, mtime_t date)
284 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
285 vout_control_WaitEmpty(&vout->p->control);
288 void vout_Reset(vout_thread_t *vout)
290 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
291 vout_control_WaitEmpty(&vout->p->control);
294 bool vout_IsEmpty(vout_thread_t *vout)
296 vlc_mutex_lock(&vout->p->picture_lock);
298 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
300 picture_Release(picture);
302 vlc_mutex_unlock(&vout->p->picture_lock);
307 void vout_FixLeaks( vout_thread_t *vout )
309 vlc_mutex_lock(&vout->p->picture_lock);
311 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
313 picture = picture_pool_Get(vout->p->decoder_pool);
317 picture_Release(picture);
318 /* Not all pictures has been displayed yet or some are
320 vlc_mutex_unlock(&vout->p->picture_lock);
324 /* There is no reason that no pictures are available, force one
325 * from the pool, becarefull with it though */
326 msg_Err(vout, "pictures leaked, trying to workaround");
329 picture_pool_NonEmpty(vout->p->decoder_pool, false);
331 vlc_mutex_unlock(&vout->p->picture_lock);
333 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
335 vout_control_cmd_t cmd;
336 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
337 cmd.u.time_ptr = duration;
339 vout_control_Push(&vout->p->control, &cmd);
340 vout_control_WaitEmpty(&vout->p->control);
343 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
346 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
349 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
351 spu_DisplaySubpicture(vout->p->p_spu, subpic);
353 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
355 return spu_RegisterChannel(vout->p->p_spu);
357 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
359 spu_ClearChannel(vout->p->p_spu, channel);
362 /* vout_Control* are usable by anyone at anytime */
363 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
365 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
368 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
370 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
373 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
375 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
378 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
380 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
383 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
384 unsigned num, unsigned den)
386 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
389 void vout_ControlChangeCropRatio(vout_thread_t *vout,
390 unsigned num, unsigned den)
392 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
395 void vout_ControlChangeCropWindow(vout_thread_t *vout,
396 int x, int y, int width, int height)
398 vout_control_cmd_t cmd;
399 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
402 cmd.u.window.width = width;
403 cmd.u.window.height = height;
405 vout_control_Push(&vout->p->control, &cmd);
407 void vout_ControlChangeCropBorder(vout_thread_t *vout,
408 int left, int top, int right, int bottom)
410 vout_control_cmd_t cmd;
411 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
412 cmd.u.border.left = left;
413 cmd.u.border.top = top;
414 cmd.u.border.right = right;
415 cmd.u.border.bottom = bottom;
417 vout_control_Push(&vout->p->control, &cmd);
419 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
421 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
424 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
426 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
431 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
433 /* Load configuration */
434 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
435 cfg->display.title = title;
436 const int display_width = var_CreateGetInteger(vout, "width");
437 const int display_height = var_CreateGetInteger(vout, "height");
438 cfg->display.width = display_width > 0 ? display_width : 0;
439 cfg->display.height = display_height > 0 ? display_height : 0;
440 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
441 cfg->display.sar.num = 1; /* TODO monitor AR */
442 cfg->display.sar.den = 1;
443 unsigned zoom_den = 1000;
444 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
445 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
446 cfg->zoom.num = zoom_num;
447 cfg->zoom.den = zoom_den;
448 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
449 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
450 const int align_mask = var_CreateGetInteger(vout, "align");
451 if (align_mask & 0x1)
452 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
453 else if (align_mask & 0x2)
454 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
455 if (align_mask & 0x4)
456 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
457 else if (align_mask & 0x8)
458 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
461 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
462 const vout_window_cfg_t *cfg)
465 vout_window_cfg_t cfg_override = *cfg;
467 if (!var_InheritBool( vout, "embedded-video"))
468 cfg_override.is_standalone = true;
470 if (vout->p->window.is_unused && vout->p->window.object) {
471 assert(!vout->p->splitter_name);
472 if (!cfg_override.is_standalone == !vout->p->window.cfg.is_standalone &&
473 cfg_override.type == vout->p->window.cfg.type) {
474 /* Reuse the stored window */
475 msg_Dbg(vout, "Reusing previous vout window");
476 vout_window_t *window = vout->p->window.object;
477 if (cfg_override.width != vout->p->window.cfg.width ||
478 cfg_override.height != vout->p->window.cfg.height)
479 vout_window_SetSize(window,
480 cfg_override.width, cfg_override.height);
481 vout->p->window.is_unused = false;
482 vout->p->window.cfg = cfg_override;
486 vout_window_Delete(vout->p->window.object);
487 vout->p->window.is_unused = true;
488 vout->p->window.object = NULL;
491 vout_window_t *window = vout_window_New(VLC_OBJECT(vout), NULL,
495 if (!vout->p->splitter_name) {
496 vout->p->window.is_unused = false;
497 vout->p->window.cfg = cfg_override;
498 vout->p->window.object = window;
503 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
504 vout_window_t *window)
507 if (!vout->p->window.is_unused && vout->p->window.object == window)
508 vout->p->window.is_unused = true;
510 vout_window_Delete(window);
514 static picture_t *VoutVideoFilterNewPicture(filter_t *filter)
516 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
517 return picture_pool_Get(vout->p->private_pool);
519 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
522 picture_Release(picture);
524 static int VoutVideoFilterAllocationSetup(filter_t *filter, void *data)
526 filter->pf_video_buffer_new = VoutVideoFilterNewPicture;
527 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
528 filter->p_owner = data; /* vout */
533 static int ThreadDisplayPicture(vout_thread_t *vout,
534 bool now, mtime_t *deadline)
536 vout_display_t *vd = vout->p->display.vd;
537 int displayed_count = 0;
541 const mtime_t date = mdate();
542 const bool is_paused = vout->p->pause.is_on;
543 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
546 /* FIXME/XXX we must redisplay the last decoded picture (because
547 * of potential vout updated, or filters update or SPU update)
548 * For now a high update period is needed but it coulmd be removed
550 * - vout module emits events from theselves.
551 * - *and* SPU is modified to emit an event or a deadline when needed.
553 * So it will be done latter.
556 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
558 is_forced = peek->b_force || is_paused || now;
559 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
560 picture_Release(peek);
566 /* FIXME a better way for this delay is needed */
567 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
568 if (date_update > date || !vout->p->displayed.decoded) {
569 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
574 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
576 if (*deadline > VOUT_MWAIT_TOLERANCE)
577 *deadline -= VOUT_MWAIT_TOLERANCE;
579 /* If we are too early and can wait, do it */
580 if (date < *deadline && !now)
585 decoded = vout->p->displayed.decoded;
586 vout->p->displayed.decoded = NULL;
588 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
590 if (!is_forced && !vout->p->is_late_dropped) {
591 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
592 const mtime_t late = predicted - decoded->date;
594 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
595 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
596 msg_Warn(vout, "rejected picture because of render time");
598 picture_Release(decoded);
605 vout->p->displayed.is_interlaced = !decoded->b_progressive;
606 vout->p->displayed.qtype = decoded->i_qtype;
608 vout->p->displayed.timestamp = decoded->date;
611 if (vout->p->displayed.decoded)
612 picture_Release(vout->p->displayed.decoded);
613 picture_Hold(decoded);
614 vout->p->displayed.decoded = decoded;
617 vout_chrono_Start(&vout->p->render);
619 picture_t *filtered = NULL;
621 vlc_mutex_lock(&vout->p->vfilter_lock);
622 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
623 //assert(filtered == decoded); // TODO implement
624 vlc_mutex_unlock(&vout->p->vfilter_lock);
630 * Check for subpictures to display
632 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
633 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
634 if (vout->p->pause.is_on)
635 spu_render_time = vout->p->pause.date;
637 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
639 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
646 * - be sure to end up with a direct buffer.
647 * - blend subtitles, and in a fast access buffer
649 picture_t *direct = NULL;
651 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
653 if (vout->p->is_decoder_pool_slow)
654 render = picture_NewFromFormat(&vd->source);
655 else if (vout->p->decoder_pool != vout->p->display_pool)
656 render = picture_pool_Get(vout->p->display_pool);
658 render = picture_pool_Get(vout->p->private_pool);
661 picture_Copy(render, filtered);
663 spu_RenderSubpictures(vout->p->p_spu,
665 subpic, &vd->source, spu_render_time);
667 if (vout->p->is_decoder_pool_slow) {
668 direct = picture_pool_Get(vout->p->display_pool);
670 picture_Copy(direct, render);
671 picture_Release(render);
676 picture_Release(filtered);
683 * Take a snapshot if requested
685 if (direct && do_snapshot)
686 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
688 /* Render the direct buffer returned by vout_RenderPicture */
690 vout_RenderWrapper(vout, direct);
692 vout_chrono_Stop(&vout->p->render);
697 msg_Info(vout, "render: avg %d ms var %d ms",
698 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
703 /* Wait the real date (for rendering jitter) */
705 mwait(decoded->date);
707 /* Display the direct buffer returned by vout_RenderPicture */
708 vout->p->displayed.date = mdate();
710 vout_DisplayWrapper(vout, direct);
716 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
717 if (displayed_count <= 0)
722 static void ThreadManage(vout_thread_t *vout,
724 vout_interlacing_support_t *interlacing,
725 vout_postprocessing_support_t *postprocessing)
727 vlc_mutex_lock(&vout->p->picture_lock);
729 *deadline = VLC_TS_INVALID;
730 ThreadDisplayPicture(vout, false, deadline);
732 const int picture_qtype = vout->p->displayed.qtype;
733 const bool picture_interlaced = vout->p->displayed.is_interlaced;
735 vlc_mutex_unlock(&vout->p->picture_lock);
737 /* Post processing */
738 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
741 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
743 vout_ManageWrapper(vout);
746 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
748 if (!vout->p->title.show)
751 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
752 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
756 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
759 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
760 fmt.video = vout->p->original;
762 vlc_mutex_lock(&vout->p->vfilter_lock);
764 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
765 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
767 msg_Err(vout, "Video filter chain creation failed");
769 vlc_mutex_unlock(&vout->p->vfilter_lock);
772 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
774 spu_ChangeFilters(vout->p->p_spu, filters);
777 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
779 assert(!vout->p->pause.is_on || !is_paused);
781 if (vout->p->pause.is_on) {
782 const mtime_t duration = date - vout->p->pause.date;
784 if (vout->p->step.timestamp > VLC_TS_INVALID)
785 vout->p->step.timestamp += duration;
786 if (vout->p->step.last > VLC_TS_INVALID)
787 vout->p->step.last += duration;
788 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
789 if (vout->p->displayed.decoded)
790 vout->p->displayed.decoded->date += duration;
792 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
794 vout->p->step.timestamp = VLC_TS_INVALID;
795 vout->p->step.last = VLC_TS_INVALID;
797 vout->p->pause.is_on = is_paused;
798 vout->p->pause.date = date;
801 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
803 vout->p->step.timestamp = VLC_TS_INVALID;
804 vout->p->step.last = VLC_TS_INVALID;
806 picture_t *last = vout->p->displayed.decoded;
808 if (( below && last->date <= date) ||
809 (!below && last->date >= date)) {
810 picture_Release(last);
812 vout->p->displayed.decoded = NULL;
813 vout->p->displayed.date = VLC_TS_INVALID;
814 vout->p->displayed.timestamp = VLC_TS_INVALID;
817 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
820 static void ThreadReset(vout_thread_t *vout)
822 ThreadFlush(vout, true, INT64_MAX);
823 if (vout->p->decoder_pool)
824 picture_pool_NonEmpty(vout->p->decoder_pool, true);
825 vout->p->pause.is_on = false;
826 vout->p->pause.date = mdate();
829 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
833 if (vout->p->step.last <= VLC_TS_INVALID)
834 vout->p->step.last = vout->p->displayed.timestamp;
837 if (ThreadDisplayPicture(vout, true, &dummy))
840 vout->p->step.timestamp = vout->p->displayed.timestamp;
842 if (vout->p->step.last > VLC_TS_INVALID &&
843 vout->p->step.timestamp > vout->p->step.last) {
844 *duration = vout->p->step.timestamp - vout->p->step.last;
845 vout->p->step.last = vout->p->step.timestamp;
846 /* TODO advance subpicture by the duration ... */
850 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
852 /* FIXME not sure setting "fullscreen" is good ... */
853 var_SetBool(vout, "fullscreen", fullscreen);
854 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
857 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
859 vout_SetWindowState(vout->p->display.vd,
860 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
861 VOUT_WINDOW_STATE_NORMAL);
864 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
866 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
869 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
871 if (num * 10 < den) {
874 } else if (num > den * 10) {
878 vout_SetDisplayZoom(vout->p->display.vd, num, den);
881 static void ThreadChangeAspectRatio(vout_thread_t *vout,
882 unsigned num, unsigned den)
884 const video_format_t *source = &vout->p->original;
886 if (num > 0 && den > 0) {
887 num *= source->i_visible_height;
888 den *= source->i_visible_width;
889 vlc_ureduce(&num, &den, num, den, 0);
891 vout_SetDisplayAspect(vout->p->display.vd, num, den);
895 static void ThreadExecuteCropWindow(vout_thread_t *vout,
896 unsigned crop_num, unsigned crop_den,
897 unsigned x, unsigned y,
898 unsigned width, unsigned height)
900 const video_format_t *source = &vout->p->original;
902 vout_SetDisplayCrop(vout->p->display.vd,
904 source->i_x_offset + x,
905 source->i_y_offset + y,
908 static void ThreadExecuteCropBorder(vout_thread_t *vout,
909 unsigned left, unsigned top,
910 unsigned right, unsigned bottom)
912 const video_format_t *source = &vout->p->original;
913 ThreadExecuteCropWindow(vout, 0, 0,
916 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
917 source->i_visible_width - (left + right),
918 source->i_visible_height - (top + bottom));
921 static void ThreadExecuteCropRatio(vout_thread_t *vout,
922 unsigned num, unsigned den)
924 const video_format_t *source = &vout->p->original;
925 ThreadExecuteCropWindow(vout, num, den,
927 source->i_visible_width,
928 source->i_visible_height);
931 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
933 vlc_mouse_Init(&vout->p->mouse);
934 vout->p->decoder_fifo = picture_fifo_New();
935 vout->p->decoder_pool = NULL;
936 vout->p->display_pool = NULL;
937 vout->p->private_pool = NULL;
939 vout->p->vfilter_chain =
940 filter_chain_New( vout, "video filter2", false,
941 VoutVideoFilterAllocationSetup, NULL, vout);
943 vout_display_state_t state_default;
945 VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
946 state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
947 VOUT_WINDOW_STATE_NORMAL;
948 state_default.sar.num = 0;
949 state_default.sar.den = 0;
951 state = &state_default;
954 if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
956 if (vout_InitWrapper(vout))
958 assert(vout->p->decoder_pool);
960 vout->p->displayed.decoded = NULL;
961 vout->p->displayed.date = VLC_TS_INVALID;
962 vout->p->displayed.decoded = NULL;
963 vout->p->displayed.timestamp = VLC_TS_INVALID;
964 vout->p->displayed.qtype = QTYPE_NONE;
965 vout->p->displayed.is_interlaced = false;
967 vout->p->step.last = VLC_TS_INVALID;
968 vout->p->step.timestamp = VLC_TS_INVALID;
970 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
974 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
976 /* Destroy the video filters2 */
977 filter_chain_Delete(vout->p->vfilter_chain);
979 /* Destroy translation tables */
980 if (vout->p->display.vd) {
981 if (vout->p->decoder_pool) {
982 ThreadFlush(vout, true, INT64_MAX);
983 vout_EndWrapper(vout);
985 vout_CloseWrapper(vout, state);
988 if (vout->p->decoder_fifo)
989 picture_fifo_Delete(vout->p->decoder_fifo);
990 assert(!vout->p->decoder_pool);
993 static void ThreadInit(vout_thread_t *vout)
995 vout->p->window.is_unused = true;
996 vout->p->window.object = NULL;
997 vout->p->dead = false;
998 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
999 vout->p->pause.is_on = false;
1000 vout->p->pause.date = VLC_TS_INVALID;
1002 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1005 static void ThreadClean(vout_thread_t *vout)
1007 if (vout->p->window.object) {
1008 assert(vout->p->window.is_unused);
1009 vout_window_Delete(vout->p->window.object);
1011 vout_chrono_Clean(&vout->p->render);
1012 vout->p->dead = true;
1013 vout_control_Dead(&vout->p->control);
1016 static int ThreadReinit(vout_thread_t *vout,
1017 const video_format_t *fmt)
1019 video_format_t original;
1020 if (VoutValidateFormat(&original, fmt)) {
1021 ThreadStop(vout, NULL);
1023 return VLC_EGENERIC;
1025 if (video_format_IsSimilar(&original, &vout->p->original))
1028 vout_display_state_t state;
1029 memset(&state, 0, sizeof(state));
1031 ThreadStop(vout, &state);
1033 if (!state.cfg.is_fullscreen) {
1034 state.cfg.display.width = 0;
1035 state.cfg.display.height = 0;
1039 /* FIXME current vout "variables" are not in sync here anymore
1040 * and I am not sure what to do */
1042 vout->p->original = original;
1043 if (ThreadStart(vout, &state)) {
1045 return VLC_EGENERIC;
1050 /*****************************************************************************
1051 * Thread: video output thread
1052 *****************************************************************************
1053 * Video output thread. This function does only returns when the thread is
1054 * terminated. It handles the pictures arriving in the video heap and the
1055 * display device events.
1056 *****************************************************************************/
1057 static void *Thread(void *object)
1059 vout_thread_t *vout = object;
1061 vout_interlacing_support_t interlacing = {
1062 .is_interlaced = false,
1065 vout_postprocessing_support_t postprocessing = {
1066 .qtype = QTYPE_NONE,
1069 mtime_t deadline = VLC_TS_INVALID;
1071 vout_control_cmd_t cmd;
1073 /* FIXME remove thoses ugly timeouts
1075 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1077 case VOUT_CONTROL_INIT:
1079 if (ThreadStart(vout, NULL)) {
1080 ThreadStop(vout, NULL);
1085 case VOUT_CONTROL_CLEAN:
1086 ThreadStop(vout, NULL);
1089 case VOUT_CONTROL_REINIT:
1090 if (ThreadReinit(vout, cmd.u.reinit.fmt))
1093 case VOUT_CONTROL_OSD_TITLE:
1094 ThreadDisplayOsdTitle(vout, cmd.u.string);
1096 case VOUT_CONTROL_CHANGE_FILTERS:
1097 ThreadChangeFilters(vout, cmd.u.string);
1099 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1100 ThreadChangeSubFilters(vout, cmd.u.string);
1102 case VOUT_CONTROL_PAUSE:
1103 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1105 case VOUT_CONTROL_FLUSH:
1106 ThreadFlush(vout, false, cmd.u.time);
1108 case VOUT_CONTROL_RESET:
1111 case VOUT_CONTROL_STEP:
1112 ThreadStep(vout, cmd.u.time_ptr);
1114 case VOUT_CONTROL_FULLSCREEN:
1115 ThreadChangeFullscreen(vout, cmd.u.boolean);
1117 case VOUT_CONTROL_ON_TOP:
1118 ThreadChangeOnTop(vout, cmd.u.boolean);
1120 case VOUT_CONTROL_DISPLAY_FILLED:
1121 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1123 case VOUT_CONTROL_ZOOM:
1124 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1126 case VOUT_CONTROL_ASPECT_RATIO:
1127 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1129 case VOUT_CONTROL_CROP_RATIO:
1130 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1132 case VOUT_CONTROL_CROP_WINDOW:
1133 ThreadExecuteCropWindow(vout, 0, 0,
1134 cmd.u.window.x, cmd.u.window.y,
1135 cmd.u.window.width, cmd.u.window.height);
1137 case VOUT_CONTROL_CROP_BORDER:
1138 ThreadExecuteCropBorder(vout,
1139 cmd.u.border.left, cmd.u.border.top,
1140 cmd.u.border.right, cmd.u.border.bottom);
1145 vout_control_cmd_Clean(&cmd);
1148 ThreadManage(vout, &deadline, &interlacing, &postprocessing);