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