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