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