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>
36 #include "../video_filter/filter_common.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open (vlc_object_t *, const char *module);
43 static void Close(vlc_object_t *);
45 #define DECLARE_OPEN(name) \
46 static int Open##name(vlc_object_t *object) { return Open(object, #name); }
54 #define DECLARE_MODULE(name, priority) \
55 set_description( "Video display "#name" wrapper" ) \
56 set_shortname( "Video display "#name" wrapper" ) \
57 set_capability( "video output", priority ) \
58 set_callbacks( Open##name, Close ) \
62 set_category( CAT_VIDEO )
63 set_subcategory( SUBCAT_VIDEO_VOUT )
65 DECLARE_MODULE(aalib, 10)
68 DECLARE_MODULE(caca, 12)
71 DECLARE_MODULE(sdl, 60)
77 /*****************************************************************************
79 *****************************************************************************/
86 struct picture_sys_t {
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Init (vout_thread_t *);
94 static void End (vout_thread_t *);
95 static int Manage (vout_thread_t *);
96 static void Render (vout_thread_t *, picture_t *);
97 static void Display(vout_thread_t *, picture_t *);
99 static void VoutGetDisplayCfg(vout_thread_t *,
100 vout_display_cfg_t *, const char *title);
102 /*****************************************************************************
104 *****************************************************************************/
105 static int Open(vlc_object_t *object, const char *module)
107 vout_thread_t *vout = (vout_thread_t *)object;
110 msg_Err(vout, "Opening vout display wrapper");
113 sys = malloc(sizeof(*sys));
117 sys->title = var_CreateGetNonEmptyString(vout, "video-title");
120 video_format_t source = vout->fmt_render;
121 source.i_visible_width = source.i_width;
122 source.i_visible_height = source.i_height;
123 source.i_x_offset = 0;
124 source.i_y_offset = 0;
126 vout_display_state_t state;
127 VoutGetDisplayCfg(vout, &state.cfg, sys->title);
128 state.is_on_top = var_CreateGetBool(vout, "video-on-top");
132 const mtime_t double_click_timeout = 300000;
133 const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
135 sys->vd = vout_NewDisplay(vout, &source, &state, module,
136 double_click_timeout, hide_timeout);
144 vout->pf_init = Init;
146 vout->pf_manage = Manage;
147 vout->pf_render = Render;
148 vout->pf_display = Display;
149 vout->pf_control = NULL;
155 /*****************************************************************************
157 *****************************************************************************/
158 static void Close(vlc_object_t *object)
160 vout_thread_t *vout = (vout_thread_t *)object;
161 vout_sys_t *sys = vout->p_sys;
164 vout_DeleteDisplay(sys->vd, NULL);
169 /*****************************************************************************
171 *****************************************************************************/
172 static int Init(vout_thread_t *vout)
174 vout_sys_t *sys = vout->p_sys;
175 vout_display_t *vd = sys->vd;
178 video_format_t source = vd->source;
180 vout->output.i_chroma = source.i_chroma;
181 vout->output.i_width = source.i_width;
182 vout->output.i_height = source.i_height;
183 vout->output.i_aspect = source.i_aspect;
184 vout->output.i_rmask = source.i_rmask;
185 vout->output.i_gmask = source.i_gmask;
186 vout->output.i_bmask = source.i_bmask;
187 vout->output.pf_setpalette = NULL; /* FIXME What to do ? Seems unused anyway */
189 /* also set fmt_out (completly broken API) */
190 vout->fmt_out.i_chroma = vout->output.i_chroma;
191 vout->fmt_out.i_width =
192 vout->fmt_out.i_visible_width = vout->output.i_width;
193 vout->fmt_out.i_height =
194 vout->fmt_out.i_visible_height = vout->output.i_height;
195 vout->fmt_out.i_aspect = vout->output.i_aspect;
196 vout->fmt_out.i_x_offset = 0;
197 vout->fmt_out.i_y_offset = 0;
201 if (p_vout->fmt_render.i_visible_width != source.i_visible_width ||
202 p_vout->fmt_render.i_visible_height != source.i_visible_height ||
203 p_vout->fmt_render.i_x_offset != source.i_x_offset ||
204 p_vout->fmt_render.i_y_offset != source.i_y_offset )
206 p_vout->i_changes |= VOUT_CROP_CHANGE;
210 vout_SetDisplayOnTop(vd, true);
212 /* XXX For non dr case, the current vout implementation force us to
213 * create at most 1 direct picture (otherwise the buffers will be kept
214 * referenced even through the Init/End.
216 sys->use_dr = !vout_IsDisplayFiltered(vd);
217 const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
218 const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
219 for (vout->output.i_pictures = 0;
220 vout->output.i_pictures < picture_max;
221 vout->output.i_pictures++) {
222 /* Find an empty picture slot */
223 picture_t *picture = NULL;
224 for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
225 if (vout->p_picture[index].i_status == FREE_PICTURE) {
226 picture = &vout->p_picture[index];
232 memset(picture, 0, sizeof(*picture));
234 picture->p_sys = malloc(sizeof(*picture->p_sys));
237 picture_t *direct = vout_display_Get(vd);
240 picture->format = direct->format;
241 picture->i_planes = direct->i_planes;
242 for (int i = 0; i < direct->i_planes; i++)
243 picture->p[i] = direct->p[i];
244 picture->b_slow = vd->info.is_slow;
246 picture->p_sys->direct = direct;
248 vout_AllocatePicture(VLC_OBJECT(vd), picture,
250 vd->source.i_width, vd->source.i_height,
251 vd->source.i_aspect);
252 if (!picture->i_planes)
254 picture->p_sys->direct = NULL;
256 picture->i_status = DESTROYED_PICTURE;
257 picture->i_type = DIRECT_PICTURE;
259 vout->output.pp_picture[vout->output.i_pictures] = picture;
264 /*****************************************************************************
266 *****************************************************************************/
267 static void End(vout_thread_t *vout)
269 vout_sys_t *sys = vout->p_sys;
271 for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
272 picture_t *picture = &vout->p_picture[i];
274 if (picture->i_type != DIRECT_PICTURE)
277 if (picture->p_sys->direct)
278 picture_Release(picture->p_sys->direct);
280 free(picture->p_data_orig);
281 free(picture->p_sys);
285 /*****************************************************************************
287 *****************************************************************************/
288 static int Manage(vout_thread_t *vout)
290 vout_sys_t *sys = vout->p_sys;
291 vout_display_t *vd = sys->vd;
293 while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
300 if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
301 vout->b_fullscreen = !vout->b_fullscreen;
303 var_SetBool(vout, "fullscreen", vout->b_fullscreen);
304 vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
305 vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
307 if (vout->i_changes & VOUT_ASPECT_CHANGE) {
308 vout->output.i_aspect =
309 vout->fmt_out.i_aspect = vout->fmt_in.i_aspect;
310 vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
311 vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
313 vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
315 vout->i_changes &= ~VOUT_ASPECT_CHANGE;
317 if (vout->i_changes & VOUT_ZOOM_CHANGE) {
318 const float zoom = var_GetFloat(vout, "scale");
320 unsigned den = ZOOM_FP_FACTOR;
321 unsigned num = den * zoom;
322 if (num < (ZOOM_FP_FACTOR+9) / 10)
323 num = (ZOOM_FP_FACTOR+9) / 10;
324 else if (num > ZOOM_FP_FACTOR * 10)
325 num = ZOOM_FP_FACTOR * 10;
327 vout_SetDisplayZoom(vd, num, den);
329 vout->i_changes &= ~VOUT_ZOOM_CHANGE;
331 if (vout->i_changes & VOUT_SCALE_CHANGE) {
332 const bool is_display_filled = var_GetBool(vout, "autoscale");
334 vout_SetDisplayFilled(vd, is_display_filled);
336 vout->i_changes &= ~VOUT_SCALE_CHANGE;
338 if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
339 vout_SetDisplayOnTop(vd, vout->b_on_top);
341 vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
343 if (vout->i_changes & VOUT_CROP_CHANGE) {
344 const video_format_t crop = vout->fmt_in;
345 const video_format_t org = vout->fmt_render;
346 /* FIXME because of rounding errors, the reconstructed ratio is wrong */
349 if (crop.i_x_offset == org.i_x_offset &&
350 crop.i_visible_width == org.i_visible_width &&
351 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
352 vlc_ureduce(&num, &den,
353 crop.i_visible_width * crop.i_sar_num,
354 crop.i_visible_height * crop.i_sar_den, 0);
355 } else if (crop.i_y_offset == org.i_y_offset &&
356 crop.i_visible_height == org.i_visible_height &&
357 crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
358 vlc_ureduce(&num, &den,
359 crop.i_visible_width * crop.i_sar_num,
360 crop.i_visible_height * crop.i_sar_den, 0);
362 vout_SetDisplayCrop(vd, num, den,
363 crop.i_x_offset, crop.i_y_offset,
364 crop.i_visible_width, crop.i_visible_height);
365 vout->i_changes &= ~VOUT_CROP_CHANGE;
370 if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
371 vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
374 vout_ManageDisplay(vd);
378 /*****************************************************************************
380 *****************************************************************************/
381 static void Render(vout_thread_t *vout, picture_t *picture)
383 vout_sys_t *sys = vout->p_sys;
384 vout_display_t *vd = sys->vd;
386 assert(sys->use_dr || !picture->p_sys->direct);
389 assert(!vout_IsDisplayFiltered(vd));
390 assert(picture->p_sys->direct);
392 vout_display_Prepare(vd, picture->p_sys->direct);
394 picture_t *filtered = vout_FilterDisplay(vd, picture);
396 picture_t *direct = picture->p_sys->direct = vout_display_Get(vd);
398 picture_Copy(direct, filtered);
399 vout_display_Prepare(vd, direct);
401 picture_Release(filtered);
406 /*****************************************************************************
408 *****************************************************************************/
409 static void Display(vout_thread_t *vout, picture_t *picture)
411 vout_sys_t *sys = vout->p_sys;
412 vout_display_t *vd = sys->vd;
414 picture_t *direct = picture->p_sys->direct;
418 /* XXX This is a hack that will work with current vout_display_t modules */
420 picture_Hold(direct);
422 vout_display_Display(vd, direct);
425 picture->p_sys->direct = NULL;
428 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
430 /* Load configuration */
431 cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
432 cfg->display.title = title;
433 cfg->display.width = var_CreateGetInteger(vout, "width");
434 cfg->display.height = var_CreateGetInteger(vout, "height");
435 cfg->is_display_filled = var_CreateGetBool(vout, "autoscale");
436 cfg->display.sar.num = 1; /* TODO monitor AR */
437 cfg->display.sar.den = 1;
438 unsigned zoom_den = 1000;
439 unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
440 vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
441 cfg->zoom.num = zoom_num;
442 cfg->zoom.den = zoom_den;
443 cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
444 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
445 const int align_mask = var_CreateGetInteger(vout, "align");
446 if (align_mask & 0x1)
447 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
448 else if (align_mask & 0x2)
449 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
450 if (align_mask & 0x4)
451 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
452 else if (align_mask & 0x8)
453 cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;