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>
31 #include "transition_composite.h"
33 static inline int dissolve_yuv( mlt_frame this, mlt_frame that, float weight, int width, int height )
37 int width_src = width, height_src = height;
38 mlt_image_format format = mlt_image_yuv422;
39 uint8_t *p_src, *p_dest;
42 int mix = weight * ( 1 << 16 );
44 if ( mlt_properties_get( &this->parent, "distort" ) )
45 mlt_properties_set( &that->parent, "distort", mlt_properties_get( &this->parent, "distort" ) );
46 mlt_frame_get_image( this, &p_dest, &format, &width, &height, 1 );
47 alpha_dst = mlt_frame_get_alpha_mask( this );
48 mlt_frame_get_image( that, &p_src, &format, &width_src, &height_src, 0 );
49 alpha_src = mlt_frame_get_alpha_mask( that );
51 // Pick the lesser of two evils ;-)
52 width_src = width_src > width ? width : width_src;
53 height_src = height_src > height ? height : height_src;
57 composite_line_yuv( p_dest, p_src, width_src, alpha_src, alpha_dst, mix, NULL, 0, 0 );
58 p_src += width_src << 1;
60 alpha_src += width_src;
67 // image processing functions
69 static inline int32_t smoothstep( int32_t edge1, int32_t edge2, uint32_t a )
77 a = ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
79 return ( ( ( a * a ) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a ) ) ) >> 16;
84 \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
86 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
87 uint16_t *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
88 int *width, int *height )
90 int width_src = *width, height_src = *height;
91 int width_dest = *width, height_dest = *height;
92 mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
93 uint8_t *p_src, *p_dest;
99 if ( mlt_properties_get( &a_frame->parent, "distort" ) )
100 mlt_properties_set( &b_frame->parent, "distort", mlt_properties_get( &a_frame->parent, "distort" ) );
101 mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 );
102 mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 );
104 if ( *width == 0 || *height == 0 )
107 // Pick the lesser of two evils ;-)
108 width_src = width_src > width_dest ? width_dest : width_src;
109 height_src = height_src > height_dest ? height_dest : height_src;
111 stride_src = width_src * 2;
112 stride_dest = width_dest * 2;
114 // Offset the position based on which field we're looking at ...
115 int32_t field_pos[ 2 ];
116 field_pos[ 0 ] = ( pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
117 field_pos[ 1 ] = ( pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
126 int32_t x_diff = ( luma_width << 16 ) / *width;
127 int32_t y_diff = ( luma_height << 16 ) / *height;
128 int32_t x_offset = 0;
129 int32_t y_offset = 0;
133 int32_t i_softness = softness * ( 1 << 16 );
135 int field_count = field_order < 0 ? 1 : 2;
136 int field_stride_src = field_count * stride_src;
137 int field_stride_dest = field_count * stride_dest;
140 // composite using luma map
141 while ( field < field_count )
143 p_row = p_src + field * stride_src;
144 q_row = p_dest + field * stride_dest;
145 y_offset = field << 16;
148 while ( i < height_src )
153 l = luma_bitmap + ( y_offset >> 16 ) * ( luma_width * field_count );
159 weight = l[ x_offset >> 16 ];
160 value = smoothstep( weight, i_softness + weight, field_pos[ field ] );
161 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
162 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
168 p_row += field_stride_src;
169 q_row += field_stride_dest;
176 /** Load the luma map from PGM stream.
179 static void luma_read_pgm( FILE *f, uint16_t **map, int *width, int *height )
181 uint8_t *data = NULL;
193 // get the magic code
194 if ( fgets( line, 127, f ) == NULL )
198 while ( sscanf( line, " #%s", comment ) > 0 )
199 if ( fgets( line, 127, f ) == NULL )
202 if ( line[0] != 'P' || line[1] != '5' )
205 // skip white space and see if a new line must be fetched
206 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
207 if ( ( line[i] == '\0' || line[i] == '#' ) && fgets( line, 127, f ) == NULL )
211 while ( sscanf( line, " #%s", comment ) > 0 )
212 if ( fgets( line, 127, f ) == NULL )
215 // get the dimensions
216 if ( line[0] == 'P' )
217 i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
219 i = sscanf( line, "%d %d %d", width, height, &maxval );
221 // get the height value, if not yet
224 if ( fgets( line, 127, f ) == NULL )
228 while ( sscanf( line, " #%s", comment ) > 0 )
229 if ( fgets( line, 127, f ) == NULL )
232 i = sscanf( line, "%d", height );
239 // get the maximum gray value, if not yet
242 if ( fgets( line, 127, f ) == NULL )
246 while ( sscanf( line, " #%s", comment ) > 0 )
247 if ( fgets( line, 127, f ) == NULL )
250 i = sscanf( line, "%d", &maxval );
255 // determine if this is one or two bytes per pixel
256 bpp = maxval > 255 ? 2 : 1;
258 // allocate temporary storage for the raw data
259 data = mlt_pool_alloc( *width * *height * bpp );
264 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
267 // allocate the luma bitmap
268 *map = p = (uint16_t*)mlt_pool_alloc( *width * *height * sizeof( uint16_t ) );
272 // proces the raw data into the luma bitmap
273 for ( i = 0; i < *width * *height * bpp; i += bpp )
276 *p++ = data[ i ] << 8;
278 *p++ = ( data[ i ] << 8 ) + data[ i+1 ];
285 mlt_pool_release( data );
288 /** Generate a luma map from an RGB image.
291 static void luma_read_yuv422( uint8_t *image, uint16_t **map, int width, int height )
294 int size = width * height * 2;
296 // allocate the luma bitmap
297 uint16_t *p = *map = ( uint16_t* )mlt_pool_alloc( width * height * sizeof( uint16_t ) );
301 // proces the image data into the luma bitmap
302 for ( i = 0; i < size; i += 2 )
303 *p++ = ( image[ i ] - 16 ) * 299; // 299 = 65535 / 219
309 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
311 // Get the b frame from the stack
312 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
314 // Get the transition object
315 mlt_transition transition = mlt_frame_pop_service( a_frame );
317 // Get the properties of the transition
318 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
320 // Get the properties of the a frame
321 mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
323 // Get the properties of the b frame
324 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
326 // This compositer is yuv422 only
327 *format = mlt_image_yuv422;
329 mlt_service_lock( MLT_TRANSITION_SERVICE( transition ) );
331 // The cached luma map information
332 int luma_width = mlt_properties_get_int( properties, "width" );
333 int luma_height = mlt_properties_get_int( properties, "height" );
334 uint16_t *luma_bitmap = mlt_properties_get_data( properties, "bitmap", NULL );
335 char *current_resource = mlt_properties_get( properties, "_resource" );
337 // If the filename property changed, reload the map
338 char *resource = mlt_properties_get( properties, "resource" );
340 // Correct width/height if not specified
341 if ( luma_width == 0 || luma_height == 0 )
344 luma_height = *height;
347 if ( resource && ( !current_resource || strcmp( resource, current_resource ) ) )
350 char *extension = strrchr( resource, '.' );
351 char *orig_resource = resource;
353 if ( strchr( resource, '%' ) )
356 sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
357 test = fopen( temp, "r" );
359 strcat( temp, ".png" );
363 extension = strrchr( resource, '.' );
366 // See if it is a PGM
367 if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
370 FILE *f = fopen( resource, "r" );
374 luma_read_pgm( f, &luma_bitmap, &luma_width, &luma_height );
377 // Set the transition properties
378 mlt_properties_set_int( properties, "width", luma_width );
379 mlt_properties_set_int( properties, "height", luma_height );
380 mlt_properties_set( properties, "_resource", orig_resource );
381 mlt_properties_set_data( properties, "bitmap", luma_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
387 mlt_properties_set( properties, "_resource", NULL );
388 mlt_properties_set_data( properties, "bitmap", luma_bitmap, 0, mlt_pool_release, NULL );
392 // Get the factory producer service
393 char *factory = mlt_properties_get( properties, "factory" );
395 // Create the producer
396 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
397 mlt_producer producer = mlt_factory_producer( profile, factory, resource );
400 if ( producer != NULL )
402 // Get the producer properties
403 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
405 // Ensure that we loop
406 mlt_properties_set( producer_properties, "eof", "loop" );
408 // Now pass all producer. properties on the transition down
409 mlt_properties_pass( producer_properties, properties, "producer." );
411 // We will get the alpha frame from the producer
412 mlt_frame luma_frame = NULL;
414 // Get the luma frame
415 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 )
417 uint8_t *luma_image = NULL;
418 mlt_image_format luma_format = mlt_image_yuv422;
420 // Get image from the luma producer
421 mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "nearest" );
422 mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
424 // Generate the luma map
425 if ( luma_image != NULL )
426 luma_read_yuv422( luma_image, &luma_bitmap, luma_width, luma_height );
428 // Set the transition properties
429 mlt_properties_set_int( properties, "width", luma_width );
430 mlt_properties_set_int( properties, "height", luma_height );
431 mlt_properties_set( properties, "_resource", orig_resource);
432 mlt_properties_set_data( properties, "bitmap", luma_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
434 // Cleanup the luma frame
435 mlt_frame_close( luma_frame );
438 // Cleanup the luma producer
439 mlt_producer_close( producer );
444 // Arbitrary composite defaults
445 float mix = mlt_transition_get_progress( transition, a_frame );
446 float frame_delta = mlt_transition_get_progress_delta( transition, a_frame );
447 float luma_softness = mlt_properties_get_double( properties, "softness" );
449 mlt_properties_get_int( a_props, "consumer_deinterlace" ) ||
450 mlt_properties_get_int( properties, "progressive" ) ||
451 mlt_properties_get_int( b_props, "luma.progressive" );
452 int top_field_first = mlt_properties_get_int( b_props, "top_field_first" );
453 int reverse = mlt_properties_get_int( properties, "reverse" );
454 int invert = mlt_properties_get_int( properties, "invert" );
456 // Honour the reverse here
460 if ( mlt_properties_get( properties, "fixed" ) )
461 mix = mlt_properties_get_double( properties, "fixed" );
463 if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
465 reverse = invert ? !reverse : reverse;
466 mix = reverse ? 1 - mix : mix;
467 frame_delta *= reverse ? -1.0 : 1.0;
468 // Composite the frames using a luma map
469 luma_composite( !invert ? a_frame : b_frame, !invert ? b_frame : a_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
470 luma_softness, progressive ? -1 : top_field_first, width, height );
474 mix = ( reverse || invert ) ? 1 - mix : mix;
476 // Dissolve the frames using the time offset for mix value
477 dissolve_yuv( a_frame, b_frame, mix, *width, *height );
480 mlt_service_unlock( MLT_TRANSITION_SERVICE( transition ) );
482 // Extract the a_frame image info
483 *width = mlt_properties_get_int( !invert ? a_props : b_props, "width" );
484 *height = mlt_properties_get_int( !invert ? a_props : b_props, "height" );
485 *image = mlt_properties_get_data( !invert ? a_props : b_props, "image", NULL );
491 /** Luma transition processing.
494 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
496 // Push the transition on to the frame
497 mlt_frame_push_service( a_frame, transition );
499 // Push the b_frame on to the stack
500 mlt_frame_push_frame( a_frame, b_frame );
502 // Push the transition method
503 mlt_frame_push_get_image( a_frame, transition_get_image );
508 /** Constructor for the filter.
511 mlt_transition transition_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *lumafile )
513 mlt_transition transition = mlt_transition_new( );
514 if ( transition != NULL )
517 transition->process = transition_process;
520 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "factory", mlt_environment( "MLT_PRODUCER" ) );
522 // Set the main property
523 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "resource", lumafile );
525 // Inform apps and framework that this is a video only transition
526 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );