]> git.sesse.net Git - vlc/blob - src/video_output/vout_wrapper.c
Removed picture_heap_t::i_aspect/width/height/chroma.
[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
49 struct picture_sys_t {
50     picture_t *direct;
51 };
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static void VoutGetDisplayCfg(vout_thread_t *,
57                               vout_display_cfg_t *, const char *title);
58 #ifdef WIN32
59 static int  Forward(vlc_object_t *, char const *,
60                     vlc_value_t, vlc_value_t, void *);
61 #endif
62
63 /*****************************************************************************
64  *
65  *****************************************************************************/
66 int vout_OpenWrapper(vout_thread_t *vout, const char *name)
67 {
68     vout_sys_t *sys;
69
70     msg_Dbg(vout, "Opening vout display wrapper");
71
72     /* */
73     sys = malloc(sizeof(*sys));
74     if (!sys)
75         return VLC_ENOMEM;
76
77     sys->title = var_CreateGetNonEmptyString(vout, "video-title");
78
79     /* */
80     video_format_t source   = vout->fmt_render;
81     source.i_visible_width  = source.i_width;
82     source.i_visible_height = source.i_height;
83     source.i_x_offset       = 0;
84     source.i_y_offset       = 0;
85
86     vout_display_state_t state;
87     VoutGetDisplayCfg(vout, &state.cfg, sys->title);
88     state.is_on_top = var_CreateGetBool(vout, "video-on-top");
89     state.sar.num = 0;
90     state.sar.den = 0;
91
92     const mtime_t double_click_timeout = 300000;
93     const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
94
95     sys->vd = vout_NewDisplay(vout, &source, &state, name ? name : "$vout",
96                               double_click_timeout, hide_timeout);
97     /* If we need to video filter and it fails, then try a splitter
98      * XXX it is a hack for now FIXME */
99     if (name && !sys->vd)
100         sys->vd = vout_NewSplitter(vout, &source, &state, "$vout", name,
101                                    double_click_timeout, hide_timeout);
102     if (!sys->vd) {
103         free(sys->title);
104         free(sys);
105         return VLC_EGENERIC;
106     }
107
108     /* */
109 #ifdef WIN32
110     var_Create(vout, "direct3d-desktop", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
111     var_AddCallback(vout, "direct3d-desktop", Forward, NULL);
112     var_Create(vout, "video-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
113     var_AddCallback(vout, "video-wallpaper", Forward, NULL);
114 #endif
115
116     /* */
117     vout->p->p_sys = sys;
118
119     return VLC_SUCCESS;
120 }
121
122 /*****************************************************************************
123  *
124  *****************************************************************************/
125 void vout_CloseWrapper(vout_thread_t *vout)
126 {
127     vout_sys_t *sys = vout->p->p_sys;
128
129 #ifdef WIN32
130     var_DelCallback(vout, "direct3d-desktop", Forward, NULL);
131     var_DelCallback(vout, "video-wallpaper", Forward, NULL);
132 #endif
133     vout_DeleteDisplay(sys->vd, NULL);
134     free(sys->title);
135     free(sys );
136 }
137
138 /*****************************************************************************
139  *
140  *****************************************************************************/
141 int vout_InitWrapper(vout_thread_t *vout)
142 {
143     vout_sys_t *sys = vout->p->p_sys;
144     vout_display_t *vd = sys->vd;
145
146     /* */
147     video_format_t source = vd->source;
148
149     vout->fmt_out.i_chroma         = source.i_chroma;
150     vout->fmt_out.i_width          =
151     vout->fmt_out.i_visible_width  = source.i_width;
152     vout->fmt_out.i_height         =
153     vout->fmt_out.i_visible_height = source.i_height;
154     vout->fmt_out.i_sar_num        = source.i_sar_num;
155     vout->fmt_out.i_sar_den        = source.i_sar_den;
156     vout->fmt_out.i_x_offset       = 0;
157     vout->fmt_out.i_y_offset       = 0;
158     vout->fmt_out.i_rmask          = source.i_rmask;
159     vout->fmt_out.i_gmask          = source.i_gmask;
160     vout->fmt_out.i_bmask          = source.i_bmask;
161
162     if (vout->fmt_in.i_visible_width  != source.i_visible_width ||
163         vout->fmt_in.i_visible_height != source.i_visible_height ||
164         vout->fmt_in.i_x_offset       != source.i_x_offset ||
165         vout->fmt_in.i_y_offset       != source.i_y_offset )
166         vout->i_changes |= VOUT_CROP_CHANGE;
167
168     if (vout->b_on_top)
169         vout_SetWindowState(vd, VOUT_WINDOW_STATE_ABOVE);
170
171     /* XXX For non dr case, the current vout implementation force us to
172      * create at most 1 direct picture (otherwise the buffers will be kept
173      * referenced even through the Init/End.
174      */
175     sys->use_dr = !vout_IsDisplayFiltered(vd);
176     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
177     const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
178     for (vout->output.i_pictures = 0;
179             vout->output.i_pictures < picture_max;
180                 vout->output.i_pictures++) {
181         /* Find an empty picture slot */
182         picture_t *picture = NULL;
183         for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
184             if (vout->p_picture[index].i_status == FREE_PICTURE) {
185                 picture = &vout->p_picture[index];
186                 break;
187             }
188         }
189         if (!picture)
190             break;
191         memset(picture, 0, sizeof(*picture));
192
193         picture->p_sys = malloc(sizeof(*picture->p_sys));
194
195         if (sys->use_dr) {
196             picture_pool_t *pool = vout_display_Pool(vd, picture_max);
197             if (!pool)
198                 break;
199             picture_t *direct = picture_pool_Get(pool);
200             if (!direct)
201                 break;
202             picture->format = direct->format;
203             picture->i_planes = direct->i_planes;
204             for (int i = 0; i < direct->i_planes; i++)
205                 picture->p[i] = direct->p[i];
206             picture->b_slow = vd->info.is_slow;
207
208             picture->p_sys->direct = direct;
209         } else {
210             vout_AllocatePicture(VLC_OBJECT(vd), picture,
211                                  vd->source.i_chroma,
212                                  vd->source.i_width, vd->source.i_height,
213                                  vd->source.i_sar_num, vd->source.i_sar_den);
214             if (!picture->i_planes)
215                 break;
216             picture->p_sys->direct = NULL;
217         }
218         picture->i_status = DESTROYED_PICTURE;
219         picture->i_type    = DIRECT_PICTURE;
220
221         vout->output.pp_picture[vout->output.i_pictures] = picture;
222     }
223     return VLC_SUCCESS;
224 }
225
226 /*****************************************************************************
227  *
228  *****************************************************************************/
229 void vout_EndWrapper(vout_thread_t *vout)
230 {
231     vout_sys_t *sys = vout->p->p_sys;
232
233     for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
234         picture_t *picture = &vout->p_picture[i];
235
236         if (picture->i_type != DIRECT_PICTURE)
237             continue;
238
239         if (picture->p_sys->direct)
240             picture_Release(picture->p_sys->direct);
241         if (!sys->use_dr)
242             free(picture->p_data_orig);
243         free(picture->p_sys);
244
245         picture->i_status = FREE_PICTURE;
246     }
247     if (sys->use_dr && vout_AreDisplayPicturesInvalid(sys->vd))
248         vout_ManageDisplay(sys->vd, true);
249 }
250
251 /*****************************************************************************
252  *
253  *****************************************************************************/
254 int vout_ManageWrapper(vout_thread_t *vout)
255 {
256     vout_sys_t *sys = vout->p->p_sys;
257     vout_display_t *vd = sys->vd;
258
259     while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
260                               VOUT_ASPECT_CHANGE |
261                               VOUT_ZOOM_CHANGE |
262                               VOUT_SCALE_CHANGE |
263                               VOUT_ON_TOP_CHANGE |
264                               VOUT_CROP_CHANGE)) {
265         /* */
266         if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
267             vout->b_fullscreen = !vout->b_fullscreen;
268
269             var_SetBool(vout, "fullscreen", vout->b_fullscreen);
270             vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
271             vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
272         }
273         if (vout->i_changes & VOUT_ASPECT_CHANGE) {
274             vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
275             vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
276
277             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
278
279             vout->i_changes &= ~VOUT_ASPECT_CHANGE;
280         }
281         if (vout->i_changes & VOUT_ZOOM_CHANGE) {
282             const float zoom = var_GetFloat(vout, "scale");
283
284             unsigned den = ZOOM_FP_FACTOR;
285             unsigned num = den * zoom;
286             if (num < (ZOOM_FP_FACTOR+9) / 10)
287                 num = (ZOOM_FP_FACTOR+9) / 10;
288             else if (num > ZOOM_FP_FACTOR * 10)
289                 num = ZOOM_FP_FACTOR * 10;
290
291             vout_SetDisplayZoom(vd, num, den);
292
293             vout->i_changes &= ~VOUT_ZOOM_CHANGE;
294         }
295         if (vout->i_changes & VOUT_SCALE_CHANGE) {
296             const bool is_display_filled = var_GetBool(vout, "autoscale");
297
298             vout_SetDisplayFilled(vd, is_display_filled);
299
300             vout->i_changes &= ~VOUT_SCALE_CHANGE;
301         }
302         if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
303             vout_SetWindowState(vd, vout->b_on_top
304                 ? VOUT_WINDOW_STATE_ABOVE
305                 : VOUT_WINDOW_STATE_NORMAL);
306
307             vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
308         }
309         if (vout->i_changes & VOUT_CROP_CHANGE) {
310             const video_format_t crop = vout->fmt_in;
311             const video_format_t org = vout->fmt_render;
312             /* FIXME because of rounding errors, the reconstructed ratio is wrong */
313             unsigned num = 0;
314             unsigned den = 0;
315             if (crop.i_x_offset == org.i_x_offset &&
316                 crop.i_visible_width == org.i_visible_width &&
317                 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
318                 vlc_ureduce(&num, &den,
319                             crop.i_visible_width * crop.i_sar_num,
320                             crop.i_visible_height * crop.i_sar_den, 0);
321             } else if (crop.i_y_offset == org.i_y_offset &&
322                        crop.i_visible_height == org.i_visible_height &&
323                        crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
324                 vlc_ureduce(&num, &den,
325                             crop.i_visible_width * crop.i_sar_num,
326                             crop.i_visible_height * crop.i_sar_den, 0);
327             }
328             vout_SetDisplayCrop(vd, num, den,
329                                 crop.i_x_offset, crop.i_y_offset,
330                                 crop.i_visible_width, crop.i_visible_height);
331             vout->i_changes &= ~VOUT_CROP_CHANGE;
332         }
333
334     }
335
336     if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
337         vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
338     }
339     vout_ManageDisplay(vd, !sys->use_dr);
340     return VLC_SUCCESS;
341 }
342
343 /*****************************************************************************
344  * Render
345  *****************************************************************************/
346 void vout_RenderWrapper(vout_thread_t *vout, picture_t *picture)
347 {
348     vout_sys_t *sys = vout->p->p_sys;
349     vout_display_t *vd = sys->vd;
350
351     assert(sys->use_dr || !picture->p_sys->direct);
352     assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
353
354     if (sys->use_dr) {
355         assert(picture->p_sys->direct);
356         vout_display_Prepare(vd, picture->p_sys->direct);
357     } else {
358         picture_t *direct = picture->p_sys->direct = vout_FilterDisplay(vd, picture);
359         if (direct) {
360             vout_display_Prepare(vd, direct);
361         }
362     }
363 }
364
365 /*****************************************************************************
366  *
367  *****************************************************************************/
368 void vout_DisplayWrapper(vout_thread_t *vout, picture_t *picture)
369 {
370     vout_sys_t *sys = vout->p->p_sys;
371     vout_display_t *vd = sys->vd;
372
373     picture_t *direct = picture->p_sys->direct;
374     if (!direct)
375         return;
376
377     /* XXX This is a hack that will work with current vout_display_t modules */
378     if (sys->use_dr)
379         picture_Hold(direct);
380
381      vout_display_Display(vd, direct);
382
383      if (sys->use_dr) {
384          for (int i = 0; i < picture->i_planes; i++) {
385              picture->p[i].p_pixels = direct->p[i].p_pixels;
386              picture->p[i].i_pitch  = direct->p[i].i_pitch;
387              picture->p[i].i_lines  = direct->p[i].i_lines;
388          }
389      } else {
390          picture->p_sys->direct = NULL;
391      }
392 }
393
394 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
395 {
396     /* Load configuration */
397     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
398     cfg->display.title = title;
399     const int display_width = var_CreateGetInteger(vout, "width");
400     const int display_height = var_CreateGetInteger(vout, "height");
401     cfg->display.width   = display_width > 0  ? display_width  : 0;
402     cfg->display.height  = display_height > 0 ? display_height : 0;
403     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
404     cfg->display.sar.num = 1; /* TODO monitor AR */
405     cfg->display.sar.den = 1;
406     unsigned zoom_den = 1000;
407     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
408     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
409     cfg->zoom.num = zoom_num;
410     cfg->zoom.den = zoom_den;
411     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
412     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
413     const int align_mask = var_CreateGetInteger(vout, "align");
414     if (align_mask & 0x1)
415         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
416     else if (align_mask & 0x2)
417         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
418     if (align_mask & 0x4)
419         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
420     else if (align_mask & 0x8)
421         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
422 }
423
424 #ifdef WIN32
425 static int Forward(vlc_object_t *object, char const *var,
426                    vlc_value_t oldval, vlc_value_t newval, void *data)
427 {
428     vout_thread_t *vout = (vout_thread_t*)object;
429
430     VLC_UNUSED(oldval);
431     VLC_UNUSED(data);
432     return var_Set(vout->p_sys->vd, var, newval);
433 }
434 #endif