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