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 // 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",
259 mlt_properties_get_double( mlt_frame_properties( this ), "consumer_aspect_ratio" ) );
260 mlt_properties_set_double( b_props
, "consumer_scale",
261 mlt_properties_get_double( mlt_frame_properties( this ), "consumer_scale" ) );
263 // Honour the reverse here
264 mix
= reverse ?
1 - mix
: mix
;
266 // Ensure we get scaling on the b_frame
267 mlt_properties_set( b_props
, "rescale.interp", "nearest" );
269 if ( luma_width
> 0 && luma_height
> 0 && luma_bitmap
!= NULL
)
270 // Composite the frames using a luma map
271 luma_composite( this, b_frame
, luma_width
, luma_height
, luma_bitmap
, mix
, frame_delta
,
272 luma_softness
, progressive
> 0 ?
-1 : top_field_first
, width
, height
);
274 // Dissolve the frames using the time offset for mix value
275 frame_composite_yuv( this, b_frame
, 0, 0, mix
, width
, height
);
277 // Extract the a_frame image info
278 *width
= mlt_properties_get_int( a_props
, "width" );
279 *height
= mlt_properties_get_int( a_props
, "height" );
280 *image
= mlt_properties_get_data( a_props
, "image", NULL
);
285 /** Load the luma map from PGM stream.
288 static void luma_read_pgm( FILE *f
, float **map
, int *width
, int *height
)
290 uint8_t *data
= NULL
;
301 // get the magic code
302 if ( fgets( line
, 127, f
) == NULL
)
304 if ( line
[0] != 'P' || line
[1] != '5' )
307 // skip white space and see if a new line must be fetched
308 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
309 if ( line
[i
] == '\0' && fgets( line
, 127, f
) == NULL
)
312 // get the dimensions
313 if ( line
[0] == 'P' )
314 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
316 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
318 // get the height value, if not yet
321 if ( fgets( line
, 127, f
) == NULL
)
323 i
= sscanf( line
, "%d", height
);
330 // get the maximum gray value, if not yet
333 if ( fgets( line
, 127, f
) == NULL
)
335 i
= sscanf( line
, "%d", &maxval
);
340 // determine if this is one or two bytes per pixel
341 bpp
= maxval
> 255 ?
2 : 1;
343 // allocate temporary storage for the raw data
345 data
= malloc( *width
* *height
* bpp
);
350 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
353 // allocate the luma bitmap
355 *map
= p
= (float*) malloc( *width
* *height
* sizeof( float ) );
359 // proces the raw data into the luma bitmap
360 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
363 *p
++ = (float) data
[ i
] / (float) maxval
;
365 *p
++ = (float) ( ( data
[ i
] << 8 ) + data
[ i
+1 ] ) / (float) maxval
;
376 /** Luma transition processing.
379 static mlt_frame
transition_process( mlt_transition transition
, mlt_frame a_frame
, mlt_frame b_frame
)
381 transition_luma
*this = (transition_luma
*) transition
->child
;
383 // Get the properties of the transition
384 mlt_properties properties
= mlt_transition_properties( transition
);
386 // Get the properties of the b frame
387 mlt_properties b_props
= mlt_frame_properties( b_frame
);
389 // If the filename property changed, reload the map
390 char *luma_file
= mlt_properties_get( properties
, "resource" );
391 if ( luma_file
!= NULL
&& ( this->filename
== NULL
|| ( this->filename
&& strcmp( luma_file
, this->filename
) ) ) )
395 free( this->filename
);
396 this->filename
= strdup( luma_file
);
397 pipe
= fopen( luma_file
, "r" );
400 free( this->bitmap
);
401 luma_read_pgm( pipe
, &this->bitmap
, &this->width
, &this->height
);
406 // Determine the time position of this frame in the transition duration
407 mlt_position in
= mlt_transition_get_in( transition
);
408 mlt_position out
= mlt_transition_get_out( transition
);
409 mlt_position time
= mlt_frame_get_position( b_frame
);
410 float pos
= ( float )( time
- in
) / ( float )( out
- in
+ 1 );
412 // Set the b frame properties
413 mlt_properties_set_double( b_props
, "image.mix", pos
);
414 mlt_properties_set_int( b_props
, "luma.width", this->width
);
415 mlt_properties_set_int( b_props
, "luma.height", this->height
);
416 mlt_properties_set_data( b_props
, "luma.bitmap", this->bitmap
, 0, NULL
, NULL
);
417 mlt_properties_set_int( b_props
, "luma.reverse", mlt_properties_get_int( properties
, "reverse" ) );
418 mlt_properties_set_double( b_props
, "luma.softness", mlt_properties_get_double( properties
, "softness" ) );
420 mlt_frame_push_get_image( a_frame
, transition_get_image
);
421 mlt_frame_push_frame( a_frame
, b_frame
);
426 /** Constructor for the filter.
429 mlt_transition
transition_luma_init( char *lumafile
)
431 transition_luma
*this = calloc( sizeof( transition_luma
), 1 );
434 mlt_transition transition
= &this->parent
;
435 mlt_transition_init( transition
, this );
436 transition
->process
= transition_process
;
437 transition
->close
= transition_close
;
438 mlt_properties_set( mlt_transition_properties( transition
), "resource", lumafile
);
439 return &this->parent
;
444 /** Close the transition.
447 static void transition_close( mlt_transition parent
)
449 transition_luma
*this = (transition_luma
*) parent
->child
;
450 free( this->bitmap
);
451 free( this->filename
);