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