pgm scaling in transition_composite.
[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
613 /** Scale 16bit greyscale luma map using nearest neighbor.
614 */
615
616 static inline void
617 scale_luma ( uint16_t *dest_buf, int dest_width, int dest_height, const uint16_t *src_buf, int src_width, int src_height )
618 {
619 register int i, j;
620 register int x_step = ( src_width << 16 ) / dest_width;
621 register int y_step = ( src_height << 16 ) / dest_height;
622 register int x, y = 0;
623
624 for ( i = 0; i < dest_height; i++ )
625 {
626 const uint16_t *src = src_buf + ( y >> 16 ) * src_width;
627 x = 0;
628
629 for ( j = 0; j < dest_width; j++ )
630 {
631 *dest_buf++ = src[ x >> 16 ];
632 x += x_step;
633 }
634 y += y_step;
635 }
636 }
637
638 static uint16_t* get_luma( mlt_properties properties, int width, int height )
639 {
640 // The cached luma map information
641 int luma_width = mlt_properties_get_int( properties, "_luma.width" );
642 int luma_height = mlt_properties_get_int( properties, "_luma.height" );
643 uint16_t *luma_bitmap = mlt_properties_get_data( properties, "_luma.bitmap", NULL );
644
645 // If the filename property changed, reload the map
646 char *resource = mlt_properties_get( properties, "luma" );
647
648 if ( resource != NULL && ( luma_bitmap == NULL || luma_width != width || luma_height != height ) )
649 {
650 uint16_t *orig_bitmap = mlt_properties_get_data( properties, "_luma.orig_bitmap", NULL );
651 luma_width = mlt_properties_get_int( properties, "_luma.orig_width" );
652 luma_height = mlt_properties_get_int( properties, "_luma.orig_height" );
653
654 // Load the original luma once
655 if ( orig_bitmap == NULL )
656 {
657 char *extension = extension = strrchr( resource, '.' );
658
659 // See if it is a PGM
660 if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
661 {
662 // Open PGM
663 FILE *f = fopen( resource, "r" );
664 if ( f != NULL )
665 {
666 // Load from PGM
667 luma_read_pgm( f, &orig_bitmap, &luma_width, &luma_height );
668 fclose( f );
669
670 // Remember the original size for subsequent scaling
671 mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
672 mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
673 mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
674 }
675 }
676 else
677 {
678 // Get the factory producer service
679 char *factory = mlt_properties_get( properties, "factory" );
680
681 // Create the producer
682 mlt_producer producer = mlt_factory_producer( factory, resource );
683
684 // If we have one
685 if ( producer != NULL )
686 {
687 // Get the producer properties
688 mlt_properties producer_properties = mlt_producer_properties( producer );
689
690 // Ensure that we loop
691 mlt_properties_set( producer_properties, "eof", "loop" );
692
693 // Now pass all producer. properties on the transition down
694 mlt_properties_pass( producer_properties, properties, "luma." );
695
696 // We will get the alpha frame from the producer
697 mlt_frame luma_frame = NULL;
698
699 // Get the luma frame
700 if ( mlt_service_get_frame( mlt_producer_service( producer ), &luma_frame, 0 ) == 0 )
701 {
702 uint8_t *luma_image;
703 mlt_image_format luma_format = mlt_image_yuv422;
704
705 // Get image from the luma producer
706 mlt_properties_set( mlt_frame_properties( luma_frame ), "rescale.interp", "none" );
707 mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
708
709 // Generate the luma map
710 if ( luma_image != NULL && luma_format == mlt_image_yuv422 )
711 luma_read_yuv422( luma_image, &orig_bitmap, luma_width, luma_height );
712
713 // Remember the original size for subsequent scaling
714 mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
715 mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
716 mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
717
718 // Cleanup the luma frame
719 mlt_frame_close( luma_frame );
720 }
721
722 // Cleanup the luma producer
723 mlt_producer_close( producer );
724 }
725 }
726 }
727 // Scale luma map
728 luma_bitmap = mlt_pool_alloc( width * height * sizeof( uint16_t ) );
729 scale_luma( luma_bitmap, width, height, orig_bitmap, luma_width, luma_height );
730
731 // Remember the scaled luma size to prevent unnecessary scaling
732 mlt_properties_set_int( properties, "_luma.width", width );
733 mlt_properties_set_int( properties, "_luma.height", height );
734 mlt_properties_set_data( properties, "_luma.bitmap", luma_bitmap, width * height * 2, mlt_pool_release, NULL );
735 }
736 return luma_bitmap;
737 }
738
739 /** Get the properly sized image from b_frame.
740 */
741
742 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
743 {
744 int ret = 0;
745 mlt_image_format format = mlt_image_yuv422;
746
747 // Get the properties objects
748 mlt_properties b_props = mlt_frame_properties( b_frame );
749 mlt_properties properties = mlt_transition_properties( this );
750
751 if ( mlt_properties_get( properties, "distort" ) == NULL && geometry->distort == 0 )
752 {
753 // Adjust b_frame pixel aspect
754 int normalised_width = geometry->w;
755 int normalised_height = geometry->h;
756 int real_width = get_value( b_props, "real_width", "width" );
757 int real_height = get_value( b_props, "real_height", "height" );
758 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
759 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
760 int scaled_width = input_ar / output_ar * real_width;
761 int scaled_height = real_height;
762
763 // Now ensure that our images fit in the normalised frame
764 if ( scaled_width > normalised_width )
765 {
766 scaled_height = scaled_height * normalised_width / scaled_width;
767 scaled_width = normalised_width;
768 }
769 if ( scaled_height > normalised_height )
770 {
771 scaled_width = scaled_width * normalised_height / scaled_height;
772 scaled_height = normalised_height;
773 }
774
775 // Now apply the fill
776 // TODO: Should combine fill/distort in one property
777 if ( mlt_properties_get( properties, "fill" ) != NULL )
778 {
779 scaled_width = ( geometry->w / scaled_width ) * scaled_width;
780 scaled_height = ( geometry->h / scaled_height ) * scaled_height;
781 }
782
783 // Save the new scaled dimensions
784 geometry->sw = scaled_width;
785 geometry->sh = scaled_height;
786 }
787 else
788 {
789 geometry->sw = geometry->w;
790 geometry->sh = geometry->h;
791 }
792
793 // We want to ensure that we bypass resize now...
794 mlt_properties_set( b_props, "distort", "true" );
795
796 // Take into consideration alignment for optimisation
797 alignment_calculate( geometry );
798
799 // Adjust to consumer scale
800 int x = geometry->x * *width / geometry->nw;
801 int y = geometry->y * *height / geometry->nh;
802 *width = geometry->sw * *width / geometry->nw;
803 *height = geometry->sh * *height / geometry->nh;
804
805 x -= x % 2;
806
807 // optimization points - no work to do
808 if ( *width <= 0 || *height <= 0 )
809 return 1;
810
811 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
812 return 1;
813
814 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
815
816 return ret;
817 }
818
819
820 struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transition this, mlt_frame a_frame, float position )
821 {
822 // Get the properties from the transition
823 mlt_properties properties = mlt_transition_properties( this );
824
825 // Get the properties from the frame
826 mlt_properties a_props = mlt_frame_properties( a_frame );
827
828 // Structures for geometry
829 struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
830
831 // Now parse the geometries
832 if ( start == NULL )
833 {
834 // Obtain the normalised width and height from the a_frame
835 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
836 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
837
838 // Parse the transitions properties
839 start = transition_parse_keys( this, normalised_width, normalised_height );
840 }
841
842 // Do the calculation
843 geometry_calculate( result, start, position );
844
845 // Now parse the alignment
846 result->halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
847 result->valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
848
849 return start;
850 }
851
852 mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_position frame_position )
853 {
854 // Create a frame to return
855 mlt_frame b_frame = mlt_frame_init( );
856
857 // Get the properties of the a frame
858 mlt_properties a_props = mlt_frame_properties( a_frame );
859
860 // Get the properties of the b frame
861 mlt_properties b_props = mlt_frame_properties( b_frame );
862
863 // Get the position
864 float position = position_calculate( this, frame_position );
865
866 // Destination image
867 uint8_t *dest = NULL;
868
869 // Get the image and dimensions
870 uint8_t *image = mlt_properties_get_data( a_props, "image", NULL );
871 int width = mlt_properties_get_int( a_props, "width" );
872 int height = mlt_properties_get_int( a_props, "height" );
873
874 // Pointers for copy operation
875 uint8_t *p;
876 uint8_t *q;
877 uint8_t *r;
878
879 // Corrdinates
880 int w = 0;
881 int h = 0;
882 int x = 0;
883 int y = 0;
884
885 // Will need to know region to copy
886 struct geometry_s result;
887
888 // Calculate the region now
889 composite_calculate( &result, this, a_frame, position );
890
891 // Need to scale down to actual dimensions
892 x = result.x * width / result.nw ;
893 y = result.y * height / result.nh;
894 w = result.w * width / result.nw;
895 h = result.h * height / result.nh;
896
897 x &= 0xfffffffe;
898 w &= 0xfffffffe;
899
900 // Now we need to create a new destination image
901 dest = mlt_pool_alloc( w * h * 2 );
902
903 // Copy the region of the image
904 p = image + y * width * 2 + x * 2;
905 q = dest;
906 r = dest + w * h * 2;
907
908 while ( q < r )
909 {
910 memcpy( q, p, w * 2 );
911 q += w * 2;
912 p += width * 2;
913 }
914
915 // Assign to the new frame
916 mlt_properties_set_data( b_props, "image", dest, w * h * 2, mlt_pool_release, NULL );
917 mlt_properties_set_int( b_props, "width", w );
918 mlt_properties_set_int( b_props, "height", h );
919
920 // Assign this position to the b frame
921 mlt_frame_set_position( b_frame, frame_position );
922
923 // Return the frame
924 return b_frame;
925 }
926
927 /** Get the image.
928 */
929
930 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
931 {
932 // Get the b frame from the stack
933 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
934
935 // Get the transition from the a frame
936 mlt_transition this = mlt_frame_pop_service( a_frame );
937
938 // This compositer is yuv422 only
939 *format = mlt_image_yuv422;
940
941 // Get the image from the a frame
942 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
943
944 if ( b_frame != NULL )
945 {
946 // Get the properties of the a frame
947 mlt_properties a_props = mlt_frame_properties( a_frame );
948
949 // Get the properties of the b frame
950 mlt_properties b_props = mlt_frame_properties( b_frame );
951
952 // Get the properties from the transition
953 mlt_properties properties = mlt_transition_properties( this );
954
955 // Structures for geometry
956 struct geometry_s result;
957
958 // Calculate the position
959 float position = mlt_properties_get_double( b_props, "relative_position" );
960 float delta = delta_calculate( this, a_frame );
961
962 // Do the calculation
963 struct geometry_s *start = composite_calculate( &result, this, a_frame, position );
964
965 // Optimisation - no compositing required
966 if ( result.mix == 0 || ( result.w == 0 && result.h == 0 ) )
967 return 0;
968
969 // Since we are the consumer of the b_frame, we must pass along these
970 // consumer properties from the a_frame
971 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
972
973 // Get the image from the b frame
974 uint8_t *image_b = NULL;
975 int width_b = *width;
976 int height_b = *height;
977
978 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
979 {
980 uint8_t *dest = *image;
981 uint8_t *src = image_b;
982 int bpp = 2;
983 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
984 int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
985 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
986 mlt_properties_get_int( properties, "progressive" );
987 int field;
988
989 int32_t luma_softness = mlt_properties_get_double( properties, "softness" ) * ( 1 << 16 );
990 uint16_t *luma_bitmap = get_luma( properties, width_b, height_b );
991
992 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
993 {
994 // Assume lower field (0) first
995 float field_position = position + field * delta;
996
997 // Do the calculation if we need to
998 geometry_calculate( &result, start, field_position );
999
1000 // Align
1001 alignment_calculate( &result );
1002
1003 // Composite the b_frame on the a_frame
1004 composite_yuv( dest, *width, *height, bpp, src, width_b, height_b, alpha, result, progressive ? -1 : field, luma_bitmap, luma_softness );
1005 }
1006 }
1007 }
1008
1009 return 0;
1010 }
1011
1012 /** Composition transition processing.
1013 */
1014
1015 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
1016 {
1017 // Propogate the transition properties to the b frame
1018 mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame ) ) );
1019 mlt_frame_push_service( a_frame, this );
1020 mlt_frame_push_get_image( a_frame, transition_get_image );
1021 mlt_frame_push_frame( a_frame, b_frame );
1022 return a_frame;
1023 }
1024
1025 /** Constructor for the filter.
1026 */
1027
1028 mlt_transition transition_composite_init( char *arg )
1029 {
1030 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
1031 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
1032 {
1033 this->process = composite_process;
1034 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
1035
1036 // Default factory
1037 mlt_properties_set( mlt_transition_properties( this ), "factory", "fezzik" );
1038 }
1039 return this;
1040 }