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