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