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
52 /*****************************************************************************
54 *****************************************************************************/
55 static int CreateVout ( vlc_object_t * );
56 static void DestroyVout ( vlc_object_t * );
57 static int Init ( vout_thread_t * );
58 static void End ( vout_thread_t * );
59 static int Manage ( vout_thread_t * );
60 static void Render ( vout_thread_t *, picture_t * );
61 static void DisplayVideo ( vout_thread_t *, picture_t * );
62 static int Control ( vout_thread_t *, int, va_list );
64 static inline int GetAlignedSize( int );
66 /*****************************************************************************
68 *****************************************************************************/
69 #define EFFECT_TEXT N_("Select effect")
70 #define EFFECT_LONGTEXT N_( \
71 "Allows you to select different visual effects.")
74 set_description( _("OpenGL video output") );
75 set_capability( "video output", 20 );
76 add_shortcut( "opengl" );
77 set_callbacks( CreateVout, DestroyVout );
79 add_integer( "opengl-effect", 0, NULL, EFFECT_TEXT,
80 EFFECT_LONGTEXT, VLC_TRUE );
83 /*****************************************************************************
84 * vout_sys_t: video output method descriptor
85 *****************************************************************************
86 * This structure is part of the video output thread descriptor.
87 * It describes the OpenGL specific properties of the output thread.
88 *****************************************************************************/
91 vout_thread_t *p_vout;
102 /*****************************************************************************
103 * CreateVout: This function allocates and initializes the OpenGL vout method.
104 *****************************************************************************/
105 static int CreateVout( vlc_object_t *p_this )
107 vout_thread_t *p_vout = (vout_thread_t *)p_this;
111 /* Allocate structure */
112 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
115 msg_Err( p_vout, "out of memory" );
119 var_Create( p_vout, "opengl-effect", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
120 var_Get( p_vout, "opengl-effect", &val );
121 p_sys->i_effect = val.i_int;
123 /* A texture must have a size aligned on a power of 2 */
124 p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
125 p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
127 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
128 p_sys->i_tex_height );
132 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
133 if( p_sys->p_vout == NULL )
135 msg_Err( p_vout, "out of memory" );
138 vlc_object_attach( p_sys->p_vout, p_this );
140 p_sys->p_vout->i_window_width = p_vout->i_window_width;
141 p_sys->p_vout->i_window_height = p_vout->i_window_height;
142 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
143 p_sys->p_vout->render.i_width = p_vout->render.i_width;
144 p_sys->p_vout->render.i_height = p_vout->render.i_height;
145 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
146 p_sys->p_vout->b_scale = p_vout->b_scale;
147 p_sys->p_vout->i_alignment = p_vout->i_alignment;
149 p_sys->p_vout->p_module =
150 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
151 if( p_sys->p_vout->p_module == NULL )
153 msg_Err( p_vout, "No OpenGL provider found" );
154 vlc_object_detach( p_sys->p_vout );
155 vlc_object_destroy( p_sys->p_vout );
159 p_vout->pf_init = Init;
160 p_vout->pf_end = End;
161 p_vout->pf_manage = Manage;
162 p_vout->pf_render = Render;
163 p_vout->pf_display = DisplayVideo;
164 p_vout->pf_control = Control;
169 /*****************************************************************************
170 * Init: initialize the OpenGL video thread output method
171 *****************************************************************************/
172 static int Init( vout_thread_t *p_vout )
174 vout_sys_t *p_sys = p_vout->p_sys;
177 p_sys->p_vout->pf_init( p_sys->p_vout );
179 /* No YUV textures :( */
181 #if VLCGL_RGB_FORMAT == GL_RGB
182 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
183 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
184 p_vout->output.i_rmask = 0x000000ff;
185 p_vout->output.i_gmask = 0x0000ff00;
186 p_vout->output.i_bmask = 0x00ff0000;
189 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
190 p_vout->output.i_rmask = 0xf800;
191 p_vout->output.i_gmask = 0x07e0;
192 p_vout->output.i_bmask = 0x001f;
196 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
197 p_vout->output.i_rmask = 0x000000ff;
198 p_vout->output.i_gmask = 0x0000ff00;
199 p_vout->output.i_bmask = 0x00ff0000;
203 /* Since OpenGL can do rescaling for us, stick to the default
204 * coordinates and aspect. */
205 p_vout->output.i_width = p_vout->render.i_width;
206 p_vout->output.i_height = p_vout->render.i_height;
207 p_vout->output.i_aspect = p_vout->render.i_aspect;
209 /* We know the chroma, allocate a buffer which will be used
210 * directly by the decoder */
211 p_vout->p_picture[0].i_planes = 1;
213 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
214 if( !p_sys->p_buffer )
216 msg_Err( p_vout, "Out of memory" );
220 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
221 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
222 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
223 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
224 p_vout->p_picture[0].p->i_pixel_pitch;
225 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
226 p_vout->p_picture[0].p->i_pixel_pitch;
228 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
229 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
231 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
232 I_OUTPUTPICTURES = 1;
234 /* Set the texture parameters */
235 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0 );
237 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
238 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
240 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
241 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
243 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
246 glDisable(GL_DEPTH_TEST);
247 glDepthMask(GL_FALSE);
248 glDisable(GL_CULL_FACE);
249 glClear( GL_COLOR_BUFFER_BIT );
251 if( p_sys->i_effect == 1 )
253 glEnable( GL_CULL_FACE);
255 /* Set the perpective */
256 glMatrixMode( GL_PROJECTION );
258 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
259 glMatrixMode( GL_MODELVIEW );
261 glTranslatef( 0.0, 0.0, - 5.0 );
267 /*****************************************************************************
268 * End: terminate GLX video thread output method
269 *****************************************************************************/
270 static void End( vout_thread_t *p_vout )
276 /*****************************************************************************
277 * Destroy: destroy GLX video thread output method
278 *****************************************************************************
279 * Terminate an output method created by CreateVout
280 *****************************************************************************/
281 static void DestroyVout( vlc_object_t *p_this )
283 vout_thread_t *p_vout = (vout_thread_t *)p_this;
284 vout_sys_t *p_sys = p_vout->p_sys;
286 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
287 vlc_object_detach( p_sys->p_vout );
288 vlc_object_destroy( p_sys->p_vout );
290 /* Free the texture buffer*/
291 if( p_sys->p_buffer ) free( p_sys->p_buffer );
296 /*****************************************************************************
297 * Manage: handle Sys events
298 *****************************************************************************
299 * This function should be called regularly by video output thread. It returns
300 * a non null value if an error occured.
301 *****************************************************************************/
302 static int Manage( vout_thread_t *p_vout )
304 vout_sys_t *p_sys = p_vout->p_sys;
306 return p_sys->p_vout->pf_manage( p_sys->p_vout );
309 /*****************************************************************************
310 * Render: render previously calculated output
311 *****************************************************************************/
312 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
314 vout_sys_t *p_sys = p_vout->p_sys;
315 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
316 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
318 glClear( GL_COLOR_BUFFER_BIT );
320 glTexImage2D( GL_TEXTURE_2D, 0, 3,
321 p_sys->i_tex_width, p_sys->i_tex_height , 0,
322 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
324 if( p_sys->i_effect != 1 )
326 glEnable( GL_TEXTURE_2D );
327 glBegin( GL_POLYGON );
328 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
329 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
330 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
331 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
336 glRotatef( 1.0, 0.3, 0.5, 0.7 );
338 glEnable( GL_TEXTURE_2D );
342 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
343 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
344 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
345 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
348 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
349 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
350 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
351 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
354 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
355 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
356 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
357 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
360 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
361 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
362 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
363 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
366 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
367 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
368 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
369 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
372 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
373 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
374 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
375 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
379 glDisable( GL_TEXTURE_2D);
382 /*****************************************************************************
383 * DisplayVideo: displays previously rendered output
384 *****************************************************************************/
385 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
387 vout_sys_t *p_sys = p_vout->p_sys;
388 p_sys->p_vout->pf_swap( p_sys->p_vout );
391 int GetAlignedSize( int i_size )
393 /* Return the nearest power of 2 */
395 while( i_result < i_size )
402 /*****************************************************************************
403 * Control: control facility for the vout
404 *****************************************************************************/
405 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
407 vout_sys_t *p_sys = p_vout->p_sys;
409 if( p_sys->p_vout->pf_control )
410 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
412 return vout_vaControlDefault( p_vout, i_query, args );