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