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