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