opengl: factorize program creation
[vlc.git] / modules / video_output / vmem.c
blob2bdcd63d9e6da40c5f7df719d99b62e4af9953aa
1 /*****************************************************************************
2 * vmem.c: memory video driver for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
5 * Copyright (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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <assert.h>
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 #define T_WIDTH N_("Width")
42 #define LT_WIDTH N_("Video memory buffer width.")
44 #define T_HEIGHT N_("Height")
45 #define LT_HEIGHT N_("Video memory buffer height.")
47 #define T_PITCH N_("Pitch")
48 #define LT_PITCH N_("Video memory buffer pitch in bytes.")
50 #define T_CHROMA N_("Chroma")
51 #define LT_CHROMA N_("Output chroma for the memory image as a 4-character " \
52 "string, eg. \"RV32\".")
54 static int Open(vout_display_t *vd, const vout_display_cfg_t *cfg,
55 video_format_t *fmtp, vlc_video_context *context);
56 static void Close(vout_display_t *vd);
58 vlc_module_begin()
59 set_description(N_("Video memory output"))
60 set_shortname(N_("Video memory"))
62 set_category(CAT_VIDEO)
63 set_subcategory(SUBCAT_VIDEO_VOUT)
65 add_integer("vmem-width", 320, T_WIDTH, LT_WIDTH, false)
66 change_private()
67 add_integer("vmem-height", 200, T_HEIGHT, LT_HEIGHT, false)
68 change_private()
69 add_integer("vmem-pitch", 640, T_PITCH, LT_PITCH, false)
70 change_private()
71 add_string("vmem-chroma", "RV16", T_CHROMA, LT_CHROMA, true)
72 change_private()
73 add_obsolete_string("vmem-lock") /* obsoleted since 1.1.1 */
74 add_obsolete_string("vmem-unlock") /* obsoleted since 1.1.1 */
75 add_obsolete_string("vmem-data") /* obsoleted since 1.1.1 */
77 set_callback_display(Open, 0)
78 vlc_module_end()
80 /*****************************************************************************
81 * Local prototypes
82 *****************************************************************************/
83 typedef struct
85 void *id;
86 } picture_sys_t;
88 /* NOTE: the callback prototypes must match those of LibVLC */
89 struct vout_display_sys_t {
90 void *opaque;
91 void *pic_opaque;
92 void *(*lock)(void *sys, void **plane);
93 void (*unlock)(void *sys, void *id, void *const *plane);
94 void (*display)(void *sys, void *id);
95 void (*cleanup)(void *sys);
97 unsigned pitches[PICTURE_PLANE_MAX];
98 unsigned lines[PICTURE_PLANE_MAX];
101 typedef unsigned (*vlc_format_cb)(void **, char *, unsigned *, unsigned *,
102 unsigned *, unsigned *);
104 static void Prepare(vout_display_t *, picture_t *, subpicture_t *, vlc_tick_t);
105 static void Display(vout_display_t *, picture_t *);
106 static int Control(vout_display_t *, int, va_list);
108 /*****************************************************************************
109 * Open: allocates video thread
110 *****************************************************************************
111 * This function allocates and initializes a vout method.
112 *****************************************************************************/
113 static int Open(vout_display_t *vd, const vout_display_cfg_t *cfg,
114 video_format_t *fmtp, vlc_video_context *context)
116 vout_display_sys_t *sys = malloc(sizeof(*sys));
117 if (unlikely(!sys))
118 return VLC_ENOMEM;
120 /* Get the callbacks */
121 vlc_format_cb setup = var_InheritAddress(vd, "vmem-setup");
123 sys->lock = var_InheritAddress(vd, "vmem-lock");
124 if (sys->lock == NULL) {
125 msg_Err(vd, "missing lock callback");
126 free(sys);
127 return VLC_EGENERIC;
129 sys->unlock = var_InheritAddress(vd, "vmem-unlock");
130 sys->display = var_InheritAddress(vd, "vmem-display");
131 sys->cleanup = var_InheritAddress(vd, "vmem-cleanup");
132 sys->opaque = var_InheritAddress(vd, "vmem-data");
134 /* Define the video format */
135 video_format_t fmt;
136 video_format_ApplyRotation(&fmt, fmtp);
138 if (setup != NULL) {
139 char chroma[5];
141 memcpy(chroma, &fmt.i_chroma, 4);
142 chroma[4] = '\0';
143 memset(sys->pitches, 0, sizeof(sys->pitches));
144 memset(sys->lines, 0, sizeof(sys->lines));
146 unsigned widths[2], heights[2];
147 widths[0] = fmt.i_width;
148 widths[1] = fmt.i_visible_width;
150 heights[0] = fmt.i_height;
151 heights[1] = fmt.i_visible_height;
153 if (setup(&sys->opaque, chroma, widths, heights,
154 sys->pitches, sys->lines) == 0) {
155 msg_Err(vd, "video format setup failure (no pictures)");
156 free(sys);
157 return VLC_EGENERIC;
159 fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
160 fmt.i_width = widths[0];
161 fmt.i_height = heights[0];
163 } else {
164 char *chroma = var_InheritString(vd, "vmem-chroma");
165 fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
166 free(chroma);
168 fmt.i_width = var_InheritInteger(vd, "vmem-width");
169 fmt.i_height = var_InheritInteger(vd, "vmem-height");
170 sys->pitches[0] = var_InheritInteger(vd, "vmem-pitch");
171 sys->lines[0] = fmt.i_height;
172 for (size_t i = 1; i < PICTURE_PLANE_MAX; i++)
174 sys->pitches[i] = sys->pitches[0];
175 sys->lines[i] = sys->lines[0];
177 sys->cleanup = NULL;
179 fmt.i_x_offset = fmt.i_y_offset = 0;
180 fmt.i_visible_width = fmt.i_width;
181 fmt.i_visible_height = fmt.i_height;
183 if (!fmt.i_chroma) {
184 msg_Err(vd, "vmem-chroma should be 4 characters long");
185 free(sys);
186 return VLC_EGENERIC;
189 /* Define the bitmasks */
190 switch (fmt.i_chroma)
192 case VLC_CODEC_RGB15:
193 fmt.i_rmask = 0x001f;
194 fmt.i_gmask = 0x03e0;
195 fmt.i_bmask = 0x7c00;
196 break;
197 case VLC_CODEC_RGB16:
198 fmt.i_rmask = 0x001f;
199 fmt.i_gmask = 0x07e0;
200 fmt.i_bmask = 0xf800;
201 break;
202 case VLC_CODEC_RGB24:
203 case VLC_CODEC_RGB32:
204 fmt.i_rmask = 0xff0000;
205 fmt.i_gmask = 0x00ff00;
206 fmt.i_bmask = 0x0000ff;
207 break;
208 default:
209 fmt.i_rmask = 0;
210 fmt.i_gmask = 0;
211 fmt.i_bmask = 0;
212 break;
215 /* */
216 *fmtp = fmt;
218 vd->sys = sys;
219 vd->prepare = Prepare;
220 vd->display = Display;
221 vd->control = Control;
222 vd->close = Close;
224 (void) cfg; (void) context;
225 return VLC_SUCCESS;
228 static void Close(vout_display_t *vd)
230 vout_display_sys_t *sys = vd->sys;
232 if (sys->cleanup)
233 sys->cleanup(sys->opaque);
234 free(sys);
237 static void Prepare(vout_display_t *vd, picture_t *pic, subpicture_t *subpic,
238 vlc_tick_t date)
240 VLC_UNUSED(date);
241 vout_display_sys_t *sys = vd->sys;
242 picture_resource_t rsc = { .p_sys = NULL };
243 void *planes[PICTURE_PLANE_MAX];
245 sys->pic_opaque = sys->lock(sys->opaque, planes);
247 for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++) {
248 rsc.p[i].p_pixels = planes[i];
249 rsc.p[i].i_lines = sys->lines[i];
250 rsc.p[i].i_pitch = sys->pitches[i];
253 picture_t *locked = picture_NewFromResource(&vd->fmt, &rsc);
254 if (likely(locked != NULL)) {
255 picture_CopyPixels(locked, pic);
256 picture_Release(locked);
259 if (sys->unlock != NULL)
260 sys->unlock(sys->opaque, sys->pic_opaque, planes);
262 (void) subpic;
265 static void Display(vout_display_t *vd, picture_t *pic)
267 vout_display_sys_t *sys = vd->sys;
268 VLC_UNUSED(pic);
270 if (sys->display != NULL)
271 sys->display(sys->opaque, sys->pic_opaque);
274 static int Control(vout_display_t *vd, int query, va_list args)
276 (void) vd; (void) args;
278 switch (query) {
279 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
280 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
281 case VOUT_DISPLAY_CHANGE_ZOOM:
282 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
283 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
284 return VLC_SUCCESS;
286 return VLC_EGENERIC;