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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * vmem.c: memory video driver for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2008 the VideoLAN team
  5.  * $Id: f132021a2d8e5ef690a9d37753e8bfbafc0b080e $
  6.  *
  7.  * Authors: Sam 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_vout.h>
  32. /*****************************************************************************
  33.  * Local prototypes
  34.  *****************************************************************************/
  35. static int  Create    ( vlc_object_t * );
  36. static void Destroy   ( vlc_object_t * );
  37. static int  Init          ( vout_thread_t * );
  38. static void End           ( vout_thread_t * );
  39. static int  LockPicture   ( vout_thread_t *, picture_t * );
  40. static int  UnlockPicture ( vout_thread_t *, picture_t * );
  41. /*****************************************************************************
  42.  * Module descriptor
  43.  *****************************************************************************/
  44. #define T_WIDTH N_( "Width" )
  45. #define LT_WIDTH N_( "Video memory buffer width." )
  46. #define T_HEIGHT N_( "Height" )
  47. #define LT_HEIGHT N_( "Video memory buffer height." )
  48. #define T_PITCH N_( "Pitch" )
  49. #define LT_PITCH N_( "Video memory buffer pitch in bytes." )
  50. #define T_CHROMA N_( "Chroma" )
  51. #define LT_CHROMA N_( "Output chroma for the memory image as a 4-character " 
  52.                       "string, eg. "RV32"." )
  53. #define T_LOCK N_( "Lock function" )
  54. #define LT_LOCK N_( "Address of the locking callback function. This " 
  55.                     "function must fill in valid plane memory address " 
  56.                     "information for use by the video renderer." )
  57. #define T_UNLOCK N_( "Unlock function" )
  58. #define LT_UNLOCK N_( "Address of the unlocking callback function" )
  59. #define T_DATA N_( "Callback data" )
  60. #define LT_DATA N_( "Data for the locking and unlocking functions" )
  61. vlc_module_begin ()
  62.     set_description( N_( "Video memory output" ) )
  63.     set_shortname( N_("Video memory") )
  64.     set_category( CAT_VIDEO )
  65.     set_subcategory( SUBCAT_VIDEO_VOUT )
  66.     set_capability( "video output", 0 )
  67.     add_integer( "vmem-width", 320, NULL, T_WIDTH, LT_WIDTH, false )
  68.     add_integer( "vmem-height", 200, NULL, T_HEIGHT, LT_HEIGHT, false )
  69.     add_integer( "vmem-pitch", 640, NULL, T_PITCH, LT_PITCH, false )
  70.     add_string( "vmem-chroma", "RV16", NULL, T_CHROMA, LT_CHROMA, true )
  71.     add_string( "vmem-lock", "0", NULL, T_LOCK, LT_LOCK, true )
  72.     add_string( "vmem-unlock", "0", NULL, T_UNLOCK, LT_UNLOCK, true )
  73.     add_string( "vmem-data", "0", NULL, T_DATA, LT_DATA, true )
  74.     set_callbacks( Create, Destroy )
  75. vlc_module_end ()
  76. /*****************************************************************************
  77.  * vout_sys_t: video output descriptor
  78.  *****************************************************************************/
  79. struct vout_sys_t
  80. {
  81.     int i_width, i_height, i_pitch;
  82.     void (*pf_lock) (void *, void **);
  83.     void (*pf_unlock) (void *);
  84.     void *p_data;
  85. };
  86. /*****************************************************************************
  87.  * Create: allocates video thread
  88.  *****************************************************************************
  89.  * This function allocates and initializes a vout method.
  90.  *****************************************************************************/
  91. static int Create( vlc_object_t *p_this )
  92. {
  93.     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
  94.     /* Allocate instance and initialize some members */
  95.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  96.     if( ! p_vout->p_sys )
  97.         return VLC_ENOMEM;
  98.     p_vout->pf_init = Init;
  99.     p_vout->pf_end = End;
  100.     p_vout->pf_manage = NULL;
  101.     p_vout->pf_render = NULL;
  102.     p_vout->pf_display = NULL;
  103.     return VLC_SUCCESS;
  104. }
  105. /*****************************************************************************
  106.  * Init: initialize video thread
  107.  *****************************************************************************/
  108. static int Init( vout_thread_t *p_vout )
  109. {
  110.     int i_index;
  111.     picture_t *p_pic;
  112.     char *psz_chroma, *psz_tmp;
  113.     int i_width, i_height, i_pitch, i_chroma;
  114.     i_width = var_CreateGetInteger( p_vout, "vmem-width" );
  115.     i_height = var_CreateGetInteger( p_vout, "vmem-height" );
  116.     i_pitch = var_CreateGetInteger( p_vout, "vmem-pitch" );
  117.     psz_chroma = var_CreateGetString( p_vout, "vmem-chroma" );
  118.     if( psz_chroma )
  119.     {
  120.         if( strlen( psz_chroma ) < 4 )
  121.         {
  122.             msg_Err( p_vout, "vmem-chroma should be 4 characters long" );
  123.             free( psz_chroma );
  124.             return VLC_EGENERIC;
  125.         }
  126.         i_chroma = VLC_FOURCC( psz_chroma[0], psz_chroma[1],
  127.                                psz_chroma[2], psz_chroma[3] );
  128.         free( psz_chroma );
  129.     }
  130.     else
  131.     {
  132.         msg_Err( p_vout, "Cannot find chroma information." );
  133.         return VLC_EGENERIC;
  134.     }
  135.     psz_tmp = var_CreateGetString( p_vout, "vmem-lock" );
  136.     p_vout->p_sys->pf_lock = (void (*) (void *, void **))(intptr_t)atoll( psz_tmp );
  137.     free( psz_tmp );
  138.     psz_tmp = var_CreateGetString( p_vout, "vmem-unlock" );
  139.     p_vout->p_sys->pf_unlock = (void (*) (void *))(intptr_t)atoll( psz_tmp );
  140.     free( psz_tmp );
  141.     psz_tmp = var_CreateGetString( p_vout, "vmem-data" );
  142.     p_vout->p_sys->p_data = (void *)(intptr_t)atoll( psz_tmp );
  143.     free( psz_tmp );
  144.     if( !p_vout->p_sys->pf_lock || !p_vout->p_sys->pf_unlock )
  145.     {
  146.         msg_Err( p_vout, "Invalid lock or unlock callbacks" );
  147.         return VLC_EGENERIC;
  148.     }
  149.     I_OUTPUTPICTURES = 0;
  150.     /* Initialize the output structure */
  151.     p_vout->output.i_chroma = i_chroma;
  152.     p_vout->output.pf_setpalette = NULL;
  153.     p_vout->output.i_width = i_width;
  154.     p_vout->output.i_height = i_height;
  155.     p_vout->output.i_aspect = p_vout->output.i_width
  156.                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
  157.     /* Define the bitmasks */
  158.     switch( i_chroma )
  159.     {
  160.       case VLC_FOURCC( 'R','V','1','5' ):
  161.         p_vout->output.i_rmask = 0x001f;
  162.         p_vout->output.i_gmask = 0x03e0;
  163.         p_vout->output.i_bmask = 0x7c00;
  164.         break;
  165.       case VLC_FOURCC( 'R','V','1','6' ):
  166.         p_vout->output.i_rmask = 0x001f;
  167.         p_vout->output.i_gmask = 0x07e0;
  168.         p_vout->output.i_bmask = 0xf800;
  169.         break;
  170.       case VLC_FOURCC( 'R','V','2','4' ):
  171.         p_vout->output.i_rmask = 0xff0000;
  172.         p_vout->output.i_gmask = 0x00ff00;
  173.         p_vout->output.i_bmask = 0x0000ff;
  174.         break;
  175.       case VLC_FOURCC( 'R','V','3','2' ):
  176.         p_vout->output.i_rmask = 0xff0000;
  177.         p_vout->output.i_gmask = 0x00ff00;
  178.         p_vout->output.i_bmask = 0x0000ff;
  179.         break;
  180.     }
  181.     /* Try to initialize 1 direct buffer */
  182.     p_pic = NULL;
  183.     /* Find an empty picture slot */
  184.     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  185.     {
  186.         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  187.         {
  188.             p_pic = p_vout->p_picture + i_index;
  189.             break;
  190.         }
  191.     }
  192.     /* Allocate the picture */
  193.     if( p_pic == NULL )
  194.     {
  195.         return VLC_SUCCESS;
  196.     }
  197.     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
  198.                       p_vout->output.i_width, p_vout->output.i_height,
  199.                       p_vout->output.i_aspect );
  200.     p_pic->p->i_pitch = i_pitch;
  201.     p_pic->pf_lock = LockPicture;
  202.     p_pic->pf_unlock = UnlockPicture;
  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.     return VLC_SUCCESS;
  208. }
  209. /*****************************************************************************
  210.  * End: terminate video thread output method
  211.  *****************************************************************************/
  212. static void End( vout_thread_t *p_vout )
  213. {
  214.     (void)p_vout;
  215. }
  216. /*****************************************************************************
  217.  * Destroy: destroy video thread
  218.  *****************************************************************************
  219.  * Terminate an output method created by Create
  220.  *****************************************************************************/
  221. static void Destroy( vlc_object_t *p_this )
  222. {
  223.     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
  224.     /* Destroy structure */
  225.     free( p_vout->p_sys );
  226. }
  227. /*****************************************************************************
  228.  * LockPicture: lock a picture
  229.  *****************************************************************************
  230.  * This function locks the picture and prepares it for direct pixel access.
  231.  *****************************************************************************/
  232. static int LockPicture( vout_thread_t *p_vout, picture_t *p_pic )
  233. {
  234.     int i_index;
  235.     void *planes[p_pic->i_planes];
  236.     p_vout->p_sys->pf_lock( p_vout->p_sys->p_data, planes );
  237.     for( i_index = 0; i_index < p_pic->i_planes; i_index++ )
  238.     {
  239.         p_pic->p[i_index].p_pixels = planes[i_index];
  240.     }
  241.     return VLC_SUCCESS;
  242. }
  243. /*****************************************************************************
  244.  * UnlockPicture: unlock a picture
  245.  *****************************************************************************
  246.  * This function unlocks a previously locked picture.
  247.  *****************************************************************************/
  248. static int UnlockPicture( vout_thread_t *p_vout, picture_t *p_pic )
  249. {
  250.     p_vout->p_sys->pf_unlock( p_vout->p_sys->p_data );
  251.     (void)p_pic;
  252.     return VLC_SUCCESS;
  253. }