f3695d2b775ae3cb2567ef6fae684df9e444fc0d
[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 + 0.5;
152 output->y = in->y + ( out->y - in->y ) * position + 0.5;
153 output->w = in->w + ( out->w - in->w ) * position;
154 output->h = in->h + ( out->h - in->h ) * position;
155 output->mix = in->mix + ( out->mix - in->mix ) * position;
156 output->distort = in->distort;
157 }
158
159 void transition_destroy_keys( void *arg )
160 {
161 struct geometry_s *ptr = arg;
162 struct geometry_s *next = NULL;
163
164 while ( ptr != NULL )
165 {
166 next = ptr->next;
167 free( ptr );
168 ptr = next;
169 }
170 }
171
172 static struct geometry_s *transition_parse_keys( mlt_transition this, int normalised_width, int normalised_height )
173 {
174 // Loop variable for property interrogation
175 int i = 0;
176
177 // Get the properties of the transition
178 mlt_properties properties = mlt_transition_properties( this );
179
180 // Get the in and out position
181 mlt_position in = mlt_transition_get_in( this );
182 mlt_position out = mlt_transition_get_out( this );
183
184 // Create the start
185 struct geometry_s *start = calloc( 1, sizeof( struct geometry_s ) );
186
187 // Create the end (we always need two entries)
188 struct geometry_s *end = calloc( 1, sizeof( struct geometry_s ) );
189
190 // Pointer
191 struct geometry_s *ptr = start;
192
193 // Parse the start property
194 geometry_parse( start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
195
196 // Parse the keys in between
197 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
198 {
199 // Get the name of the property
200 char *name = mlt_properties_get_name( properties, i );
201
202 // Check that it's valid
203 if ( !strncmp( name, "key[", 4 ) )
204 {
205 // Get the value of the property
206 char *value = mlt_properties_get_value( properties, i );
207
208 // Determine the frame number
209 int frame = atoi( name + 4 );
210
211 // Determine the position
212 float position = 0;
213
214 if ( frame >= 0 && frame < ( out - in ) )
215 position = ( float )frame / ( float )( out - in + 1 );
216 else if ( frame < 0 && - frame < ( out - in ) )
217 position = ( float )( out - in + frame ) / ( float )( out - in + 1 );
218
219 // For now, we'll exclude all keys received out of order
220 if ( position > ptr->position )
221 {
222 // Create a new geometry
223 struct geometry_s *temp = calloc( 1, sizeof( struct geometry_s ) );
224
225 // Parse and add to the list
226 geometry_parse( temp, ptr, value, normalised_width, normalised_height );
227
228 // Assign the position
229 temp->position = position;
230
231 // Allow the next to be appended after this one
232 ptr = temp;
233 }
234 else
235 {
236 fprintf( stderr, "Key out of order - skipping %s\n", name );
237 }
238 }
239 }
240
241 // Parse the end
242 geometry_parse( end, ptr, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
243 if ( out > 0 )
244 end->position = ( float )( out - in ) / ( float )( out - in + 1 );
245 else
246 end->position = 1;
247
248 // Assign to properties to ensure we get destroyed
249 mlt_properties_set_data( properties, "geometries", start, 0, transition_destroy_keys, NULL );
250
251 return start;
252 }
253
254 /** Parse the alignment properties into the geometry.
255 */
256
257 static int alignment_parse( char* align )
258 {
259 int ret = 0;
260
261 if ( align == NULL );
262 else if ( isdigit( align[ 0 ] ) )
263 ret = atoi( align );
264 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
265 ret = 1;
266 else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
267 ret = 2;
268
269 return ret;
270 }
271
272 /** Adjust position according to scaled size and alignment properties.
273 */
274
275 static void alignment_calculate( struct geometry_s *geometry )
276 {
277 geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2 + 0.5;
278 geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2 + 0.5;
279 }
280
281 /** Calculate the position for this frame.
282 */
283
284 static inline float position_calculate( mlt_transition this, mlt_frame frame )
285 {
286 // Get the in and out position
287 mlt_position in = mlt_transition_get_in( this );
288 mlt_position out = mlt_transition_get_out( this );
289
290 // Get the position
291 mlt_position position = mlt_frame_get_position( frame );
292
293 // Now do the calcs
294 return ( float )( position - in ) / ( float )( out - in + 1 );
295 }
296
297 /** Calculate the field delta for this frame - position between two frames.
298 */
299
300 static inline float delta_calculate( mlt_transition this, mlt_frame frame )
301 {
302 // Get the in and out position
303 mlt_position in = mlt_transition_get_in( this );
304 mlt_position out = mlt_transition_get_out( this );
305
306 // Get the position of the frame
307 mlt_position position = mlt_frame_get_position( frame );
308
309 // Now do the calcs
310 float x = ( float )( position - in ) / ( float )( out - in + 1 );
311 float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
312
313 return ( y - x ) / 2.0;
314 }
315
316 static int get_value( mlt_properties properties, char *preferred, char *fallback )
317 {
318 int value = mlt_properties_get_int( properties, preferred );
319 if ( value == 0 )
320 value = mlt_properties_get_int( properties, fallback );
321 return value;
322 }
323
324 /** Composite function.
325 */
326
327 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 )
328 {
329 int ret = 0;
330 int i, j;
331 int x_src = 0, y_src = 0;
332 int32_t weight = ( 1 << 16 ) * ( geometry.mix / 100 );
333 int stride_src = width_src * bpp;
334 int stride_dest = width_dest * bpp;
335
336 // Adjust to consumer scale
337 int x = geometry.x * width_dest / geometry.nw + 0.5;
338 int y = geometry.y * height_dest / geometry.nh + 0.5;
339
340 if ( bpp == 2 )
341 x -= x % 2;
342
343 // optimization points - no work to do
344 if ( width_src <= 0 || height_src <= 0 )
345 return ret;
346
347 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
348 return ret;
349
350 // crop overlay off the left edge of frame
351 if ( x < 0 )
352 {
353 x_src = -x;
354 width_src -= x_src;
355 x = 0;
356 }
357
358 // crop overlay beyond right edge of frame
359 else if ( x + width_src > width_dest )
360 width_src = width_dest - x;
361
362 // crop overlay off the top edge of the frame
363 if ( y < 0 )
364 {
365 y_src = -y;
366 height_src -= y_src;
367 }
368 // crop overlay below bottom edge of frame
369 else if ( y + height_src > height_dest )
370 height_src = height_dest - y;
371
372 // offset pointer into overlay buffer based on cropping
373 p_src += x_src * bpp + y_src * stride_src;
374
375 // offset pointer into frame buffer based upon positive coordinates only!
376 p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
377
378 // offset pointer into alpha channel based upon cropping
379 if ( p_alpha )
380 p_alpha += x_src + y_src * stride_src / bpp;
381
382 // Assuming lower field first
383 // Special care is taken to make sure the b_frame is aligned to the correct field.
384 // field 0 = lower field and y should be odd (y is 0-based).
385 // field 1 = upper field and y should be even.
386 if ( ( field > -1 ) && ( y % 2 == field ) )
387 {
388 //fprintf( stderr, "field %d y %d\n", field, y );
389 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
390 p_dest += stride_dest;
391 else
392 p_dest -= stride_dest;
393 }
394
395 // On the second field, use the other lines from b_frame
396 if ( field == 1 )
397 {
398 p_src += stride_src;
399 if ( p_alpha )
400 p_alpha += stride_src / bpp;
401 height_src--;
402 }
403
404 uint8_t *p = p_src;
405 uint8_t *q = p_dest;
406 uint8_t *o = p_dest;
407 uint8_t *z = p_alpha;
408
409 uint8_t a;
410 int32_t value;
411 int step = ( field > -1 ) ? 2 : 1;
412
413 stride_src = stride_src * step;
414 int alpha_stride = stride_src / bpp;
415 stride_dest = stride_dest * step;
416
417 // now do the compositing only to cropped extents
418 for ( i = 0; i < height_src; i += step )
419 {
420 p = p_src;
421 q = p_dest;
422 o = q;
423 z = p_alpha;
424
425 for ( j = 0; j < width_src; j ++ )
426 {
427 a = ( z == NULL ) ? 255 : *z ++;
428 value = ( weight * ( a + 1 ) ) >> 8;
429 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
430 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
431 }
432
433 p_src += stride_src;
434 p_dest += stride_dest;
435 if ( p_alpha )
436 p_alpha += alpha_stride;
437 }
438
439 return ret;
440 }
441
442
443 /** Get the properly sized image from b_frame.
444 */
445
446 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
447 {
448 int ret = 0;
449 mlt_image_format format = mlt_image_yuv422;
450
451 // Get the properties objects
452 mlt_properties b_props = mlt_frame_properties( b_frame );
453 mlt_properties properties = mlt_transition_properties( this );
454
455 // ???: Not getting the logic of this...
456 geometry->sw = geometry->w;
457 geometry->sh = geometry->h;
458
459 if ( mlt_properties_get( properties, "distort" ) == NULL && geometry->distort == 0 )
460 {
461 // Adjust b_frame pixel aspect
462 int normalised_width = geometry->w;
463 int normalised_height = geometry->h;
464 int real_width = get_value( b_props, "real_width", "width" );
465 int real_height = get_value( b_props, "real_height", "height" );
466 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
467 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
468 int scaled_width = real_width;
469 int scaled_height = real_height;
470 double output_sar = ( double ) geometry->nw / geometry->nh / output_ar;
471
472 // If the output is fat pixels (NTSC) then stretch our input horizontally
473 // derived from: output_sar / input_sar * real_width
474 scaled_width = output_sar * real_height * input_ar;
475
476 // Now ensure that our images fit in the normalised frame
477 if ( scaled_width > normalised_width )
478 {
479 scaled_height = scaled_height * normalised_width / scaled_width;
480 scaled_width = normalised_width;
481 }
482 if ( scaled_height > normalised_height )
483 {
484 scaled_width = scaled_width * normalised_height / scaled_height;
485 scaled_height = normalised_height;
486 }
487
488 // Now apply the fill
489 // TODO: Should combine fill/distort in one property
490 if ( mlt_properties_get( properties, "fill" ) != NULL )
491 {
492 scaled_width = ( geometry->w / scaled_width ) * scaled_width;
493 scaled_height = ( geometry->h / scaled_height ) * scaled_height;
494 }
495
496 // Save the new scaled dimensions
497 geometry->sw = scaled_width;
498 geometry->sh = scaled_height;
499 }
500
501 // We want to ensure that we bypass resize now...
502 mlt_properties_set( b_props, "distort", "true" );
503
504 // Take into consideration alignment for optimisation
505 alignment_calculate( geometry );
506
507 // Adjust to consumer scale
508 int x = geometry->x * *width / geometry->nw + 0.5;
509 int y = geometry->y * *height / geometry->nh + 0.5;
510 *width = geometry->sw * *width / geometry->nw;
511 *height = geometry->sh * *height / geometry->nh;
512
513 x -= x % 2;
514
515 // optimization points - no work to do
516 if ( *width <= 0 || *height <= 0 )
517 return 1;
518
519 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
520 return 1;
521
522 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
523
524 return ret;
525 }
526
527
528 static uint8_t *transition_get_alpha_mask( mlt_frame this )
529 {
530 // Obtain properties of frame
531 mlt_properties properties = mlt_frame_properties( this );
532
533 // Return the alpha mask
534 return mlt_properties_get_data( properties, "alpha", NULL );
535 }
536
537 /** Get the image.
538 */
539
540 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
541 {
542 // Get the b frame from the stack
543 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
544
545 // This compositer is yuv422 only
546 *format = mlt_image_yuv422;
547
548 // Get the transition from the a frame
549 mlt_transition this = mlt_frame_pop_service( a_frame );
550
551 // Get the image from the a frame
552 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
553
554 if ( b_frame != NULL )
555 {
556 // Get the properties of the a frame
557 mlt_properties a_props = mlt_frame_properties( a_frame );
558
559 // Get the properties of the b frame
560 mlt_properties b_props = mlt_frame_properties( b_frame );
561
562 // Get the properties from the transition
563 mlt_properties properties = mlt_transition_properties( this );
564
565 // Structures for geometry
566 struct geometry_s result;
567 struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
568
569 // Calculate the position
570 float position = mlt_properties_get_double( b_props, "relative_position" );
571 float delta = delta_calculate( this, a_frame );
572
573 // Now parse the geometries
574 if ( start == NULL )
575 {
576 // Obtain the normalised width and height from the a_frame
577 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
578 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
579
580 // Parse the transitions properties
581 start = transition_parse_keys( this, normalised_width, normalised_height );
582 }
583
584 // Since we are the consumer of the b_frame, we must pass along these
585 // consumer properties from the a_frame
586 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
587 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
588
589 // Do the calculation
590 geometry_calculate( &result, start, position );
591
592 // Now parse the alignment
593 result.halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
594 result.valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
595
596 // Get the image from the b frame
597 uint8_t *image_b = NULL;
598 int width_b = *width;
599 int height_b = *height;
600
601 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
602 {
603 uint8_t *dest = *image;
604 uint8_t *src = image_b;
605 int bpp = 2;
606 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
607 int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
608 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
609 mlt_properties_get_int( properties, "progressive" );
610 int field;
611
612 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
613 {
614 // Assume lower field (0) first
615 float field_position = position + field * delta;
616
617 // Do the calculation if we need to
618 geometry_calculate( &result, start, field_position );
619
620 // Align
621 alignment_calculate( &result );
622
623 // Composite the b_frame on the a_frame
624 composite_yuv( dest, *width, *height, bpp, src, width_b, height_b, alpha, result, progressive ? -1 : field );
625 }
626 }
627 }
628
629 return 0;
630 }
631
632 /** Composition transition processing.
633 */
634
635 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
636 {
637 // Propogate the transition properties to the b frame
638 mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, a_frame ) );
639 mlt_frame_push_service( a_frame, this );
640 mlt_frame_push_get_image( a_frame, transition_get_image );
641 mlt_frame_push_frame( a_frame, b_frame );
642 return a_frame;
643 }
644
645 /** Constructor for the filter.
646 */
647
648 mlt_transition transition_composite_init( char *arg )
649 {
650 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
651 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
652 {
653 this->process = composite_process;
654 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
655 }
656 return this;
657 }
658