]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
Store the last displayed picture pointer in vout private structure.
[vlc] / src / video_output / video_output.c
1 /*****************************************************************************
2  * video_output.c : video output thread
3  *
4  * This module describes the programming interface for video output threads.
5  * It includes functions allowing to open a new thread, send pictures to a
6  * thread, and destroy a previously oppened video output thread.
7  *****************************************************************************
8  * Copyright (C) 2000-2007 the VideoLAN team
9  * $Id$
10  *
11  * Authors: Vincent Seguin <seguin@via.ecp.fr>
12  *          Gildas Bazin <gbazin@videolan.org>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37
38 #include <stdlib.h>                                                /* free() */
39 #include <string.h>
40
41
42 #ifdef HAVE_SYS_TIMES_H
43 #   include <sys/times.h>
44 #endif
45
46 #include <vlc_vout.h>
47
48 #include <vlc_filter.h>
49 #include <vlc_osd.h>
50 #include <assert.h>
51
52 #if defined( __APPLE__ )
53 /* Include darwin_specific.h here if needed */
54 #endif
55
56 /** FIXME This is quite ugly but needed while we don't have counters
57  * helpers */
58 //#include "input/input_internal.h"
59
60 #include <libvlc.h>
61 #include <vlc_input.h>
62 #include "vout_pictures.h"
63 #include "vout_internal.h"
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int      InitThread        ( vout_thread_t * );
69 static void*    RunThread         ( vlc_object_t *  );
70 static void     ErrorThread       ( vout_thread_t * );
71 static void     CleanThread       ( vout_thread_t * );
72 static void     EndThread         ( vout_thread_t * );
73
74 static void     AspectRatio       ( int, int *, int * );
75
76 static void VideoFormatImportRgb( video_format_t *, const picture_heap_t * );
77 static void PictureHeapFixRgb( picture_heap_t * );
78
79 static void     vout_Destructor   ( vlc_object_t * p_this );
80
81 /* Object variables callbacks */
82 static int DeinterlaceCallback( vlc_object_t *, char const *,
83                                 vlc_value_t, vlc_value_t, void * );
84 static int FilterCallback( vlc_object_t *, char const *,
85                            vlc_value_t, vlc_value_t, void * );
86 static int VideoFilter2Callback( vlc_object_t *, char const *,
87                                  vlc_value_t, vlc_value_t, void * );
88
89 /* From vout_intf.c */
90 int vout_Snapshot( vout_thread_t *, picture_t * );
91
92 /* Display media title in OSD */
93 static void DisplayTitleOnOSD( vout_thread_t *p_vout );
94
95 /* */
96 static void DropPicture( vout_thread_t *p_vout, picture_t *p_picture );
97
98 /*****************************************************************************
99  * Video Filter2 functions
100  *****************************************************************************/
101 static picture_t *video_new_buffer_filter( filter_t *p_filter )
102 {
103     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
104     picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
105
106     p_picture->i_status = READY_PICTURE;
107
108     return p_picture;
109 }
110
111 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
112 {
113     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
114
115     DropPicture( p_vout, p_pic );
116 }
117
118 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
119 {
120     p_filter->pf_vout_buffer_new = video_new_buffer_filter;
121     p_filter->pf_vout_buffer_del = video_del_buffer_filter;
122     p_filter->p_owner = p_data; /* p_vout */
123     return VLC_SUCCESS;
124 }
125
126 /*****************************************************************************
127  * vout_Request: find a video output thread, create one, or destroy one.
128  *****************************************************************************
129  * This function looks for a video output thread matching the current
130  * properties. If not found, it spawns a new one.
131  *****************************************************************************/
132 vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
133                                video_format_t *p_fmt )
134 {
135     const bool b_vout_provided = p_vout != NULL;
136     if( !p_fmt )
137     {
138         /* Video output is no longer used.
139          * TODO: support for reusing video outputs with proper _thread-safe_
140          * reference handling. */
141         if( p_vout )
142             vout_CloseAndRelease( p_vout );
143         return NULL;
144     }
145
146     /* If a video output was provided, lock it, otherwise look for one. */
147     if( p_vout )
148     {
149         vlc_object_hold( p_vout );
150     }
151
152     /* TODO: find a suitable unused video output */
153
154     /* If we now have a video output, check it has the right properties */
155     if( p_vout )
156     {
157         char *psz_filter_chain;
158         vlc_value_t val;
159
160         vlc_mutex_lock( &p_vout->change_lock );
161
162         /* We don't directly check for the "vout-filter" variable for obvious
163          * performance reasons. */
164         if( p_vout->p->b_filter_change )
165         {
166             var_Get( p_vout, "vout-filter", &val );
167             psz_filter_chain = val.psz_string;
168
169             if( psz_filter_chain && !*psz_filter_chain )
170             {
171                 free( psz_filter_chain );
172                 psz_filter_chain = NULL;
173             }
174             if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
175             {
176                 free( p_vout->p->psz_filter_chain );
177                 p_vout->p->psz_filter_chain = NULL;
178             }
179
180             if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
181             {
182                 p_vout->p->b_filter_change = false;
183             }
184
185             free( psz_filter_chain );
186         }
187
188         if( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ||
189             p_vout->fmt_render.i_width != p_fmt->i_width ||
190             p_vout->fmt_render.i_height != p_fmt->i_height ||
191             p_vout->p->b_filter_change )
192         {
193             vlc_mutex_unlock( &p_vout->change_lock );
194
195             /* We are not interested in this format, close this vout */
196             vout_CloseAndRelease( p_vout );
197             vlc_object_release( p_vout );
198             p_vout = NULL;
199         }
200         else
201         {
202             /* This video output is cool! Hijack it. */
203             if( p_vout->fmt_render.i_aspect != p_fmt->i_aspect )
204             {
205                 /* Correct aspect ratio on change
206                  * FIXME factorize this code with other aspect ration related code */
207                 unsigned int i_sar_num;
208                 unsigned int i_sar_den;
209                 unsigned int i_aspect;
210
211                 i_aspect = p_fmt->i_aspect;
212                 vlc_ureduce( &i_sar_num, &i_sar_den,
213                              p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
214 #if 0
215                 /* What's that, it does not seems to be used correcly everywhere
216                  * beside the previous p_vout->fmt_render.i_aspect != p_fmt->i_aspect
217                  * should be fixed to use it too then */
218                 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
219                 {
220                     i_sar_num *= p_vout->i_par_den;
221                     i_sar_den *= p_vout->i_par_num;
222                     i_aspect = i_aspect * p_vout->i_par_den / p_vout->i_par_num;
223                 }
224 #endif
225
226                 if( i_sar_num > 0 && i_sar_den > 0 && i_aspect > 0 )
227                 {
228                     p_vout->fmt_in.i_sar_num = i_sar_num;
229                     p_vout->fmt_in.i_sar_den = i_sar_den;
230                     p_vout->fmt_in.i_aspect  = i_aspect;
231
232                     p_vout->fmt_render.i_sar_num = i_sar_num;
233                     p_vout->fmt_render.i_sar_den = i_sar_den;
234                     p_vout->fmt_render.i_aspect  = i_aspect;
235
236                     p_vout->render.i_aspect   = i_aspect;
237
238                     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
239
240                 }
241             }
242             vlc_mutex_unlock( &p_vout->change_lock );
243
244             vlc_object_release( p_vout );
245         }
246
247         if( p_vout )
248         {
249             msg_Dbg( p_this, "reusing provided vout" );
250
251             spu_Attach( p_vout->p_spu, p_this, true );
252
253             vlc_object_detach( p_vout );
254             vlc_object_attach( p_vout, p_this );
255
256             /* Display title if we are not using the vout given to vout_Request.
257              * XXX for now b_vout_provided is always true at this stage */
258             if( p_vout->p->b_title_show && !b_vout_provided )
259                 DisplayTitleOnOSD( p_vout );
260         }
261     }
262
263     if( !p_vout )
264     {
265         msg_Dbg( p_this, "no usable vout present, spawning one" );
266
267         p_vout = vout_Create( p_this, p_fmt );
268     }
269
270     return p_vout;
271 }
272
273 /*****************************************************************************
274  * vout_Create: creates a new video output thread
275  *****************************************************************************
276  * This function creates a new video output thread, and returns a pointer
277  * to its description. On error, it returns NULL.
278  *****************************************************************************/
279 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
280 {
281     vout_thread_t  * p_vout;                            /* thread descriptor */
282     int              i_index;                               /* loop variable */
283     vlc_value_t      val, text;
284
285     unsigned int i_width = p_fmt->i_width;
286     unsigned int i_height = p_fmt->i_height;
287     vlc_fourcc_t i_chroma = p_fmt->i_chroma;
288     unsigned int i_aspect = p_fmt->i_aspect;
289
290     config_chain_t *p_cfg;
291     char *psz_parser;
292     char *psz_name;
293
294     if( i_width <= 0 || i_height <= 0 || i_aspect <= 0 )
295         return NULL;
296
297     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
298                  p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
299     if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
300         return NULL;
301
302     /* Allocate descriptor */
303     static const char typename[] = "video output";
304     p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
305                                 typename );
306     if( p_vout == NULL )
307         return NULL;
308
309     /* */
310     p_vout->p = calloc( 1, sizeof(*p_vout->p) );
311     if( !p_vout->p )
312     {
313         vlc_object_release( p_vout );
314         return NULL;
315     }
316
317     /* Initialize pictures - translation tables and functions
318      * will be initialized later in InitThread */
319     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
320     {
321         p_vout->p_picture[i_index].pf_lock = NULL;
322         p_vout->p_picture[i_index].pf_unlock = NULL;
323         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
324         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
325         p_vout->p_picture[i_index].b_slow   = 0;
326     }
327
328     /* No images in the heap */
329     p_vout->i_heap_size = 0;
330
331     /* Initialize the rendering heap */
332     I_RENDERPICTURES = 0;
333
334     p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
335     p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
336
337     p_vout->render.i_width    = i_width;
338     p_vout->render.i_height   = i_height;
339     p_vout->render.i_chroma   = i_chroma;
340     p_vout->render.i_aspect   = i_aspect;
341
342     p_vout->render.i_rmask    = p_fmt->i_rmask;
343     p_vout->render.i_gmask    = p_fmt->i_gmask;
344     p_vout->render.i_bmask    = p_fmt->i_bmask;
345
346     p_vout->render.i_last_used_pic = -1;
347     p_vout->render.b_allow_modify_pics = 1;
348
349     /* Zero the output heap */
350     I_OUTPUTPICTURES = 0;
351     p_vout->output.i_width    = 0;
352     p_vout->output.i_height   = 0;
353     p_vout->output.i_chroma   = 0;
354     p_vout->output.i_aspect   = 0;
355
356     p_vout->output.i_rmask    = 0;
357     p_vout->output.i_gmask    = 0;
358     p_vout->output.i_bmask    = 0;
359
360     /* Initialize misc stuff */
361     p_vout->i_changes    = 0;
362     p_vout->b_scale      = 1;
363     p_vout->b_fullscreen = 0;
364     p_vout->i_alignment  = 0;
365     p_vout->p->render_time  = 10;
366     p_vout->p->c_fps_samples = 0;
367     p_vout->p->i_picture_lost = 0;
368     p_vout->p->i_picture_displayed = 0;
369     p_vout->p->b_filter_change = 0;
370     p_vout->p->b_paused = false;
371     p_vout->p->i_pause_date = 0;
372     p_vout->pf_control = NULL;
373     p_vout->p_window = NULL;
374     p_vout->p->i_par_num =
375     p_vout->p->i_par_den = 1;
376     p_vout->p->p_picture_displayed = NULL;
377
378     /* Initialize locks */
379     vlc_mutex_init_recursive( &p_vout->picture_lock );
380     vlc_mutex_init( &p_vout->change_lock );
381     vlc_mutex_init( &p_vout->p->vfilter_lock );
382
383     /* Mouse coordinates */
384     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
385     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
386     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
387     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
388     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
389
390     /* Initialize subpicture unit */
391     p_vout->p_spu = spu_Create( p_vout );
392     spu_Attach( p_vout->p_spu, p_parent, true );
393
394     /* Attach the new object now so we can use var inheritance below */
395     vlc_object_attach( p_vout, p_parent );
396
397     spu_Init( p_vout->p_spu );
398
399     /* Take care of some "interface/control" related initialisations */
400     vout_IntfInit( p_vout );
401
402     /* If the parent is not a VOUT object, that means we are at the start of
403      * the video output pipe */
404     if( p_parent->i_object_type != VLC_OBJECT_VOUT )
405     {
406         /* Look for the default filter configuration */
407         p_vout->p->psz_filter_chain =
408             var_CreateGetStringCommand( p_vout, "vout-filter" );
409
410         /* Apply video filter2 objects on the first vout */
411         p_vout->p->psz_vf2 =
412             var_CreateGetStringCommand( p_vout, "video-filter" );
413     }
414     else
415     {
416         /* continue the parent's filter chain */
417         char *psz_tmp;
418
419         /* Ugly hack to jump to our configuration chain */
420         p_vout->p->psz_filter_chain
421             = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
422         p_vout->p->psz_filter_chain
423             = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
424         config_ChainDestroy( p_cfg );
425         free( psz_tmp );
426
427         /* Create a video filter2 var ... but don't inherit values */
428         var_Create( p_vout, "video-filter",
429                     VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
430         p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
431     }
432
433     var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
434     p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
435         false, video_filter_buffer_allocation_init, NULL, p_vout );
436
437     /* Choose the video output module */
438     if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
439     {
440         var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
441         var_Get( p_vout, "vout", &val );
442         psz_parser = val.psz_string;
443     }
444     else
445     {
446         psz_parser = strdup( p_vout->p->psz_filter_chain );
447         p_vout->p->b_title_show = false;
448     }
449
450     /* Create the vout thread */
451     char* psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
452     free( psz_parser );
453     free( psz_tmp );
454     p_vout->p_cfg = p_cfg;
455     p_vout->p_module = module_need( p_vout,
456         ( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain ) ?
457         "video filter" : "video output", psz_name, p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain );
458     free( psz_name );
459
460     if( p_vout->p_module == NULL )
461     {
462         msg_Err( p_vout, "no suitable vout module" );
463         vlc_object_set_destructor( p_vout, vout_Destructor );
464         vlc_object_release( p_vout );
465         return NULL;
466     }
467
468     /* Create a few object variables for interface interaction */
469     var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
470     text.psz_string = _("Deinterlace");
471     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
472     val.psz_string = (char *)""; text.psz_string = _("Disable");
473     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
474     val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
475     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
476     val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
477     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
478     val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
479     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
480     val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
481     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
482     val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
483     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
484     val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
485     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
486
487     if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
488     {
489         var_Set( p_vout, "deinterlace", val );
490         free( val.psz_string );
491     }
492     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
493
494     var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
495     text.psz_string = _("Filters");
496     var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
497     var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
498
499     if( vlc_thread_create( p_vout, "video output", RunThread,
500                            VLC_THREAD_PRIORITY_OUTPUT, true ) )
501     {
502         module_unneed( p_vout, p_vout->p_module );
503         p_vout->p_module = NULL;
504         vlc_object_set_destructor( p_vout, vout_Destructor );
505         vlc_object_release( p_vout );
506         return NULL;
507     }
508
509     vlc_object_set_destructor( p_vout, vout_Destructor );
510
511     if( p_vout->b_error )
512     {
513         msg_Err( p_vout, "video output creation failed" );
514         vout_CloseAndRelease( p_vout );
515         return NULL;
516     }
517
518     return p_vout;
519 }
520
521 /*****************************************************************************
522  * vout_Close: Close a vout created by vout_Create.
523  *****************************************************************************
524  * You HAVE to call it on vout created by vout_Create before vlc_object_release.
525  * You should NEVER call it on vout not obtained through vout_Create
526  * (like with vout_Request or vlc_object_find.)
527  * You can use vout_CloseAndRelease() as a convenient method.
528  *****************************************************************************/
529 void vout_Close( vout_thread_t *p_vout )
530 {
531     assert( p_vout );
532
533     vlc_object_kill( p_vout );
534     vlc_thread_join( p_vout );
535     module_unneed( p_vout, p_vout->p_module );
536     p_vout->p_module = NULL;
537 }
538
539 /* */
540 static void vout_Destructor( vlc_object_t * p_this )
541 {
542     vout_thread_t *p_vout = (vout_thread_t *)p_this;
543
544     /* Make sure the vout was stopped first */
545     assert( !p_vout->p_module );
546
547     /* */
548     spu_Destroy( p_vout->p_spu );
549
550     /* Destroy the locks */
551     vlc_mutex_destroy( &p_vout->picture_lock );
552     vlc_mutex_destroy( &p_vout->change_lock );
553     vlc_mutex_destroy( &p_vout->p->vfilter_lock );
554
555     free( p_vout->p->psz_filter_chain );
556
557     config_ChainDestroy( p_vout->p_cfg );
558
559     free( p_vout->p );
560
561 #ifndef __APPLE__
562     vout_thread_t *p_another_vout;
563
564     /* This is a dirty hack mostly for Linux, where there is no way to get the
565      * GUI back if you closed it while playing video. This is solved in
566      * Mac OS X, where we have this novelty called menubar, that will always
567      * allow you access to the applications main functionality. They should try
568      * that on linux sometime. */
569     p_another_vout = vlc_object_find( p_this->p_libvlc,
570                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
571     if( p_another_vout == NULL )
572         var_SetBool( p_this->p_libvlc, "intf-show", true );
573     else
574         vlc_object_release( p_another_vout );
575 #endif
576 }
577
578 /* */
579 void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
580 {
581     vlc_object_lock( p_vout );
582
583     assert( !p_vout->p->b_paused || !b_paused );
584     if( p_vout->p->b_paused )
585     {
586         const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
587
588         for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
589         {
590             picture_t *p_pic = PP_RENDERPICTURE[i_index];
591
592             if( p_pic->i_status == READY_PICTURE )
593                 p_pic->date += i_duration;
594         }
595         spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
596     }
597     p_vout->p->b_paused = b_paused;
598     p_vout->p->i_pause_date = i_date;
599
600     vlc_object_unlock( p_vout );
601 }
602 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
603 {
604     vlc_object_lock( p_vout );
605
606     *pi_displayed = p_vout->p->i_picture_displayed;
607     *pi_lost = p_vout->p->i_picture_lost;
608
609     p_vout->p->i_picture_displayed = 0;
610     p_vout->p->i_picture_lost = 0;
611
612     vlc_object_unlock( p_vout );
613 }
614 void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
615 {
616     vlc_mutex_lock( &p_vout->picture_lock );
617     for( int i = 0; i < p_vout->render.i_pictures; i++ )
618     {
619         picture_t *p_pic = p_vout->render.pp_picture[i];
620
621         if( p_pic->i_status == READY_PICTURE ||
622             p_pic->i_status == DISPLAYED_PICTURE )
623         {
624             /* We cannot change picture status if it is in READY_PICTURE state,
625              * Just make sure they won't be displayed */
626             if( p_pic->date > i_date )
627                 p_pic->date = i_date;
628         }
629     }
630     vlc_mutex_unlock( &p_vout->picture_lock );
631 }
632 void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
633 {
634     int i_pic, i_ready_pic;
635
636     vlc_mutex_lock( &p_vout->picture_lock );
637
638     for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
639     {
640         const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
641
642         if( p_pic->i_status == READY_PICTURE )
643         {
644             i_ready_pic++;
645             /* If we have at least 2 ready pictures, wait for the vout thread to
646              * process one */
647             if( i_ready_pic >= 2 )
648                 break;
649
650             continue;
651         }
652
653         if( p_pic->i_status == DISPLAYED_PICTURE )
654         {
655             /* If at least one displayed picture is not referenced
656              * let vout free it */
657             if( p_pic->i_refcount == 0 )
658                 break;
659         }
660     }
661     if( i_pic < p_vout->render.i_pictures && !b_forced )
662     {
663         vlc_mutex_unlock( &p_vout->picture_lock );
664         return;
665     }
666
667     /* Too many pictures are still referenced, there is probably a bug
668      * with the decoder */
669     if( !b_forced )
670         msg_Err( p_vout, "pictures leaked, resetting the heap" );
671
672     /* Just free all the pictures */
673     for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
674     {
675         picture_t *p_pic = p_vout->render.pp_picture[i_pic];
676
677         if( p_pic->i_status == RESERVED_PICTURE )
678             vout_DestroyPicture( p_vout, p_pic );
679         if( p_pic->i_refcount > 0 )
680             vout_UnlinkPicture( p_vout, p_pic );
681     }
682     vlc_mutex_unlock( &p_vout->picture_lock );
683 }
684
685 /*****************************************************************************
686  * InitThread: initialize video output thread
687  *****************************************************************************
688  * This function is called from RunThread and performs the second step of the
689  * initialization. It returns 0 on success. Note that the thread's flag are not
690  * modified inside this function.
691  * XXX You have to enter it with change_lock taken.
692  *****************************************************************************/
693 static int ChromaCreate( vout_thread_t *p_vout );
694 static void ChromaDestroy( vout_thread_t *p_vout );
695
696 static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
697 {
698      if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
699          return false;
700
701      if( p_output->i_chroma != FOURCC_RV15 &&
702          p_output->i_chroma != FOURCC_RV16 &&
703          p_output->i_chroma != FOURCC_RV24 &&
704          p_output->i_chroma != FOURCC_RV32 )
705          return true;
706
707      return p_output->i_rmask == p_render->i_rmask &&
708             p_output->i_gmask == p_render->i_gmask &&
709             p_output->i_bmask == p_render->i_bmask;
710 }
711
712 static int InitThread( vout_thread_t *p_vout )
713 {
714     int i, i_aspect_x, i_aspect_y;
715
716     /* Initialize output method, it allocates direct buffers for us */
717     if( p_vout->pf_init( p_vout ) )
718         return VLC_EGENERIC;
719
720     p_vout->p->p_picture_displayed = NULL;
721
722     if( !I_OUTPUTPICTURES )
723     {
724         msg_Err( p_vout, "plugin was unable to allocate at least "
725                          "one direct buffer" );
726         p_vout->pf_end( p_vout );
727         return VLC_EGENERIC;
728     }
729
730     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
731     {
732         msg_Err( p_vout, "plugin allocated too many direct buffers, "
733                          "our internal buffers must have overflown." );
734         p_vout->pf_end( p_vout );
735         return VLC_EGENERIC;
736     }
737
738     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
739
740     AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
741
742     msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
743              "chroma %4.4s, ar %i:%i, sar %i:%i",
744              p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
745              p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
746              p_vout->fmt_render.i_visible_width,
747              p_vout->fmt_render.i_visible_height,
748              (char*)&p_vout->fmt_render.i_chroma,
749              i_aspect_x, i_aspect_y,
750              p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
751
752     AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
753
754     msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
755              "chroma %4.4s, ar %i:%i, sar %i:%i",
756              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
757              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
758              p_vout->fmt_in.i_visible_width,
759              p_vout->fmt_in.i_visible_height,
760              (char*)&p_vout->fmt_in.i_chroma,
761              i_aspect_x, i_aspect_y,
762              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
763
764     if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
765     {
766         p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
767             p_vout->output.i_width;
768         p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
769             p_vout->output.i_height;
770         p_vout->fmt_out.i_x_offset =  p_vout->fmt_out.i_y_offset = 0;
771
772         p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
773         p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
774     }
775     if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
776     {
777         p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
778             p_vout->fmt_out.i_height;
779         p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
780             p_vout->fmt_out.i_width;
781     }
782
783     vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
784                  p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
785
786     AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
787
788     msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
789              "chroma %4.4s, ar %i:%i, sar %i:%i",
790              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
791              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
792              p_vout->fmt_out.i_visible_width,
793              p_vout->fmt_out.i_visible_height,
794              (char*)&p_vout->fmt_out.i_chroma,
795              i_aspect_x, i_aspect_y,
796              p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
797
798     /* FIXME removed the need of both fmt_* and heap infos */
799     /* Calculate shifts from system-updated masks */
800     PictureHeapFixRgb( &p_vout->render );
801     VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
802
803     PictureHeapFixRgb( &p_vout->output );
804     VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
805
806     /* Check whether we managed to create direct buffers similar to
807      * the render buffers, ie same size and chroma */
808     if( ( p_vout->output.i_width == p_vout->render.i_width )
809      && ( p_vout->output.i_height == p_vout->render.i_height )
810      && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
811     {
812         /* Cool ! We have direct buffers, we can ask the decoder to
813          * directly decode into them ! Map the first render buffers to
814          * the first direct buffers, but keep the first direct buffer
815          * for memcpy operations */
816         p_vout->p->b_direct = true;
817
818         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
819         {
820             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
821                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
822                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
823             {
824                 /* We have enough direct buffers so there's no need to
825                  * try to use system memory buffers. */
826                 break;
827             }
828             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
829             I_RENDERPICTURES++;
830         }
831
832         msg_Dbg( p_vout, "direct render, mapping "
833                  "render pictures 0-%i to system pictures 1-%i",
834                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
835     }
836     else
837     {
838         /* Rats... Something is wrong here, we could not find an output
839          * plugin able to directly render what we decode. See if we can
840          * find a chroma plugin to do the conversion */
841         p_vout->p->b_direct = false;
842
843         if( ChromaCreate( p_vout ) )
844         {
845             p_vout->pf_end( p_vout );
846             return VLC_EGENERIC;
847         }
848
849         msg_Dbg( p_vout, "indirect render, mapping "
850                  "render pictures 0-%i to system pictures %i-%i",
851                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
852                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
853
854         /* Append render buffers after the direct buffers */
855         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
856         {
857             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
858             I_RENDERPICTURES++;
859
860             /* Check if we have enough render pictures */
861             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
862                 break;
863         }
864     }
865
866     /* Link pictures back to their heap */
867     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
868     {
869         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
870     }
871
872     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
873     {
874         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
875     }
876
877     return VLC_SUCCESS;
878 }
879
880 /*****************************************************************************
881  * RunThread: video output thread
882  *****************************************************************************
883  * Video output thread. This function does only returns when the thread is
884  * terminated. It handles the pictures arriving in the video heap and the
885  * display device events.
886  *****************************************************************************/
887 static void* RunThread( vlc_object_t *p_this )
888 {
889     vout_thread_t *p_vout = (vout_thread_t *)p_this;
890     int             i_idle_loops = 0;  /* loops without displaying a picture */
891
892     subpicture_t *  p_subpic = NULL;                   /* subpicture pointer */
893
894     bool            b_drop_late;
895
896     int canc = vlc_savecancel();
897
898     /*
899      * Initialize thread
900      */
901     vlc_mutex_lock( &p_vout->change_lock );
902     p_vout->b_error = InitThread( p_vout );
903
904     b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
905
906     /* signal the creation of the vout */
907     vlc_thread_ready( p_vout );
908
909     if( p_vout->b_error )
910     {
911         EndThread( p_vout );
912         vlc_mutex_unlock( &p_vout->change_lock );
913         vlc_restorecancel( canc );
914         return NULL;
915     }
916
917     vlc_object_lock( p_vout );
918
919     if( p_vout->p->b_title_show )
920         DisplayTitleOnOSD( p_vout );
921
922     /*
923      * Main loop - it is not executed if an error occurred during
924      * initialization
925      */
926     while( vlc_object_alive( p_vout ) && !p_vout->b_error )
927     {
928         /* Initialize loop variables */
929         const mtime_t current_date = mdate();
930         picture_t *p_picture = NULL;
931         picture_t *p_filtered_picture;
932         mtime_t display_date = 0;
933         picture_t *p_directbuffer;
934         int i_index;
935
936         /* Find the picture to display (the one with the earliest date). */
937         vlc_mutex_lock( &p_vout->picture_lock );
938
939         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
940         {
941             picture_t *p_pic = PP_RENDERPICTURE[i_index];
942
943             if( p_pic->i_status == READY_PICTURE &&
944                 ( p_picture == NULL || p_pic->date < display_date ) )
945             {
946                 p_picture = p_pic;
947                 display_date = p_picture->date;
948             }
949         }
950         if( p_vout->p->b_paused && p_vout->p->p_picture_displayed != NULL )
951         {
952             p_picture = NULL;
953             if( p_vout->p->p_picture_displayed->date == 1 )
954             {
955                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
956                 {
957                     picture_t *p_pic = PP_RENDERPICTURE[i_index];
958
959                     if( p_pic->i_status != READY_PICTURE )
960                         continue;
961                     if( p_pic->date <= p_vout->p->p_picture_displayed->date && p_pic != p_vout->p->p_picture_displayed )
962                     {
963                         DropPicture( p_vout, p_pic );
964                     }
965                     else if( p_pic->date > p_vout->p->p_picture_displayed->date && ( p_picture == NULL || p_pic->date < display_date ) )
966                     {
967                         p_picture = p_pic;
968                         display_date = p_picture->date;
969                     }
970                 }
971             }
972             if( !p_picture )
973                 p_picture = p_vout->p->p_picture_displayed;
974         }
975
976         if( p_picture )
977         {
978             /* If we met the last picture, parse again to see whether there is
979              * a more appropriate one. */
980             if( p_picture == p_vout->p->p_picture_displayed && !p_vout->p->b_paused )
981             {
982                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
983                 {
984                     picture_t *p_pic = PP_RENDERPICTURE[i_index];
985
986                     if( p_pic->i_status == READY_PICTURE &&
987                         p_pic != p_vout->p->p_picture_displayed &&
988                         ( p_picture == p_vout->p->p_picture_displayed || p_pic->date < display_date ) )
989                     {
990                         p_picture = p_pic;
991                         display_date = p_picture->date;
992                     }
993                 }
994             }
995
996             /* If we found better than the last picture, destroy it */
997             if( p_vout->p->p_picture_displayed && p_picture != p_vout->p->p_picture_displayed )
998             {
999                 DropPicture( p_vout, p_vout->p->p_picture_displayed );
1000                 p_vout->p->p_picture_displayed = NULL;
1001             }
1002
1003             /* Compute FPS rate */
1004             p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ]
1005                 = display_date;
1006
1007             if( !p_picture->b_force &&
1008                 p_picture != p_vout->p->p_picture_displayed &&
1009                 display_date < current_date + p_vout->p->render_time &&
1010                 b_drop_late )
1011             {
1012                 /* Picture is late: it will be destroyed and the thread
1013                  * will directly choose the next picture */
1014                 DropPicture( p_vout, p_picture );
1015                 p_vout->p->i_picture_lost++;
1016                 msg_Warn( p_vout, "late picture skipped (%"PRId64")",
1017                                   current_date - display_date );
1018                 vlc_mutex_unlock( &p_vout->picture_lock );
1019                 continue;
1020             }
1021
1022             if( display_date > current_date + VOUT_DISPLAY_DELAY )
1023             {
1024                 /* A picture is ready to be rendered, but its rendering date
1025                  * is far from the current one so the thread will perform an
1026                  * empty loop as if no picture were found. The picture state
1027                  * is unchanged */
1028                 p_picture    = NULL;
1029                 display_date = 0;
1030             }
1031             else if( p_picture == p_vout->p->p_picture_displayed )
1032             {
1033                 /* We are asked to repeat the previous picture, but we first
1034                  * wait for a couple of idle loops */
1035                 if( i_idle_loops < 4 )
1036                 {
1037                     p_picture    = NULL;
1038                     display_date = 0;
1039                 }
1040                 else
1041                 {
1042                     /* We set the display date to something high, otherwise
1043                      * we'll have lots of problems with late pictures */
1044                     display_date = current_date + p_vout->p->render_time;
1045                 }
1046             }
1047         }
1048         if( p_picture )
1049             p_vout->p->p_picture_displayed = p_picture;
1050         vlc_mutex_unlock( &p_vout->picture_lock );
1051
1052         if( p_picture == NULL )
1053             i_idle_loops++;
1054
1055         p_filtered_picture = NULL;
1056         if( p_picture )
1057             p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
1058                                                            p_picture );
1059
1060         /* FIXME it is a bit ugly that b_snapshot is not locked but I do not
1061          * know which lock to use (here and in the snapshot callback) */
1062         const bool b_snapshot = p_vout->p->b_snapshot;
1063         if( b_snapshot )
1064             p_vout->p->b_snapshot = false;
1065
1066         /*
1067          * Check for subpictures to display
1068          */
1069         if( display_date > 0 )
1070             p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
1071                                             p_vout->p->b_paused, b_snapshot );
1072
1073         /*
1074          * Perform rendering
1075          */
1076         p_vout->p->i_picture_displayed++;
1077         p_directbuffer = vout_RenderPicture( p_vout, p_filtered_picture,
1078                                              p_subpic, p_vout->p->b_paused );
1079
1080         /*
1081          * Take a snapshot if requested
1082          */
1083         if( p_directbuffer && b_snapshot )
1084             vout_Snapshot( p_vout, p_directbuffer );
1085
1086         /*
1087          * Call the plugin-specific rendering method if there is one
1088          */
1089         if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
1090         {
1091             /* Render the direct buffer returned by vout_RenderPicture */
1092             p_vout->pf_render( p_vout, p_directbuffer );
1093         }
1094
1095         /*
1096          * Sleep, wake up
1097          */
1098         if( display_date != 0 && p_directbuffer != NULL )
1099         {
1100             mtime_t current_render_time = mdate() - current_date;
1101             /* if render time is very large we don't include it in the mean */
1102             if( current_render_time < p_vout->p->render_time +
1103                 VOUT_DISPLAY_DELAY )
1104             {
1105                 /* Store render time using a sliding mean weighting to
1106                  * current value in a 3 to 1 ratio*/
1107                 p_vout->p->render_time *= 3;
1108                 p_vout->p->render_time += current_render_time;
1109                 p_vout->p->render_time >>= 2;
1110             }
1111         }
1112
1113         /* Give back change lock */
1114         vlc_mutex_unlock( &p_vout->change_lock );
1115
1116         vlc_object_unlock( p_vout );
1117
1118         /* Sleep a while or until a given date */
1119         if( display_date != 0 )
1120         {
1121             /* If there are filters in the chain, better give them the picture
1122              * in advance */
1123             if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
1124             {
1125                 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1126             }
1127         }
1128         else
1129         {
1130             msleep( VOUT_IDLE_SLEEP );
1131         }
1132
1133         /* On awakening, take back lock and send immediately picture
1134          * to display. */
1135         vlc_object_lock( p_vout );
1136         /* Note: vlc_object_alive() could be false here, and we
1137          * could be dead */
1138         vlc_mutex_lock( &p_vout->change_lock );
1139
1140         /*
1141          * Display the previously rendered picture
1142          */
1143         if( p_filtered_picture != NULL && p_directbuffer != NULL )
1144         {
1145             /* Display the direct buffer returned by vout_RenderPicture */
1146             if( p_vout->pf_display )
1147                 p_vout->pf_display( p_vout, p_directbuffer );
1148
1149             /* Tell the vout this was the last picture and that it does not
1150              * need to be forced anymore. */
1151             p_picture->b_force = false;
1152         }
1153
1154         /* Drop the filtered picture if created by video filters */
1155         if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
1156             DropPicture( p_vout, p_filtered_picture );
1157
1158         if( p_picture != NULL )
1159         {
1160             /* Reinitialize idle loop count */
1161             i_idle_loops = 0;
1162         }
1163
1164         /*
1165          * Check events and manage thread
1166          */
1167         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
1168         {
1169             /* A fatal error occurred, and the thread must terminate
1170              * immediately, without displaying anything - setting b_error to 1
1171              * causes the immediate end of the main while() loop. */
1172             // FIXME pf_end
1173             p_vout->b_error = 1;
1174             break;
1175         }
1176
1177         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
1178         {
1179             /* this must only happen when the vout plugin is incapable of
1180              * rescaling the picture itself. In this case we need to destroy
1181              * the current picture buffers and recreate new ones with the right
1182              * dimensions */
1183             int i;
1184
1185             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
1186
1187             assert( !p_vout->p->b_direct );
1188
1189             ChromaDestroy( p_vout );
1190
1191             vlc_mutex_lock( &p_vout->picture_lock );
1192
1193             p_vout->pf_end( p_vout );
1194
1195             p_vout->p->p_picture_displayed = NULL;
1196             for( i = 0; i < I_OUTPUTPICTURES; i++ )
1197                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
1198
1199             I_OUTPUTPICTURES = 0;
1200
1201             if( p_vout->pf_init( p_vout ) )
1202             {
1203                 msg_Err( p_vout, "cannot resize display" );
1204                 /* FIXME: pf_end will be called again in EndThread() */
1205                 p_vout->b_error = 1;
1206             }
1207
1208             vlc_mutex_unlock( &p_vout->picture_lock );
1209
1210             /* Need to reinitialise the chroma plugin. Since we might need
1211              * resizing too and it's not sure that we already had it,
1212              * recreate the chroma plugin chain from scratch. */
1213             /* dionoea */
1214             if( ChromaCreate( p_vout ) )
1215             {
1216                 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
1217                 p_vout->b_error = 1;
1218             }
1219             if( p_vout->b_error )
1220                 break;
1221         }
1222
1223         if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1224         {
1225             /* This happens when the picture buffers need to be recreated.
1226              * This is useful on multimonitor displays for instance.
1227              *
1228              * Warning: This only works when the vout creates only 1 picture
1229              * buffer!! */
1230             p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1231
1232             if( !p_vout->p->b_direct )
1233                 ChromaDestroy( p_vout );
1234
1235             vlc_mutex_lock( &p_vout->picture_lock );
1236
1237             p_vout->pf_end( p_vout );
1238
1239             I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1240
1241             p_vout->b_error = InitThread( p_vout );
1242             if( p_vout->b_error )
1243                 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed\n" );
1244
1245             vlc_mutex_unlock( &p_vout->picture_lock );
1246
1247             if( p_vout->b_error )
1248                 break;
1249         }
1250
1251         /* Check for "video filter2" changes */
1252         vlc_mutex_lock( &p_vout->p->vfilter_lock );
1253         if( p_vout->p->psz_vf2 )
1254         {
1255             es_format_t fmt;
1256
1257             es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
1258             fmt.video = p_vout->fmt_render;
1259             filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
1260
1261             if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
1262                                                p_vout->p->psz_vf2 ) < 0 )
1263                 msg_Err( p_vout, "Video filter chain creation failed" );
1264
1265             free( p_vout->p->psz_vf2 );
1266             p_vout->p->psz_vf2 = NULL;
1267         }
1268         vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1269     }
1270
1271     /*
1272      * Error loop - wait until the thread destruction is requested
1273      */
1274     if( p_vout->b_error )
1275         ErrorThread( p_vout );
1276
1277     /* End of thread */
1278     CleanThread( p_vout );
1279     EndThread( p_vout );
1280     vlc_mutex_unlock( &p_vout->change_lock );
1281
1282     vlc_object_unlock( p_vout );
1283     vlc_restorecancel( canc );
1284     return NULL;
1285 }
1286
1287 /*****************************************************************************
1288  * ErrorThread: RunThread() error loop
1289  *****************************************************************************
1290  * This function is called when an error occurred during thread main's loop.
1291  * The thread can still receive feed, but must be ready to terminate as soon
1292  * as possible.
1293  *****************************************************************************/
1294 static void ErrorThread( vout_thread_t *p_vout )
1295 {
1296     /* Wait until a `die' order */
1297     while( vlc_object_alive( p_vout ) )
1298         vlc_object_wait( p_vout );
1299 }
1300
1301 /*****************************************************************************
1302  * CleanThread: clean up after InitThread
1303  *****************************************************************************
1304  * This function is called after a sucessful
1305  * initialization. It frees all resources allocated by InitThread.
1306  * XXX You have to enter it with change_lock taken.
1307  *****************************************************************************/
1308 static void CleanThread( vout_thread_t *p_vout )
1309 {
1310     int     i_index;                                        /* index in heap */
1311
1312     if( !p_vout->p->b_direct )
1313         ChromaDestroy( p_vout );
1314
1315     /* Destroy all remaining pictures */
1316     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1317     {
1318         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1319         {
1320             free( p_vout->p_picture[i_index].p_data_orig );
1321         }
1322     }
1323
1324     /* Destroy translation tables */
1325     if( !p_vout->b_error )
1326         p_vout->pf_end( p_vout );
1327 }
1328
1329 /*****************************************************************************
1330  * EndThread: thread destruction
1331  *****************************************************************************
1332  * This function is called when the thread ends.
1333  * It frees all resources not allocated by InitThread.
1334  * XXX You have to enter it with change_lock taken.
1335  *****************************************************************************/
1336 static void EndThread( vout_thread_t *p_vout )
1337 {
1338 #ifdef STATS
1339     {
1340         struct tms cpu_usage;
1341         times( &cpu_usage );
1342
1343         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1344                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1345     }
1346 #endif
1347
1348     /* FIXME does that function *really* need to be called inside the thread ? */
1349
1350     /* Destroy subpicture unit */
1351     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
1352
1353     /* Destroy the video filters2 */
1354     filter_chain_Delete( p_vout->p->p_vf2_chain );
1355 }
1356
1357 /* Thread helpers */
1358 static picture_t *ChromaGetPicture( filter_t *p_filter )
1359 {
1360     picture_t *p_pic = (picture_t *)p_filter->p_owner;
1361     p_filter->p_owner = NULL;
1362     return p_pic;
1363 }
1364
1365 static int ChromaCreate( vout_thread_t *p_vout )
1366 {
1367     static const char typename[] = "chroma";
1368     filter_t *p_chroma;
1369
1370     /* Choose the best module */
1371     p_chroma = p_vout->p->p_chroma =
1372         vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
1373                            typename );
1374
1375     vlc_object_attach( p_chroma, p_vout );
1376
1377     /* TODO: Set the fmt_in and fmt_out stuff here */
1378     p_chroma->fmt_in.video = p_vout->fmt_render;
1379     p_chroma->fmt_out.video = p_vout->fmt_out;
1380     VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
1381     VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
1382
1383     p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, 0 );
1384
1385     if( p_chroma->p_module == NULL )
1386     {
1387         msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
1388                  (char*)&p_vout->render.i_chroma,
1389                  (char*)&p_vout->output.i_chroma,
1390                  p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
1391                  p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
1392                  );
1393
1394         vlc_object_release( p_vout->p->p_chroma );
1395         p_vout->p->p_chroma = NULL;
1396
1397         return VLC_EGENERIC;
1398     }
1399     p_chroma->pf_vout_buffer_new = ChromaGetPicture;
1400     return VLC_SUCCESS;
1401 }
1402
1403 static void ChromaDestroy( vout_thread_t *p_vout )
1404 {
1405     assert( !p_vout->p->b_direct );
1406
1407     if( !p_vout->p->p_chroma )
1408         return;
1409
1410     module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
1411     vlc_object_release( p_vout->p->p_chroma );
1412     p_vout->p->p_chroma = NULL;
1413 }
1414
1415 static void DropPicture( vout_thread_t *p_vout, picture_t *p_picture )
1416 {
1417     vlc_mutex_lock( &p_vout->picture_lock );
1418     if( p_picture->i_refcount )
1419     {
1420         /* Pretend we displayed the picture, but don't destroy
1421          * it since the decoder might still need it. */
1422         p_picture->i_status = DISPLAYED_PICTURE;
1423     }
1424     else
1425     {
1426         /* Destroy the picture without displaying it */
1427         p_picture->i_status = DESTROYED_PICTURE;
1428         p_vout->i_heap_size--;
1429         picture_CleanupQuant( p_picture );
1430     }
1431     vlc_mutex_unlock( &p_vout->picture_lock );
1432 }
1433
1434
1435
1436 /* following functions are local */
1437
1438 static int ReduceHeight( int i_ratio )
1439 {
1440     int i_dummy = VOUT_ASPECT_FACTOR;
1441     int i_pgcd  = 1;
1442
1443     if( !i_ratio )
1444     {
1445         return i_pgcd;
1446     }
1447
1448     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1449     while( !(i_ratio & 1) && !(i_dummy & 1) )
1450     {
1451         i_ratio >>= 1;
1452         i_dummy >>= 1;
1453         i_pgcd  <<= 1;
1454     }
1455
1456     while( !(i_ratio % 3) && !(i_dummy % 3) )
1457     {
1458         i_ratio /= 3;
1459         i_dummy /= 3;
1460         i_pgcd  *= 3;
1461     }
1462
1463     while( !(i_ratio % 5) && !(i_dummy % 5) )
1464     {
1465         i_ratio /= 5;
1466         i_dummy /= 5;
1467         i_pgcd  *= 5;
1468     }
1469
1470     return i_pgcd;
1471 }
1472
1473 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1474 {
1475     unsigned int i_pgcd = ReduceHeight( i_aspect );
1476     *i_aspect_x = i_aspect / i_pgcd;
1477     *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1478 }
1479
1480 /**
1481  * This function copies all RGB informations from a picture_heap_t into
1482  * a video_format_t
1483  */
1484 static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
1485 {
1486     p_fmt->i_rmask = p_heap->i_rmask;
1487     p_fmt->i_gmask = p_heap->i_gmask;
1488     p_fmt->i_bmask = p_heap->i_bmask;
1489     p_fmt->i_rrshift = p_heap->i_rrshift;
1490     p_fmt->i_lrshift = p_heap->i_lrshift;
1491     p_fmt->i_rgshift = p_heap->i_rgshift;
1492     p_fmt->i_lgshift = p_heap->i_lgshift;
1493     p_fmt->i_rbshift = p_heap->i_rbshift;
1494     p_fmt->i_lbshift = p_heap->i_lbshift;
1495 }
1496
1497 /**
1498  * This funtion copes all RGB informations from a video_format_t into
1499  * a picture_heap_t
1500  */
1501 static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
1502 {
1503     p_heap->i_rmask = p_fmt->i_rmask;
1504     p_heap->i_gmask = p_fmt->i_gmask;
1505     p_heap->i_bmask = p_fmt->i_bmask;
1506     p_heap->i_rrshift = p_fmt->i_rrshift;
1507     p_heap->i_lrshift = p_fmt->i_lrshift;
1508     p_heap->i_rgshift = p_fmt->i_rgshift;
1509     p_heap->i_lgshift = p_fmt->i_lgshift;
1510     p_heap->i_rbshift = p_fmt->i_rbshift;
1511     p_heap->i_lbshift = p_fmt->i_lbshift;
1512 }
1513
1514 /**
1515  * This function computes rgb shifts from masks
1516  */
1517 static void PictureHeapFixRgb( picture_heap_t *p_heap )
1518 {
1519     video_format_t fmt;
1520
1521     /* */
1522     fmt.i_chroma = p_heap->i_chroma;
1523     VideoFormatImportRgb( &fmt, p_heap );
1524
1525     /* */
1526     video_format_FixRgb( &fmt );
1527
1528     VideoFormatExportRgb( &fmt, p_heap );
1529 }
1530
1531 /*****************************************************************************
1532  * object variables callbacks: a bunch of object variables are used by the
1533  * interfaces to interact with the vout.
1534  *****************************************************************************/
1535 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1536                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1537 {
1538     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1539     input_thread_t *p_input;
1540     vlc_value_t val;
1541
1542     char *psz_mode = newval.psz_string;
1543     char *psz_filter, *psz_deinterlace = NULL;
1544     (void)psz_cmd; (void)oldval; (void)p_data;
1545
1546     var_Get( p_vout, "vout-filter", &val );
1547     psz_filter = val.psz_string;
1548     if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1549
1550     if( !psz_mode || !*psz_mode )
1551     {
1552         if( psz_deinterlace )
1553         {
1554             char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1555             if( psz_src[0] == ':' ) psz_src++;
1556             memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1557         }
1558     }
1559     else if( !psz_deinterlace )
1560     {
1561         psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1562                               sizeof(":deinterlace") );
1563         if( psz_filter )
1564         {
1565             if( *psz_filter )
1566                 strcat( psz_filter, ":" );
1567             strcat( psz_filter, "deinterlace" );
1568         }
1569     }
1570
1571     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1572                                                  FIND_PARENT );
1573     if( !p_input )
1574     {
1575         free( psz_filter );
1576         return VLC_EGENERIC;
1577     }
1578
1579     if( psz_mode && *psz_mode )
1580     {
1581         /* Modify input as well because the vout might have to be restarted */
1582         val.psz_string = psz_mode;
1583         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1584         var_Set( p_input, "deinterlace-mode", val );
1585     }
1586     vlc_object_release( p_input );
1587
1588     val.b_bool = true;
1589     var_Set( p_vout, "intf-change", val );
1590
1591     val.psz_string = psz_filter;
1592     var_Set( p_vout, "vout-filter", val );
1593     free( psz_filter );
1594
1595     return VLC_SUCCESS;
1596 }
1597
1598 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1599                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1600 {
1601     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1602     input_thread_t *p_input;
1603     vlc_value_t val;
1604     (void)psz_cmd; (void)oldval; (void)p_data;
1605
1606     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1607                                                  FIND_PARENT );
1608     if (!p_input)
1609     {
1610         msg_Err( p_vout, "Input not found" );
1611         return( VLC_EGENERIC );
1612     }
1613
1614     val.b_bool = true;
1615     var_Set( p_vout, "intf-change", val );
1616
1617     /* Modify input as well because the vout might have to be restarted */
1618     val.psz_string = newval.psz_string;
1619     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1620
1621     var_Set( p_input, "vout-filter", val );
1622
1623     /* Now restart current video stream */
1624     input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1625     vlc_object_release( p_input );
1626
1627     return VLC_SUCCESS;
1628 }
1629
1630 /*****************************************************************************
1631  * Video Filter2 stuff
1632  *****************************************************************************/
1633 static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
1634                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1635 {
1636     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1637     (void)psz_cmd; (void)oldval; (void)p_data;
1638
1639     vlc_mutex_lock( &p_vout->p->vfilter_lock );
1640     p_vout->p->psz_vf2 = strdup( newval.psz_string );
1641     vlc_mutex_unlock( &p_vout->p->vfilter_lock );
1642
1643     return VLC_SUCCESS;
1644 }
1645
1646 static void DisplayTitleOnOSD( vout_thread_t *p_vout )
1647 {
1648     input_thread_t *p_input;
1649     mtime_t i_now, i_stop;
1650
1651     if( !config_GetInt( p_vout, "osd" ) ) return;
1652
1653     p_input = (input_thread_t *)vlc_object_find( p_vout,
1654               VLC_OBJECT_INPUT, FIND_ANYWHERE );
1655     if( p_input )
1656     {
1657         i_now = mdate();
1658         i_stop = i_now + (mtime_t)(p_vout->p->i_title_timeout * 1000);
1659         char *psz_nowplaying =
1660             input_item_GetNowPlaying( input_GetItem( p_input ) );
1661         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
1662         char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
1663         if( EMPTY_STR( psz_name ) )
1664         {
1665             free( psz_name );
1666             psz_name = input_item_GetName( input_GetItem( p_input ) );
1667         }
1668         if( !EMPTY_STR( psz_nowplaying ) )
1669         {
1670             vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1671                                    psz_nowplaying, NULL,
1672                                    p_vout->p->i_title_position,
1673                                    30 + p_vout->fmt_in.i_width
1674                                       - p_vout->fmt_in.i_visible_width
1675                                       - p_vout->fmt_in.i_x_offset,
1676                                    20 + p_vout->fmt_in.i_y_offset,
1677                                    i_now, i_stop );
1678         }
1679         else if( !EMPTY_STR( psz_artist ) )
1680         {
1681             char *psz_string = NULL;
1682             if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
1683             {
1684                 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1685                                        psz_string, NULL,
1686                                        p_vout->p->i_title_position,
1687                                        30 + p_vout->fmt_in.i_width
1688                                           - p_vout->fmt_in.i_visible_width
1689                                           - p_vout->fmt_in.i_x_offset,
1690                                        20 + p_vout->fmt_in.i_y_offset,
1691                                        i_now, i_stop );
1692                 free( psz_string );
1693             }
1694         }
1695         else
1696         {
1697             vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
1698                                    psz_name, NULL,
1699                                    p_vout->p->i_title_position,
1700                                    30 + p_vout->fmt_in.i_width
1701                                       - p_vout->fmt_in.i_visible_width
1702                                       - p_vout->fmt_in.i_x_offset,
1703                                    20 + p_vout->fmt_in.i_y_offset,
1704                                    i_now, i_stop );
1705         }
1706         vlc_object_release( p_input );
1707         free( psz_artist );
1708         free( psz_name );
1709         free( psz_nowplaying );
1710     }
1711 }
1712