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 /* Mac OS X < 10.3 does not have GL/gl.h */
39 #include <OpenGL/gl.h>
43 #ifndef GL_UNSIGNED_SHORT_5_6_5
44 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
46 //#define VLCGL_RGB_FORMAT GL_RGB
47 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
50 //#define VLCGL_RGB_FORMAT GL_RGB
51 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
54 #define VLCGL_RGB_FORMAT GL_RGBA
55 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
58 #define OPENGL_EFFECT_NONE 1
59 #define OPENGL_EFFECT_CUBE 2
60 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
62 /*****************************************************************************
64 *****************************************************************************/
65 static int CreateVout ( vlc_object_t * );
66 static void DestroyVout ( vlc_object_t * );
67 static int Init ( vout_thread_t * );
68 static void End ( vout_thread_t * );
69 static int Manage ( vout_thread_t * );
70 static void Render ( vout_thread_t *, picture_t * );
71 static void DisplayVideo ( vout_thread_t *, picture_t * );
72 static int Control ( vout_thread_t *, int, va_list );
74 static inline int GetAlignedSize( int );
76 static int SendEvents( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
79 /*****************************************************************************
81 *****************************************************************************/
82 #define EFFECT_TEXT N_("Select effect")
83 #define EFFECT_LONGTEXT N_( \
84 "Allows you to select different visual effects.")
87 set_description( _("OpenGL video output") );
88 set_capability( "video output", 20 );
89 add_shortcut( "opengl" );
90 set_callbacks( CreateVout, DestroyVout );
92 add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
93 EFFECT_LONGTEXT, VLC_TRUE );
96 /*****************************************************************************
97 * vout_sys_t: video output method descriptor
98 *****************************************************************************
99 * This structure is part of the video output thread descriptor.
100 * It describes the OpenGL specific properties of the output thread.
101 *****************************************************************************/
104 vout_thread_t *p_vout;
115 /*****************************************************************************
116 * CreateVout: This function allocates and initializes the OpenGL vout method.
117 *****************************************************************************/
118 static int CreateVout( vlc_object_t *p_this )
120 vout_thread_t *p_vout = (vout_thread_t *)p_this;
123 /* Allocate structure */
124 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
127 msg_Err( p_vout, "out of memory" );
131 var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
133 /* A texture must have a size aligned on a power of 2 */
134 p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
135 p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
137 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
138 p_sys->i_tex_height );
142 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
143 if( p_sys->p_vout == NULL )
145 msg_Err( p_vout, "out of memory" );
148 vlc_object_attach( p_sys->p_vout, p_this );
150 p_sys->p_vout->i_window_width = p_vout->i_window_width;
151 p_sys->p_vout->i_window_height = p_vout->i_window_height;
152 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
153 p_sys->p_vout->render.i_width = p_vout->render.i_width;
154 p_sys->p_vout->render.i_height = p_vout->render.i_height;
155 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
156 p_sys->p_vout->b_scale = p_vout->b_scale;
157 p_sys->p_vout->i_alignment = p_vout->i_alignment;
159 p_sys->p_vout->p_module =
160 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
161 if( p_sys->p_vout->p_module == NULL )
163 msg_Err( p_vout, "No OpenGL provider found" );
164 vlc_object_detach( p_sys->p_vout );
165 vlc_object_destroy( p_sys->p_vout );
169 p_vout->pf_init = Init;
170 p_vout->pf_end = End;
171 p_vout->pf_manage = Manage;
172 p_vout->pf_render = Render;
173 p_vout->pf_display = DisplayVideo;
174 p_vout->pf_control = Control;
176 /* Forward events from the opengl provider */
177 var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
178 var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
179 var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
180 var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
185 /*****************************************************************************
186 * Init: initialize the OpenGL video thread output method
187 *****************************************************************************/
188 static int Init( vout_thread_t *p_vout )
190 vout_sys_t *p_sys = p_vout->p_sys;
194 p_sys->p_vout->pf_init( p_sys->p_vout );
196 /* No YUV textures :( */
198 #if VLCGL_RGB_FORMAT == GL_RGB
199 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
200 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
201 p_vout->output.i_rmask = 0x000000ff;
202 p_vout->output.i_gmask = 0x0000ff00;
203 p_vout->output.i_bmask = 0x00ff0000;
206 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
207 p_vout->output.i_rmask = 0xf800;
208 p_vout->output.i_gmask = 0x07e0;
209 p_vout->output.i_bmask = 0x001f;
213 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
214 p_vout->output.i_rmask = 0x000000ff;
215 p_vout->output.i_gmask = 0x0000ff00;
216 p_vout->output.i_bmask = 0x00ff0000;
220 /* Since OpenGL can do rescaling for us, stick to the default
221 * coordinates and aspect. */
222 p_vout->output.i_width = p_vout->render.i_width;
223 p_vout->output.i_height = p_vout->render.i_height;
224 p_vout->output.i_aspect = p_vout->render.i_aspect;
226 /* We know the chroma, allocate a buffer which will be used
227 * directly by the decoder */
228 p_vout->p_picture[0].i_planes = 1;
230 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
231 if( !p_sys->p_buffer )
233 msg_Err( p_vout, "Out of memory" );
237 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
238 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
239 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
240 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
241 p_vout->p_picture[0].p->i_pixel_pitch;
242 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
243 p_vout->p_picture[0].p->i_pixel_pitch;
245 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
246 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
248 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
249 I_OUTPUTPICTURES = 1;
251 /* Set the texture parameters */
252 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0 );
254 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
255 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
257 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
258 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
260 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
263 glDisable(GL_DEPTH_TEST);
264 glDepthMask(GL_FALSE);
265 glDisable(GL_CULL_FACE);
266 glClear( GL_COLOR_BUFFER_BIT );
268 /* Check if the user asked for useless visual effects */
269 var_Get( p_vout, "opengl-effect", &val );
270 if( !val.psz_string || !strcmp( val.psz_string, "none" ))
272 p_sys->i_effect = OPENGL_EFFECT_NONE;
274 else if( !strcmp( val.psz_string, "cube" ) )
276 p_sys->i_effect = OPENGL_EFFECT_CUBE;
278 glEnable( GL_CULL_FACE);
279 //glEnable( GL_DEPTH_TEST );
281 else if( !strcmp( val.psz_string, "transparent-cube" ) )
283 p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
285 glDisable( GL_DEPTH_TEST );
286 glEnable( GL_BLEND );
287 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
291 msg_Warn( p_vout, "no valid opengl effect provided, using "
293 p_sys->i_effect = OPENGL_EFFECT_NONE;
295 if( val.psz_string ) free( val.psz_string );
297 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
298 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
300 /* Set the perpective */
301 glMatrixMode( GL_PROJECTION );
303 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
304 glMatrixMode( GL_MODELVIEW );
306 glTranslatef( 0.0, 0.0, - 5.0 );
312 /*****************************************************************************
313 * End: terminate GLX video thread output method
314 *****************************************************************************/
315 static void End( vout_thread_t *p_vout )
321 /*****************************************************************************
322 * Destroy: destroy GLX video thread output method
323 *****************************************************************************
324 * Terminate an output method created by CreateVout
325 *****************************************************************************/
326 static void DestroyVout( vlc_object_t *p_this )
328 vout_thread_t *p_vout = (vout_thread_t *)p_this;
329 vout_sys_t *p_sys = p_vout->p_sys;
331 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
332 vlc_object_detach( p_sys->p_vout );
333 vlc_object_destroy( p_sys->p_vout );
335 /* Free the texture buffer*/
336 if( p_sys->p_buffer ) free( p_sys->p_buffer );
341 /*****************************************************************************
342 * Manage: handle Sys events
343 *****************************************************************************
344 * This function should be called regularly by video output thread. It returns
345 * a non null value if an error occured.
346 *****************************************************************************/
347 static int Manage( vout_thread_t *p_vout )
349 vout_sys_t *p_sys = p_vout->p_sys;
351 return p_sys->p_vout->pf_manage( p_sys->p_vout );
354 /*****************************************************************************
355 * Render: render previously calculated output
356 *****************************************************************************/
357 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
359 vout_sys_t *p_sys = p_vout->p_sys;
360 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
361 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
363 glClear( GL_COLOR_BUFFER_BIT );
365 glTexImage2D( GL_TEXTURE_2D, 0, 3,
366 p_sys->i_tex_width, p_sys->i_tex_height , 0,
367 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
369 if( p_sys->i_effect == OPENGL_EFFECT_NONE )
371 glEnable( GL_TEXTURE_2D );
372 glBegin( GL_POLYGON );
373 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
374 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
375 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
376 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
381 glRotatef( 1.0, 0.3, 0.5, 0.7 );
383 glEnable( GL_TEXTURE_2D );
387 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
388 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
389 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
390 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
393 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
394 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
395 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
396 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
399 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
400 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
401 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
402 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
405 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
406 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
407 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
408 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
411 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
412 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
413 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
414 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
417 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
418 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
419 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
420 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
424 glDisable( GL_TEXTURE_2D);
427 /*****************************************************************************
428 * DisplayVideo: displays previously rendered output
429 *****************************************************************************/
430 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
432 vout_sys_t *p_sys = p_vout->p_sys;
433 p_sys->p_vout->pf_swap( p_sys->p_vout );
436 int GetAlignedSize( int i_size )
438 /* Return the nearest power of 2 */
440 while( i_result < i_size )
447 /*****************************************************************************
448 * Control: control facility for the vout
449 *****************************************************************************/
450 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
452 vout_sys_t *p_sys = p_vout->p_sys;
454 if( p_sys->p_vout->pf_control )
455 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
457 return vout_vaControlDefault( p_vout, i_query, args );
460 /*****************************************************************************
461 * SendEvents: forward mouse and keyboard events to the parent p_vout
462 *****************************************************************************/
463 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
464 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
466 return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );