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