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