]> git.sesse.net Git - vlc/blob - src/video_output/vout_wrapper.c
Enabled support for video splitter directly at the core.
[vlc] / src / video_output / vout_wrapper.c
1 /*****************************************************************************
2  * vout_display.c: "vout display" -> "video output" wrapper
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_vout_wrapper.h>
35 #include <vlc_vout.h>
36 #include <assert.h>
37 #include "vout_internal.h"
38 #include "display.h"
39
40 /*****************************************************************************
41  *
42  *****************************************************************************/
43 struct vout_sys_t {
44     char           *title;
45     vout_display_t *vd;
46     bool           use_dr;
47 };
48
49 struct picture_sys_t {
50     picture_t *direct;
51 };
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  Init   (vout_thread_t *);
57 static void End    (vout_thread_t *);
58 static int  Manage (vout_thread_t *);
59 static void Render (vout_thread_t *, picture_t *);
60 static void Display(vout_thread_t *, picture_t *);
61
62 static void VoutGetDisplayCfg(vout_thread_t *,
63                               vout_display_cfg_t *, const char *title);
64 #ifdef WIN32
65 static int  Forward(vlc_object_t *, char const *,
66                     vlc_value_t, vlc_value_t, void *);
67 #endif
68
69 /*****************************************************************************
70  *
71  *****************************************************************************/
72 int vout_OpenWrapper(vout_thread_t *vout, const char *name)
73 {
74     vout_sys_t *sys;
75
76     msg_Dbg(vout, "Opening vout display wrapper");
77
78     /* */
79     sys = malloc(sizeof(*sys));
80     if (!sys)
81         return VLC_ENOMEM;
82
83     sys->title = var_CreateGetNonEmptyString(vout, "video-title");
84
85     /* */
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;
91
92     vout_display_state_t state;
93     VoutGetDisplayCfg(vout, &state.cfg, sys->title);
94     state.is_on_top = var_CreateGetBool(vout, "video-on-top");
95     state.sar.num = 0;
96     state.sar.den = 0;
97
98     const mtime_t double_click_timeout = 300000;
99     const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
100
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);
108     if (!sys->vd) {
109         free(sys->title);
110         free(sys);
111         return VLC_EGENERIC;
112     }
113
114     /* */
115 #ifdef WIN32
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);
120 #endif
121
122     /* */
123     vout->pf_init    = Init;
124     vout->pf_end     = End;
125     vout->pf_manage  = Manage;
126     vout->pf_render  = Render;
127     vout->pf_display = Display;
128     vout->pf_control = NULL;
129     vout->p_sys      = sys;
130
131     return VLC_SUCCESS;
132 }
133
134 /*****************************************************************************
135  *
136  *****************************************************************************/
137 void vout_CloseWrapper(vout_thread_t *vout)
138 {
139     vout_sys_t *sys = vout->p_sys;
140
141 #ifdef WIN32
142     var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
143     var_DelCallback(vout, "video-wallpaper", Forward, NULL);
144 #endif
145     vout_DeleteDisplay(sys->vd, NULL);
146     free(sys->title);
147     free(sys );
148 }
149
150 /*****************************************************************************
151  *
152  *****************************************************************************/
153 static int Init(vout_thread_t *vout)
154 {
155     vout_sys_t *sys = vout->p_sys;
156     vout_display_t *vd = sys->vd;
157
158     /* */
159     video_format_t source = vd->source;
160
161     vout->output.i_chroma = source.i_chroma;
162     vout->output.i_width  = source.i_width;
163     vout->output.i_height = source.i_height;
164     vout->output.i_aspect = (int64_t)source.i_sar_num * source.i_width * VOUT_ASPECT_FACTOR / source.i_sar_den / source.i_height;
165     vout->output.i_rmask  = source.i_rmask;
166     vout->output.i_gmask  = source.i_gmask;
167     vout->output.i_bmask  = source.i_bmask;
168     vout->output.pf_setpalette = NULL; /* FIXME What to do ? Seems unused anyway */
169
170     /* also set fmt_out (completly broken API) */
171     vout->fmt_out.i_chroma         = vout->output.i_chroma;
172     vout->fmt_out.i_width          =
173     vout->fmt_out.i_visible_width  = vout->output.i_width;
174     vout->fmt_out.i_height         =
175     vout->fmt_out.i_visible_height = vout->output.i_height;
176     vout->fmt_out.i_sar_num        = vout->output.i_aspect * vout->output.i_height;
177     vout->fmt_out.i_sar_den        = VOUT_ASPECT_FACTOR    * vout->output.i_width;
178     vout->fmt_out.i_x_offset       = 0;
179     vout->fmt_out.i_y_offset       = 0;
180
181     if (vout->fmt_in.i_visible_width  != source.i_visible_width ||
182         vout->fmt_in.i_visible_height != source.i_visible_height ||
183         vout->fmt_in.i_x_offset       != source.i_x_offset ||
184         vout->fmt_in.i_y_offset       != source.i_y_offset )
185         vout->i_changes |= VOUT_CROP_CHANGE;
186
187     if (vout->b_on_top)
188         vout_SetWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
189
190     /* XXX For non dr case, the current vout implementation force us to
191      * create at most 1 direct picture (otherwise the buffers will be kept
192      * referenced even through the Init/End.
193      */
194     sys->use_dr = !vout_IsDisplayFiltered(vd);
195     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
196     const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
197     for (vout->output.i_pictures = 0;
198             vout->output.i_pictures < picture_max;
199                 vout->output.i_pictures++) {
200         /* Find an empty picture slot */
201         picture_t *picture = NULL;
202         for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
203             if (vout->p_picture[index].i_status == FREE_PICTURE) {
204                 picture = &vout->p_picture[index];
205                 break;
206             }
207         }
208         if (!picture)
209             break;
210         memset(picture, 0, sizeof(*picture));
211
212         picture->p_sys = malloc(sizeof(*picture->p_sys));
213
214         if (sys->use_dr) {
215             picture_pool_t *pool = vout_display_Pool(vd, picture_max);
216             if (!pool)
217                 break;
218             picture_t *direct = picture_pool_Get(pool);
219             if (!direct)
220                 break;
221             picture->format = direct->format;
222             picture->i_planes = direct->i_planes;
223             for (int i = 0; i < direct->i_planes; i++)
224                 picture->p[i] = direct->p[i];
225             picture->b_slow = vd->info.is_slow;
226
227             picture->p_sys->direct = direct;
228         } else {
229             vout_AllocatePicture(VLC_OBJECT(vd), picture,
230                                  vd->source.i_chroma,
231                                  vd->source.i_width, vd->source.i_height,
232                                  vd->source.i_sar_num, vd->source.i_sar_den);
233             if (!picture->i_planes)
234                 break;
235             picture->p_sys->direct = NULL;
236         }
237         picture->i_status = DESTROYED_PICTURE;
238         picture->i_type    = DIRECT_PICTURE;
239
240         vout->output.pp_picture[vout->output.i_pictures] = picture;
241     }
242     return VLC_SUCCESS;
243 }
244
245 /*****************************************************************************
246  *
247  *****************************************************************************/
248 static void End(vout_thread_t *vout)
249 {
250     vout_sys_t *sys = vout->p_sys;
251
252     for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
253         picture_t *picture = &vout->p_picture[i];
254
255         if (picture->i_type != DIRECT_PICTURE)
256             continue;
257
258         if (picture->p_sys->direct)
259             picture_Release(picture->p_sys->direct);
260         if (!sys->use_dr)
261             free(picture->p_data_orig);
262         free(picture->p_sys);
263
264         picture->i_status = FREE_PICTURE;
265     }
266     if (sys->use_dr && vout_AreDisplayPicturesInvalid(sys->vd))
267         vout_ManageDisplay(sys->vd, true);
268 }
269
270 /*****************************************************************************
271  *
272  *****************************************************************************/
273 static int Manage(vout_thread_t *vout)
274 {
275     vout_sys_t *sys = vout->p_sys;
276     vout_display_t *vd = sys->vd;
277
278     while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
279                               VOUT_ASPECT_CHANGE |
280                               VOUT_ZOOM_CHANGE |
281                               VOUT_SCALE_CHANGE |
282                               VOUT_ON_TOP_CHANGE |
283                               VOUT_CROP_CHANGE)) {
284         /* */
285         if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
286             vout->b_fullscreen = !vout->b_fullscreen;
287
288             var_SetBool(vout, "fullscreen", vout->b_fullscreen);
289             vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
290             vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
291         }
292         if (vout->i_changes & VOUT_ASPECT_CHANGE) {
293             vout->output.i_aspect   = (int64_t)vout->fmt_in.i_sar_num * vout->fmt_in.i_width * VOUT_ASPECT_FACTOR /
294                                       vout->fmt_in.i_sar_den / vout->fmt_in.i_height;
295             vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
296             vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
297
298             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
299
300             vout->i_changes &= ~VOUT_ASPECT_CHANGE;
301         }
302         if (vout->i_changes & VOUT_ZOOM_CHANGE) {
303             const float zoom = var_GetFloat(vout, "scale");
304
305             unsigned den = ZOOM_FP_FACTOR;
306             unsigned num = den * zoom;
307             if (num < (ZOOM_FP_FACTOR+9) / 10)
308                 num = (ZOOM_FP_FACTOR+9) / 10;
309             else if (num > ZOOM_FP_FACTOR * 10)
310                 num = ZOOM_FP_FACTOR * 10;
311
312             vout_SetDisplayZoom(vd, num, den);
313
314             vout->i_changes &= ~VOUT_ZOOM_CHANGE;
315         }
316         if (vout->i_changes & VOUT_SCALE_CHANGE) {
317             const bool is_display_filled = var_GetBool(vout, "autoscale");
318
319             vout_SetDisplayFilled(vd, is_display_filled);
320
321             vout->i_changes &= ~VOUT_SCALE_CHANGE;
322         }
323         if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
324             vout_SetWindowState(vd, vout->b_on_top
325                 ? VOUT_WINDOW_STATE_ABOVE
326                 : VOUT_WINDOW_STATE_NORMAL);
327
328             vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
329         }
330         if (vout->i_changes & VOUT_CROP_CHANGE) {
331             const video_format_t crop = vout->fmt_in;
332             const video_format_t org = vout->fmt_render;
333             /* FIXME because of rounding errors, the reconstructed ratio is wrong */
334             unsigned num = 0;
335             unsigned den = 0;
336             if (crop.i_x_offset == org.i_x_offset &&
337                 crop.i_visible_width == org.i_visible_width &&
338                 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
339                 vlc_ureduce(&num, &den,
340                             crop.i_visible_width * crop.i_sar_num,
341                             crop.i_visible_height * crop.i_sar_den, 0);
342             } else if (crop.i_y_offset == org.i_y_offset &&
343                        crop.i_visible_height == org.i_visible_height &&
344                        crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
345                 vlc_ureduce(&num, &den,
346                             crop.i_visible_width * crop.i_sar_num,
347                             crop.i_visible_height * crop.i_sar_den, 0);
348             }
349             vout_SetDisplayCrop(vd, num, den,
350                                 crop.i_x_offset, crop.i_y_offset,
351                                 crop.i_visible_width, crop.i_visible_height);
352             vout->i_changes &= ~VOUT_CROP_CHANGE;
353         }
354
355     }
356
357     if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
358         vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
359     }
360     vout_ManageDisplay(vd, !sys->use_dr);
361     return VLC_SUCCESS;
362 }
363
364 /*****************************************************************************
365  * Render
366  *****************************************************************************/
367 static void Render(vout_thread_t *vout, picture_t *picture)
368 {
369     vout_sys_t *sys = vout->p_sys;
370     vout_display_t *vd = sys->vd;
371
372     assert(sys->use_dr || !picture->p_sys->direct);
373     assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
374
375     if (sys->use_dr) {
376         assert(picture->p_sys->direct);
377         vout_display_Prepare(vd, picture->p_sys->direct);
378     } else {
379         picture_t *direct = picture->p_sys->direct = vout_FilterDisplay(vd, picture);
380         if (direct) {
381             vout_display_Prepare(vd, direct);
382         }
383     }
384 }
385
386 /*****************************************************************************
387  *
388  *****************************************************************************/
389 static void Display(vout_thread_t *vout, picture_t *picture)
390 {
391     vout_sys_t *sys = vout->p_sys;
392     vout_display_t *vd = sys->vd;
393
394     picture_t *direct = picture->p_sys->direct;
395     if (!direct)
396         return;
397
398     /* XXX This is a hack that will work with current vout_display_t modules */
399     if (sys->use_dr)
400         picture_Hold(direct);
401
402      vout_display_Display(vd, direct);
403
404      if (sys->use_dr) {
405          for (int i = 0; i < picture->i_planes; i++) {
406              picture->p[i].p_pixels = direct->p[i].p_pixels;
407              picture->p[i].i_pitch  = direct->p[i].i_pitch;
408              picture->p[i].i_lines  = direct->p[i].i_lines;
409          }
410      } else {
411          picture->p_sys->direct = NULL;
412      }
413 }
414
415 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
416 {
417     /* Load configuration */
418     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
419     cfg->display.title = title;
420     const int display_width = var_CreateGetInteger(vout, "width");
421     const int display_height = var_CreateGetInteger(vout, "height");
422     cfg->display.width   = display_width > 0  ? display_width  : 0;
423     cfg->display.height  = display_height > 0 ? display_height : 0;
424     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
425     cfg->display.sar.num = 1; /* TODO monitor AR */
426     cfg->display.sar.den = 1;
427     unsigned zoom_den = 1000;
428     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
429     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
430     cfg->zoom.num = zoom_num;
431     cfg->zoom.den = zoom_den;
432     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
433     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
434     const int align_mask = var_CreateGetInteger(vout, "align");
435     if (align_mask & 0x1)
436         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
437     else if (align_mask & 0x2)
438         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
439     if (align_mask & 0x4)
440         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
441     else if (align_mask & 0x8)
442         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
443 }
444
445 #ifdef WIN32
446 static int Forward(vlc_object_t *object, char const *var,
447                    vlc_value_t oldval, vlc_value_t newval, void *data)
448 {
449     vout_thread_t *vout = (vout_thread_t*)object;
450
451     VLC_UNUSED(oldval);
452     VLC_UNUSED(data);
453     return var_Set(vout->p_sys->vd, var, newval);
454 }
455 #endif