]> git.sesse.net Git - vlc/blob - modules/video_output/opengllayer.m
Don't include config.h from the headers - refs #297.
[vlc] / modules / video_output / opengllayer.m
1 /*****************************************************************************
2  * opengl.c: CAOpenGLLayer (Mac OS X) video output. Display a video output in
3  * a layer. The layer will register itself to the drawable object stored in 
4  * the "drawable" variable. 
5  *****************************************************************************
6  * Copyright (C) 2004 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Cyril Deguet <asmax@videolan.org>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *          Eric Petit <titer@m0k.org>
12  *          Cedric Cocquebert <cedric.cocquebert@supelec.fr>
13  *          Pierre d'Herbemont <pdherbemont # videolan.org>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc/vlc.h>
40 #include <vlc_vout.h>
41
42 #import <QuartzCore/QuartzCore.h>
43 #import <Cocoa/Cocoa.h>
44 #import <OpenGL/OpenGL.h>
45
46 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
47    This allows sizes which are not powers of 2 */
48 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
49
50 /* OS X OpenGL supports YUV. Hehe. */
51 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
52 #define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
53
54 /* RV32 */
55 #define VLCGL_RGB_FORMAT GL_RGBA
56 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
57
58 /* YUY2 */
59 #ifndef YCBCR_MESA
60 #define YCBCR_MESA 0x8757
61 #endif
62 #ifndef UNSIGNED_SHORT_8_8_MESA
63 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
64 #endif
65 #define VLCGL_YUV_FORMAT YCBCR_MESA
66 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
67
68
69 #ifndef GL_CLAMP_TO_EDGE
70 #   define GL_CLAMP_TO_EDGE 0x812F
71 #endif
72
73 @interface VLCVideoView : NSObject
74 - (void)addVoutLayer:(CALayer *)layer;
75 @end
76
77 /*****************************************************************************
78  * Vout interface
79  *****************************************************************************/
80 static int  CreateVout   ( vlc_object_t * );
81 static void DestroyVout  ( vlc_object_t * );
82 static int  Init         ( vout_thread_t * );
83 static void End          ( vout_thread_t * );
84 static int  Manage       ( vout_thread_t * );
85 static void Render       ( vout_thread_t *, picture_t * );
86 static void DisplayVideo ( vout_thread_t *, picture_t * );
87 static int  Control      ( vout_thread_t *, int, va_list );
88
89 static int InitTextures  ( vout_thread_t * );
90
91 vlc_module_begin();
92     set_shortname( "OpenGLLayer" );
93     set_category( CAT_VIDEO );
94     set_subcategory( SUBCAT_VIDEO_VOUT );
95     set_description( _("Core Animation OpenGL Layer (Mac OS X)") );
96     set_capability( "video output", 20 );
97     add_shortcut( "opengllayer" );
98     set_callbacks( CreateVout, DestroyVout );
99 vlc_module_end();
100
101 @interface VLCVoutLayer : CAOpenGLLayer {
102     vout_thread_t * p_vout;
103 }
104 + (id)layerWithVout:(vout_thread_t*)_p_vout; 
105 @end
106
107 /*****************************************************************************
108  * vout_sys_t: video output method descriptor
109  *****************************************************************************
110  * This structure is part of the video output thread descriptor.
111  * It describes the OpenGL specific properties of the output thread.
112  *****************************************************************************/
113 struct vout_sys_t
114 {
115     vout_thread_t * p_vout;
116
117     uint8_t    *pp_buffer[2]; /* one last rendered, one to be rendered */
118     int         i_index;
119     vlc_bool_t  b_frame_available;
120     
121     CGLContextObj glContext;
122
123     int         i_tex_width;
124     int         i_tex_height;
125     GLuint      p_textures[2];
126
127     NSAutoreleasePool *autorealease_pool;
128     VLCVoutLayer * o_layer;
129     id          o_cocoa_container;
130 };
131
132 /*****************************************************************************
133  * CreateVout: This function allocates and initializes the OpenGL vout method.
134  *****************************************************************************/
135 static int CreateVout( vlc_object_t *p_this )
136 {
137     vout_thread_t *p_vout = (vout_thread_t *)p_this;
138     vout_sys_t *p_sys;
139     char * psz;
140
141     /* Allocate structure */
142     p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
143     if( p_sys == NULL )
144     {
145         msg_Err( p_vout, "out of memory" );
146         return VLC_EGENERIC;
147     }
148
149     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
150     p_sys->i_tex_height = p_vout->fmt_in.i_height;
151
152     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
153              p_sys->i_tex_height );
154
155     p_vout->pf_init = Init;
156     p_vout->pf_end = End;
157     p_vout->pf_manage = Manage;
158     p_vout->pf_render = Render;
159     p_vout->pf_display = DisplayVideo;
160     p_vout->pf_control = Control;
161
162     return VLC_SUCCESS;
163 }
164
165 /*****************************************************************************
166  * Init: initialize the OpenGL video thread output method
167  *****************************************************************************/
168 static int Init( vout_thread_t *p_vout )
169 {
170     vout_sys_t *p_sys = p_vout->p_sys;
171     int i_pixel_pitch;
172     vlc_value_t val;
173
174 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
175     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
176     i_pixel_pitch = 2;
177 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
178     p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
179     i_pixel_pitch = 2;
180 #endif
181
182     /* Since OpenGL can do rescaling for us, stick to the default
183      * coordinates and aspect. */
184     p_vout->output.i_width  = p_vout->render.i_width;
185     p_vout->output.i_height = p_vout->render.i_height;
186     p_vout->output.i_aspect = p_vout->render.i_aspect;
187
188     /* We do need a drawable to work properly */
189     vlc_value_t value_drawable;
190     var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
191     var_Get( p_vout, "drawable", &value_drawable );
192
193     p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
194     
195     p_vout->fmt_out = p_vout->fmt_in;
196     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
197
198     /* We know the chroma, allocate two buffer which will be used
199      * directly by the decoder */
200     int i;
201     for( i = 0; i < 2; i++ )
202     {
203         p_sys->pp_buffer[i] =
204             malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
205         if( !p_sys->pp_buffer[i] )
206         {
207             msg_Err( p_vout, "out of memory" );
208             return VLC_EGENERIC;
209         }
210     }
211     p_sys->b_frame_available = VLC_FALSE;
212     p_sys->i_index = 0;
213
214     p_vout->p_picture[0].i_planes = 1;
215     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
216     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
217     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
218     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
219     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
220         p_vout->p_picture[0].p->i_pixel_pitch;
221     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
222         p_vout->p_picture[0].p->i_pixel_pitch;
223
224     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
225     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
226
227     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
228
229     I_OUTPUTPICTURES = 1;
230     p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
231
232     [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
233                              withObject:[NSValue valueWithPointer:p_vout]
234                              waitUntilDone:YES];
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     vout_sys_t *p_sys = p_vout->p_sys;
245
246     p_vout->p_sys->b_frame_available = VLC_FALSE;
247
248     [p_vout->p_sys->o_cocoa_container performSelectorOnMainThread:@selector(removeVoutLayer:) withObject:p_vout->p_sys->o_layer waitUntilDone:YES];
249
250     // Should be done automatically
251     [p_sys->o_layer release];
252     [p_sys->autorealease_pool release];
253
254     /* Free the texture buffer*/
255     free( p_sys->pp_buffer[0] );
256     free( p_sys->pp_buffer[1] );
257 }
258
259 /*****************************************************************************
260  * Destroy: destroy GLX video thread output method
261  *****************************************************************************
262  * Terminate an output method created by CreateVout
263  *****************************************************************************/
264 static void DestroyVout( vlc_object_t *p_this )
265 {
266     vout_thread_t *p_vout = (vout_thread_t *)p_this;
267     vout_sys_t *p_sys = p_vout->p_sys;
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 occurred.
276  *****************************************************************************/
277 static int Manage( vout_thread_t *p_vout )
278 {
279     vout_sys_t *p_sys = p_vout->p_sys;
280
281     return VLC_SUCCESS;
282 }
283
284 /*****************************************************************************
285  * Render: render previously calculated output
286  *****************************************************************************/
287 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
288 {
289     vout_sys_t *p_sys = p_vout->p_sys;
290
291     @synchronized( p_sys->o_layer ) /* Make sure the p_sys->glContext isn't edited */
292     {
293         if( p_sys->glContext )
294         {
295             CGLLockContext(p_sys->glContext);
296             CGLSetCurrentContext(p_sys->glContext);
297             int i_new_index;
298             i_new_index = ( p_sys->i_index + 1 ) & 1;
299
300
301             /* Update the texture */
302             glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
303             glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
304                          p_vout->fmt_out.i_width,
305                          p_vout->fmt_out.i_height,
306                          VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
307
308             /* Bind to the previous texture for drawing */
309             glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
310
311             /* Switch buffers */
312             p_sys->i_index = i_new_index;
313             p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
314             CGLUnlockContext(p_sys->glContext);
315             
316             p_sys->b_frame_available = VLC_TRUE;
317         }
318     }
319
320     /* Give a buffer where the image will be rendered */
321     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
322 }
323
324 /*****************************************************************************
325  * DisplayVideo: displays previously rendered output
326  *****************************************************************************/
327 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
328 {
329     vout_sys_t *p_sys = p_vout->p_sys;
330     
331     [p_sys->o_layer performSelectorOnMainThread:@selector(display)
332                     withObject:nil waitUntilDone:YES];
333 }
334
335 /*****************************************************************************
336  * Control: control facility for the vout
337  *****************************************************************************/
338 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
339 {
340     vout_sys_t *p_sys = p_vout->p_sys;
341
342     switch( i_query )
343     {
344     case VOUT_SNAPSHOT:
345         return vout_vaControlDefault( p_vout, i_query, args );
346
347     default:
348         if( p_sys->p_vout->pf_control )
349             return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
350         else
351             return vout_vaControlDefault( p_vout, i_query, args );
352     }
353 }
354
355 /*****************************************************************************
356  * InitTextures
357  *****************************************************************************/
358 static int InitTextures( vout_thread_t *p_vout )
359 {
360     vout_sys_t *p_sys = p_vout->p_sys;
361     int i_index;
362
363     glDeleteTextures( 2, p_sys->p_textures );
364     glGenTextures( 2, p_sys->p_textures );
365
366     for( i_index = 0; i_index < 2; i_index++ )
367     {
368         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
369
370         /* Set the texture parameters */
371         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
372
373         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
374         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
375
376         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
377         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
378
379         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
380
381         /* Note: It seems that we can't bypass those, and even
382          * disabled they are used. They are the cause of the flickering */
383
384         /* Tell the driver not to make a copy of the texture but to use
385            our buffer */
386         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
387         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
388
389         /* Use AGP texturing */
390         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
391
392         /* Call glTexImage2D only once, and use glTexSubImage2D later */
393         glTexImage2D( VLCGL_TARGET, 0, 4, p_sys->i_tex_width,
394                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
395                       p_sys->pp_buffer[i_index] );
396     }
397
398     return 0;
399 }
400
401 /*****************************************************************************
402  * @implementation VLCVoutLayer
403  */
404 @implementation VLCVoutLayer
405
406 /*****************************************************************************
407  * autoinitInVout: Called from the video thread to create a layer.
408  * The created layer is stored in the p_vout. We do that way because, cocoa
409  * doesn't support layer creation on non-main thread.
410  *****************************************************************************/
411 + (void)autoinitInVout:(NSValue*)arg
412 {
413     vout_thread_t * p_vout = [arg pointerValue];
414     p_vout->p_sys->o_layer = [[VLCVoutLayer layerWithVout:p_vout] retain];
415     [p_vout->p_sys->o_cocoa_container addVoutLayer:p_vout->p_sys->o_layer];
416 }
417
418 + (id)layerWithVout:(vout_thread_t*)_p_vout 
419 {
420     VLCVoutLayer* me = [[[self alloc] init] autorelease];
421     if( me )
422     {
423         me->p_vout = _p_vout;
424         me.asynchronous = NO;
425         me.bounds = CGRectMake( 0.0, 0.0, 
426                                 (float)_p_vout->fmt_in.i_visible_width * _p_vout->fmt_in.i_sar_num,
427                                 (float)_p_vout->fmt_in.i_visible_height * _p_vout->fmt_in.i_sar_den );
428     }
429     return me;
430 }
431
432 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
433 {
434     /* Only draw the frame if we have a frame that was previously rendered */
435         return p_vout->p_sys->b_frame_available; // Flag is cleared by drawInCGLContext:pixelFormat:forLayerTime:displayTime:
436 }
437
438 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
439 {
440     CGLLockContext( glContext );
441     CGLSetCurrentContext( glContext );
442
443     float f_width, f_height, f_x, f_y;
444
445     f_x = (float)p_vout->fmt_out.i_x_offset;
446     f_y = (float)p_vout->fmt_out.i_y_offset;
447     f_width = (float)p_vout->fmt_out.i_x_offset +
448               (float)p_vout->fmt_out.i_visible_width;
449     f_height = (float)p_vout->fmt_out.i_y_offset +
450                (float)p_vout->fmt_out.i_visible_height;
451
452     glClear( GL_COLOR_BUFFER_BIT );
453
454     glEnable( VLCGL_TARGET );
455     glBegin( GL_POLYGON );
456     glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
457     glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
458     glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
459     glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
460     glEnd();
461
462     glDisable( VLCGL_TARGET );
463
464     glFlush();
465
466     CGLUnlockContext( glContext );
467 }
468
469 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
470 {
471     CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
472
473     CGLLockContext( context );
474
475     CGLSetCurrentContext( context );
476
477     /* Swap buffers only during the vertical retrace of the monitor.
478     http://developer.apple.com/documentation/GraphicsImaging/
479     Conceptual/OpenGL/chap5/chapter_5_section_44.html */
480
481     GLint params = 1;
482     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
483                      &params );
484
485     InitTextures( p_vout );
486
487     glDisable( GL_BLEND );
488     glDisable( GL_DEPTH_TEST );
489     glDepthMask( GL_FALSE );
490     glDisable( GL_CULL_FACE) ;
491     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
492     glClear( GL_COLOR_BUFFER_BIT );
493
494     CGLUnlockContext( context );
495     @synchronized( self )
496     {
497         p_vout->p_sys->glContext = context;
498     }
499
500     return context;
501 }
502
503 - (void)releaseCGLContext:(CGLContextObj)glContext
504 {
505     @synchronized( self )
506     {
507         p_vout->p_sys->glContext = nil;
508     }
509
510     CGLLockContext( glContext );
511     CGLSetCurrentContext( glContext );
512
513     glDeleteTextures( 2, p_vout->p_sys->p_textures );
514
515     CGLUnlockContext( glContext );
516 }
517 @end