]> git.sesse.net Git - mlt/blob - src/modules/core/transition_composite.c
split getting of b_frame image and composite
[mlt] / 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, int width_dest, int height_dest, uint8_t *p_src, int width_src, int height_src, uint8_t *p_alpha, struct geometry_s geometry )
145 {
146         int ret = 0;
147         int i, j;
148         int x_src = 0, y_src = 0;
149         float weight = geometry.mix / 100;
150         int x = ( geometry.x * width_dest ) / geometry.nw;
151         int y = ( geometry.y * height_dest ) / geometry.nh;
152         int stride_src = width_src * 2;
153         int stride_dest = width_dest * 2;
154
155         x -= x % 2;
156
157         // optimization points - no work to do
158         if ( width_src <= 0 || height_src <= 0 )
159                 return ret;
160
161         if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
162                 return ret;
163
164         // crop overlay off the left edge of frame
165         if ( x < 0 )
166         {
167                 x_src = -x;
168                 width_src -= x_src;
169                 x = 0;
170         }
171         
172         // crop overlay beyond right edge of frame
173         else if ( x + width_src > width_dest )
174                 width_src = width_dest - x;
175
176         // crop overlay off the top edge of the frame
177         if ( y < 0 )
178         {
179                 y_src = -y;
180                 height_src -= y_src;
181         }
182         // crop overlay below bottom edge of frame
183         else if ( y + height_src > height_dest )
184                 height_src = height_dest - y;
185
186         // offset pointer into overlay buffer based on cropping
187         p_src += x_src * 2 + y_src * stride_src;
188
189         // offset pointer into frame buffer based upon positive, even coordinates only!
190         p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
191
192         // offset pointer into alpha channel based upon cropping
193         if ( p_alpha )
194                 p_alpha += x_src + y_src * stride_src / 2;
195
196         uint8_t *p = p_src;
197         uint8_t *q = p_dest;
198         uint8_t *o = p_dest;
199         uint8_t *z = p_alpha;
200
201         uint8_t Y;
202         uint8_t UV;
203         uint8_t a;
204         float value;
205
206         // now do the compositing only to cropped extents
207         for ( i = 0; i < height_src; i++ )
208         {
209                 p = p_src;
210                 q = p_dest;
211                 o = p_dest;
212                 z = p_alpha;
213
214                 for ( j = 0; j < width_src; j ++ )
215                 {
216                         Y = *p ++;
217                         UV = *p ++;
218                         a = ( z == NULL ) ? 255 : *z ++;
219                         value = ( weight * ( float ) a / 255.0 );
220                         *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
221                         *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
222                 }
223
224                 p_src += stride_src;
225                 p_dest += stride_dest;
226                 if ( p_alpha )
227                         p_alpha += stride_src / 2;
228         }
229
230         return ret;
231 }
232
233
234 /** Get the properly sized image from b_frame.
235 */
236
237 static int get_b_frame_image( mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
238 {
239         int ret = 0;
240         mlt_image_format format = mlt_image_yuv422;
241
242         // Compute the dimensioning rectangle
243         mlt_properties b_props = mlt_frame_properties( b_frame );
244         mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
245         mlt_properties properties = mlt_transition_properties( this );
246
247         if ( mlt_properties_get( properties, "distort" ) == NULL )
248         {
249                 // Now do additional calcs based on real_width/height etc
250                 //int normalised_width = mlt_properties_get_int( b_props, "normalised_width" );
251                 //int normalised_height = mlt_properties_get_int( b_props, "normalised_height" );
252                 int normalised_width = geometry->w;
253                 int normalised_height = geometry->h;
254                 int real_width = get_value( b_props, "real_width", "width" );
255                 int real_height = get_value( b_props, "real_height", "height" );
256                 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
257                 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
258                 int scaled_width = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_width;
259                 int scaled_height = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_height;
260
261                 // Now ensure that our images fit in the normalised frame
262                 if ( scaled_width > normalised_width )
263                 {
264                         scaled_height = scaled_height * normalised_width / scaled_width;
265                         scaled_width = normalised_width;
266                 }
267                 if ( scaled_height > normalised_height )
268                 {
269                         scaled_width = scaled_width * normalised_height / scaled_height;
270                         scaled_height = normalised_height;
271                 }
272
273                 // Special case
274                 if ( scaled_height == normalised_height )
275                         scaled_width = normalised_width;
276
277                 // Now we need to align to the geometry
278                 if ( scaled_width <= geometry->w && scaled_height <= geometry->h )
279                 {
280                         // TODO: Should take into account requested alignment here...
281                         // Assume centred alignment for now
282
283                         geometry->x = geometry->x + ( geometry->w - scaled_width ) / 2;
284                         geometry->y = geometry->y + ( geometry->h - scaled_height ) / 2;
285                         geometry->w = scaled_width;
286                         geometry->h = scaled_height;
287                         mlt_properties_set( b_props, "distort", "true" );
288                 }
289                 else
290                 {
291                         mlt_properties_set( b_props, "distort", "true" );
292                 }
293         }
294         else
295         {
296                 // We want to ensure that we bypass resize now...
297                 mlt_properties_set( b_props, "distort", "true" );
298         }
299
300         int x = ( geometry->x * *width ) / geometry->nw;
301         int y = ( geometry->y * *height ) / geometry->nh;
302         *width = ( geometry->w * *width ) / geometry->nw;
303         *height = ( geometry->h * *height ) / geometry->nh;
304
305         x -= x % 2;
306
307         // optimization points - no work to do
308         if ( *width <= 0 || *height <= 0 )
309                 return 1;
310
311         if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
312                 return 1;
313
314         ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 /* writable */ );
315
316         return ret;
317 }
318
319
320 /** Get the image.
321 */
322
323 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
324 {
325         // Get the b frame from the stack
326         mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
327
328         // This compositer is yuv422 only
329         *format = mlt_image_yuv422;
330
331         // Get the image from the a frame
332         mlt_frame_get_image( a_frame, image, format, width, height, 1 );
333
334         if ( b_frame != NULL )
335         {
336                 // Get the properties of the a frame
337                 mlt_properties a_props = mlt_frame_properties( a_frame );
338
339                 // Get the properties of the b frame
340                 mlt_properties b_props = mlt_frame_properties( b_frame );
341
342                 // Get the transition from the b frame
343                 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
344
345                 // Get the properties from the transition
346                 mlt_properties properties = mlt_transition_properties( this );
347
348                 // Structures for geometry
349                 struct geometry_s result;
350                 struct geometry_s start;
351                 struct geometry_s end;
352
353                 // Calculate the position
354                 float position = position_calculate( this, a_frame );
355
356                 // Obtain the normalised width and height from the a_frame
357                 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
358                 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
359
360                 // Now parse the geometries
361                 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
362                 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
363
364                 // Do the calculation
365                 geometry_calculate( &result, &start, &end, position );
366
367                 // Since we are the consumer of the b_frame, we must pass along these
368                 // consumer properties from the a_frame
369                 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
370                 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
371
372                 // Get the image from the b frame
373                 uint8_t *image_b;
374                 int width_b = *width;
375                 int height_b = *height;
376                 if ( get_b_frame_image( b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
377                 {
378                         uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
379                         
380                         // Composite the b_frame on the a_frame
381                         composite_yuv( *image, *width, *height, image_b, width_b, height_b, alpha, result );
382                 }
383         }
384
385         return 0;
386 }
387
388 /** Composition transition processing.
389 */
390
391 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
392 {
393         // Propogate the transition properties to the b frame
394         mlt_properties b_props = mlt_frame_properties( b_frame );
395         mlt_properties_set_data( b_props, "transition_composite", this, 0, NULL, NULL );
396         mlt_frame_push_get_image( a_frame, transition_get_image );
397         mlt_frame_push_frame( a_frame, b_frame );
398         return a_frame;
399 }
400
401 /** Constructor for the filter.
402 */
403
404 mlt_transition transition_composite_init( char *arg )
405 {
406         mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
407         if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
408         {
409                 this->process = composite_process;
410                 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
411                 mlt_properties_set( mlt_transition_properties( this ), "end", "" );
412         }
413         return this;
414 }
415