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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * noise.c : "add grain to image" video filter
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2007 the VideoLAN team
  5.  * $Id: 28f54eb813ffb07c3c3a5088f0ed5f671e99b48a $
  6.  *
  7.  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_vout.h>
  32. #include "vlc_filter.h"
  33. #include "filter_picture.h"
  34. /*****************************************************************************
  35.  * Local prototypes
  36.  *****************************************************************************/
  37. static int  Create    ( vlc_object_t * );
  38. static void Destroy   ( vlc_object_t * );
  39. static picture_t *Filter( filter_t *, picture_t * );
  40. #define FILTER_PREFIX "grain-"
  41. /*****************************************************************************
  42.  * Module descriptor
  43.  *****************************************************************************/
  44. vlc_module_begin ()
  45.     set_description( N_("Grain video filter") )
  46.     set_shortname( N_( "Grain" ))
  47.     set_capability( "video filter2", 0 )
  48.     set_category( CAT_VIDEO )
  49.     set_subcategory( SUBCAT_VIDEO_VFILTER )
  50.     set_callbacks( Create, Destroy )
  51. vlc_module_end ()
  52. struct filter_sys_t
  53. {
  54.     int *p_noise;
  55. };
  56. static int Create( vlc_object_t *p_this )
  57. {
  58.     filter_t *p_filter = (filter_t *)p_this;
  59.     switch( p_filter->fmt_in.video.i_chroma )
  60.     {
  61.         CASE_PLANAR_YUV
  62.             break;
  63.         default:
  64.             msg_Err( p_filter, "Unsupported input chroma (%4s)",
  65.                      (char*)&(p_filter->fmt_in.video.i_chroma) );
  66.             return VLC_EGENERIC;
  67.     }
  68.     /* Allocate structure */
  69.     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  70.     if( p_filter->p_sys == NULL )
  71.         return VLC_ENOMEM;
  72.     p_filter->pf_video_filter = Filter;
  73.     p_filter->p_sys->p_noise = NULL;
  74.     return VLC_SUCCESS;
  75. }
  76. static void Destroy( vlc_object_t *p_this )
  77. {
  78.     filter_t *p_filter = (filter_t *)p_this;
  79.     free( p_filter->p_sys->p_noise );
  80.     free( p_filter->p_sys );
  81. }
  82. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  83. {
  84.     picture_t *p_outpic;
  85.     filter_sys_t *p_sys = p_filter->p_sys;
  86.     int i_index;
  87.     if( !p_pic ) return NULL;
  88.     p_outpic = filter_NewPicture( p_filter );
  89.     if( !p_outpic )
  90.     {
  91.         picture_Release( p_pic );
  92.         return NULL;
  93.     }
  94.     {
  95.         uint8_t *p_in = p_pic->p[Y_PLANE].p_pixels;
  96.         uint8_t *p_out = p_outpic->p[Y_PLANE].p_pixels;
  97.         const int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
  98.         const int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
  99.         const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
  100.         int i_line, i_col;
  101.         int *p_noise = p_sys->p_noise;
  102.         if( !p_noise )
  103.         {
  104.             p_noise = p_sys->p_noise =
  105.                 (int*)malloc(i_pitch*i_num_lines*sizeof(int));
  106.         }
  107.         for( i_line = 0; i_line < i_num_lines; i_line++ )
  108.         {
  109.             for( i_col = 0; i_col < i_num_cols; i_col++ )
  110.             {
  111.                 p_noise[i_line*i_pitch+i_col] = ((rand()&0x1f)-0x0f);
  112.             }
  113.         }
  114.         for( i_line = 2/*0*/ ; i_line < i_num_lines-2/**/; i_line++ )
  115.         {
  116.             for( i_col = 2/*0*/; i_col < i_num_cols/2; i_col++ )
  117.             {
  118.                 p_out[i_line*i_pitch+i_col] = clip_uint8_vlc(
  119.                           p_in[i_line*i_pitch+i_col]
  120. #if 0
  121.                         + p_noise[i_line*i_pitch+i_col] );
  122. #else
  123. /* 2 rows up */
  124.               + ((  ( p_noise[(i_line-2)*i_pitch+i_col-2]<<1 )
  125.               + ( p_noise[(i_line-2)*i_pitch+i_col-1]<<2 )
  126.               + ( p_noise[(i_line-2)*i_pitch+i_col]<<2 )
  127.               + ( p_noise[(i_line-2)*i_pitch+i_col+1]<<2 )
  128.               + ( p_noise[(i_line-2)*i_pitch+i_col+2]<<1 )
  129.               /* 1 row up */
  130.               + ( p_noise[(i_line-1)*i_pitch+i_col-2]<<2 )
  131.               + ( p_noise[(i_line-1)*i_pitch+i_col-1]<<3 )
  132.               + ( p_noise[(i_line-1)*i_pitch+i_col]*12 )
  133.               + ( p_noise[(i_line-1)*i_pitch+i_col+1]<<3 )
  134.               + ( p_noise[(i_line-1)*i_pitch+i_col+2]<<2 )
  135.               /* */
  136.               + ( p_noise[i_line*i_pitch+i_col-2]<<2 )
  137.               + ( p_noise[i_line*i_pitch+i_col-1]*12 )
  138.               + ( p_noise[i_line*i_pitch+i_col]<<4 )
  139.               + ( p_noise[i_line*i_pitch+i_col+1]*12 )
  140.               + ( p_noise[i_line*i_pitch+i_col+2]<<2 )
  141.               /* 1 row down */
  142.               + ( p_noise[(i_line+1)*i_pitch+i_col-2]<<2 )
  143.               + ( p_noise[(i_line+1)*i_pitch+i_col-1]<<3 )
  144.               + ( p_noise[(i_line+1)*i_pitch+i_col]*12 )
  145.               + ( p_noise[(i_line+1)*i_pitch+i_col+1]<<3 )
  146.               + ( p_noise[(i_line+1)*i_pitch+i_col+2]<<2 )
  147.               /* 2 rows down */
  148.               + ( p_noise[(i_line+2)*i_pitch+i_col-2]<<1 )
  149.               + ( p_noise[(i_line+2)*i_pitch+i_col-1]<<2 )
  150.               + ( p_noise[(i_line+2)*i_pitch+i_col]<<2 )
  151.               + ( p_noise[(i_line+2)*i_pitch+i_col+1]<<2 )
  152.               + ( p_noise[(i_line+2)*i_pitch+i_col+2]<<1 )
  153.               )>>7/*/152*/));
  154. #endif
  155.             }
  156.             for( ; i_col < i_num_cols; i_col++ )
  157.                 p_out[i_line*i_pitch+i_col] = p_in[i_line*i_pitch+i_col];
  158.         }
  159.     }
  160.     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
  161.     {
  162.         uint8_t *p_in = p_pic->p[i_index].p_pixels;
  163.         uint8_t *p_out = p_outpic->p[i_index].p_pixels;
  164.         const int i_lines = p_pic->p[i_index].i_lines;
  165.         const int i_pitch = p_pic->p[i_index].i_pitch;
  166.         vlc_memcpy( p_out, p_in, i_lines * i_pitch );
  167.     }
  168.     return CopyInfoAndRelease( p_outpic, p_pic );
  169. }