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