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);
115 vout_control_cmd_t cmd;
116 vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
117 cmd.u.reinit.fmt = fmt;
119 vout_control_Push(&vout->p->control, &cmd);
120 vout_control_WaitEmpty(&vout->p->control);
121 if (!vout->p->dead) {
122 vlc_object_attach(vout, object);
123 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), true);
125 msg_Dbg(object, "reusing provided vout");
128 vout_CloseAndRelease(vout);
130 msg_Warn(object, "cannot reuse provided vout");
132 return vout_Create(object, fmt);
135 /*****************************************************************************
136 * vout_Create: creates a new video output thread
137 *****************************************************************************
138 * This function creates a new video output thread, and returns a pointer
139 * to its description. On error, it returns NULL.
140 *****************************************************************************/
141 vout_thread_t *(vout_Create)(vlc_object_t *object, const video_format_t *fmt)
143 video_format_t original;
144 if (VoutValidateFormat(&original, fmt))
147 /* Allocate descriptor */
148 vout_thread_t *vout = vlc_custom_create(object,
149 sizeof(*vout) + sizeof(*vout->p),
150 VLC_OBJECT_VOUT, "video output");
152 video_format_Clean(&original);
157 vout->p = (vout_thread_sys_t*)&vout[1];
159 vout->p->original = original;
161 vout_control_Init(&vout->p->control);
162 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
164 vout_statistic_Init(&vout->p->statistic);
166 vout->p->i_par_den = 1;
168 vout_snapshot_Init(&vout->p->snapshot);
170 /* Initialize locks */
171 vlc_mutex_init(&vout->p->picture_lock);
172 vlc_mutex_init(&vout->p->vfilter_lock);
174 /* Attach the new object now so we can use var inheritance below */
175 vlc_object_attach(vout, object);
177 /* Initialize subpicture unit */
178 vout->p->p_spu = spu_Create(vout);
181 spu_Init(vout->p->p_spu);
183 /* Take care of some "interface/control" related initialisations */
186 /* Get splitter name if present */
187 char *splitter_name = var_GetNonEmptyString(vout, "vout-filter");
189 if (asprintf(&vout->p->splitter_name, "%s,none", splitter_name) < 0)
190 vout->p->splitter_name = NULL;
193 vout->p->splitter_name = NULL;
197 vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
200 vlc_object_set_destructor(vout, VoutDestructor);
203 if (vlc_clone(&vout->p->thread, Thread, vout,
204 VLC_THREAD_PRIORITY_OUTPUT)) {
205 vlc_object_release(vout);
208 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), true);
210 vout_control_WaitEmpty(&vout->p->control);
213 msg_Err(vout, "video output creation failed");
214 vout_CloseAndRelease(vout);
221 /*****************************************************************************
222 * vout_Close: Close a vout created by vout_Create.
223 *****************************************************************************
224 * You HAVE to call it on vout created by vout_Create before vlc_object_release.
225 * You should NEVER call it on vout not obtained through vout_Create
226 * (like with vout_Request or vlc_object_find.)
227 * You can use vout_CloseAndRelease() as a convenience method.
228 *****************************************************************************/
229 void vout_Close(vout_thread_t *vout)
233 spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
235 vout_snapshot_End(&vout->p->snapshot);
237 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
238 vlc_join(vout->p->thread, NULL);
242 static void VoutDestructor(vlc_object_t *object)
244 vout_thread_t *vout = (vout_thread_t *)object;
246 /* Make sure the vout was stopped first */
247 //assert(!vout->p_module);
249 free(vout->p->splitter_name);
252 spu_Destroy(vout->p->p_spu);
254 /* Destroy the locks */
255 vlc_mutex_destroy(&vout->p->picture_lock);
256 vlc_mutex_destroy(&vout->p->vfilter_lock);
257 vout_control_Clean(&vout->p->control);
260 vout_statistic_Clean(&vout->p->statistic);
263 vout_snapshot_Clean(&vout->p->snapshot);
265 video_format_Clean(&vout->p->original);
269 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
271 vout_control_cmd_t cmd;
272 vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
273 cmd.u.pause.is_on = is_paused;
274 cmd.u.pause.date = date;
275 vout_control_Push(&vout->p->control, &cmd);
277 vout_control_WaitEmpty(&vout->p->control);
280 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
282 vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
285 void vout_Flush(vout_thread_t *vout, mtime_t date)
287 vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
288 vout_control_WaitEmpty(&vout->p->control);
291 void vout_Reset(vout_thread_t *vout)
293 vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
294 vout_control_WaitEmpty(&vout->p->control);
297 bool vout_IsEmpty(vout_thread_t *vout)
299 vlc_mutex_lock(&vout->p->picture_lock);
301 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
303 picture_Release(picture);
305 vlc_mutex_unlock(&vout->p->picture_lock);
310 void vout_FixLeaks( vout_thread_t *vout )
312 vlc_mutex_lock(&vout->p->picture_lock);
314 picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
316 picture = picture_pool_Get(vout->p->decoder_pool);
320 picture_Release(picture);
321 /* Not all pictures has been displayed yet or some are
323 vlc_mutex_unlock(&vout->p->picture_lock);
327 /* There is no reason that no pictures are available, force one
328 * from the pool, becarefull with it though */
329 msg_Err(vout, "pictures leaked, trying to workaround");
332 picture_pool_NonEmpty(vout->p->decoder_pool, false);
334 vlc_mutex_unlock(&vout->p->picture_lock);
336 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
338 vout_control_cmd_t cmd;
339 vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
340 cmd.u.time_ptr = duration;
342 vout_control_Push(&vout->p->control, &cmd);
343 vout_control_WaitEmpty(&vout->p->control);
346 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
349 vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
352 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
354 spu_DisplaySubpicture(vout->p->p_spu, subpic);
356 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
358 return spu_RegisterChannel(vout->p->p_spu);
360 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
362 spu_ClearChannel(vout->p->p_spu, channel);
365 spu_t *vout_GetSpu(vout_thread_t *vout)
367 return vout->p->p_spu;
370 /* vout_Control* are usable by anyone at anytime */
371 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
373 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
376 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
378 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
381 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
383 vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
386 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
388 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
391 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
392 unsigned num, unsigned den)
394 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
397 void vout_ControlChangeCropRatio(vout_thread_t *vout,
398 unsigned num, unsigned den)
400 vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
403 void vout_ControlChangeCropWindow(vout_thread_t *vout,
404 int x, int y, int width, int height)
406 vout_control_cmd_t cmd;
407 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
410 cmd.u.window.width = width;
411 cmd.u.window.height = height;
413 vout_control_Push(&vout->p->control, &cmd);
415 void vout_ControlChangeCropBorder(vout_thread_t *vout,
416 int left, int top, int right, int bottom)
418 vout_control_cmd_t cmd;
419 vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
420 cmd.u.border.left = left;
421 cmd.u.border.top = top;
422 cmd.u.border.right = right;
423 cmd.u.border.bottom = bottom;
425 vout_control_Push(&vout->p->control, &cmd);
427 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
429 vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
434 static picture_t *VoutVideoFilterNewPicture(filter_t *filter)
436 vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
437 return picture_pool_Get(vout->p->private_pool);
439 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
442 picture_Release(picture);
444 static int VoutVideoFilterAllocationSetup(filter_t *filter, void *data)
446 filter->pf_video_buffer_new = VoutVideoFilterNewPicture;
447 filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
448 filter->p_owner = data; /* vout */
453 static int ThreadDisplayPicture(vout_thread_t *vout,
454 bool now, mtime_t *deadline)
456 vout_display_t *vd = vout->p->display.vd;
457 int displayed_count = 0;
461 const mtime_t date = mdate();
462 const bool is_paused = vout->p->pause.is_on;
463 bool redisplay = is_paused && !now && vout->p->displayed.decoded;
466 /* FIXME/XXX we must redisplay the last decoded picture (because
467 * of potential vout updated, or filters update or SPU update)
468 * For now a high update period is needed but it coulmd be removed
470 * - vout module emits events from theselves.
471 * - *and* SPU is modified to emit an event or a deadline when needed.
473 * So it will be done latter.
476 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
478 is_forced = peek->b_force || is_paused || now;
479 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
480 picture_Release(peek);
486 /* FIXME a better way for this delay is needed */
487 const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
488 if (date_update > date || !vout->p->displayed.decoded) {
489 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
494 *deadline = date - vout_chrono_GetHigh(&vout->p->render);
496 if (*deadline > VOUT_MWAIT_TOLERANCE)
497 *deadline -= VOUT_MWAIT_TOLERANCE;
499 /* If we are too early and can wait, do it */
500 if (date < *deadline && !now)
505 decoded = vout->p->displayed.decoded;
506 vout->p->displayed.decoded = NULL;
508 decoded = picture_fifo_Pop(vout->p->decoder_fifo);
510 if (!is_forced && !vout->p->is_late_dropped) {
511 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
512 const mtime_t late = predicted - decoded->date;
514 msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
515 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
516 msg_Warn(vout, "rejected picture because of render time");
518 picture_Release(decoded);
525 vout->p->displayed.is_interlaced = !decoded->b_progressive;
526 vout->p->displayed.qtype = decoded->i_qtype;
528 vout->p->displayed.timestamp = decoded->date;
531 if (vout->p->displayed.decoded)
532 picture_Release(vout->p->displayed.decoded);
533 picture_Hold(decoded);
534 vout->p->displayed.decoded = decoded;
537 vout_chrono_Start(&vout->p->render);
539 picture_t *filtered = NULL;
541 vlc_mutex_lock(&vout->p->vfilter_lock);
542 filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
543 //assert(filtered == decoded); // TODO implement
544 vlc_mutex_unlock(&vout->p->vfilter_lock);
550 * Check for subpictures to display
552 const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
553 mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
554 if (vout->p->pause.is_on)
555 spu_render_time = vout->p->pause.date;
557 spu_render_time = filtered->date > 1 ? filtered->date : mdate();
559 subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
566 * - be sure to end up with a direct buffer.
567 * - blend subtitles, and in a fast access buffer
569 picture_t *direct = NULL;
571 (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
573 if (vout->p->is_decoder_pool_slow)
574 render = picture_NewFromFormat(&vd->source);
575 else if (vout->p->decoder_pool != vout->p->display_pool)
576 render = picture_pool_Get(vout->p->display_pool);
578 render = picture_pool_Get(vout->p->private_pool);
581 picture_Copy(render, filtered);
583 spu_RenderSubpictures(vout->p->p_spu,
585 subpic, &vd->source, spu_render_time);
587 if (vout->p->is_decoder_pool_slow) {
588 direct = picture_pool_Get(vout->p->display_pool);
590 picture_Copy(direct, render);
591 picture_Release(render);
596 picture_Release(filtered);
603 * Take a snapshot if requested
605 if (direct && do_snapshot)
606 vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
608 /* Render the direct buffer returned by vout_RenderPicture */
610 vout_RenderWrapper(vout, direct);
612 vout_chrono_Stop(&vout->p->render);
617 msg_Info(vout, "render: avg %d ms var %d ms",
618 (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
623 /* Wait the real date (for rendering jitter) */
625 mwait(decoded->date);
627 /* Display the direct buffer returned by vout_RenderPicture */
628 vout->p->displayed.date = mdate();
630 vout_DisplayWrapper(vout, direct);
636 vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
637 if (displayed_count <= 0)
642 static void ThreadManage(vout_thread_t *vout,
644 vout_interlacing_support_t *interlacing,
645 vout_postprocessing_support_t *postprocessing)
647 vlc_mutex_lock(&vout->p->picture_lock);
649 *deadline = VLC_TS_INVALID;
650 ThreadDisplayPicture(vout, false, deadline);
652 const int picture_qtype = vout->p->displayed.qtype;
653 const bool picture_interlaced = vout->p->displayed.is_interlaced;
655 vlc_mutex_unlock(&vout->p->picture_lock);
657 /* Post processing */
658 vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
661 vout_SetInterlacingState(vout, interlacing, picture_interlaced);
663 vout_ManageWrapper(vout);
666 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
668 if (!vout->p->title.show)
671 vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
672 vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
676 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
679 es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
680 fmt.video = vout->p->original;
682 vlc_mutex_lock(&vout->p->vfilter_lock);
684 filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
685 if (filter_chain_AppendFromString(vout->p->vfilter_chain,
687 msg_Err(vout, "Video filter chain creation failed");
689 vlc_mutex_unlock(&vout->p->vfilter_lock);
692 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
694 assert(!vout->p->pause.is_on || !is_paused);
696 if (vout->p->pause.is_on) {
697 const mtime_t duration = date - vout->p->pause.date;
699 if (vout->p->step.timestamp > VLC_TS_INVALID)
700 vout->p->step.timestamp += duration;
701 if (vout->p->step.last > VLC_TS_INVALID)
702 vout->p->step.last += duration;
703 picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
704 if (vout->p->displayed.decoded)
705 vout->p->displayed.decoded->date += duration;
707 spu_OffsetSubtitleDate(vout->p->p_spu, duration);
709 vout->p->step.timestamp = VLC_TS_INVALID;
710 vout->p->step.last = VLC_TS_INVALID;
712 vout->p->pause.is_on = is_paused;
713 vout->p->pause.date = date;
716 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
718 vout->p->step.timestamp = VLC_TS_INVALID;
719 vout->p->step.last = VLC_TS_INVALID;
721 picture_t *last = vout->p->displayed.decoded;
723 if (( below && last->date <= date) ||
724 (!below && last->date >= date)) {
725 picture_Release(last);
727 vout->p->displayed.decoded = NULL;
728 vout->p->displayed.date = VLC_TS_INVALID;
729 vout->p->displayed.timestamp = VLC_TS_INVALID;
732 picture_fifo_Flush(vout->p->decoder_fifo, date, below);
735 static void ThreadReset(vout_thread_t *vout)
737 ThreadFlush(vout, true, INT64_MAX);
738 if (vout->p->decoder_pool)
739 picture_pool_NonEmpty(vout->p->decoder_pool, true);
740 vout->p->pause.is_on = false;
741 vout->p->pause.date = mdate();
744 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
748 if (vout->p->step.last <= VLC_TS_INVALID)
749 vout->p->step.last = vout->p->displayed.timestamp;
752 if (ThreadDisplayPicture(vout, true, &dummy))
755 vout->p->step.timestamp = vout->p->displayed.timestamp;
757 if (vout->p->step.last > VLC_TS_INVALID &&
758 vout->p->step.timestamp > vout->p->step.last) {
759 *duration = vout->p->step.timestamp - vout->p->step.last;
760 vout->p->step.last = vout->p->step.timestamp;
761 /* TODO advance subpicture by the duration ... */
765 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
767 /* FIXME not sure setting "fullscreen" is good ... */
768 var_SetBool(vout, "fullscreen", fullscreen);
769 vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
772 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
774 vout_SetWindowState(vout->p->display.vd,
775 is_on_top ? VOUT_WINDOW_STATE_ABOVE :
776 VOUT_WINDOW_STATE_NORMAL);
779 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
781 vout_SetDisplayFilled(vout->p->display.vd, is_filled);
784 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
786 if (num * 10 < den) {
789 } else if (num > den * 10) {
793 vout_SetDisplayZoom(vout->p->display.vd, num, den);
796 static void ThreadChangeAspectRatio(vout_thread_t *vout,
797 unsigned num, unsigned den)
799 const video_format_t *source = &vout->p->original;
801 if (num > 0 && den > 0) {
802 num *= source->i_visible_height;
803 den *= source->i_visible_width;
804 vlc_ureduce(&num, &den, num, den, 0);
806 vout_SetDisplayAspect(vout->p->display.vd, num, den);
810 static void ThreadExecuteCropWindow(vout_thread_t *vout,
811 unsigned crop_num, unsigned crop_den,
812 unsigned x, unsigned y,
813 unsigned width, unsigned height)
815 const video_format_t *source = &vout->p->original;
817 vout_SetDisplayCrop(vout->p->display.vd,
819 source->i_x_offset + x,
820 source->i_y_offset + y,
823 static void ThreadExecuteCropBorder(vout_thread_t *vout,
824 unsigned left, unsigned top,
825 unsigned right, unsigned bottom)
827 const video_format_t *source = &vout->p->original;
828 ThreadExecuteCropWindow(vout, 0, 0,
831 /* At worst, it becomes < 0 (but unsigned) and will be rejected */
832 source->i_visible_width - (left + right),
833 source->i_visible_height - (top + bottom));
836 static void ThreadExecuteCropRatio(vout_thread_t *vout,
837 unsigned num, unsigned den)
839 const video_format_t *source = &vout->p->original;
843 if (num <= 0 || den <= 0) {
848 width = source->i_visible_width;
849 height = source->i_visible_height;
851 unsigned scaled_width = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
852 unsigned scaled_height = (uint64_t)source->i_visible_width * den * source->i_sar_num / num / source->i_sar_den;
854 if (scaled_width < source->i_visible_width) {
855 x = (source->i_visible_width - scaled_width) / 2;
857 width = scaled_width;
858 height = source->i_visible_height;
861 y = (source->i_visible_height - scaled_height) / 2;
862 width = source->i_visible_width;
863 height = scaled_height;
866 ThreadExecuteCropWindow(vout, num, den, x, y, width, height);
869 static int ThreadInit(vout_thread_t *vout)
871 vout->p->dead = false;
872 vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
873 vlc_mouse_Init(&vout->p->mouse);
874 vout->p->decoder_fifo = picture_fifo_New();
875 vout->p->decoder_pool = NULL;
876 vout->p->display_pool = NULL;
877 vout->p->private_pool = NULL;
879 vout->p->vfilter_chain =
880 filter_chain_New( vout, "video filter2", false,
881 VoutVideoFilterAllocationSetup, NULL, vout);
883 if (vout_OpenWrapper(vout, vout->p->splitter_name))
885 if (vout_InitWrapper(vout))
887 assert(vout->p->decoder_pool);
889 vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
891 vout->p->displayed.decoded = NULL;
892 vout->p->displayed.date = VLC_TS_INVALID;
893 vout->p->displayed.decoded = NULL;
894 vout->p->displayed.timestamp = VLC_TS_INVALID;
895 vout->p->displayed.qtype = QTYPE_NONE;
896 vout->p->displayed.is_interlaced = false;
898 vout->p->step.last = VLC_TS_INVALID;
899 vout->p->step.timestamp = VLC_TS_INVALID;
901 vout->p->pause.is_on = false;
902 vout->p->pause.date = VLC_TS_INVALID;
904 video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
908 static void ThreadClean(vout_thread_t *vout)
910 /* Destroy the video filters2 */
911 filter_chain_Delete(vout->p->vfilter_chain);
913 /* Destroy translation tables */
914 if (vout->p->display.vd) {
915 if (vout->p->decoder_pool) {
916 ThreadFlush(vout, true, INT64_MAX);
917 vout_EndWrapper(vout);
919 vout_CloseWrapper(vout);
922 /* Detach subpicture unit from vout */
923 vlc_object_detach(vout->p->p_spu);
925 if (vout->p->decoder_fifo)
926 picture_fifo_Delete(vout->p->decoder_fifo);
927 assert(!vout->p->decoder_pool);
928 vout_chrono_Clean(&vout->p->render);
930 vout->p->dead = true;
931 vout_control_Dead(&vout->p->control);
933 static int ThreadReinit(vout_thread_t *vout,
934 const video_format_t *fmt)
936 video_format_t original;
937 if (VoutValidateFormat(&original, fmt))
939 if (video_format_IsSimilar(&original, &vout->p->original))
946 /*****************************************************************************
947 * Thread: video output thread
948 *****************************************************************************
949 * Video output thread. This function does only returns when the thread is
950 * terminated. It handles the pictures arriving in the video heap and the
951 * display device events.
952 *****************************************************************************/
953 static void *Thread(void *object)
955 vout_thread_t *vout = object;
957 vout_interlacing_support_t interlacing = {
958 .is_interlaced = false,
961 vout_postprocessing_support_t postprocessing = {
965 mtime_t deadline = VLC_TS_INVALID;
967 vout_control_cmd_t cmd;
969 /* FIXME remove thoses ugly timeouts
971 while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
973 case VOUT_CONTROL_INIT:
974 if (ThreadInit(vout)) {
979 case VOUT_CONTROL_CLEAN:
982 case VOUT_CONTROL_REINIT:
983 if (ThreadReinit(vout, cmd.u.reinit.fmt)) {
988 case VOUT_CONTROL_OSD_TITLE:
989 ThreadDisplayOsdTitle(vout, cmd.u.string);
991 case VOUT_CONTROL_CHANGE_FILTERS:
992 ThreadChangeFilters(vout, cmd.u.string);
994 case VOUT_CONTROL_PAUSE:
995 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
997 case VOUT_CONTROL_FLUSH:
998 ThreadFlush(vout, false, cmd.u.time);
1000 case VOUT_CONTROL_RESET:
1003 case VOUT_CONTROL_STEP:
1004 ThreadStep(vout, cmd.u.time_ptr);
1006 case VOUT_CONTROL_FULLSCREEN:
1007 ThreadChangeFullscreen(vout, cmd.u.boolean);
1009 case VOUT_CONTROL_ON_TOP:
1010 ThreadChangeOnTop(vout, cmd.u.boolean);
1012 case VOUT_CONTROL_DISPLAY_FILLED:
1013 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1015 case VOUT_CONTROL_ZOOM:
1016 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1018 case VOUT_CONTROL_ASPECT_RATIO:
1019 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1021 case VOUT_CONTROL_CROP_RATIO:
1022 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1024 case VOUT_CONTROL_CROP_WINDOW:
1025 ThreadExecuteCropWindow(vout, 0, 0,
1026 cmd.u.window.x, cmd.u.window.y,
1027 cmd.u.window.width, cmd.u.window.height);
1029 case VOUT_CONTROL_CROP_BORDER:
1030 ThreadExecuteCropBorder(vout,
1031 cmd.u.border.left, cmd.u.border.top,
1032 cmd.u.border.right, cmd.u.border.bottom);
1037 vout_control_cmd_Clean(&cmd);
1040 ThreadManage(vout, &deadline, &interlacing, &postprocessing);