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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * distort.c : Misc video effects plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
  5.  * $Id: distort.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 <math.h>                                            /* sin(), cos() */
  29. #include <vlc/vlc.h>
  30. #include <vlc/vout.h>
  31. #include "filter_common.h"
  32. #define DISTORT_MODE_WAVE    1
  33. #define DISTORT_MODE_RIPPLE  2
  34. /*****************************************************************************
  35.  * Local prototypes
  36.  *****************************************************************************/
  37. static int  Create    ( vlc_object_t * );
  38. static void Destroy   ( vlc_object_t * );
  39. static int  Init      ( vout_thread_t * );
  40. static void End       ( vout_thread_t * );
  41. static void Render    ( vout_thread_t *, picture_t * );
  42. static void DistortWave    ( vout_thread_t *, picture_t *, picture_t * );
  43. static void DistortRipple  ( vout_thread_t *, picture_t *, picture_t * );
  44. static int  SendEvents   ( vlc_object_t *, char const *,
  45.                            vlc_value_t, vlc_value_t, void * );
  46. /*****************************************************************************
  47.  * Module descriptor
  48.  *****************************************************************************/
  49. #define MODE_TEXT N_("Distort mode")
  50. #define MODE_LONGTEXT N_("Distort mode, one of "wave" and "ripple"")
  51. static char *mode_list[] = { "wave", "ripple" };
  52. static char *mode_list_text[] = { N_("Wave"), N_("Ripple") };
  53. vlc_module_begin();
  54.     set_description( _("Distort video filter") );
  55.     set_capability( "video filter", 0 );
  56.     add_string( "distort-mode", "wave", NULL, MODE_TEXT, MODE_LONGTEXT,
  57.                 VLC_FALSE );
  58.         change_string_list( mode_list, mode_list_text, 0 );
  59.     add_shortcut( "distort" );
  60.     set_callbacks( Create, Destroy );
  61. vlc_module_end();
  62. /*****************************************************************************
  63.  * vout_sys_t: Distort video output method descriptor
  64.  *****************************************************************************
  65.  * This structure is part of the video output thread descriptor.
  66.  * It describes the Distort specific properties of an output thread.
  67.  *****************************************************************************/
  68. struct vout_sys_t
  69. {
  70.     int i_mode;
  71.     vout_thread_t *p_vout;
  72.     /* For the wave mode */
  73.     double  f_angle;
  74.     mtime_t last_date;
  75. };
  76. /*****************************************************************************
  77.  * Control: control facility for the vout (forwards to child vout)
  78.  *****************************************************************************/
  79. static int Control( vout_thread_t *p_vout, int i_query, va_list args )
  80. {
  81.     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
  82. }
  83. /*****************************************************************************
  84.  * Create: allocates Distort video thread output method
  85.  *****************************************************************************
  86.  * This function allocates and initializes a Distort vout method.
  87.  *****************************************************************************/
  88. static int Create( vlc_object_t *p_this )
  89. {
  90.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  91.     char *psz_method, *psz_method_tmp;
  92.     /* Allocate structure */
  93.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  94.     if( p_vout->p_sys == NULL )
  95.     {
  96.         msg_Err( p_vout, "out of memory" );
  97.         return VLC_ENOMEM;
  98.     }
  99.     p_vout->pf_init = Init;
  100.     p_vout->pf_end = End;
  101.     p_vout->pf_manage = NULL;
  102.     p_vout->pf_render = Render;
  103.     p_vout->pf_display = NULL;
  104.     p_vout->pf_control = Control;
  105.     p_vout->p_sys->i_mode = 0;
  106.     if( !(psz_method = psz_method_tmp
  107.           = config_GetPsz( p_vout, "distort-mode" )) )
  108.     {
  109.         msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
  110.                          "distort-mode" );
  111.         p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
  112.     }
  113.     else
  114.     {
  115.         if( !strcmp( psz_method, "wave" ) )
  116.         {
  117.             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
  118.         }
  119.         else if( !strcmp( psz_method, "ripple" ) )
  120.         {
  121.             p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
  122.         }
  123.         else
  124.         {
  125.             msg_Err( p_vout, "no valid distort mode provided, "
  126.                              "using wave" );
  127.             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
  128.         }
  129.     }
  130.     free( psz_method_tmp );
  131.     return VLC_SUCCESS;
  132. }
  133. /*****************************************************************************
  134.  * Init: initialize Distort video thread output method
  135.  *****************************************************************************/
  136. static int Init( vout_thread_t *p_vout )
  137. {
  138.     int i_index;
  139.     picture_t *p_pic;
  140.     I_OUTPUTPICTURES = 0;
  141.     /* Initialize the output structure */
  142.     p_vout->output.i_chroma = p_vout->render.i_chroma;
  143.     p_vout->output.i_width  = p_vout->render.i_width;
  144.     p_vout->output.i_height = p_vout->render.i_height;
  145.     p_vout->output.i_aspect = p_vout->render.i_aspect;
  146.     /* Try to open the real video output */
  147.     msg_Dbg( p_vout, "spawning the real video output" );
  148.     p_vout->p_sys->p_vout = vout_Create( p_vout,
  149.                            p_vout->render.i_width, p_vout->render.i_height,
  150.                            p_vout->render.i_chroma, p_vout->render.i_aspect );
  151.     /* Everything failed */
  152.     if( p_vout->p_sys->p_vout == NULL )
  153.     {
  154.         msg_Err( p_vout, "cannot open vout, aborting" );
  155.         return VLC_EGENERIC;
  156.     }
  157.     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
  158.     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
  159.     ADD_PARENT_CALLBACKS( SendEventsToChild );
  160.     p_vout->p_sys->f_angle = 0.0;
  161.     p_vout->p_sys->last_date = 0;
  162.     return VLC_SUCCESS;
  163. }
  164. /*****************************************************************************
  165.  * End: terminate Distort video thread output method
  166.  *****************************************************************************/
  167. static void End( vout_thread_t *p_vout )
  168. {
  169.     int i_index;
  170.     /* Free the fake output buffers we allocated */
  171.     for( i_index = I_OUTPUTPICTURES ; i_index ; )
  172.     {
  173.         i_index--;
  174.         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
  175.     }
  176. }
  177. /*****************************************************************************
  178.  * Destroy: destroy Distort video thread output method
  179.  *****************************************************************************
  180.  * Terminate an output method created by DistortCreateOutputMethod
  181.  *****************************************************************************/
  182. static void Destroy( vlc_object_t *p_this )
  183. {
  184.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  185.     if( p_vout->p_sys->p_vout )
  186.     {
  187.         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
  188.         vlc_object_detach( p_vout->p_sys->p_vout );
  189.         vout_Destroy( p_vout->p_sys->p_vout );
  190.     }
  191.     DEL_PARENT_CALLBACKS( SendEventsToChild );
  192.     free( p_vout->p_sys );
  193. }
  194. /*****************************************************************************
  195.  * Render: displays previously rendered output
  196.  *****************************************************************************
  197.  * This function send the currently rendered image to Distort image, waits
  198.  * until it is displayed and switch the two rendering buffers, preparing next
  199.  * frame.
  200.  *****************************************************************************/
  201. static void Render( vout_thread_t *p_vout, picture_t *p_pic )
  202. {
  203.     picture_t *p_outpic;
  204.     /* This is a new frame. Get a structure from the video_output. */
  205.     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
  206.               == NULL )
  207.     {
  208.         if( p_vout->b_die || p_vout->b_error )
  209.         {
  210.             return;
  211.         }
  212.         msleep( VOUT_OUTMEM_SLEEP );
  213.     }
  214.     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
  215.     switch( p_vout->p_sys->i_mode )
  216.     {
  217.         case DISTORT_MODE_WAVE:
  218.             DistortWave( p_vout, p_pic, p_outpic );
  219.             break;
  220.         case DISTORT_MODE_RIPPLE:
  221.             DistortRipple( p_vout, p_pic, p_outpic );
  222.             break;
  223.         default:
  224.             break;
  225.     }
  226.     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
  227. }
  228. /*****************************************************************************
  229.  * DistortWave: draw a wave effect on the picture
  230.  *****************************************************************************/
  231. static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
  232.                                                 picture_t *p_outpic )
  233. {
  234.     int i_index;
  235.     double f_angle;
  236.     mtime_t new_date = mdate();
  237.     p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
  238.     p_vout->p_sys->last_date = new_date;
  239.     f_angle = p_vout->p_sys->f_angle;
  240.     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
  241.     {
  242.         int i_line, i_num_lines, i_offset;
  243.         uint8_t black_pixel;
  244.         uint8_t *p_in, *p_out;
  245.         p_in = p_inpic->p[i_index].p_pixels;
  246.         p_out = p_outpic->p[i_index].p_pixels;
  247.         i_num_lines = p_inpic->p[i_index].i_visible_lines;
  248.         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
  249.         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
  250.         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
  251.         {
  252.             /* Calculate today's offset, don't go above 1/20th of the screen */
  253.             i_offset = (int)( (double)(p_inpic->p[i_index].i_visible_pitch)
  254.                          * sin( f_angle + 10.0 * (double)i_line
  255.                                                / (double)i_num_lines )
  256.                          / 20.0 );
  257.             if( i_offset )
  258.             {
  259.                 if( i_offset < 0 )
  260.                 {
  261.                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
  262.                              p_inpic->p[i_index].i_visible_pitch + i_offset );
  263.                     p_in += p_inpic->p[i_index].i_pitch;
  264.                     p_out += p_outpic->p[i_index].i_pitch;
  265.                     memset( p_out + i_offset, black_pixel, -i_offset );
  266.                 }
  267.                 else
  268.                 {
  269.                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
  270.                              p_inpic->p[i_index].i_visible_pitch - i_offset );
  271.                     memset( p_out, black_pixel, i_offset );
  272.                     p_in += p_inpic->p[i_index].i_pitch;
  273.                     p_out += p_outpic->p[i_index].i_pitch;
  274.                 }
  275.             }
  276.             else
  277.             {
  278.                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
  279.                                           p_inpic->p[i_index].i_visible_pitch );
  280.                 p_in += p_inpic->p[i_index].i_pitch;
  281.                 p_out += p_outpic->p[i_index].i_pitch;
  282.             }
  283.         }
  284.     }
  285. }
  286. /*****************************************************************************
  287.  * DistortRipple: draw a ripple effect at the bottom of the picture
  288.  *****************************************************************************/
  289. static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
  290.                                                   picture_t *p_outpic )
  291. {
  292.     int i_index;
  293.     double f_angle;
  294.     mtime_t new_date = mdate();
  295.     p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
  296.     p_vout->p_sys->last_date = new_date;
  297.     f_angle = p_vout->p_sys->f_angle;
  298.     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
  299.     {
  300.         int i_line, i_first_line, i_num_lines, i_offset;
  301.         uint8_t black_pixel;
  302.         uint8_t *p_in, *p_out;
  303.         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
  304.         i_num_lines = p_inpic->p[i_index].i_visible_lines;
  305.         i_first_line = i_num_lines * 4 / 5;
  306.         p_in = p_inpic->p[i_index].p_pixels;
  307.         p_out = p_outpic->p[i_index].p_pixels;
  308.         for( i_line = 0 ; i_line < i_first_line ; i_line++ )
  309.         {
  310.             p_vout->p_vlc->pf_memcpy( p_out, p_in,
  311.                                       p_inpic->p[i_index].i_visible_pitch );
  312.             p_in += p_inpic->p[i_index].i_pitch;
  313.             p_out += p_outpic->p[i_index].i_pitch;
  314.         }
  315.         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
  316.         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
  317.         {
  318.             /* Calculate today's offset, don't go above 1/20th of the screen */
  319.             i_offset = (int)( (double)(p_inpic->p[i_index].i_pitch)
  320.                          * sin( f_angle + 2.0 * (double)i_line
  321.                                               / (double)( 1 + i_line
  322.                                                             - i_first_line) )
  323.                          * (double)(i_line - i_first_line)
  324.                          / (double)i_num_lines
  325.                          / 8.0 );
  326.             if( i_offset )
  327.             {
  328.                 if( i_offset < 0 )
  329.                 {
  330.                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
  331.                              p_inpic->p[i_index].i_visible_pitch + i_offset );
  332.                     p_in -= p_inpic->p[i_index].i_pitch;
  333.                     p_out += p_outpic->p[i_index].i_pitch;
  334.                     memset( p_out + i_offset, black_pixel, -i_offset );
  335.                 }
  336.                 else
  337.                 {
  338.                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
  339.                              p_inpic->p[i_index].i_visible_pitch - i_offset );
  340.                     memset( p_out, black_pixel, i_offset );
  341.                     p_in -= p_inpic->p[i_index].i_pitch;
  342.                     p_out += p_outpic->p[i_index].i_pitch;
  343.                 }
  344.             }
  345.             else
  346.             {
  347.                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
  348.                                           p_inpic->p[i_index].i_visible_pitch );
  349.                 p_in -= p_inpic->p[i_index].i_pitch;
  350.                 p_out += p_outpic->p[i_index].i_pitch;
  351.             }
  352.         }
  353.     }
  354. }
  355. /*****************************************************************************
  356.  * SendEvents: forward mouse and keyboard events to the parent p_vout
  357.  *****************************************************************************/
  358. static int SendEvents( vlc_object_t *p_this, char const *psz_var,
  359.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  360. {
  361.     var_Set( (vlc_object_t *)p_data, psz_var, newval );
  362.     return VLC_SUCCESS;
  363. }
  364. /*****************************************************************************
  365.  * SendEventsToChild: forward events to the child/children vout
  366.  *****************************************************************************/
  367. static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
  368.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  369. {
  370.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  371.     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
  372.     return VLC_SUCCESS;
  373. }