1 /*
2 * producer_consumer.c -- produce as a consumer of an encapsulated producer
3 * Copyright (C) 2008-2020 Meltytech, LLC
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <framework/mlt.h>
21
22 #define CONSUMER_PROPERTIES_PREFIX "consumer."
23 #define PRODUCER_PROPERTIES_PREFIX "producer."
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29
30 struct context_s {
31 mlt_producer self;
32 mlt_producer producer;
33 mlt_consumer consumer;
34 mlt_profile profile;
35 int64_t audio_counter;
36 mlt_position audio_position;
37 };
38 typedef struct context_s *context;
39
40
get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)41 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
42 {
43 context cx = mlt_frame_pop_service( frame );
44 mlt_frame nested_frame = mlt_frame_pop_service( frame );
45
46 *width = cx->profile->width;
47 *height = cx->profile->height;
48
49 int result = mlt_frame_get_image( nested_frame, image, format, width, height, writable );
50
51 // Allocate the image
52 int size = mlt_image_format_size( *format, *width, *height, NULL );
53 uint8_t *new_image = mlt_pool_alloc( size );
54
55 // Update the frame
56 mlt_properties properties = mlt_frame_properties( frame );
57 mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
58 memcpy( new_image, *image, size );
59 mlt_properties_set( properties, "progressive", mlt_properties_get( MLT_FRAME_PROPERTIES(nested_frame), "progressive" ) );
60 *image = new_image;
61
62 // Copy the alpha channel
63 uint8_t *alpha = mlt_properties_get_data( MLT_FRAME_PROPERTIES( nested_frame ), "alpha", &size );
64 if ( alpha && size > 0 )
65 {
66 new_image = mlt_pool_alloc( size );
67 memcpy( new_image, alpha, size );
68 mlt_frame_set_alpha( frame, new_image, size, mlt_pool_release );
69 }
70
71 return result;
72 }
73
get_audio(mlt_frame frame,void ** buffer,mlt_audio_format * format,int * frequency,int * channels,int * samples)74 static int get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
75 {
76 context cx = mlt_frame_pop_audio( frame );
77 mlt_frame nested_frame = mlt_frame_pop_audio( frame );
78 int result = 0;
79
80 // if not repeating last frame
81 if ( mlt_frame_get_position( nested_frame ) != cx->audio_position )
82 {
83 double fps = mlt_profile_fps( cx->profile );
84 if ( mlt_producer_get_fps( cx->self ) < fps ) {
85 fps = mlt_producer_get_fps( cx->self );
86 mlt_properties_set_double( MLT_FRAME_PROPERTIES(nested_frame), "producer_consumer_fps", fps );
87 }
88 *samples = mlt_audio_calculate_frame_samples( fps, *frequency, cx->audio_counter++ );
89 result = mlt_frame_get_audio( nested_frame, buffer, format, frequency, channels, samples );
90 int size = mlt_audio_format_size( *format, *samples, *channels );
91 int16_t *new_buffer = mlt_pool_alloc( size );
92
93 mlt_frame_set_audio( frame, new_buffer, *format, size, mlt_pool_release );
94 memcpy( new_buffer, *buffer, size );
95 *buffer = new_buffer;
96 cx->audio_position = mlt_frame_get_position( nested_frame );
97 }
98 else
99 {
100 // otherwise return no samples
101 *samples = 0;
102 }
103
104 return result;
105 }
106
property_changed(mlt_properties owner,mlt_consumer self,char * name)107 static void property_changed( mlt_properties owner, mlt_consumer self, char *name )
108 {
109 mlt_properties properties = MLT_PRODUCER_PROPERTIES(self);
110 context cx = mlt_properties_get_data( properties, "context", NULL );
111
112 if ( !cx )
113 return;
114
115 if ( name == strstr( name, CONSUMER_PROPERTIES_PREFIX ) )
116 mlt_properties_set(MLT_CONSUMER_PROPERTIES( cx->consumer ), name + strlen( CONSUMER_PROPERTIES_PREFIX ),
117 mlt_properties_get( properties, name ));
118
119 if ( name == strstr( name, PRODUCER_PROPERTIES_PREFIX ) )
120 mlt_properties_set(MLT_PRODUCER_PROPERTIES( cx->producer ), name + strlen( PRODUCER_PROPERTIES_PREFIX ),
121 mlt_properties_get( properties, name ));
122 }
123
get_frame(mlt_producer self,mlt_frame_ptr frame,int index)124 static int get_frame( mlt_producer self, mlt_frame_ptr frame, int index )
125 {
126 mlt_properties properties = MLT_PRODUCER_PROPERTIES(self);
127 context cx = mlt_properties_get_data( properties, "context", NULL );
128
129 if ( !cx )
130 {
131 // Allocate and initialize our context
132 cx = mlt_pool_alloc( sizeof( struct context_s ) );
133 memset( cx, 0, sizeof( *cx ) );
134 mlt_properties_set_data( properties, "context", cx, 0, mlt_pool_release, NULL );
135 cx->self = self;
136 char *profile_name = mlt_properties_get( properties, "profile" );
137 if ( !profile_name )
138 profile_name = mlt_properties_get( properties, "mlt_profile" );
139 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self ) );
140
141 if ( profile_name )
142 {
143 cx->profile = mlt_profile_init( profile_name );
144 cx->profile->is_explicit = 1;
145 }
146 else
147 {
148 cx->profile = mlt_profile_clone( profile );
149 cx->profile->is_explicit = 0;
150 }
151
152 // Encapsulate a real producer for the resource
153 cx->producer = mlt_factory_producer( cx->profile, NULL,
154 mlt_properties_get( properties, "resource" ) );
155 if ( ( profile_name && !strcmp( profile_name, "auto" ) ) ||
156 mlt_properties_get_int( properties, "autoprofile" ) )
157 {
158 mlt_profile_from_producer( cx->profile, cx->producer );
159 mlt_producer_close( cx->producer );
160 cx->producer = mlt_factory_producer( cx->profile, NULL, mlt_properties_get( properties, "resource" ) );
161 }
162
163 // Since we control the seeking, prevent it from seeking on its own
164 mlt_producer_set_speed( cx->producer, 0 );
165 cx->audio_position = -1;
166
167 // We will encapsulate a consumer
168 cx->consumer = mlt_consumer_new( cx->profile );
169 // Do not use _pass_list on real_time so that it defaults to 0 in the absence of
170 // an explicit real_time property.
171 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( cx->consumer ), "real_time",
172 mlt_properties_get_int( properties, "real_time" ) );
173 mlt_properties_pass_list( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties,
174 "buffer, prefill, deinterlace_method, rescale" );
175
176 mlt_properties_pass( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties, CONSUMER_PROPERTIES_PREFIX );
177 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( cx->producer ), properties, PRODUCER_PROPERTIES_PREFIX );
178 mlt_events_listen( properties, self, "property-changed", ( mlt_listener )property_changed );
179
180 // Connect it all together
181 mlt_consumer_connect( cx->consumer, MLT_PRODUCER_SERVICE( cx->producer ) );
182 mlt_consumer_start( cx->consumer );
183 }
184
185 // Generate a frame
186 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( self ) );
187 if ( *frame )
188 {
189 // Seek the producer to the correct place
190 // Calculate our positions
191 double actual_position = (double) mlt_producer_frame( self );
192 if ( mlt_producer_get_speed( self ) != 0 )
193 actual_position *= mlt_producer_get_speed( self );
194 mlt_position need_first = floor( actual_position );
195 mlt_producer_seek( cx->producer,
196 lrint( need_first * mlt_profile_fps( cx->profile ) / mlt_producer_get_fps( self ) ) );
197
198 // Get the nested frame
199 mlt_frame nested_frame = mlt_consumer_rt_frame( cx->consumer );
200
201 // Stack the producer and our methods on the nested frame
202 mlt_frame_push_service( *frame, nested_frame );
203 mlt_frame_push_service( *frame, cx );
204 mlt_frame_push_get_image( *frame, get_image );
205 mlt_frame_push_audio( *frame, nested_frame );
206 mlt_frame_push_audio( *frame, cx );
207 mlt_frame_push_audio( *frame, get_audio );
208
209 // Give the returned frame temporal identity
210 mlt_frame_set_position( *frame, mlt_producer_position( self ) );
211
212 // Store the nested frame on the produced frame for destruction
213 mlt_properties frame_props = MLT_FRAME_PROPERTIES( *frame );
214 mlt_properties_set_data( frame_props, "_producer_consumer.frame", nested_frame, 0, (mlt_destructor) mlt_frame_close, NULL );
215
216 // Inform the normalizers about our video properties
217 mlt_properties_set_double( frame_props, "aspect_ratio", mlt_profile_sar( cx->profile ) );
218 mlt_properties_set_int( frame_props, "width", cx->profile->width );
219 mlt_properties_set_int( frame_props, "height", cx->profile->height );
220 mlt_properties_set_int( frame_props, "meta.media.width", cx->profile->width );
221 mlt_properties_set_int( frame_props, "meta.media.height", cx->profile->height );
222 mlt_properties_set_int( frame_props, "progressive", cx->profile->progressive );
223 }
224
225 // Calculate the next timecode
226 mlt_producer_prepare_next( self );
227
228 return 0;
229 }
230
producer_close(mlt_producer self)231 static void producer_close( mlt_producer self )
232 {
233 context cx = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( self ), "context", NULL );
234
235 // Shut down all the encapsulated services
236 if ( cx )
237 {
238 mlt_consumer_stop( cx->consumer );
239 mlt_consumer_close( cx->consumer );
240 mlt_producer_close( cx->producer );
241 mlt_profile_close( cx->profile );
242 }
243
244 self->close = NULL;
245 mlt_producer_close( self );
246 free( self );
247 }
248
producer_consumer_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)249 mlt_producer producer_consumer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
250 {
251 mlt_producer self = mlt_producer_new( profile );
252
253 // Encapsulate the real producer
254 mlt_profile temp_profile = mlt_profile_clone( profile );
255 temp_profile->is_explicit = 0;
256 mlt_producer real_producer = mlt_factory_producer( temp_profile, NULL, arg );
257
258 if ( self && real_producer )
259 {
260 // Override some producer methods
261 self->close = ( mlt_destructor )producer_close;
262 self->get_frame = get_frame;
263
264 // Get the properties of this producer
265 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
266 mlt_properties_set( properties, "resource", arg );
267 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "out, length" );
268
269 // Done with the producer - will re-open later when we have the profile property
270 mlt_producer_close( real_producer );
271 }
272 else
273 {
274 if ( self )
275 mlt_producer_close( self );
276 if ( real_producer )
277 mlt_producer_close( real_producer );
278
279 self = NULL;
280 }
281 mlt_profile_close( temp_profile );
282 return self;
283 }
284