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 static int frame_composite_yuv( mlt_frame this, mlt_frame that, int x, int y, float weight, int *width, int *height )
65 int width_src = *width, height_src = *height;
66 int width_dest = *width, height_dest = *height;
67 mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
68 uint8_t *p_src, *p_dest;
72 int x_src = 0, y_src = 0;
74 // optimization point - no work to do
75 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
78 format_src = mlt_image_yuv422;
79 format_dest = mlt_image_yuv422;
81 mlt_frame_get_image( this, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
82 mlt_frame_get_image( that, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
84 stride_src = width_src * 2;
85 stride_dest = width_dest * 2;
87 // crop overlay off the left edge of frame
95 // crop overlay beyond right edge of frame
96 else if ( x + width_src > width_dest )
97 width_src = width_dest - x;
99 // crop overlay off the top edge of the frame
105 // crop overlay below bottom edge of frame
106 else if ( y + height_src > height_dest )
107 height_src = height_dest - y;
109 // offset pointer into overlay buffer based on cropping
110 p_src += x_src * 2 + y_src * stride_src;
112 // offset pointer into frame buffer based upon positive, even coordinates only!
113 p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
115 // Get the alpha channel of the overlay
116 uint8_t *p_alpha = mlt_frame_get_alpha_mask( that );
118 // offset pointer into alpha channel based upon cropping
120 p_alpha += x_src + y_src * stride_src / 2;
125 uint8_t *z = p_alpha;
132 // now do the compositing only to cropped extents
133 for ( i = 0; i < height_src; i++ )
140 for ( j = 0; j < width_src; j ++ )
144 a = ( z == NULL ) ? 255 : *z ++;
145 value = ( weight * ( float ) a / 255.0 );
146 *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
147 *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
151 p_dest += stride_dest;
153 p_alpha += stride_src / 2;
161 \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
163 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
164 float *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
165 int *width, int *height )
167 int width_src = *width, height_src = *height;
168 int width_dest = *width, height_dest = *height;
169 mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
170 uint8_t *p_src, *p_dest;
177 format_src = mlt_image_yuv422;
178 format_dest = mlt_image_yuv422;
180 mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
181 mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
183 stride_src = width_src * 2;
184 stride_dest = width_dest * 2;
186 // Offset the position based on which field we're looking at ...
187 float field_pos[ 2 ];
188 field_pos[ 0 ] = pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 );
189 field_pos[ 1 ] = pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 );
191 // adjust the position for the softness level
192 field_pos[ 0 ] *= ( 1.0 + softness );
193 field_pos[ 1 ] *= ( 1.0 + softness );
204 float x_diff = ( float )luma_width / ( float )*width;
205 float y_diff = ( float )luma_height / ( float )*height;
207 // composite using luma map
208 for ( field = 0; field < ( field_order < 0 ? 1 : 2 ); ++field )
210 for ( i = field; i < height_src; i += ( field_order < 0 ? 1 : 2 ) )
212 p = &p_src[ i * stride_src ];
213 q = &p_dest[ i * stride_dest ];
214 o = &p_dest[ i * stride_dest ];
215 l = &luma_bitmap[ ( int )( ( float )i * y_diff ) * luma_width ];
217 for ( j = 0; j < width_src; j ++ )
221 weight = l[ ( int )( ( float )j * x_diff ) ];
222 value = smoothstep( weight, weight + softness, field_pos[ field ] );
224 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
225 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
234 static int transition_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
236 // Get the properties of the a frame
237 mlt_properties a_props = mlt_frame_properties( this );
239 // Get the b frame from the stack
240 mlt_frame b_frame = mlt_frame_pop_frame( this );
242 // Get the properties of the b frame
243 mlt_properties b_props = mlt_frame_properties( b_frame );
245 // Arbitrary composite defaults
246 float frame_delta = 1 / mlt_properties_get_double( b_props, "fps" );
247 float mix = mlt_properties_get_double( b_props, "image.mix" );
248 int luma_width = mlt_properties_get_int( b_props, "luma.width" );
249 int luma_height = mlt_properties_get_int( b_props, "luma.height" );
250 float *luma_bitmap = mlt_properties_get_data( b_props, "luma.bitmap", NULL );
251 float luma_softness = mlt_properties_get_double( b_props, "luma.softness" );
252 int progressive = mlt_properties_get_int( b_props, "progressive" );
253 int top_field_first = mlt_properties_get_int( b_props, "top_field_first" );
254 int reverse = mlt_properties_get_int( b_props, "luma.reverse" );
256 // Honour the reverse here
257 mix = reverse ? 1 - mix : mix;
259 // Ensure we get scaling on the b_frame
260 mlt_properties_set( b_props, "rescale.interp", "nearest" );
262 if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
263 // Composite the frames using a luma map
264 luma_composite( this, b_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
265 luma_softness, progressive > 0 ? -1 : top_field_first, width, height );
267 // Dissolve the frames using the time offset for mix value
268 frame_composite_yuv( this, b_frame, 0, 0, mix, width, height );
270 // Extract the a_frame image info
271 *width = mlt_properties_get_int( a_props, "width" );
272 *height = mlt_properties_get_int( a_props, "height" );
273 *image = mlt_properties_get_data( a_props, "image", NULL );
278 /** Load the luma map from PGM stream.
281 static void luma_read_pgm( FILE *f, float **map, int *width, int *height )
283 uint8_t *data = NULL;
294 // get the magic code
295 if ( fgets( line, 127, f ) == NULL )
297 if ( line[0] != 'P' || line[1] != '5' )
300 // skip white space and see if a new line must be fetched
301 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
302 if ( line[i] == '\0' && fgets( line, 127, f ) == NULL )
305 // get the dimensions
306 if ( line[0] == 'P' )
307 i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
309 i = sscanf( line, "%d %d %d", width, height, &maxval );
311 // get the height value, if not yet
314 if ( fgets( line, 127, f ) == NULL )
316 i = sscanf( line, "%d", height );
323 // get the maximum gray value, if not yet
326 if ( fgets( line, 127, f ) == NULL )
328 i = sscanf( line, "%d", &maxval );
333 // determine if this is one or two bytes per pixel
334 bpp = maxval > 255 ? 2 : 1;
335 // allocate temporary storage for the raw data
336 data = malloc( *width * *height * bpp );
341 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
344 // allocate the luma bitmap
345 *map = p = (float*) malloc( *width * *height * sizeof( float ) );
349 // proces the raw data into the luma bitmap
350 for ( i = 0; i < *width * *height * bpp; i += bpp )
353 *p++ = (float) data[ i ] / (float) maxval;
355 *p++ = (float) ( ( data[ i ] << 8 ) + data[ i+1 ] ) / (float) maxval;
366 /** Luma transition processing.
369 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
371 transition_luma *this = (transition_luma*) transition->child;
373 // Get the properties of the transition
374 mlt_properties properties = mlt_transition_properties( transition );
376 // Get the properties of the b frame
377 mlt_properties b_props = mlt_frame_properties( b_frame );
379 // If the filename property changed, reload the map
380 char *luma_file = mlt_properties_get( properties, "resource" );
381 if ( luma_file != NULL && ( this->filename == NULL || ( this->filename && strcmp( luma_file, this->filename ) ) ) )
385 free( this->filename );
386 this->filename = strdup( luma_file );
387 pipe = fopen( luma_file, "r" );
390 free( this->bitmap );
391 luma_read_pgm( pipe, &this->bitmap, &this->width, &this->height );
396 // Determine the time position of this frame in the transition duration
397 mlt_position in = mlt_transition_get_in( transition );
398 mlt_position out = mlt_transition_get_out( transition );
399 mlt_position time = mlt_frame_get_position( b_frame );
400 float pos = ( float )( time - in ) / ( float )( out - in + 1 );
402 // Set the b frame properties
403 mlt_properties_set_double( b_props, "image.mix", pos );
404 mlt_properties_set_int( b_props, "luma.width", this->width );
405 mlt_properties_set_int( b_props, "luma.height", this->height );
406 mlt_properties_set_data( b_props, "luma.bitmap", this->bitmap, 0, NULL, NULL );
407 mlt_properties_set_int( b_props, "luma.reverse", mlt_properties_get_int( properties, "reverse" ) );
408 mlt_properties_set_double( b_props, "luma.softness", mlt_properties_get_double( properties, "softness" ) );
410 mlt_frame_push_get_image( a_frame, transition_get_image );
411 mlt_frame_push_frame( a_frame, b_frame );
416 /** Constructor for the filter.
419 mlt_transition transition_luma_init( char *lumafile )
421 transition_luma *this = calloc( sizeof( transition_luma ), 1 );
424 mlt_transition transition = &this->parent;
425 mlt_transition_init( transition, this );
426 transition->process = transition_process;
427 transition->close = transition_close;
428 mlt_properties_set( mlt_transition_properties( transition ), "resource", lumafile );
429 return &this->parent;
434 /** Close the transition.
437 static void transition_close( mlt_transition parent )
439 transition_luma *this = (transition_luma*) parent->child;
440 free( this->bitmap );
441 free( this->filename );