]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
* modules/video_output/opengl.c: forward mouse events from opengl-provider module.
[vlc] / modules / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: OpenGL video output
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34
35 #ifndef SYS_DARWIN
36 #include <GL/gl.h>
37 #else
38 /* Mac OS X < 10.3 does not have GL/gl.h */
39 #include <OpenGL/gl.h>
40 #endif
41
42 /* RV16 */
43 #ifndef GL_UNSIGNED_SHORT_5_6_5
44 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
45 #endif
46 //#define VLCGL_RGB_FORMAT GL_RGB
47 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
48
49 /* RV24 */
50 //#define VLCGL_RGB_FORMAT GL_RGB
51 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
52
53 /* RV32 */
54 #define VLCGL_RGB_FORMAT GL_RGBA
55 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
56
57 /* OpenGL effects */
58 #define OPENGL_EFFECT_NONE             1
59 #define OPENGL_EFFECT_CUBE             2
60 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
61
62 /*****************************************************************************
63  * Vout interface
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 );
73
74 static inline int GetAlignedSize( int );
75
76 static int SendEvents( vlc_object_t *, char const *,
77                        vlc_value_t, vlc_value_t, void * );
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 #define EFFECT_TEXT N_("Select effect")
83 #define EFFECT_LONGTEXT N_( \
84     "Allows you to select different visual effects.")
85
86 vlc_module_begin();
87     set_description( _("OpenGL video output") );
88     set_capability( "video output", 20 );
89     add_shortcut( "opengl" );
90     set_callbacks( CreateVout, DestroyVout );
91
92     add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
93                  EFFECT_LONGTEXT, VLC_TRUE );
94 vlc_module_end();
95
96 /*****************************************************************************
97  * vout_sys_t: video output method descriptor
98  *****************************************************************************
99  * This structure is part of the video output thread descriptor.
100  * It describes the OpenGL specific properties of the output thread.
101  *****************************************************************************/
102 struct vout_sys_t
103 {
104     vout_thread_t *p_vout;
105
106     uint8_t     *p_buffer;
107     int         i_index;
108     int         i_tex_width;
109     int         i_tex_height;
110     GLuint      texture;
111
112     int         i_effect;
113 };
114
115 /*****************************************************************************
116  * CreateVout: This function allocates and initializes the OpenGL vout method.
117  *****************************************************************************/
118 static int CreateVout( vlc_object_t *p_this )
119 {
120     vout_thread_t *p_vout = (vout_thread_t *)p_this;
121     vout_sys_t *p_sys;
122
123     /* Allocate structure */
124     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
125     if( p_sys == NULL )
126     {
127         msg_Err( p_vout, "out of memory" );
128         return VLC_EGENERIC;
129     }
130
131     var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
132
133     /* A texture must have a size aligned on a power of 2 */
134     p_sys->i_tex_width  = GetAlignedSize( p_vout->render.i_width );
135     p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
136
137     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
138              p_sys->i_tex_height );
139
140     /* Get window */
141     p_sys->p_vout =
142         (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
143     if( p_sys->p_vout == NULL )
144     {
145         msg_Err( p_vout, "out of memory" );
146         return VLC_ENOMEM;
147     }
148     vlc_object_attach( p_sys->p_vout, p_this );
149
150     p_sys->p_vout->i_window_width = p_vout->i_window_width;
151     p_sys->p_vout->i_window_height = p_vout->i_window_height;
152     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
153     p_sys->p_vout->render.i_width = p_vout->render.i_width;
154     p_sys->p_vout->render.i_height = p_vout->render.i_height;
155     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
156     p_sys->p_vout->b_scale = p_vout->b_scale;
157     p_sys->p_vout->i_alignment = p_vout->i_alignment;
158
159     p_sys->p_vout->p_module =
160         module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
161     if( p_sys->p_vout->p_module == NULL )
162     {
163         msg_Err( p_vout, "No OpenGL provider found" );
164         vlc_object_detach( p_sys->p_vout );
165         vlc_object_destroy( p_sys->p_vout );
166         return VLC_ENOOBJ;
167     }
168
169     p_vout->pf_init = Init;
170     p_vout->pf_end = End;
171     p_vout->pf_manage = Manage;
172     p_vout->pf_render = Render;
173     p_vout->pf_display = DisplayVideo;
174     p_vout->pf_control = Control;
175
176     /* Forward events from the opengl provider */
177     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
178     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
179     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
180     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
181
182     return VLC_SUCCESS;
183 }
184
185 /*****************************************************************************
186  * Init: initialize the OpenGL video thread output method
187  *****************************************************************************/
188 static int Init( vout_thread_t *p_vout )
189 {
190     vout_sys_t *p_sys = p_vout->p_sys;
191     int i_pixel_pitch;
192     vlc_value_t val;
193
194     p_sys->p_vout->pf_init( p_sys->p_vout );
195
196     /* No YUV textures :( */
197
198 #if VLCGL_RGB_FORMAT == GL_RGB
199 #   if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
200     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
201     p_vout->output.i_rmask = 0x000000ff;
202     p_vout->output.i_gmask = 0x0000ff00;
203     p_vout->output.i_bmask = 0x00ff0000;
204     i_pixel_pitch = 3;
205 #   else
206     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
207     p_vout->output.i_rmask = 0xf800;
208     p_vout->output.i_gmask = 0x07e0;
209     p_vout->output.i_bmask = 0x001f;
210     i_pixel_pitch = 2;
211 #   endif
212 #else
213     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
214     p_vout->output.i_rmask = 0x000000ff;
215     p_vout->output.i_gmask = 0x0000ff00;
216     p_vout->output.i_bmask = 0x00ff0000;
217     i_pixel_pitch = 4;
218 #endif
219
220     /* Since OpenGL can do rescaling for us, stick to the default
221      * coordinates and aspect. */
222     p_vout->output.i_width  = p_vout->render.i_width;
223     p_vout->output.i_height = p_vout->render.i_height;
224     p_vout->output.i_aspect = p_vout->render.i_aspect;
225
226     /* We know the chroma, allocate a buffer which will be used
227      * directly by the decoder */
228     p_vout->p_picture[0].i_planes = 1;
229     p_sys->p_buffer =
230         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
231     if( !p_sys->p_buffer )
232     {
233         msg_Err( p_vout, "Out of memory" );
234         return -1;
235     }
236
237     p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
238     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
239     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
240     p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
241         p_vout->p_picture[0].p->i_pixel_pitch;
242     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
243         p_vout->p_picture[0].p->i_pixel_pitch;
244
245     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
246     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
247
248     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
249     I_OUTPUTPICTURES = 1;
250
251     /* Set the texture parameters */
252     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0 );
253
254     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
255     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
256
257     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
258     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
259
260     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
261
262     glDisable(GL_BLEND);
263     glDisable(GL_DEPTH_TEST);
264     glDepthMask(GL_FALSE);
265     glDisable(GL_CULL_FACE);
266     glClear( GL_COLOR_BUFFER_BIT );
267
268     /* Check if the user asked for useless visual effects */
269     var_Get( p_vout, "opengl-effect", &val );
270     if( !val.psz_string || !strcmp( val.psz_string, "none" ))
271     {
272         p_sys->i_effect = OPENGL_EFFECT_NONE;
273     }
274     else if( !strcmp( val.psz_string, "cube" ) )
275     {
276         p_sys->i_effect = OPENGL_EFFECT_CUBE;
277
278         glEnable( GL_CULL_FACE);
279         //glEnable( GL_DEPTH_TEST );
280     }
281     else if( !strcmp( val.psz_string, "transparent-cube" ) )
282     {
283         p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
284
285         glDisable( GL_DEPTH_TEST );
286         glEnable( GL_BLEND );
287         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
288     }
289     else
290     {
291         msg_Warn( p_vout, "no valid opengl effect provided, using "
292                   "\"none\"" );
293         p_sys->i_effect = OPENGL_EFFECT_NONE;
294     }
295     if( val.psz_string ) free( val.psz_string );
296
297     if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
298                 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
299     {
300         /* Set the perpective */
301         glMatrixMode( GL_PROJECTION );
302         glLoadIdentity();
303         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
304         glMatrixMode( GL_MODELVIEW );
305         glLoadIdentity();
306         glTranslatef( 0.0, 0.0, - 5.0 );
307     }
308
309     return 0;
310 }
311
312 /*****************************************************************************
313  * End: terminate GLX video thread output method
314  *****************************************************************************/
315 static void End( vout_thread_t *p_vout )
316 {
317     glFinish();
318     glFlush();
319 }
320
321 /*****************************************************************************
322  * Destroy: destroy GLX video thread output method
323  *****************************************************************************
324  * Terminate an output method created by CreateVout
325  *****************************************************************************/
326 static void DestroyVout( vlc_object_t *p_this )
327 {
328     vout_thread_t *p_vout = (vout_thread_t *)p_this;
329     vout_sys_t *p_sys = p_vout->p_sys;
330
331     module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
332     vlc_object_detach( p_sys->p_vout );
333     vlc_object_destroy( p_sys->p_vout );
334
335     /* Free the texture buffer*/
336     if( p_sys->p_buffer ) free( p_sys->p_buffer );
337
338     free( p_sys );
339 }
340
341 /*****************************************************************************
342  * Manage: handle Sys events
343  *****************************************************************************
344  * This function should be called regularly by video output thread. It returns
345  * a non null value if an error occured.
346  *****************************************************************************/
347 static int Manage( vout_thread_t *p_vout )
348 {
349     vout_sys_t *p_sys = p_vout->p_sys;
350
351     return p_sys->p_vout->pf_manage( p_sys->p_vout );
352 }
353
354 /*****************************************************************************
355  * Render: render previously calculated output
356  *****************************************************************************/
357 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
358 {
359     vout_sys_t *p_sys = p_vout->p_sys;
360     float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
361     float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
362
363     glClear( GL_COLOR_BUFFER_BIT );
364
365     glTexImage2D( GL_TEXTURE_2D, 0, 3,
366                   p_sys->i_tex_width, p_sys->i_tex_height , 0,
367                   VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
368
369     if( p_sys->i_effect == OPENGL_EFFECT_NONE )
370     {
371         glEnable( GL_TEXTURE_2D );
372         glBegin( GL_POLYGON );
373         glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
374         glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
375         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
376         glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
377         glEnd();
378     }
379     else
380     {
381         glRotatef( 1.0, 0.3, 0.5, 0.7 );
382
383         glEnable( GL_TEXTURE_2D );
384         glBegin( GL_QUADS );
385
386         /* Front */
387         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
388         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
389         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
390         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
391
392         /* Left */
393         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
394         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
395         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
396         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
397
398         /* Back */
399         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
400         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
401         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
402         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
403
404         /* Right */
405         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
406         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
407         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
408         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
409
410         /* Top */
411         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
412         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
413         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
414         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
415
416         /* Bottom */
417         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
418         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
419         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
420         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
421         glEnd();
422     }
423
424     glDisable( GL_TEXTURE_2D);
425 }
426
427 /*****************************************************************************
428  * DisplayVideo: displays previously rendered output
429  *****************************************************************************/
430 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
431 {
432     vout_sys_t *p_sys = p_vout->p_sys;
433     p_sys->p_vout->pf_swap( p_sys->p_vout );
434 }
435
436 int GetAlignedSize( int i_size )
437 {
438     /* Return the nearest power of 2 */
439     int i_result = 1;
440     while( i_result < i_size )
441     {
442         i_result *= 2;
443     }
444     return i_result;
445 }
446
447 /*****************************************************************************
448  * Control: control facility for the vout
449  *****************************************************************************/
450 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
451 {
452     vout_sys_t *p_sys = p_vout->p_sys;
453
454     if( p_sys->p_vout->pf_control )
455         return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
456     else
457         return vout_vaControlDefault( p_vout, i_query, args );
458 }
459
460 /*****************************************************************************
461  * SendEvents: forward mouse and keyboard events to the parent p_vout
462  *****************************************************************************/
463 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
464                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
465 {
466     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
467 }