]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
* modules/video_output/x11/glx.c: OpenGL context is initialized in pf_init() because...
[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     vlc_object_attach( p_sys->p_vout, p_this );
126
127     p_sys->p_vout->i_window_width = p_vout->i_window_width;
128     p_sys->p_vout->i_window_height = p_vout->i_window_height;
129     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
130     p_sys->p_vout->render.i_width = p_vout->render.i_width;
131     p_sys->p_vout->render.i_height = p_vout->render.i_height;
132     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
133     p_sys->p_vout->i_window_height = p_vout->i_window_height;
134
135     p_sys->p_vout->p_module =
136         module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
137     if( p_sys->p_vout->p_module == NULL )
138     {
139         msg_Err( p_vout, "No OpenGL provider found" );
140         vlc_object_detach( p_sys->p_vout );
141         vlc_object_destroy( p_sys->p_vout );
142         return VLC_ENOOBJ;
143     }
144
145     p_vout->pf_init = Init;
146     p_vout->pf_end = End;
147     p_vout->pf_manage = Manage;
148     p_vout->pf_render = Render;
149     p_vout->pf_display = DisplayVideo;
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Init: initialize the OpenGL video thread output method
156  *****************************************************************************/
157 static int Init( vout_thread_t *p_vout )
158 {
159     vout_sys_t *p_sys = p_vout->p_sys;
160     int i_pixel_pitch;
161
162     p_sys->p_vout->pf_init( p_sys->p_vout );
163
164     /* No YUV textures :( */
165
166 #if VLCGL_RGB_FORMAT == GL_RGB
167 #   if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
168     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
169     p_vout->output.i_rmask = 0x000000ff;
170     p_vout->output.i_gmask = 0x0000ff00;
171     p_vout->output.i_bmask = 0x00ff0000;
172     i_pixel_pitch = 3;
173 #   else
174     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
175     p_vout->output.i_rmask = 0xf800;
176     p_vout->output.i_gmask = 0x07e0;
177     p_vout->output.i_bmask = 0x001f;
178     i_pixel_pitch = 2;
179 #   endif
180 #else
181     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
182     p_vout->output.i_rmask = 0x000000ff;
183     p_vout->output.i_gmask = 0x0000ff00;
184     p_vout->output.i_bmask = 0x00ff0000;
185     i_pixel_pitch = 4;
186 #endif
187
188     /* Since OpenGL can do rescaling for us, stick to the default
189      * coordinates and aspect. */
190     p_vout->output.i_width  = p_vout->render.i_width;
191     p_vout->output.i_height = p_vout->render.i_height;
192     p_vout->output.i_aspect = p_vout->render.i_aspect;
193
194     /* We know the chroma, allocate a buffer which will be used
195      * directly by the decoder */
196     p_vout->p_picture[0].i_planes = 1;
197     p_sys->p_buffer =
198         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
199     if( !p_sys->p_buffer )
200     {
201         msg_Err( p_vout, "Out of memory" );
202         return -1;
203     }
204
205     p_vout->p_picture[0].p->p_pixels = p_sys->p_buffer;
206     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
207     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
208     p_vout->p_picture[0].p->i_pitch = p_sys->i_tex_width *
209         p_vout->p_picture[0].p->i_pixel_pitch;
210     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
211         p_vout->p_picture[0].p->i_pixel_pitch;
212
213     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
214     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
215
216     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
217     I_OUTPUTPICTURES = 1;
218
219     /* Set the texture parameters */
220     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
221     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
222
223     if( p_sys->i_effect )
224     {
225         glEnable( GL_CULL_FACE);
226         /* glDisable( GL_DEPTH_TEST );
227         glEnable( GL_BLEND );
228         glBlendFunc( GL_SRC_ALPHA, GL_ONE );*/
229
230         /* Set the perpective */
231         glMatrixMode( GL_PROJECTION );
232         glLoadIdentity();
233         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
234         glMatrixMode( GL_MODELVIEW );
235         glLoadIdentity();
236         glTranslatef( 0.0, 0.0, - 5.0 );
237     }
238
239     return 0;
240 }
241
242 /*****************************************************************************
243  * End: terminate GLX video thread output method
244  *****************************************************************************/
245 static void End( vout_thread_t *p_vout )
246 {
247     glFinish();
248     glFlush();
249 }
250
251 /*****************************************************************************
252  * Destroy: destroy GLX video thread output method
253  *****************************************************************************
254  * Terminate an output method created by CreateVout
255  *****************************************************************************/
256 static void DestroyVout( vlc_object_t *p_this )
257 {
258     vout_thread_t *p_vout = (vout_thread_t *)p_this;
259     vout_sys_t *p_sys = p_vout->p_sys;
260
261     module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
262     vlc_object_detach( p_sys->p_vout );
263     vlc_object_destroy( p_sys->p_vout );
264
265     /* Free the texture buffer*/
266     if( p_sys->p_buffer ) free( p_sys->p_buffer );
267
268     free( p_sys );
269 }
270
271 /*****************************************************************************
272  * Manage: handle Sys events
273  *****************************************************************************
274  * This function should be called regularly by video output thread. It returns
275  * a non null value if an error occured.
276  *****************************************************************************/
277 static int Manage( vout_thread_t *p_vout )
278 {
279     vout_sys_t *p_sys = p_vout->p_sys;
280     return p_sys->p_vout->pf_manage( p_sys->p_vout );
281 }
282
283 /*****************************************************************************
284  * Render: render previously calculated output
285  *****************************************************************************/
286 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
287 {
288     vout_sys_t *p_sys = p_vout->p_sys;
289     float f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
290     float f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
291
292     glClear( GL_COLOR_BUFFER_BIT );
293
294     glTexImage2D( GL_TEXTURE_2D, 0, 3,
295                   p_sys->i_tex_width, p_sys->i_tex_height , 0,
296                   VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->p_buffer );
297
298     if( !p_sys->i_effect )
299     {
300         glEnable( GL_TEXTURE_2D );
301         glBegin( GL_POLYGON );
302         glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
303         glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
304         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
305         glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
306         glEnd();
307     }
308     else
309     {
310         glRotatef( 1.0, 0.3, 0.5, 0.7 );
311
312         glEnable( GL_TEXTURE_2D );
313         glBegin( GL_QUADS );
314
315         /* Front */
316         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
317         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
318         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
319         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
320
321         /* Left */
322         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
323         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
324         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
325         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
326
327         /* Back */
328         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
329         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
330         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
331         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
332
333         /* Right */
334         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
335         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
336         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
337         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
338
339         /* Top */
340         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
341         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
342         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
343         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
344
345         /* Bottom */
346         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
347         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
348         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
349         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
350         glEnd();
351     }
352
353     glDisable( GL_TEXTURE_2D);
354 }
355
356 /*****************************************************************************
357  * DisplayVideo: displays previously rendered output
358  *****************************************************************************/
359 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
360 {
361     vout_sys_t *p_sys = p_vout->p_sys;
362     p_sys->p_vout->pf_swap( p_sys->p_vout );
363 }
364
365 int GetAlignedSize( int i_size )
366 {
367     /* Return the nearest power of 2 */
368     int i_result = 1;
369     while( i_result < i_size )
370     {
371         i_result *= 2;
372     }
373     return i_result;
374 }