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