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