]> git.sesse.net Git - mlt/blob - src/modules/core/transition_composite.c
service stack, various fixes
[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 #include <ctype.h>
27 #include <string.h>
28
29 /** Geometry struct.
30 */
31
32 struct geometry_s
33 {
34         float mix;
35         int nw; // normalised width
36         int nh; // normalised height
37         int sw; // scaled width, not including consumer scale based upon w/nw
38         int sh; // scaled height, not including consumer scale based upon h/nh
39         float x;
40         float y;
41         float w;
42         float h;
43         int halign; // horizontal alignment: 0=left, 1=center, 2=right
44         int valign; // vertical alignment: 0=top, 1=middle, 2=bottom
45 };
46
47 /** Parse a value from a geometry string.
48 */
49
50 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
51 {
52         float value = defaults;
53
54         if ( *ptr != NULL && **ptr != '\0' )
55         {
56                 char *end = NULL;
57                 value = strtod( *ptr, &end );
58                 if ( end != NULL )
59                 {
60                         if ( *end == '%' )
61                                 value = ( value / 100.0 ) * normalisation;
62                         while ( *end == delim || *end == '%' )
63                                 end ++;
64                 }
65                 *ptr = end;
66         }
67
68         return value;
69 }
70
71 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be 
72         expressed as a percentage by appending a % after the value, otherwise values are
73         assumed to be relative to the normalised dimensions of the consumer.
74 */
75
76 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
77 {
78         memset( geometry, 0, sizeof( struct geometry_s ) );
79
80         // Assign normalised width and height
81         geometry->nw = nw;
82         geometry->nh = nh;
83
84         // Assign from defaults if available
85         if ( defaults != NULL )
86         {
87                 geometry->x = defaults->x;
88                 geometry->y = defaults->y;
89                 geometry->w = geometry->sw = defaults->w;
90                 geometry->h = geometry->sh = defaults->h;
91                 geometry->mix = defaults->mix;
92         }
93         else
94         {
95                 geometry->mix = 100;
96         }
97
98         // Parse the geomtry string
99         if ( property != NULL )
100         {
101                 char *ptr = property;
102                 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
103                 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
104                 geometry->w = geometry->sw = parse_value( &ptr, nw, 'x', geometry->w );
105                 geometry->h = geometry->sh = parse_value( &ptr, nh, ':', geometry->h );
106                 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
107         }
108 }
109
110 /** Calculate real geometry.
111 */
112
113 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position )
114 {
115         // Calculate this frames geometry
116         output->nw = in->nw;
117         output->nh = in->nh;
118         output->x = in->x + ( out->x - in->x ) * position + 0.5;
119         output->y = in->y + ( out->y - in->y ) * position + 0.5;
120         output->w = in->w + ( out->w - in->w ) * position;
121         output->h = in->h + ( out->h - in->h ) * position;
122         output->mix = in->mix + ( out->mix - in->mix ) * position;
123         if ( output->mix > 100 )
124         fprintf( stderr, "%f = %f + ( %f - %f ) * %f\n", output->mix, in->mix, out->mix, in->mix, position );
125 }
126
127 /** Parse the alignment properties into the geometry.
128 */
129
130 static int alignment_parse( char* align )
131 {
132         int ret = 0;
133         
134         if ( align == NULL );
135         else if ( isdigit( align[ 0 ] ) )
136                 ret = atoi( align );
137         else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
138                 ret = 1;
139         else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
140                 ret = 2;
141
142         return ret;
143 }
144
145 /** Adjust position according to scaled size and alignment properties.
146 */
147
148 static void alignment_calculate( struct geometry_s *geometry )
149 {
150         geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2 + 0.5;
151         geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2 + 0.5;
152 }
153
154 /** Calculate the position for this frame.
155 */
156
157 static inline float position_calculate( mlt_transition this, mlt_frame frame )
158 {
159         // Get the in and out position
160         mlt_position in = mlt_transition_get_in( this );
161         mlt_position out = mlt_transition_get_out( this );
162
163         // Get the position
164         mlt_position position = mlt_frame_get_position( frame );
165
166         // Now do the calcs
167         return ( float )( position - in ) / ( float )( out - in + 1 );
168 }
169
170 /** Calculate the field delta for this frame - position between two frames.
171 */
172
173 static inline float delta_calculate( mlt_transition this, mlt_frame frame )
174 {
175         // Get the in and out position
176         mlt_position in = mlt_transition_get_in( this );
177         mlt_position out = mlt_transition_get_out( this );
178
179         // Get the position of the frame
180         mlt_position position = mlt_frame_get_position( frame );
181
182         // Now do the calcs
183         float x = ( float )( position - in ) / ( float )( out - in + 1 );
184         float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
185
186         return ( y - x ) / 2.0;
187 }
188
189 static int get_value( mlt_properties properties, char *preferred, char *fallback )
190 {
191         int value = mlt_properties_get_int( properties, preferred );
192         if ( value == 0 )
193                 value = mlt_properties_get_int( properties, fallback );
194         return value;
195 }
196
197 /** Composite function.
198 */
199
200 static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, int bpp, uint8_t *p_src, int width_src, int height_src, uint8_t *p_alpha, struct geometry_s geometry, int field )
201 {
202         int ret = 0;
203         int i, j;
204         int x_src = 0, y_src = 0;
205         int32_t weight = ( 1 << 16 ) * ( geometry.mix / 100 );
206         if ( geometry.mix > 100 )
207                 fprintf( stderr, "%f %d\n", geometry.mix, weight );
208         int stride_src = width_src * bpp;
209         int stride_dest = width_dest * bpp;
210
211         // Adjust to consumer scale
212         int x = geometry.x * width_dest / geometry.nw + 0.5;
213         int y = geometry.y * height_dest / geometry.nh + 0.5;
214
215         if ( bpp == 2 )
216                 x -= x % 2;
217                 
218         // optimization points - no work to do
219         if ( width_src <= 0 || height_src <= 0 )
220                 return ret;
221
222         if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
223                 return ret;
224
225         // crop overlay off the left edge of frame
226         if ( x < 0 )
227         {
228                 x_src = -x;
229                 width_src -= x_src;
230                 x = 0;
231         }
232         
233         // crop overlay beyond right edge of frame
234         else if ( x + width_src > width_dest )
235                 width_src = width_dest - x;
236
237         // crop overlay off the top edge of the frame
238         if ( y < 0 )
239         {
240                 y_src = -y;
241                 height_src -= y_src;
242         }
243         // crop overlay below bottom edge of frame
244         else if ( y + height_src > height_dest )
245                 height_src = height_dest - y;
246
247         // offset pointer into overlay buffer based on cropping
248         p_src += x_src * bpp + y_src * stride_src;
249
250         // offset pointer into frame buffer based upon positive coordinates only!
251         p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
252
253         // offset pointer into alpha channel based upon cropping
254         if ( p_alpha )
255                 p_alpha += x_src + y_src * stride_src / bpp;
256
257         // Assuming lower field first
258         // Special care is taken to make sure the b_frame is aligned to the correct field.
259         // field 0 = lower field and y should be odd (y is 0-based).
260         // field 1 = upper field and y should be even.
261         if ( ( field > -1 ) && ( y % 2 == field ) )
262         {
263                 //fprintf( stderr, "field %d y %d\n", field, y );
264                 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
265                         p_dest += stride_dest;
266                 else
267                         p_dest -= stride_dest;
268         }
269
270         // On the second field, use the other lines from b_frame
271         if ( field == 1 )
272         {
273                 p_src += stride_src;
274                 if ( p_alpha )
275                         p_alpha += stride_src / bpp;
276                 height_src--;
277         }
278
279         uint8_t *p = p_src;
280         uint8_t *q = p_dest;
281         uint8_t *o = p_dest;
282         uint8_t *z = p_alpha;
283
284         uint8_t a;
285         int32_t value;
286         int step = ( field > -1 ) ? 2 : 1;
287
288         stride_src = stride_src * step;
289         int alpha_stride = stride_src / bpp;
290         stride_dest = stride_dest * step;
291
292         // now do the compositing only to cropped extents
293         for ( i = 0; i < height_src; i += step )
294         {
295                 p = p_src;
296                 q = p_dest;
297                 o = q;
298                 z = p_alpha;
299
300                 for ( j = 0; j < width_src; j ++ )
301                 {
302                         a = ( z == NULL ) ? 255 : *z ++;
303                         value = ( weight * ( a + 1 ) ) >> 8;
304                         *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
305                         *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
306                 }
307
308                 p_src += stride_src;
309                 p_dest += stride_dest;
310                 if ( p_alpha )
311                         p_alpha += alpha_stride;
312         }
313
314         return ret;
315 }
316
317
318 /** Get the properly sized image from b_frame.
319 */
320
321 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
322 {
323         int ret = 0;
324         mlt_image_format format = mlt_image_yuv422;
325
326         // Initialise the scaled dimensions from the computed
327         geometry->sw = geometry->w;
328         geometry->sh = geometry->h;
329
330         // Compute the dimensioning rectangle
331         mlt_properties b_props = mlt_frame_properties( b_frame );
332         mlt_properties properties = mlt_transition_properties( this );
333
334         if ( mlt_properties_get( properties, "distort" ) == NULL )
335         {
336                 // Adjust b_frame pixel aspect
337                 int normalised_width = geometry->w;
338                 int normalised_height = geometry->h;
339                 int real_width = get_value( b_props, "real_width", "width" );
340                 int real_height = get_value( b_props, "real_height", "height" );
341                 int scaled_width = real_width;
342                 int scaled_height = real_height;
343
344                 // Now ensure that our images fit in the normalised frame
345                 if ( scaled_width > normalised_width )
346                 {
347                         scaled_height = scaled_height * normalised_width / scaled_width;
348                         scaled_width = normalised_width;
349                 }
350                 if ( scaled_height > normalised_height )
351                 {
352                         scaled_width = scaled_width * normalised_height / scaled_height;
353                         scaled_height = normalised_height;
354                 }
355
356                 // Now we need to align to the geometry
357                 if ( scaled_width <= geometry->w && scaled_height <= geometry->h )
358                 {
359                         // Save the new scaled dimensions
360                         geometry->sw = scaled_width;
361                         geometry->sh = scaled_height;
362                 }
363         }
364
365         // We want to ensure that we bypass resize now...
366         mlt_properties_set( b_props, "distort", "true" );
367
368         // Take into consideration alignment for optimisation
369         alignment_calculate( geometry );
370
371         // Adjust to consumer scale
372         int x = geometry->x * *width / geometry->nw + 0.5;
373         int y = geometry->y * *height / geometry->nh + 0.5;
374         *width = geometry->sw * *width / geometry->nw;
375         *height = geometry->sh * *height / geometry->nh;
376
377         x -= x % 2;
378
379         // optimization points - no work to do
380         if ( *width <= 0 || *height <= 0 )
381                 return 1;
382
383         if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
384                 return 1;
385
386         ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
387
388         return ret;
389 }
390
391
392 static uint8_t *transition_get_alpha_mask( mlt_frame this )
393 {
394         // Obtain properties of frame
395         mlt_properties properties = mlt_frame_properties( this );
396
397         // Return the alpha mask
398         return mlt_properties_get_data( properties, "alpha", NULL );
399 }
400
401 /** Get the image.
402 */
403
404 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
405 {
406         // Get the b frame from the stack
407         mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
408
409         // This compositer is yuv422 only
410         *format = mlt_image_yuv422;
411
412         // Get the transition from the a frame
413         mlt_transition this = mlt_frame_pop_service( a_frame );
414
415         // Get the image from the a frame
416         mlt_frame_get_image( a_frame, image, format, width, height, 1 );
417
418         if ( b_frame != NULL )
419         {
420                 // Get the properties of the a frame
421                 mlt_properties a_props = mlt_frame_properties( a_frame );
422
423                 // Get the properties of the b frame
424                 mlt_properties b_props = mlt_frame_properties( b_frame );
425
426                 // Get the properties from the transition
427                 mlt_properties properties = mlt_transition_properties( this );
428
429                 // Structures for geometry
430                 struct geometry_s result;
431                 struct geometry_s start;
432                 struct geometry_s end;
433
434                 // Calculate the position
435                 float position = mlt_properties_get_double( b_props, "relative_position" );
436                 float delta = delta_calculate( this, a_frame );
437
438                 // Obtain the normalised width and height from the a_frame
439                 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
440                 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
441
442                 // Now parse the geometries
443                 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
444                 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
445
446                 // Now parse the alignment
447                 result.halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
448                 result.valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
449
450                 // Since we are the consumer of the b_frame, we must pass along these
451                 // consumer properties from the a_frame
452                 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
453                 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
454
455                 // Do the calculation
456                 geometry_calculate( &result, &start, &end, position );
457
458                 // Get the image from the b frame
459                 uint8_t *image_b;
460                 int width_b = *width;
461                 int height_b = *height;
462                 
463                 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
464                 {
465                         uint8_t *dest = *image;
466                         uint8_t *src = image_b;
467                         int bpp = 2;
468                         uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
469                         int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
470                                         mlt_properties_get_int( a_props, "consumer_progressive" ) ||
471                                         mlt_properties_get_int( properties, "progressive" );
472                         int field;
473
474                         for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
475                         {
476                                 // Assume lower field (0) first
477                                 float field_position = position + field * delta;
478                                 
479                                 // Do the calculation
480                                 geometry_calculate( &result, &start, &end, field_position );
481
482                                 // Align
483                                 alignment_calculate( &result );
484
485                                 // Composite the b_frame on the a_frame
486                                 composite_yuv( dest, *width, *height, bpp, src, width_b, height_b, alpha, result, progressive ? -1 : field );
487                         }
488                 }
489         }
490
491         return 0;
492 }
493
494 /** Composition transition processing.
495 */
496
497 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
498 {
499         // Propogate the transition properties to the b frame
500         mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, a_frame ) );
501         mlt_frame_push_service( a_frame, this );
502         mlt_frame_push_get_image( a_frame, transition_get_image );
503         mlt_frame_push_frame( a_frame, b_frame );
504         return a_frame;
505 }
506
507 /** Constructor for the filter.
508 */
509
510 mlt_transition transition_composite_init( char *arg )
511 {
512         mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
513         if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
514         {
515                 this->process = composite_process;
516                 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
517                 mlt_properties_set( mlt_transition_properties( this ), "end", "" );
518         }
519         return this;
520 }
521