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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * ripple.c : Ripple video effect plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2006 the VideoLAN team
  5.  * $Id: 6eab4f1c17fbf0a28c01b8f608edf507dcb0f181 $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *          Antoine Cellerier <dionoea -at- videolan -dot- 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. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <math.h>                                            /* sin(), cos() */
  31. #include <vlc_common.h>
  32. #include <vlc_plugin.h>
  33. #include <vlc_vout.h>
  34. #include <vlc_filter.h>
  35. #include "filter_picture.h"
  36. /*****************************************************************************
  37.  * Local prototypes
  38.  *****************************************************************************/
  39. static int  Create    ( vlc_object_t * );
  40. static void Destroy   ( vlc_object_t * );
  41. static picture_t *Filter( filter_t *, picture_t * );
  42. /*****************************************************************************
  43.  * Module descriptor
  44.  *****************************************************************************/
  45. vlc_module_begin ()
  46.     set_description( N_("Ripple video filter") )
  47.     set_shortname( N_( "Ripple" ))
  48.     set_capability( "video filter2", 0 )
  49.     set_category( CAT_VIDEO )
  50.     set_subcategory( SUBCAT_VIDEO_VFILTER )
  51.     add_shortcut( "ripple" )
  52.     set_callbacks( Create, Destroy )
  53. vlc_module_end ()
  54. /*****************************************************************************
  55.  * vout_sys_t: Distort video output method descriptor
  56.  *****************************************************************************
  57.  * This structure is part of the video output thread descriptor.
  58.  * It describes the Distort specific properties of an output thread.
  59.  *****************************************************************************/
  60. struct filter_sys_t
  61. {
  62.     double  f_angle;
  63.     mtime_t last_date;
  64. };
  65. /*****************************************************************************
  66.  * Create: allocates Distort video thread output method
  67.  *****************************************************************************
  68.  * This function allocates and initializes a Distort vout method.
  69.  *****************************************************************************/
  70. static int Create( vlc_object_t *p_this )
  71. {
  72.     filter_t *p_filter = (filter_t *)p_this;
  73.     /* Allocate structure */
  74.     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  75.     if( p_filter->p_sys == NULL )
  76.         return VLC_ENOMEM;
  77.     p_filter->pf_video_filter = Filter;
  78.     p_filter->p_sys->f_angle = 0.0;
  79.     p_filter->p_sys->last_date = 0;
  80.     return VLC_SUCCESS;
  81. }
  82. /*****************************************************************************
  83.  * Destroy: destroy Distort video thread output method
  84.  *****************************************************************************
  85.  * Terminate an output method created by DistortCreateOutputMethod
  86.  *****************************************************************************/
  87. static void Destroy( vlc_object_t *p_this )
  88. {
  89.     filter_t *p_filter = (filter_t *)p_this;
  90.     free( p_filter->p_sys );
  91. }
  92. /*****************************************************************************
  93.  * Render: displays previously rendered output
  94.  *****************************************************************************
  95.  * This function send the currently rendered image to Distort image, waits
  96.  * until it is displayed and switch the two rendering buffers, preparing next
  97.  * frame.
  98.  *****************************************************************************/
  99. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  100. {
  101.     picture_t *p_outpic;
  102.     int i_index;
  103.     double f_angle;
  104.     mtime_t new_date = mdate();
  105.     if( !p_pic ) return NULL;
  106.     p_outpic = filter_NewPicture( p_filter );
  107.     if( !p_outpic )
  108.     {
  109.         picture_Release( p_pic );
  110.         return NULL;
  111.     }
  112.     p_filter->p_sys->f_angle -= (p_filter->p_sys->last_date - new_date) / 100000.0;
  113.     p_filter->p_sys->last_date = new_date;
  114.     f_angle = p_filter->p_sys->f_angle;
  115.     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
  116.     {
  117.         int i_line, i_first_line, i_num_lines, i_offset, i_pixel_pitch,
  118.             i_visible_pixels;
  119.         uint8_t black_pixel;
  120.         uint8_t *p_in, *p_out;
  121.         black_pixel = ( p_pic->i_planes > 1 && i_index == Y_PLANE ) ? 0x00
  122.                                                                     : 0x80;
  123.         i_num_lines = p_pic->p[i_index].i_visible_lines;
  124.         i_pixel_pitch = p_pic->p[i_index].i_pixel_pitch;
  125.         i_visible_pixels = p_pic->p[i_index].i_visible_pitch/p_pic->p[i_index].i_pixel_pitch;
  126.         i_first_line = i_num_lines * 4 / 5;
  127.         p_in = p_pic->p[i_index].p_pixels;
  128.         p_out = p_outpic->p[i_index].p_pixels;
  129.         for( i_line = 0 ; i_line < i_first_line ; i_line++ )
  130.         {
  131.             vlc_memcpy( p_out, p_in, p_pic->p[i_index].i_visible_pitch );
  132.             p_in += p_pic->p[i_index].i_pitch;
  133.             p_out += p_outpic->p[i_index].i_pitch;
  134.         }
  135.         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
  136.         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
  137.         {
  138.             /* Calculate today's offset, don't go above 1/20th of the screen */
  139.             i_offset = (int)( (double)(i_visible_pixels)
  140.                          * sin( f_angle + 2.0 * (double)i_line
  141.                                               / (double)( 1 + i_line
  142.                                                             - i_first_line) )
  143.                          * (double)(i_line - i_first_line)
  144.                          / (double)i_num_lines
  145.                          / 8.0 )*p_pic->p[i_index].i_pixel_pitch;
  146.             if( i_offset )
  147.             {
  148.                 if( i_offset < 0 )
  149.                 {
  150.                     vlc_memcpy( p_out, p_in - i_offset,
  151.                                 p_pic->p[i_index].i_visible_pitch + i_offset );
  152.                     p_in -= p_pic->p[i_index].i_pitch;
  153.                     p_out += p_outpic->p[i_index].i_pitch;
  154.                     vlc_memset( p_out + i_offset, black_pixel, -i_offset );
  155.                 }
  156.                 else
  157.                 {
  158.                     vlc_memcpy( p_out + i_offset, p_in,
  159.                                 p_pic->p[i_index].i_visible_pitch - i_offset );
  160.                     vlc_memset( p_out, black_pixel, i_offset );
  161.                     p_in -= p_pic->p[i_index].i_pitch;
  162.                     p_out += p_outpic->p[i_index].i_pitch;
  163.                 }
  164.             }
  165.             else
  166.             {
  167.                 vlc_memcpy( p_out, p_in, p_pic->p[i_index].i_visible_pitch );
  168.                 p_in -= p_pic->p[i_index].i_pitch;
  169.                 p_out += p_outpic->p[i_index].i_pitch;
  170.             }
  171.         }
  172.     }
  173.     return CopyInfoAndRelease( p_outpic, p_pic );
  174. }