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 * 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.
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.
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.
21 #include "transition_luma.h"
22 #include <framework/mlt_frame.h>
34 struct mlt_transition_s parent;
43 // forward declarations
44 static void transition_close( mlt_transition parent );
47 // image processing functions
49 static inline float smoothstep( float edge1, float edge2, float a )
57 a = ( a - edge1 ) / ( edge2 - edge1 );
59 return ( a * a * ( 3 - 2 * a ) );
62 /** Calculate the position for this frame.
65 static float position_calculate( mlt_transition this, mlt_frame frame )
67 // Get the in and out position
68 mlt_position in = mlt_transition_get_in( this );
69 mlt_position out = mlt_transition_get_out( this );
71 // Get the position of the frame
72 mlt_position position = mlt_frame_get_position( frame );
75 return ( float )( position - in ) / ( float )( out - in + 1 );
78 /** Calculate the field delta for this frame - position between two frames.
81 static float delta_calculate( mlt_transition this, mlt_frame frame )
83 // Get the in and out position
84 mlt_position in = mlt_transition_get_in( this );
85 mlt_position out = mlt_transition_get_out( this );
87 // Get the position of the frame
88 mlt_position position = mlt_frame_get_position( frame );
91 float x = ( float )( position - in ) / ( float )( out - in + 1 );
93 float y = ( float )( position - in ) / ( float )( out - in + 1 );
95 return ( y - x ) / 2.0;
98 static inline int dissolve_yuv( mlt_frame this, mlt_frame that, float weight, int width, int height )
101 int width_src = width, height_src = height;
102 mlt_image_format format = mlt_image_yuv422;
103 uint8_t *p_src, *p_dest;
104 float weight_complement = 1 - weight;
108 mlt_frame_get_image( this, &p_dest, &format, &width, &height, 1 /* writable */ );
109 mlt_frame_get_image( that, &p_src, &format, &width_src, &height_src, 0 /* writable */ );
112 limit = p_dest + height_src * width_src * 2;
115 *p_dest++ = ( uint8_t )( *p_src++ * weight + *p++ * weight_complement );
122 \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
124 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
125 float *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
126 int *width, int *height )
128 int width_src = *width, height_src = *height;
129 int width_dest = *width, height_dest = *height;
130 mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
131 uint8_t *p_src, *p_dest;
138 format_src = mlt_image_yuv422;
139 format_dest = mlt_image_yuv422;
141 mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
142 mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
144 stride_src = width_src * 2;
145 stride_dest = width_dest * 2;
147 // Offset the position based on which field we're looking at ...
148 float field_pos[ 2 ];
149 field_pos[ 0 ] = pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 );
150 field_pos[ 1 ] = pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 );
152 // adjust the position for the softness level
153 field_pos[ 0 ] *= ( 1.0 + softness );
154 field_pos[ 1 ] *= ( 1.0 + softness );
165 float x_diff = ( float )luma_width / ( float )*width;
166 float y_diff = ( float )luma_height / ( float )*height;
168 // composite using luma map
169 for ( field = 0; field < ( field_order < 0 ? 1 : 2 ); ++field )
171 for ( i = field; i < height_src; i += ( field_order < 0 ? 1 : 2 ) )
173 p = &p_src[ i * stride_src ];
174 q = &p_dest[ i * stride_dest ];
175 o = &p_dest[ i * stride_dest ];
176 l = &luma_bitmap[ ( int )( ( float )i * y_diff ) * luma_width ];
178 for ( j = 0; j < width_src; j ++ )
182 weight = l[ ( int )( ( float )j * x_diff ) ];
183 value = smoothstep( weight, weight + softness, field_pos[ field ] );
185 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
186 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
195 static int transition_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
197 // Get the properties of the a frame
198 mlt_properties a_props = mlt_frame_properties( this );
200 // Get the b frame from the stack
201 mlt_frame b_frame = mlt_frame_pop_frame( this );
203 // Get the properties of the b frame
204 mlt_properties b_props = mlt_frame_properties( b_frame );
206 // Arbitrary composite defaults
207 float mix = mlt_properties_get_double( b_props, "image.mix" );
208 float frame_delta = mlt_properties_get_double( b_props, "luma.delta" );
209 int luma_width = mlt_properties_get_int( b_props, "luma.width" );
210 int luma_height = mlt_properties_get_int( b_props, "luma.height" );
211 float *luma_bitmap = mlt_properties_get_data( b_props, "luma.bitmap", NULL );
212 float luma_softness = mlt_properties_get_double( b_props, "luma.softness" );
213 int progressive = mlt_properties_get_int( b_props, "progressive" ) ||
214 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
215 mlt_properties_get_int( b_props, "luma.progressive" );
217 int top_field_first = mlt_properties_get_int( b_props, "top_field_first" );
218 int reverse = mlt_properties_get_int( b_props, "luma.reverse" );
220 // Since we are the consumer of the b_frame, we must pass along this
221 // consumer property from the a_frame
222 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
223 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
225 // Honour the reverse here
226 mix = reverse ? 1 - mix : mix;
227 frame_delta *= reverse ? -1.0 : 1.0;
229 // Ensure we get scaling on the b_frame
230 mlt_properties_set( b_props, "rescale.interp", "nearest" );
232 if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
233 // Composite the frames using a luma map
234 luma_composite( this, b_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
235 luma_softness, progressive ? -1 : top_field_first, width, height );
237 // Dissolve the frames using the time offset for mix value
238 dissolve_yuv( this, b_frame, mix, *width, *height );
240 // Extract the a_frame image info
241 *width = mlt_properties_get_int( a_props, "width" );
242 *height = mlt_properties_get_int( a_props, "height" );
243 *image = mlt_properties_get_data( a_props, "image", NULL );
248 /** Load the luma map from PGM stream.
251 static void luma_read_pgm( FILE *f, float **map, int *width, int *height )
253 void *release = NULL;
254 uint8_t *data = NULL;
265 // get the magic code
266 if ( fgets( line, 127, f ) == NULL )
268 if ( line[0] != 'P' || line[1] != '5' )
271 // skip white space and see if a new line must be fetched
272 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
273 if ( line[i] == '\0' && fgets( line, 127, f ) == NULL )
276 // get the dimensions
277 if ( line[0] == 'P' )
278 i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
280 i = sscanf( line, "%d %d %d", width, height, &maxval );
282 // get the height value, if not yet
285 if ( fgets( line, 127, f ) == NULL )
287 i = sscanf( line, "%d", height );
294 // get the maximum gray value, if not yet
297 if ( fgets( line, 127, f ) == NULL )
299 i = sscanf( line, "%d", &maxval );
304 // determine if this is one or two bytes per pixel
305 bpp = maxval > 255 ? 2 : 1;
307 // allocate temporary storage for the raw data
308 data = mlt_pool_allocate( *width * *height * bpp, &release );
313 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
316 // allocate the luma bitmap
318 // Difficult here - need to change the function prototype....
319 *map = p = (float*) malloc( *width * *height * sizeof( float ) );
323 // proces the raw data into the luma bitmap
324 for ( i = 0; i < *width * *height * bpp; i += bpp )
327 *p++ = (float) data[ i ] / (float) maxval;
329 *p++ = (float) ( ( data[ i ] << 8 ) + data[ i+1 ] ) / (float) maxval;
335 if ( release != NULL )
336 mlt_pool_release( release );
340 /** Luma transition processing.
343 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
345 transition_luma *this = (transition_luma*) transition->child;
347 // Get the properties of the transition
348 mlt_properties properties = mlt_transition_properties( transition );
350 // Get the properties of the b frame
351 mlt_properties b_props = mlt_frame_properties( b_frame );
353 // If the filename property changed, reload the map
354 char *luma_file = mlt_properties_get( properties, "resource" );
355 if ( luma_file != NULL && ( this->filename == NULL || ( this->filename && strcmp( luma_file, this->filename ) ) ) )
359 free( this->filename );
360 this->filename = strdup( luma_file );
361 pipe = fopen( luma_file, "r" );
364 free( this->bitmap );
365 luma_read_pgm( pipe, &this->bitmap, &this->width, &this->height );
370 // Set the b frame properties
371 mlt_properties_set_double( b_props, "image.mix", position_calculate( transition, b_frame ) );
372 mlt_properties_set_double( b_props, "luma.delta", delta_calculate( transition, b_frame ) );
373 mlt_properties_set_int( b_props, "luma.width", this->width );
374 mlt_properties_set_int( b_props, "luma.height", this->height );
375 mlt_properties_set_data( b_props, "luma.bitmap", this->bitmap, 0, NULL, NULL );
376 mlt_properties_set_int( b_props, "luma.reverse", mlt_properties_get_int( properties, "reverse" ) );
377 mlt_properties_set_double( b_props, "luma.softness", mlt_properties_get_double( properties, "softness" ) );
379 mlt_frame_push_get_image( a_frame, transition_get_image );
380 mlt_frame_push_frame( a_frame, b_frame );
385 /** Constructor for the filter.
388 mlt_transition transition_luma_init( char *lumafile )
390 transition_luma *this = calloc( sizeof( transition_luma ), 1 );
393 mlt_transition transition = &this->parent;
394 mlt_transition_init( transition, this );
395 transition->process = transition_process;
396 transition->close = transition_close;
397 mlt_properties_set( mlt_transition_properties( transition ), "resource", lumafile );
398 return &this->parent;
403 /** Close the transition.
406 static void transition_close( mlt_transition parent )
408 transition_luma *this = (transition_luma*) parent->child;
409 free( this->bitmap );
410 free( this->filename );