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