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" );
125 vlc_object_attach( p_sys->p_vout, p_this );
127 p_sys->p_vout->i_window_width = p_vout->i_window_width;
128 p_sys->p_vout->i_window_height = p_vout->i_window_height;
129 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
130 p_sys->p_vout->render.i_width = p_vout->render.i_width;
131 p_sys->p_vout->render.i_height = p_vout->render.i_height;
132 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
133 p_sys->p_vout->i_window_height = p_vout->i_window_height;
135 p_sys->p_vout->p_module =
136 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
137 if( p_sys->p_vout->p_module == NULL )
139 msg_Err( p_vout, "No OpenGL provider found" );
140 vlc_object_detach( p_sys->p_vout );
141 vlc_object_destroy( p_sys->p_vout );
145 p_vout->pf_init = Init;
146 p_vout->pf_end = End;
147 p_vout->pf_manage = Manage;
148 p_vout->pf_render = Render;
149 p_vout->pf_display = DisplayVideo;
154 /*****************************************************************************
155 * Init: initialize the OpenGL video thread output method
156 *****************************************************************************/
157 static int Init( vout_thread_t *p_vout )
159 vout_sys_t *p_sys = p_vout->p_sys;
162 p_sys->p_vout->pf_init( p_sys->p_vout );
164 /* No YUV textures :( */
166 #if VLCGL_RGB_FORMAT == GL_RGB
167 # if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
168 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
169 p_vout->output.i_rmask = 0x000000ff;
170 p_vout->output.i_gmask = 0x0000ff00;
171 p_vout->output.i_bmask = 0x00ff0000;
174 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
175 p_vout->output.i_rmask = 0xf800;
176 p_vout->output.i_gmask = 0x07e0;
177 p_vout->output.i_bmask = 0x001f;
181 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
182 p_vout->output.i_rmask = 0x000000ff;
183 p_vout->output.i_gmask = 0x0000ff00;
184 p_vout->output.i_bmask = 0x00ff0000;
188 /* Since OpenGL can do rescaling for us, stick to the default
189 * coordinates and aspect. */
190 p_vout->output.i_width = p_vout->render.i_width;
191 p_vout->output.i_height = p_vout->render.i_height;
192 p_vout->output.i_aspect = p_vout->render.i_aspect;
194 /* We know the chroma, allocate a buffer which will be used
195 * directly by the decoder */
196 p_vout->p_picture[0].i_planes = 1;
198 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
199 if( !p_sys->p_buffer )
201 msg_Err( p_vout, "Out of memory" );
205 p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
206 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
207 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
208 p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
209 p_vout->p_picture[0].p->i_pixel_pitch;
210 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
211 p_vout->p_picture[0].p->i_pixel_pitch;
213 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
214 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
216 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
217 I_OUTPUTPICTURES = 1;
219 /* Set the texture parameters */
220 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
221 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
223 if( p_sys->i_effect )
225 glEnable( GL_CULL_FACE);
226 /* glDisable( GL_DEPTH_TEST );
227 glEnable( GL_BLEND );
228 glBlendFunc( GL_SRC_ALPHA, GL_ONE );*/
230 /* Set the perpective */
231 glMatrixMode( GL_PROJECTION );
233 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
234 glMatrixMode( GL_MODELVIEW );
236 glTranslatef( 0.0, 0.0, - 5.0 );
242 /*****************************************************************************
243 * End: terminate GLX video thread output method
244 *****************************************************************************/
245 static void End( vout_thread_t *p_vout )
251 /*****************************************************************************
252 * Destroy: destroy GLX video thread output method
253 *****************************************************************************
254 * Terminate an output method created by CreateVout
255 *****************************************************************************/
256 static void DestroyVout( vlc_object_t *p_this )
258 vout_thread_t *p_vout = (vout_thread_t *)p_this;
259 vout_sys_t *p_sys = p_vout->p_sys;
261 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
262 vlc_object_detach( p_sys->p_vout );
263 vlc_object_destroy( p_sys->p_vout );
265 /* Free the texture buffer*/
266 if( p_sys->p_buffer ) free( p_sys->p_buffer );
271 /*****************************************************************************
272 * Manage: handle Sys events
273 *****************************************************************************
274 * This function should be called regularly by video output thread. It returns
275 * a non null value if an error occured.
276 *****************************************************************************/
277 static int Manage( vout_thread_t *p_vout )
279 vout_sys_t *p_sys = p_vout->p_sys;
280 return p_sys->p_vout->pf_manage( p_sys->p_vout );
283 /*****************************************************************************
284 * Render: render previously calculated output
285 *****************************************************************************/
286 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
288 vout_sys_t *p_sys = p_vout->p_sys;
289 float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
290 float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
292 glClear( GL_COLOR_BUFFER_BIT );
294 glTexImage2D( GL_TEXTURE_2D, 0, 3,
295 p_sys->i_tex_width, p_sys->i_tex_height , 0,
296 VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
298 if( !p_sys->i_effect )
300 glEnable( GL_TEXTURE_2D );
301 glBegin( GL_POLYGON );
302 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
303 glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
304 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
305 glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
310 glRotatef( 1.0, 0.3, 0.5, 0.7 );
312 glEnable( GL_TEXTURE_2D );
316 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
317 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
318 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
319 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
322 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
323 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
324 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
325 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
328 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
329 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
330 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
331 glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
334 glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
335 glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
336 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
337 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
340 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
341 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
342 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
343 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
346 glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
347 glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
348 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
349 glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
353 glDisable( GL_TEXTURE_2D);
356 /*****************************************************************************
357 * DisplayVideo: displays previously rendered output
358 *****************************************************************************/
359 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
361 vout_sys_t *p_sys = p_vout->p_sys;
362 p_sys->p_vout->pf_swap( p_sys->p_vout );
365 int GetAlignedSize( int i_size )
367 /* Return the nearest power of 2 */
369 while( i_result < i_size )