opengllayer.m
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:18k
源码类别:

midi

开发平台:

Unix_Linux

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