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