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