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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * vout_aa.c: Aa video output display method for testing purposes
  3.  *****************************************************************************
  4.  * Copyright (C) 2002 VideoLAN
  5.  * $Id: aa.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
  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>                                                /* free() */
  28. #include <string.h>                                            /* strerror() */
  29. #include <aalib.h>
  30. #include <vlc/vlc.h>
  31. #include <vlc/vout.h>
  32. #include <vlc/intf.h>
  33. /*****************************************************************************
  34.  * Local prototypes
  35.  *****************************************************************************/
  36. static int  Create    ( vlc_object_t * );
  37. static void Destroy   ( vlc_object_t * );
  38. static int  Init      ( vout_thread_t * );
  39. static void End       ( vout_thread_t * );
  40. static int  Manage    ( vout_thread_t * );
  41. static void Render    ( vout_thread_t *, picture_t * );
  42. static void Display   ( vout_thread_t *, picture_t * );
  43. static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
  44. /*****************************************************************************
  45.  * Module descriptor
  46.  *****************************************************************************/
  47. vlc_module_begin();
  48.     set_description( _("ASCII-art video output") );
  49.     set_capability( "video output", 10 );
  50.     add_shortcut( "aalib" );
  51.     set_callbacks( Create, Destroy );
  52. vlc_module_end();
  53. /*****************************************************************************
  54.  * vout_sys_t: aa video output method descriptor
  55.  *****************************************************************************
  56.  * This structure is part of the video output thread descriptor.
  57.  * It describes the aa specific properties of an output thread.
  58.  *****************************************************************************/
  59. struct vout_sys_t
  60. {
  61.     struct aa_context*  aa_context;
  62.     aa_palette          palette;
  63.     int                 i_width;                     /* width of main window */
  64.     int                 i_height;                   /* height of main window */
  65. };
  66. /*****************************************************************************
  67.  * Create: allocates aa video thread output method
  68.  *****************************************************************************
  69.  * This function allocates and initializes a aa vout method.
  70.  *****************************************************************************/
  71. static int Create( vlc_object_t *p_this )
  72. {
  73.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  74.     /* Allocate structure */
  75.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  76.     if( p_vout->p_sys == NULL )
  77.     {
  78.         msg_Err( p_vout, "out of memory" );
  79.         return( 1 );
  80.     }
  81.     /* Don't parse any options, but take $AAOPTS into account */
  82.     aa_parseoptions( NULL, NULL, NULL, NULL );
  83.     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
  84.     {
  85.         msg_Err( p_vout, "cannot initialize aalib" );
  86.         return( 1 );
  87.     }
  88.     p_vout->pf_init = Init;
  89.     p_vout->pf_end = End;
  90.     p_vout->pf_manage = Manage;
  91.     p_vout->pf_render = Render;
  92.     p_vout->pf_display = Display;
  93.     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
  94.     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
  95.     aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
  96.     aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
  97.     aa_hidemouse( p_vout->p_sys->aa_context );
  98.     return( 0 );
  99. }
  100. /*****************************************************************************
  101.  * Init: initialize aa video thread output method
  102.  *****************************************************************************/
  103. static int Init( vout_thread_t *p_vout )
  104. {
  105.     int i_index;
  106.     picture_t *p_pic = NULL;
  107.     I_OUTPUTPICTURES = 0;
  108.     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
  109.     p_vout->output.i_width = p_vout->p_sys->i_width;
  110.     p_vout->output.i_height = p_vout->p_sys->i_height;
  111.     p_vout->output.i_aspect = p_vout->p_sys->i_width
  112.                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
  113.     p_vout->output.pf_setpalette = SetPalette;
  114.     /* Find an empty picture slot */
  115.     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  116.     {
  117.         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  118.         {
  119.             p_pic = p_vout->p_picture + i_index;
  120.             break;
  121.         }
  122.     }
  123.     if( p_pic == NULL )
  124.     {
  125.         return -1;
  126.     }
  127.     /* Allocate the picture */
  128.     p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
  129.     p_pic->p->i_lines = p_vout->p_sys->i_height;
  130.     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
  131.     p_pic->p->i_pitch = p_vout->p_sys->i_width;
  132.     p_pic->p->i_pixel_pitch = 1;
  133.     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
  134.     p_pic->i_planes = 1;
  135.     p_pic->i_status = DESTROYED_PICTURE;
  136.     p_pic->i_type   = DIRECT_PICTURE;
  137.     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
  138.     I_OUTPUTPICTURES++;
  139.     return 0;
  140. }
  141. /*****************************************************************************
  142.  * End: terminate aa video thread output method
  143.  *****************************************************************************/
  144. static void End( vout_thread_t *p_vout )
  145. {
  146.     ;
  147. }
  148. /*****************************************************************************
  149.  * Destroy: destroy aa video thread output method
  150.  *****************************************************************************
  151.  * Terminate an output method created by AaCreateOutputMethod
  152.  *****************************************************************************/
  153. static void Destroy( vlc_object_t *p_this )
  154. {
  155.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  156.     aa_close( p_vout->p_sys->aa_context );
  157.     free( p_vout->p_sys );
  158. }
  159. /*****************************************************************************
  160.  * Manage: handle aa events
  161.  *****************************************************************************
  162.  * This function should be called regularly by video output thread. It manages
  163.  * console events. It returns a non null value on error.
  164.  *****************************************************************************/
  165. static int Manage( vout_thread_t *p_vout )
  166. {
  167.     int event, x, y, b;
  168.     event = aa_getevent( p_vout->p_sys->aa_context, 0 );
  169.     switch ( event )
  170.     {
  171.     case AA_MOUSE:
  172.         aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
  173.         if ( b & AA_BUTTON3 )
  174.         {
  175.             intf_thread_t *p_intf;
  176.             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
  177.             if( p_intf )
  178.             {
  179.                 p_intf->b_menu_change = 1;
  180.                 vlc_object_release( p_intf );
  181.             }
  182.         }
  183.         break;
  184.     case AA_RESIZE:
  185.         p_vout->i_changes |= VOUT_SIZE_CHANGE;
  186.         aa_resize( p_vout->p_sys->aa_context );
  187.         p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
  188.         p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
  189.         break;
  190.     default:
  191.         break;
  192.     }
  193.     return( 0 );
  194. }
  195. /*****************************************************************************
  196.  * Render: render previously calculated output
  197.  *****************************************************************************/
  198. static void Render( vout_thread_t *p_vout, picture_t *p_pic )
  199. {
  200.   aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
  201.                  aa_imgwidth( p_vout->p_sys->aa_context ),
  202.                  aa_imgheight( p_vout->p_sys->aa_context ) );
  203. }
  204. /*****************************************************************************
  205.  * Display: displays previously rendered output
  206.  *****************************************************************************/
  207. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  208. {
  209.     /* No need to do anything, the fake direct buffers stay as they are */
  210.     int i_width, i_height, i_x, i_y;
  211.     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
  212.                        &i_x, &i_y, &i_width, &i_height );
  213.     aa_flush(p_vout->p_sys->aa_context);
  214. }
  215. /*****************************************************************************
  216.  * SetPalette: set the 8bpp palette
  217.  *****************************************************************************/
  218. static void SetPalette( vout_thread_t *p_vout,
  219.                         uint16_t *red, uint16_t *green, uint16_t *blue )
  220. {
  221.     int i;
  222.     /* Fill colors with color information */
  223.     for( i = 0; i < 256; i++ )
  224.     {
  225.         aa_setpalette( p_vout->p_sys->palette, 256 -i,
  226.                        red[ i ], green[ i ], blue[ i ] );
  227.     }
  228. }