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