1 /*****************************************************************************
2 * opengl.c: OpenGL video output
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* malloc(), free() */
38 #ifndef GL_UNSIGNED_SHORT_5_6_5
39 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
41 //#define VLCGL_RGB_FORMAT GL_RGB
42 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
45 //#define VLCGL_RGB_FORMAT GL_RGB
46 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
49 #define VLCGL_RGB_FORMAT GL_RGBA
50 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
53 #define OPENGL_EFFECT_NONE 1
54 #define OPENGL_EFFECT_CUBE 2
55 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
57 /*****************************************************************************
59 *****************************************************************************/
60 static int CreateVout ( vlc_object_t * );
61 static void DestroyVout ( vlc_object_t * );
62 static int Init ( vout_thread_t * );
63 static void End ( vout_thread_t * );
64 static int Manage ( vout_thread_t * );
65 static void Render ( vout_thread_t *, picture_t * );
66 static void DisplayVideo ( vout_thread_t *, picture_t * );
67 static int Control ( vout_thread_t *, int, va_list );
69 static inline int GetAlignedSize( int );
71 /*****************************************************************************
73 *****************************************************************************/
74 #define EFFECT_TEXT N_("Select effect")
75 #define EFFECT_LONGTEXT N_( \
76 "Allows you to select different visual effects.")
79 set_description( _("OpenGL video output") );
80 set_capability( "video output", 20 );
81 add_shortcut( "opengl" );
82 set_callbacks( CreateVout, DestroyVout );
84 add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
85 EFFECT_LONGTEXT, VLC_TRUE );
88 /*****************************************************************************
89 * vout_sys_t: video output method descriptor
90 *****************************************************************************
91 * This structure is part of the video output thread descriptor.
92 * It describes the OpenGL specific properties of the output thread.
93 *****************************************************************************/
96 vout_thread_t *p_vout;
107 /*****************************************************************************
108 * CreateVout: This function allocates and initializes the OpenGL vout method.
109 *****************************************************************************/
110 static int CreateVout( vlc_object_t *p_this )
112 vout_thread_t *p_vout = (vout_thread_t *)p_this;
115 /* Allocate structure */
116 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
119 msg_Err( p_vout, "out of memory" );
123 var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
125 /* A texture must have a size aligned on a power of 2 */
126 p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
127 p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
129 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
130 p_sys->i_tex_height );
134 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
135 if( p_sys->p_vout == NULL )
137 msg_Err( p_vout, "out of memory" );
140 vlc_object_attach( p_sys->p_vout, p_this );
142 p_sys->p_vout->i_window_width = p_vout->i_window_width;
143 p_sys->p_vout->i_window_height = p_vout->i_window_height;
144 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
145 p_sys->p_vout->render.i_width = p_vout->render.i_width;
146 p_sys->p_vout->render.i_height = p_vout->render.i_height;
147 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
148 p_sys->p_vout->b_scale = p_vout->b_scale;
149 p_sys->p_vout->i_alignment = p_vout->i_alignment;
151 p_sys->p_vout->p_module =
152 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
153 if( p_sys->p_vout->p_module == NULL )
155 msg_Err( p_vout, "No OpenGL provider found" );
156 vlc_object_detach( p_sys->p_vout );
157 vlc_object_destroy( p_sys->p_vout );
161 p_vout->pf_init = Init;
162 p_vout->pf_end = End;
163 p_vout->pf_manage = Manage;
164 p_vout->pf_render = Render;
165 p_vout->pf_display = DisplayVideo;
166 p_vout->pf_control = Control;
171 /*****************************************************************************
172 * Init: initialize the OpenGL video thread output method
173 *****************************************************************************/
174 static int Init( vout_thread_t *p_vout )
176 vout_sys_t *p_sys = p_vout->p_sys;
180 p_sys->p_vout->pf_init( p_sys->p_vout );
182 /* No YUV textures :( */
184 #if VLCGL_RGB_FORMAT == GL_RGB
185 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
186 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
187 p_vout->output.i_rmask = 0x000000ff;
188 p_vout->output.i_gmask = 0x0000ff00;
189 p_vout->output.i_bmask = 0x00ff0000;
192 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
193 p_vout->output.i_rmask = 0xf800;
194 p_vout->output.i_gmask = 0x07e0;
195 p_vout->output.i_bmask = 0x001f;
199 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
200 p_vout->output.i_rmask = 0x000000ff;
201 p_vout->output.i_gmask = 0x0000ff00;
202 p_vout->output.i_bmask = 0x00ff0000;
206 /* Since OpenGL can do rescaling for us, stick to the default
207 * coordinates and aspect. */
208 p_vout->output.i_width = p_vout->render.i_width;
209 p_vout->output.i_height = p_vout->render.i_height;
210 p_vout->output.i_aspect = p_vout->render.i_aspect;
212 /* We know the chroma, allocate a buffer which will be used
213 * directly by the decoder */
214 p_vout->p_picture[0].i_planes = 1;
216 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
217 if( !p_sys->p_buffer )
219 msg_Err( p_vout, "Out of memory" );
223 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
224 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
225 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
226 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
227 p_vout->p_picture[0].p->i_pixel_pitch;
228 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
229 p_vout->p_picture[0].p->i_pixel_pitch;
231 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
232 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
234 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
235 I_OUTPUTPICTURES = 1;
237 /* Set the texture parameters */
238 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0 );
240 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
241 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
243 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
244 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
246 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
249 glDisable(GL_DEPTH_TEST);
250 glDepthMask(GL_FALSE);
251 glDisable(GL_CULL_FACE);
252 glClear( GL_COLOR_BUFFER_BIT );
254 /* Check if the user asked for useless visual effects */
255 var_Get( p_vout, "opengl-effect", &val );
256 if( !val.psz_string || !strcmp( val.psz_string, "none" ))
258 p_sys->i_effect = OPENGL_EFFECT_NONE;
260 else if( !strcmp( val.psz_string, "cube" ) )
262 p_sys->i_effect = OPENGL_EFFECT_CUBE;
264 glEnable( GL_CULL_FACE);
265 //glEnable( GL_DEPTH_TEST );
267 else if( !strcmp( val.psz_string, "transparent-cube" ) )
269 p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
271 glDisable( GL_DEPTH_TEST );
272 glEnable( GL_BLEND );
273 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
277 msg_Warn( p_vout, "no valid opengl effect provided, using "
279 p_sys->i_effect = OPENGL_EFFECT_NONE;
281 if( val.psz_string ) free( val.psz_string );
283 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
284 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
286 /* Set the perpective */
287 glMatrixMode( GL_PROJECTION );
289 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
290 glMatrixMode( GL_MODELVIEW );
292 glTranslatef( 0.0, 0.0, - 5.0 );
298 /*****************************************************************************
299 * End: terminate GLX video thread output method
300 *****************************************************************************/
301 static void End( vout_thread_t *p_vout )
307 /*****************************************************************************
308 * Destroy: destroy GLX video thread output method
309 *****************************************************************************
310 * Terminate an output method created by CreateVout
311 *****************************************************************************/
312 static void DestroyVout( vlc_object_t *p_this )
314 vout_thread_t *p_vout = (vout_thread_t *)p_this;
315 vout_sys_t *p_sys = p_vout->p_sys;
317 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
318 vlc_object_detach( p_sys->p_vout );
319 vlc_object_destroy( p_sys->p_vout );
321 /* Free the texture buffer*/
322 if( p_sys->p_buffer ) free( p_sys->p_buffer );
327 /*****************************************************************************
328 * Manage: handle Sys events
329 *****************************************************************************
330 * This function should be called regularly by video output thread. It returns
331 * a non null value if an error occured.
332 *****************************************************************************/
333 static int Manage( vout_thread_t *p_vout )
335 vout_sys_t *p_sys = p_vout->p_sys;
337 return p_sys->p_vout->pf_manage( p_sys->p_vout );
340 /*****************************************************************************
341 * Render: render previously calculated output
342 *****************************************************************************/
343 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
345 vout_sys_t *p_sys = p_vout->p_sys;
346 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
347 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
349 glClear( GL_COLOR_BUFFER_BIT );
351 glTexImage2D( GL_TEXTURE_2D, 0, 3,
352 p_sys->i_tex_width, p_sys->i_tex_height , 0,
353 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
355 if( p_sys->i_effect == OPENGL_EFFECT_NONE )
357 glEnable( GL_TEXTURE_2D );
358 glBegin( GL_POLYGON );
359 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
360 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
361 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
362 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
367 glRotatef( 1.0, 0.3, 0.5, 0.7 );
369 glEnable( GL_TEXTURE_2D );
373 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
374 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
375 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
376 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
379 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
380 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
381 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
382 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
385 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
386 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
387 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
388 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
391 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
392 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
393 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
394 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
397 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
398 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
399 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
400 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
403 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
404 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
405 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
406 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
410 glDisable( GL_TEXTURE_2D);
413 /*****************************************************************************
414 * DisplayVideo: displays previously rendered output
415 *****************************************************************************/
416 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
418 vout_sys_t *p_sys = p_vout->p_sys;
419 p_sys->p_vout->pf_swap( p_sys->p_vout );
422 int GetAlignedSize( int i_size )
424 /* Return the nearest power of 2 */
426 while( i_result < i_size )
433 /*****************************************************************************
434 * Control: control facility for the vout
435 *****************************************************************************/
436 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
438 vout_sys_t *p_sys = p_vout->p_sys;
440 if( p_sys->p_vout->pf_control )
441 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
443 return vout_vaControlDefault( p_vout, i_query, args );