]> git.sesse.net Git - mlt/blob - src/modules/core/transition_composite.c
+ Clean ups and corrections
[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.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <math.h>
29
30 typedef void ( *composite_line_fn )( uint8_t *dest, uint8_t *src, int width_src, uint8_t *alpha_b, uint8_t *alpha_a, int weight, uint16_t *luma, int softness );
31
32 /** Geometry struct.
33 */
34
35 struct geometry_s
36 {
37         struct mlt_geometry_item_s item;
38         int nw; // normalised width
39         int nh; // normalised height
40         int sw; // scaled width, not including consumer scale based upon w/nw
41         int sh; // scaled height, not including consumer scale based upon h/nh
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 the alignment properties into the geometry.
47 */
48
49 static int alignment_parse( char* align )
50 {
51         int ret = 0;
52         
53         if ( align == NULL );
54         else if ( isdigit( align[ 0 ] ) )
55                 ret = atoi( align );
56         else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
57                 ret = 1;
58         else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
59                 ret = 2;
60
61         return ret;
62 }
63
64 /** Calculate real geometry.
65 */
66
67 static void geometry_calculate( mlt_transition this, struct geometry_s *output, double position )
68 {
69         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
70         mlt_geometry geometry = mlt_properties_get_data( properties, "geometries", NULL );
71         int mirror_off = mlt_properties_get_int( properties, "mirror_off" );
72         int repeat_off = mlt_properties_get_int( properties, "repeat_off" );
73         int length = mlt_geometry_get_length( geometry );
74
75         // Allow wrapping
76         if ( !repeat_off && position >= length && length != 0 )
77         {
78                 int section = position / length;
79                 position -= section * length;
80                 if ( !mirror_off && section % 2 == 1 )
81                         position = length - position;
82         }
83
84         // Fetch the key for the position
85         mlt_geometry_fetch( geometry, &output->item, position );
86 }
87
88 static mlt_geometry transition_parse_keys( mlt_transition this, int normalised_width, int normalised_height )
89 {
90         // Loop variable for property interrogation
91         int i = 0;
92
93         // Get the properties of the transition
94         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
95
96         // Create an empty geometries object
97         mlt_geometry geometry = mlt_geometry_init( );
98
99         // Get the in and out position
100         mlt_position in = mlt_transition_get_in( this );
101         mlt_position out = mlt_transition_get_out( this );
102         int length = out - in + 1;
103         double cycle = mlt_properties_get_double( properties, "cycle" );
104
105         // Get the new style geometry string
106         char *property = mlt_properties_get( properties, "geometry" );
107
108         // Allow a geometry repeat cycle
109         if ( cycle >= 1 )
110                 length = cycle;
111         else if ( cycle > 0 )
112                 length *= cycle;
113
114         // Parse the geometry if we have one
115         mlt_geometry_parse( geometry, property, length, normalised_width, normalised_height );
116
117         // Check if we're using the old style geometry
118         if ( property == NULL )
119         {
120                 // DEPRECATED: Multiple keys for geometry information is inefficient and too rigid for 
121                 // practical use - while deprecated, it has been slightly extended too - keys can now
122                 // be specified out of order, and can be blanked or NULL to simulate removal
123
124                 // Structure to use for parsing and inserting
125                 struct mlt_geometry_item_s item;
126
127                 // Parse the start property
128                 item.frame = 0;
129                 if ( mlt_geometry_parse_item( geometry, &item, mlt_properties_get( properties, "start" ) ) == 0 )
130                         mlt_geometry_insert( geometry, &item );
131
132                 // Parse the keys in between
133                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
134                 {
135                         // Get the name of the property
136                         char *name = mlt_properties_get_name( properties, i );
137         
138                         // Check that it's valid
139                         if ( !strncmp( name, "key[", 4 ) )
140                         {
141                                 // Get the value of the property
142                                 char *value = mlt_properties_get_value( properties, i );
143         
144                                 // Determine the frame number
145                                 item.frame = atoi( name + 4 );
146         
147                                 // Parse and add to the list
148                                 if ( mlt_geometry_parse_item( geometry, &item, value ) == 0 )
149                                         mlt_geometry_insert( geometry, &item );
150                                 else
151                                         fprintf( stderr, "Invalid Key - skipping %s = %s\n", name, value );
152                         }
153                 }
154
155                 // Parse the end
156                 item.frame = -1;
157                 if ( mlt_geometry_parse_item( geometry, &item, mlt_properties_get( properties, "end" ) ) == 0 )
158                         mlt_geometry_insert( geometry, &item );
159         }
160         
161         return geometry;
162 }
163
164 /** Adjust position according to scaled size and alignment properties.
165 */
166
167 static void alignment_calculate( struct geometry_s *geometry )
168 {
169         geometry->item.x += ( geometry->item.w - geometry->sw ) * geometry->halign / 2;
170         geometry->item.y += ( geometry->item.h - geometry->sh ) * geometry->valign / 2;
171 }
172
173 /** Calculate the position for this frame.
174 */
175
176 static int position_calculate( mlt_transition this, mlt_position position )
177 {
178         // Get the in and out position
179         mlt_position in = mlt_transition_get_in( this );
180
181         // Now do the calcs
182         return position - in;
183 }
184
185 /** Calculate the field delta for this frame - position between two frames.
186 */
187
188 static inline double delta_calculate( mlt_transition this, mlt_frame frame, mlt_position position )
189 {
190         // Get the in and out position
191         mlt_position in = mlt_transition_get_in( this );
192         mlt_position out = mlt_transition_get_out( this );
193         double length = out - in + 1;
194
195         // Now do the calcs
196         double x = ( double )( position - in ) / length;
197         double y = ( double )( position + 1 - in ) / length;
198
199         return length * ( y - x ) / 2.0;
200 }
201
202 static int get_value( mlt_properties properties, char *preferred, char *fallback )
203 {
204         int value = mlt_properties_get_int( properties, preferred );
205         if ( value == 0 )
206                 value = mlt_properties_get_int( properties, fallback );
207         return value;
208 }
209
210 /** A linear threshold determination function.
211 */
212
213 static inline int32_t linearstep( int32_t edge1, int32_t edge2, int32_t a )
214 {
215         if ( a < edge1 )
216                 return 0;
217
218         if ( a >= edge2 )
219                 return 0x10000;
220
221         return ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
222 }
223
224 /** A smoother, non-linear threshold determination function.
225 */
226
227 static inline int32_t smoothstep( int32_t edge1, int32_t edge2, uint32_t a )
228 {
229         if ( a < edge1 )
230                 return 0;
231
232         if ( a >= edge2 )
233                 return 0x10000;
234
235         a = ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
236
237         return ( ( ( a * a ) >> 16 )  * ( ( 3 << 16 ) - ( 2 * a ) ) ) >> 16;
238 }
239
240 /** Load the luma map from PGM stream.
241 */
242
243 static void luma_read_pgm( FILE *f, uint16_t **map, int *width, int *height )
244 {
245         uint8_t *data = NULL;
246         while (1)
247         {
248                 char line[128];
249                 char comment[128];
250                 int i = 2;
251                 int maxval;
252                 int bpp;
253                 uint16_t *p;
254
255                 line[127] = '\0';
256
257                 // get the magic code
258                 if ( fgets( line, 127, f ) == NULL )
259                         break;
260
261                 // skip comments
262                 while ( sscanf( line, " #%s", comment ) > 0 )
263                         if ( fgets( line, 127, f ) == NULL )
264                                 break;
265
266                 if ( line[0] != 'P' || line[1] != '5' )
267                         break;
268
269                 // skip white space and see if a new line must be fetched
270                 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
271                 if ( ( line[i] == '\0' || line[i] == '#' ) && fgets( line, 127, f ) == NULL )
272                         break;
273
274                 // skip comments
275                 while ( sscanf( line, " #%s", comment ) > 0 )
276                         if ( fgets( line, 127, f ) == NULL )
277                                 break;
278
279                 // get the dimensions
280                 if ( line[0] == 'P' )
281                         i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
282                 else
283                         i = sscanf( line, "%d %d %d", width, height, &maxval );
284
285                 // get the height value, if not yet
286                 if ( i < 2 )
287                 {
288                         if ( fgets( line, 127, f ) == NULL )
289                                 break;
290
291                         // skip comments
292                         while ( sscanf( line, " #%s", comment ) > 0 )
293                                 if ( fgets( line, 127, f ) == NULL )
294                                         break;
295
296                         i = sscanf( line, "%d", height );
297                         if ( i == 0 )
298                                 break;
299                         else
300                                 i = 2;
301                 }
302
303                 // get the maximum gray value, if not yet
304                 if ( i < 3 )
305                 {
306                         if ( fgets( line, 127, f ) == NULL )
307                                 break;
308
309                         // skip comments
310                         while ( sscanf( line, " #%s", comment ) > 0 )
311                                 if ( fgets( line, 127, f ) == NULL )
312                                         break;
313
314                         i = sscanf( line, "%d", &maxval );
315                         if ( i == 0 )
316                                 break;
317                 }
318
319                 // determine if this is one or two bytes per pixel
320                 bpp = maxval > 255 ? 2 : 1;
321
322                 // allocate temporary storage for the raw data
323                 data = mlt_pool_alloc( *width * *height * bpp );
324                 if ( data == NULL )
325                         break;
326
327                 // read the raw data
328                 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
329                         break;
330
331                 // allocate the luma bitmap
332                 *map = p = (uint16_t*)mlt_pool_alloc( *width * *height * sizeof( uint16_t ) );
333                 if ( *map == NULL )
334                         break;
335
336                 // proces the raw data into the luma bitmap
337                 for ( i = 0; i < *width * *height * bpp; i += bpp )
338                 {
339                         if ( bpp == 1 )
340                                 *p++ = data[ i ] << 8;
341                         else
342                                 *p++ = ( data[ i ] << 8 ) + data[ i + 1 ];
343                 }
344
345                 break;
346         }
347
348         if ( data != NULL )
349                 mlt_pool_release( data );
350 }
351
352 /** Generate a luma map from any YUV image.
353 */
354
355 static void luma_read_yuv422( uint8_t *image, uint16_t **map, int width, int height )
356 {
357         int i;
358         
359         // allocate the luma bitmap
360         uint16_t *p = *map = ( uint16_t* )mlt_pool_alloc( width * height * sizeof( uint16_t ) );
361         if ( *map == NULL )
362                 return;
363
364         // proces the image data into the luma bitmap
365         for ( i = 0; i < width * height * 2; i += 2 )
366                 *p++ = ( image[ i ] - 16 ) * 299; // 299 = 65535 / 219
367 }
368
369 static inline int calculate_mix( uint16_t *luma, int j, int soft, int weight, int alpha )
370 {
371         return ( ( ( luma == NULL ) ? weight : smoothstep( luma[ j ], luma[ j ] + soft, weight + soft ) ) * alpha ) >> 8;
372 }
373
374 static inline uint8_t sample_mix( uint8_t dest, uint8_t src, int mix )
375 {
376         return ( src * mix + dest * ( ( 1 << 16 ) - mix ) ) >> 16;
377 }
378
379 /** Composite a source line over a destination line
380 */
381
382 static void composite_line_yuv( uint8_t *dest, uint8_t *src, int width, uint8_t *alpha_b, uint8_t *alpha_a, int weight, uint16_t *luma, int soft )
383 {
384         register int j;
385         register int mix;
386
387         for ( j = 0; j < width; j ++ )
388         {
389                 mix = calculate_mix( luma, j, soft, weight, *alpha_b ++ );
390                 *dest = sample_mix( *dest, *src++, mix );
391                 dest++;
392                 *dest = sample_mix( *dest, *src++, mix );
393                 dest++;
394                 *alpha_a = mix | *alpha_a;
395                 alpha_a ++;
396         }
397 }
398
399 static void composite_line_yuv_or( uint8_t *dest, uint8_t *src, int width, uint8_t *alpha_b, uint8_t *alpha_a, int weight, uint16_t *luma, int soft )
400 {
401         register int j;
402         register int mix;
403
404         for ( j = 0; j < width; j ++ )
405         {
406                 mix = calculate_mix( luma, j, soft, weight, *alpha_b ++ | *alpha_a );
407                 *dest = sample_mix( *dest, *src++, mix );
408                 dest++;
409                 *dest = sample_mix( *dest, *src++, mix );
410                 dest++;
411                 *alpha_a ++ = mix;
412         }
413 }
414
415 static void composite_line_yuv_and( uint8_t *dest, uint8_t *src, int width, uint8_t *alpha_b, uint8_t *alpha_a, int weight, uint16_t *luma, int soft )
416 {
417         register int j;
418         register int mix;
419
420         for ( j = 0; j < width; j ++ )
421         {
422                 mix = calculate_mix( luma, j, soft, weight, *alpha_b ++ & *alpha_a );
423                 *dest = sample_mix( *dest, *src++, mix );
424                 dest++;
425                 *dest = sample_mix( *dest, *src++, mix );
426                 dest++;
427                 *alpha_a ++ = mix;
428         }
429 }
430
431 static void composite_line_yuv_xor( uint8_t *dest, uint8_t *src, int width, uint8_t *alpha_b, uint8_t *alpha_a, int weight, uint16_t *luma, int soft )
432 {
433         register int j;
434         register int mix;
435
436         for ( j = 0; j < width; j ++ )
437         {
438                 mix = calculate_mix( luma, j, soft, weight, *alpha_b ++ ^ *alpha_a );
439                 *dest = sample_mix( *dest, *src++, mix );
440                 dest++;
441                 *dest = sample_mix( *dest, *src++, mix );
442                 dest++;
443                 *alpha_a ++ = mix;
444         }
445 }
446
447 /** Composite function.
448 */
449
450 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 *alpha_b, uint8_t *alpha_a, struct geometry_s geometry, int field, uint16_t *p_luma, int32_t softness, composite_line_fn line_fn )
451 {
452         int ret = 0;
453         int i;
454         int x_src = 0, y_src = 0;
455         int32_t weight = ( ( 1 << 16 ) - 1 ) * ( geometry.item.mix / 100 );
456         int step = ( field > -1 ) ? 2 : 1;
457         int bpp = 2;
458         int stride_src = width_src * bpp;
459         int stride_dest = width_dest * bpp;
460         
461         // Adjust to consumer scale
462         int x = rint( 0.5 + geometry.item.x * width_dest / geometry.nw );
463         int y = rint( 0.5 + geometry.item.y * height_dest / geometry.nh );
464         int uneven_x = ( x % 2 );
465
466         // optimization points - no work to do
467         if ( width_src <= 0 || height_src <= 0 )
468                 return ret;
469
470         if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
471                 return ret;
472
473         // crop overlay off the left edge of frame
474         if ( x < 0 )
475         {
476                 x_src = -x;
477                 width_src -= x_src;
478                 x = 0;
479         }
480         
481         // crop overlay beyond right edge of frame
482         if ( x + width_src > width_dest )
483                 width_src = width_dest - x;
484
485         // crop overlay off the top edge of the frame
486         if ( y < 0 )
487         {
488                 y_src = -y;
489                 height_src -= y_src;
490                 y = 0;
491         }
492         
493         // crop overlay below bottom edge of frame
494         if ( y + height_src > height_dest )
495                 height_src = height_dest - y;
496
497         // offset pointer into overlay buffer based on cropping
498         p_src += x_src * bpp + y_src * stride_src;
499
500         // offset pointer into frame buffer based upon positive coordinates only!
501         p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
502
503         // offset pointer into alpha channel based upon cropping
504         alpha_b += x_src + y_src * stride_src / bpp;
505         alpha_a += x + y * stride_dest / bpp;
506
507         // offset pointer into luma channel based upon cropping
508         if ( p_luma )
509                 p_luma += x_src + y_src * stride_src / bpp;
510         
511         // Assuming lower field first
512         // Special care is taken to make sure the b_frame is aligned to the correct field.
513         // field 0 = lower field and y should be odd (y is 0-based).
514         // field 1 = upper field and y should be even.
515         if ( ( field > -1 ) && ( y % 2 == field ) )
516         {
517                 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
518                         p_dest += stride_dest;
519                 else
520                         p_dest -= stride_dest;
521         }
522
523         // On the second field, use the other lines from b_frame
524         if ( field == 1 )
525         {
526                 p_src += stride_src;
527                 alpha_b += stride_src / bpp;
528                 alpha_a += stride_dest / bpp;
529                 height_src--;
530         }
531
532         stride_src *= step;
533         stride_dest *= step;
534         int alpha_b_stride = stride_src / bpp;
535         int alpha_a_stride = stride_dest / bpp;
536
537         p_src += uneven_x * 2;
538         width_src -= 2 * uneven_x;
539         alpha_b += uneven_x;
540         uneven_x = 0;
541
542         // now do the compositing only to cropped extents
543         for ( i = 0; i < height_src; i += step )
544         {
545                 line_fn( p_dest, p_src, width_src, alpha_b, alpha_a, weight, p_luma, softness );
546
547                 p_src += stride_src;
548                 p_dest += stride_dest;
549                 alpha_b += alpha_b_stride;
550                 alpha_a += alpha_a_stride;
551                 if ( p_luma )
552                         p_luma += alpha_b_stride;
553         }
554
555         return ret;
556 }
557
558
559 /** Scale 16bit greyscale luma map using nearest neighbor.
560 */
561
562 static inline void
563 scale_luma ( uint16_t *dest_buf, int dest_width, int dest_height, const uint16_t *src_buf, int src_width, int src_height, int invert )
564 {
565         register int i, j;
566         register int x_step = ( src_width << 16 ) / dest_width;
567         register int y_step = ( src_height << 16 ) / dest_height;
568         register int x, y = 0;
569
570         for ( i = 0; i < dest_height; i++ )
571         {
572                 const uint16_t *src = src_buf + ( y >> 16 ) * src_width;
573                 x = 0;
574                 
575                 for ( j = 0; j < dest_width; j++ )
576                 {
577                         *dest_buf++ = src[ x >> 16 ] ^ invert;
578                         x += x_step;
579                 }
580                 y += y_step;
581         }
582 }
583
584 static uint16_t* get_luma( mlt_properties properties, int width, int height )
585 {
586         // The cached luma map information
587         int luma_width = mlt_properties_get_int( properties, "_luma.width" );
588         int luma_height = mlt_properties_get_int( properties, "_luma.height" );
589         uint16_t *luma_bitmap = mlt_properties_get_data( properties, "_luma.bitmap", NULL );
590         int invert = mlt_properties_get_int( properties, "luma_invert" );
591         
592         // If the filename property changed, reload the map
593         char *resource = mlt_properties_get( properties, "luma" );
594
595         char temp[ 512 ];
596
597         if ( luma_width == 0 || luma_height == 0 )
598         {
599                 luma_width = width;
600                 luma_height = height;
601         }
602
603         if ( resource != NULL && strchr( resource, '%' ) )
604         {
605                 // TODO: Clean up quick and dirty compressed/existence check
606                 FILE *test;
607                 sprintf( temp, "%s/lumas/%s/%s", mlt_factory_prefix( ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
608                 test = fopen( temp, "r" );
609                 if ( test == NULL )
610                         strcat( temp, ".png" );
611                 else
612                         fclose( test );
613                 resource = temp;
614         }
615
616         if ( resource != NULL && ( luma_bitmap == NULL || luma_width != width || luma_height != height ) )
617         {
618                 uint16_t *orig_bitmap = mlt_properties_get_data( properties, "_luma.orig_bitmap", NULL );
619                 luma_width = mlt_properties_get_int( properties, "_luma.orig_width" );
620                 luma_height = mlt_properties_get_int( properties, "_luma.orig_height" );
621
622                 // Load the original luma once
623                 if ( orig_bitmap == NULL )
624                 {
625                         char *extension = strrchr( resource, '.' );
626                         
627                         // See if it is a PGM
628                         if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
629                         {
630                                 // Open PGM
631                                 FILE *f = fopen( resource, "r" );
632                                 if ( f != NULL )
633                                 {
634                                         // Load from PGM
635                                         luma_read_pgm( f, &orig_bitmap, &luma_width, &luma_height );
636                                         fclose( f );
637                                         
638                                         // Remember the original size for subsequent scaling
639                                         mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
640                                         mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
641                                         mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
642                                 }
643                         }
644                         else
645                         {
646                                 // Get the factory producer service
647                                 char *factory = mlt_properties_get( properties, "factory" );
648         
649                                 // Create the producer
650                                 mlt_producer producer = mlt_factory_producer( factory, resource );
651         
652                                 // If we have one
653                                 if ( producer != NULL )
654                                 {
655                                         // Get the producer properties
656                                         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
657         
658                                         // Ensure that we loop
659                                         mlt_properties_set( producer_properties, "eof", "loop" );
660         
661                                         // Now pass all producer. properties on the transition down
662                                         mlt_properties_pass( producer_properties, properties, "luma." );
663         
664                                         // We will get the alpha frame from the producer
665                                         mlt_frame luma_frame = NULL;
666         
667                                         // Get the luma frame
668                                         if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 )
669                                         {
670                                                 uint8_t *luma_image;
671                                                 mlt_image_format luma_format = mlt_image_yuv422;
672         
673                                                 // Get image from the luma producer
674                                                 mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "none" );
675                                                 mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
676         
677                                                 // Generate the luma map
678                                                 if ( luma_image != NULL && luma_format == mlt_image_yuv422 )
679                                                         luma_read_yuv422( luma_image, &orig_bitmap, luma_width, luma_height );
680         
681                                                 // Remember the original size for subsequent scaling
682                                                 mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
683                                                 mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
684                                                 mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
685                                                 
686                                                 // Cleanup the luma frame
687                                                 mlt_frame_close( luma_frame );
688                                         }
689         
690                                         // Cleanup the luma producer
691                                         mlt_producer_close( producer );
692                                 }
693                         }
694                 }
695                 // Scale luma map
696                 luma_bitmap = mlt_pool_alloc( width * height * sizeof( uint16_t ) );
697                 scale_luma( luma_bitmap, width, height, orig_bitmap, luma_width, luma_height, invert * ( ( 1 << 16 ) - 1 ) );
698
699                 // Remember the scaled luma size to prevent unnecessary scaling
700                 mlt_properties_set_int( properties, "_luma.width", width );
701                 mlt_properties_set_int( properties, "_luma.height", height );
702                 mlt_properties_set_data( properties, "_luma.bitmap", luma_bitmap, width * height * 2, mlt_pool_release, NULL );
703         }
704         return luma_bitmap;
705 }
706
707 /** Get the properly sized image from b_frame.
708 */
709
710 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
711 {
712         int ret = 0;
713         mlt_image_format format = mlt_image_yuv422;
714
715         // Get the properties objects
716         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
717         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
718
719         if ( mlt_properties_get_int( properties, "distort" ) == 0 && mlt_properties_get_int( b_props, "distort" ) == 0 && geometry->item.distort == 0 )
720         {
721                 // Adjust b_frame pixel aspect
722                 int normalised_width = geometry->item.w;
723                 int normalised_height = geometry->item.h;
724                 int real_width = get_value( b_props, "real_width", "width" );
725                 int real_height = get_value( b_props, "real_height", "height" );
726                 double input_ar = mlt_properties_get_double( b_props, "aspect_ratio" );
727                 double consumer_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
728                 double background_ar = mlt_properties_get_double( b_props, "output_ratio" );
729                 double output_ar = background_ar != 0.0 ? background_ar : consumer_ar;
730                 int scaled_width = rint( 0.5 + ( input_ar == 0.0 ? output_ar : input_ar ) / output_ar * real_width );
731                 int scaled_height = real_height;
732
733                 // Now ensure that our images fit in the normalised frame
734                 if ( scaled_width > normalised_width )
735                 {
736                         scaled_height = rint( 0.5 + scaled_height * normalised_width / scaled_width );
737                         scaled_width = normalised_width;
738                 }
739                 if ( scaled_height > normalised_height )
740                 {
741                         scaled_width = rint( 0.5 + scaled_width * normalised_height / scaled_height );
742                         scaled_height = normalised_height;
743                 }
744
745                 // Honour the fill request - this will scale the image to fill width or height while maintaining a/r
746                 // ????: Shouln't this be the default behaviour?
747                 if ( mlt_properties_get_int( properties, "fill" ) && scaled_width > 0 && scaled_height > 0 )
748                 {
749                         if ( scaled_height < normalised_height && scaled_width * normalised_height / scaled_height < normalised_width )
750                         {
751                                 scaled_width = rint( 0.5 + scaled_width * normalised_height / scaled_height );
752                                 scaled_height = normalised_height;
753                         }
754                         else if ( scaled_width < normalised_width && scaled_height * normalised_width / scaled_width < normalised_height )
755                         {
756                                 scaled_height = rint( 0.5 + scaled_height * normalised_width / scaled_width );
757                                 scaled_width = normalised_width;
758                         }
759                 }
760
761                 // Save the new scaled dimensions
762                 geometry->sw = scaled_width;
763                 geometry->sh = scaled_height;
764         }
765         else
766         {
767                 geometry->sw = geometry->item.w;
768                 geometry->sh = geometry->item.h;
769         }
770
771         // We want to ensure that we bypass resize now...
772         mlt_properties_set_int( b_props, "distort", mlt_properties_get_int( properties, "distort" ) );
773
774         // Take into consideration alignment for optimisation (titles are a special case)
775         if ( !mlt_properties_get_int( properties, "titles" ) )
776                 alignment_calculate( geometry );
777
778         // Adjust to consumer scale
779         *width = rint( 0.5 + geometry->sw * *width / geometry->nw );
780         *height = rint( 0.5 + geometry->sh * *height / geometry->nh );
781
782         ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
783
784         return ret && image != NULL;
785 }
786
787
788 static mlt_geometry composite_calculate( mlt_transition this, struct geometry_s *result, mlt_frame a_frame, double position )
789 {
790         // Get the properties from the transition
791         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
792
793         // Get the properties from the frame
794         mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
795         
796         // Structures for geometry
797         mlt_geometry start = mlt_properties_get_data( properties, "geometries", NULL );
798
799         // Obtain the normalised width and height from the a_frame
800         int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
801         int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
802
803         char *name = mlt_properties_get( properties, "_unique_id" );
804         char key[ 256 ];
805
806         sprintf( key, "%s.in", name );
807         if ( mlt_properties_get( a_props, key ) )
808         {
809                 sscanf( mlt_properties_get( a_props, key ), "%f,%f,%f,%f,%f,%d,%d", &result->item.x, &result->item.y, &result->item.w, &result->item.h, &result->item.mix, &result->nw, &result->nh );
810         }
811         else
812         {
813                 // Now parse the geometries
814                 if ( start == NULL )
815                 {
816                         // Parse the transitions properties
817                         start = transition_parse_keys( this, normalised_width, normalised_height );
818
819                         // Assign to properties to ensure we get destroyed
820                         mlt_properties_set_data( properties, "geometries", start, 0, ( mlt_destructor )mlt_geometry_close, NULL );
821                 }
822                 else
823                 {
824                         int length = mlt_transition_get_out( this ) - mlt_transition_get_in( this ) + 1;
825                         double cycle = mlt_properties_get_double( properties, "cycle" );
826                         if ( cycle > 1 )
827                                 length = cycle;
828                         else if ( cycle > 0 )
829                                 length *= cycle;
830                         mlt_geometry_refresh( start, mlt_properties_get( properties, "geometry" ), length, normalised_width, normalised_height );
831                 }
832
833                 // Do the calculation
834                 geometry_calculate( this, result, position );
835
836                 // Assign normalised info
837                 result->nw = normalised_width;
838                 result->nh = normalised_height;
839         }
840
841         // Now parse the alignment
842         result->halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
843         result->valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
844
845         return start;
846 }
847
848 mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_position frame_position )
849 {
850         // Create a frame to return
851         mlt_frame b_frame = mlt_frame_init( );
852
853         // Get the properties of the a frame
854         mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
855
856         // Get the properties of the b frame
857         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
858
859         // Get the position
860         int position = position_calculate( this, frame_position );
861
862         // Get the unique id of the transition
863         char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
864         char key[ 256 ];
865
866         // Destination image
867         uint8_t *dest = NULL;
868
869         // Get the image and dimensions
870         uint8_t *image = mlt_properties_get_data( a_props, "image", NULL );
871         int width = mlt_properties_get_int( a_props, "width" );
872         int height = mlt_properties_get_int( a_props, "height" );
873         int format = mlt_properties_get_int( a_props, "format" );
874
875         // Pointers for copy operation
876         uint8_t *p;
877
878         // Coordinates
879         int w = 0;
880         int h = 0;
881         int x = 0;
882         int y = 0;
883
884         int ss = 0;
885         int ds = 0;
886
887         // Will need to know region to copy
888         struct geometry_s result;
889
890         // Calculate the region now
891         composite_calculate( this, &result, a_frame, position );
892
893         // Need to scale down to actual dimensions
894         x = rint( 0.5 + result.item.x * width / result.nw );
895         y = rint( 0.5 + result.item.y * height / result.nh );
896         w = rint( 0.5 + result.item.w * width / result.nw );
897         h = rint( 0.5 + result.item.h * height / result.nh );
898
899         if ( x % 2 )
900         {
901                 x --;
902                 w ++;
903         }
904
905         // Store the key
906         sprintf( key, "%s.in=%d,%d,%d,%d,%f,%d,%d", name, x, y, w, h, result.item.mix, width, height );
907         mlt_properties_parse( a_props, key );
908         sprintf( key, "%s.out=%d,%d,%d,%d,%f,%d,%d", name, x, y, w, h, result.item.mix, width, height );
909         mlt_properties_parse( a_props, key );
910
911         ds = w * 2;
912         ss = width * 2;
913
914         // Now we need to create a new destination image
915         dest = mlt_pool_alloc( w * h * 2 );
916
917         // Assign to the new frame
918         mlt_properties_set_data( b_props, "image", dest, w * h * 2, mlt_pool_release, NULL );
919         mlt_properties_set_int( b_props, "width", w );
920         mlt_properties_set_int( b_props, "height", h );
921         mlt_properties_set_int( b_props, "format", format );
922
923         if ( y < 0 )
924         {
925                 dest += ( ds * -y );
926                 h += y;
927                 y = 0;
928         }
929
930         if ( y + h > height )
931                 h -= ( y + h - height );
932
933         if ( x < 0 )
934         {
935                 dest += -x * 2;
936                 w += x;
937                 x = 0;
938         }
939
940         if ( w > 0 && h > 0 )
941         {
942                 // Copy the region of the image
943                 p = image + y * ss + x * 2;
944
945                 while ( h -- )
946                 {
947                         memcpy( dest, p, w * 2 );
948                         dest += ds;
949                         p += ss;
950                 }
951         }
952
953         // Assign this position to the b frame
954         mlt_frame_set_position( b_frame, frame_position );
955         mlt_properties_set_int( b_props, "distort", 1 );
956
957         // Return the frame
958         return b_frame;
959 }
960
961 /** Get the image.
962 */
963
964 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
965 {
966         // Get the b frame from the stack
967         mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
968
969         // Get the transition from the a frame
970         mlt_transition this = mlt_frame_pop_service( a_frame );
971
972         // Get in and out
973         double position = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( a_frame ) );
974         int out = mlt_frame_pop_service_int( a_frame );
975         int in = mlt_frame_pop_service_int( a_frame );
976
977         // Get the properties from the transition
978         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
979
980         // TODO: clean up always_active behaviour
981         if ( mlt_properties_get_int( properties, "always_active" ) )
982         {
983                 mlt_events_block( properties, properties );
984                 mlt_properties_set_int( properties, "in", in );
985                 mlt_properties_set_int( properties, "out", out );
986                 mlt_events_unblock( properties, properties );
987         }
988
989         // This compositer is yuv422 only
990         *format = mlt_image_yuv422;
991
992         if ( b_frame != NULL )
993         {
994                 // Get the properties of the a frame
995                 mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
996
997                 // Get the properties of the b frame
998                 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
999
1000                 // Structures for geometry
1001                 struct geometry_s result;
1002
1003                 // Calculate the position
1004                 double delta = delta_calculate( this, a_frame, position );
1005
1006                 // Get the image from the b frame
1007                 uint8_t *image_b = NULL;
1008                 int width_b = *width;
1009                 int height_b = *height;
1010         
1011                 // Vars for alphas
1012                 uint8_t *alpha_a = NULL;
1013                 uint8_t *alpha_b = NULL;
1014
1015                 // Composites always need scaling... defaulting to lowest
1016                 char *rescale = mlt_properties_get( a_props, "rescale.interp" );
1017                 if ( rescale == NULL || !strcmp( rescale, "none" ) )
1018                         rescale = "nearest";
1019                 mlt_properties_set( a_props, "rescale.interp", rescale );
1020                 mlt_properties_set( b_props, "rescale.interp", rescale );
1021
1022                 // Do the calculation
1023                 // NB: Locks needed here since the properties are being modified
1024                 mlt_service_lock( MLT_TRANSITION_SERVICE( this ) );
1025                 composite_calculate( this, &result, a_frame, position );
1026                 mlt_service_unlock( MLT_TRANSITION_SERVICE( this ) );
1027
1028                 // Since we are the consumer of the b_frame, we must pass along these
1029                 // consumer properties from the a_frame
1030                 mlt_properties_set_double( b_props, "consumer_deinterlace", mlt_properties_get_double( a_props, "consumer_deinterlace" ) );
1031                 mlt_properties_set( b_props, "consumer_deinterlace_method", mlt_properties_get( a_props, "consumer_deinterlace_method" ) );
1032                 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
1033
1034                 // TODO: Dangerous/temporary optimisation - if nothing to do, then do nothing
1035                 if ( mlt_properties_get_int( properties, "no_alpha" ) && 
1036                          result.item.x == 0 && result.item.y == 0 && result.item.w == *width && result.item.h == *height && result.item.mix == 100 )
1037                 {
1038                         mlt_frame_get_image( b_frame, image, format, width, height, 1 );
1039                         if ( !mlt_frame_is_test_card( a_frame ) )
1040                                 mlt_frame_replace_image( a_frame, *image, *format, *width, *height );
1041                         return 0;
1042                 }
1043
1044                 if ( a_frame == b_frame )
1045                 {
1046                         double aspect_ratio = mlt_frame_get_aspect_ratio( b_frame );
1047                         get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result );
1048                         alpha_b = mlt_frame_get_alpha_mask( b_frame );
1049                         mlt_properties_set_double( a_props, "aspect_ratio", aspect_ratio );
1050                 }
1051
1052                 // Get the image from the a frame
1053                 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
1054                 alpha_a = mlt_frame_get_alpha_mask( a_frame );
1055
1056                 // Optimisation - no compositing required
1057                 if ( result.item.mix == 0 || ( result.item.w == 0 && result.item.h == 0 ) )
1058                         return 0;
1059
1060                 // Need to keep the width/height of the a_frame on the b_frame for titling
1061                 if ( mlt_properties_get( a_props, "dest_width" ) == NULL )
1062                 {
1063                         mlt_properties_set_int( a_props, "dest_width", *width );
1064                         mlt_properties_set_int( a_props, "dest_height", *height );
1065                         mlt_properties_set_int( b_props, "dest_width", *width );
1066                         mlt_properties_set_int( b_props, "dest_height", *height );
1067                 }
1068                 else
1069                 {
1070                         mlt_properties_set_int( b_props, "dest_width", mlt_properties_get_int( a_props, "dest_width" ) );
1071                         mlt_properties_set_int( b_props, "dest_height", mlt_properties_get_int( a_props, "dest_height" ) );
1072                 }
1073
1074                 // Special case for titling...
1075                 if ( mlt_properties_get_int( properties, "titles" ) )
1076                 {
1077                         if ( mlt_properties_get( b_props, "rescale.interp" ) == NULL )
1078                                 mlt_properties_set( b_props, "rescale.interp", "hyper" );
1079                         width_b = mlt_properties_get_int( a_props, "dest_width" );
1080                         height_b = mlt_properties_get_int( a_props, "dest_height" );
1081                 }
1082
1083                 if ( *image != image_b && ( image_b != NULL || get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 ) )
1084                 {
1085                         uint8_t *dest = *image;
1086                         uint8_t *src = image_b;
1087                         int progressive = 
1088                                         mlt_properties_get_int( a_props, "consumer_deinterlace" ) ||
1089                                         mlt_properties_get_int( properties, "progressive" );
1090                         int field;
1091                         
1092                         int32_t luma_softness = mlt_properties_get_double( properties, "softness" ) * ( 1 << 16 );
1093                         uint16_t *luma_bitmap = get_luma( properties, width_b, height_b );
1094                         char *operator = mlt_properties_get( properties, "operator" );
1095
1096                         alpha_b = alpha_b == NULL ? mlt_frame_get_alpha_mask( b_frame ) : alpha_b;
1097
1098                         composite_line_fn line_fn = composite_line_yuv;
1099
1100                         // Replacement and override
1101                         if ( operator != NULL )
1102                         {
1103                                 if ( !strcmp( operator, "or" ) )
1104                                         line_fn = composite_line_yuv_or;
1105                                 if ( !strcmp( operator, "and" ) )
1106                                         line_fn = composite_line_yuv_and;
1107                                 if ( !strcmp( operator, "xor" ) )
1108                                         line_fn = composite_line_yuv_xor;
1109                         }
1110
1111                         // Allow the user to completely obliterate the alpha channels from both frames
1112                         if ( mlt_properties_get( properties, "alpha_a" ) )
1113                                 memset( alpha_a, mlt_properties_get_int( properties, "alpha_a" ), *width * *height );
1114
1115                         if ( mlt_properties_get( properties, "alpha_b" ) )
1116                                 memset( alpha_b, mlt_properties_get_int( properties, "alpha_b" ), width_b * height_b );
1117
1118                         for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
1119                         {
1120                                 // Assume lower field (0) first
1121                                 double field_position = position + field * delta;
1122                                 
1123                                 // Do the calculation if we need to
1124                                 // NB: Locks needed here since the properties are being modified
1125                                 mlt_service_lock( MLT_TRANSITION_SERVICE( this ) );
1126                                 composite_calculate( this, &result, a_frame, field_position );
1127                                 mlt_service_unlock( MLT_TRANSITION_SERVICE( this ) );
1128
1129                                 if ( mlt_properties_get_int( properties, "titles" ) )
1130                                 {
1131                                         result.item.w = rint( 0.5 + *width * ( result.item.w / result.nw ) );
1132                                         result.nw = result.item.w;
1133                                         result.item.h = rint( 0.5 + *height * ( result.item.h / result.nh ) );
1134                                         result.nh = *height;
1135                                         result.sw = width_b;
1136                                         result.sh = height_b;
1137                                 }
1138
1139                                 // Align
1140                                 alignment_calculate( &result );
1141
1142                                 // Composite the b_frame on the a_frame
1143                                 composite_yuv( dest, *width, *height, src, width_b, height_b, alpha_b, alpha_a, result, progressive ? -1 : field, luma_bitmap, luma_softness, line_fn );
1144                         }
1145                 }
1146         }
1147         else
1148         {
1149                 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
1150         }
1151
1152         return 0;
1153 }
1154
1155 /** Composition transition processing.
1156 */
1157
1158 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
1159 {
1160         // UGH - this is a TODO - find a more reliable means of obtaining in/out for the always_active case
1161         if ( mlt_properties_get_int(  MLT_TRANSITION_PROPERTIES( this ), "always_active" ) == 0 )
1162         {
1163                 mlt_frame_push_service_int( a_frame, mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( this ), "in" ) );
1164                 mlt_frame_push_service_int( a_frame, mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( this ), "out" ) );
1165                 mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( a_frame ), position_calculate( this, mlt_frame_get_position( a_frame ) ) );
1166         }
1167         else
1168         {
1169                 mlt_properties props = mlt_properties_get_data( MLT_FRAME_PROPERTIES( b_frame ), "_producer", NULL );
1170                 mlt_frame_push_service_int( a_frame, mlt_properties_get_int( props, "in" ) );
1171                 mlt_frame_push_service_int( a_frame, mlt_properties_get_int( props, "out" ) );
1172                 mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( a_frame ), mlt_properties_get_int( props, "_frame" ) - mlt_properties_get_int( props, "in" ) );
1173         }
1174         
1175         mlt_frame_push_service( a_frame, this );
1176         mlt_frame_push_frame( a_frame, b_frame );
1177         mlt_frame_push_get_image( a_frame, transition_get_image );
1178         return a_frame;
1179 }
1180
1181 /** Constructor for the filter.
1182 */
1183
1184 mlt_transition transition_composite_init( char *arg )
1185 {
1186         mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
1187         if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
1188         {
1189                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
1190                 
1191                 this->process = composite_process;
1192                 
1193                 // Default starting motion and zoom
1194                 mlt_properties_set( properties, "start", arg != NULL ? arg : "0,0:100%x100%" );
1195                 
1196                 // Default factory
1197                 mlt_properties_set( properties, "factory", "fezzik" );
1198
1199                 // Inform apps and framework that this is a video only transition
1200                 mlt_properties_set_int( properties, "_transition_type", 1 );
1201         }
1202         return this;
1203 }