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