2 * filter_obscure.c -- obscure filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU 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.
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 General Public License for more details.
16 * You should have received a copy of the GNU 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.
21 #include "filter_obscure.h"
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_deque.h>
44 /** Parse a value from a geometry string.
47 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
49 float value = defaults;
51 if ( *ptr != NULL && **ptr != '\0' )
54 value = strtod( *ptr, &end );
58 value = ( value / 100.0 ) * normalisation;
59 while ( *end == delim || *end == '%' )
68 /** Parse a geometry property string.
71 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
73 // Assign normalised width and height
77 // Assign from defaults if available
78 if ( defaults != NULL )
80 geometry->x = defaults->x;
81 geometry->y = defaults->y;
82 geometry->w = defaults->w;
83 geometry->h = defaults->h;
84 geometry->mask_w = defaults->mask_w;
85 geometry->mask_h = defaults->mask_h;
89 geometry->mask_w = 20;
90 geometry->mask_h = 20;
93 // Parse the geomtry string
94 if ( property != NULL )
97 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
98 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
99 geometry->w = parse_value( &ptr, nw, 'x', geometry->w );
100 geometry->h = parse_value( &ptr, nh, ':', geometry->h );
101 geometry->mask_w = parse_value( &ptr, nw, 'x', geometry->mask_w );
102 geometry->mask_h = parse_value( &ptr, nh, ' ', geometry->mask_h );
106 /** A Timism but not as clean ;-).
109 static float lerp( float value, float lower, float upper )
113 else if ( value > upper )
118 /** Calculate real geometry.
121 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position, int ow, int oh )
123 // Calculate this frames geometry
124 output->x = lerp( ( in->x + ( out->x - in->x ) * position ) / ( float )out->nw * ow, 0, ow );
125 output->y = lerp( ( in->y + ( out->y - in->y ) * position ) / ( float )out->nh * oh, 0, oh );
126 output->w = lerp( ( in->w + ( out->w - in->w ) * position ) / ( float )out->nw * ow, 0, ow - output->x );
127 output->h = lerp( ( in->h + ( out->h - in->h ) * position ) / ( float )out->nh * oh, 0, oh - output->y );
128 output->mask_w = in->mask_w + ( out->mask_w - in->mask_w ) * position;
129 output->mask_h = in->mask_h + ( out->mask_h - in->mask_h ) * position;
132 /** Calculate the position for this frame.
135 static float position_calculate( mlt_filter this, mlt_frame frame )
137 // Get the in and out position
138 mlt_position in = mlt_filter_get_in( this );
139 mlt_position out = mlt_filter_get_out( this );
141 // Get the position of the frame
142 mlt_position position = mlt_frame_get_position( frame );
145 return ( float )( position - in ) / ( float )( out - in + 1 );
148 /** The averaging function...
151 void obscure_average( uint8_t *start, int width, int height, int stride )
155 int Y = ( *start + *( start + 2 ) ) / 2;
156 int U = *( start + 1 );
157 int V = *( start + 3 );
159 for ( y = 0; y < height; y ++ )
161 uint8_t *p = start + y * stride;
162 for ( x = 0; x < width / 2; x ++ )
164 Y = ( Y + *p ++ ) / 2;
165 U = ( U + *p ++ ) / 2;
166 Y = ( Y + *p ++ ) / 2;
167 V = ( V + *p ++ ) / 2;
171 for ( y = 0; y < height; y ++ )
173 uint8_t *p = start + y * stride;
174 for ( x = 0; x < width / 2; x ++ )
185 /** The obscurer rendering function...
188 static void obscure_render( uint8_t *image, int width, int height, struct geometry_s result )
190 int area_x = result.x;
191 int area_y = result.y;
192 int area_w = result.w;
193 int area_h = result.h;
195 int mw = result.mask_w;
196 int mh = result.mask_h;
200 uint8_t *p = image + area_y * width * 2 + area_x * 2;
202 for ( w = 0; w < area_w; w += mw )
204 for ( h = 0; h < area_h; h += mh )
206 int aw = w + mw > area_w ? mw - ( w + mw - area_w ) : mw;
207 int ah = h + mh > area_h ? mh - ( h + mh - area_h ) : mh;
208 if ( aw > 1 && ah > 1 );
209 obscure_average( p + h * width * 2 + w * 2, aw, ah, width * 2 );
217 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
219 // Get the frame properties
220 mlt_properties frame_properties = mlt_frame_properties( frame );
222 // Fetch the obscure stack for this frame
223 mlt_deque deque = mlt_properties_get_data( frame_properties, "filter_obscure", NULL );
225 // Pop the top of stack now
226 mlt_filter this = mlt_deque_pop_back( deque );
228 // Get the image from the frame
229 if ( mlt_frame_get_image( frame, image, format, width, height, 1 ) == 0 )
233 // Get the filter properties
234 mlt_properties properties = mlt_filter_properties( this );
236 // Obtain the normalised width and height from the frame
237 int normalised_width = mlt_properties_get_int( frame_properties, "normalised_width" );
238 int normalised_height = mlt_properties_get_int( frame_properties, "normalised_height" );
240 // Structures for geometry
241 struct geometry_s result;
242 struct geometry_s start;
243 struct geometry_s end;
245 // Calculate the position
246 float position = position_calculate( this, frame );
248 // Now parse the geometries
249 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
250 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
252 // Do the calculation
253 geometry_calculate( &result, &start, &end, position, *width, *height );
255 // Now actually render it
256 obscure_render( *image, *width, *height, result );
263 /** Filter processing.
266 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
268 // Get the frame properties
269 mlt_properties frame_properties = mlt_frame_properties( frame );
271 // Fetch the obscure stack for this frame
272 mlt_deque deque = mlt_properties_get_data( frame_properties, "filter_obscure", NULL );
274 // Create stack if necessary
278 deque = mlt_deque_init( );
280 // Assign to the frame
281 mlt_properties_set_data( frame_properties, "filter_obscure", deque, 0, ( mlt_destructor )mlt_deque_close, NULL );
284 // Push this on to the obscure stack
285 mlt_deque_push_back( deque, this );
287 // Push the get image call
288 mlt_frame_push_get_image( frame, filter_get_image );
293 /** Constructor for the filter.
296 mlt_filter filter_obscure_init( void *arg )
298 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
299 mlt_properties properties = mlt_filter_properties( this );
300 mlt_filter_init( this, NULL );
301 this->process = filter_process;
302 mlt_properties_set( properties, "start", arg != NULL ? arg : "0%,0%:100%x100%" );
303 mlt_properties_set( properties, "end", "" );