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