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