]> git.sesse.net Git - mlt/blob - src/modules/core/filter_obscure.c
Fix reading binary files on Windows.
[mlt] / src / modules / core / filter_obscure.c
1 /*
2  * filter_obscure.c -- obscure filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_profile.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 /** Geometry struct.
29 */
30
31 struct geometry_s
32 {
33         int nw;
34         int nh;
35         float x;
36         float y;
37         float w;
38         float h;
39         int mask_w;
40         int mask_h;
41 };
42
43 /** Parse a value from a geometry string.
44 */
45
46 static inline float parse_value( char **ptr, int normalisation, char delim, float defaults )
47 {
48         float value = defaults;
49
50         if ( *ptr != NULL && **ptr != '\0' )
51         {
52                 char *end = NULL;
53                 value = strtod( *ptr, &end );
54                 if ( end != NULL )
55                 {
56                         if ( *end == '%' )
57                                 value = ( value / 100.0 ) * normalisation;
58                         while ( *end == delim || *end == '%' || ( delim == ',' && *end == '/' ) )
59                                 end ++;
60                 }
61                 *ptr = end;
62         }
63
64         return value;
65 }
66
67 /** Parse a geometry property string.
68 */
69
70 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
71 {
72         // Assign normalised width and height
73         geometry->nw = nw;
74         geometry->nh = nh;
75
76         // Assign from defaults if available
77         if ( defaults != NULL )
78         {
79                 geometry->x = defaults->x;
80                 geometry->y = defaults->y;
81                 geometry->w = defaults->w;
82                 geometry->h = defaults->h;
83                 geometry->mask_w = defaults->mask_w;
84                 geometry->mask_h = defaults->mask_h;
85         }
86         else
87         {
88                 geometry->x = 0;
89                 geometry->y = 0;
90                 geometry->w = nw;
91                 geometry->h = nh;
92                 geometry->mask_w = 20;
93                 geometry->mask_h = 20;
94         }
95
96         // Parse the geomtry string
97         if ( property != NULL )
98         {
99                 char *ptr = property;
100                 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
101                 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
102                 geometry->w = parse_value( &ptr, nw, 'x', geometry->w );
103                 geometry->h = parse_value( &ptr, nh, ':', geometry->h );
104                 geometry->mask_w = parse_value( &ptr, nw, 'x', geometry->mask_w );
105                 geometry->mask_h = parse_value( &ptr, nh, ' ', geometry->mask_h );
106         }
107 }
108
109 /** A Timism but not as clean ;-).
110 */
111
112 static float lerp( float value, float lower, float upper )
113 {
114         if ( value < lower )
115                 return lower;
116         else if ( upper > lower && value > upper )
117                 return upper;
118         return value;
119 }
120
121 /** Calculate real geometry.
122 */
123
124 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position, int ow, int oh )
125 {
126         // Calculate this frames geometry
127         output->x = lerp( ( in->x + ( out->x - in->x ) * position ) / ( float )out->nw * ow, 0, ow );
128         output->y = lerp( ( in->y + ( out->y - in->y ) * position ) / ( float )out->nh * oh, 0, oh );
129         output->w = lerp( ( in->w + ( out->w - in->w ) * position ) / ( float )out->nw * ow, 0, ow - output->x );
130         output->h = lerp( ( in->h + ( out->h - in->h ) * position ) / ( float )out->nh * oh, 0, oh - output->y );
131         output->mask_w = lerp( in->mask_w + ( out->mask_w - in->mask_w ) * position, 1, -1 );
132         output->mask_h = lerp( in->mask_h + ( out->mask_h - in->mask_h ) * position, 1, -1 );
133 }
134
135 /** The averaging function...
136 */
137
138 static inline void obscure_average( uint8_t *start, int width, int height, int stride )
139 {
140         register int y;
141         register int x;
142         register int Y = ( *start + *( start + 2 ) ) / 2;
143         register int U = *( start + 1 );
144         register int V = *( start + 3 );
145         register uint8_t *p;
146         register int components = width >> 1;
147
148         y = height;
149         while( y -- )
150         {
151                 p = start;
152                 x = components;
153                 while( x -- )
154                 {
155                         Y = ( Y + *p ++ ) >> 1;
156                         U = ( U + *p ++ ) >> 1;
157                         Y = ( Y + *p ++ ) >> 1;
158                         V = ( V + *p ++ ) >> 1;
159                 }
160                 start += stride;
161         }
162
163         start -= height * stride;
164         y = height;
165         while( y -- )
166         {
167                 p = start;
168                 x = components;
169                 while( x -- )
170                 {
171                         *p ++ = Y;
172                         *p ++ = U;
173                         *p ++ = Y;
174                         *p ++ = V;
175                 }
176                 start += stride;
177         }
178 }
179
180
181 /** The obscurer rendering function...
182 */
183
184 static void obscure_render( uint8_t *image, int width, int height, struct geometry_s result )
185 {
186         int area_x = result.x;
187         int area_y = result.y;
188         int area_w = result.w;
189         int area_h = result.h;
190
191         int mw = result.mask_w;
192         int mh = result.mask_h;
193         int w;
194         int h;
195         int aw;
196         int ah;
197
198         uint8_t *p = image + area_y * width * 2 + area_x * 2;
199
200         for ( w = 0; w < area_w; w += mw )
201         {
202                 for ( h = 0; h < area_h; h += mh )
203                 {
204                         aw = w + mw > area_w ? mw - ( w + mw - area_w ) : mw;
205                         ah = h + mh > area_h ? mh - ( h + mh - area_h ) : mh;
206                         if ( aw > 1 && ah > 1 )
207                                 obscure_average( p + h * ( width << 1 ) + ( w << 1 ), aw, ah, width << 1 );
208                 }
209         }
210 }
211
212 /** Do it :-).
213 */
214
215 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
216 {
217         // Pop the top of stack now
218         mlt_filter filter = mlt_frame_pop_service( frame );
219
220         // Get the image from the frame
221         *format = mlt_image_yuv422;
222         int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
223
224         // Get the image from the frame
225         if ( error == 0 )
226         {
227                 if ( filter != NULL )
228                 {
229                         // Get the filter properties
230                         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
231                         mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
232
233                         // Structures for geometry
234                         struct geometry_s result;
235                         struct geometry_s start;
236                         struct geometry_s end;
237
238                         // Retrieve the position
239                         float position = mlt_filter_get_progress( filter, frame );
240
241                         // Now parse the geometries
242                         geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), profile->width, profile->height );
243                         geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), profile->width, profile->height );
244
245                         // Do the calculation
246                         geometry_calculate( &result, &start, &end, position, *width, *height );
247
248                         // Now actually render it
249                         obscure_render( *image, *width, *height, result );
250                 }
251         }
252
253         return error;
254 }
255
256 /** Filter processing.
257 */
258
259 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
260 {
261         // Push this on to the service stack
262         mlt_frame_push_service( frame, filter );
263         
264         // Push the get image call
265         mlt_frame_push_get_image( frame, filter_get_image );
266
267         return frame;
268 }
269
270 /** Constructor for the filter.
271 */
272
273 mlt_filter filter_obscure_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
274 {
275         mlt_filter filter = mlt_filter_new( );
276         if ( filter != NULL )
277         {
278                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
279                 filter->process = filter_process;
280                 mlt_properties_set( properties, "start", arg != NULL ? arg : "0%/0%:100%x100%" );
281                 mlt_properties_set( properties, "end", "" );
282         }
283         return filter;
284 }
285