]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Removed vlc_object_find_name in vout.
[vlc] / src / video_output / video_output.c
1 /*****************************************************************************
2  * video_output.c : video output thread
3  *
4  * This module describes the programming interface for video output threads.
5  * It includes functions allowing to open a new thread, send pictures to a
6  * thread, and destroy a previously oppened video output thread.
7  *****************************************************************************
8  * Copyright (C) 2000-2007 the VideoLAN team
9  * $Id$
10  *
11  * Authors: Vincent Seguin <seguin@via.ecp.fr>
12  *          Gildas Bazin <gbazin@videolan.org>
13  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38
39 #include <stdlib.h>                                                /* free() */
40 #include <string.h>
41 #include <assert.h>
42
43 #include <vlc_vout.h>
44
45 #include <vlc_filter.h>
46 #include <vlc_osd.h>
47
48 #include <libvlc.h>
49 #include <vlc_input.h>
50 #include "vout_pictures.h"
51 #include "vout_internal.h"
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static void     *Thread(void *);
57 static void     vout_Destructor(vlc_object_t *);
58
59 /* Object variables callbacks */
60 static int FilterCallback( vlc_object_t *, char const *,
61                            vlc_value_t, vlc_value_t, void * );
62 static int VideoFilter2Callback( vlc_object_t *, char const *,
63                                  vlc_value_t, vlc_value_t, void * );
64
65 /* */
66 static void PostProcessEnable( vout_thread_t * );
67 static void PostProcessDisable( vout_thread_t * );
68 static int  PostProcessCallback( vlc_object_t *, char const *,
69                                  vlc_value_t, vlc_value_t, void * );
70 /* */
71 static void DeinterlaceEnable( vout_thread_t *, bool is_interlaced );
72 static void DeinterlaceNeeded( vout_thread_t *, bool );
73
74 /* Display media title in OSD */
75 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
76
77 /* */
78 static void PrintVideoFormat(vout_thread_t *, const char *, const video_format_t *);
79
80 /* Maximum delay between 2 displayed pictures.
81  * XXX it is needed for now but should be removed in the long term.
82  */
83 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
84
85 /**
86  * Late pictures having a delay higher than this value are thrashed.
87  */
88 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
89
90 /* Better be in advance when awakening than late... */
91 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
92
93 /*****************************************************************************
94  * Video Filter2 functions
95  *****************************************************************************/
96 static picture_t *video_new_buffer_filter( filter_t *p_filter )
97 {
98     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
99     return picture_pool_Get(p_vout->p->private_pool);
100 }
101
102 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
103 {
104     VLC_UNUSED(p_filter);
105     picture_Release(p_pic);
106 }
107
108 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
109 {
110     p_filter->pf_video_buffer_new = video_new_buffer_filter;
111     p_filter->pf_video_buffer_del = video_del_buffer_filter;
112     p_filter->p_owner = p_data; /* p_vout */
113     return VLC_SUCCESS;
114 }
115
116 #undef vout_Request
117 /*****************************************************************************
118  * vout_Request: find a video output thread, create one, or destroy one.
119  *****************************************************************************
120  * This function looks for a video output thread matching the current
121  * properties. If not found, it spawns a new one.
122  *****************************************************************************/
123 vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
124                              video_format_t *p_fmt )
125 {
126     if( !p_fmt )
127     {
128         /* Video output is no longer used.
129          * TODO: support for reusing video outputs with proper _thread-safe_
130          * reference handling. */
131         if( p_vout )
132             vout_CloseAndRelease( p_vout );
133         return NULL;
134     }
135
136     /* If a video output was provided, lock it, otherwise look for one. */
137     if( p_vout )
138     {
139         vlc_object_hold( p_vout );
140     }
141
142     /* TODO: find a suitable unused video output */
143
144     /* If we now have a video output, check it has the right properties */
145     if( p_vout )
146     {
147         vlc_mutex_lock( &p_vout->p->change_lock );
148
149         /* We don't directly check for the "vout-filter" variable for obvious
150          * performance reasons. */
151         if( p_vout->p->b_filter_change )
152         {
153             char *psz_filter_chain = var_GetString( p_vout, "vout-filter" );
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->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
161             {
162                 free( p_vout->p->psz_filter_chain );
163                 p_vout->p->psz_filter_chain = NULL;
164             }
165
166             if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
167             {
168                 p_vout->p->b_filter_change = false;
169             }
170
171             free( psz_filter_chain );
172         }
173
174 #warning "FIXME: Check RGB masks in vout_Request"
175         /* FIXME: check RGB masks */
176         if( p_vout->fmt_render.i_chroma != vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma ) ||
177             p_vout->fmt_render.i_width != p_fmt->i_width ||
178             p_vout->fmt_render.i_height != p_fmt->i_height ||
179             p_vout->p->b_filter_change )
180         {
181             vlc_mutex_unlock( &p_vout->p->change_lock );
182
183             /* We are not interested in this format, close this vout */
184             vout_CloseAndRelease( p_vout );
185             vlc_object_release( p_vout );
186             p_vout = NULL;
187         }
188         else
189         {
190             /* This video output is cool! Hijack it. */
191             /* Correct aspect ratio on change
192              * FIXME factorize this code with other aspect ration related code */
193             unsigned int i_sar_num;
194             unsigned int i_sar_den;
195             vlc_ureduce( &i_sar_num, &i_sar_den,
196                          p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
197 #if 0
198             /* What's that, it does not seems to be used correcly everywhere */
199             if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
200             {
201                 i_sar_num *= p_vout->i_par_den;
202                 i_sar_den *= p_vout->i_par_num;
203             }
204 #endif
205
206             if( i_sar_num > 0 && i_sar_den > 0 &&
207                 ( i_sar_num != p_vout->fmt_render.i_sar_num ||
208                   i_sar_den != p_vout->fmt_render.i_sar_den ) )
209             {
210                 p_vout->fmt_in.i_sar_num = i_sar_num;
211                 p_vout->fmt_in.i_sar_den = i_sar_den;
212
213                 p_vout->fmt_render.i_sar_num = i_sar_num;
214                 p_vout->fmt_render.i_sar_den = i_sar_den;
215                 p_vout->p->i_changes |= VOUT_ASPECT_CHANGE;
216             }
217             vlc_mutex_unlock( &p_vout->p->change_lock );
218
219             vlc_object_release( p_vout );
220         }
221
222         if( p_vout )
223         {
224             msg_Dbg( p_this, "reusing provided vout" );
225
226             spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
227             vlc_object_detach( p_vout );
228
229             vlc_object_attach( p_vout, p_this );
230             spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
231         }
232     }
233
234     if( !p_vout )
235     {
236         msg_Dbg( p_this, "no usable vout present, spawning one" );
237
238         p_vout = vout_Create( p_this, p_fmt );
239     }
240
241     return p_vout;
242 }
243
244 /*****************************************************************************
245  * vout_Create: creates a new video output thread
246  *****************************************************************************
247  * This function creates a new video output thread, and returns a pointer
248  * to its description. On error, it returns NULL.
249  *****************************************************************************/
250 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
251 {
252     vout_thread_t  *p_vout;                            /* thread descriptor */
253     vlc_value_t     text;
254
255
256     config_chain_t *p_cfg;
257     char *psz_parser;
258     char *psz_name;
259
260     if( p_fmt->i_width <= 0 || p_fmt->i_height <= 0 )
261         return NULL;
262     const vlc_fourcc_t i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma );
263
264     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
265                  p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
266     if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
267         return NULL;
268
269     /* Allocate descriptor */
270     static const char typename[] = "video output";
271     p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
272                                 typename );
273     if( p_vout == NULL )
274         return NULL;
275
276     /* */
277     p_vout->p = calloc( 1, sizeof(*p_vout->p) );
278     if( !p_vout->p )
279     {
280         vlc_object_release( p_vout );
281         return NULL;
282     }
283
284     /* */
285     p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
286     p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
287
288     p_vout->fmt_render.i_chroma = 
289     p_vout->fmt_in.i_chroma     = i_chroma;
290     video_format_FixRgb( &p_vout->fmt_render );
291     video_format_FixRgb( &p_vout->fmt_in );
292
293     /* Initialize misc stuff */
294     p_vout->p->i_changes    = 0;
295     p_vout->p->b_fullscreen = 0;
296     vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
297     vout_statistic_Init( &p_vout->p->statistic );
298     p_vout->p->b_filter_change = 0;
299     p_vout->p->i_par_num =
300     p_vout->p->i_par_den = 1;
301     p_vout->p->b_picture_empty = false;
302     p_vout->p->displayed.date = VLC_TS_INVALID;
303     p_vout->p->displayed.decoded = NULL;
304     p_vout->p->displayed.timestamp = VLC_TS_INVALID;
305     p_vout->p->displayed.qtype = QTYPE_NONE;
306     p_vout->p->displayed.is_interlaced = false;
307
308     p_vout->p->step.is_requested = false;
309     p_vout->p->step.last         = VLC_TS_INVALID;
310     p_vout->p->step.timestamp    = VLC_TS_INVALID;
311
312     p_vout->p->pause.is_on  = false;
313     p_vout->p->pause.date   = VLC_TS_INVALID;
314
315     p_vout->p->decoder_fifo = picture_fifo_New();
316     p_vout->p->decoder_pool = NULL;
317
318     vlc_mouse_Init( &p_vout->p->mouse );
319
320     vout_snapshot_Init( &p_vout->p->snapshot );
321
322     /* Initialize locks */
323     vlc_mutex_init( &p_vout->p->picture_lock );
324     vlc_cond_init( &p_vout->p->picture_wait );
325     vlc_mutex_init( &p_vout->p->change_lock );
326     vlc_mutex_init( &p_vout->p->vfilter_lock );
327
328     /* Mouse coordinates */
329     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
330     var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
331     var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
332     /* Mouse object (area of interest in a video filter) */
333     var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
334
335     /* Attach the new object now so we can use var inheritance below */
336     vlc_object_attach( p_vout, p_parent );
337
338     /* Initialize subpicture unit */
339     p_vout->p->p_spu = spu_Create( p_vout );
340
341     /* */
342     spu_Init( p_vout->p->p_spu );
343
344     spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
345
346     p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
347
348     /* Take care of some "interface/control" related initialisations */
349     vout_IntfInit( p_vout );
350
351     /* Look for the default filter configuration */
352     p_vout->p->psz_filter_chain =
353         var_CreateGetStringCommand( p_vout, "vout-filter" );
354
355     /* Apply video filter2 objects on the first vout */
356     p_vout->p->psz_vf2 =
357         var_CreateGetStringCommand( p_vout, "video-filter" );
358
359     var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
360     p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
361         false, video_filter_buffer_allocation_init, NULL, p_vout );
362
363     /* Choose the video output module */
364     if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
365     {
366         psz_parser = NULL;
367     }
368     else
369     {
370         psz_parser = strdup( p_vout->p->psz_filter_chain );
371         p_vout->p->title.show = false;
372     }
373
374     /* Create the vout thread */
375     char *psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
376     free( psz_parser );
377     free( psz_tmp );
378     p_vout->p->p_cfg = p_cfg;
379
380     /* Create a few object variables for interface interaction */
381     var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
382     text.psz_string = _("Filters");
383     var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
384     var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
385
386     /* */
387     DeinterlaceEnable( p_vout, p_vout->p->displayed.is_interlaced );
388
389     if( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain )
390     {
391         char *psz_tmp;
392         if( asprintf( &psz_tmp, "%s,none", psz_name ) < 0 )
393             psz_tmp = strdup( "" );
394         free( psz_name );
395         psz_name = psz_tmp;
396     }
397     p_vout->p->psz_module_name = psz_name;
398
399     /* */
400     vlc_object_set_destructor( p_vout, vout_Destructor );
401
402     /* */
403     vlc_cond_init( &p_vout->p->change_wait );
404     if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
405                    VLC_THREAD_PRIORITY_OUTPUT ) )
406     {
407         spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
408         spu_Destroy( p_vout->p->p_spu );
409         p_vout->p->p_spu = NULL;
410         vlc_object_release( p_vout );
411         return NULL;
412     }
413
414     vlc_mutex_lock( &p_vout->p->change_lock );
415     while( !p_vout->p->b_ready )
416     {   /* We are (ab)using the same condition in opposite directions for
417          * b_ready and b_done. This works because of the strict ordering. */
418         assert( !p_vout->p->b_done );
419         vlc_cond_wait( &p_vout->p->change_wait, &p_vout->p->change_lock );
420     }
421     vlc_mutex_unlock( &p_vout->p->change_lock );
422
423     if( p_vout->p->b_error )
424     {
425         msg_Err( p_vout, "video output creation failed" );
426         vout_CloseAndRelease( p_vout );
427         return NULL;
428     }
429
430     return p_vout;
431 }
432
433 /*****************************************************************************
434  * vout_Close: Close a vout created by vout_Create.
435  *****************************************************************************
436  * You HAVE to call it on vout created by vout_Create before vlc_object_release.
437  * You should NEVER call it on vout not obtained through vout_Create
438  * (like with vout_Request or vlc_object_find.)
439  * You can use vout_CloseAndRelease() as a convenience method.
440  *****************************************************************************/
441 void vout_Close( vout_thread_t *p_vout )
442 {
443     assert( p_vout );
444
445     vlc_mutex_lock( &p_vout->p->change_lock );
446     p_vout->p->b_done = true;
447     vlc_cond_signal( &p_vout->p->change_wait );
448     vlc_mutex_unlock( &p_vout->p->change_lock );
449
450     vout_snapshot_End( &p_vout->p->snapshot );
451
452     vlc_join( p_vout->p->thread, NULL );
453 }
454
455 /* */
456 static void vout_Destructor( vlc_object_t * p_this )
457 {
458     vout_thread_t *p_vout = (vout_thread_t *)p_this;
459
460     /* Make sure the vout was stopped first */
461     //assert( !p_vout->p_module );
462
463     free( p_vout->p->psz_module_name );
464
465     /* */
466     if( p_vout->p->p_spu )
467         spu_Destroy( p_vout->p->p_spu );
468
469     vout_chrono_Clean( &p_vout->p->render );
470
471     if( p_vout->p->decoder_fifo )
472         picture_fifo_Delete( p_vout->p->decoder_fifo );
473     assert( !p_vout->p->decoder_pool );
474
475     /* Destroy the locks */
476     vlc_cond_destroy( &p_vout->p->change_wait );
477     vlc_cond_destroy( &p_vout->p->picture_wait );
478     vlc_mutex_destroy( &p_vout->p->picture_lock );
479     vlc_mutex_destroy( &p_vout->p->change_lock );
480     vlc_mutex_destroy( &p_vout->p->vfilter_lock );
481
482     /* */
483     vout_statistic_Clean( &p_vout->p->statistic );
484
485     /* */
486     vout_snapshot_Clean( &p_vout->p->snapshot );
487
488     /* */
489     free( p_vout->p->psz_filter_chain );
490     free( p_vout->p->title.value );
491
492     config_ChainDestroy( p_vout->p->p_cfg );
493
494     free( p_vout->p );
495
496 }
497
498 /* */
499 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
500 {
501     vlc_mutex_lock( &p_vout->p->change_lock );
502
503     assert( !p_vout->p->pause.is_on || !b_paused );
504
505     vlc_mutex_lock( &p_vout->p->picture_lock );
506
507     if( p_vout->p->pause.is_on )
508     {
509         const mtime_t i_duration = i_date - p_vout->p->pause.date;
510
511         if (p_vout->p->step.timestamp > VLC_TS_INVALID)
512             p_vout->p->step.timestamp += i_duration;
513         if (!b_paused)
514             p_vout->p->step.last = p_vout->p->step.timestamp;
515         picture_fifo_OffsetDate( p_vout->p->decoder_fifo, i_duration );
516         if (p_vout->p->displayed.decoded)
517             p_vout->p->displayed.decoded->date += i_duration;
518
519         vlc_cond_signal( &p_vout->p->picture_wait );
520         vlc_mutex_unlock( &p_vout->p->picture_lock );
521
522         spu_OffsetSubtitleDate( p_vout->p->p_spu, i_duration );
523     }
524     else
525     {
526         if (b_paused)
527             p_vout->p->step.last = p_vout->p->step.timestamp;
528         vlc_mutex_unlock( &p_vout->p->picture_lock );
529     }
530     p_vout->p->pause.is_on = b_paused;
531     p_vout->p->pause.date  = i_date;
532
533     vlc_mutex_unlock( &p_vout->p->change_lock );
534 }
535
536 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
537 {
538     vout_statistic_GetReset( &p_vout->p->statistic,
539                              pi_displayed, pi_lost );
540 }
541
542 static void Flush(vout_thread_t *vout, mtime_t date, bool reset, bool below)
543 {
544     vlc_assert_locked(&vout->p->picture_lock);
545     vout->p->step.timestamp = VLC_TS_INVALID;
546     vout->p->step.last      = VLC_TS_INVALID;
547
548     picture_t *last = vout->p->displayed.decoded;
549     if (last) {
550         if (reset) {
551             picture_Release(last);
552             vout->p->displayed.decoded = NULL;
553         } else if (( below  && last->date <= date) ||
554                    (!below && last->date >= date)) {
555             vout->p->step.is_requested = true;
556         }
557     }
558     picture_fifo_Flush( vout->p->decoder_fifo, date, below );
559 }
560
561 void vout_Flush(vout_thread_t *vout, mtime_t date)
562 {
563     vlc_mutex_lock(&vout->p->picture_lock);
564
565     Flush(vout, date, false, false);
566
567     vlc_cond_signal(&vout->p->picture_wait);
568     vlc_mutex_unlock(&vout->p->picture_lock);
569 }
570
571 void vout_Reset(vout_thread_t *vout)
572 {
573     vlc_mutex_lock(&vout->p->picture_lock);
574
575     Flush(vout, INT64_MAX, true, true);
576     if (vout->p->decoder_pool)
577         picture_pool_NonEmpty(vout->p->decoder_pool, true);
578     vout->p->pause.is_on = false;
579     vout->p->pause.date  = mdate();
580
581     vlc_cond_signal( &vout->p->picture_wait );
582     vlc_mutex_unlock(&vout->p->picture_lock);
583 }
584
585 void vout_FixLeaks( vout_thread_t *vout )
586 {
587     vlc_mutex_lock(&vout->p->picture_lock);
588
589     picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
590     if (!picture) {
591         picture = picture_pool_Get(vout->p->decoder_pool);
592     }
593
594     if (picture) {
595         picture_Release(picture);
596         /* Not all pictures has been displayed yet or some are
597          * free */
598         vlc_mutex_unlock(&vout->p->picture_lock);
599         return;
600     }
601
602     /* There is no reason that no pictures are available, force one
603      * from the pool, becarefull with it though */
604     msg_Err(vout, "pictures leaked, trying to workaround");
605
606     /* */
607     picture_pool_NonEmpty(vout->p->decoder_pool, false);
608
609     vlc_cond_signal(&vout->p->picture_wait);
610     vlc_mutex_unlock(&vout->p->picture_lock);
611 }
612 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
613 {
614     vlc_mutex_lock(&vout->p->picture_lock);
615
616     vout->p->b_picture_empty = false;
617     vout->p->step.is_requested = true;
618
619     /* FIXME I highly doubt that it can works with only one cond_t FIXME */
620     vlc_cond_signal(&vout->p->picture_wait);
621
622     while (vout->p->step.is_requested && !vout->p->b_picture_empty)
623         vlc_cond_wait(&vout->p->picture_wait, &vout->p->picture_lock);
624
625     if (vout->p->step.last > VLC_TS_INVALID &&
626         vout->p->step.timestamp > vout->p->step.last) {
627         *duration = vout->p->step.timestamp - vout->p->step.last;
628         vout->p->step.last = vout->p->step.timestamp;
629     } else {
630         *duration = 0;
631     }
632
633     /* TODO advance subpicture by the duration ... */
634     vlc_mutex_unlock(&vout->p->picture_lock);
635 }
636
637 void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
638 {
639     assert( psz_title );
640
641     if( !var_InheritBool( p_vout, "osd" ) )
642         return;
643
644     vlc_mutex_lock( &p_vout->p->change_lock );
645     free( p_vout->p->title.value );
646     p_vout->p->title.value = strdup( psz_title );
647     vlc_mutex_unlock( &p_vout->p->change_lock );
648 }
649
650 spu_t *vout_GetSpu( vout_thread_t *p_vout )
651 {
652     return p_vout->p->p_spu;
653 }
654
655 /*****************************************************************************
656  * InitThread: initialize video output thread
657  *****************************************************************************
658  * This function is called from Thread and performs the second step of the
659  * initialization. It returns 0 on success. Note that the thread's flag are not
660  * modified inside this function.
661  * XXX You have to enter it with change_lock taken.
662  *****************************************************************************/
663 static int ThreadInit(vout_thread_t *vout)
664 {
665     /* Initialize output method, it allocates direct buffers for us */
666     if (vout_InitWrapper(vout))
667         return VLC_EGENERIC;
668     assert(vout->p->decoder_pool);
669
670     vout->p->displayed.decoded = NULL;
671
672     /* print some usefull debug info about different vout formats
673      */
674     PrintVideoFormat(vout, "pic render", &vout->fmt_render);
675     PrintVideoFormat(vout, "pic in",     &vout->fmt_in);
676     PrintVideoFormat(vout, "pic out",    &vout->fmt_out);
677
678     assert(vout->fmt_out.i_width  == vout->fmt_render.i_width &&
679            vout->fmt_out.i_height == vout->fmt_render.i_height &&
680            vout->fmt_out.i_chroma == vout->fmt_render.i_chroma);
681     return VLC_SUCCESS;
682 }
683
684 /*****************************************************************************
685  * CleanThread: clean up after InitThread
686  *****************************************************************************
687  * This function is called after a sucessful
688  * initialization. It frees all resources allocated by InitThread.
689  * XXX You have to enter it with change_lock taken.
690  *****************************************************************************/
691 static void ThreadClean(vout_thread_t *vout)
692 {
693     /* Destroy translation tables */
694     if (!vout->p->b_error) {
695         picture_fifo_Flush(vout->p->decoder_fifo, INT64_MAX, false);
696         vout_EndWrapper(vout);
697     }
698 }
699
700 static int ThreadDisplayPicture(vout_thread_t *vout,
701                                 bool now, mtime_t *deadline)
702 {
703     int displayed_count = 0;
704     int lost_count = 0;
705
706     for (;;) {
707         const mtime_t date = mdate();
708         const bool is_paused = vout->p->pause.is_on;
709         bool redisplay = is_paused && !now;
710         bool is_forced;
711
712         /* FIXME/XXX we must redisplay the last decoded picture (because
713          * of potential vout updated, or filters update or SPU update)
714          * For now a high update period is needed but it coulmd be removed
715          * if and only if:
716          * - vout module emits events from theselves.
717          * - *and* SPU is modified to emit an event or a deadline when needed.
718          *
719          * So it will be done latter.
720          */
721         if (!redisplay) {
722             picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
723             if (peek) {
724                 is_forced = peek->b_force || is_paused || now;
725                 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
726                 picture_Release(peek);
727             } else {
728                 redisplay = true;
729             }
730         }
731         if (redisplay) {
732              /* FIXME a better way for this delay is needed */
733             const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
734             if (date_update > date || !vout->p->displayed.decoded) {
735                 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
736                 break;
737             }
738             /* */
739             is_forced = true;
740             *deadline = date - vout_chrono_GetHigh(&vout->p->render);
741         }
742         if (*deadline > VOUT_MWAIT_TOLERANCE)
743             *deadline -= VOUT_MWAIT_TOLERANCE;
744
745         /* If we are too early and can wait, do it */
746         if (date < *deadline && !now)
747             break;
748
749         picture_t *decoded;
750         if (redisplay) {
751             decoded = vout->p->displayed.decoded;
752             vout->p->displayed.decoded = NULL;
753         } else {
754             decoded = picture_fifo_Pop(vout->p->decoder_fifo);
755             assert(decoded);
756             if (!is_forced && !vout->p->is_late_dropped) {
757                 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
758                 const mtime_t late = predicted - decoded->date;
759                 if (late > 0) {
760                     msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
761                     if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
762                         msg_Warn(vout, "rejected picture because of render time");
763                         /* TODO */
764                         picture_Release(decoded);
765                         lost_count++;
766                         break;
767                     }
768                 }
769             }
770
771             vout->p->displayed.is_interlaced = !decoded->b_progressive;
772             vout->p->displayed.qtype         = decoded->i_qtype;
773         }
774         vout->p->displayed.timestamp = decoded->date;
775
776         /* */
777         if (vout->p->displayed.decoded)
778             picture_Release(vout->p->displayed.decoded);
779         picture_Hold(decoded);
780         vout->p->displayed.decoded = decoded;
781
782         /* */
783         vout_chrono_Start(&vout->p->render);
784
785         picture_t *filtered = NULL;
786         if (decoded) {
787             vlc_mutex_lock(&vout->p->vfilter_lock);
788             filtered = filter_chain_VideoFilter(vout->p->p_vf2_chain, decoded);
789             //assert(filtered == decoded); // TODO implement
790             vlc_mutex_unlock(&vout->p->vfilter_lock);
791             if (!filtered)
792                 continue;
793         }
794
795         /*
796          * Check for subpictures to display
797          */
798         const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
799         mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
800         if (vout->p->pause.is_on)
801             spu_render_time = vout->p->pause.date;
802         else
803             spu_render_time = filtered->date > 1 ? filtered->date : mdate();
804
805         subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
806                                                    spu_render_time,
807                                                    do_snapshot);
808         /*
809          * Perform rendering
810          *
811          * We have to:
812          * - be sure to end up with a direct buffer.
813          * - blend subtitles, and in a fast access buffer
814          */
815         picture_t *direct = NULL;
816         if (filtered &&
817             (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
818             picture_t *render;
819             if (vout->p->is_decoder_pool_slow)
820                 render = picture_NewFromFormat(&vout->fmt_out);
821             else if (vout->p->decoder_pool != vout->p->display_pool)
822                 render = picture_pool_Get(vout->p->display_pool);
823             else
824                 render = picture_pool_Get(vout->p->private_pool);
825
826             if (render) {
827                 picture_Copy(render, filtered);
828
829                 spu_RenderSubpictures(vout->p->p_spu,
830                                       render, &vout->fmt_out,
831                                       subpic, &vout->fmt_in, spu_render_time);
832             }
833             if (vout->p->is_decoder_pool_slow) {
834                 direct = picture_pool_Get(vout->p->display_pool);
835                 if (direct)
836                     picture_Copy(direct, render);
837                 picture_Release(render);
838
839             } else {
840                 direct = render;
841             }
842             picture_Release(filtered);
843             filtered = NULL;
844         } else {
845             direct = filtered;
846         }
847
848         /*
849          * Take a snapshot if requested
850          */
851         if (direct && do_snapshot)
852             vout_snapshot_Set(&vout->p->snapshot, &vout->fmt_out, direct);
853
854         /* Render the direct buffer returned by vout_RenderPicture */
855         if (direct) {
856             vout_RenderWrapper(vout, direct);
857
858             vout_chrono_Stop(&vout->p->render);
859 #if 0
860             {
861             static int i = 0;
862             if (((i++)%10) == 0)
863                 msg_Info(vout, "render: avg %d ms var %d ms",
864                          (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
865             }
866 #endif
867         }
868
869         /* Wait the real date (for rendering jitter) */
870         if (!is_forced)
871             mwait(decoded->date);
872
873         /* Display the direct buffer returned by vout_RenderPicture */
874         vout->p->displayed.date = mdate();
875         if (direct)
876             vout_DisplayWrapper(vout, direct);
877
878         displayed_count++;
879         break;
880     }
881
882     vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
883     if (displayed_count <= 0)
884         return VLC_EGENERIC;
885     return VLC_SUCCESS;
886 }
887
888 /*****************************************************************************
889  * Thread: video output thread
890  *****************************************************************************
891  * Video output thread. This function does only returns when the thread is
892  * terminated. It handles the pictures arriving in the video heap and the
893  * display device events.
894  *****************************************************************************/
895 static void *Thread(void *object)
896 {
897     vout_thread_t *vout = object;
898     bool          has_wrapper;
899
900     /*
901      * Initialize thread
902      */
903     has_wrapper = !vout_OpenWrapper(vout, vout->p->psz_module_name);
904
905     vlc_mutex_lock(&vout->p->change_lock);
906
907     if (has_wrapper)
908         vout->p->b_error = ThreadInit(vout);
909     else
910         vout->p->b_error = true;
911
912     /* signal the creation of the vout */
913     vout->p->b_ready = true;
914     vlc_cond_signal(&vout->p->change_wait);
915
916     if (vout->p->b_error)
917         goto exit_thread;
918
919     /* */
920     bool    last_picture_interlaced      = false;
921     int     last_picture_qtype           = QTYPE_NONE;
922     mtime_t last_picture_interlaced_date = mdate();
923
924     /*
925      * Main loop - it is not executed if an error occurred during
926      * initialization
927      */
928     while (!vout->p->b_done && !vout->p->b_error) {
929         /* */
930         if(vout->p->title.show && vout->p->title.value)
931             DisplayTitleOnOSD(vout);
932
933         vlc_mutex_lock(&vout->p->picture_lock);
934
935         mtime_t deadline = VLC_TS_INVALID;
936         bool has_displayed = !ThreadDisplayPicture(vout, vout->p->step.is_requested, &deadline);
937
938         if (has_displayed) {
939             vout->p->step.timestamp = vout->p->displayed.timestamp;
940             if (vout->p->step.last <= VLC_TS_INVALID)
941                 vout->p->step.last = vout->p->step.timestamp;
942         }
943         if (vout->p->step.is_requested) {
944             if (!has_displayed && !vout->p->b_picture_empty) {
945                 picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
946                 if (peek)
947                     picture_Release(peek);
948                 if (!peek) {
949                     vout->p->b_picture_empty = true;
950                     vlc_cond_signal(&vout->p->picture_wait);
951                 }
952             }
953             if (has_displayed) {
954                 vout->p->step.is_requested = false;
955                 vlc_cond_signal(&vout->p->picture_wait);
956             }
957         }
958
959         const int  picture_qtype      = vout->p->displayed.qtype;
960         const bool picture_interlaced = vout->p->displayed.is_interlaced;
961
962         vlc_mutex_unlock(&vout->p->picture_lock);
963
964         if (vout_ManageWrapper(vout)) {
965             /* A fatal error occurred, and the thread must terminate
966              * immediately, without displaying anything - setting b_error to 1
967              * causes the immediate end of the main while() loop. */
968             // FIXME pf_end
969             vout->p->b_error = true;
970             break;
971         }
972
973         /* Post processing */
974         const int postproc_change = (picture_qtype != QTYPE_NONE) - (last_picture_qtype != QTYPE_NONE);
975         if (postproc_change == 1)
976             PostProcessEnable(vout);
977         else if (postproc_change == -1)
978             PostProcessDisable(vout);
979         if (postproc_change)
980             last_picture_qtype = picture_qtype;
981
982         /* Deinterlacing
983          * Wait 30s before quiting interlacing mode */
984         const int interlacing_change = (!!picture_interlaced) - (!!last_picture_interlaced);
985         if ((interlacing_change == 1) ||
986             (interlacing_change == -1 && last_picture_interlaced_date + 30000000 < mdate())) {
987             DeinterlaceNeeded(vout, picture_interlaced);
988             last_picture_interlaced = picture_interlaced;
989         }
990         if (picture_interlaced)
991             last_picture_interlaced_date = mdate();
992
993         /* Check for "video filter2" changes */
994         vlc_mutex_lock(&vout->p->vfilter_lock);
995         if (vout->p->psz_vf2) {
996             es_format_t fmt;
997
998             es_format_Init(&fmt, VIDEO_ES, vout->fmt_render.i_chroma);
999             fmt.video = vout->fmt_render;
1000             filter_chain_Reset(vout->p->p_vf2_chain, &fmt, &fmt);
1001
1002             if (filter_chain_AppendFromString(vout->p->p_vf2_chain,
1003                                               vout->p->psz_vf2) < 0)
1004                 msg_Err(vout, "Video filter chain creation failed");
1005
1006             free(vout->p->psz_vf2);
1007             vout->p->psz_vf2 = NULL;
1008         }
1009         vlc_mutex_unlock(&vout->p->vfilter_lock);
1010
1011         vlc_mutex_unlock(&vout->p->change_lock);
1012
1013         if (deadline > VLC_TS_INVALID) {
1014             vlc_mutex_lock(&vout->p->picture_lock);
1015             vlc_cond_timedwait(&vout->p->picture_wait, &vout->p->picture_lock, deadline);
1016             vlc_mutex_unlock(&vout->p->picture_lock);
1017         }
1018
1019         vlc_mutex_lock(&vout->p->change_lock);
1020     }
1021
1022     /*
1023      * Error loop - wait until the thread destruction is requested
1024      *
1025      * XXX I wonder if we should periodically clean the decoder_fifo
1026      * or have a way to prevent it filling up.
1027      */
1028     while (vout->p->b_error && !vout->p->b_done)
1029         vlc_cond_wait(&vout->p->change_wait, &vout->p->change_lock);
1030
1031     /* Clean thread */
1032     ThreadClean(vout);
1033
1034 exit_thread:
1035     /* Detach subpicture unit from both input and vout */
1036     spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
1037     vlc_object_detach(vout->p->p_spu);
1038
1039     /* Destroy the video filters2 */
1040     filter_chain_Delete(vout->p->p_vf2_chain);
1041
1042     vlc_mutex_unlock(&vout->p->change_lock);
1043
1044     if (has_wrapper)
1045         vout_CloseWrapper(vout);
1046
1047     return NULL;
1048 }
1049
1050 /*****************************************************************************
1051  * object variables callbacks: a bunch of object variables are used by the
1052  * interfaces to interact with the vout.
1053  *****************************************************************************/
1054 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1055                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1056 {
1057     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1058     input_thread_t *p_input;
1059     (void)psz_cmd; (void)oldval; (void)p_data;
1060
1061     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1062                                                  FIND_PARENT );
1063     if (!p_input)
1064     {
1065         msg_Err( p_vout, "Input not found" );
1066         return VLC_EGENERIC;
1067     }
1068
1069     var_SetBool( p_vout, "intf-change", true );
1070
1071     /* Modify input as well because the vout might have to be restarted */
1072     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1073     var_SetString( p_input, "vout-filter", newval.psz_string );
1074
1075     /* Now restart current video stream */
1076     input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1077     vlc_object_release( p_input );
1078
1079     return VLC_SUCCESS;
1080 }
1081
1082 /*****************************************************************************
1083  * Video Filter2 stuff
1084  *****************************************************************************/
1085 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1086                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1087 {
1088     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1089     (void)psz_cmd; (void)oldval; (void)p_data;
1090
1091     vlc_mutex_lock( &p_vout->p->vfilter_lock );
1092     p_vout->p->psz_vf2 = strdup( newval.psz_string );
1093     vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1094
1095     return VLC_SUCCESS;
1096 }
1097
1098 /*****************************************************************************
1099  * Post-processing
1100  *****************************************************************************/
1101 static bool PostProcessIsPresent( const char *psz_filter )
1102 {
1103     const char  *psz_pp = "postproc";
1104     const size_t i_pp = strlen(psz_pp);
1105     return psz_filter &&
1106            !strncmp( psz_filter, psz_pp, strlen(psz_pp) ) &&
1107            ( psz_filter[i_pp] == '\0' || psz_filter[i_pp] == ':' );
1108 }
1109
1110 static int PostProcessCallback( vlc_object_t *p_this, char const *psz_cmd,
1111                                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1112 {
1113     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1114     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1115
1116     static const char *psz_pp = "postproc";
1117
1118     char *psz_vf2 = var_GetString( p_vout, "video-filter" );
1119
1120     if( newval.i_int <= 0 )
1121     {
1122         if( PostProcessIsPresent( psz_vf2 ) )
1123         {
1124             strcpy( psz_vf2, &psz_vf2[strlen(psz_pp)] );
1125             if( *psz_vf2 == ':' )
1126                 strcpy( psz_vf2, &psz_vf2[1] );
1127         }
1128     }
1129     else
1130     {
1131         if( !PostProcessIsPresent( psz_vf2 ) )
1132         {
1133             if( psz_vf2 )
1134             {
1135                 char *psz_tmp = psz_vf2;
1136                 if( asprintf( &psz_vf2, "%s:%s", psz_pp, psz_tmp ) < 0 )
1137                     psz_vf2 = psz_tmp;
1138                 else
1139                     free( psz_tmp );
1140             }
1141             else
1142             {
1143                 psz_vf2 = strdup( psz_pp );
1144             }
1145         }
1146     }
1147     if( newval.i_int > 0 )
1148         var_SetInteger( p_vout, "postproc-q", newval.i_int );
1149     if( psz_vf2 )
1150     {
1151         var_SetString( p_vout, "video-filter", psz_vf2 );
1152         free( psz_vf2 );
1153     }
1154     else if( newval.i_int > 0 )
1155     {
1156         var_TriggerCallback( p_vout, "video-filter" );
1157     }
1158     return VLC_SUCCESS;
1159 }
1160 static void PostProcessEnable( vout_thread_t *p_vout )
1161 {
1162     vlc_value_t text;
1163     msg_Dbg( p_vout, "Post-processing available" );
1164     var_Create( p_vout, "postprocess", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
1165     text.psz_string = _("Post processing");
1166     var_Change( p_vout, "postprocess", VLC_VAR_SETTEXT, &text, NULL );
1167
1168     for( int i = 0; i <= 6; i++ )
1169     {
1170         vlc_value_t val;
1171         vlc_value_t text;
1172         char psz_text[1+1];
1173
1174         val.i_int = i;
1175         snprintf( psz_text, sizeof(psz_text), "%d", i );
1176         if( i == 0 )
1177             text.psz_string = _("Disable");
1178         else
1179             text.psz_string = psz_text;
1180         var_Change( p_vout, "postprocess", VLC_VAR_ADDCHOICE, &val, &text );
1181     }
1182     var_AddCallback( p_vout, "postprocess", PostProcessCallback, NULL );
1183
1184     /* */
1185     char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1186     int i_postproc_q = 0;
1187     if( PostProcessIsPresent( psz_filter ) )
1188         i_postproc_q = var_CreateGetInteger( p_vout, "postproc-q" );
1189
1190     var_SetInteger( p_vout, "postprocess", i_postproc_q );
1191
1192     free( psz_filter );
1193 }
1194 static void PostProcessDisable( vout_thread_t *p_vout )
1195 {
1196     msg_Dbg( p_vout, "Post-processing no more available" );
1197     var_Destroy( p_vout, "postprocess" );
1198 }
1199
1200 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1201 {
1202     const mtime_t i_start = mdate();
1203     const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->title.timeout;
1204
1205     if( i_stop <= i_start )
1206         return;
1207
1208     vlc_assert_locked( &p_vout->p->change_lock );
1209
1210     vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1211                            p_vout->p->title.value, NULL,
1212                            p_vout->p->title.position,
1213                            30 + p_vout->fmt_in.i_width
1214                               - p_vout->fmt_in.i_visible_width
1215                               - p_vout->fmt_in.i_x_offset,
1216                            20 + p_vout->fmt_in.i_y_offset,
1217                            i_start, i_stop );
1218
1219     free( p_vout->p->title.value );
1220
1221     p_vout->p->title.value = NULL;
1222 }
1223
1224 /*****************************************************************************
1225  * Deinterlacing
1226  *****************************************************************************/
1227 /* XXX
1228  * You can use the non vout filter if and only if the video properties stay the
1229  * same (width/height/chroma/fps), at least for now.
1230  */
1231 static const char *deinterlace_modes[] = {
1232     ""
1233     //"discard",
1234     "blend",
1235     //"mean",
1236     //"bob",
1237     //"linear",
1238     "x",
1239     //"yadif",
1240     //"yadif2x",
1241     NULL
1242 };
1243 static bool DeinterlaceIsModeValid(const char *mode)
1244 {
1245     for (unsigned i = 0; deinterlace_modes[i]; i++) {
1246         if( !strcmp(deinterlace_modes[i], mode))
1247             return true;
1248     }
1249     return false;
1250 }
1251
1252 static char *FilterFind( char *psz_filter_base, const char *psz_module )
1253 {
1254     const size_t i_module = strlen( psz_module );
1255     const char *psz_filter = psz_filter_base;
1256
1257     if( !psz_filter || i_module <= 0 )
1258         return NULL;
1259
1260     for( ;; )
1261     {
1262         char *psz_find = strstr( psz_filter, psz_module );
1263         if( !psz_find )
1264             return NULL;
1265         if( psz_find[i_module] == '\0' || psz_find[i_module] == ':' )
1266             return psz_find;
1267         psz_filter = &psz_find[i_module];
1268     }
1269 }
1270
1271 static bool DeinterlaceIsPresent( vout_thread_t *p_vout )
1272 {
1273     char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1274
1275     bool b_found = FilterFind( psz_filter, "deinterlace" ) != NULL;
1276
1277     free( psz_filter );
1278
1279     return b_found;
1280 }
1281
1282 static void DeinterlaceRemove( vout_thread_t *p_vout )
1283 {
1284     char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1285
1286     char *psz = FilterFind( psz_filter, "deinterlace" );
1287     if( !psz )
1288     {
1289         free( psz_filter );
1290         return;
1291     }
1292
1293     /* */
1294     strcpy( &psz[0], &psz[strlen("deinterlace")] );
1295     if( *psz == ':' )
1296         strcpy( &psz[0], &psz[1] );
1297
1298     var_SetString( p_vout, "video-filter", psz_filter );
1299     free( psz_filter );
1300 }
1301 static void DeinterlaceAdd( vout_thread_t *p_vout )
1302 {
1303     char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
1304
1305     if( FilterFind( psz_filter, "deinterlace" ) )
1306     {
1307         free( psz_filter );
1308         return;
1309     }
1310
1311     /* */
1312     if( psz_filter )
1313     {
1314         char *psz_tmp = psz_filter;
1315         if( asprintf( &psz_filter, "%s:%s", psz_tmp, "deinterlace" ) < 0 )
1316             psz_filter = psz_tmp;
1317         else
1318             free( psz_tmp );
1319     }
1320     else
1321     {
1322         psz_filter = strdup( "deinterlace" );
1323     }
1324
1325     if( psz_filter )
1326     {
1327         var_SetString( p_vout, "video-filter", psz_filter );
1328         free( psz_filter );
1329     }
1330 }
1331
1332 static void DeinterlaceSave( vout_thread_t *p_vout, int i_deinterlace, const char *psz_mode )
1333 {
1334     /* We have to set input variable to ensure restart support
1335      * FIXME to be removed when vout_Request does the right job.
1336      */
1337     vlc_object_t *p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
1338     if( !p_input )
1339         return;
1340
1341     var_Create( p_input, "deinterlace", VLC_VAR_INTEGER );
1342     var_SetInteger( p_input, "deinterlace", i_deinterlace );
1343
1344     static const char * const ppsz_variable[] = {
1345         "deinterlace-mode",
1346         "sout-deinterlace-mode",
1347         NULL
1348     };
1349     for( int i = 0; ppsz_variable[i]; i++ )
1350     {
1351         var_Create( p_input, ppsz_variable[i], VLC_VAR_STRING );
1352         var_SetString( p_input, ppsz_variable[i], psz_mode );
1353     }
1354
1355     vlc_object_release( p_input );
1356 }
1357 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1358                                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1359 {
1360     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1361     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1362
1363     /* */
1364     const int  i_deinterlace = var_GetInteger( p_this, "deinterlace" );
1365     char       *psz_mode     = var_GetString( p_this, "deinterlace-mode" );
1366     const bool is_needed     = var_GetBool( p_this, "deinterlace-needed" );
1367     if( !psz_mode || !DeinterlaceIsModeValid(psz_mode) )
1368         return VLC_EGENERIC;
1369
1370     DeinterlaceSave( p_vout, i_deinterlace, psz_mode );
1371
1372     /* */
1373     char *psz_old = var_CreateGetString( p_vout, "sout-deinterlace-mode" );
1374
1375     msg_Dbg( p_vout, "deinterlace %d, mode %s, is_needed %d", i_deinterlace, psz_mode, is_needed );
1376     if( i_deinterlace == 0 || ( i_deinterlace == -1 && !is_needed ) )
1377     {
1378         DeinterlaceRemove( p_vout );
1379     }
1380     else if( !DeinterlaceIsPresent( p_vout ) )
1381     {
1382         DeinterlaceAdd( p_vout );
1383     }
1384     else if( psz_old && strcmp( psz_old, psz_mode ) )
1385     {
1386         var_TriggerCallback( p_vout, "video-filter" );
1387     }
1388
1389     /* */
1390     free( psz_old );
1391     free( psz_mode );
1392     return VLC_SUCCESS;
1393 }
1394
1395 static void DeinterlaceEnable( vout_thread_t *p_vout, bool is_interlaced )
1396 {
1397     vlc_value_t val, text;
1398
1399     msg_Dbg( p_vout, "Deinterlacing available" );
1400
1401     /* Create the configuration variables */
1402     /* */
1403     var_Create( p_vout, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_HASCHOICE );
1404     int i_deinterlace = var_GetInteger( p_vout, "deinterlace" );
1405
1406     text.psz_string = _("Deinterlace");
1407     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
1408
1409     const module_config_t *p_optd = config_FindConfig( VLC_OBJECT(p_vout), "deinterlace" );
1410     var_Change( p_vout, "deinterlace", VLC_VAR_CLEARCHOICES, NULL, NULL );
1411     for( int i = 0; p_optd && i < p_optd->i_list; i++ )
1412     {
1413         val.i_int  = p_optd->pi_list[i];
1414         text.psz_string = (char*)vlc_gettext(p_optd->ppsz_list_text[i]);
1415         var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
1416     }
1417     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
1418     /* */
1419     var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_HASCHOICE );
1420     char *psz_deinterlace = var_GetNonEmptyString( p_vout, "deinterlace-mode" );
1421
1422     text.psz_string = _("Deinterlace mode");
1423     var_Change( p_vout, "deinterlace-mode", VLC_VAR_SETTEXT, &text, NULL );
1424
1425     const module_config_t *p_optm = config_FindConfig( VLC_OBJECT(p_vout), "deinterlace-mode" );
1426     var_Change( p_vout, "deinterlace-mode", VLC_VAR_CLEARCHOICES, NULL, NULL );
1427     for( int i = 0; p_optm && i < p_optm->i_list; i++ )
1428     {
1429         if( !DeinterlaceIsModeValid( p_optm->ppsz_list[i] ) )
1430             continue;
1431
1432         val.psz_string  = p_optm->ppsz_list[i];
1433         text.psz_string = (char*)vlc_gettext(p_optm->ppsz_list_text[i]);
1434         var_Change( p_vout, "deinterlace-mode", VLC_VAR_ADDCHOICE, &val, &text );
1435     }
1436     var_AddCallback( p_vout, "deinterlace-mode", DeinterlaceCallback, NULL );
1437     /* */
1438     var_Create( p_vout, "deinterlace-needed", VLC_VAR_BOOL );
1439     var_AddCallback( p_vout, "deinterlace-needed", DeinterlaceCallback, NULL );
1440
1441     /* Override the initial value from filters if present */
1442     char *psz_filter_mode = NULL;
1443     if( DeinterlaceIsPresent( p_vout ) )
1444         psz_filter_mode = var_CreateGetNonEmptyString( p_vout, "sout-deinterlace-mode" );
1445     if( psz_filter_mode )
1446     {
1447         free( psz_deinterlace );
1448         if( i_deinterlace >= -1 )
1449             i_deinterlace = 1;
1450         psz_deinterlace = psz_filter_mode;
1451     }
1452
1453     /* */
1454     if( i_deinterlace < 0 )
1455         i_deinterlace = -1;
1456
1457     /* */
1458     val.psz_string = psz_deinterlace ? psz_deinterlace : p_optm->orig.psz;
1459     var_Change( p_vout, "deinterlace-mode", VLC_VAR_SETVALUE, &val, NULL );
1460     val.b_bool = is_interlaced;
1461     var_Change( p_vout, "deinterlace-needed", VLC_VAR_SETVALUE, &val, NULL );
1462
1463     var_SetInteger( p_vout, "deinterlace", i_deinterlace );
1464     free( psz_deinterlace );
1465 }
1466
1467 static void DeinterlaceNeeded( vout_thread_t *p_vout, bool is_interlaced )
1468 {
1469     msg_Dbg( p_vout, "Detected %s video",
1470              is_interlaced ? "interlaced" : "progressive" );
1471     var_SetBool( p_vout, "deinterlace-needed", is_interlaced );
1472 }
1473
1474 /* */
1475 static void PrintVideoFormat(vout_thread_t *vout,
1476                              const char *description,
1477                              const video_format_t *fmt)
1478 {
1479     msg_Dbg(vout, "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
1480             description,
1481             fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
1482             fmt->i_visible_width, fmt->i_visible_height,
1483             (char*)&fmt->i_chroma,
1484             fmt->i_sar_num, fmt->i_sar_den,
1485             fmt->i_rmask, fmt->i_gmask, fmt->i_bmask);
1486 }
1487