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