]> git.sesse.net Git - vlc/blob - modules/video_output/wrapper.c
Enable XCB X11 output
[vlc] / modules / video_output / wrapper.c
1 /*****************************************************************************
2  * vout_display.c: "vout display" -> "video output" wrapper
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_vout_wrapper.h>
35 #include <vlc_vout.h>
36 #include "../video_filter/filter_common.h"
37 #include <assert.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open (vlc_object_t *, const char *module);
43 static void Close(vlc_object_t *);
44
45 #define DECLARE_OPEN(name) \
46         static int Open##name(vlc_object_t *object) { return Open(object, #name); }
47
48 DECLARE_OPEN(aalib);
49 DECLARE_OPEN(caca);
50 DECLARE_OPEN(sdl);
51 DECLARE_OPEN(xcb);
52 DECLARE_OPEN(xcb_xv);
53
54 #undef DECLARE_OPEN
55
56 #define DECLARE_MODULE(name, priority)                  \
57     set_description( "Video display "#name" wrapper" )  \
58     set_shortname( "Video display "#name" wrapper" )    \
59     set_capability( "video output", priority )          \
60     set_callbacks( Open##name, Close )                  \
61     add_shortcut( #name )
62
63 vlc_module_begin()
64     set_category( CAT_VIDEO )
65     set_subcategory( SUBCAT_VIDEO_VOUT )
66
67     DECLARE_MODULE(aalib, 10)
68
69     add_submodule()
70     DECLARE_MODULE(caca, 12)
71
72     add_submodule()
73     DECLARE_MODULE(sdl, 60)
74
75     add_submodule()
76     DECLARE_MODULE(xcb, 75)
77
78     add_submodule()
79     DECLARE_MODULE(xcb_xv, 0)
80
81 vlc_module_end()
82
83 #undef DECLARE_MODULE
84
85 /*****************************************************************************
86  *
87  *****************************************************************************/
88 struct vout_sys_t {
89     char           *title;
90     vout_display_t *vd;
91     bool           use_dr;
92 };
93
94 struct picture_sys_t {
95     picture_t *direct;
96 };
97
98 /*****************************************************************************
99  * Local prototypes
100  *****************************************************************************/
101 static int  Init   (vout_thread_t *);
102 static void End    (vout_thread_t *);
103 static int  Manage (vout_thread_t *);
104 static void Render (vout_thread_t *, picture_t *);
105 static void Display(vout_thread_t *, picture_t *);
106
107 static void VoutGetDisplayCfg(vout_thread_t *,
108                               vout_display_cfg_t *, const char *title);
109
110 /*****************************************************************************
111  *
112  *****************************************************************************/
113 static int Open(vlc_object_t *object, const char *module)
114 {
115     vout_thread_t *vout = (vout_thread_t *)object;
116     vout_sys_t *sys;
117
118     msg_Err(vout, "Opening vout display wrapper");
119
120     /* */
121     sys = malloc(sizeof(*sys));
122     if (!sys)
123         return VLC_ENOMEM;
124
125     sys->title = var_CreateGetNonEmptyString(vout, "video-title");
126
127     /* */
128     video_format_t source   = vout->fmt_render;
129     source.i_visible_width  = source.i_width;
130     source.i_visible_height = source.i_height;
131     source.i_x_offset       = 0;
132     source.i_y_offset       = 0;
133
134     vout_display_state_t state;
135     VoutGetDisplayCfg(vout, &state.cfg, sys->title);
136     state.is_on_top = var_CreateGetBool(vout, "video-on-top");
137     state.sar.num = 0;
138     state.sar.den = 0;
139
140     const mtime_t double_click_timeout = 300000;
141     const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
142
143     sys->vd = vout_NewDisplay(vout, &source, &state, module,
144                               double_click_timeout, hide_timeout);
145     if (!sys->vd) {
146         free(sys->title);
147         free(sys);
148         return VLC_EGENERIC;
149     }
150
151     /* */
152     vout->pf_init    = Init;
153     vout->pf_end     = End;
154     vout->pf_manage  = Manage;
155     vout->pf_render  = Render;
156     vout->pf_display = Display;
157     vout->pf_control = NULL;
158     vout->p_sys      = sys;
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  *
165  *****************************************************************************/
166 static void Close(vlc_object_t *object)
167 {
168     vout_thread_t *vout = (vout_thread_t *)object;
169     vout_sys_t *sys = vout->p_sys;
170
171     if (sys->vd)
172         vout_DeleteDisplay(sys->vd, NULL);
173     free(sys->title);
174     free(sys );
175 }
176
177 /*****************************************************************************
178  *
179  *****************************************************************************/
180 static int Init(vout_thread_t *vout)
181 {
182     vout_sys_t *sys = vout->p_sys;
183     vout_display_t *vd = sys->vd;
184
185     /* */
186     video_format_t source = vd->source;
187
188     vout->output.i_chroma = source.i_chroma;
189     vout->output.i_width  = source.i_width;
190     vout->output.i_height = source.i_height;
191     vout->output.i_aspect = source.i_aspect;
192     vout->output.i_rmask  = source.i_rmask;
193     vout->output.i_gmask  = source.i_gmask;
194     vout->output.i_bmask  = source.i_bmask;
195     vout->output.pf_setpalette = NULL; /* FIXME What to do ? Seems unused anyway */
196
197     /* also set fmt_out (completly broken API) */
198     vout->fmt_out.i_chroma         = vout->output.i_chroma;
199     vout->fmt_out.i_width          =
200     vout->fmt_out.i_visible_width  = vout->output.i_width;
201     vout->fmt_out.i_height         =
202     vout->fmt_out.i_visible_height = vout->output.i_height;
203     vout->fmt_out.i_aspect         = vout->output.i_aspect;
204     vout->fmt_out.i_x_offset       = 0;
205     vout->fmt_out.i_y_offset       = 0;
206
207     /* TODO */
208 #if 0
209     if (p_vout->fmt_render.i_visible_width  != source.i_visible_width ||
210         p_vout->fmt_render.i_visible_height != source.i_visible_height ||
211         p_vout->fmt_render.i_x_offset != source.i_x_offset ||
212         p_vout->fmt_render.i_y_offset != source.i_y_offset )
213     {
214         p_vout->i_changes |= VOUT_CROP_CHANGE;
215     }
216 #endif
217     if (vout->b_on_top)
218         vout_SetDisplayOnTop(vd, true);
219
220     /* XXX For non dr case, the current vout implementation force us to
221      * create at most 1 direct picture (otherwise the buffers will be kept
222      * referenced even through the Init/End.
223      */
224     sys->use_dr = !vout_IsDisplayFiltered(vd);
225     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
226     const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
227     for (vout->output.i_pictures = 0;
228             vout->output.i_pictures < picture_max;
229                 vout->output.i_pictures++) {
230         /* Find an empty picture slot */
231         picture_t *picture = NULL;
232         for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
233             if (vout->p_picture[index].i_status == FREE_PICTURE) {
234                 picture = &vout->p_picture[index];
235                 break;
236             }
237         }
238         if (!picture)
239             break;
240         memset(picture, 0, sizeof(*picture));
241
242         picture->p_sys = malloc(sizeof(*picture->p_sys));
243
244         if (sys->use_dr) {
245             picture_t *direct = vout_display_Get(vd);
246             if (!direct)
247                 break;
248             picture->format = direct->format;
249             picture->i_planes = direct->i_planes;
250             for (int i = 0; i < direct->i_planes; i++)
251                 picture->p[i] = direct->p[i];
252             picture->b_slow = vd->info.is_slow;
253
254             picture->p_sys->direct = direct;
255         } else {
256             vout_AllocatePicture(VLC_OBJECT(vd), picture,
257                                  vd->source.i_chroma,
258                                  vd->source.i_width, vd->source.i_height,
259                                  vd->source.i_aspect);
260             if (!picture->i_planes)
261                 break;
262             picture->p_sys->direct = NULL;
263         }
264         picture->i_status = DESTROYED_PICTURE;
265         picture->i_type    = DIRECT_PICTURE;
266
267         vout->output.pp_picture[vout->output.i_pictures] = picture;
268     }
269     return VLC_SUCCESS;
270 }
271
272 /*****************************************************************************
273  *
274  *****************************************************************************/
275 static void End(vout_thread_t *vout)
276 {
277     vout_sys_t *sys = vout->p_sys;
278
279     for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
280         picture_t *picture = &vout->p_picture[i];
281
282         if (picture->i_type != DIRECT_PICTURE)
283             continue;
284
285         if (picture->p_sys->direct)
286             picture_Release(picture->p_sys->direct);
287         if (!sys->use_dr)
288             free(picture->p_data_orig);
289         free(picture->p_sys);
290     }
291 }
292
293 /*****************************************************************************
294  *
295  *****************************************************************************/
296 static int Manage(vout_thread_t *vout)
297 {
298     vout_sys_t *sys = vout->p_sys;
299     vout_display_t *vd = sys->vd;
300
301     while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
302                               VOUT_ASPECT_CHANGE |
303                               VOUT_ZOOM_CHANGE |
304                               VOUT_SCALE_CHANGE |
305                               VOUT_ON_TOP_CHANGE |
306                               VOUT_CROP_CHANGE)) {
307         /* */
308         if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
309             vout->b_fullscreen = !vout->b_fullscreen;
310
311             var_SetBool(vout, "fullscreen", vout->b_fullscreen);
312             vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
313             vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
314         }
315         if (vout->i_changes & VOUT_ASPECT_CHANGE) {
316             vout->output.i_aspect   =
317             vout->fmt_out.i_aspect  = vout->fmt_in.i_aspect;
318             vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
319             vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
320
321             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
322
323             vout->i_changes &= ~VOUT_ASPECT_CHANGE;
324         }
325         if (vout->i_changes & VOUT_ZOOM_CHANGE) {
326             const float zoom = var_GetFloat(vout, "scale");
327
328             unsigned den = ZOOM_FP_FACTOR;
329             unsigned num = den * zoom;
330             if (num < (ZOOM_FP_FACTOR+9) / 10)
331                 num = (ZOOM_FP_FACTOR+9) / 10;
332             else if (num > ZOOM_FP_FACTOR * 10)
333                 num = ZOOM_FP_FACTOR * 10;
334
335             vout_SetDisplayZoom(vd, num, den);
336
337             vout->i_changes &= ~VOUT_ZOOM_CHANGE;
338         }
339         if (vout->i_changes & VOUT_SCALE_CHANGE) {
340             const bool is_display_filled = var_GetBool(vout, "autoscale");
341
342             vout_SetDisplayFilled(vd, is_display_filled);
343
344             vout->i_changes &= ~VOUT_SCALE_CHANGE;
345         }
346         if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
347             vout_SetDisplayOnTop(vd, vout->b_on_top);
348
349             vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
350         }
351         if (vout->i_changes & VOUT_CROP_CHANGE) {
352             const video_format_t crop = vout->fmt_in;
353             const video_format_t org = vout->fmt_render;
354             /* FIXME because of rounding errors, the reconstructed ratio is wrong */
355             unsigned num = 0;
356             unsigned den = 0;
357             if (crop.i_x_offset == org.i_x_offset &&
358                 crop.i_visible_width == org.i_visible_width &&
359                 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
360                 vlc_ureduce(&num, &den,
361                             crop.i_visible_width * crop.i_sar_num,
362                             crop.i_visible_height * crop.i_sar_den, 0);
363             } else if (crop.i_y_offset == org.i_y_offset &&
364                        crop.i_visible_height == org.i_visible_height &&
365                        crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
366                 vlc_ureduce(&num, &den,
367                             crop.i_visible_width * crop.i_sar_num,
368                             crop.i_visible_height * crop.i_sar_den, 0);
369             }
370             vout_SetDisplayCrop(vd, num, den,
371                                 crop.i_x_offset, crop.i_y_offset,
372                                 crop.i_visible_width, crop.i_visible_height);
373             vout->i_changes &= ~VOUT_CROP_CHANGE;
374         }
375
376     }
377
378     if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
379         vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
380     }
381
382     vout_ManageDisplay(vd);
383     return VLC_SUCCESS;
384 }
385
386 /*****************************************************************************
387  * Render
388  *****************************************************************************/
389 static void Render(vout_thread_t *vout, picture_t *picture)
390 {
391     vout_sys_t *sys = vout->p_sys;
392     vout_display_t *vd = sys->vd;
393
394     assert(sys->use_dr || !picture->p_sys->direct);
395
396     if (sys->use_dr) {
397         assert(!vout_IsDisplayFiltered(vd));
398         assert(picture->p_sys->direct);
399
400         vout_display_Prepare(vd, picture->p_sys->direct);
401     } else {
402         picture_t *filtered = vout_FilterDisplay(vd, picture);
403         if (filtered) {
404             picture_t *direct = picture->p_sys->direct = vout_display_Get(vd);
405             if (direct) {
406                 picture_Copy(direct, filtered);
407                 vout_display_Prepare(vd, direct);
408             }
409             picture_Release(filtered);
410         }
411     }
412 }
413
414 /*****************************************************************************
415  *
416  *****************************************************************************/
417 static void Display(vout_thread_t *vout, picture_t *picture)
418 {
419     vout_sys_t *sys = vout->p_sys;
420     vout_display_t *vd = sys->vd;
421
422     picture_t *direct = picture->p_sys->direct;
423     if (!direct)
424         return;
425
426     /* XXX This is a hack that will work with current vout_display_t modules */
427     if (sys->use_dr)
428         picture_Hold(direct);
429
430      vout_display_Display(vd, direct);
431
432      if (!sys->use_dr)
433          picture->p_sys->direct = NULL;
434 }
435
436 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
437 {
438     /* Load configuration */
439     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
440     cfg->display.title = title;
441     cfg->display.width   = var_CreateGetInteger(vout, "width");
442     cfg->display.height  = var_CreateGetInteger(vout, "height");
443     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
444     cfg->display.sar.num = 1; /* TODO monitor AR */
445     cfg->display.sar.den = 1;
446     unsigned zoom_den = 1000;
447     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
448     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
449     cfg->zoom.num = zoom_num;
450     cfg->zoom.den = zoom_den;
451     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
452     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
453     const int align_mask = var_CreateGetInteger(vout, "align");
454     if (align_mask & 0x1)
455         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
456     else if (align_mask & 0x2)
457         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
458     if (align_mask & 0x4)
459         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
460     else if (align_mask & 0x8)
461         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
462 }
463