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