7a32dd5020d69a3e483df7c5200a0219e0aab791
[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
27 /** Get the image.
28 */
29
30 static int transition_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
31 {
32 // Get the properties of the a frame
33 mlt_properties a_props = mlt_frame_properties( this );
34
35 // Get the b frame from the stack
36 mlt_frame b_frame = mlt_frame_pop_frame( this );
37
38 if ( b_frame != NULL )
39 {
40 // Get the properties of the b frame
41 mlt_properties b_props = mlt_frame_properties( b_frame );
42
43 // Arbitrary composite defaults
44 int x = 0;
45 int y = 0;
46 double mix = 1.0;
47
48 // Override from b frame properties if provided
49 if ( mlt_properties_get( b_props, "x" ) != NULL )
50 x = mlt_properties_get_int( b_props, "x" );
51 if ( mlt_properties_get( b_props, "y" ) != NULL )
52 y = mlt_properties_get_int( b_props, "y" );
53 if ( mlt_properties_get( b_props, "image.mix" ) != NULL )
54 mix = mlt_properties_get_double( b_props, "image.mix" );
55
56 // Composite the b_frame on the a_frame
57 mlt_frame_composite_yuv( this, b_frame, x, y, mix );
58
59 // Extract the a_frame image info
60 *width = mlt_properties_get_int( a_props, "width" );
61 *height = mlt_properties_get_int( a_props, "height" );
62 *image = mlt_properties_get_data( a_props, "image", NULL );
63 }
64 else if ( a_props != NULL )
65 {
66 // Extract the a_frame image info
67 *width = mlt_properties_get_int( a_props, "width" );
68 *height = mlt_properties_get_int( a_props, "height" );
69 *image = mlt_properties_get_data( a_props, "image", NULL );
70 }
71
72 return 0;
73 }
74
75 /** Composition transition processing.
76 */
77
78 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
79 {
80 // Propogate the transition properties to the b frame
81 mlt_properties properties = mlt_transition_properties( this );
82 mlt_properties b_props = mlt_frame_properties( b_frame );
83
84 if ( mlt_properties_get( properties, "x" ) != NULL )
85 mlt_properties_set_int( b_props, "x", mlt_properties_get_int( properties, "x" ) );
86 if ( mlt_properties_get( properties, "y" ) != NULL )
87 mlt_properties_set_int( b_props, "y", mlt_properties_get_int( properties, "y" ) );
88
89 // Only if mix is specified, otherwise a producer may set the mix
90 if ( mlt_properties_get( properties, "mix" ) != NULL )
91 {
92 // A negative means dissolve
93 if ( mlt_properties_get_double( properties, "mix" ) < 0 )
94 {
95 // Determine the time position of this frame in the transition duration
96 mlt_position in = mlt_transition_get_in( this );
97 mlt_position out = mlt_transition_get_out( this );
98 mlt_position time = mlt_frame_get_position( b_frame );
99 double mix = ( double )( time - in ) / ( double )( out - in + 1 );
100 mlt_properties_set_double( b_props, "image.mix", mix );
101 }
102 else
103 mlt_properties_set_double( b_props, "image.mix", mlt_properties_get_double( properties, "mix" ) );
104 }
105
106 mlt_frame_push_get_image( a_frame, transition_get_image );
107 mlt_frame_push_frame( a_frame, b_frame );
108
109 return a_frame;
110 }
111
112 /** Constructor for the filter.
113 */
114
115 mlt_transition transition_composite_init( char *arg )
116 {
117 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
118 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
119 {
120 this->process = composite_process;
121 if ( arg != NULL )
122 mlt_properties_set_double( mlt_transition_properties( this ), "mix", atof( arg ) );
123 }
124 return this;
125 }
126