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