7324293fae67de7d09843ac0b47e48e4191ce2ef
[melted] / 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 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.
10 *
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.
15 *
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.
19 */
20
21 #include "filter_obscure.h"
22
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_deque.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 /** Geometry struct.
30 */
31
32 struct geometry_s
33 {
34 int nw;
35 int nh;
36 float x;
37 float y;
38 float w;
39 float h;
40 int mask_w;
41 int mask_h;
42 };
43
44 /** Parse a value from a geometry string.
45 */
46
47 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
48 {
49 float value = defaults;
50
51 if ( *ptr != NULL && **ptr != '\0' )
52 {
53 char *end = NULL;
54 value = strtod( *ptr, &end );
55 if ( end != NULL )
56 {
57 if ( *end == '%' )
58 value = ( value / 100.0 ) * normalisation;
59 while ( *end == delim || *end == '%' )
60 end ++;
61 }
62 *ptr = end;
63 }
64
65 return value;
66 }
67
68 /** Parse a geometry property string.
69 */
70
71 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
72 {
73 // Assign normalised width and height
74 geometry->nw = nw;
75 geometry->nh = nh;
76
77 // Assign from defaults if available
78 if ( defaults != NULL )
79 {
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;
86 }
87 else
88 {
89 geometry->mask_w = 20;
90 geometry->mask_h = 20;
91 }
92
93 // Parse the geomtry string
94 if ( property != NULL )
95 {
96 char *ptr = property;
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 );
103 }
104 }
105
106 /** A Timism but not as clean ;-).
107 */
108
109 static float lerp( float value, float lower, float upper )
110 {
111 if ( value < lower )
112 return lower;
113 else if ( value > upper )
114 return upper;
115 return value;
116 }
117
118 /** Calculate real geometry.
119 */
120
121 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position, int ow, int oh )
122 {
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;
130 }
131
132 /** Calculate the position for this frame.
133 */
134
135 static float position_calculate( mlt_filter this, mlt_frame frame )
136 {
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 );
140
141 // Get the position of the frame
142 mlt_position position = mlt_frame_get_position( frame );
143
144 // Now do the calcs
145 return ( float )( position - in ) / ( float )( out - in + 1 );
146 }
147
148 /** The averaging function...
149 */
150
151 void obscure_average( uint8_t *start, int width, int height, int stride, uint8_t *alpha )
152 {
153 int y;
154 int x;
155 int Y = ( *start + *( start + 2 ) ) / 2;
156 int U = *( start + 1 );
157 int V = *( start + 3 );
158
159 for ( y = 0; y < height; y ++ )
160 {
161 uint8_t *p = start + y * stride;
162 for ( x = 0; x < width / 2; x ++ )
163 {
164 Y = ( Y + *p ++ ) / 2;
165 U = ( U + *p ++ ) / 2;
166 Y = ( Y + *p ++ ) / 2;
167 V = ( V + *p ++ ) / 2;
168 }
169 }
170
171 for ( y = 0; y < height; y ++ )
172 {
173 uint8_t *p = start + y * stride;
174 uint8_t *z = alpha;
175
176 if ( z != NULL )
177 z += y * stride / 2;
178
179 for ( x = 0; x < width / 2; x ++ )
180 {
181 if ( z == NULL || ( z != NULL && *z++ > 127 ) )
182 {
183 *p ++ = Y;
184 *p ++ = U;
185 }
186 else
187 {
188 p += 2;
189 }
190
191 if ( z == NULL || ( z != NULL && *z++ > 127 ) )
192 {
193
194 *p ++ = Y;
195 *p ++ = V;
196 }
197 else
198 {
199 p += 2;
200 }
201 }
202 }
203 }
204
205
206 /** The obscurer rendering function...
207 */
208
209 static void obscure_render( uint8_t *image, int width, int height, struct geometry_s result, uint8_t *alpha )
210 {
211 int area_x = result.x;
212 int area_y = result.y;
213 int area_w = result.w;
214 int area_h = result.h;
215
216 int mw = result.mask_w;
217 int mh = result.mask_h;
218 int w;
219 int h;
220
221 uint8_t *p = image + area_y * width * 2 + area_x * 2;
222 uint8_t *z = alpha;
223 if ( z != NULL )
224 z += area_y * width + area_x;
225
226 for ( w = 0; w < area_w; w += mw )
227 {
228 for ( h = 0; h < area_h; h += mh )
229 {
230 int aw = w + mw > area_w ? mw - ( w + mw - area_w ) : mw;
231 int ah = h + mh > area_h ? mh - ( h + mh - area_h ) : mh;
232 if ( aw > 1 && ah > 1 )
233 obscure_average( p + h * width * 2 + w * 2, aw, ah, width * 2,
234 ( z == NULL ? z : z + h * width + w ) );
235 }
236 }
237 }
238
239 /** Do it :-).
240 */
241
242 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
243 {
244 // Get the frame properties
245 mlt_properties frame_properties = mlt_frame_properties( frame );
246
247 // Fetch the obscure stack for this frame
248 mlt_deque deque = mlt_properties_get_data( frame_properties, "filter_obscure", NULL );
249
250 // Pop the top of stack now
251 mlt_filter this = mlt_deque_pop_back( deque );
252
253 // Get the image from the frame
254 if ( mlt_frame_get_image( frame, image, format, width, height, 1 ) == 0 )
255 {
256 if ( this != NULL )
257 {
258 // Get the filter properties
259 mlt_properties properties = mlt_filter_properties( this );
260
261 // Obtain the normalised width and height from the frame
262 int normalised_width = mlt_properties_get_int( frame_properties, "normalised_width" );
263 int normalised_height = mlt_properties_get_int( frame_properties, "normalised_height" );
264
265 // Structures for geometry
266 struct geometry_s result;
267 struct geometry_s start;
268 struct geometry_s end;
269
270 // Calculate the position
271 float position = position_calculate( this, frame );
272
273 // Now parse the geometries
274 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
275 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
276
277 // Do the calculation
278 geometry_calculate( &result, &start, &end, position, *width, *height );
279
280 // Now actually render it
281 obscure_render( *image, *width, *height, result, mlt_frame_get_alpha_mask( frame ) );
282 }
283 }
284
285 return 0;
286 }
287
288 /** Filter processing.
289 */
290
291 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
292 {
293 // Get the frame properties
294 mlt_properties frame_properties = mlt_frame_properties( frame );
295
296 // Fetch the obscure stack for this frame
297 mlt_deque deque = mlt_properties_get_data( frame_properties, "filter_obscure", NULL );
298
299 // Create stack if necessary
300 if ( deque == NULL )
301 {
302 // Create the deque
303 deque = mlt_deque_init( );
304
305 // Assign to the frame
306 mlt_properties_set_data( frame_properties, "filter_obscure", deque, 0, ( mlt_destructor )mlt_deque_close, NULL );
307 }
308
309 // Push this on to the obscure stack
310 mlt_deque_push_back( deque, this );
311
312 // Push the get image call
313 mlt_frame_push_get_image( frame, filter_get_image );
314
315 return frame;
316 }
317
318 /** Constructor for the filter.
319 */
320
321 mlt_filter filter_obscure_init( void *arg )
322 {
323 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
324 mlt_properties properties = mlt_filter_properties( this );
325 mlt_filter_init( this, NULL );
326 this->process = filter_process;
327 mlt_properties_set( properties, "start", arg != NULL ? arg : "0%,0%:100%x100%" );
328 mlt_properties_set( properties, "end", "" );
329 return this;
330 }
331