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->filter.lock);
129 vlc_mutex_init(&vout->p->spu_lock);
131 /* Attach the new object now so we can use var inheritance below */
132 vlc_object_attach(vout, object);
134 /* Initialize subpicture unit */
135 vout->p->p_spu = spu_Create(vout);
137 /* Take care of some "interface/control" related initialisations */
140 /* Get splitter name if present */
141 char *splitter_name = var_GetNonEmptyString(vout, "vout-filter");
143 if (asprintf(&vout->p->splitter_name, "%s,none", splitter_name) < 0)
144 vout->p->splitter_name = NULL;
147 vout->p->splitter_name = NULL;
151 vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
154 vlc_object_set_destructor(vout, VoutDestructor);
157 if (vlc_clone(&vout->p->thread, Thread, vout,
158 VLC_THREAD_PRIORITY_OUTPUT)) {
159 spu_Destroy(vout->p->p_spu);
160 vlc_object_release(vout);
164 vout_control_WaitEmpty(&vout->p->control);
167 msg_Err(vout, "video output creation failed");
168 vout_CloseAndRelease(vout);
172 vout->p->input = cfg->input;
174 spu_Attach(vout->p->p_spu, vout->p->input, true);
179 vout_thread_t *(vout_Request)(vlc_object_t *object,
180 const vout_configuration_t *cfg)
182 vout_thread_t *vout = cfg->vout;
183 if (cfg->change_fmt && !cfg->fmt) {
185 vout_CloseAndRelease(vout);
189 /* If a vout is provided, try reusing it */
191 if (vout->p->input != cfg->input) {
193 spu_Attach(vout->p->p_spu, vout->p->input, false);
194 vout->p->input = cfg->input;
196 spu_Attach(vout->p->p_spu, vout->p->input, true);
199 if (cfg->change_fmt) {
200 vout_control_cmd_t cmd;
201 vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
204 vout_control_Push(&vout->p->control, &cmd);
205 vout_control_WaitEmpty(&vout->p->control);
208 if (!vout->p->dead) {
209 msg_Dbg(object, "reusing provided vout");
212 vout_CloseAndRelease(vout);
214 msg_Warn(object, "cannot reuse provided vout");
216 return VoutCreate(object, cfg);
219 void vout_Close(vout_thread_t *vout)
224 spu_Attach(vout->p->p_spu, vout->p->input, false);
226 vout_snapshot_End(&vout->p->snapshot);
228 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
229 vlc_join(vout->p->thread, NULL);
231 vlc_mutex_lock(&vout->p->spu_lock);
232 spu_Destroy(vout->p->p_spu);
233 vout->p->p_spu = NULL;
234 vlc_mutex_unlock(&vout->p->spu_lock);
238 static void VoutDestructor(vlc_object_t *object)
240 vout_thread_t *vout = (vout_thread_t *)object;
242 /* Make sure the vout was stopped first */
243 //assert(!vout->p_module);
245 free(vout->p->splitter_name);
247 /* Destroy the locks */
248 vlc_mutex_destroy(&vout->p->spu_lock);
249 vlc_mutex_destroy(&vout->p->picture_lock);
250 vlc_mutex_destroy(&vout->p->filter.lock);
251 vout_control_Clean(&vout->p->control);
254 vout_statistic_Clean(&vout->p->statistic);
257 vout_snapshot_Clean(&vout->p->snapshot);
259 video_format_Clean(&vout->p->original);
263 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
265 vout_control_cmd_t cmd;
266 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
267 cmd.u.pause.is_on = is_paused;
268 cmd.u.pause.date = date;
269 vout_control_Push(&vout->p->control, &cmd);
271 vout_control_WaitEmpty(&vout->p->control);
274 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
276 vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
279 void vout_Flush(vout_thread_t *vout, mtime_t date)
281 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
282 vout_control_WaitEmpty(&vout->p->control);
285 void vout_Reset(vout_thread_t *vout)
287 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
288 vout_control_WaitEmpty(&vout->p->control);
291 bool vout_IsEmpty(vout_thread_t *vout)
293 vlc_mutex_lock(&vout->p->picture_lock);
295 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
297 picture_Release(picture);
299 vlc_mutex_unlock(&vout->p->picture_lock);
304 void vout_FixLeaks( vout_thread_t *vout )
306 vlc_mutex_lock(&vout->p->picture_lock);
308 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
310 picture = picture_pool_Get(vout->p->decoder_pool);
314 picture_Release(picture);
315 /* Not all pictures has been displayed yet or some are
317 vlc_mutex_unlock(&vout->p->picture_lock);
321 /* There is no reason that no pictures are available, force one
322 * from the pool, becarefull with it though */
323 msg_Err(vout, "pictures leaked, trying to workaround");
326 picture_pool_NonEmpty(vout->p->decoder_pool, false);
328 vlc_mutex_unlock(&vout->p->picture_lock);
330 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
332 vout_control_cmd_t cmd;
333 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
334 cmd.u.time_ptr = duration;
336 vout_control_Push(&vout->p->control, &cmd);
337 vout_control_WaitEmpty(&vout->p->control);
340 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
343 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
346 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
348 vout_control_cmd_t cmd;
349 vout_control_cmd_Init(&cmd, VOUT_CONTROL_SUBPICTURE);
350 cmd.u.subpicture = subpic;
352 vout_control_Push(&vout->p->control, &cmd);
354 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
356 int channel = SPU_DEFAULT_CHANNEL;
358 vlc_mutex_lock(&vout->p->spu_lock);
360 channel = spu_RegisterChannel(vout->p->p_spu);
361 vlc_mutex_unlock(&vout->p->spu_lock);
365 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
367 vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_FLUSH_SUBPICTURE,
371 /* vout_Control* are usable by anyone at anytime */
372 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
374 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
377 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
379 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
382 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
384 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
387 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
389 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
392 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
393 unsigned num, unsigned den)
395 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
398 void vout_ControlChangeCropRatio(vout_thread_t *vout,
399 unsigned num, unsigned den)
401 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
404 void vout_ControlChangeCropWindow(vout_thread_t *vout,
405 int x, int y, int width, int height)
407 vout_control_cmd_t cmd;
408 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
411 cmd.u.window.width = width;
412 cmd.u.window.height = height;
414 vout_control_Push(&vout->p->control, &cmd);
416 void vout_ControlChangeCropBorder(vout_thread_t *vout,
417 int left, int top, int right, int bottom)
419 vout_control_cmd_t cmd;
420 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
421 cmd.u.border.left = left;
422 cmd.u.border.top = top;
423 cmd.u.border.right = right;
424 cmd.u.border.bottom = bottom;
426 vout_control_Push(&vout->p->control, &cmd);
428 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
430 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
433 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
435 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
438 void vout_ControlChangeSubMargin(vout_thread_t *vout, int margin)
440 vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_MARGIN,
445 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
447 /* Load configuration */
448 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
449 cfg->display.title = title;
450 const int display_width = var_CreateGetInteger(vout, "width");
451 const int display_height = var_CreateGetInteger(vout, "height");
452 cfg->display.width = display_width > 0 ? display_width : 0;
453 cfg->display.height = display_height > 0 ? display_height : 0;
454 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
455 cfg->display.sar.num = 1; /* TODO monitor AR */
456 cfg->display.sar.den = 1;
457 unsigned zoom_den = 1000;
458 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
459 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
460 cfg->zoom.num = zoom_num;
461 cfg->zoom.den = zoom_den;
462 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
463 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
464 const int align_mask = var_CreateGetInteger(vout, "align");
465 if (align_mask & 0x1)
466 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
467 else if (align_mask & 0x2)
468 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
469 if (align_mask & 0x4)
470 cfg->align.vertical = VOUT_DISPLAY_ALIGN_TOP;
471 else if (align_mask & 0x8)
472 cfg->align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
475 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
476 const vout_window_cfg_t *cfg)
479 vout_window_cfg_t cfg_override = *cfg;
481 if (!var_InheritBool( vout, "embedded-video"))
482 cfg_override.is_standalone = true;
484 if (vout->p->window.is_unused && vout->p->window.object) {
485 assert(!vout->p->splitter_name);
486 if (!cfg_override.is_standalone == !vout->p->window.cfg.is_standalone &&
487 cfg_override.type == vout->p->window.cfg.type) {
488 /* Reuse the stored window */
489 msg_Dbg(vout, "Reusing previous vout window");
490 vout_window_t *window = vout->p->window.object;
491 if (cfg_override.width != vout->p->window.cfg.width ||
492 cfg_override.height != vout->p->window.cfg.height)
493 vout_window_SetSize(window,
494 cfg_override.width, cfg_override.height);
495 vout->p->window.is_unused = false;
496 vout->p->window.cfg = cfg_override;
500 vout_window_Delete(vout->p->window.object);
501 vout->p->window.is_unused = true;
502 vout->p->window.object = NULL;
505 vout_window_t *window = vout_window_New(VLC_OBJECT(vout), "$window",
509 if (!vout->p->splitter_name) {
510 vout->p->window.is_unused = false;
511 vout->p->window.cfg = cfg_override;
512 vout->p->window.object = window;
517 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
518 vout_window_t *window)
521 if (!vout->p->window.is_unused && vout->p->window.object == window) {
522 vout->p->window.is_unused = true;
523 } else if (vout->p->window.is_unused && vout->p->window.object && !window) {
524 vout_window_Delete(vout->p->window.object);
525 vout->p->window.is_unused = true;
526 vout->p->window.object = NULL;
528 vout_window_Delete(window);
534 static picture_t *VoutVideoFilterInteractiveNewPicture(filter_t *filter)
536 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
538 return picture_pool_Get(vout->p->private_pool);
540 static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
542 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
544 vlc_assert_locked(&vout->p->filter.lock);
545 if (filter_chain_GetLength(vout->p->filter.chain_interactive) == 0)
546 return VoutVideoFilterInteractiveNewPicture(filter);
547 return picture_NewFromFormat(&filter->fmt_out.video);
549 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
552 picture_Release(picture);
554 static int VoutVideoFilterStaticAllocationSetup(filter_t *filter, void *data)
556 filter->pf_video_buffer_new = VoutVideoFilterStaticNewPicture;
557 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
558 filter->p_owner = data; /* vout */
561 static int VoutVideoFilterInteractiveAllocationSetup(filter_t *filter, void *data)
563 filter->pf_video_buffer_new = VoutVideoFilterInteractiveNewPicture;
564 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
565 filter->p_owner = data; /* vout */
570 static picture_t *ThreadDisplayGetDecodedPicture(vout_thread_t *vout,
571 int *lost_count, bool *is_forced,
572 bool now, mtime_t *deadline)
574 vout_display_t *vd = vout->p->display.vd;
576 const mtime_t date = mdate();
577 const bool is_paused = vout->p->pause.is_on;
578 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
580 mtime_t filter_delay = 0;
581 for (int i = 0; i < VOUT_FILTER_DELAYS; i++)
582 filter_delay = __MAX(filter_delay, vout->p->filter.delay[i]);
584 /* FIXME/XXX we must redisplay the last decoded picture (because
585 * of potential vout updated, or filters update or SPU update)
586 * For now a high update period is needed but it coulmd be removed
588 * - vout module emits events from theselves.
589 * - *and* SPU is modified to emit an event or a deadline when needed.
591 * So it will be done latter.
594 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
596 *is_forced = peek->b_force || is_paused || now;
597 *deadline = (*is_forced ? date : peek->date) -
598 vout_chrono_GetHigh(&vout->p->render) -
600 picture_Release(peek);
606 /* FIXME a better way for this delay is needed */
607 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
608 if (date_update > date || !vout->p->displayed.decoded) {
609 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
614 *deadline = date - vout_chrono_GetHigh(&vout->p->render) - filter_delay;
616 if (*deadline > VOUT_MWAIT_TOLERANCE)
617 *deadline -= VOUT_MWAIT_TOLERANCE;
619 /* If we are too early and can wait, do it */
620 if (date < *deadline && !now)
625 decoded = vout->p->displayed.decoded;
626 vout->p->displayed.decoded = NULL;
628 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
630 if (!*is_forced && !vout->p->is_late_dropped) {
631 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
632 const mtime_t late = predicted - decoded->date;
634 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
635 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
636 msg_Warn(vout, "rejected picture because of render time");
638 picture_Release(decoded);
645 vout->p->displayed.is_interlaced = !decoded->b_progressive;
646 vout->p->displayed.qtype = decoded->i_qtype;
648 vout->p->displayed.timestamp = decoded->date;
651 if (vout->p->displayed.decoded)
652 picture_Release(vout->p->displayed.decoded);
653 picture_Hold(decoded);
654 vout->p->displayed.decoded = decoded;
659 static int ThreadDisplayPicture(vout_thread_t *vout,
660 bool now, mtime_t *deadline)
662 vout_display_t *vd = vout->p->display.vd;
663 int displayed_count = 0;
668 picture_t *decoded = ThreadDisplayGetDecodedPicture(vout,
669 &lost_count, &is_forced,
675 vout_chrono_Start(&vout->p->render);
677 picture_t *filtered = NULL;
679 vlc_mutex_lock(&vout->p->filter.lock);
680 filtered = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
682 filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, filtered);
683 vlc_mutex_unlock(&vout->p->filter.lock);
686 vout->p->filter.delay[vout->p->filter.delay_index] = decoded->date - filtered->date;
687 vout->p->filter.delay_index = (vout->p->filter.delay_index + 1) % VOUT_FILTER_DELAYS;
691 * Check for subpictures to display
693 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
694 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
695 if (vout->p->pause.is_on)
696 spu_render_time = vout->p->pause.date;
698 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
700 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
707 * - be sure to end up with a direct buffer.
708 * - blend subtitles, and in a fast access buffer
710 picture_t *direct = NULL;
712 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
714 if (vout->p->is_decoder_pool_slow)
715 render = picture_NewFromFormat(&vd->source);
716 else if (vout->p->decoder_pool != vout->p->display_pool)
717 render = picture_pool_Get(vout->p->display_pool);
719 render = picture_pool_Get(vout->p->private_pool);
722 picture_Copy(render, filtered);
724 spu_RenderSubpictures(vout->p->p_spu,
726 subpic, &vd->source, spu_render_time);
728 if (vout->p->is_decoder_pool_slow) {
729 direct = picture_pool_Get(vout->p->display_pool);
731 picture_Copy(direct, render);
732 picture_Release(render);
737 picture_Release(filtered);
744 * Take a snapshot if requested
746 if (direct && do_snapshot)
747 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
749 /* Render the direct buffer returned by vout_RenderPicture */
751 vout_RenderWrapper(vout, direct);
753 vout_chrono_Stop(&vout->p->render);
758 msg_Info(vout, "render: avg %d ms var %d ms",
759 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
764 /* Wait the real date (for rendering jitter) */
768 /* Display the direct buffer returned by vout_RenderPicture */
769 vout->p->displayed.date = mdate();
771 vout_DisplayWrapper(vout, direct);
777 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
778 if (displayed_count <= 0)
783 static void ThreadManage(vout_thread_t *vout,
785 vout_interlacing_support_t *interlacing,
786 vout_postprocessing_support_t *postprocessing)
788 vlc_mutex_lock(&vout->p->picture_lock);
790 *deadline = VLC_TS_INVALID;
791 ThreadDisplayPicture(vout, false, deadline);
793 const int picture_qtype = vout->p->displayed.qtype;
794 const bool picture_interlaced = vout->p->displayed.is_interlaced;
796 vlc_mutex_unlock(&vout->p->picture_lock);
798 /* Post processing */
799 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
802 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
804 vout_ManageWrapper(vout);
807 static void ThreadDisplaySubpicture(vout_thread_t *vout,
808 subpicture_t *subpicture)
810 spu_DisplaySubpicture(vout->p->p_spu, subpicture);
813 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
815 spu_ClearChannel(vout->p->p_spu, channel);
818 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
820 if (!vout->p->title.show)
823 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
824 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
833 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
835 vlc_array_t array_static;
836 vlc_array_t array_interactive;
838 vlc_array_init(&array_static);
839 vlc_array_init(&array_interactive);
840 char *current = filters ? strdup(filters) : NULL;
844 char *next = config_ChainCreate(&name, &cfg, current);
847 vout_filter_t *e = xmalloc(sizeof(*e));
850 if (!strcmp(e->name, "deinterlace") ||
851 !strcmp(e->name, "postproc")) {
852 vlc_array_append(&array_static, e);
854 vlc_array_append(&array_interactive, e);
858 config_ChainDestroy(cfg);
866 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
867 fmt.video = vout->p->original;
869 vlc_mutex_lock(&vout->p->filter.lock);
871 filter_chain_Reset(vout->p->filter.chain_static, &fmt, &fmt);
872 filter_chain_Reset(vout->p->filter.chain_interactive, &fmt, &fmt);
874 for (int a = 0; a < 2; a++) {
875 vlc_array_t *array = a == 0 ? &array_static :
877 filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
878 vout->p->filter.chain_interactive;
880 for (int i = 0; i < vlc_array_count(array); i++) {
881 vout_filter_t *e = vlc_array_item_at_index(array, i);
882 msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
883 if (!filter_chain_AppendFilter(chain, e->name, e->cfg, NULL, NULL)) {
884 msg_Err(vout, "Failed to add filter '%s'", e->name);
885 config_ChainDestroy(e->cfg);
890 vlc_array_clear(array);
893 vlc_mutex_unlock(&vout->p->filter.lock);
895 vout->p->filter.delay_index = 0;
896 for (int i = 0; i < VOUT_FILTER_DELAYS; i++)
897 vout->p->filter.delay[i] = 0;
900 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
902 spu_ChangeFilters(vout->p->p_spu, filters);
904 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
906 spu_ChangeMargin(vout->p->p_spu, margin);
909 static void ThreadFilterFlush(vout_thread_t *vout)
911 vlc_mutex_lock(&vout->p->filter.lock);
912 filter_chain_VideoFlush(vout->p->filter.chain_static);
913 filter_chain_VideoFlush(vout->p->filter.chain_interactive);
914 vlc_mutex_unlock(&vout->p->filter.lock);
917 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
919 assert(!vout->p->pause.is_on || !is_paused);
921 if (vout->p->pause.is_on) {
922 const mtime_t duration = date - vout->p->pause.date;
924 if (vout->p->step.timestamp > VLC_TS_INVALID)
925 vout->p->step.timestamp += duration;
926 if (vout->p->step.last > VLC_TS_INVALID)
927 vout->p->step.last += duration;
928 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
929 if (vout->p->displayed.decoded)
930 vout->p->displayed.decoded->date += duration;
932 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
934 ThreadFilterFlush(vout);
936 vout->p->step.timestamp = VLC_TS_INVALID;
937 vout->p->step.last = VLC_TS_INVALID;
939 vout->p->pause.is_on = is_paused;
940 vout->p->pause.date = date;
943 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
945 vout->p->step.timestamp = VLC_TS_INVALID;
946 vout->p->step.last = VLC_TS_INVALID;
948 picture_t *last = vout->p->displayed.decoded;
950 if (( below && last->date <= date) ||
951 (!below && last->date >= date)) {
952 picture_Release(last);
954 vout->p->displayed.decoded = NULL;
955 vout->p->displayed.date = VLC_TS_INVALID;
956 vout->p->displayed.timestamp = VLC_TS_INVALID;
959 ThreadFilterFlush(vout);
961 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
964 static void ThreadReset(vout_thread_t *vout)
966 ThreadFlush(vout, true, INT64_MAX);
967 if (vout->p->decoder_pool)
968 picture_pool_NonEmpty(vout->p->decoder_pool, true);
969 vout->p->pause.is_on = false;
970 vout->p->pause.date = mdate();
973 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
977 if (vout->p->step.last <= VLC_TS_INVALID)
978 vout->p->step.last = vout->p->displayed.timestamp;
981 if (ThreadDisplayPicture(vout, true, &dummy))
984 vout->p->step.timestamp = vout->p->displayed.timestamp;
986 if (vout->p->step.last > VLC_TS_INVALID &&
987 vout->p->step.timestamp > vout->p->step.last) {
988 *duration = vout->p->step.timestamp - vout->p->step.last;
989 vout->p->step.last = vout->p->step.timestamp;
990 /* TODO advance subpicture by the duration ... */
994 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
996 /* FIXME not sure setting "fullscreen" is good ... */
997 var_SetBool(vout, "fullscreen", fullscreen);
998 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
1001 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
1003 vout_SetWindowState(vout->p->display.vd,
1004 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
1005 VOUT_WINDOW_STATE_NORMAL);
1008 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1010 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1013 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1015 if (num * 10 < den) {
1018 } else if (num > den * 10) {
1022 vout_SetDisplayZoom(vout->p->display.vd, num, den);
1025 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1026 unsigned num, unsigned den)
1028 const video_format_t *source = &vout->p->original;
1030 if (num > 0 && den > 0) {
1031 num *= source->i_visible_height;
1032 den *= source->i_visible_width;
1033 vlc_ureduce(&num, &den, num, den, 0);
1035 vout_SetDisplayAspect(vout->p->display.vd, num, den);
1039 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1040 unsigned crop_num, unsigned crop_den,
1041 unsigned x, unsigned y,
1042 unsigned width, unsigned height)
1044 const video_format_t *source = &vout->p->original;
1046 vout_SetDisplayCrop(vout->p->display.vd,
1048 source->i_x_offset + x,
1049 source->i_y_offset + y,
1052 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1053 unsigned left, unsigned top,
1054 unsigned right, unsigned bottom)
1056 const video_format_t *source = &vout->p->original;
1057 ThreadExecuteCropWindow(vout, 0, 0,
1060 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
1061 source->i_visible_width - (left + right),
1062 source->i_visible_height - (top + bottom));
1065 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1066 unsigned num, unsigned den)
1068 const video_format_t *source = &vout->p->original;
1069 ThreadExecuteCropWindow(vout, num, den,
1071 source->i_visible_width,
1072 source->i_visible_height);
1075 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
1077 vlc_mouse_Init(&vout->p->mouse);
1078 vout->p->decoder_fifo = picture_fifo_New();
1079 vout->p->decoder_pool = NULL;
1080 vout->p->display_pool = NULL;
1081 vout->p->private_pool = NULL;
1083 vout->p->filter.chain_static =
1084 filter_chain_New( vout, "video filter2", false,
1085 VoutVideoFilterStaticAllocationSetup, NULL, vout);
1086 vout->p->filter.chain_interactive =
1087 filter_chain_New( vout, "video filter2", false,
1088 VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
1090 vout->p->filter.delay_index = 0;
1091 for (int i = 0; i < VOUT_FILTER_DELAYS; i++)
1092 vout->p->filter.delay[i] = 0;
1094 vout_display_state_t state_default;
1096 VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1097 state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
1098 VOUT_WINDOW_STATE_NORMAL;
1099 state_default.sar.num = 0;
1100 state_default.sar.den = 0;
1102 state = &state_default;
1105 if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1106 return VLC_EGENERIC;
1107 if (vout_InitWrapper(vout))
1108 return VLC_EGENERIC;
1109 assert(vout->p->decoder_pool);
1111 vout->p->displayed.decoded = NULL;
1112 vout->p->displayed.date = VLC_TS_INVALID;
1113 vout->p->displayed.decoded = NULL;
1114 vout->p->displayed.timestamp = VLC_TS_INVALID;
1115 vout->p->displayed.qtype = QTYPE_NONE;
1116 vout->p->displayed.is_interlaced = false;
1118 vout->p->step.last = VLC_TS_INVALID;
1119 vout->p->step.timestamp = VLC_TS_INVALID;
1121 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1125 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1127 /* Destroy translation tables */
1128 if (vout->p->display.vd) {
1129 if (vout->p->decoder_pool) {
1130 ThreadFlush(vout, true, INT64_MAX);
1131 vout_EndWrapper(vout);
1133 vout_CloseWrapper(vout, state);
1136 /* Destroy the video filters2 */
1137 filter_chain_Delete(vout->p->filter.chain_interactive);
1138 filter_chain_Delete(vout->p->filter.chain_static);
1140 if (vout->p->decoder_fifo)
1141 picture_fifo_Delete(vout->p->decoder_fifo);
1142 assert(!vout->p->decoder_pool);
1145 static void ThreadInit(vout_thread_t *vout)
1147 vout->p->window.is_unused = true;
1148 vout->p->window.object = NULL;
1149 vout->p->dead = false;
1150 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
1151 vout->p->pause.is_on = false;
1152 vout->p->pause.date = VLC_TS_INVALID;
1154 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1157 static void ThreadClean(vout_thread_t *vout)
1159 if (vout->p->window.object) {
1160 assert(vout->p->window.is_unused);
1161 vout_window_Delete(vout->p->window.object);
1163 vout_chrono_Clean(&vout->p->render);
1164 vout->p->dead = true;
1165 vout_control_Dead(&vout->p->control);
1168 static int ThreadReinit(vout_thread_t *vout,
1169 const vout_configuration_t *cfg)
1171 video_format_t original;
1172 if (VoutValidateFormat(&original, cfg->fmt)) {
1173 ThreadStop(vout, NULL);
1175 return VLC_EGENERIC;
1177 if (video_format_IsSimilar(&original, &vout->p->original)) {
1178 if (cfg->dpb_size <= vout->p->dpb_size)
1180 msg_Warn(vout, "DPB need to be increased");
1183 vout_display_state_t state;
1184 memset(&state, 0, sizeof(state));
1186 ThreadStop(vout, &state);
1188 if (!state.cfg.is_fullscreen) {
1189 state.cfg.display.width = 0;
1190 state.cfg.display.height = 0;
1194 /* FIXME current vout "variables" are not in sync here anymore
1195 * and I am not sure what to do */
1197 vout->p->original = original;
1198 vout->p->dpb_size = cfg->dpb_size;
1199 if (ThreadStart(vout, &state)) {
1201 return VLC_EGENERIC;
1206 /*****************************************************************************
1207 * Thread: video output thread
1208 *****************************************************************************
1209 * Video output thread. This function does only returns when the thread is
1210 * terminated. It handles the pictures arriving in the video heap and the
1211 * display device events.
1212 *****************************************************************************/
1213 static void *Thread(void *object)
1215 vout_thread_t *vout = object;
1217 vout_interlacing_support_t interlacing = {
1218 .is_interlaced = false,
1221 vout_postprocessing_support_t postprocessing = {
1222 .qtype = QTYPE_NONE,
1225 mtime_t deadline = VLC_TS_INVALID;
1227 vout_control_cmd_t cmd;
1229 /* FIXME remove thoses ugly timeouts
1231 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1233 case VOUT_CONTROL_INIT:
1235 if (ThreadStart(vout, NULL)) {
1236 ThreadStop(vout, NULL);
1241 case VOUT_CONTROL_CLEAN:
1242 ThreadStop(vout, NULL);
1245 case VOUT_CONTROL_REINIT:
1246 if (ThreadReinit(vout, cmd.u.cfg))
1249 case VOUT_CONTROL_SUBPICTURE:
1250 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1251 cmd.u.subpicture = NULL;
1253 case VOUT_CONTROL_FLUSH_SUBPICTURE:
1254 ThreadFlushSubpicture(vout, cmd.u.integer);
1256 case VOUT_CONTROL_OSD_TITLE:
1257 ThreadDisplayOsdTitle(vout, cmd.u.string);
1259 case VOUT_CONTROL_CHANGE_FILTERS:
1260 ThreadChangeFilters(vout, cmd.u.string);
1262 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1263 ThreadChangeSubFilters(vout, cmd.u.string);
1265 case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1266 ThreadChangeSubMargin(vout, cmd.u.integer);
1268 case VOUT_CONTROL_PAUSE:
1269 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1271 case VOUT_CONTROL_FLUSH:
1272 ThreadFlush(vout, false, cmd.u.time);
1274 case VOUT_CONTROL_RESET:
1277 case VOUT_CONTROL_STEP:
1278 ThreadStep(vout, cmd.u.time_ptr);
1280 case VOUT_CONTROL_FULLSCREEN:
1281 ThreadChangeFullscreen(vout, cmd.u.boolean);
1283 case VOUT_CONTROL_ON_TOP:
1284 ThreadChangeOnTop(vout, cmd.u.boolean);
1286 case VOUT_CONTROL_DISPLAY_FILLED:
1287 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1289 case VOUT_CONTROL_ZOOM:
1290 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1292 case VOUT_CONTROL_ASPECT_RATIO:
1293 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1295 case VOUT_CONTROL_CROP_RATIO:
1296 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1298 case VOUT_CONTROL_CROP_WINDOW:
1299 ThreadExecuteCropWindow(vout, 0, 0,
1300 cmd.u.window.x, cmd.u.window.y,
1301 cmd.u.window.width, cmd.u.window.height);
1303 case VOUT_CONTROL_CROP_BORDER:
1304 ThreadExecuteCropBorder(vout,
1305 cmd.u.border.left, cmd.u.border.top,
1306 cmd.u.border.right, cmd.u.border.bottom);
1311 vout_control_cmd_Clean(&cmd);
1314 ThreadManage(vout, &deadline, &interlacing, &postprocessing);