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