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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * mga.c : Matrox Graphic Array plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001 VideoLAN
  5.  * $Id: mga.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  8.  *          Samuel Hocevar <sam@zoy.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <errno.h>                                                 /* ENOMEM */
  28. #include <unistd.h>                                               /* close() */
  29. #include <stdlib.h>                                                /* free() */
  30. #include <string.h>                                            /* strerror() */
  31. #include <fcntl.h>                                                 /* open() */
  32. #include <sys/ioctl.h>                                            /* ioctl() */
  33. #include <sys/mman.h>                                          /* PROT_WRITE */
  34. #include <vlc/vlc.h>
  35. #include <vlc/vout.h>
  36. #ifdef SYS_BSD
  37. #include <sys/types.h>                                     /* typedef ushort */
  38. #endif
  39. /*****************************************************************************
  40.  * Local prototypes
  41.  *****************************************************************************/
  42. static int  Create    ( vlc_object_t * );
  43. static void Destroy   ( vlc_object_t * );
  44. static int  Init      ( vout_thread_t * );
  45. static void End       ( vout_thread_t * );
  46. static void Display   ( vout_thread_t *, picture_t * );
  47. static int  NewPicture     ( vout_thread_t *, picture_t * );
  48. /*****************************************************************************
  49.  * Module descriptor
  50.  *****************************************************************************/
  51. vlc_module_begin();
  52.     set_description( _("Matrox Graphic Array video output") );
  53.     set_capability( "video output", 10 );
  54.     set_callbacks( Create, Destroy );
  55. vlc_module_end();
  56. /*****************************************************************************
  57.  * vout_sys_t: video output MGA method descriptor
  58.  *****************************************************************************
  59.  * This structure is part of the video output thread descriptor.
  60.  * It describes the MGA specific properties of an output thread.
  61.  *****************************************************************************/
  62. #ifndef __LINUX_MGAVID_H
  63. #   define __LINUX_MGAVID_H
  64. #   define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
  65. #   define MGA_VID_ON     _IO ('J', 2)
  66. #   define MGA_VID_OFF    _IO ('J', 3)
  67. #   define MGA_VID_FSEL   _IOR('J', 4, int)
  68. #   define MGA_G200 0x1234
  69. #   define MGA_G400 0x5678
  70. #   define MGA_VID_FORMAT_YV12 0x32315659
  71. #   define MGA_VID_FORMAT_IYUV (('I'<<24)|('Y'<<16)|('U'<<8)|'V')
  72. #   define MGA_VID_FORMAT_I420 (('I'<<24)|('4'<<16)|('2'<<8)|'0')
  73. #   define MGA_VID_FORMAT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
  74. #   define MGA_VID_FORMAT_UYVY (('U'<<24)|('Y'<<16)|('V'<<8)|'Y')
  75. #   define MGA_VID_VERSION     0x0201
  76. #   define MGA_NUM_FRAMES      1
  77. typedef struct mga_vid_config_t
  78. {
  79.     uint16_t version;
  80.     uint16_t card_type;
  81.     uint32_t ram_size;
  82.     uint32_t src_width;
  83.     uint32_t src_height;
  84.     uint32_t dest_width;
  85.     uint32_t dest_height;
  86.     uint32_t x_org;
  87.     uint32_t y_org;
  88.     uint8_t  colkey_on;
  89.     uint8_t  colkey_red;
  90.     uint8_t  colkey_green;
  91.     uint8_t  colkey_blue;
  92.     uint32_t format;
  93.     uint32_t frame_size;
  94.     uint32_t num_frames;
  95. } mga_vid_config_t;
  96. #endif
  97. struct vout_sys_t
  98. {
  99.     mga_vid_config_t    mga;
  100.     int                 i_fd;
  101.     byte_t *            p_video;
  102. };
  103. struct picture_sys_t
  104. {
  105.     int     i_frame;
  106. };
  107. #define CEIL32(x) (((x)+31)&~31)
  108. /*****************************************************************************
  109.  * Create: allocates dummy video thread output method
  110.  *****************************************************************************
  111.  * This function allocates and initializes a dummy vout method.
  112.  *****************************************************************************/
  113. static int Create( vlc_object_t *p_this )
  114. {
  115.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  116.     /* Allocate structure */
  117.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  118.     if( p_vout->p_sys == NULL )
  119.     {
  120.         msg_Err( p_vout, "out of memory" );
  121.         return( 1 );
  122.     }
  123.     p_vout->p_sys->i_fd = open( "/dev/mga_vid", O_RDWR );
  124.     if( p_vout->p_sys->i_fd == -1 )
  125.     {
  126.         msg_Err( p_vout, "cannot open MGA driver /dev/mga_vid" );
  127.         free( p_vout->p_sys );
  128.         return( 1 );
  129.     }
  130.     p_vout->pf_init = Init;
  131.     p_vout->pf_end = End;
  132.     p_vout->pf_manage = NULL;
  133.     p_vout->pf_render = NULL;
  134.     p_vout->pf_display = Display;
  135.     return( 0 );
  136. }
  137. /*****************************************************************************
  138.  * Init: initialize dummy video thread output method
  139.  *****************************************************************************/
  140. static int Init( vout_thread_t *p_vout )
  141. {
  142.     int i_index;
  143.     picture_t *p_pic;
  144.     I_OUTPUTPICTURES = 0;
  145.     /* create the MGA output */
  146.     p_vout->output.i_width = p_vout->render.i_width;
  147.     p_vout->output.i_height = p_vout->render.i_height;
  148.     p_vout->output.i_aspect = p_vout->render.i_aspect;
  149.     /* Set coordinates and aspect ratio */
  150.     p_vout->p_sys->mga.src_width = CEIL32(p_vout->output.i_width);
  151.     p_vout->p_sys->mga.src_height = p_vout->output.i_height;
  152.     vout_PlacePicture( p_vout, 1024, 768,
  153.                        &p_vout->p_sys->mga.x_org, &p_vout->p_sys->mga.y_org,
  154.                        &p_vout->p_sys->mga.dest_width,
  155.                        &p_vout->p_sys->mga.dest_height );
  156.     /* Initialize a video buffer */
  157.     p_vout->p_sys->mga.colkey_on = 0;
  158.     p_vout->p_sys->mga.num_frames = MGA_NUM_FRAMES;
  159.     p_vout->p_sys->mga.frame_size = CEIL32(p_vout->output.i_width)
  160.                                      * p_vout->output.i_height * 2;
  161.     p_vout->p_sys->mga.version = MGA_VID_VERSION;
  162.     /* Assume we only do YMGA for the moment. XXX: mga_vid calls this
  163.      * YV12, but it's actually some strange format with packed UV. */
  164.     p_vout->output.i_chroma = VLC_FOURCC('Y','M','G','A');
  165.     p_vout->p_sys->mga.format = MGA_VID_FORMAT_YV12;
  166.     if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, &p_vout->p_sys->mga) )
  167.     {
  168.         msg_Err( p_vout, "MGA config ioctl failed" );
  169.         return -1;
  170.     }
  171.     if( p_vout->p_sys->mga.card_type == MGA_G200 )
  172.     {
  173.         msg_Dbg( p_vout, "detected MGA G200 (%d MB Ram)",
  174.                          p_vout->p_sys->mga.ram_size );
  175.     }
  176.     else
  177.     {
  178.         msg_Dbg( p_vout, "detected MGA G400/G450 (%d MB Ram)",
  179.                          p_vout->p_sys->mga.ram_size );
  180.     }
  181.     p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->mga.frame_size
  182.                                        * MGA_NUM_FRAMES,
  183.                                    PROT_WRITE, MAP_SHARED,
  184.                                    p_vout->p_sys->i_fd, 0 );
  185.     /* Try to initialize up to MGA_NUM_FRAMES direct buffers */
  186.     while( I_OUTPUTPICTURES < MGA_NUM_FRAMES )
  187.     {
  188.         p_pic = NULL;
  189.         /* Find an empty picture slot */
  190.         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  191.         {
  192.             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  193.             {
  194.                 p_pic = p_vout->p_picture + i_index;
  195.                 break;
  196.             }
  197.         }
  198.         /* Allocate the picture */
  199.         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
  200.         {
  201.             break;
  202.         }
  203.         p_pic->i_status = DESTROYED_PICTURE;
  204.         p_pic->i_type   = DIRECT_PICTURE;
  205.         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
  206.         I_OUTPUTPICTURES++;
  207.     }
  208.     /* Blank the windows */
  209.     for( i_index = 0; i_index < I_OUTPUTPICTURES; i_index++ )
  210.     {
  211.         memset( p_vout->p_sys->p_video
  212.                  + p_vout->p_sys->mga.frame_size * i_index,
  213.                 0x00, p_vout->p_sys->mga.frame_size / 2 );
  214.         memset( p_vout->p_sys->p_video
  215.                  + p_vout->p_sys->mga.frame_size * ( 2*i_index + 1 ) / 2,
  216.                 0x80, p_vout->p_sys->mga.frame_size / 2 );
  217.     }
  218.     /* Display the image */
  219.     ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
  220.     return( 0 );
  221. }
  222. /*****************************************************************************
  223.  * End: terminate dummy video thread output method
  224.  *****************************************************************************/
  225. static void End( vout_thread_t *p_vout )
  226. {
  227.     int i_index;
  228.     ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
  229.     /* Free the output buffers we allocated */
  230.     for( i_index = I_OUTPUTPICTURES ; i_index ; )
  231.     {
  232.         i_index--;
  233.     }
  234. }
  235. /*****************************************************************************
  236.  * Destroy: destroy dummy video thread output method
  237.  *****************************************************************************
  238.  * Terminate an output method created by DummyCreateOutputMethod
  239.  *****************************************************************************/
  240. static void Destroy( vlc_object_t *p_this )
  241. {
  242.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  243.     close( p_vout->p_sys->i_fd );
  244.     free( p_vout->p_sys );
  245. }
  246. /*****************************************************************************
  247.  * Display: displays previously rendered output
  248.  *****************************************************************************/
  249. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  250. {
  251.     ioctl( p_vout->p_sys->i_fd, MGA_VID_FSEL, &p_pic->p_sys->i_frame );
  252. }
  253. /* Following functions are local */
  254. /*****************************************************************************
  255.  * NewPicture: allocate a picture
  256.  *****************************************************************************
  257.  * Returns 0 on success, -1 otherwise
  258.  *****************************************************************************/
  259. static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
  260. {
  261.     /* We know the chroma, allocate a buffer which will be used
  262.      * directly by the decoder */
  263.     p_pic->p_data = p_vout->p_sys->p_video + I_OUTPUTPICTURES
  264.                                               * p_vout->p_sys->mga.frame_size;
  265.     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
  266.     if( p_pic->p_sys == NULL )
  267.     {
  268.         return -1;
  269.     }
  270.     p_pic->Y_PIXELS = p_pic->p_data;
  271.     p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
  272.     p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
  273.     p_pic->p[Y_PLANE].i_pitch = CEIL32( p_vout->output.i_width );
  274.     p_pic->p[Y_PLANE].i_pixel_pitch = 1;
  275.     p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
  276.     p_pic->U_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 2/4;
  277.     p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
  278.     p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
  279.     p_pic->p[U_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
  280.     p_pic->p[U_PLANE].i_pixel_pitch = 1;
  281.     p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
  282.     p_pic->V_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 3/4;
  283.     p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
  284.     p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
  285.     p_pic->p[V_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
  286.     p_pic->p[V_PLANE].i_pixel_pitch = 1;
  287.     p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
  288.     p_pic->p_sys->i_frame = I_OUTPUTPICTURES;
  289.     p_pic->i_planes = 3;
  290.     return 0;
  291. }