]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
fingerprinter: don't hardcode acoustid module selection
[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         todisplay = picture_pool_Get(vout->p->private_pool);
975         if (todisplay) {
976             VideoFormatCopyCropAr(&todisplay->format, &filtered->format);
977             picture_Copy(todisplay, filtered);
978             if (vout->p->spu_blend)
979                 picture_BlendSubpicture(todisplay, vout->p->spu_blend, subpic);
980         }
981         picture_Release(filtered);
982         subpicture_Delete(subpic);
983         subpic = NULL;
984
985         if (!todisplay)
986             return VLC_EGENERIC;
987     }
988
989     picture_t *direct;
990     if (!is_direct && todisplay) {
991         direct = picture_pool_Get(vout->p->display_pool);
992         if (direct) {
993             VideoFormatCopyCropAr(&direct->format, &todisplay->format);
994             picture_Copy(direct, todisplay);
995         }
996         picture_Release(todisplay);
997     } else {
998         direct = todisplay;
999     }
1000
1001     if (!direct) {
1002         if (subpic)
1003             subpicture_Delete(subpic);
1004         return VLC_EGENERIC;
1005     }
1006
1007     /*
1008      * Take a snapshot if requested
1009      */
1010     if (do_snapshot)
1011         vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
1012
1013     /* Render the direct buffer */
1014     assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
1015     vout_UpdateDisplaySourceProperties(vd, &direct->format);
1016     if (sys->display.use_dr) {
1017         vout_display_Prepare(vd, direct, subpic);
1018     } else {
1019         sys->display.filtered = vout_FilterDisplay(vd, direct);
1020         if (sys->display.filtered) {
1021             if (!do_dr_spu && !do_early_spu && vout->p->spu_blend && subpic)
1022                 picture_BlendSubpicture(sys->display.filtered, vout->p->spu_blend, subpic);
1023             vout_display_Prepare(vd, sys->display.filtered, do_dr_spu ? subpic : NULL);
1024         }
1025         if (!do_dr_spu && subpic)
1026             subpicture_Delete(subpic);
1027         if (!sys->display.filtered)
1028             return VLC_EGENERIC;
1029     }
1030
1031     vout_chrono_Stop(&vout->p->render);
1032 #if 0
1033         {
1034         static int i = 0;
1035         if (((i++)%10) == 0)
1036             msg_Info(vout, "render: avg %d ms var %d ms",
1037                      (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
1038         }
1039 #endif
1040
1041     /* Wait the real date (for rendering jitter) */
1042 #if 0
1043     mtime_t delay = direct->date - mdate();
1044     if (delay < 1000)
1045         msg_Warn(vout, "picture is late (%lld ms)", delay / 1000);
1046 #endif
1047     if (!is_forced)
1048         mwait(direct->date);
1049
1050     /* Display the direct buffer returned by vout_RenderPicture */
1051     vout->p->displayed.date = mdate();
1052     vout_display_Display(vd,
1053                          sys->display.filtered ? sys->display.filtered
1054                                                 : direct,
1055                          subpic);
1056     sys->display.filtered = NULL;
1057
1058     vout_statistic_AddDisplayed(&vout->p->statistic, 1);
1059
1060     return VLC_SUCCESS;
1061 }
1062
1063 static int ThreadDisplayPicture(vout_thread_t *vout,
1064                                 bool now, mtime_t *deadline)
1065 {
1066     bool is_late_dropped = vout->p->is_late_dropped && !vout->p->pause.is_on && !now;
1067     bool first = !vout->p->displayed.current;
1068     if (first && ThreadDisplayPreparePicture(vout, true, is_late_dropped)) /* FIXME not sure it is ok */
1069         return VLC_EGENERIC;
1070     if (!vout->p->pause.is_on || now) {
1071         while (!vout->p->displayed.next) {
1072             if (ThreadDisplayPreparePicture(vout, false, is_late_dropped)) {
1073                 break;
1074             }
1075         }
1076     }
1077
1078     const mtime_t date = mdate();
1079     const mtime_t render_delay = vout_chrono_GetHigh(&vout->p->render) + VOUT_MWAIT_TOLERANCE;
1080
1081     mtime_t date_next = VLC_TS_INVALID;
1082     if (!vout->p->pause.is_on && vout->p->displayed.next)
1083         date_next = vout->p->displayed.next->date - render_delay;
1084
1085     /* FIXME/XXX we must redisplay the last decoded picture (because
1086      * of potential vout updated, or filters update or SPU update)
1087      * For now a high update period is needed but it coulmd be removed
1088      * if and only if:
1089      * - vout module emits events from theselves.
1090      * - *and* SPU is modified to emit an event or a deadline when needed.
1091      *
1092      * So it will be done latter.
1093      */
1094     mtime_t date_refresh = VLC_TS_INVALID;
1095     if (vout->p->displayed.date > VLC_TS_INVALID)
1096         date_refresh = vout->p->displayed.date + VOUT_REDISPLAY_DELAY - render_delay;
1097
1098     bool drop = now;
1099     if (date_next != VLC_TS_INVALID)
1100         drop |= date_next + 0 <= date;
1101
1102     bool refresh = false;
1103     if (date_refresh > VLC_TS_INVALID)
1104         refresh = date_refresh <= date;
1105
1106     if (!first && !refresh && !drop) {
1107         if (date_next != VLC_TS_INVALID && date_refresh != VLC_TS_INVALID)
1108             *deadline = __MIN(date_next, date_refresh);
1109         else if (date_next != VLC_TS_INVALID)
1110             *deadline = date_next;
1111         else if (date_refresh != VLC_TS_INVALID)
1112             *deadline = date_refresh;
1113         return VLC_EGENERIC;
1114     }
1115
1116     if (drop) {
1117         picture_Release(vout->p->displayed.current);
1118         vout->p->displayed.current = vout->p->displayed.next;
1119         vout->p->displayed.next    = NULL;
1120     }
1121     if (!vout->p->displayed.current)
1122         return VLC_EGENERIC;
1123
1124     bool is_forced = now || (!drop && refresh) || vout->p->displayed.current->b_force;
1125     return ThreadDisplayRenderPicture(vout, is_forced);
1126 }
1127
1128 static void ThreadManage(vout_thread_t *vout,
1129                          mtime_t *deadline,
1130                          vout_interlacing_support_t *interlacing)
1131 {
1132     vlc_mutex_lock(&vout->p->picture_lock);
1133
1134     *deadline = VLC_TS_INVALID;
1135     for (;;) {
1136         if (ThreadDisplayPicture(vout, false, deadline))
1137             break;
1138     }
1139
1140     const bool picture_interlaced = vout->p->displayed.is_interlaced;
1141
1142     vlc_mutex_unlock(&vout->p->picture_lock);
1143
1144     /* Deinterlacing */
1145     vout_SetInterlacingState(vout, interlacing, picture_interlaced);
1146
1147     vout_ManageWrapper(vout);
1148 }
1149
1150 static void ThreadDisplaySubpicture(vout_thread_t *vout,
1151                                     subpicture_t *subpicture)
1152 {
1153     spu_PutSubpicture(vout->p->spu, subpicture);
1154 }
1155
1156 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
1157 {
1158     spu_ClearChannel(vout->p->spu, channel);
1159 }
1160
1161 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
1162 {
1163     if (!vout->p->title.show)
1164         return;
1165
1166     vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
1167                  vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
1168                  string);
1169 }
1170
1171 static void ThreadChangeSubSources(vout_thread_t *vout, const char *filters)
1172 {
1173     spu_ChangeSources(vout->p->spu, filters);
1174 }
1175
1176 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
1177 {
1178     spu_ChangeFilters(vout->p->spu, filters);
1179 }
1180
1181 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
1182 {
1183     spu_ChangeMargin(vout->p->spu, margin);
1184 }
1185
1186 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
1187 {
1188     assert(!vout->p->pause.is_on || !is_paused);
1189
1190     if (vout->p->pause.is_on) {
1191         const mtime_t duration = date - vout->p->pause.date;
1192
1193         if (vout->p->step.timestamp > VLC_TS_INVALID)
1194             vout->p->step.timestamp += duration;
1195         if (vout->p->step.last > VLC_TS_INVALID)
1196             vout->p->step.last += duration;
1197         picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
1198         if (vout->p->displayed.decoded)
1199             vout->p->displayed.decoded->date += duration;
1200         spu_OffsetSubtitleDate(vout->p->spu, duration);
1201
1202         ThreadFilterFlush(vout, false);
1203     } else {
1204         vout->p->step.timestamp = VLC_TS_INVALID;
1205         vout->p->step.last      = VLC_TS_INVALID;
1206     }
1207     vout->p->pause.is_on = is_paused;
1208     vout->p->pause.date  = date;
1209 }
1210
1211 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
1212 {
1213     vout->p->step.timestamp = VLC_TS_INVALID;
1214     vout->p->step.last      = VLC_TS_INVALID;
1215
1216     ThreadFilterFlush(vout, false); /* FIXME too much */
1217
1218     picture_t *last = vout->p->displayed.decoded;
1219     if (last) {
1220         if (( below && last->date <= date) ||
1221             (!below && last->date >= date)) {
1222             picture_Release(last);
1223
1224             vout->p->displayed.decoded   = NULL;
1225             vout->p->displayed.date      = VLC_TS_INVALID;
1226             vout->p->displayed.timestamp = VLC_TS_INVALID;
1227         }
1228     }
1229
1230     picture_fifo_Flush(vout->p->decoder_fifo, date, below);
1231 }
1232
1233 static void ThreadReset(vout_thread_t *vout)
1234 {
1235     ThreadFlush(vout, true, INT64_MAX);
1236     if (vout->p->decoder_pool)
1237         picture_pool_NonEmpty(vout->p->decoder_pool, true);
1238     vout->p->pause.is_on = false;
1239     vout->p->pause.date  = mdate();
1240 }
1241
1242 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
1243 {
1244     *duration = 0;
1245
1246     if (vout->p->step.last <= VLC_TS_INVALID)
1247         vout->p->step.last = vout->p->displayed.timestamp;
1248
1249     mtime_t dummy;
1250     if (ThreadDisplayPicture(vout, true, &dummy))
1251         return;
1252
1253     vout->p->step.timestamp = vout->p->displayed.timestamp;
1254
1255     if (vout->p->step.last > VLC_TS_INVALID &&
1256         vout->p->step.timestamp > vout->p->step.last) {
1257         *duration = vout->p->step.timestamp - vout->p->step.last;
1258         vout->p->step.last = vout->p->step.timestamp;
1259         /* TODO advance subpicture by the duration ... */
1260     }
1261 }
1262
1263 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
1264 {
1265     vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
1266 }
1267
1268 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
1269 {
1270     vout_SetWindowState(vout->p->display.vd,
1271                         is_on_top ? VOUT_WINDOW_STATE_ABOVE :
1272                                     VOUT_WINDOW_STATE_NORMAL);
1273 }
1274
1275 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
1276 {
1277     vout_SetDisplayFilled(vout->p->display.vd, is_filled);
1278 }
1279
1280 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
1281 {
1282     if (num * 10 < den) {
1283         num = den;
1284         den *= 10;
1285     } else if (num > den * 10) {
1286         num = den * 10;
1287     }
1288
1289     vout_SetDisplayZoom(vout->p->display.vd, num, den);
1290 }
1291
1292 static void ThreadChangeAspectRatio(vout_thread_t *vout,
1293                                     unsigned num, unsigned den)
1294 {
1295     vout_SetDisplayAspect(vout->p->display.vd, num, den);
1296 }
1297
1298
1299 static void ThreadExecuteCropWindow(vout_thread_t *vout,
1300                                     unsigned x, unsigned y,
1301                                     unsigned width, unsigned height)
1302 {
1303     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1304                         x, y, width, height);
1305 }
1306 static void ThreadExecuteCropBorder(vout_thread_t *vout,
1307                                     unsigned left, unsigned top,
1308                                     unsigned right, unsigned bottom)
1309 {
1310     msg_Err(vout, "ThreadExecuteCropBorder %d.%d %dx%d", left, top, right, bottom);
1311     vout_SetDisplayCrop(vout->p->display.vd, 0, 0,
1312                         left, top, -(int)right, -(int)bottom);
1313 }
1314
1315 static void ThreadExecuteCropRatio(vout_thread_t *vout,
1316                                    unsigned num, unsigned den)
1317 {
1318     vout_SetDisplayCrop(vout->p->display.vd, num, den,
1319                         0, 0, 0, 0);
1320 }
1321
1322 static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
1323 {
1324     vlc_mouse_Init(&vout->p->mouse);
1325     vout->p->decoder_fifo = picture_fifo_New();
1326     vout->p->decoder_pool = NULL;
1327     vout->p->display_pool = NULL;
1328     vout->p->private_pool = NULL;
1329
1330     vout->p->filter.configuration = NULL;
1331     video_format_Copy(&vout->p->filter.format, &vout->p->original);
1332     vout->p->filter.chain_static =
1333         filter_chain_New( vout, "video filter2", true,
1334                           VoutVideoFilterStaticAllocationSetup, NULL, vout);
1335     vout->p->filter.chain_interactive =
1336         filter_chain_New( vout, "video filter2", true,
1337                           VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
1338
1339     vout_display_state_t state_default;
1340     if (!state) {
1341         VoutGetDisplayCfg(vout, &state_default.cfg, vout->p->display.title);
1342         state_default.wm_state = var_CreateGetBool(vout, "video-on-top") ? VOUT_WINDOW_STATE_ABOVE :
1343                                                                            VOUT_WINDOW_STATE_NORMAL;
1344         state_default.sar.num = 0;
1345         state_default.sar.den = 0;
1346
1347         state = &state_default;
1348     }
1349
1350     if (vout_OpenWrapper(vout, vout->p->splitter_name, state))
1351         return VLC_EGENERIC;
1352     if (vout_InitWrapper(vout))
1353         return VLC_EGENERIC;
1354     assert(vout->p->decoder_pool);
1355
1356     vout->p->displayed.current       = NULL;
1357     vout->p->displayed.next          = NULL;
1358     vout->p->displayed.decoded       = NULL;
1359     vout->p->displayed.date          = VLC_TS_INVALID;
1360     vout->p->displayed.timestamp     = VLC_TS_INVALID;
1361     vout->p->displayed.is_interlaced = false;
1362
1363     vout->p->step.last               = VLC_TS_INVALID;
1364     vout->p->step.timestamp          = VLC_TS_INVALID;
1365
1366     vout->p->spu_blend_chroma        = 0;
1367     vout->p->spu_blend               = NULL;
1368
1369     video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
1370     return VLC_SUCCESS;
1371 }
1372
1373 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
1374 {
1375     if (vout->p->spu_blend)
1376         filter_DeleteBlend(vout->p->spu_blend);
1377
1378     /* Destroy translation tables */
1379     if (vout->p->display.vd) {
1380         if (vout->p->decoder_pool) {
1381             ThreadFlush(vout, true, INT64_MAX);
1382             vout_EndWrapper(vout);
1383         }
1384         vout_CloseWrapper(vout, state);
1385     }
1386
1387     /* Destroy the video filters2 */
1388     filter_chain_Delete(vout->p->filter.chain_interactive);
1389     filter_chain_Delete(vout->p->filter.chain_static);
1390     video_format_Clean(&vout->p->filter.format);
1391     free(vout->p->filter.configuration);
1392
1393     if (vout->p->decoder_fifo)
1394         picture_fifo_Delete(vout->p->decoder_fifo);
1395     assert(!vout->p->decoder_pool);
1396 }
1397
1398 static void ThreadInit(vout_thread_t *vout)
1399 {
1400     vout->p->window.is_unused = true;
1401     vout->p->window.object    = NULL;
1402     vout->p->dead             = false;
1403     vout->p->is_late_dropped  = var_InheritBool(vout, "drop-late-frames");
1404     vout->p->pause.is_on      = false;
1405     vout->p->pause.date       = VLC_TS_INVALID;
1406
1407     vout_chrono_Init(&vout->p->render, 5, 10000); /* Arbitrary initial time */
1408 }
1409
1410 static void ThreadClean(vout_thread_t *vout)
1411 {
1412     if (vout->p->window.object) {
1413         assert(vout->p->window.is_unused);
1414         vout_window_Delete(vout->p->window.object);
1415     }
1416     vout_chrono_Clean(&vout->p->render);
1417     vout->p->dead = true;
1418     vout_control_Dead(&vout->p->control);
1419 }
1420
1421 static int ThreadReinit(vout_thread_t *vout,
1422                         const vout_configuration_t *cfg)
1423 {
1424     video_format_t original;
1425     if (VoutValidateFormat(&original, cfg->fmt)) {
1426         ThreadStop(vout, NULL);
1427         ThreadClean(vout);
1428         return VLC_EGENERIC;
1429     }
1430     /* We ignore crop/ar changes at this point, they are dynamically supported */
1431     VideoFormatCopyCropAr(&vout->p->original, &original);
1432     if (video_format_IsSimilar(&original, &vout->p->original)) {
1433         if (cfg->dpb_size <= vout->p->dpb_size)
1434             return VLC_SUCCESS;
1435         msg_Warn(vout, "DPB need to be increased");
1436     }
1437
1438     vout_display_state_t state;
1439     memset(&state, 0, sizeof(state));
1440
1441     ThreadStop(vout, &state);
1442
1443     if (!state.cfg.is_fullscreen) {
1444         state.cfg.display.width  = 0;
1445         state.cfg.display.height = 0;
1446     }
1447     state.sar.num = 0;
1448     state.sar.den = 0;
1449     /* FIXME current vout "variables" are not in sync here anymore
1450      * and I am not sure what to do */
1451
1452     vout->p->original = original;
1453     vout->p->dpb_size = cfg->dpb_size;
1454     if (ThreadStart(vout, &state)) {
1455         ThreadClean(vout);
1456         return VLC_EGENERIC;
1457     }
1458     return VLC_SUCCESS;
1459 }
1460
1461 /*****************************************************************************
1462  * Thread: video output thread
1463  *****************************************************************************
1464  * Video output thread. This function does only returns when the thread is
1465  * terminated. It handles the pictures arriving in the video heap and the
1466  * display device events.
1467  *****************************************************************************/
1468 static void *Thread(void *object)
1469 {
1470     vout_thread_t *vout = object;
1471
1472     vout_interlacing_support_t interlacing = {
1473         .is_interlaced = false,
1474         .date = mdate(),
1475     };
1476
1477     mtime_t deadline = VLC_TS_INVALID;
1478     for (;;) {
1479         vout_control_cmd_t cmd;
1480
1481         /* FIXME remove thoses ugly timeouts
1482          */
1483         while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1484             switch(cmd.type) {
1485             case VOUT_CONTROL_INIT:
1486                 ThreadInit(vout);
1487                 if (ThreadStart(vout, NULL)) {
1488                     ThreadStop(vout, NULL);
1489                     ThreadClean(vout);
1490                     return NULL;
1491                 }
1492                 break;
1493             case VOUT_CONTROL_CLEAN:
1494                 ThreadStop(vout, NULL);
1495                 ThreadClean(vout);
1496                 return NULL;
1497             case VOUT_CONTROL_REINIT:
1498                 if (ThreadReinit(vout, cmd.u.cfg))
1499                     return NULL;
1500                 break;
1501             case VOUT_CONTROL_SUBPICTURE:
1502                 ThreadDisplaySubpicture(vout, cmd.u.subpicture);
1503                 cmd.u.subpicture = NULL;
1504                 break;
1505             case VOUT_CONTROL_FLUSH_SUBPICTURE:
1506                 ThreadFlushSubpicture(vout, cmd.u.integer);
1507                 break;
1508             case VOUT_CONTROL_OSD_TITLE:
1509                 ThreadDisplayOsdTitle(vout, cmd.u.string);
1510                 break;
1511             case VOUT_CONTROL_CHANGE_FILTERS:
1512                 ThreadChangeFilters(vout, NULL, cmd.u.string, false);
1513                 break;
1514             case VOUT_CONTROL_CHANGE_SUB_SOURCES:
1515                 ThreadChangeSubSources(vout, cmd.u.string);
1516                 break;
1517             case VOUT_CONTROL_CHANGE_SUB_FILTERS:
1518                 ThreadChangeSubFilters(vout, cmd.u.string);
1519                 break;
1520             case VOUT_CONTROL_CHANGE_SUB_MARGIN:
1521                 ThreadChangeSubMargin(vout, cmd.u.integer);
1522                 break;
1523             case VOUT_CONTROL_PAUSE:
1524                 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1525                 break;
1526             case VOUT_CONTROL_FLUSH:
1527                 ThreadFlush(vout, false, cmd.u.time);
1528                 break;
1529             case VOUT_CONTROL_RESET:
1530                 ThreadReset(vout);
1531                 break;
1532             case VOUT_CONTROL_STEP:
1533                 ThreadStep(vout, cmd.u.time_ptr);
1534                 break;
1535             case VOUT_CONTROL_FULLSCREEN:
1536                 ThreadChangeFullscreen(vout, cmd.u.boolean);
1537                 break;
1538             case VOUT_CONTROL_ON_TOP:
1539                 ThreadChangeOnTop(vout, cmd.u.boolean);
1540                 break;
1541             case VOUT_CONTROL_DISPLAY_FILLED:
1542                 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1543                 break;
1544             case VOUT_CONTROL_ZOOM:
1545                 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1546                 break;
1547             case VOUT_CONTROL_ASPECT_RATIO:
1548                 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1549                 break;
1550            case VOUT_CONTROL_CROP_RATIO:
1551                 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1552                 break;
1553             case VOUT_CONTROL_CROP_WINDOW:
1554                 ThreadExecuteCropWindow(vout,
1555                                         cmd.u.window.x, cmd.u.window.y,
1556                                         cmd.u.window.width, cmd.u.window.height);
1557                 break;
1558             case VOUT_CONTROL_CROP_BORDER:
1559                 ThreadExecuteCropBorder(vout,
1560                                         cmd.u.border.left,  cmd.u.border.top,
1561                                         cmd.u.border.right, cmd.u.border.bottom);
1562                 break;
1563             default:
1564                 break;
1565             }
1566             vout_control_cmd_Clean(&cmd);
1567         }
1568
1569         ThreadManage(vout, &deadline, &interlacing);
1570     }
1571 }
1572