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