]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
No functionnal changes (vout).
[vlc] / src / video_output / video_output.c
1 /*****************************************************************************
2  * video_output.c : video output thread
3  *
4  * This module describes the programming interface for video output threads.
5  * It includes functions allowing to open a new thread, send pictures to a
6  * thread, and destroy a previously oppened video output thread.
7  *****************************************************************************
8  * Copyright (C) 2000-2007 the VideoLAN team
9  * $Id$
10  *
11  * Authors: Vincent Seguin <seguin@via.ecp.fr>
12  *          Gildas Bazin <gbazin@videolan.org>
13  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38
39 #include <stdlib.h>                                                /* free() */
40 #include <string.h>
41 #include <assert.h>
42
43 #include <vlc_vout.h>
44
45 #include <vlc_filter.h>
46 #include <vlc_vout_osd.h>
47
48 #include <libvlc.h>
49 #include <vlc_input.h>
50 #include "vout_internal.h"
51 #include "interlacing.h"
52 #include "postprocessing.h"
53 #include "display.h"
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static void     *Thread(void *);
59 static void     vout_Destructor(vlc_object_t *);
60
61 /* Object variables callbacks */
62 static int FilterCallback( vlc_object_t *, char const *,
63                            vlc_value_t, vlc_value_t, void * );
64 static int VideoFilter2Callback( vlc_object_t *, char const *,
65                                  vlc_value_t, vlc_value_t, void * );
66
67 /* */
68 static void PrintVideoFormat(vout_thread_t *, const char *, const video_format_t *);
69
70 /* Maximum delay between 2 displayed pictures.
71  * XXX it is needed for now but should be removed in the long term.
72  */
73 #define VOUT_REDISPLAY_DELAY (INT64_C(80000))
74
75 /**
76  * Late pictures having a delay higher than this value are thrashed.
77  */
78 #define VOUT_DISPLAY_LATE_THRESHOLD (INT64_C(20000))
79
80 /* Better be in advance when awakening than late... */
81 #define VOUT_MWAIT_TOLERANCE (INT64_C(1000))
82
83 /*****************************************************************************
84  * Video Filter2 functions
85  *****************************************************************************/
86 static picture_t *video_new_buffer_filter( filter_t *p_filter )
87 {
88     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
89     return picture_pool_Get(p_vout->p->private_pool);
90 }
91
92 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
93 {
94     VLC_UNUSED(p_filter);
95     picture_Release(p_pic);
96 }
97
98 static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
99 {
100     p_filter->pf_video_buffer_new = video_new_buffer_filter;
101     p_filter->pf_video_buffer_del = video_del_buffer_filter;
102     p_filter->p_owner = p_data; /* p_vout */
103     return VLC_SUCCESS;
104 }
105
106 #undef vout_Request
107 /*****************************************************************************
108  * vout_Request: find a video output thread, create one, or destroy one.
109  *****************************************************************************
110  * This function looks for a video output thread matching the current
111  * properties. If not found, it spawns a new one.
112  *****************************************************************************/
113 vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
114                              video_format_t *p_fmt )
115 {
116     if( !p_fmt )
117     {
118         /* Video output is no longer used.
119          * TODO: support for reusing video outputs with proper _thread-safe_
120          * reference handling. */
121         if( p_vout )
122             vout_CloseAndRelease( p_vout );
123         return NULL;
124     }
125
126     /* If a video output was provided, lock it, otherwise look for one. */
127     if( p_vout )
128     {
129         vlc_object_hold( p_vout );
130     }
131
132     /* TODO: find a suitable unused video output */
133
134     /* If we now have a video output, check it has the right properties */
135     if( p_vout )
136     {
137         if( !video_format_IsSimilar( &p_vout->p->original, p_fmt ) )
138         {
139             /* We are not interested in this format, close this vout */
140             vout_CloseAndRelease( p_vout );
141             vlc_object_release( p_vout );
142             p_vout = NULL;
143         }
144         else
145         {
146             /* This video output is cool! Hijack it. */
147             vlc_object_release( p_vout );
148         }
149
150         if( p_vout )
151         {
152             msg_Dbg( p_this, "reusing provided vout" );
153
154             spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
155             vlc_object_detach( p_vout );
156
157             vlc_object_attach( p_vout, p_this );
158             spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
159         }
160     }
161
162     if( !p_vout )
163     {
164         msg_Dbg( p_this, "no usable vout present, spawning one" );
165
166         p_vout = vout_Create( p_this, p_fmt );
167     }
168
169     return p_vout;
170 }
171
172 /*****************************************************************************
173  * vout_Create: creates a new video output thread
174  *****************************************************************************
175  * This function creates a new video output thread, and returns a pointer
176  * to its description. On error, it returns NULL.
177  *****************************************************************************/
178 vout_thread_t * (vout_Create)( vlc_object_t *p_parent, video_format_t *p_fmt )
179 {
180     vout_thread_t  *p_vout;                            /* thread descriptor */
181     vlc_value_t     text;
182
183
184     config_chain_t *p_cfg;
185     char *psz_parser;
186     char *psz_name;
187
188     if( p_fmt->i_width <= 0 || p_fmt->i_height <= 0 )
189         return NULL;
190     const vlc_fourcc_t i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, p_fmt->i_chroma );
191
192     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
193                  p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
194     if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
195         return NULL;
196
197     /* Allocate descriptor */
198     static const char typename[] = "video output";
199     p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
200                                 typename );
201     if( p_vout == NULL )
202         return NULL;
203
204     /* */
205     p_vout->p = calloc( 1, sizeof(*p_vout->p) );
206     if( !p_vout->p )
207     {
208         vlc_object_release( p_vout );
209         return NULL;
210     }
211
212     /* */
213     p_vout->p->original = *p_fmt;   /* FIXME palette */
214     p_vout->p->original.i_chroma = i_chroma;
215     video_format_FixRgb( &p_vout->p->original );
216
217     /* Initialize misc stuff */
218     vout_control_Init( &p_vout->p->control );
219     vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_INIT );
220     vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
221     vout_statistic_Init( &p_vout->p->statistic );
222     p_vout->p->i_par_num =
223     p_vout->p->i_par_den = 1;
224     p_vout->p->displayed.date = VLC_TS_INVALID;
225     p_vout->p->displayed.decoded = NULL;
226     p_vout->p->displayed.timestamp = VLC_TS_INVALID;
227     p_vout->p->displayed.qtype = QTYPE_NONE;
228     p_vout->p->displayed.is_interlaced = false;
229
230     p_vout->p->step.last         = VLC_TS_INVALID;
231     p_vout->p->step.timestamp    = VLC_TS_INVALID;
232
233     p_vout->p->pause.is_on  = false;
234     p_vout->p->pause.date   = VLC_TS_INVALID;
235
236     p_vout->p->decoder_fifo = picture_fifo_New();
237     p_vout->p->decoder_pool = NULL;
238
239     vlc_mouse_Init( &p_vout->p->mouse );
240
241     vout_snapshot_Init( &p_vout->p->snapshot );
242
243     p_vout->p->vfilter_chain =
244         filter_chain_New( p_vout, "video filter2", false,
245                           video_filter_buffer_allocation_init, NULL, p_vout );
246
247     /* Initialize locks */
248     vlc_mutex_init( &p_vout->p->picture_lock );
249     vlc_mutex_init( &p_vout->p->vfilter_lock );
250
251     /* Mouse coordinates */
252     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
253     var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
254     var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
255     /* Mouse object (area of interest in a video filter) */
256     var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
257
258     /* Attach the new object now so we can use var inheritance below */
259     vlc_object_attach( p_vout, p_parent );
260
261     /* Initialize subpicture unit */
262     p_vout->p->p_spu = spu_Create( p_vout );
263
264     /* */
265     spu_Init( p_vout->p->p_spu );
266
267     spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), true );
268
269     p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
270
271     /* Take care of some "interface/control" related initialisations */
272     vout_IntfInit( p_vout );
273
274     /* Look for the default filter configuration */
275     p_vout->p->psz_filter_chain =
276         var_CreateGetStringCommand( p_vout, "vout-filter" );
277
278     /* Apply video filter2 objects on the first vout */
279     var_Create( p_vout, "video-filter",
280                 VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
281     var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
282     var_TriggerCallback( p_vout, "video-filter" );
283
284     /* Choose the video output module */
285     if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
286     {
287         psz_parser = NULL;
288     }
289     else
290     {
291         psz_parser = strdup( p_vout->p->psz_filter_chain );
292         p_vout->p->title.show = false;
293     }
294
295     /* Create the vout thread */
296     char *psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
297     free( psz_parser );
298     free( psz_tmp );
299     p_vout->p->p_cfg = p_cfg;
300
301     /* Create a few object variables for interface interaction */
302     var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
303     text.psz_string = _("Filters");
304     var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
305     var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
306
307     /* */
308     vout_InitInterlacingSupport( p_vout, p_vout->p->displayed.is_interlaced );
309
310     if( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain )
311     {
312         char *psz_tmp;
313         if( asprintf( &psz_tmp, "%s,none", psz_name ) < 0 )
314             psz_tmp = strdup( "" );
315         free( psz_name );
316         psz_name = psz_tmp;
317     }
318     p_vout->p->psz_module_name = psz_name;
319
320     /* */
321     vlc_object_set_destructor( p_vout, vout_Destructor );
322
323     /* */
324     if( vlc_clone( &p_vout->p->thread, Thread, p_vout,
325                    VLC_THREAD_PRIORITY_OUTPUT ) )
326     {
327         spu_Attach( p_vout->p->p_spu, VLC_OBJECT(p_vout), false );
328         spu_Destroy( p_vout->p->p_spu );
329         p_vout->p->p_spu = NULL;
330         vlc_object_release( p_vout );
331         return NULL;
332     }
333
334     vout_control_WaitEmpty( &p_vout->p->control );
335
336     if (p_vout->p->dead )
337     {
338         msg_Err( p_vout, "video output creation failed" );
339         vout_CloseAndRelease( p_vout );
340         return NULL;
341     }
342
343     return p_vout;
344 }
345
346 /*****************************************************************************
347  * vout_Close: Close a vout created by vout_Create.
348  *****************************************************************************
349  * You HAVE to call it on vout created by vout_Create before vlc_object_release.
350  * You should NEVER call it on vout not obtained through vout_Create
351  * (like with vout_Request or vlc_object_find.)
352  * You can use vout_CloseAndRelease() as a convenience method.
353  *****************************************************************************/
354 void vout_Close( vout_thread_t *p_vout )
355 {
356     assert( p_vout );
357
358     vout_snapshot_End( &p_vout->p->snapshot );
359
360     vout_control_PushVoid( &p_vout->p->control, VOUT_CONTROL_CLEAN );
361     vlc_join( p_vout->p->thread, NULL );
362 }
363
364 /* */
365 static void vout_Destructor( vlc_object_t * p_this )
366 {
367     vout_thread_t *p_vout = (vout_thread_t *)p_this;
368
369     /* Make sure the vout was stopped first */
370     //assert( !p_vout->p_module );
371
372     free( p_vout->p->psz_module_name );
373
374     /* */
375     if( p_vout->p->p_spu )
376         spu_Destroy( p_vout->p->p_spu );
377
378     vout_chrono_Clean( &p_vout->p->render );
379
380     if( p_vout->p->decoder_fifo )
381         picture_fifo_Delete( p_vout->p->decoder_fifo );
382     assert( !p_vout->p->decoder_pool );
383
384     /* Destroy the locks */
385     vlc_mutex_destroy( &p_vout->p->picture_lock );
386     vlc_mutex_destroy( &p_vout->p->vfilter_lock );
387     vout_control_Clean( &p_vout->p->control );
388
389     /* */
390     vout_statistic_Clean( &p_vout->p->statistic );
391
392     /* */
393     vout_snapshot_Clean( &p_vout->p->snapshot );
394
395     /* */
396     free( p_vout->p->psz_filter_chain );
397
398     config_ChainDestroy( p_vout->p->p_cfg );
399
400     free( p_vout->p );
401
402 }
403
404 /* */
405 void vout_ChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
406 {
407     vout_control_cmd_t cmd;
408     vout_control_cmd_Init(&cmd, VOUT_CONTROL_PAUSE);
409     cmd.u.pause.is_on = is_paused;
410     cmd.u.pause.date  = date;
411     vout_control_Push(&vout->p->control, &cmd);
412
413     vout_control_WaitEmpty(&vout->p->control);
414 }
415
416 void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
417 {
418     vout_statistic_GetReset( &p_vout->p->statistic,
419                              pi_displayed, pi_lost );
420 }
421
422 void vout_Flush(vout_thread_t *vout, mtime_t date)
423 {
424     vout_control_PushTime(&vout->p->control, VOUT_CONTROL_FLUSH, date);
425     vout_control_WaitEmpty(&vout->p->control);
426 }
427
428 void vout_Reset(vout_thread_t *vout)
429 {
430     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_RESET);
431     vout_control_WaitEmpty(&vout->p->control);
432 }
433
434 bool vout_IsEmpty(vout_thread_t *vout)
435 {
436     vlc_mutex_lock(&vout->p->picture_lock);
437
438     picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
439     if (picture)
440         picture_Release(picture);
441
442     vlc_mutex_unlock(&vout->p->picture_lock);
443
444     return !picture;
445 }
446
447 void vout_FixLeaks( vout_thread_t *vout )
448 {
449     vlc_mutex_lock(&vout->p->picture_lock);
450
451     picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
452     if (!picture) {
453         picture = picture_pool_Get(vout->p->decoder_pool);
454     }
455
456     if (picture) {
457         picture_Release(picture);
458         /* Not all pictures has been displayed yet or some are
459          * free */
460         vlc_mutex_unlock(&vout->p->picture_lock);
461         return;
462     }
463
464     /* There is no reason that no pictures are available, force one
465      * from the pool, becarefull with it though */
466     msg_Err(vout, "pictures leaked, trying to workaround");
467
468     /* */
469     picture_pool_NonEmpty(vout->p->decoder_pool, false);
470
471     vlc_mutex_unlock(&vout->p->picture_lock);
472 }
473 void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
474 {
475     vout_control_cmd_t cmd;
476     vout_control_cmd_Init(&cmd, VOUT_CONTROL_STEP);
477     cmd.u.time_ptr = duration;
478
479     vout_control_Push(&vout->p->control, &cmd);
480     vout_control_WaitEmpty(&vout->p->control);
481 }
482
483 void vout_DisplayTitle(vout_thread_t *vout, const char *title)
484 {
485     assert(title);
486     vout_control_PushString(&vout->p->control, VOUT_CONTROL_OSD_TITLE, title);
487 }
488
489 void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
490 {
491     spu_DisplaySubpicture(vout->p->p_spu, subpic);
492 }
493 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
494 {
495     return spu_RegisterChannel(vout->p->p_spu);
496 }
497 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
498 {
499     spu_ClearChannel(vout->p->p_spu, channel);
500 }
501
502 spu_t *vout_GetSpu( vout_thread_t *p_vout )
503 {
504     return p_vout->p->p_spu;
505 }
506
507 /* vout_Control* are usable by anyone at anytime */
508 void vout_ControlChangeFullscreen(vout_thread_t *vout, bool fullscreen)
509 {
510     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_FULLSCREEN,
511                           fullscreen);
512 }
513 void vout_ControlChangeOnTop(vout_thread_t *vout, bool is_on_top)
514 {
515     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_ON_TOP,
516                           is_on_top);
517 }
518 void vout_ControlChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
519 {
520     vout_control_PushBool(&vout->p->control, VOUT_CONTROL_DISPLAY_FILLED,
521                           is_filled);
522 }
523 void vout_ControlChangeZoom(vout_thread_t *vout, int num, int den)
524 {
525     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ZOOM,
526                           num, den);
527 }
528 void vout_ControlChangeSampleAspectRatio(vout_thread_t *vout,
529                                          unsigned num, unsigned den)
530 {
531     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_ASPECT_RATIO,
532                           num, den);
533 }
534 void vout_ControlChangeCropRatio(vout_thread_t *vout,
535                                  unsigned num, unsigned den)
536 {
537     vout_control_PushPair(&vout->p->control, VOUT_CONTROL_CROP_RATIO,
538                           num, den);
539 }
540 void vout_ControlChangeCropWindow(vout_thread_t *vout,
541                                   int x, int y, int width, int height)
542 {
543     vout_control_cmd_t cmd;
544     vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_WINDOW);
545     cmd.u.window.x      = x;
546     cmd.u.window.y      = y;
547     cmd.u.window.width  = width;
548     cmd.u.window.height = height;
549
550     vout_control_Push(&vout->p->control, &cmd);
551 }
552 void vout_ControlChangeCropBorder(vout_thread_t *vout,
553                                   int left, int top, int right, int bottom)
554 {
555     vout_control_cmd_t cmd;
556     vout_control_cmd_Init(&cmd, VOUT_CONTROL_CROP_BORDER);
557     cmd.u.border.left   = left;
558     cmd.u.border.top    = top;
559     cmd.u.border.right  = right;
560     cmd.u.border.bottom = bottom;
561
562     vout_control_Push(&vout->p->control, &cmd);
563 }
564
565 /* */
566 static int ThreadDisplayPicture(vout_thread_t *vout,
567                                 bool now, mtime_t *deadline)
568 {
569     vout_display_t *vd = vout->p->display.vd;
570     int displayed_count = 0;
571     int lost_count = 0;
572
573     for (;;) {
574         const mtime_t date = mdate();
575         const bool is_paused = vout->p->pause.is_on;
576         bool redisplay = is_paused && !now && vout->p->displayed.decoded;
577         bool is_forced;
578
579         /* FIXME/XXX we must redisplay the last decoded picture (because
580          * of potential vout updated, or filters update or SPU update)
581          * For now a high update period is needed but it coulmd be removed
582          * if and only if:
583          * - vout module emits events from theselves.
584          * - *and* SPU is modified to emit an event or a deadline when needed.
585          *
586          * So it will be done latter.
587          */
588         if (!redisplay) {
589             picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
590             if (peek) {
591                 is_forced = peek->b_force || is_paused || now;
592                 *deadline = (is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
593                 picture_Release(peek);
594             } else {
595                 redisplay = true;
596             }
597         }
598         if (redisplay) {
599              /* FIXME a better way for this delay is needed */
600             const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
601             if (date_update > date || !vout->p->displayed.decoded) {
602                 *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
603                 break;
604             }
605             /* */
606             is_forced = true;
607             *deadline = date - vout_chrono_GetHigh(&vout->p->render);
608         }
609         if (*deadline > VOUT_MWAIT_TOLERANCE)
610             *deadline -= VOUT_MWAIT_TOLERANCE;
611
612         /* If we are too early and can wait, do it */
613         if (date < *deadline && !now)
614             break;
615
616         picture_t *decoded;
617         if (redisplay) {
618             decoded = vout->p->displayed.decoded;
619             vout->p->displayed.decoded = NULL;
620         } else {
621             decoded = picture_fifo_Pop(vout->p->decoder_fifo);
622             assert(decoded);
623             if (!is_forced && !vout->p->is_late_dropped) {
624                 const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
625                 const mtime_t late = predicted - decoded->date;
626                 if (late > 0) {
627                     msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
628                     if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
629                         msg_Warn(vout, "rejected picture because of render time");
630                         /* TODO */
631                         picture_Release(decoded);
632                         lost_count++;
633                         break;
634                     }
635                 }
636             }
637
638             vout->p->displayed.is_interlaced = !decoded->b_progressive;
639             vout->p->displayed.qtype         = decoded->i_qtype;
640         }
641         vout->p->displayed.timestamp = decoded->date;
642
643         /* */
644         if (vout->p->displayed.decoded)
645             picture_Release(vout->p->displayed.decoded);
646         picture_Hold(decoded);
647         vout->p->displayed.decoded = decoded;
648
649         /* */
650         vout_chrono_Start(&vout->p->render);
651
652         picture_t *filtered = NULL;
653         if (decoded) {
654             vlc_mutex_lock(&vout->p->vfilter_lock);
655             filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
656             //assert(filtered == decoded); // TODO implement
657             vlc_mutex_unlock(&vout->p->vfilter_lock);
658             if (!filtered)
659                 continue;
660         }
661
662         /*
663          * Check for subpictures to display
664          */
665         const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
666         mtime_t spu_render_time = is_forced ? mdate() : filtered->date;
667         if (vout->p->pause.is_on)
668             spu_render_time = vout->p->pause.date;
669         else
670             spu_render_time = filtered->date > 1 ? filtered->date : mdate();
671
672         subpicture_t *subpic = spu_SortSubpictures(vout->p->p_spu,
673                                                    spu_render_time,
674                                                    do_snapshot);
675         /*
676          * Perform rendering
677          *
678          * We have to:
679          * - be sure to end up with a direct buffer.
680          * - blend subtitles, and in a fast access buffer
681          */
682         picture_t *direct = NULL;
683         if (filtered &&
684             (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
685             picture_t *render;
686             if (vout->p->is_decoder_pool_slow)
687                 render = picture_NewFromFormat(&vd->source);
688             else if (vout->p->decoder_pool != vout->p->display_pool)
689                 render = picture_pool_Get(vout->p->display_pool);
690             else
691                 render = picture_pool_Get(vout->p->private_pool);
692
693             if (render) {
694                 picture_Copy(render, filtered);
695
696                 spu_RenderSubpictures(vout->p->p_spu,
697                                       render, &vd->source,
698                                       subpic, &vd->source, spu_render_time);
699             }
700             if (vout->p->is_decoder_pool_slow) {
701                 direct = picture_pool_Get(vout->p->display_pool);
702                 if (direct)
703                     picture_Copy(direct, render);
704                 picture_Release(render);
705
706             } else {
707                 direct = render;
708             }
709             picture_Release(filtered);
710             filtered = NULL;
711         } else {
712             direct = filtered;
713         }
714
715         /*
716          * Take a snapshot if requested
717          */
718         if (direct && do_snapshot)
719             vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
720
721         /* Render the direct buffer returned by vout_RenderPicture */
722         if (direct) {
723             vout_RenderWrapper(vout, direct);
724
725             vout_chrono_Stop(&vout->p->render);
726 #if 0
727             {
728             static int i = 0;
729             if (((i++)%10) == 0)
730                 msg_Info(vout, "render: avg %d ms var %d ms",
731                          (int)(vout->p->render.avg/1000), (int)(vout->p->render.var/1000));
732             }
733 #endif
734         }
735
736         /* Wait the real date (for rendering jitter) */
737         if (!is_forced)
738             mwait(decoded->date);
739
740         /* Display the direct buffer returned by vout_RenderPicture */
741         vout->p->displayed.date = mdate();
742         if (direct)
743             vout_DisplayWrapper(vout, direct);
744
745         displayed_count++;
746         break;
747     }
748
749     vout_statistic_Update(&vout->p->statistic, displayed_count, lost_count);
750     if (displayed_count <= 0)
751         return VLC_EGENERIC;
752     return VLC_SUCCESS;
753 }
754
755 static void ThreadManage(vout_thread_t *vout,
756                          mtime_t *deadline,
757                          vout_interlacing_support_t *interlacing,
758                          vout_postprocessing_support_t *postprocessing)
759 {
760     vlc_mutex_lock(&vout->p->picture_lock);
761
762     *deadline = VLC_TS_INVALID;
763     ThreadDisplayPicture(vout, false, deadline);
764
765     const int  picture_qtype      = vout->p->displayed.qtype;
766     const bool picture_interlaced = vout->p->displayed.is_interlaced;
767
768     vlc_mutex_unlock(&vout->p->picture_lock);
769
770     /* Post processing */
771     vout_SetPostProcessingState(vout, postprocessing, picture_qtype);
772
773     /* Deinterlacing */
774     vout_SetInterlacingState(vout, interlacing, picture_interlaced);
775
776     vout_ManageWrapper(vout);
777 }
778
779 static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
780 {
781     if (!vout->p->title.show)
782         return;
783
784     vout_OSDText(vout, SPU_DEFAULT_CHANNEL,
785                  vout->p->title.position, INT64_C(1000) * vout->p->title.timeout,
786                  string);
787 }
788
789 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
790 {
791     es_format_t fmt;
792     es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
793     fmt.video = vout->p->original;
794
795     vlc_mutex_lock(&vout->p->vfilter_lock);
796
797     filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
798     if (filter_chain_AppendFromString(vout->p->vfilter_chain,
799                                       filters) < 0)
800         msg_Err(vout, "Video filter chain creation failed");
801
802     vlc_mutex_unlock(&vout->p->vfilter_lock);
803 }
804
805 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
806 {
807     assert(!vout->p->pause.is_on || !is_paused);
808
809     if (vout->p->pause.is_on) {
810         const mtime_t duration = date - vout->p->pause.date;
811
812         if (vout->p->step.timestamp > VLC_TS_INVALID)
813             vout->p->step.timestamp += duration;
814         if (vout->p->step.last > VLC_TS_INVALID)
815             vout->p->step.last += duration;
816         picture_fifo_OffsetDate(vout->p->decoder_fifo, duration);
817         if (vout->p->displayed.decoded)
818             vout->p->displayed.decoded->date += duration;
819
820         spu_OffsetSubtitleDate(vout->p->p_spu, duration);
821     } else {
822         vout->p->step.timestamp = VLC_TS_INVALID;
823         vout->p->step.last      = VLC_TS_INVALID;
824     }
825     vout->p->pause.is_on = is_paused;
826     vout->p->pause.date  = date;
827 }
828
829 static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
830 {
831     vout->p->step.timestamp = VLC_TS_INVALID;
832     vout->p->step.last      = VLC_TS_INVALID;
833
834     picture_t *last = vout->p->displayed.decoded;
835     if (last) {
836         if (( below && last->date <= date) ||
837             (!below && last->date >= date)) {
838             picture_Release(last);
839
840             vout->p->displayed.decoded   = NULL;
841             vout->p->displayed.date      = VLC_TS_INVALID;
842             vout->p->displayed.timestamp = VLC_TS_INVALID;
843         }
844     }
845     picture_fifo_Flush(vout->p->decoder_fifo, date, below);
846 }
847
848 static void ThreadReset(vout_thread_t *vout)
849 {
850     ThreadFlush(vout, true, INT64_MAX);
851     if (vout->p->decoder_pool)
852         picture_pool_NonEmpty(vout->p->decoder_pool, true);
853     vout->p->pause.is_on = false;
854     vout->p->pause.date  = mdate();
855 }
856
857 static void ThreadStep(vout_thread_t *vout, mtime_t *duration)
858 {
859     *duration = 0;
860
861     if (vout->p->step.last <= VLC_TS_INVALID)
862         vout->p->step.last = vout->p->displayed.timestamp;
863
864     mtime_t dummy;
865     if (ThreadDisplayPicture(vout, true, &dummy))
866         return;
867
868     vout->p->step.timestamp = vout->p->displayed.timestamp;
869
870     if (vout->p->step.last > VLC_TS_INVALID &&
871         vout->p->step.timestamp > vout->p->step.last) {
872         *duration = vout->p->step.timestamp - vout->p->step.last;
873         vout->p->step.last = vout->p->step.timestamp;
874         /* TODO advance subpicture by the duration ... */
875     }
876 }
877
878 static void ThreadChangeFullscreen(vout_thread_t *vout, bool fullscreen)
879 {
880     /* FIXME not sure setting "fullscreen" is good ... */
881     var_SetBool(vout, "fullscreen", fullscreen);
882     vout_SetDisplayFullscreen(vout->p->display.vd, fullscreen);
883 }
884
885 static void ThreadChangeOnTop(vout_thread_t *vout, bool is_on_top)
886 {
887     vout_SetWindowState(vout->p->display.vd,
888                         is_on_top ? VOUT_WINDOW_STATE_ABOVE :
889                                     VOUT_WINDOW_STATE_NORMAL);
890 }
891
892 static void ThreadChangeDisplayFilled(vout_thread_t *vout, bool is_filled)
893 {
894     vout_SetDisplayFilled(vout->p->display.vd, is_filled);
895 }
896
897 static void ThreadChangeZoom(vout_thread_t *vout, int num, int den)
898 {
899     if (num * 10 < den) {
900         num = den;
901         den *= 10;
902     } else if (num > den * 10) {
903         num = den * 10;
904     }
905
906     vout_SetDisplayZoom(vout->p->display.vd, num, den);
907 }
908
909 static void ThreadChangeAspectRatio(vout_thread_t *vout,
910                                     unsigned num, unsigned den)
911 {
912     const video_format_t *source = &vout->p->original;
913
914     if (num > 0 && den > 0) {
915         num *= source->i_visible_height;
916         den *= source->i_visible_width;
917         vlc_ureduce(&num, &den, num, den, 0);
918     }
919     vout_SetDisplayAspect(vout->p->display.vd, num, den);
920 }
921
922
923 static void ThreadExecuteCropWindow(vout_thread_t *vout,
924                                     unsigned crop_num, unsigned crop_den,
925                                     unsigned x, unsigned y,
926                                     unsigned width, unsigned height)
927 {
928     const video_format_t *source = &vout->p->original;
929
930     vout_SetDisplayCrop(vout->p->display.vd,
931                         crop_num, crop_den,
932                         source->i_x_offset + x,
933                         source->i_y_offset + y,
934                         width, height);
935 }
936 static void ThreadExecuteCropBorder(vout_thread_t *vout,
937                                     unsigned left, unsigned top,
938                                     unsigned right, unsigned bottom)
939 {
940     const video_format_t *source = &vout->p->original;
941     ThreadExecuteCropWindow(vout, 0, 0,
942                             left,
943                             top,
944                             /* At worst, it becomes < 0 (but unsigned) and will be rejected */
945                             source->i_visible_width  - (left + right),
946                             source->i_visible_height - (top  + bottom));
947 }
948
949 static void ThreadExecuteCropRatio(vout_thread_t *vout,
950                                    unsigned num, unsigned den)
951 {
952     const video_format_t *source = &vout->p->original;
953
954     int x, y;
955     int width, height;
956     if (num <= 0 || den <= 0) {
957         num = 0;
958         den = 0;
959         x   = 0;
960         y   = 0;
961         width  = source->i_visible_width;
962         height = source->i_visible_height;
963     } else {
964         unsigned scaled_width  = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
965         unsigned scaled_height = (uint64_t)source->i_visible_width  * den * source->i_sar_num / num / source->i_sar_den;
966
967         if (scaled_width < source->i_visible_width) {
968             x      = (source->i_visible_width - scaled_width) / 2;
969             y      = 0;
970             width  = scaled_width;
971             height = source->i_visible_height;
972         } else {
973             x      = 0;
974             y      = (source->i_visible_height - scaled_height) / 2;
975             width  = source->i_visible_width;
976             height = scaled_height;
977         }
978     }
979     ThreadExecuteCropWindow(vout, num, den, x, y, width, height);
980 }
981
982 static int ThreadInit(vout_thread_t *vout)
983 {
984     vout->p->dead = false;
985
986     if (vout_OpenWrapper(vout, vout->p->psz_module_name))
987         return VLC_EGENERIC;
988     if (vout_InitWrapper(vout))
989         return VLC_EGENERIC;
990     assert(vout->p->decoder_pool);
991
992     vout->p->displayed.decoded = NULL;
993
994     PrintVideoFormat(vout, "original format", &vout->p->original);
995     return VLC_SUCCESS;
996 }
997
998 static void ThreadClean(vout_thread_t *vout)
999 {
1000     /* Destroy the video filters2 */
1001     filter_chain_Delete(vout->p->vfilter_chain);
1002
1003     /* Destroy translation tables */
1004     if (vout->p->display.vd) {
1005         if (vout->p->decoder_pool) {
1006             ThreadFlush(vout, true, INT64_MAX);
1007             vout_EndWrapper(vout);
1008         }
1009         vout_CloseWrapper(vout);
1010     }
1011
1012     /* Detach subpicture unit from both input and vout */
1013     spu_Attach(vout->p->p_spu, VLC_OBJECT(vout), false);
1014     vlc_object_detach(vout->p->p_spu);
1015
1016     vout->p->dead = true;
1017     vout_control_Dead(&vout->p->control);
1018 }
1019
1020 /*****************************************************************************
1021  * Thread: video output thread
1022  *****************************************************************************
1023  * Video output thread. This function does only returns when the thread is
1024  * terminated. It handles the pictures arriving in the video heap and the
1025  * display device events.
1026  *****************************************************************************/
1027 static void *Thread(void *object)
1028 {
1029     vout_thread_t *vout = object;
1030
1031     vout_interlacing_support_t interlacing = {
1032         .is_interlaced = false,
1033         .date = mdate(),
1034     };
1035     vout_postprocessing_support_t postprocessing = {
1036         .qtype = QTYPE_NONE,
1037     };
1038
1039     mtime_t deadline = VLC_TS_INVALID;
1040     for (;;) {
1041         vout_control_cmd_t cmd;
1042
1043         /* FIXME remove thoses ugly timeouts
1044          */
1045         while (!vout_control_Pop(&vout->p->control, &cmd, deadline, 100000)) {
1046             switch(cmd.type) {
1047             case VOUT_CONTROL_INIT:
1048                 if (ThreadInit(vout)) {
1049                     ThreadClean(vout);
1050                     return NULL;
1051                 }
1052                 break;
1053             case VOUT_CONTROL_CLEAN:
1054                 ThreadClean(vout);
1055                 return NULL;
1056             case VOUT_CONTROL_OSD_TITLE:
1057                 ThreadDisplayOsdTitle(vout, cmd.u.string);
1058                 break;
1059             case VOUT_CONTROL_CHANGE_FILTERS:
1060                 ThreadChangeFilters(vout, cmd.u.string);
1061                 break;
1062             case VOUT_CONTROL_PAUSE:
1063                 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
1064                 break;
1065             case VOUT_CONTROL_FLUSH:
1066                 ThreadFlush(vout, false, cmd.u.time);
1067                 break;
1068             case VOUT_CONTROL_RESET:
1069                 ThreadReset(vout);
1070                 break;
1071             case VOUT_CONTROL_STEP:
1072                 ThreadStep(vout, cmd.u.time_ptr);
1073                 break;
1074             case VOUT_CONTROL_FULLSCREEN:
1075                 ThreadChangeFullscreen(vout, cmd.u.boolean);
1076                 break;
1077             case VOUT_CONTROL_ON_TOP:
1078                 ThreadChangeOnTop(vout, cmd.u.boolean);
1079                 break;
1080             case VOUT_CONTROL_DISPLAY_FILLED:
1081                 ThreadChangeDisplayFilled(vout, cmd.u.boolean);
1082                 break;
1083             case VOUT_CONTROL_ZOOM:
1084                 ThreadChangeZoom(vout, cmd.u.pair.a, cmd.u.pair.b);
1085                 break;
1086             case VOUT_CONTROL_ASPECT_RATIO:
1087                 ThreadChangeAspectRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1088                 break;
1089            case VOUT_CONTROL_CROP_RATIO:
1090                 ThreadExecuteCropRatio(vout, cmd.u.pair.a, cmd.u.pair.b);
1091                 break;
1092             case VOUT_CONTROL_CROP_WINDOW:
1093                 ThreadExecuteCropWindow(vout, 0, 0,
1094                                         cmd.u.window.x, cmd.u.window.y,
1095                                         cmd.u.window.width, cmd.u.window.height);
1096                 break;
1097             case VOUT_CONTROL_CROP_BORDER:
1098                 ThreadExecuteCropBorder(vout,
1099                                         cmd.u.border.left,  cmd.u.border.top,
1100                                         cmd.u.border.right, cmd.u.border.bottom);
1101                 break;
1102             default:
1103                 break;
1104             }
1105             vout_control_cmd_Clean(&cmd);
1106         }
1107
1108         ThreadManage(vout, &deadline, &interlacing, &postprocessing);
1109     }
1110 }
1111
1112 /*****************************************************************************
1113  * object variables callbacks: a bunch of object variables are used by the
1114  * interfaces to interact with the vout.
1115  *****************************************************************************/
1116 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1117                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1118 {
1119     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1120     input_thread_t *p_input;
1121     (void)psz_cmd; (void)oldval; (void)p_data;
1122
1123     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1124                                                  FIND_PARENT );
1125     if (!p_input)
1126     {
1127         msg_Err( p_vout, "Input not found" );
1128         return VLC_EGENERIC;
1129     }
1130
1131     /* Modify input as well because the vout might have to be restarted */
1132     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1133     var_SetString( p_input, "vout-filter", newval.psz_string );
1134
1135     /* Now restart current video stream */
1136     input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
1137     vlc_object_release( p_input );
1138
1139     return VLC_SUCCESS;
1140 }
1141
1142 /*****************************************************************************
1143  * Video Filter2 stuff
1144  *****************************************************************************/
1145 static int VideoFilter2Callback(vlc_object_t *object, char const *cmd,
1146                                 vlc_value_t oldval, vlc_value_t newval,
1147                                 void *data)
1148 {
1149     vout_thread_t *vout = (vout_thread_t *)object;
1150     (void)cmd; (void)oldval; (void)data;
1151
1152     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
1153                             newval.psz_string);
1154     return VLC_SUCCESS;
1155 }
1156
1157 /* */
1158 static void PrintVideoFormat(vout_thread_t *vout,
1159                              const char *description,
1160                              const video_format_t *fmt)
1161 {
1162     msg_Dbg(vout, "%s sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, sar %i:%i, msk r0x%x g0x%x b0x%x",
1163             description,
1164             fmt->i_width, fmt->i_height, fmt->i_x_offset, fmt->i_y_offset,
1165             fmt->i_visible_width, fmt->i_visible_height,
1166             (char*)&fmt->i_chroma,
1167             fmt->i_sar_num, fmt->i_sar_den,
1168             fmt->i_rmask, fmt->i_gmask, fmt->i_bmask);
1169 }
1170