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