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