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 if (source.i_sar_num > 0 && source.i_sar_den > 0) {
164 vlc_ureduce(&vout->fmt_out.i_sar_num, &vout->fmt_out.i_sar_den,
165 source.i_sar_num, source.i_sar_den, 0);
167 vout->fmt_out.i_sar_num = 1;
168 vout->fmt_out.i_sar_den = 1;
170 vout->fmt_out.i_sar_num = source.i_sar_num;
171 vout->fmt_out.i_sar_den = source.i_sar_den;
172 vout->fmt_out.i_x_offset = 0;
173 vout->fmt_out.i_y_offset = 0;
174 vout->fmt_out.i_rmask = source.i_rmask;
175 vout->fmt_out.i_gmask = source.i_gmask;
176 vout->fmt_out.i_bmask = source.i_bmask;
177 video_format_FixRgb(&vout->fmt_out);
179 if (vout->fmt_in.i_visible_width != source.i_visible_width ||
180 vout->fmt_in.i_visible_height != source.i_visible_height ||
181 vout->fmt_in.i_x_offset != source.i_x_offset ||
182 vout->fmt_in.i_y_offset != source.i_y_offset )
183 vout->p->i_changes |= VOUT_CROP_CHANGE;
185 #warning "vout_InitWrapper: vout_SetWindowState should NOT be called there"
186 if (vout->p->b_on_top)
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;
196 picture_pool_t *display_pool = vout_display_Pool(vd, allow_dr ? VOUT_MAX_PICTURES : 3);
197 if (allow_dr && picture_pool_GetSize(display_pool) >= VOUT_MIN_DIRECT_PICTURES) {
198 vout->p->decoder_pool = display_pool;
199 vout->p->display_pool = display_pool;
200 vout->p->is_decoder_pool_slow = vd->info.is_slow;
201 } else if (!vout->p->decoder_pool) {
202 vout->p->decoder_pool = picture_pool_NewFromFormat(&source, VOUT_MAX_PICTURES);
204 vout->p->display_pool = display_pool;
206 vout->p->display_pool = picture_pool_Reserve(vout->p->decoder_pool, 1);;
207 vout->p->is_decoder_pool_slow = false;
209 vout->p->private_pool = picture_pool_Reserve(vout->p->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
210 sys->filtered = NULL;
214 /*****************************************************************************
216 *****************************************************************************/
217 void vout_EndWrapper(vout_thread_t *vout)
219 vout_sys_t *sys = vout->p->p_sys;
221 assert(!sys->filtered);
222 if (vout->p->private_pool)
223 picture_pool_Delete(vout->p->private_pool);
225 if (vout->p->decoder_pool != vout->p->display_pool) {
227 picture_pool_Delete(vout->p->display_pool);
228 picture_pool_Delete(vout->p->decoder_pool);
232 /*****************************************************************************
234 *****************************************************************************/
235 int vout_ManageWrapper(vout_thread_t *vout)
237 vout_sys_t *sys = vout->p->p_sys;
238 vout_display_t *vd = sys->vd;
240 while (vout->p->i_changes & (VOUT_FULLSCREEN_CHANGE |
247 if (vout->p->i_changes & VOUT_FULLSCREEN_CHANGE) {
248 vout->p->b_fullscreen = !vout->p->b_fullscreen;
250 var_SetBool(vout, "fullscreen", vout->p->b_fullscreen);
251 vout_SetDisplayFullscreen(vd, vout->p->b_fullscreen);
252 vout->p->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
254 if (vout->p->i_changes & VOUT_ASPECT_CHANGE) {
255 vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
256 vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
258 vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
260 vout->p->i_changes &= ~VOUT_ASPECT_CHANGE;
262 if (vout->p->i_changes & VOUT_ZOOM_CHANGE) {
263 const float zoom = var_GetFloat(vout, "scale");
265 unsigned den = ZOOM_FP_FACTOR;
266 unsigned num = den * zoom;
267 if (num < (ZOOM_FP_FACTOR+9) / 10)
268 num = (ZOOM_FP_FACTOR+9) / 10;
269 else if (num > ZOOM_FP_FACTOR * 10)
270 num = ZOOM_FP_FACTOR * 10;
272 vout_SetDisplayZoom(vd, num, den);
274 vout->p->i_changes &= ~VOUT_ZOOM_CHANGE;
276 if (vout->p->i_changes & VOUT_SCALE_CHANGE) {
277 const bool is_display_filled = var_GetBool(vout, "autoscale");
279 vout_SetDisplayFilled(vd, is_display_filled);
281 vout->p->i_changes &= ~VOUT_SCALE_CHANGE;
283 if (vout->p->i_changes & VOUT_ON_TOP_CHANGE) {
284 vout_SetWindowState(vd, vout->p->b_on_top
285 ? VOUT_WINDOW_STATE_ABOVE
286 : VOUT_WINDOW_STATE_NORMAL);
288 vout->p->i_changes &= ~VOUT_ON_TOP_CHANGE;
290 if (vout->p->i_changes & VOUT_CROP_CHANGE) {
291 const video_format_t crop = vout->fmt_in;
292 const video_format_t org = vout->fmt_render;
293 /* FIXME because of rounding errors, the reconstructed ratio is wrong */
296 if (crop.i_x_offset == org.i_x_offset &&
297 crop.i_visible_width == org.i_visible_width &&
298 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
299 vlc_ureduce(&num, &den,
300 crop.i_visible_width * crop.i_sar_num,
301 crop.i_visible_height * crop.i_sar_den, 0);
302 } else if (crop.i_y_offset == org.i_y_offset &&
303 crop.i_visible_height == org.i_visible_height &&
304 crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
305 vlc_ureduce(&num, &den,
306 crop.i_visible_width * crop.i_sar_num,
307 crop.i_visible_height * crop.i_sar_den, 0);
309 vout_SetDisplayCrop(vd, num, den,
310 crop.i_x_offset, crop.i_y_offset,
311 crop.i_visible_width, crop.i_visible_height);
312 vout->p->i_changes &= ~VOUT_CROP_CHANGE;
317 bool reset_display_pool = sys->use_dr && vout_AreDisplayPicturesInvalid(vd);
318 vout_ManageDisplay(vd, !sys->use_dr || reset_display_pool);
320 if (reset_display_pool)
321 vout->p->display_pool = vout_display_Pool(vd, 3);
326 /*****************************************************************************
328 *****************************************************************************/
329 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
331 vout_sys_t *sys = vout->p->p_sys;
332 vout_display_t *vd = sys->vd;
334 assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
337 vout_display_Prepare(vd, picture);
339 sys->filtered = vout_FilterDisplay(vd, picture);
341 vout_display_Prepare(vd, sys->filtered);
345 /*****************************************************************************
347 *****************************************************************************/
348 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
350 vout_sys_t *sys = vout->p->p_sys;
351 vout_display_t *vd = sys->vd;
353 vout_display_Display(vd, sys->filtered ? sys->filtered : picture);
354 sys->filtered = NULL;
357 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
359 /* Load configuration */
360 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
361 cfg->display.title = title;
362 const int display_width = var_CreateGetInteger(vout, "width");
363 const int display_height = var_CreateGetInteger(vout, "height");
364 cfg->display.width = display_width > 0 ? display_width : 0;
365 cfg->display.height = display_height > 0 ? display_height : 0;
366 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
367 cfg->display.sar.num = 1; /* TODO monitor AR */
368 cfg->display.sar.den = 1;
369 unsigned zoom_den = 1000;
370 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
371 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
372 cfg->zoom.num = zoom_num;
373 cfg->zoom.den = zoom_den;
374 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
375 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
376 const int align_mask = var_CreateGetInteger(vout, "align");
377 if (align_mask & 0x1)
378 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
379 else if (align_mask & 0x2)
380 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
381 if (align_mask & 0x4)
382 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
383 else if (align_mask & 0x8)
384 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
388 static int Forward(vlc_object_t *object, char const *var,
389 vlc_value_t oldval, vlc_value_t newval, void *data)
391 vout_thread_t *vout = (vout_thread_t*)object;
395 return var_Set(vout->p->p_sys->vd, var, newval);