]> git.sesse.net Git - vlc/blob - modules/video_output/wrapper.c
Fixed VOUT_DISPLAY_RESET_PICTURES support in wrapper.
[vlc] / modules / video_output / 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 "../video_filter/filter_common.h"
37 #include <assert.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open (vlc_object_t *, const char *module);
43 static void Close(vlc_object_t *);
44
45 #define DECLARE_OPEN(name) \
46         static int Open##name(vlc_object_t *object) { return Open(object, #name); }
47
48 DECLARE_OPEN(aalib);
49 DECLARE_OPEN(caca);
50 DECLARE_OPEN(sdl);
51 DECLARE_OPEN(xcb_x11);
52 DECLARE_OPEN(xcb_xv);
53 DECLARE_OPEN(dummy);
54 DECLARE_OPEN(fb);
55 DECLARE_OPEN(directfb);
56 DECLARE_OPEN(yuv);
57 DECLARE_OPEN(snapshot);
58 DECLARE_OPEN(vmem);
59
60 #undef DECLARE_OPEN
61
62 #define DECLARE_MODULE(name, priority)                  \
63     set_description( "Video display "#name" wrapper" )  \
64     set_shortname( "Video display "#name" wrapper" )    \
65     set_capability( "video output", priority )          \
66     set_callbacks( Open##name, Close )                  \
67     add_shortcut( #name )
68
69 vlc_module_begin()
70     set_category( CAT_VIDEO )
71     set_subcategory( SUBCAT_VIDEO_VOUT )
72
73     DECLARE_MODULE(aalib, 10)
74
75     add_submodule()
76     DECLARE_MODULE(caca, 12)
77
78     add_submodule()
79     DECLARE_MODULE(sdl, 60)
80
81     add_submodule()
82     DECLARE_MODULE(xcb_x11, 75)
83
84     add_submodule()
85     DECLARE_MODULE(xcb_xv, 155)
86
87     add_submodule()
88     DECLARE_MODULE(dummy, 1)
89
90     add_submodule()
91     DECLARE_MODULE(fb, 30)
92
93     add_submodule()
94     DECLARE_MODULE(directfb, 60)
95
96     add_submodule()
97     DECLARE_MODULE(yuv, 0)
98
99     add_submodule()
100     DECLARE_MODULE(snapshot, 0)
101
102     add_submodule()
103     DECLARE_MODULE(vmem, 0)
104
105 vlc_module_end()
106
107 #undef DECLARE_MODULE
108
109 /*****************************************************************************
110  *
111  *****************************************************************************/
112 struct vout_sys_t {
113     char           *title;
114     vout_display_t *vd;
115     bool           use_dr;
116 };
117
118 struct picture_sys_t {
119     picture_t *direct;
120 };
121
122 /*****************************************************************************
123  * Local prototypes
124  *****************************************************************************/
125 static int  Init   (vout_thread_t *);
126 static void End    (vout_thread_t *);
127 static int  Manage (vout_thread_t *);
128 static void Render (vout_thread_t *, picture_t *);
129 static void Display(vout_thread_t *, picture_t *);
130
131 static void VoutGetDisplayCfg(vout_thread_t *,
132                               vout_display_cfg_t *, const char *title);
133
134 /*****************************************************************************
135  *
136  *****************************************************************************/
137 static int Open(vlc_object_t *object, const char *module)
138 {
139     vout_thread_t *vout = (vout_thread_t *)object;
140     vout_sys_t *sys;
141
142     msg_Warn(vout, "Opening vout display wrapper");
143
144     /* */
145     sys = malloc(sizeof(*sys));
146     if (!sys)
147         return VLC_ENOMEM;
148
149     sys->title = var_CreateGetNonEmptyString(vout, "video-title");
150
151     /* */
152     video_format_t source   = vout->fmt_render;
153     source.i_visible_width  = source.i_width;
154     source.i_visible_height = source.i_height;
155     source.i_x_offset       = 0;
156     source.i_y_offset       = 0;
157
158     vout_display_state_t state;
159     VoutGetDisplayCfg(vout, &state.cfg, sys->title);
160     state.is_on_top = var_CreateGetBool(vout, "video-on-top");
161     state.sar.num = 0;
162     state.sar.den = 0;
163
164     const mtime_t double_click_timeout = 300000;
165     const mtime_t hide_timeout = var_CreateGetInteger(vout, "mouse-hide-timeout") * 1000;
166
167     sys->vd = vout_NewDisplay(vout, &source, &state, module,
168                               double_click_timeout, hide_timeout);
169     if (!sys->vd) {
170         free(sys->title);
171         free(sys);
172         return VLC_EGENERIC;
173     }
174
175     /* */
176     vout->pf_init    = Init;
177     vout->pf_end     = End;
178     vout->pf_manage  = Manage;
179     vout->pf_render  = Render;
180     vout->pf_display = Display;
181     vout->pf_control = NULL;
182     vout->p_sys      = sys;
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  *
189  *****************************************************************************/
190 static void Close(vlc_object_t *object)
191 {
192     vout_thread_t *vout = (vout_thread_t *)object;
193     vout_sys_t *sys = vout->p_sys;
194
195     vout_DeleteDisplay(sys->vd, NULL);
196     free(sys->title);
197     free(sys );
198 }
199
200 /*****************************************************************************
201  *
202  *****************************************************************************/
203 static int Init(vout_thread_t *vout)
204 {
205     vout_sys_t *sys = vout->p_sys;
206     vout_display_t *vd = sys->vd;
207
208     /* */
209     video_format_t source = vd->source;
210
211     vout->output.i_chroma = source.i_chroma;
212     vout->output.i_width  = source.i_width;
213     vout->output.i_height = source.i_height;
214     vout->output.i_aspect = source.i_aspect;
215     vout->output.i_rmask  = source.i_rmask;
216     vout->output.i_gmask  = source.i_gmask;
217     vout->output.i_bmask  = source.i_bmask;
218     vout->output.pf_setpalette = NULL; /* FIXME What to do ? Seems unused anyway */
219
220     /* also set fmt_out (completly broken API) */
221     vout->fmt_out.i_chroma         = vout->output.i_chroma;
222     vout->fmt_out.i_width          =
223     vout->fmt_out.i_visible_width  = vout->output.i_width;
224     vout->fmt_out.i_height         =
225     vout->fmt_out.i_visible_height = vout->output.i_height;
226     vout->fmt_out.i_aspect         = vout->output.i_aspect;
227     vout->fmt_out.i_x_offset       = 0;
228     vout->fmt_out.i_y_offset       = 0;
229
230     /* TODO */
231 #if 0
232     if (p_vout->fmt_render.i_visible_width  != source.i_visible_width ||
233         p_vout->fmt_render.i_visible_height != source.i_visible_height ||
234         p_vout->fmt_render.i_x_offset != source.i_x_offset ||
235         p_vout->fmt_render.i_y_offset != source.i_y_offset )
236     {
237         p_vout->i_changes |= VOUT_CROP_CHANGE;
238     }
239 #endif
240     if (vout->b_on_top)
241         vout_SetDisplayOnTop(vd, true);
242
243     /* XXX For non dr case, the current vout implementation force us to
244      * create at most 1 direct picture (otherwise the buffers will be kept
245      * referenced even through the Init/End.
246      */
247     sys->use_dr = !vout_IsDisplayFiltered(vd);
248     const bool allow_dr = !vd->info.has_pictures_invalid && sys->use_dr;
249     const int picture_max = allow_dr ? VOUT_MAX_PICTURES : 1;
250     for (vout->output.i_pictures = 0;
251             vout->output.i_pictures < picture_max;
252                 vout->output.i_pictures++) {
253         /* Find an empty picture slot */
254         picture_t *picture = NULL;
255         for (int index = 0; index < VOUT_MAX_PICTURES; index++) {
256             if (vout->p_picture[index].i_status == FREE_PICTURE) {
257                 picture = &vout->p_picture[index];
258                 break;
259             }
260         }
261         if (!picture)
262             break;
263         memset(picture, 0, sizeof(*picture));
264
265         picture->p_sys = malloc(sizeof(*picture->p_sys));
266
267         if (sys->use_dr) {
268             picture_t *direct = vout_display_Get(vd);
269             if (!direct)
270                 break;
271             picture->format = direct->format;
272             picture->i_planes = direct->i_planes;
273             for (int i = 0; i < direct->i_planes; i++)
274                 picture->p[i] = direct->p[i];
275             picture->b_slow = vd->info.is_slow;
276
277             picture->p_sys->direct = direct;
278         } else {
279             vout_AllocatePicture(VLC_OBJECT(vd), picture,
280                                  vd->source.i_chroma,
281                                  vd->source.i_width, vd->source.i_height,
282                                  vd->source.i_aspect);
283             if (!picture->i_planes)
284                 break;
285             picture->p_sys->direct = NULL;
286         }
287         picture->i_status = DESTROYED_PICTURE;
288         picture->i_type    = DIRECT_PICTURE;
289
290         vout->output.pp_picture[vout->output.i_pictures] = picture;
291     }
292     return VLC_SUCCESS;
293 }
294
295 /*****************************************************************************
296  *
297  *****************************************************************************/
298 static void End(vout_thread_t *vout)
299 {
300     vout_sys_t *sys = vout->p_sys;
301
302     for (int i = 0; i < VOUT_MAX_PICTURES; i++) {
303         picture_t *picture = &vout->p_picture[i];
304
305         if (picture->i_type != DIRECT_PICTURE)
306             continue;
307
308         if (picture->p_sys->direct)
309             picture_Release(picture->p_sys->direct);
310         if (!sys->use_dr)
311             free(picture->p_data_orig);
312         free(picture->p_sys);
313
314         picture->i_status = FREE_PICTURE;
315     }
316     if (sys->use_dr && vout_AreDisplayPicturesInvalid(sys->vd))
317         vout_ManageDisplay(sys->vd, true);
318 }
319
320 /*****************************************************************************
321  *
322  *****************************************************************************/
323 static int Manage(vout_thread_t *vout)
324 {
325     vout_sys_t *sys = vout->p_sys;
326     vout_display_t *vd = sys->vd;
327
328     while (vout->i_changes & (VOUT_FULLSCREEN_CHANGE |
329                               VOUT_ASPECT_CHANGE |
330                               VOUT_ZOOM_CHANGE |
331                               VOUT_SCALE_CHANGE |
332                               VOUT_ON_TOP_CHANGE |
333                               VOUT_CROP_CHANGE)) {
334         /* */
335         if (vout->i_changes & VOUT_FULLSCREEN_CHANGE) {
336             vout->b_fullscreen = !vout->b_fullscreen;
337
338             var_SetBool(vout, "fullscreen", vout->b_fullscreen);
339             vout_SetDisplayFullscreen(vd, vout->b_fullscreen);
340             vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
341         }
342         if (vout->i_changes & VOUT_ASPECT_CHANGE) {
343             vout->output.i_aspect   =
344             vout->fmt_out.i_aspect  = vout->fmt_in.i_aspect;
345             vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
346             vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
347
348             vout_SetDisplayAspect(vd, vout->fmt_in.i_sar_num, vout->fmt_in.i_sar_den);
349
350             vout->i_changes &= ~VOUT_ASPECT_CHANGE;
351         }
352         if (vout->i_changes & VOUT_ZOOM_CHANGE) {
353             const float zoom = var_GetFloat(vout, "scale");
354
355             unsigned den = ZOOM_FP_FACTOR;
356             unsigned num = den * zoom;
357             if (num < (ZOOM_FP_FACTOR+9) / 10)
358                 num = (ZOOM_FP_FACTOR+9) / 10;
359             else if (num > ZOOM_FP_FACTOR * 10)
360                 num = ZOOM_FP_FACTOR * 10;
361
362             vout_SetDisplayZoom(vd, num, den);
363
364             vout->i_changes &= ~VOUT_ZOOM_CHANGE;
365         }
366         if (vout->i_changes & VOUT_SCALE_CHANGE) {
367             const bool is_display_filled = var_GetBool(vout, "autoscale");
368
369             vout_SetDisplayFilled(vd, is_display_filled);
370
371             vout->i_changes &= ~VOUT_SCALE_CHANGE;
372         }
373         if (vout->i_changes & VOUT_ON_TOP_CHANGE) {
374             vout_SetDisplayOnTop(vd, vout->b_on_top);
375
376             vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
377         }
378         if (vout->i_changes & VOUT_CROP_CHANGE) {
379             const video_format_t crop = vout->fmt_in;
380             const video_format_t org = vout->fmt_render;
381             /* FIXME because of rounding errors, the reconstructed ratio is wrong */
382             unsigned num = 0;
383             unsigned den = 0;
384             if (crop.i_x_offset == org.i_x_offset &&
385                 crop.i_visible_width == org.i_visible_width &&
386                 crop.i_y_offset == org.i_y_offset + (org.i_visible_height - crop.i_visible_height)/2) {
387                 vlc_ureduce(&num, &den,
388                             crop.i_visible_width * crop.i_sar_num,
389                             crop.i_visible_height * crop.i_sar_den, 0);
390             } else if (crop.i_y_offset == org.i_y_offset &&
391                        crop.i_visible_height == org.i_visible_height &&
392                        crop.i_x_offset == org.i_x_offset + (org.i_visible_width - crop.i_visible_width)/2) {
393                 vlc_ureduce(&num, &den,
394                             crop.i_visible_width * crop.i_sar_num,
395                             crop.i_visible_height * crop.i_sar_den, 0);
396             }
397             vout_SetDisplayCrop(vd, num, den,
398                                 crop.i_x_offset, crop.i_y_offset,
399                                 crop.i_visible_width, crop.i_visible_height);
400             vout->i_changes &= ~VOUT_CROP_CHANGE;
401         }
402
403     }
404
405     if (sys->use_dr && vout_AreDisplayPicturesInvalid(vd)) {
406         vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
407     }
408     vout_ManageDisplay(vd, !sys->use_dr);
409     return VLC_SUCCESS;
410 }
411
412 /*****************************************************************************
413  * Render
414  *****************************************************************************/
415 static void Render(vout_thread_t *vout, picture_t *picture)
416 {
417     vout_sys_t *sys = vout->p_sys;
418     vout_display_t *vd = sys->vd;
419
420     assert(sys->use_dr || !picture->p_sys->direct);
421     assert(vout_IsDisplayFiltered(vd) == !sys->use_dr);
422
423     if (sys->use_dr) {
424         assert(picture->p_sys->direct);
425         vout_display_Prepare(vd, picture->p_sys->direct);
426     } else {
427         picture_t *direct = picture->p_sys->direct = vout_FilterDisplay(vd, picture);
428         if (direct) {
429             vout_display_Prepare(vd, direct);
430         }
431     }
432 }
433
434 /*****************************************************************************
435  *
436  *****************************************************************************/
437 static void Display(vout_thread_t *vout, picture_t *picture)
438 {
439     vout_sys_t *sys = vout->p_sys;
440     vout_display_t *vd = sys->vd;
441
442     picture_t *direct = picture->p_sys->direct;
443     if (!direct)
444         return;
445
446     /* XXX This is a hack that will work with current vout_display_t modules */
447     if (sys->use_dr)
448         picture_Hold(direct);
449
450      vout_display_Display(vd, direct);
451
452      if (!sys->use_dr)
453          picture->p_sys->direct = NULL;
454 }
455
456 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
457 {
458     /* Load configuration */
459     cfg->is_fullscreen = var_CreateGetBool(vout, "fullscreen");
460     cfg->display.title = title;
461     const int display_width = var_CreateGetInteger(vout, "width");
462     const int display_height = var_CreateGetInteger(vout, "height");
463     cfg->display.width   = display_width > 0  ? display_width  : 0;
464     cfg->display.height  = display_height > 0 ? display_height : 0;
465     cfg->is_display_filled  = var_CreateGetBool(vout, "autoscale");
466     cfg->display.sar.num = 1; /* TODO monitor AR */
467     cfg->display.sar.den = 1;
468     unsigned zoom_den = 1000;
469     unsigned zoom_num = zoom_den * var_CreateGetFloat(vout, "scale");
470     vlc_ureduce(&zoom_num, &zoom_den, zoom_num, zoom_den, 0);
471     cfg->zoom.num = zoom_num;
472     cfg->zoom.den = zoom_den;
473     cfg->align.vertical = VOUT_DISPLAY_ALIGN_CENTER;
474     cfg->align.horizontal = VOUT_DISPLAY_ALIGN_CENTER;
475     const int align_mask = var_CreateGetInteger(vout, "align");
476     if (align_mask & 0x1)
477         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_LEFT;
478     else if (align_mask & 0x2)
479         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
480     if (align_mask & 0x4)
481         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
482     else if (align_mask & 0x8)
483         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
484 }