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