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