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