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