test: timestamps_filter: add SPU offset check after discontinuity
[vlc.git] / modules / video_filter / alphamask.c
1 /*****************************************************************************
2  * alphamask.c : Alpha layer mask video filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
5  *
6  * Authors: Antoine Cellerier <dionoea at videolan tod org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <vlc_image.h>
35 #include <vlc_filter.h>
36 #include <vlc_picture.h>
37 #include <vlc_url.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 filter", 0 )
69     add_shortcut( "alphamask", "mask" )
70     set_callbacks( Create, Destroy )
71
72     add_loadfile(CFG_PREFIX "mask", NULL, MASK_TEXT, MASK_LONGTEXT)
73 vlc_module_end ()
74
75 static const char *const ppsz_filter_options[] = {
76     "mask", NULL
77 };
78
79 typedef struct
80 {
81     picture_t *p_mask;
82     vlc_mutex_t mask_lock;
83 } filter_sys_t;
84
85 static int Create( vlc_object_t *p_this )
86 {
87     filter_t *p_filter = (filter_t *)p_this;
88     filter_sys_t *p_sys;
89     char *psz_string;
90
91     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA )
92     {
93         msg_Err( p_filter,
94                  "Unsupported input chroma \"%4.4s\". "
95                  "Alphamask can only use \"YUVA\".",
96                  (char*)&p_filter->fmt_in.video.i_chroma );
97         return VLC_EGENERIC;
98     }
99
100     /* Allocate structure */
101     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
102     if( p_filter->p_sys == NULL )
103         return VLC_ENOMEM;
104     p_sys = p_filter->p_sys;
105
106     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
107                        p_filter->p_cfg );
108
109     psz_string =
110         var_CreateGetStringCommand( p_filter, CFG_PREFIX "mask" );
111     if( psz_string && *psz_string )
112     {
113         LoadMask( p_filter, psz_string );
114         if( !p_sys->p_mask )
115             msg_Err( p_filter, "Error while loading mask (%s).",
116                      psz_string );
117     }
118     else
119        p_sys->p_mask = NULL;
120     free( psz_string );
121
122     vlc_mutex_init( &p_sys->mask_lock );
123     var_AddCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
124                      p_filter );
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     var_DelCallback( p_filter, CFG_PREFIX "mask", MaskCallback,
136                      p_filter );
137
138     vlc_mutex_destroy( &p_sys->mask_lock );
139     if( p_sys->p_mask )
140         picture_Release( p_sys->p_mask );
141
142     free( 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_Err( 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         plane_CopyPixels( p_apic, p_mask );
168     }
169     vlc_mutex_unlock( &p_sys->mask_lock );
170     return p_pic;
171 }
172
173 /* copied from video_filters/erase.c . Gruik ? */
174 static void LoadMask( filter_t *p_filter, const char *psz_filename )
175 {
176     filter_sys_t *p_sys = p_filter->p_sys;
177     image_handler_t *p_image;
178     video_format_t fmt_out;
179     video_format_Init( &fmt_out, VLC_CODEC_YUVA );
180     if( p_sys->p_mask )
181         picture_Release( p_sys->p_mask );
182     p_image = image_HandlerCreate( p_filter );
183     char *psz_url = vlc_path2uri( psz_filename, NULL );
184     p_sys->p_mask =
185         image_ReadUrl( p_image, psz_url, &fmt_out );
186     free( psz_url );
187     video_format_Clean( &fmt_out );
188     image_HandlerDelete( p_image );
189 }
190
191 /*****************************************************************************
192 * Callback to update params on the fly
193 *****************************************************************************/
194 static int MaskCallback( vlc_object_t *p_this, char const *psz_var,
195                          vlc_value_t oldval, vlc_value_t newval,
196                          void *p_data )
197 {
198     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
199     filter_t *p_filter = (filter_t *)p_data;
200     filter_sys_t *p_sys = p_filter->p_sys;
201     int i_ret = VLC_SUCCESS;
202
203 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
204     if( VAR_IS( "mask" ) )
205     {
206         vlc_mutex_lock( &p_sys->mask_lock );
207         if( newval.psz_string && *newval.psz_string )
208         {
209             LoadMask( p_filter, newval.psz_string );
210             if( !p_sys->p_mask )
211             {
212                 msg_Err( p_filter, "Error while loading mask (%s).",
213                          newval.psz_string );
214                 i_ret = VLC_EGENERIC;
215             }
216         }
217         else if( p_sys->p_mask )
218         {
219             picture_Release( p_sys->p_mask );
220             p_sys->p_mask = NULL;
221         }
222         vlc_mutex_unlock( &p_sys->mask_lock );
223     }
224 #undef VAR_IS
225
226     return i_ret;
227 }