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>
39 /** Parse a geometry property string.
42 static void geometry_parse( struct geometry_s
*geometry
, struct geometry_s
*defaults
, char *property
)
44 // Assign from defaults if available
45 if ( defaults
!= NULL
)
47 geometry
->x
= defaults
->x
;
48 geometry
->y
= defaults
->y
;
49 geometry
->w
= defaults
->w
;
50 geometry
->h
= defaults
->h
;
51 geometry
->mix
= defaults
->mix
;
58 // Parse the geomtry string
59 if ( property
!= NULL
)
60 sscanf( property
, "%f,%f:%fx%f:%f", &geometry
->x
, &geometry
->y
, &geometry
->w
, &geometry
->h
, &geometry
->mix
);
63 /** Calculate real geometry.
66 static void geometry_calculate( struct geometry_s
*output
, struct geometry_s
*in
, struct geometry_s
*out
, float position
)
68 // Calculate this frames geometry
69 output
->x
= in
->x
+ ( out
->x
- in
->x
) * position
;
70 output
->y
= in
->y
+ ( out
->y
- in
->y
) * position
;
71 output
->w
= in
->w
+ ( out
->w
- in
->w
) * position
;
72 output
->h
= in
->h
+ ( out
->h
- in
->h
) * position
;
73 output
->mix
= in
->mix
+ ( out
->mix
- in
->mix
) * position
;
76 /** Calculate the position for this frame.
79 static float position_calculate( mlt_transition
this, mlt_frame frame
)
81 // Get the in and out position
82 mlt_position in
= mlt_transition_get_in( this );
83 mlt_position out
= mlt_transition_get_out( this );
85 // Get the position of the frame
86 mlt_position position
= mlt_frame_get_position( frame
);
89 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
92 /** Composite function.
95 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
)
102 int x_src
= 0, y_src
= 0;
104 mlt_image_format format_src
= format_dest
;
105 int x
= ( int )( ( float )width_dest
* geometry
.x
/ 100 );
106 int y
= ( int )( ( float )height_dest
* geometry
.y
/ 100 );
107 float weight
= geometry
.mix
/ 100;
108 int width_src
= ( int )( ( float )width_dest
* geometry
.w
/ 100 );
109 int height_src
= ( int )( ( float )height_dest
* geometry
.h
/ 100 );
111 mlt_properties b_props
= mlt_frame_properties( that
);
112 mlt_transition
this = mlt_properties_get_data( b_props
, "transition_composite", NULL
);
113 mlt_properties properties
= mlt_transition_properties( this );
115 if ( mlt_properties_get( properties
, "distort" ) == NULL
&&
116 mlt_properties_get( mlt_frame_properties( that
), "real_width" ) != NULL
)
118 int width_b
= mlt_properties_get_int( mlt_frame_properties( that
), "real_width" );
119 int height_b
= mlt_properties_get_int( mlt_frame_properties( that
), "real_height" );
121 if ( width_b
< width_src
)
123 if ( height_b
< height_src
)
124 height_src
= height_b
;
125 mlt_properties_set( mlt_frame_properties( that
), "rescale.interp", "none" );
127 else if ( mlt_properties_get( mlt_frame_properties( that
), "real_width" ) != NULL
)
129 mlt_properties_set( mlt_frame_properties( that
), "rescale.interp", "none" );
134 if ( width_src
<= 0 || height_src
<= 0 )
137 // optimization point - no work to do
138 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
141 format_src
= mlt_image_yuv422
;
142 format_dest
= mlt_image_yuv422
;
144 mlt_frame_get_image( that
, &p_src
, &format_src
, &width_src
, &height_src
, 1 /* writable */ );
146 stride_src
= width_src
* 2;
147 stride_dest
= width_dest
* 2;
149 // crop overlay off the left edge of frame
157 // crop overlay beyond right edge of frame
158 else if ( x
+ width_src
> width_dest
)
159 width_src
= width_dest
- x
;
161 // crop overlay off the top edge of the frame
167 // crop overlay below bottom edge of frame
168 else if ( y
+ height_src
> height_dest
)
169 height_src
= height_dest
- y
;
171 // offset pointer into overlay buffer based on cropping
172 p_src
+= x_src
* 2 + y_src
* stride_src
;
174 // offset pointer into frame buffer based upon positive, even coordinates only!
175 p_dest
+= ( x
< 0 ?
0 : x
) * 2 + ( y
< 0 ?
0 : y
) * stride_dest
;
177 // Get the alpha channel of the overlay
178 uint8_t *p_alpha
= mlt_frame_get_alpha_mask( that
);
180 // offset pointer into alpha channel based upon cropping
182 p_alpha
+= x_src
+ y_src
* stride_src
/ 2;
187 uint8_t *z
= p_alpha
;
194 // now do the compositing only to cropped extents
195 for ( i
= 0; i
< height_src
; i
++ )
202 for ( j
= 0; j
< width_src
; j
++ )
206 a
= ( z
== NULL
) ?
255 : *z
++;
207 value
= ( weight
* ( float ) a
/ 255.0 );
208 *o
++ = (uint8_t)( Y
* value
+ *q
++ * ( 1 - value
) );
209 *o
++ = (uint8_t)( UV
* value
+ *q
++ * ( 1 - value
) );
213 p_dest
+= stride_dest
;
215 p_alpha
+= stride_src
/ 2;
225 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
227 // Get the b frame from the stack
228 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
230 // Get the image from the a frame
231 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
233 if ( b_frame
!= NULL
)
235 // Get the properties of the b frame
236 mlt_properties b_props
= mlt_frame_properties( b_frame
);
238 // Get the transition from the b frame
239 mlt_transition
this = mlt_properties_get_data( b_props
, "transition_composite", NULL
);
241 // Get the properties from the transition
242 mlt_properties properties
= mlt_transition_properties( this );
244 // Structures for geometry
245 struct geometry_s result
;
246 struct geometry_s start
;
247 struct geometry_s end
;
249 // Calculate the position
250 float position
= position_calculate( this, a_frame
);
252 // Now parse the geometries
253 geometry_parse( &start
, NULL
, mlt_properties_get( properties
, "start" ) );
254 geometry_parse( &end
, &start
, mlt_properties_get( properties
, "end" ) );
256 // Do the calculation
257 geometry_calculate( &result
, &start
, &end
, position
);
259 // Composite the b_frame on the a_frame
260 composite_yuv( *image
, *format
, *width
, *height
, b_frame
, result
);
266 /** Composition transition processing.
269 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
271 // Propogate the transition properties to the b frame
272 mlt_properties b_props
= mlt_frame_properties( b_frame
);
273 mlt_properties_set_data( b_props
, "transition_composite", this, 0, NULL
, NULL
);
274 mlt_frame_push_get_image( a_frame
, transition_get_image
);
275 mlt_frame_push_frame( a_frame
, b_frame
);
279 /** Constructor for the filter.
282 mlt_transition
transition_composite_init( char *arg
)
284 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
285 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
287 this->process
= composite_process
;
288 mlt_properties_set( mlt_transition_properties( this ), "start", arg
!= NULL ? arg
: "85,5:10x10" );
289 mlt_properties_set( mlt_transition_properties( this ), "end", "" );