]> git.sesse.net Git - vlc/blob - modules/video_output/vmem.c
Fix spelling.
[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         change_private()
79     add_integer("vmem-height", 200, NULL, T_HEIGHT, LT_HEIGHT, false)
80         change_private()
81     add_integer("vmem-pitch", 640, NULL, T_PITCH, LT_PITCH, false)
82         change_private()
83     add_string("vmem-chroma", "RV16", NULL, T_CHROMA, LT_CHROMA, true)
84         change_private()
85     add_string("vmem-lock", "0", NULL, T_LOCK, LT_LOCK, true)
86         change_volatile()
87     add_string("vmem-unlock", "0", NULL, T_UNLOCK, LT_UNLOCK, true)
88         change_volatile()
89     add_string("vmem-data", "0", NULL, T_DATA, LT_DATA, true)
90         change_volatile()
91
92     set_callbacks(Open, Close)
93 vlc_module_end()
94
95 /*****************************************************************************
96  * Local prototypes
97  *****************************************************************************/
98 struct picture_sys_t {
99     void (*lock)(void *sys, void **plane);
100     void (*unlock)(void *sys);
101     void *sys;
102 };
103
104 struct vout_display_sys_t {
105     picture_pool_t *pool;
106 };
107
108 static picture_pool_t *Pool  (vout_display_t *, unsigned);
109 static void           Display(vout_display_t *, picture_t *);
110 static int            Control(vout_display_t *, int, va_list);
111 static void           Manage (vout_display_t *);
112
113 static int            Lock(picture_t *);
114 static void           Unlock(picture_t *);
115
116 /*****************************************************************************
117  * Open: allocates video thread
118  *****************************************************************************
119  * This function allocates and initializes a vout method.
120  *****************************************************************************/
121 static int Open(vlc_object_t *object)
122 {
123     vout_display_t *vd = (vout_display_t *)object;
124
125     /* */
126     char *chroma_format = var_CreateGetString(vd, "vmem-chroma");
127     const vlc_fourcc_t chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma_format);
128     free(chroma_format);
129     if (!chroma) {
130         msg_Err(vd, "vmem-chroma should be 4 characters long");
131         return VLC_EGENERIC;
132     }
133
134     /* */
135     picture_sys_t cfg;
136
137     char *tmp;
138     tmp = var_CreateGetString(vd, "vmem-lock");
139     cfg.lock = (void (*)(void *, void **))(intptr_t)atoll(tmp);
140     free(tmp);
141
142     tmp = var_CreateGetString(vd, "vmem-unlock");
143     cfg.unlock = (void (*)(void *))(intptr_t)atoll(tmp);
144     free(tmp);
145
146     tmp = var_CreateGetString(vd, "vmem-data");
147     cfg.sys = (void *)(intptr_t)atoll(tmp);
148     free(tmp);
149
150     /* lock and unlock are mandatory */
151     if (!cfg.lock || !cfg.unlock) {
152         msg_Err(vd, "Invalid lock or unlock callbacks");
153         return VLC_EGENERIC;
154     }
155
156     /* */
157     video_format_t fmt = vd->fmt;
158
159     fmt.i_chroma = chroma;
160     fmt.i_width  = var_CreateGetInteger(vd, "vmem-width");
161     fmt.i_height = var_CreateGetInteger(vd, "vmem-height");
162
163     /* Define the bitmasks */
164     switch (chroma)
165     {
166     case VLC_CODEC_RGB15:
167         fmt.i_rmask = 0x001f;
168         fmt.i_gmask = 0x03e0;
169         fmt.i_bmask = 0x7c00;
170         break;
171     case VLC_CODEC_RGB16:
172         fmt.i_rmask = 0x001f;
173         fmt.i_gmask = 0x07e0;
174         fmt.i_bmask = 0xf800;
175         break;
176     case VLC_CODEC_RGB24:
177         fmt.i_rmask = 0xff0000;
178         fmt.i_gmask = 0x00ff00;
179         fmt.i_bmask = 0x0000ff;
180         break;
181     case VLC_CODEC_RGB32:
182         fmt.i_rmask = 0xff0000;
183         fmt.i_gmask = 0x00ff00;
184         fmt.i_bmask = 0x0000ff;
185         break;
186     default:
187         fmt.i_rmask = 0;
188         fmt.i_gmask = 0;
189         fmt.i_bmask = 0;
190         break;
191     }
192
193     /* */
194     vout_display_sys_t *sys;
195     vd->sys = sys = calloc(1, sizeof(*sys));
196     if (!sys)
197         return VLC_EGENERIC;
198
199     /* */
200     const int pitch = var_CreateGetInteger(vd, "vmem-pitch");
201     picture_resource_t rsc;
202     rsc.p_sys = malloc(sizeof(*rsc.p_sys));
203     *rsc.p_sys = cfg;
204     for (int i = 0; i < PICTURE_PLANE_MAX; i++) {
205         /* vmem-lock is responsible for the allocation */
206         rsc.p[i].p_pixels = NULL;
207         rsc.p[i].i_lines  = fmt.i_height;
208         rsc.p[i].i_pitch  = pitch;
209     }
210     picture_t *picture = picture_NewFromResource(&fmt, &rsc);
211     if (!picture) {
212         free(rsc.p_sys);
213         free(sys);
214         return VLC_EGENERIC;
215     }
216
217     /* */
218     picture_pool_configuration_t pool;
219     memset(&pool, 0, sizeof(pool));
220     pool.picture_count = 1;
221     pool.picture       = &picture;
222     pool.lock          = Lock;
223     pool.unlock        = Unlock;
224     sys->pool = picture_pool_NewExtended(&pool);
225     if (!sys->pool) {
226         picture_Release(picture);
227         free(sys);
228         return VLC_EGENERIC;
229     }
230
231     /* */
232     vout_display_info_t info = vd->info;
233     info.has_hide_mouse = true;
234
235     /* */
236     vd->fmt     = fmt;
237     vd->info    = info;
238     vd->pool    = Pool;
239     vd->prepare = NULL;
240     vd->display = Display;
241     vd->control = Control;
242     vd->manage  = Manage;
243
244     /* */
245     vout_display_SendEventFullscreen(vd, false);
246     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
247     return VLC_SUCCESS;
248 }
249
250 static void Close(vlc_object_t *object)
251 {
252     vout_display_t *vd = (vout_display_t *)object;
253     vout_display_sys_t *sys = vd->sys;
254
255     picture_pool_Delete(sys->pool);
256     free(sys);
257 }
258
259 /* */
260 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
261 {
262     VLC_UNUSED(count);
263     return vd->sys->pool;
264 }
265
266 static void Display(vout_display_t *vd, picture_t *picture)
267 {
268     VLC_UNUSED(vd);
269     assert(!picture_IsReferenced(picture));
270     picture_Release(picture);
271 }
272 static int Control(vout_display_t *vd, int query, va_list args)
273 {
274     switch (query) {
275     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
276     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
277         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
278         if (cfg->display.width  != vd->fmt.i_width ||
279             cfg->display.height != vd->fmt.i_height)
280             return VLC_EGENERIC;
281         if (cfg->is_fullscreen)
282             return VLC_EGENERIC;
283         return VLC_SUCCESS;
284     }
285     default:
286         return VLC_EGENERIC;
287     }
288 }
289 static void Manage(vout_display_t *vd)
290 {
291     VLC_UNUSED(vd);
292 }
293
294 /* */
295 static int Lock(picture_t *picture)
296 {
297     picture_sys_t *sys = picture->p_sys;
298
299     void *planes[PICTURE_PLANE_MAX];
300     sys->lock(sys->sys, planes);
301
302     for (int i = 0; i < picture->i_planes; i++)
303         picture->p[i].p_pixels = planes[i];
304
305     return VLC_SUCCESS;
306 }
307 static void Unlock(picture_t *picture)
308 {
309     picture_sys_t *sys = picture->p_sys;
310
311     sys->unlock(sys->sys);
312 }
313