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