1 /*****************************************************************************
2  * video_output.c : video output thread
3  *
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 VLC authors and VideoLAN
9  * $Id: d552160befaaf888f6cb1af70a39bb34e87bacb8 $
10  *
11  * Authors: Vincent Seguin <seguin@via.ecp.fr>
12  *          Gildas Bazin <gbazin@videolan.org>
13  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
14  *
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU Lesser General Public License as published by
17  * the Free Software Foundation; either version 2.1 of the License, or
18  * (at your option) any later version.
19  *
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 Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with this program; if not, write to the Free Software Foundation,
27  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29 
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36 
37 #include <vlc_common.h>
38 
39 #include <stdlib.h>                                                /* free() */
40 #include <string.h>
41 #include <assert.h>
42 
43 #include <vlc_vout.h>
44 
45 #include <vlc_filter.h>
46 #include <vlc_spu.h>
47 #include <vlc_vout_osd.h>
48 #include <vlc_image.h>
49 #include <vlc_plugin.h>
50 
51 #include <libvlc.h>
52 #include "vout_internal.h"
53 #include "interlacing.h"
54 #include "display.h"
55 #include "window.h"
56 #include "../misc/variables.h"
57 
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static void *Thread(void *);
62 static void VoutDestructor(vlc_object_t *);
63 
64 /* Maximum delay between 2 displayed pictures.
65  * XXX it is needed for now but should be removed in the long term.
66  */
67 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
68 
69 /**
70  * Late pictures having a delay higher than this value are thrashed.
71  */
72 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
73 
74 /* Better be in advance when awakening than late... */
75 #define VOUT_MWAIT_TOLERANCE (INT64_C(4000))
76 
77 /* */
VoutValidateFormat(video_format_t * dst,const video_format_t * src)78 static int VoutValidateFormat(video_format_t *dst,
79                               const video_format_t *src)
80 {
81     if (src->i_width == 0  || src->i_width  > 8192 ||
82         src->i_height == 0 || src->i_height > 8192)
83         return VLC_EGENERIC;
84     if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
85         return VLC_EGENERIC;
86 
87     /* */
88     video_format_Copy(dst, src);
89     dst->i_chroma = vlc_fourcc_GetCodec(VIDEO_ES, src->i_chroma);
90     vlc_ureduce( &dst->i_sar_num, &dst->i_sar_den,
91                  src->i_sar_num,  src->i_sar_den, 50000 );
92     if (dst->i_sar_num <= 0 || dst->i_sar_den <= 0) {
93         dst->i_sar_num = 1;
94         dst->i_sar_den = 1;
95     }
96     video_format_FixRgb(dst);
97     return VLC_SUCCESS;
98 }
VideoFormatCopyCropAr(video_format_t * dst,const video_format_t * src)99 static void VideoFormatCopyCropAr(video_format_t *dst,
100                                   const video_format_t *src)
101 {
102     video_format_CopyCrop(dst, src);
103     dst->i_sar_num = src->i_sar_num;
104     dst->i_sar_den = src->i_sar_den;
105 }
VideoFormatIsCropArEqual(video_format_t * dst,const video_format_t * src)106 static bool VideoFormatIsCropArEqual(video_format_t *dst,
107                                      const video_format_t *src)
108 {
109     return dst->i_sar_num * src->i_sar_den == dst->i_sar_den * src->i_sar_num &&
110            dst->i_x_offset       == src->i_x_offset &&
111            dst->i_y_offset       == src->i_y_offset &&
112            dst->i_visible_width  == src->i_visible_width &&
113            dst->i_visible_height == src->i_visible_height;
114 }
115 
VoutCreate(vlc_object_t * object,const vout_configuration_t * cfg)116 static vout_thread_t *VoutCreate(vlc_object_t *object,
117                                  const vout_configuration_t *cfg)
118 {
119     video_format_t original;
120     if (VoutValidateFormat(&original, cfg->fmt))
121         return NULL;
122 
123     /* Allocate descriptor */
124     vout_thread_t *vout = vlc_custom_create(object,
125                                             sizeof(*vout) + sizeof(*vout->p),
126                                             "video output");
127     if (!vout) {
128         video_format_Clean(&original);
129         return NULL;
130     }
131 
132     /* */
133     vout->p = (vout_thread_sys_t*)&vout[1];
134 
135     vout->p->original = original;
136     vout->p->dpb_size = cfg->dpb_size;
137 
138     vout_control_Init(&vout->p->control);
139     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
140 
141     vout_statistic_Init(&vout->p->statistic);
142 
143     vout_snapshot_Init(&vout->p->snapshot);
144 
145     /* Initialize locks */
146     vlc_mutex_init(&vout->p->filter.lock);
147     vlc_mutex_init(&vout->p->spu_lock);
148 
149     /* Take care of some "interface/control" related initialisations */
150     vout_IntfInit(vout);
151 
152     /* Initialize subpicture unit */
153     vout->p->spu = spu_Create(vout, vout);
154 
155     vout->p->title.show     = var_InheritBool(vout, "video-title-show");
156     vout->p->title.timeout  = var_InheritInteger(vout, "video-title-timeout");
157     vout->p->title.position = var_InheritInteger(vout, "video-title-position");
158 
159     /* Get splitter name if present */
160     vout->p->splitter_name = var_InheritString(vout, "video-splitter");
161 
162     /* */
163     vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
164 
165     /* Window */
166     if (vout->p->splitter_name == NULL) {
167         vout_window_cfg_t wcfg = {
168             .is_standalone = !var_InheritBool(vout, "embedded-video"),
169             .is_fullscreen = var_GetBool(vout, "fullscreen"),
170             .type = VOUT_WINDOW_TYPE_INVALID,
171             // TODO: take pixel A/R, crop and zoom into account
172 #ifdef __APPLE__
173             .x = var_InheritInteger(vout, "video-x"),
174             .y = var_InheritInteger(vout, "video-y"),
175 #endif
176             .width = cfg->fmt->i_visible_width,
177             .height = cfg->fmt->i_visible_height,
178         };
179 
180         vout_window_t *window = vout_display_window_New(vout, &wcfg);
181         if (window != NULL)
182         {
183             if (var_InheritBool(vout, "video-wallpaper"))
184                 vout_window_SetState(window, VOUT_WINDOW_STATE_BELOW);
185             else if (var_InheritBool(vout, "video-on-top"))
186                 vout_window_SetState(window, VOUT_WINDOW_STATE_ABOVE);
187         }
188         vout->p->window = window;
189     } else
190         vout->p->window = NULL;
191 
192     /* */
193     vlc_object_set_destructor(vout, VoutDestructor);
194 
195     /* */
196     if (vlc_clone(&vout->p->thread, Thread, vout,
197                   VLC_THREAD_PRIORITY_OUTPUT)) {
198         if (vout->p->window != NULL)
199             vout_display_window_Delete(vout->p->window);
200         spu_Destroy(vout->p->spu);
201         vlc_object_release(vout);
202         return NULL;
203     }
204 
205     vout_control_WaitEmpty(&vout->p->control);
206 
207     if (vout->p->dead) {
208         msg_Err(vout, "video output creation failed");
209         vout_CloseAndRelease(vout);
210         return NULL;
211     }
212 
213     vout->p->input = cfg->input;
214     if (vout->p->input)
215         spu_Attach(vout->p->spu, vout->p->input, true);
216 
217     return vout;
218 }
219 
220 #undef vout_Request
vout_Request(vlc_object_t * object,const vout_configuration_t * cfg)221 vout_thread_t *vout_Request(vlc_object_t *object,
222                               const vout_configuration_t *cfg)
223 {
224     vout_thread_t *vout = cfg->vout;
225     if (cfg->change_fmt && !cfg->fmt) {
226         if (vout)
227             vout_CloseAndRelease(vout);
228         return NULL;
229     }
230 
231     /* If a vout is provided, try reusing it */
232     if (vout) {
233         if (vout->p->input != cfg->input) {
234             if (vout->p->input)
235                 spu_Attach(vout->p->spu, vout->p->input, false);
236             vout->p->input = cfg->input;
237             if (vout->p->input)
238                 spu_Attach(vout->p->spu, vout->p->input, true);
239         }
240 
241         if (cfg->change_fmt) {
242             vout_control_cmd_t cmd;
243             vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
244             cmd.u.cfg = cfg;
245 
246             vout_control_Push(&vout->p->control, &cmd);
247             vout_control_WaitEmpty(&vout->p->control);
248             vout_IntfReinit(vout);
249         }
250 
251         if (!vout->p->dead) {
252             msg_Dbg(object, "reusing provided vout");
253             return vout;
254         }
255         vout_CloseAndRelease(vout);
256 
257         msg_Warn(object, "cannot reuse provided vout");
258     }
259     return VoutCreate(object, cfg);
260 }
261 
vout_Close(vout_thread_t * vout)262 void vout_Close(vout_thread_t *vout)
263 {
264     assert(vout);
265 
266     if (vout->p->input)
267         spu_Attach(vout->p->spu, vout->p->input, false);
268 
269     vout_snapshot_End(&vout->p->snapshot);
270 
271     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
272     vlc_join(vout->p->thread, NULL);
273 
274     if (vout->p->window != NULL)
275         vout_display_window_Delete(vout->p->window);
276 
277     vlc_mutex_lock(&vout->p->spu_lock);
278     spu_Destroy(vout->p->spu);
279     vout->p->spu = NULL;
280     vlc_mutex_unlock(&vout->p->spu_lock);
281 }
282 
283 /* */
VoutDestructor(vlc_object_t * object)284 static void VoutDestructor(vlc_object_t *object)
285 {
286     vout_thread_t *vout = (vout_thread_t *)object;
287 
288     /* Make sure the vout was stopped first */
289     //assert(!vout->p_module);
290 
291     free(vout->p->splitter_name);
292 
293     /* Destroy the locks */
294     vlc_mutex_destroy(&vout->p->spu_lock);
295     vlc_mutex_destroy(&vout->p->filter.lock);
296     vout_control_Clean(&vout->p->control);
297 
298     /* */
299     vout_statistic_Clean(&vout->p->statistic);
300 
301     /* */
302     vout_snapshot_Clean(&vout->p->snapshot);
303 
304     video_format_Clean(&vout->p->original);
305 }
306 
307 /* */
vout_Cancel(vout_thread_t * vout,bool canceled)308 void vout_Cancel(vout_thread_t *vout, bool canceled)
309 {
310     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_CANCEL, canceled);
311     vout_control_WaitEmpty(&vout->p->control);
312 }
313 
vout_ChangePause(vout_thread_t * vout,bool is_paused,mtime_t date)314 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
315 {
316     vout_control_cmd_t cmd;
317     vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
318     cmd.u.pause.is_on = is_paused;
319     cmd.u.pause.date  = date;
320     vout_control_Push(&vout->p->control, &cmd);
321 
322     vout_control_WaitEmpty(&vout->p->control);
323 }
324 
vout_GetResetStatistic(vout_thread_t * vout,unsigned * restrict displayed,unsigned * restrict lost)325 void vout_GetResetStatistic(vout_thread_t *vout, unsigned *restrict displayed,
326                             unsigned *restrict lost)
327 {
328     vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
329 }
330 
vout_Flush(vout_thread_t * vout,mtime_t date)331 void vout_Flush(vout_thread_t *vout, mtime_t date)
332 {
333     vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
334     vout_control_WaitEmpty(&vout->p->control);
335 }
336 
vout_IsEmpty(vout_thread_t * vout)337 bool vout_IsEmpty(vout_thread_t *vout)
338 {
339     picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
340     if (picture)
341         picture_Release(picture);
342 
343     return !picture;
344 }
345 
vout_NextPicture(vout_thread_t * vout,mtime_t * duration)346 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
347 {
348     vout_control_cmd_t cmd;
349     vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
350     cmd.u.time_ptr = duration;
351 
352     vout_control_Push(&vout->p->control, &cmd);
353     vout_control_WaitEmpty(&vout->p->control);
354 }
355 
vout_DisplayTitle(vout_thread_t * vout,const char * title)356 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
357 {
358     assert(title);
359     vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
360 }
361 
vout_WindowMouseEvent(vout_thread_t * vout,const vout_window_mouse_event_t * mouse)362 void vout_WindowMouseEvent(vout_thread_t *vout,
363                            const vout_window_mouse_event_t *mouse)
364 {
365     assert(mouse);
366     vout_control_cmd_t cmd;
367     vout_control_cmd_Init(&cmd, VOUT_CONTROL_WINDOW_MOUSE);
368     cmd.u.window_mouse = *mouse;
369 
370     vout_control_Push(&vout->p->control, &cmd);
371 }
372 
vout_PutSubpicture(vout_thread_t * vout,subpicture_t * subpic)373 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
374 {
375     vout_control_cmd_t cmd;
376     vout_control_cmd_Init(&cmd, VOUT_CONTROL_SUBPICTURE);
377     cmd.u.subpicture = subpic;
378 
379     vout_control_Push(&vout->p->control, &cmd);
380 }
vout_RegisterSubpictureChannel(vout_thread_t * vout)381 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
382 {
383     int channel = VOUT_SPU_CHANNEL_AVAIL_FIRST;
384 
385     vlc_mutex_lock(&vout->p->spu_lock);
386     if (vout->p->spu)
387         channel = spu_RegisterChannel(vout->p->spu);
388     vlc_mutex_unlock(&vout->p->spu_lock);
389 
390     return channel;
391 }
vout_FlushSubpictureChannel(vout_thread_t * vout,int channel)392 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
393 {
394     vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_FLUSH_SUBPICTURE,
395                              channel);
396 }
397 
398 /**
399  * Allocates a video output picture buffer.
400  *
401  * Either vout_PutPicture() or picture_Release() must be used to return the
402  * buffer to the video output free buffer pool.
403  *
404  * You may use picture_Hold() (paired with picture_Release()) to keep a
405  * read-only reference.
406  */
vout_GetPicture(vout_thread_t * vout)407 picture_t *vout_GetPicture(vout_thread_t *vout)
408 {
409     picture_t *picture = picture_pool_Wait(vout->p->decoder_pool);
410     if (likely(picture != NULL)) {
411         picture_Reset(picture);
412         VideoFormatCopyCropAr(&picture->format, &vout->p->original);
413     }
414     return picture;
415 }
416 
417 /**
418  * It gives to the vout a picture to be displayed.
419  *
420  * The given picture MUST comes from vout_GetPicture.
421  *
422  * Becareful, after vout_PutPicture is called, picture_t::p_next cannot be
423  * read/used.
424  */
vout_PutPicture(vout_thread_t * vout,picture_t * picture)425 void vout_PutPicture(vout_thread_t *vout, picture_t *picture)
426 {
427     picture->p_next = NULL;
428     if (picture_pool_OwnsPic(vout->p->decoder_pool, picture))
429     {
430         picture_fifo_Push(vout->p->decoder_fifo, picture);
431 
432         vout_control_Wake(&vout->p->control);
433     }
434     else
435     {
436         /* FIXME: HACK: Drop this picture because the vout changed. The old
437          * picture pool need to be kept by the new vout. This requires a major
438          * "vout display" API change. */
439         picture_Release(picture);
440     }
441 }
442 
443 /* */
vout_GetSnapshot(vout_thread_t * vout,block_t ** image_dst,picture_t ** picture_dst,video_format_t * fmt,const char * type,mtime_t timeout)444 int vout_GetSnapshot(vout_thread_t *vout,
445                      block_t **image_dst, picture_t **picture_dst,
446                      video_format_t *fmt,
447                      const char *type, mtime_t timeout)
448 {
449     picture_t *picture = vout_snapshot_Get(&vout->p->snapshot, timeout);
450     if (!picture) {
451         msg_Err(vout, "Failed to grab a snapshot");
452         return VLC_EGENERIC;
453     }
454 
455     if (image_dst) {
456         vlc_fourcc_t codec = VLC_CODEC_PNG;
457         if (type && image_Type2Fourcc(type))
458             codec = image_Type2Fourcc(type);
459 
460         const int override_width  = var_InheritInteger(vout, "snapshot-width");
461         const int override_height = var_InheritInteger(vout, "snapshot-height");
462 
463         if (picture_Export(VLC_OBJECT(vout), image_dst, fmt,
464                            picture, codec, override_width, override_height)) {
465             msg_Err(vout, "Failed to convert image for snapshot");
466             picture_Release(picture);
467             return VLC_EGENERIC;
468         }
469     }
470     if (picture_dst)
471         *picture_dst = picture;
472     else
473         picture_Release(picture);
474     return VLC_SUCCESS;
475 }
476 
vout_ChangeAspectRatio(vout_thread_t * p_vout,unsigned int i_num,unsigned int i_den)477 void vout_ChangeAspectRatio( vout_thread_t *p_vout,
478                              unsigned int i_num, unsigned int i_den )
479 {
480     vout_ControlChangeSampleAspectRatio( p_vout, i_num, i_den );
481 }
482 
483 /* vout_Control* are usable by anyone at anytime */
vout_ControlChangeFullscreen(vout_thread_t * vout,bool fullscreen)484 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
485 {
486     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
487                           fullscreen);
488 }
vout_ControlChangeWindowState(vout_thread_t * vout,unsigned st)489 void vout_ControlChangeWindowState(vout_thread_t *vout, unsigned st)
490 {
491     vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_WINDOW_STATE, st);
492 }
vout_ControlChangeDisplayFilled(vout_thread_t * vout,bool is_filled)493 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
494 {
495     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
496                           is_filled);
497 }
vout_ControlChangeZoom(vout_thread_t * vout,int num,int den)498 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
499 {
500     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
501                           num, den);
502 }
vout_ControlChangeSampleAspectRatio(vout_thread_t * vout,unsigned num,unsigned den)503 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
504                                          unsigned num, unsigned den)
505 {
506     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
507                           num, den);
508 }
vout_ControlChangeCropRatio(vout_thread_t * vout,unsigned num,unsigned den)509 void vout_ControlChangeCropRatio(vout_thread_t *vout,
510                                  unsigned num, unsigned den)
511 {
512     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
513                           num, den);
514 }
vout_ControlChangeCropWindow(vout_thread_t * vout,int x,int y,int width,int height)515 void vout_ControlChangeCropWindow(vout_thread_t *vout,
516                                   int x, int y, int width, int height)
517 {
518     vout_control_cmd_t cmd;
519     vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
520     cmd.u.window.x      = __MAX(x, 0);
521     cmd.u.window.y      = __MAX(y, 0);
522     cmd.u.window.width  = __MAX(width, 0);
523     cmd.u.window.height = __MAX(height, 0);
524 
525     vout_control_Push(&vout->p->control, &cmd);
526 }
vout_ControlChangeCropBorder(vout_thread_t * vout,int left,int top,int right,int bottom)527 void vout_ControlChangeCropBorder(vout_thread_t *vout,
528                                   int left, int top, int right, int bottom)
529 {
530     vout_control_cmd_t cmd;
531     vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
532     cmd.u.border.left   = __MAX(left, 0);
533     cmd.u.border.top    = __MAX(top, 0);
534     cmd.u.border.right  = __MAX(right, 0);
535     cmd.u.border.bottom = __MAX(bottom, 0);
536 
537     vout_control_Push(&vout->p->control, &cmd);
538 }
vout_ControlChangeFilters(vout_thread_t * vout,const char * filters)539 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
540 {
541     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
542                             filters);
543 }
vout_ControlChangeSubSources(vout_thread_t * vout,const char * filters)544 void vout_ControlChangeSubSources(vout_thread_t *vout, const char *filters)
545 {
546     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_SOURCES,
547                             filters);
548 }
vout_ControlChangeSubFilters(vout_thread_t * vout,const char * filters)549 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
550 {
551     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
552                             filters);
553 }
vout_ControlChangeSubMargin(vout_thread_t * vout,int margin)554 void vout_ControlChangeSubMargin(vout_thread_t *vout, int margin)
555 {
556     vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_MARGIN,
557                              margin);
558 }
559 
vout_ControlChangeViewpoint(vout_thread_t * vout,const vlc_viewpoint_t * p_viewpoint)560 void vout_ControlChangeViewpoint(vout_thread_t *vout,
561                                  const vlc_viewpoint_t *p_viewpoint)
562 {
563     vout_control_cmd_t cmd;
564     vout_control_cmd_Init(&cmd, VOUT_CONTROL_VIEWPOINT);
565     cmd.u.viewpoint = *p_viewpoint;
566     vout_control_Push(&vout->p->control, &cmd);
567 }
568 
569 /* */
VoutGetDisplayCfg(vout_thread_t * vout,vout_display_cfg_t * cfg,const char * title)570 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
571 {
572     /* Load configuration */
573 #if defined(_WIN32) || defined(__OS2__)
574     cfg->is_fullscreen = var_GetBool(vout, "fullscreen")
575                          || var_GetBool(vout, "video-wallpaper");
576 #endif
577     cfg->viewpoint = vout->p->original.pose;
578 
579     cfg->display.title = title;
580     const int display_width = var_GetInteger(vout, "width");
581     const int display_height = var_GetInteger(vout, "height");
582     cfg->display.width   = display_width > 0  ? display_width  : 0;
583     cfg->display.height  = display_height > 0 ? display_height : 0;
584     cfg->is_display_filled  = var_GetBool(vout, "autoscale");
585     unsigned msar_num, msar_den;
586     if (var_InheritURational(vout, &msar_num, &msar_den, "monitor-par") ||
587         msar_num <= 0 || msar_den <= 0) {
588         msar_num = 1;
589         msar_den = 1;
590     }
591     cfg->display.sar.num = msar_num;
592     cfg->display.sar.den = msar_den;
593     unsigned zoom_den = 1000;
594     unsigned zoom_num = zoom_den * var_GetFloat(vout, "zoom");
595     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
596     cfg->zoom.num = zoom_num;
597     cfg->zoom.den = zoom_den;
598     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
599     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
600     const int align_mask = var_GetInteger(vout, "align");
601     if (align_mask & 0x1)
602         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
603     else if (align_mask & 0x2)
604         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
605     if (align_mask & 0x4)
606         cfg->align.vertical = VOUT_DISPLAY_ALIGN_TOP;
607     else if (align_mask & 0x8)
608         cfg->align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
609 }
610 
vout_NewDisplayWindow(vout_thread_t * vout,unsigned type)611 vout_window_t *vout_NewDisplayWindow(vout_thread_t *vout, unsigned type)
612 {
613     vout_window_t *window = vout->p->window;
614 
615     assert(vout->p->splitter_name == NULL);
616 
617     if (window == NULL)
618         return NULL;
619     if (type != VOUT_WINDOW_TYPE_INVALID && type != window->type)
620         return NULL;
621     return window;
622 }
623 
vout_DeleteDisplayWindow(vout_thread_t * vout,vout_window_t * window)624 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_window_t *window)
625 {
626     if (window == NULL && vout->p->window != NULL) {
627         vout_display_window_Delete(vout->p->window);
628         vout->p->window = NULL;
629     }
630     assert(vout->p->window == window);
631 }
632 
vout_SetDisplayWindowSize(vout_thread_t * vout,unsigned width,unsigned height)633 void vout_SetDisplayWindowSize(vout_thread_t *vout,
634                                unsigned width, unsigned height)
635 {
636     vout_window_t *window = vout->p->window;
637 
638     if (window != NULL)
639     /* Request a resize of the window. If it fails, there is nothing to do.
640      * If it succeeds, the window will emit a resize event later. */
641         vout_window_SetSize(window, width, height);
642     else
643     if (vout->p->display.vd != NULL)
644     /* Force a resize of window-less display. This is not allowed to fail,
645      * although the display is allowed to ignore the size anyway. */
646         /* FIXME: remove this, fix MSW and OS/2 window providers */
647         vout_display_SendEventDisplaySize(vout->p->display.vd, width, height);
648 }
649 
vout_HideWindowMouse(vout_thread_t * vout,bool hide)650 int vout_HideWindowMouse(vout_thread_t *vout, bool hide)
651 {
652     vout_window_t *window = vout->p->window;
653 
654     return window != NULL ? vout_window_HideMouse(window, hide) : VLC_EGENERIC;
655 }
656 
657 /* */
FilterRestartCallback(vlc_object_t * p_this,char const * psz_var,vlc_value_t oldval,vlc_value_t newval,void * p_data)658 static int FilterRestartCallback(vlc_object_t *p_this, char const *psz_var,
659                                  vlc_value_t oldval, vlc_value_t newval,
660                                  void *p_data)
661 {
662     (void) p_this; (void) psz_var; (void) oldval; (void) newval;
663     vout_ControlChangeFilters((vout_thread_t *)p_data, NULL);
664     return 0;
665 }
666 
ThreadDelFilterCallbacks(filter_t * filter,void * opaque)667 static int ThreadDelFilterCallbacks(filter_t *filter, void *opaque)
668 {
669     filter_DelProxyCallbacks((vlc_object_t *)opaque, filter,
670                              FilterRestartCallback);
671     return VLC_SUCCESS;
672 }
673 
ThreadDelAllFilterCallbacks(vout_thread_t * vout)674 static void ThreadDelAllFilterCallbacks(vout_thread_t *vout)
675 {
676     assert(vout->p->filter.chain_interactive != NULL);
677     filter_chain_ForEach(vout->p->filter.chain_interactive,
678                          ThreadDelFilterCallbacks, vout);
679 }
680 
VoutVideoFilterInteractiveNewPicture(filter_t * filter)681 static picture_t *VoutVideoFilterInteractiveNewPicture(filter_t *filter)
682 {
683     vout_thread_t *vout = filter->owner.sys;
684 
685     picture_t *picture = picture_pool_Get(vout->p->private_pool);
686     if (picture) {
687         picture_Reset(picture);
688         VideoFormatCopyCropAr(&picture->format, &filter->fmt_out.video);
689     }
690     return picture;
691 }
692 
VoutVideoFilterStaticNewPicture(filter_t * filter)693 static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
694 {
695     vout_thread_t *vout = filter->owner.sys;
696 
697     vlc_assert_locked(&vout->p->filter.lock);
698     if (filter_chain_IsEmpty(vout->p->filter.chain_interactive))
699         return VoutVideoFilterInteractiveNewPicture(filter);
700 
701     return picture_NewFromFormat(&filter->fmt_out.video);
702 }
703 
ThreadFilterFlush(vout_thread_t * vout,bool is_locked)704 static void ThreadFilterFlush(vout_thread_t *vout, bool is_locked)
705 {
706     if (vout->p->displayed.current)
707         picture_Release( vout->p->displayed.current );
708     vout->p->displayed.current = NULL;
709 
710     if (vout->p->displayed.next)
711         picture_Release( vout->p->displayed.next );
712     vout->p->displayed.next = NULL;
713 
714     if (!is_locked)
715         vlc_mutex_lock(&vout->p->filter.lock);
716     filter_chain_VideoFlush(vout->p->filter.chain_static);
717     filter_chain_VideoFlush(vout->p->filter.chain_interactive);
718     if (!is_locked)
719         vlc_mutex_unlock(&vout->p->filter.lock);
720 }
721 
722 typedef struct {
723     char           *name;
724     config_chain_t *cfg;
725 } vout_filter_t;
726 
ThreadChangeFilters(vout_thread_t * vout,const video_format_t * source,const char * filters,int deinterlace,bool is_locked)727 static void ThreadChangeFilters(vout_thread_t *vout,
728                                 const video_format_t *source,
729                                 const char *filters,
730                                 int deinterlace,
731                                 bool is_locked)
732 {
733     ThreadFilterFlush(vout, is_locked);
734     ThreadDelAllFilterCallbacks(vout);
735 
736     vlc_array_t array_static;
737     vlc_array_t array_interactive;
738 
739     vlc_array_init(&array_static);
740     vlc_array_init(&array_interactive);
741 
742     if ((vout->p->filter.has_deint =
743          deinterlace == 1 || (deinterlace == -1 && vout->p->filter.has_deint)))
744     {
745         vout_filter_t *e = malloc(sizeof(*e));
746 
747         if (likely(e))
748         {
749             free(config_ChainCreate(&e->name, &e->cfg, "deinterlace"));
750             vlc_array_append_or_abort(&array_static, e);
751         }
752     }
753 
754     char *current = filters ? strdup(filters) : NULL;
755     while (current) {
756         config_chain_t *cfg;
757         char *name;
758         char *next = config_ChainCreate(&name, &cfg, current);
759 
760         if (name && *name) {
761             vout_filter_t *e = malloc(sizeof(*e));
762 
763             if (likely(e)) {
764                 e->name = name;
765                 e->cfg  = cfg;
766                 if (!strcmp(e->name, "postproc"))
767                     vlc_array_append_or_abort(&array_static, e);
768                 else
769                     vlc_array_append_or_abort(&array_interactive, e);
770             }
771             else {
772                 if (cfg)
773                     config_ChainDestroy(cfg);
774                 free(name);
775             }
776         } else {
777             if (cfg)
778                 config_ChainDestroy(cfg);
779             free(name);
780         }
781         free(current);
782         current = next;
783     }
784 
785     if (!is_locked)
786         vlc_mutex_lock(&vout->p->filter.lock);
787 
788     es_format_t fmt_target;
789     es_format_InitFromVideo(&fmt_target, source ? source : &vout->p->filter.format);
790 
791     const es_format_t *p_fmt_current = &fmt_target;
792 
793     for (int a = 0; a < 2; a++) {
794         vlc_array_t    *array = a == 0 ? &array_static :
795                                          &array_interactive;
796         filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
797                                          vout->p->filter.chain_interactive;
798 
799         filter_chain_Reset(chain, p_fmt_current, p_fmt_current);
800         for (size_t i = 0; i < vlc_array_count(array); i++) {
801             vout_filter_t *e = vlc_array_item_at_index(array, i);
802             msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
803             filter_t *filter = filter_chain_AppendFilter(chain, e->name, e->cfg,
804                                NULL, NULL);
805             if (!filter)
806             {
807                 msg_Err(vout, "Failed to add filter '%s'", e->name);
808                 config_ChainDestroy(e->cfg);
809             }
810             else if (a == 1) /* Add callbacks for interactive filters */
811                 filter_AddProxyCallbacks(vout, filter, FilterRestartCallback);
812 
813             free(e->name);
814             free(e);
815         }
816         p_fmt_current = filter_chain_GetFmtOut(chain);
817         vlc_array_clear(array);
818     }
819 
820     if (!es_format_IsSimilar(p_fmt_current, &fmt_target)) {
821         msg_Dbg(vout, "Adding a filter to compensate for format changes");
822         if (filter_chain_AppendConverter(vout->p->filter.chain_interactive,
823                                          p_fmt_current, &fmt_target) != 0) {
824             msg_Err(vout, "Failed to compensate for the format changes, removing all filters");
825             ThreadDelAllFilterCallbacks(vout);
826             filter_chain_Reset(vout->p->filter.chain_static,      &fmt_target, &fmt_target);
827             filter_chain_Reset(vout->p->filter.chain_interactive, &fmt_target, &fmt_target);
828         }
829     }
830 
831     es_format_Clean(&fmt_target);
832 
833     if (vout->p->filter.configuration != filters) {
834         free(vout->p->filter.configuration);
835         vout->p->filter.configuration = filters ? strdup(filters) : NULL;
836     }
837     if (source) {
838         video_format_Clean(&vout->p->filter.format);
839         video_format_Copy(&vout->p->filter.format, source);
840     }
841 
842     if (!is_locked)
843         vlc_mutex_unlock(&vout->p->filter.lock);
844 }
845 
846 
847 /* */
ThreadDisplayPreparePicture(vout_thread_t * vout,bool reuse,bool frame_by_frame)848 static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool frame_by_frame)
849 {
850     bool is_late_dropped = vout->p->is_late_dropped && !vout->p->pause.is_on && !frame_by_frame;
851 
852     vlc_mutex_lock(&vout->p->filter.lock);
853 
854     picture_t *picture = filter_chain_VideoFilter(vout->p->filter.chain_static, NULL);
855     assert(!reuse || !picture);
856 
857     while (!picture) {
858         picture_t *decoded;
859         if (reuse && vout->p->displayed.decoded) {
860             decoded = picture_Hold(vout->p->displayed.decoded);
861         } else {
862             decoded = picture_fifo_Pop(vout->p->decoder_fifo);
863             if (decoded) {
864                 if (is_late_dropped && !decoded->b_force) {
865                     mtime_t late_threshold;
866                     if (decoded->format.i_frame_rate && decoded->format.i_frame_rate_base)
867                         late_threshold = ((CLOCK_FREQ/2) * decoded->format.i_frame_rate_base) / decoded->format.i_frame_rate;
868                     else
869                         late_threshold = VOUT_DISPLAY_LATE_THRESHOLD;
870                     const mtime_t predicted = mdate() + 0; /* TODO improve */
871                     const mtime_t late = predicted - decoded->date;
872                     if (late > late_threshold) {
873                         msg_Warn(vout, "picture is too late to be displayed (missing %"PRId64" ms)", late/1000);
874                         picture_Release(decoded);
875                         vout_statistic_AddLost(&vout->p->statistic, 1);
876                         continue;
877                     } else if (late > 0) {
878                         msg_Dbg(vout, "picture might be displayed late (missing %"PRId64" ms)", late/1000);
879                     }
880                 }
881                 if (!VideoFormatIsCropArEqual(&decoded->format, &vout->p->filter.format))
882                     ThreadChangeFilters(vout, &decoded->format, vout->p->filter.configuration, -1, true);
883             }
884         }
885 
886         if (!decoded)
887             break;
888         reuse = false;
889 
890         if (vout->p->displayed.decoded)
891             picture_Release(vout->p->displayed.decoded);
892 
893         vout->p->displayed.decoded       = picture_Hold(decoded);
894         vout->p->displayed.timestamp     = decoded->date;
895         vout->p->displayed.is_interlaced = !decoded->b_progressive;
896 
897         picture = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
898     }
899 
900     vlc_mutex_unlock(&vout->p->filter.lock);
901 
902     if (!picture)
903         return VLC_EGENERIC;
904 
905     assert(!vout->p->displayed.next);
906     if (!vout->p->displayed.current)
907         vout->p->displayed.current = picture;
908     else
909         vout->p->displayed.next    = picture;
910     return VLC_SUCCESS;
911 }
912 
ConvertRGB32AndBlendBufferNew(filter_t * filter)913 static picture_t *ConvertRGB32AndBlendBufferNew(filter_t *filter)
914 {
915     return picture_NewFromFormat(&filter->fmt_out.video);
916 }
917 
ConvertRGB32AndBlend(vout_thread_t * vout,picture_t * pic,subpicture_t * subpic)918 static picture_t *ConvertRGB32AndBlend(vout_thread_t *vout, picture_t *pic,
919                                      subpicture_t *subpic)
920 {
921     /* This function will convert the pic to RGB32 and blend the subpic to it.
922      * The returned pic can't be used to display since the chroma will be
923      * different than the "vout display" one, but it can be used for snapshots.
924      * */
925 
926     assert(vout->p->spu_blend);
927 
928     filter_owner_t owner = {
929         .video = {
930             .buffer_new = ConvertRGB32AndBlendBufferNew,
931         },
932     };
933     filter_chain_t *filterc = filter_chain_NewVideo(vout, false, &owner);
934     if (!filterc)
935         return NULL;
936 
937     es_format_t src = vout->p->spu_blend->fmt_out;
938     es_format_t dst = src;
939     dst.video.i_chroma = VLC_CODEC_RGB32;
940     video_format_FixRgb(&dst.video);
941 
942     if (filter_chain_AppendConverter(filterc, &src, &dst) != 0)
943     {
944         filter_chain_Delete(filterc);
945         return NULL;
946     }
947 
948     picture_Hold(pic);
949     pic = filter_chain_VideoFilter(filterc, pic);
950     filter_chain_Delete(filterc);
951 
952     if (pic)
953     {
954         filter_t *swblend = filter_NewBlend(VLC_OBJECT(vout), &dst.video);
955         if (swblend)
956         {
957             bool success = picture_BlendSubpicture(pic, swblend, subpic) > 0;
958             filter_DeleteBlend(swblend);
959             if (success)
960                 return pic;
961         }
962         picture_Release(pic);
963     }
964     return NULL;
965 }
966 
ThreadDisplayRenderPicture(vout_thread_t * vout,bool is_forced)967 static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
968 {
969     vout_thread_sys_t *sys = vout->p;
970     vout_display_t *vd = vout->p->display.vd;
971 
972     picture_t *torender = picture_Hold(vout->p->displayed.current);
973 
974     vout_chrono_Start(&vout->p->render);
975 
976     vlc_mutex_lock(&vout->p->filter.lock);
977     picture_t *filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, torender);
978     vlc_mutex_unlock(&vout->p->filter.lock);
979 
980     if (!filtered)
981         return VLC_EGENERIC;
982 
983     if (filtered->date != vout->p->displayed.current->date)
984         msg_Warn(vout, "Unsupported timestamp modifications done by chain_interactive");
985 
986     /*
987      * Get the subpicture to be displayed
988      */
989     const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
990     mtime_t render_subtitle_date;
991     if (vout->p->pause.is_on)
992         render_subtitle_date = vout->p->pause.date;
993     else
994         render_subtitle_date = filtered->date > 1 ? filtered->date : mdate();
995     mtime_t render_osd_date = mdate(); /* FIXME wrong */
996 
997     /*
998      * Get the subpicture to be displayed
999      */
1000     const bool do_dr_spu = !do_snapshot &&
1001                            vd->info.subpicture_chromas &&
1002                            *vd->info.subpicture_chromas != 0;
1003 
1004     //FIXME: Denying do_early_spu if vd->source.orientation != ORIENT_NORMAL
1005     //will have the effect that snapshots miss the subpictures. We do this
1006     //because there is currently no way to transform subpictures to match
1007     //the source format.
1008     const bool do_early_spu = !do_dr_spu &&
1009                                vd->source.orientation == ORIENT_NORMAL &&
1010                               (vd->info.is_slow ||
1011                                sys->display.use_dr ||
1012                                do_snapshot ||
1013                                vd->fmt.i_width * vd->fmt.i_height <= vd->source.i_width * vd->source.i_height);
1014 
1015     const vlc_fourcc_t *subpicture_chromas;
1016     video_format_t fmt_spu;
1017     if (do_dr_spu) {
1018         vout_display_place_t place;
1019         vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
1020 
1021         fmt_spu = vd->source;
1022         if (fmt_spu.i_width * fmt_spu.i_height < place.width * place.height) {
1023             fmt_spu.i_sar_num = vd->cfg->display.sar.num;
1024             fmt_spu.i_sar_den = vd->cfg->display.sar.den;
1025             fmt_spu.i_width          =
1026             fmt_spu.i_visible_width  = place.width;
1027             fmt_spu.i_height         =
1028             fmt_spu.i_visible_height = place.height;
1029         }
1030         subpicture_chromas = vd->info.subpicture_chromas;
1031     } else {
1032         if (do_early_spu) {
1033             fmt_spu = vd->source;
1034         } else {
1035             fmt_spu = vd->fmt;
1036             fmt_spu.i_sar_num = vd->cfg->display.sar.num;
1037             fmt_spu.i_sar_den = vd->cfg->display.sar.den;
1038         }
1039         subpicture_chromas = NULL;
1040 
1041         if (vout->p->spu_blend &&
1042             vout->p->spu_blend->fmt_out.video.i_chroma != fmt_spu.i_chroma) {
1043             filter_DeleteBlend(vout->p->spu_blend);
1044             vout->p->spu_blend = NULL;
1045             vout->p->spu_blend_chroma = 0;
1046         }
1047         if (!vout->p->spu_blend && vout->p->spu_blend_chroma != fmt_spu.i_chroma) {
1048             vout->p->spu_blend_chroma = fmt_spu.i_chroma;
1049             vout->p->spu_blend = filter_NewBlend(VLC_OBJECT(vout), &fmt_spu);
1050             if (!vout->p->spu_blend)
1051                 msg_Err(vout, "Failed to create blending filter, OSD/Subtitles will not work");
1052         }
1053     }
1054 
1055     video_format_t fmt_spu_rot;
1056     video_format_ApplyRotation(&fmt_spu_rot, &fmt_spu);
1057     subpicture_t *subpic = spu_Render(vout->p->spu,
1058                                       subpicture_chromas, &fmt_spu_rot,
1059                                       &vd->source,
1060                                       render_subtitle_date, render_osd_date,
1061                                       do_snapshot);
1062     /*
1063      * Perform rendering
1064      *
1065      * We have to:
1066      * - be sure to end up with a direct buffer.
1067      * - blend subtitles, and in a fast access buffer
1068      */
1069     bool is_direct = vout->p->decoder_pool == vout->p->display_pool;
1070     picture_t *todisplay = filtered;
1071     picture_t *snap_pic = todisplay;
1072     if (do_early_spu && subpic) {
1073         if (vout->p->spu_blend) {
1074             picture_t *blent = picture_pool_Get(vout->p->private_pool);
1075             if (blent) {
1076                 VideoFormatCopyCropAr(&blent->format, &filtered->format);
1077                 picture_Copy(blent, filtered);
1078                 if (picture_BlendSubpicture(blent, vout->p->spu_blend, subpic)) {
1079                     picture_Release(todisplay);
1080                     snap_pic = todisplay = blent;
1081                 } else
1082                 {
1083                     /* Blending failed, likely because the picture is opaque or
1084                      * read-only. Try to convert the opaque picture to a
1085                      * software RGB32 one before blending it. */
1086                     if (do_snapshot)
1087                     {
1088                         picture_t *copy = ConvertRGB32AndBlend(vout, blent, subpic);
1089                         if (copy)
1090                             snap_pic = copy;
1091                     }
1092                     picture_Release(blent);
1093                 }
1094             }
1095         }
1096         subpicture_Delete(subpic);
1097         subpic = NULL;
1098     }
1099 
1100     assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
1101     if (sys->display.use_dr && !is_direct) {
1102         picture_t *direct = NULL;
1103         if (likely(vout->p->display_pool != NULL))
1104             direct = picture_pool_Get(vout->p->display_pool);
1105         if (!direct) {
1106             picture_Release(todisplay);
1107             if (subpic)
1108                 subpicture_Delete(subpic);
1109             return VLC_EGENERIC;
1110         }
1111 
1112         /* The display uses direct rendering (no conversion), but its pool of
1113          * pictures is not usable by the decoder (too few, too slow or
1114          * subject to invalidation...). Since there are no filters, copying
1115          * pictures from the decoder to the output is unavoidable. */
1116         VideoFormatCopyCropAr(&direct->format, &todisplay->format);
1117         picture_Copy(direct, todisplay);
1118         picture_Release(todisplay);
1119         snap_pic = todisplay = direct;
1120     }
1121 
1122     /*
1123      * Take a snapshot if requested
1124      */
1125     if (do_snapshot)
1126     {
1127         assert(snap_pic);
1128         vout_snapshot_Set(&vout->p->snapshot, &vd->source, snap_pic);
1129         if (snap_pic != todisplay)
1130             picture_Release(snap_pic);
1131     }
1132 
1133     /* Render the direct buffer */
1134     vout_UpdateDisplaySourceProperties(vd, &todisplay->format);
1135 
1136     todisplay = vout_FilterDisplay(vd, todisplay);
1137     if (todisplay == NULL) {
1138         if (subpic != NULL)
1139             subpicture_Delete(subpic);
1140         return VLC_EGENERIC;
1141     }
1142 
1143     if (sys->display.use_dr) {
1144         vout_display_Prepare(vd, todisplay, subpic);
1145     } else {
1146         if (!do_dr_spu && !do_early_spu && vout->p->spu_blend && subpic)
1147             picture_BlendSubpicture(todisplay, vout->p->spu_blend, subpic);
1148         vout_display_Prepare(vd, todisplay, do_dr_spu ? subpic : NULL);
1149 
1150         if (!do_dr_spu && subpic)
1151         {
1152             subpicture_Delete(subpic);
1153             subpic = NULL;
1154         }
1155     }
1156 
1157     vout_chrono_Stop(&vout->p->render);
1158 #if 0
1159         {
1160         static int i = 0;
1161         if (((i++)%10) == 0)
1162             msg_Info(vout, "render: avg %d ms var %d ms",
1163                      (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
1164         }
1165 #endif
1166 
1167     /* Wait the real date (for rendering jitter) */
1168 #if 0
1169     mtime_t delay = todisplay->date - mdate();
1170     if (delay < 1000)
1171         msg_Warn(vout, "picture is late (%lld ms)", delay / 1000);
1172 #endif
1173     if (!is_forced)
1174         mwait(todisplay->date);
1175 
1176     /* Display the direct buffer returned by vout_RenderPicture */
1177     vout->p->displayed.date = mdate();
1178     vout_display_Display(vd, todisplay, subpic);
1179 
1180     vout_statistic_AddDisplayed(&vout->p->statistic, 1);
1181 
1182     return VLC_SUCCESS;
1183 }
1184 
ThreadDisplayPicture(vout_thread_t * vout,mtime_t * deadline)1185 static int ThreadDisplayPicture(vout_thread_t *vout, mtime_t *deadline)
1186 {
1187     bool frame_by_frame = !deadline;
1188     bool paused = vout->p->pause.is_on;
1189     bool first = !vout->p->displayed.current;
1190 
1191     if (first)
1192         if (ThreadDisplayPreparePicture(vout, true, frame_by_frame)) /* FIXME not sure it is ok */
1193             return VLC_EGENERIC;
1194 
1195     if (!paused || frame_by_frame)
1196         while (!vout->p->displayed.next && !ThreadDisplayPreparePicture(vout, false, frame_by_frame))
1197             ;
1198 
1199     const mtime_t date = mdate();
1200     const mtime_t render_delay = vout_chrono_GetHigh(&vout->p->render) + VOUT_MWAIT_TOLERANCE;
1201 
1202     bool drop_next_frame = frame_by_frame;
1203     mtime_t date_next = VLC_TS_INVALID;
1204     if (!paused && vout->p->displayed.next) {
1205         date_next = vout->p->displayed.next->date - render_delay;
1206         if (date_next /* + 0 FIXME */ <= date)
1207             drop_next_frame = true;
1208     }
1209 
1210     /* FIXME/XXX we must redisplay the last decoded picture (because
1211      * of potential vout updated, or filters update or SPU update)
1212      * For now a high update period is needed but it could be removed
1213      * if and only if:
1214      * - vout module emits events from theselves.
1215      * - *and* SPU is modified to emit an event or a deadline when needed.
1216      *
1217      * So it will be done later.
1218      */
1219     bool refresh = false;
1220 
1221     mtime_t date_refresh = VLC_TS_INVALID;
1222     if (vout->p->displayed.date > VLC_TS_INVALID) {
1223         date_refresh = vout->p->displayed.date + VOUT_REDISPLAY_DELAY - render_delay;
1224         refresh = date_refresh <= date;
1225     }
1226     bool force_refresh = !drop_next_frame && refresh;
1227 
1228     if (!frame_by_frame) {
1229         if (date_refresh != VLC_TS_INVALID)
1230             *deadline = date_refresh;
1231         if (date_next != VLC_TS_INVALID && date_next < *deadline)
1232             *deadline = date_next;
1233     }
1234 
1235     if (!first && !refresh && !drop_next_frame) {
1236         return VLC_EGENERIC;
1237     }
1238 
1239     if (drop_next_frame) {
1240         picture_Release(vout->p->displayed.current);
1241         vout->p->displayed.current = vout->p->displayed.next;
1242         vout->p->displayed.next    = NULL;
1243     }
1244 
1245     if (!vout->p->displayed.current)
1246         return VLC_EGENERIC;
1247 
1248     /* display the picture immediately */
1249     bool is_forced = frame_by_frame || force_refresh || vout->p->displayed.current->b_force;
1250     int ret = ThreadDisplayRenderPicture(vout, is_forced);
1251     return force_refresh ? VLC_EGENERIC : ret;
1252 }
1253 
ThreadDisplaySubpicture(vout_thread_t * vout,subpicture_t * subpicture)1254 static void ThreadDisplaySubpicture(vout_thread_t *vout,
1255                                     subpicture_t *subpicture)
1256 {
1257     spu_PutSubpicture(vout->p->spu, subpicture);
1258 }
1259 
ThreadFlushSubpicture(vout_thread_t * vout,int channel)1260 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
1261 {
1262     spu_ClearChannel(vout->p->spu, channel);
1263 }
1264 
ThreadDisplayOsdTitle(vout_thread_t * vout,const char * string)1265 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
1266 {
1267     if (!vout->p->title.show)
1268         return;
1269 
1270     vout_OSDText(vout, VOUT_SPU_CHANNEL_OSD,
1271                  vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
1272                  string);
1273 }
1274 
ThreadChangeSubSources(vout_thread_t * vout,const char * filters)1275 static void ThreadChangeSubSources(vout_thread_t *vout, const char *filters)
1276 {
1277     spu_ChangeSources(vout->p->spu, filters);
1278 }
1279 
ThreadChangeSubFilters(vout_thread_t * vout,const char * filters)1280 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
1281 {
1282     spu_ChangeFilters(vout->p->spu, filters);
1283 }
1284 
ThreadChangeSubMargin(vout_thread_t * vout,int margin)1285 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
1286 {
1287     spu_ChangeMargin(vout->p->spu, margin);
1288 }
1289 
ThreadChangePause(vout_thread_t * vout,bool is_paused,mtime_t date)1290 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
1291 {
1292     assert(!vout->p->pause.is_on || !is_paused);
1293 
1294     if (vout->p->pause.is_on) {
1295         const mtime_t duration = date - vout->p->pause.date;
1296 
1297         if (vout->p->step.timestamp > VLC_TS_INVALID)
1298             vout->p->step.timestamp += duration;
1299         if (vout->p->step.last > VLC_TS_INVALID)
1300             vout->p->step.last += duration;
1301         picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
1302         if (vout->p->displayed.decoded)
1303             vout->p->displayed.decoded->date += duration;
1304         spu_OffsetSubtitleDate(vout->p->spu, duration);
1305 
1306         ThreadFilterFlush(vout, false);
1307     } else {
1308         vout->p->step.timestamp = VLC_TS_INVALID;
1309         vout->p->step.last      = VLC_TS_INVALID;
1310     }
1311     vout->p->pause.is_on = is_paused;
1312     vout->p->pause.date  = date;
1313 
1314     vout_window_t *window = vout->p->window;
1315     if (window != NULL)
1316         vout_window_SetInhibition(window, !is_paused);
1317 }
1318 
ThreadFlush(vout_thread_t * vout,bool below,mtime_t date)1319 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
1320 {
1321     vout->p->step.timestamp = VLC_TS_INVALID;
1322     vout->p->step.last      = VLC_TS_INVALID;
1323 
1324     ThreadFilterFlush(vout, false); /* FIXME too much */
1325 
1326     picture_t *last = vout->p->displayed.decoded;
1327     if (last) {
1328         if (( below && last->date <= date) ||
1329             (!below && last->date >= date)) {
1330             picture_Release(last);
1331 
1332             vout->p->displayed.decoded   = NULL;
1333             vout->p->displayed.date      = VLC_TS_INVALID;
1334             vout->p->displayed.timestamp = VLC_TS_INVALID;
1335         }
1336     }
1337 
1338     picture_fifo_Flush(vout->p->decoder_fifo, date, below);
1339     vout_FilterFlush(vout->p->display.vd);
1340 }
1341 
ThreadStep(vout_thread_t * vout,mtime_t * duration)1342 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
1343 {
1344     *duration = 0;
1345 
1346     if (vout->p->step.last <= VLC_TS_INVALID)
1347         vout->p->step.last = vout->p->displayed.timestamp;
1348 
1349     if (ThreadDisplayPicture(vout, NULL))
1350         return;
1351 
1352     vout->p->step.timestamp = vout->p->displayed.timestamp;
1353 
1354     if (vout->p->step.last > VLC_TS_INVALID &&
1355         vout->p->step.timestamp > vout->p->step.last) {
1356         *duration = vout->p->step.timestamp - vout->p->step.last;
1357         vout->p->step.last = vout->p->step.timestamp;
1358         /* TODO advance subpicture by the duration ... */
1359     }
1360 }
1361 
ThreadChangeFullscreen(vout_thread_t * vout,bool fullscreen)1362 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
1363 {
1364     vout_window_t *window = vout->p->window;
1365 
1366 #if !defined(_WIN32) && !defined(__OS2__)
1367     if (window != NULL)
1368         vout_window_SetFullScreen(window, fullscreen);
1369 #else
1370     bool window_fullscreen = false;
1371     if (window != NULL
1372      && vout_window_SetFullScreen(window, fullscreen) == VLC_SUCCESS)
1373         window_fullscreen = true;
1374     /* FIXME: remove this event */
1375     if (vout->p->display.vd != NULL)
1376         vout_display_SendEventFullscreen(vout->p->display.vd, fullscreen, window_fullscreen);
1377 #endif
1378 }
1379 
ThreadChangeWindowState(vout_thread_t * vout,unsigned state)1380 static void ThreadChangeWindowState(vout_thread_t *vout, unsigned state)
1381 {
1382     vout_window_t *window = vout->p->window;
1383 
1384     if (window != NULL)
1385         vout_window_SetState(window, state);
1386 #if defined(_WIN32) || defined(__OS2__)
1387     else /* FIXME: remove this event */
1388     if (vout->p->display.vd != NULL)
1389         vout_display_SendWindowState(vout->p->display.vd, state);
1390 #endif
1391 }
1392 
ThreadChangeWindowMouse(vout_thread_t * vout,const vout_window_mouse_event_t * mouse)1393 static void ThreadChangeWindowMouse(vout_thread_t *vout,
1394                                     const vout_window_mouse_event_t *mouse)
1395 {
1396     vout_display_t *vd = vout->p->display.vd;
1397     switch (mouse->type)
1398     {
1399         case VOUT_WINDOW_MOUSE_STATE:
1400         case VOUT_WINDOW_MOUSE_MOVED:
1401         {
1402             vout_display_place_t place;
1403             vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
1404 
1405             if (place.width <= 0 || place.height <= 0)
1406                 return;
1407 
1408             const int x = vd->source.i_x_offset +
1409                 (int64_t)(mouse->x - place.x) *
1410                 vd->source.i_visible_width / place.width;
1411             const int y = vd->source.i_y_offset +
1412                 (int64_t)(mouse->y - place.y) *
1413                 vd->source.i_visible_height/ place.height;
1414 
1415             if (mouse->type == VOUT_WINDOW_MOUSE_STATE)
1416                 vout_display_SendEventMouseState(vd, x, y, mouse->button_mask);
1417             else
1418                 vout_display_SendEventMouseMoved(vd, x, y);
1419             break;
1420         }
1421         case VOUT_WINDOW_MOUSE_PRESSED:
1422             vout_display_SendEventMousePressed(vd, mouse->button_mask);
1423             break;
1424         case VOUT_WINDOW_MOUSE_RELEASED:
1425             vout_display_SendEventMouseReleased(vd, mouse->button_mask);
1426             break;
1427         case VOUT_WINDOW_MOUSE_DOUBLE_CLICK:
1428             if (mouse->button_mask == 0)
1429                 vout_display_SendEventMouseDoubleClick(vd);
1430             break;
1431         default: vlc_assert_unreachable();
1432             break;
1433     }
1434 }
1435 
ThreadChangeDisplayFilled(vout_thread_t * vout,bool is_filled)1436 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1437 {
1438     vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1439 }
1440 
ThreadChangeZoom(vout_thread_t * vout,int num,int den)1441 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1442 {
1443     if (num * 10 < den) {
1444         num = den;
1445         den *= 10;
1446     } else if (num > den * 10) {
1447         num = den * 10;
1448     }
1449 
1450     vout_SetDisplayZoom(vout->p->display.vd, num, den);
1451 }
1452 
ThreadChangeAspectRatio(vout_thread_t * vout,unsigned num,unsigned den)1453 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1454                                     unsigned num, unsigned den)
1455 {
1456     vout_SetDisplayAspect(vout->p->display.vd, num, den);
1457 }
1458 
1459 
ThreadExecuteCropWindow(vout_thread_t * vout,unsigned x,unsigned y,unsigned width,unsigned height)1460 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1461                                     unsigned x, unsigned y,
1462                                     unsigned width, unsigned height)
1463 {
1464     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1465                         x, y, width, height);
1466 }
ThreadExecuteCropBorder(vout_thread_t * vout,unsigned left,unsigned top,unsigned right,unsigned bottom)1467 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1468                                     unsigned left, unsigned top,
1469                                     unsigned right, unsigned bottom)
1470 {
1471     msg_Dbg(vout, "ThreadExecuteCropBorder %d.%d %dx%d", left, top, right, bottom);
1472     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1473                         left, top, -(int)right, -(int)bottom);
1474 }
1475 
ThreadExecuteCropRatio(vout_thread_t * vout,unsigned num,unsigned den)1476 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1477                                    unsigned num, unsigned den)
1478 {
1479     vout_SetDisplayCrop(vout->p->display.vd, num, den,
1480                         0, 0, 0, 0);
1481 }
1482 
ThreadExecuteViewpoint(vout_thread_t * vout,const vlc_viewpoint_t * p_viewpoint)1483 static void ThreadExecuteViewpoint(vout_thread_t *vout,
1484                                    const vlc_viewpoint_t *p_viewpoint)
1485 {
1486     vout_SetDisplayViewpoint(vout->p->display.vd, p_viewpoint);
1487 }
1488 
ThreadStart(vout_thread_t * vout,vout_display_state_t * state)1489 static int ThreadStart(vout_thread_t *vout, vout_display_state_t *state)
1490 {
1491     vlc_mouse_Init(&vout->p->mouse);
1492     vout->p->decoder_fifo = picture_fifo_New();
1493     vout->p->decoder_pool = NULL;
1494     vout->p->display_pool = NULL;
1495     vout->p->private_pool = NULL;
1496 
1497     vout->p->filter.configuration = NULL;
1498     video_format_Copy(&vout->p->filter.format, &vout->p->original);
1499 
1500     filter_owner_t owner = {
1501         .sys = vout,
1502         .video = {
1503             .buffer_new = VoutVideoFilterStaticNewPicture,
1504         },
1505     };
1506     vout->p->filter.chain_static =
1507         filter_chain_NewVideo( vout, true, &owner );
1508 
1509     owner.video.buffer_new = VoutVideoFilterInteractiveNewPicture;
1510     vout->p->filter.chain_interactive =
1511         filter_chain_NewVideo( vout, true, &owner );
1512 
1513     vout_display_state_t state_default;
1514     if (!state) {
1515         VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1516 
1517 #if defined(_WIN32) || defined(__OS2__)
1518         bool below = var_InheritBool(vout, "video-wallpaper");
1519         bool above = var_InheritBool(vout, "video-on-top");
1520 
1521         state_default.wm_state = below ? VOUT_WINDOW_STATE_BELOW
1522                                : above ? VOUT_WINDOW_STATE_ABOVE
1523                                : VOUT_WINDOW_STATE_NORMAL;
1524 #endif
1525         state_default.sar.num = 0;
1526         state_default.sar.den = 0;
1527 
1528         state = &state_default;
1529     }
1530 
1531     if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1532         goto error;
1533     if (vout_InitWrapper(vout))
1534     {
1535         vout_CloseWrapper(vout, state);
1536         goto error;
1537     }
1538     assert(vout->p->decoder_pool && vout->p->private_pool);
1539 
1540     vout->p->displayed.current       = NULL;
1541     vout->p->displayed.next          = NULL;
1542     vout->p->displayed.decoded       = NULL;
1543     vout->p->displayed.date          = VLC_TS_INVALID;
1544     vout->p->displayed.timestamp     = VLC_TS_INVALID;
1545     vout->p->displayed.is_interlaced = false;
1546 
1547     vout->p->step.last               = VLC_TS_INVALID;
1548     vout->p->step.timestamp          = VLC_TS_INVALID;
1549 
1550     vout->p->spu_blend_chroma        = 0;
1551     vout->p->spu_blend               = NULL;
1552 
1553     video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1554     return VLC_SUCCESS;
1555 error:
1556     if (vout->p->filter.chain_interactive != NULL)
1557     {
1558         ThreadDelAllFilterCallbacks(vout);
1559         filter_chain_Delete(vout->p->filter.chain_interactive);
1560     }
1561     if (vout->p->filter.chain_static != NULL)
1562         filter_chain_Delete(vout->p->filter.chain_static);
1563     video_format_Clean(&vout->p->filter.format);
1564     if (vout->p->decoder_fifo != NULL)
1565         picture_fifo_Delete(vout->p->decoder_fifo);
1566     return VLC_EGENERIC;
1567 }
1568 
ThreadStop(vout_thread_t * vout,vout_display_state_t * state)1569 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1570 {
1571     if (vout->p->spu_blend)
1572         filter_DeleteBlend(vout->p->spu_blend);
1573 
1574     /* Destroy translation tables */
1575     if (vout->p->display.vd) {
1576         if (vout->p->decoder_pool) {
1577             ThreadFlush(vout, true, INT64_MAX);
1578             vout_EndWrapper(vout);
1579         }
1580         vout_CloseWrapper(vout, state);
1581     }
1582 
1583     /* Destroy the video filters */
1584     ThreadDelAllFilterCallbacks(vout);
1585     filter_chain_Delete(vout->p->filter.chain_interactive);
1586     filter_chain_Delete(vout->p->filter.chain_static);
1587     video_format_Clean(&vout->p->filter.format);
1588     free(vout->p->filter.configuration);
1589 
1590     if (vout->p->decoder_fifo)
1591         picture_fifo_Delete(vout->p->decoder_fifo);
1592     assert(!vout->p->decoder_pool);
1593 }
1594 
ThreadInit(vout_thread_t * vout)1595 static void ThreadInit(vout_thread_t *vout)
1596 {
1597     vout->p->dead            = false;
1598     vout->p->is_late_dropped = var_InheritBool(vout, "drop-late-frames");
1599     vout->p->pause.is_on     = false;
1600     vout->p->pause.date      = VLC_TS_INVALID;
1601 
1602     vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1603 }
1604 
ThreadClean(vout_thread_t * vout)1605 static void ThreadClean(vout_thread_t *vout)
1606 {
1607     vout_chrono_Clean(&vout->p->render);
1608     vout->p->dead = true;
1609     vout_control_Dead(&vout->p->control);
1610 }
1611 
ThreadReinit(vout_thread_t * vout,const vout_configuration_t * cfg)1612 static int ThreadReinit(vout_thread_t *vout,
1613                         const vout_configuration_t *cfg)
1614 {
1615     video_format_t original;
1616 
1617     vout->p->pause.is_on = false;
1618     vout->p->pause.date  = VLC_TS_INVALID;
1619 
1620     if (VoutValidateFormat(&original, cfg->fmt)) {
1621         ThreadStop(vout, NULL);
1622         ThreadClean(vout);
1623         return VLC_EGENERIC;
1624     }
1625 
1626     /* We ignore ar changes at this point, they are dynamically supported.
1627      * #19268: don't ignore crop changes (fix vouts using the crop size of the
1628      * previous format). */
1629     vout->p->original.i_sar_num = original.i_sar_num;
1630     vout->p->original.i_sar_den = original.i_sar_den;
1631     if (video_format_IsSimilar(&original, &vout->p->original)) {
1632         if (cfg->dpb_size <= vout->p->dpb_size) {
1633             video_format_Clean(&original);
1634             return VLC_SUCCESS;
1635         }
1636         msg_Warn(vout, "DPB need to be increased");
1637     }
1638 
1639     vout_display_state_t state;
1640     memset(&state, 0, sizeof(state));
1641 
1642     ThreadStop(vout, &state);
1643 
1644     vout_ReinitInterlacingSupport(vout);
1645 
1646 #if defined(_WIN32) || defined(__OS2__)
1647     if (!state.cfg.is_fullscreen)
1648 #endif
1649     {
1650         state.cfg.display.width  = 0;
1651         state.cfg.display.height = 0;
1652     }
1653     state.sar.num = 0;
1654     state.sar.den = 0;
1655 
1656     /* FIXME current vout "variables" are not in sync here anymore
1657      * and I am not sure what to do */
1658     if (state.cfg.display.sar.num <= 0 || state.cfg.display.sar.den <= 0) {
1659         state.cfg.display.sar.num = 1;
1660         state.cfg.display.sar.den = 1;
1661     }
1662     if (state.cfg.zoom.num <= 0 || state.cfg.zoom.den <= 0) {
1663         state.cfg.zoom.num = 1;
1664         state.cfg.zoom.den = 1;
1665     }
1666 
1667     vout->p->original = original;
1668     vout->p->dpb_size = cfg->dpb_size;
1669     if (ThreadStart(vout, &state)) {
1670         ThreadClean(vout);
1671         return VLC_EGENERIC;
1672     }
1673     return VLC_SUCCESS;
1674 }
1675 
ThreadCancel(vout_thread_t * vout,bool canceled)1676 static void ThreadCancel(vout_thread_t *vout, bool canceled)
1677 {
1678     picture_pool_Cancel(vout->p->decoder_pool, canceled);
1679 }
1680 
ThreadControl(vout_thread_t * vout,vout_control_cmd_t cmd)1681 static int ThreadControl(vout_thread_t *vout, vout_control_cmd_t cmd)
1682 {
1683     switch(cmd.type) {
1684     case VOUT_CONTROL_INIT:
1685         ThreadInit(vout);
1686         if (ThreadStart(vout, NULL))
1687         {
1688             ThreadClean(vout);
1689             return 1;
1690         }
1691         break;
1692     case VOUT_CONTROL_CLEAN:
1693         ThreadStop(vout, NULL);
1694         ThreadClean(vout);
1695         return 1;
1696     case VOUT_CONTROL_REINIT:
1697         if (ThreadReinit(vout, cmd.u.cfg))
1698             return 1;
1699         break;
1700     case VOUT_CONTROL_CANCEL:
1701         ThreadCancel(vout, cmd.u.boolean);
1702         break;
1703     case VOUT_CONTROL_SUBPICTURE:
1704         ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1705         cmd.u.subpicture = NULL;
1706         break;
1707     case VOUT_CONTROL_FLUSH_SUBPICTURE:
1708         ThreadFlushSubpicture(vout, cmd.u.integer);
1709         break;
1710     case VOUT_CONTROL_OSD_TITLE:
1711         ThreadDisplayOsdTitle(vout, cmd.u.string);
1712         break;
1713     case VOUT_CONTROL_CHANGE_FILTERS:
1714         ThreadChangeFilters(vout, NULL,
1715                             cmd.u.string != NULL ?
1716                             cmd.u.string : vout->p->filter.configuration,
1717                             -1, false);
1718         break;
1719     case VOUT_CONTROL_CHANGE_INTERLACE:
1720         ThreadChangeFilters(vout, NULL, vout->p->filter.configuration,
1721                             cmd.u.boolean ? 1 : 0, false);
1722         break;
1723     case VOUT_CONTROL_CHANGE_SUB_SOURCES:
1724         ThreadChangeSubSources(vout, cmd.u.string);
1725         break;
1726     case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1727         ThreadChangeSubFilters(vout, cmd.u.string);
1728         break;
1729     case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1730         ThreadChangeSubMargin(vout, cmd.u.integer);
1731         break;
1732     case VOUT_CONTROL_PAUSE:
1733         ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1734         break;
1735     case VOUT_CONTROL_FLUSH:
1736         ThreadFlush(vout, false, cmd.u.time);
1737         break;
1738     case VOUT_CONTROL_STEP:
1739         ThreadStep(vout, cmd.u.time_ptr);
1740         break;
1741     case VOUT_CONTROL_FULLSCREEN:
1742         ThreadChangeFullscreen(vout, cmd.u.boolean);
1743         break;
1744     case VOUT_CONTROL_WINDOW_STATE:
1745         ThreadChangeWindowState(vout, cmd.u.integer);
1746         break;
1747     case VOUT_CONTROL_WINDOW_MOUSE:
1748         ThreadChangeWindowMouse(vout, &cmd.u.window_mouse);
1749         break;
1750     case VOUT_CONTROL_DISPLAY_FILLED:
1751         ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1752         break;
1753     case VOUT_CONTROL_ZOOM:
1754         ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1755         break;
1756     case VOUT_CONTROL_ASPECT_RATIO:
1757         ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1758         break;
1759     case VOUT_CONTROL_CROP_RATIO:
1760         ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1761         break;
1762     case VOUT_CONTROL_CROP_WINDOW:
1763         ThreadExecuteCropWindow(vout,
1764                 cmd.u.window.x, cmd.u.window.y,
1765                 cmd.u.window.width, cmd.u.window.height);
1766         break;
1767     case VOUT_CONTROL_CROP_BORDER:
1768         ThreadExecuteCropBorder(vout,
1769                 cmd.u.border.left,  cmd.u.border.top,
1770                 cmd.u.border.right, cmd.u.border.bottom);
1771         break;
1772     case VOUT_CONTROL_VIEWPOINT:
1773         ThreadExecuteViewpoint(vout, &cmd.u.viewpoint);
1774         break;
1775     default:
1776         break;
1777     }
1778     vout_control_cmd_Clean(&cmd);
1779     return 0;
1780 }
1781 
1782 /*****************************************************************************
1783  * Thread: video output thread
1784  *****************************************************************************
1785  * Video output thread. This function does only returns when the thread is
1786  * terminated. It handles the pictures arriving in the video heap and the
1787  * display device events.
1788  *****************************************************************************/
Thread(void * object)1789 static void *Thread(void *object)
1790 {
1791     vout_thread_t *vout = object;
1792     vout_thread_sys_t *sys = vout->p;
1793 
1794     mtime_t deadline = VLC_TS_INVALID;
1795     bool wait = false;
1796     for (;;) {
1797         vout_control_cmd_t cmd;
1798 
1799         if (wait)
1800         {
1801             const mtime_t max_deadline = mdate() + 100000;
1802             deadline = deadline <= VLC_TS_INVALID ? max_deadline : __MIN(deadline, max_deadline);
1803         } else {
1804             deadline = VLC_TS_INVALID;
1805         }
1806         while (!vout_control_Pop(&sys->control, &cmd, deadline))
1807             if (ThreadControl(vout, cmd))
1808                 return NULL;
1809 
1810         deadline = VLC_TS_INVALID;
1811         wait = ThreadDisplayPicture(vout, &deadline) != VLC_SUCCESS;
1812 
1813         const bool picture_interlaced = sys->displayed.is_interlaced;
1814 
1815         vout_SetInterlacingState(vout, picture_interlaced);
1816         vout_ManageWrapper(vout);
1817     }
1818 }
1819