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>
43 /** Parse a value from a geometry string.
46 static inline float parse_value( char **ptr
, int normalisation
, char delim
, float defaults
)
48 float value
= defaults
;
50 if ( *ptr
!= NULL
&& **ptr
!= '\0' )
53 value
= strtod( *ptr
, &end
);
57 value
= ( value
/ 100.0 ) * normalisation
;
58 while ( *end
== delim
|| *end
== '%' )
67 /** Parse a geometry property string.
70 static void geometry_parse( struct geometry_s
*geometry
, struct geometry_s
*defaults
, char *property
, int nw
, int nh
)
72 // Assign normalised width and height
76 // Assign from defaults if available
77 if ( defaults
!= NULL
)
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
;
92 geometry
->mask_w
= 20;
93 geometry
->mask_h
= 20;
96 // Parse the geomtry string
97 if ( property
!= NULL
)
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
);
109 /** A Timism but not as clean ;-).
112 static float lerp( float value
, float lower
, float upper
)
116 else if ( value
> upper
)
121 /** Calculate real geometry.
124 static void geometry_calculate( struct geometry_s
*output
, struct geometry_s
*in
, struct geometry_s
*out
, float position
, int ow
, int oh
)
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
= in
->mask_w
+ ( out
->mask_w
- in
->mask_w
) * position
;
132 output
->mask_h
= in
->mask_h
+ ( out
->mask_h
- in
->mask_h
) * position
;
135 /** Calculate the position for this frame.
138 static float position_calculate( mlt_filter
this, mlt_frame frame
)
140 // Get the in and out position
141 mlt_position in
= mlt_filter_get_in( this );
142 mlt_position out
= mlt_filter_get_out( this );
144 // Get the position of the frame
145 mlt_position position
= mlt_frame_get_position( frame
);
148 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
151 /** The averaging function...
154 void obscure_average( uint8_t *start
, int width
, int height
, int stride
)
158 register int Y
= ( *start
+ *( start
+ 2 ) ) / 2;
159 register int U
= *( start
+ 1 );
160 register int V
= *( start
+ 3 );
163 for ( y
= 0; y
< height
; y
++ )
165 p
= start
+ y
* stride
;
166 for ( x
= 0; x
< width
/ 2; x
++ )
168 Y
= ( Y
+ *p
++ ) / 2;
169 U
= ( U
+ *p
++ ) / 2;
170 Y
= ( Y
+ *p
++ ) / 2;
171 V
= ( V
+ *p
++ ) / 2;
175 for ( y
= 0; y
< height
; y
++ )
177 p
= start
+ y
* stride
;
179 for ( x
= 0; x
< width
/ 2; x
++ )
190 /** The obscurer rendering function...
193 static void obscure_render( uint8_t *image
, int width
, int height
, struct geometry_s result
)
195 int area_x
= result
.x
;
196 int area_y
= result
.y
;
197 int area_w
= result
.w
;
198 int area_h
= result
.h
;
200 int mw
= result
.mask_w
;
201 int mh
= result
.mask_h
;
207 uint8_t *p
= image
+ area_y
* width
* 2 + area_x
* 2;
209 for ( w
= 0; w
< area_w
; w
+= mw
)
211 for ( h
= 0; h
< area_h
; h
+= mh
)
213 aw
= w
+ mw
> area_w ? mw
- ( w
+ mw
- area_w
) : mw
;
214 ah
= h
+ mh
> area_h ? mh
- ( h
+ mh
- area_h
) : mh
;
215 if ( aw
> 1 && ah
> 1 )
216 obscure_average( p
+ h
* width
* 2 + w
* 2, aw
, ah
, width
* 2 );
224 static int filter_get_image( mlt_frame frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
226 // Get the frame properties
227 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
229 // Pop the top of stack now
230 mlt_filter
this = mlt_frame_pop_service( frame
);
232 // Get the image from the frame
233 int error
= mlt_frame_get_image( frame
, image
, format
, width
, height
, 1 );
235 // Get the image from the frame
236 if ( error
== 0 && *format
== mlt_image_yuv422
)
240 // Get the filter properties
241 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
243 // Obtain the normalised width and height from the frame
244 int normalised_width
= mlt_properties_get_int( frame_properties
, "normalised_width" );
245 int normalised_height
= mlt_properties_get_int( frame_properties
, "normalised_height" );
247 // Structures for geometry
248 struct geometry_s result
;
249 struct geometry_s start
;
250 struct geometry_s end
;
252 // Calculate the position
253 float position
= position_calculate( this, frame
);
255 // Now parse the geometries
256 geometry_parse( &start
, NULL
, mlt_properties_get( properties
, "start" ), normalised_width
, normalised_height
);
257 geometry_parse( &end
, &start
, mlt_properties_get( properties
, "end" ), normalised_width
, normalised_height
);
259 // Do the calculation
260 geometry_calculate( &result
, &start
, &end
, position
, *width
, *height
);
262 // Now actually render it
263 obscure_render( *image
, *width
, *height
, result
);
270 /** Filter processing.
273 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
275 // Push this on to the service stack
276 mlt_frame_push_service( frame
, this );
278 // Push the get image call
279 mlt_frame_push_get_image( frame
, filter_get_image
);
284 /** Constructor for the filter.
287 mlt_filter
filter_obscure_init( void *arg
)
289 mlt_filter
this = mlt_filter_new( );
292 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
293 this->process
= filter_process
;
294 mlt_properties_set( properties
, "start", arg
!= NULL ? arg
: "0%,0%:100%x100%" );
295 mlt_properties_set( properties
, "end", "" );