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

midi

开发平台:

Unix_Linux

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