demux: mkv: handle WAVE_FORMAT_MPEG_ADTS_AAC
[vlc.git] / modules / video_filter / alphamask.c
blob716f8b3a51532ec664b5e8403ef1ebc38ff26a4b
1 /*****************************************************************************
2 * alphamask.c : Alpha layer mask video filter for vlc
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_image.h>
36 #include <vlc_filter.h>
37 #include <vlc_picture.h>
38 #include <vlc_url.h>
40 #define ALPHAMASK_HELP N_( \
41 "Use an image's alpha channel as a transparency mask." )
43 #define MASK_TEXT N_("Transparency mask")
44 #define MASK_LONGTEXT N_( \
45 "Alpha blending transparency mask. Uses a png alpha channel.")
47 #define CFG_PREFIX "alphamask-"
49 /*****************************************************************************
50 * Local prototypes
51 *****************************************************************************/
52 static int Create ( vlc_object_t * );
53 static void Destroy ( vlc_object_t * );
55 static picture_t *Filter( filter_t *, picture_t * );
56 static void LoadMask( filter_t *, const char * );
57 static int MaskCallback( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
60 /*****************************************************************************
61 * Module descriptor
62 *****************************************************************************/
63 vlc_module_begin ()
64 set_description( N_("Alpha mask video filter") )
65 set_shortname( N_("Alpha mask" ))
66 set_help( ALPHAMASK_HELP )
67 set_category( CAT_VIDEO )
68 set_subcategory( SUBCAT_VIDEO_VFILTER )
69 set_capability( "video filter", 0 )
70 add_shortcut( "alphamask", "mask" )
71 set_callbacks( Create, Destroy )
73 add_loadfile( CFG_PREFIX "mask", NULL, MASK_TEXT,
74 MASK_LONGTEXT, false )
75 vlc_module_end ()
77 static const char *const ppsz_filter_options[] = {
78 "mask", NULL
81 struct filter_sys_t
83 picture_t *p_mask;
84 vlc_mutex_t mask_lock;
87 static int Create( vlc_object_t *p_this )
89 filter_t *p_filter = (filter_t *)p_this;
90 filter_sys_t *p_sys;
91 char *psz_string;
93 if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA )
95 msg_Err( p_filter,
96 "Unsupported input chroma \"%4.4s\". "
97 "Alphamask can only use \"YUVA\".",
98 (char*)&p_filter->fmt_in.video.i_chroma );
99 return VLC_EGENERIC;
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;
108 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
109 p_filter->p_cfg );
111 psz_string =
112 var_CreateGetStringCommand( p_filter, CFG_PREFIX "mask" );
113 if( psz_string && *psz_string )
115 LoadMask( p_filter, psz_string );
116 if( !p_sys->p_mask )
117 msg_Err( p_filter, "Error while loading mask (%s).",
118 psz_string );
120 else
121 p_sys->p_mask = NULL;
122 free( psz_string );
124 vlc_mutex_init( &p_sys->mask_lock );
125 var_AddCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
126 p_filter );
127 p_filter->pf_video_filter = Filter;
129 return VLC_SUCCESS;
132 static void Destroy( vlc_object_t *p_this )
134 filter_t *p_filter = (filter_t *)p_this;
135 filter_sys_t *p_sys = p_filter->p_sys;
137 var_DelCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
138 p_filter );
140 vlc_mutex_destroy( &p_sys->mask_lock );
141 if( p_sys->p_mask )
142 picture_Release( p_sys->p_mask );
144 free( p_sys );
147 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
149 filter_sys_t *p_sys = p_filter->p_sys;
151 vlc_mutex_lock( &p_sys->mask_lock );
152 plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
153 plane_t *p_apic = p_pic->p+A_PLANE;
154 if( p_mask->i_visible_pitch
155 != p_apic->i_visible_pitch
156 || p_mask->i_visible_lines
157 != p_apic->i_visible_lines )
159 msg_Err( p_filter,
160 "Mask size (%d x %d) and image size (%d x %d) "
161 "don't match. The mask will not be applied.",
162 p_mask->i_visible_pitch,
163 p_mask->i_visible_lines,
164 p_apic->i_visible_pitch,
165 p_apic->i_visible_lines );
167 else
169 plane_CopyPixels( p_apic, p_mask );
171 vlc_mutex_unlock( &p_sys->mask_lock );
172 return p_pic;
175 /* copied from video_filters/erase.c . Gruik ? */
176 static void LoadMask( filter_t *p_filter, const char *psz_filename )
178 image_handler_t *p_image;
179 video_format_t fmt_in, fmt_out;
180 video_format_Init( &fmt_in, 0 );
181 video_format_Init( &fmt_out, 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 char *psz_url = vlc_path2uri( psz_filename, NULL );
186 p_filter->p_sys->p_mask =
187 image_ReadUrl( p_image, psz_url, &fmt_in, &fmt_out );
188 free( psz_url );
189 video_format_Clean( &fmt_in );
190 video_format_Clean( &fmt_out );
191 image_HandlerDelete( p_image );
194 /*****************************************************************************
195 * Callback to update params on the fly
196 *****************************************************************************/
197 static int MaskCallback( vlc_object_t *p_this, char const *psz_var,
198 vlc_value_t oldval, vlc_value_t newval,
199 void *p_data )
201 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
202 filter_t *p_filter = (filter_t *)p_data;
203 filter_sys_t *p_sys = p_filter->p_sys;
204 int i_ret = VLC_SUCCESS;
206 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
207 if( VAR_IS( "mask" ) )
209 vlc_mutex_lock( &p_sys->mask_lock );
210 if( newval.psz_string && *newval.psz_string )
212 LoadMask( p_filter, newval.psz_string );
213 if( !p_sys->p_mask )
215 msg_Err( p_filter, "Error while loading mask (%s).",
216 newval.psz_string );
217 i_ret = VLC_EGENERIC;
220 else if( p_sys->p_mask )
222 picture_Release( p_sys->p_mask );
223 p_sys->p_mask = NULL;
225 vlc_mutex_unlock( &p_sys->mask_lock );
227 #undef VAR_IS
229 return i_ret;