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