]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
ThreadDisplayPicture: simplify prototype
[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 "display.h"
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static void *Thread(void *);
58 static void VoutDestructor(vlc_object_t *);
59
60 /* Maximum delay between 2 displayed pictures.
61  * XXX it is needed for now but should be removed in the long term.
62  */
63 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
64
65 /**
66  * Late pictures having a delay higher than this value are thrashed.
67  */
68 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
69
70 /* Better be in advance when awakening than late... */
71 #define VOUT_MWAIT_TOLERANCE (INT64_C(4000))
72
73 /* */
74 static int VoutValidateFormat(video_format_t *dst,
75                               const video_format_t *src)
76 {
77     if (src->i_width <= 0  || src->i_width  > 8192 ||
78         src->i_height <= 0 || src->i_height > 8192)
79         return VLC_EGENERIC;
80     if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
81         return VLC_EGENERIC;
82
83     /* */
84     video_format_Copy(dst, src);
85     dst->i_chroma = vlc_fourcc_GetCodec(VIDEO_ES, src->i_chroma);
86     vlc_ureduce( &dst->i_sar_num, &dst->i_sar_den,
87                  src->i_sar_num,  src->i_sar_den, 50000 );
88     if (dst->i_sar_num <= 0 || dst->i_sar_den <= 0) {
89         dst->i_sar_num = 1;
90         dst->i_sar_den = 1;
91     }
92     video_format_FixRgb(dst);
93     return VLC_SUCCESS;
94 }
95 static void VideoFormatCopyCropAr(video_format_t *dst,
96                                   const video_format_t *src)
97 {
98     video_format_CopyCrop(dst, src);
99     dst->i_sar_num = src->i_sar_num;
100     dst->i_sar_den = src->i_sar_den;
101 }
102 static bool VideoFormatIsCropArEqual(video_format_t *dst,
103                                      const video_format_t *src)
104 {
105     return dst->i_sar_num * src->i_sar_den == dst->i_sar_den * src->i_sar_num &&
106            dst->i_x_offset       == src->i_x_offset &&
107            dst->i_y_offset       == src->i_y_offset &&
108            dst->i_visible_width  == src->i_visible_width &&
109            dst->i_visible_height == src->i_visible_height;
110 }
111
112 static vout_thread_t *VoutCreate(vlc_object_t *object,
113                                  const vout_configuration_t *cfg)
114 {
115     video_format_t original;
116     if (VoutValidateFormat(&original, cfg->fmt))
117         return NULL;
118
119     /* Allocate descriptor */
120     vout_thread_t *vout = vlc_custom_create(object,
121                                             sizeof(*vout) + sizeof(*vout->p),
122                                             "video output");
123     if (!vout) {
124         video_format_Clean(&original);
125         return NULL;
126     }
127
128     /* */
129     vout->p = (vout_thread_sys_t*)&vout[1];
130
131     vout->p->original = original;
132     vout->p->dpb_size = cfg->dpb_size;
133
134     vout_control_Init(&vout->p->control);
135     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_INIT);
136
137     vout_statistic_Init(&vout->p->statistic);
138
139     vout_snapshot_Init(&vout->p->snapshot);
140
141     /* Initialize locks */
142     vlc_mutex_init(&vout->p->picture_lock);
143     vlc_mutex_init(&vout->p->filter.lock);
144     vlc_mutex_init(&vout->p->spu_lock);
145
146     /* Initialize subpicture unit */
147     vout->p->spu = spu_Create(vout);
148
149     /* Take care of some "interface/control" related initialisations */
150     vout_IntfInit(vout);
151
152     vout->p->title.show     = var_InheritBool(vout, "video-title-show");
153     vout->p->title.timeout  = var_InheritInteger(vout, "video-title-timeout");
154     vout->p->title.position = var_InheritInteger(vout, "video-title-position");
155
156     /* Get splitter name if present */
157     char *splitter_name = var_InheritString(vout, "video-splitter");
158     if (splitter_name && *splitter_name) {
159         vout->p->splitter_name = splitter_name;
160     } else {
161         free(splitter_name);
162     }
163
164     /* */
165     vout_InitInterlacingSupport(vout, vout->p->displayed.is_interlaced);
166
167     /* */
168     vlc_object_set_destructor(vout, VoutDestructor);
169
170     /* */
171     if (vlc_clone(&vout->p->thread, Thread, vout,
172                   VLC_THREAD_PRIORITY_OUTPUT)) {
173         spu_Destroy(vout->p->spu);
174         vlc_object_release(vout);
175         return NULL;
176     }
177
178     vout_control_WaitEmpty(&vout->p->control);
179
180     if (vout->p->dead) {
181         msg_Err(vout, "video output creation failed");
182         vout_CloseAndRelease(vout);
183         return NULL;
184     }
185
186     vout->p->input = cfg->input;
187     if (vout->p->input)
188         spu_Attach(vout->p->spu, vout->p->input, true);
189
190     return vout;
191 }
192
193 vout_thread_t *(vout_Request)(vlc_object_t *object,
194                               const vout_configuration_t *cfg)
195 {
196     vout_thread_t *vout = cfg->vout;
197     if (cfg->change_fmt && !cfg->fmt) {
198         if (vout)
199             vout_CloseAndRelease(vout);
200         return NULL;
201     }
202
203     /* If a vout is provided, try reusing it */
204     if (vout) {
205         if (vout->p->input != cfg->input) {
206             if (vout->p->input)
207                 spu_Attach(vout->p->spu, vout->p->input, false);
208             vout->p->input = cfg->input;
209             if (vout->p->input)
210                 spu_Attach(vout->p->spu, vout->p->input, true);
211         }
212
213         if (cfg->change_fmt) {
214             vout_control_cmd_t cmd;
215             vout_control_cmd_Init(&cmd, VOUT_CONTROL_REINIT);
216             cmd.u.cfg = cfg;
217
218             vout_control_Push(&vout->p->control, &cmd);
219             vout_control_WaitEmpty(&vout->p->control);
220         }
221
222         if (!vout->p->dead) {
223             msg_Dbg(object, "reusing provided vout");
224             vout_IntfReinit(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_InheritInteger(vout, "snapshot-width");
474         const int override_height = var_InheritInteger(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
859         picture = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
860     }
861
862     vlc_mutex_unlock(&vout->p->filter.lock);
863
864     vout_statistic_AddLost(&vout->p->statistic, lost_count);
865     if (!picture)
866         return VLC_EGENERIC;
867
868     assert(!vout->p->displayed.next);
869     if (!vout->p->displayed.current)
870         vout->p->displayed.current = picture;
871     else
872         vout->p->displayed.next    = picture;
873     return VLC_SUCCESS;
874 }
875
876 static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
877 {
878     vout_thread_sys_t *sys = vout->p;
879     vout_display_t *vd = vout->p->display.vd;
880
881     picture_t *torender = picture_Hold(vout->p->displayed.current);
882
883     vout_chrono_Start(&vout->p->render);
884
885     vlc_mutex_lock(&vout->p->filter.lock);
886     picture_t *filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, torender);
887     vlc_mutex_unlock(&vout->p->filter.lock);
888
889     if (!filtered)
890         return VLC_EGENERIC;
891
892     if (filtered->date != vout->p->displayed.current->date)
893         msg_Warn(vout, "Unsupported timestamp modifications done by chain_interactive");
894
895     /*
896      * Get the subpicture to be displayed
897      */
898     const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
899     mtime_t render_subtitle_date;
900     if (vout->p->pause.is_on)
901         render_subtitle_date = vout->p->pause.date;
902     else
903         render_subtitle_date = filtered->date > 1 ? filtered->date : mdate();
904     mtime_t render_osd_date = mdate(); /* FIXME wrong */
905
906     /*
907      * Get the subpicture to be displayed
908      */
909     const bool do_dr_spu = !do_snapshot &&
910                            vd->info.subpicture_chromas &&
911                            *vd->info.subpicture_chromas != 0;
912     const bool do_early_spu = !do_dr_spu &&
913                               (vd->info.is_slow ||
914                                sys->display.use_dr ||
915                                do_snapshot ||
916                                !vout_IsDisplayFiltered(vd) ||
917                                vd->fmt.i_width * vd->fmt.i_height <= vd->source.i_width * vd->source.i_height);
918
919     const vlc_fourcc_t *subpicture_chromas;
920     video_format_t fmt_spu;
921     if (do_dr_spu) {
922         vout_display_place_t place;
923         vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
924
925         fmt_spu = vd->source;
926         if (fmt_spu.i_width * fmt_spu.i_height < place.width * place.height) {
927             fmt_spu.i_sar_num = vd->cfg->display.sar.num;
928             fmt_spu.i_sar_den = vd->cfg->display.sar.den;
929             fmt_spu.i_width          =
930             fmt_spu.i_visible_width  = place.width;
931             fmt_spu.i_height         =
932             fmt_spu.i_visible_height = place.height;
933         }
934         subpicture_chromas = vd->info.subpicture_chromas;
935     } else {
936         if (do_early_spu) {
937             fmt_spu = vd->source;
938         } else {
939             fmt_spu = vd->fmt;
940             fmt_spu.i_sar_num = vd->cfg->display.sar.num;
941             fmt_spu.i_sar_den = vd->cfg->display.sar.den;
942         }
943         subpicture_chromas = NULL;
944
945         if (vout->p->spu_blend &&
946             vout->p->spu_blend->fmt_out.video.i_chroma != fmt_spu.i_chroma) {
947             filter_DeleteBlend(vout->p->spu_blend);
948             vout->p->spu_blend = NULL;
949             vout->p->spu_blend_chroma = 0;
950         }
951         if (!vout->p->spu_blend && vout->p->spu_blend_chroma != fmt_spu.i_chroma) {
952             vout->p->spu_blend_chroma = fmt_spu.i_chroma;
953             vout->p->spu_blend = filter_NewBlend(VLC_OBJECT(vout), &fmt_spu);
954             if (!vout->p->spu_blend)
955                 msg_Err(vout, "Failed to create blending filter, OSD/Subtitles will not work");
956         }
957     }
958
959     subpicture_t *subpic = spu_Render(vout->p->spu,
960                                       subpicture_chromas, &fmt_spu,
961                                       &vd->source,
962                                       render_subtitle_date, render_osd_date,
963                                       do_snapshot);
964     /*
965      * Perform rendering
966      *
967      * We have to:
968      * - be sure to end up with a direct buffer.
969      * - blend subtitles, and in a fast access buffer
970      */
971     bool is_direct = vout->p->decoder_pool == vout->p->display_pool;
972     picture_t *todisplay = filtered;
973     if (do_early_spu && subpic) {
974         picture_t *blent = picture_pool_Get(vout->p->private_pool);
975         if (blent) {
976             VideoFormatCopyCropAr(&blent->format, &filtered->format);
977             picture_Copy(blent, filtered);
978             if (vout->p->spu_blend
979              && picture_BlendSubpicture(blent, vout->p->spu_blend, subpic)) {
980                 picture_Release(todisplay);
981                 todisplay = blent;
982             } else
983                 picture_Release(blent);
984         }
985         subpicture_Delete(subpic);
986         subpic = NULL;
987     }
988
989     assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
990     if (sys->display.use_dr && !is_direct) {
991         picture_t *direct = picture_pool_Get(vout->p->display_pool);
992         if (!direct) {
993             picture_Release(todisplay);
994             if (subpic)
995                 subpicture_Delete(subpic);
996             return VLC_EGENERIC;
997         }
998
999         /* The display uses direct rendering (no conversion), but its pool of
1000          * pictures is not usable by the decoder (too few, too slow or
1001          * subject to invalidation...). Since there are no filters, copying
1002          * pictures from the decoder to the output is unavoidable. */
1003         VideoFormatCopyCropAr(&direct->format, &todisplay->format);
1004         picture_Copy(direct, todisplay);
1005         picture_Release(todisplay);
1006         todisplay = direct;
1007     }
1008
1009     /*
1010      * Take a snapshot if requested
1011      */
1012     if (do_snapshot)
1013         vout_snapshot_Set(&vout->p->snapshot, &vd->source, todisplay);
1014
1015     /* Render the direct buffer */
1016     vout_UpdateDisplaySourceProperties(vd, &todisplay->format);
1017     if (sys->display.use_dr) {
1018         vout_display_Prepare(vd, todisplay, subpic);
1019     } else {
1020         sys->display.filtered = vout_FilterDisplay(vd, todisplay);
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         {
1028             subpicture_Delete(subpic);
1029             subpic = NULL;
1030         }
1031         if (!sys->display.filtered)
1032             return VLC_EGENERIC;
1033     }
1034
1035     vout_chrono_Stop(&vout->p->render);
1036 #if 0
1037         {
1038         static int i = 0;
1039         if (((i++)%10) == 0)
1040             msg_Info(vout, "render: avg %d ms var %d ms",
1041                      (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
1042         }
1043 #endif
1044
1045     /* Wait the real date (for rendering jitter) */
1046 #if 0
1047     mtime_t delay = direct->date - mdate();
1048     if (delay < 1000)
1049         msg_Warn(vout, "picture is late (%lld ms)", delay / 1000);
1050 #endif
1051     if (!is_forced)
1052         mwait(todisplay->date);
1053
1054     /* Display the direct buffer returned by vout_RenderPicture */
1055     vout->p->displayed.date = mdate();
1056     vout_display_Display(vd,
1057                          sys->display.filtered ? sys->display.filtered
1058                                                 : todisplay,
1059                          subpic);
1060     sys->display.filtered = NULL;
1061
1062     vout_statistic_AddDisplayed(&vout->p->statistic, 1);
1063
1064     return VLC_SUCCESS;
1065 }
1066
1067 static int ThreadDisplayPicture(vout_thread_t *vout, mtime_t *deadline)
1068 {
1069     bool now = !deadline;
1070     bool is_late_dropped = vout->p->is_late_dropped && !vout->p->pause.is_on && !now;
1071     bool first = !vout->p->displayed.current;
1072     if (first && ThreadDisplayPreparePicture(vout, true, is_late_dropped)) /* FIXME not sure it is ok */
1073         return VLC_EGENERIC;
1074     if (!vout->p->pause.is_on || now) {
1075         while (!vout->p->displayed.next) {
1076             if (ThreadDisplayPreparePicture(vout, false, is_late_dropped)) {
1077                 break;
1078             }
1079         }
1080     }
1081
1082     const mtime_t date = mdate();
1083     const mtime_t render_delay = vout_chrono_GetHigh(&vout->p->render) + VOUT_MWAIT_TOLERANCE;
1084
1085     mtime_t date_next = VLC_TS_INVALID;
1086     if (!vout->p->pause.is_on && vout->p->displayed.next)
1087         date_next = vout->p->displayed.next->date - render_delay;
1088
1089     /* FIXME/XXX we must redisplay the last decoded picture (because
1090      * of potential vout updated, or filters update or SPU update)
1091      * For now a high update period is needed but it coulmd be removed
1092      * if and only if:
1093      * - vout module emits events from theselves.
1094      * - *and* SPU is modified to emit an event or a deadline when needed.
1095      *
1096      * So it will be done latter.
1097      */
1098     mtime_t date_refresh = VLC_TS_INVALID;
1099     if (vout->p->displayed.date > VLC_TS_INVALID)
1100         date_refresh = vout->p->displayed.date + VOUT_REDISPLAY_DELAY - render_delay;
1101
1102     bool drop = now;
1103     if (date_next != VLC_TS_INVALID)
1104         drop |= date_next + 0 <= date;
1105
1106     bool refresh = false;
1107     if (date_refresh > VLC_TS_INVALID)
1108         refresh = date_refresh <= date;
1109
1110     if (!first && !refresh && !drop) {
1111         if (!now) {
1112             if (date_next != VLC_TS_INVALID && date_refresh != VLC_TS_INVALID)
1113                 *deadline = __MIN(date_next, date_refresh);
1114             else if (date_next != VLC_TS_INVALID)
1115                 *deadline = date_next;
1116             else if (date_refresh != VLC_TS_INVALID)
1117                 *deadline = date_refresh;
1118         }
1119         return VLC_EGENERIC;
1120     }
1121
1122     if (drop) {
1123         picture_Release(vout->p->displayed.current);
1124         vout->p->displayed.current = vout->p->displayed.next;
1125         vout->p->displayed.next    = NULL;
1126     }
1127     if (!vout->p->displayed.current)
1128         return VLC_EGENERIC;
1129
1130     bool is_forced = now || (!drop && refresh) || vout->p->displayed.current->b_force;
1131     return ThreadDisplayRenderPicture(vout, is_forced);
1132 }
1133
1134 static void ThreadManage(vout_thread_t *vout,
1135                          mtime_t *deadline,
1136                          vout_interlacing_support_t *interlacing)
1137 {
1138     vlc_mutex_lock(&vout->p->picture_lock);
1139
1140     *deadline = VLC_TS_INVALID;
1141     for (;;)
1142         if (ThreadDisplayPicture(vout, deadline))
1143             break;
1144
1145     const bool picture_interlaced = vout->p->displayed.is_interlaced;
1146
1147     vlc_mutex_unlock(&vout->p->picture_lock);
1148
1149     /* Deinterlacing */
1150     vout_SetInterlacingState(vout, interlacing, picture_interlaced);
1151
1152     vout_ManageWrapper(vout);
1153 }
1154
1155 static void ThreadDisplaySubpicture(vout_thread_t *vout,
1156                                     subpicture_t *subpicture)
1157 {
1158     spu_PutSubpicture(vout->p->spu, subpicture);
1159 }
1160
1161 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
1162 {
1163     spu_ClearChannel(vout->p->spu, channel);
1164 }
1165
1166 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
1167 {
1168     if (!vout->p->title.show)
1169         return;
1170
1171     vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
1172                  vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
1173                  string);
1174 }
1175
1176 static void ThreadChangeSubSources(vout_thread_t *vout, const char *filters)
1177 {
1178     spu_ChangeSources(vout->p->spu, filters);
1179 }
1180
1181 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
1182 {
1183     spu_ChangeFilters(vout->p->spu, filters);
1184 }
1185
1186 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
1187 {
1188     spu_ChangeMargin(vout->p->spu, margin);
1189 }
1190
1191 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
1192 {
1193     assert(!vout->p->pause.is_on || !is_paused);
1194
1195     if (vout->p->pause.is_on) {
1196         const mtime_t duration = date - vout->p->pause.date;
1197
1198         if (vout->p->step.timestamp > VLC_TS_INVALID)
1199             vout->p->step.timestamp += duration;
1200         if (vout->p->step.last > VLC_TS_INVALID)
1201             vout->p->step.last += duration;
1202         picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
1203         if (vout->p->displayed.decoded)
1204             vout->p->displayed.decoded->date += duration;
1205         spu_OffsetSubtitleDate(vout->p->spu, duration);
1206
1207         ThreadFilterFlush(vout, false);
1208     } else {
1209         vout->p->step.timestamp = VLC_TS_INVALID;
1210         vout->p->step.last      = VLC_TS_INVALID;
1211     }
1212     vout->p->pause.is_on = is_paused;
1213     vout->p->pause.date  = date;
1214 }
1215
1216 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
1217 {
1218     vout->p->step.timestamp = VLC_TS_INVALID;
1219     vout->p->step.last      = VLC_TS_INVALID;
1220
1221     ThreadFilterFlush(vout, false); /* FIXME too much */
1222
1223     picture_t *last = vout->p->displayed.decoded;
1224     if (last) {
1225         if (( below && last->date <= date) ||
1226             (!below && last->date >= date)) {
1227             picture_Release(last);
1228
1229             vout->p->displayed.decoded   = NULL;
1230             vout->p->displayed.date      = VLC_TS_INVALID;
1231             vout->p->displayed.timestamp = VLC_TS_INVALID;
1232         }
1233     }
1234
1235     picture_fifo_Flush(vout->p->decoder_fifo, date, below);
1236 }
1237
1238 static void ThreadReset(vout_thread_t *vout)
1239 {
1240     ThreadFlush(vout, true, INT64_MAX);
1241     if (vout->p->decoder_pool)
1242         picture_pool_NonEmpty(vout->p->decoder_pool, true);
1243     vout->p->pause.is_on = false;
1244     vout->p->pause.date  = mdate();
1245 }
1246
1247 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
1248 {
1249     *duration = 0;
1250
1251     if (vout->p->step.last <= VLC_TS_INVALID)
1252         vout->p->step.last = vout->p->displayed.timestamp;
1253
1254     if (ThreadDisplayPicture(vout, NULL))
1255         return;
1256
1257     vout->p->step.timestamp = vout->p->displayed.timestamp;
1258
1259     if (vout->p->step.last > VLC_TS_INVALID &&
1260         vout->p->step.timestamp > vout->p->step.last) {
1261         *duration = vout->p->step.timestamp - vout->p->step.last;
1262         vout->p->step.last = vout->p->step.timestamp;
1263         /* TODO advance subpicture by the duration ... */
1264     }
1265 }
1266
1267 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
1268 {
1269     vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
1270 }
1271
1272 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
1273 {
1274     vout_SetWindowState(vout->p->display.vd,
1275                         is_on_top ? VOUT_WINDOW_STATE_ABOVE :
1276                                     VOUT_WINDOW_STATE_NORMAL);
1277 }
1278
1279 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1280 {
1281     vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1282 }
1283
1284 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1285 {
1286     if (num * 10 < den) {
1287         num = den;
1288         den *= 10;
1289     } else if (num > den * 10) {
1290         num = den * 10;
1291     }
1292
1293     vout_SetDisplayZoom(vout->p->display.vd, num, den);
1294 }
1295
1296 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1297                                     unsigned num, unsigned den)
1298 {
1299     vout_SetDisplayAspect(vout->p->display.vd, num, den);
1300 }
1301
1302
1303 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1304                                     unsigned x, unsigned y,
1305                                     unsigned width, unsigned height)
1306 {
1307     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1308                         x, y, width, height);
1309 }
1310 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1311                                     unsigned left, unsigned top,
1312                                     unsigned right, unsigned bottom)
1313 {
1314     msg_Err(vout, "ThreadExecuteCropBorder %d.%d %dx%d", left, top, right, bottom);
1315     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1316                         left, top, -(int)right, -(int)bottom);
1317 }
1318
1319 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1320                                    unsigned num, unsigned den)
1321 {
1322     vout_SetDisplayCrop(vout->p->display.vd, num, den,
1323                         0, 0, 0, 0);
1324 }
1325
1326 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
1327 {
1328     vlc_mouse_Init(&vout->p->mouse);
1329     vout->p->decoder_fifo = picture_fifo_New();
1330     vout->p->decoder_pool = NULL;
1331     vout->p->display_pool = NULL;
1332     vout->p->private_pool = NULL;
1333
1334     vout->p->filter.configuration = NULL;
1335     video_format_Copy(&vout->p->filter.format, &vout->p->original);
1336     vout->p->filter.chain_static =
1337         filter_chain_New( vout, "video filter2", true,
1338                           VoutVideoFilterStaticAllocationSetup, NULL, vout);
1339     vout->p->filter.chain_interactive =
1340         filter_chain_New( vout, "video filter2", true,
1341                           VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
1342
1343     vout_display_state_t state_default;
1344     if (!state) {
1345         VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1346         state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
1347                                                                            VOUT_WINDOW_STATE_NORMAL;
1348         state_default.sar.num = 0;
1349         state_default.sar.den = 0;
1350
1351         state = &state_default;
1352     }
1353
1354     if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1355         return VLC_EGENERIC;
1356     if (vout_InitWrapper(vout))
1357         return VLC_EGENERIC;
1358     assert(vout->p->decoder_pool);
1359
1360     vout->p->displayed.current       = NULL;
1361     vout->p->displayed.next          = NULL;
1362     vout->p->displayed.decoded       = NULL;
1363     vout->p->displayed.date          = VLC_TS_INVALID;
1364     vout->p->displayed.timestamp     = VLC_TS_INVALID;
1365     vout->p->displayed.is_interlaced = false;
1366
1367     vout->p->step.last               = VLC_TS_INVALID;
1368     vout->p->step.timestamp          = VLC_TS_INVALID;
1369
1370     vout->p->spu_blend_chroma        = 0;
1371     vout->p->spu_blend               = NULL;
1372
1373     video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1374     return VLC_SUCCESS;
1375 }
1376
1377 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1378 {
1379     if (vout->p->spu_blend)
1380         filter_DeleteBlend(vout->p->spu_blend);
1381
1382     /* Destroy translation tables */
1383     if (vout->p->display.vd) {
1384         if (vout->p->decoder_pool) {
1385             ThreadFlush(vout, true, INT64_MAX);
1386             vout_EndWrapper(vout);
1387         }
1388         vout_CloseWrapper(vout, state);
1389     }
1390
1391     /* Destroy the video filters2 */
1392     filter_chain_Delete(vout->p->filter.chain_interactive);
1393     filter_chain_Delete(vout->p->filter.chain_static);
1394     video_format_Clean(&vout->p->filter.format);
1395     free(vout->p->filter.configuration);
1396
1397     if (vout->p->decoder_fifo)
1398         picture_fifo_Delete(vout->p->decoder_fifo);
1399     assert(!vout->p->decoder_pool);
1400 }
1401
1402 static void ThreadInit(vout_thread_t *vout)
1403 {
1404     vout->p->window.is_unused = true;
1405     vout->p->window.object    = NULL;
1406     vout->p->dead             = false;
1407     vout->p->is_late_dropped  = var_InheritBool(vout, "drop-late-frames");
1408     vout->p->pause.is_on      = false;
1409     vout->p->pause.date       = VLC_TS_INVALID;
1410
1411     vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1412 }
1413
1414 static void ThreadClean(vout_thread_t *vout)
1415 {
1416     if (vout->p->window.object) {
1417         assert(vout->p->window.is_unused);
1418         vout_window_Delete(vout->p->window.object);
1419     }
1420     vout_chrono_Clean(&vout->p->render);
1421     vout->p->dead = true;
1422     vout_control_Dead(&vout->p->control);
1423 }
1424
1425 static int ThreadReinit(vout_thread_t *vout,
1426                         const vout_configuration_t *cfg)
1427 {
1428     video_format_t original;
1429     if (VoutValidateFormat(&original, cfg->fmt)) {
1430         ThreadStop(vout, NULL);
1431         ThreadClean(vout);
1432         return VLC_EGENERIC;
1433     }
1434     /* We ignore crop/ar changes at this point, they are dynamically supported */
1435     VideoFormatCopyCropAr(&vout->p->original, &original);
1436     if (video_format_IsSimilar(&original, &vout->p->original)) {
1437         if (cfg->dpb_size <= vout->p->dpb_size)
1438             return VLC_SUCCESS;
1439         msg_Warn(vout, "DPB need to be increased");
1440     }
1441
1442     vout_display_state_t state;
1443     memset(&state, 0, sizeof(state));
1444
1445     ThreadStop(vout, &state);
1446
1447     if (!state.cfg.is_fullscreen) {
1448         state.cfg.display.width  = 0;
1449         state.cfg.display.height = 0;
1450     }
1451     state.sar.num = 0;
1452     state.sar.den = 0;
1453     /* FIXME current vout "variables" are not in sync here anymore
1454      * and I am not sure what to do */
1455
1456     vout->p->original = original;
1457     vout->p->dpb_size = cfg->dpb_size;
1458     if (ThreadStart(vout, &state)) {
1459         ThreadClean(vout);
1460         return VLC_EGENERIC;
1461     }
1462     return VLC_SUCCESS;
1463 }
1464
1465 /*****************************************************************************
1466  * Thread: video output thread
1467  *****************************************************************************
1468  * Video output thread. This function does only returns when the thread is
1469  * terminated. It handles the pictures arriving in the video heap and the
1470  * display device events.
1471  *****************************************************************************/
1472 static void *Thread(void *object)
1473 {
1474     vout_thread_t *vout = object;
1475
1476     vout_interlacing_support_t interlacing = {
1477         .is_interlaced = false,
1478         .date = mdate(),
1479     };
1480
1481     mtime_t deadline = VLC_TS_INVALID;
1482     for (;;) {
1483         vout_control_cmd_t cmd;
1484
1485         /* FIXME remove thoses ugly timeouts
1486          */
1487         while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1488             switch(cmd.type) {
1489             case VOUT_CONTROL_INIT:
1490                 ThreadInit(vout);
1491                 if (ThreadStart(vout, NULL)) {
1492                     ThreadStop(vout, NULL);
1493                     ThreadClean(vout);
1494                     return NULL;
1495                 }
1496                 break;
1497             case VOUT_CONTROL_CLEAN:
1498                 ThreadStop(vout, NULL);
1499                 ThreadClean(vout);
1500                 return NULL;
1501             case VOUT_CONTROL_REINIT:
1502                 if (ThreadReinit(vout, cmd.u.cfg))
1503                     return NULL;
1504                 break;
1505             case VOUT_CONTROL_SUBPICTURE:
1506                 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1507                 cmd.u.subpicture = NULL;
1508                 break;
1509             case VOUT_CONTROL_FLUSH_SUBPICTURE:
1510                 ThreadFlushSubpicture(vout, cmd.u.integer);
1511                 break;
1512             case VOUT_CONTROL_OSD_TITLE:
1513                 ThreadDisplayOsdTitle(vout, cmd.u.string);
1514                 break;
1515             case VOUT_CONTROL_CHANGE_FILTERS:
1516                 ThreadChangeFilters(vout, NULL, cmd.u.string, false);
1517                 break;
1518             case VOUT_CONTROL_CHANGE_SUB_SOURCES:
1519                 ThreadChangeSubSources(vout, cmd.u.string);
1520                 break;
1521             case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1522                 ThreadChangeSubFilters(vout, cmd.u.string);
1523                 break;
1524             case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1525                 ThreadChangeSubMargin(vout, cmd.u.integer);
1526                 break;
1527             case VOUT_CONTROL_PAUSE:
1528                 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1529                 break;
1530             case VOUT_CONTROL_FLUSH:
1531                 ThreadFlush(vout, false, cmd.u.time);
1532                 break;
1533             case VOUT_CONTROL_RESET:
1534                 ThreadReset(vout);
1535                 break;
1536             case VOUT_CONTROL_STEP:
1537                 ThreadStep(vout, cmd.u.time_ptr);
1538                 break;
1539             case VOUT_CONTROL_FULLSCREEN:
1540                 ThreadChangeFullscreen(vout, cmd.u.boolean);
1541                 break;
1542             case VOUT_CONTROL_ON_TOP:
1543                 ThreadChangeOnTop(vout, cmd.u.boolean);
1544                 break;
1545             case VOUT_CONTROL_DISPLAY_FILLED:
1546                 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1547                 break;
1548             case VOUT_CONTROL_ZOOM:
1549                 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1550                 break;
1551             case VOUT_CONTROL_ASPECT_RATIO:
1552                 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1553                 break;
1554            case VOUT_CONTROL_CROP_RATIO:
1555                 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1556                 break;
1557             case VOUT_CONTROL_CROP_WINDOW:
1558                 ThreadExecuteCropWindow(vout,
1559                                         cmd.u.window.x, cmd.u.window.y,
1560                                         cmd.u.window.width, cmd.u.window.height);
1561                 break;
1562             case VOUT_CONTROL_CROP_BORDER:
1563                 ThreadExecuteCropBorder(vout,
1564                                         cmd.u.border.left,  cmd.u.border.top,
1565                                         cmd.u.border.right, cmd.u.border.bottom);
1566                 break;
1567             default:
1568                 break;
1569             }
1570             vout_control_cmd_Clean(&cmd);
1571         }
1572
1573         ThreadManage(vout, &deadline, &interlacing);
1574     }
1575 }
1576