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