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
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>
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.
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.
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 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
38 #import <QuartzCore/QuartzCore.h>
39 #import <Cocoa/Cocoa.h>
40 #import <OpenGL/OpenGL.h>
42 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
43 This allows sizes which are not powers of 2 */
44 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
46 /* OS X OpenGL supports YUV. Hehe. */
47 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
48 #define VLCGL_TYPE GL_UNSIGNED_SHORT_8_8_APPLE
51 #define VLCGL_RGB_FORMAT GL_RGBA
52 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
56 #define YCBCR_MESA 0x8757
58 #ifndef UNSIGNED_SHORT_8_8_MESA
59 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
61 #define VLCGL_YUV_FORMAT YCBCR_MESA
62 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
65 #ifndef GL_CLAMP_TO_EDGE
66 # define GL_CLAMP_TO_EDGE 0x812F
69 @interface VLCVideoView : NSObject
70 - (void)addVoutLayer:(CALayer *)layer;
73 /*****************************************************************************
75 *****************************************************************************/
76 static int CreateVout ( vlc_object_t * );
77 static void DestroyVout ( vlc_object_t * );
78 static int Init ( vout_thread_t * );
79 static void End ( vout_thread_t * );
80 static int Manage ( vout_thread_t * );
81 static void Render ( vout_thread_t *, picture_t * );
82 static void DisplayVideo ( vout_thread_t *, picture_t * );
83 static int Control ( vout_thread_t *, int, va_list );
85 static int InitTextures ( vout_thread_t * );
88 set_shortname( "OpenGLLayer" );
89 set_category( CAT_VIDEO );
90 set_subcategory( SUBCAT_VIDEO_VOUT );
91 set_description( _("Core Animation OpenGL Layer (Mac OS X)") );
92 set_capability( "video output", 20 );
93 add_shortcut( "opengllayer" );
94 set_callbacks( CreateVout, DestroyVout );
97 @interface VLCVoutLayer : CAOpenGLLayer {
98 vout_thread_t * p_vout;
100 + (id)layerWithVout:(vout_thread_t*)_p_vout;
103 /*****************************************************************************
104 * vout_sys_t: video output method descriptor
105 *****************************************************************************
106 * This structure is part of the video output thread descriptor.
107 * It describes the OpenGL specific properties of the output thread.
108 *****************************************************************************/
111 vout_thread_t * p_vout;
113 uint8_t *pp_buffer[2]; /* one last rendered, one to be rendered */
115 vlc_bool_t b_frame_available;
117 CGLContextObj glContext;
121 GLuint p_textures[2];
123 NSAutoreleasePool *autorealease_pool;
124 VLCVoutLayer * o_layer;
125 id o_cocoa_container;
128 /*****************************************************************************
129 * CreateVout: This function allocates and initializes the OpenGL vout method.
130 *****************************************************************************/
131 static int CreateVout( vlc_object_t *p_this )
133 vout_thread_t *p_vout = (vout_thread_t *)p_this;
137 /* Allocate structure */
138 p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
141 msg_Err( p_vout, "out of memory" );
145 p_sys->i_tex_width = p_vout->fmt_in.i_width;
146 p_sys->i_tex_height = p_vout->fmt_in.i_height;
148 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
149 p_sys->i_tex_height );
151 p_vout->pf_init = Init;
152 p_vout->pf_end = End;
153 p_vout->pf_manage = Manage;
154 p_vout->pf_render = Render;
155 p_vout->pf_display = DisplayVideo;
156 p_vout->pf_control = Control;
161 /*****************************************************************************
162 * Init: initialize the OpenGL video thread output method
163 *****************************************************************************/
164 static int Init( vout_thread_t *p_vout )
166 vout_sys_t *p_sys = p_vout->p_sys;
170 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
171 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
173 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
174 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
178 /* Since OpenGL can do rescaling for us, stick to the default
179 * coordinates and aspect. */
180 p_vout->output.i_width = p_vout->render.i_width;
181 p_vout->output.i_height = p_vout->render.i_height;
182 p_vout->output.i_aspect = p_vout->render.i_aspect;
184 /* We do need a drawable to work properly */
185 vlc_value_t value_drawable;
186 var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
187 var_Get( p_vout, "drawable", &value_drawable );
189 p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
191 p_vout->fmt_out = p_vout->fmt_in;
192 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
194 /* We know the chroma, allocate two buffer which will be used
195 * directly by the decoder */
197 for( i = 0; i < 2; i++ )
199 p_sys->pp_buffer[i] =
200 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
201 if( !p_sys->pp_buffer[i] )
203 msg_Err( p_vout, "out of memory" );
207 p_sys->b_frame_available = VLC_FALSE;
210 p_vout->p_picture[0].i_planes = 1;
211 p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
212 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
213 p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
214 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
215 p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
216 p_vout->p_picture[0].p->i_pixel_pitch;
217 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
218 p_vout->p_picture[0].p->i_pixel_pitch;
220 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
221 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
223 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
225 I_OUTPUTPICTURES = 1;
226 p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
228 [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
229 withObject:[NSValue valueWithPointer:p_vout]
235 /*****************************************************************************
236 * End: terminate GLX video thread output method
237 *****************************************************************************/
238 static void End( vout_thread_t *p_vout )
240 vout_sys_t *p_sys = p_vout->p_sys;
242 p_vout->p_sys->b_frame_available = VLC_FALSE;
244 [p_vout->p_sys->o_cocoa_container performSelectorOnMainThread:@selector(removeVoutLayer:) withObject:p_vout->p_sys->o_layer waitUntilDone:YES];
246 // Should be done automatically
247 [p_sys->o_layer release];
248 [p_sys->autorealease_pool release];
250 /* Free the texture buffer*/
251 free( p_sys->pp_buffer[0] );
252 free( p_sys->pp_buffer[1] );
255 /*****************************************************************************
256 * Destroy: destroy GLX video thread output method
257 *****************************************************************************
258 * Terminate an output method created by CreateVout
259 *****************************************************************************/
260 static void DestroyVout( vlc_object_t *p_this )
262 vout_thread_t *p_vout = (vout_thread_t *)p_this;
263 vout_sys_t *p_sys = p_vout->p_sys;
267 /*****************************************************************************
268 * Manage: handle Sys events
269 *****************************************************************************
270 * This function should be called regularly by video output thread. It returns
271 * a non null value if an error occurred.
272 *****************************************************************************/
273 static int Manage( vout_thread_t *p_vout )
275 vout_sys_t *p_sys = p_vout->p_sys;
280 /*****************************************************************************
281 * Render: render previously calculated output
282 *****************************************************************************/
283 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
285 vout_sys_t *p_sys = p_vout->p_sys;
287 @synchronized( p_sys->o_layer ) /* Make sure the p_sys->glContext isn't edited */
289 if( p_sys->glContext )
291 CGLLockContext(p_sys->glContext);
292 CGLSetCurrentContext(p_sys->glContext);
294 i_new_index = ( p_sys->i_index + 1 ) & 1;
297 /* Update the texture */
298 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
299 glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
300 p_vout->fmt_out.i_width,
301 p_vout->fmt_out.i_height,
302 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
304 /* Bind to the previous texture for drawing */
305 glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
308 p_sys->i_index = i_new_index;
309 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
310 CGLUnlockContext(p_sys->glContext);
312 p_sys->b_frame_available = VLC_TRUE;
316 /* Give a buffer where the image will be rendered */
317 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
320 /*****************************************************************************
321 * DisplayVideo: displays previously rendered output
322 *****************************************************************************/
323 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
325 vout_sys_t *p_sys = p_vout->p_sys;
327 [p_sys->o_layer performSelectorOnMainThread:@selector(display)
328 withObject:nil waitUntilDone:YES];
331 /*****************************************************************************
332 * Control: control facility for the vout
333 *****************************************************************************/
334 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
336 vout_sys_t *p_sys = p_vout->p_sys;
341 return vout_vaControlDefault( p_vout, i_query, args );
344 if( p_sys->p_vout->pf_control )
345 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
347 return vout_vaControlDefault( p_vout, i_query, args );
351 /*****************************************************************************
353 *****************************************************************************/
354 static int InitTextures( vout_thread_t *p_vout )
356 vout_sys_t *p_sys = p_vout->p_sys;
359 glDeleteTextures( 2, p_sys->p_textures );
360 glGenTextures( 2, p_sys->p_textures );
362 for( i_index = 0; i_index < 2; i_index++ )
364 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
366 /* Set the texture parameters */
367 glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
369 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
370 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
372 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
373 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
375 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
377 /* Note: It seems that we can't bypass those, and even
378 * disabled they are used. They are the cause of the flickering */
380 /* Tell the driver not to make a copy of the texture but to use
382 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
383 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
385 /* Use AGP texturing */
386 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
388 /* Call glTexImage2D only once, and use glTexSubImage2D later */
389 glTexImage2D( VLCGL_TARGET, 0, 4, p_sys->i_tex_width,
390 p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
391 p_sys->pp_buffer[i_index] );
397 /*****************************************************************************
398 * @implementation VLCVoutLayer
400 @implementation VLCVoutLayer
402 /*****************************************************************************
403 * autoinitInVout: Called from the video thread to create a layer.
404 * The created layer is stored in the p_vout. We do that way because, cocoa
405 * doesn't support layer creation on non-main thread.
406 *****************************************************************************/
407 + (void)autoinitInVout:(NSValue*)arg
409 vout_thread_t * p_vout = [arg pointerValue];
410 p_vout->p_sys->o_layer = [[VLCVoutLayer layerWithVout:p_vout] retain];
411 [p_vout->p_sys->o_cocoa_container addVoutLayer:p_vout->p_sys->o_layer];
414 + (id)layerWithVout:(vout_thread_t*)_p_vout
416 VLCVoutLayer* me = [[[self alloc] init] autorelease];
419 me->p_vout = _p_vout;
420 me.asynchronous = NO;
421 me.bounds = CGRectMake( 0.0, 0.0,
422 (float)_p_vout->fmt_in.i_visible_width * _p_vout->fmt_in.i_sar_num,
423 (float)_p_vout->fmt_in.i_visible_height * _p_vout->fmt_in.i_sar_den );
428 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
430 /* Only draw the frame if we have a frame that was previously rendered */
431 return p_vout->p_sys->b_frame_available; // Flag is cleared by drawInCGLContext:pixelFormat:forLayerTime:displayTime:
434 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
436 CGLLockContext( glContext );
437 CGLSetCurrentContext( glContext );
439 float f_width, f_height, f_x, f_y;
441 f_x = (float)p_vout->fmt_out.i_x_offset;
442 f_y = (float)p_vout->fmt_out.i_y_offset;
443 f_width = (float)p_vout->fmt_out.i_x_offset +
444 (float)p_vout->fmt_out.i_visible_width;
445 f_height = (float)p_vout->fmt_out.i_y_offset +
446 (float)p_vout->fmt_out.i_visible_height;
448 glClear( GL_COLOR_BUFFER_BIT );
450 glEnable( VLCGL_TARGET );
451 glBegin( GL_POLYGON );
452 glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
453 glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
454 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
455 glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
458 glDisable( VLCGL_TARGET );
462 CGLUnlockContext( glContext );
465 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
467 CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
469 CGLLockContext( context );
471 CGLSetCurrentContext( context );
473 /* Swap buffers only during the vertical retrace of the monitor.
474 http://developer.apple.com/documentation/GraphicsImaging/
475 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
478 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
481 InitTextures( p_vout );
483 glDisable( GL_BLEND );
484 glDisable( GL_DEPTH_TEST );
485 glDepthMask( GL_FALSE );
486 glDisable( GL_CULL_FACE) ;
487 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
488 glClear( GL_COLOR_BUFFER_BIT );
490 CGLUnlockContext( context );
491 @synchronized( self )
493 p_vout->p_sys->glContext = context;
499 - (void)releaseCGLContext:(CGLContextObj)glContext
501 @synchronized( self )
503 p_vout->p_sys->glContext = nil;
506 CGLLockContext( glContext );
507 CGLSetCurrentContext( glContext );
509 glDeleteTextures( 2, p_vout->p_sys->p_textures );
511 CGLUnlockContext( glContext );