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