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