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