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
70 /*****************************************************************************
72 *****************************************************************************/
73 static int CreateVout ( vlc_object_t * );
74 static void DestroyVout ( vlc_object_t * );
75 static int Init ( vout_thread_t * );
76 static void End ( vout_thread_t * );
77 static int Manage ( vout_thread_t * );
78 static void Render ( vout_thread_t *, picture_t * );
79 static void DisplayVideo ( vout_thread_t *, picture_t * );
80 static int Control ( vout_thread_t *, int, va_list );
82 static int InitTextures ( vout_thread_t * );
85 set_shortname( "OpenGLLayer" );
86 set_category( CAT_VIDEO );
87 set_subcategory( SUBCAT_VIDEO_VOUT );
88 set_description( _("Core Animation OpenGL Layer (Mac OS X)") );
89 set_capability( "video output", 20 );
90 add_shortcut( "opengllayer" );
91 set_callbacks( CreateVout, DestroyVout );
94 @interface VLCVideoLayer : CAOpenGLLayer {
95 vout_thread_t * p_vout;
97 + (id)layerWithVout:(vout_thread_t*)_p_vout;
100 /*****************************************************************************
101 * vout_sys_t: video output method descriptor
102 *****************************************************************************
103 * This structure is part of the video output thread descriptor.
104 * It describes the OpenGL specific properties of the output thread.
105 *****************************************************************************/
108 vout_thread_t * p_vout;
110 uint8_t *pp_buffer[2]; /* one last rendered, one to be rendered */
112 vlc_bool_t b_frame_available;
116 GLuint p_textures[2];
118 NSAutoreleasePool *autorealease_pool;
119 VLCVideoLayer * o_layer;
120 id o_cocoa_container;
123 /*****************************************************************************
124 * CreateVout: This function allocates and initializes the OpenGL vout method.
125 *****************************************************************************/
126 static int CreateVout( vlc_object_t *p_this )
128 vout_thread_t *p_vout = (vout_thread_t *)p_this;
132 /* Allocate structure */
133 p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
136 msg_Err( p_vout, "out of memory" );
140 p_sys->i_tex_width = p_vout->fmt_in.i_width;
141 p_sys->i_tex_height = p_vout->fmt_in.i_height;
143 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
144 p_sys->i_tex_height );
147 p_vout->pf_init = Init;
148 p_vout->pf_end = End;
149 p_vout->pf_manage = Manage;
150 p_vout->pf_render = Render;
151 p_vout->pf_display = DisplayVideo;
152 p_vout->pf_control = Control;
157 /*****************************************************************************
158 * Init: initialize the OpenGL video thread output method
159 *****************************************************************************/
160 static int Init( vout_thread_t *p_vout )
162 vout_sys_t *p_sys = p_vout->p_sys;
166 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
167 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
169 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
170 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
174 /* Since OpenGL can do rescaling for us, stick to the default
175 * coordinates and aspect. */
176 p_vout->output.i_width = p_vout->render.i_width;
177 p_vout->output.i_height = p_vout->render.i_height;
178 p_vout->output.i_aspect = p_vout->render.i_aspect;
180 /* We do need a drawable to work properly */
181 vlc_value_t value_drawable;
182 var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
183 var_Get( p_vout, "drawable", &value_drawable );
185 p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
187 p_vout->fmt_out = p_vout->fmt_in;
188 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
190 /* We know the chroma, allocate two buffer which will be used
191 * directly by the decoder */
193 for( i = 0; i < 2; i++ )
195 p_sys->pp_buffer[i] =
196 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
197 if( !p_sys->pp_buffer[i] )
199 msg_Err( p_vout, "out of memory" );
203 p_sys->b_frame_available = VLC_FALSE;
206 p_vout->p_picture[0].i_planes = 1;
207 p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
208 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
209 p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
210 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
211 p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
212 p_vout->p_picture[0].p->i_pixel_pitch;
213 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
214 p_vout->p_picture[0].p->i_pixel_pitch;
216 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
217 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
219 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
221 I_OUTPUTPICTURES = 1;
222 p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
224 [VLCVideoLayer performSelectorOnMainThread:@selector(autoinitInVout:)
225 withObject:[NSValue valueWithPointer:p_vout]
231 /*****************************************************************************
232 * End: terminate GLX video thread output method
233 *****************************************************************************/
234 static void End( vout_thread_t *p_vout )
236 vout_sys_t *p_sys = p_vout->p_sys;
238 p_vout->p_sys->b_frame_available = 0;
240 [p_sys->o_layer performSelectorOnMainThread:@selector(removeFromSuperlayer)
241 withObject:nil waitUntilDone:YES];
242 [p_sys->o_layer release];
243 [p_sys->autorealease_pool release];
245 /* Free the texture buffer*/
246 glDeleteTextures( 2, p_sys->p_textures );
247 if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
248 if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
251 /*****************************************************************************
252 * Destroy: destroy GLX video thread output method
253 *****************************************************************************
254 * Terminate an output method created by CreateVout
255 *****************************************************************************/
256 static void DestroyVout( vlc_object_t *p_this )
258 vout_thread_t *p_vout = (vout_thread_t *)p_this;
259 vout_sys_t *p_sys = p_vout->p_sys;
263 /*****************************************************************************
264 * Manage: handle Sys events
265 *****************************************************************************
266 * This function should be called regularly by video output thread. It returns
267 * a non null value if an error occurred.
268 *****************************************************************************/
269 static int Manage( vout_thread_t *p_vout )
274 /*****************************************************************************
275 * Render: render previously calculated output
276 *****************************************************************************/
277 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
279 vout_sys_t *p_sys = p_vout->p_sys;
283 @synchronized(p_sys->o_layer)
285 p_new_index = (p_sys->i_index + 1) & 1;
288 /* Give a buffer where the image will be rendered */
289 p_pic->p->p_pixels = p_sys->pp_buffer[p_new_index];
293 /*****************************************************************************
294 * DisplayVideo: displays previously rendered output
295 *****************************************************************************/
296 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
298 vout_sys_t *p_sys = p_vout->p_sys;
300 /* The frame is ready, give its number so the o_layer can display it */
301 @synchronized(p_sys->o_layer)
303 p_sys->i_index = (p_sys->i_index + 1) & 1; /* Indicate the layer should use that index */
306 p_sys->b_frame_available = 1;
309 /*****************************************************************************
310 * Control: control facility for the vout
311 *****************************************************************************/
312 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
314 vout_sys_t *p_sys = p_vout->p_sys;
319 return vout_vaControlDefault( p_vout, i_query, args );
322 if( p_sys->p_vout->pf_control )
323 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
325 return vout_vaControlDefault( p_vout, i_query, args );
329 /*****************************************************************************
331 *****************************************************************************/
332 static int InitTextures( vout_thread_t *p_vout )
334 vout_sys_t *p_sys = p_vout->p_sys;
337 glDeleteTextures( 2, p_sys->p_textures );
338 glGenTextures( 2, p_sys->p_textures );
340 for( i_index = 0; i_index < 2; i_index++ )
342 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
344 /* Set the texture parameters */
345 glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
347 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
348 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
350 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
351 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
353 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
355 #ifdef __DISABLED_FOR_NOW__
356 /* Tell the driver not to make a copy of the texture but to use
358 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
359 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
361 /* Use AGP texturing */
362 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
363 GL_STORAGE_SHARED_APPLE );
365 /* Call glTexImage2D only once, and use glTexSubImage2D later */
366 glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
367 p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
368 p_sys->pp_buffer[i_index] );
374 /*****************************************************************************
375 * @implementation VLCVideoLayer
377 @implementation VLCVideoLayer
379 /*****************************************************************************
380 * autoinitInVout: Called from the video thread to create a layer.
381 * The created layer is stored in the p_vout. We do that way because, cocoa
382 * doesn't support layer creation on non-main thread.
383 *****************************************************************************/
384 + (void)autoinitInVout:(NSValue*)arg
386 vout_thread_t * p_vout = [arg pointerValue];
387 p_vout->p_sys->o_layer = [[VLCVideoLayer layerWithVout: p_vout] retain];
388 [p_vout->p_sys->o_cocoa_container addVoutSubview:p_vout->p_sys->o_layer];
391 - (void)setVout:(vout_thread_t*)_p_vout
396 + (id)layerWithVout:(vout_thread_t*)_p_vout
398 VLCVideoLayer* me = [super layer];
401 me.asynchronous = YES;
402 [me setVout: _p_vout];
407 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
409 /* Only draw the frame when if have a frame that was previously rendered */
410 return p_vout->p_sys->b_frame_available;
413 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
416 CGLLockContext( glContext );
417 CGLSetCurrentContext( glContext );
419 float f_width, f_height, f_x, f_y;
421 f_x = (float)p_vout->fmt_out.i_x_offset;
422 f_y = (float)p_vout->fmt_out.i_y_offset;
423 f_width = (float)p_vout->fmt_out.i_x_offset +
424 (float)p_vout->fmt_out.i_visible_width;
425 f_height = (float)p_vout->fmt_out.i_y_offset +
426 (float)p_vout->fmt_out.i_visible_height;
430 glBindTexture( VLCGL_TARGET, p_vout->p_sys->p_textures[p_vout->p_sys->i_index] );
432 glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
433 p_vout->fmt_out.i_width,
434 p_vout->fmt_out.i_height,
435 VLCGL_FORMAT, VLCGL_TYPE, p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index] );
439 glClear( GL_COLOR_BUFFER_BIT );
441 glEnable( VLCGL_TARGET );
442 glBegin( GL_POLYGON );
443 glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
444 glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
445 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
446 glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
449 glDisable( VLCGL_TARGET );
452 CGLUnlockContext( glContext );
455 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
459 NSOpenGLPFANoRecovery,
461 NSOpenGLPFAAccelerated,
462 NSOpenGLPFADoubleBuffer,
463 NSOpenGLPFAColorSize, 24,
464 NSOpenGLPFAAlphaSize, 8,
465 NSOpenGLPFADepthSize, 24,
466 NSOpenGLPFAStencilSize, 8,
467 NSOpenGLPFAAccumSize, 0,
471 NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: (NSOpenGLPixelFormatAttribute*) attribs];
474 return [fmt CGLPixelFormatObj];
477 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
479 CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
481 CGLLockContext( context );
482 CGLSetCurrentContext( context );
483 InitTextures( p_vout );
486 glDisable(GL_DEPTH_TEST);
487 glDepthMask(GL_FALSE);
488 glDisable(GL_CULL_FACE);
489 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
490 glClear( GL_COLOR_BUFFER_BIT );
492 CGLUnlockContext( context );