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