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