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