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