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