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" ) || mlt_properties_get_int( a_props
, "consumer_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 // Since we are the consumer of the b_frame, we must pass along this
257 // consumer property from the a_frame
258 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
259 mlt_properties_set_double( b_props
, "consumer_scale", mlt_properties_get_double( a_props
, "consumer_scale" ) );
261 // Honour the reverse here
262 mix
= reverse ?
1 - mix
: mix
;
264 // Ensure we get scaling on the b_frame
265 mlt_properties_set( b_props
, "rescale.interp", "nearest" );
267 if ( luma_width
> 0 && luma_height
> 0 && luma_bitmap
!= NULL
)
268 // Composite the frames using a luma map
269 luma_composite( this, b_frame
, luma_width
, luma_height
, luma_bitmap
, mix
, frame_delta
,
270 luma_softness
, progressive ?
-1 : top_field_first
, width
, height
);
272 // Dissolve the frames using the time offset for mix value
273 frame_composite_yuv( this, b_frame
, 0, 0, mix
, width
, height
);
275 // Extract the a_frame image info
276 *width
= mlt_properties_get_int( a_props
, "width" );
277 *height
= mlt_properties_get_int( a_props
, "height" );
278 *image
= mlt_properties_get_data( a_props
, "image", NULL
);
283 /** Load the luma map from PGM stream.
286 static void luma_read_pgm( FILE *f
, float **map
, int *width
, int *height
)
288 uint8_t *data
= NULL
;
299 // get the magic code
300 if ( fgets( line
, 127, f
) == NULL
)
302 if ( line
[0] != 'P' || line
[1] != '5' )
305 // skip white space and see if a new line must be fetched
306 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
307 if ( line
[i
] == '\0' && fgets( line
, 127, f
) == NULL
)
310 // get the dimensions
311 if ( line
[0] == 'P' )
312 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
314 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
316 // get the height value, if not yet
319 if ( fgets( line
, 127, f
) == NULL
)
321 i
= sscanf( line
, "%d", height
);
328 // get the maximum gray value, if not yet
331 if ( fgets( line
, 127, f
) == NULL
)
333 i
= sscanf( line
, "%d", &maxval
);
338 // determine if this is one or two bytes per pixel
339 bpp
= maxval
> 255 ?
2 : 1;
341 // allocate temporary storage for the raw data
343 data
= malloc( *width
* *height
* bpp
);
348 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
351 // allocate the luma bitmap
353 *map
= p
= (float*) malloc( *width
* *height
* sizeof( float ) );
357 // proces the raw data into the luma bitmap
358 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
361 *p
++ = (float) data
[ i
] / (float) maxval
;
363 *p
++ = (float) ( ( data
[ i
] << 8 ) + data
[ i
+1 ] ) / (float) maxval
;
374 /** Luma transition processing.
377 static mlt_frame
transition_process( mlt_transition transition
, mlt_frame a_frame
, mlt_frame b_frame
)
379 transition_luma
*this = (transition_luma
*) transition
->child
;
381 // Get the properties of the transition
382 mlt_properties properties
= mlt_transition_properties( transition
);
384 // Get the properties of the b frame
385 mlt_properties b_props
= mlt_frame_properties( b_frame
);
387 // If the filename property changed, reload the map
388 char *luma_file
= mlt_properties_get( properties
, "resource" );
389 if ( luma_file
!= NULL
&& ( this->filename
== NULL
|| ( this->filename
&& strcmp( luma_file
, this->filename
) ) ) )
393 free( this->filename
);
394 this->filename
= strdup( luma_file
);
395 pipe
= fopen( luma_file
, "r" );
398 free( this->bitmap
);
399 luma_read_pgm( pipe
, &this->bitmap
, &this->width
, &this->height
);
404 // Determine the time position of this frame in the transition duration
405 mlt_position in
= mlt_transition_get_in( transition
);
406 mlt_position out
= mlt_transition_get_out( transition
);
407 mlt_position time
= mlt_frame_get_position( b_frame
);
408 float pos
= ( float )( time
- in
) / ( float )( out
- in
+ 1 );
410 // Set the b frame properties
411 mlt_properties_set_double( b_props
, "image.mix", pos
);
412 mlt_properties_set_int( b_props
, "luma.width", this->width
);
413 mlt_properties_set_int( b_props
, "luma.height", this->height
);
414 mlt_properties_set_data( b_props
, "luma.bitmap", this->bitmap
, 0, NULL
, NULL
);
415 mlt_properties_set_int( b_props
, "luma.reverse", mlt_properties_get_int( properties
, "reverse" ) );
416 mlt_properties_set_double( b_props
, "luma.softness", mlt_properties_get_double( properties
, "softness" ) );
418 mlt_frame_push_get_image( a_frame
, transition_get_image
);
419 mlt_frame_push_frame( a_frame
, b_frame
);
424 /** Constructor for the filter.
427 mlt_transition
transition_luma_init( char *lumafile
)
429 transition_luma
*this = calloc( sizeof( transition_luma
), 1 );
432 mlt_transition transition
= &this->parent
;
433 mlt_transition_init( transition
, this );
434 transition
->process
= transition_process
;
435 transition
->close
= transition_close
;
436 mlt_properties_set( mlt_transition_properties( transition
), "resource", lumafile
);
437 return &this->parent
;
442 /** Close the transition.
445 static void transition_close( mlt_transition parent
)
447 transition_luma
*this = (transition_luma
*) parent
->child
;
448 free( this->bitmap
);
449 free( this->filename
);