regionalised fx part 1
[melted] / src / modules / core / transition_composite.c
1 /*
2 * transition_composite.c -- compose one image over another using alpha channel
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_composite.h"
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <math.h>
29
30 /** Geometry struct.
31 */
32
33 struct geometry_s
34 {
35 float position;
36 float mix;
37 int nw; // normalised width
38 int nh; // normalised height
39 int sw; // scaled width, not including consumer scale based upon w/nw
40 int sh; // scaled height, not including consumer scale based upon h/nh
41 float x;
42 float y;
43 float w;
44 float h;
45 int halign; // horizontal alignment: 0=left, 1=center, 2=right
46 int valign; // vertical alignment: 0=top, 1=middle, 2=bottom
47 int distort;
48 struct geometry_s *next;
49 };
50
51 /** Parse a value from a geometry string.
52 */
53
54 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
55 {
56 float value = defaults;
57
58 if ( *ptr != NULL && **ptr != '\0' )
59 {
60 char *end = NULL;
61 value = strtod( *ptr, &end );
62 if ( end != NULL )
63 {
64 if ( *end == '%' )
65 value = ( value / 100.0 ) * normalisation;
66 while ( *end == delim || *end == '%' )
67 end ++;
68 }
69 *ptr = end;
70 }
71
72 return value;
73 }
74
75 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
76 expressed as a percentage by appending a % after the value, otherwise values are
77 assumed to be relative to the normalised dimensions of the consumer.
78 */
79
80 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
81 {
82 // Assign normalised width and height
83 geometry->nw = nw;
84 geometry->nh = nh;
85
86 // Assign from defaults if available
87 if ( defaults != NULL )
88 {
89 geometry->x = defaults->x;
90 geometry->y = defaults->y;
91 geometry->w = geometry->sw = defaults->w;
92 geometry->h = geometry->sh = defaults->h;
93 geometry->distort = defaults->distort;
94 geometry->mix = defaults->mix;
95 defaults->next = geometry;
96 }
97 else
98 {
99 geometry->mix = 100;
100 }
101
102 // Parse the geomtry string
103 if ( property != NULL && strcmp( property, "" ) )
104 {
105 char *ptr = property;
106 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
107 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
108 geometry->w = geometry->sw = parse_value( &ptr, nw, 'x', geometry->w );
109 geometry->h = geometry->sh = parse_value( &ptr, nh, ':', geometry->h );
110 if ( *ptr == '!' )
111 {
112 geometry->distort = 1;
113 ptr ++;
114 if ( *ptr == ':' )
115 ptr ++;
116 }
117 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
118 }
119 }
120
121 /** Calculate real geometry.
122 */
123
124 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, float position )
125 {
126 // Search in for position
127 struct geometry_s *out = in->next;
128
129 if ( position >= 1.0 )
130 {
131 int section = floor( position );
132 position -= section;
133 if ( section % 2 == 1 )
134 position = 1.0 - position;
135 }
136
137 while ( out->next != NULL )
138 {
139 if ( position >= in->position && position < out->position )
140 break;
141
142 in = out;
143 out = in->next;
144 }
145
146 position = ( position - in->position ) / ( out->position - in->position );
147
148 // Calculate this frames geometry
149 output->nw = in->nw;
150 output->nh = in->nh;
151 output->x = in->x + ( out->x - in->x ) * position;
152 output->y = in->y + ( out->y - in->y ) * position;
153 output->w = in->w + ( out->w - in->w ) * position;
154 output->h = in->h + ( out->h - in->h ) * position;
155 output->sw = output->w;
156 output->sh = output->h;
157 output->mix = in->mix + ( out->mix - in->mix ) * position;
158 output->distort = in->distort;
159
160 output->x = ( int )floor( output->x ) & 0xfffffffe;
161 output->w = ( int )floor( output->w ) & 0xfffffffe;
162 output->sw &= 0xfffffffe;
163 }
164
165 void transition_destroy_keys( void *arg )
166 {
167 struct geometry_s *ptr = arg;
168 struct geometry_s *next = NULL;
169
170 while ( ptr != NULL )
171 {
172 next = ptr->next;
173 free( ptr );
174 ptr = next;
175 }
176 }
177
178 static struct geometry_s *transition_parse_keys( mlt_transition this, int normalised_width, int normalised_height )
179 {
180 // Loop variable for property interrogation
181 int i = 0;
182
183 // Get the properties of the transition
184 mlt_properties properties = mlt_transition_properties( this );
185
186 // Get the in and out position
187 mlt_position in = mlt_transition_get_in( this );
188 mlt_position out = mlt_transition_get_out( this );
189
190 // Create the start
191 struct geometry_s *start = calloc( 1, sizeof( struct geometry_s ) );
192
193 // Create the end (we always need two entries)
194 struct geometry_s *end = calloc( 1, sizeof( struct geometry_s ) );
195
196 // Pointer
197 struct geometry_s *ptr = start;
198
199 // Parse the start property
200 geometry_parse( start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
201
202 // Parse the keys in between
203 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
204 {
205 // Get the name of the property
206 char *name = mlt_properties_get_name( properties, i );
207
208 // Check that it's valid
209 if ( !strncmp( name, "key[", 4 ) )
210 {
211 // Get the value of the property
212 char *value = mlt_properties_get_value( properties, i );
213
214 // Determine the frame number
215 int frame = atoi( name + 4 );
216
217 // Determine the position
218 float position = 0;
219
220 if ( frame >= 0 && frame < ( out - in ) )
221 position = ( float )frame / ( float )( out - in + 1 );
222 else if ( frame < 0 && - frame < ( out - in ) )
223 position = ( float )( out - in + frame ) / ( float )( out - in + 1 );
224
225 // For now, we'll exclude all keys received out of order
226 if ( position > ptr->position )
227 {
228 // Create a new geometry
229 struct geometry_s *temp = calloc( 1, sizeof( struct geometry_s ) );
230
231 // Parse and add to the list
232 geometry_parse( temp, ptr, value, normalised_width, normalised_height );
233
234 // Assign the position
235 temp->position = position;
236
237 // Allow the next to be appended after this one
238 ptr = temp;
239 }
240 else
241 {
242 fprintf( stderr, "Key out of order - skipping %s\n", name );
243 }
244 }
245 }
246
247 // Parse the end
248 geometry_parse( end, ptr, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
249 if ( out > 0 )
250 end->position = ( float )( out - in ) / ( float )( out - in + 1 );
251 else
252 end->position = 1;
253
254 // Assign to properties to ensure we get destroyed
255 mlt_properties_set_data( properties, "geometries", start, 0, transition_destroy_keys, NULL );
256
257 return start;
258 }
259
260 /** Parse the alignment properties into the geometry.
261 */
262
263 static int alignment_parse( char* align )
264 {
265 int ret = 0;
266
267 if ( align == NULL );
268 else if ( isdigit( align[ 0 ] ) )
269 ret = atoi( align );
270 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
271 ret = 1;
272 else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
273 ret = 2;
274
275 return ret;
276 }
277
278 /** Adjust position according to scaled size and alignment properties.
279 */
280
281 static void alignment_calculate( struct geometry_s *geometry )
282 {
283 geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2;
284 geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2;
285 }
286
287 /** Calculate the position for this frame.
288 */
289
290 static float position_calculate( mlt_transition this, mlt_frame frame )
291 {
292 // Get the in and out position
293 mlt_position in = mlt_transition_get_in( this );
294 mlt_position out = mlt_transition_get_out( this );
295
296 // Get the position
297 mlt_position position = mlt_frame_get_position( frame );
298
299 // Now do the calcs
300 return ( float )( position - in ) / ( float )( out - in + 1 );
301 }
302
303 /** Calculate the field delta for this frame - position between two frames.
304 */
305
306 static inline float delta_calculate( mlt_transition this, mlt_frame frame )
307 {
308 // Get the in and out position
309 mlt_position in = mlt_transition_get_in( this );
310 mlt_position out = mlt_transition_get_out( this );
311
312 // Get the position of the frame
313 mlt_position position = mlt_frame_get_position( frame );
314
315 // Now do the calcs
316 float x = ( float )( position - in ) / ( float )( out - in + 1 );
317 float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
318
319 return ( y - x ) / 2.0;
320 }
321
322 static int get_value( mlt_properties properties, char *preferred, char *fallback )
323 {
324 int value = mlt_properties_get_int( properties, preferred );
325 if ( value == 0 )
326 value = mlt_properties_get_int( properties, fallback );
327 return value;
328 }
329
330 /** Composite function.
331 */
332
333 static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, int bpp, uint8_t *p_src, int width_src, int height_src, uint8_t *p_alpha, struct geometry_s geometry, int field )
334 {
335 int ret = 0;
336 int i, j;
337 int x_src = 0, y_src = 0;
338 int32_t weight = ( 1 << 16 ) * ( geometry.mix / 100 );
339 int stride_src = width_src * bpp;
340 int stride_dest = width_dest * bpp;
341
342 // Adjust to consumer scale
343 int x = geometry.x * width_dest / geometry.nw;
344 int y = geometry.y * height_dest / geometry.nh;
345
346 x &= 0xfffffffe;
347 width_src &= 0xfffffffe;
348
349 // optimization points - no work to do
350 if ( width_src <= 0 || height_src <= 0 )
351 return ret;
352
353 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
354 return ret;
355
356 // crop overlay off the left edge of frame
357 if ( x < 0 )
358 {
359 x_src = -x;
360 width_src -= x_src;
361 x = 0;
362 }
363
364 // crop overlay beyond right edge of frame
365 else if ( x + width_src > width_dest )
366 width_src = width_dest - x;
367
368 // crop overlay off the top edge of the frame
369 if ( y < 0 )
370 {
371 y_src = -y;
372 height_src -= y_src;
373 }
374 // crop overlay below bottom edge of frame
375 else if ( y + height_src > height_dest )
376 height_src = height_dest - y;
377
378 // offset pointer into overlay buffer based on cropping
379 p_src += x_src * bpp + y_src * stride_src;
380
381 // offset pointer into frame buffer based upon positive coordinates only!
382 p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
383
384 // offset pointer into alpha channel based upon cropping
385 if ( p_alpha )
386 p_alpha += x_src + y_src * stride_src / bpp;
387
388 // Assuming lower field first
389 // Special care is taken to make sure the b_frame is aligned to the correct field.
390 // field 0 = lower field and y should be odd (y is 0-based).
391 // field 1 = upper field and y should be even.
392 if ( ( field > -1 ) && ( y % 2 == field ) )
393 {
394 //fprintf( stderr, "field %d y %d\n", field, y );
395 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
396 p_dest += stride_dest;
397 else
398 p_dest -= stride_dest;
399 }
400
401 // On the second field, use the other lines from b_frame
402 if ( field == 1 )
403 {
404 p_src += stride_src;
405 if ( p_alpha )
406 p_alpha += stride_src / bpp;
407 height_src--;
408 }
409
410 uint8_t *p = p_src;
411 uint8_t *q = p_dest;
412 uint8_t *o = p_dest;
413 uint8_t *z = p_alpha;
414
415 uint8_t a;
416 int32_t value;
417 int step = ( field > -1 ) ? 2 : 1;
418
419 stride_src = stride_src * step;
420 int alpha_stride = stride_src / bpp;
421 stride_dest = stride_dest * step;
422
423 // now do the compositing only to cropped extents
424 for ( i = 0; i < height_src; i += step )
425 {
426 p = p_src;
427 q = p_dest;
428 o = q;
429 z = p_alpha;
430
431 for ( j = 0; j < width_src; j ++ )
432 {
433 a = ( z == NULL ) ? 255 : *z ++;
434 value = ( weight * ( a + 1 ) ) >> 8;
435 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
436 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
437 }
438
439 p_src += stride_src;
440 p_dest += stride_dest;
441 if ( p_alpha )
442 p_alpha += alpha_stride;
443 }
444
445 return ret;
446 }
447
448
449 /** Get the properly sized image from b_frame.
450 */
451
452 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
453 {
454 int ret = 0;
455 mlt_image_format format = mlt_image_yuv422;
456
457 // Get the properties objects
458 mlt_properties b_props = mlt_frame_properties( b_frame );
459 mlt_properties properties = mlt_transition_properties( this );
460
461 if ( mlt_properties_get( properties, "distort" ) == NULL && geometry->distort == 0 )
462 {
463 // Adjust b_frame pixel aspect
464 int normalised_width = geometry->w;
465 int normalised_height = geometry->h;
466 int real_width = get_value( b_props, "real_width", "width" );
467 int real_height = get_value( b_props, "real_height", "height" );
468 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
469 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
470 int scaled_width = real_width;
471 int scaled_height = real_height;
472 double output_sar = ( double ) geometry->nw / geometry->nh / output_ar;
473
474 // If the output is fat pixels (NTSC) then stretch our input horizontally
475 // derived from: output_sar / input_sar * real_width
476 scaled_width = output_sar * real_height * input_ar;
477
478 // Now ensure that our images fit in the normalised frame
479 if ( scaled_width > normalised_width )
480 {
481 scaled_height = scaled_height * normalised_width / scaled_width;
482 scaled_width = normalised_width;
483 }
484 if ( scaled_height > normalised_height )
485 {
486 scaled_width = scaled_width * normalised_height / scaled_height;
487 scaled_height = normalised_height;
488 }
489
490 // Now apply the fill
491 // TODO: Should combine fill/distort in one property
492 if ( mlt_properties_get( properties, "fill" ) != NULL )
493 {
494 scaled_width = ( geometry->w / scaled_width ) * scaled_width;
495 scaled_height = ( geometry->h / scaled_height ) * scaled_height;
496 }
497
498 // Save the new scaled dimensions
499 geometry->sw = scaled_width;
500 geometry->sh = scaled_height;
501 }
502
503 // We want to ensure that we bypass resize now...
504 mlt_properties_set( b_props, "distort", "true" );
505
506 // Take into consideration alignment for optimisation
507 alignment_calculate( geometry );
508
509 // Adjust to consumer scale
510 int x = geometry->x * *width / geometry->nw;
511 int y = geometry->y * *height / geometry->nh;
512 *width = geometry->sw * *width / geometry->nw;
513 *height = geometry->sh * *height / geometry->nh;
514
515 x -= x % 2;
516
517 // optimization points - no work to do
518 if ( *width <= 0 || *height <= 0 )
519 return 1;
520
521 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
522 return 1;
523
524 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
525
526 return ret;
527 }
528
529
530 static uint8_t *transition_get_alpha_mask( mlt_frame this )
531 {
532 // Obtain properties of frame
533 mlt_properties properties = mlt_frame_properties( this );
534
535 // Return the alpha mask
536 return mlt_properties_get_data( properties, "alpha", NULL );
537 }
538
539 struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transition this, mlt_frame a_frame, float position )
540 {
541 // Get the properties from the transition
542 mlt_properties properties = mlt_transition_properties( this );
543
544 // Get the properties from the frame
545 mlt_properties a_props = mlt_frame_properties( a_frame );
546
547 // Structures for geometry
548 struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
549
550 // Now parse the geometries
551 if ( start == NULL )
552 {
553 // Obtain the normalised width and height from the a_frame
554 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
555 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
556
557 // Parse the transitions properties
558 start = transition_parse_keys( this, normalised_width, normalised_height );
559 }
560
561 // Do the calculation
562 geometry_calculate( result, start, position );
563
564 // Now parse the alignment
565 result->halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
566 result->valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
567
568 return start;
569 }
570
571 mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame )
572 {
573 // Create a frame to return
574 mlt_frame b_frame = mlt_frame_init( );
575
576 // Get the properties of the a frame
577 mlt_properties a_props = mlt_frame_properties( a_frame );
578
579 // Get the properties of the b frame
580 mlt_properties b_props = mlt_frame_properties( b_frame );
581
582 // Get the position
583 float position = position_calculate( this, a_frame );
584
585 // Destination image
586 uint8_t *dest = NULL;
587
588 // Get the image and dimensions
589 uint8_t *image = mlt_properties_get_data( a_props, "image", NULL );
590 int width = mlt_properties_get_int( a_props, "width" );
591 int height = mlt_properties_get_int( a_props, "height" );
592
593 // Pointers for copy operation
594 uint8_t *p;
595 uint8_t *q;
596 uint8_t *r;
597
598 // Corrdinates
599 int w = 0;
600 int h = 0;
601 int x = 0;
602 int y = 0;
603
604 // Will need to know region to copy
605 struct geometry_s result;
606
607 // Calculate the region now
608 composite_calculate( &result, this, a_frame, position );
609
610 // Need to scale down to actual dimensions
611 x = result.x * width / result.nw ;
612 y = result.y * height / result.nh;
613 w = result.sw * width / result.nw;
614 h = result.sh * height / result.nh;
615
616 x &= 0xfffffffe;
617 w &= 0xfffffffe;
618
619 // Now we need to create a new destination image
620 dest = mlt_pool_alloc( w * h * 2 );
621
622 // Copy the region of the image
623 p = image + y * width * 2 + x * 2;
624 q = dest;
625 r = dest + w * h * 2;
626
627 while ( q < r )
628 {
629 memcpy( q, p, w * 2 );
630 q += w * 2;
631 p += width * 2;
632 }
633
634 // Assign to the new frame
635 mlt_properties_set_data( b_props, "image", dest, w * h * 2, mlt_pool_release, NULL );
636 mlt_properties_set_int( b_props, "width", w );
637 mlt_properties_set_int( b_props, "height", h );
638
639 // Return the frame
640 return b_frame;
641 }
642
643 /** Get the image.
644 */
645
646 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
647 {
648 // Get the b frame from the stack
649 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
650
651 // Get the transition from the a frame
652 mlt_transition this = mlt_frame_pop_service( a_frame );
653
654 // This compositer is yuv422 only
655 *format = mlt_image_yuv422;
656
657 // Get the image from the a frame
658 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
659
660 if ( b_frame != NULL )
661 {
662 // Get the properties of the a frame
663 mlt_properties a_props = mlt_frame_properties( a_frame );
664
665 // Get the properties of the b frame
666 mlt_properties b_props = mlt_frame_properties( b_frame );
667
668 // Get the properties from the transition
669 mlt_properties properties = mlt_transition_properties( this );
670
671 // Structures for geometry
672 struct geometry_s result;
673
674 // Calculate the position
675 float position = mlt_properties_get_double( b_props, "relative_position" );
676 float delta = delta_calculate( this, a_frame );
677
678 // Do the calculation
679 struct geometry_s *start = composite_calculate( &result, this, a_frame, position );
680
681 // Since we are the consumer of the b_frame, we must pass along these
682 // consumer properties from the a_frame
683 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
684 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
685
686 // Get the image from the b frame
687 uint8_t *image_b = NULL;
688 int width_b = *width;
689 int height_b = *height;
690
691 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
692 {
693 uint8_t *dest = *image;
694 uint8_t *src = image_b;
695 int bpp = 2;
696 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
697 int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
698 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
699 mlt_properties_get_int( properties, "progressive" );
700 int field;
701
702 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
703 {
704 // Assume lower field (0) first
705 float field_position = position + field * delta;
706
707 // Do the calculation if we need to
708 geometry_calculate( &result, start, field_position );
709
710 // Align
711 alignment_calculate( &result );
712
713 // Composite the b_frame on the a_frame
714 composite_yuv( dest, *width, *height, bpp, src, width_b, height_b, alpha, result, progressive ? -1 : field );
715 }
716 }
717 }
718
719 return 0;
720 }
721
722 /** Composition transition processing.
723 */
724
725 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
726 {
727 // Propogate the transition properties to the b frame
728 mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, a_frame ) );
729 mlt_frame_push_service( a_frame, this );
730 mlt_frame_push_get_image( a_frame, transition_get_image );
731 mlt_frame_push_frame( a_frame, b_frame );
732 return a_frame;
733 }
734
735 /** Constructor for the filter.
736 */
737
738 mlt_transition transition_composite_init( char *arg )
739 {
740 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
741 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
742 {
743 this->process = composite_process;
744 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
745 }
746 return this;
747 }
748