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 */
40 #include <vlc_plugin.h>
43 #import <QuartzCore/QuartzCore.h>
44 #import <Cocoa/Cocoa.h>
45 #import <OpenGL/OpenGL.h>
49 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
50 This allows sizes which are not powers of 2 */
51 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
53 /* OS X OpenGL supports YUV. Hehe. */
54 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
55 #define VLCGL_TYPE GL_UNSIGNED_SHORT_8_8_APPLE
58 #define VLCGL_RGB_FORMAT GL_RGBA
59 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
63 #define YCBCR_MESA 0x8757
65 #ifndef UNSIGNED_SHORT_8_8_MESA
66 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
68 #define VLCGL_YUV_FORMAT YCBCR_MESA
69 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
72 #ifndef GL_CLAMP_TO_EDGE
73 # define GL_CLAMP_TO_EDGE 0x812F
76 @interface VLCVideoView : NSObject
77 - (void)addVoutLayer:(CALayer *)layer;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int CreateVout ( vlc_object_t * );
84 static void DestroyVout ( vlc_object_t * );
85 static int Init ( vout_thread_t * );
86 static void End ( vout_thread_t * );
87 static int Manage ( vout_thread_t * );
88 static void Render ( vout_thread_t *, picture_t * );
89 static void DisplayVideo ( vout_thread_t *, picture_t * );
90 static int Control ( vout_thread_t *, int, va_list );
92 static int InitTextures ( vout_thread_t * );
95 set_shortname( "OpenGLLayer" );
96 set_category( CAT_VIDEO );
97 set_subcategory( SUBCAT_VIDEO_VOUT );
98 set_description( _("Core Animation OpenGL Layer (Mac OS X)") );
99 set_capability( "video output", 20 );
100 add_shortcut( "opengllayer" );
101 set_callbacks( CreateVout, DestroyVout );
104 @interface VLCVoutLayer : CAOpenGLLayer {
105 vout_thread_t * p_vout;
107 + (id)layerWithVout:(vout_thread_t*)_p_vout;
110 /*****************************************************************************
111 * vout_sys_t: video output method descriptor
112 *****************************************************************************
113 * This structure is part of the video output thread descriptor.
114 * It describes the OpenGL specific properties of the output thread.
115 *****************************************************************************/
118 vout_thread_t * p_vout;
120 uint8_t *pp_buffer[2]; /* one last rendered, one to be rendered */
122 bool b_frame_available;
124 CGLContextObj glContext;
128 GLuint p_textures[2];
130 NSAutoreleasePool *autorealease_pool;
131 VLCVoutLayer * o_layer;
132 id o_cocoa_container;
135 /*****************************************************************************
136 * CreateVout: This function allocates and initializes the OpenGL vout method.
137 *****************************************************************************/
138 static int CreateVout( vlc_object_t *p_this )
140 vout_thread_t *p_vout = (vout_thread_t *)p_this;
144 /* Allocate structure */
145 p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
148 msg_Err( p_vout, "out of memory" );
152 p_sys->i_tex_width = p_vout->fmt_in.i_width;
153 p_sys->i_tex_height = p_vout->fmt_in.i_height;
155 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
156 p_sys->i_tex_height );
158 p_vout->pf_init = Init;
159 p_vout->pf_end = End;
160 p_vout->pf_manage = Manage;
161 p_vout->pf_render = Render;
162 p_vout->pf_display = DisplayVideo;
163 p_vout->pf_control = Control;
168 /*****************************************************************************
169 * Init: initialize the OpenGL video thread output method
170 *****************************************************************************/
171 static int Init( vout_thread_t *p_vout )
173 vout_sys_t *p_sys = p_vout->p_sys;
177 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
178 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
180 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
181 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
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;
191 /* We do need a drawable to work properly */
192 vlc_value_t value_drawable;
193 var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
194 var_Get( p_vout, "drawable", &value_drawable );
196 p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
198 p_vout->fmt_out = p_vout->fmt_in;
199 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
201 /* We know the chroma, allocate two buffer which will be used
202 * directly by the decoder */
204 for( i = 0; i < 2; i++ )
206 p_sys->pp_buffer[i] =
207 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
208 if( !p_sys->pp_buffer[i] )
210 msg_Err( p_vout, "out of memory" );
214 p_sys->b_frame_available = false;
217 p_vout->p_picture[0].i_planes = 1;
218 p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
219 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
220 p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
221 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
222 p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
223 p_vout->p_picture[0].p->i_pixel_pitch;
224 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
225 p_vout->p_picture[0].p->i_pixel_pitch;
227 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
228 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
230 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
232 I_OUTPUTPICTURES = 1;
233 p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
235 [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
236 withObject:[NSValue valueWithPointer:p_vout]
242 /*****************************************************************************
243 * End: terminate GLX video thread output method
244 *****************************************************************************/
245 static void End( vout_thread_t *p_vout )
247 vout_sys_t *p_sys = p_vout->p_sys;
249 p_vout->p_sys->b_frame_available = false;
251 [p_vout->p_sys->o_cocoa_container performSelectorOnMainThread:@selector(removeVoutLayer:) withObject:p_vout->p_sys->o_layer waitUntilDone:YES];
253 // Should be done automatically
254 [p_sys->o_layer release];
255 [p_sys->autorealease_pool release];
257 /* Free the texture buffer*/
258 free( p_sys->pp_buffer[0] );
259 free( p_sys->pp_buffer[1] );
262 /*****************************************************************************
263 * Destroy: destroy GLX video thread output method
264 *****************************************************************************
265 * Terminate an output method created by CreateVout
266 *****************************************************************************/
267 static void DestroyVout( vlc_object_t *p_this )
269 vout_thread_t *p_vout = (vout_thread_t *)p_this;
270 vout_sys_t *p_sys = p_vout->p_sys;
274 /*****************************************************************************
275 * Manage: handle Sys events
276 *****************************************************************************
277 * This function should be called regularly by video output thread. It returns
278 * a non null value if an error occurred.
279 *****************************************************************************/
280 static int Manage( vout_thread_t *p_vout )
282 vout_sys_t *p_sys = p_vout->p_sys;
287 /*****************************************************************************
288 * Render: render previously calculated output
289 *****************************************************************************/
290 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
292 vout_sys_t *p_sys = p_vout->p_sys;
294 @synchronized( p_sys->o_layer ) /* Make sure the p_sys->glContext isn't edited */
296 if( p_sys->glContext )
298 CGLLockContext(p_sys->glContext);
299 CGLSetCurrentContext(p_sys->glContext);
301 i_new_index = ( p_sys->i_index + 1 ) & 1;
304 /* Update the texture */
305 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
306 glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
307 p_vout->fmt_out.i_width,
308 p_vout->fmt_out.i_height,
309 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
311 /* Bind to the previous texture for drawing */
312 glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
315 p_sys->i_index = i_new_index;
316 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
317 CGLUnlockContext(p_sys->glContext);
319 p_sys->b_frame_available = true;
323 /* Give a buffer where the image will be rendered */
324 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
327 /*****************************************************************************
328 * DisplayVideo: displays previously rendered output
329 *****************************************************************************/
330 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
332 vout_sys_t *p_sys = p_vout->p_sys;
334 [p_sys->o_layer performSelectorOnMainThread:@selector(display)
335 withObject:nil waitUntilDone:YES];
338 /*****************************************************************************
339 * Control: control facility for the vout
340 *****************************************************************************/
341 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
343 vout_sys_t *p_sys = p_vout->p_sys;
348 return vout_vaControlDefault( p_vout, i_query, args );
351 if( p_sys->p_vout->pf_control )
352 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
354 return vout_vaControlDefault( p_vout, i_query, args );
358 /*****************************************************************************
360 *****************************************************************************/
361 static int InitTextures( vout_thread_t *p_vout )
363 vout_sys_t *p_sys = p_vout->p_sys;
366 glDeleteTextures( 2, p_sys->p_textures );
367 glGenTextures( 2, p_sys->p_textures );
369 for( i_index = 0; i_index < 2; i_index++ )
371 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
373 /* Set the texture parameters */
374 glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
376 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
377 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
379 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
380 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
382 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
384 /* Note: It seems that we can't bypass those, and even
385 * disabled they are used. They are the cause of the flickering */
387 /* Tell the driver not to make a copy of the texture but to use
389 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
390 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
392 /* Use AGP texturing */
393 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
395 /* Call glTexImage2D only once, and use glTexSubImage2D later */
396 glTexImage2D( VLCGL_TARGET, 0, 4, p_sys->i_tex_width,
397 p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
398 p_sys->pp_buffer[i_index] );
404 /*****************************************************************************
405 * @implementation VLCVoutLayer
407 @implementation VLCVoutLayer
409 /*****************************************************************************
410 * autoinitInVout: Called from the video thread to create a layer.
411 * The created layer is stored in the p_vout. We do that way because, cocoa
412 * doesn't support layer creation on non-main thread.
413 *****************************************************************************/
414 + (void)autoinitInVout:(NSValue*)arg
416 vout_thread_t * p_vout = [arg pointerValue];
417 p_vout->p_sys->o_layer = [[VLCVoutLayer layerWithVout:p_vout] retain];
418 [p_vout->p_sys->o_cocoa_container addVoutLayer:p_vout->p_sys->o_layer];
421 + (id)layerWithVout:(vout_thread_t*)_p_vout
423 VLCVoutLayer* me = [[[self alloc] init] autorelease];
426 me->p_vout = _p_vout;
427 me.asynchronous = NO;
428 me.bounds = CGRectMake( 0.0, 0.0,
429 (float)_p_vout->fmt_in.i_visible_width * _p_vout->fmt_in.i_sar_num,
430 (float)_p_vout->fmt_in.i_visible_height * _p_vout->fmt_in.i_sar_den );
435 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
437 /* Only draw the frame if we have a frame that was previously rendered */
438 return p_vout->p_sys->b_frame_available; // Flag is cleared by drawInCGLContext:pixelFormat:forLayerTime:displayTime:
441 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
443 CGLLockContext( glContext );
444 CGLSetCurrentContext( glContext );
446 float f_width, f_height, f_x, f_y;
448 f_x = (float)p_vout->fmt_out.i_x_offset;
449 f_y = (float)p_vout->fmt_out.i_y_offset;
450 f_width = (float)p_vout->fmt_out.i_x_offset +
451 (float)p_vout->fmt_out.i_visible_width;
452 f_height = (float)p_vout->fmt_out.i_y_offset +
453 (float)p_vout->fmt_out.i_visible_height;
455 glClear( GL_COLOR_BUFFER_BIT );
457 glEnable( VLCGL_TARGET );
458 glBegin( GL_POLYGON );
459 glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
460 glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
461 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
462 glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
465 glDisable( VLCGL_TARGET );
469 CGLUnlockContext( glContext );
472 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
474 CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
476 CGLLockContext( context );
478 CGLSetCurrentContext( context );
480 /* Swap buffers only during the vertical retrace of the monitor.
481 http://developer.apple.com/documentation/GraphicsImaging/
482 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
485 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
488 InitTextures( p_vout );
490 glDisable( GL_BLEND );
491 glDisable( GL_DEPTH_TEST );
492 glDepthMask( GL_FALSE );
493 glDisable( GL_CULL_FACE) ;
494 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
495 glClear( GL_COLOR_BUFFER_BIT );
497 CGLUnlockContext( context );
498 @synchronized( self )
500 p_vout->p_sys->glContext = context;
506 - (void)releaseCGLContext:(CGLContextObj)glContext
508 @synchronized( self )
510 p_vout->p_sys->glContext = nil;
513 CGLLockContext( glContext );
514 CGLSetCurrentContext( glContext );
516 glDeleteTextures( 2, p_vout->p_sys->p_textures );
518 CGLUnlockContext( glContext );