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 //#define VLCGL_RGB_FORMAT GL_RGB
39 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
42 //#define VLCGL_RGB_FORMAT GL_RGB
43 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
46 #define VLCGL_RGB_FORMAT GL_RGBA
47 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
49 /*****************************************************************************
51 *****************************************************************************/
52 static int CreateVout ( vlc_object_t * );
53 static void DestroyVout ( vlc_object_t * );
54 static int Init ( vout_thread_t * );
55 static void End ( vout_thread_t * );
56 static int Manage ( vout_thread_t * );
57 static void Render ( vout_thread_t *, picture_t * );
58 static void DisplayVideo ( vout_thread_t *, picture_t * );
60 static inline int GetAlignedSize( int );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( _("OpenGL video output") );
67 set_capability( "video output", 20 );
68 add_shortcut( "opengl" );
69 set_callbacks( CreateVout, DestroyVout );
72 /*****************************************************************************
73 * vout_sys_t: video output method descriptor
74 *****************************************************************************
75 * This structure is part of the video output thread descriptor.
76 * It describes the OpenGL specific properties of the output thread.
77 *****************************************************************************/
80 vout_thread_t *p_vout;
91 /*****************************************************************************
92 * CreateVout: This function allocates and initializes the OpenGL vout method.
93 *****************************************************************************/
94 static int CreateVout( vlc_object_t *p_this )
96 vout_thread_t *p_vout = (vout_thread_t *)p_this;
99 /* Allocate structure */
100 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
103 msg_Err( p_vout, "out of memory" );
107 //XXX set to 0 to disable the cube effect
110 /* A texture must have a size aligned on a power of 2 */
111 p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
112 p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
114 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
115 p_sys->i_tex_height );
119 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
120 if( p_sys->p_vout == NULL )
122 msg_Err( p_vout, "out of memory" );
126 p_sys->p_vout->i_window_width = p_vout->i_window_width;
127 p_sys->p_vout->i_window_height = p_vout->i_window_height;
128 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
129 p_sys->p_vout->render.i_width = p_vout->render.i_width;
130 p_sys->p_vout->render.i_height = p_vout->render.i_height;
131 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
132 p_sys->p_vout->i_window_height = p_vout->i_window_height;
134 p_sys->p_vout->p_module =
135 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
136 if( p_sys->p_vout->p_module == NULL )
138 msg_Err( p_vout, "No OpenGL provider found" );
139 vlc_object_destroy( p_sys->p_vout );
142 vlc_object_attach( p_sys->p_vout, p_this );
144 p_vout->pf_init = Init;
145 p_vout->pf_end = End;
146 p_vout->pf_manage = Manage;
147 p_vout->pf_render = Render;
148 p_vout->pf_display = DisplayVideo;
153 /*****************************************************************************
154 * Init: initialize the OpenGL video thread output method
155 *****************************************************************************/
156 static int Init( vout_thread_t *p_vout )
158 vout_sys_t *p_sys = p_vout->p_sys;
161 /* No YUV textures :( */
163 #if VLCGL_RGB_FORMAT == GL_RGB
164 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
165 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
166 p_vout->output.i_rmask = 0x000000ff;
167 p_vout->output.i_gmask = 0x0000ff00;
168 p_vout->output.i_bmask = 0x00ff0000;
171 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
172 p_vout->output.i_rmask = 0xf800;
173 p_vout->output.i_gmask = 0x07e0;
174 p_vout->output.i_bmask = 0x001f;
178 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
179 p_vout->output.i_rmask = 0x000000ff;
180 p_vout->output.i_gmask = 0x0000ff00;
181 p_vout->output.i_bmask = 0x00ff0000;
185 /* Since OpenGL can do rescaling for us, stick to the default
186 * coordinates and aspect. */
187 p_vout->output.i_width = p_vout->render.i_width;
188 p_vout->output.i_height = p_vout->render.i_height;
189 p_vout->output.i_aspect = p_vout->render.i_aspect;
191 /* We know the chroma, allocate a buffer which will be used
192 * directly by the decoder */
193 p_vout->p_picture[0].i_planes = 1;
195 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
196 if( !p_sys->p_buffer )
198 msg_Err( p_vout, "Out of memory" );
202 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
203 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
204 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
205 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
206 p_vout->p_picture[0].p->i_pixel_pitch;
207 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
208 p_vout->p_picture[0].p->i_pixel_pitch;
210 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
211 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
213 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
214 I_OUTPUTPICTURES = 1;
216 /* Set the texture parameters */
217 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
218 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
220 if( p_sys->i_effect )
222 glEnable( GL_CULL_FACE);
223 /* glDisable( GL_DEPTH_TEST );
224 glEnable( GL_BLEND );
225 glBlendFunc( GL_SRC_ALPHA, GL_ONE );*/
227 /* Set the perpective */
228 glMatrixMode( GL_PROJECTION );
230 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
231 glMatrixMode( GL_MODELVIEW );
233 glTranslatef( 0.0, 0.0, - 5.0 );
239 /*****************************************************************************
240 * End: terminate GLX video thread output method
241 *****************************************************************************/
242 static void End( vout_thread_t *p_vout )
248 /*****************************************************************************
249 * Destroy: destroy GLX video thread output method
250 *****************************************************************************
251 * Terminate an output method created by CreateVout
252 *****************************************************************************/
253 static void DestroyVout( vlc_object_t *p_this )
255 vout_thread_t *p_vout = (vout_thread_t *)p_this;
256 vout_sys_t *p_sys = p_vout->p_sys;
258 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
259 vlc_object_detach( p_sys->p_vout );
260 vlc_object_destroy( p_sys->p_vout );
262 /* Free the texture buffer*/
263 if( p_sys->p_buffer ) free( p_sys->p_buffer );
268 /*****************************************************************************
269 * Manage: handle Sys events
270 *****************************************************************************
271 * This function should be called regularly by video output thread. It returns
272 * a non null value if an error occured.
273 *****************************************************************************/
274 static int Manage( vout_thread_t *p_vout )
276 vout_sys_t *p_sys = p_vout->p_sys;
277 return p_sys->p_vout->pf_manage( p_sys->p_vout );
280 /*****************************************************************************
281 * Render: render previously calculated output
282 *****************************************************************************/
283 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
285 vout_sys_t *p_sys = p_vout->p_sys;
286 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
287 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
289 glClear( GL_COLOR_BUFFER_BIT );
291 glTexImage2D( GL_TEXTURE_2D, 0, 3,
292 p_sys->i_tex_width, p_sys->i_tex_height , 0,
293 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
295 if( !p_sys->i_effect )
297 glEnable( GL_TEXTURE_2D );
298 glBegin( GL_POLYGON );
299 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
300 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
301 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
302 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
307 glRotatef( 1.0, 0.3, 0.5, 0.7 );
309 glEnable( GL_TEXTURE_2D );
313 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
314 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
315 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
316 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
319 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
320 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
321 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
322 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
325 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
326 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
327 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
328 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
331 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
332 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
333 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
334 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
337 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
338 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
339 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
340 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
343 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
344 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
345 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
346 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
350 glDisable( GL_TEXTURE_2D);
353 /*****************************************************************************
354 * DisplayVideo: displays previously rendered output
355 *****************************************************************************/
356 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
358 vout_sys_t *p_sys = p_vout->p_sys;
359 p_sys->p_vout->pf_swap( p_sys->p_vout );
362 int GetAlignedSize( int i_size )
364 /* Return the nearest power of 2 */
366 while( i_result < i_size )