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