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