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 */
42 #import <QuartzCore/QuartzCore.h>
43 #import <Cocoa/Cocoa.h>
44 #import <OpenGL/OpenGL.h>
48 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
49 This allows sizes which are not powers of 2 */
50 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
52 /* OS X OpenGL supports YUV. Hehe. */
53 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
54 #define VLCGL_TYPE GL_UNSIGNED_SHORT_8_8_APPLE
57 #define VLCGL_RGB_FORMAT GL_RGBA
58 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
62 #define YCBCR_MESA 0x8757
64 #ifndef UNSIGNED_SHORT_8_8_MESA
65 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
67 #define VLCGL_YUV_FORMAT YCBCR_MESA
68 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
71 #ifndef GL_CLAMP_TO_EDGE
72 # define GL_CLAMP_TO_EDGE 0x812F
75 @interface VLCVideoView : NSObject
76 - (void)addVoutLayer:(CALayer *)layer;
79 /*****************************************************************************
81 *****************************************************************************/
82 static int CreateVout ( vlc_object_t * );
83 static void DestroyVout ( vlc_object_t * );
84 static int Init ( vout_thread_t * );
85 static void End ( vout_thread_t * );
86 static int Manage ( vout_thread_t * );
87 static void Render ( vout_thread_t *, picture_t * );
88 static void DisplayVideo ( vout_thread_t *, picture_t * );
89 static int Control ( vout_thread_t *, int, va_list );
91 static int InitTextures ( vout_thread_t * );
94 set_shortname( "OpenGLLayer" );
95 set_category( CAT_VIDEO );
96 set_subcategory( SUBCAT_VIDEO_VOUT );
97 set_description( _("Core Animation OpenGL Layer (Mac OS X)") );
98 set_capability( "video output", 20 );
99 add_shortcut( "opengllayer" );
100 set_callbacks( CreateVout, DestroyVout );
103 @interface VLCVoutLayer : CAOpenGLLayer {
104 vout_thread_t * p_vout;
106 + (id)layerWithVout:(vout_thread_t*)_p_vout;
109 /*****************************************************************************
110 * vout_sys_t: video output method descriptor
111 *****************************************************************************
112 * This structure is part of the video output thread descriptor.
113 * It describes the OpenGL specific properties of the output thread.
114 *****************************************************************************/
117 vout_thread_t * p_vout;
119 uint8_t *pp_buffer[2]; /* one last rendered, one to be rendered */
121 bool b_frame_available;
123 CGLContextObj glContext;
127 GLuint p_textures[2];
129 NSAutoreleasePool *autorealease_pool;
130 VLCVoutLayer * o_layer;
131 id o_cocoa_container;
134 /*****************************************************************************
135 * CreateVout: This function allocates and initializes the OpenGL vout method.
136 *****************************************************************************/
137 static int CreateVout( vlc_object_t *p_this )
139 vout_thread_t *p_vout = (vout_thread_t *)p_this;
143 /* Allocate structure */
144 p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
147 msg_Err( p_vout, "out of memory" );
151 p_sys->i_tex_width = p_vout->fmt_in.i_width;
152 p_sys->i_tex_height = p_vout->fmt_in.i_height;
154 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
155 p_sys->i_tex_height );
157 p_vout->pf_init = Init;
158 p_vout->pf_end = End;
159 p_vout->pf_manage = Manage;
160 p_vout->pf_render = Render;
161 p_vout->pf_display = DisplayVideo;
162 p_vout->pf_control = Control;
167 /*****************************************************************************
168 * Init: initialize the OpenGL video thread output method
169 *****************************************************************************/
170 static int Init( vout_thread_t *p_vout )
172 vout_sys_t *p_sys = p_vout->p_sys;
176 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
177 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
179 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
180 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
184 /* Since OpenGL can do rescaling for us, stick to the default
185 * coordinates and aspect. */
186 p_vout->output.i_width = p_vout->render.i_width;
187 p_vout->output.i_height = p_vout->render.i_height;
188 p_vout->output.i_aspect = p_vout->render.i_aspect;
190 /* We do need a drawable to work properly */
191 vlc_value_t value_drawable;
192 var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
193 var_Get( p_vout, "drawable", &value_drawable );
195 p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
197 p_vout->fmt_out = p_vout->fmt_in;
198 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
200 /* We know the chroma, allocate two buffer which will be used
201 * directly by the decoder */
203 for( i = 0; i < 2; i++ )
205 p_sys->pp_buffer[i] =
206 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
207 if( !p_sys->pp_buffer[i] )
209 msg_Err( p_vout, "out of memory" );
213 p_sys->b_frame_available = false;
216 p_vout->p_picture[0].i_planes = 1;
217 p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
218 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
219 p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
220 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
221 p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
222 p_vout->p_picture[0].p->i_pixel_pitch;
223 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
224 p_vout->p_picture[0].p->i_pixel_pitch;
226 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
227 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
229 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
231 I_OUTPUTPICTURES = 1;
232 p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
234 [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
235 withObject:[NSValue valueWithPointer:p_vout]
241 /*****************************************************************************
242 * End: terminate GLX video thread output method
243 *****************************************************************************/
244 static void End( vout_thread_t *p_vout )
246 vout_sys_t *p_sys = p_vout->p_sys;
248 p_vout->p_sys->b_frame_available = false;
250 [p_vout->p_sys->o_cocoa_container performSelectorOnMainThread:@selector(removeVoutLayer:) withObject:p_vout->p_sys->o_layer waitUntilDone:YES];
252 // Should be done automatically
253 [p_sys->o_layer release];
254 [p_sys->autorealease_pool release];
256 /* Free the texture buffer*/
257 free( p_sys->pp_buffer[0] );
258 free( p_sys->pp_buffer[1] );
261 /*****************************************************************************
262 * Destroy: destroy GLX video thread output method
263 *****************************************************************************
264 * Terminate an output method created by CreateVout
265 *****************************************************************************/
266 static void DestroyVout( vlc_object_t *p_this )
268 vout_thread_t *p_vout = (vout_thread_t *)p_this;
269 vout_sys_t *p_sys = p_vout->p_sys;
273 /*****************************************************************************
274 * Manage: handle Sys events
275 *****************************************************************************
276 * This function should be called regularly by video output thread. It returns
277 * a non null value if an error occurred.
278 *****************************************************************************/
279 static int Manage( vout_thread_t *p_vout )
281 vout_sys_t *p_sys = p_vout->p_sys;
286 /*****************************************************************************
287 * Render: render previously calculated output
288 *****************************************************************************/
289 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
291 vout_sys_t *p_sys = p_vout->p_sys;
293 @synchronized( p_sys->o_layer ) /* Make sure the p_sys->glContext isn't edited */
295 if( p_sys->glContext )
297 CGLLockContext(p_sys->glContext);
298 CGLSetCurrentContext(p_sys->glContext);
300 i_new_index = ( p_sys->i_index + 1 ) & 1;
303 /* Update the texture */
304 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
305 glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
306 p_vout->fmt_out.i_width,
307 p_vout->fmt_out.i_height,
308 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
310 /* Bind to the previous texture for drawing */
311 glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
314 p_sys->i_index = i_new_index;
315 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
316 CGLUnlockContext(p_sys->glContext);
318 p_sys->b_frame_available = true;
322 /* Give a buffer where the image will be rendered */
323 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
326 /*****************************************************************************
327 * DisplayVideo: displays previously rendered output
328 *****************************************************************************/
329 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
331 vout_sys_t *p_sys = p_vout->p_sys;
333 [p_sys->o_layer performSelectorOnMainThread:@selector(display)
334 withObject:nil waitUntilDone:YES];
337 /*****************************************************************************
338 * Control: control facility for the vout
339 *****************************************************************************/
340 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
342 vout_sys_t *p_sys = p_vout->p_sys;
347 return vout_vaControlDefault( p_vout, i_query, args );
350 if( p_sys->p_vout->pf_control )
351 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
353 return vout_vaControlDefault( p_vout, i_query, args );
357 /*****************************************************************************
359 *****************************************************************************/
360 static int InitTextures( vout_thread_t *p_vout )
362 vout_sys_t *p_sys = p_vout->p_sys;
365 glDeleteTextures( 2, p_sys->p_textures );
366 glGenTextures( 2, p_sys->p_textures );
368 for( i_index = 0; i_index < 2; i_index++ )
370 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
372 /* Set the texture parameters */
373 glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
375 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
376 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
378 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
379 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
381 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
383 /* Note: It seems that we can't bypass those, and even
384 * disabled they are used. They are the cause of the flickering */
386 /* Tell the driver not to make a copy of the texture but to use
388 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
389 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
391 /* Use AGP texturing */
392 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
394 /* Call glTexImage2D only once, and use glTexSubImage2D later */
395 glTexImage2D( VLCGL_TARGET, 0, 4, p_sys->i_tex_width,
396 p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
397 p_sys->pp_buffer[i_index] );
403 /*****************************************************************************
404 * @implementation VLCVoutLayer
406 @implementation VLCVoutLayer
408 /*****************************************************************************
409 * autoinitInVout: Called from the video thread to create a layer.
410 * The created layer is stored in the p_vout. We do that way because, cocoa
411 * doesn't support layer creation on non-main thread.
412 *****************************************************************************/
413 + (void)autoinitInVout:(NSValue*)arg
415 vout_thread_t * p_vout = [arg pointerValue];
416 p_vout->p_sys->o_layer = [[VLCVoutLayer layerWithVout:p_vout] retain];
417 [p_vout->p_sys->o_cocoa_container addVoutLayer:p_vout->p_sys->o_layer];
420 + (id)layerWithVout:(vout_thread_t*)_p_vout
422 VLCVoutLayer* me = [[[self alloc] init] autorelease];
425 me->p_vout = _p_vout;
426 me.asynchronous = NO;
427 me.bounds = CGRectMake( 0.0, 0.0,
428 (float)_p_vout->fmt_in.i_visible_width * _p_vout->fmt_in.i_sar_num,
429 (float)_p_vout->fmt_in.i_visible_height * _p_vout->fmt_in.i_sar_den );
434 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
436 /* Only draw the frame if we have a frame that was previously rendered */
437 return p_vout->p_sys->b_frame_available; // Flag is cleared by drawInCGLContext:pixelFormat:forLayerTime:displayTime:
440 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
442 CGLLockContext( glContext );
443 CGLSetCurrentContext( glContext );
445 float f_width, f_height, f_x, f_y;
447 f_x = (float)p_vout->fmt_out.i_x_offset;
448 f_y = (float)p_vout->fmt_out.i_y_offset;
449 f_width = (float)p_vout->fmt_out.i_x_offset +
450 (float)p_vout->fmt_out.i_visible_width;
451 f_height = (float)p_vout->fmt_out.i_y_offset +
452 (float)p_vout->fmt_out.i_visible_height;
454 glClear( GL_COLOR_BUFFER_BIT );
456 glEnable( VLCGL_TARGET );
457 glBegin( GL_POLYGON );
458 glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
459 glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
460 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
461 glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
464 glDisable( VLCGL_TARGET );
468 CGLUnlockContext( glContext );
471 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
473 CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
475 CGLLockContext( context );
477 CGLSetCurrentContext( context );
479 /* Swap buffers only during the vertical retrace of the monitor.
480 http://developer.apple.com/documentation/GraphicsImaging/
481 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
484 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
487 InitTextures( p_vout );
489 glDisable( GL_BLEND );
490 glDisable( GL_DEPTH_TEST );
491 glDepthMask( GL_FALSE );
492 glDisable( GL_CULL_FACE) ;
493 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
494 glClear( GL_COLOR_BUFFER_BIT );
496 CGLUnlockContext( context );
497 @synchronized( self )
499 p_vout->p_sys->glContext = context;
505 - (void)releaseCGLContext:(CGLContextObj)glContext
507 @synchronized( self )
509 p_vout->p_sys->glContext = nil;
512 CGLLockContext( glContext );
513 CGLSetCurrentContext( glContext );
515 glDeleteTextures( 2, p_vout->p_sys->p_textures );
517 CGLUnlockContext( glContext );