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