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