efa251c3ece08c4de6bc85fdf397611a045d9154
[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 float x;
33 float y;
34 float w;
35 float h;
36 float mix;
37 };
38
39 /** Parse a geometry property string.
40 */
41
42 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property )
43 {
44 // Assign from defaults if available
45 if ( defaults != NULL )
46 {
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;
52 }
53 else
54 {
55 geometry->mix = 100;
56 }
57
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 );
61 }
62
63 /** Calculate real geometry.
64 */
65
66 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position )
67 {
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;
74 }
75
76 /** Calculate the position for this frame.
77 */
78
79 static float position_calculate( mlt_transition this, mlt_frame frame )
80 {
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 );
84
85 // Get the position of the frame
86 mlt_position position = mlt_frame_get_position( frame );
87
88 // Now do the calcs
89 return ( float )( position - in ) / ( float )( out - in + 1 );
90 }
91
92 /** Composite function.
93 */
94
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 )
96 {
97 int ret = 0;
98 uint8_t *p_src;
99 int i, j;
100 int stride_src;
101 int stride_dest;
102 int x_src = 0, y_src = 0;
103
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
109 // Compute the dimensioning rectangle
110 int width_src = ( int )( ( float )width_dest * geometry.w / 100 );
111 int height_src = ( int )( ( float )height_dest * geometry.h / 100 );
112
113 mlt_properties b_props = mlt_frame_properties( that );
114 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
115 mlt_properties properties = mlt_transition_properties( this );
116
117 if ( mlt_properties_get( properties, "distort" ) == NULL &&
118 mlt_properties_get( mlt_frame_properties( that ), "real_width" ) != NULL )
119 {
120 int width_b = mlt_properties_get_double( b_props, "real_width" );
121 int height_b = mlt_properties_get_double( b_props, "real_height" );
122
123 // See if we need to normalise pixel aspect ratio
124 // We can use consumer_aspect_ratio because the a_frame will take on this aspect
125 double aspect = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
126 if ( aspect != 0 )
127 {
128 // Derive the consumer pixel aspect
129 double oaspect = aspect / ( double )width_dest * height_dest;
130
131 // Get the b frame pixel aspect - usually 1
132 double iaspect = mlt_properties_get_double( b_props, "aspect_ratio" ) / width_b * height_b;
133
134 // Normalise pixel aspect
135 if ( iaspect != 0 && iaspect != oaspect )
136 width_b = iaspect / oaspect * ( double )width_b + 0.5;
137
138 // Tell rescale not to normalise display aspect
139 mlt_frame_set_aspect_ratio( that, aspect );
140 }
141
142 // Constrain the overlay to the dimensioning rectangle
143 if ( width_b < width_src )
144 width_src = width_b;
145 if ( height_b < height_src )
146 height_src = height_b;
147
148 // Adjust overall scale for consumer
149 double consumer_scale = mlt_properties_get_double( b_props, "consumer_scale" );
150 if ( consumer_scale > 0 )
151 {
152 width_src = consumer_scale * width_src + 0.5;
153 height_src = consumer_scale * height_src + 0.5;
154 }
155 }
156 else if ( mlt_properties_get( b_props, "real_width" ) != NULL )
157 {
158 // Tell rescale not to normalise display aspect
159 mlt_properties_set_double( b_props, "consumer_aspect_ratio", 0 );
160 }
161
162 x -= x % 2;
163
164 // optimization points - no work to do
165 if ( width_src <= 0 || height_src <= 0 )
166 return ret;
167
168 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
169 return ret;
170
171 format_src = mlt_image_yuv422;
172 format_dest = mlt_image_yuv422;
173
174 mlt_frame_get_image( that, &p_src, &format_src, &width_src, &height_src, 1 /* writable */ );
175
176 stride_src = width_src * 2;
177 stride_dest = width_dest * 2;
178
179 // crop overlay off the left edge of frame
180 if ( x < 0 )
181 {
182 x_src = -x;
183 width_src -= x_src;
184 x = 0;
185 }
186
187 // crop overlay beyond right edge of frame
188 else if ( x + width_src > width_dest )
189 width_src = width_dest - x;
190
191 // crop overlay off the top edge of the frame
192 if ( y < 0 )
193 {
194 y_src = -y;
195 height_src -= y_src;
196 }
197 // crop overlay below bottom edge of frame
198 else if ( y + height_src > height_dest )
199 height_src = height_dest - y;
200
201 // offset pointer into overlay buffer based on cropping
202 p_src += x_src * 2 + y_src * stride_src;
203
204 // offset pointer into frame buffer based upon positive, even coordinates only!
205 p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
206
207 // Get the alpha channel of the overlay
208 uint8_t *p_alpha = mlt_frame_get_alpha_mask( that );
209
210 // offset pointer into alpha channel based upon cropping
211 if ( p_alpha )
212 p_alpha += x_src + y_src * stride_src / 2;
213
214 uint8_t *p = p_src;
215 uint8_t *q = p_dest;
216 uint8_t *o = p_dest;
217 uint8_t *z = p_alpha;
218
219 uint8_t Y;
220 uint8_t UV;
221 uint8_t a;
222 float value;
223
224 // now do the compositing only to cropped extents
225 for ( i = 0; i < height_src; i++ )
226 {
227 p = p_src;
228 q = p_dest;
229 o = p_dest;
230 z = p_alpha;
231
232 for ( j = 0; j < width_src; j ++ )
233 {
234 Y = *p ++;
235 UV = *p ++;
236 a = ( z == NULL ) ? 255 : *z ++;
237 value = ( weight * ( float ) a / 255.0 );
238 *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
239 *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
240 }
241
242 p_src += stride_src;
243 p_dest += stride_dest;
244 if ( p_alpha )
245 p_alpha += stride_src / 2;
246 }
247
248 return ret;
249 }
250
251
252 /** Get the image.
253 */
254
255 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
256 {
257 // Get the b frame from the stack
258 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
259
260 // Get the image from the a frame
261 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
262
263 if ( b_frame != NULL )
264 {
265 // Get the properties of the b frame
266 mlt_properties b_props = mlt_frame_properties( b_frame );
267
268 // Get the transition from the b frame
269 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
270
271 // Get the properties from the transition
272 mlt_properties properties = mlt_transition_properties( this );
273
274 // Structures for geometry
275 struct geometry_s result;
276 struct geometry_s start;
277 struct geometry_s end;
278
279 // Calculate the position
280 float position = position_calculate( this, a_frame );
281
282 // Now parse the geometries
283 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ) );
284 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ) );
285
286 // Do the calculation
287 geometry_calculate( &result, &start, &end, position );
288
289 // Since we are the consumer of the b_frame, we must pass along these
290 // consumer properties from the a_frame
291 mlt_properties_set_double( b_props, "consumer_aspect_ratio",
292 mlt_properties_get_double( mlt_frame_properties( a_frame ), "consumer_aspect_ratio" ) );
293 mlt_properties_set_double( b_props, "consumer_scale",
294 mlt_properties_get_double( mlt_frame_properties( a_frame ), "consumer_scale" ) );
295
296 // Composite the b_frame on the a_frame
297 composite_yuv( *image, *format, *width, *height, b_frame, result );
298 }
299
300 return 0;
301 }
302
303 /** Composition transition processing.
304 */
305
306 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
307 {
308 // Propogate the transition properties to the b frame
309 mlt_properties b_props = mlt_frame_properties( b_frame );
310 mlt_properties_set_data( b_props, "transition_composite", this, 0, NULL, NULL );
311 mlt_frame_push_get_image( a_frame, transition_get_image );
312 mlt_frame_push_frame( a_frame, b_frame );
313 return a_frame;
314 }
315
316 /** Constructor for the filter.
317 */
318
319 mlt_transition transition_composite_init( char *arg )
320 {
321 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
322 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
323 {
324 this->process = composite_process;
325 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85,5:10x10" );
326 mlt_properties_set( mlt_transition_properties( this ), "end", "" );
327 }
328 return this;
329 }
330