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