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 double smoothstep( double edge1
, double edge2
, double a
)
57 a
= ( a
- edge1
) / ( edge2
- edge1
);
59 return ( a
* a
* ( 3 - 2 * a
) );
64 \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
66 static void luma_composite( mlt_frame
this, mlt_frame b_frame
, int luma_width
, int luma_height
,
67 double *luma_bitmap
, double pos
, double frame_delta
, double softness
, int field_order
)
69 int width_src
, height_src
;
70 int width_dest
, height_dest
;
71 mlt_image_format format_src
, format_dest
;
72 uint8_t *p_src
, *p_dest
;
79 format_src
= mlt_image_yuv422
;
80 format_dest
= mlt_image_yuv422
;
82 mlt_frame_get_image( this, &p_dest
, &format_dest
, &width_dest
, &height_dest
, 1 /* writable */ );
83 mlt_frame_get_image( b_frame
, &p_src
, &format_src
, &width_src
, &height_src
, 0 /* writable */ );
85 stride_src
= width_src
* 2;
86 stride_dest
= width_dest
* 2;
88 // composite using luma map
89 for ( field
= 0; field
< ( field_order
< 0 ?
1 : 2 ); ++field
)
91 // Offset the position based on which field we're looking at ...
92 double field_pos
= pos
+ ( ( field_order
== 0 ?
1 - field
: field
) * frame_delta
* 0.5 );
94 // adjust the position for the softness level
95 field_pos
*= ( 1.0 + softness
);
97 for ( i
= field
; i
< height_src
; i
+= ( field_order
< 0 ?
1 : 2 ) )
99 uint8_t *p
= &p_src
[ i
* stride_src
];
100 uint8_t *q
= &p_dest
[ i
* stride_dest
];
101 uint8_t *o
= &p_dest
[ i
* stride_dest
];
102 double *l
= &luma_bitmap
[ i
* luma_width
];
104 for ( j
= 0; j
< width_src
; j
++ )
109 double value
= smoothstep( weight
, weight
+ softness
, field_pos
);
111 *o
++ = (uint8_t)( y
* value
+ *q
++ * ( 1 - value
) );
112 *o
++ = (uint8_t)( uv
* value
+ *q
++ * ( 1 - value
) );
121 static int transition_get_image( mlt_frame
this, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
123 // Get the properties of the a frame
124 mlt_properties a_props
= mlt_frame_properties( this );
126 // Get the b frame from the stack
127 mlt_frame b_frame
= mlt_frame_pop_frame( this );
129 // Get the properties of the b frame
130 mlt_properties b_props
= mlt_frame_properties( b_frame
);
132 // Arbitrary composite defaults
133 static double previous_mix
= 0;
137 double *luma_bitmap
= NULL
;
138 double luma_softness
= 0;
140 int top_field_first
= 0;
142 // mix is the offset time value in the duration of the transition
143 // - also used as the mixing level for a dissolve
144 if ( mlt_properties_get( b_props
, "mix" ) != NULL
)
145 mix
= mlt_properties_get_double( b_props
, "mix" );
147 // (mix - previous_mix) is the animation delta, if backwards reset previous
148 if ( mix
< previous_mix
)
151 // Get the interlace and field properties of the frame
152 if ( mlt_properties_get( b_props
, "progressive" ) != NULL
)
153 progressive
= mlt_properties_get_int( b_props
, "progressive" );
154 if ( mlt_properties_get( b_props
, "top_field_first" ) != NULL
)
155 top_field_first
= mlt_properties_get_int( b_props
, "top_field_first" );
157 // Get the luma map parameters
158 if ( mlt_properties_get( b_props
, "luma.width" ) != NULL
)
159 luma_width
= mlt_properties_get_int( b_props
, "luma.width" );
160 if ( mlt_properties_get( b_props
, "luma.height" ) != NULL
)
161 luma_height
= mlt_properties_get_int( b_props
, "luma.height" );
162 if ( mlt_properties_get( b_props
, "luma.softness" ) != NULL
)
163 luma_softness
= mlt_properties_get_double( b_props
, "luma.softness" );
164 luma_bitmap
= (double*) mlt_properties_get_data( b_props
, "luma.bitmap", NULL
);
166 if ( luma_width
> 0 && luma_height
> 0 && luma_bitmap
!= NULL
)
167 // Composite the frames using a luma map
168 luma_composite( this, b_frame
, luma_width
, luma_height
, luma_bitmap
, mix
, mix
- previous_mix
,
169 luma_softness
, progressive
> 0 ?
-1 : top_field_first
);
171 // Dissolve the frames using the time offset for mix value
172 mlt_frame_composite_yuv( this, b_frame
, 0, 0, mix
);
174 // Extract the a_frame image info
175 *width
= mlt_properties_get_int( a_props
, "width" );
176 *height
= mlt_properties_get_int( a_props
, "height" );
177 *image
= mlt_properties_get_data( a_props
, "image", NULL
);
180 mlt_frame_close( b_frame
);
187 static int transition_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
189 // Get the properties of the a frame
190 mlt_properties a_props
= mlt_frame_properties( frame
);
192 // Get the b frame from the stack
193 mlt_frame b_frame
= mlt_frame_pop_frame( frame
);
195 // Get the properties of the b frame
196 mlt_properties b_props
= mlt_frame_properties( b_frame
);
198 // Restore the original get_audio
199 frame
->get_audio
= mlt_properties_get_data( a_props
, "get_audio", NULL
);
202 if ( mlt_properties_get( b_props
, "mix" ) != NULL
)
203 mix
= mlt_properties_get_double( b_props
, "mix" );
204 mlt_frame_mix_audio( frame
, b_frame
, mix
, buffer
, format
, frequency
, channels
, samples
);
206 // Push the b_frame back on for get_image
207 mlt_frame_push_frame( frame
, b_frame
);
213 /** Load the luma map from PGM stream.
216 static void luma_read_pgm( FILE *f
, double **map
, int *width
, int *height
)
218 uint8_t *data
= NULL
;
229 // get the magic code
230 if ( fgets( line
, 127, f
) == NULL
)
232 if ( line
[0] != 'P' || line
[1] != '5' )
235 // skip white space and see if a new line must be fetched
236 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
237 if ( line
[i
] == '\0' && fgets( line
, 127, f
) == NULL
)
240 // get the dimensions
241 if ( line
[0] == 'P' )
242 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
244 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
246 // get the height value, if not yet
249 if ( fgets( line
, 127, f
) == NULL
)
251 i
= sscanf( line
, "%d", height
);
258 // get the maximum gray value, if not yet
261 if ( fgets( line
, 127, f
) == NULL
)
263 i
= sscanf( line
, "%d", &maxval
);
268 // determine if this is one or two bytes per pixel
269 bpp
= maxval
> 255 ?
2 : 1;
270 // allocate temporary storage for the raw data
271 data
= malloc( *width
* *height
* bpp
);
276 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
279 // allocate the luma bitmap
280 *map
= p
= (double*) malloc( *width
* *height
* sizeof( double ) );
284 // proces the raw data into the luma bitmap
285 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
288 *p
++ = (double) data
[ i
] / (double) maxval
;
290 *p
++ = (double) ( ( data
[ i
] << 8 ) + data
[ i
+1 ] ) / (double) maxval
;
301 /** Luma transition processing.
304 static mlt_frame
transition_process( mlt_transition transition
, mlt_frame a_frame
, mlt_frame b_frame
)
306 transition_luma
*this = (transition_luma
*) transition
->child
;
308 // Get the properties of the transition
309 mlt_properties properties
= mlt_transition_properties( transition
);
311 // Get the properties of the b frame
312 mlt_properties b_props
= mlt_frame_properties( b_frame
);
314 // If the filename property changed, reload the map
315 char *luma_file
= mlt_properties_get( properties
, "filename" );
316 if ( luma_file
!= NULL
&& luma_file
!= this->filename
)
318 int width
= mlt_properties_get_int( b_props
, "width" );
319 int height
= mlt_properties_get_int( b_props
, "height" );
323 command
[ 511 ] = '\0';
324 this->filename
= luma_file
;
325 snprintf( command
, 511, "anytopnm %s | pnmscale -width %d -height %d", luma_file
, width
, height
);
326 //pipe = popen( command, "r" );
327 pipe
= fopen( luma_file
, "r" );
330 luma_read_pgm( pipe
, &this->bitmap
, &this->width
, &this->height
);
337 // Determine the time position of this frame in the transition duration
338 mlt_timecode in
= mlt_transition_get_in( transition
);
339 mlt_timecode out
= mlt_transition_get_out( transition
);
340 mlt_timecode time
= mlt_frame_get_timecode( b_frame
);
341 double pos
= ( time
- in
) / ( out
- in
);
343 // Set the b frame properties
344 mlt_properties_set_double( b_props
, "mix", pos
);
345 mlt_properties_set_int( b_props
, "luma.width", this->width
);
346 mlt_properties_set_int( b_props
, "luma.height", this->height
);
347 mlt_properties_set_data( b_props
, "luma.bitmap", this->bitmap
, 0, NULL
, NULL
);
348 if ( mlt_properties_get( properties
, "softness" ) != NULL
)
349 mlt_properties_set_double( b_props
, "luma.softness", mlt_properties_get_double( properties
, "softness" ) );
351 mlt_frame_push_get_image( a_frame
, transition_get_image
);
352 mlt_frame_push_frame( a_frame
, b_frame
);
354 /************************ AUDIO ***************************/
355 // Backup the original get_audio (it's still needed)
356 mlt_properties_set_data( mlt_frame_properties( a_frame
), "get_audio", a_frame
->get_audio
, 0, NULL
, NULL
);
358 // Override the get_audio method
359 a_frame
->get_audio
= transition_get_audio
;
364 /** Constructor for the filter.
367 mlt_transition
transition_luma_init( char *lumafile
)
369 transition_luma
*this = calloc( sizeof( transition_luma
), 1 );
372 mlt_transition transition
= &this->parent
;
373 mlt_transition_init( transition
, this );
374 transition
->process
= transition_process
;
375 transition
->close
= transition_close
;
377 if ( lumafile
!= NULL
)
378 mlt_properties_set( mlt_transition_properties( transition
), "filename", lumafile
);
380 return &this->parent
;
385 /** Close the transition.
388 static void transition_close( mlt_transition parent
)
390 transition_luma
*this = (transition_luma
*) parent
->child
;
393 free( this->bitmap
);
395 parent
->close
= NULL
;
396 mlt_transition_close( parent
);