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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * Psychedelic.c : Psychedelic video effect plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2006 the VideoLAN team
  5.  * $Id: 2e1c53c36acbb924206f16db2b97e675cdcd2ae7 $
  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_filter.h"
  34. #include "vlc_image.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_("Psychedelic video filter") )
  47.     set_shortname( N_( "Psychedelic" ))
  48.     set_capability( "video filter2", 0 )
  49.     set_category( CAT_VIDEO )
  50.     set_subcategory( SUBCAT_VIDEO_VFILTER )
  51.     add_shortcut( "psychedelic" )
  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.     image_handler_t *p_image;
  63.     unsigned int x, y, scale;
  64.     int xinc, yinc, scaleinc;
  65.     uint8_t u,v;
  66. };
  67. /*****************************************************************************
  68.  * Create: allocates Distort video thread output method
  69.  *****************************************************************************
  70.  * This function allocates and initializes a Distort vout method.
  71.  *****************************************************************************/
  72. static int Create( vlc_object_t *p_this )
  73. {
  74.     filter_t *p_filter = (filter_t *)p_this;
  75.     /* Allocate structure */
  76.     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  77.     if( p_filter->p_sys == NULL )
  78.         return VLC_ENOMEM;
  79.     p_filter->pf_video_filter = Filter;
  80.     p_filter->p_sys->x = 10;
  81.     p_filter->p_sys->y = 10;
  82.     p_filter->p_sys->scale = 1;
  83.     p_filter->p_sys->xinc = 1;
  84.     p_filter->p_sys->yinc = 1;
  85.     p_filter->p_sys->scaleinc = 1;
  86.     p_filter->p_sys->u = 0;
  87.     p_filter->p_sys->v = 0;
  88.     p_filter->p_sys->p_image = NULL;
  89.     return VLC_SUCCESS;
  90. }
  91. /*****************************************************************************
  92.  * Destroy: destroy Distort video thread output method
  93.  *****************************************************************************
  94.  * Terminate an output method created by DistortCreateOutputMethod
  95.  *****************************************************************************/
  96. static void Destroy( vlc_object_t *p_this )
  97. {
  98.     filter_t *p_filter = (filter_t *)p_this;
  99.     if( p_filter->p_sys->p_image )
  100.         image_HandlerDelete( p_filter->p_sys->p_image );
  101.     p_filter->p_sys->p_image = NULL;
  102.     free( p_filter->p_sys );
  103. }
  104. /*****************************************************************************
  105.  * Render: displays previously rendered output
  106.  *****************************************************************************
  107.  * This function send the currently rendered image to Distort image, waits
  108.  * until it is displayed and switch the two rendering buffers, preparing next
  109.  * frame.
  110.  *****************************************************************************/
  111. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  112. {
  113.     picture_t *p_outpic;
  114.     unsigned int w, h;
  115.     int x,y;
  116.     uint8_t u,v;
  117.     picture_t *p_converted;
  118.     video_format_t fmt_out;
  119.     memset( &fmt_out, 0, sizeof(video_format_t) );
  120.     fmt_out.p_palette = NULL;
  121.     if( !p_pic ) return NULL;
  122.     p_outpic = filter_NewPicture( p_filter );
  123.     if( !p_outpic )
  124.     {
  125.         picture_Release( p_pic );
  126.         return NULL;
  127.     }
  128.     if( !p_filter->p_sys->p_image )
  129.         p_filter->p_sys->p_image = image_HandlerCreate( p_filter );
  130.     /* chrominance */
  131.     u = p_filter->p_sys->u;
  132.     v = p_filter->p_sys->v;
  133.     for( y = 0; y<p_outpic->p[U_PLANE].i_lines; y++)
  134.     {
  135.         vlc_memset(
  136.                 p_outpic->p[U_PLANE].p_pixels+y*p_outpic->p[U_PLANE].i_pitch,
  137.                 u, p_outpic->p[U_PLANE].i_pitch );
  138.         vlc_memset(
  139.                 p_outpic->p[V_PLANE].p_pixels+y*p_outpic->p[V_PLANE].i_pitch,
  140.                 v, p_outpic->p[V_PLANE].i_pitch );
  141.         if( v == 0 && u != 0 )
  142.             u --;
  143.         else if( u == 0xff )
  144.             v --;
  145.         else if( v == 0xff )
  146.             u ++;
  147.         else if( u == 0 )
  148.             v ++;
  149.     }
  150.     /* luminance */
  151.     vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_pic->p[Y_PLANE].p_pixels,
  152.                 p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
  153.     /* image visualization */
  154.     fmt_out = p_filter->fmt_out.video;
  155.     fmt_out.i_width = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
  156.     fmt_out.i_height = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
  157.     p_converted = image_Convert( p_filter->p_sys->p_image, p_pic,
  158.                                  &(p_pic->format), &fmt_out );
  159.     if( p_converted )
  160.     {
  161. #define copyimage( plane, b ) 
  162.         for( y=0; y<p_converted->p[plane].i_visible_lines; y++) { 
  163.         for( x=0; x<p_converted->p[plane].i_visible_pitch; x++) { 
  164.             int nx, ny; 
  165.             if( p_filter->p_sys->yinc == 1 ) 
  166.                 ny= y; 
  167.             else 
  168.                 ny = p_converted->p[plane].i_visible_lines-y; 
  169.             if( p_filter->p_sys->xinc == 1 ) 
  170.                 nx = x; 
  171.             else 
  172.                 nx = p_converted->p[plane].i_visible_pitch-x; 
  173.             p_outpic->p[plane].p_pixels[(p_filter->p_sys->x*b+nx)+(ny+p_filter->p_sys->y*b)*p_outpic->p[plane].i_pitch ] = p_converted->p[plane].p_pixels[y*p_converted->p[plane].i_pitch+x]; 
  174.         } }
  175.         copyimage( Y_PLANE, 2 );
  176.         copyimage( U_PLANE, 1 );
  177.         copyimage( V_PLANE, 1 );
  178. #undef copyimage
  179.         picture_Release( p_converted );
  180.     }
  181.     else
  182.     {
  183.         msg_Err( p_filter, "Image scaling failed miserably." );
  184.     }
  185.     p_filter->p_sys->x += p_filter->p_sys->xinc;
  186.     p_filter->p_sys->y += p_filter->p_sys->yinc;
  187.     p_filter->p_sys->scale += p_filter->p_sys->scaleinc;
  188.     if( p_filter->p_sys->scale >= 50 ) p_filter->p_sys->scaleinc = -1;
  189.     if( p_filter->p_sys->scale <= 1 ) p_filter->p_sys->scaleinc = 1;
  190.     w = p_filter->fmt_out.video.i_width*p_filter->p_sys->scale/150;
  191.     h = p_filter->fmt_out.video.i_height*p_filter->p_sys->scale/150;
  192.     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
  193.         p_filter->p_sys->xinc = -1;
  194.     if( p_filter->p_sys->x <= 0 )
  195.         p_filter->p_sys->xinc = 1;
  196.     if( p_filter->p_sys->x*2 + w >= p_filter->fmt_out.video.i_width )
  197.         p_filter->p_sys->x = (p_filter->fmt_out.video.i_width-w)/2;
  198.     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
  199.         p_filter->p_sys->y = (p_filter->fmt_out.video.i_height-h)/2;
  200.     if( p_filter->p_sys->y*2 + h >= p_filter->fmt_out.video.i_height )
  201.         p_filter->p_sys->yinc = -1;
  202.     if( p_filter->p_sys->y <= 0 )
  203.         p_filter->p_sys->yinc = 1;
  204.     for( y = 0; y< 16; y++ )
  205.     {
  206.         if( p_filter->p_sys->v == 0 && p_filter->p_sys->u != 0 )
  207.             p_filter->p_sys->u -= 1;
  208.         else if( p_filter->p_sys->u == 0xff )
  209.             p_filter->p_sys->v -= 1;
  210.         else if( p_filter->p_sys->v == 0xff )
  211.             p_filter->p_sys->u += 1;
  212.         else if( p_filter->p_sys->u == 0 )
  213.             p_filter->p_sys->v += 1;
  214.     }
  215.     return CopyInfoAndRelease( p_outpic, p_pic );
  216. }