]> git.sesse.net Git - mlt/blob - src/modules/core/transition_luma.c
Refactor to mlt_transition_get_progress_delta().
[mlt] / src / modules / core / transition_luma.c
1 /*
2  * transition_luma.c -- a generic dissolve/wipe processor
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * Adapted from Kino Plugin Timfx, which is
7  * Copyright (C) 2002 Timothy M. Shead <tshead@k-3d.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <framework/mlt.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <math.h>
31
32 static inline int dissolve_yuv( mlt_frame this, mlt_frame that, float weight, int width, int height )
33 {
34         int ret = 0;
35         int width_src = width, height_src = height;
36         mlt_image_format format = mlt_image_yuv422;
37         uint8_t *p_src, *p_dest;
38         uint8_t *p, *q;
39         uint8_t *limit;
40         uint8_t *alpha_src;
41         uint8_t *alpha_dst;
42
43         int32_t weigh = weight * ( 1 << 16 );
44         int32_t weigh_complement = ( 1 - weight ) * ( 1 << 16 );
45
46         if ( mlt_properties_get( &this->parent, "distort" ) )
47                 mlt_properties_set( &that->parent, "distort", mlt_properties_get( &this->parent, "distort" ) );
48         mlt_properties_set_int( &that->parent, "consumer_deinterlace", mlt_properties_get_int( &this->parent, "consumer_deinterlace" ) );
49         mlt_frame_get_image( this, &p_dest, &format, &width, &height, 1 );
50         alpha_dst = mlt_frame_get_alpha_mask( this );
51         mlt_frame_get_image( that, &p_src, &format, &width_src, &height_src, 0 );
52         alpha_src = mlt_frame_get_alpha_mask( that );
53
54         // Pick the lesser of two evils ;-)
55         width_src = width_src > width ? width : width_src;
56         height_src = height_src > height ? height : height_src;
57         
58         p = p_dest;
59         q = alpha_dst;
60         limit = p_dest + height_src * width_src * 2;
61
62         while ( p < limit )
63         {
64                 *p_dest++ = ( *p_src++ * weigh + *p++ * weigh_complement ) >> 16;
65                 *p_dest++ = ( *p_src++ * weigh + *p++ * weigh_complement ) >> 16;
66                 *alpha_dst++ = ( *alpha_src++ * weigh + *q++ * weigh_complement ) >> 16;
67         }
68
69         return ret;
70 }
71
72 // image processing functions
73
74 static inline int32_t smoothstep( int32_t edge1, int32_t edge2, uint32_t a )
75 {
76         if ( a < edge1 )
77                 return 0;
78
79         if ( a >= edge2 )
80                 return 0x10000;
81
82         a = ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
83
84         return ( ( ( a * a ) >> 16 )  * ( ( 3 << 16 ) - ( 2 * a ) ) ) >> 16;
85 }
86
87 /** powerful stuff
88
89     \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
90 */
91 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
92                                                         uint16_t *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
93                                                         int *width, int *height )
94 {
95         int width_src = *width, height_src = *height;
96         int width_dest = *width, height_dest = *height;
97         mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
98         uint8_t *p_src, *p_dest;
99         int i, j;
100         int stride_src;
101         int stride_dest;
102         uint16_t weight = 0;
103
104         if ( mlt_properties_get( &a_frame->parent, "distort" ) )
105                 mlt_properties_set( &b_frame->parent, "distort", mlt_properties_get( &a_frame->parent, "distort" ) );
106         mlt_properties_set_int( &b_frame->parent, "consumer_deinterlace", mlt_properties_get_int( &a_frame->parent, "consumer_deinterlace" ) );
107         mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 );
108         mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 );
109
110         if ( *width == 0 || *height == 0 )
111                 return;
112
113         // Pick the lesser of two evils ;-)
114         width_src = width_src > width_dest ? width_dest : width_src;
115         height_src = height_src > height_dest ? height_dest : height_src;
116         
117         stride_src = width_src * 2;
118         stride_dest = width_dest * 2;
119
120         // Offset the position based on which field we're looking at ...
121         int32_t field_pos[ 2 ];
122         field_pos[ 0 ] = ( pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
123         field_pos[ 1 ] = ( pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
124
125         register uint8_t *p;
126         register uint8_t *q;
127         register uint8_t *o;
128         uint16_t  *l;
129
130         uint32_t value;
131
132         int32_t x_diff = ( luma_width << 16 ) / *width;
133         int32_t y_diff = ( luma_height << 16 ) / *height;
134         int32_t x_offset = 0;
135         int32_t y_offset = 0;
136         uint8_t *p_row;
137         uint8_t *q_row;
138
139         int32_t i_softness = softness * ( 1 << 16 );
140
141         int field_count = field_order < 0 ? 1 : 2;
142         int field_stride_src = field_count * stride_src;
143         int field_stride_dest = field_count * stride_dest;
144         int field = 0;
145
146         // composite using luma map
147         while ( field < field_count )
148         {
149                 p_row = p_src + field * stride_src;
150                 q_row = p_dest + field * stride_dest;
151                 y_offset = field << 16;
152                 i = field;
153
154                 while ( i < height_src )
155                 {
156                         p = p_row;
157                         q = q_row;
158                         o = q;
159                         l = luma_bitmap + ( y_offset >> 16 ) * ( luma_width * field_count );
160                         x_offset = 0;
161                         j = width_src;
162
163                         while( j -- )
164                         {
165                 weight = l[ x_offset >> 16 ];
166                                 value = smoothstep( weight, i_softness + weight, field_pos[ field ] );
167                                 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
168                                 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
169                                 x_offset += x_diff;
170                         }
171
172                         y_offset += y_diff;
173                         i += field_count;
174                         p_row += field_stride_src;
175                         q_row += field_stride_dest;
176                 }
177
178                 field ++;
179         }
180 }
181
182 /** Load the luma map from PGM stream.
183 */
184
185 static void luma_read_pgm( FILE *f, uint16_t **map, int *width, int *height )
186 {
187         uint8_t *data = NULL;
188         while (1)
189         {
190                 char line[128];
191                 char comment[128];
192                 int i = 2;
193                 int maxval;
194                 int bpp;
195                 uint16_t *p;
196
197                 line[127] = '\0';
198
199                 // get the magic code
200                 if ( fgets( line, 127, f ) == NULL )
201                         break;
202
203                 // skip comments
204                 while ( sscanf( line, " #%s", comment ) > 0 )
205                         if ( fgets( line, 127, f ) == NULL )
206                                 break;
207
208                 if ( line[0] != 'P' || line[1] != '5' )
209                         break;
210
211                 // skip white space and see if a new line must be fetched
212                 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
213                 if ( ( line[i] == '\0' || line[i] == '#' ) && fgets( line, 127, f ) == NULL )
214                         break;
215
216                 // skip comments
217                 while ( sscanf( line, " #%s", comment ) > 0 )
218                         if ( fgets( line, 127, f ) == NULL )
219                                 break;
220
221                 // get the dimensions
222                 if ( line[0] == 'P' )
223                         i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
224                 else
225                         i = sscanf( line, "%d %d %d", width, height, &maxval );
226
227                 // get the height value, if not yet
228                 if ( i < 2 )
229                 {
230                         if ( fgets( line, 127, f ) == NULL )
231                                 break;
232
233                         // skip comments
234                         while ( sscanf( line, " #%s", comment ) > 0 )
235                                 if ( fgets( line, 127, f ) == NULL )
236                                         break;
237
238                         i = sscanf( line, "%d", height );
239                         if ( i == 0 )
240                                 break;
241                         else
242                                 i = 2;
243                 }
244
245                 // get the maximum gray value, if not yet
246                 if ( i < 3 )
247                 {
248                         if ( fgets( line, 127, f ) == NULL )
249                                 break;
250
251                         // skip comments
252                         while ( sscanf( line, " #%s", comment ) > 0 )
253                                 if ( fgets( line, 127, f ) == NULL )
254                                         break;
255
256                         i = sscanf( line, "%d", &maxval );
257                         if ( i == 0 )
258                                 break;
259                 }
260
261                 // determine if this is one or two bytes per pixel
262                 bpp = maxval > 255 ? 2 : 1;
263
264                 // allocate temporary storage for the raw data
265                 data = mlt_pool_alloc( *width * *height * bpp );
266                 if ( data == NULL )
267                         break;
268
269                 // read the raw data
270                 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
271                         break;
272
273                 // allocate the luma bitmap
274                 *map = p = (uint16_t*)mlt_pool_alloc( *width * *height * sizeof( uint16_t ) );
275                 if ( *map == NULL )
276                         break;
277
278                 // proces the raw data into the luma bitmap
279                 for ( i = 0; i < *width * *height * bpp; i += bpp )
280                 {
281                         if ( bpp == 1 )
282                                 *p++ = data[ i ] << 8;
283                         else
284                                 *p++ = ( data[ i ] << 8 ) + data[ i+1 ];
285                 }
286
287                 break;
288         }
289
290         if ( data != NULL )
291                 mlt_pool_release( data );
292 }
293
294 /** Generate a luma map from an RGB image.
295 */
296
297 static void luma_read_yuv422( uint8_t *image, uint16_t **map, int width, int height )
298 {
299         int i;
300         int size = width * height * 2;
301         
302         // allocate the luma bitmap
303         uint16_t *p = *map = ( uint16_t* )mlt_pool_alloc( width * height * sizeof( uint16_t ) );
304         if ( *map == NULL )
305                 return;
306
307         // proces the image data into the luma bitmap
308         for ( i = 0; i < size; i += 2 )
309                 *p++ = ( image[ i ] - 16 ) * 299; // 299 = 65535 / 219
310 }
311
312 /** Get the image.
313 */
314
315 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
316 {
317         // Get the b frame from the stack
318         mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
319
320         // Get the transition object
321         mlt_transition transition = mlt_frame_pop_service( a_frame );
322
323         // Get the properties of the transition
324         mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
325
326         // Get the properties of the a frame
327         mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
328
329         // Get the properties of the b frame
330         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
331
332         // This compositer is yuv422 only
333         *format = mlt_image_yuv422;
334
335         mlt_service_lock( MLT_TRANSITION_SERVICE( transition ) );
336
337         // The cached luma map information
338         int luma_width = mlt_properties_get_int( properties, "width" );
339         int luma_height = mlt_properties_get_int( properties, "height" );
340         uint16_t *luma_bitmap = mlt_properties_get_data( properties, "bitmap", NULL );
341         char *current_resource = mlt_properties_get( properties, "_resource" );
342         
343         // If the filename property changed, reload the map
344         char *resource = mlt_properties_get( properties, "resource" );
345
346         // Correct width/height if not specified
347         if ( luma_width == 0 || luma_height == 0 )
348         {
349                 luma_width = *width;
350                 luma_height = *height;
351         }
352                 
353         if ( resource && ( !current_resource || strcmp( resource, current_resource ) ) )
354         {
355                 char temp[ 512 ];
356                 char *extension = strrchr( resource, '.' );
357                 char *orig_resource = resource;
358
359                 if ( strchr( resource, '%' ) )
360                 {
361                         FILE *test;
362                         sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
363                         test = fopen( temp, "r" );
364                         if ( test == NULL )
365                                 strcat( temp, ".png" );
366                         else
367                                 fclose( test ); 
368                         resource = temp;
369                         extension = strrchr( resource, '.' );
370                 }
371
372                 // See if it is a PGM
373                 if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
374                 {
375                         // Open PGM
376                         FILE *f = fopen( resource, "r" );
377                         if ( f != NULL )
378                         {
379                                 // Load from PGM
380                                 luma_read_pgm( f, &luma_bitmap, &luma_width, &luma_height );
381                                 fclose( f );
382
383                                 // Set the transition properties
384                                 mlt_properties_set_int( properties, "width", luma_width );
385                                 mlt_properties_set_int( properties, "height", luma_height );
386                                 mlt_properties_set( properties, "_resource", orig_resource );
387                                 mlt_properties_set_data( properties, "bitmap", luma_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
388                         }
389                 }
390                 else if (!*resource) 
391                 {
392                     luma_bitmap = NULL;
393                     mlt_properties_set( properties, "_resource", NULL );
394                     mlt_properties_set_data( properties, "bitmap", luma_bitmap, 0, mlt_pool_release, NULL );
395                 }
396                 else
397                 {
398                         // Get the factory producer service
399                         char *factory = mlt_properties_get( properties, "factory" );
400
401                         // Create the producer
402                         mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
403                         mlt_producer producer = mlt_factory_producer( profile, factory, resource );
404
405                         // If we have one
406                         if ( producer != NULL )
407                         {
408                                 // Get the producer properties
409                                 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
410
411                                 // Ensure that we loop
412                                 mlt_properties_set( producer_properties, "eof", "loop" );
413
414                                 // Now pass all producer. properties on the transition down
415                                 mlt_properties_pass( producer_properties, properties, "producer." );
416
417                                 // We will get the alpha frame from the producer
418                                 mlt_frame luma_frame = NULL;
419
420                                 // Get the luma frame
421                                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 )
422                                 {
423                                         uint8_t *luma_image = NULL;
424                                         mlt_image_format luma_format = mlt_image_yuv422;
425
426                                         // Get image from the luma producer
427                                         mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "nearest" );
428                                         mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
429
430                                         // Generate the luma map
431                                         if ( luma_image != NULL )
432                                                 luma_read_yuv422( luma_image, &luma_bitmap, luma_width, luma_height );
433                                         
434                                         // Set the transition properties
435                                         mlt_properties_set_int( properties, "width", luma_width );
436                                         mlt_properties_set_int( properties, "height", luma_height );
437                                         mlt_properties_set( properties, "_resource", orig_resource);
438                                         mlt_properties_set_data( properties, "bitmap", luma_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
439
440                                         // Cleanup the luma frame
441                                         mlt_frame_close( luma_frame );
442                                 }
443
444                                 // Cleanup the luma producer
445                                 mlt_producer_close( producer );
446                         }
447                 }
448         }
449
450         // Arbitrary composite defaults
451         float mix = mlt_transition_get_progress( transition, a_frame );
452         float frame_delta = mlt_transition_get_progress_delta( transition, a_frame );
453         float luma_softness = mlt_properties_get_double( properties, "softness" );
454         int progressive = 
455                         mlt_properties_get_int( a_props, "consumer_deinterlace" ) ||
456                         mlt_properties_get_int( properties, "progressive" ) ||
457                         mlt_properties_get_int( b_props, "luma.progressive" );
458         int top_field_first =  mlt_properties_get_int( b_props, "top_field_first" );
459         int reverse = mlt_properties_get_int( properties, "reverse" );
460         int invert = mlt_properties_get_int( properties, "invert" );
461
462         if ( mlt_properties_get( a_props, "rescale.interp" ) == NULL || !strcmp( mlt_properties_get( a_props, "rescale.interp" ), "none" ) )
463                 mlt_properties_set( a_props, "rescale.interp", "nearest" );
464
465         // Since we are the consumer of the b_frame, we must pass along this
466         // consumer property from the a_frame
467         if ( mlt_properties_get_double( a_props, "aspect_ratio" ) == 0.0 )
468                 mlt_properties_set_double( a_props, "aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
469         if ( mlt_properties_get_double( b_props, "aspect_ratio" ) == 0.0 )
470                 mlt_properties_set_double( b_props, "aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
471         mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
472
473         // Honour the reverse here
474         if ( mix >= 1.0 )
475                 mix -= floor( mix );
476
477         // Ensure we get scaling on the b_frame
478         if ( mlt_properties_get( b_props, "rescale.interp" ) == NULL || !strcmp( mlt_properties_get( b_props, "rescale.interp" ), "none" ) )
479                 mlt_properties_set( b_props, "rescale.interp", mlt_properties_get( a_props, "rescale.interp" ) );
480         
481         if ( invert )
482                 mlt_properties_set_int( b_props, "consumer_deinterlace", mlt_properties_get_int( a_props, "consumer_deinterlace") );
483
484         if ( mlt_properties_get( properties, "fixed" ) )
485                 mix = mlt_properties_get_double( properties, "fixed" );
486
487         if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
488         {
489                 reverse = invert ? !reverse : reverse;
490                 mix = reverse ? 1 - mix : mix;
491                 frame_delta *= reverse ? -1.0 : 1.0;
492                 // Composite the frames using a luma map
493                 luma_composite( !invert ? a_frame : b_frame, !invert ? b_frame : a_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
494                         luma_softness, progressive ? -1 : top_field_first, width, height );
495         }
496         else
497         {
498                 mix = ( reverse || invert ) ? 1 - mix : mix;
499                 invert = 0;
500                 // Dissolve the frames using the time offset for mix value
501                 dissolve_yuv( a_frame, b_frame, mix, *width, *height );
502         }
503         
504         mlt_service_unlock( MLT_TRANSITION_SERVICE( transition ) );
505
506         // Extract the a_frame image info
507         *width = mlt_properties_get_int( !invert ? a_props : b_props, "width" );
508         *height = mlt_properties_get_int( !invert ? a_props : b_props, "height" );
509         *image = mlt_properties_get_data( !invert ? a_props : b_props, "image", NULL );
510
511         return 0;
512 }
513
514
515 /** Luma transition processing.
516 */
517
518 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
519 {
520         // Get a unique name to store the frame position
521         char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( transition ), "_unique_id" );
522
523         // Assign the current position to the name
524         mlt_properties_set_position( MLT_FRAME_PROPERTIES( a_frame ), name, mlt_frame_get_position( a_frame ) );
525
526         // Push the transition on to the frame
527         mlt_frame_push_service( a_frame, transition );
528
529         // Push the b_frame on to the stack
530         mlt_frame_push_frame( a_frame, b_frame );
531
532         // Push the transition method
533         mlt_frame_push_get_image( a_frame, transition_get_image );
534         
535         return a_frame;
536 }
537
538 /** Constructor for the filter.
539 */
540
541 mlt_transition transition_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *lumafile )
542 {
543         mlt_transition transition = mlt_transition_new( );
544         if ( transition != NULL )
545         {
546                 // Set the methods
547                 transition->process = transition_process;
548                 
549                 // Default factory
550                 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "factory", mlt_environment( "MLT_PRODUCER" ) );
551
552                 // Set the main property
553                 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "resource", lumafile );
554                 
555                 // Inform apps and framework that this is a video only transition
556                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
557
558                 return transition;
559         }
560         return NULL;
561 }