1 /*****************************************************************************
2 * noise.c : "add noise to image" video filter
3 *****************************************************************************
4 * Copyright (C) 2000-2006 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 *****************************************************************************/
33 #include <vlc_plugin.h>
36 #include "vlc_filter.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
44 static picture_t *Filter( filter_t *, picture_t * );
46 #define FILTER_PREFIX "noise-"
48 /*****************************************************************************
50 *****************************************************************************/
52 set_description( _("Noise video filter") );
53 set_shortname( _( "Noise" ));
54 set_capability( "video filter2", 0 );
55 set_category( CAT_VIDEO );
56 set_subcategory( SUBCAT_VIDEO_VFILTER );
58 add_shortcut( "noise" );
59 set_callbacks( Create, Destroy );
62 static const char *ppsz_filter_options[] = {
66 /*****************************************************************************
67 * vout_sys_t: Distort video output method descriptor
68 *****************************************************************************
69 * This structure is part of the video output thread descriptor.
70 * It describes the Distort specific properties of an output thread.
71 *****************************************************************************/
77 /*****************************************************************************
78 * Create: allocates Distort video thread output method
79 *****************************************************************************
80 * This function allocates and initializes a Distort vout method.
81 *****************************************************************************/
82 static int Create( vlc_object_t *p_this )
84 filter_t *p_filter = (filter_t *)p_this;
86 /* Allocate structure */
87 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
88 if( p_filter->p_sys == NULL )
90 msg_Err( p_filter, "out of memory" );
94 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
97 p_filter->pf_video_filter = Filter;
99 p_filter->p_sys->last_date = 0;
104 /*****************************************************************************
105 * Destroy: destroy Distort video thread output method
106 *****************************************************************************
107 * Terminate an output method created by DistortCreateOutputMethod
108 *****************************************************************************/
109 static void Destroy( vlc_object_t *p_this )
111 filter_t *p_filter = (filter_t *)p_this;
112 free( p_filter->p_sys );
115 /*****************************************************************************
116 * Render: displays previously rendered output
117 *****************************************************************************
118 * This function send the currently rendered image to Distort image, waits
119 * until it is displayed and switch the two rendering buffers, preparing next
121 *****************************************************************************/
122 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
125 filter_sys_t *p_sys = p_filter->p_sys;
127 mtime_t new_date = mdate();
129 if( !p_pic ) return NULL;
131 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
134 msg_Warn( p_filter, "can't get output picture" );
135 if( p_pic->pf_release )
136 p_pic->pf_release( p_pic );
140 p_sys->last_date = new_date;
142 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
144 uint8_t *p_in = p_pic->p[i_index].p_pixels;
145 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
147 const int i_num_lines = p_pic->p[i_index].i_visible_lines;
148 const int i_num_cols = p_pic->p[i_index].i_visible_pitch;
149 const int i_pitch = p_pic->p[i_index].i_pitch;
153 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
157 /* line isn't noisy */
158 vlc_memcpy( p_out+i_line*i_pitch, p_in+i_line*i_pitch,
163 /* this line is noisy */
164 int noise_level = rand()%8+2;
165 for( i_col = 0; i_col < i_num_cols ; i_col++ )
167 if( rand()%noise_level )
169 p_out[i_line*i_pitch+i_col] =
170 p_in[i_line*i_pitch+i_col];
174 p_out[i_line*i_pitch+i_col] = (rand()%3)*0x7f;
181 p_outpic->date = p_pic->date;
182 p_outpic->b_force = p_pic->b_force;
183 p_outpic->i_nb_fields = p_pic->i_nb_fields;
184 p_outpic->b_progressive = p_pic->b_progressive;
185 p_outpic->b_top_field_first = p_pic->b_top_field_first;
187 if( p_pic->pf_release )
188 p_pic->pf_release( p_pic );