2 * transition_luma.c -- a generic dissolve/wipe processor
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * Adapted from Kino Plugin Timfx, which is
7 * Copyright (C) 2002 Timothy M. Shead <tshead@k-3d.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <framework/mlt.h>
32 /** Calculate the position for this frame.
35 static float position_calculate( mlt_transition
this, mlt_frame frame
)
37 // Get the in and out position
38 mlt_position in
= mlt_transition_get_in( this );
39 mlt_position out
= mlt_transition_get_out( this );
41 // Get the position of the frame
42 char *name
= mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
43 mlt_position position
= mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame
), name
);
46 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
49 /** Calculate the field delta for this frame - position between two frames.
52 static float delta_calculate( mlt_transition
this, mlt_frame frame
)
54 // Get the in and out position
55 mlt_position in
= mlt_transition_get_in( this );
56 mlt_position out
= mlt_transition_get_out( this );
58 // Get the position of the frame
59 mlt_position position
= mlt_frame_get_position( frame
);
62 float x
= ( float )( position
- in
) / ( float )( out
- in
+ 1 );
63 float y
= ( float )( position
+ 1 - in
) / ( float )( out
- in
+ 1 );
65 return ( y
- x
) / 2.0;
68 static inline int dissolve_yuv( mlt_frame
this, mlt_frame that
, float weight
, int width
, int height
)
71 int width_src
= width
, height_src
= height
;
72 mlt_image_format format
= mlt_image_yuv422
;
73 uint8_t *p_src
, *p_dest
;
79 int32_t weigh
= weight
* ( 1 << 16 );
80 int32_t weigh_complement
= ( 1 - weight
) * ( 1 << 16 );
82 if ( mlt_properties_get( &this->parent
, "distort" ) )
83 mlt_properties_set( &that
->parent
, "distort", mlt_properties_get( &this->parent
, "distort" ) );
84 mlt_properties_set_int( &that
->parent
, "consumer_deinterlace", mlt_properties_get_int( &this->parent
, "consumer_deinterlace" ) );
85 mlt_frame_get_image( this, &p_dest
, &format
, &width
, &height
, 1 );
86 alpha_dst
= mlt_frame_get_alpha_mask( this );
87 mlt_frame_get_image( that
, &p_src
, &format
, &width_src
, &height_src
, 0 );
88 alpha_src
= mlt_frame_get_alpha_mask( that
);
90 // Pick the lesser of two evils ;-)
91 width_src
= width_src
> width ? width
: width_src
;
92 height_src
= height_src
> height ? height
: height_src
;
96 limit
= p_dest
+ height_src
* width_src
* 2;
100 *p_dest
++ = ( *p_src
++ * weigh
+ *p
++ * weigh_complement
) >> 16;
101 *p_dest
++ = ( *p_src
++ * weigh
+ *p
++ * weigh_complement
) >> 16;
102 *alpha_dst
++ = ( *alpha_src
++ * weigh
+ *q
++ * weigh_complement
) >> 16;
108 // image processing functions
110 static inline int32_t smoothstep( int32_t edge1
, int32_t edge2
, uint32_t a
)
118 a
= ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
120 return ( ( ( a
* a
) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a
) ) ) >> 16;
125 \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
127 static void luma_composite( mlt_frame a_frame
, mlt_frame b_frame
, int luma_width
, int luma_height
,
128 uint16_t *luma_bitmap
, float pos
, float frame_delta
, float softness
, int field_order
,
129 int *width
, int *height
)
131 int width_src
= *width
, height_src
= *height
;
132 int width_dest
= *width
, height_dest
= *height
;
133 mlt_image_format format_src
= mlt_image_yuv422
, format_dest
= mlt_image_yuv422
;
134 uint8_t *p_src
, *p_dest
;
140 format_src
= mlt_image_yuv422
;
141 format_dest
= mlt_image_yuv422
;
143 if ( mlt_properties_get( &a_frame
->parent
, "distort" ) )
144 mlt_properties_set( &b_frame
->parent
, "distort", mlt_properties_get( &a_frame
->parent
, "distort" ) );
145 mlt_properties_set_int( &b_frame
->parent
, "consumer_deinterlace", mlt_properties_get_int( &a_frame
->parent
, "consumer_deinterlace" ) );
146 mlt_frame_get_image( a_frame
, &p_dest
, &format_dest
, &width_dest
, &height_dest
, 1 );
147 mlt_frame_get_image( b_frame
, &p_src
, &format_src
, &width_src
, &height_src
, 0 );
149 if ( *width
== 0 || *height
== 0 )
152 // Pick the lesser of two evils ;-)
153 width_src
= width_src
> width_dest ? width_dest
: width_src
;
154 height_src
= height_src
> height_dest ? height_dest
: height_src
;
156 stride_src
= width_src
* 2;
157 stride_dest
= width_dest
* 2;
159 // Offset the position based on which field we're looking at ...
160 int32_t field_pos
[ 2 ];
161 field_pos
[ 0 ] = ( pos
+ ( ( field_order
== 0 ?
1 : 0 ) * frame_delta
* 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness
);
162 field_pos
[ 1 ] = ( pos
+ ( ( field_order
== 0 ?
0 : 1 ) * frame_delta
* 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness
);
171 int32_t x_diff
= ( luma_width
<< 16 ) / *width
;
172 int32_t y_diff
= ( luma_height
<< 16 ) / *height
;
173 int32_t x_offset
= 0;
174 int32_t y_offset
= 0;
178 int32_t i_softness
= softness
* ( 1 << 16 );
180 int field_count
= field_order
< 0 ?
1 : 2;
181 int field_stride_src
= field_count
* stride_src
;
182 int field_stride_dest
= field_count
* stride_dest
;
185 // composite using luma map
186 while ( field
< field_count
)
188 p_row
= p_src
+ field
* stride_src
;
189 q_row
= p_dest
+ field
* stride_dest
;
190 y_offset
= field
<< 16;
193 while ( i
< height_src
)
198 l
= luma_bitmap
+ ( y_offset
>> 16 ) * ( luma_width
* field_count
);
204 weight
= l
[ x_offset
>> 16 ];
205 value
= smoothstep( weight
, i_softness
+ weight
, field_pos
[ field
] );
206 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
207 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
213 p_row
+= field_stride_src
;
214 q_row
+= field_stride_dest
;
221 /** Load the luma map from PGM stream.
224 static void luma_read_pgm( FILE *f
, uint16_t **map
, int *width
, int *height
)
226 uint8_t *data
= NULL
;
238 // get the magic code
239 if ( fgets( line
, 127, f
) == NULL
)
243 while ( sscanf( line
, " #%s", comment
) > 0 )
244 if ( fgets( line
, 127, f
) == NULL
)
247 if ( line
[0] != 'P' || line
[1] != '5' )
250 // skip white space and see if a new line must be fetched
251 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
252 if ( ( line
[i
] == '\0' || line
[i
] == '#' ) && fgets( line
, 127, f
) == NULL
)
256 while ( sscanf( line
, " #%s", comment
) > 0 )
257 if ( fgets( line
, 127, f
) == NULL
)
260 // get the dimensions
261 if ( line
[0] == 'P' )
262 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
264 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
266 // get the height value, if not yet
269 if ( fgets( line
, 127, f
) == NULL
)
273 while ( sscanf( line
, " #%s", comment
) > 0 )
274 if ( fgets( line
, 127, f
) == NULL
)
277 i
= sscanf( line
, "%d", height
);
284 // get the maximum gray value, if not yet
287 if ( fgets( line
, 127, f
) == NULL
)
291 while ( sscanf( line
, " #%s", comment
) > 0 )
292 if ( fgets( line
, 127, f
) == NULL
)
295 i
= sscanf( line
, "%d", &maxval
);
300 // determine if this is one or two bytes per pixel
301 bpp
= maxval
> 255 ?
2 : 1;
303 // allocate temporary storage for the raw data
304 data
= mlt_pool_alloc( *width
* *height
* bpp
);
309 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
312 // allocate the luma bitmap
313 *map
= p
= (uint16_t*)mlt_pool_alloc( *width
* *height
* sizeof( uint16_t ) );
317 // proces the raw data into the luma bitmap
318 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
321 *p
++ = data
[ i
] << 8;
323 *p
++ = ( data
[ i
] << 8 ) + data
[ i
+1 ];
330 mlt_pool_release( data
);
333 /** Generate a luma map from an RGB image.
336 static void luma_read_yuv422( uint8_t *image
, uint16_t **map
, int width
, int height
)
339 int size
= width
* height
* 2;
341 // allocate the luma bitmap
342 uint16_t *p
= *map
= ( uint16_t* )mlt_pool_alloc( width
* height
* sizeof( uint16_t ) );
346 // proces the image data into the luma bitmap
347 for ( i
= 0; i
< size
; i
+= 2 )
348 *p
++ = ( image
[ i
] - 16 ) * 299; // 299 = 65535 / 219
351 /** Generate a luma map from a YUV image.
353 static void luma_read_rgb24( uint8_t *image
, uint16_t **map
, int width
, int height
)
360 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
362 // Get the b frame from the stack
363 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
365 // Get the transition object
366 mlt_transition transition
= mlt_frame_pop_service( a_frame
);
368 // Get the properties of the transition
369 mlt_properties properties
= MLT_TRANSITION_PROPERTIES( transition
);
371 // Get the properties of the a frame
372 mlt_properties a_props
= MLT_FRAME_PROPERTIES( a_frame
);
374 // Get the properties of the b frame
375 mlt_properties b_props
= MLT_FRAME_PROPERTIES( b_frame
);
377 // This compositer is yuv422 only
378 *format
= mlt_image_yuv422
;
380 // The cached luma map information
381 int luma_width
= mlt_properties_get_int( properties
, "width" );
382 int luma_height
= mlt_properties_get_int( properties
, "height" );
383 uint16_t *luma_bitmap
= mlt_properties_get_data( properties
, "bitmap", NULL
);
384 char *current_resource
= mlt_properties_get( properties
, "_resource" );
386 // If the filename property changed, reload the map
387 char *resource
= mlt_properties_get( properties
, "resource" );
389 // Correct width/height if not specified
390 if ( luma_width
== 0 || luma_height
== 0 )
392 luma_width
= mlt_properties_get_int( a_props
, "width" );
393 luma_height
= mlt_properties_get_int( a_props
, "height" );
396 if ( resource
!= current_resource
)
399 char *extension
= strrchr( resource
, '.' );
401 if ( strchr( resource
, '%' ) )
404 sprintf( temp
, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource
, '%' ) + 1 );
405 test
= fopen( temp
, "r" );
407 strcat( temp
, ".png" );
411 extension
= strrchr( resource
, '.' );
414 // See if it is a PGM
415 if ( extension
!= NULL
&& strcmp( extension
, ".pgm" ) == 0 )
418 FILE *f
= fopen( resource
, "r" );
422 luma_read_pgm( f
, &luma_bitmap
, &luma_width
, &luma_height
);
425 // Set the transition properties
426 mlt_properties_set_int( properties
, "width", luma_width
);
427 mlt_properties_set_int( properties
, "height", luma_height
);
428 mlt_properties_set( properties
, "_resource", resource
);
429 mlt_properties_set_data( properties
, "bitmap", luma_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
435 mlt_properties_set( properties
, "_resource", NULL
);
436 mlt_properties_set_data( properties
, "bitmap", luma_bitmap
, 0, mlt_pool_release
, NULL
);
440 // Get the factory producer service
441 char *factory
= mlt_properties_get( properties
, "factory" );
443 // Create the producer
444 mlt_profile profile
= mlt_service_profile( MLT_TRANSITION_SERVICE( transition
) );
445 mlt_producer producer
= mlt_factory_producer( profile
, factory
, resource
);
448 if ( producer
!= NULL
)
450 // Get the producer properties
451 mlt_properties producer_properties
= MLT_PRODUCER_PROPERTIES( producer
);
453 // Ensure that we loop
454 mlt_properties_set( producer_properties
, "eof", "loop" );
456 // Now pass all producer. properties on the transition down
457 mlt_properties_pass( producer_properties
, properties
, "producer." );
459 // We will get the alpha frame from the producer
460 mlt_frame luma_frame
= NULL
;
462 // Get the luma frame
463 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer
), &luma_frame
, 0 ) == 0 )
465 uint8_t *luma_image
= NULL
;
466 mlt_image_format luma_format
= mlt_image_yuv422
;
468 // Get image from the luma producer
469 mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame
), "rescale.interp", "nearest" );
470 mlt_frame_get_image( luma_frame
, &luma_image
, &luma_format
, &luma_width
, &luma_height
, 0 );
472 // Generate the luma map
473 if ( luma_image
!= NULL
&& luma_format
== mlt_image_yuv422
)
474 luma_read_yuv422( luma_image
, &luma_bitmap
, luma_width
, luma_height
);
476 else if ( luma_image
!= NULL
&& luma_format
== mlt_image_rgb24
)
477 luma_read_rgb24( luma_image
, &luma_bitmap
, luma_width
, luma_height
);
479 // Set the transition properties
480 mlt_properties_set_int( properties
, "width", luma_width
);
481 mlt_properties_set_int( properties
, "height", luma_height
);
482 mlt_properties_set( properties
, "_resource", resource
);
483 mlt_properties_set_data( properties
, "bitmap", luma_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
485 // Cleanup the luma frame
486 mlt_frame_close( luma_frame
);
489 // Cleanup the luma producer
490 mlt_producer_close( producer
);
495 // Arbitrary composite defaults
496 float mix
= position_calculate( transition
, a_frame
);
497 float frame_delta
= delta_calculate( transition
, a_frame
);
499 float luma_softness
= mlt_properties_get_double( properties
, "softness" );
501 mlt_properties_get_int( a_props
, "consumer_deinterlace" ) ||
502 mlt_properties_get_int( properties
, "progressive" ) ||
503 mlt_properties_get_int( b_props
, "luma.progressive" );
504 int top_field_first
= mlt_properties_get_int( b_props
, "top_field_first" );
505 int reverse
= mlt_properties_get_int( properties
, "reverse" );
506 int invert
= mlt_properties_get_int( properties
, "invert" );
508 if ( mlt_properties_get( a_props
, "rescale.interp" ) == NULL
|| !strcmp( mlt_properties_get( a_props
, "rescale.interp" ), "none" ) )
509 mlt_properties_set( a_props
, "rescale.interp", "nearest" );
511 // Since we are the consumer of the b_frame, we must pass along this
512 // consumer property from the a_frame
513 if ( mlt_properties_get_double( a_props
, "aspect_ratio" ) == 0.0 )
514 mlt_properties_set_double( a_props
, "aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
515 if ( mlt_properties_get_double( b_props
, "aspect_ratio" ) == 0.0 )
516 mlt_properties_set_double( b_props
, "aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
517 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
519 // Honour the reverse here
523 mix
= reverse
|| invert ?
1 - mix
: mix
;
524 frame_delta
*= reverse
|| invert ?
-1.0 : 1.0;
526 // Ensure we get scaling on the b_frame
527 if ( mlt_properties_get( b_props
, "rescale.interp" ) == NULL
|| !strcmp( mlt_properties_get( b_props
, "rescale.interp" ), "none" ) )
528 mlt_properties_set( b_props
, "rescale.interp", "nearest" );
530 if ( mlt_properties_get( properties
, "fixed" ) )
531 mix
= mlt_properties_get_double( properties
, "fixed" );
533 if ( luma_width
> 0 && luma_height
> 0 && luma_bitmap
!= NULL
)
534 // Composite the frames using a luma map
535 luma_composite( !invert ? a_frame
: b_frame
, !invert ? b_frame
: a_frame
, luma_width
, luma_height
, luma_bitmap
, mix
, frame_delta
,
536 luma_softness
, progressive ?
-1 : top_field_first
, width
, height
);
538 // Dissolve the frames using the time offset for mix value
539 dissolve_yuv( a_frame
, b_frame
, mix
, *width
, *height
);
541 // Extract the a_frame image info
542 *width
= mlt_properties_get_int( !invert ? a_props
: b_props
, "width" );
543 *height
= mlt_properties_get_int( !invert ? a_props
: b_props
, "height" );
544 *image
= mlt_properties_get_data( !invert ? a_props
: b_props
, "image", NULL
);
550 /** Luma transition processing.
553 static mlt_frame
transition_process( mlt_transition transition
, mlt_frame a_frame
, mlt_frame b_frame
)
555 // Get a unique name to store the frame position
556 char *name
= mlt_properties_get( MLT_TRANSITION_PROPERTIES( transition
), "_unique_id" );
558 // Assign the current position to the name
559 mlt_properties_set_position( MLT_FRAME_PROPERTIES( a_frame
), name
, mlt_frame_get_position( a_frame
) );
561 // Push the transition on to the frame
562 mlt_frame_push_service( a_frame
, transition
);
564 // Push the b_frame on to the stack
565 mlt_frame_push_frame( a_frame
, b_frame
);
567 // Push the transition method
568 mlt_frame_push_get_image( a_frame
, transition_get_image
);
573 /** Constructor for the filter.
576 mlt_transition
transition_luma_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *lumafile
)
578 mlt_transition transition
= mlt_transition_new( );
579 if ( transition
!= NULL
)
582 transition
->process
= transition_process
;
585 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition
), "factory", "fezzik" );
587 // Set the main property
588 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition
), "resource", lumafile
);
590 // Inform apps and framework that this is a video only transition
591 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition
), "_transition_type", 1 );