]> git.sesse.net Git - vlc/blob - modules/video_output/vmem.c
demux: libmp4: add mp4a/mp4v esds restrictions
[vlc] / modules / video_output / vmem.c
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
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     vout_display_sys_t *sys;
86     void *id;
87 };
88
89 /* NOTE: the callback prototypes must match those of LibVLC */
90 struct vout_display_sys_t {
91     picture_pool_t *pool;
92     unsigned        count;
93
94     void *opaque;
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);
99
100     unsigned pitches[PICTURE_PLANE_MAX];
101     unsigned lines[PICTURE_PLANE_MAX];
102 };
103
104 typedef unsigned (*vlc_format_cb)(void **, char *, unsigned *, unsigned *,
105                                   unsigned *, unsigned *);
106
107 static picture_pool_t *Pool  (vout_display_t *, unsigned);
108 static void           Display(vout_display_t *, picture_t *, subpicture_t *);
109 static int            Control(vout_display_t *, int, va_list);
110
111 static int            Lock(picture_t *);
112 static void           Unlock(picture_t *);
113
114 /*****************************************************************************
115  * Open: allocates video thread
116  *****************************************************************************
117  * This function allocates and initializes a vout method.
118  *****************************************************************************/
119 static int Open(vlc_object_t *object)
120 {
121     vout_display_t *vd = (vout_display_t *)object;
122     vout_display_sys_t *sys = malloc(sizeof(*sys));
123     if (unlikely(!sys))
124         return VLC_ENOMEM;
125
126     /* Get the callbacks */
127     vlc_format_cb setup = var_InheritAddress(vd, "vmem-setup");
128
129     sys->lock = var_InheritAddress(vd, "vmem-lock");
130     if (sys->lock == NULL) {
131         msg_Err(vd, "missing lock callback");
132         free(sys);
133         return VLC_EGENERIC;
134     }
135     sys->unlock = var_InheritAddress(vd, "vmem-unlock");
136     sys->display = var_InheritAddress(vd, "vmem-display");
137     sys->cleanup = var_InheritAddress(vd, "vmem-cleanup");
138     sys->opaque = var_InheritAddress(vd, "vmem-data");
139     sys->pool = NULL;
140
141     /* Define the video format */
142     video_format_t fmt;
143     video_format_ApplyRotation(&fmt, &vd->fmt);
144
145     if (setup != NULL) {
146         char chroma[5];
147
148         memcpy(chroma, &fmt.i_chroma, 4);
149         chroma[4] = '\0';
150         memset(sys->pitches, 0, sizeof(sys->pitches));
151         memset(sys->lines, 0, sizeof(sys->lines));
152
153         sys->count = setup(&sys->opaque, chroma, &fmt.i_width, &fmt.i_height,
154                            sys->pitches, sys->lines);
155         if (sys->count == 0) {
156             msg_Err(vd, "video format setup failure (no pictures)");
157             free(sys);
158             return VLC_EGENERIC;
159         }
160         fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
161
162     } else {
163         char *chroma = var_InheritString(vd, "vmem-chroma");
164         fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
165         free(chroma);
166
167         fmt.i_width  = var_InheritInteger(vd, "vmem-width");
168         fmt.i_height = var_InheritInteger(vd, "vmem-height");
169         sys->pitches[0] = var_InheritInteger(vd, "vmem-pitch");
170         sys->lines[0] = fmt.i_height;
171         for (size_t i = 1; i < PICTURE_PLANE_MAX; i++)
172         {
173             sys->pitches[i] = sys->pitches[0];
174             sys->lines[i] = sys->lines[0];
175         }
176         sys->count = 1;
177         sys->cleanup = NULL;
178     }
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;
182
183     if (!fmt.i_chroma) {
184         msg_Err(vd, "vmem-chroma should be 4 characters long");
185         free(sys);
186         return VLC_EGENERIC;
187     }
188
189     /* Define the bitmasks */
190     switch (fmt.i_chroma)
191     {
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;
213     }
214
215     /* */
216     vout_display_info_t info = vd->info;
217     info.has_hide_mouse = true;
218
219     /* */
220     vd->sys     = sys;
221     vd->fmt     = fmt;
222     vd->info    = info;
223     vd->pool    = Pool;
224     vd->prepare = NULL;
225     vd->display = Display;
226     vd->control = Control;
227     vd->manage  = NULL;
228
229     /* */
230     vout_display_SendEventFullscreen(vd, false);
231     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, false);
232     return VLC_SUCCESS;
233 }
234
235 static void Close(vlc_object_t *object)
236 {
237     vout_display_t *vd = (vout_display_t *)object;
238     vout_display_sys_t *sys = vd->sys;
239
240     if (sys->cleanup)
241         sys->cleanup(sys->opaque);
242     picture_pool_Delete(sys->pool);
243     free(sys);
244 }
245
246 /* */
247 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
248 {
249     vout_display_sys_t *sys = vd->sys;
250
251     if (sys->pool)
252         return sys->pool;
253
254     if (count > sys->count)
255         count = sys->count;
256
257     picture_t *pictures[count];
258
259     for (unsigned i = 0; i < count; i++) {
260         picture_sys_t *picsys = malloc(sizeof (*picsys));
261         if (unlikely(picsys == NULL))
262         {
263             count = i;
264             break;
265         }
266         picsys->sys = sys;
267         picsys->id = NULL;
268
269         picture_resource_t rsc = { .p_sys = picsys };
270
271         for (unsigned i = 0; i < PICTURE_PLANE_MAX; i++) {
272             /* vmem-lock is responsible for the allocation */
273             rsc.p[i].p_pixels = NULL;
274             rsc.p[i].i_lines  = sys->lines[i];
275             rsc.p[i].i_pitch  = sys->pitches[i];
276         }
277
278         pictures[i] = picture_NewFromResource(&vd->fmt, &rsc);
279         if (!pictures[i]) {
280             free(rsc.p_sys);
281             count = i;
282             break;
283         }
284     }
285
286     /* */
287     picture_pool_configuration_t pool;
288     memset(&pool, 0, sizeof(pool));
289     pool.picture_count = count;
290     pool.picture       = pictures;
291     pool.lock          = Lock;
292     pool.unlock        = Unlock;
293     sys->pool = picture_pool_NewExtended(&pool);
294     if (!sys->pool) {
295         for (unsigned i = 0; i < count; i++)
296             picture_Release(pictures[i]);
297     }
298
299     return sys->pool;
300 }
301
302 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
303 {
304     vout_display_sys_t *sys = vd->sys;
305
306     assert(!picture_IsReferenced(picture));
307     if (sys->display != NULL)
308         sys->display(sys->opaque, picture->p_sys->id);
309     picture_Release(picture);
310     VLC_UNUSED(subpicture);
311 }
312
313 static int Control(vout_display_t *vd, int query, va_list args)
314 {
315     switch (query) {
316     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
317     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
318         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
319         if (cfg->display.width  != vd->fmt.i_width ||
320             cfg->display.height != vd->fmt.i_height)
321             return VLC_EGENERIC;
322         if (cfg->is_fullscreen)
323             return VLC_EGENERIC;
324         return VLC_SUCCESS;
325     }
326     default:
327         return VLC_EGENERIC;
328     }
329 }
330
331 /* */
332 static int Lock(picture_t *picture)
333 {
334     picture_sys_t *picsys = picture->p_sys;
335     vout_display_sys_t *sys = picsys->sys;
336     void *planes[PICTURE_PLANE_MAX];
337
338     picsys->id = sys->lock(sys->opaque, planes);
339
340     for (int i = 0; i < picture->i_planes; i++)
341         picture->p[i].p_pixels = planes[i];
342
343     return VLC_SUCCESS;
344 }
345
346 static void Unlock(picture_t *picture)
347 {
348     picture_sys_t *picsys = picture->p_sys;
349     vout_display_sys_t *sys = picsys->sys;
350
351     void *planes[PICTURE_PLANE_MAX];
352
353     for (int i = 0; i < picture->i_planes; i++)
354         planes[i] = picture->p[i].p_pixels;
355
356     if (sys->unlock != NULL)
357         sys->unlock(sys->opaque, picsys->id, planes);
358 }