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