]> git.sesse.net Git - vlc/blob - modules/video_output/wrapper.c
6243893f147a13be348398b712613440779d6f62
[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_x11);
52 DECLARE_OPEN(xcb_xv);
53 DECLARE_OPEN(xcb_glx);
54 DECLARE_OPEN(dummy);
55 DECLARE_OPEN(fb);
56 DECLARE_OPEN(directfb);
57 DECLARE_OPEN(yuv);
58 DECLARE_OPEN(snapshot);
59 DECLARE_OPEN(vmem);
60 DECLARE_OPEN(direct3d_xp);
61 DECLARE_OPEN(direct3d_vista);
62 DECLARE_OPEN(glwin32);
63
64 #undef DECLARE_OPEN
65
66 #define DECLARE_MODULE_EXT(name, module, priority)      \
67     set_description( "Video display "#module" wrapper" )  \
68     set_shortname( "Video display "#module" wrapper" )    \
69     set_capability( "video output", priority )          \
70     set_callbacks( Open##module, Close )                \
71     add_shortcut( #name )
72
73 #define DECLARE_MODULE(name, priority)                  \
74     DECLARE_MODULE_EXT(name, name, priority)
75
76 vlc_module_begin()
77     set_category( CAT_VIDEO )
78     set_subcategory( SUBCAT_VIDEO_VOUT )
79
80     DECLARE_MODULE(aalib, 10)
81
82     add_submodule()
83     DECLARE_MODULE(caca, 12)
84
85     add_submodule()
86     DECLARE_MODULE(sdl, 60)
87
88     add_submodule()
89     DECLARE_MODULE(xcb_x11, 75)
90
91     add_submodule()
92     DECLARE_MODULE(xcb_xv, 155)
93
94     add_submodule()
95     DECLARE_MODULE(xcb_glx, 20)
96
97     add_submodule()
98     DECLARE_MODULE(dummy, 1)
99
100     add_submodule()
101     DECLARE_MODULE(fb, 30)
102
103     add_submodule()
104     DECLARE_MODULE(directfb, 60)
105
106     add_submodule()
107     DECLARE_MODULE(yuv, 0)
108
109     add_submodule()
110     DECLARE_MODULE(snapshot, 0)
111
112     add_submodule()
113     DECLARE_MODULE(vmem, 0)
114
115     add_submodule()
116     DECLARE_MODULE_EXT(direct3d, direct3d_vista, 150)
117
118     add_submodule()
119     DECLARE_MODULE_EXT(direct3d, direct3d_xp, 70)
120
121     add_submodule()
122     DECLARE_MODULE(glwin32, 20)
123
124 vlc_module_end()
125
126 #undef DECLARE_MODULE
127
128 /*****************************************************************************
129  *
130  *****************************************************************************/
131 struct vout_sys_t {
132     const char     *name;
133     char           *title;
134     vout_display_t *vd;
135     bool           use_dr;
136 };
137
138 struct picture_sys_t {
139     picture_t *direct;
140 };
141
142 /*****************************************************************************
143  * Local prototypes
144  *****************************************************************************/
145 static int  Init   (vout_thread_t *);
146 static void End    (vout_thread_t *);
147 static int  Manage (vout_thread_t *);
148 static void Render (vout_thread_t *, picture_t *);
149 static void Display(vout_thread_t *, picture_t *);
150
151 static void VoutGetDisplayCfg(vout_thread_t *,
152                               vout_display_cfg_t *, const char *title);
153
154 static int  Forward(vlc_object_t *, char const *,
155                     vlc_value_t, vlc_value_t, void *);
156
157
158 /*****************************************************************************
159  *
160  *****************************************************************************/
161 static int Open(vlc_object_t *object, const char *module)
162 {
163     vout_thread_t *vout = (vout_thread_t *)object;
164     vout_sys_t *sys;
165
166     msg_Warn(vout, "Opening vout display wrapper");
167
168     /* */
169     sys = malloc(sizeof(*sys));
170     if (!sys)
171         return VLC_ENOMEM;
172
173     sys->name = module;
174     sys->title = var_CreateGetNonEmptyString(vout, "video-title");
175
176     /* */
177     video_format_t source   = vout->fmt_render;
178     source.i_visible_width  = source.i_width;
179     source.i_visible_height = source.i_height;
180     source.i_x_offset       = 0;
181     source.i_y_offset       = 0;
182
183     vout_display_state_t state;
184     VoutGetDisplayCfg(vout, &state.cfg, sys->title);
185     state.is_on_top = var_CreateGetBool(vout, "video-on-top");
186     state.sar.num = 0;
187     state.sar.den = 0;
188
189     const mtime_t double_click_timeout = 300000;
190     const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
191
192     sys->vd = vout_NewDisplay(vout, &source, &state, module,
193                               double_click_timeout, hide_timeout);
194     if (!sys->vd) {
195         free(sys->title);
196         free(sys);
197         return VLC_EGENERIC;
198     }
199
200     /* */
201     if (!strcmp(sys->name, "direct3d_xp") || !strcmp(sys->name, "direct3d_vista")) {
202         var_Create(vout, "direct3d-desktop", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
203         var_AddCallback(vout, "direct3d-desktop", Forward, NULL);
204     }
205
206     /* */
207     vout->pf_init    = Init;
208     vout->pf_end     = End;
209     vout->pf_manage  = Manage;
210     vout->pf_render  = Render;
211     vout->pf_display = Display;
212     vout->pf_control = NULL;
213     vout->p_sys      = sys;
214
215     return VLC_SUCCESS;
216 }
217
218 /*****************************************************************************
219  *
220  *****************************************************************************/
221 static void Close(vlc_object_t *object)
222 {
223     vout_thread_t *vout = (vout_thread_t *)object;
224     vout_sys_t *sys = vout->p_sys;
225
226     /* */
227     if (!strcmp(sys->name, "direct3d_xp") || !strcmp(sys->name, "direct3d_vista")) {
228         var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
229     }
230
231     vout_DeleteDisplay(sys->vd, NULL);
232     free(sys->title);
233     free(sys );
234 }
235
236 /*****************************************************************************
237  *
238  *****************************************************************************/
239 static int Init(vout_thread_t *vout)
240 {
241     vout_sys_t *sys = vout->p_sys;
242     vout_display_t *vd = sys->vd;
243
244     /* */
245     video_format_t source = vd->source;
246
247     vout->output.i_chroma = source.i_chroma;
248     vout->output.i_width  = source.i_width;
249     vout->output.i_height = source.i_height;
250     vout->output.i_aspect = source.i_aspect;
251     vout->output.i_rmask  = source.i_rmask;
252     vout->output.i_gmask  = source.i_gmask;
253     vout->output.i_bmask  = source.i_bmask;
254     vout->output.pf_setpalette = NULL; /* FIXME What to do ? Seems unused anyway */
255
256     /* also set fmt_out (completly broken API) */
257     vout->fmt_out.i_chroma         = vout->output.i_chroma;
258     vout->fmt_out.i_width          =
259     vout->fmt_out.i_visible_width  = vout->output.i_width;
260     vout->fmt_out.i_height         =
261     vout->fmt_out.i_visible_height = vout->output.i_height;
262     vout->fmt_out.i_aspect         = vout->output.i_aspect;
263     vout->fmt_out.i_x_offset       = 0;
264     vout->fmt_out.i_y_offset       = 0;
265
266     /* TODO */
267 #if 0
268     if (p_vout->fmt_render.i_visible_width  != source.i_visible_width ||
269         p_vout->fmt_render.i_visible_height != source.i_visible_height ||
270         p_vout->fmt_render.i_x_offset != source.i_x_offset ||
271         p_vout->fmt_render.i_y_offset != source.i_y_offset )
272     {
273         p_vout->i_changes |= VOUT_CROP_CHANGE;
274     }
275 #endif
276     if (vout->b_on_top)
277         vout_SetDisplayOnTop(vd, true);
278
279     /* XXX For non dr case, the current vout implementation force us to
280      * create at most 1 direct picture (otherwise the buffers will be kept
281      * referenced even through the Init/End.
282      */
283     sys->use_dr = !vout_IsDisplayFiltered(vd);
284     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
285     const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
286     for (vout->output.i_pictures = 0;
287             vout->output.i_pictures < picture_max;
288                 vout->output.i_pictures++) {
289         /* Find an empty picture slot */
290         picture_t *picture = NULL;
291         for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
292             if (vout->p_picture[index].i_status == FREE_PICTURE) {
293                 picture = &vout->p_picture[index];
294                 break;
295             }
296         }
297         if (!picture)
298             break;
299         memset(picture, 0, sizeof(*picture));
300
301         picture->p_sys = malloc(sizeof(*picture->p_sys));
302
303         if (sys->use_dr) {
304             picture_pool_t *pool = vout_display_Pool(vd, picture_max);
305             if (!pool)
306                 break;
307             picture_t *direct = picture_pool_Get(pool);
308             if (!direct)
309                 break;
310             picture->format = direct->format;
311             picture->i_planes = direct->i_planes;
312             for (int i = 0; i < direct->i_planes; i++)
313                 picture->p[i] = direct->p[i];
314             picture->b_slow = vd->info.is_slow;
315
316             picture->p_sys->direct = direct;
317         } else {
318             vout_AllocatePicture(VLC_OBJECT(vd), picture,
319                                  vd->source.i_chroma,
320                                  vd->source.i_width, vd->source.i_height,
321                                  vd->source.i_aspect);
322             if (!picture->i_planes)
323                 break;
324             picture->p_sys->direct = NULL;
325         }
326         picture->i_status = DESTROYED_PICTURE;
327         picture->i_type    = DIRECT_PICTURE;
328
329         vout->output.pp_picture[vout->output.i_pictures] = picture;
330     }
331     return VLC_SUCCESS;
332 }
333
334 /*****************************************************************************
335  *
336  *****************************************************************************/
337 static void End(vout_thread_t *vout)
338 {
339     vout_sys_t *sys = vout->p_sys;
340
341     for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
342         picture_t *picture = &vout->p_picture[i];
343
344         if (picture->i_type != DIRECT_PICTURE)
345             continue;
346
347         if (picture->p_sys->direct)
348             picture_Release(picture->p_sys->direct);
349         if (!sys->use_dr)
350             free(picture->p_data_orig);
351         free(picture->p_sys);
352
353         picture->i_status = FREE_PICTURE;
354     }
355     if (sys->use_dr && vout_AreDisplayPicturesInvalid(sys->vd))
356         vout_ManageDisplay(sys->vd, true);
357 }
358
359 /*****************************************************************************
360  *
361  *****************************************************************************/
362 static int Manage(vout_thread_t *vout)
363 {
364     vout_sys_t *sys = vout->p_sys;
365     vout_display_t *vd = sys->vd;
366
367     while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
368                               VOUT_ASPECT_CHANGE |
369                               VOUT_ZOOM_CHANGE |
370                               VOUT_SCALE_CHANGE |
371                               VOUT_ON_TOP_CHANGE |
372                               VOUT_CROP_CHANGE)) {
373         /* */
374         if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
375             vout->b_fullscreen = !vout->b_fullscreen;
376
377             var_SetBool(vout, "fullscreen", vout->b_fullscreen);
378             vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
379             vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
380         }
381         if (vout->i_changes & VOUT_ASPECT_CHANGE) {
382             vout->output.i_aspect   =
383             vout->fmt_out.i_aspect  = vout->fmt_in.i_aspect;
384             vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
385             vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
386
387             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
388
389             vout->i_changes &= ~VOUT_ASPECT_CHANGE;
390         }
391         if (vout->i_changes & VOUT_ZOOM_CHANGE) {
392             const float zoom = var_GetFloat(vout, "scale");
393
394             unsigned den = ZOOM_FP_FACTOR;
395             unsigned num = den * zoom;
396             if (num < (ZOOM_FP_FACTOR+9) / 10)
397                 num = (ZOOM_FP_FACTOR+9) / 10;
398             else if (num > ZOOM_FP_FACTOR * 10)
399                 num = ZOOM_FP_FACTOR * 10;
400
401             vout_SetDisplayZoom(vd, num, den);
402
403             vout->i_changes &= ~VOUT_ZOOM_CHANGE;
404         }
405         if (vout->i_changes & VOUT_SCALE_CHANGE) {
406             const bool is_display_filled = var_GetBool(vout, "autoscale");
407
408             vout_SetDisplayFilled(vd, is_display_filled);
409
410             vout->i_changes &= ~VOUT_SCALE_CHANGE;
411         }
412         if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
413             vout_SetDisplayOnTop(vd, vout->b_on_top);
414
415             vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
416         }
417         if (vout->i_changes & VOUT_CROP_CHANGE) {
418             const video_format_t crop = vout->fmt_in;
419             const video_format_t org = vout->fmt_render;
420             /* FIXME because of rounding errors, the reconstructed ratio is wrong */
421             unsigned num = 0;
422             unsigned den = 0;
423             if (crop.i_x_offset == org.i_x_offset &&
424                 crop.i_visible_width == org.i_visible_width &&
425                 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
426                 vlc_ureduce(&num, &den,
427                             crop.i_visible_width * crop.i_sar_num,
428                             crop.i_visible_height * crop.i_sar_den, 0);
429             } else if (crop.i_y_offset == org.i_y_offset &&
430                        crop.i_visible_height == org.i_visible_height &&
431                        crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
432                 vlc_ureduce(&num, &den,
433                             crop.i_visible_width * crop.i_sar_num,
434                             crop.i_visible_height * crop.i_sar_den, 0);
435             }
436             vout_SetDisplayCrop(vd, num, den,
437                                 crop.i_x_offset, crop.i_y_offset,
438                                 crop.i_visible_width, crop.i_visible_height);
439             vout->i_changes &= ~VOUT_CROP_CHANGE;
440         }
441
442     }
443
444     if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
445         vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
446     }
447     vout_ManageDisplay(vd, !sys->use_dr);
448     return VLC_SUCCESS;
449 }
450
451 /*****************************************************************************
452  * Render
453  *****************************************************************************/
454 static void Render(vout_thread_t *vout, picture_t *picture)
455 {
456     vout_sys_t *sys = vout->p_sys;
457     vout_display_t *vd = sys->vd;
458
459     assert(sys->use_dr || !picture->p_sys->direct);
460     assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
461
462     if (sys->use_dr) {
463         assert(picture->p_sys->direct);
464         vout_display_Prepare(vd, picture->p_sys->direct);
465     } else {
466         picture_t *direct = picture->p_sys->direct = vout_FilterDisplay(vd, picture);
467         if (direct) {
468             vout_display_Prepare(vd, direct);
469         }
470     }
471 }
472
473 /*****************************************************************************
474  *
475  *****************************************************************************/
476 static void Display(vout_thread_t *vout, picture_t *picture)
477 {
478     vout_sys_t *sys = vout->p_sys;
479     vout_display_t *vd = sys->vd;
480
481     picture_t *direct = picture->p_sys->direct;
482     if (!direct)
483         return;
484
485     /* XXX This is a hack that will work with current vout_display_t modules */
486     if (sys->use_dr)
487         picture_Hold(direct);
488
489      vout_display_Display(vd, direct);
490
491      if (!sys->use_dr)
492          picture->p_sys->direct = NULL;
493 }
494
495 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
496 {
497     /* Load configuration */
498     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
499     cfg->display.title = title;
500     const int display_width = var_CreateGetInteger(vout, "width");
501     const int display_height = var_CreateGetInteger(vout, "height");
502     cfg->display.width   = display_width > 0  ? display_width  : 0;
503     cfg->display.height  = display_height > 0 ? display_height : 0;
504     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
505     cfg->display.sar.num = 1; /* TODO monitor AR */
506     cfg->display.sar.den = 1;
507     unsigned zoom_den = 1000;
508     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
509     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
510     cfg->zoom.num = zoom_num;
511     cfg->zoom.den = zoom_den;
512     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
513     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
514     const int align_mask = var_CreateGetInteger(vout, "align");
515     if (align_mask & 0x1)
516         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
517     else if (align_mask & 0x2)
518         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
519     if (align_mask & 0x4)
520         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
521     else if (align_mask & 0x8)
522         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
523 }
524
525 static int Forward(vlc_object_t *object, char const *var,
526                    vlc_value_t oldval, vlc_value_t newval, void *data)
527 {
528     vout_thread_t *vout = (vout_thread_t*)object;
529
530     VLC_UNUSED(oldval);
531     VLC_UNUSED(data);
532     return var_Set(vout->p_sys->vd, var, newval);
533 }
534