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