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 *****************************************************************************/
70 set_description( _("OpenGL video output") );
71 set_capability( "video output", 20 );
72 add_shortcut( "opengl" );
73 set_callbacks( CreateVout, DestroyVout );
76 /*****************************************************************************
77 * vout_sys_t: video output method descriptor
78 *****************************************************************************
79 * This structure is part of the video output thread descriptor.
80 * It describes the OpenGL specific properties of the output thread.
81 *****************************************************************************/
84 vout_thread_t *p_vout;
95 /*****************************************************************************
96 * CreateVout: This function allocates and initializes the OpenGL vout method.
97 *****************************************************************************/
98 static int CreateVout( vlc_object_t *p_this )
100 vout_thread_t *p_vout = (vout_thread_t *)p_this;
103 /* Allocate structure */
104 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
107 msg_Err( p_vout, "out of memory" );
111 //XXX set to 0 to disable the cube effect
114 /* A texture must have a size aligned on a power of 2 */
115 p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
116 p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
118 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
119 p_sys->i_tex_height );
123 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
124 if( p_sys->p_vout == NULL )
126 msg_Err( p_vout, "out of memory" );
129 vlc_object_attach( p_sys->p_vout, p_this );
131 p_sys->p_vout->i_window_width = p_vout->i_window_width;
132 p_sys->p_vout->i_window_height = p_vout->i_window_height;
133 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
134 p_sys->p_vout->render.i_width = p_vout->render.i_width;
135 p_sys->p_vout->render.i_height = p_vout->render.i_height;
136 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
138 p_sys->p_vout->p_module =
139 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
140 if( p_sys->p_vout->p_module == NULL )
142 msg_Err( p_vout, "No OpenGL provider found" );
143 vlc_object_detach( p_sys->p_vout );
144 vlc_object_destroy( p_sys->p_vout );
148 p_vout->pf_init = Init;
149 p_vout->pf_end = End;
150 p_vout->pf_manage = Manage;
151 p_vout->pf_render = Render;
152 p_vout->pf_display = DisplayVideo;
153 p_vout->pf_control = Control;
158 /*****************************************************************************
159 * Init: initialize the OpenGL video thread output method
160 *****************************************************************************/
161 static int Init( vout_thread_t *p_vout )
163 vout_sys_t *p_sys = p_vout->p_sys;
166 p_sys->p_vout->pf_init( p_sys->p_vout );
168 /* No YUV textures :( */
170 #if VLCGL_RGB_FORMAT == GL_RGB
171 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
172 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
173 p_vout->output.i_rmask = 0x000000ff;
174 p_vout->output.i_gmask = 0x0000ff00;
175 p_vout->output.i_bmask = 0x00ff0000;
178 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
179 p_vout->output.i_rmask = 0xf800;
180 p_vout->output.i_gmask = 0x07e0;
181 p_vout->output.i_bmask = 0x001f;
185 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
186 p_vout->output.i_rmask = 0x000000ff;
187 p_vout->output.i_gmask = 0x0000ff00;
188 p_vout->output.i_bmask = 0x00ff0000;
192 /* Since OpenGL can do rescaling for us, stick to the default
193 * coordinates and aspect. */
194 p_vout->output.i_width = p_vout->render.i_width;
195 p_vout->output.i_height = p_vout->render.i_height;
196 p_vout->output.i_aspect = p_vout->render.i_aspect;
198 /* We know the chroma, allocate a buffer which will be used
199 * directly by the decoder */
200 p_vout->p_picture[0].i_planes = 1;
202 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
203 if( !p_sys->p_buffer )
205 msg_Err( p_vout, "Out of memory" );
209 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
210 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
211 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
212 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
213 p_vout->p_picture[0].p->i_pixel_pitch;
214 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
215 p_vout->p_picture[0].p->i_pixel_pitch;
217 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
218 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
220 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
221 I_OUTPUTPICTURES = 1;
223 /* Set the texture parameters */
224 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
225 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
227 if( p_sys->i_effect )
229 glEnable( GL_CULL_FACE);
230 /* glDisable( GL_DEPTH_TEST );
231 glEnable( GL_BLEND );
232 glBlendFunc( GL_SRC_ALPHA, GL_ONE );*/
234 /* Set the perpective */
235 glMatrixMode( GL_PROJECTION );
237 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
238 glMatrixMode( GL_MODELVIEW );
240 glTranslatef( 0.0, 0.0, - 5.0 );
246 /*****************************************************************************
247 * End: terminate GLX video thread output method
248 *****************************************************************************/
249 static void End( vout_thread_t *p_vout )
255 /*****************************************************************************
256 * Destroy: destroy GLX video thread output method
257 *****************************************************************************
258 * Terminate an output method created by CreateVout
259 *****************************************************************************/
260 static void DestroyVout( vlc_object_t *p_this )
262 vout_thread_t *p_vout = (vout_thread_t *)p_this;
263 vout_sys_t *p_sys = p_vout->p_sys;
265 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
266 vlc_object_detach( p_sys->p_vout );
267 vlc_object_destroy( p_sys->p_vout );
269 /* Free the texture buffer*/
270 if( p_sys->p_buffer ) free( p_sys->p_buffer );
275 /*****************************************************************************
276 * Manage: handle Sys events
277 *****************************************************************************
278 * This function should be called regularly by video output thread. It returns
279 * a non null value if an error occured.
280 *****************************************************************************/
281 static int Manage( vout_thread_t *p_vout )
283 vout_sys_t *p_sys = p_vout->p_sys;
284 return p_sys->p_vout->pf_manage( p_sys->p_vout );
287 /*****************************************************************************
288 * Render: render previously calculated output
289 *****************************************************************************/
290 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
292 vout_sys_t *p_sys = p_vout->p_sys;
293 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
294 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
296 glClear( GL_COLOR_BUFFER_BIT );
298 glTexImage2D( GL_TEXTURE_2D, 0, 3,
299 p_sys->i_tex_width, p_sys->i_tex_height , 0,
300 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
302 if( !p_sys->i_effect )
304 glEnable( GL_TEXTURE_2D );
305 glBegin( GL_POLYGON );
306 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
307 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
308 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
309 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
314 glRotatef( 1.0, 0.3, 0.5, 0.7 );
316 glEnable( GL_TEXTURE_2D );
320 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
321 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
322 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
323 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
326 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
327 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
328 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
329 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
332 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
333 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
334 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
335 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
338 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
339 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
340 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
341 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
344 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
345 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
346 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
347 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
350 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
351 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
352 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
353 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
357 glDisable( GL_TEXTURE_2D);
360 /*****************************************************************************
361 * DisplayVideo: displays previously rendered output
362 *****************************************************************************/
363 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
365 vout_sys_t *p_sys = p_vout->p_sys;
366 p_sys->p_vout->pf_swap( p_sys->p_vout );
369 int GetAlignedSize( int i_size )
371 /* Return the nearest power of 2 */
373 while( i_result < i_size )
380 /*****************************************************************************
381 * Control: control facility for the vout
382 *****************************************************************************/
383 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
385 vout_sys_t *p_sys = p_vout->p_sys;
387 if( p_sys->p_vout->pf_control )
388 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
390 return vout_vaControlDefault( p_vout, i_query, args );