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 *****************************************************************************/
51 /* Minimum number of direct pictures the video output will accept without
52 * creating additional pictures in system memory */
53 #ifdef OPTIMIZE_MEMORY
54 # define VOUT_MIN_DIRECT_PICTURES (VOUT_MAX_PICTURES/2)
56 # define VOUT_MIN_DIRECT_PICTURES (3*VOUT_MAX_PICTURES/4)
59 /*****************************************************************************
61 *****************************************************************************/
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->p->p_sys = sys;
124 vout->p->decoder_pool = NULL;
129 /*****************************************************************************
131 *****************************************************************************/
132 void vout_CloseWrapper(vout_thread_t *vout)
134 vout_sys_t *sys = vout->p->p_sys;
137 var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
138 var_DelCallback(vout, "video-wallpaper", Forward, NULL);
140 vout->p->decoder_pool = NULL; /* FIXME remove */
142 vout_DeleteDisplay(sys->vd, NULL);
147 /*****************************************************************************
149 *****************************************************************************/
150 int vout_InitWrapper(vout_thread_t *vout)
152 vout_sys_t *sys = vout->p->p_sys;
153 vout_display_t *vd = sys->vd;
156 video_format_t source = vd->source;
158 vout->fmt_out.i_chroma = source.i_chroma;
159 vout->fmt_out.i_width =
160 vout->fmt_out.i_visible_width = source.i_width;
161 vout->fmt_out.i_height =
162 vout->fmt_out.i_visible_height = source.i_height;
163 vout->fmt_out.i_sar_num = source.i_sar_num;
164 vout->fmt_out.i_sar_den = source.i_sar_den;
165 vout->fmt_out.i_x_offset = 0;
166 vout->fmt_out.i_y_offset = 0;
167 vout->fmt_out.i_rmask = source.i_rmask;
168 vout->fmt_out.i_gmask = source.i_gmask;
169 vout->fmt_out.i_bmask = source.i_bmask;
171 if (vout->fmt_in.i_visible_width != source.i_visible_width ||
172 vout->fmt_in.i_visible_height != source.i_visible_height ||
173 vout->fmt_in.i_x_offset != source.i_x_offset ||
174 vout->fmt_in.i_y_offset != source.i_y_offset )
175 vout->p->i_changes |= VOUT_CROP_CHANGE;
177 #warning "vout_InitWrapper: vout_SetWindowState should NOT be called there"
178 if (vout->p->b_on_top)
179 vout_SetWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
181 /* XXX For non dr case, the current vout implementation force us to
182 * create at most 1 direct picture (otherwise the buffers will be kept
183 * referenced even through the Init/End.
185 sys->use_dr = !vout_IsDisplayFiltered(vd);
186 const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
188 picture_pool_t *display_pool = vout_display_Pool(vd, allow_dr ? VOUT_MAX_PICTURES : 3);
189 if (allow_dr && picture_pool_GetSize(display_pool) >= VOUT_MIN_DIRECT_PICTURES) {
190 vout->p->decoder_pool = display_pool;
191 vout->p->display_pool = display_pool;
192 vout->p->is_decoder_pool_slow = vd->info.is_slow;
193 } else if (!vout->p->decoder_pool) {
194 vout->p->decoder_pool = picture_pool_NewFromFormat(&source, VOUT_MAX_PICTURES);
196 vout->p->display_pool = display_pool;
198 vout->p->display_pool = picture_pool_Reserve(vout->p->decoder_pool, 1);;
199 vout->p->is_decoder_pool_slow = false;
201 vout->p->private_pool = picture_pool_Reserve(vout->p->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
202 sys->filtered = NULL;
206 /*****************************************************************************
208 *****************************************************************************/
209 void vout_EndWrapper(vout_thread_t *vout)
211 vout_sys_t *sys = vout->p->p_sys;
213 assert(!sys->filtered);
214 if (vout->p->private_pool)
215 picture_pool_Delete(vout->p->private_pool);
217 if (vout->p->decoder_pool != vout->p->display_pool) {
219 picture_pool_Delete(vout->p->display_pool);
220 picture_pool_Delete(vout->p->decoder_pool);
224 /*****************************************************************************
226 *****************************************************************************/
227 int vout_ManageWrapper(vout_thread_t *vout)
229 vout_sys_t *sys = vout->p->p_sys;
230 vout_display_t *vd = sys->vd;
232 while (vout->p->i_changes & (VOUT_FULLSCREEN_CHANGE |
239 if (vout->p->i_changes & VOUT_FULLSCREEN_CHANGE) {
240 vout->p->b_fullscreen = !vout->p->b_fullscreen;
242 var_SetBool(vout, "fullscreen", vout->p->b_fullscreen);
243 vout_SetDisplayFullscreen(vd, vout->p->b_fullscreen);
244 vout->p->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
246 if (vout->p->i_changes & VOUT_ASPECT_CHANGE) {
247 vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
248 vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
250 vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
252 vout->p->i_changes &= ~VOUT_ASPECT_CHANGE;
254 if (vout->p->i_changes & VOUT_ZOOM_CHANGE) {
255 const float zoom = var_GetFloat(vout, "scale");
257 unsigned den = ZOOM_FP_FACTOR;
258 unsigned num = den * zoom;
259 if (num < (ZOOM_FP_FACTOR+9) / 10)
260 num = (ZOOM_FP_FACTOR+9) / 10;
261 else if (num > ZOOM_FP_FACTOR * 10)
262 num = ZOOM_FP_FACTOR * 10;
264 vout_SetDisplayZoom(vd, num, den);
266 vout->p->i_changes &= ~VOUT_ZOOM_CHANGE;
268 if (vout->p->i_changes & VOUT_SCALE_CHANGE) {
269 const bool is_display_filled = var_GetBool(vout, "autoscale");
271 vout_SetDisplayFilled(vd, is_display_filled);
273 vout->p->i_changes &= ~VOUT_SCALE_CHANGE;
275 if (vout->p->i_changes & VOUT_ON_TOP_CHANGE) {
276 vout_SetWindowState(vd, vout->p->b_on_top
277 ? VOUT_WINDOW_STATE_ABOVE
278 : VOUT_WINDOW_STATE_NORMAL);
280 vout->p->i_changes &= ~VOUT_ON_TOP_CHANGE;
282 if (vout->p->i_changes & VOUT_CROP_CHANGE) {
283 const video_format_t crop = vout->fmt_in;
284 const video_format_t org = vout->fmt_render;
285 /* FIXME because of rounding errors, the reconstructed ratio is wrong */
288 if (crop.i_x_offset == org.i_x_offset &&
289 crop.i_visible_width == org.i_visible_width &&
290 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
291 vlc_ureduce(&num, &den,
292 crop.i_visible_width * crop.i_sar_num,
293 crop.i_visible_height * crop.i_sar_den, 0);
294 } else if (crop.i_y_offset == org.i_y_offset &&
295 crop.i_visible_height == org.i_visible_height &&
296 crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
297 vlc_ureduce(&num, &den,
298 crop.i_visible_width * crop.i_sar_num,
299 crop.i_visible_height * crop.i_sar_den, 0);
301 vout_SetDisplayCrop(vd, num, den,
302 crop.i_x_offset, crop.i_y_offset,
303 crop.i_visible_width, crop.i_visible_height);
304 vout->p->i_changes &= ~VOUT_CROP_CHANGE;
309 bool reset_display_pool = sys->use_dr && vout_AreDisplayPicturesInvalid(vd);
310 vout_ManageDisplay(vd, !sys->use_dr || reset_display_pool);
312 if (reset_display_pool)
313 vout->p->display_pool = vout_display_Pool(vd, 3);
318 /*****************************************************************************
320 *****************************************************************************/
321 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
323 vout_sys_t *sys = vout->p->p_sys;
324 vout_display_t *vd = sys->vd;
326 assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
329 vout_display_Prepare(vd, picture);
331 sys->filtered = vout_FilterDisplay(vd, picture);
333 vout_display_Prepare(vd, sys->filtered);
337 /*****************************************************************************
339 *****************************************************************************/
340 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
342 vout_sys_t *sys = vout->p->p_sys;
343 vout_display_t *vd = sys->vd;
345 vout_display_Display(vd, sys->filtered ? sys->filtered : picture);
346 sys->filtered = NULL;
349 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
351 /* Load configuration */
352 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
353 cfg->display.title = title;
354 const int display_width = var_CreateGetInteger(vout, "width");
355 const int display_height = var_CreateGetInteger(vout, "height");
356 cfg->display.width = display_width > 0 ? display_width : 0;
357 cfg->display.height = display_height > 0 ? display_height : 0;
358 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
359 cfg->display.sar.num = 1; /* TODO monitor AR */
360 cfg->display.sar.den = 1;
361 unsigned zoom_den = 1000;
362 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
363 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
364 cfg->zoom.num = zoom_num;
365 cfg->zoom.den = zoom_den;
366 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
367 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
368 const int align_mask = var_CreateGetInteger(vout, "align");
369 if (align_mask & 0x1)
370 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
371 else if (align_mask & 0x2)
372 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
373 if (align_mask & 0x4)
374 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
375 else if (align_mask & 0x8)
376 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
380 static int Forward(vlc_object_t *object, char const *var,
381 vlc_value_t oldval, vlc_value_t newval, void *data)
383 vout_thread_t *vout = (vout_thread_t*)object;
387 return var_Set(vout->p->p_sys->vd, var, newval);