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