]> git.sesse.net Git - vlc/blob - src/video_output/vout_wrapper.c
Reworked the way pictures are handled by the vout 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     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     vout->fmt_out.i_sar_num        = source.i_sar_num;
164     vout->fmt_out.i_sar_den        = source.i_sar_den;
165     vout->fmt_out.i_x_offset       = 0;
166     vout->fmt_out.i_y_offset       = 0;
167     vout->fmt_out.i_rmask          = source.i_rmask;
168     vout->fmt_out.i_gmask          = source.i_gmask;
169     vout->fmt_out.i_bmask          = source.i_bmask;
170
171     if (vout->fmt_in.i_visible_width  != source.i_visible_width ||
172         vout->fmt_in.i_visible_height != source.i_visible_height ||
173         vout->fmt_in.i_x_offset       != source.i_x_offset ||
174         vout->fmt_in.i_y_offset       != source.i_y_offset )
175         vout->p->i_changes |= VOUT_CROP_CHANGE;
176
177 #warning "vout_InitWrapper: vout_SetWindowState should NOT be called there"
178     if (vout->p->b_on_top)
179         vout_SetWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
180
181     /* XXX For non dr case, the current vout implementation force us to
182      * create at most 1 direct picture (otherwise the buffers will be kept
183      * referenced even through the Init/End.
184      */
185     sys->use_dr = !vout_IsDisplayFiltered(vd);
186     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
187
188     picture_pool_t *display_pool = vout_display_Pool(vd, allow_dr ? VOUT_MAX_PICTURES : 3);
189     if (allow_dr && picture_pool_GetSize(display_pool) >= VOUT_MIN_DIRECT_PICTURES) {
190         vout->p->decoder_pool = display_pool;
191         vout->p->display_pool = display_pool;
192         vout->p->is_decoder_pool_slow = vd->info.is_slow;
193     } else if (!vout->p->decoder_pool) {
194         vout->p->decoder_pool = picture_pool_NewFromFormat(&source, VOUT_MAX_PICTURES);
195         if (sys->use_dr)
196             vout->p->display_pool = display_pool;
197         else
198             vout->p->display_pool = picture_pool_Reserve(vout->p->decoder_pool, 1);;
199         vout->p->is_decoder_pool_slow = false;
200     }
201     vout->p->private_pool = picture_pool_Reserve(vout->p->decoder_pool, 3); /* XXX 2 for filter, 1 for SPU */
202     sys->filtered = NULL;
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  *
208  *****************************************************************************/
209 void vout_EndWrapper(vout_thread_t *vout)
210 {
211     vout_sys_t *sys = vout->p->p_sys;
212
213     assert(!sys->filtered);
214     if (vout->p->private_pool)
215         picture_pool_Delete(vout->p->private_pool);
216
217     if (vout->p->decoder_pool != vout->p->display_pool) {
218         if (!sys->use_dr)
219             picture_pool_Delete(vout->p->display_pool);
220         picture_pool_Delete(vout->p->decoder_pool);
221     }
222 }
223
224 /*****************************************************************************
225  *
226  *****************************************************************************/
227 int vout_ManageWrapper(vout_thread_t *vout)
228 {
229     vout_sys_t *sys = vout->p->p_sys;
230     vout_display_t *vd = sys->vd;
231
232     while (vout->p->i_changes & (VOUT_FULLSCREEN_CHANGE |
233                               VOUT_ASPECT_CHANGE |
234                               VOUT_ZOOM_CHANGE |
235                               VOUT_SCALE_CHANGE |
236                               VOUT_ON_TOP_CHANGE |
237                               VOUT_CROP_CHANGE)) {
238         /* */
239         if (vout->p->i_changes & VOUT_FULLSCREEN_CHANGE) {
240             vout->p->b_fullscreen = !vout->p->b_fullscreen;
241
242             var_SetBool(vout, "fullscreen", vout->p->b_fullscreen);
243             vout_SetDisplayFullscreen(vd, vout->p->b_fullscreen);
244             vout->p->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
245         }
246         if (vout->p->i_changes & VOUT_ASPECT_CHANGE) {
247             vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
248             vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
249
250             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
251
252             vout->p->i_changes &= ~VOUT_ASPECT_CHANGE;
253         }
254         if (vout->p->i_changes & VOUT_ZOOM_CHANGE) {
255             const float zoom = var_GetFloat(vout, "scale");
256
257             unsigned den = ZOOM_FP_FACTOR;
258             unsigned num = den * zoom;
259             if (num < (ZOOM_FP_FACTOR+9) / 10)
260                 num = (ZOOM_FP_FACTOR+9) / 10;
261             else if (num > ZOOM_FP_FACTOR * 10)
262                 num = ZOOM_FP_FACTOR * 10;
263
264             vout_SetDisplayZoom(vd, num, den);
265
266             vout->p->i_changes &= ~VOUT_ZOOM_CHANGE;
267         }
268         if (vout->p->i_changes & VOUT_SCALE_CHANGE) {
269             const bool is_display_filled = var_GetBool(vout, "autoscale");
270
271             vout_SetDisplayFilled(vd, is_display_filled);
272
273             vout->p->i_changes &= ~VOUT_SCALE_CHANGE;
274         }
275         if (vout->p->i_changes & VOUT_ON_TOP_CHANGE) {
276             vout_SetWindowState(vd, vout->p->b_on_top
277                 ? VOUT_WINDOW_STATE_ABOVE
278                 : VOUT_WINDOW_STATE_NORMAL);
279
280             vout->p->i_changes &= ~VOUT_ON_TOP_CHANGE;
281         }
282         if (vout->p->i_changes & VOUT_CROP_CHANGE) {
283             const video_format_t crop = vout->fmt_in;
284             const video_format_t org = vout->fmt_render;
285             /* FIXME because of rounding errors, the reconstructed ratio is wrong */
286             unsigned num = 0;
287             unsigned den = 0;
288             if (crop.i_x_offset == org.i_x_offset &&
289                 crop.i_visible_width == org.i_visible_width &&
290                 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
291                 vlc_ureduce(&num, &den,
292                             crop.i_visible_width * crop.i_sar_num,
293                             crop.i_visible_height * crop.i_sar_den, 0);
294             } else if (crop.i_y_offset == org.i_y_offset &&
295                        crop.i_visible_height == org.i_visible_height &&
296                        crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
297                 vlc_ureduce(&num, &den,
298                             crop.i_visible_width * crop.i_sar_num,
299                             crop.i_visible_height * crop.i_sar_den, 0);
300             }
301             vout_SetDisplayCrop(vd, num, den,
302                                 crop.i_x_offset, crop.i_y_offset,
303                                 crop.i_visible_width, crop.i_visible_height);
304             vout->p->i_changes &= ~VOUT_CROP_CHANGE;
305         }
306
307     }
308
309     bool reset_display_pool = sys->use_dr && vout_AreDisplayPicturesInvalid(vd);
310     vout_ManageDisplay(vd, !sys->use_dr || reset_display_pool);
311
312     if (reset_display_pool)
313         vout->p->display_pool = vout_display_Pool(vd, 3);
314
315     return VLC_SUCCESS;
316 }
317
318 /*****************************************************************************
319  * Render
320  *****************************************************************************/
321 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
322 {
323     vout_sys_t *sys = vout->p->p_sys;
324     vout_display_t *vd = sys->vd;
325
326     assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
327
328     if (sys->use_dr) {
329         vout_display_Prepare(vd, picture);
330     } else {
331         sys->filtered = vout_FilterDisplay(vd, picture);
332         if (sys->filtered)
333             vout_display_Prepare(vd, sys->filtered);
334     }
335 }
336
337 /*****************************************************************************
338  *
339  *****************************************************************************/
340 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
341 {
342     vout_sys_t *sys = vout->p->p_sys;
343     vout_display_t *vd = sys->vd;
344
345      vout_display_Display(vd, sys->filtered ? sys->filtered : picture);
346      sys->filtered = NULL;
347 }
348
349 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
350 {
351     /* Load configuration */
352     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
353     cfg->display.title = title;
354     const int display_width = var_CreateGetInteger(vout, "width");
355     const int display_height = var_CreateGetInteger(vout, "height");
356     cfg->display.width   = display_width > 0  ? display_width  : 0;
357     cfg->display.height  = display_height > 0 ? display_height : 0;
358     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
359     cfg->display.sar.num = 1; /* TODO monitor AR */
360     cfg->display.sar.den = 1;
361     unsigned zoom_den = 1000;
362     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
363     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
364     cfg->zoom.num = zoom_num;
365     cfg->zoom.den = zoom_den;
366     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
367     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
368     const int align_mask = var_CreateGetInteger(vout, "align");
369     if (align_mask & 0x1)
370         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
371     else if (align_mask & 0x2)
372         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
373     if (align_mask & 0x4)
374         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
375     else if (align_mask & 0x8)
376         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
377 }
378
379 #ifdef WIN32
380 static int Forward(vlc_object_t *object, char const *var,
381                    vlc_value_t oldval, vlc_value_t newval, void *data)
382 {
383     vout_thread_t *vout = (vout_thread_t*)object;
384
385     VLC_UNUSED(oldval);
386     VLC_UNUSED(data);
387     return var_Set(vout->p->p_sys->vd, var, newval);
388 }
389 #endif