]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
c6eedbc9e7e163a6adb942b19ec3c6939c29b7b7
[vlc] / src / video_output / video_output.c
1 /*****************************************************************************
2  * video_output.c : video output thread
3  * This module describes the programming interface for video output threads.
4  * It includes functions allowing to open a new thread, send pictures to a
5  * thread, and destroy a previously oppened video output thread.
6  *****************************************************************************
7  * Copyright (C) 2000-2001 VideoLAN
8  * $Id: video_output.c,v 1.229 2003/07/14 21:32:59 sigmunau Exp $
9  *
10  * Authors: Vincent Seguin <seguin@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                                /* free() */
31
32 #include <vlc/vlc.h>
33
34 #ifdef HAVE_SYS_TIMES_H
35 #   include <sys/times.h>
36 #endif
37
38 #include "vlc_video.h"
39 #include "video_output.h"
40 #include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
41
42 #if defined( SYS_DARWIN )
43 #include "darwin_specific.h"
44 #endif
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int      InitThread        ( vout_thread_t * );
50 static void     RunThread         ( vout_thread_t * );
51 static void     ErrorThread       ( vout_thread_t * );
52 static void     EndThread         ( vout_thread_t * );
53 static void     DestroyThread     ( vout_thread_t * );
54
55 static int      ReduceHeight      ( int );
56 static int      BinaryLog         ( uint32_t );
57 static void     MaskToShift       ( int *, int *, uint32_t );
58 static void     InitWindowSize    ( vout_thread_t *, int *, int * );
59
60 /* Object variables callbacks */
61 static int FullscreenCallback( vlc_object_t *, char const *,
62                                vlc_value_t, vlc_value_t, void * );
63 static int DeinterlaceCallback( vlc_object_t *, char const *,
64                                 vlc_value_t, vlc_value_t, void * );
65
66 /*****************************************************************************
67  * vout_Request: find a video output thread, create one, or destroy one.
68  *****************************************************************************
69  * This function looks for a video output thread matching the current
70  * properties. If not found, it spawns a new one.
71  *****************************************************************************/
72 vout_thread_t * __vout_Request ( vlc_object_t *p_this, vout_thread_t *p_vout,
73                                  unsigned int i_width, unsigned int i_height,
74                                  vlc_fourcc_t i_chroma, unsigned int i_aspect )
75 {
76     if( !i_width || !i_height || !i_chroma )
77     {
78         /* Reattach video output to input before bailing out */
79         if( p_vout )
80         {
81             vlc_object_t *p_input;
82
83             p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
84
85             if( p_input )
86             {
87                 vlc_object_detach( p_vout );
88                 vlc_object_attach( p_vout, p_input );
89
90                 vlc_object_release( p_input );
91             }
92             else
93             {
94                 vlc_object_detach( p_vout );
95                 /* vlc_object_release( p_vout ); */
96                 vout_Destroy( p_vout );
97             }
98         }
99         return NULL;
100     }
101
102     /* If a video output was provided, lock it, otherwise look for one. */
103     if( p_vout )
104     {
105         vlc_object_yield( p_vout );
106     }
107     else
108     {
109         p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
110
111         if( !p_vout )
112         {
113             vlc_object_t *p_input;
114
115             p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
116             if( p_input )
117             {
118                 p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT,
119                                           FIND_CHILD );
120                 /* only first children of p_input for unused vout */
121                 if( p_vout && p_vout->p_parent != p_input )
122                 {
123                     vlc_object_release( p_vout );
124                     p_vout = NULL;
125                 }
126                 vlc_object_release( p_input );
127             }
128         }
129     }
130
131     /* If we now have a video output, check it has the right properties */
132     if( p_vout )
133     {
134         char *psz_filter_chain;
135
136         /* We don't directly check for the "filter" variable for obvious
137          * performance reasons. */
138         if( p_vout->b_filter_change )
139         {
140             psz_filter_chain = config_GetPsz( p_this, "filter" );
141
142             if( psz_filter_chain && !*psz_filter_chain )
143             {
144                 free( psz_filter_chain );
145                 psz_filter_chain = NULL;
146             }
147             if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
148             {
149                 free( p_vout->psz_filter_chain );
150                 p_vout->psz_filter_chain = NULL;
151             }
152
153             if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
154                 ( psz_filter_chain && p_vout->psz_filter_chain &&
155                   !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
156             {
157                 p_vout->b_filter_change = VLC_FALSE;
158             }
159
160             if( psz_filter_chain ) free( psz_filter_chain );
161         }
162
163         if( ( p_vout->render.i_width != i_width ) ||
164             ( p_vout->render.i_height != i_height ) ||
165             ( p_vout->render.i_chroma != i_chroma ) ||
166             ( p_vout->render.i_aspect != i_aspect
167                     && !p_vout->b_override_aspect ) ||
168             p_vout->b_filter_change )
169         {
170             /* We are not interested in this format, close this vout */
171             vlc_object_detach( p_vout );
172             vlc_object_release( p_vout );
173             vout_Destroy( p_vout );
174             p_vout = NULL;
175         }
176         else
177         {
178             /* This video output is cool! Hijack it. */
179             vlc_object_detach( p_vout );
180             vlc_object_attach( p_vout, p_this );
181             vlc_object_release( p_vout );
182         }
183     }
184
185     if( !p_vout )
186     {
187         msg_Dbg( p_this, "no usable vout present, spawning one" );
188
189         p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
190     }
191
192     return p_vout;
193 }
194
195 /*****************************************************************************
196  * vout_Create: creates a new video output thread
197  *****************************************************************************
198  * This function creates a new video output thread, and returns a pointer
199  * to its description. On error, it returns NULL.
200  *****************************************************************************/
201 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
202                                unsigned int i_width, unsigned int i_height,
203                                vlc_fourcc_t i_chroma, unsigned int i_aspect )
204 {
205     vout_thread_t  * p_vout;                            /* thread descriptor */
206     input_thread_t * p_input_thread;
207     int              i_index;                               /* loop variable */
208     char           * psz_plugin;
209     vlc_value_t      val, text;
210
211     /* Allocate descriptor */
212     p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
213     if( p_vout == NULL )
214     {
215         msg_Err( p_parent, "out of memory" );
216         return NULL;
217     }
218
219     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
220     val.b_bool = VLC_TRUE;
221     var_Set( p_vout, "intf-change", val );
222
223     p_vout->b_override_aspect = VLC_FALSE;
224
225     /* If the parent is not a VOUT object, that means we are at the start of
226      * the video output pipe */
227     if( p_parent->i_object_type != VLC_OBJECT_VOUT )
228     {
229         char *psz_aspect = config_GetPsz( p_parent, "aspect-ratio" );
230
231         /* Check whether the user tried to override aspect ratio */
232         if( psz_aspect )
233         {
234             unsigned int i_new_aspect = i_aspect;
235             char *psz_parser = strchr( psz_aspect, ':' );
236
237             if( psz_parser )
238             {
239                 *psz_parser++ = '\0';
240                 i_new_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR
241                                                   / atoi( psz_parser );
242             }
243             else
244             {
245                 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
246                                        * atof( psz_aspect )
247                                        / i_height;
248             }
249
250             free( psz_aspect );
251
252             if( i_new_aspect && i_new_aspect != i_aspect )
253             {
254                 int i_pgcd = ReduceHeight( i_new_aspect );
255
256                 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
257                          i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
258
259                 i_aspect = i_new_aspect;
260
261                 p_vout->b_override_aspect = VLC_TRUE;
262             }
263         }
264
265         /* Look for the default filter configuration */
266         p_vout->psz_filter_chain = config_GetPsz( p_parent, "filter" );
267     }
268     else
269     {
270         /* continue the parent's filter chain */
271         char *psz_end;
272
273         psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
274         if( psz_end && *(psz_end+1) )
275             p_vout->psz_filter_chain = strdup( psz_end+1 );
276         else p_vout->psz_filter_chain = NULL;
277     }
278
279     /* Choose the video output module */
280     if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
281     {
282         psz_plugin = config_GetPsz( p_parent, "vout" );
283     }
284     else
285     {
286         /* the filter chain is a string list of filters separated by double
287          * colons */
288         char *psz_end;
289
290         psz_end = strchr( p_vout->psz_filter_chain, ':' );
291         if( psz_end )
292             psz_plugin = strndup( p_vout->psz_filter_chain,
293                                   psz_end - p_vout->psz_filter_chain );
294         else psz_plugin = strdup( p_vout->psz_filter_chain );
295     }
296
297     /* Initialize pictures and subpictures - translation tables and functions
298      * will be initialized later in InitThread */
299     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
300     {
301         p_vout->p_picture[i_index].pf_lock = NULL;
302         p_vout->p_picture[i_index].pf_unlock = NULL;
303         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
304         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
305     }
306
307     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
308     {
309         p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
310         p_vout->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
311     }
312
313     /* No images in the heap */
314     p_vout->i_heap_size = 0;
315
316     /* Initialize the rendering heap */
317     I_RENDERPICTURES = 0;
318     p_vout->render.i_width    = i_width;
319     p_vout->render.i_height   = i_height;
320     p_vout->render.i_chroma   = i_chroma;
321     p_vout->render.i_aspect   = i_aspect;
322
323     p_vout->render.i_rmask    = 0;
324     p_vout->render.i_gmask    = 0;
325     p_vout->render.i_bmask    = 0;
326
327     p_vout->render.i_last_used_pic = -1;
328     p_vout->render.b_allow_modify_pics = 1;
329
330     /* Zero the output heap */
331     I_OUTPUTPICTURES = 0;
332     p_vout->output.i_width    = 0;
333     p_vout->output.i_height   = 0;
334     p_vout->output.i_chroma   = 0;
335     p_vout->output.i_aspect   = 0;
336
337     p_vout->output.i_rmask    = 0;
338     p_vout->output.i_gmask    = 0;
339     p_vout->output.i_bmask    = 0;
340
341     /* Initialize misc stuff */
342     p_vout->i_changes    = 0;
343     p_vout->f_gamma      = 0;
344     p_vout->b_grayscale  = 0;
345     p_vout->b_info       = 0;
346     p_vout->b_interface  = 0;
347     p_vout->b_scale      = 1;
348     p_vout->b_fullscreen = 0;
349     p_vout->render_time  = 10;
350     p_vout->c_fps_samples = 0;
351     p_vout->b_filter_change = 0;
352
353     /* Mouse coordinates */
354     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
355     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
356     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
357     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
358     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
359     var_Create( p_vout, "key-pressed", VLC_VAR_STRING );
360
361     /* Initialize the dimensions of the video window */
362     InitWindowSize( p_vout, &p_vout->i_window_width,
363                     &p_vout->i_window_height );
364
365     /* Create thread and set locks */
366     vlc_mutex_init( p_vout, &p_vout->picture_lock );
367     vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
368     vlc_mutex_init( p_vout, &p_vout->change_lock );
369
370     vlc_object_attach( p_vout, p_parent );
371
372     p_vout->p_module = module_Need( p_vout,
373                            ( p_vout->psz_filter_chain &&
374                                *p_vout->psz_filter_chain ) ?
375                            "video filter" : "video output",
376                            psz_plugin );
377
378     if( psz_plugin ) free( psz_plugin );
379     if( p_vout->p_module == NULL )
380     {
381         msg_Err( p_vout, "no suitable vout module" );
382         vlc_object_destroy( p_vout );
383         return NULL;
384     }
385     p_vout->p_text_renderer_module = module_Need( p_vout, "text renderer", 
386                                                   NULL );
387     if( p_vout->p_text_renderer_module == NULL )
388     {
389         msg_Warn( p_vout, "no suitable text renderer module" );
390         p_vout->pf_add_string = NULL;
391     }
392     /* Create a few object variables for interface interaction */
393     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
394     text.psz_string = _("Fullscreen");
395     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
396     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
397     if( val.b_bool )
398     {
399         /* user requested fullscreen */
400         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
401     }
402     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
403
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 = ""; text.psz_string = _("Disable");
408     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
409     val.psz_string = "discard"; text.psz_string = _("Discard");
410     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
411     val.psz_string = "blend"; text.psz_string = _("Blend");
412     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
413     val.psz_string = "mean"; text.psz_string = _("Mean");
414     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
415     val.psz_string = "bob"; text.psz_string = _("Bob");
416     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
417     val.psz_string = "linear"; text.psz_string = _("Linear");
418     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
419     if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
420     {
421         var_Set( p_vout, "deinterlace", val );
422     }
423     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
424
425     /* Calculate delay created by internal caching */
426     p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
427                                            VLC_OBJECT_INPUT, FIND_ANYWHERE );
428     if( p_input_thread )
429     {
430         p_vout->i_pts_delay = p_input_thread->i_pts_delay;
431         vlc_object_release( p_input_thread );
432     }
433     else
434     {
435         p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
436     }
437
438     if( vlc_thread_create( p_vout, "video output", RunThread,
439                            VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
440     {
441         msg_Err( p_vout, "out of memory" );
442         module_Unneed( p_vout, p_vout->p_module );
443         vlc_object_destroy( p_vout );
444         return NULL;
445     }
446
447     return p_vout;
448 }
449
450 /*****************************************************************************
451  * vout_Destroy: destroys a previously created video output
452  *****************************************************************************
453  * Destroy a terminated thread.
454  * The function will request a destruction of the specified thread. If pi_error
455  * is NULL, it will return once the thread is destroyed. Else, it will be
456  * update using one of the THREAD_* constants.
457  *****************************************************************************/
458 void vout_Destroy( vout_thread_t *p_vout )
459
460     vlc_object_t *p_playlist;
461     
462     /* Request thread destruction */
463     p_vout->b_die = VLC_TRUE;
464     vlc_thread_join( p_vout );
465
466     var_Destroy( p_vout, "intf-change" );
467
468     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, 
469                                   FIND_ANYWHERE );
470
471     /* Free structure */
472     vlc_object_destroy( p_vout );
473
474     /* If it was the last vout, tell the interface to show up */
475     if( p_playlist != NULL )
476     {
477         vout_thread_t *p_another_vout = vlc_object_find( p_playlist, 
478                                             VLC_OBJECT_VOUT, FIND_ANYWHERE );
479         if( p_another_vout == NULL )
480         {
481             vlc_value_t val;
482             val.b_bool = VLC_TRUE;
483             var_Set( p_playlist, "intf-show", val );
484         }
485         else
486         {
487             vlc_object_release( p_another_vout );
488         }
489         vlc_object_release( p_playlist );
490     }
491 }
492
493 /*****************************************************************************
494  * InitThread: initialize video output thread
495  *****************************************************************************
496  * This function is called from RunThread and performs the second step of the
497  * initialization. It returns 0 on success. Note that the thread's flag are not
498  * modified inside this function.
499  *****************************************************************************/
500 static int InitThread( vout_thread_t *p_vout )
501 {
502     int i, i_pgcd;
503
504     vlc_mutex_lock( &p_vout->change_lock );
505
506 #ifdef STATS
507     p_vout->c_loops = 0;
508 #endif
509
510     /* Initialize output method, it allocates direct buffers for us */
511     if( p_vout->pf_init( p_vout ) )
512     {
513         vlc_mutex_unlock( &p_vout->change_lock );
514         return VLC_EGENERIC;
515     }
516
517     if( !I_OUTPUTPICTURES )
518     {
519         msg_Err( p_vout, "plugin was unable to allocate at least "
520                          "one direct buffer" );
521         p_vout->pf_end( p_vout );
522         vlc_mutex_unlock( &p_vout->change_lock );
523         return VLC_EGENERIC;
524     }
525
526     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
527     {
528         msg_Err( p_vout, "plugin allocated too many direct buffers, "
529                          "our internal buffers must have overflown." );
530         p_vout->pf_end( p_vout );
531         vlc_mutex_unlock( &p_vout->change_lock );
532         return VLC_EGENERIC;
533     }
534
535     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
536
537 #if 0
538     if( !p_vout->psz_filter_chain )
539     {
540         char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
541
542         if( psz_aspect )
543         {
544             int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
545                                                       * atof( psz_aspect )
546                                                       / p_vout->output.i_height;
547             free( psz_aspect );
548
549             if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
550             {
551                 int i_pgcd = ReduceHeight( i_new_aspect );
552                 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
553                          i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
554                 p_vout->output.i_aspect = i_new_aspect;
555             }
556         }
557     }
558 #endif
559
560     i_pgcd = ReduceHeight( p_vout->render.i_aspect );
561     msg_Dbg( p_vout,
562              "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
563              p_vout->render.i_width, p_vout->render.i_height,
564              p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
565              p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
566
567     i_pgcd = ReduceHeight( p_vout->output.i_aspect );
568     msg_Dbg( p_vout,
569              "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
570              p_vout->output.i_width, p_vout->output.i_height,
571              p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
572              p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
573
574     /* Calculate shifts from system-updated masks */
575     MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
576                  p_vout->output.i_rmask );
577     MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
578                  p_vout->output.i_gmask );
579     MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
580                  p_vout->output.i_bmask );
581
582     /* Check whether we managed to create direct buffers similar to
583      * the render buffers, ie same size and chroma */
584     if( ( p_vout->output.i_width == p_vout->render.i_width )
585      && ( p_vout->output.i_height == p_vout->render.i_height )
586      && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
587     {
588         /* Cool ! We have direct buffers, we can ask the decoder to
589          * directly decode into them ! Map the first render buffers to
590          * the first direct buffers, but keep the first direct buffer
591          * for memcpy operations */
592         p_vout->b_direct = 1;
593
594         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
595         {
596             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
597                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
598                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
599             {
600                 /* We have enough direct buffers so there's no need to
601                  * try to use system memory buffers. */
602                 break;
603             }
604             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
605             I_RENDERPICTURES++;
606         }
607
608         msg_Dbg( p_vout, "direct render, mapping "
609                  "render pictures 0-%i to system pictures 1-%i",
610                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
611     }
612     else
613     {
614         /* Rats... Something is wrong here, we could not find an output
615          * plugin able to directly render what we decode. See if we can
616          * find a chroma plugin to do the conversion */
617         p_vout->b_direct = 0;
618
619         /* Choose the best module */
620         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
621
622         if( p_vout->chroma.p_module == NULL )
623         {
624             msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
625                      (char*)&p_vout->render.i_chroma,
626                      (char*)&p_vout->output.i_chroma );
627             p_vout->pf_end( p_vout );
628             vlc_mutex_unlock( &p_vout->change_lock );
629             return VLC_EGENERIC;
630         }
631
632         msg_Dbg( p_vout, "indirect render, mapping "
633                  "render pictures 0-%i to system pictures %i-%i",
634                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
635                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
636
637         /* Append render buffers after the direct buffers */
638         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
639         {
640             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
641             I_RENDERPICTURES++;
642
643             /* Check if we have enough render pictures */
644             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
645                 break;
646         }
647     }
648
649     /* Link pictures back to their heap */
650     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
651     {
652         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
653     }
654
655     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
656     {
657         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
658     }
659
660 /* XXX XXX mark thread ready */
661     return VLC_SUCCESS;
662 }
663
664 /*****************************************************************************
665  * RunThread: video output thread
666  *****************************************************************************
667  * Video output thread. This function does only returns when the thread is
668  * terminated. It handles the pictures arriving in the video heap and the
669  * display device events.
670  *****************************************************************************/
671 static void RunThread( vout_thread_t *p_vout)
672 {
673     int             i_index;                                /* index in heap */
674     int             i_idle_loops = 0;  /* loops without displaying a picture */
675     mtime_t         current_date;                            /* current date */
676     mtime_t         display_date;                            /* display date */
677
678     picture_t *     p_picture;                            /* picture pointer */
679     picture_t *     p_last_picture = NULL;                   /* last picture */
680     picture_t *     p_directbuffer;              /* direct buffer to display */
681
682     subpicture_t *  p_subpic;                          /* subpicture pointer */
683
684     /*
685      * Initialize thread
686      */
687     p_vout->b_error = InitThread( p_vout );
688
689     /* signal the creation of the vout */
690     vlc_thread_ready( p_vout );
691
692     if( p_vout->b_error )
693     {
694         /* Destroy thread structures allocated by Create and InitThread */
695         DestroyThread( p_vout );
696         return;
697     }
698
699     /*
700      * Main loop - it is not executed if an error occured during
701      * initialization
702      */
703     while( (!p_vout->b_die) && (!p_vout->b_error) )
704     {
705         /* Initialize loop variables */
706         p_picture = NULL;
707         display_date = 0;
708         current_date = mdate();
709
710 #if 0
711         p_vout->c_loops++;
712         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
713         {
714             msg_Dbg( p_vout, "picture heap: %d/%d",
715                      I_RENDERPICTURES, p_vout->i_heap_size );
716         }
717 #endif
718
719         /*
720          * Find the picture to display (the one with the earliest date).
721          * This operation does not need lock, since only READY_PICTUREs
722          * are handled. */
723         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
724         {
725             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
726                 && ( (p_picture == NULL) ||
727                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
728             {
729                 p_picture = PP_RENDERPICTURE[i_index];
730                 display_date = p_picture->date;
731             }
732         }
733
734         if( p_picture )
735         {
736             /* If we met the last picture, parse again to see whether there is
737              * a more appropriate one. */
738             if( p_picture == p_last_picture )
739             {
740                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
741                 {
742                     if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
743                         && (PP_RENDERPICTURE[i_index] != p_last_picture)
744                         && ((p_picture == p_last_picture) ||
745                             (PP_RENDERPICTURE[i_index]->date < display_date)) )
746                     {
747                         p_picture = PP_RENDERPICTURE[i_index];
748                         display_date = p_picture->date;
749                     }
750                 }
751             }
752
753             /* If we found better than the last picture, destroy it */
754             if( p_last_picture && p_picture != p_last_picture )
755             {
756                 vlc_mutex_lock( &p_vout->picture_lock );
757                 if( p_last_picture->i_refcount )
758                 {
759                     p_last_picture->i_status = DISPLAYED_PICTURE;
760                 }
761                 else
762                 {
763                     p_last_picture->i_status = DESTROYED_PICTURE;
764                     p_vout->i_heap_size--;
765                 }
766                 vlc_mutex_unlock( &p_vout->picture_lock );
767                 p_last_picture = NULL;
768             }
769
770             /* Compute FPS rate */
771             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
772                 = display_date;
773
774             if( !p_picture->b_force &&
775                 p_picture != p_last_picture &&
776                 display_date < current_date + p_vout->render_time )
777             {
778                 /* Picture is late: it will be destroyed and the thread
779                  * will directly choose the next picture */
780                 vlc_mutex_lock( &p_vout->picture_lock );
781                 if( p_picture->i_refcount )
782                 {
783                     /* Pretend we displayed the picture, but don't destroy
784                      * it since the decoder might still need it. */
785                     p_picture->i_status = DISPLAYED_PICTURE;
786                 }
787                 else
788                 {
789                     /* Destroy the picture without displaying it */
790                     p_picture->i_status = DESTROYED_PICTURE;
791                     p_vout->i_heap_size--;
792                 }
793                 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
794                                   current_date - display_date );
795                 vlc_mutex_unlock( &p_vout->picture_lock );
796
797                 continue;
798             }
799
800             if( display_date >
801                 current_date + p_vout->i_pts_delay +  VOUT_BOGUS_DELAY )
802             {
803                 /* Picture is waaay too early: it will be destroyed */
804                 vlc_mutex_lock( &p_vout->picture_lock );
805                 if( p_picture->i_refcount )
806                 {
807                     /* Pretend we displayed the picture, but don't destroy
808                      * it since the decoder might still need it. */
809                     p_picture->i_status = DISPLAYED_PICTURE;
810                 }
811                 else
812                 {
813                     /* Destroy the picture without displaying it */
814                     p_picture->i_status = DESTROYED_PICTURE;
815                     p_vout->i_heap_size--;
816                 }
817                 msg_Warn( p_vout, "vout warning: early picture skipped "
818                           "("I64Fd")", display_date - current_date
819                           - p_vout->i_pts_delay );
820                 vlc_mutex_unlock( &p_vout->picture_lock );
821
822                 continue;
823             }
824
825             if( display_date > current_date + VOUT_DISPLAY_DELAY )
826             {
827                 /* A picture is ready to be rendered, but its rendering date
828                  * is far from the current one so the thread will perform an
829                  * empty loop as if no picture were found. The picture state
830                  * is unchanged */
831                 p_picture    = NULL;
832                 display_date = 0;
833             }
834             else if( p_picture == p_last_picture )
835             {
836                 /* We are asked to repeat the previous picture, but we first
837                  * wait for a couple of idle loops */
838                 if( i_idle_loops < 4 )
839                 {
840                     p_picture    = NULL;
841                     display_date = 0;
842                 }
843                 else
844                 {
845                     /* We set the display date to something high, otherwise
846                      * we'll have lots of problems with late pictures */
847                     display_date = current_date + p_vout->render_time;
848                 }
849             }
850         }
851
852         if( p_picture == NULL )
853         {
854             i_idle_loops++;
855         }
856
857         /*
858          * Check for subpictures to display
859          */
860         p_subpic = vout_SortSubPictures( p_vout, display_date );
861
862         /*
863          * Perform rendering
864          */
865         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
866
867         /*
868          * Call the plugin-specific rendering method if there is one
869          */
870         if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
871         {
872             /* Render the direct buffer returned by vout_RenderPicture */
873             p_vout->pf_render( p_vout, p_directbuffer );
874         }
875
876         /*
877          * Sleep, wake up
878          */
879         if( display_date != 0 && p_directbuffer != NULL )
880         {
881             /* Store render time using a sliding mean */
882             p_vout->render_time += mdate() - current_date;
883             p_vout->render_time >>= 1;
884         }
885
886         /* Give back change lock */
887         vlc_mutex_unlock( &p_vout->change_lock );
888
889         /* Sleep a while or until a given date */
890         if( display_date != 0 )
891         {
892             mwait( display_date - VOUT_MWAIT_TOLERANCE );
893         }
894         else
895         {
896             msleep( VOUT_IDLE_SLEEP );
897         }
898
899         /* On awakening, take back lock and send immediately picture
900          * to display. */
901         vlc_mutex_lock( &p_vout->change_lock );
902
903         /*
904          * Display the previously rendered picture
905          */
906         if( p_picture != NULL && p_directbuffer != NULL )
907         {
908             /* Display the direct buffer returned by vout_RenderPicture */
909             if( p_vout->pf_display )
910             {
911                 p_vout->pf_display( p_vout, p_directbuffer );
912             }
913
914             /* Reinitialize idle loop count */
915             i_idle_loops = 0;
916
917             /* Tell the vout this was the last picture and that it does not
918              * need to be forced anymore. */
919             p_last_picture = p_picture;
920             p_last_picture->b_force = 0;
921         }
922
923         /*
924          * Check events and manage thread
925          */
926         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
927         {
928             /* A fatal error occured, and the thread must terminate
929              * immediately, without displaying anything - setting b_error to 1
930              * causes the immediate end of the main while() loop. */
931             p_vout->b_error = 1;
932         }
933
934         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
935         {
936             /* this must only happen when the vout plugin is incapable of
937              * rescaling the picture itself. In this case we need to destroy
938              * the current picture buffers and recreate new ones with the right
939              * dimensions */
940             int i;
941
942             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
943
944             p_vout->pf_end( p_vout );
945             for( i = 0; i < I_OUTPUTPICTURES; i++ )
946                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
947
948             I_OUTPUTPICTURES = 0;
949             if( p_vout->pf_init( p_vout ) )
950             {
951                 msg_Err( p_vout, "cannot resize display" );
952                 /* FIXME: pf_end will be called again in EndThread() */
953                 p_vout->b_error = 1;
954             }
955
956             /* Need to reinitialise the chroma plugin */
957             if( p_vout->chroma.p_module->pf_deactivate )
958                 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
959             p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
960         }
961     }
962
963     /*
964      * Error loop - wait until the thread destruction is requested
965      */
966     if( p_vout->b_error )
967     {
968         ErrorThread( p_vout );
969     }
970
971     /* End of thread */
972     EndThread( p_vout );
973
974     /* Destroy thread structures allocated by CreateThread */
975     DestroyThread( p_vout );
976 }
977
978 /*****************************************************************************
979  * ErrorThread: RunThread() error loop
980  *****************************************************************************
981  * This function is called when an error occured during thread main's loop. The
982  * thread can still receive feed, but must be ready to terminate as soon as
983  * possible.
984  *****************************************************************************/
985 static void ErrorThread( vout_thread_t *p_vout )
986 {
987     /* Wait until a `die' order */
988     while( !p_vout->b_die )
989     {
990         /* Sleep a while */
991         msleep( VOUT_IDLE_SLEEP );
992     }
993 }
994
995 /*****************************************************************************
996  * EndThread: thread destruction
997  *****************************************************************************
998  * This function is called when the thread ends after a sucessful
999  * initialization. It frees all ressources allocated by InitThread.
1000  *****************************************************************************/
1001 static void EndThread( vout_thread_t *p_vout )
1002 {
1003     int     i_index;                                        /* index in heap */
1004
1005 #ifdef STATS
1006     {
1007         struct tms cpu_usage;
1008         times( &cpu_usage );
1009
1010         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1011                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1012     }
1013 #endif
1014
1015     if( !p_vout->b_direct )
1016     {
1017         module_Unneed( p_vout, p_vout->chroma.p_module );
1018     }
1019     
1020     /* Destroy all remaining pictures */
1021     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1022     {
1023         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1024         {
1025             free( p_vout->p_picture[i_index].p_data_orig );
1026         }
1027     }
1028
1029     /* Destroy all remaining subpictures */
1030     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1031     {
1032         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1033         {
1034             vout_DestroySubPicture( p_vout,
1035                                     &p_vout->p_subpicture[i_index] );
1036         }
1037     }
1038
1039     module_Unneed( p_vout, p_vout->p_text_renderer_module );
1040     
1041     /* Destroy translation tables */
1042     p_vout->pf_end( p_vout );
1043
1044     /* Release the change lock */
1045     vlc_mutex_unlock( &p_vout->change_lock );
1046 }
1047
1048 /*****************************************************************************
1049  * DestroyThread: thread destruction
1050  *****************************************************************************
1051  * This function is called when the thread ends. It frees all ressources
1052  * allocated by CreateThread. Status is available at this stage.
1053  *****************************************************************************/
1054 static void DestroyThread( vout_thread_t *p_vout )
1055 {
1056     /* Destroy the locks */
1057     vlc_mutex_destroy( &p_vout->picture_lock );
1058     vlc_mutex_destroy( &p_vout->subpicture_lock );
1059     vlc_mutex_destroy( &p_vout->change_lock );
1060
1061     /* Release the module */
1062     module_Unneed( p_vout, p_vout->p_module );
1063 }
1064
1065 /* following functions are local */
1066
1067 static int ReduceHeight( int i_ratio )
1068 {
1069     int i_dummy = VOUT_ASPECT_FACTOR;
1070     int i_pgcd  = 1;
1071  
1072     if( !i_ratio )
1073     {
1074         return i_pgcd;
1075     }
1076
1077     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1078     while( !(i_ratio & 1) && !(i_dummy & 1) )
1079     {
1080         i_ratio >>= 1;
1081         i_dummy >>= 1;
1082         i_pgcd  <<= 1;
1083     }
1084
1085     while( !(i_ratio % 3) && !(i_dummy % 3) )
1086     {
1087         i_ratio /= 3;
1088         i_dummy /= 3;
1089         i_pgcd  *= 3;
1090     }
1091
1092     while( !(i_ratio % 5) && !(i_dummy % 5) )
1093     {
1094         i_ratio /= 5;
1095         i_dummy /= 5;
1096         i_pgcd  *= 5;
1097     }
1098
1099     return i_pgcd;
1100 }
1101
1102 /*****************************************************************************
1103  * BinaryLog: computes the base 2 log of a binary value
1104  *****************************************************************************
1105  * This functions is used by MaskToShift, to get a bit index from a binary
1106  * value.
1107  *****************************************************************************/
1108 static int BinaryLog( uint32_t i )
1109 {
1110     int i_log = 0;
1111
1112     if( i == 0 ) return -31337;
1113
1114     if( i & 0xffff0000 ) i_log += 16;
1115     if( i & 0xff00ff00 ) i_log += 8;
1116     if( i & 0xf0f0f0f0 ) i_log += 4;
1117     if( i & 0xcccccccc ) i_log += 2;
1118     if( i & 0xaaaaaaaa ) i_log += 1;
1119
1120     return i_log;
1121 }
1122
1123 /*****************************************************************************
1124  * MaskToShift: transform a color mask into right and left shifts
1125  *****************************************************************************
1126  * This function is used for obtaining color shifts from masks.
1127  *****************************************************************************/
1128 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1129 {
1130     uint32_t i_low, i_high;            /* lower hand higher bits of the mask */
1131
1132     if( !i_mask )
1133     {
1134         *pi_left = *pi_right = 0;
1135         return;
1136     }
1137
1138     /* Get bits */
1139     i_low =  i_mask & (- (int32_t)i_mask);          /* lower bit of the mask */
1140     i_high = i_mask + i_low;                       /* higher bit of the mask */
1141
1142     /* Transform bits into an index */
1143     i_low =  BinaryLog (i_low);
1144     i_high = BinaryLog (i_high);
1145
1146     /* Update pointers and return */
1147     *pi_left =   i_low;
1148     *pi_right = (8 - i_high + i_low);
1149 }
1150
1151 /*****************************************************************************
1152  * InitWindowSize: find the initial dimensions the video window should have.
1153  *****************************************************************************
1154  * This function will check the "width", "height" and "zoom" config options and
1155  * will calculate the size that the video window should have.
1156  *****************************************************************************/
1157 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1158                             int *pi_height )
1159 {
1160     int i_width, i_height;
1161     uint64_t ll_zoom;
1162
1163 #define FP_FACTOR 1000                             /* our fixed point factor */
1164
1165     i_width = config_GetInt( p_vout, "width" );
1166     i_height = config_GetInt( p_vout, "height" );
1167     ll_zoom = (uint64_t)( FP_FACTOR * config_GetFloat( p_vout, "zoom" ) );
1168
1169     if( i_width > 0 && i_height > 0)
1170     {
1171         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1172         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1173         return;
1174     }
1175     else if( i_width > 0 )
1176     {
1177         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1178         *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1179                             p_vout->render.i_aspect / FP_FACTOR );
1180         return;
1181     }
1182     else if( i_height > 0 )
1183     {
1184         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1185         *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1186                            VOUT_ASPECT_FACTOR / FP_FACTOR );
1187         return;
1188     }
1189
1190     if( p_vout->render.i_height * p_vout->render.i_aspect
1191         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1192     {
1193         *pi_width = (int)( p_vout->render.i_height * ll_zoom
1194           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1195         *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1196     }
1197     else
1198     {
1199         *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1200         *pi_height = (int)( p_vout->render.i_width * ll_zoom
1201           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1202     }
1203
1204 #undef FP_FACTOR
1205 }
1206
1207 /*****************************************************************************
1208  * vout_VarCallback: generic callback for intf variables
1209  *****************************************************************************/
1210 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1211                       vlc_value_t old_value, vlc_value_t new_value,
1212                       void * unused )
1213 {
1214     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1215     vlc_value_t val;
1216     val.b_bool = VLC_TRUE;
1217     var_Set( p_vout, "intf-change", val );
1218     return VLC_SUCCESS;
1219 }
1220
1221 /*****************************************************************************
1222  * object variables callbacks: a bunch of object variables are used by the
1223  * interfaces to interact with the vout.
1224  *****************************************************************************/
1225 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1226                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1227 {
1228     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1229     input_thread_t *p_input;
1230     vlc_value_t val;
1231
1232     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1233
1234     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1235                                                  FIND_PARENT );
1236     if( p_input )
1237     {
1238         /* Modify input as well because the vout might have to be restarted */
1239         var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1240         var_Set( p_input, "fullscreen", newval );
1241
1242         vlc_object_release( p_input );
1243     }
1244
1245     val.b_bool = VLC_TRUE;
1246     var_Set( p_vout, "intf-change", val );
1247     return VLC_SUCCESS;
1248 }
1249
1250 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1251                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1252 {
1253     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1254     input_thread_t *p_input;
1255     vlc_value_t val;
1256
1257     char *psz_mode = newval.psz_string;
1258     char *psz_filter;
1259     unsigned int  i;
1260
1261     psz_filter = config_GetPsz( p_vout, "filter" );
1262
1263     if( !psz_mode || !*psz_mode )
1264     {
1265         config_PutPsz( p_vout, "filter", "" );
1266     }
1267     else
1268     {
1269         if( !psz_filter || !*psz_filter )
1270         {
1271             config_PutPsz( p_vout, "filter", "deinterlace" );
1272         }
1273         else
1274         {
1275             if( strstr( psz_filter, "deinterlace" ) == NULL )
1276             {
1277                 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1278                 strcat( psz_filter, ",deinterlace" );
1279             }
1280             config_PutPsz( p_vout, "filter", psz_filter );
1281         }
1282     }
1283
1284     if( psz_filter ) free( psz_filter );
1285
1286
1287     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1288                                                  FIND_PARENT );
1289     if( !p_input ) return VLC_EGENERIC;
1290
1291     if( psz_mode && *psz_mode )
1292     {
1293         val.psz_string = psz_mode;
1294         var_Set( p_vout, "deinterlace-mode", val );
1295         /* Modify input as well because the vout might have to be restarted */
1296         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1297         var_Set( p_input, "deinterlace-mode", val );
1298     }
1299
1300     /* now restart all video streams */
1301     vlc_mutex_lock( &p_input->stream.stream_lock );
1302
1303     p_vout->b_filter_change = VLC_TRUE;
1304
1305 #define ES p_input->stream.pp_es[i]
1306
1307     for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1308     {
1309         if( ( ES->i_cat == VIDEO_ES ) && ES->p_decoder_fifo != NULL )
1310         {
1311             input_UnselectES( p_input, ES );
1312             input_SelectES( p_input, ES );
1313         }
1314 #undef ES
1315     }
1316     vlc_mutex_unlock( &p_input->stream.stream_lock );
1317
1318     vlc_object_release( p_input );
1319
1320     val.b_bool = VLC_TRUE;
1321     var_Set( p_vout, "intf-change", val );
1322     return VLC_SUCCESS;
1323 }