framework: remove global profile, rather share one mlt_profile across a service netwo...
[melted] / src / modules / core / transition_mix.c
1 /*
2 * transition_mix.c -- mix two audio streams
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <framework/mlt_transition.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27
28 /** Get the audio.
29 */
30
31 static int transition_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
32 {
33 // Get the b frame from the stack
34 mlt_frame b_frame = mlt_frame_pop_audio( frame );
35
36 // Get the effect
37 mlt_transition effect = mlt_frame_pop_audio( frame );
38
39 // Get the properties of the b frame
40 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
41
42 if ( mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( effect ), "combine" ) == 0 )
43 {
44 double mix_start = 0.5, mix_end = 0.5;
45 if ( mlt_properties_get( b_props, "audio.previous_mix" ) != NULL )
46 mix_start = mlt_properties_get_double( b_props, "audio.previous_mix" );
47 if ( mlt_properties_get( b_props, "audio.mix" ) != NULL )
48 mix_end = mlt_properties_get_double( b_props, "audio.mix" );
49 if ( mlt_properties_get_int( b_props, "audio.reverse" ) )
50 {
51 mix_start = 1 - mix_start;
52 mix_end = 1 - mix_end;
53 }
54
55 mlt_frame_mix_audio( frame, b_frame, mix_start, mix_end, buffer, format, frequency, channels, samples );
56 }
57 else
58 {
59 mlt_frame_combine_audio( frame, b_frame, buffer, format, frequency, channels, samples );
60 }
61
62 return 0;
63 }
64
65
66 /** Mix transition processing.
67 */
68
69 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
70 {
71 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
72 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
73
74 // Only if mix is specified, otherwise a producer may set the mix
75 if ( mlt_properties_get( properties, "start" ) != NULL )
76 {
77 // Determine the time position of this frame in the transition duration
78 mlt_properties props = mlt_properties_get_data( MLT_FRAME_PROPERTIES( b_frame ), "_producer", NULL );
79 int always_active = mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( this ), "always_active" );
80 mlt_position in = !always_active ? mlt_transition_get_in( this ) : mlt_properties_get_int( props, "in" );
81 mlt_position out = !always_active ? mlt_transition_get_out( this ) : mlt_properties_get_int( props, "out" );
82 int length = mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( this ), "length" );
83 mlt_position time = !always_active ? mlt_frame_get_position( b_frame ) : mlt_properties_get_int( props, "_frame" );
84 double mix = ( double )( time - in ) / ( double )( out - in + 1 );
85
86 // TODO: Check the logic here - shouldn't we be computing current and next mixing levels in all cases?
87 if ( length == 0 )
88 {
89 // If there is an end mix level adjust mix to the range
90 if ( mlt_properties_get( properties, "end" ) != NULL )
91 {
92 double start = mlt_properties_get_double( properties, "start" );
93 double end = mlt_properties_get_double( properties, "end" );
94 mix = start + ( end - start ) * mix;
95 }
96 // A negative means total crossfade (uses position)
97 else if ( mlt_properties_get_double( properties, "start" ) >= 0 )
98 {
99 // Otherwise, start/constructor is a constant mix level
100 mix = mlt_properties_get_double( properties, "start" );
101 }
102
103 // Finally, set the mix property on the frame
104 mlt_properties_set_double( b_props, "audio.mix", mix );
105
106 // Initialise transition previous mix value to prevent an inadvertant jump from 0
107 if ( mlt_properties_get( properties, "previous_mix" ) == NULL )
108 mlt_properties_set_double( properties, "previous_mix", mlt_properties_get_double( b_props, "audio.mix" ) );
109
110 // Tell b frame what the previous mix level was
111 mlt_properties_set_double( b_props, "audio.previous_mix", mlt_properties_get_double( properties, "previous_mix" ) );
112
113 // Save the current mix level for the next iteration
114 mlt_properties_set_double( properties, "previous_mix", mlt_properties_get_double( b_props, "audio.mix" ) );
115
116 mlt_properties_set_double( b_props, "audio.reverse", mlt_properties_get_double( properties, "reverse" ) );
117 }
118 else
119 {
120 double level = mlt_properties_get_double( properties, "start" );
121 double mix_start = level;
122 double mix_end = mix_start;
123 double mix_increment = 1.0 / length;
124 if ( time - in < length )
125 {
126 mix_start = mix_start * ( ( double )( time - in ) / length );
127 mix_end = mix_start + mix_increment;
128 }
129 else if ( time > out - length )
130 {
131 mix_end = mix_start * ( ( double )( out - time - in ) / length );
132 mix_start = mix_end - mix_increment;
133 }
134
135 mix_start = mix_start < 0 ? 0 : mix_start > level ? level : mix_start;
136 mix_end = mix_end < 0 ? 0 : mix_end > level ? level : mix_end;
137 mlt_properties_set_double( b_props, "audio.previous_mix", mix_start );
138 mlt_properties_set_double( b_props, "audio.mix", mix_end );
139 }
140 }
141
142 // Override the get_audio method
143 mlt_frame_push_audio( a_frame, this );
144 mlt_frame_push_audio( a_frame, b_frame );
145 mlt_frame_push_audio( a_frame, transition_get_audio );
146
147 return a_frame;
148 }
149
150 /** Constructor for the transition.
151 */
152
153 mlt_transition transition_mix_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
154 {
155 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
156 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
157 {
158 this->process = transition_process;
159 if ( arg != NULL )
160 mlt_properties_set_double( MLT_TRANSITION_PROPERTIES( this ), "start", atof( arg ) );
161 // Inform apps and framework that this is an audio only transition
162 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( this ), "_transition_type", 2 );
163 }
164 return this;
165 }
166