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