glide.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:11k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * glide.c : 3dfx Glide plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001 VideoLAN
  5.  * $Id: glide.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include <errno.h>                                                 /* ENOMEM */
  27. #include <stdlib.h>                                      /* malloc(), free() */
  28. #include <string.h>
  29. #include <vlc/vlc.h>
  30. #include <vlc/intf.h>
  31. #include <vlc/vout.h>
  32. #ifndef __linux__
  33. #   include <conio.h>                                         /* for glide ? */
  34. #endif
  35. #include <glide.h>
  36. #include <linutil.h>                            /* Glide kbhit() and getch() */
  37. #define GLIDE_WIDTH 800
  38. #define GLIDE_HEIGHT 600
  39. #define GLIDE_BITS_PER_PLANE 16
  40. #define GLIDE_BYTES_PER_PIXEL 2
  41. /*****************************************************************************
  42.  * Local prototypes
  43.  *****************************************************************************/
  44. static int  Create    ( vlc_object_t * );
  45. static void Destroy   ( vlc_object_t * );
  46. static int  Init      ( vout_thread_t * );
  47. static void End       ( vout_thread_t * );
  48. static int  Manage    ( vout_thread_t * );
  49. static void Display   ( vout_thread_t *, picture_t * );
  50. static int  OpenDisplay    ( vout_thread_t * );
  51. static void CloseDisplay   ( vout_thread_t * );
  52. /*****************************************************************************
  53.  * Module descriptor
  54.  *****************************************************************************/
  55. vlc_module_begin();
  56.     set_description( _("3dfx Glide video output") );
  57.     set_capability( "video output", 20 );
  58.     add_shortcut( "3dfx" );
  59.     set_callbacks( Create, Destroy );
  60. vlc_module_end();
  61. /*****************************************************************************
  62.  * vout_sys_t: Glide video output method descriptor
  63.  *****************************************************************************
  64.  * This structure is part of the video output thread descriptor.
  65.  * It describes the Glide specific properties of an output thread.
  66.  *****************************************************************************/
  67. struct vout_sys_t
  68. {
  69.     GrLfbInfo_t                 p_buffer_info;           /* back buffer info */
  70.     uint8_t * pp_buffer[2];
  71.     int i_index;
  72. };
  73. /*****************************************************************************
  74.  * Create: allocates Glide video thread output method
  75.  *****************************************************************************
  76.  * This function allocates and initializes a Glide vout method.
  77.  *****************************************************************************/
  78. static int Create( vlc_object_t *p_this )
  79. {
  80.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  81.     /* Allocate structure */
  82.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  83.     if( p_vout->p_sys == NULL )
  84.     {
  85.         msg_Err( p_vout, "out of memory" );
  86.         return( 1 );
  87.     }
  88.     /* Open and initialize device */
  89.     if( OpenDisplay( p_vout ) )
  90.     {
  91.         msg_Err( p_vout, "cannot open display" );
  92.         free( p_vout->p_sys );
  93.         return( 1 );
  94.     }
  95.     p_vout->pf_init = Init;
  96.     p_vout->pf_end = End;
  97.     p_vout->pf_manage = Manage;
  98.     p_vout->pf_render = NULL;
  99.     p_vout->pf_display = Display;
  100.     return( 0 );
  101. }
  102. /*****************************************************************************
  103.  * Init: initialize Glide video thread output method
  104.  *****************************************************************************/
  105. static int Init( vout_thread_t *p_vout )
  106. {
  107.     int i_index;
  108.     picture_t *p_pic;
  109.     /* FIXME: we don't set i_chroma !! */
  110.     p_vout->output.i_rmask = 0xf800;
  111.     p_vout->output.i_gmask = 0x07e0;
  112.     p_vout->output.i_bmask = 0x001f;
  113.     I_OUTPUTPICTURES = 0;
  114.     p_pic = NULL;
  115.     /* Find an empty picture slot */
  116.     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  117.     {
  118.         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  119.         {
  120.             p_pic = p_vout->p_picture + i_index;
  121.             break;
  122.         }
  123.     }
  124.     if( p_pic == NULL )
  125.     {
  126.         return -1;
  127.     }
  128.     /* We know the chroma, allocate a buffer which will be used
  129.      * directly by the decoder */
  130.     p_pic->i_planes = 1;
  131.     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index];
  132.     p_pic->p->i_lines = GLIDE_HEIGHT;
  133.     p_pic->p->i_visible_lines = GLIDE_HEIGHT;
  134.     p_pic->p->i_pitch = p_vout->p_sys->p_buffer_info.strideInBytes;
  135.                          /*1024 * GLIDE_BYTES_PER_PIXEL*/
  136.     p_pic->p->i_pixel_pitch = GLIDE_BYTES_PER_PIXEL;
  137.     p_pic->p->i_visible_pitch = GLIDE_WIDTH * GLIDE_BYTES_PER_PIXEL;
  138.     p_pic->i_status = DESTROYED_PICTURE;
  139.     p_pic->i_type   = DIRECT_PICTURE;
  140.     PP_OUTPUTPICTURE[ 0 ] = p_pic;
  141.     I_OUTPUTPICTURES = 1;
  142.     return 0;
  143. }
  144. /*****************************************************************************
  145.  * End: terminate Glide video thread output method
  146.  *****************************************************************************/
  147. static void End( vout_thread_t *p_vout )
  148. {
  149.     ;
  150. }
  151. /*****************************************************************************
  152.  * Destroy: destroy Glide video thread output method
  153.  *****************************************************************************
  154.  * Terminate an output method created by Create
  155.  *****************************************************************************/
  156. static void Destroy( vlc_object_t *p_this )
  157. {
  158.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  159.     CloseDisplay( p_vout );
  160.     free( p_vout->p_sys );
  161. }
  162. /*****************************************************************************
  163.  * Manage: handle Glide events
  164.  *****************************************************************************
  165.  * This function should be called regularly by video output thread. It manages
  166.  * console events. It returns a non null value on error.
  167.  *****************************************************************************/
  168. static int Manage( vout_thread_t *p_vout )
  169. {
  170.     int buf;
  171.     /* very Linux specific - see tlib.c in Glide for other versions */
  172.     while( kbhit() )
  173.     {
  174.         buf = getch();
  175.         switch( (char)buf )
  176.         {
  177.         case 'q':
  178.             p_vout->p_vlc->b_die = 1;
  179.             break;
  180.         default:
  181.             break;
  182.         }
  183.     }
  184.     return 0;
  185. }
  186. /*****************************************************************************
  187.  * Display: displays previously rendered output
  188.  *****************************************************************************/
  189. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  190. {
  191.     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
  192.     grBufferSwap( 0 );
  193.     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
  194.                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
  195.                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
  196.     {
  197.         msg_Err( p_vout, "cannot take 3dfx back buffer lock" );
  198.     }
  199. }
  200. /* following functions are local */
  201. /*****************************************************************************
  202.  * OpenDisplay: open and initialize 3dfx device
  203.  *****************************************************************************/
  204. static int OpenDisplay( vout_thread_t *p_vout )
  205. {
  206.     static char version[80];
  207.     GrHwConfiguration hwconfig;
  208.     GrScreenResolution_t resolution = GR_RESOLUTION_800x600;
  209.     GrLfbInfo_t p_front_buffer_info;                    /* front buffer info */
  210.     grGlideGetVersion( version );
  211.     grGlideInit();
  212.     if( !grSstQueryHardware(&hwconfig) )
  213.     {
  214.         msg_Err( p_vout, "cannot get 3dfx hardware config" );
  215.         return( 1 );
  216.     }
  217.     grSstSelect( 0 );
  218.     if( !grSstWinOpen( 0, resolution, GR_REFRESH_60Hz,
  219.                        GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ) )
  220.     {
  221.         msg_Err( p_vout, "cannot open 3dfx screen" );
  222.         return( 1 );
  223.     }
  224.     /* disable dithering */
  225.     /*grDitherMode( GR_DITHER_DISABLE );*/
  226.     /* clear both buffers */
  227.     grRenderBuffer( GR_BUFFER_BACKBUFFER );
  228.     grBufferClear( 0, 0, 0 );
  229.     grRenderBuffer( GR_BUFFER_FRONTBUFFER );
  230.     grBufferClear( 0, 0, 0 );
  231.     grRenderBuffer( GR_BUFFER_BACKBUFFER );
  232.     p_vout->p_sys->p_buffer_info.size = sizeof( GrLfbInfo_t );
  233.     p_front_buffer_info.size          = sizeof( GrLfbInfo_t );
  234.     /* lock the buffers to find their adresses */
  235.     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER,
  236.                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
  237.                    &p_front_buffer_info) == FXFALSE )
  238.     {
  239.         msg_Err( p_vout, "cannot take 3dfx front buffer lock" );
  240.         grGlideShutdown();
  241.         return( 1 );
  242.     }
  243.     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER );
  244.     if ( grLfbLock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER,
  245.                    GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE,
  246.                    &p_vout->p_sys->p_buffer_info) == FXFALSE )
  247.     {
  248.         msg_Err( p_vout, "cannot take 3dfx back buffer lock" );
  249.         grGlideShutdown();
  250.         return( 1 );
  251.     }
  252.     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
  253.     grBufferClear( 0, 0, 0 );
  254.     p_vout->p_sys->pp_buffer[0] = p_vout->p_sys->p_buffer_info.lfbPtr;
  255.     p_vout->p_sys->pp_buffer[1] = p_front_buffer_info.lfbPtr;
  256.     p_vout->p_sys->i_index = 0;
  257.     return( 0 );
  258. }
  259. /*****************************************************************************
  260.  * CloseDisplay: close and reset 3dfx device
  261.  *****************************************************************************
  262.  * Returns all resources allocated by OpenDisplay and restore the original
  263.  * state of the device.
  264.  *****************************************************************************/
  265. static void CloseDisplay( vout_thread_t *p_vout )
  266. {
  267.     /* unlock the hidden buffer */
  268.     grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER );
  269.     /* shutdown Glide */
  270.     grGlideShutdown();
  271. }