]> git.sesse.net Git - vlc/blob - modules/video_filter/alphamask.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_filter / alphamask.c
1 /*****************************************************************************
2  * alphamask.c : Alpha layer mask video filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id: invert.c 18062 2006-11-26 14:20:34Z zorglub $
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod 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 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30
31 #include <vlc_image.h>
32 #include <vlc_filter.h>
33
34 #define ALPHAMASK_HELP N_( \
35     "Use an image's alpha channel as a transparency mask." )
36
37 #define MASK_TEXT N_("Transparency mask")
38 #define MASK_LONGTEXT N_( \
39     "Alpha blending transparency mask. Use's a png alpha channel.")
40
41 #define CFG_PREFIX "alphamask-"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create      ( vlc_object_t * );
47 static void Destroy     ( vlc_object_t * );
48
49 static picture_t *Filter( filter_t *, picture_t * );
50 static void LoadMask( filter_t *, const char * );
51 static int MaskCallback( vlc_object_t *, char const *,
52                          vlc_value_t, vlc_value_t, void * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_description( _("Alpha mask video filter") );
59     set_shortname( _("Alpha mask" ));
60     set_help( ALPHAMASK_HELP );
61     set_category( CAT_VIDEO );
62     set_subcategory( SUBCAT_VIDEO_VFILTER );
63     set_capability( "video filter2", 0 );
64     add_shortcut( "alphamask" );
65     add_shortcut( "mask" );
66     set_callbacks( Create, Destroy );
67
68     add_string( CFG_PREFIX "mask", NULL, NULL, MASK_TEXT,
69                 MASK_LONGTEXT, VLC_FALSE );
70         change_safe();
71 vlc_module_end();
72
73 static const char *ppsz_filter_options[] = {
74     "mask", NULL
75 };
76
77 struct filter_sys_t
78 {
79     picture_t *p_mask;
80     vlc_mutex_t mask_lock;
81 };
82
83 static int Create( vlc_object_t *p_this )
84 {
85     filter_t *p_filter = (filter_t *)p_this;
86     filter_sys_t *p_sys;
87     char *psz_string;
88
89     if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') )
90     {
91         msg_Err( p_filter,
92                  "Unsupported input chroma \"%4s\". "
93                  "Alphamask can only use \"YUVA\".",
94                  (char*)&p_filter->fmt_in.video.i_chroma );
95         return VLC_EGENERIC;
96     }
97
98     /* Allocate structure */
99     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
100     if( p_filter->p_sys == NULL )
101     {
102         msg_Err( p_filter, "out of memory" );
103         return VLC_ENOMEM;
104     }
105     p_sys = p_filter->p_sys;
106
107     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
108                        p_filter->p_cfg );
109
110     vlc_mutex_init( p_filter, &p_sys->mask_lock );
111     psz_string =
112         var_CreateGetStringCommand( p_filter, CFG_PREFIX "mask" );
113     var_AddCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
114                      p_filter );
115     p_sys->p_mask = NULL;
116     if( psz_string && *psz_string )
117     {
118         LoadMask( p_filter, psz_string );
119         if( !p_sys->p_mask )
120             msg_Err( p_filter, "Error while loading mask (%s).",
121                      psz_string );
122     }
123     free( psz_string );
124
125     p_filter->pf_video_filter = Filter;
126
127     return VLC_SUCCESS;
128 }
129
130 static void Destroy( vlc_object_t *p_this )
131 {
132     filter_t *p_filter = (filter_t *)p_this;
133     filter_sys_t *p_sys = p_filter->p_sys;
134
135     vlc_mutex_destroy( &p_sys->mask_lock );
136     if( p_filter->p_sys->p_mask )
137         p_filter->p_sys->p_mask->pf_release( p_filter->p_sys->p_mask );
138
139     free( p_filter->p_sys );
140 }
141
142 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
143 {
144     filter_sys_t *p_sys = p_filter->p_sys;
145
146     vlc_mutex_lock( &p_sys->mask_lock );
147     plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
148     plane_t *p_apic = p_pic->p+A_PLANE;
149     if(    p_mask->i_visible_pitch
150         != p_apic->i_visible_pitch
151         || p_mask->i_visible_lines
152         != p_apic->i_visible_lines )
153     {
154         msg_Warn( p_filter,
155                   "Mask size (%d x %d) and image size (%d x %d) "
156                   "don't match. The mask will not be applied.",
157                   p_mask->i_visible_pitch,
158                   p_mask->i_visible_lines,
159                   p_apic->i_visible_pitch,
160                   p_apic->i_visible_lines );
161     }
162     else
163     {
164         if( p_mask->i_pitch != p_apic->i_pitch
165         ||  p_mask->i_lines != p_apic->i_lines )
166         {
167             /* visible plane sizes match ... but not the underlying
168              * buffer. I'm not sure that this can happen,
169              * but better safe than sorry. */
170             int i_line;
171             int i_lines = p_mask->i_visible_lines;
172             uint8_t *p_src = p_mask->p_pixels;
173             uint8_t *p_dst = p_apic->p_pixels;
174             int i_src_pitch = p_mask->i_pitch;
175             int i_dst_pitch = p_apic->i_pitch;
176             int i_visible_pitch = p_mask->i_visible_pitch;
177             for( i_line = 0; i_line < i_lines; i_line++,
178                  p_src += i_src_pitch, p_dst += i_dst_pitch )
179             {
180                 p_filter->p_libvlc->pf_memcpy(
181                     p_dst, p_src, i_visible_pitch );
182             }
183         }
184         else
185         {
186             /* plane sizes match */
187             p_filter->p_libvlc->pf_memcpy(
188                 p_apic->p_pixels, p_mask->p_pixels,
189                 p_mask->i_pitch * p_mask->i_lines );
190         }
191     }
192     vlc_mutex_unlock( &p_sys->mask_lock );
193     return p_pic;
194 }
195
196 /* copied from video_filters/erase.c . Gruik ? */
197 static void LoadMask( filter_t *p_filter, const char *psz_filename )
198 {
199     image_handler_t *p_image;
200     video_format_t fmt_in, fmt_out;
201     memset( &fmt_in, 0, sizeof( video_format_t ) );
202     memset( &fmt_out, 0, sizeof( video_format_t ) );
203     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
204     if( p_filter->p_sys->p_mask )
205         p_filter->p_sys->p_mask->pf_release( p_filter->p_sys->p_mask );
206     p_image = image_HandlerCreate( p_filter );
207     p_filter->p_sys->p_mask =
208         image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
209     image_HandlerDelete( p_image );
210 }
211
212 /*****************************************************************************
213 * Callback to update params on the fly
214 *****************************************************************************/
215 static int MaskCallback( vlc_object_t *p_this, char const *psz_var,
216                          vlc_value_t oldval, vlc_value_t newval,
217                          void *p_data )
218 {
219     filter_t *p_filter = (filter_t *)p_data;
220     filter_sys_t *p_sys = p_filter->p_sys;
221     int i_ret = VLC_SUCCESS;
222
223 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
224     if( VAR_IS( "mask" ) )
225     {
226         vlc_mutex_lock( &p_sys->mask_lock );
227         if( newval.psz_string && *newval.psz_string )
228         {
229             LoadMask( p_filter, newval.psz_string );
230             if( !p_sys->p_mask )
231             {
232                 msg_Err( p_filter, "Error while loading mask (%s).",
233                          newval.psz_string );
234                 i_ret = VLC_EGENERIC;
235             }
236         }
237         else if( p_sys->p_mask )
238         {
239             p_sys->p_mask->pf_release( p_sys->p_mask );
240             p_sys->p_mask = NULL;
241         }
242         vlc_mutex_unlock( &p_sys->mask_lock );
243     }
244 #undef VAR_IS
245
246     return i_ret;
247 }