]> git.sesse.net Git - vlc/blob - modules/video_filter/alphamask.c
7b8806dfecfd6bfa6b96ab0fa10d4d77f092c054
[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$
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 #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_image.h>
37 #include <vlc_filter.h>
38
39 #define ALPHAMASK_HELP N_( \
40     "Use an image's alpha channel as a transparency mask." )
41
42 #define MASK_TEXT N_("Transparency mask")
43 #define MASK_LONGTEXT N_( \
44     "Alpha blending transparency mask. Uses a png alpha channel.")
45
46 #define CFG_PREFIX "alphamask-"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int  Create      ( vlc_object_t * );
52 static void Destroy     ( vlc_object_t * );
53
54 static picture_t *Filter( filter_t *, picture_t * );
55 static void LoadMask( filter_t *, const char * );
56 static int MaskCallback( vlc_object_t *, char const *,
57                          vlc_value_t, vlc_value_t, void * );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin ()
63     set_description( N_("Alpha mask video filter") )
64     set_shortname( N_("Alpha mask" ))
65     set_help( ALPHAMASK_HELP )
66     set_category( CAT_VIDEO )
67     set_subcategory( SUBCAT_VIDEO_VFILTER )
68     set_capability( "video filter2", 0 )
69     add_shortcut( "alphamask" )
70     add_shortcut( "mask" )
71     set_callbacks( Create, Destroy )
72
73     add_string( CFG_PREFIX "mask", NULL, NULL, MASK_TEXT,
74                 MASK_LONGTEXT, false )
75 vlc_module_end ()
76
77 static const char *const ppsz_filter_options[] = {
78     "mask", NULL
79 };
80
81 struct filter_sys_t
82 {
83     picture_t *p_mask;
84     vlc_mutex_t mask_lock;
85 };
86
87 static int Create( vlc_object_t *p_this )
88 {
89     filter_t *p_filter = (filter_t *)p_this;
90     filter_sys_t *p_sys;
91     char *psz_string;
92
93     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA )
94     {
95         msg_Err( p_filter,
96                  "Unsupported input chroma \"%4s\". "
97                  "Alphamask can only use \"YUVA\".",
98                  (char*)&p_filter->fmt_in.video.i_chroma );
99         return VLC_EGENERIC;
100     }
101
102     /* Allocate structure */
103     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
104     if( p_filter->p_sys == NULL )
105         return VLC_ENOMEM;
106     p_sys = p_filter->p_sys;
107
108     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
109                        p_filter->p_cfg );
110
111     vlc_mutex_init( &p_sys->mask_lock );
112     psz_string =
113         var_CreateGetStringCommand( p_filter, CFG_PREFIX "mask" );
114     var_AddCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
115                      p_filter );
116     p_sys->p_mask = NULL;
117     if( psz_string && *psz_string )
118     {
119         LoadMask( p_filter, psz_string );
120         if( !p_sys->p_mask )
121             msg_Err( p_filter, "Error while loading mask (%s).",
122                      psz_string );
123     }
124     free( psz_string );
125
126     p_filter->pf_video_filter = Filter;
127
128     return VLC_SUCCESS;
129 }
130
131 static void Destroy( vlc_object_t *p_this )
132 {
133     filter_t *p_filter = (filter_t *)p_this;
134     filter_sys_t *p_sys = p_filter->p_sys;
135
136     var_DelCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
137                      p_filter );
138
139     vlc_mutex_destroy( &p_sys->mask_lock );
140     if( p_sys->p_mask )
141         picture_Release( p_sys->p_mask );
142
143     free( p_sys );
144 }
145
146 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
147 {
148     filter_sys_t *p_sys = p_filter->p_sys;
149
150     vlc_mutex_lock( &p_sys->mask_lock );
151     plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
152     plane_t *p_apic = p_pic->p+A_PLANE;
153     if(    p_mask->i_visible_pitch
154         != p_apic->i_visible_pitch
155         || p_mask->i_visible_lines
156         != p_apic->i_visible_lines )
157     {
158         msg_Err( p_filter,
159                   "Mask size (%d x %d) and image size (%d x %d) "
160                   "don't match. The mask will not be applied.",
161                   p_mask->i_visible_pitch,
162                   p_mask->i_visible_lines,
163                   p_apic->i_visible_pitch,
164                   p_apic->i_visible_lines );
165     }
166     else
167     {
168         plane_CopyPixels( p_apic, p_mask );
169     }
170     vlc_mutex_unlock( &p_sys->mask_lock );
171     return p_pic;
172 }
173
174 /* copied from video_filters/erase.c . Gruik ? */
175 static void LoadMask( filter_t *p_filter, const char *psz_filename )
176 {
177     image_handler_t *p_image;
178     video_format_t fmt_in, fmt_out;
179     memset( &fmt_in, 0, sizeof( video_format_t ) );
180     memset( &fmt_out, 0, sizeof( video_format_t ) );
181     fmt_out.i_chroma = VLC_CODEC_YUVA;
182     if( p_filter->p_sys->p_mask )
183         picture_Release( p_filter->p_sys->p_mask );
184     p_image = image_HandlerCreate( p_filter );
185     p_filter->p_sys->p_mask =
186         image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
187     image_HandlerDelete( p_image );
188 }
189
190 /*****************************************************************************
191 * Callback to update params on the fly
192 *****************************************************************************/
193 static int MaskCallback( vlc_object_t *p_this, char const *psz_var,
194                          vlc_value_t oldval, vlc_value_t newval,
195                          void *p_data )
196 {
197     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
198     filter_t *p_filter = (filter_t *)p_data;
199     filter_sys_t *p_sys = p_filter->p_sys;
200     int i_ret = VLC_SUCCESS;
201
202 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
203     if( VAR_IS( "mask" ) )
204     {
205         vlc_mutex_lock( &p_sys->mask_lock );
206         if( newval.psz_string && *newval.psz_string )
207         {
208             LoadMask( p_filter, newval.psz_string );
209             if( !p_sys->p_mask )
210             {
211                 msg_Err( p_filter, "Error while loading mask (%s).",
212                          newval.psz_string );
213                 i_ret = VLC_EGENERIC;
214             }
215         }
216         else if( p_sys->p_mask )
217         {
218             picture_Release( p_sys->p_mask );
219             p_sys->p_mask = NULL;
220         }
221         vlc_mutex_unlock( &p_sys->mask_lock );
222     }
223 #undef VAR_IS
224
225     return i_ret;
226 }