]> git.sesse.net Git - vlc/blob - src/video_output/vout_wrapper.c
No functionnal changes (vout).
[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     picture_t      *filtered;
49 };
50
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)
55 #else
56 #   define VOUT_MIN_DIRECT_PICTURES        (3*VOUT_MAX_PICTURES/4)
57 #endif
58
59 /*****************************************************************************
60  * Local prototypes
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->p->p_sys = sys;
124     vout->p->decoder_pool = NULL;
125
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  *
131  *****************************************************************************/
132 void vout_CloseWrapper(vout_thread_t *vout)
133 {
134     vout_sys_t *sys = vout->p->p_sys;
135
136 #ifdef WIN32
137     var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
138     var_DelCallback(vout, "video-wallpaper", Forward, NULL);
139 #endif
140     vout->p->decoder_pool = NULL; /* FIXME remove */
141
142     vout_DeleteDisplay(sys->vd, NULL);
143     free(sys->title);
144     free(sys );
145 }
146
147 /*****************************************************************************
148  *
149  *****************************************************************************/
150 int vout_InitWrapper(vout_thread_t *vout)
151 {
152     vout_sys_t *sys = vout->p->p_sys;
153     vout_display_t *vd = sys->vd;
154
155     /* */
156     video_format_t source = vd->source;
157
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);
166     } else {
167         vout->fmt_out.i_sar_num    = 1;
168         vout->fmt_out.i_sar_den    = 1;
169     }
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);
178
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;
184
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);
188
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.
192      */
193     sys->use_dr = !vout_IsDisplayFiltered(vd);
194     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
195
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);
203         if (sys->use_dr)
204             vout->p->display_pool = display_pool;
205         else
206             vout->p->display_pool = picture_pool_Reserve(vout->p->decoder_pool, 1);;
207         vout->p->is_decoder_pool_slow = false;
208     }
209     vout->p->private_pool = picture_pool_Reserve(vout->p->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
210     sys->filtered = NULL;
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  *
216  *****************************************************************************/
217 void vout_EndWrapper(vout_thread_t *vout)
218 {
219     vout_sys_t *sys = vout->p->p_sys;
220
221     assert(!sys->filtered);
222     if (vout->p->private_pool)
223         picture_pool_Delete(vout->p->private_pool);
224
225     if (vout->p->decoder_pool != vout->p->display_pool) {
226         if (!sys->use_dr)
227             picture_pool_Delete(vout->p->display_pool);
228         picture_pool_Delete(vout->p->decoder_pool);
229     }
230 }
231
232 /*****************************************************************************
233  *
234  *****************************************************************************/
235 int vout_ManageWrapper(vout_thread_t *vout)
236 {
237     vout_sys_t *sys = vout->p->p_sys;
238     vout_display_t *vd = sys->vd;
239
240     while (vout->p->i_changes & (VOUT_FULLSCREEN_CHANGE |
241                               VOUT_ASPECT_CHANGE |
242                               VOUT_ZOOM_CHANGE |
243                               VOUT_SCALE_CHANGE |
244                               VOUT_ON_TOP_CHANGE |
245                               VOUT_CROP_CHANGE)) {
246         /* */
247         if (vout->p->i_changes & VOUT_FULLSCREEN_CHANGE) {
248             vout->p->b_fullscreen = !vout->p->b_fullscreen;
249
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;
253         }
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;
257
258             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
259
260             vout->p->i_changes &= ~VOUT_ASPECT_CHANGE;
261         }
262         if (vout->p->i_changes & VOUT_ZOOM_CHANGE) {
263             const float zoom = var_GetFloat(vout, "scale");
264
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;
271
272             vout_SetDisplayZoom(vd, num, den);
273
274             vout->p->i_changes &= ~VOUT_ZOOM_CHANGE;
275         }
276         if (vout->p->i_changes & VOUT_SCALE_CHANGE) {
277             const bool is_display_filled = var_GetBool(vout, "autoscale");
278
279             vout_SetDisplayFilled(vd, is_display_filled);
280
281             vout->p->i_changes &= ~VOUT_SCALE_CHANGE;
282         }
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);
287
288             vout->p->i_changes &= ~VOUT_ON_TOP_CHANGE;
289         }
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 */
294             unsigned num = 0;
295             unsigned den = 0;
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);
308             }
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;
313         }
314
315     }
316
317     bool reset_display_pool = sys->use_dr && vout_AreDisplayPicturesInvalid(vd);
318     vout_ManageDisplay(vd, !sys->use_dr || reset_display_pool);
319
320     if (reset_display_pool)
321         vout->p->display_pool = vout_display_Pool(vd, 3);
322
323     return VLC_SUCCESS;
324 }
325
326 /*****************************************************************************
327  * Render
328  *****************************************************************************/
329 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
330 {
331     vout_sys_t *sys = vout->p->p_sys;
332     vout_display_t *vd = sys->vd;
333
334     assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
335
336     if (sys->use_dr) {
337         vout_display_Prepare(vd, picture);
338     } else {
339         sys->filtered = vout_FilterDisplay(vd, picture);
340         if (sys->filtered)
341             vout_display_Prepare(vd, sys->filtered);
342     }
343 }
344
345 /*****************************************************************************
346  *
347  *****************************************************************************/
348 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
349 {
350     vout_sys_t *sys = vout->p->p_sys;
351     vout_display_t *vd = sys->vd;
352
353      vout_display_Display(vd, sys->filtered ? sys->filtered : picture);
354      sys->filtered = NULL;
355 }
356
357 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
358 {
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;
385 }
386
387 #ifdef WIN32
388 static int Forward(vlc_object_t *object, char const *var,
389                    vlc_value_t oldval, vlc_value_t newval, void *data)
390 {
391     vout_thread_t *vout = (vout_thread_t*)object;
392
393     VLC_UNUSED(oldval);
394     VLC_UNUSED(data);
395     return var_Set(vout->p->p_sys->vd, var, newval);
396 }
397 #endif