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