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