1 /*****************************************************************************
2 * vout_display.c: "vout display" -> "video output" wrapper
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_vout_wrapper.h>
37 #include "vout_internal.h"
40 /*****************************************************************************
42 *****************************************************************************/
49 struct picture_sys_t {
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Init (vout_thread_t *);
57 static void End (vout_thread_t *);
58 static int Manage (vout_thread_t *);
59 static void Render (vout_thread_t *, picture_t *);
60 static void Display(vout_thread_t *, picture_t *);
62 static void VoutGetDisplayCfg(vout_thread_t *,
63 vout_display_cfg_t *, const char *title);
65 static int Forward(vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void *);
69 /*****************************************************************************
71 *****************************************************************************/
72 int vout_OpenWrapper(vout_thread_t *vout, const char *name)
76 msg_Dbg(vout, "Opening vout display wrapper");
79 sys = malloc(sizeof(*sys));
83 sys->title = var_CreateGetNonEmptyString(vout, "video-title");
86 video_format_t source = vout->fmt_render;
87 source.i_visible_width = source.i_width;
88 source.i_visible_height = source.i_height;
89 source.i_x_offset = 0;
90 source.i_y_offset = 0;
92 vout_display_state_t state;
93 VoutGetDisplayCfg(vout, &state.cfg, sys->title);
94 state.is_on_top = var_CreateGetBool(vout, "video-on-top");
98 const mtime_t double_click_timeout = 300000;
99 const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
101 sys->vd = vout_NewDisplay(vout, &source, &state, name ? name : "$vout",
102 double_click_timeout, hide_timeout);
103 /* If we need to video filter and it fails, then try a splitter
104 * XXX it is a hack for now FIXME */
105 if (name && !sys->vd)
106 sys->vd = vout_NewSplitter(vout, &source, &state, "$vout", name,
107 double_click_timeout, hide_timeout);
116 var_Create(vout, "direct3d-desktop", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
117 var_AddCallback(vout, "direct3d-desktop", Forward, NULL);
118 var_Create(vout, "video-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
119 var_AddCallback(vout, "video-wallpaper", Forward, NULL);
123 vout->pf_init = Init;
125 vout->pf_manage = Manage;
126 vout->pf_render = Render;
127 vout->pf_display = Display;
133 /*****************************************************************************
135 *****************************************************************************/
136 void vout_CloseWrapper(vout_thread_t *vout)
138 vout_sys_t *sys = vout->p_sys;
141 var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
142 var_DelCallback(vout, "video-wallpaper", Forward, NULL);
144 vout_DeleteDisplay(sys->vd, NULL);
149 /*****************************************************************************
151 *****************************************************************************/
152 static int Init(vout_thread_t *vout)
154 vout_sys_t *sys = vout->p_sys;
155 vout_display_t *vd = sys->vd;
158 video_format_t source = vd->source;
160 vout->output.i_chroma = source.i_chroma;
161 vout->output.i_width = source.i_width;
162 vout->output.i_height = source.i_height;
163 vout->output.i_aspect = (int64_t)source.i_sar_num * source.i_width * VOUT_ASPECT_FACTOR / source.i_sar_den / source.i_height;
164 vout->output.i_rmask = source.i_rmask;
165 vout->output.i_gmask = source.i_gmask;
166 vout->output.i_bmask = source.i_bmask;
167 vout->output.pf_setpalette = NULL; /* FIXME What to do ? Seems unused anyway */
169 /* also set fmt_out (completly broken API) */
170 vout->fmt_out.i_chroma = vout->output.i_chroma;
171 vout->fmt_out.i_width =
172 vout->fmt_out.i_visible_width = vout->output.i_width;
173 vout->fmt_out.i_height =
174 vout->fmt_out.i_visible_height = vout->output.i_height;
175 vout->fmt_out.i_sar_num = vout->output.i_aspect * vout->output.i_height;
176 vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR * vout->output.i_width;
177 vout->fmt_out.i_x_offset = 0;
178 vout->fmt_out.i_y_offset = 0;
180 if (vout->fmt_in.i_visible_width != source.i_visible_width ||
181 vout->fmt_in.i_visible_height != source.i_visible_height ||
182 vout->fmt_in.i_x_offset != source.i_x_offset ||
183 vout->fmt_in.i_y_offset != source.i_y_offset )
184 vout->i_changes |= VOUT_CROP_CHANGE;
187 vout_SetWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
189 /* XXX For non dr case, the current vout implementation force us to
190 * create at most 1 direct picture (otherwise the buffers will be kept
191 * referenced even through the Init/End.
193 sys->use_dr = !vout_IsDisplayFiltered(vd);
194 const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
195 const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
196 for (vout->output.i_pictures = 0;
197 vout->output.i_pictures < picture_max;
198 vout->output.i_pictures++) {
199 /* Find an empty picture slot */
200 picture_t *picture = NULL;
201 for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
202 if (vout->p_picture[index].i_status == FREE_PICTURE) {
203 picture = &vout->p_picture[index];
209 memset(picture, 0, sizeof(*picture));
211 picture->p_sys = malloc(sizeof(*picture->p_sys));
214 picture_pool_t *pool = vout_display_Pool(vd, picture_max);
217 picture_t *direct = picture_pool_Get(pool);
220 picture->format = direct->format;
221 picture->i_planes = direct->i_planes;
222 for (int i = 0; i < direct->i_planes; i++)
223 picture->p[i] = direct->p[i];
224 picture->b_slow = vd->info.is_slow;
226 picture->p_sys->direct = direct;
228 vout_AllocatePicture(VLC_OBJECT(vd), picture,
230 vd->source.i_width, vd->source.i_height,
231 vd->source.i_sar_num, vd->source.i_sar_den);
232 if (!picture->i_planes)
234 picture->p_sys->direct = NULL;
236 picture->i_status = DESTROYED_PICTURE;
237 picture->i_type = DIRECT_PICTURE;
239 vout->output.pp_picture[vout->output.i_pictures] = picture;
244 /*****************************************************************************
246 *****************************************************************************/
247 static void End(vout_thread_t *vout)
249 vout_sys_t *sys = vout->p_sys;
251 for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
252 picture_t *picture = &vout->p_picture[i];
254 if (picture->i_type != DIRECT_PICTURE)
257 if (picture->p_sys->direct)
258 picture_Release(picture->p_sys->direct);
260 free(picture->p_data_orig);
261 free(picture->p_sys);
263 picture->i_status = FREE_PICTURE;
265 if (sys->use_dr && vout_AreDisplayPicturesInvalid(sys->vd))
266 vout_ManageDisplay(sys->vd, true);
269 /*****************************************************************************
271 *****************************************************************************/
272 static int Manage(vout_thread_t *vout)
274 vout_sys_t *sys = vout->p_sys;
275 vout_display_t *vd = sys->vd;
277 while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
284 if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
285 vout->b_fullscreen = !vout->b_fullscreen;
287 var_SetBool(vout, "fullscreen", vout->b_fullscreen);
288 vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
289 vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
291 if (vout->i_changes & VOUT_ASPECT_CHANGE) {
292 vout->output.i_aspect = (int64_t)vout->fmt_in.i_sar_num * vout->fmt_in.i_width * VOUT_ASPECT_FACTOR /
293 vout->fmt_in.i_sar_den / vout->fmt_in.i_height;
294 vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
295 vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
297 vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
299 vout->i_changes &= ~VOUT_ASPECT_CHANGE;
301 if (vout->i_changes & VOUT_ZOOM_CHANGE) {
302 const float zoom = var_GetFloat(vout, "scale");
304 unsigned den = ZOOM_FP_FACTOR;
305 unsigned num = den * zoom;
306 if (num < (ZOOM_FP_FACTOR+9) / 10)
307 num = (ZOOM_FP_FACTOR+9) / 10;
308 else if (num > ZOOM_FP_FACTOR * 10)
309 num = ZOOM_FP_FACTOR * 10;
311 vout_SetDisplayZoom(vd, num, den);
313 vout->i_changes &= ~VOUT_ZOOM_CHANGE;
315 if (vout->i_changes & VOUT_SCALE_CHANGE) {
316 const bool is_display_filled = var_GetBool(vout, "autoscale");
318 vout_SetDisplayFilled(vd, is_display_filled);
320 vout->i_changes &= ~VOUT_SCALE_CHANGE;
322 if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
323 vout_SetWindowState(vd, vout->b_on_top
324 ? VOUT_WINDOW_STATE_ABOVE
325 : VOUT_WINDOW_STATE_NORMAL);
327 vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
329 if (vout->i_changes & VOUT_CROP_CHANGE) {
330 const video_format_t crop = vout->fmt_in;
331 const video_format_t org = vout->fmt_render;
332 /* FIXME because of rounding errors, the reconstructed ratio is wrong */
335 if (crop.i_x_offset == org.i_x_offset &&
336 crop.i_visible_width == org.i_visible_width &&
337 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
338 vlc_ureduce(&num, &den,
339 crop.i_visible_width * crop.i_sar_num,
340 crop.i_visible_height * crop.i_sar_den, 0);
341 } else if (crop.i_y_offset == org.i_y_offset &&
342 crop.i_visible_height == org.i_visible_height &&
343 crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
344 vlc_ureduce(&num, &den,
345 crop.i_visible_width * crop.i_sar_num,
346 crop.i_visible_height * crop.i_sar_den, 0);
348 vout_SetDisplayCrop(vd, num, den,
349 crop.i_x_offset, crop.i_y_offset,
350 crop.i_visible_width, crop.i_visible_height);
351 vout->i_changes &= ~VOUT_CROP_CHANGE;
356 if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
357 vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
359 vout_ManageDisplay(vd, !sys->use_dr);
363 /*****************************************************************************
365 *****************************************************************************/
366 static void Render(vout_thread_t *vout, picture_t *picture)
368 vout_sys_t *sys = vout->p_sys;
369 vout_display_t *vd = sys->vd;
371 assert(sys->use_dr || !picture->p_sys->direct);
372 assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
375 assert(picture->p_sys->direct);
376 vout_display_Prepare(vd, picture->p_sys->direct);
378 picture_t *direct = picture->p_sys->direct = vout_FilterDisplay(vd, picture);
380 vout_display_Prepare(vd, direct);
385 /*****************************************************************************
387 *****************************************************************************/
388 static void Display(vout_thread_t *vout, picture_t *picture)
390 vout_sys_t *sys = vout->p_sys;
391 vout_display_t *vd = sys->vd;
393 picture_t *direct = picture->p_sys->direct;
397 /* XXX This is a hack that will work with current vout_display_t modules */
399 picture_Hold(direct);
401 vout_display_Display(vd, direct);
404 for (int i = 0; i < picture->i_planes; i++) {
405 picture->p[i].p_pixels = direct->p[i].p_pixels;
406 picture->p[i].i_pitch = direct->p[i].i_pitch;
407 picture->p[i].i_lines = direct->p[i].i_lines;
410 picture->p_sys->direct = NULL;
414 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
416 /* Load configuration */
417 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
418 cfg->display.title = title;
419 const int display_width = var_CreateGetInteger(vout, "width");
420 const int display_height = var_CreateGetInteger(vout, "height");
421 cfg->display.width = display_width > 0 ? display_width : 0;
422 cfg->display.height = display_height > 0 ? display_height : 0;
423 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
424 cfg->display.sar.num = 1; /* TODO monitor AR */
425 cfg->display.sar.den = 1;
426 unsigned zoom_den = 1000;
427 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
428 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
429 cfg->zoom.num = zoom_num;
430 cfg->zoom.den = zoom_den;
431 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
432 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
433 const int align_mask = var_CreateGetInteger(vout, "align");
434 if (align_mask & 0x1)
435 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
436 else if (align_mask & 0x2)
437 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
438 if (align_mask & 0x4)
439 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
440 else if (align_mask & 0x8)
441 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
445 static int Forward(vlc_object_t *object, char const *var,
446 vlc_value_t oldval, vlc_value_t newval, void *data)
448 vout_thread_t *vout = (vout_thread_t*)object;
452 return var_Set(vout->p_sys->vd, var, newval);