]> git.sesse.net Git - vlc/blob - src/video_output/vout_wrapper.c
Removed useless vlc_osd.h includes.
[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->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;
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
135     /* */
136     video_format_t source = vd->source;
137
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);
146     } else {
147         vout->p->fmt_out.i_sar_num    = 1;
148         vout->p->fmt_out.i_sar_den    = 1;
149     }
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);
158
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;
164
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.
168      */
169     sys->display.use_dr = !vout_IsDisplayFiltered(vd);
170     const bool allow_dr = !vd->info.has_pictures_invalid && sys->display.use_dr;
171
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;
181         else
182             sys->display_pool = picture_pool_Reserve(sys->decoder_pool, 1);;
183         sys->is_decoder_pool_slow = false;
184     }
185     sys->private_pool = picture_pool_Reserve(sys->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
186     sys->display.filtered = NULL;
187     return VLC_SUCCESS;
188 }
189
190 /*****************************************************************************
191  *
192  *****************************************************************************/
193 void vout_EndWrapper(vout_thread_t *vout)
194 {
195     vout_thread_sys_t *sys = vout->p;
196
197     assert(!sys->display.filtered);
198     if (sys->private_pool)
199         picture_pool_Delete(sys->private_pool);
200
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);
205     }
206 }
207
208 /*****************************************************************************
209  *
210  *****************************************************************************/
211 int vout_ManageWrapper(vout_thread_t *vout)
212 {
213     vout_thread_sys_t *sys = vout->p;
214     vout_display_t *vd = sys->display.vd;
215
216     while (sys->i_changes & (VOUT_ASPECT_CHANGE |
217                               VOUT_CROP_CHANGE)) {
218         /* */
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;
222
223             vout_SetDisplayAspect(vd, vout->p->fmt_in.i_sar_num, vout->p->fmt_in.i_sar_den);
224
225             sys->i_changes &= ~VOUT_ASPECT_CHANGE;
226         }
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 */
231             unsigned num = 0;
232             unsigned den = 0;
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);
245             }
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;
250         }
251
252     }
253
254     bool reset_display_pool = sys->display.use_dr && vout_AreDisplayPicturesInvalid(vd);
255     vout_ManageDisplay(vd, !sys->display.use_dr || reset_display_pool);
256
257     if (reset_display_pool)
258         sys->display_pool = vout_display_Pool(vd, 3);
259
260     return VLC_SUCCESS;
261 }
262
263 /*****************************************************************************
264  * Render
265  *****************************************************************************/
266 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
267 {
268     vout_thread_sys_t *sys = vout->p;
269     vout_display_t *vd = sys->display.vd;
270
271     assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
272
273     if (sys->display.use_dr) {
274         vout_display_Prepare(vd, picture);
275     } else {
276         sys->display.filtered = vout_FilterDisplay(vd, picture);
277         if (sys->display.filtered)
278             vout_display_Prepare(vd, sys->display.filtered);
279     }
280 }
281
282 /*****************************************************************************
283  *
284  *****************************************************************************/
285 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
286 {
287     vout_thread_sys_t *sys = vout->p;
288     vout_display_t *vd = sys->display.vd;
289
290      vout_display_Display(vd, sys->display.filtered ? sys->display.filtered : picture);
291      sys->display.filtered = NULL;
292 }
293
294 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
295 {
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;
322 }
323
324 #ifdef WIN32
325 static int Forward(vlc_object_t *object, char const *var,
326                    vlc_value_t oldval, vlc_value_t newval, void *data)
327 {
328     vout_thread_t *vout = (vout_thread_t*)object;
329
330     VLC_UNUSED(oldval);
331     VLC_UNUSED(data);
332     return var_Set(vout->p->display.vd, var, newval);
333 }
334 #endif
335