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