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 /*****************************************************************************
78 *****************************************************************************/
79 #define EFFECT_TEXT N_("Select effect")
80 #define EFFECT_LONGTEXT N_( \
81 "Allows you to select different visual effects.")
84 set_description( _("OpenGL video output") );
85 set_capability( "video output", 20 );
86 add_shortcut( "opengl" );
87 set_callbacks( CreateVout, DestroyVout );
89 add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
90 EFFECT_LONGTEXT, VLC_TRUE );
93 /*****************************************************************************
94 * vout_sys_t: video output method descriptor
95 *****************************************************************************
96 * This structure is part of the video output thread descriptor.
97 * It describes the OpenGL specific properties of the output thread.
98 *****************************************************************************/
101 vout_thread_t *p_vout;
112 /*****************************************************************************
113 * CreateVout: This function allocates and initializes the OpenGL vout method.
114 *****************************************************************************/
115 static int CreateVout( vlc_object_t *p_this )
117 vout_thread_t *p_vout = (vout_thread_t *)p_this;
120 /* Allocate structure */
121 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
124 msg_Err( p_vout, "out of memory" );
128 var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
130 /* A texture must have a size aligned on a power of 2 */
131 p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
132 p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
134 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
135 p_sys->i_tex_height );
139 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
140 if( p_sys->p_vout == NULL )
142 msg_Err( p_vout, "out of memory" );
145 vlc_object_attach( p_sys->p_vout, p_this );
147 p_sys->p_vout->i_window_width = p_vout->i_window_width;
148 p_sys->p_vout->i_window_height = p_vout->i_window_height;
149 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
150 p_sys->p_vout->render.i_width = p_vout->render.i_width;
151 p_sys->p_vout->render.i_height = p_vout->render.i_height;
152 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
153 p_sys->p_vout->b_scale = p_vout->b_scale;
154 p_sys->p_vout->i_alignment = p_vout->i_alignment;
156 p_sys->p_vout->p_module =
157 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
158 if( p_sys->p_vout->p_module == NULL )
160 msg_Err( p_vout, "No OpenGL provider found" );
161 vlc_object_detach( p_sys->p_vout );
162 vlc_object_destroy( p_sys->p_vout );
166 p_vout->pf_init = Init;
167 p_vout->pf_end = End;
168 p_vout->pf_manage = Manage;
169 p_vout->pf_render = Render;
170 p_vout->pf_display = DisplayVideo;
171 p_vout->pf_control = Control;
176 /*****************************************************************************
177 * Init: initialize the OpenGL video thread output method
178 *****************************************************************************/
179 static int Init( vout_thread_t *p_vout )
181 vout_sys_t *p_sys = p_vout->p_sys;
185 p_sys->p_vout->pf_init( p_sys->p_vout );
187 /* No YUV textures :( */
189 #if VLCGL_RGB_FORMAT == GL_RGB
190 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
191 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
192 p_vout->output.i_rmask = 0x000000ff;
193 p_vout->output.i_gmask = 0x0000ff00;
194 p_vout->output.i_bmask = 0x00ff0000;
197 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
198 p_vout->output.i_rmask = 0xf800;
199 p_vout->output.i_gmask = 0x07e0;
200 p_vout->output.i_bmask = 0x001f;
204 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
205 p_vout->output.i_rmask = 0x000000ff;
206 p_vout->output.i_gmask = 0x0000ff00;
207 p_vout->output.i_bmask = 0x00ff0000;
211 /* Since OpenGL can do rescaling for us, stick to the default
212 * coordinates and aspect. */
213 p_vout->output.i_width = p_vout->render.i_width;
214 p_vout->output.i_height = p_vout->render.i_height;
215 p_vout->output.i_aspect = p_vout->render.i_aspect;
217 /* We know the chroma, allocate a buffer which will be used
218 * directly by the decoder */
219 p_vout->p_picture[0].i_planes = 1;
221 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
222 if( !p_sys->p_buffer )
224 msg_Err( p_vout, "Out of memory" );
228 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
229 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
230 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
231 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
232 p_vout->p_picture[0].p->i_pixel_pitch;
233 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
234 p_vout->p_picture[0].p->i_pixel_pitch;
236 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
237 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
239 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
240 I_OUTPUTPICTURES = 1;
242 /* Set the texture parameters */
243 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0 );
245 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
246 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
248 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
249 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
251 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
254 glDisable(GL_DEPTH_TEST);
255 glDepthMask(GL_FALSE);
256 glDisable(GL_CULL_FACE);
257 glClear( GL_COLOR_BUFFER_BIT );
259 /* Check if the user asked for useless visual effects */
260 var_Get( p_vout, "opengl-effect", &val );
261 if( !val.psz_string || !strcmp( val.psz_string, "none" ))
263 p_sys->i_effect = OPENGL_EFFECT_NONE;
265 else if( !strcmp( val.psz_string, "cube" ) )
267 p_sys->i_effect = OPENGL_EFFECT_CUBE;
269 glEnable( GL_CULL_FACE);
270 //glEnable( GL_DEPTH_TEST );
272 else if( !strcmp( val.psz_string, "transparent-cube" ) )
274 p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
276 glDisable( GL_DEPTH_TEST );
277 glEnable( GL_BLEND );
278 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
282 msg_Warn( p_vout, "no valid opengl effect provided, using "
284 p_sys->i_effect = OPENGL_EFFECT_NONE;
286 if( val.psz_string ) free( val.psz_string );
288 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
289 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
291 /* Set the perpective */
292 glMatrixMode( GL_PROJECTION );
294 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
295 glMatrixMode( GL_MODELVIEW );
297 glTranslatef( 0.0, 0.0, - 5.0 );
303 /*****************************************************************************
304 * End: terminate GLX video thread output method
305 *****************************************************************************/
306 static void End( vout_thread_t *p_vout )
312 /*****************************************************************************
313 * Destroy: destroy GLX video thread output method
314 *****************************************************************************
315 * Terminate an output method created by CreateVout
316 *****************************************************************************/
317 static void DestroyVout( vlc_object_t *p_this )
319 vout_thread_t *p_vout = (vout_thread_t *)p_this;
320 vout_sys_t *p_sys = p_vout->p_sys;
322 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
323 vlc_object_detach( p_sys->p_vout );
324 vlc_object_destroy( p_sys->p_vout );
326 /* Free the texture buffer*/
327 if( p_sys->p_buffer ) free( p_sys->p_buffer );
332 /*****************************************************************************
333 * Manage: handle Sys events
334 *****************************************************************************
335 * This function should be called regularly by video output thread. It returns
336 * a non null value if an error occured.
337 *****************************************************************************/
338 static int Manage( vout_thread_t *p_vout )
340 vout_sys_t *p_sys = p_vout->p_sys;
342 return p_sys->p_vout->pf_manage( p_sys->p_vout );
345 /*****************************************************************************
346 * Render: render previously calculated output
347 *****************************************************************************/
348 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
350 vout_sys_t *p_sys = p_vout->p_sys;
351 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
352 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
354 glClear( GL_COLOR_BUFFER_BIT );
356 glTexImage2D( GL_TEXTURE_2D, 0, 3,
357 p_sys->i_tex_width, p_sys->i_tex_height , 0,
358 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
360 if( p_sys->i_effect == OPENGL_EFFECT_NONE )
362 glEnable( GL_TEXTURE_2D );
363 glBegin( GL_POLYGON );
364 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
365 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
366 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
367 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
372 glRotatef( 1.0, 0.3, 0.5, 0.7 );
374 glEnable( GL_TEXTURE_2D );
378 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
379 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
380 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
381 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
384 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
385 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
386 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
387 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
390 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
391 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
392 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
393 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
396 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
397 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
398 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
399 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
402 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
403 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
404 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
405 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
408 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
409 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
410 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
411 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
415 glDisable( GL_TEXTURE_2D);
418 /*****************************************************************************
419 * DisplayVideo: displays previously rendered output
420 *****************************************************************************/
421 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
423 vout_sys_t *p_sys = p_vout->p_sys;
424 p_sys->p_vout->pf_swap( p_sys->p_vout );
427 int GetAlignedSize( int i_size )
429 /* Return the nearest power of 2 */
431 while( i_result < i_size )
438 /*****************************************************************************
439 * Control: control facility for the vout
440 *****************************************************************************/
441 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
443 vout_sys_t *p_sys = p_vout->p_sys;
445 if( p_sys->p_vout->pf_control )
446 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
448 return vout_vaControlDefault( p_vout, i_query, args );