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