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