]> git.sesse.net Git - mlt/blob - src/modules/vmfx/filter_shape.c
Fix reading binary files on Windows.
[mlt] / src / modules / vmfx / filter_shape.c
1 /*
2  * filter_shape.c -- Arbitrary alpha channel shaping
3  * Copyright (C) 2005 Visual Media Fx Inc.
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <framework/mlt.h>
22 #include <string.h>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_producer.h>
26 #include <framework/mlt_geometry.h>
27
28 static inline double smoothstep( const double e1, const double e2, const double a )
29 {
30     if ( a < e1 ) return 0.0;
31     if ( a > e2 ) return 1.0;
32     double v = ( a - e1 ) / ( e2 - e1 );
33     return ( v * v * ( 3 - 2 * v ) );
34 }
35
36 /** Get the images and apply the luminance of the mask to the alpha of the frame.
37 */
38
39 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
40 {
41         // Fetch the data from the stack (mix, mask, filter)
42         double mix = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( this ) );
43         mlt_frame mask = mlt_frame_pop_service( this );
44         mlt_filter filter = mlt_frame_pop_service( this );
45
46         // Obtain the constants
47         double softness = mlt_properties_get_double( MLT_FILTER_PROPERTIES( filter ), "softness" );
48         int use_luminance = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "use_luminance" );
49         int invert = mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "invert" ) * 255;
50
51         // Render the frame
52         *format = mlt_image_yuv422;
53         if ( mlt_frame_get_image( this, image, format, width, height, writable ) == 0 && ( !use_luminance || ( int )mix != 1 ) )
54         {
55                 // Get the alpha mask of the source
56                 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
57
58                 // Obtain a scaled/distorted mask to match
59                 uint8_t *mask_img = NULL;
60                 mlt_image_format mask_fmt = mlt_image_yuv422;
61                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( mask ), "distort", 1 );
62                 mlt_properties_pass_list( MLT_FRAME_PROPERTIES( mask ), MLT_FRAME_PROPERTIES( this ), "deinterlace,deinterlace_method,rescale.interp" );
63
64                 if ( mlt_frame_get_image( mask, &mask_img, &mask_fmt, width, height, 0 ) == 0 )
65                 {
66                         int size = *width * *height;
67                         uint8_t *p = alpha;
68                         double a = 0;
69                         double b = 0;
70                         if ( !use_luminance )
71                         {
72                                 uint8_t *q = mlt_frame_get_alpha_mask( mask );
73                                 while( size -- )
74                                 {
75                                         a = ( double )*q ++ / 255.0;
76                         b = 1.0 - smoothstep( a, a + softness, mix );
77                                         *p = ( uint8_t )( *p * b ) ^ invert;
78                                         p ++;
79                                 }
80                         }
81                         else if ( ( int )mix != 1 )
82                         {
83                                 uint8_t *q = mask_img;
84                                 // Ensure softness tends to zero has mix tends to 1
85                                 softness *= ( 1.0 - mix );
86                                 while( size -- )
87                                 {
88                                         a = ( ( double )*q - 16 ) / 235.0;
89                         b = smoothstep( a, a + softness, mix );
90                                         *p = ( uint8_t )( *p * b ) ^ invert;
91                                         p ++;
92                                         q += 2;
93                                 }
94                         }
95                 }
96         }
97
98         return 0;
99 }
100
101 /** Filter processing.
102 */
103
104 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
105 {
106         // Obtain the shape instance
107         char *resource = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "resource" );
108         char *last_resource = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_resource" );
109         mlt_producer producer = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "instance", NULL );
110
111         // Get the key framed values
112         mlt_geometry alpha = mlt_properties_get_data( MLT_FILTER_PROPERTIES( this ), "_alpha", NULL );
113         char *alpha_data = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "mix" );
114         double alpha_mix = 0.0;
115
116         // Calculate the position and length
117         int position = mlt_filter_get_position( this, frame );
118         int in = mlt_filter_get_in( this );
119         int out = mlt_filter_get_out( this );
120         int length;
121
122         // Special case for attached filters - in/out come through on the frame
123         if ( out == 0 )
124         {
125                 in = mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "in" );
126                 out = mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "out" );
127                 position -= in;
128         }
129
130         // Duration of the shape
131         length = out - in + 1;
132
133         // If we haven't created the instance or it's changed
134         if ( producer == NULL || strcmp( resource, last_resource ) )
135         {
136                 char temp[ 512 ];
137                 char *extension = strrchr( resource, '.' );
138
139                 // Store the last resource now
140                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "_resource", resource );
141
142                 // This is a hack - the idea is that we can indirectly reference the
143                 // luma modules pgm or png images by a short cut like %luma01.pgm - we then replace
144                 // the % with the full path to the image and use it if it exists, if not, check for
145                 // the file ending in a .png, and failing that, default to a fade in
146                 if ( strchr( resource, '%' ) )
147                 {
148                         FILE *test;
149                         sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
150                         test = fopen( temp, "r" );
151
152                         if ( test == NULL )
153                         {
154                                 strcat( temp, ".png" );
155                                 test = fopen( temp, "r" );
156                         }
157
158                         if ( test )
159                                 fclose( test ); 
160                         else
161                                 strcpy( temp, "colour:0x00000080" );
162
163                         resource = temp;
164                         extension = strrchr( resource, '.' );
165                 }
166
167                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( this ) );
168                 producer = mlt_factory_producer( profile, NULL, resource );
169                 if ( producer != NULL )
170                         mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
171                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "instance", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
172         }
173
174         // Construct the geometry item if needed, otherwise refresh it
175         if ( alpha == NULL )
176         {
177                 alpha = mlt_geometry_init( );
178                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "_alpha", alpha, 0, ( mlt_destructor )mlt_geometry_close, NULL );
179                 mlt_geometry_parse( alpha, alpha_data, length, 100, 100 );
180         }
181         else
182         {
183                 mlt_geometry_refresh( alpha, alpha_data, length, 100, 100 );
184         }
185
186         // We may still not have a producer in which case, we do nothing
187         if ( producer != NULL )
188         {
189                 mlt_frame mask = NULL;
190                 struct mlt_geometry_item_s item;
191                 mlt_geometry_fetch( alpha, &item, position );
192                 alpha_mix = item.x;
193                 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( producer ), MLT_FILTER_PROPERTIES( this ), "producer." );
194                 mlt_producer_seek( producer, position );
195                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &mask, 0 ) == 0 )
196                 {
197                         char *name = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_unique_id" );
198                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), name, mask, 0, ( mlt_destructor )mlt_frame_close, NULL );
199                         mlt_frame_push_service( frame, this );
200                         mlt_frame_push_service( frame, mask );
201                         mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( frame ), alpha_mix / 100.0 );
202                         mlt_frame_push_get_image( frame, filter_get_image );
203                         if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "audio_match" ) )
204                         {
205                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "meta.mixdown", 1 );
206                                 mlt_properties_set_double( MLT_FRAME_PROPERTIES( frame ), "meta.volume", alpha_mix / 100.0 );
207                         }
208                 }
209         }
210
211         return frame;
212 }
213
214 /** Constructor for the filter.
215 */
216
217 mlt_filter filter_shape_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
218 {
219         mlt_filter this = mlt_filter_new( );
220         if ( this != NULL )
221         {
222                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "resource", arg );
223                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "mix", "100" );
224                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "audio_match", 1 );
225                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "invert", 0 );
226                 mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "softness", 0.1 );
227                 this->process = filter_process;
228         }
229         return this;
230 }
231