1 /*****************************************************************************
2 * vmem.c: memory video driver for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
5 * Copyrgiht (C) 2010 RĂ©mi Denis-Courmont
7 * Authors: Sam Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_picture_pool.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 #define T_WIDTH N_("Width")
43 #define LT_WIDTH N_("Video memory buffer width.")
45 #define T_HEIGHT N_("Height")
46 #define LT_HEIGHT N_("Video memory buffer height.")
48 #define T_PITCH N_("Pitch")
49 #define LT_PITCH N_("Video memory buffer pitch in bytes.")
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\".")
55 static int Open (vlc_object_t *);
56 static void Close(vlc_object_t *);
59 set_description(N_("Video memory output"))
60 set_shortname(N_("Video memory"))
62 set_category(CAT_VIDEO)
63 set_subcategory(SUBCAT_VIDEO_VOUT)
64 set_capability("vout display", 0)
66 add_integer("vmem-width", 320, T_WIDTH, LT_WIDTH, false)
68 add_integer("vmem-height", 200, T_HEIGHT, LT_HEIGHT, false)
70 add_integer("vmem-pitch", 640, T_PITCH, LT_PITCH, false)
72 add_string("vmem-chroma", "RV16", T_CHROMA, LT_CHROMA, true)
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 */
78 set_callbacks(Open, Close)
81 /*****************************************************************************
83 *****************************************************************************/
84 struct picture_sys_t {
85 vout_display_sys_t *sys;
89 /* NOTE: the callback prototypes must match those of LibVLC */
90 struct vout_display_sys_t {
95 void *(*lock)(void *sys, void **plane);
96 void (*unlock)(void *sys, void *id, void *const *plane);
97 void (*display)(void *sys, void *id);
98 void (*cleanup)(void *sys);
100 unsigned pitches[PICTURE_PLANE_MAX];
101 unsigned lines[PICTURE_PLANE_MAX];
104 typedef unsigned (*vlc_format_cb)(void **, char *, unsigned *, unsigned *,
105 unsigned *, unsigned *);
107 static picture_pool_t *Pool (vout_display_t *, unsigned);
108 static void Prepare(vout_display_t *, picture_t *, subpicture_t *);
109 static void Display(vout_display_t *, picture_t *, subpicture_t *);
110 static int Control(vout_display_t *, int, va_list);
112 static void Lock(void *data, picture_t *pic)
114 vout_display_sys_t *sys = data;
115 picture_sys_t *picsys = pic->p_sys;
116 void *planes[PICTURE_PLANE_MAX];
118 picsys->id = sys->lock(sys->opaque, planes);
120 for (int i = 0; i < pic->i_planes; i++)
121 pic->p[i].p_pixels = planes[i];
124 static void Unlock(void *data, picture_t *pic)
126 vout_display_sys_t *sys = data;
127 picture_sys_t *picsys = pic->p_sys;
128 void *planes[PICTURE_PLANE_MAX];
130 assert(!picture_IsReferenced(pic));
132 for (int i = 0; i < pic->i_planes; i++)
133 planes[i] = pic->p[i].p_pixels;
135 if (sys->unlock != NULL)
136 sys->unlock(sys->opaque, picsys->id, planes);
140 /*****************************************************************************
141 * Open: allocates video thread
142 *****************************************************************************
143 * This function allocates and initializes a vout method.
144 *****************************************************************************/
145 static int Open(vlc_object_t *object)
147 vout_display_t *vd = (vout_display_t *)object;
148 vout_display_sys_t *sys = malloc(sizeof(*sys));
152 /* Get the callbacks */
153 vlc_format_cb setup = var_InheritAddress(vd, "vmem-setup");
155 sys->lock = var_InheritAddress(vd, "vmem-lock");
156 if (sys->lock == NULL) {
157 msg_Err(vd, "missing lock callback");
161 sys->unlock = var_InheritAddress(vd, "vmem-unlock");
162 sys->display = var_InheritAddress(vd, "vmem-display");
163 sys->cleanup = var_InheritAddress(vd, "vmem-cleanup");
164 sys->opaque = var_InheritAddress(vd, "vmem-data");
167 /* Define the video format */
169 video_format_ApplyRotation(&fmt, &vd->fmt);
174 memcpy(chroma, &fmt.i_chroma, 4);
176 memset(sys->pitches, 0, sizeof(sys->pitches));
177 memset(sys->lines, 0, sizeof(sys->lines));
179 sys->count = setup(&sys->opaque, chroma, &fmt.i_width, &fmt.i_height,
180 sys->pitches, sys->lines);
181 if (sys->count == 0) {
182 msg_Err(vd, "video format setup failure (no pictures)");
186 fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
189 char *chroma = var_InheritString(vd, "vmem-chroma");
190 fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
193 fmt.i_width = var_InheritInteger(vd, "vmem-width");
194 fmt.i_height = var_InheritInteger(vd, "vmem-height");
195 sys->pitches[0] = var_InheritInteger(vd, "vmem-pitch");
196 sys->lines[0] = fmt.i_height;
197 for (size_t i = 1; i < PICTURE_PLANE_MAX; i++)
199 sys->pitches[i] = sys->pitches[0];
200 sys->lines[i] = sys->lines[0];
205 fmt.i_x_offset = fmt.i_y_offset = 0;
206 fmt.i_visible_width = fmt.i_width;
207 fmt.i_visible_height = fmt.i_height;
210 msg_Err(vd, "vmem-chroma should be 4 characters long");
215 /* Define the bitmasks */
216 switch (fmt.i_chroma)
218 case VLC_CODEC_RGB15:
219 fmt.i_rmask = 0x001f;
220 fmt.i_gmask = 0x03e0;
221 fmt.i_bmask = 0x7c00;
223 case VLC_CODEC_RGB16:
224 fmt.i_rmask = 0x001f;
225 fmt.i_gmask = 0x07e0;
226 fmt.i_bmask = 0xf800;
228 case VLC_CODEC_RGB24:
229 case VLC_CODEC_RGB32:
230 fmt.i_rmask = 0xff0000;
231 fmt.i_gmask = 0x00ff00;
232 fmt.i_bmask = 0x0000ff;
242 vout_display_info_t info = vd->info;
243 info.has_hide_mouse = true;
250 vd->prepare = Prepare;
251 vd->display = Display;
252 vd->control = Control;
256 vout_display_SendEventFullscreen(vd, false);
257 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height);
258 vout_display_DeleteWindow(vd, NULL);
262 static void Close(vlc_object_t *object)
264 vout_display_t *vd = (vout_display_t *)object;
265 vout_display_sys_t *sys = vd->sys;
268 sys->cleanup(sys->opaque);
270 picture_pool_Enum(sys->pool, Unlock, sys);
271 picture_pool_Release(sys->pool);
275 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
277 vout_display_sys_t *sys = vd->sys;
282 if (count > sys->count)
285 picture_t *pictures[count];
287 for (unsigned i = 0; i < count; i++) {
288 picture_sys_t *picsys = malloc(sizeof (*picsys));
289 if (unlikely(picsys == NULL))
297 picture_resource_t rsc = { .p_sys = picsys };
299 for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++) {
300 /* vmem-lock is responsible for the allocation */
301 rsc.p[i].p_pixels = NULL;
302 rsc.p[i].i_lines = sys->lines[i];
303 rsc.p[i].i_pitch = sys->pitches[i];
306 pictures[i] = picture_NewFromResource(&vd->fmt, &rsc);
315 sys->pool = picture_pool_New(count, pictures);
317 for (unsigned i = 0; i < count; i++)
318 picture_Release(pictures[i]);
321 picture_pool_Enum(sys->pool, Lock, sys);
325 static void Prepare(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
327 Unlock(vd->sys, pic);
332 static void Display(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
334 vout_display_sys_t *sys = vd->sys;
336 if (sys->display != NULL)
337 sys->display(sys->opaque, pic->p_sys->id);
340 picture_Release(pic);
344 static int Control(vout_display_t *vd, int query, va_list args)
346 (void) vd; (void) query; (void) args;