2cec6a650e2c1cd2ec1431df8f2870987d2a3116
[melted] / src / modules / core / transition_composite.c
1 /*
2 * transition_composite.c -- compose one image over another using alpha channel
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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 "transition_composite.h"
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 /** Geometry struct.
28 */
29
30 struct geometry_s
31 {
32 int nw;
33 int nh;
34 float x;
35 float y;
36 float w;
37 float h;
38 float mix;
39 };
40
41 /** Parse a value from a geometry string.
42 */
43
44 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
45 {
46 float value = defaults;
47
48 if ( *ptr != NULL && **ptr != '\0' )
49 {
50 char *end = NULL;
51 value = strtod( *ptr, &end );
52 if ( end != NULL )
53 {
54 if ( *end == '%' )
55 value = ( value / 100.0 ) * normalisation;
56 while ( *end == delim || *end == '%' )
57 end ++;
58 }
59 *ptr = end;
60 }
61
62 return value;
63 }
64
65 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
66 expressed as a percentage by appending a % after the value, otherwise values are
67 assumed to be relative to the normalised dimensions of the consumer.
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->mix = defaults->mix;
84 }
85 else
86 {
87 geometry->mix = 100;
88 }
89
90 // Parse the geomtry string
91 if ( property != NULL )
92 {
93 char *ptr = property;
94 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
95 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
96 geometry->w = parse_value( &ptr, nw, 'x', geometry->w );
97 geometry->h = parse_value( &ptr, nh, ':', geometry->h );
98 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
99 }
100 }
101
102 /** Calculate real geometry.
103 */
104
105 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position )
106 {
107 // Calculate this frames geometry
108 output->nw = in->nw;
109 output->nh = in->nh;
110 output->x = in->x + ( out->x - in->x ) * position;
111 output->y = in->y + ( out->y - in->y ) * position;
112 output->w = in->w + ( out->w - in->w ) * position;
113 output->h = in->h + ( out->h - in->h ) * position;
114 output->mix = in->mix + ( out->mix - in->mix ) * position;
115 }
116
117 /** Calculate the position for this frame.
118 */
119
120 static float position_calculate( mlt_transition this, mlt_frame frame )
121 {
122 // Get the in and out position
123 mlt_position in = mlt_transition_get_in( this );
124 mlt_position out = mlt_transition_get_out( this );
125
126 // Get the position of the frame
127 mlt_position position = mlt_frame_get_position( frame );
128
129 // Now do the calcs
130 return ( float )( position - in ) / ( float )( out - in + 1 );
131 }
132
133 static int get_value( mlt_properties properties, char *preferred, char *fallback )
134 {
135 int value = mlt_properties_get_int( properties, preferred );
136 if ( value == 0 )
137 value = mlt_properties_get_int( properties, fallback );
138 return value;
139 }
140
141 /** Composite function.
142 */
143
144 static int composite_yuv( uint8_t *p_dest, mlt_image_format format_dest, int width_dest, int height_dest, mlt_frame that, struct geometry_s geometry )
145 {
146 int ret = 0;
147 uint8_t *p_src;
148 int i, j;
149 int stride_src;
150 int stride_dest;
151 int x_src = 0, y_src = 0;
152
153 mlt_image_format format_src = format_dest;
154 float weight = geometry.mix / 100;
155
156 // Compute the dimensioning rectangle
157 mlt_properties b_props = mlt_frame_properties( that );
158 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
159 mlt_properties properties = mlt_transition_properties( this );
160
161 if ( mlt_properties_get( properties, "distort" ) == NULL )
162 {
163 // Now do additional calcs based on real_width/height etc
164 //int normalised_width = mlt_properties_get_int( b_props, "normalised_width" );
165 //int normalised_height = mlt_properties_get_int( b_props, "normalised_height" );
166 int normalised_width = geometry.w;
167 int normalised_height = geometry.h;
168 int real_width = get_value( b_props, "real_width", "width" );
169 int real_height = get_value( b_props, "real_height", "height" );
170 double input_ar = mlt_frame_get_aspect_ratio( that );
171 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
172 int scaled_width = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_width;
173 int scaled_height = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_height;
174
175 // Now ensure that our images fit in the normalised frame
176 if ( scaled_width > normalised_width )
177 {
178 scaled_height = scaled_height * normalised_width / scaled_width;
179 scaled_width = normalised_width;
180 }
181 if ( scaled_height > normalised_height )
182 {
183 scaled_width = scaled_width * normalised_height / scaled_height;
184 scaled_height = normalised_height;
185 }
186
187 // Special case
188 if ( scaled_height == normalised_height )
189 scaled_width = normalised_width;
190
191 // Now we need to align to the geometry
192 if ( scaled_width <= geometry.w && scaled_height <= geometry.h )
193 {
194 // TODO: Should take into account requested alignment here...
195 // Assume centred alignment for now
196
197 geometry.x = geometry.x + ( geometry.w - scaled_width ) / 2;
198 geometry.y = geometry.y + ( geometry.h - scaled_height ) / 2;
199 geometry.w = scaled_width;
200 geometry.h = scaled_height;
201 mlt_properties_set( b_props, "distort", "true" );
202 }
203 else
204 {
205 mlt_properties_set( b_props, "distort", "true" );
206 }
207 }
208 else
209 {
210 // We want to ensure that we bypass resize now...
211 mlt_properties_set( b_props, "distort", "true" );
212 }
213
214 int x = ( geometry.x * width_dest ) / geometry.nw;
215 int y = ( geometry.y * height_dest ) / geometry.nh;
216 int width_src = ( geometry.w * width_dest ) / geometry.nw;
217 int height_src = ( geometry.h * height_dest ) / geometry.nh;
218
219 x -= x % 2;
220
221 // optimization points - no work to do
222 if ( width_src <= 0 || height_src <= 0 )
223 return ret;
224
225 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
226 return ret;
227
228 format_src = mlt_image_yuv422;
229 format_dest = mlt_image_yuv422;
230
231 mlt_frame_get_image( that, &p_src, &format_src, &width_src, &height_src, 1 /* writable */ );
232
233 stride_src = width_src * 2;
234 stride_dest = width_dest * 2;
235
236 // crop overlay off the left edge of frame
237 if ( x < 0 )
238 {
239 x_src = -x;
240 width_src -= x_src;
241 x = 0;
242 }
243
244 // crop overlay beyond right edge of frame
245 else if ( x + width_src > width_dest )
246 width_src = width_dest - x;
247
248 // crop overlay off the top edge of the frame
249 if ( y < 0 )
250 {
251 y_src = -y;
252 height_src -= y_src;
253 }
254 // crop overlay below bottom edge of frame
255 else if ( y + height_src > height_dest )
256 height_src = height_dest - y;
257
258 // offset pointer into overlay buffer based on cropping
259 p_src += x_src * 2 + y_src * stride_src;
260
261 // offset pointer into frame buffer based upon positive, even coordinates only!
262 p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
263
264 // Get the alpha channel of the overlay
265 uint8_t *p_alpha = mlt_frame_get_alpha_mask( that );
266
267 // offset pointer into alpha channel based upon cropping
268 if ( p_alpha )
269 p_alpha += x_src + y_src * stride_src / 2;
270
271 uint8_t *p = p_src;
272 uint8_t *q = p_dest;
273 uint8_t *o = p_dest;
274 uint8_t *z = p_alpha;
275
276 uint8_t Y;
277 uint8_t UV;
278 uint8_t a;
279 float value;
280
281 // now do the compositing only to cropped extents
282 for ( i = 0; i < height_src; i++ )
283 {
284 p = p_src;
285 q = p_dest;
286 o = p_dest;
287 z = p_alpha;
288
289 for ( j = 0; j < width_src; j ++ )
290 {
291 Y = *p ++;
292 UV = *p ++;
293 a = ( z == NULL ) ? 255 : *z ++;
294 value = ( weight * ( float ) a / 255.0 );
295 *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
296 *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
297 }
298
299 p_src += stride_src;
300 p_dest += stride_dest;
301 if ( p_alpha )
302 p_alpha += stride_src / 2;
303 }
304
305 return ret;
306 }
307
308
309 /** Get the image.
310 */
311
312 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
313 {
314 // Get the b frame from the stack
315 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
316
317 // Get the image from the a frame
318 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
319
320 if ( b_frame != NULL )
321 {
322 // Get the properties of the a frame
323 mlt_properties a_props = mlt_frame_properties( a_frame );
324
325 // Get the properties of the b frame
326 mlt_properties b_props = mlt_frame_properties( b_frame );
327
328 // Get the transition from the b frame
329 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
330
331 // Get the properties from the transition
332 mlt_properties properties = mlt_transition_properties( this );
333
334 // Structures for geometry
335 struct geometry_s result;
336 struct geometry_s start;
337 struct geometry_s end;
338
339 // Calculate the position
340 float position = position_calculate( this, a_frame );
341
342 // Obtain the normalised width and height from the a_frame
343 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
344 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
345
346 // Now parse the geometries
347 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
348 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
349
350 // Do the calculation
351 geometry_calculate( &result, &start, &end, position );
352
353 // Since we are the consumer of the b_frame, we must pass along these
354 // consumer properties from the a_frame
355 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
356 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
357
358 // Composite the b_frame on the a_frame
359 composite_yuv( *image, *format, *width, *height, b_frame, result );
360 }
361
362 return 0;
363 }
364
365 /** Composition transition processing.
366 */
367
368 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
369 {
370 // Propogate the transition properties to the b frame
371 mlt_properties b_props = mlt_frame_properties( b_frame );
372 mlt_properties_set_data( b_props, "transition_composite", this, 0, NULL, NULL );
373 mlt_frame_push_get_image( a_frame, transition_get_image );
374 mlt_frame_push_frame( a_frame, b_frame );
375 return a_frame;
376 }
377
378 /** Constructor for the filter.
379 */
380
381 mlt_transition transition_composite_init( char *arg )
382 {
383 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
384 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
385 {
386 this->process = composite_process;
387 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
388 mlt_properties_set( mlt_transition_properties( this ), "end", "" );
389 }
390 return this;
391 }
392