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 void VoutGetDisplayCfg(vout_thread_t *,
57 vout_display_cfg_t *, const char *title);
59 static int Forward(vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void *);
63 /*****************************************************************************
65 *****************************************************************************/
66 int vout_OpenWrapper(vout_thread_t *vout, const char *name)
70 msg_Dbg(vout, "Opening vout display wrapper");
73 sys = malloc(sizeof(*sys));
77 sys->title = var_CreateGetNonEmptyString(vout, "video-title");
80 video_format_t source = vout->fmt_render;
81 source.i_visible_width = source.i_width;
82 source.i_visible_height = source.i_height;
83 source.i_x_offset = 0;
84 source.i_y_offset = 0;
86 vout_display_state_t state;
87 VoutGetDisplayCfg(vout, &state.cfg, sys->title);
88 state.is_on_top = var_CreateGetBool(vout, "video-on-top");
92 const mtime_t double_click_timeout = 300000;
93 const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
95 sys->vd = vout_NewDisplay(vout, &source, &state, name ? name : "$vout",
96 double_click_timeout, hide_timeout);
97 /* If we need to video filter and it fails, then try a splitter
98 * XXX it is a hack for now FIXME */
100 sys->vd = vout_NewSplitter(vout, &source, &state, "$vout", name,
101 double_click_timeout, hide_timeout);
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);
117 vout->p->p_sys = sys;
122 /*****************************************************************************
124 *****************************************************************************/
125 void vout_CloseWrapper(vout_thread_t *vout)
127 vout_sys_t *sys = vout->p->p_sys;
130 var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
131 var_DelCallback(vout, "video-wallpaper", Forward, NULL);
133 vout_DeleteDisplay(sys->vd, NULL);
138 /*****************************************************************************
140 *****************************************************************************/
141 int vout_InitWrapper(vout_thread_t *vout)
143 vout_sys_t *sys = vout->p->p_sys;
144 vout_display_t *vd = sys->vd;
147 video_format_t source = vd->source;
149 vout->output.i_chroma = source.i_chroma;
150 vout->output.i_width = source.i_width;
151 vout->output.i_height = source.i_height;
152 vout->output.i_aspect = (int64_t)source.i_sar_num * source.i_width * VOUT_ASPECT_FACTOR / source.i_sar_den / source.i_height;
154 /* also set fmt_out (completly broken API) */
155 vout->fmt_out.i_chroma = vout->output.i_chroma;
156 vout->fmt_out.i_width =
157 vout->fmt_out.i_visible_width = vout->output.i_width;
158 vout->fmt_out.i_height =
159 vout->fmt_out.i_visible_height = vout->output.i_height;
160 vout->fmt_out.i_sar_num = vout->output.i_aspect * vout->output.i_height;
161 vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR * vout->output.i_width;
162 vout->fmt_out.i_x_offset = 0;
163 vout->fmt_out.i_y_offset = 0;
164 vout->fmt_out.i_rmask = source.i_rmask;
165 vout->fmt_out.i_gmask = source.i_gmask;
166 vout->fmt_out.i_bmask = source.i_bmask;
168 if (vout->fmt_in.i_visible_width != source.i_visible_width ||
169 vout->fmt_in.i_visible_height != source.i_visible_height ||
170 vout->fmt_in.i_x_offset != source.i_x_offset ||
171 vout->fmt_in.i_y_offset != source.i_y_offset )
172 vout->i_changes |= VOUT_CROP_CHANGE;
175 vout_SetWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
177 /* XXX For non dr case, the current vout implementation force us to
178 * create at most 1 direct picture (otherwise the buffers will be kept
179 * referenced even through the Init/End.
181 sys->use_dr = !vout_IsDisplayFiltered(vd);
182 const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
183 const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
184 for (vout->output.i_pictures = 0;
185 vout->output.i_pictures < picture_max;
186 vout->output.i_pictures++) {
187 /* Find an empty picture slot */
188 picture_t *picture = NULL;
189 for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
190 if (vout->p_picture[index].i_status == FREE_PICTURE) {
191 picture = &vout->p_picture[index];
197 memset(picture, 0, sizeof(*picture));
199 picture->p_sys = malloc(sizeof(*picture->p_sys));
202 picture_pool_t *pool = vout_display_Pool(vd, picture_max);
205 picture_t *direct = picture_pool_Get(pool);
208 picture->format = direct->format;
209 picture->i_planes = direct->i_planes;
210 for (int i = 0; i < direct->i_planes; i++)
211 picture->p[i] = direct->p[i];
212 picture->b_slow = vd->info.is_slow;
214 picture->p_sys->direct = direct;
216 vout_AllocatePicture(VLC_OBJECT(vd), picture,
218 vd->source.i_width, vd->source.i_height,
219 vd->source.i_sar_num, vd->source.i_sar_den);
220 if (!picture->i_planes)
222 picture->p_sys->direct = NULL;
224 picture->i_status = DESTROYED_PICTURE;
225 picture->i_type = DIRECT_PICTURE;
227 vout->output.pp_picture[vout->output.i_pictures] = picture;
232 /*****************************************************************************
234 *****************************************************************************/
235 void vout_EndWrapper(vout_thread_t *vout)
237 vout_sys_t *sys = vout->p->p_sys;
239 for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
240 picture_t *picture = &vout->p_picture[i];
242 if (picture->i_type != DIRECT_PICTURE)
245 if (picture->p_sys->direct)
246 picture_Release(picture->p_sys->direct);
248 free(picture->p_data_orig);
249 free(picture->p_sys);
251 picture->i_status = FREE_PICTURE;
253 if (sys->use_dr && vout_AreDisplayPicturesInvalid(sys->vd))
254 vout_ManageDisplay(sys->vd, true);
257 /*****************************************************************************
259 *****************************************************************************/
260 int vout_ManageWrapper(vout_thread_t *vout)
262 vout_sys_t *sys = vout->p->p_sys;
263 vout_display_t *vd = sys->vd;
265 while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
272 if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
273 vout->b_fullscreen = !vout->b_fullscreen;
275 var_SetBool(vout, "fullscreen", vout->b_fullscreen);
276 vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
277 vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
279 if (vout->i_changes & VOUT_ASPECT_CHANGE) {
280 vout->output.i_aspect = (int64_t)vout->fmt_in.i_sar_num * vout->fmt_in.i_width * VOUT_ASPECT_FACTOR /
281 vout->fmt_in.i_sar_den / vout->fmt_in.i_height;
282 vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
283 vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
285 vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
287 vout->i_changes &= ~VOUT_ASPECT_CHANGE;
289 if (vout->i_changes & VOUT_ZOOM_CHANGE) {
290 const float zoom = var_GetFloat(vout, "scale");
292 unsigned den = ZOOM_FP_FACTOR;
293 unsigned num = den * zoom;
294 if (num < (ZOOM_FP_FACTOR+9) / 10)
295 num = (ZOOM_FP_FACTOR+9) / 10;
296 else if (num > ZOOM_FP_FACTOR * 10)
297 num = ZOOM_FP_FACTOR * 10;
299 vout_SetDisplayZoom(vd, num, den);
301 vout->i_changes &= ~VOUT_ZOOM_CHANGE;
303 if (vout->i_changes & VOUT_SCALE_CHANGE) {
304 const bool is_display_filled = var_GetBool(vout, "autoscale");
306 vout_SetDisplayFilled(vd, is_display_filled);
308 vout->i_changes &= ~VOUT_SCALE_CHANGE;
310 if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
311 vout_SetWindowState(vd, vout->b_on_top
312 ? VOUT_WINDOW_STATE_ABOVE
313 : VOUT_WINDOW_STATE_NORMAL);
315 vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
317 if (vout->i_changes & VOUT_CROP_CHANGE) {
318 const video_format_t crop = vout->fmt_in;
319 const video_format_t org = vout->fmt_render;
320 /* FIXME because of rounding errors, the reconstructed ratio is wrong */
323 if (crop.i_x_offset == org.i_x_offset &&
324 crop.i_visible_width == org.i_visible_width &&
325 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
326 vlc_ureduce(&num, &den,
327 crop.i_visible_width * crop.i_sar_num,
328 crop.i_visible_height * crop.i_sar_den, 0);
329 } else if (crop.i_y_offset == org.i_y_offset &&
330 crop.i_visible_height == org.i_visible_height &&
331 crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
332 vlc_ureduce(&num, &den,
333 crop.i_visible_width * crop.i_sar_num,
334 crop.i_visible_height * crop.i_sar_den, 0);
336 vout_SetDisplayCrop(vd, num, den,
337 crop.i_x_offset, crop.i_y_offset,
338 crop.i_visible_width, crop.i_visible_height);
339 vout->i_changes &= ~VOUT_CROP_CHANGE;
344 if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
345 vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
347 vout_ManageDisplay(vd, !sys->use_dr);
351 /*****************************************************************************
353 *****************************************************************************/
354 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
356 vout_sys_t *sys = vout->p->p_sys;
357 vout_display_t *vd = sys->vd;
359 assert(sys->use_dr || !picture->p_sys->direct);
360 assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
363 assert(picture->p_sys->direct);
364 vout_display_Prepare(vd, picture->p_sys->direct);
366 picture_t *direct = picture->p_sys->direct = vout_FilterDisplay(vd, picture);
368 vout_display_Prepare(vd, direct);
373 /*****************************************************************************
375 *****************************************************************************/
376 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
378 vout_sys_t *sys = vout->p->p_sys;
379 vout_display_t *vd = sys->vd;
381 picture_t *direct = picture->p_sys->direct;
385 /* XXX This is a hack that will work with current vout_display_t modules */
387 picture_Hold(direct);
389 vout_display_Display(vd, direct);
392 for (int i = 0; i < picture->i_planes; i++) {
393 picture->p[i].p_pixels = direct->p[i].p_pixels;
394 picture->p[i].i_pitch = direct->p[i].i_pitch;
395 picture->p[i].i_lines = direct->p[i].i_lines;
398 picture->p_sys->direct = NULL;
402 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
404 /* Load configuration */
405 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
406 cfg->display.title = title;
407 const int display_width = var_CreateGetInteger(vout, "width");
408 const int display_height = var_CreateGetInteger(vout, "height");
409 cfg->display.width = display_width > 0 ? display_width : 0;
410 cfg->display.height = display_height > 0 ? display_height : 0;
411 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
412 cfg->display.sar.num = 1; /* TODO monitor AR */
413 cfg->display.sar.den = 1;
414 unsigned zoom_den = 1000;
415 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
416 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
417 cfg->zoom.num = zoom_num;
418 cfg->zoom.den = zoom_den;
419 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
420 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
421 const int align_mask = var_CreateGetInteger(vout, "align");
422 if (align_mask & 0x1)
423 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
424 else if (align_mask & 0x2)
425 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
426 if (align_mask & 0x4)
427 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
428 else if (align_mask & 0x8)
429 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
433 static int Forward(vlc_object_t *object, char const *var,
434 vlc_value_t oldval, vlc_value_t newval, void *data)
436 vout_thread_t *vout = (vout_thread_t*)object;
440 return var_Set(vout->p_sys->vd, var, newval);