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>
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 "transition_composite.h"
22 #include <framework/mlt_frame.h>
41 /** Parse a value from a geometry string.
44 static float parse_value( char **ptr
, int normalisation
, char delim
, float defaults
)
46 float value
= defaults
;
48 if ( *ptr
!= NULL
&& **ptr
!= '\0' )
51 value
= strtod( *ptr
, &end
);
55 value
= ( value
/ 100.0 ) * normalisation
;
56 while ( *end
== delim
|| *end
== '%' )
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.
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
->mix
= defaults
->mix
;
90 // Parse the geomtry string
91 if ( property
!= NULL
)
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
);
102 /** Calculate real geometry.
105 static void geometry_calculate( struct geometry_s
*output
, struct geometry_s
*in
, struct geometry_s
*out
, float position
)
107 // Calculate this frames geometry
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
;
117 /** Calculate the position for this frame.
120 static float position_calculate( mlt_transition
this, mlt_frame frame
)
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 );
126 // Get the position of the frame
127 mlt_position position
= mlt_frame_get_position( frame
);
130 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
133 static int get_value( mlt_properties properties
, char *preferred
, char *fallback
)
135 int value
= mlt_properties_get_int( properties
, preferred
);
137 value
= mlt_properties_get_int( properties
, fallback
);
141 /** Composite function.
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
)
151 int x_src
= 0, y_src
= 0;
153 mlt_image_format format_src
= format_dest
;
154 float weight
= geometry
.mix
/ 100;
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 );
161 if ( mlt_properties_get( properties
, "distort" ) == NULL
)
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
;
175 // Now ensure that our images fit in the normalised frame
176 if ( scaled_width
> normalised_width
)
178 scaled_height
= scaled_height
* normalised_width
/ scaled_width
;
179 scaled_width
= normalised_width
;
181 if ( scaled_height
> normalised_height
)
183 scaled_width
= scaled_width
* normalised_height
/ scaled_height
;
184 scaled_height
= normalised_height
;
188 if ( scaled_height
== normalised_height
)
189 scaled_width
= normalised_width
;
191 // Now we need to align to the geometry
192 if ( scaled_width
<= geometry
.w
&& scaled_height
<= geometry
.h
)
194 // TODO: Should take into account requested alignment here...
195 // Assume centred alignment for now
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" );
205 mlt_properties_set( b_props
, "distort", "true" );
210 // We want to ensure that we bypass resize now...
211 mlt_properties_set( b_props
, "distort", "true" );
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
;
221 // optimization points - no work to do
222 if ( width_src
<= 0 || height_src
<= 0 )
225 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
228 format_src
= mlt_image_yuv422
;
229 format_dest
= mlt_image_yuv422
;
231 mlt_frame_get_image( that
, &p_src
, &format_src
, &width_src
, &height_src
, 1 /* writable */ );
233 stride_src
= width_src
* 2;
234 stride_dest
= width_dest
* 2;
236 // crop overlay off the left edge of frame
244 // crop overlay beyond right edge of frame
245 else if ( x
+ width_src
> width_dest
)
246 width_src
= width_dest
- x
;
248 // crop overlay off the top edge of the frame
254 // crop overlay below bottom edge of frame
255 else if ( y
+ height_src
> height_dest
)
256 height_src
= height_dest
- y
;
258 // offset pointer into overlay buffer based on cropping
259 p_src
+= x_src
* 2 + y_src
* stride_src
;
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
;
264 // Get the alpha channel of the overlay
265 uint8_t *p_alpha
= mlt_frame_get_alpha_mask( that
);
267 // offset pointer into alpha channel based upon cropping
269 p_alpha
+= x_src
+ y_src
* stride_src
/ 2;
274 uint8_t *z
= p_alpha
;
281 // now do the compositing only to cropped extents
282 for ( i
= 0; i
< height_src
; i
++ )
289 for ( j
= 0; j
< width_src
; j
++ )
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
) );
300 p_dest
+= stride_dest
;
302 p_alpha
+= stride_src
/ 2;
312 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
314 // Get the b frame from the stack
315 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
317 // Get the image from the a frame
318 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
320 if ( b_frame
!= NULL
)
322 // Get the properties of the a frame
323 mlt_properties a_props
= mlt_frame_properties( a_frame
);
325 // Get the properties of the b frame
326 mlt_properties b_props
= mlt_frame_properties( b_frame
);
328 // Get the transition from the b frame
329 mlt_transition
this = mlt_properties_get_data( b_props
, "transition_composite", NULL
);
331 // Get the properties from the transition
332 mlt_properties properties
= mlt_transition_properties( this );
334 // Structures for geometry
335 struct geometry_s result
;
336 struct geometry_s start
;
337 struct geometry_s end
;
339 // Calculate the position
340 float position
= position_calculate( this, a_frame
);
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" );
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
);
350 // Do the calculation
351 geometry_calculate( &result
, &start
, &end
, position
);
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" ) );
358 // Composite the b_frame on the a_frame
359 composite_yuv( *image
, *format
, *width
, *height
, b_frame
, result
);
365 /** Composition transition processing.
368 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
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
);
378 /** Constructor for the filter.
381 mlt_transition
transition_composite_init( char *arg
)
383 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
384 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
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", "" );