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