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