]> git.sesse.net Git - vlc/blob - modules/video_filter/grain.c
A lot of missing const in options lists
[vlc] / modules / video_filter / grain.c
1 /*****************************************************************************
2  * noise.c : "add grain to image" video filter
3  *****************************************************************************
4  * Copyright (C) 2000-2007 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/vlc.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 "grain-"
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();
52     set_description( N_("Grain video filter") );
53     set_shortname( N_( "Grain" ));
54     set_capability( "video filter2", 0 );
55     set_category( CAT_VIDEO );
56     set_subcategory( SUBCAT_VIDEO_VFILTER );
57
58     set_callbacks( Create, Destroy );
59 vlc_module_end();
60
61 struct filter_sys_t
62 {
63     int *p_noise;
64 };
65
66 static int Create( vlc_object_t *p_this )
67 {
68     filter_t *p_filter = (filter_t *)p_this;
69
70     /* FIXME: check that chroma is YUV based */
71
72     /* Allocate structure */
73     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
74     if( p_filter->p_sys == NULL )
75     {
76         msg_Err( p_filter, "out of memory" );
77         return VLC_ENOMEM;
78     }
79
80     p_filter->pf_video_filter = Filter;
81
82     p_filter->p_sys->p_noise = NULL;
83
84     return VLC_SUCCESS;
85 }
86
87 static void Destroy( vlc_object_t *p_this )
88 {
89     filter_t *p_filter = (filter_t *)p_this;
90     free( p_filter->p_sys->p_noise );
91     free( p_filter->p_sys );
92 }
93
94 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
95 {
96     picture_t *p_outpic;
97     filter_sys_t *p_sys = p_filter->p_sys;
98     int i_index;
99
100     if( !p_pic ) return NULL;
101
102     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
103     if( !p_outpic )
104     {
105         msg_Warn( p_filter, "can't get output picture" );
106         if( p_pic->pf_release )
107             p_pic->pf_release( p_pic );
108         return NULL;
109     }
110
111     {
112         uint8_t *p_in = p_pic->p[Y_PLANE].p_pixels;
113         uint8_t *p_out = p_outpic->p[Y_PLANE].p_pixels;
114
115         const int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
116         const int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
117         const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
118
119         int i_line, i_col;
120
121         int *p_noise = p_sys->p_noise;
122         if( !p_noise )
123         {
124             p_noise = p_sys->p_noise =
125                 (int*)malloc(i_pitch*i_num_lines*sizeof(int));
126         }
127
128         for( i_line = 0; i_line < i_num_lines; i_line++ )
129         {
130             for( i_col = 0; i_col < i_num_cols; i_col++ )
131             {
132                 p_noise[i_line*i_pitch+i_col] = ((rand()&0x1f)-0x0f);
133             }
134         }
135
136         for( i_line = 2/*0*/ ; i_line < i_num_lines-2/**/; i_line++ )
137         {
138             for( i_col = 2/*0*/; i_col < i_num_cols/2; i_col++ )
139             {
140                 p_out[i_line*i_pitch+i_col] = clip_uint8_vlc(
141                           p_in[i_line*i_pitch+i_col]
142 #if 0
143                         + p_noise[i_line*i_pitch+i_col] );
144 #else
145 /* 2 rows up */
146               + ((  ( p_noise[(i_line-2)*i_pitch+i_col-2]<<1 )
147               + ( p_noise[(i_line-2)*i_pitch+i_col-1]<<2 )
148               + ( p_noise[(i_line-2)*i_pitch+i_col]<<2 )
149               + ( p_noise[(i_line-2)*i_pitch+i_col+1]<<2 )
150               + ( p_noise[(i_line-2)*i_pitch+i_col+2]<<1 )
151               /* 1 row up */
152               + ( p_noise[(i_line-1)*i_pitch+i_col-2]<<2 )
153               + ( p_noise[(i_line-1)*i_pitch+i_col-1]<<3 )
154               + ( p_noise[(i_line-1)*i_pitch+i_col]*12 )
155               + ( p_noise[(i_line-1)*i_pitch+i_col+1]<<3 )
156               + ( p_noise[(i_line-1)*i_pitch+i_col+2]<<2 )
157               /* */
158               + ( p_noise[i_line*i_pitch+i_col-2]<<2 )
159               + ( p_noise[i_line*i_pitch+i_col-1]*12 )
160               + ( p_noise[i_line*i_pitch+i_col]<<4 )
161               + ( p_noise[i_line*i_pitch+i_col+1]*12 )
162               + ( p_noise[i_line*i_pitch+i_col+2]<<2 )
163               /* 1 row down */
164               + ( p_noise[(i_line+1)*i_pitch+i_col-2]<<2 )
165               + ( p_noise[(i_line+1)*i_pitch+i_col-1]<<3 )
166               + ( p_noise[(i_line+1)*i_pitch+i_col]*12 )
167               + ( p_noise[(i_line+1)*i_pitch+i_col+1]<<3 )
168               + ( p_noise[(i_line+1)*i_pitch+i_col+2]<<2 )
169               /* 2 rows down */
170               + ( p_noise[(i_line+2)*i_pitch+i_col-2]<<1 )
171               + ( p_noise[(i_line+2)*i_pitch+i_col-1]<<2 )
172               + ( p_noise[(i_line+2)*i_pitch+i_col]<<2 )
173               + ( p_noise[(i_line+2)*i_pitch+i_col+1]<<2 )
174               + ( p_noise[(i_line+2)*i_pitch+i_col+2]<<1 )
175               )>>7/*/152*/));
176 #endif
177
178             }
179             for( ; i_col < i_num_cols; i_col++ )
180                 p_out[i_line*i_pitch+i_col] = p_in[i_line*i_pitch+i_col];
181         }
182     }
183
184     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
185     {
186         uint8_t *p_in = p_pic->p[i_index].p_pixels;
187         uint8_t *p_out = p_outpic->p[i_index].p_pixels;
188
189         const int i_lines = p_pic->p[i_index].i_lines;
190         const int i_pitch = p_pic->p[i_index].i_pitch;
191
192         vlc_memcpy( p_out, p_in, i_lines * i_pitch );
193
194     }
195
196     p_outpic->date = p_pic->date;
197     p_outpic->b_force = p_pic->b_force;
198     p_outpic->i_nb_fields = p_pic->i_nb_fields;
199     p_outpic->b_progressive = p_pic->b_progressive;
200     p_outpic->b_top_field_first = p_pic->b_top_field_first;
201
202     if( p_pic->pf_release )
203         p_pic->pf_release( p_pic );
204
205     return p_outpic;
206 }