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