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_wrapper.h>
36 #include "vout_internal.h"
39 /* Minimum number of direct pictures the video output will accept without
40 * creating additional pictures in system memory */
41 #ifdef OPTIMIZE_MEMORY
42 # define VOUT_MIN_DIRECT_PICTURES (VOUT_MAX_PICTURES/2)
44 # define VOUT_MIN_DIRECT_PICTURES (3*VOUT_MAX_PICTURES/4)
47 /*****************************************************************************
49 *****************************************************************************/
50 static void VoutGetDisplayCfg(vout_thread_t *,
51 vout_display_cfg_t *, const char *title);
53 static int Forward(vlc_object_t *, char const *,
54 vlc_value_t, vlc_value_t, void *);
57 /*****************************************************************************
59 *****************************************************************************/
60 int vout_OpenWrapper(vout_thread_t *vout, const char *name)
62 vout_thread_sys_t *sys = vout->p;
63 msg_Dbg(vout, "Opening vout display wrapper");
66 sys->display.title = var_CreateGetNonEmptyString(vout, "video-title");
69 video_format_t source = vout->p->fmt_render;
70 source.i_visible_width = source.i_width;
71 source.i_visible_height = source.i_height;
72 source.i_x_offset = 0;
73 source.i_y_offset = 0;
75 vout_display_state_t state;
76 VoutGetDisplayCfg(vout, &state.cfg, sys->display.title);
77 state.is_on_top = var_CreateGetBool(vout, "video-on-top");
81 const mtime_t double_click_timeout = 300000;
82 const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
84 sys->display.vd = vout_NewDisplay(vout, &source, &state, name ? name : "$vout",
85 double_click_timeout, hide_timeout);
86 /* If we need to video filter and it fails, then try a splitter
87 * XXX it is a hack for now FIXME */
88 if (name && !sys->display.vd)
89 sys->display.vd = vout_NewSplitter(vout, &source, &state, "$vout", name,
90 double_click_timeout, hide_timeout);
91 if (!sys->display.vd) {
92 free(sys->display.title);
98 var_Create(vout, "direct3d-desktop", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
99 var_AddCallback(vout, "direct3d-desktop", Forward, NULL);
100 var_Create(vout, "video-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
101 var_AddCallback(vout, "video-wallpaper", Forward, NULL);
105 sys->decoder_pool = NULL;
110 /*****************************************************************************
112 *****************************************************************************/
113 void vout_CloseWrapper(vout_thread_t *vout)
115 vout_thread_sys_t *sys = vout->p;
118 var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
119 var_DelCallback(vout, "video-wallpaper", Forward, NULL);
121 sys->decoder_pool = NULL; /* FIXME remove */
123 vout_DeleteDisplay(sys->display.vd, NULL);
124 free(sys->display.title);
127 /*****************************************************************************
129 *****************************************************************************/
130 int vout_InitWrapper(vout_thread_t *vout)
132 vout_thread_sys_t *sys = vout->p;
133 vout_display_t *vd = sys->display.vd;
136 video_format_t source = vd->source;
138 vout->p->fmt_out.i_chroma = source.i_chroma;
139 vout->p->fmt_out.i_width =
140 vout->p->fmt_out.i_visible_width = source.i_width;
141 vout->p->fmt_out.i_height =
142 vout->p->fmt_out.i_visible_height = source.i_height;
143 if (source.i_sar_num > 0 && source.i_sar_den > 0) {
144 vlc_ureduce(&vout->p->fmt_out.i_sar_num, &vout->p->fmt_out.i_sar_den,
145 source.i_sar_num, source.i_sar_den, 0);
147 vout->p->fmt_out.i_sar_num = 1;
148 vout->p->fmt_out.i_sar_den = 1;
150 vout->p->fmt_out.i_sar_num = source.i_sar_num;
151 vout->p->fmt_out.i_sar_den = source.i_sar_den;
152 vout->p->fmt_out.i_x_offset = 0;
153 vout->p->fmt_out.i_y_offset = 0;
154 vout->p->fmt_out.i_rmask = source.i_rmask;
155 vout->p->fmt_out.i_gmask = source.i_gmask;
156 vout->p->fmt_out.i_bmask = source.i_bmask;
157 video_format_FixRgb(&vout->p->fmt_out);
159 if (vout->p->fmt_in.i_visible_width != source.i_visible_width ||
160 vout->p->fmt_in.i_visible_height != source.i_visible_height ||
161 vout->p->fmt_in.i_x_offset != source.i_x_offset ||
162 vout->p->fmt_in.i_y_offset != source.i_y_offset )
163 sys->i_changes |= VOUT_CROP_CHANGE;
165 /* XXX For non dr case, the current vout implementation force us to
166 * create at most 1 direct picture (otherwise the buffers will be kept
167 * referenced even through the Init/End.
169 sys->display.use_dr = !vout_IsDisplayFiltered(vd);
170 const bool allow_dr = !vd->info.has_pictures_invalid && sys->display.use_dr;
172 picture_pool_t *display_pool = vout_display_Pool(vd, allow_dr ? VOUT_MAX_PICTURES : 3);
173 if (allow_dr && picture_pool_GetSize(display_pool) >= VOUT_MIN_DIRECT_PICTURES) {
174 sys->decoder_pool = display_pool;
175 sys->display_pool = display_pool;
176 sys->is_decoder_pool_slow = vd->info.is_slow;
177 } else if (!sys->decoder_pool) {
178 sys->decoder_pool = picture_pool_NewFromFormat(&source, VOUT_MAX_PICTURES);
179 if (sys->display.use_dr)
180 sys->display_pool = display_pool;
182 sys->display_pool = picture_pool_Reserve(sys->decoder_pool, 1);;
183 sys->is_decoder_pool_slow = false;
185 sys->private_pool = picture_pool_Reserve(sys->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
186 sys->display.filtered = NULL;
190 /*****************************************************************************
192 *****************************************************************************/
193 void vout_EndWrapper(vout_thread_t *vout)
195 vout_thread_sys_t *sys = vout->p;
197 assert(!sys->display.filtered);
198 if (sys->private_pool)
199 picture_pool_Delete(sys->private_pool);
201 if (sys->decoder_pool != sys->display_pool) {
202 if (!sys->display.use_dr)
203 picture_pool_Delete(sys->display_pool);
204 picture_pool_Delete(sys->decoder_pool);
208 /*****************************************************************************
210 *****************************************************************************/
211 int vout_ManageWrapper(vout_thread_t *vout)
213 vout_thread_sys_t *sys = vout->p;
214 vout_display_t *vd = sys->display.vd;
216 while (sys->i_changes & (VOUT_ASPECT_CHANGE |
219 if (sys->i_changes & VOUT_ASPECT_CHANGE) {
220 vout->p->fmt_out.i_sar_num = vout->p->fmt_in.i_sar_num;
221 vout->p->fmt_out.i_sar_den = vout->p->fmt_in.i_sar_den;
223 vout_SetDisplayAspect(vd, vout->p->fmt_in.i_sar_num, vout->p->fmt_in.i_sar_den);
225 sys->i_changes &= ~VOUT_ASPECT_CHANGE;
227 if (sys->i_changes & VOUT_CROP_CHANGE) {
228 const video_format_t crop = vout->p->fmt_in;
229 const video_format_t org = vout->p->fmt_render;
230 /* FIXME because of rounding errors, the reconstructed ratio is wrong */
233 if (crop.i_x_offset == org.i_x_offset &&
234 crop.i_visible_width == org.i_visible_width &&
235 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
236 vlc_ureduce(&num, &den,
237 crop.i_visible_width * crop.i_sar_num,
238 crop.i_visible_height * crop.i_sar_den, 0);
239 } else if (crop.i_y_offset == org.i_y_offset &&
240 crop.i_visible_height == org.i_visible_height &&
241 crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
242 vlc_ureduce(&num, &den,
243 crop.i_visible_width * crop.i_sar_num,
244 crop.i_visible_height * crop.i_sar_den, 0);
246 vout_SetDisplayCrop(vd, num, den,
247 crop.i_x_offset, crop.i_y_offset,
248 crop.i_visible_width, crop.i_visible_height);
249 sys->i_changes &= ~VOUT_CROP_CHANGE;
254 bool reset_display_pool = sys->display.use_dr && vout_AreDisplayPicturesInvalid(vd);
255 vout_ManageDisplay(vd, !sys->display.use_dr || reset_display_pool);
257 if (reset_display_pool)
258 sys->display_pool = vout_display_Pool(vd, 3);
263 /*****************************************************************************
265 *****************************************************************************/
266 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
268 vout_thread_sys_t *sys = vout->p;
269 vout_display_t *vd = sys->display.vd;
271 assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
273 if (sys->display.use_dr) {
274 vout_display_Prepare(vd, picture);
276 sys->display.filtered = vout_FilterDisplay(vd, picture);
277 if (sys->display.filtered)
278 vout_display_Prepare(vd, sys->display.filtered);
282 /*****************************************************************************
284 *****************************************************************************/
285 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
287 vout_thread_sys_t *sys = vout->p;
288 vout_display_t *vd = sys->display.vd;
290 vout_display_Display(vd, sys->display.filtered ? sys->display.filtered : picture);
291 sys->display.filtered = NULL;
294 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
296 /* Load configuration */
297 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
298 cfg->display.title = title;
299 const int display_width = var_CreateGetInteger(vout, "width");
300 const int display_height = var_CreateGetInteger(vout, "height");
301 cfg->display.width = display_width > 0 ? display_width : 0;
302 cfg->display.height = display_height > 0 ? display_height : 0;
303 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
304 cfg->display.sar.num = 1; /* TODO monitor AR */
305 cfg->display.sar.den = 1;
306 unsigned zoom_den = 1000;
307 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
308 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
309 cfg->zoom.num = zoom_num;
310 cfg->zoom.den = zoom_den;
311 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
312 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
313 const int align_mask = var_CreateGetInteger(vout, "align");
314 if (align_mask & 0x1)
315 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
316 else if (align_mask & 0x2)
317 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
318 if (align_mask & 0x4)
319 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
320 else if (align_mask & 0x8)
321 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
325 static int Forward(vlc_object_t *object, char const *var,
326 vlc_value_t oldval, vlc_value_t newval, void *data)
328 vout_thread_t *vout = (vout_thread_t*)object;
332 return var_Set(vout->p->display.vd, var, newval);