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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * invert.c : Invert video plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
  5.  * $Id: invert.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 <stdlib.h>                                      /* malloc(), free() */
  27. #include <string.h>
  28. #include <vlc/vlc.h>
  29. #include <vlc/vout.h>
  30. #include "filter_common.h"
  31. /*****************************************************************************
  32.  * Local prototypes
  33.  *****************************************************************************/
  34. static int  Create    ( vlc_object_t * );
  35. static void Destroy   ( vlc_object_t * );
  36. static int  Init      ( vout_thread_t * );
  37. static void End       ( vout_thread_t * );
  38. static void Render    ( vout_thread_t *, picture_t * );
  39. static int  SendEvents( vlc_object_t *, char const *,
  40.                         vlc_value_t, vlc_value_t, void * );
  41. /*****************************************************************************
  42.  * Module descriptor
  43.  *****************************************************************************/
  44. vlc_module_begin();
  45.     set_description( _("Invert video filter") );
  46.     set_capability( "video filter", 0 );
  47.     add_shortcut( "invert" );
  48.     set_callbacks( Create, Destroy );
  49. vlc_module_end();
  50. /*****************************************************************************
  51.  * vout_sys_t: Invert video output method descriptor
  52.  *****************************************************************************
  53.  * This structure is part of the video output thread descriptor.
  54.  * It describes the Invert specific properties of an output thread.
  55.  *****************************************************************************/
  56. struct vout_sys_t
  57. {
  58.     vout_thread_t *p_vout;
  59. };
  60. /*****************************************************************************
  61.  * Control: control facility for the vout (forwards to child vout)
  62.  *****************************************************************************/
  63. static int Control( vout_thread_t *p_vout, int i_query, va_list args )
  64. {
  65.     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
  66. }
  67. /*****************************************************************************
  68.  * Create: allocates Invert video thread output method
  69.  *****************************************************************************
  70.  * This function allocates and initializes a Invert vout method.
  71.  *****************************************************************************/
  72. static int Create( vlc_object_t *p_this )
  73. {
  74.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  75.     /* Allocate structure */
  76.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  77.     if( p_vout->p_sys == NULL )
  78.     {
  79.         msg_Err( p_vout, "out of memory" );
  80.         return VLC_ENOMEM;
  81.     }
  82.     p_vout->pf_init = Init;
  83.     p_vout->pf_end = End;
  84.     p_vout->pf_manage = NULL;
  85.     p_vout->pf_render = Render;
  86.     p_vout->pf_display = NULL;
  87.     p_vout->pf_control = Control;
  88.     return VLC_SUCCESS;
  89. }
  90. /*****************************************************************************
  91.  * Init: initialize Invert video thread output method
  92.  *****************************************************************************/
  93. static int Init( vout_thread_t *p_vout )
  94. {
  95.     int i_index;
  96.     picture_t *p_pic;
  97.     I_OUTPUTPICTURES = 0;
  98.     /* Initialize the output structure */
  99.     p_vout->output.i_chroma = p_vout->render.i_chroma;
  100.     p_vout->output.i_width  = p_vout->render.i_width;
  101.     p_vout->output.i_height = p_vout->render.i_height;
  102.     p_vout->output.i_aspect = p_vout->render.i_aspect;
  103.     /* Try to open the real video output */
  104.     msg_Dbg( p_vout, "spawning the real video output" );
  105.     p_vout->p_sys->p_vout = vout_Create( p_vout,
  106.                            p_vout->render.i_width, p_vout->render.i_height,
  107.                            p_vout->render.i_chroma, p_vout->render.i_aspect );
  108.     /* Everything failed */
  109.     if( p_vout->p_sys->p_vout == NULL )
  110.     {
  111.         msg_Err( p_vout, "can't open vout, aborting" );
  112.         return VLC_EGENERIC;
  113.     }
  114.     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
  115.     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
  116.     ADD_PARENT_CALLBACKS( SendEventsToChild );
  117.     return VLC_SUCCESS;
  118. }
  119. /*****************************************************************************
  120.  * End: terminate Invert video thread output method
  121.  *****************************************************************************/
  122. static void End( vout_thread_t *p_vout )
  123. {
  124.     int i_index;
  125.     /* Free the fake output buffers we allocated */
  126.     for( i_index = I_OUTPUTPICTURES ; i_index ; )
  127.     {
  128.         i_index--;
  129.         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
  130.     }
  131. }
  132. /*****************************************************************************
  133.  * Destroy: destroy Invert video thread output method
  134.  *****************************************************************************
  135.  * Terminate an output method created by InvertCreateOutputMethod
  136.  *****************************************************************************/
  137. static void Destroy( vlc_object_t *p_this )
  138. {
  139.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  140.     if( p_vout->p_sys->p_vout )
  141.     {
  142.         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
  143.         vlc_object_detach( p_vout->p_sys->p_vout );
  144.         vout_Destroy( p_vout->p_sys->p_vout );
  145.     }
  146.     DEL_PARENT_CALLBACKS( SendEventsToChild );
  147.     free( p_vout->p_sys );
  148. }
  149. /*****************************************************************************
  150.  * Render: displays previously rendered output
  151.  *****************************************************************************
  152.  * This function send the currently rendered image to Invert image, waits
  153.  * until it is displayed and switch the two rendering buffers, preparing next
  154.  * frame.
  155.  *****************************************************************************/
  156. static void Render( vout_thread_t *p_vout, picture_t *p_pic )
  157. {
  158.     picture_t *p_outpic;
  159.     int i_index;
  160.     /* This is a new frame. Get a structure from the video_output. */
  161.     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
  162.               == NULL )
  163.     {
  164.         if( p_vout->b_die || p_vout->b_error )
  165.         {
  166.             return;
  167.         }
  168.         msleep( VOUT_OUTMEM_SLEEP );
  169.     }
  170.     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
  171.     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
  172.     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
  173.     {
  174.         uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
  175.         p_in = p_pic->p[i_index].p_pixels;
  176.         p_in_end = p_in + p_pic->p[i_index].i_visible_lines
  177.                            * p_pic->p[i_index].i_pitch;
  178.         p_out = p_outpic->p[i_index].p_pixels;
  179.         for( ; p_in < p_in_end ; )
  180.         {
  181.             uint64_t *p_in64, *p_out64;
  182.             p_line_end = p_in + p_pic->p[i_index].i_visible_pitch - 64;
  183.             p_in64 = (uint64_t*)p_in;
  184.             p_out64 = (uint64_t*)p_out;
  185.             for( ; (ptrdiff_t)p_in64 < (ptrdiff_t)p_line_end ; )
  186.             {
  187.                 /* Do 64 pixels at a time */
  188.                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
  189.                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
  190.                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
  191.                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
  192.             }
  193.             p_in = (uint8_t*)p_in64;
  194.             p_out = (uint8_t*)p_out64;
  195.             p_line_end += 64;
  196.             for( ; p_in < p_line_end ; )
  197.             {
  198.                 *p_out++ = ~( *p_in++ );
  199.             }
  200.             p_in += p_pic->p[i_index].i_pitch
  201.                      - p_pic->p[i_index].i_visible_pitch;
  202.             p_out += p_outpic->p[i_index].i_pitch
  203.                      - p_outpic->p[i_index].i_visible_pitch;
  204.         }
  205.     }
  206.     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
  207.     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
  208. }
  209. /*****************************************************************************
  210.  * SendEvents: forward mouse and keyboard events to the parent p_vout
  211.  *****************************************************************************/
  212. static int SendEvents( vlc_object_t *p_this, char const *psz_var,
  213.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  214. {
  215.     var_Set( (vlc_object_t *)p_data, psz_var, newval );
  216.     return VLC_SUCCESS;
  217. }
  218. /*****************************************************************************
  219.  * SendEventsToChild: forward events to the child/children vout
  220.  *****************************************************************************/
  221. static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
  222.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  223. {
  224.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  225.     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
  226.     return VLC_SUCCESS;
  227. }