]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
LGPL
[vlc] / src / video_output / video_output.c
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$
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_vout_osd.h>
47 #include <vlc_image.h>
48
49 #include <libvlc.h>
50 #include "vout_internal.h"
51 #include "interlacing.h"
52 #include "postprocessing.h"
53 #include "display.h"
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static void *Thread(void *);
59 static void VoutDestructor(vlc_object_t *);
60
61 /* Maximum delay between 2 displayed pictures.
62  * XXX it is needed for now but should be removed in the long term.
63  */
64 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
65
66 /**
67  * Late pictures having a delay higher than this value are thrashed.
68  */
69 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
70
71 /* Better be in advance when awakening than late... */
72 #define VOUT_MWAIT_TOLERANCE (INT64_C(4000))
73
74 /* */
75 static int VoutValidateFormat(video_format_t *dst,
76                               const video_format_t *src)
77 {
78     if (src->i_width <= 0  || src->i_width  > 8192 ||
79         src->i_height <= 0 || src->i_height > 8192)
80         return VLC_EGENERIC;
81     if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
82         return VLC_EGENERIC;
83
84     /* */
85     video_format_Copy(dst, src);
86     dst->i_chroma = vlc_fourcc_GetCodec(VIDEO_ES, src->i_chroma);
87     vlc_ureduce( &dst->i_sar_num, &dst->i_sar_den,
88                  src->i_sar_num,  src->i_sar_den, 50000 );
89     if (dst->i_sar_num <= 0 || dst->i_sar_den <= 0) {
90         dst->i_sar_num = 1;
91         dst->i_sar_den = 1;
92     }
93     video_format_FixRgb(dst);
94     return VLC_SUCCESS;
95 }
96 static void VideoFormatCopyCropAr(video_format_t *dst,
97                                   const video_format_t *src)
98 {
99     video_format_CopyCrop(dst, src);
100     dst->i_sar_num = src->i_sar_num;
101     dst->i_sar_den = src->i_sar_den;
102 }
103 static bool VideoFormatIsCropArEqual(video_format_t *dst,
104                                      const video_format_t *src)
105 {
106     return dst->i_sar_num * src->i_sar_den == dst->i_sar_den * src->i_sar_num &&
107            dst->i_x_offset       == src->i_x_offset &&
108            dst->i_y_offset       == src->i_y_offset &&
109            dst->i_visible_width  == src->i_visible_width &&
110            dst->i_visible_height == src->i_visible_height;
111 }
112
113 static vout_thread_t *VoutCreate(vlc_object_t *object,
114                                  const vout_configuration_t *cfg)
115 {
116     video_format_t original;
117     if (VoutValidateFormat(&original, cfg->fmt))
118         return NULL;
119
120     /* Allocate descriptor */
121     vout_thread_t *vout = vlc_custom_create(object,
122                                             sizeof(*vout) + sizeof(*vout->p),
123                                             "video output");
124     if (!vout) {
125         video_format_Clean(&original);
126         return NULL;
127     }
128
129     /* */
130     vout->p = (vout_thread_sys_t*)&vout[1];
131
132     vout->p->original = original;
133     vout->p->dpb_size = cfg->dpb_size;
134
135     vout_control_Init(&vout->p->control);
136     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
137
138     vout_statistic_Init(&vout->p->statistic);
139
140     vout_snapshot_Init(&vout->p->snapshot);
141
142     /* Initialize locks */
143     vlc_mutex_init(&vout->p->picture_lock);
144     vlc_mutex_init(&vout->p->filter.lock);
145     vlc_mutex_init(&vout->p->spu_lock);
146
147     /* Initialize subpicture unit */
148     vout->p->spu = spu_Create(vout);
149
150     /* Take care of some "interface/control" related initialisations */
151     vout_IntfInit(vout);
152
153     vout->p->title.show     = var_GetBool(vout, "video-title-show");
154     vout->p->title.timeout  = var_GetInteger(vout, "video-title-timeout");
155     vout->p->title.position = var_GetInteger(vout, "video-title-position");
156
157     /* Get splitter name if present */
158     char *splitter_name = var_InheritString(vout, "video-splitter");
159     if (splitter_name && *splitter_name) {
160         vout->p->splitter_name = splitter_name;
161     } else {
162         free(splitter_name);
163     }
164
165     /* */
166     vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
167
168     /* */
169     vlc_object_set_destructor(vout, VoutDestructor);
170
171     /* */
172     if (vlc_clone(&vout->p->thread, Thread, vout,
173                   VLC_THREAD_PRIORITY_OUTPUT)) {
174         spu_Destroy(vout->p->spu);
175         vlc_object_release(vout);
176         return NULL;
177     }
178
179     vout_control_WaitEmpty(&vout->p->control);
180
181     if (vout->p->dead) {
182         msg_Err(vout, "video output creation failed");
183         vout_CloseAndRelease(vout);
184         return NULL;
185     }
186
187     vout->p->input = cfg->input;
188     if (vout->p->input)
189         spu_Attach(vout->p->spu, vout->p->input, true);
190
191     return vout;
192 }
193
194 vout_thread_t *(vout_Request)(vlc_object_t *object,
195                               const vout_configuration_t *cfg)
196 {
197     vout_thread_t *vout = cfg->vout;
198     if (cfg->change_fmt && !cfg->fmt) {
199         if (vout)
200             vout_CloseAndRelease(vout);
201         return NULL;
202     }
203
204     /* If a vout is provided, try reusing it */
205     if (vout) {
206         if (vout->p->input != cfg->input) {
207             if (vout->p->input)
208                 spu_Attach(vout->p->spu, vout->p->input, false);
209             vout->p->input = cfg->input;
210             if (vout->p->input)
211                 spu_Attach(vout->p->spu, vout->p->input, true);
212         }
213
214         if (cfg->change_fmt) {
215             vout_control_cmd_t cmd;
216             vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
217             cmd.u.cfg = cfg;
218
219             vout_control_Push(&vout->p->control, &cmd);
220             vout_control_WaitEmpty(&vout->p->control);
221         }
222
223         if (!vout->p->dead) {
224             msg_Dbg(object, "reusing provided vout");
225             return vout;
226         }
227         vout_CloseAndRelease(vout);
228
229         msg_Warn(object, "cannot reuse provided vout");
230     }
231     return VoutCreate(object, cfg);
232 }
233
234 void vout_Close(vout_thread_t *vout)
235 {
236     assert(vout);
237
238     if (vout->p->input)
239         spu_Attach(vout->p->spu, vout->p->input, false);
240
241     vout_snapshot_End(&vout->p->snapshot);
242
243     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
244     vlc_join(vout->p->thread, NULL);
245
246     vlc_mutex_lock(&vout->p->spu_lock);
247     spu_Destroy(vout->p->spu);
248     vout->p->spu = NULL;
249     vlc_mutex_unlock(&vout->p->spu_lock);
250 }
251
252 /* */
253 static void VoutDestructor(vlc_object_t *object)
254 {
255     vout_thread_t *vout = (vout_thread_t *)object;
256
257     /* Make sure the vout was stopped first */
258     //assert(!vout->p_module);
259
260     free(vout->p->splitter_name);
261
262     /* Destroy the locks */
263     vlc_mutex_destroy(&vout->p->spu_lock);
264     vlc_mutex_destroy(&vout->p->picture_lock);
265     vlc_mutex_destroy(&vout->p->filter.lock);
266     vout_control_Clean(&vout->p->control);
267
268     /* */
269     vout_statistic_Clean(&vout->p->statistic);
270
271     /* */
272     vout_snapshot_Clean(&vout->p->snapshot);
273
274     video_format_Clean(&vout->p->original);
275 }
276
277 /* */
278 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
279 {
280     vout_control_cmd_t cmd;
281     vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
282     cmd.u.pause.is_on = is_paused;
283     cmd.u.pause.date  = date;
284     vout_control_Push(&vout->p->control, &cmd);
285
286     vout_control_WaitEmpty(&vout->p->control);
287 }
288
289 void vout_GetResetStatistic(vout_thread_t *vout, int *displayed, int *lost)
290 {
291     vout_statistic_GetReset( &vout->p->statistic, displayed, lost );
292 }
293
294 void vout_Flush(vout_thread_t *vout, mtime_t date)
295 {
296     vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
297     vout_control_WaitEmpty(&vout->p->control);
298 }
299
300 void vout_Reset(vout_thread_t *vout)
301 {
302     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
303     vout_control_WaitEmpty(&vout->p->control);
304 }
305
306 bool vout_IsEmpty(vout_thread_t *vout)
307 {
308     vlc_mutex_lock(&vout->p->picture_lock);
309
310     picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
311     if (picture)
312         picture_Release(picture);
313
314     vlc_mutex_unlock(&vout->p->picture_lock);
315
316     return !picture;
317 }
318
319 void vout_FixLeaks( vout_thread_t *vout )
320 {
321     vlc_mutex_lock(&vout->p->picture_lock);
322
323     picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
324     if (!picture) {
325         picture = picture_pool_Get(vout->p->decoder_pool);
326     }
327
328     if (picture) {
329         picture_Release(picture);
330         /* Not all pictures has been displayed yet or some are
331          * free */
332         vlc_mutex_unlock(&vout->p->picture_lock);
333         return;
334     }
335
336     /* There is no reason that no pictures are available, force one
337      * from the pool, becarefull with it though */
338     msg_Err(vout, "pictures leaked, trying to workaround");
339
340     /* */
341     picture_pool_NonEmpty(vout->p->decoder_pool, false);
342
343     vlc_mutex_unlock(&vout->p->picture_lock);
344 }
345 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
346 {
347     vout_control_cmd_t cmd;
348     vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
349     cmd.u.time_ptr = duration;
350
351     vout_control_Push(&vout->p->control, &cmd);
352     vout_control_WaitEmpty(&vout->p->control);
353 }
354
355 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
356 {
357     assert(title);
358     vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
359 }
360
361 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
362 {
363     vout_control_cmd_t cmd;
364     vout_control_cmd_Init(&cmd, VOUT_CONTROL_SUBPICTURE);
365     cmd.u.subpicture = subpic;
366
367     vout_control_Push(&vout->p->control, &cmd);
368 }
369 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
370 {
371     int channel = SPU_DEFAULT_CHANNEL;
372
373     vlc_mutex_lock(&vout->p->spu_lock);
374     if (vout->p->spu)
375         channel = spu_RegisterChannel(vout->p->spu);
376     vlc_mutex_unlock(&vout->p->spu_lock);
377
378     return channel;
379 }
380 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
381 {
382     vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_FLUSH_SUBPICTURE,
383                              channel);
384 }
385
386 /**
387  * It retreives a picture from the vout or NULL if no pictures are
388  * available yet.
389  *
390  * You MUST call vout_PutPicture or vout_ReleasePicture on it.
391  *
392  * You may use vout_HoldPicture(paired with vout_ReleasePicture) to keep a
393  * read-only reference.
394  */
395 picture_t *vout_GetPicture(vout_thread_t *vout)
396 {
397     /* Get lock */
398     vlc_mutex_lock(&vout->p->picture_lock);
399     picture_t *picture = picture_pool_Get(vout->p->decoder_pool);
400     if (picture) {
401         picture_Reset(picture);
402         VideoFormatCopyCropAr(&picture->format, &vout->p->original);
403     }
404     vlc_mutex_unlock(&vout->p->picture_lock);
405
406     return picture;
407 }
408
409 /**
410  * It gives to the vout a picture to be displayed.
411  *
412  * The given picture MUST comes from vout_GetPicture.
413  *
414  * Becareful, after vout_PutPicture is called, picture_t::p_next cannot be
415  * read/used.
416  */
417 void vout_PutPicture(vout_thread_t *vout, picture_t *picture)
418 {
419     vlc_mutex_lock(&vout->p->picture_lock);
420
421     picture->p_next = NULL;
422     picture_fifo_Push(vout->p->decoder_fifo, picture);
423
424     vlc_mutex_unlock(&vout->p->picture_lock);
425
426     vout_control_Wake(&vout->p->control);
427 }
428
429 /**
430  * It releases a picture retreived by vout_GetPicture.
431  */
432 void vout_ReleasePicture(vout_thread_t *vout, picture_t *picture)
433 {
434     vlc_mutex_lock(&vout->p->picture_lock);
435
436     picture_Release(picture);
437
438     vlc_mutex_unlock(&vout->p->picture_lock);
439
440     vout_control_Wake(&vout->p->control);
441 }
442
443 /**
444  * It increment the reference counter of a picture retreived by
445  * vout_GetPicture.
446  */
447 void vout_HoldPicture(vout_thread_t *vout, picture_t *picture)
448 {
449     vlc_mutex_lock(&vout->p->picture_lock);
450
451     picture_Hold(picture);
452
453     vlc_mutex_unlock(&vout->p->picture_lock);
454 }
455
456 /* */
457 int vout_GetSnapshot(vout_thread_t *vout,
458                      block_t **image_dst, picture_t **picture_dst,
459                      video_format_t *fmt,
460                      const char *type, mtime_t timeout)
461 {
462     picture_t *picture = vout_snapshot_Get(&vout->p->snapshot, timeout);
463     if (!picture) {
464         msg_Err(vout, "Failed to grab a snapshot");
465         return VLC_EGENERIC;
466     }
467
468     if (image_dst) {
469         vlc_fourcc_t codec = VLC_CODEC_PNG;
470         if (type && image_Type2Fourcc(type))
471             codec = image_Type2Fourcc(type);
472
473         const int override_width  = var_GetInteger(vout, "snapshot-width");
474         const int override_height = var_GetInteger(vout, "snapshot-height");
475
476         if (picture_Export(VLC_OBJECT(vout), image_dst, fmt,
477                            picture, codec, override_width, override_height)) {
478             msg_Err(vout, "Failed to convert image for snapshot");
479             picture_Release(picture);
480             return VLC_EGENERIC;
481         }
482     }
483     if (picture_dst)
484         *picture_dst = picture;
485     else
486         picture_Release(picture);
487     return VLC_SUCCESS;
488 }
489
490 /* vout_Control* are usable by anyone at anytime */
491 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
492 {
493     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
494                           fullscreen);
495 }
496 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
497 {
498     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
499                           is_on_top);
500 }
501 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
502 {
503     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
504                           is_filled);
505 }
506 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
507 {
508     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
509                           num, den);
510 }
511 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
512                                          unsigned num, unsigned den)
513 {
514     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
515                           num, den);
516 }
517 void vout_ControlChangeCropRatio(vout_thread_t *vout,
518                                  unsigned num, unsigned den)
519 {
520     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
521                           num, den);
522 }
523 void vout_ControlChangeCropWindow(vout_thread_t *vout,
524                                   int x, int y, int width, int height)
525 {
526     vout_control_cmd_t cmd;
527     vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
528     cmd.u.window.x      = __MAX(x, 0);
529     cmd.u.window.y      = __MAX(y, 0);
530     cmd.u.window.width  = __MAX(width, 0);
531     cmd.u.window.height = __MAX(height, 0);
532
533     vout_control_Push(&vout->p->control, &cmd);
534 }
535 void vout_ControlChangeCropBorder(vout_thread_t *vout,
536                                   int left, int top, int right, int bottom)
537 {
538     vout_control_cmd_t cmd;
539     vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
540     cmd.u.border.left   = __MAX(left, 0);
541     cmd.u.border.top    = __MAX(top, 0);
542     cmd.u.border.right  = __MAX(right, 0);
543     cmd.u.border.bottom = __MAX(bottom, 0);
544
545     vout_control_Push(&vout->p->control, &cmd);
546 }
547 void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
548 {
549     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
550                             filters);
551 }
552 void vout_ControlChangeSubSources(vout_thread_t *vout, const char *filters)
553 {
554     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_SOURCES,
555                             filters);
556 }
557 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
558 {
559     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
560                             filters);
561 }
562 void vout_ControlChangeSubMargin(vout_thread_t *vout, int margin)
563 {
564     vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_MARGIN,
565                              margin);
566 }
567
568 /* */
569 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
570 {
571     /* Load configuration */
572     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
573     cfg->display.title = title;
574     const int display_width = var_CreateGetInteger(vout, "width");
575     const int display_height = var_CreateGetInteger(vout, "height");
576     cfg->display.width   = display_width > 0  ? display_width  : 0;
577     cfg->display.height  = display_height > 0 ? display_height : 0;
578     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
579     unsigned msar_num, msar_den;
580     if (var_InheritURational(vout, &msar_num, &msar_den, "monitor-par") ||
581         msar_num <= 0 || msar_den <= 0) {
582         msar_num = 1;
583         msar_den = 1;
584     }
585     cfg->display.sar.num = msar_num;
586     cfg->display.sar.den = msar_den;
587     unsigned zoom_den = 1000;
588     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
589     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
590     cfg->zoom.num = zoom_num;
591     cfg->zoom.den = zoom_den;
592     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
593     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
594     const int align_mask = var_CreateGetInteger(vout, "align");
595     if (align_mask & 0x1)
596         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
597     else if (align_mask & 0x2)
598         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
599     if (align_mask & 0x4)
600         cfg->align.vertical = VOUT_DISPLAY_ALIGN_TOP;
601     else if (align_mask & 0x8)
602         cfg->align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
603 }
604
605 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
606                                       const vout_window_cfg_t *cfg)
607 {
608     VLC_UNUSED(vd);
609     vout_window_cfg_t cfg_override = *cfg;
610
611     if (!var_InheritBool( vout, "embedded-video"))
612         cfg_override.is_standalone = true;
613
614     if (vout->p->window.is_unused && vout->p->window.object) {
615         assert(!vout->p->splitter_name);
616         if (!cfg_override.is_standalone == !vout->p->window.cfg.is_standalone &&
617             cfg_override.type           == vout->p->window.cfg.type) {
618             /* Reuse the stored window */
619             msg_Dbg(vout, "Reusing previous vout window");
620             vout_window_t *window = vout->p->window.object;
621             if (cfg_override.width  != vout->p->window.cfg.width ||
622                 cfg_override.height != vout->p->window.cfg.height)
623                 vout_window_SetSize(window,
624                                     cfg_override.width, cfg_override.height);
625             vout->p->window.is_unused = false;
626             vout->p->window.cfg       = cfg_override;
627             return window;
628         }
629
630         vout_window_Delete(vout->p->window.object);
631         vout->p->window.is_unused = true;
632         vout->p->window.object    = NULL;
633     }
634
635     vout_window_t *window = vout_window_New(VLC_OBJECT(vout), "$window",
636                                             &cfg_override);
637     if (!window)
638         return NULL;
639     if (!vout->p->splitter_name) {
640         vout->p->window.is_unused = false;
641         vout->p->window.cfg       = cfg_override;
642         vout->p->window.object    = window;
643     }
644     return window;
645 }
646
647 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
648                               vout_window_t *window)
649 {
650     VLC_UNUSED(vd);
651     if (!vout->p->window.is_unused && vout->p->window.object == window) {
652         vout->p->window.is_unused = true;
653     } else if (vout->p->window.is_unused && vout->p->window.object && !window) {
654         vout_window_Delete(vout->p->window.object);
655         vout->p->window.is_unused = true;
656         vout->p->window.object    = NULL;
657     } else if (window) {
658         vout_window_Delete(window);
659     }
660 }
661
662 /* */
663 static picture_t *VoutVideoFilterInteractiveNewPicture(filter_t *filter)
664 {
665     vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
666
667     picture_t *picture = picture_pool_Get(vout->p->private_pool);
668     if (picture) {
669         picture_Reset(picture);
670         VideoFormatCopyCropAr(&picture->format, &filter->fmt_out.video);
671     }
672     return picture;
673 }
674 static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
675 {
676     vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
677
678     vlc_assert_locked(&vout->p->filter.lock);
679     if (filter_chain_GetLength(vout->p->filter.chain_interactive) == 0)
680         return VoutVideoFilterInteractiveNewPicture(filter);
681
682     return picture_NewFromFormat(&filter->fmt_out.video);
683 }
684 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
685 {
686     VLC_UNUSED(filter);
687     picture_Release(picture);
688 }
689 static int VoutVideoFilterStaticAllocationSetup(filter_t *filter, void *data)
690 {
691     filter->pf_video_buffer_new = VoutVideoFilterStaticNewPicture;
692     filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
693     filter->p_owner             = data; /* vout */
694     return VLC_SUCCESS;
695 }
696 static int VoutVideoFilterInteractiveAllocationSetup(filter_t *filter, void *data)
697 {
698     filter->pf_video_buffer_new = VoutVideoFilterInteractiveNewPicture;
699     filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
700     filter->p_owner             = data; /* vout */
701     return VLC_SUCCESS;
702 }
703 static void ThreadFilterFlush(vout_thread_t *vout, bool is_locked)
704 {
705     if (vout->p->displayed.current)
706         picture_Release( vout->p->displayed.current );
707     vout->p->displayed.current = NULL;
708
709     if (vout->p->displayed.next)
710         picture_Release( vout->p->displayed.next );
711     vout->p->displayed.next = NULL;
712
713     if (!is_locked)
714         vlc_mutex_lock(&vout->p->filter.lock);
715     filter_chain_VideoFlush(vout->p->filter.chain_static);
716     filter_chain_VideoFlush(vout->p->filter.chain_interactive);
717     if (!is_locked)
718         vlc_mutex_unlock(&vout->p->filter.lock);
719 }
720
721 typedef struct {
722     char           *name;
723     config_chain_t *cfg;
724 } vout_filter_t;
725
726 static void ThreadChangeFilters(vout_thread_t *vout,
727                                 const video_format_t *source,
728                                 const char *filters,
729                                 bool is_locked)
730 {
731     ThreadFilterFlush(vout, is_locked);
732
733     vlc_array_t array_static;
734     vlc_array_t array_interactive;
735
736     vlc_array_init(&array_static);
737     vlc_array_init(&array_interactive);
738     char *current = filters ? strdup(filters) : NULL;
739     while (current) {
740         config_chain_t *cfg;
741         char *name;
742         char *next = config_ChainCreate(&name, &cfg, current);
743
744         if (name && *name) {
745             vout_filter_t *e = xmalloc(sizeof(*e));
746             e->name = name;
747             e->cfg  = cfg;
748             if (!strcmp(e->name, "deinterlace") ||
749                 !strcmp(e->name, "postproc")) {
750                 vlc_array_append(&array_static, e);
751             } else {
752                 vlc_array_append(&array_interactive, e);
753             }
754         } else {
755             if (cfg)
756                 config_ChainDestroy(cfg);
757             free(name);
758         }
759         free(current);
760         current = next;
761     }
762
763     if (!is_locked)
764         vlc_mutex_lock(&vout->p->filter.lock);
765
766     es_format_t fmt_target;
767     es_format_InitFromVideo(&fmt_target, source ? source : &vout->p->filter.format);
768
769     es_format_t fmt_current = fmt_target;
770
771     for (int a = 0; a < 2; a++) {
772         vlc_array_t    *array = a == 0 ? &array_static :
773                                          &array_interactive;
774         filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
775                                          vout->p->filter.chain_interactive;
776
777         filter_chain_Reset(chain, &fmt_current, &fmt_current);
778         for (int i = 0; i < vlc_array_count(array); i++) {
779             vout_filter_t *e = vlc_array_item_at_index(array, i);
780             msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
781             if (!filter_chain_AppendFilter(chain, e->name, e->cfg, NULL, NULL)) {
782                 msg_Err(vout, "Failed to add filter '%s'", e->name);
783                 config_ChainDestroy(e->cfg);
784             }
785             free(e->name);
786             free(e);
787         }
788         fmt_current = *filter_chain_GetFmtOut(chain);
789         vlc_array_clear(array);
790     }
791     VideoFormatCopyCropAr(&fmt_target.video, &fmt_current.video);
792     if (!es_format_IsSimilar(&fmt_current, &fmt_target)) {
793         msg_Dbg(vout, "Adding a filter to compensate for format changes");
794         if (!filter_chain_AppendFilter(vout->p->filter.chain_interactive, NULL, NULL,
795                                        &fmt_current, &fmt_target)) {
796             msg_Err(vout, "Failed to compensate for the format changes, removing all filters");
797             filter_chain_Reset(vout->p->filter.chain_static,      &fmt_target, &fmt_target);
798             filter_chain_Reset(vout->p->filter.chain_interactive, &fmt_target, &fmt_target);
799         }
800     }
801
802     if (vout->p->filter.configuration != filters) {
803         free(vout->p->filter.configuration);
804         vout->p->filter.configuration = filters ? strdup(filters) : NULL;
805     }
806     if (source) {
807         video_format_Clean(&vout->p->filter.format);
808         video_format_Copy(&vout->p->filter.format, source);
809     }
810
811     if (!is_locked)
812         vlc_mutex_unlock(&vout->p->filter.lock);
813 }
814
815
816 /* */
817 static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_late_dropped)
818 {
819     int lost_count = 0;
820
821     vlc_mutex_lock(&vout->p->filter.lock);
822
823     picture_t *picture = filter_chain_VideoFilter(vout->p->filter.chain_static, NULL);
824     assert(!reuse || !picture);
825
826     while (!picture) {
827         picture_t *decoded;
828         if (reuse && vout->p->displayed.decoded) {
829             decoded = picture_Hold(vout->p->displayed.decoded);
830         } else {
831             decoded = picture_fifo_Pop(vout->p->decoder_fifo);
832             if (is_late_dropped && decoded && !decoded->b_force) {
833                 const mtime_t predicted = mdate() + 0; /* TODO improve */
834                 const mtime_t late = predicted - decoded->date;
835                 if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
836                     msg_Warn(vout, "picture is too late to be displayed (missing %d ms)", (int)(late/1000));
837                     picture_Release(decoded);
838                     lost_count++;
839                     continue;
840                 } else if (late > 0) {
841                     msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
842                 }
843             }
844             if (decoded &&
845                 !VideoFormatIsCropArEqual(&decoded->format, &vout->p->filter.format))
846                 ThreadChangeFilters(vout, &decoded->format, vout->p->filter.configuration, true);
847         }
848         if (!decoded)
849             break;
850         reuse = false;
851
852         if (vout->p->displayed.decoded)
853             picture_Release(vout->p->displayed.decoded);
854
855         vout->p->displayed.decoded       = picture_Hold(decoded);
856         vout->p->displayed.timestamp     = decoded->date;
857         vout->p->displayed.is_interlaced = !decoded->b_progressive;
858         vout->p->displayed.qtype         = decoded->i_qtype;
859
860         picture = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
861     }
862
863     vlc_mutex_unlock(&vout->p->filter.lock);
864
865     vout_statistic_Update(&vout->p->statistic, 0, lost_count);
866     if (!picture)
867         return VLC_EGENERIC;
868
869     assert(!vout->p->displayed.next);
870     if (!vout->p->displayed.current)
871         vout->p->displayed.current = picture;
872     else
873         vout->p->displayed.next    = picture;
874     return VLC_SUCCESS;
875 }
876
877 static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
878 {
879     vout_thread_sys_t *sys = vout->p;
880     vout_display_t *vd = vout->p->display.vd;
881
882     picture_t *torender = picture_Hold(vout->p->displayed.current);
883
884     vout_chrono_Start(&vout->p->render);
885
886     vlc_mutex_lock(&vout->p->filter.lock);
887     picture_t *filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, torender);
888     vlc_mutex_unlock(&vout->p->filter.lock);
889
890     if (!filtered)
891         return VLC_EGENERIC;
892
893     if (filtered->date != vout->p->displayed.current->date)
894         msg_Warn(vout, "Unsupported timestamp modifications done by chain_interactive");
895
896     /*
897      * Get the subpicture to be displayed
898      */
899     const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
900     mtime_t render_subtitle_date;
901     if (vout->p->pause.is_on)
902         render_subtitle_date = vout->p->pause.date;
903     else
904         render_subtitle_date = filtered->date > 1 ? filtered->date : mdate();
905     mtime_t render_osd_date = mdate(); /* FIXME wrong */
906
907     /*
908      * Get the subpicture to be displayed
909      */
910     const bool do_dr_spu = !do_snapshot &&
911                            vd->info.subpicture_chromas &&
912                            *vd->info.subpicture_chromas != 0;
913     const bool do_early_spu = !do_dr_spu &&
914                               (vd->info.is_slow ||
915                                sys->display.use_dr ||
916                                do_snapshot ||
917                                !vout_IsDisplayFiltered(vd) ||
918                                vd->fmt.i_width * vd->fmt.i_height <= vd->source.i_width * vd->source.i_height);
919
920     const vlc_fourcc_t *subpicture_chromas;
921     video_format_t fmt_spu;
922     if (do_dr_spu) {
923         vout_display_place_t place;
924         vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
925
926         fmt_spu = vd->source;
927         if (fmt_spu.i_width * fmt_spu.i_height < place.width * place.height) {
928             fmt_spu.i_sar_num = vd->cfg->display.sar.num;
929             fmt_spu.i_sar_den = vd->cfg->display.sar.den;
930             fmt_spu.i_width          =
931             fmt_spu.i_visible_width  = place.width;
932             fmt_spu.i_height         =
933             fmt_spu.i_visible_height = place.height;
934         }
935         subpicture_chromas = vd->info.subpicture_chromas;
936     } else {
937         if (do_early_spu) {
938             fmt_spu = vd->source;
939         } else {
940             fmt_spu = vd->fmt;
941             fmt_spu.i_sar_num = vd->cfg->display.sar.num;
942             fmt_spu.i_sar_den = vd->cfg->display.sar.den;
943         }
944         subpicture_chromas = NULL;
945
946         if (vout->p->spu_blend &&
947             vout->p->spu_blend->fmt_out.video.i_chroma != fmt_spu.i_chroma) {
948             filter_DeleteBlend(vout->p->spu_blend);
949             vout->p->spu_blend = NULL;
950             vout->p->spu_blend_chroma = 0;
951         }
952         if (!vout->p->spu_blend && vout->p->spu_blend_chroma != fmt_spu.i_chroma) {
953             vout->p->spu_blend_chroma = fmt_spu.i_chroma;
954             vout->p->spu_blend = filter_NewBlend(VLC_OBJECT(vout), &fmt_spu);
955             if (!vout->p->spu_blend)
956                 msg_Err(vout, "Failed to create blending filter, OSD/Subtitles will not work");
957         }
958     }
959
960     subpicture_t *subpic = spu_Render(vout->p->spu,
961                                       subpicture_chromas, &fmt_spu,
962                                       &vd->source,
963                                       render_subtitle_date, render_osd_date,
964                                       do_snapshot);
965     /*
966      * Perform rendering
967      *
968      * We have to:
969      * - be sure to end up with a direct buffer.
970      * - blend subtitles, and in a fast access buffer
971      */
972     bool is_direct = vout->p->decoder_pool == vout->p->display_pool;
973     picture_t *todisplay = filtered;
974     if (do_early_spu && subpic) {
975         todisplay = picture_pool_Get(vout->p->private_pool);
976         if (todisplay) {
977             VideoFormatCopyCropAr(&todisplay->format, &filtered->format);
978             picture_Copy(todisplay, filtered);
979             if (vout->p->spu_blend)
980                 picture_BlendSubpicture(todisplay, vout->p->spu_blend, subpic);
981         }
982         picture_Release(filtered);
983         subpicture_Delete(subpic);
984         subpic = NULL;
985
986         if (!todisplay)
987             return VLC_EGENERIC;
988     }
989
990     picture_t *direct;
991     if (!is_direct && todisplay) {
992         direct = picture_pool_Get(vout->p->display_pool);
993         if (direct) {
994             VideoFormatCopyCropAr(&direct->format, &todisplay->format);
995             picture_Copy(direct, todisplay);
996         }
997         picture_Release(todisplay);
998     } else {
999         direct = todisplay;
1000     }
1001
1002     if (!direct) {
1003         if (subpic)
1004             subpicture_Delete(subpic);
1005         return VLC_EGENERIC;
1006     }
1007
1008     /*
1009      * Take a snapshot if requested
1010      */
1011     if (do_snapshot)
1012         vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
1013
1014     /* Render the direct buffer */
1015     assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
1016     vout_UpdateDisplaySourceProperties(vd, &direct->format);
1017     if (sys->display.use_dr) {
1018         vout_display_Prepare(vd, direct, subpic);
1019     } else {
1020         sys->display.filtered = vout_FilterDisplay(vd, direct);
1021         if (sys->display.filtered) {
1022             if (!do_dr_spu && !do_early_spu && vout->p->spu_blend && subpic)
1023                 picture_BlendSubpicture(sys->display.filtered, vout->p->spu_blend, subpic);
1024             vout_display_Prepare(vd, sys->display.filtered, do_dr_spu ? subpic : NULL);
1025         }
1026         if (!do_dr_spu && subpic)
1027             subpicture_Delete(subpic);
1028         if (!sys->display.filtered)
1029             return VLC_EGENERIC;
1030     }
1031
1032     vout_chrono_Stop(&vout->p->render);
1033 #if 0
1034         {
1035         static int i = 0;
1036         if (((i++)%10) == 0)
1037             msg_Info(vout, "render: avg %d ms var %d ms",
1038                      (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
1039         }
1040 #endif
1041
1042     /* Wait the real date (for rendering jitter) */
1043 #if 0
1044     mtime_t delay = direct->date - mdate();
1045     if (delay < 1000)
1046         msg_Warn(vout, "picture is late (%lld ms)", delay / 1000);
1047 #endif
1048     if (!is_forced)
1049         mwait(direct->date);
1050
1051     /* Display the direct buffer returned by vout_RenderPicture */
1052     vout->p->displayed.date = mdate();
1053     vout_display_Display(vd,
1054                          sys->display.filtered ? sys->display.filtered
1055                                                 : direct,
1056                          subpic);
1057     sys->display.filtered = NULL;
1058
1059     vout_statistic_Update(&vout->p->statistic, 1, 0);
1060
1061     return VLC_SUCCESS;
1062 }
1063
1064 static int ThreadDisplayPicture(vout_thread_t *vout,
1065                                 bool now, mtime_t *deadline)
1066 {
1067     bool is_late_dropped = vout->p->is_late_dropped && !vout->p->pause.is_on && !now;
1068     bool first = !vout->p->displayed.current;
1069     if (first && ThreadDisplayPreparePicture(vout, true, is_late_dropped)) /* FIXME not sure it is ok */
1070         return VLC_EGENERIC;
1071     if (!vout->p->pause.is_on || now) {
1072         while (!vout->p->displayed.next) {
1073             if (ThreadDisplayPreparePicture(vout, false, is_late_dropped)) {
1074                 break;
1075             }
1076         }
1077     }
1078
1079     const mtime_t date = mdate();
1080     const mtime_t render_delay = vout_chrono_GetHigh(&vout->p->render) + VOUT_MWAIT_TOLERANCE;
1081
1082     mtime_t date_next = VLC_TS_INVALID;
1083     if (!vout->p->pause.is_on && vout->p->displayed.next)
1084         date_next = vout->p->displayed.next->date - render_delay;
1085
1086     /* FIXME/XXX we must redisplay the last decoded picture (because
1087      * of potential vout updated, or filters update or SPU update)
1088      * For now a high update period is needed but it coulmd be removed
1089      * if and only if:
1090      * - vout module emits events from theselves.
1091      * - *and* SPU is modified to emit an event or a deadline when needed.
1092      *
1093      * So it will be done latter.
1094      */
1095     mtime_t date_refresh = VLC_TS_INVALID;
1096     if (vout->p->displayed.date > VLC_TS_INVALID)
1097         date_refresh = vout->p->displayed.date + VOUT_REDISPLAY_DELAY - render_delay;
1098
1099     bool drop = now;
1100     if (date_next != VLC_TS_INVALID)
1101         drop |= date_next + 0 <= date;
1102
1103     bool refresh = false;
1104     if (date_refresh > VLC_TS_INVALID)
1105         refresh = date_refresh <= date;
1106
1107     if (!first && !refresh && !drop) {
1108         if (date_next != VLC_TS_INVALID && date_refresh != VLC_TS_INVALID)
1109             *deadline = __MIN(date_next, date_refresh);
1110         else if (date_next != VLC_TS_INVALID)
1111             *deadline = date_next;
1112         else if (date_refresh != VLC_TS_INVALID)
1113             *deadline = date_refresh;
1114         return VLC_EGENERIC;
1115     }
1116
1117     if (drop) {
1118         picture_Release(vout->p->displayed.current);
1119         vout->p->displayed.current = vout->p->displayed.next;
1120         vout->p->displayed.next    = NULL;
1121     }
1122     if (!vout->p->displayed.current)
1123         return VLC_EGENERIC;
1124
1125     bool is_forced = now || (!drop && refresh) || vout->p->displayed.current->b_force;
1126     return ThreadDisplayRenderPicture(vout, is_forced);
1127 }
1128
1129 static void ThreadManage(vout_thread_t *vout,
1130                          mtime_t *deadline,
1131                          vout_interlacing_support_t *interlacing,
1132                          vout_postprocessing_support_t *postprocessing)
1133 {
1134     vlc_mutex_lock(&vout->p->picture_lock);
1135
1136     *deadline = VLC_TS_INVALID;
1137     for (;;) {
1138         if (ThreadDisplayPicture(vout, false, deadline))
1139             break;
1140     }
1141
1142     const int  picture_qtype      = vout->p->displayed.qtype;
1143     const bool picture_interlaced = vout->p->displayed.is_interlaced;
1144
1145     vlc_mutex_unlock(&vout->p->picture_lock);
1146
1147     /* Post processing */
1148     vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
1149
1150     /* Deinterlacing */
1151     vout_SetInterlacingState(vout, interlacing, picture_interlaced);
1152
1153     vout_ManageWrapper(vout);
1154 }
1155
1156 static void ThreadDisplaySubpicture(vout_thread_t *vout,
1157                                     subpicture_t *subpicture)
1158 {
1159     spu_PutSubpicture(vout->p->spu, subpicture);
1160 }
1161
1162 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
1163 {
1164     spu_ClearChannel(vout->p->spu, channel);
1165 }
1166
1167 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
1168 {
1169     if (!vout->p->title.show)
1170         return;
1171
1172     vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
1173                  vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
1174                  string);
1175 }
1176
1177 static void ThreadChangeSubSources(vout_thread_t *vout, const char *filters)
1178 {
1179     spu_ChangeSources(vout->p->spu, filters);
1180 }
1181
1182 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
1183 {
1184     spu_ChangeFilters(vout->p->spu, filters);
1185 }
1186
1187 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
1188 {
1189     spu_ChangeMargin(vout->p->spu, margin);
1190 }
1191
1192 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
1193 {
1194     assert(!vout->p->pause.is_on || !is_paused);
1195
1196     if (vout->p->pause.is_on) {
1197         const mtime_t duration = date - vout->p->pause.date;
1198
1199         if (vout->p->step.timestamp > VLC_TS_INVALID)
1200             vout->p->step.timestamp += duration;
1201         if (vout->p->step.last > VLC_TS_INVALID)
1202             vout->p->step.last += duration;
1203         picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
1204         if (vout->p->displayed.decoded)
1205             vout->p->displayed.decoded->date += duration;
1206         spu_OffsetSubtitleDate(vout->p->spu, duration);
1207
1208         ThreadFilterFlush(vout, false);
1209     } else {
1210         vout->p->step.timestamp = VLC_TS_INVALID;
1211         vout->p->step.last      = VLC_TS_INVALID;
1212     }
1213     vout->p->pause.is_on = is_paused;
1214     vout->p->pause.date  = date;
1215 }
1216
1217 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
1218 {
1219     vout->p->step.timestamp = VLC_TS_INVALID;
1220     vout->p->step.last      = VLC_TS_INVALID;
1221
1222     ThreadFilterFlush(vout, false); /* FIXME too much */
1223
1224     picture_t *last = vout->p->displayed.decoded;
1225     if (last) {
1226         if (( below && last->date <= date) ||
1227             (!below && last->date >= date)) {
1228             picture_Release(last);
1229
1230             vout->p->displayed.decoded   = NULL;
1231             vout->p->displayed.date      = VLC_TS_INVALID;
1232             vout->p->displayed.timestamp = VLC_TS_INVALID;
1233         }
1234     }
1235
1236     picture_fifo_Flush(vout->p->decoder_fifo, date, below);
1237 }
1238
1239 static void ThreadReset(vout_thread_t *vout)
1240 {
1241     ThreadFlush(vout, true, INT64_MAX);
1242     if (vout->p->decoder_pool)
1243         picture_pool_NonEmpty(vout->p->decoder_pool, true);
1244     vout->p->pause.is_on = false;
1245     vout->p->pause.date  = mdate();
1246 }
1247
1248 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
1249 {
1250     *duration = 0;
1251
1252     if (vout->p->step.last <= VLC_TS_INVALID)
1253         vout->p->step.last = vout->p->displayed.timestamp;
1254
1255     mtime_t dummy;
1256     if (ThreadDisplayPicture(vout, true, &dummy))
1257         return;
1258
1259     vout->p->step.timestamp = vout->p->displayed.timestamp;
1260
1261     if (vout->p->step.last > VLC_TS_INVALID &&
1262         vout->p->step.timestamp > vout->p->step.last) {
1263         *duration = vout->p->step.timestamp - vout->p->step.last;
1264         vout->p->step.last = vout->p->step.timestamp;
1265         /* TODO advance subpicture by the duration ... */
1266     }
1267 }
1268
1269 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
1270 {
1271     /* FIXME not sure setting "fullscreen" is good ... */
1272     var_SetBool(vout, "fullscreen", fullscreen);
1273     vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
1274 }
1275
1276 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
1277 {
1278     vout_SetWindowState(vout->p->display.vd,
1279                         is_on_top ? VOUT_WINDOW_STATE_ABOVE :
1280                                     VOUT_WINDOW_STATE_NORMAL);
1281 }
1282
1283 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1284 {
1285     vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1286 }
1287
1288 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1289 {
1290     if (num * 10 < den) {
1291         num = den;
1292         den *= 10;
1293     } else if (num > den * 10) {
1294         num = den * 10;
1295     }
1296
1297     vout_SetDisplayZoom(vout->p->display.vd, num, den);
1298 }
1299
1300 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1301                                     unsigned num, unsigned den)
1302 {
1303     vout_SetDisplayAspect(vout->p->display.vd, num, den);
1304 }
1305
1306
1307 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1308                                     unsigned x, unsigned y,
1309                                     unsigned width, unsigned height)
1310 {
1311     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1312                         x, y, width, height);
1313 }
1314 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1315                                     unsigned left, unsigned top,
1316                                     unsigned right, unsigned bottom)
1317 {
1318     msg_Err(vout, "ThreadExecuteCropBorder %d.%d %dx%d", left, top, right, bottom);
1319     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1320                         left, top, -(int)right, -(int)bottom);
1321 }
1322
1323 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1324                                    unsigned num, unsigned den)
1325 {
1326     vout_SetDisplayCrop(vout->p->display.vd, num, den,
1327                         0, 0, 0, 0);
1328 }
1329
1330 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
1331 {
1332     vlc_mouse_Init(&vout->p->mouse);
1333     vout->p->decoder_fifo = picture_fifo_New();
1334     vout->p->decoder_pool = NULL;
1335     vout->p->display_pool = NULL;
1336     vout->p->private_pool = NULL;
1337
1338     vout->p->filter.configuration = NULL;
1339     video_format_Copy(&vout->p->filter.format, &vout->p->original);
1340     vout->p->filter.chain_static =
1341         filter_chain_New( vout, "video filter2", true,
1342                           VoutVideoFilterStaticAllocationSetup, NULL, vout);
1343     vout->p->filter.chain_interactive =
1344         filter_chain_New( vout, "video filter2", true,
1345                           VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
1346
1347     vout_display_state_t state_default;
1348     if (!state) {
1349         VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1350         state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
1351                                                                            VOUT_WINDOW_STATE_NORMAL;
1352         state_default.sar.num = 0;
1353         state_default.sar.den = 0;
1354
1355         state = &state_default;
1356     }
1357
1358     if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1359         return VLC_EGENERIC;
1360     if (vout_InitWrapper(vout))
1361         return VLC_EGENERIC;
1362     assert(vout->p->decoder_pool);
1363
1364     vout->p->displayed.current       = NULL;
1365     vout->p->displayed.next          = NULL;
1366     vout->p->displayed.decoded       = NULL;
1367     vout->p->displayed.date          = VLC_TS_INVALID;
1368     vout->p->displayed.timestamp     = VLC_TS_INVALID;
1369     vout->p->displayed.qtype         = QTYPE_NONE;
1370     vout->p->displayed.is_interlaced = false;
1371
1372     vout->p->step.last               = VLC_TS_INVALID;
1373     vout->p->step.timestamp          = VLC_TS_INVALID;
1374
1375     vout->p->spu_blend_chroma        = 0;
1376     vout->p->spu_blend               = NULL;
1377
1378     video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1379     return VLC_SUCCESS;
1380 }
1381
1382 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1383 {
1384     if (vout->p->spu_blend)
1385         filter_DeleteBlend(vout->p->spu_blend);
1386
1387     /* Destroy translation tables */
1388     if (vout->p->display.vd) {
1389         if (vout->p->decoder_pool) {
1390             ThreadFlush(vout, true, INT64_MAX);
1391             vout_EndWrapper(vout);
1392         }
1393         vout_CloseWrapper(vout, state);
1394     }
1395
1396     /* Destroy the video filters2 */
1397     filter_chain_Delete(vout->p->filter.chain_interactive);
1398     filter_chain_Delete(vout->p->filter.chain_static);
1399     video_format_Clean(&vout->p->filter.format);
1400     free(vout->p->filter.configuration);
1401
1402     if (vout->p->decoder_fifo)
1403         picture_fifo_Delete(vout->p->decoder_fifo);
1404     assert(!vout->p->decoder_pool);
1405 }
1406
1407 static void ThreadInit(vout_thread_t *vout)
1408 {
1409     vout->p->window.is_unused = true;
1410     vout->p->window.object    = NULL;
1411     vout->p->dead             = false;
1412     vout->p->is_late_dropped  = var_InheritBool(vout, "drop-late-frames");
1413     vout->p->pause.is_on      = false;
1414     vout->p->pause.date       = VLC_TS_INVALID;
1415
1416     vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1417 }
1418
1419 static void ThreadClean(vout_thread_t *vout)
1420 {
1421     if (vout->p->window.object) {
1422         assert(vout->p->window.is_unused);
1423         vout_window_Delete(vout->p->window.object);
1424     }
1425     vout_chrono_Clean(&vout->p->render);
1426     vout->p->dead = true;
1427     vout_control_Dead(&vout->p->control);
1428 }
1429
1430 static int ThreadReinit(vout_thread_t *vout,
1431                         const vout_configuration_t *cfg)
1432 {
1433     video_format_t original;
1434     if (VoutValidateFormat(&original, cfg->fmt)) {
1435         ThreadStop(vout, NULL);
1436         ThreadClean(vout);
1437         return VLC_EGENERIC;
1438     }
1439     /* We ignore crop/ar changes at this point, they are dynamically supported */
1440     VideoFormatCopyCropAr(&vout->p->original, &original);
1441     if (video_format_IsSimilar(&original, &vout->p->original)) {
1442         if (cfg->dpb_size <= vout->p->dpb_size)
1443             return VLC_SUCCESS;
1444         msg_Warn(vout, "DPB need to be increased");
1445     }
1446
1447     vout_display_state_t state;
1448     memset(&state, 0, sizeof(state));
1449
1450     ThreadStop(vout, &state);
1451
1452     if (!state.cfg.is_fullscreen) {
1453         state.cfg.display.width  = 0;
1454         state.cfg.display.height = 0;
1455     }
1456     state.sar.num = 0;
1457     state.sar.den = 0;
1458     /* FIXME current vout "variables" are not in sync here anymore
1459      * and I am not sure what to do */
1460
1461     vout->p->original = original;
1462     vout->p->dpb_size = cfg->dpb_size;
1463     if (ThreadStart(vout, &state)) {
1464         ThreadClean(vout);
1465         return VLC_EGENERIC;
1466     }
1467     return VLC_SUCCESS;
1468 }
1469
1470 /*****************************************************************************
1471  * Thread: video output thread
1472  *****************************************************************************
1473  * Video output thread. This function does only returns when the thread is
1474  * terminated. It handles the pictures arriving in the video heap and the
1475  * display device events.
1476  *****************************************************************************/
1477 static void *Thread(void *object)
1478 {
1479     vout_thread_t *vout = object;
1480
1481     vout_interlacing_support_t interlacing = {
1482         .is_interlaced = false,
1483         .date = mdate(),
1484     };
1485     vout_postprocessing_support_t postprocessing = {
1486         .qtype = QTYPE_NONE,
1487     };
1488
1489     mtime_t deadline = VLC_TS_INVALID;
1490     for (;;) {
1491         vout_control_cmd_t cmd;
1492
1493         /* FIXME remove thoses ugly timeouts
1494          */
1495         while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1496             switch(cmd.type) {
1497             case VOUT_CONTROL_INIT:
1498                 ThreadInit(vout);
1499                 if (ThreadStart(vout, NULL)) {
1500                     ThreadStop(vout, NULL);
1501                     ThreadClean(vout);
1502                     return NULL;
1503                 }
1504                 break;
1505             case VOUT_CONTROL_CLEAN:
1506                 ThreadStop(vout, NULL);
1507                 ThreadClean(vout);
1508                 return NULL;
1509             case VOUT_CONTROL_REINIT:
1510                 if (ThreadReinit(vout, cmd.u.cfg))
1511                     return NULL;
1512                 break;
1513             case VOUT_CONTROL_SUBPICTURE:
1514                 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1515                 cmd.u.subpicture = NULL;
1516                 break;
1517             case VOUT_CONTROL_FLUSH_SUBPICTURE:
1518                 ThreadFlushSubpicture(vout, cmd.u.integer);
1519                 break;
1520             case VOUT_CONTROL_OSD_TITLE:
1521                 ThreadDisplayOsdTitle(vout, cmd.u.string);
1522                 break;
1523             case VOUT_CONTROL_CHANGE_FILTERS:
1524                 ThreadChangeFilters(vout, NULL, cmd.u.string, false);
1525                 break;
1526             case VOUT_CONTROL_CHANGE_SUB_SOURCES:
1527                 ThreadChangeSubSources(vout, cmd.u.string);
1528                 break;
1529             case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1530                 ThreadChangeSubFilters(vout, cmd.u.string);
1531                 break;
1532             case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1533                 ThreadChangeSubMargin(vout, cmd.u.integer);
1534                 break;
1535             case VOUT_CONTROL_PAUSE:
1536                 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1537                 break;
1538             case VOUT_CONTROL_FLUSH:
1539                 ThreadFlush(vout, false, cmd.u.time);
1540                 break;
1541             case VOUT_CONTROL_RESET:
1542                 ThreadReset(vout);
1543                 break;
1544             case VOUT_CONTROL_STEP:
1545                 ThreadStep(vout, cmd.u.time_ptr);
1546                 break;
1547             case VOUT_CONTROL_FULLSCREEN:
1548                 ThreadChangeFullscreen(vout, cmd.u.boolean);
1549                 break;
1550             case VOUT_CONTROL_ON_TOP:
1551                 ThreadChangeOnTop(vout, cmd.u.boolean);
1552                 break;
1553             case VOUT_CONTROL_DISPLAY_FILLED:
1554                 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1555                 break;
1556             case VOUT_CONTROL_ZOOM:
1557                 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1558                 break;
1559             case VOUT_CONTROL_ASPECT_RATIO:
1560                 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1561                 break;
1562            case VOUT_CONTROL_CROP_RATIO:
1563                 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1564                 break;
1565             case VOUT_CONTROL_CROP_WINDOW:
1566                 ThreadExecuteCropWindow(vout,
1567                                         cmd.u.window.x, cmd.u.window.y,
1568                                         cmd.u.window.width, cmd.u.window.height);
1569                 break;
1570             case VOUT_CONTROL_CROP_BORDER:
1571                 ThreadExecuteCropBorder(vout,
1572                                         cmd.u.border.left,  cmd.u.border.top,
1573                                         cmd.u.border.right, cmd.u.border.bottom);
1574                 break;
1575             default:
1576                 break;
1577             }
1578             vout_control_cmd_Clean(&cmd);
1579         }
1580
1581         ThreadManage(vout, &deadline, &interlacing, &postprocessing);
1582     }
1583 }
1584