1 /*****************************************************************************
2 * noise.c : "add grain to image" video filter
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37 #include "filter_picture.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
45 static picture_t *Filter( filter_t *, picture_t * );
47 #define FILTER_PREFIX "grain-"
49 #define NOISE_HELP N_("add grain to image")
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( N_("Grain video filter") )
55 set_shortname( N_( "Grain" ))
57 set_capability( "video filter2", 0 )
58 set_category( CAT_VIDEO )
59 set_subcategory( SUBCAT_VIDEO_VFILTER )
61 set_callbacks( Create, Destroy )
69 static int Create( vlc_object_t *p_this )
71 filter_t *p_filter = (filter_t *)p_this;
73 switch( p_filter->fmt_in.video.i_chroma )
79 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
80 (char*)&(p_filter->fmt_in.video.i_chroma) );
84 /* Allocate structure */
85 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
86 if( p_filter->p_sys == NULL )
89 p_filter->pf_video_filter = Filter;
91 p_filter->p_sys->p_noise = NULL;
96 static void Destroy( vlc_object_t *p_this )
98 filter_t *p_filter = (filter_t *)p_this;
99 free( p_filter->p_sys->p_noise );
100 free( p_filter->p_sys );
103 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
106 filter_sys_t *p_sys = p_filter->p_sys;
109 if( !p_pic ) return NULL;
111 p_outpic = filter_NewPicture( p_filter );
114 picture_Release( p_pic );
119 uint8_t *p_in = p_pic->p[Y_PLANE].p_pixels;
120 uint8_t *p_out = p_outpic->p[Y_PLANE].p_pixels;
122 const int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
123 const int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
124 const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
128 int *p_noise = p_sys->p_noise;
131 p_noise = p_sys->p_noise =
132 (int*)malloc(i_pitch*i_num_lines*sizeof(int));
135 for( i_line = 0; i_line < i_num_lines; i_line++ )
137 for( i_col = 0; i_col < i_num_cols; i_col++ )
139 p_noise[i_line*i_pitch+i_col] = ((vlc_mrand48()&0x1f)-0x0f);
143 for( i_line = 2/*0*/ ; i_line < i_num_lines-2/**/; i_line++ )
145 for( i_col = 2/*0*/; i_col < i_num_cols/2; i_col++ )
147 p_out[i_line*i_pitch+i_col] = clip_uint8_vlc(
148 p_in[i_line*i_pitch+i_col]
150 + p_noise[i_line*i_pitch+i_col] );
153 + (( ( p_noise[(i_line-2)*i_pitch+i_col-2]<<1 )
154 + ( p_noise[(i_line-2)*i_pitch+i_col-1]<<2 )
155 + ( p_noise[(i_line-2)*i_pitch+i_col]<<2 )
156 + ( p_noise[(i_line-2)*i_pitch+i_col+1]<<2 )
157 + ( p_noise[(i_line-2)*i_pitch+i_col+2]<<1 )
159 + ( p_noise[(i_line-1)*i_pitch+i_col-2]<<2 )
160 + ( p_noise[(i_line-1)*i_pitch+i_col-1]<<3 )
161 + ( p_noise[(i_line-1)*i_pitch+i_col]*12 )
162 + ( p_noise[(i_line-1)*i_pitch+i_col+1]<<3 )
163 + ( p_noise[(i_line-1)*i_pitch+i_col+2]<<2 )
165 + ( p_noise[i_line*i_pitch+i_col-2]<<2 )
166 + ( p_noise[i_line*i_pitch+i_col-1]*12 )
167 + ( p_noise[i_line*i_pitch+i_col]<<4 )
168 + ( p_noise[i_line*i_pitch+i_col+1]*12 )
169 + ( p_noise[i_line*i_pitch+i_col+2]<<2 )
171 + ( p_noise[(i_line+1)*i_pitch+i_col-2]<<2 )
172 + ( p_noise[(i_line+1)*i_pitch+i_col-1]<<3 )
173 + ( p_noise[(i_line+1)*i_pitch+i_col]*12 )
174 + ( p_noise[(i_line+1)*i_pitch+i_col+1]<<3 )
175 + ( p_noise[(i_line+1)*i_pitch+i_col+2]<<2 )
177 + ( p_noise[(i_line+2)*i_pitch+i_col-2]<<1 )
178 + ( p_noise[(i_line+2)*i_pitch+i_col-1]<<2 )
179 + ( p_noise[(i_line+2)*i_pitch+i_col]<<2 )
180 + ( p_noise[(i_line+2)*i_pitch+i_col+1]<<2 )
181 + ( p_noise[(i_line+2)*i_pitch+i_col+2]<<1 )
186 for( ; i_col < i_num_cols; i_col++ )
187 p_out[i_line*i_pitch+i_col] = p_in[i_line*i_pitch+i_col];
191 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
193 uint8_t *p_in = p_pic->p[i_index].p_pixels;
194 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
196 const int i_lines = p_pic->p[i_index].i_lines;
197 const int i_pitch = p_pic->p[i_index].i_pitch;
199 vlc_memcpy( p_out, p_in, i_lines * i_pitch );
203 return CopyInfoAndRelease( p_outpic, p_pic );