]> git.sesse.net Git - vlc/blob - modules/video_output/opengllayer.m
Use gettext_noop() consistently
[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/vlc.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 #ifdef CALayer
48
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
52
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
56
57 /* RV32 */
58 #define VLCGL_RGB_FORMAT GL_RGBA
59 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
60
61 /* YUY2 */
62 #ifndef YCBCR_MESA
63 #define YCBCR_MESA 0x8757
64 #endif
65 #ifndef UNSIGNED_SHORT_8_8_MESA
66 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
67 #endif
68 #define VLCGL_YUV_FORMAT YCBCR_MESA
69 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
70
71
72 #ifndef GL_CLAMP_TO_EDGE
73 #   define GL_CLAMP_TO_EDGE 0x812F
74 #endif
75
76 @interface VLCVideoView : NSObject
77 - (void)addVoutLayer:(CALayer *)layer;
78 @end
79
80 /*****************************************************************************
81  * Vout interface
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 );
91
92 static int InitTextures  ( vout_thread_t * );
93
94 vlc_module_begin();
95     set_shortname( "OpenGLLayer" );
96     set_category( CAT_VIDEO );
97     set_subcategory( SUBCAT_VIDEO_VOUT );
98     set_description( N_("Core Animation OpenGL Layer (Mac OS X)") );
99     set_capability( "video output", 20 );
100     add_shortcut( "opengllayer" );
101     set_callbacks( CreateVout, DestroyVout );
102 vlc_module_end();
103
104 @interface VLCVoutLayer : CAOpenGLLayer {
105     vout_thread_t * p_vout;
106 }
107 + (id)layerWithVout:(vout_thread_t*)_p_vout; 
108 @end
109
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  *****************************************************************************/
116 struct vout_sys_t
117 {
118     vout_thread_t * p_vout;
119
120     uint8_t    *pp_buffer[2]; /* one last rendered, one to be rendered */
121     int         i_index;
122     bool  b_frame_available;
123     
124     CGLContextObj glContext;
125
126     int         i_tex_width;
127     int         i_tex_height;
128     GLuint      p_textures[2];
129
130     NSAutoreleasePool *autorealease_pool;
131     VLCVoutLayer * o_layer;
132     id          o_cocoa_container;
133 };
134
135 /*****************************************************************************
136  * CreateVout: This function allocates and initializes the OpenGL vout method.
137  *****************************************************************************/
138 static int CreateVout( vlc_object_t *p_this )
139 {
140     vout_thread_t *p_vout = (vout_thread_t *)p_this;
141     vout_sys_t *p_sys;
142     char * psz;
143
144     /* Allocate structure */
145     p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
146     if( p_sys == NULL )
147     {
148         msg_Err( p_vout, "out of memory" );
149         return VLC_EGENERIC;
150     }
151
152     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
153     p_sys->i_tex_height = p_vout->fmt_in.i_height;
154
155     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
156              p_sys->i_tex_height );
157
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;
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * Init: initialize the OpenGL video thread output method
170  *****************************************************************************/
171 static int Init( vout_thread_t *p_vout )
172 {
173     vout_sys_t *p_sys = p_vout->p_sys;
174     int i_pixel_pitch;
175     vlc_value_t val;
176
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');
179     i_pixel_pitch = 2;
180 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
181     p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
182     i_pixel_pitch = 2;
183 #endif
184
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;
190
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 );
195
196     p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
197     
198     p_vout->fmt_out = p_vout->fmt_in;
199     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
200
201     /* We know the chroma, allocate two buffer which will be used
202      * directly by the decoder */
203     int i;
204     for( i = 0; i < 2; i++ )
205     {
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] )
209         {
210             msg_Err( p_vout, "out of memory" );
211             return VLC_EGENERIC;
212         }
213     }
214     p_sys->b_frame_available = false;
215     p_sys->i_index = 0;
216
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;
226
227     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
228     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
229
230     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
231
232     I_OUTPUTPICTURES = 1;
233     p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
234
235     [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
236                              withObject:[NSValue valueWithPointer:p_vout]
237                              waitUntilDone:YES];
238
239     return 0;
240 }
241
242 /*****************************************************************************
243  * End: terminate GLX video thread output method
244  *****************************************************************************/
245 static void End( vout_thread_t *p_vout )
246 {
247     vout_sys_t *p_sys = p_vout->p_sys;
248
249     p_vout->p_sys->b_frame_available = false;
250
251     [p_vout->p_sys->o_cocoa_container performSelectorOnMainThread:@selector(removeVoutLayer:) withObject:p_vout->p_sys->o_layer waitUntilDone:YES];
252
253     // Should be done automatically
254     [p_sys->o_layer release];
255     [p_sys->autorealease_pool release];
256
257     /* Free the texture buffer*/
258     free( p_sys->pp_buffer[0] );
259     free( p_sys->pp_buffer[1] );
260 }
261
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 )
268 {
269     vout_thread_t *p_vout = (vout_thread_t *)p_this;
270     vout_sys_t *p_sys = p_vout->p_sys;
271     free( p_sys );
272 }
273
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 )
281 {
282     vout_sys_t *p_sys = p_vout->p_sys;
283
284     return VLC_SUCCESS;
285 }
286
287 /*****************************************************************************
288  * Render: render previously calculated output
289  *****************************************************************************/
290 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
291 {
292     vout_sys_t *p_sys = p_vout->p_sys;
293
294     @synchronized( p_sys->o_layer ) /* Make sure the p_sys->glContext isn't edited */
295     {
296         if( p_sys->glContext )
297         {
298             CGLLockContext(p_sys->glContext);
299             CGLSetCurrentContext(p_sys->glContext);
300             int i_new_index;
301             i_new_index = ( p_sys->i_index + 1 ) & 1;
302
303
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] );
310
311             /* Bind to the previous texture for drawing */
312             glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
313
314             /* Switch buffers */
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);
318             
319             p_sys->b_frame_available = true;
320         }
321     }
322
323     /* Give a buffer where the image will be rendered */
324     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
325 }
326
327 /*****************************************************************************
328  * DisplayVideo: displays previously rendered output
329  *****************************************************************************/
330 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
331 {
332     vout_sys_t *p_sys = p_vout->p_sys;
333     
334     [p_sys->o_layer performSelectorOnMainThread:@selector(display)
335                     withObject:nil waitUntilDone:YES];
336 }
337
338 /*****************************************************************************
339  * Control: control facility for the vout
340  *****************************************************************************/
341 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
342 {
343     vout_sys_t *p_sys = p_vout->p_sys;
344
345     switch( i_query )
346     {
347     case VOUT_SNAPSHOT:
348         return vout_vaControlDefault( p_vout, i_query, args );
349
350     default:
351         if( p_sys->p_vout->pf_control )
352             return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
353         else
354             return vout_vaControlDefault( p_vout, i_query, args );
355     }
356 }
357
358 /*****************************************************************************
359  * InitTextures
360  *****************************************************************************/
361 static int InitTextures( vout_thread_t *p_vout )
362 {
363     vout_sys_t *p_sys = p_vout->p_sys;
364     int i_index;
365
366     glDeleteTextures( 2, p_sys->p_textures );
367     glGenTextures( 2, p_sys->p_textures );
368
369     for( i_index = 0; i_index < 2; i_index++ )
370     {
371         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
372
373         /* Set the texture parameters */
374         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
375
376         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
377         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
378
379         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
380         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
381
382         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
383
384         /* Note: It seems that we can't bypass those, and even
385          * disabled they are used. They are the cause of the flickering */
386
387         /* Tell the driver not to make a copy of the texture but to use
388            our buffer */
389         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
390         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
391
392         /* Use AGP texturing */
393         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
394
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] );
399     }
400
401     return 0;
402 }
403
404 /*****************************************************************************
405  * @implementation VLCVoutLayer
406  */
407 @implementation VLCVoutLayer
408
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
415 {
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];
419 }
420
421 + (id)layerWithVout:(vout_thread_t*)_p_vout 
422 {
423     VLCVoutLayer* me = [[[self alloc] init] autorelease];
424     if( me )
425     {
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 );
431     }
432     return me;
433 }
434
435 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
436 {
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:
439 }
440
441 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
442 {
443     CGLLockContext( glContext );
444     CGLSetCurrentContext( glContext );
445
446     float f_width, f_height, f_x, f_y;
447
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;
454
455     glClear( GL_COLOR_BUFFER_BIT );
456
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 );
463     glEnd();
464
465     glDisable( VLCGL_TARGET );
466
467     glFlush();
468
469     CGLUnlockContext( glContext );
470 }
471
472 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
473 {
474     CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
475
476     CGLLockContext( context );
477
478     CGLSetCurrentContext( context );
479
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 */
483
484     GLint params = 1;
485     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
486                      &params );
487
488     InitTextures( p_vout );
489
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 );
496
497     CGLUnlockContext( context );
498     @synchronized( self )
499     {
500         p_vout->p_sys->glContext = context;
501     }
502
503     return context;
504 }
505
506 - (void)releaseCGLContext:(CGLContextObj)glContext
507 {
508     @synchronized( self )
509     {
510         p_vout->p_sys->glContext = nil;
511     }
512
513     CGLLockContext( glContext );
514     CGLSetCurrentContext( glContext );
515
516     glDeleteTextures( 2, p_vout->p_sys->p_textures );
517
518     CGLUnlockContext( glContext );
519 }
520 @end
521
522 #endif