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