6fb4fe5a7288c4678d026e18d6a919c953893667
[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         ( vlc_object_t *  );
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     if( vlc_thread_create( p_vout, "video output", RunThread,
519                            VLC_THREAD_PRIORITY_OUTPUT, true ) )
520     {
521         module_unneed( p_vout, p_vout->p_module );
522         p_vout->p_module = NULL;
523         vlc_object_set_destructor( p_vout, vout_Destructor );
524         vlc_object_release( p_vout );
525         return NULL;
526     }
527
528     vlc_object_set_destructor( p_vout, vout_Destructor );
529
530     if( p_vout->b_error )
531     {
532         msg_Err( p_vout, "video output creation failed" );
533         vout_CloseAndRelease( p_vout );
534         return NULL;
535     }
536
537     return p_vout;
538 }
539
540 /*****************************************************************************
541  * vout_Close: Close a vout created by vout_Create.
542  *****************************************************************************
543  * You HAVE to call it on vout created by vout_Create before vlc_object_release.
544  * You should NEVER call it on vout not obtained through vout_Create
545  * (like with vout_Request or vlc_object_find.)
546  * You can use vout_CloseAndRelease() as a convenient method.
547  *****************************************************************************/
548 void vout_Close( vout_thread_t *p_vout )
549 {
550     assert( p_vout );
551
552     vlc_object_kill( p_vout );
553     vlc_thread_join( p_vout );
554     module_unneed( p_vout, p_vout->p_module );
555     p_vout->p_module = NULL;
556 }
557
558 /* */
559 static void vout_Destructor( vlc_object_t * p_this )
560 {
561     vout_thread_t *p_vout = (vout_thread_t *)p_this;
562
563     /* Make sure the vout was stopped first */
564     assert( !p_vout->p_module );
565
566     /* */
567     spu_Destroy( p_vout->p_spu );
568
569     /* Destroy the locks */
570     vlc_cond_destroy( &p_vout->p->picture_wait );
571     vlc_mutex_destroy( &p_vout->picture_lock );
572     vlc_mutex_destroy( &p_vout->change_lock );
573     vlc_mutex_destroy( &p_vout->p->vfilter_lock );
574
575     free( p_vout->p->psz_filter_chain );
576     free( p_vout->p->psz_title );
577
578     config_ChainDestroy( p_vout->p_cfg );
579
580     free( p_vout->p );
581
582 #ifndef __APPLE__
583     vout_thread_t *p_another_vout;
584
585     /* This is a dirty hack mostly for Linux, where there is no way to get the
586      * GUI back if you closed it while playing video. This is solved in
587      * Mac OS X, where we have this novelty called menubar, that will always
588      * allow you access to the applications main functionality. They should try
589      * that on linux sometime. */
590     p_another_vout = vlc_object_find( p_this->p_libvlc,
591                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
592     if( p_another_vout == NULL )
593         var_SetBool( p_this->p_libvlc, "intf-show", true );
594     else
595         vlc_object_release( p_another_vout );
596 #endif
597 }
598
599 /* */
600 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
601 {
602     vlc_object_lock( p_vout );
603
604     assert( !p_vout->p->b_paused || !b_paused );
605
606     vlc_mutex_lock( &p_vout->picture_lock );
607
608     p_vout->p->i_picture_displayed_date = 0;
609
610     if( p_vout->p->b_paused )
611     {
612         const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
613
614         for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
615         {
616             picture_t *p_pic = PP_RENDERPICTURE[i_index];
617
618             if( p_pic->i_status == READY_PICTURE )
619                 p_pic->date += i_duration;
620         }
621         vlc_cond_signal( &p_vout->p->picture_wait );
622         vlc_mutex_unlock( &p_vout->picture_lock );
623
624         spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
625     }
626     else
627     {
628         vlc_mutex_unlock( &p_vout->picture_lock );
629     }
630     p_vout->p->b_paused = b_paused;
631     p_vout->p->i_pause_date = i_date;
632
633     vlc_object_unlock( p_vout );
634 }
635 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
636 {
637     vlc_object_lock( p_vout );
638
639     *pi_displayed = p_vout->p->i_picture_displayed;
640     *pi_lost = p_vout->p->i_picture_lost;
641
642     p_vout->p->i_picture_displayed = 0;
643     p_vout->p->i_picture_lost = 0;
644
645     vlc_object_unlock( p_vout );
646 }
647 void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
648 {
649     vlc_mutex_lock( &p_vout->picture_lock );
650     p_vout->p->i_picture_displayed_date = 0;
651     for( int i = 0; i < p_vout->render.i_pictures; i++ )
652     {
653         picture_t *p_pic = p_vout->render.pp_picture[i];
654
655         if( p_pic->i_status == READY_PICTURE ||
656             p_pic->i_status == DISPLAYED_PICTURE )
657         {
658             /* We cannot change picture status if it is in READY_PICTURE state,
659              * Just make sure they won't be displayed */
660             if( p_pic->date > i_date )
661                 p_pic->date = i_date;
662         }
663     }
664     vlc_cond_signal( &p_vout->p->picture_wait );
665     vlc_mutex_unlock( &p_vout->picture_lock );
666 }
667 void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
668 {
669     int i_pic, i_ready_pic;
670
671     vlc_mutex_lock( &p_vout->picture_lock );
672
673     for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
674     {
675         const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
676
677         if( p_pic->i_status == READY_PICTURE )
678         {
679             i_ready_pic++;
680             /* If we have at least 2 ready pictures, wait for the vout thread to
681              * process one */
682             if( i_ready_pic >= 2 )
683                 break;
684
685             continue;
686         }
687
688         if( p_pic->i_status == DISPLAYED_PICTURE )
689         {
690             /* If at least one displayed picture is not referenced
691              * let vout free it */
692             if( p_pic->i_refcount == 0 )
693                 break;
694         }
695     }
696     if( i_pic < p_vout->render.i_pictures && !b_forced )
697     {
698         vlc_mutex_unlock( &p_vout->picture_lock );
699         return;
700     }
701
702     /* Too many pictures are still referenced, there is probably a bug
703      * with the decoder */
704     if( !b_forced )
705         msg_Err( p_vout, "pictures leaked, resetting the heap" );
706
707     /* Just free all the pictures */
708     for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
709     {
710         picture_t *p_pic = p_vout->render.pp_picture[i_pic];
711
712         msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
713         p_pic->i_refcount = 0;
714
715         switch( p_pic->i_status )
716         {
717         case READY_PICTURE:
718         case DISPLAYED_PICTURE:
719         case RESERVED_PICTURE:
720             if( p_pic != p_vout->p->p_picture_displayed )
721                 vout_UsePictureLocked( p_vout, p_pic );
722             break;
723         }
724     }
725     vlc_cond_signal( &p_vout->p->picture_wait );
726     vlc_mutex_unlock( &p_vout->picture_lock );
727 }
728 void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
729 {
730     vlc_mutex_lock( &p_vout->picture_lock );
731
732     const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
733
734     p_vout->p->b_picture_displayed = false;
735     p_vout->p->b_picture_empty = false;
736     if( p_vout->p->p_picture_displayed )
737     {
738         p_vout->p->p_picture_displayed->date = 1;
739         vlc_cond_signal( &p_vout->p->picture_wait );
740     }
741
742     while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
743         vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->picture_lock );
744
745     *pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
746
747     /* TODO advance subpicture by the duration ... */
748
749     vlc_mutex_unlock( &p_vout->picture_lock );
750 }
751 void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
752 {
753     assert( psz_title );
754
755     if( !config_GetInt( p_vout, "osd" ) )
756         return;
757
758     vlc_object_lock( p_vout );
759     free( p_vout->p->psz_title );
760     p_vout->p->psz_title = strdup( psz_title );
761     vlc_object_unlock( p_vout );
762 }
763
764 /*****************************************************************************
765  * InitThread: initialize video output thread
766  *****************************************************************************
767  * This function is called from RunThread and performs the second step of the
768  * initialization. It returns 0 on success. Note that the thread's flag are not
769  * modified inside this function.
770  * XXX You have to enter it with change_lock taken.
771  *****************************************************************************/
772 static int ChromaCreate( vout_thread_t *p_vout );
773 static void ChromaDestroy( vout_thread_t *p_vout );
774
775 static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
776 {
777      if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
778          return false;
779
780      if( p_output->i_chroma != FOURCC_RV15 &&
781          p_output->i_chroma != FOURCC_RV16 &&
782          p_output->i_chroma != FOURCC_RV24 &&
783          p_output->i_chroma != FOURCC_RV32 )
784          return true;
785
786      return p_output->i_rmask == p_render->i_rmask &&
787             p_output->i_gmask == p_render->i_gmask &&
788             p_output->i_bmask == p_render->i_bmask;
789 }
790
791 static int InitThread( vout_thread_t *p_vout )
792 {
793     int i, i_aspect_x, i_aspect_y;
794
795     /* Initialize output method, it allocates direct buffers for us */
796     if( p_vout->pf_init( p_vout ) )
797         return VLC_EGENERIC;
798
799     p_vout->p->p_picture_displayed = NULL;
800
801     if( !I_OUTPUTPICTURES )
802     {
803         msg_Err( p_vout, "plugin was unable to allocate at least "
804                          "one direct buffer" );
805         p_vout->pf_end( p_vout );
806         return VLC_EGENERIC;
807     }
808
809     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
810     {
811         msg_Err( p_vout, "plugin allocated too many direct buffers, "
812                          "our internal buffers must have overflown." );
813         p_vout->pf_end( p_vout );
814         return VLC_EGENERIC;
815     }
816
817     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
818
819     AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
820
821     msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
822              "chroma %4.4s, ar %i:%i, sar %i:%i",
823              p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
824              p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
825              p_vout->fmt_render.i_visible_width,
826              p_vout->fmt_render.i_visible_height,
827              (char*)&p_vout->fmt_render.i_chroma,
828              i_aspect_x, i_aspect_y,
829              p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
830
831     AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
832
833     msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
834              "chroma %4.4s, ar %i:%i, sar %i:%i",
835              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
836              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
837              p_vout->fmt_in.i_visible_width,
838              p_vout->fmt_in.i_visible_height,
839              (char*)&p_vout->fmt_in.i_chroma,
840              i_aspect_x, i_aspect_y,
841              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
842
843     if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
844     {
845         p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
846             p_vout->output.i_width;
847         p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
848             p_vout->output.i_height;
849         p_vout->fmt_out.i_x_offset =  p_vout->fmt_out.i_y_offset = 0;
850
851         p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
852         p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
853     }
854     if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
855     {
856         p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
857             p_vout->fmt_out.i_height;
858         p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
859             p_vout->fmt_out.i_width;
860     }
861
862     vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
863                  p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
864
865     AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
866
867     msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
868              "chroma %4.4s, ar %i:%i, sar %i:%i",
869              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
870              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
871              p_vout->fmt_out.i_visible_width,
872              p_vout->fmt_out.i_visible_height,
873              (char*)&p_vout->fmt_out.i_chroma,
874              i_aspect_x, i_aspect_y,
875              p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
876
877     /* FIXME removed the need of both fmt_* and heap infos */
878     /* Calculate shifts from system-updated masks */
879     PictureHeapFixRgb( &p_vout->render );
880     VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
881
882     PictureHeapFixRgb( &p_vout->output );
883     VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
884
885     /* Check whether we managed to create direct buffers similar to
886      * the render buffers, ie same size and chroma */
887     if( ( p_vout->output.i_width == p_vout->render.i_width )
888      && ( p_vout->output.i_height == p_vout->render.i_height )
889      && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
890     {
891         /* Cool ! We have direct buffers, we can ask the decoder to
892          * directly decode into them ! Map the first render buffers to
893          * the first direct buffers, but keep the first direct buffer
894          * for memcpy operations */
895         p_vout->p->b_direct = true;
896
897         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
898         {
899             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
900                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
901                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
902             {
903                 /* We have enough direct buffers so there's no need to
904                  * try to use system memory buffers. */
905                 break;
906             }
907             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
908             I_RENDERPICTURES++;
909         }
910
911         msg_Dbg( p_vout, "direct render, mapping "
912                  "render pictures 0-%i to system pictures 1-%i",
913                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
914     }
915     else
916     {
917         /* Rats... Something is wrong here, we could not find an output
918          * plugin able to directly render what we decode. See if we can
919          * find a chroma plugin to do the conversion */
920         p_vout->p->b_direct = false;
921
922         if( ChromaCreate( p_vout ) )
923         {
924             p_vout->pf_end( p_vout );
925             return VLC_EGENERIC;
926         }
927
928         msg_Dbg( p_vout, "indirect render, mapping "
929                  "render pictures 0-%i to system pictures %i-%i",
930                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
931                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
932
933         /* Append render buffers after the direct buffers */
934         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
935         {
936             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
937             I_RENDERPICTURES++;
938
939             /* Check if we have enough render pictures */
940             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
941                 break;
942         }
943     }
944
945     /* Link pictures back to their heap */
946     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
947     {
948         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
949     }
950
951     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
952     {
953         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
954     }
955
956     return VLC_SUCCESS;
957 }
958
959 /*****************************************************************************
960  * RunThread: video output thread
961  *****************************************************************************
962  * Video output thread. This function does only returns when the thread is
963  * terminated. It handles the pictures arriving in the video heap and the
964  * display device events.
965  *****************************************************************************/
966 static void* RunThread( vlc_object_t *p_this )
967 {
968     vout_thread_t *p_vout = (vout_thread_t *)p_this;
969     int             i_idle_loops = 0;  /* loops without displaying a picture */
970
971     bool            b_drop_late;
972
973     int canc = vlc_savecancel();
974
975     /*
976      * Initialize thread
977      */
978     vlc_mutex_lock( &p_vout->change_lock );
979     p_vout->b_error = InitThread( p_vout );
980
981     b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
982
983     /* signal the creation of the vout */
984     vlc_thread_ready( p_vout );
985
986     if( p_vout->b_error )
987     {
988         EndThread( p_vout );
989         vlc_mutex_unlock( &p_vout->change_lock );
990         vlc_restorecancel( canc );
991         return NULL;
992     }
993
994     vlc_object_lock( p_vout );
995
996     /*
997      * Main loop - it is not executed if an error occurred during
998      * initialization
999      */
1000     while( vlc_object_alive( p_vout ) && !p_vout->b_error )
1001     {
1002         /* Initialize loop variables */
1003         const mtime_t current_date = mdate();
1004         picture_t *p_picture;
1005         picture_t *p_filtered_picture;
1006         mtime_t display_date;
1007         picture_t *p_directbuffer;
1008         int i_index;
1009
1010         if( p_vout->p->b_title_show && p_vout->p->psz_title )
1011             DisplayTitleOnOSD( p_vout );
1012
1013         vlc_mutex_lock( &p_vout->picture_lock );
1014
1015         /* Look for the earliest picture but after the last displayed one */
1016         picture_t *p_last = p_vout->p->p_picture_displayed;;
1017
1018         p_picture = NULL;
1019         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
1020         {
1021             picture_t *p_pic = PP_RENDERPICTURE[i_index];
1022
1023             if( p_pic->i_status != READY_PICTURE )
1024                 continue;
1025
1026             if( p_vout->p->b_paused && p_last && p_last->date > 1 )
1027                 continue;
1028
1029             if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
1030             {
1031                 /* Drop old picture */
1032                 vout_UsePictureLocked( p_vout, p_pic );
1033             }
1034             else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
1035                      p_pic->date < current_date + p_vout->p->render_time &&
1036                      b_drop_late )
1037             {
1038                 /* Picture is late: it will be destroyed and the thread
1039                  * will directly choose the next picture */
1040                 vout_UsePictureLocked( p_vout, p_pic );
1041                 p_vout->p->i_picture_lost++;
1042
1043                 msg_Warn( p_vout, "late picture skipped (%"PRId64")",
1044                                   current_date - p_pic->date );
1045             }
1046             else if( ( !p_last || p_last->date < p_pic->date ) &&
1047                      ( p_picture == NULL || p_pic->date < p_picture->date ) )
1048             {
1049                 p_picture = p_pic;
1050             }
1051         }
1052         if( !p_picture )
1053         {
1054             p_picture = p_last;
1055
1056             if( !p_vout->p->b_picture_empty )
1057             {
1058                 p_vout->p->b_picture_empty = true;
1059                 vlc_cond_signal( &p_vout->p->picture_wait );
1060             }
1061         }
1062
1063         display_date = 0;
1064         if( p_picture )
1065         {
1066             display_date = p_picture->date;
1067
1068             /* If we found better than the last picture, destroy it */
1069             if( p_last && p_picture != p_last )
1070             {
1071                 vout_UsePictureLocked( p_vout, p_last );
1072                 p_vout->p->p_picture_displayed = p_last = NULL;
1073             }
1074
1075             /* Compute FPS rate */
1076             p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1077
1078             if( !p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1079             {
1080                 /* A picture is ready to be rendered, but its rendering date
1081                  * is far from the current one so the thread will perform an
1082                  * empty loop as if no picture were found. The picture state
1083                  * is unchanged */
1084                 p_picture    = NULL;
1085                 display_date = 0;
1086             }
1087             else if( p_picture == p_last )
1088             {
1089                 /* We are asked to repeat the previous picture, but we first
1090                  * wait for a couple of idle loops */
1091                 if( i_idle_loops < 4 )
1092                 {
1093                     p_picture    = NULL;
1094                     display_date = 0;
1095                 }
1096                 else
1097                 {
1098                     /* We set the display date to something high, otherwise
1099                      * we'll have lots of problems with late pictures */
1100                     display_date = current_date + p_vout->p->render_time;
1101                 }
1102             }
1103             else if( p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
1104             {
1105                 display_date = current_date + VOUT_DISPLAY_DELAY;
1106             }
1107
1108             if( p_picture )
1109             {
1110                 if( p_picture->date > 1 )
1111                 {
1112                     p_vout->p->i_picture_displayed_date = p_picture->date;
1113                     if( p_picture != p_last && !p_vout->p->b_picture_displayed )
1114                     {
1115                         p_vout->p->b_picture_displayed = true;
1116                         vlc_cond_signal( &p_vout->p->picture_wait );
1117                     }
1118                 }
1119                 p_vout->p->p_picture_displayed = p_picture;
1120             }
1121         }
1122         vlc_mutex_unlock( &p_vout->picture_lock );
1123
1124         if( p_picture == NULL )
1125             i_idle_loops++;
1126
1127         p_filtered_picture = NULL;
1128         if( p_picture )
1129             p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
1130                                                            p_picture );
1131
1132         /* FIXME it is ugly that b_snapshot is not locked but I do not
1133          * know which lock to use (here and in the snapshot callback) */
1134         const bool b_snapshot = p_vout->p->b_snapshot && p_picture != NULL;
1135
1136         /*
1137          * Check for subpictures to display
1138          */
1139         subpicture_t *p_subpic = NULL;
1140         if( display_date > 0 )
1141             p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
1142                                             p_vout->p->b_paused, b_snapshot );
1143
1144         /*
1145          * Perform rendering
1146          */
1147         p_vout->p->i_picture_displayed++;
1148         p_directbuffer = vout_RenderPicture( p_vout, p_filtered_picture,
1149                                              p_subpic, p_vout->p->b_paused );
1150
1151         /*
1152          * Take a snapshot if requested
1153          */
1154         if( p_directbuffer && b_snapshot )
1155         {
1156             /* FIXME lock (see b_snapshot) */
1157             p_vout->p->b_snapshot = false;
1158
1159             vout_Snapshot( p_vout, p_directbuffer );
1160         }
1161
1162         /*
1163          * Call the plugin-specific rendering method if there is one
1164          */
1165         if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
1166         {
1167             /* Render the direct buffer returned by vout_RenderPicture */
1168             p_vout->pf_render( p_vout, p_directbuffer );
1169         }
1170
1171         /*
1172          * Sleep, wake up
1173          */
1174         if( display_date != 0 && p_directbuffer != NULL )
1175         {
1176             mtime_t current_render_time = mdate() - current_date;
1177             /* if render time is very large we don't include it in the mean */
1178             if( current_render_time < p_vout->p->render_time +
1179                 VOUT_DISPLAY_DELAY )
1180             {
1181                 /* Store render time using a sliding mean weighting to
1182                  * current value in a 3 to 1 ratio*/
1183                 p_vout->p->render_time *= 3;
1184                 p_vout->p->render_time += current_render_time;
1185                 p_vout->p->render_time >>= 2;
1186             }
1187         }
1188
1189         /* Give back change lock */
1190         vlc_mutex_unlock( &p_vout->change_lock );
1191
1192         vlc_object_unlock( p_vout );
1193
1194         /* Sleep a while or until a given date */
1195         if( display_date != 0 )
1196         {
1197             /* If there are *vout* filters in the chain, better give them the picture
1198              * in advance */
1199             if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
1200             {
1201                 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1202             }
1203         }
1204         else
1205         {
1206             /* Wait until a frame is being sent or a spurious wakeup (not a problem here) */
1207             vlc_mutex_lock( &p_vout->picture_lock );
1208             vlc_cond_timedwait( &p_vout->p->picture_wait, &p_vout->picture_lock, current_date + VOUT_IDLE_SLEEP );
1209             vlc_mutex_unlock( &p_vout->picture_lock );
1210         }
1211
1212         /* On awakening, take back lock and send immediately picture
1213          * to display. */
1214         vlc_object_lock( p_vout );
1215         /* Note: vlc_object_alive() could be false here, and we
1216          * could be dead */
1217         vlc_mutex_lock( &p_vout->change_lock );
1218
1219         /*
1220          * Display the previously rendered picture
1221          */
1222         if( p_filtered_picture != NULL && p_directbuffer != NULL )
1223         {
1224             /* Display the direct buffer returned by vout_RenderPicture */
1225             if( p_vout->pf_display )
1226                 p_vout->pf_display( p_vout, p_directbuffer );
1227
1228             /* Tell the vout this was the last picture and that it does not
1229              * need to be forced anymore. */
1230             p_picture->b_force = false;
1231         }
1232
1233         /* Drop the filtered picture if created by video filters */
1234         if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
1235         {
1236             vlc_mutex_lock( &p_vout->picture_lock );
1237             vout_UsePictureLocked( p_vout, p_filtered_picture );
1238             vlc_mutex_unlock( &p_vout->picture_lock );
1239         }
1240
1241         if( p_picture != NULL )
1242         {
1243             /* Reinitialize idle loop count */
1244             i_idle_loops = 0;
1245         }
1246
1247         /*
1248          * Check events and manage thread
1249          */
1250         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1251         {
1252             /* A fatal error occurred, and the thread must terminate
1253              * immediately, without displaying anything - setting b_error to 1
1254              * causes the immediate end of the main while() loop. */
1255             // FIXME pf_end
1256             p_vout->b_error = 1;
1257             break;
1258         }
1259
1260         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1261         {
1262             /* this must only happen when the vout plugin is incapable of
1263              * rescaling the picture itself. In this case we need to destroy
1264              * the current picture buffers and recreate new ones with the right
1265              * dimensions */
1266             int i;
1267
1268             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1269
1270             assert( !p_vout->p->b_direct );
1271
1272             ChromaDestroy( p_vout );
1273
1274             vlc_mutex_lock( &p_vout->picture_lock );
1275
1276             p_vout->pf_end( p_vout );
1277
1278             p_vout->p->p_picture_displayed = NULL;
1279             for( i = 0; i < I_OUTPUTPICTURES; i++ )
1280                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1281             vlc_cond_signal( &p_vout->p->picture_wait );
1282
1283             I_OUTPUTPICTURES = 0;
1284
1285             if( p_vout->pf_init( p_vout ) )
1286             {
1287                 msg_Err( p_vout, "cannot resize display" );
1288                 /* FIXME: pf_end will be called again in EndThread() */
1289                 p_vout->b_error = 1;
1290             }
1291
1292             vlc_mutex_unlock( &p_vout->picture_lock );
1293
1294             /* Need to reinitialise the chroma plugin. Since we might need
1295              * resizing too and it's not sure that we already had it,
1296              * recreate the chroma plugin chain from scratch. */
1297             /* dionoea */
1298             if( ChromaCreate( p_vout ) )
1299             {
1300                 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1301                 p_vout->b_error = 1;
1302             }
1303             if( p_vout->b_error )
1304                 break;
1305         }
1306
1307         if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1308         {
1309             /* This happens when the picture buffers need to be recreated.
1310              * This is useful on multimonitor displays for instance.
1311              *
1312              * Warning: This only works when the vout creates only 1 picture
1313              * buffer!! */
1314             p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1315
1316             if( !p_vout->p->b_direct )
1317                 ChromaDestroy( p_vout );
1318
1319             vlc_mutex_lock( &p_vout->picture_lock );
1320
1321             p_vout->pf_end( p_vout );
1322
1323             I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1324
1325             p_vout->b_error = InitThread( p_vout );
1326             if( p_vout->b_error )
1327                 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed\n" );
1328
1329             vlc_cond_signal( &p_vout->p->picture_wait );
1330             vlc_mutex_unlock( &p_vout->picture_lock );
1331
1332             if( p_vout->b_error )
1333                 break;
1334         }
1335
1336         /* Check for "video filter2" changes */
1337         vlc_mutex_lock( &p_vout->p->vfilter_lock );
1338         if( p_vout->p->psz_vf2 )
1339         {
1340             es_format_t fmt;
1341
1342             es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1343             fmt.video = p_vout->fmt_render;
1344             filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
1345
1346             if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
1347                                                p_vout->p->psz_vf2 ) < 0 )
1348                 msg_Err( p_vout, "Video filter chain creation failed" );
1349
1350             free( p_vout->p->psz_vf2 );
1351             p_vout->p->psz_vf2 = NULL;
1352         }
1353         vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1354     }
1355
1356     /*
1357      * Error loop - wait until the thread destruction is requested
1358      */
1359     if( p_vout->b_error )
1360         ErrorThread( p_vout );
1361
1362     /* End of thread */
1363     CleanThread( p_vout );
1364     EndThread( p_vout );
1365     vlc_mutex_unlock( &p_vout->change_lock );
1366
1367     vlc_object_unlock( p_vout );
1368     vlc_restorecancel( canc );
1369     return NULL;
1370 }
1371
1372 /*****************************************************************************
1373  * ErrorThread: RunThread() error loop
1374  *****************************************************************************
1375  * This function is called when an error occurred during thread main's loop.
1376  * The thread can still receive feed, but must be ready to terminate as soon
1377  * as possible.
1378  *****************************************************************************/
1379 static void ErrorThread( vout_thread_t *p_vout )
1380 {
1381     /* Wait until a `die' order */
1382     while( vlc_object_alive( p_vout ) )
1383         vlc_object_wait( p_vout );
1384 }
1385
1386 /*****************************************************************************
1387  * CleanThread: clean up after InitThread
1388  *****************************************************************************
1389  * This function is called after a sucessful
1390  * initialization. It frees all resources allocated by InitThread.
1391  * XXX You have to enter it with change_lock taken.
1392  *****************************************************************************/
1393 static void CleanThread( vout_thread_t *p_vout )
1394 {
1395     int     i_index;                                        /* index in heap */
1396
1397     if( !p_vout->p->b_direct )
1398         ChromaDestroy( p_vout );
1399
1400     /* Destroy all remaining pictures */
1401     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1402     {
1403         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1404         {
1405             free( p_vout->p_picture[i_index].p_data_orig );
1406         }
1407     }
1408
1409     /* Destroy translation tables */
1410     if( !p_vout->b_error )
1411         p_vout->pf_end( p_vout );
1412 }
1413
1414 /*****************************************************************************
1415  * EndThread: thread destruction
1416  *****************************************************************************
1417  * This function is called when the thread ends.
1418  * It frees all resources not allocated by InitThread.
1419  * XXX You have to enter it with change_lock taken.
1420  *****************************************************************************/
1421 static void EndThread( vout_thread_t *p_vout )
1422 {
1423 #ifdef STATS
1424     {
1425         struct tms cpu_usage;
1426         times( &cpu_usage );
1427
1428         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1429                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1430     }
1431 #endif
1432
1433     /* FIXME does that function *really* need to be called inside the thread ? */
1434
1435     /* Detach subpicture unit from both input and vout */
1436     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1437     vlc_object_detach( p_vout->p_spu );
1438
1439     /* Destroy the video filters2 */
1440     filter_chain_Delete( p_vout->p->p_vf2_chain );
1441 }
1442
1443 /* Thread helpers */
1444 static picture_t *ChromaGetPicture( filter_t *p_filter )
1445 {
1446     picture_t *p_pic = (picture_t *)p_filter->p_owner;
1447     p_filter->p_owner = NULL;
1448     return p_pic;
1449 }
1450
1451 static int ChromaCreate( vout_thread_t *p_vout )
1452 {
1453     static const char typename[] = "chroma";
1454     filter_t *p_chroma;
1455
1456     /* Choose the best module */
1457     p_chroma = p_vout->p->p_chroma =
1458         vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
1459                            typename );
1460
1461     vlc_object_attach( p_chroma, p_vout );
1462
1463     /* TODO: Set the fmt_in and fmt_out stuff here */
1464     p_chroma->fmt_in.video = p_vout->fmt_render;
1465     p_chroma->fmt_out.video = p_vout->fmt_out;
1466     VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
1467     VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
1468
1469     p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, false );
1470
1471     if( p_chroma->p_module == NULL )
1472     {
1473         msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1474                  (char*)&p_vout->render.i_chroma,
1475                  (char*)&p_vout->output.i_chroma,
1476                  p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1477                  p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1478                  );
1479
1480         vlc_object_release( p_vout->p->p_chroma );
1481         p_vout->p->p_chroma = NULL;
1482
1483         return VLC_EGENERIC;
1484     }
1485     p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1486     return VLC_SUCCESS;
1487 }
1488
1489 static void ChromaDestroy( vout_thread_t *p_vout )
1490 {
1491     assert( !p_vout->p->b_direct );
1492
1493     if( !p_vout->p->p_chroma )
1494         return;
1495
1496     module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
1497     vlc_object_release( p_vout->p->p_chroma );
1498     p_vout->p->p_chroma = NULL;
1499 }
1500
1501 /* following functions are local */
1502 static int ReduceHeight( int i_ratio )
1503 {
1504     int i_dummy = VOUT_ASPECT_FACTOR;
1505     int i_pgcd  = 1;
1506
1507     if( !i_ratio )
1508     {
1509         return i_pgcd;
1510     }
1511
1512     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1513     while( !(i_ratio & 1) && !(i_dummy & 1) )
1514     {
1515         i_ratio >>= 1;
1516         i_dummy >>= 1;
1517         i_pgcd  <<= 1;
1518     }
1519
1520     while( !(i_ratio % 3) && !(i_dummy % 3) )
1521     {
1522         i_ratio /= 3;
1523         i_dummy /= 3;
1524         i_pgcd  *= 3;
1525     }
1526
1527     while( !(i_ratio % 5) && !(i_dummy % 5) )
1528     {
1529         i_ratio /= 5;
1530         i_dummy /= 5;
1531         i_pgcd  *= 5;
1532     }
1533
1534     return i_pgcd;
1535 }
1536
1537 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1538 {
1539     unsigned int i_pgcd = ReduceHeight( i_aspect );
1540     *i_aspect_x = i_aspect / i_pgcd;
1541     *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1542 }
1543
1544 /**
1545  * This function copies all RGB informations from a picture_heap_t into
1546  * a video_format_t
1547  */
1548 static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
1549 {
1550     p_fmt->i_rmask = p_heap->i_rmask;
1551     p_fmt->i_gmask = p_heap->i_gmask;
1552     p_fmt->i_bmask = p_heap->i_bmask;
1553     p_fmt->i_rrshift = p_heap->i_rrshift;
1554     p_fmt->i_lrshift = p_heap->i_lrshift;
1555     p_fmt->i_rgshift = p_heap->i_rgshift;
1556     p_fmt->i_lgshift = p_heap->i_lgshift;
1557     p_fmt->i_rbshift = p_heap->i_rbshift;
1558     p_fmt->i_lbshift = p_heap->i_lbshift;
1559 }
1560
1561 /**
1562  * This funtion copes all RGB informations from a video_format_t into
1563  * a picture_heap_t
1564  */
1565 static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
1566 {
1567     p_heap->i_rmask = p_fmt->i_rmask;
1568     p_heap->i_gmask = p_fmt->i_gmask;
1569     p_heap->i_bmask = p_fmt->i_bmask;
1570     p_heap->i_rrshift = p_fmt->i_rrshift;
1571     p_heap->i_lrshift = p_fmt->i_lrshift;
1572     p_heap->i_rgshift = p_fmt->i_rgshift;
1573     p_heap->i_lgshift = p_fmt->i_lgshift;
1574     p_heap->i_rbshift = p_fmt->i_rbshift;
1575     p_heap->i_lbshift = p_fmt->i_lbshift;
1576 }
1577
1578 /**
1579  * This function computes rgb shifts from masks
1580  */
1581 static void PictureHeapFixRgb( picture_heap_t *p_heap )
1582 {
1583     video_format_t fmt;
1584
1585     /* */
1586     fmt.i_chroma = p_heap->i_chroma;
1587     VideoFormatImportRgb( &fmt, p_heap );
1588
1589     /* */
1590     video_format_FixRgb( &fmt );
1591
1592     VideoFormatExportRgb( &fmt, p_heap );
1593 }
1594
1595 /*****************************************************************************
1596  * object variables callbacks: a bunch of object variables are used by the
1597  * interfaces to interact with the vout.
1598  *****************************************************************************/
1599 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1600                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1601 {
1602     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1603     input_thread_t *p_input;
1604     vlc_value_t val;
1605
1606     char *psz_mode = newval.psz_string;
1607     char *psz_filter, *psz_deinterlace = NULL;
1608     (void)psz_cmd; (void)oldval; (void)p_data;
1609
1610     var_Get( p_vout, "vout-filter", &val );
1611     psz_filter = val.psz_string;
1612     if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1613
1614     if( !psz_mode || !*psz_mode )
1615     {
1616         if( psz_deinterlace )
1617         {
1618             char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1619             if( psz_src[0] == ':' ) psz_src++;
1620             memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1621         }
1622     }
1623     else if( !psz_deinterlace )
1624     {
1625         psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1626                               sizeof(":deinterlace") );
1627         if( psz_filter )
1628         {
1629             if( *psz_filter )
1630                 strcat( psz_filter, ":" );
1631             strcat( psz_filter, "deinterlace" );
1632         }
1633     }
1634
1635     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1636                                                  FIND_PARENT );
1637     if( !p_input )
1638     {
1639         free( psz_filter );
1640         return VLC_EGENERIC;
1641     }
1642
1643     if( psz_mode && *psz_mode )
1644     {
1645         /* Modify input as well because the vout might have to be restarted */
1646         val.psz_string = psz_mode;
1647         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1648         var_Set( p_input, "deinterlace-mode", val );
1649     }
1650     vlc_object_release( p_input );
1651
1652     val.b_bool = true;
1653     var_Set( p_vout, "intf-change", val );
1654
1655     val.psz_string = psz_filter;
1656     var_Set( p_vout, "vout-filter", val );
1657     free( psz_filter );
1658
1659     return VLC_SUCCESS;
1660 }
1661
1662 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1663                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1664 {
1665     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1666     input_thread_t *p_input;
1667     vlc_value_t val;
1668     (void)psz_cmd; (void)oldval; (void)p_data;
1669
1670     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1671                                                  FIND_PARENT );
1672     if (!p_input)
1673     {
1674         msg_Err( p_vout, "Input not found" );
1675         return( VLC_EGENERIC );
1676     }
1677
1678     val.b_bool = true;
1679     var_Set( p_vout, "intf-change", val );
1680
1681     /* Modify input as well because the vout might have to be restarted */
1682     val.psz_string = newval.psz_string;
1683     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1684
1685     var_Set( p_input, "vout-filter", val );
1686
1687     /* Now restart current video stream */
1688     input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1689     vlc_object_release( p_input );
1690
1691     return VLC_SUCCESS;
1692 }
1693
1694 /*****************************************************************************
1695  * Video Filter2 stuff
1696  *****************************************************************************/
1697 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1698                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1699 {
1700     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1701     (void)psz_cmd; (void)oldval; (void)p_data;
1702
1703     vlc_mutex_lock( &p_vout->p->vfilter_lock );
1704     p_vout->p->psz_vf2 = strdup( newval.psz_string );
1705     vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1706
1707     return VLC_SUCCESS;
1708 }
1709
1710 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1711 {
1712     const mtime_t i_start = mdate();
1713     const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->i_title_timeout;
1714
1715     vlc_object_assert_locked( p_vout );
1716
1717     vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1718                            p_vout->p->psz_title, NULL,
1719                            p_vout->p->i_title_position,
1720                            30 + p_vout->fmt_in.i_width
1721                               - p_vout->fmt_in.i_visible_width
1722                               - p_vout->fmt_in.i_x_offset,
1723                            20 + p_vout->fmt_in.i_y_offset,
1724                            i_start, i_stop );
1725
1726     free( p_vout->p->psz_title );
1727
1728     p_vout->p->psz_title = NULL;
1729 }
1730