]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Remove the suxor thread
[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         // FIXME it's ugly but that's exactly the function that need to be called.
399         EndThread( p_vout );
400         vlc_object_detach( p_vout );
401         vlc_object_release( p_vout );
402         return NULL;
403     }
404
405     /* Create a few object variables for interface interaction */
406     var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
407     text.psz_string = _("Deinterlace");
408     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
409     val.psz_string = (char *)""; text.psz_string = _("Disable");
410     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
411     val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
412     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
413     val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
414     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
415     val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
416     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
417     val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
418     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
419     val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
420     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
421     val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
422     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
423
424     if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
425     {
426         var_Set( p_vout, "deinterlace", val );
427         free( val.psz_string );
428     }
429     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
430
431     var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
432     text.psz_string = _("Filters");
433     var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
434     var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
435
436     /* Calculate delay created by internal caching */
437     p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
438                                            VLC_OBJECT_INPUT, FIND_ANYWHERE );
439     if( p_input_thread )
440     {
441         p_vout->i_pts_delay = p_input_thread->i_pts_delay;
442         vlc_object_release( p_input_thread );
443     }
444     else
445     {
446         p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
447     }
448
449     if( vlc_thread_create( p_vout, "video output", RunThread,
450                            VLC_THREAD_PRIORITY_OUTPUT, true ) )
451     {
452         module_Unneed( p_vout, p_vout->p_module );
453         vlc_object_release( p_vout );
454         return NULL;
455     }
456
457     vlc_object_set_destructor( p_vout, vout_Destructor );
458
459     if( p_vout->b_error )
460     {
461         msg_Err( p_vout, "video output creation failed" );
462
463         /* Make sure the thread is destroyed and data released */
464         vlc_object_release( p_vout );
465         return NULL;
466     }
467
468     return p_vout;
469 }
470
471 static void vout_Destructor( vlc_object_t * p_this )
472 {
473     vout_thread_t *p_vout = (vout_thread_t *)p_this;
474
475     /* Destroy the locks */
476     vlc_mutex_destroy( &p_vout->picture_lock );
477     vlc_mutex_destroy( &p_vout->change_lock );
478     vlc_mutex_destroy( &p_vout->vfilter_lock );
479
480     /* Release the module */
481     if( p_vout->p_module )
482     {
483         module_Unneed( p_vout, p_vout->p_module );
484     }
485
486     free( p_vout->psz_filter_chain );
487
488     config_ChainDestroy( p_vout->p_cfg );
489
490 #ifndef __APPLE__
491     vout_thread_t *p_another_vout;
492
493     /* This is a dirty hack mostly for Linux, where there is no way to get the
494      * GUI back if you closed it while playing video. This is solved in
495      * Mac OS X, where we have this novelty called menubar, that will always
496      * allow you access to the applications main functionality. They should try
497      * that on linux sometime. */
498     p_another_vout = vlc_object_find( p_this->p_libvlc,
499                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
500     if( p_another_vout == NULL )
501         var_SetBool( p_this->p_libvlc, "intf-show", true );
502     else
503         vlc_object_release( p_another_vout );
504 #endif
505 }
506
507 /*****************************************************************************
508  * InitThread: initialize video output thread
509  *****************************************************************************
510  * This function is called from RunThread and performs the second step of the
511  * initialization. It returns 0 on success. Note that the thread's flag are not
512  * modified inside this function.
513  * XXX You have to enter it with change_lock taken.
514  *****************************************************************************/
515 static int ChromaCreate( vout_thread_t *p_vout );
516 static void ChromaDestroy( vout_thread_t *p_vout );
517
518 static int InitThread( vout_thread_t *p_vout )
519 {
520     int i, i_aspect_x, i_aspect_y;
521
522 #ifdef STATS
523     p_vout->c_loops = 0;
524 #endif
525
526     /* Initialize output method, it allocates direct buffers for us */
527     if( p_vout->pf_init( p_vout ) )
528         return VLC_EGENERIC;
529
530     if( !I_OUTPUTPICTURES )
531     {
532         msg_Err( p_vout, "plugin was unable to allocate at least "
533                          "one direct buffer" );
534         p_vout->pf_end( p_vout );
535         return VLC_EGENERIC;
536     }
537
538     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
539     {
540         msg_Err( p_vout, "plugin allocated too many direct buffers, "
541                          "our internal buffers must have overflown." );
542         p_vout->pf_end( p_vout );
543         return VLC_EGENERIC;
544     }
545
546     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
547
548     AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
549
550     msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
551              "chroma %4.4s, ar %i:%i, sar %i:%i",
552              p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
553              p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
554              p_vout->fmt_render.i_visible_width,
555              p_vout->fmt_render.i_visible_height,
556              (char*)&p_vout->fmt_render.i_chroma,
557              i_aspect_x, i_aspect_y,
558              p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
559
560     AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
561
562     msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
563              "chroma %4.4s, ar %i:%i, sar %i:%i",
564              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
565              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
566              p_vout->fmt_in.i_visible_width,
567              p_vout->fmt_in.i_visible_height,
568              (char*)&p_vout->fmt_in.i_chroma,
569              i_aspect_x, i_aspect_y,
570              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
571
572     if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
573     {
574         p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
575             p_vout->output.i_width;
576         p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
577             p_vout->output.i_height;
578         p_vout->fmt_out.i_x_offset =  p_vout->fmt_out.i_y_offset = 0;
579
580         p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
581         p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
582     }
583     if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
584     {
585         p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
586             p_vout->fmt_out.i_height;
587         p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
588             p_vout->fmt_out.i_width;
589     }
590
591     vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
592                  p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
593
594     AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
595
596     msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
597              "chroma %4.4s, ar %i:%i, sar %i:%i",
598              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
599              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
600              p_vout->fmt_out.i_visible_width,
601              p_vout->fmt_out.i_visible_height,
602              (char*)&p_vout->fmt_out.i_chroma,
603              i_aspect_x, i_aspect_y,
604              p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
605
606     /* Calculate shifts from system-updated masks */
607     MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
608                  p_vout->output.i_rmask );
609     MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
610                  p_vout->output.i_gmask );
611     MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
612                  p_vout->output.i_bmask );
613
614     /* Check whether we managed to create direct buffers similar to
615      * the render buffers, ie same size and chroma */
616     if( ( p_vout->output.i_width == p_vout->render.i_width )
617      && ( p_vout->output.i_height == p_vout->render.i_height )
618      && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
619     {
620         /* Cool ! We have direct buffers, we can ask the decoder to
621          * directly decode into them ! Map the first render buffers to
622          * the first direct buffers, but keep the first direct buffer
623          * for memcpy operations */
624         p_vout->b_direct = 1;
625
626         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
627         {
628             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
629                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
630                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
631             {
632                 /* We have enough direct buffers so there's no need to
633                  * try to use system memory buffers. */
634                 break;
635             }
636             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
637             I_RENDERPICTURES++;
638         }
639
640         msg_Dbg( p_vout, "direct render, mapping "
641                  "render pictures 0-%i to system pictures 1-%i",
642                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
643     }
644     else
645     {
646         /* Rats... Something is wrong here, we could not find an output
647          * plugin able to directly render what we decode. See if we can
648          * find a chroma plugin to do the conversion */
649         p_vout->b_direct = 0;
650
651         if( ChromaCreate( p_vout ) )
652         {
653             p_vout->pf_end( p_vout );
654             return VLC_EGENERIC;
655         }
656
657         msg_Dbg( p_vout, "indirect render, mapping "
658                  "render pictures 0-%i to system pictures %i-%i",
659                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
660                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
661
662         /* Append render buffers after the direct buffers */
663         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
664         {
665             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
666             I_RENDERPICTURES++;
667
668             /* Check if we have enough render pictures */
669             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
670                 break;
671         }
672     }
673
674     /* Link pictures back to their heap */
675     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
676     {
677         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
678     }
679
680     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
681     {
682         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
683     }
684
685     return VLC_SUCCESS;
686 }
687
688 /*****************************************************************************
689  * RunThread: video output thread
690  *****************************************************************************
691  * Video output thread. This function does only returns when the thread is
692  * terminated. It handles the pictures arriving in the video heap and the
693  * display device events.
694  *****************************************************************************/
695 static void RunThread( vout_thread_t *p_vout)
696 {
697     int             i_index;                                /* index in heap */
698     int             i_idle_loops = 0;  /* loops without displaying a picture */
699     mtime_t         current_date;                            /* current date */
700     mtime_t         display_date;                            /* display date */
701
702     picture_t *     p_picture;                            /* picture pointer */
703     picture_t *     p_last_picture = NULL;                   /* last picture */
704     picture_t *     p_directbuffer;              /* direct buffer to display */
705
706     subpicture_t *  p_subpic = NULL;                   /* subpicture pointer */
707
708     input_thread_t *p_input = NULL ;           /* Parent input, if it exists */
709
710     vlc_value_t     val;
711     bool      b_drop_late;
712
713     int             i_displayed = 0, i_lost = 0, i_loops = 0;
714
715     /*
716      * Initialize thread
717      */
718     vlc_mutex_lock( &p_vout->change_lock );
719     p_vout->b_error = InitThread( p_vout );
720
721     var_Create( p_vout, "drop-late-frames", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
722     var_Get( p_vout, "drop-late-frames", &val );
723     b_drop_late = val.b_bool;
724
725     /* signal the creation of the vout */
726     vlc_thread_ready( p_vout );
727
728     if( p_vout->b_error )
729     {
730         EndThread( p_vout );
731         vlc_mutex_unlock( &p_vout->change_lock );
732         return;
733     }
734
735     vlc_object_lock( p_vout );
736
737     if( p_vout->b_title_show )
738         DisplayTitleOnOSD( p_vout );
739
740     /*
741      * Main loop - it is not executed if an error occurred during
742      * initialization
743      */
744     while( (vlc_object_alive( p_vout )) && (!p_vout->b_error) )
745     {
746         /* Initialize loop variables */
747         p_picture = NULL;
748         display_date = 0;
749         current_date = mdate();
750
751         i_loops++;
752             if( !p_input )
753             {
754                 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
755                                            FIND_PARENT );
756             }
757             if( p_input )
758             {
759                 vlc_mutex_lock( &p_input->p->counters.counters_lock );
760                 stats_UpdateInteger( p_vout, p_input->p->counters.p_lost_pictures,
761                                      i_lost , NULL);
762                 stats_UpdateInteger( p_vout,
763                                      p_input->p->counters.p_displayed_pictures,
764                                      i_displayed , NULL);
765                 i_displayed = i_lost = 0;
766                 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
767                 vlc_object_release( p_input );
768                 p_input = NULL;
769             }
770 #if 0
771         p_vout->c_loops++;
772         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
773         {
774             msg_Dbg( p_vout, "picture heap: %d/%d",
775                      I_RENDERPICTURES, p_vout->i_heap_size );
776         }
777 #endif
778
779         /*
780          * Find the picture to display (the one with the earliest date).
781          * This operation does not need lock, since only READY_PICTUREs
782          * are handled. */
783         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
784         {
785             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
786                 && ( (p_picture == NULL) ||
787                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
788             {
789                 p_picture = PP_RENDERPICTURE[i_index];
790                 display_date = p_picture->date;
791             }
792         }
793
794         if( p_picture )
795         {
796             /* If we met the last picture, parse again to see whether there is
797              * a more appropriate one. */
798             if( p_picture == p_last_picture )
799             {
800                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
801                 {
802                     if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
803                         && (PP_RENDERPICTURE[i_index] != p_last_picture)
804                         && ((p_picture == p_last_picture) ||
805                             (PP_RENDERPICTURE[i_index]->date < display_date)) )
806                     {
807                         p_picture = PP_RENDERPICTURE[i_index];
808                         display_date = p_picture->date;
809                     }
810                 }
811             }
812
813             /* If we found better than the last picture, destroy it */
814             if( p_last_picture && p_picture != p_last_picture )
815             {
816                 vlc_mutex_lock( &p_vout->picture_lock );
817                 if( p_last_picture->i_refcount )
818                 {
819                     p_last_picture->i_status = DISPLAYED_PICTURE;
820                 }
821                 else
822                 {
823                     p_last_picture->i_status = DESTROYED_PICTURE;
824                     p_vout->i_heap_size--;
825                 }
826                 vlc_mutex_unlock( &p_vout->picture_lock );
827                 p_last_picture = NULL;
828             }
829
830             /* Compute FPS rate */
831             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
832                 = display_date;
833
834             if( !p_picture->b_force &&
835                 p_picture != p_last_picture &&
836                 display_date < current_date + p_vout->render_time &&
837                 b_drop_late )
838             {
839                 /* Picture is late: it will be destroyed and the thread
840                  * will directly choose the next picture */
841                 vlc_mutex_lock( &p_vout->picture_lock );
842                 if( p_picture->i_refcount )
843                 {
844                     /* Pretend we displayed the picture, but don't destroy
845                      * it since the decoder might still need it. */
846                     p_picture->i_status = DISPLAYED_PICTURE;
847                 }
848                 else
849                 {
850                     /* Destroy the picture without displaying it */
851                     p_picture->i_status = DESTROYED_PICTURE;
852                     p_vout->i_heap_size--;
853                 }
854                 msg_Warn( p_vout, "late picture skipped (%"PRId64")",
855                                   current_date - display_date );
856                 i_lost++;
857                 vlc_mutex_unlock( &p_vout->picture_lock );
858
859                 continue;
860             }
861
862             if( display_date >
863                 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
864             {
865                 /* Picture is waaay too early: it will be destroyed */
866                 vlc_mutex_lock( &p_vout->picture_lock );
867                 if( p_picture->i_refcount )
868                 {
869                     /* Pretend we displayed the picture, but don't destroy
870                      * it since the decoder might still need it. */
871                     p_picture->i_status = DISPLAYED_PICTURE;
872                 }
873                 else
874                 {
875                     /* Destroy the picture without displaying it */
876                     p_picture->i_status = DESTROYED_PICTURE;
877                     p_vout->i_heap_size--;
878                 }
879                 i_lost++;
880                 msg_Warn( p_vout, "vout warning: early picture skipped "
881                           "(%"PRId64")", display_date - current_date
882                           - p_vout->i_pts_delay );
883                 vlc_mutex_unlock( &p_vout->picture_lock );
884
885                 continue;
886             }
887
888             if( display_date > current_date + VOUT_DISPLAY_DELAY )
889             {
890                 /* A picture is ready to be rendered, but its rendering date
891                  * is far from the current one so the thread will perform an
892                  * empty loop as if no picture were found. The picture state
893                  * is unchanged */
894                 p_picture    = NULL;
895                 display_date = 0;
896             }
897             else if( p_picture == p_last_picture )
898             {
899                 /* We are asked to repeat the previous picture, but we first
900                  * wait for a couple of idle loops */
901                 if( i_idle_loops < 4 )
902                 {
903                     p_picture    = NULL;
904                     display_date = 0;
905                 }
906                 else
907                 {
908                     /* We set the display date to something high, otherwise
909                      * we'll have lots of problems with late pictures */
910                     display_date = current_date + p_vout->render_time;
911                 }
912             }
913         }
914
915         if( p_picture == NULL )
916         {
917             i_idle_loops++;
918         }
919
920         if( p_picture )
921         {
922             p_picture = filter_chain_VideoFilter( p_vout->p_vf2_chain,
923                                                   p_picture );
924         }
925
926         if( p_picture && p_vout->b_snapshot )
927         {
928             p_vout->b_snapshot = false;
929             vout_Snapshot( p_vout, p_picture );
930         }
931
932         /*
933          * Check for subpictures to display
934          */
935         if( display_date > 0 )
936         {
937             if( !p_input )
938             {
939                 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
940                                            FIND_PARENT );
941             }
942             p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
943             p_input ? var_GetBool( p_input, "state" ) == PAUSE_S : false );
944             if( p_input )
945                 vlc_object_release( p_input );
946             p_input = NULL;
947         }
948
949         /*
950          * Perform rendering
951          */
952         i_displayed++;
953         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
954
955         /*
956          * Call the plugin-specific rendering method if there is one
957          */
958         if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
959         {
960             /* Render the direct buffer returned by vout_RenderPicture */
961             p_vout->pf_render( p_vout, p_directbuffer );
962         }
963
964         /*
965          * Sleep, wake up
966          */
967         if( display_date != 0 && p_directbuffer != NULL )
968         {
969             mtime_t current_render_time = mdate() - current_date;
970             /* if render time is very large we don't include it in the mean */
971             if( current_render_time < p_vout->render_time +
972                 VOUT_DISPLAY_DELAY )
973             {
974                 /* Store render time using a sliding mean weighting to
975                  * current value in a 3 to 1 ratio*/
976                 p_vout->render_time *= 3;
977                 p_vout->render_time += current_render_time;
978                 p_vout->render_time >>= 2;
979             }
980         }
981
982         /* Give back change lock */
983         vlc_mutex_unlock( &p_vout->change_lock );
984
985         vlc_object_unlock( p_vout );
986
987         /* Sleep a while or until a given date */
988         if( display_date != 0 )
989         {
990             /* If there are filters in the chain, better give them the picture
991              * in advance */
992             if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
993             {
994                 mwait( display_date - VOUT_MWAIT_TOLERANCE );
995             }
996         }
997         else
998         {
999             msleep( VOUT_IDLE_SLEEP );
1000         }
1001
1002         /* On awakening, take back lock and send immediately picture
1003          * to display. */
1004         vlc_object_lock( p_vout );
1005         /* Note: vlc_object_alive() could be false here, and we
1006          * could be dead */
1007         vlc_mutex_lock( &p_vout->change_lock );
1008
1009         /*
1010          * Display the previously rendered picture
1011          */
1012         if( p_picture != NULL && p_directbuffer != NULL )
1013         {
1014             /* Display the direct buffer returned by vout_RenderPicture */
1015             if( p_vout->pf_display )
1016             {
1017                 p_vout->pf_display( p_vout, p_directbuffer );
1018             }
1019
1020             /* Tell the vout this was the last picture and that it does not
1021              * need to be forced anymore. */
1022             p_last_picture = p_picture;
1023             p_last_picture->b_force = 0;
1024         }
1025
1026         if( p_picture != NULL )
1027         {
1028             /* Reinitialize idle loop count */
1029             i_idle_loops = 0;
1030         }
1031
1032         /*
1033          * Check events and manage thread
1034          */
1035         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1036         {
1037             /* A fatal error occurred, and the thread must terminate
1038              * immediately, without displaying anything - setting b_error to 1
1039              * causes the immediate end of the main while() loop. */
1040             // FIXME pf_end
1041             p_vout->b_error = 1;
1042             break;
1043         }
1044
1045         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1046         {
1047             /* this must only happen when the vout plugin is incapable of
1048              * rescaling the picture itself. In this case we need to destroy
1049              * the current picture buffers and recreate new ones with the right
1050              * dimensions */
1051             int i;
1052
1053             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1054
1055             assert( !p_vout->b_direct );
1056
1057             ChromaDestroy( p_vout );
1058
1059             vlc_mutex_lock( &p_vout->picture_lock );
1060
1061             p_vout->pf_end( p_vout );
1062
1063             for( i = 0; i < I_OUTPUTPICTURES; i++ )
1064                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1065
1066             I_OUTPUTPICTURES = 0;
1067
1068             if( p_vout->pf_init( p_vout ) )
1069             {
1070                 msg_Err( p_vout, "cannot resize display" );
1071                 /* FIXME: pf_end will be called again in EndThread() */
1072                 p_vout->b_error = 1;
1073             }
1074
1075             vlc_mutex_unlock( &p_vout->picture_lock );
1076
1077             /* Need to reinitialise the chroma plugin. Since we might need
1078              * resizing too and it's not sure that we already had it,
1079              * recreate the chroma plugin chain from scratch. */
1080             /* dionoea */
1081             if( ChromaCreate( p_vout ) )
1082             {
1083                 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1084                 p_vout->b_error = 1;
1085             }
1086             if( p_vout->b_error )
1087                 break;
1088         }
1089
1090         if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1091         {
1092             /* This happens when the picture buffers need to be recreated.
1093              * This is useful on multimonitor displays for instance.
1094              *
1095              * Warning: This only works when the vout creates only 1 picture
1096              * buffer!! */
1097             p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1098
1099             if( !p_vout->b_direct )
1100                 ChromaDestroy( p_vout );
1101
1102             vlc_mutex_lock( &p_vout->picture_lock );
1103
1104             p_vout->pf_end( p_vout );
1105
1106             I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1107
1108             p_vout->b_error = InitThread( p_vout );
1109             if( p_vout->b_error )
1110                 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed\n" );
1111
1112             vlc_mutex_unlock( &p_vout->picture_lock );
1113
1114             if( p_vout->b_error )
1115                 break;
1116         }
1117
1118         /* Check for "video filter2" changes */
1119         vlc_mutex_lock( &p_vout->vfilter_lock );
1120         if( p_vout->psz_vf2 )
1121         {
1122             es_format_t fmt;
1123
1124             es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1125             fmt.video = p_vout->fmt_render;
1126             filter_chain_Reset( p_vout->p_vf2_chain, &fmt, &fmt );
1127
1128             if( filter_chain_AppendFromString( p_vout->p_vf2_chain,
1129                                                p_vout->psz_vf2 ) < 0 )
1130                 msg_Err( p_vout, "Video filter chain creation failed" );
1131
1132             free( p_vout->psz_vf2 );
1133             p_vout->psz_vf2 = NULL;
1134         }
1135         vlc_mutex_unlock( &p_vout->vfilter_lock );
1136     }
1137
1138
1139     if( p_input )
1140     {
1141         vlc_object_release( p_input );
1142     }
1143
1144     /*
1145      * Error loop - wait until the thread destruction is requested
1146      */
1147     if( p_vout->b_error )
1148         ErrorThread( p_vout );
1149
1150     /* End of thread */
1151     CleanThread( p_vout );
1152     EndThread( p_vout );
1153     vlc_mutex_unlock( &p_vout->change_lock );
1154
1155     vlc_object_unlock( p_vout );
1156 }
1157
1158 /*****************************************************************************
1159  * ErrorThread: RunThread() error loop
1160  *****************************************************************************
1161  * This function is called when an error occurred during thread main's loop.
1162  * The thread can still receive feed, but must be ready to terminate as soon
1163  * as possible.
1164  *****************************************************************************/
1165 static void ErrorThread( vout_thread_t *p_vout )
1166 {
1167     /* Wait until a `die' order */
1168     while( vlc_object_alive( p_vout ) )
1169         vlc_object_wait( p_vout );
1170 }
1171
1172 /*****************************************************************************
1173  * CleanThread: clean up after InitThread
1174  *****************************************************************************
1175  * This function is called after a sucessful
1176  * initialization. It frees all resources allocated by InitThread.
1177  * XXX You have to enter it with change_lock taken.
1178  *****************************************************************************/
1179 static void CleanThread( vout_thread_t *p_vout )
1180 {
1181     int     i_index;                                        /* index in heap */
1182
1183     if( !p_vout->b_direct )
1184         ChromaDestroy( p_vout );
1185
1186     /* Destroy all remaining pictures */
1187     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1188     {
1189         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1190         {
1191             free( p_vout->p_picture[i_index].p_data_orig );
1192         }
1193     }
1194
1195     /* Destroy translation tables */
1196     if( !p_vout->b_error )
1197         p_vout->pf_end( p_vout );
1198 }
1199
1200 /*****************************************************************************
1201  * EndThread: thread destruction
1202  *****************************************************************************
1203  * This function is called when the thread ends.
1204  * It frees all resources not allocated by InitThread.
1205  * XXX You have to enter it with change_lock taken.
1206  *****************************************************************************/
1207 static void EndThread( vout_thread_t *p_vout )
1208 {
1209 #ifdef STATS
1210     {
1211         struct tms cpu_usage;
1212         times( &cpu_usage );
1213
1214         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1215                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1216     }
1217 #endif
1218
1219     /* FIXME does that function *really* need to be called inside the thread ? */
1220
1221     /* Destroy subpicture unit */
1222     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1223     spu_Destroy( p_vout->p_spu );
1224
1225     /* Destroy the video filters2 */
1226     filter_chain_Delete( p_vout->p_vf2_chain );
1227 }
1228
1229 /* Thread helpers */
1230 static picture_t *ChromaGetPicture( filter_t *p_filter )
1231 {
1232     picture_t *p_pic = (picture_t *)p_filter->p_owner;
1233     p_filter->p_owner = NULL;
1234     return p_pic;
1235 }
1236
1237 static int ChromaCreate( vout_thread_t *p_vout )
1238 {
1239     filter_t *p_chroma;
1240
1241     /* Choose the best module */
1242     p_chroma = p_vout->p_chroma = vlc_object_create( p_vout, sizeof(filter_t) );
1243
1244     vlc_object_attach( p_chroma, p_vout );
1245
1246     /* TODO: Set the fmt_in and fmt_out stuff here */
1247     p_chroma->fmt_in.video = p_vout->fmt_render;
1248     p_chroma->fmt_out.video = p_vout->fmt_out;
1249
1250     p_chroma->fmt_out.video.i_rmask = p_vout->output.i_rmask;
1251     p_chroma->fmt_out.video.i_gmask = p_vout->output.i_gmask;
1252     p_chroma->fmt_out.video.i_bmask = p_vout->output.i_bmask;
1253     p_chroma->fmt_out.video.i_rrshift = p_vout->output.i_rrshift;
1254     p_chroma->fmt_out.video.i_lrshift = p_vout->output.i_lrshift;
1255     p_chroma->fmt_out.video.i_rgshift = p_vout->output.i_rgshift;
1256     p_chroma->fmt_out.video.i_lgshift = p_vout->output.i_lgshift;
1257     p_chroma->fmt_out.video.i_rbshift = p_vout->output.i_rbshift;
1258     p_chroma->fmt_out.video.i_lbshift = p_vout->output.i_lbshift;
1259     p_chroma->p_module = module_Need( p_chroma, "video filter2", NULL, 0 );
1260
1261     if( p_chroma->p_module == NULL )
1262     {
1263         msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1264                  (char*)&p_vout->render.i_chroma,
1265                  (char*)&p_vout->output.i_chroma,
1266                  p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1267                  p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1268                  );
1269
1270         vlc_object_release( p_vout->p_chroma );
1271         p_vout->p_chroma = NULL;
1272         return VLC_EGENERIC;
1273     }
1274     p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1275     return VLC_SUCCESS;
1276 }
1277
1278 static void ChromaDestroy( vout_thread_t *p_vout )
1279 {
1280     assert( !p_vout->b_direct );
1281
1282     if( !p_vout->p_chroma )
1283         return;
1284
1285     module_Unneed( p_vout->p_chroma, p_vout->p_chroma->p_module );
1286     vlc_object_release( p_vout->p_chroma );
1287     p_vout->p_chroma = NULL;
1288 }
1289
1290
1291
1292 /* following functions are local */
1293
1294 static int ReduceHeight( int i_ratio )
1295 {
1296     int i_dummy = VOUT_ASPECT_FACTOR;
1297     int i_pgcd  = 1;
1298
1299     if( !i_ratio )
1300     {
1301         return i_pgcd;
1302     }
1303
1304     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1305     while( !(i_ratio & 1) && !(i_dummy & 1) )
1306     {
1307         i_ratio >>= 1;
1308         i_dummy >>= 1;
1309         i_pgcd  <<= 1;
1310     }
1311
1312     while( !(i_ratio % 3) && !(i_dummy % 3) )
1313     {
1314         i_ratio /= 3;
1315         i_dummy /= 3;
1316         i_pgcd  *= 3;
1317     }
1318
1319     while( !(i_ratio % 5) && !(i_dummy % 5) )
1320     {
1321         i_ratio /= 5;
1322         i_dummy /= 5;
1323         i_pgcd  *= 5;
1324     }
1325
1326     return i_pgcd;
1327 }
1328
1329 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1330 {
1331     unsigned int i_pgcd = ReduceHeight( i_aspect );
1332     *i_aspect_x = i_aspect / i_pgcd;
1333     *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1334 }
1335
1336 /*****************************************************************************
1337  * BinaryLog: computes the base 2 log of a binary value
1338  *****************************************************************************
1339  * This functions is used by MaskToShift, to get a bit index from a binary
1340  * value.
1341  *****************************************************************************/
1342 static int BinaryLog( uint32_t i )
1343 {
1344     int i_log = 0;
1345
1346     if( i == 0 ) return -31337;
1347
1348     if( i & 0xffff0000 ) i_log += 16;
1349     if( i & 0xff00ff00 ) i_log += 8;
1350     if( i & 0xf0f0f0f0 ) i_log += 4;
1351     if( i & 0xcccccccc ) i_log += 2;
1352     if( i & 0xaaaaaaaa ) i_log += 1;
1353
1354     return i_log;
1355 }
1356
1357 /*****************************************************************************
1358  * MaskToShift: transform a color mask into right and left shifts
1359  *****************************************************************************
1360  * This function is used for obtaining color shifts from masks.
1361  *****************************************************************************/
1362 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1363 {
1364     uint32_t i_low, i_high;            /* lower hand higher bits of the mask */
1365
1366     if( !i_mask )
1367     {
1368         *pi_left = *pi_right = 0;
1369         return;
1370     }
1371
1372     /* Get bits */
1373     i_low = i_high = i_mask;
1374
1375     i_low &= - (int32_t)i_low;          /* lower bit of the mask */
1376     i_high += i_low;                    /* higher bit of the mask */
1377
1378     /* Transform bits into an index. Also deal with i_high overflow, which
1379      * is faster than changing the BinaryLog code to handle 64 bit integers. */
1380     i_low =  BinaryLog (i_low);
1381     i_high = i_high ? BinaryLog (i_high) : 32;
1382
1383     /* Update pointers and return */
1384     *pi_left =   i_low;
1385     *pi_right = (8 - i_high + i_low);
1386 }
1387
1388 /*****************************************************************************
1389  * vout_VarCallback: generic callback for intf variables
1390  *****************************************************************************/
1391 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1392                       vlc_value_t oldval, vlc_value_t newval,
1393                       void *p_data )
1394 {
1395     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1396     vlc_value_t val;
1397     (void)psz_variable; (void)newval; (void)oldval; (void)p_data;
1398     val.b_bool = true;
1399     var_Set( p_vout, "intf-change", val );
1400     return VLC_SUCCESS;
1401 }
1402
1403 /*****************************************************************************
1404  * object variables callbacks: a bunch of object variables are used by the
1405  * interfaces to interact with the vout.
1406  *****************************************************************************/
1407 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1408                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1409 {
1410     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1411     input_thread_t *p_input;
1412     vlc_value_t val;
1413
1414     char *psz_mode = newval.psz_string;
1415     char *psz_filter, *psz_deinterlace = NULL;
1416     (void)psz_cmd; (void)oldval; (void)p_data;
1417
1418     var_Get( p_vout, "vout-filter", &val );
1419     psz_filter = val.psz_string;
1420     if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1421
1422     if( !psz_mode || !*psz_mode )
1423     {
1424         if( psz_deinterlace )
1425         {
1426             char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1427             if( psz_src[0] == ':' ) psz_src++;
1428             memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1429         }
1430     }
1431     else if( !psz_deinterlace )
1432     {
1433         psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1434                               sizeof(":deinterlace") );
1435         if( psz_filter && *psz_filter ) strcat( psz_filter, ":" );
1436         strcat( psz_filter, "deinterlace" );
1437     }
1438
1439     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1440                                                  FIND_PARENT );
1441     if( !p_input ) return VLC_EGENERIC;
1442
1443     if( psz_mode && *psz_mode )
1444     {
1445         /* Modify input as well because the vout might have to be restarted */
1446         val.psz_string = psz_mode;
1447         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1448         var_Set( p_input, "deinterlace-mode", val );
1449     }
1450     vlc_object_release( p_input );
1451
1452     val.b_bool = true;
1453     var_Set( p_vout, "intf-change", val );
1454
1455     val.psz_string = psz_filter;
1456     var_Set( p_vout, "vout-filter", val );
1457     free( psz_filter );
1458
1459     return VLC_SUCCESS;
1460 }
1461
1462 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1463                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1464 {
1465     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1466     input_thread_t *p_input;
1467     vlc_value_t val;
1468     (void)psz_cmd; (void)oldval; (void)p_data;
1469
1470     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1471                                                  FIND_PARENT );
1472     if (!p_input)
1473     {
1474         msg_Err( p_vout, "Input not found" );
1475         return( VLC_EGENERIC );
1476     }
1477
1478     val.b_bool = true;
1479     var_Set( p_vout, "intf-change", val );
1480
1481     /* Modify input as well because the vout might have to be restarted */
1482     val.psz_string = newval.psz_string;
1483     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1484
1485     var_Set( p_input, "vout-filter", val );
1486
1487     /* Now restart current video stream */
1488     var_Get( p_input, "video-es", &val );
1489     if( val.i_int >= 0 )
1490     {
1491         p_vout->b_filter_change = true;
1492         /* To all (embedded) video output developpers:
1493          * Beware of callback loops if you listen on video-es. Really.
1494          * You were warned. -- Courmisch */
1495         var_SetInteger( p_input, "video-es", -VIDEO_ES );
1496         var_Set( p_input, "video-es", val );
1497     }
1498     vlc_object_release( p_input );
1499     return VLC_SUCCESS;
1500 }
1501
1502 /*****************************************************************************
1503  * Video Filter2 stuff
1504  *****************************************************************************/
1505 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1506                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1507 {
1508     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1509     (void)psz_cmd; (void)oldval; (void)p_data;
1510
1511     vlc_mutex_lock( &p_vout->vfilter_lock );
1512     p_vout->psz_vf2 = strdup( newval.psz_string );
1513     vlc_mutex_unlock( &p_vout->vfilter_lock );
1514
1515     return VLC_SUCCESS;
1516 }
1517
1518 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1519 {
1520     input_thread_t *p_input;
1521     mtime_t i_now, i_stop;
1522
1523     p_input = (input_thread_t *)vlc_object_find( p_vout,
1524               VLC_OBJECT_INPUT, FIND_ANYWHERE );
1525     if( p_input )
1526     {
1527         i_now = mdate();
1528         i_stop = i_now + (mtime_t)(p_vout->i_title_timeout * 1000);
1529         char *psz_nowplaying =
1530             input_item_GetNowPlaying( input_GetItem( p_input ) );
1531         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
1532         char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
1533         if( EMPTY_STR( psz_name ) )
1534         {
1535             free( psz_name );
1536             psz_name = input_item_GetName( input_GetItem( p_input ) );
1537         }
1538         if( !EMPTY_STR( psz_nowplaying ) )
1539         {
1540             vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1541                                    psz_nowplaying, NULL,
1542                                    p_vout->i_title_position,
1543                                    30 + p_vout->fmt_in.i_width
1544                                       - p_vout->fmt_in.i_visible_width
1545                                       - p_vout->fmt_in.i_x_offset,
1546                                    20 + p_vout->fmt_in.i_y_offset,
1547                                    i_now, i_stop );
1548         }
1549         else if( !EMPTY_STR( psz_artist ) )
1550         {
1551             char *psz_string = NULL;
1552             if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
1553             {
1554                 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1555                                        psz_string, NULL,
1556                                        p_vout->i_title_position,
1557                                        30 + p_vout->fmt_in.i_width
1558                                           - p_vout->fmt_in.i_visible_width
1559                                           - p_vout->fmt_in.i_x_offset,
1560                                        20 + p_vout->fmt_in.i_y_offset,
1561                                        i_now, i_stop );
1562                 free( psz_string );
1563             }
1564         }
1565         else
1566         {
1567             vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1568                                    psz_name, NULL,
1569                                    p_vout->i_title_position,
1570                                    30 + p_vout->fmt_in.i_width
1571                                       - p_vout->fmt_in.i_visible_width
1572                                       - p_vout->fmt_in.i_x_offset,
1573                                    20 + p_vout->fmt_in.i_y_offset,
1574                                    i_now, i_stop );
1575         }
1576         vlc_object_release( p_input );
1577         free( psz_artist );
1578         free( psz_name );
1579         free( psz_nowplaying );
1580     }
1581 }
1582