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