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
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *
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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
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     void *id;
86 };
87 
88 /* NOTE: the callback prototypes must match those of LibVLC */
89 struct vout_display_sys_t {
90     picture_pool_t *pool;
91 
92     void *opaque;
93     void *pic_opaque;
94     void *(*lock)(void *sys, void **plane);
95     void (*unlock)(void *sys, void *id, void *const *plane);
96     void (*display)(void *sys, void *id);
97     void (*cleanup)(void *sys);
98 
99     unsigned pitches[PICTURE_PLANE_MAX];
100     unsigned lines[PICTURE_PLANE_MAX];
101 };
102 
103 typedef unsigned (*vlc_format_cb)(void **, char *, unsigned *, unsigned *,
104                                   unsigned *, unsigned *);
105 
106 static picture_pool_t *Pool  (vout_display_t *, unsigned);
107 static void           Prepare(vout_display_t *, picture_t *, subpicture_t *);
108 static void           Display(vout_display_t *, picture_t *, subpicture_t *);
109 static int            Control(vout_display_t *, int, va_list);
110 
111 /*****************************************************************************
112  * Open: allocates video thread
113  *****************************************************************************
114  * This function allocates and initializes a vout method.
115  *****************************************************************************/
Open(vlc_object_t * object)116 static int Open(vlc_object_t *object)
117 {
118     vout_display_t *vd = (vout_display_t *)object;
119     vout_display_sys_t *sys = malloc(sizeof(*sys));
120     if (unlikely(!sys))
121         return VLC_ENOMEM;
122 
123     /* Get the callbacks */
124     vlc_format_cb setup = var_InheritAddress(vd, "vmem-setup");
125 
126     sys->lock = var_InheritAddress(vd, "vmem-lock");
127     if (sys->lock == NULL) {
128         msg_Err(vd, "missing lock callback");
129         free(sys);
130         return VLC_EGENERIC;
131     }
132     sys->unlock = var_InheritAddress(vd, "vmem-unlock");
133     sys->display = var_InheritAddress(vd, "vmem-display");
134     sys->cleanup = var_InheritAddress(vd, "vmem-cleanup");
135     sys->opaque = var_InheritAddress(vd, "vmem-data");
136     sys->pool = NULL;
137 
138     /* Define the video format */
139     video_format_t fmt;
140     video_format_ApplyRotation(&fmt, &vd->fmt);
141 
142     if (setup != NULL) {
143         char chroma[5];
144 
145         memcpy(chroma, &fmt.i_chroma, 4);
146         chroma[4] = '\0';
147         memset(sys->pitches, 0, sizeof(sys->pitches));
148         memset(sys->lines, 0, sizeof(sys->lines));
149 
150         if (setup(&sys->opaque, chroma, &fmt.i_width, &fmt.i_height,
151                            sys->pitches, sys->lines) == 0) {
152             msg_Err(vd, "video format setup failure (no pictures)");
153             free(sys);
154             return VLC_EGENERIC;
155         }
156         fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
157 
158     } else {
159         char *chroma = var_InheritString(vd, "vmem-chroma");
160         fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
161         free(chroma);
162 
163         fmt.i_width  = var_InheritInteger(vd, "vmem-width");
164         fmt.i_height = var_InheritInteger(vd, "vmem-height");
165         sys->pitches[0] = var_InheritInteger(vd, "vmem-pitch");
166         sys->lines[0] = fmt.i_height;
167         for (size_t i = 1; i < PICTURE_PLANE_MAX; i++)
168         {
169             sys->pitches[i] = sys->pitches[0];
170             sys->lines[i] = sys->lines[0];
171         }
172         sys->cleanup = NULL;
173     }
174     fmt.i_x_offset = fmt.i_y_offset = 0;
175     fmt.i_visible_width = fmt.i_width;
176     fmt.i_visible_height = fmt.i_height;
177 
178     if (!fmt.i_chroma) {
179         msg_Err(vd, "vmem-chroma should be 4 characters long");
180         free(sys);
181         return VLC_EGENERIC;
182     }
183 
184     /* Define the bitmasks */
185     switch (fmt.i_chroma)
186     {
187     case VLC_CODEC_RGB15:
188         fmt.i_rmask = 0x001f;
189         fmt.i_gmask = 0x03e0;
190         fmt.i_bmask = 0x7c00;
191         break;
192     case VLC_CODEC_RGB16:
193         fmt.i_rmask = 0x001f;
194         fmt.i_gmask = 0x07e0;
195         fmt.i_bmask = 0xf800;
196         break;
197     case VLC_CODEC_RGB24:
198     case VLC_CODEC_RGB32:
199         fmt.i_rmask = 0xff0000;
200         fmt.i_gmask = 0x00ff00;
201         fmt.i_bmask = 0x0000ff;
202         break;
203     default:
204         fmt.i_rmask = 0;
205         fmt.i_gmask = 0;
206         fmt.i_bmask = 0;
207         break;
208     }
209 
210     /* */
211     vd->sys     = sys;
212     vd->fmt     = fmt;
213     vd->pool    = Pool;
214     vd->prepare = Prepare;
215     vd->display = Display;
216     vd->control = Control;
217 
218     /* */
219     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height);
220     vout_display_DeleteWindow(vd, NULL);
221     return VLC_SUCCESS;
222 }
223 
Close(vlc_object_t * object)224 static void Close(vlc_object_t *object)
225 {
226     vout_display_t *vd = (vout_display_t *)object;
227     vout_display_sys_t *sys = vd->sys;
228 
229     if (sys->cleanup)
230         sys->cleanup(sys->opaque);
231     if (sys->pool)
232         picture_pool_Release(sys->pool);
233     free(sys);
234 }
235 
Pool(vout_display_t * vd,unsigned count)236 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
237 {
238     vout_display_sys_t *sys = vd->sys;
239 
240     if (sys->pool == NULL)
241         sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
242     return sys->pool;
243 }
244 
Prepare(vout_display_t * vd,picture_t * pic,subpicture_t * subpic)245 static void Prepare(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
246 {
247     vout_display_sys_t *sys = vd->sys;
248     picture_resource_t rsc = { .p_sys = NULL };
249     void *planes[PICTURE_PLANE_MAX];
250 
251     sys->pic_opaque = sys->lock(sys->opaque, planes);
252 
253     for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++) {
254         rsc.p[i].p_pixels = planes[i];
255         rsc.p[i].i_lines  = sys->lines[i];
256         rsc.p[i].i_pitch  = sys->pitches[i];
257     }
258 
259     picture_t *locked = picture_NewFromResource(&vd->fmt, &rsc);
260     if (likely(locked != NULL)) {
261         picture_CopyPixels(locked, pic);
262         picture_Release(locked);
263     }
264 
265     if (sys->unlock != NULL)
266         sys->unlock(sys->opaque, sys->pic_opaque, planes);
267 
268     (void) subpic;
269 }
270 
Display(vout_display_t * vd,picture_t * pic,subpicture_t * subpic)271 static void Display(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
272 {
273     vout_display_sys_t *sys = vd->sys;
274 
275     if (sys->display != NULL)
276         sys->display(sys->opaque, sys->pic_opaque);
277 
278     picture_Release(pic);
279     VLC_UNUSED(subpic);
280 }
281 
Control(vout_display_t * vd,int query,va_list args)282 static int Control(vout_display_t *vd, int query, va_list args)
283 {
284     (void) vd; (void) query; (void) args;
285     return VLC_EGENERIC;
286 }
287