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 static inline int dissolve_yuv( mlt_frame this, mlt_frame that, float weight, int width, int height )
35 int width_src = width, height_src = height;
36 mlt_image_format format = mlt_image_yuv422;
37 uint8_t *p_src, *p_dest;
43 int32_t weigh = weight * ( 1 << 16 );
44 int32_t weigh_complement = ( 1 - weight ) * ( 1 << 16 );
46 if ( mlt_properties_get( &this->parent, "distort" ) )
47 mlt_properties_set( &that->parent, "distort", mlt_properties_get( &this->parent, "distort" ) );
48 mlt_frame_get_image( this, &p_dest, &format, &width, &height, 1 );
49 alpha_dst = mlt_frame_get_alpha_mask( this );
50 mlt_frame_get_image( that, &p_src, &format, &width_src, &height_src, 0 );
51 alpha_src = mlt_frame_get_alpha_mask( that );
53 // Pick the lesser of two evils ;-)
54 width_src = width_src > width ? width : width_src;
55 height_src = height_src > height ? height : height_src;
59 limit = p_dest + height_src * width_src * 2;
63 *p_dest++ = ( *p_src++ * weigh + *p++ * weigh_complement ) >> 16;
64 *p_dest++ = ( *p_src++ * weigh + *p++ * weigh_complement ) >> 16;
65 *alpha_dst++ = ( *alpha_src++ * weigh + *q++ * weigh_complement ) >> 16;
71 // image processing functions
73 static inline int32_t smoothstep( int32_t edge1, int32_t edge2, uint32_t a )
81 a = ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
83 return ( ( ( a * a ) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a ) ) ) >> 16;
88 \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
90 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
91 uint16_t *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
92 int *width, int *height )
94 int width_src = *width, height_src = *height;
95 int width_dest = *width, height_dest = *height;
96 mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
97 uint8_t *p_src, *p_dest;
103 if ( mlt_properties_get( &a_frame->parent, "distort" ) )
104 mlt_properties_set( &b_frame->parent, "distort", mlt_properties_get( &a_frame->parent, "distort" ) );
105 mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 );
106 mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 );
108 if ( *width == 0 || *height == 0 )
111 // Pick the lesser of two evils ;-)
112 width_src = width_src > width_dest ? width_dest : width_src;
113 height_src = height_src > height_dest ? height_dest : height_src;
115 stride_src = width_src * 2;
116 stride_dest = width_dest * 2;
118 // Offset the position based on which field we're looking at ...
119 int32_t field_pos[ 2 ];
120 field_pos[ 0 ] = ( pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
121 field_pos[ 1 ] = ( pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
130 int32_t x_diff = ( luma_width << 16 ) / *width;
131 int32_t y_diff = ( luma_height << 16 ) / *height;
132 int32_t x_offset = 0;
133 int32_t y_offset = 0;
137 int32_t i_softness = softness * ( 1 << 16 );
139 int field_count = field_order < 0 ? 1 : 2;
140 int field_stride_src = field_count * stride_src;
141 int field_stride_dest = field_count * stride_dest;
144 // composite using luma map
145 while ( field < field_count )
147 p_row = p_src + field * stride_src;
148 q_row = p_dest + field * stride_dest;
149 y_offset = field << 16;
152 while ( i < height_src )
157 l = luma_bitmap + ( y_offset >> 16 ) * ( luma_width * field_count );
163 weight = l[ x_offset >> 16 ];
164 value = smoothstep( weight, i_softness + weight, field_pos[ field ] );
165 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
166 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
172 p_row += field_stride_src;
173 q_row += field_stride_dest;
180 /** Load the luma map from PGM stream.
183 static void luma_read_pgm( FILE *f, uint16_t **map, int *width, int *height )
185 uint8_t *data = NULL;
197 // get the magic code
198 if ( fgets( line, 127, f ) == NULL )
202 while ( sscanf( line, " #%s", comment ) > 0 )
203 if ( fgets( line, 127, f ) == NULL )
206 if ( line[0] != 'P' || line[1] != '5' )
209 // skip white space and see if a new line must be fetched
210 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
211 if ( ( line[i] == '\0' || line[i] == '#' ) && fgets( line, 127, f ) == NULL )
215 while ( sscanf( line, " #%s", comment ) > 0 )
216 if ( fgets( line, 127, f ) == NULL )
219 // get the dimensions
220 if ( line[0] == 'P' )
221 i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
223 i = sscanf( line, "%d %d %d", width, height, &maxval );
225 // get the height value, if not yet
228 if ( fgets( line, 127, f ) == NULL )
232 while ( sscanf( line, " #%s", comment ) > 0 )
233 if ( fgets( line, 127, f ) == NULL )
236 i = sscanf( line, "%d", height );
243 // get the maximum gray value, if not yet
246 if ( fgets( line, 127, f ) == NULL )
250 while ( sscanf( line, " #%s", comment ) > 0 )
251 if ( fgets( line, 127, f ) == NULL )
254 i = sscanf( line, "%d", &maxval );
259 // determine if this is one or two bytes per pixel
260 bpp = maxval > 255 ? 2 : 1;
262 // allocate temporary storage for the raw data
263 data = mlt_pool_alloc( *width * *height * bpp );
268 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
271 // allocate the luma bitmap
272 *map = p = (uint16_t*)mlt_pool_alloc( *width * *height * sizeof( uint16_t ) );
276 // proces the raw data into the luma bitmap
277 for ( i = 0; i < *width * *height * bpp; i += bpp )
280 *p++ = data[ i ] << 8;
282 *p++ = ( data[ i ] << 8 ) + data[ i+1 ];
289 mlt_pool_release( data );
292 /** Generate a luma map from an RGB image.
295 static void luma_read_yuv422( uint8_t *image, uint16_t **map, int width, int height )
298 int size = width * height * 2;
300 // allocate the luma bitmap
301 uint16_t *p = *map = ( uint16_t* )mlt_pool_alloc( width * height * sizeof( uint16_t ) );
305 // proces the image data into the luma bitmap
306 for ( i = 0; i < size; i += 2 )
307 *p++ = ( image[ i ] - 16 ) * 299; // 299 = 65535 / 219
313 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
315 // Get the b frame from the stack
316 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
318 // Get the transition object
319 mlt_transition transition = mlt_frame_pop_service( a_frame );
321 // Get the properties of the transition
322 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
324 // Get the properties of the a frame
325 mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
327 // Get the properties of the b frame
328 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
330 // This compositer is yuv422 only
331 *format = mlt_image_yuv422;
333 mlt_service_lock( MLT_TRANSITION_SERVICE( transition ) );
335 // The cached luma map information
336 int luma_width = mlt_properties_get_int( properties, "width" );
337 int luma_height = mlt_properties_get_int( properties, "height" );
338 uint16_t *luma_bitmap = mlt_properties_get_data( properties, "bitmap", NULL );
339 char *current_resource = mlt_properties_get( properties, "_resource" );
341 // If the filename property changed, reload the map
342 char *resource = mlt_properties_get( properties, "resource" );
344 // Correct width/height if not specified
345 if ( luma_width == 0 || luma_height == 0 )
348 luma_height = *height;
351 if ( resource && ( !current_resource || strcmp( resource, current_resource ) ) )
354 char *extension = strrchr( resource, '.' );
355 char *orig_resource = resource;
357 if ( strchr( resource, '%' ) )
360 sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
361 test = fopen( temp, "r" );
363 strcat( temp, ".png" );
367 extension = strrchr( resource, '.' );
370 // See if it is a PGM
371 if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
374 FILE *f = fopen( resource, "r" );
378 luma_read_pgm( f, &luma_bitmap, &luma_width, &luma_height );
381 // Set the transition properties
382 mlt_properties_set_int( properties, "width", luma_width );
383 mlt_properties_set_int( properties, "height", luma_height );
384 mlt_properties_set( properties, "_resource", orig_resource );
385 mlt_properties_set_data( properties, "bitmap", luma_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
391 mlt_properties_set( properties, "_resource", NULL );
392 mlt_properties_set_data( properties, "bitmap", luma_bitmap, 0, mlt_pool_release, NULL );
396 // Get the factory producer service
397 char *factory = mlt_properties_get( properties, "factory" );
399 // Create the producer
400 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
401 mlt_producer producer = mlt_factory_producer( profile, factory, resource );
404 if ( producer != NULL )
406 // Get the producer properties
407 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
409 // Ensure that we loop
410 mlt_properties_set( producer_properties, "eof", "loop" );
412 // Now pass all producer. properties on the transition down
413 mlt_properties_pass( producer_properties, properties, "producer." );
415 // We will get the alpha frame from the producer
416 mlt_frame luma_frame = NULL;
418 // Get the luma frame
419 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 )
421 uint8_t *luma_image = NULL;
422 mlt_image_format luma_format = mlt_image_yuv422;
424 // Get image from the luma producer
425 mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "nearest" );
426 mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
428 // Generate the luma map
429 if ( luma_image != NULL )
430 luma_read_yuv422( luma_image, &luma_bitmap, luma_width, luma_height );
432 // Set the transition properties
433 mlt_properties_set_int( properties, "width", luma_width );
434 mlt_properties_set_int( properties, "height", luma_height );
435 mlt_properties_set( properties, "_resource", orig_resource);
436 mlt_properties_set_data( properties, "bitmap", luma_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
438 // Cleanup the luma frame
439 mlt_frame_close( luma_frame );
442 // Cleanup the luma producer
443 mlt_producer_close( producer );
448 // Arbitrary composite defaults
449 float mix = mlt_transition_get_progress( transition, a_frame );
450 float frame_delta = mlt_transition_get_progress_delta( transition, a_frame );
451 float luma_softness = mlt_properties_get_double( properties, "softness" );
453 mlt_properties_get_int( a_props, "consumer_deinterlace" ) ||
454 mlt_properties_get_int( properties, "progressive" ) ||
455 mlt_properties_get_int( b_props, "luma.progressive" );
456 int top_field_first = mlt_properties_get_int( b_props, "top_field_first" );
457 int reverse = mlt_properties_get_int( properties, "reverse" );
458 int invert = mlt_properties_get_int( properties, "invert" );
460 // Honour the reverse here
464 if ( mlt_properties_get( properties, "fixed" ) )
465 mix = mlt_properties_get_double( properties, "fixed" );
467 if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
469 reverse = invert ? !reverse : reverse;
470 mix = reverse ? 1 - mix : mix;
471 frame_delta *= reverse ? -1.0 : 1.0;
472 // Composite the frames using a luma map
473 luma_composite( !invert ? a_frame : b_frame, !invert ? b_frame : a_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
474 luma_softness, progressive ? -1 : top_field_first, width, height );
478 mix = ( reverse || invert ) ? 1 - mix : mix;
480 // Dissolve the frames using the time offset for mix value
481 dissolve_yuv( a_frame, b_frame, mix, *width, *height );
484 mlt_service_unlock( MLT_TRANSITION_SERVICE( transition ) );
486 // Extract the a_frame image info
487 *width = mlt_properties_get_int( !invert ? a_props : b_props, "width" );
488 *height = mlt_properties_get_int( !invert ? a_props : b_props, "height" );
489 *image = mlt_properties_get_data( !invert ? a_props : b_props, "image", NULL );
495 /** Luma transition processing.
498 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
500 // Push the transition on to the frame
501 mlt_frame_push_service( a_frame, transition );
503 // Push the b_frame on to the stack
504 mlt_frame_push_frame( a_frame, b_frame );
506 // Push the transition method
507 mlt_frame_push_get_image( a_frame, transition_get_image );
512 /** Constructor for the filter.
515 mlt_transition transition_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *lumafile )
517 mlt_transition transition = mlt_transition_new( );
518 if ( transition != NULL )
521 transition->process = transition_process;
524 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "factory", mlt_environment( "MLT_PRODUCER" ) );
526 // Set the main property
527 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "resource", lumafile );
529 // Inform apps and framework that this is a video only transition
530 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );