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