]> git.sesse.net Git - vlc/blob - modules/video_output/vmem.c
vmem: better ordering.
[vlc] / modules / video_output / vmem.c
1 /*****************************************************************************
2  * vmem.c: memory video driver for vlc
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
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 <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static int  Init          ( vout_thread_t * );
43 static void End           ( vout_thread_t * );
44 static int  LockPicture   ( vout_thread_t *, picture_t * );
45 static int  UnlockPicture ( vout_thread_t *, picture_t * );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 #define T_WIDTH N_( "Width" )
51 #define LT_WIDTH N_( "Video memory buffer width." )
52
53 #define T_HEIGHT N_( "Height" )
54 #define LT_HEIGHT N_( "Video memory buffer height." )
55
56 #define T_PITCH N_( "Pitch" )
57 #define LT_PITCH N_( "Video memory buffer pitch in bytes." )
58
59 #define T_CHROMA N_( "Chroma" )
60 #define LT_CHROMA N_( "Output chroma for the memory image as a 4-character " \
61                       "string, eg. \"RV32\"." )
62
63 #define T_LOCK N_( "Lock function" )
64 #define LT_LOCK N_( "Address of the locking callback function. This " \
65                     "function must fill in valid plane memory address " \
66                     "information for use by the video renderer." )
67
68 #define T_UNLOCK N_( "Unlock function" )
69 #define LT_UNLOCK N_( "Address of the unlocking callback function" )
70
71 #define T_DATA N_( "Callback data" )
72 #define LT_DATA N_( "Data for the locking and unlocking functions" )
73
74 vlc_module_begin ()
75     set_description( N_( "Video memory output" ) )
76     set_shortname( N_("Video memory") )
77
78     set_category( CAT_VIDEO )
79     set_subcategory( SUBCAT_VIDEO_VOUT )
80     set_capability( "video output", 0 )
81
82     add_integer( "vmem-width", 320, NULL, T_WIDTH, LT_WIDTH, false )
83     add_integer( "vmem-height", 200, NULL, T_HEIGHT, LT_HEIGHT, false )
84     add_integer( "vmem-pitch", 640, NULL, T_PITCH, LT_PITCH, false )
85     add_string( "vmem-chroma", "RV16", NULL, T_CHROMA, LT_CHROMA, true )
86     add_string( "vmem-lock", "0", NULL, T_LOCK, LT_LOCK, true )
87     add_string( "vmem-unlock", "0", NULL, T_UNLOCK, LT_UNLOCK, true )
88     add_string( "vmem-data", "0", NULL, T_DATA, LT_DATA, true )
89
90     set_callbacks( Create, Destroy )
91 vlc_module_end ()
92
93 /*****************************************************************************
94  * vout_sys_t: video output descriptor
95  *****************************************************************************/
96 struct vout_sys_t
97 {
98     int i_width, i_height, i_pitch;
99
100     void (*pf_lock) (void *, void **);
101     void (*pf_unlock) (void *);
102     void *p_data;
103 };
104
105 /*****************************************************************************
106  * Create: allocates video thread
107  *****************************************************************************
108  * This function allocates and initializes a vout method.
109  *****************************************************************************/
110 static int Create( vlc_object_t *p_this )
111 {
112     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
113
114     /* Allocate instance and initialize some members */
115     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
116     if( ! p_vout->p_sys )
117         return VLC_ENOMEM;
118
119     p_vout->pf_init = Init;
120     p_vout->pf_end = End;
121     p_vout->pf_manage = NULL;
122     p_vout->pf_render = NULL;
123     p_vout->pf_display = NULL;
124
125     return VLC_SUCCESS;
126 }
127
128 /*****************************************************************************
129  * Init: initialize video thread
130  *****************************************************************************/
131 static int Init( vout_thread_t *p_vout )
132 {
133     int i_index;
134     picture_t *p_pic;
135     char *psz_chroma, *psz_tmp;
136     int i_width, i_height, i_pitch;
137     vlc_fourcc_t i_chroma;
138
139     i_width = var_CreateGetInteger( p_vout, "vmem-width" );
140     i_height = var_CreateGetInteger( p_vout, "vmem-height" );
141     i_pitch = var_CreateGetInteger( p_vout, "vmem-pitch" );
142
143     psz_chroma = var_CreateGetString( p_vout, "vmem-chroma" );
144     if( !psz_chroma )
145     {
146         msg_Err( p_vout, "Cannot find chroma information." );
147         return VLC_EGENERIC;
148     }
149
150     i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_chroma );
151     free( psz_chroma );
152
153     if( !i_chroma )
154     {
155         msg_Err( p_vout, "vmem-chroma should be 4 characters long" );
156         return VLC_EGENERIC;
157     }
158
159     psz_tmp = var_CreateGetString( p_vout, "vmem-lock" );
160     p_vout->p_sys->pf_lock = (void (*) (void *, void **))(intptr_t)atoll( psz_tmp );
161     free( psz_tmp );
162
163     psz_tmp = var_CreateGetString( p_vout, "vmem-unlock" );
164     p_vout->p_sys->pf_unlock = (void (*) (void *))(intptr_t)atoll( psz_tmp );
165     free( psz_tmp );
166
167     /* pf_lock and pf_unlock are mandatory */
168     if( !p_vout->p_sys->pf_lock || !p_vout->p_sys->pf_unlock )
169     {
170         msg_Err( p_vout, "Invalid lock or unlock callbacks" );
171         return VLC_EGENERIC;
172     }
173
174     psz_tmp = var_CreateGetString( p_vout, "vmem-data" );
175     p_vout->p_sys->p_data = (void *)(intptr_t)atoll( psz_tmp );
176     free( psz_tmp );
177
178     I_OUTPUTPICTURES = 0;
179
180     /* Initialize the output structure */
181     p_vout->output.i_chroma = i_chroma;
182     p_vout->output.pf_setpalette = NULL;
183     p_vout->output.i_width = i_width;
184     p_vout->output.i_height = i_height;
185     p_vout->output.i_aspect = p_vout->output.i_width
186                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
187
188     /* Define the bitmasks */
189     switch( i_chroma )
190     {
191       case VLC_CODEC_RGB15:
192         p_vout->output.i_rmask = 0x001f;
193         p_vout->output.i_gmask = 0x03e0;
194         p_vout->output.i_bmask = 0x7c00;
195         break;
196
197       case VLC_CODEC_RGB16:
198         p_vout->output.i_rmask = 0x001f;
199         p_vout->output.i_gmask = 0x07e0;
200         p_vout->output.i_bmask = 0xf800;
201         break;
202
203       case VLC_CODEC_RGB24:
204         p_vout->output.i_rmask = 0xff0000;
205         p_vout->output.i_gmask = 0x00ff00;
206         p_vout->output.i_bmask = 0x0000ff;
207         break;
208
209       case VLC_CODEC_RGB32:
210         p_vout->output.i_rmask = 0xff0000;
211         p_vout->output.i_gmask = 0x00ff00;
212         p_vout->output.i_bmask = 0x0000ff;
213         break;
214     }
215
216     /* Try to initialize 1 direct buffer */
217     p_pic = NULL;
218
219     /* Find an empty picture slot */
220     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
221     {
222         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
223         {
224             p_pic = p_vout->p_picture + i_index;
225             break;
226         }
227     }
228
229     /* Allocate the picture */
230     if( p_pic == NULL )
231     {
232         return VLC_SUCCESS;
233     }
234
235     if( picture_Setup( p_pic, p_vout->output.i_chroma,
236                        p_vout->output.i_width, p_vout->output.i_height,
237                        p_vout->output.i_aspect ) )
238     {
239         free( p_pic );
240         return VLC_EGENERIC;
241     }
242
243     p_pic->p->i_pitch = i_pitch;
244
245     p_pic->pf_lock = LockPicture;
246     p_pic->pf_unlock = UnlockPicture;
247
248     p_pic->i_status = DESTROYED_PICTURE;
249     p_pic->i_type   = DIRECT_PICTURE;
250
251     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
252
253     I_OUTPUTPICTURES++;
254
255     return VLC_SUCCESS;
256 }
257
258 /*****************************************************************************
259  * End: terminate video thread output method
260  *****************************************************************************/
261 static void End( vout_thread_t *p_vout )
262 {
263     (void)p_vout;
264 }
265
266 /*****************************************************************************
267  * Destroy: destroy video thread
268  *****************************************************************************
269  * Terminate an output method created by Create
270  *****************************************************************************/
271 static void Destroy( vlc_object_t *p_this )
272 {
273     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
274
275     /* Destroy structure */
276     free( p_vout->p_sys );
277 }
278
279 /*****************************************************************************
280  * LockPicture: lock a picture
281  *****************************************************************************
282  * This function locks the picture and prepares it for direct pixel access.
283  *****************************************************************************/
284 static int LockPicture( vout_thread_t *p_vout, picture_t *p_pic )
285 {
286     int i_index;
287     void *planes[p_pic->i_planes];
288
289     p_vout->p_sys->pf_lock( p_vout->p_sys->p_data, planes );
290
291     for( i_index = 0; i_index < p_pic->i_planes; i_index++ )
292     {
293         p_pic->p[i_index].p_pixels = planes[i_index];
294     }
295
296     return VLC_SUCCESS;
297 }
298
299 /*****************************************************************************
300  * UnlockPicture: unlock a picture
301  *****************************************************************************
302  * This function unlocks a previously locked picture.
303  *****************************************************************************/
304 static int UnlockPicture( vout_thread_t *p_vout, picture_t *p_pic )
305 {
306     p_vout->p_sys->pf_unlock( p_vout->p_sys->p_data );
307
308     (void)p_pic;
309
310     return VLC_SUCCESS;
311 }
312