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