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