]> git.sesse.net Git - vlc/blob - modules/video_filter/grain.c
Remove stdlib.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 #include <string.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31
32 #include "vlc_filter.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Create    ( vlc_object_t * );
38 static void Destroy   ( vlc_object_t * );
39
40 static picture_t *Filter( filter_t *, picture_t * );
41
42 #define FILTER_PREFIX "grain-"
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( _("Grain video filter") );
49     set_shortname( _( "Grain" ));
50     set_capability( "video filter2", 0 );
51     set_category( CAT_VIDEO );
52     set_subcategory( SUBCAT_VIDEO_VFILTER );
53
54     set_callbacks( Create, Destroy );
55 vlc_module_end();
56
57 static const char *ppsz_filter_options[] = {
58     NULL
59 };
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     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
81                        p_filter->p_cfg );
82
83     p_filter->pf_video_filter = Filter;
84
85     p_filter->p_sys->p_noise = NULL;
86
87     return VLC_SUCCESS;
88 }
89
90 static void Destroy( vlc_object_t *p_this )
91 {
92     filter_t *p_filter = (filter_t *)p_this;
93     free( p_filter->p_sys->p_noise );
94     free( p_filter->p_sys );
95 }
96
97 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
98 {
99     picture_t *p_outpic;
100     filter_sys_t *p_sys = p_filter->p_sys;
101     int i_index;
102
103     if( !p_pic ) return NULL;
104
105     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
106     if( !p_outpic )
107     {
108         msg_Warn( p_filter, "can't get output picture" );
109         if( p_pic->pf_release )
110             p_pic->pf_release( p_pic );
111         return NULL;
112     }
113
114     {
115         uint8_t *p_in = p_pic->p[Y_PLANE].p_pixels;
116         uint8_t *p_out = p_outpic->p[Y_PLANE].p_pixels;
117
118         const int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
119         const int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
120         const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
121
122         int i_line, i_col;
123
124         int *p_noise = p_sys->p_noise;
125         if( !p_noise )
126         {
127             p_noise = p_sys->p_noise =
128                 (int*)malloc(i_pitch*i_num_lines*sizeof(int));
129         }
130
131         for( i_line = 0; i_line < i_num_lines; i_line++ )
132         {
133             for( i_col = 0; i_col < i_num_cols; i_col++ )
134             {
135                 p_noise[i_line*i_pitch+i_col] = ((rand()&0x1f)-0x0f);
136             }
137         }
138
139         for( i_line = 2/*0*/ ; i_line < i_num_lines-2/**/; i_line++ )
140         {
141             for( i_col = 2/*0*/; i_col < i_num_cols/2; i_col++ )
142             {
143                 p_out[i_line*i_pitch+i_col] = clip_uint8_vlc(
144                           p_in[i_line*i_pitch+i_col]
145 #if 0
146                         + p_noise[i_line*i_pitch+i_col] );
147 #else
148 /* 2 rows up */
149               + ((  ( p_noise[(i_line-2)*i_pitch+i_col-2]<<1 )
150               + ( p_noise[(i_line-2)*i_pitch+i_col-1]<<2 )
151               + ( p_noise[(i_line-2)*i_pitch+i_col]<<2 )
152               + ( p_noise[(i_line-2)*i_pitch+i_col+1]<<2 )
153               + ( p_noise[(i_line-2)*i_pitch+i_col+2]<<1 )
154               /* 1 row up */
155               + ( p_noise[(i_line-1)*i_pitch+i_col-2]<<2 )
156               + ( p_noise[(i_line-1)*i_pitch+i_col-1]<<3 )
157               + ( p_noise[(i_line-1)*i_pitch+i_col]*12 )
158               + ( p_noise[(i_line-1)*i_pitch+i_col+1]<<3 )
159               + ( p_noise[(i_line-1)*i_pitch+i_col+2]<<2 )
160               /* */
161               + ( p_noise[i_line*i_pitch+i_col-2]<<2 )
162               + ( p_noise[i_line*i_pitch+i_col-1]*12 )
163               + ( p_noise[i_line*i_pitch+i_col]<<4 )
164               + ( p_noise[i_line*i_pitch+i_col+1]*12 )
165               + ( p_noise[i_line*i_pitch+i_col+2]<<2 )
166               /* 1 row down */
167               + ( p_noise[(i_line+1)*i_pitch+i_col-2]<<2 )
168               + ( p_noise[(i_line+1)*i_pitch+i_col-1]<<3 )
169               + ( p_noise[(i_line+1)*i_pitch+i_col]*12 )
170               + ( p_noise[(i_line+1)*i_pitch+i_col+1]<<3 )
171               + ( p_noise[(i_line+1)*i_pitch+i_col+2]<<2 )
172               /* 2 rows down */
173               + ( p_noise[(i_line+2)*i_pitch+i_col-2]<<1 )
174               + ( p_noise[(i_line+2)*i_pitch+i_col-1]<<2 )
175               + ( p_noise[(i_line+2)*i_pitch+i_col]<<2 )
176               + ( p_noise[(i_line+2)*i_pitch+i_col+1]<<2 )
177               + ( p_noise[(i_line+2)*i_pitch+i_col+2]<<1 )
178               )>>7/*/152*/));
179 #endif
180
181             }
182             for( ; i_col < i_num_cols; i_col++ )
183                 p_out[i_line*i_pitch+i_col] = p_in[i_line*i_pitch+i_col];
184         }
185     }
186
187     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
188     {
189         uint8_t *p_in = p_pic->p[i_index].p_pixels;
190         uint8_t *p_out = p_outpic->p[i_index].p_pixels;
191
192         const int i_lines = p_pic->p[i_index].i_lines;
193         const int i_pitch = p_pic->p[i_index].i_pitch;
194
195         p_filter->p_libvlc->pf_memcpy( p_out, p_in, i_lines * i_pitch );
196
197     }
198
199     p_outpic->date = p_pic->date;
200     p_outpic->b_force = p_pic->b_force;
201     p_outpic->i_nb_fields = p_pic->i_nb_fields;
202     p_outpic->b_progressive = p_pic->b_progressive;
203     p_outpic->b_top_field_first = p_pic->b_top_field_first;
204
205     if( p_pic->pf_release )
206         p_pic->pf_release( p_pic );
207
208     return p_outpic;
209 }