8562f5e177ff68e92d1c5c332ed5a02989494711
[vlc.git] / modules / video_filter / noise.c
1 /*****************************************************************************
2  * noise.c : "add noise to image" video filter
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc_vout.h>
34
35 #include "vlc_filter.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static picture_t *Filter( filter_t *, picture_t * );
44
45 #define FILTER_PREFIX "noise-"
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 vlc_module_begin();
51     set_description( _("Noise video filter") );
52     set_shortname( _( "Noise" ));
53     set_capability( "video filter2", 0 );
54     set_category( CAT_VIDEO );
55     set_subcategory( SUBCAT_VIDEO_VFILTER );
56
57     add_shortcut( "noise" );
58     set_callbacks( Create, Destroy );
59 vlc_module_end();
60
61 static const char *ppsz_filter_options[] = {
62     NULL
63 };
64
65 /*****************************************************************************
66  * vout_sys_t: Distort video output method descriptor
67  *****************************************************************************
68  * This structure is part of the video output thread descriptor.
69  * It describes the Distort specific properties of an output thread.
70  *****************************************************************************/
71 struct filter_sys_t
72 {
73     mtime_t last_date;
74 };
75
76 /*****************************************************************************
77  * Create: allocates Distort video thread output method
78  *****************************************************************************
79  * This function allocates and initializes a Distort vout method.
80  *****************************************************************************/
81 static int Create( vlc_object_t *p_this )
82 {
83     filter_t *p_filter = (filter_t *)p_this;
84
85     /* Allocate structure */
86     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
87     if( p_filter->p_sys == NULL )
88     {
89         msg_Err( p_filter, "out of memory" );
90         return VLC_ENOMEM;
91     }
92
93     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
94                        p_filter->p_cfg );
95
96     p_filter->pf_video_filter = Filter;
97
98     p_filter->p_sys->last_date = 0;
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Destroy: destroy Distort video thread output method
105  *****************************************************************************
106  * Terminate an output method created by DistortCreateOutputMethod
107  *****************************************************************************/
108 static void Destroy( vlc_object_t *p_this )
109 {
110     filter_t *p_filter = (filter_t *)p_this;
111     free( p_filter->p_sys );
112 }
113
114 /*****************************************************************************
115  * Render: displays previously rendered output
116  *****************************************************************************
117  * This function send the currently rendered image to Distort image, waits
118  * until it is displayed and switch the two rendering buffers, preparing next
119  * frame.
120  *****************************************************************************/
121 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
122 {
123     picture_t *p_outpic;
124     filter_sys_t *p_sys = p_filter->p_sys;
125     int i_index;
126     mtime_t new_date = mdate();
127
128     if( !p_pic ) return NULL;
129
130     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
131     if( !p_outpic )
132     {
133         msg_Warn( p_filter, "can't get output picture" );
134         if( p_pic->pf_release )
135             p_pic->pf_release( p_pic );
136         return NULL;
137     }
138
139     p_sys->last_date = new_date;
140
141     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
142     {
143         int i_line, i_num_lines, i_col, i_num_cols;
144         uint8_t *p_in, *p_out;
145
146         p_in = p_pic->p[i_index].p_pixels;
147         p_out = p_outpic->p[i_index].p_pixels;
148
149         i_num_lines = p_pic->p[i_index].i_visible_lines;
150         i_num_cols = p_pic->p[i_index].i_visible_pitch;
151
152         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
153         {
154             if( rand()%8 )
155             {
156                 /* line isn't noisy */
157                 p_filter->p_libvlc->pf_memcpy( p_out+i_line*i_num_cols,
158                                                p_in+i_line*i_num_cols,
159                                                i_num_cols );
160             }
161             else
162             {
163                 /* this line is noisy */
164                 int noise_level = rand()%8+2;
165                 for( i_col = 0; i_col < i_num_cols ; i_col++ )
166                 {
167                     if( rand()%noise_level )
168                     {
169                         p_out[i_line*i_num_cols+i_col] =
170                             p_in[i_line*i_num_cols+i_col];
171                     }
172                     else
173                     {
174                         p_out[i_line*i_num_cols+i_col] = (rand()%3)*0x7f;
175                     }
176                 }
177             }
178         }
179     }
180
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;
186
187     if( p_pic->pf_release )
188         p_pic->pf_release( p_pic );
189
190     return p_outpic;
191 }