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