]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Fix massive leak when --vout-filter or --vout module_Need fails
[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  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37
38 #include <stdlib.h>                                                /* free() */
39 #include <string.h>
40
41
42 #ifdef HAVE_SYS_TIMES_H
43 #   include <sys/times.h>
44 #endif
45
46 #include <vlc_vout.h>
47
48 #include <vlc_filter.h>
49 #include <vlc_osd.h>
50 #include <assert.h>
51
52 #if defined( __APPLE__ )
53 /* Include darwin_specific.h here if needed */
54 #endif
55
56 /** FIXME This is quite ugly but needed while we don't have counters
57  * helpers */
58 //#include "input/input_internal.h"
59
60 #include <libvlc.h>
61 #include <vlc_input.h>
62 #include "vout_pictures.h"
63 #include "vout_internal.h"
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int      InitThread        ( vout_thread_t * );
69 static void*    RunThread         ( void *  );
70 static void     ErrorThread       ( vout_thread_t * );
71 static void     CleanThread       ( vout_thread_t * );
72 static void     EndThread         ( vout_thread_t * );
73
74 static void     AspectRatio       ( int, int *, int * );
75
76 static void VideoFormatImportRgb( video_format_t *, const picture_heap_t * );
77 static void PictureHeapFixRgb( picture_heap_t * );
78
79 static void     vout_Destructor   ( vlc_object_t * p_this );
80
81 /* Object variables callbacks */
82 static int DeinterlaceCallback( vlc_object_t *, char const *,
83                                 vlc_value_t, vlc_value_t, void * );
84 static int FilterCallback( vlc_object_t *, char const *,
85                            vlc_value_t, vlc_value_t, void * );
86 static int VideoFilter2Callback( vlc_object_t *, char const *,
87                                  vlc_value_t, vlc_value_t, void * );
88 static void PostProcessEnable( vout_thread_t * );
89 static void PostProcessDisable( vout_thread_t * );
90 static void PostProcessSetFilterQuality( vout_thread_t *p_vout );
91 static int  PostProcessCallback( vlc_object_t *, char const *,
92                                  vlc_value_t, vlc_value_t, void * );
93
94 /* From vout_intf.c */
95 int vout_Snapshot( vout_thread_t *, picture_t * );
96
97 /* Display media title in OSD */
98 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
99
100 /* Time during which the thread will sleep if it has nothing to
101  * display (in micro-seconds) */
102 #define VOUT_IDLE_SLEEP                 ((int)(0.020*CLOCK_FREQ))
103
104 /* Maximum lap of time allowed between the beginning of rendering and
105  * display. If, compared to the current date, the next image is too
106  * late, the thread will perform an idle loop. This time should be
107  * at least VOUT_IDLE_SLEEP plus the time required to render a few
108  * images, to avoid trashing of decoded images */
109 #define VOUT_DISPLAY_DELAY              ((int)(0.200*CLOCK_FREQ))
110
111 /* Better be in advance when awakening than late... */
112 #define VOUT_MWAIT_TOLERANCE            ((mtime_t)(0.020*CLOCK_FREQ))
113
114 /* Minimum number of direct pictures the video output will accept without
115  * creating additional pictures in system memory */
116 #ifdef OPTIMIZE_MEMORY
117 #   define VOUT_MIN_DIRECT_PICTURES        (VOUT_MAX_PICTURES/2)
118 #else
119 #   define VOUT_MIN_DIRECT_PICTURES        (3*VOUT_MAX_PICTURES/4)
120 #endif
121
122 /*****************************************************************************
123  * Video Filter2 functions
124  *****************************************************************************/
125 static picture_t *video_new_buffer_filter( filter_t *p_filter )
126 {
127     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
128     picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
129
130     p_picture->i_status = READY_PICTURE;
131
132     return p_picture;
133 }
134
135 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
136 {
137     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
138
139     vlc_mutex_lock( &p_vout->picture_lock );
140     vout_UsePictureLocked( p_vout, p_pic );
141     vlc_mutex_unlock( &p_vout->picture_lock );
142 }
143
144 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
145 {
146     p_filter->pf_vout_buffer_new = video_new_buffer_filter;
147     p_filter->pf_vout_buffer_del = video_del_buffer_filter;
148     p_filter->p_owner = p_data; /* p_vout */
149     return VLC_SUCCESS;
150 }
151
152 /*****************************************************************************
153  * vout_Request: find a video output thread, create one, or destroy one.
154  *****************************************************************************
155  * This function looks for a video output thread matching the current
156  * properties. If not found, it spawns a new one.
157  *****************************************************************************/
158 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
159                                video_format_t *p_fmt )
160 {
161     if( !p_fmt )
162     {
163         /* Video output is no longer used.
164          * TODO: support for reusing video outputs with proper _thread-safe_
165          * reference handling. */
166         if( p_vout )
167             vout_CloseAndRelease( p_vout );
168         return NULL;
169     }
170
171     /* If a video output was provided, lock it, otherwise look for one. */
172     if( p_vout )
173     {
174         vlc_object_hold( p_vout );
175     }
176
177     /* TODO: find a suitable unused video output */
178
179     /* If we now have a video output, check it has the right properties */
180     if( p_vout )
181     {
182         char *psz_filter_chain;
183         vlc_value_t val;
184
185         vlc_mutex_lock( &p_vout->change_lock );
186
187         /* We don't directly check for the "vout-filter" variable for obvious
188          * performance reasons. */
189         if( p_vout->p->b_filter_change )
190         {
191             var_Get( p_vout, "vout-filter", &val );
192             psz_filter_chain = val.psz_string;
193
194             if( psz_filter_chain && !*psz_filter_chain )
195             {
196                 free( psz_filter_chain );
197                 psz_filter_chain = NULL;
198             }
199             if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
200             {
201                 free( p_vout->p->psz_filter_chain );
202                 p_vout->p->psz_filter_chain = NULL;
203             }
204
205             if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
206             {
207                 p_vout->p->b_filter_change = false;
208             }
209
210             free( psz_filter_chain );
211         }
212
213         if( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ||
214             p_vout->fmt_render.i_width != p_fmt->i_width ||
215             p_vout->fmt_render.i_height != p_fmt->i_height ||
216             p_vout->p->b_filter_change )
217         {
218             vlc_mutex_unlock( &p_vout->change_lock );
219
220             /* We are not interested in this format, close this vout */
221             vout_CloseAndRelease( p_vout );
222             vlc_object_release( p_vout );
223             p_vout = NULL;
224         }
225         else
226         {
227             /* This video output is cool! Hijack it. */
228             if( p_vout->fmt_render.i_aspect != p_fmt->i_aspect )
229             {
230                 /* Correct aspect ratio on change
231                  * FIXME factorize this code with other aspect ration related code */
232                 unsigned int i_sar_num;
233                 unsigned int i_sar_den;
234                 unsigned int i_aspect;
235
236                 i_aspect = p_fmt->i_aspect;
237                 vlc_ureduce( &i_sar_num, &i_sar_den,
238                              p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
239 #if 0
240                 /* What's that, it does not seems to be used correcly everywhere
241                  * beside the previous p_vout->fmt_render.i_aspect != p_fmt->i_aspect
242                  * should be fixed to use it too then */
243                 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
244                 {
245                     i_sar_num *= p_vout->i_par_den;
246                     i_sar_den *= p_vout->i_par_num;
247                     i_aspect = i_aspect * p_vout->i_par_den / p_vout->i_par_num;
248                 }
249 #endif
250
251                 if( i_sar_num > 0 && i_sar_den > 0 && i_aspect > 0 )
252                 {
253                     p_vout->fmt_in.i_sar_num = i_sar_num;
254                     p_vout->fmt_in.i_sar_den = i_sar_den;
255                     p_vout->fmt_in.i_aspect  = i_aspect;
256
257                     p_vout->fmt_render.i_sar_num = i_sar_num;
258                     p_vout->fmt_render.i_sar_den = i_sar_den;
259                     p_vout->fmt_render.i_aspect  = i_aspect;
260
261                     p_vout->render.i_aspect   = i_aspect;
262
263                     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
264
265                 }
266             }
267             vlc_mutex_unlock( &p_vout->change_lock );
268
269             vlc_object_release( p_vout );
270         }
271
272         if( p_vout )
273         {
274             msg_Dbg( p_this, "reusing provided vout" );
275
276             spu_Attach( p_vout->p_spu, p_this, true );
277
278             vlc_object_detach( p_vout );
279             vlc_object_attach( p_vout, p_this );
280         }
281     }
282
283     if( !p_vout )
284     {
285         msg_Dbg( p_this, "no usable vout present, spawning one" );
286
287         p_vout = vout_Create( p_this, p_fmt );
288     }
289
290     return p_vout;
291 }
292
293 /*****************************************************************************
294  * vout_Create: creates a new video output thread
295  *****************************************************************************
296  * This function creates a new video output thread, and returns a pointer
297  * to its description. On error, it returns NULL.
298  *****************************************************************************/
299 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
300 {
301     vout_thread_t  * p_vout;                            /* thread descriptor */
302     int              i_index;                               /* loop variable */
303     vlc_value_t      val, text;
304
305     unsigned int i_width = p_fmt->i_width;
306     unsigned int i_height = p_fmt->i_height;
307     vlc_fourcc_t i_chroma = p_fmt->i_chroma;
308     unsigned int i_aspect = p_fmt->i_aspect;
309
310     config_chain_t *p_cfg;
311     char *psz_parser;
312     char *psz_name;
313
314     if( i_width <= 0 || i_height <= 0 || i_aspect <= 0 )
315         return NULL;
316
317     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
318                  p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
319     if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
320         return NULL;
321
322     /* Allocate descriptor */
323     static const char typename[] = "video output";
324     p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
325                                 typename );
326     if( p_vout == NULL )
327         return NULL;
328
329     /* */
330     p_vout->p = calloc( 1, sizeof(*p_vout->p) );
331     if( !p_vout->p )
332     {
333         vlc_object_release( p_vout );
334         return NULL;
335     }
336
337     /* Initialize pictures - translation tables and functions
338      * will be initialized later in InitThread */
339     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
340     {
341         p_vout->p_picture[i_index].pf_lock = NULL;
342         p_vout->p_picture[i_index].pf_unlock = NULL;
343         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
344         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
345         p_vout->p_picture[i_index].b_slow   = 0;
346     }
347
348     /* No images in the heap */
349     p_vout->i_heap_size = 0;
350
351     /* Initialize the rendering heap */
352     I_RENDERPICTURES = 0;
353
354     p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
355     p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
356
357     p_vout->render.i_width    = i_width;
358     p_vout->render.i_height   = i_height;
359     p_vout->render.i_chroma   = i_chroma;
360     p_vout->render.i_aspect   = i_aspect;
361
362     p_vout->render.i_rmask    = p_fmt->i_rmask;
363     p_vout->render.i_gmask    = p_fmt->i_gmask;
364     p_vout->render.i_bmask    = p_fmt->i_bmask;
365
366     p_vout->render.i_last_used_pic = -1;
367     p_vout->render.b_allow_modify_pics = 1;
368
369     /* Zero the output heap */
370     I_OUTPUTPICTURES = 0;
371     p_vout->output.i_width    = 0;
372     p_vout->output.i_height   = 0;
373     p_vout->output.i_chroma   = 0;
374     p_vout->output.i_aspect   = 0;
375
376     p_vout->output.i_rmask    = 0;
377     p_vout->output.i_gmask    = 0;
378     p_vout->output.i_bmask    = 0;
379
380     /* Initialize misc stuff */
381     p_vout->i_changes    = 0;
382     p_vout->b_autoscale  = 1;
383     p_vout->i_zoom      = ZOOM_FP_FACTOR;
384     p_vout->b_fullscreen = 0;
385     p_vout->i_alignment  = 0;
386     p_vout->p->render_time  = 10;
387     p_vout->p->c_fps_samples = 0;
388     p_vout->p->i_picture_lost = 0;
389     p_vout->p->i_picture_displayed = 0;
390     p_vout->p->b_filter_change = 0;
391     p_vout->p->b_paused = false;
392     p_vout->p->i_pause_date = 0;
393     p_vout->pf_control = NULL;
394     p_vout->p->i_par_num =
395     p_vout->p->i_par_den = 1;
396     p_vout->p->p_picture_displayed = NULL;
397     p_vout->p->i_picture_displayed_date = 0;
398     p_vout->p->b_picture_displayed = false;
399     p_vout->p->b_picture_empty = false;
400     p_vout->p->i_picture_qtype = QTYPE_NONE;
401
402     /* Initialize locks */
403     vlc_mutex_init( &p_vout->picture_lock );
404     vlc_cond_init( &p_vout->p->picture_wait );
405     vlc_mutex_init( &p_vout->change_lock );
406     vlc_mutex_init( &p_vout->p->vfilter_lock );
407
408     /* Mouse coordinates */
409     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
410     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
411     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
412     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
413     var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
414
415     /* Initialize subpicture unit */
416     p_vout->p_spu = spu_Create( p_vout );
417     spu_Attach( p_vout->p_spu, p_parent, true );
418
419     /* Attach the new object now so we can use var inheritance below */
420     vlc_object_attach( p_vout, p_parent );
421
422     spu_Init( p_vout->p_spu );
423
424     /* Take care of some "interface/control" related initialisations */
425     vout_IntfInit( p_vout );
426
427     /* If the parent is not a VOUT object, that means we are at the start of
428      * the video output pipe */
429     if( vlc_internals( p_parent )->i_object_type != VLC_OBJECT_VOUT )
430     {
431         /* Look for the default filter configuration */
432         p_vout->p->psz_filter_chain =
433             var_CreateGetStringCommand( p_vout, "vout-filter" );
434
435         /* Apply video filter2 objects on the first vout */
436         p_vout->p->psz_vf2 =
437             var_CreateGetStringCommand( p_vout, "video-filter" );
438     }
439     else
440     {
441         /* continue the parent's filter chain */
442         char *psz_tmp;
443
444         /* Ugly hack to jump to our configuration chain */
445         p_vout->p->psz_filter_chain
446             = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
447         p_vout->p->psz_filter_chain
448             = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
449         config_ChainDestroy( p_cfg );
450         free( psz_tmp );
451
452         /* Create a video filter2 var ... but don't inherit values */
453         var_Create( p_vout, "video-filter",
454                     VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
455         p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
456     }
457
458     var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
459     p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
460         false, video_filter_buffer_allocation_init, NULL, p_vout );
461
462     /* Choose the video output module */
463     if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
464     {
465         var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
466         var_Get( p_vout, "vout", &val );
467         psz_parser = val.psz_string;
468     }
469     else
470     {
471         psz_parser = strdup( p_vout->p->psz_filter_chain );
472         p_vout->p->b_title_show = false;
473     }
474
475     /* Create the vout thread */
476     char* psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
477     free( psz_parser );
478     free( psz_tmp );
479     p_vout->p_cfg = p_cfg;
480     p_vout->p_module = module_need( p_vout,
481         ( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain ) ?
482         "video filter" : "video output", psz_name, p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain );
483     free( psz_name );
484
485     vlc_object_set_destructor( p_vout, vout_Destructor );
486
487     if( p_vout->p_module == NULL )
488     {
489         msg_Err( p_vout, "no suitable vout module" );
490         spu_Attach( p_vout->p_spu, p_vout, false );
491         spu_Destroy( p_vout->p_spu );
492         p_vout->p_spu = NULL;
493         vlc_object_release( p_vout );
494         return NULL;
495     }
496
497     /* Create a few object variables for interface interaction */
498     /* Deinterlacing */
499     var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
500     text.psz_string = _("Deinterlace");
501     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
502     val.psz_string = (char *)""; text.psz_string = _("Disable");
503     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
504     val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
505     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
506     val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
507     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
508     val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
509     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
510     val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
511     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
512     val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
513     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
514     val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
515     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
516
517     if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
518     {
519         var_Set( p_vout, "deinterlace", val );
520         free( val.psz_string );
521     }
522     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
523
524     /* */
525     var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
526     text.psz_string = _("Filters");
527     var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
528     var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
529
530     vlc_cond_init( &p_vout->p->change_wait );
531     if( vlc_clone( &p_vout->p->thread, RunThread, p_vout,
532                    VLC_THREAD_PRIORITY_OUTPUT ) )
533     {
534         module_unneed( p_vout, p_vout->p_module );
535         p_vout->p_module = NULL;
536         vlc_object_release( p_vout );
537         return NULL;
538     }
539
540     vlc_mutex_lock( &p_vout->change_lock );
541     while( !p_vout->p->b_ready )
542     {   /* We are (ab)using the same condition in opposite directions for
543          * b_ready and b_done. This works because of the strict ordering. */
544         assert( !p_vout->p->b_done );
545         vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
546     }
547     vlc_mutex_unlock( &p_vout->change_lock );
548
549     if( p_vout->b_error )
550     {
551         msg_Err( p_vout, "video output creation failed" );
552         vout_CloseAndRelease( p_vout );
553         return NULL;
554     }
555
556     return p_vout;
557 }
558
559 /*****************************************************************************
560  * vout_Close: Close a vout created by vout_Create.
561  *****************************************************************************
562  * You HAVE to call it on vout created by vout_Create before vlc_object_release.
563  * You should NEVER call it on vout not obtained through vout_Create
564  * (like with vout_Request or vlc_object_find.)
565  * You can use vout_CloseAndRelease() as a convenience method.
566  *****************************************************************************/
567 void vout_Close( vout_thread_t *p_vout )
568 {
569     assert( p_vout );
570
571     vlc_mutex_lock( &p_vout->change_lock );
572     p_vout->p->b_done = true;
573     vlc_cond_signal( &p_vout->p->change_wait );
574     vlc_mutex_unlock( &p_vout->change_lock );
575     vlc_join( p_vout->p->thread, NULL );
576     module_unneed( p_vout, p_vout->p_module );
577     p_vout->p_module = NULL;
578 }
579
580 /* */
581 static void vout_Destructor( vlc_object_t * p_this )
582 {
583     vout_thread_t *p_vout = (vout_thread_t *)p_this;
584
585     /* Make sure the vout was stopped first */
586     assert( !p_vout->p_module );
587
588     /* */
589     if( p_vout->p_spu )
590         spu_Destroy( p_vout->p_spu );
591
592     /* Destroy the locks */
593     vlc_cond_destroy( &p_vout->p->change_wait );
594     vlc_cond_destroy( &p_vout->p->picture_wait );
595     vlc_mutex_destroy( &p_vout->picture_lock );
596     vlc_mutex_destroy( &p_vout->change_lock );
597     vlc_mutex_destroy( &p_vout->p->vfilter_lock );
598
599     free( p_vout->p->psz_filter_chain );
600     free( p_vout->p->psz_title );
601
602     config_ChainDestroy( p_vout->p_cfg );
603
604     free( p_vout->p );
605
606 #ifndef __APPLE__
607     vout_thread_t *p_another_vout;
608
609     /* This is a dirty hack mostly for Linux, where there is no way to get the
610      * GUI back if you closed it while playing video. This is solved in
611      * Mac OS X, where we have this novelty called menubar, that will always
612      * allow you access to the applications main functionality. They should try
613      * that on linux sometime. */
614     p_another_vout = vlc_object_find( p_this->p_libvlc,
615                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
616     if( p_another_vout == NULL )
617         var_SetBool( p_this->p_libvlc, "intf-show", true );
618     else
619         vlc_object_release( p_another_vout );
620 #endif
621 }
622
623 /* */
624 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
625 {
626     vlc_mutex_lock( &p_vout->change_lock );
627
628     assert( !p_vout->p->b_paused || !b_paused );
629
630     vlc_mutex_lock( &p_vout->picture_lock );
631
632     p_vout->p->i_picture_displayed_date = 0;
633
634     if( p_vout->p->b_paused )
635     {
636         const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
637
638         for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
639         {
640             picture_t *p_pic = PP_RENDERPICTURE[i_index];
641
642             if( p_pic->i_status == READY_PICTURE )
643                 p_pic->date += i_duration;
644         }
645         vlc_cond_signal( &p_vout->p->picture_wait );
646         vlc_mutex_unlock( &p_vout->picture_lock );
647
648         spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
649     }
650     else
651     {
652         vlc_mutex_unlock( &p_vout->picture_lock );
653     }
654     p_vout->p->b_paused = b_paused;
655     p_vout->p->i_pause_date = i_date;
656
657     vlc_mutex_unlock( &p_vout->change_lock );
658 }
659
660 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
661 {
662     vlc_mutex_lock( &p_vout->change_lock );
663
664     *pi_displayed = p_vout->p->i_picture_displayed;
665     *pi_lost = p_vout->p->i_picture_lost;
666
667     p_vout->p->i_picture_displayed = 0;
668     p_vout->p->i_picture_lost = 0;
669
670     vlc_mutex_unlock( &p_vout->change_lock );
671 }
672
673 void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
674 {
675     vlc_mutex_lock( &p_vout->picture_lock );
676     p_vout->p->i_picture_displayed_date = 0;
677     for( int i = 0; i < p_vout->render.i_pictures; i++ )
678     {
679         picture_t *p_pic = p_vout->render.pp_picture[i];
680
681         if( p_pic->i_status == READY_PICTURE ||
682             p_pic->i_status == DISPLAYED_PICTURE )
683         {
684             /* We cannot change picture status if it is in READY_PICTURE state,
685              * Just make sure they won't be displayed */
686             if( p_pic->date > i_date )
687                 p_pic->date = i_date;
688         }
689     }
690     vlc_cond_signal( &p_vout->p->picture_wait );
691     vlc_mutex_unlock( &p_vout->picture_lock );
692 }
693
694 void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
695 {
696     int i_pic, i_ready_pic;
697
698     vlc_mutex_lock( &p_vout->picture_lock );
699
700     for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
701     {
702         const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
703
704         if( p_pic->i_status == READY_PICTURE )
705         {
706             i_ready_pic++;
707             /* If we have at least 2 ready pictures, wait for the vout thread to
708              * process one */
709             if( i_ready_pic >= 2 )
710                 break;
711
712             continue;
713         }
714
715         if( p_pic->i_status == DISPLAYED_PICTURE )
716         {
717             /* If at least one displayed picture is not referenced
718              * let vout free it */
719             if( p_pic->i_refcount == 0 )
720                 break;
721         }
722     }
723     if( i_pic < p_vout->render.i_pictures && !b_forced )
724     {
725         vlc_mutex_unlock( &p_vout->picture_lock );
726         return;
727     }
728
729     /* Too many pictures are still referenced, there is probably a bug
730      * with the decoder */
731     if( !b_forced )
732         msg_Err( p_vout, "pictures leaked, resetting the heap" );
733
734     /* Just free all the pictures */
735     for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
736     {
737         picture_t *p_pic = p_vout->render.pp_picture[i_pic];
738
739         msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
740         p_pic->i_refcount = 0;
741
742         switch( p_pic->i_status )
743         {
744         case READY_PICTURE:
745         case DISPLAYED_PICTURE:
746         case RESERVED_PICTURE:
747             if( p_pic != p_vout->p->p_picture_displayed )
748                 vout_UsePictureLocked( p_vout, p_pic );
749             break;
750         }
751     }
752     vlc_cond_signal( &p_vout->p->picture_wait );
753     vlc_mutex_unlock( &p_vout->picture_lock );
754 }
755 void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
756 {
757     vlc_mutex_lock( &p_vout->picture_lock );
758
759     const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
760
761     p_vout->p->b_picture_displayed = false;
762     p_vout->p->b_picture_empty = false;
763     if( p_vout->p->p_picture_displayed )
764     {
765         p_vout->p->p_picture_displayed->date = 1;
766         vlc_cond_signal( &p_vout->p->picture_wait );
767     }
768
769     while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
770         vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->picture_lock );
771
772     *pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
773
774     /* TODO advance subpicture by the duration ... */
775
776     vlc_mutex_unlock( &p_vout->picture_lock );
777 }
778
779 void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
780 {
781     assert( psz_title );
782
783     if( !config_GetInt( p_vout, "osd" ) )
784         return;
785
786     vlc_mutex_lock( &p_vout->change_lock );
787     free( p_vout->p->psz_title );
788     p_vout->p->psz_title = strdup( psz_title );
789     vlc_mutex_unlock( &p_vout->change_lock );
790 }
791
792 /*****************************************************************************
793  * InitThread: initialize video output thread
794  *****************************************************************************
795  * This function is called from RunThread and performs the second step of the
796  * initialization. It returns 0 on success. Note that the thread's flag are not
797  * modified inside this function.
798  * XXX You have to enter it with change_lock taken.
799  *****************************************************************************/
800 static int ChromaCreate( vout_thread_t *p_vout );
801 static void ChromaDestroy( vout_thread_t *p_vout );
802
803 static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
804 {
805      if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
806          return false;
807
808      if( p_output->i_chroma != FOURCC_RV15 &&
809          p_output->i_chroma != FOURCC_RV16 &&
810          p_output->i_chroma != FOURCC_RV24 &&
811          p_output->i_chroma != FOURCC_RV32 )
812          return true;
813
814      return p_output->i_rmask == p_render->i_rmask &&
815             p_output->i_gmask == p_render->i_gmask &&
816             p_output->i_bmask == p_render->i_bmask;
817 }
818
819 static int InitThread( vout_thread_t *p_vout )
820 {
821     int i, i_aspect_x, i_aspect_y;
822
823     /* Initialize output method, it allocates direct buffers for us */
824     if( p_vout->pf_init( p_vout ) )
825         return VLC_EGENERIC;
826
827     p_vout->p->p_picture_displayed = NULL;
828
829     if( !I_OUTPUTPICTURES )
830     {
831         msg_Err( p_vout, "plugin was unable to allocate at least "
832                          "one direct buffer" );
833         p_vout->pf_end( p_vout );
834         return VLC_EGENERIC;
835     }
836
837     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
838     {
839         msg_Err( p_vout, "plugin allocated too many direct buffers, "
840                          "our internal buffers must have overflown." );
841         p_vout->pf_end( p_vout );
842         return VLC_EGENERIC;
843     }
844
845     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
846
847     AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
848
849     AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
850
851     if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
852     {
853         p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
854             p_vout->output.i_width;
855         p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
856             p_vout->output.i_height;
857         p_vout->fmt_out.i_x_offset =  p_vout->fmt_out.i_y_offset = 0;
858
859         p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
860         p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
861     }
862     if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
863     {
864         p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
865             p_vout->fmt_out.i_height;
866         p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
867             p_vout->fmt_out.i_width;
868     }
869
870     vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
871                  p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
872
873     AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
874
875     /* FIXME removed the need of both fmt_* and heap infos */
876     /* Calculate shifts from system-updated masks */
877     PictureHeapFixRgb( &p_vout->render );
878     VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
879
880     PictureHeapFixRgb( &p_vout->output );
881     VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
882
883     /* print some usefull debug info about different vout formats
884      */
885     msg_Dbg( p_vout, "pic render sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
886              p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
887              p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
888              p_vout->fmt_render.i_visible_width,
889              p_vout->fmt_render.i_visible_height,
890              (char*)&p_vout->fmt_render.i_chroma,
891              i_aspect_x, i_aspect_y,
892              p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den,
893              p_vout->fmt_render.i_rmask, p_vout->fmt_render.i_gmask, p_vout->fmt_render.i_bmask );
894
895     msg_Dbg( p_vout, "pic in sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
896              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
897              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
898              p_vout->fmt_in.i_visible_width,
899              p_vout->fmt_in.i_visible_height,
900              (char*)&p_vout->fmt_in.i_chroma,
901              i_aspect_x, i_aspect_y,
902              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den,
903              p_vout->fmt_in.i_rmask, p_vout->fmt_in.i_gmask, p_vout->fmt_in.i_bmask );
904
905     msg_Dbg( p_vout, "pic out sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
906              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
907              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
908              p_vout->fmt_out.i_visible_width,
909              p_vout->fmt_out.i_visible_height,
910              (char*)&p_vout->fmt_out.i_chroma,
911              i_aspect_x, i_aspect_y,
912              p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den,
913              p_vout->fmt_out.i_rmask, p_vout->fmt_out.i_gmask, p_vout->fmt_out.i_bmask );
914
915     /* Check whether we managed to create direct buffers similar to
916      * the render buffers, ie same size and chroma */
917     if( ( p_vout->output.i_width == p_vout->render.i_width )
918      && ( p_vout->output.i_height == p_vout->render.i_height )
919      && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
920     {
921         /* Cool ! We have direct buffers, we can ask the decoder to
922          * directly decode into them ! Map the first render buffers to
923          * the first direct buffers, but keep the first direct buffer
924          * for memcpy operations */
925         p_vout->p->b_direct = true;
926
927         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
928         {
929             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
930                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
931                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
932             {
933                 /* We have enough direct buffers so there's no need to
934                  * try to use system memory buffers. */
935                 break;
936             }
937             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
938             I_RENDERPICTURES++;
939         }
940
941         msg_Dbg( p_vout, "direct render, mapping "
942                  "render pictures 0-%i to system pictures 1-%i",
943                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
944     }
945     else
946     {
947         /* Rats... Something is wrong here, we could not find an output
948          * plugin able to directly render what we decode. See if we can
949          * find a chroma plugin to do the conversion */
950         p_vout->p->b_direct = false;
951
952         if( ChromaCreate( p_vout ) )
953         {
954             p_vout->pf_end( p_vout );
955             return VLC_EGENERIC;
956         }
957
958         msg_Dbg( p_vout, "indirect render, mapping "
959                  "render pictures 0-%i to system pictures %i-%i",
960                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
961                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
962
963         /* Append render buffers after the direct buffers */
964         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
965         {
966             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
967             I_RENDERPICTURES++;
968
969             /* Check if we have enough render pictures */
970             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
971                 break;
972         }
973     }
974
975     /* Link pictures back to their heap */
976     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
977     {
978         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
979     }
980
981     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
982     {
983         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
984     }
985
986     return VLC_SUCCESS;
987 }
988
989 /*****************************************************************************
990  * RunThread: video output thread
991  *****************************************************************************
992  * Video output thread. This function does only returns when the thread is
993  * terminated. It handles the pictures arriving in the video heap and the
994  * display device events.
995  *****************************************************************************/
996 static void* RunThread( void *p_this )
997 {
998     vout_thread_t *p_vout = p_this;
999     int             i_idle_loops = 0;  /* loops without displaying a picture */
1000     int             i_picture_qtype_last = QTYPE_NONE;
1001
1002     bool            b_drop_late;
1003
1004     /*
1005      * Initialize thread
1006      */
1007     vlc_mutex_lock( &p_vout->change_lock );
1008     p_vout->b_error = InitThread( p_vout );
1009
1010     b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
1011
1012     /* signal the creation of the vout */
1013     p_vout->p->b_ready = true;
1014     vlc_cond_signal( &p_vout->p->change_wait );
1015
1016     if( p_vout->b_error )
1017     {
1018         EndThread( p_vout );
1019         vlc_mutex_unlock( &p_vout->change_lock );
1020         return NULL;
1021     }
1022
1023     /*
1024      * Main loop - it is not executed if an error occurred during
1025      * initialization
1026      */
1027     while( !p_vout->p->b_done && !p_vout->b_error )
1028     {
1029         /* Initialize loop variables */
1030         const mtime_t current_date = mdate();
1031         picture_t *p_picture;
1032         picture_t *p_filtered_picture;
1033         mtime_t display_date;
1034         picture_t *p_directbuffer;
1035         int i_index;
1036
1037         if( p_vout->p->b_title_show && p_vout->p->psz_title )
1038             DisplayTitleOnOSD( p_vout );
1039
1040         vlc_mutex_lock( &p_vout->picture_lock );
1041
1042         /* Look for the earliest picture but after the last displayed one */
1043         picture_t *p_last = p_vout->p->p_picture_displayed;;
1044
1045         p_picture = NULL;
1046         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
1047         {
1048             picture_t *p_pic = PP_RENDERPICTURE[i_index];
1049
1050             if( p_pic->i_status != READY_PICTURE )
1051                 continue;
1052
1053             if( p_vout->p->b_paused && p_last && p_last->date > 1 )
1054                 continue;
1055
1056             if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
1057             {
1058                 /* Drop old picture */
1059                 vout_UsePictureLocked( p_vout, p_pic );
1060             }
1061             else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
1062                      p_pic->date < current_date + p_vout->p->render_time &&
1063                      b_drop_late )
1064             {
1065                 /* Picture is late: it will be destroyed and the thread
1066                  * will directly choose the next picture */
1067                 vout_UsePictureLocked( p_vout, p_pic );
1068                 p_vout->p->i_picture_lost++;
1069
1070                 msg_Warn( p_vout, "late picture skipped (%"PRId64" > %d)",
1071                                   current_date - p_pic->date, - p_vout->p->render_time );
1072             }
1073             else if( ( !p_last || p_last->date < p_pic->date ) &&
1074                      ( p_picture == NULL || p_pic->date < p_picture->date ) )
1075             {
1076                 p_picture = p_pic;
1077             }
1078         }
1079         if( !p_picture )
1080         {
1081             p_picture = p_last;
1082
1083             if( !p_vout->p->b_picture_empty )
1084             {
1085                 p_vout->p->b_picture_empty = true;
1086                 vlc_cond_signal( &p_vout->p->picture_wait );
1087             }
1088         }
1089
1090         display_date = 0;
1091         if( p_picture )
1092         {
1093             display_date = p_picture->date;
1094
1095             /* If we found better than the last picture, destroy it */
1096             if( p_last && p_picture != p_last )
1097             {
1098                 vout_UsePictureLocked( p_vout, p_last );
1099                 p_vout->p->p_picture_displayed = p_last = NULL;
1100             }
1101
1102             /* Compute FPS rate */
1103             p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1104
1105             if( !p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1106             {
1107                 /* A picture is ready to be rendered, but its rendering date
1108                  * is far from the current one so the thread will perform an
1109                  * empty loop as if no picture were found. The picture state
1110                  * is unchanged */
1111                 p_picture    = NULL;
1112                 display_date = 0;
1113             }
1114             else if( p_picture == p_last )
1115             {
1116                 /* We are asked to repeat the previous picture, but we first
1117                  * wait for a couple of idle loops */
1118                 if( i_idle_loops < 4 )
1119                 {
1120                     p_picture    = NULL;
1121                     display_date = 0;
1122                 }
1123                 else
1124                 {
1125                     /* We set the display date to something high, otherwise
1126                      * we'll have lots of problems with late pictures */
1127                     display_date = current_date + p_vout->p->render_time;
1128                 }
1129             }
1130             else if( p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1131             {
1132                 display_date = current_date + VOUT_DISPLAY_DELAY;
1133             }
1134
1135             if( p_picture )
1136             {
1137                 if( p_picture->date > 1 )
1138                 {
1139                     p_vout->p->i_picture_displayed_date = p_picture->date;
1140                     if( p_picture != p_last && !p_vout->p->b_picture_displayed )
1141                     {
1142                         p_vout->p->b_picture_displayed = true;
1143                         vlc_cond_signal( &p_vout->p->picture_wait );
1144                     }
1145                 }
1146                 p_vout->p->p_picture_displayed = p_picture;
1147             }
1148         }
1149
1150         /* */
1151         const int i_postproc_type = p_vout->p->i_picture_qtype;
1152         const int i_postproc_state = (p_vout->p->i_picture_qtype != QTYPE_NONE) - (i_picture_qtype_last != QTYPE_NONE);
1153
1154         vlc_mutex_unlock( &p_vout->picture_lock );
1155
1156         if( p_picture == NULL )
1157             i_idle_loops++;
1158
1159         p_filtered_picture = NULL;
1160         if( p_picture )
1161             p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
1162                                                            p_picture );
1163
1164         /* FIXME it is ugly that b_snapshot is not locked but I do not
1165          * know which lock to use (here and in the snapshot callback) */
1166         const bool b_snapshot = p_vout->p->b_snapshot && p_picture != NULL;
1167
1168         /*
1169          * Check for subpictures to display
1170          */
1171         subpicture_t *p_subpic = NULL;
1172         if( display_date > 0 )
1173             p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
1174                                             p_vout->p->b_paused, b_snapshot );
1175
1176         /*
1177          * Perform rendering
1178          */
1179         p_vout->p->i_picture_displayed++;
1180         p_directbuffer = vout_RenderPicture( p_vout, p_filtered_picture,
1181                                              p_subpic, p_vout->p->b_paused );
1182
1183         /*
1184          * Take a snapshot if requested
1185          */
1186         if( p_directbuffer && b_snapshot )
1187         {
1188             /* FIXME lock (see b_snapshot) */
1189             p_vout->p->b_snapshot = false;
1190
1191             vout_Snapshot( p_vout, p_directbuffer );
1192         }
1193
1194         /*
1195          * Call the plugin-specific rendering method if there is one
1196          */
1197         if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
1198         {
1199             /* Render the direct buffer returned by vout_RenderPicture */
1200             p_vout->pf_render( p_vout, p_directbuffer );
1201         }
1202
1203         /*
1204          * Sleep, wake up
1205          */
1206         if( display_date != 0 && p_directbuffer != NULL )
1207         {
1208             mtime_t current_render_time = mdate() - current_date;
1209             /* if render time is very large we don't include it in the mean */
1210             if( current_render_time < p_vout->p->render_time +
1211                 VOUT_DISPLAY_DELAY )
1212             {
1213                 /* Store render time using a sliding mean weighting to
1214                  * current value in a 3 to 1 ratio*/
1215                 p_vout->p->render_time *= 3;
1216                 p_vout->p->render_time += current_render_time;
1217                 p_vout->p->render_time >>= 2;
1218             }
1219             else
1220                 msg_Dbg( p_vout, "skipped big render time %d > %d", (int) current_render_time,
1221                  (int) (p_vout->p->render_time +VOUT_DISPLAY_DELAY ) ) ;
1222         }
1223
1224         /* Give back change lock */
1225         vlc_mutex_unlock( &p_vout->change_lock );
1226
1227         /* Sleep a while or until a given date */
1228         if( display_date != 0 )
1229         {
1230             /* If there are *vout* filters in the chain, better give them the picture
1231              * in advance */
1232             if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
1233             {
1234                 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1235             }
1236         }
1237         else
1238         {
1239             /* Wait until a frame is being sent or a spurious wakeup (not a problem here) */
1240             vlc_mutex_lock( &p_vout->picture_lock );
1241             vlc_cond_timedwait( &p_vout->p->picture_wait, &p_vout->picture_lock, current_date + VOUT_IDLE_SLEEP );
1242             vlc_mutex_unlock( &p_vout->picture_lock );
1243         }
1244
1245         /* On awakening, take back lock and send immediately picture
1246          * to display. */
1247         /* Note: p_vout->p->b_done could be true here and now */
1248         vlc_mutex_lock( &p_vout->change_lock );
1249
1250         /*
1251          * Display the previously rendered picture
1252          */
1253         if( p_filtered_picture != NULL && p_directbuffer != NULL )
1254         {
1255             /* Display the direct buffer returned by vout_RenderPicture */
1256             if( p_vout->pf_display )
1257                 p_vout->pf_display( p_vout, p_directbuffer );
1258
1259             /* Tell the vout this was the last picture and that it does not
1260              * need to be forced anymore. */
1261             p_picture->b_force = false;
1262         }
1263
1264         /* Drop the filtered picture if created by video filters */
1265         if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
1266         {
1267             vlc_mutex_lock( &p_vout->picture_lock );
1268             vout_UsePictureLocked( p_vout, p_filtered_picture );
1269             vlc_mutex_unlock( &p_vout->picture_lock );
1270         }
1271
1272         if( p_picture != NULL )
1273         {
1274             /* Reinitialize idle loop count */
1275             i_idle_loops = 0;
1276         }
1277
1278         /*
1279          * Check events and manage thread
1280          */
1281         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1282         {
1283             /* A fatal error occurred, and the thread must terminate
1284              * immediately, without displaying anything - setting b_error to 1
1285              * causes the immediate end of the main while() loop. */
1286             // FIXME pf_end
1287             p_vout->b_error = 1;
1288             break;
1289         }
1290
1291         while( p_vout->i_changes & VOUT_ON_TOP_CHANGE )
1292         {
1293             p_vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
1294             vlc_mutex_unlock( &p_vout->change_lock );
1295             vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, p_vout->b_on_top );
1296             vlc_mutex_lock( &p_vout->change_lock );
1297         }
1298
1299         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1300         {
1301             /* this must only happen when the vout plugin is incapable of
1302              * rescaling the picture itself. In this case we need to destroy
1303              * the current picture buffers and recreate new ones with the right
1304              * dimensions */
1305             int i;
1306
1307             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1308
1309             assert( !p_vout->p->b_direct );
1310
1311             ChromaDestroy( p_vout );
1312
1313             vlc_mutex_lock( &p_vout->picture_lock );
1314
1315             p_vout->pf_end( p_vout );
1316
1317             p_vout->p->p_picture_displayed = NULL;
1318             for( i = 0; i < I_OUTPUTPICTURES; i++ )
1319                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1320             vlc_cond_signal( &p_vout->p->picture_wait );
1321
1322             I_OUTPUTPICTURES = 0;
1323
1324             if( p_vout->pf_init( p_vout ) )
1325             {
1326                 msg_Err( p_vout, "cannot resize display" );
1327                 /* FIXME: pf_end will be called again in EndThread() */
1328                 p_vout->b_error = 1;
1329             }
1330
1331             vlc_mutex_unlock( &p_vout->picture_lock );
1332
1333             /* Need to reinitialise the chroma plugin. Since we might need
1334              * resizing too and it's not sure that we already had it,
1335              * recreate the chroma plugin chain from scratch. */
1336             /* dionoea */
1337             if( ChromaCreate( p_vout ) )
1338             {
1339                 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1340                 p_vout->b_error = 1;
1341             }
1342             if( p_vout->b_error )
1343                 break;
1344         }
1345
1346         if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1347         {
1348             /* This happens when the picture buffers need to be recreated.
1349              * This is useful on multimonitor displays for instance.
1350              *
1351              * Warning: This only works when the vout creates only 1 picture
1352              * buffer!! */
1353             p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1354
1355             if( !p_vout->p->b_direct )
1356                 ChromaDestroy( p_vout );
1357
1358             vlc_mutex_lock( &p_vout->picture_lock );
1359
1360             p_vout->pf_end( p_vout );
1361
1362             I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1363
1364             p_vout->b_error = InitThread( p_vout );
1365             if( p_vout->b_error )
1366                 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed" );
1367
1368             vlc_cond_signal( &p_vout->p->picture_wait );
1369             vlc_mutex_unlock( &p_vout->picture_lock );
1370
1371             if( p_vout->b_error )
1372                 break;
1373         }
1374
1375         /* Post processing */
1376         if( i_postproc_state == 1 )
1377             PostProcessEnable( p_vout );
1378         else if( i_postproc_state == -1 )
1379             PostProcessDisable( p_vout );
1380         if( i_postproc_state != 0 )
1381             i_picture_qtype_last = i_postproc_type;
1382
1383         /* Check for "video filter2" changes */
1384         vlc_mutex_lock( &p_vout->p->vfilter_lock );
1385         if( p_vout->p->psz_vf2 )
1386         {
1387             es_format_t fmt;
1388
1389             es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1390             fmt.video = p_vout->fmt_render;
1391             filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
1392
1393             if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
1394                                                p_vout->p->psz_vf2 ) < 0 )
1395                 msg_Err( p_vout, "Video filter chain creation failed" );
1396
1397             free( p_vout->p->psz_vf2 );
1398             p_vout->p->psz_vf2 = NULL;
1399
1400             if( i_picture_qtype_last != QTYPE_NONE )
1401                 PostProcessSetFilterQuality( p_vout );
1402         }
1403         vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1404     }
1405
1406     /*
1407      * Error loop - wait until the thread destruction is requested
1408      */
1409     if( p_vout->b_error )
1410         ErrorThread( p_vout );
1411
1412     /* End of thread */
1413     CleanThread( p_vout );
1414     EndThread( p_vout );
1415     vlc_mutex_unlock( &p_vout->change_lock );
1416
1417     return NULL;
1418 }
1419
1420 /*****************************************************************************
1421  * ErrorThread: RunThread() error loop
1422  *****************************************************************************
1423  * This function is called when an error occurred during thread main's loop.
1424  * The thread can still receive feed, but must be ready to terminate as soon
1425  * as possible.
1426  *****************************************************************************/
1427 static void ErrorThread( vout_thread_t *p_vout )
1428 {
1429     /* Wait until a `close' order */
1430     while( !p_vout->p->b_done )
1431         vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
1432 }
1433
1434 /*****************************************************************************
1435  * CleanThread: clean up after InitThread
1436  *****************************************************************************
1437  * This function is called after a sucessful
1438  * initialization. It frees all resources allocated by InitThread.
1439  * XXX You have to enter it with change_lock taken.
1440  *****************************************************************************/
1441 static void CleanThread( vout_thread_t *p_vout )
1442 {
1443     int     i_index;                                        /* index in heap */
1444
1445     if( !p_vout->p->b_direct )
1446         ChromaDestroy( p_vout );
1447
1448     /* Destroy all remaining pictures */
1449     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1450     {
1451         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1452         {
1453             free( p_vout->p_picture[i_index].p_data_orig );
1454         }
1455     }
1456
1457     /* Destroy translation tables */
1458     if( !p_vout->b_error )
1459         p_vout->pf_end( p_vout );
1460 }
1461
1462 /*****************************************************************************
1463  * EndThread: thread destruction
1464  *****************************************************************************
1465  * This function is called when the thread ends.
1466  * It frees all resources not allocated by InitThread.
1467  * XXX You have to enter it with change_lock taken.
1468  *****************************************************************************/
1469 static void EndThread( vout_thread_t *p_vout )
1470 {
1471 #ifdef STATS
1472     {
1473         struct tms cpu_usage;
1474         times( &cpu_usage );
1475
1476         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1477                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1478     }
1479 #endif
1480
1481     /* FIXME does that function *really* need to be called inside the thread ? */
1482
1483     /* Detach subpicture unit from both input and vout */
1484     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1485     vlc_object_detach( p_vout->p_spu );
1486
1487     /* Destroy the video filters2 */
1488     filter_chain_Delete( p_vout->p->p_vf2_chain );
1489 }
1490
1491 /* Thread helpers */
1492 static picture_t *ChromaGetPicture( filter_t *p_filter )
1493 {
1494     picture_t *p_pic = (picture_t *)p_filter->p_owner;
1495     p_filter->p_owner = NULL;
1496     return p_pic;
1497 }
1498
1499 static int ChromaCreate( vout_thread_t *p_vout )
1500 {
1501     static const char typename[] = "chroma";
1502     filter_t *p_chroma;
1503
1504     /* Choose the best module */
1505     p_chroma = p_vout->p->p_chroma =
1506         vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
1507                            typename );
1508
1509     vlc_object_attach( p_chroma, p_vout );
1510
1511     /* TODO: Set the fmt_in and fmt_out stuff here */
1512     p_chroma->fmt_in.video = p_vout->fmt_render;
1513     p_chroma->fmt_out.video = p_vout->fmt_out;
1514     VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
1515     VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
1516
1517     p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, false );
1518
1519     if( p_chroma->p_module == NULL )
1520     {
1521         msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1522                  (char*)&p_vout->render.i_chroma,
1523                  (char*)&p_vout->output.i_chroma,
1524                  p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1525                  p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1526                  );
1527
1528         vlc_object_release( p_vout->p->p_chroma );
1529         p_vout->p->p_chroma = NULL;
1530
1531         return VLC_EGENERIC;
1532     }
1533     p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1534     return VLC_SUCCESS;
1535 }
1536
1537 static void ChromaDestroy( vout_thread_t *p_vout )
1538 {
1539     assert( !p_vout->p->b_direct );
1540
1541     if( !p_vout->p->p_chroma )
1542         return;
1543
1544     module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
1545     vlc_object_release( p_vout->p->p_chroma );
1546     p_vout->p->p_chroma = NULL;
1547 }
1548
1549 /* following functions are local */
1550 static int ReduceHeight( int i_ratio )
1551 {
1552     int i_dummy = VOUT_ASPECT_FACTOR;
1553     int i_pgcd  = 1;
1554
1555     if( !i_ratio )
1556     {
1557         return i_pgcd;
1558     }
1559
1560     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1561     while( !(i_ratio & 1) && !(i_dummy & 1) )
1562     {
1563         i_ratio >>= 1;
1564         i_dummy >>= 1;
1565         i_pgcd  <<= 1;
1566     }
1567
1568     while( !(i_ratio % 3) && !(i_dummy % 3) )
1569     {
1570         i_ratio /= 3;
1571         i_dummy /= 3;
1572         i_pgcd  *= 3;
1573     }
1574
1575     while( !(i_ratio % 5) && !(i_dummy % 5) )
1576     {
1577         i_ratio /= 5;
1578         i_dummy /= 5;
1579         i_pgcd  *= 5;
1580     }
1581
1582     return i_pgcd;
1583 }
1584
1585 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1586 {
1587     unsigned int i_pgcd = ReduceHeight( i_aspect );
1588     *i_aspect_x = i_aspect / i_pgcd;
1589     *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1590 }
1591
1592 /**
1593  * This function copies all RGB informations from a picture_heap_t into
1594  * a video_format_t
1595  */
1596 static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
1597 {
1598     p_fmt->i_rmask = p_heap->i_rmask;
1599     p_fmt->i_gmask = p_heap->i_gmask;
1600     p_fmt->i_bmask = p_heap->i_bmask;
1601     p_fmt->i_rrshift = p_heap->i_rrshift;
1602     p_fmt->i_lrshift = p_heap->i_lrshift;
1603     p_fmt->i_rgshift = p_heap->i_rgshift;
1604     p_fmt->i_lgshift = p_heap->i_lgshift;
1605     p_fmt->i_rbshift = p_heap->i_rbshift;
1606     p_fmt->i_lbshift = p_heap->i_lbshift;
1607 }
1608
1609 /**
1610  * This funtion copes all RGB informations from a video_format_t into
1611  * a picture_heap_t
1612  */
1613 static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
1614 {
1615     p_heap->i_rmask = p_fmt->i_rmask;
1616     p_heap->i_gmask = p_fmt->i_gmask;
1617     p_heap->i_bmask = p_fmt->i_bmask;
1618     p_heap->i_rrshift = p_fmt->i_rrshift;
1619     p_heap->i_lrshift = p_fmt->i_lrshift;
1620     p_heap->i_rgshift = p_fmt->i_rgshift;
1621     p_heap->i_lgshift = p_fmt->i_lgshift;
1622     p_heap->i_rbshift = p_fmt->i_rbshift;
1623     p_heap->i_lbshift = p_fmt->i_lbshift;
1624 }
1625
1626 /**
1627  * This function computes rgb shifts from masks
1628  */
1629 static void PictureHeapFixRgb( picture_heap_t *p_heap )
1630 {
1631     video_format_t fmt;
1632
1633     /* */
1634     fmt.i_chroma = p_heap->i_chroma;
1635     VideoFormatImportRgb( &fmt, p_heap );
1636
1637     /* */
1638     video_format_FixRgb( &fmt );
1639
1640     VideoFormatExportRgb( &fmt, p_heap );
1641 }
1642
1643 /*****************************************************************************
1644  * object variables callbacks: a bunch of object variables are used by the
1645  * interfaces to interact with the vout.
1646  *****************************************************************************/
1647 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1648                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1649 {
1650     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1651     input_thread_t *p_input;
1652     vlc_value_t val;
1653
1654     char *psz_mode = newval.psz_string;
1655     char *psz_filter, *psz_deinterlace = NULL;
1656     (void)psz_cmd; (void)oldval; (void)p_data;
1657
1658     var_Get( p_vout, "vout-filter", &val );
1659     psz_filter = val.psz_string;
1660     if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1661
1662     if( !psz_mode || !*psz_mode )
1663     {
1664         if( psz_deinterlace )
1665         {
1666             char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1667             if( psz_src[0] == ':' ) psz_src++;
1668             memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1669         }
1670     }
1671     else if( !psz_deinterlace )
1672     {
1673         psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1674                               sizeof(":deinterlace") );
1675         if( psz_filter )
1676         {
1677             if( *psz_filter )
1678                 strcat( psz_filter, ":" );
1679             strcat( psz_filter, "deinterlace" );
1680         }
1681     }
1682
1683     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1684                                                  FIND_PARENT );
1685     if( !p_input )
1686     {
1687         free( psz_filter );
1688         return VLC_EGENERIC;
1689     }
1690
1691     if( psz_mode && *psz_mode )
1692     {
1693         /* Modify input as well because the vout might have to be restarted */
1694         val.psz_string = psz_mode;
1695         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1696         var_Set( p_input, "deinterlace-mode", val );
1697     }
1698     vlc_object_release( p_input );
1699
1700     val.b_bool = true;
1701     var_Set( p_vout, "intf-change", val );
1702
1703     val.psz_string = psz_filter;
1704     var_Set( p_vout, "vout-filter", val );
1705     free( psz_filter );
1706
1707     return VLC_SUCCESS;
1708 }
1709
1710 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1711                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1712 {
1713     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1714     input_thread_t *p_input;
1715     vlc_value_t val;
1716     (void)psz_cmd; (void)oldval; (void)p_data;
1717
1718     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1719                                                  FIND_PARENT );
1720     if (!p_input)
1721     {
1722         msg_Err( p_vout, "Input not found" );
1723         return( VLC_EGENERIC );
1724     }
1725
1726     val.b_bool = true;
1727     var_Set( p_vout, "intf-change", val );
1728
1729     /* Modify input as well because the vout might have to be restarted */
1730     val.psz_string = newval.psz_string;
1731     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1732
1733     var_Set( p_input, "vout-filter", val );
1734
1735     /* Now restart current video stream */
1736     input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1737     vlc_object_release( p_input );
1738
1739     return VLC_SUCCESS;
1740 }
1741
1742 /*****************************************************************************
1743  * Video Filter2 stuff
1744  *****************************************************************************/
1745 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1746                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1747 {
1748     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1749     (void)psz_cmd; (void)oldval; (void)p_data;
1750
1751     vlc_mutex_lock( &p_vout->p->vfilter_lock );
1752     p_vout->p->psz_vf2 = strdup( newval.psz_string );
1753     vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1754
1755     return VLC_SUCCESS;
1756 }
1757
1758 /*****************************************************************************
1759  * Post-processing
1760  *****************************************************************************/
1761 static bool PostProcessIsPresent( const char *psz_filter )
1762 {
1763      const char  *psz_pp = "postproc";
1764      const size_t i_pp = strlen(psz_pp);
1765     return psz_filter &&
1766            !strncmp( psz_filter, psz_pp, strlen(psz_pp) ) &&
1767            ( psz_filter[i_pp] == '\0' || psz_filter[i_pp] == ':' );
1768 }
1769
1770 static int PostProcessCallback( vlc_object_t *p_this, char const *psz_cmd,
1771                                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1772 {
1773     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1774     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1775
1776     static const char *psz_pp = "postproc";
1777
1778     char *psz_vf2 = var_GetString( p_vout, "video-filter" );
1779
1780     if( newval.i_int <= 0 )
1781     {
1782         if( PostProcessIsPresent( psz_vf2 ) )
1783         {
1784             strcpy( psz_vf2, &psz_vf2[strlen(psz_pp)] );
1785             if( *psz_vf2 == ':' )
1786                 strcpy( psz_vf2, &psz_vf2[1] );
1787         }
1788     }
1789     else
1790     {
1791         if( !PostProcessIsPresent( psz_vf2 ) )
1792         {
1793             if( psz_vf2 )
1794             {
1795                 char *psz_tmp = psz_vf2;
1796                 if( asprintf( &psz_vf2, "%s:%s", psz_pp, psz_tmp ) < 0 )
1797                     psz_vf2 = psz_tmp;
1798                 else
1799                     free( psz_tmp );
1800             }
1801             else
1802             {
1803                 psz_vf2 = strdup( psz_pp );
1804             }
1805         }
1806     }
1807     if( psz_vf2 )
1808         var_SetString( p_vout, "video-filter", psz_vf2 );
1809
1810     return VLC_SUCCESS;
1811 }
1812 static void PostProcessEnable( vout_thread_t *p_vout )
1813 {
1814     msg_Dbg( p_vout, "Post-processing available" );
1815     var_Create( p_vout, "postprocess", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
1816     for( int i = 0; i <= 6; i++ )
1817     {
1818         vlc_value_t val;
1819         vlc_value_t text;
1820         char psz_text[1+1];
1821
1822         val.i_int = i;
1823         snprintf( psz_text, sizeof(psz_text), "%d", i );
1824         if( i == 0 )
1825             text.psz_string = _("Disable");
1826         else
1827             text.psz_string = psz_text;
1828         var_Change( p_vout, "postprocess", VLC_VAR_ADDCHOICE, &val, &text );
1829     }
1830     var_AddCallback( p_vout, "postprocess", PostProcessCallback, NULL );
1831
1832     /* */
1833     char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1834     int i_postproc_q = 0;
1835     if( PostProcessIsPresent( psz_filter ) )
1836         i_postproc_q = var_CreateGetInteger( p_vout, "postproc-q" );
1837
1838     var_SetInteger( p_vout, "postprocess", i_postproc_q );
1839
1840     free( psz_filter );
1841 }
1842 static void PostProcessDisable( vout_thread_t *p_vout )
1843 {
1844     msg_Dbg( p_vout, "Post-processing no more available" );
1845     var_Destroy( p_vout, "postprocess" );
1846 }
1847 static void PostProcessSetFilterQuality( vout_thread_t *p_vout )
1848 {
1849     vlc_object_t *p_pp = vlc_object_find_name( p_vout, "postproc", FIND_CHILD );
1850     if( !p_pp )
1851         return;
1852
1853     var_SetInteger( p_pp, "postproc-q", var_GetInteger( p_vout, "postprocess" ) );
1854     vlc_object_release( p_pp );
1855 }
1856
1857
1858 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1859 {
1860     const mtime_t i_start = mdate();
1861     const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->i_title_timeout;
1862
1863     vlc_assert_locked( &p_vout->change_lock );
1864
1865     vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1866                            p_vout->p->psz_title, NULL,
1867                            p_vout->p->i_title_position,
1868                            30 + p_vout->fmt_in.i_width
1869                               - p_vout->fmt_in.i_visible_width
1870                               - p_vout->fmt_in.i_x_offset,
1871                            20 + p_vout->fmt_in.i_y_offset,
1872                            i_start, i_stop );
1873
1874     free( p_vout->p->psz_title );
1875
1876     p_vout->p->psz_title = NULL;
1877 }
1878