2 * filter_obscure.c -- obscure filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
42 /** Parse a value from a geometry string.
45 static inline float parse_value( char **ptr, int normalisation, char delim, float defaults )
47 float value = defaults;
49 if ( *ptr != NULL && **ptr != '\0' )
52 value = strtod( *ptr, &end );
56 value = ( value / 100.0 ) * normalisation;
57 while ( *end == delim || *end == '%' )
66 /** Parse a geometry property string.
69 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
71 // Assign normalised width and height
75 // Assign from defaults if available
76 if ( defaults != NULL )
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;
91 geometry->mask_w = 20;
92 geometry->mask_h = 20;
95 // Parse the geomtry string
96 if ( property != NULL )
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 );
108 /** A Timism but not as clean ;-).
111 static float lerp( float value, float lower, float upper )
115 else if ( upper > lower && value > upper )
120 /** Calculate real geometry.
123 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position, int ow, int oh )
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 );
134 /** The averaging function...
137 static inline void obscure_average( uint8_t *start, int width, int height, int stride )
141 register int Y = ( *start + *( start + 2 ) ) / 2;
142 register int U = *( start + 1 );
143 register int V = *( start + 3 );
145 register int components = width >> 1;
154 Y = ( Y + *p ++ ) >> 1;
155 U = ( U + *p ++ ) >> 1;
156 Y = ( Y + *p ++ ) >> 1;
157 V = ( V + *p ++ ) >> 1;
162 start -= height * stride;
180 /** The obscurer rendering function...
183 static void obscure_render( uint8_t *image, int width, int height, struct geometry_s result )
185 int area_x = result.x;
186 int area_y = result.y;
187 int area_w = result.w;
188 int area_h = result.h;
190 int mw = result.mask_w;
191 int mh = result.mask_h;
197 uint8_t *p = image + area_y * width * 2 + area_x * 2;
199 for ( w = 0; w < area_w; w += mw )
201 for ( h = 0; h < area_h; h += mh )
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 );
214 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
216 // Get the frame properties
217 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
219 // Pop the top of stack now
220 mlt_filter this = mlt_frame_pop_service( frame );
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 );
226 // Get the image from the frame
231 // Get the filter properties
232 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
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" );
238 // Structures for geometry
239 struct geometry_s result;
240 struct geometry_s start;
241 struct geometry_s end;
243 // Retrieve the position
244 float position = mlt_filter_get_progress( this, frame );
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 );
250 // Do the calculation
251 geometry_calculate( &result, &start, &end, position, *width, *height );
253 // Now actually render it
254 obscure_render( *image, *width, *height, result );
261 /** Filter processing.
264 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
266 // Push this on to the service stack
267 mlt_frame_push_service( frame, this );
269 // Push the get image call
270 mlt_frame_push_get_image( frame, filter_get_image );
275 /** Constructor for the filter.
278 mlt_filter filter_obscure_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
280 mlt_filter this = mlt_filter_new( );
283 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
284 this->process = filter_process;
285 mlt_properties_set( properties, "start", arg != NULL ? arg : "0%,0%:100%x100%" );
286 mlt_properties_set( properties, "end", "" );