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