]> git.sesse.net Git - vlc/blob - modules/video_output/vmem.c
Updated vou display module after vout_display_SendEventDisplaySize change.
[vlc] / modules / video_output / vmem.c
1 /*****************************************************************************
2  * vmem.c: memory video driver for vlc
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_picture_pool.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 #define T_WIDTH N_("Width")
43 #define LT_WIDTH N_("Video memory buffer width.")
44
45 #define T_HEIGHT N_("Height")
46 #define LT_HEIGHT N_("Video memory buffer height.")
47
48 #define T_PITCH N_("Pitch")
49 #define LT_PITCH N_("Video memory buffer pitch in bytes.")
50
51 #define T_CHROMA N_("Chroma")
52 #define LT_CHROMA N_("Output chroma for the memory image as a 4-character " \
53                       "string, eg. \"RV32\".")
54
55 #define T_LOCK N_("Lock function")
56 #define LT_LOCK N_("Address of the locking callback function. This " \
57                     "function must fill in valid plane memory address " \
58                     "information for use by the video renderer.")
59
60 #define T_UNLOCK N_("Unlock function")
61 #define LT_UNLOCK N_("Address of the unlocking callback function")
62
63 #define T_DATA N_("Callback data")
64 #define LT_DATA N_("Data for the locking and unlocking functions")
65
66 static int  Open (vlc_object_t *);
67 static void Close(vlc_object_t *);
68
69 vlc_module_begin()
70     set_description(N_("Video memory output"))
71     set_shortname(N_("Video memory"))
72
73     set_category(CAT_VIDEO)
74     set_subcategory(SUBCAT_VIDEO_VOUT)
75     set_capability("vout display", 0)
76
77     add_integer("vmem-width", 320, NULL, T_WIDTH, LT_WIDTH, false)
78     add_integer("vmem-height", 200, NULL, T_HEIGHT, LT_HEIGHT, false)
79     add_integer("vmem-pitch", 640, NULL, T_PITCH, LT_PITCH, false)
80     add_string("vmem-chroma", "RV16", NULL, T_CHROMA, LT_CHROMA, true)
81     add_string("vmem-lock", "0", NULL, T_LOCK, LT_LOCK, true)
82     add_string("vmem-unlock", "0", NULL, T_UNLOCK, LT_UNLOCK, true)
83     add_string("vmem-data", "0", NULL, T_DATA, LT_DATA, true)
84
85     set_callbacks(Open, Close)
86 vlc_module_end()
87
88 /*****************************************************************************
89  * Local prototypes
90  *****************************************************************************/
91 struct picture_sys_t {
92     void (*lock)(void *sys, void **plane);
93     void (*unlock)(void *sys);
94     void *sys;
95 };
96
97 struct vout_display_sys_t {
98     picture_pool_t *pool;
99 };
100
101 static picture_t *Get    (vout_display_t *);
102 static void       Display(vout_display_t *, picture_t *);
103 static int        Control(vout_display_t *, int, va_list);
104 static void       Manage (vout_display_t *);
105
106 static int        Lock(picture_t *);
107 static void       Unlock(picture_t *);
108
109 /*****************************************************************************
110  * Open: allocates video thread
111  *****************************************************************************
112  * This function allocates and initializes a vout method.
113  *****************************************************************************/
114 static int Open(vlc_object_t *object)
115 {
116     vout_display_t *vd = (vout_display_t *)object;
117
118     /* */
119     char *chroma_format = var_CreateGetString(vd, "vmem-chroma");
120     const vlc_fourcc_t chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma_format);
121     free(chroma_format);
122     if (!chroma) {
123         msg_Err(vd, "vmem-chroma should be 4 characters long");
124         return VLC_EGENERIC;
125     }
126
127     /* */
128     picture_sys_t cfg;
129
130     char *tmp;
131     tmp = var_CreateGetString(vd, "vmem-lock");
132     cfg.lock = (void (*)(void *, void **))(intptr_t)atoll(tmp);
133     free(tmp);
134
135     tmp = var_CreateGetString(vd, "vmem-unlock");
136     cfg.unlock = (void (*)(void *))(intptr_t)atoll(tmp);
137     free(tmp);
138
139     tmp = var_CreateGetString(vd, "vmem-data");
140     cfg.sys = (void *)(intptr_t)atoll(tmp);
141     free(tmp);
142
143     /* lock and unlock are mandatory */
144     if (!cfg.lock || !cfg.unlock) {
145         msg_Err(vd, "Invalid lock or unlock callbacks");
146         return VLC_EGENERIC;
147     }
148
149     /* */
150     video_format_t fmt = vd->fmt;
151
152     fmt.i_chroma = chroma;
153     fmt.i_width  = var_CreateGetInteger(vd, "vmem-width");
154     fmt.i_height = var_CreateGetInteger(vd, "vmem-height");
155
156     /* Define the bitmasks */
157     switch (chroma)
158     {
159     case VLC_CODEC_RGB15:
160         fmt.i_rmask = 0x001f;
161         fmt.i_gmask = 0x03e0;
162         fmt.i_bmask = 0x7c00;
163         break;
164     case VLC_CODEC_RGB16:
165         fmt.i_rmask = 0x001f;
166         fmt.i_gmask = 0x07e0;
167         fmt.i_bmask = 0xf800;
168         break;
169     case VLC_CODEC_RGB24:
170         fmt.i_rmask = 0xff0000;
171         fmt.i_gmask = 0x00ff00;
172         fmt.i_bmask = 0x0000ff;
173         break;
174     case VLC_CODEC_RGB32:
175         fmt.i_rmask = 0xff0000;
176         fmt.i_gmask = 0x00ff00;
177         fmt.i_bmask = 0x0000ff;
178         break;
179     default:
180         fmt.i_rmask = 0;
181         fmt.i_gmask = 0;
182         fmt.i_bmask = 0;
183         break;
184     }
185
186     /* */
187     vout_display_sys_t *sys;
188     vd->sys = sys = calloc(1, sizeof(*sys));
189     if (!sys)
190         return VLC_EGENERIC;
191
192     /* */
193     const int pitch = var_CreateGetInteger(vd, "vmem-pitch");
194     picture_resource_t rsc;
195     rsc.p_sys = malloc(sizeof(*rsc.p_sys));
196     *rsc.p_sys = cfg;
197     for (int i = 0; i < PICTURE_PLANE_MAX; i++) {
198         /* vmem-lock is responsible for the allocation */
199         rsc.p[i].p_pixels = NULL;
200         rsc.p[i].i_lines  = fmt.i_height;
201         rsc.p[i].i_pitch  = pitch;
202     }
203     picture_t *picture = picture_NewFromResource(&fmt, &rsc);
204     if (!picture) {
205         free(rsc.p_sys);
206         free(sys);
207         return VLC_EGENERIC;
208     }
209
210     /* */
211     picture_pool_configuration_t pool;
212     memset(&pool, 0, sizeof(pool));
213     pool.picture_count = 1;
214     pool.picture       = &picture;
215     pool.lock          = Lock;
216     pool.unlock        = Unlock;
217     sys->pool = picture_pool_NewExtended(&pool);
218     if (!sys->pool) {
219         picture_Release(picture);
220         free(sys);
221         return VLC_SUCCESS;
222     }
223
224     /* */
225     vout_display_info_t info = vd->info;
226     info.has_hide_mouse = true;
227
228     /* */
229     vd->fmt     = fmt;
230     vd->info    = info;
231     vd->get     = Get;
232     vd->prepare = NULL;
233     vd->display = Display;
234     vd->control = Control;
235     vd->manage  = Manage;
236
237     /* */
238     vout_display_SendEventFullscreen(vd, false);
239     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
240     return VLC_SUCCESS;
241 }
242
243 static void Close(vlc_object_t *object)
244 {
245     vout_display_t *vd = (vout_display_t *)object;
246     vout_display_sys_t *sys = vd->sys;
247
248     picture_pool_Delete(sys->pool);
249     free(sys);
250 }
251
252 /* */
253 static picture_t *Get(vout_display_t *vd)
254 {
255     return picture_pool_Get(vd->sys->pool);
256 }
257
258 static void Display(vout_display_t *vd, picture_t *picture)
259 {
260     VLC_UNUSED(vd);
261     assert(!picture_IsReferenced(picture));
262     picture_Release(picture);
263 }
264 static int Control(vout_display_t *vd, int query, va_list args)
265 {
266     switch (query) {
267     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
268     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
269         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
270         if (cfg->display.width  != vd->fmt.i_width ||
271             cfg->display.height != vd->fmt.i_height)
272             return VLC_EGENERIC;
273         if (cfg->is_fullscreen)
274             return VLC_EGENERIC;
275         return VLC_SUCCESS;
276     }
277     default:
278         return VLC_EGENERIC;
279     }
280 }
281 static void Manage(vout_display_t *vd)
282 {
283     VLC_UNUSED(vd);
284 }
285
286 /* */
287 static int Lock(picture_t *picture)
288 {
289     picture_sys_t *sys = picture->p_sys;
290
291     void *planes[PICTURE_PLANE_MAX];
292     sys->lock(sys->sys, planes);
293
294     for (int i = 0; i < picture->i_planes; i++)
295         picture->p[i].p_pixels = planes[i];
296
297     return VLC_SUCCESS;
298 }
299 static void Unlock(picture_t *picture)
300 {
301     picture_sys_t *sys = picture->p_sys;
302
303     sys->unlock(sys->sys);
304 }
305