]> git.sesse.net Git - vlc/blob - src/video_output/vout_wrapper.c
1d43e61c3acc662328a7448b93a41e7e4b4fb7d7
[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_wrapper.h>
34 #include <vlc_vout.h>
35 #include <assert.h>
36 #include "vout_internal.h"
37 #include "display.h"
38
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)
43 #else
44 #   define VOUT_MIN_DIRECT_PICTURES        (3*VOUT_MAX_PICTURES/4)
45 #endif
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static void VoutGetDisplayCfg(vout_thread_t *,
51                               vout_display_cfg_t *, const char *title);
52 #ifdef WIN32
53 static int  Forward(vlc_object_t *, char const *,
54                     vlc_value_t, vlc_value_t, void *);
55 #endif
56
57 /*****************************************************************************
58  *
59  *****************************************************************************/
60 int vout_OpenWrapper(vout_thread_t *vout, const char *name)
61 {
62     vout_thread_sys_t *sys = vout->p;
63     msg_Dbg(vout, "Opening vout display wrapper");
64
65     /* */
66     sys->display.title = var_CreateGetNonEmptyString(vout, "video-title");
67
68     /* */
69     video_format_t source   = vout->p->original;
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;
74
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");
78     state.sar.num = 0;
79     state.sar.den = 0;
80
81     const mtime_t double_click_timeout = 300000;
82     const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
83
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);
93         return VLC_EGENERIC;
94     }
95
96     /* */
97 #ifdef WIN32
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);
102 #endif
103
104     /* */
105     sys->decoder_pool = NULL;
106
107     return VLC_SUCCESS;
108 }
109
110 /*****************************************************************************
111  *
112  *****************************************************************************/
113 void vout_CloseWrapper(vout_thread_t *vout)
114 {
115     vout_thread_sys_t *sys = vout->p;
116
117 #ifdef WIN32
118     var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
119     var_DelCallback(vout, "video-wallpaper", Forward, NULL);
120 #endif
121     sys->decoder_pool = NULL; /* FIXME remove */
122
123     vout_DeleteDisplay(sys->display.vd, NULL);
124     free(sys->display.title);
125 }
126
127 /*****************************************************************************
128  *
129  *****************************************************************************/
130 int vout_InitWrapper(vout_thread_t *vout)
131 {
132     vout_thread_sys_t *sys = vout->p;
133     vout_display_t *vd = sys->display.vd;
134     video_format_t source = vd->source;
135
136     /* XXX For non dr case, the current vout implementation force us to
137      * create at most 1 direct picture (otherwise the buffers will be kept
138      * referenced even through the Init/End.
139      */
140     sys->display.use_dr = !vout_IsDisplayFiltered(vd);
141     const bool allow_dr = !vd->info.has_pictures_invalid && sys->display.use_dr;
142
143     picture_pool_t *display_pool = vout_display_Pool(vd, allow_dr ? VOUT_MAX_PICTURES : 3);
144     if (allow_dr && picture_pool_GetSize(display_pool) >= VOUT_MIN_DIRECT_PICTURES) {
145         sys->decoder_pool = display_pool;
146         sys->display_pool = display_pool;
147         sys->is_decoder_pool_slow = vd->info.is_slow;
148     } else if (!sys->decoder_pool) {
149         sys->decoder_pool = picture_pool_NewFromFormat(&source, VOUT_MAX_PICTURES);
150         if (sys->display.use_dr)
151             sys->display_pool = display_pool;
152         else
153             sys->display_pool = picture_pool_Reserve(sys->decoder_pool, 1);;
154         sys->is_decoder_pool_slow = false;
155     }
156     sys->private_pool = picture_pool_Reserve(sys->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
157     sys->display.filtered = NULL;
158     return VLC_SUCCESS;
159 }
160
161 /*****************************************************************************
162  *
163  *****************************************************************************/
164 void vout_EndWrapper(vout_thread_t *vout)
165 {
166     vout_thread_sys_t *sys = vout->p;
167
168     assert(!sys->display.filtered);
169     if (sys->private_pool)
170         picture_pool_Delete(sys->private_pool);
171
172     if (sys->decoder_pool != sys->display_pool) {
173         if (!sys->display.use_dr)
174             picture_pool_Delete(sys->display_pool);
175         picture_pool_Delete(sys->decoder_pool);
176     }
177 }
178
179 /*****************************************************************************
180  *
181  *****************************************************************************/
182 void vout_ManageWrapper(vout_thread_t *vout)
183 {
184     vout_thread_sys_t *sys = vout->p;
185     vout_display_t *vd = sys->display.vd;
186
187     bool reset_display_pool = sys->display.use_dr && vout_AreDisplayPicturesInvalid(vd);
188     vout_ManageDisplay(vd, !sys->display.use_dr || reset_display_pool);
189
190     if (reset_display_pool)
191         sys->display_pool = vout_display_Pool(vd, 3);
192 }
193
194 /*****************************************************************************
195  * Render
196  *****************************************************************************/
197 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
198 {
199     vout_thread_sys_t *sys = vout->p;
200     vout_display_t *vd = sys->display.vd;
201
202     assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
203
204     if (sys->display.use_dr) {
205         vout_display_Prepare(vd, picture);
206     } else {
207         sys->display.filtered = vout_FilterDisplay(vd, picture);
208         if (sys->display.filtered)
209             vout_display_Prepare(vd, sys->display.filtered);
210     }
211 }
212
213 /*****************************************************************************
214  *
215  *****************************************************************************/
216 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
217 {
218     vout_thread_sys_t *sys = vout->p;
219     vout_display_t *vd = sys->display.vd;
220
221      vout_display_Display(vd, sys->display.filtered ? sys->display.filtered : picture);
222      sys->display.filtered = NULL;
223 }
224
225 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
226 {
227     /* Load configuration */
228     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
229     cfg->display.title = title;
230     const int display_width = var_CreateGetInteger(vout, "width");
231     const int display_height = var_CreateGetInteger(vout, "height");
232     cfg->display.width   = display_width > 0  ? display_width  : 0;
233     cfg->display.height  = display_height > 0 ? display_height : 0;
234     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
235     cfg->display.sar.num = 1; /* TODO monitor AR */
236     cfg->display.sar.den = 1;
237     unsigned zoom_den = 1000;
238     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
239     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
240     cfg->zoom.num = zoom_num;
241     cfg->zoom.den = zoom_den;
242     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
243     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
244     const int align_mask = var_CreateGetInteger(vout, "align");
245     if (align_mask & 0x1)
246         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
247     else if (align_mask & 0x2)
248         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
249     if (align_mask & 0x4)
250         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
251     else if (align_mask & 0x8)
252         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
253 }
254
255 #ifdef WIN32
256 static int Forward(vlc_object_t *object, char const *var,
257                    vlc_value_t oldval, vlc_value_t newval, void *data)
258 {
259     vout_thread_t *vout = (vout_thread_t*)object;
260
261     VLC_UNUSED(oldval);
262     VLC_UNUSED(data);
263     return var_Set(vout->p->display.vd, var, newval);
264 }
265 #endif
266