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