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