1 /*
2  * producer_consumer.c -- produce as a consumer of an encapsulated producer
3  * Copyright (C) 2008-2021 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,mlt_event_data event_data)107 static void property_changed( mlt_properties owner, mlt_consumer self, mlt_event_data event_data )
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 	const char *name = mlt_event_data_to_string(event_data);
116 	if ( name && name == strstr( name, CONSUMER_PROPERTIES_PREFIX ) )
117 		mlt_properties_set(MLT_CONSUMER_PROPERTIES( cx->consumer ), name + strlen( CONSUMER_PROPERTIES_PREFIX ),
118 			mlt_properties_get( properties, name ));
119 
120 	if ( name && name == strstr( name, PRODUCER_PROPERTIES_PREFIX ) )
121 		mlt_properties_set(MLT_PRODUCER_PROPERTIES( cx->producer ), name + strlen( PRODUCER_PROPERTIES_PREFIX ),
122 			mlt_properties_get( properties, name ));
123 }
124 
get_frame(mlt_producer self,mlt_frame_ptr frame,int index)125 static int get_frame( mlt_producer self, mlt_frame_ptr frame, int index )
126 {
127 	mlt_properties properties = MLT_PRODUCER_PROPERTIES(self);
128 	context cx = mlt_properties_get_data( properties, "context", NULL );
129 
130 	if ( !cx )
131 	{
132 		// Allocate and initialize our context
133 		cx = mlt_pool_alloc( sizeof( struct context_s ) );
134 		memset( cx, 0, sizeof( *cx ) );
135 		mlt_properties_set_data( properties, "context", cx, 0, mlt_pool_release, NULL );
136 		cx->self = self;
137 		char *profile_name = mlt_properties_get( properties, "profile" );
138 		if ( !profile_name )
139 			profile_name = mlt_properties_get( properties, "mlt_profile" );
140 		mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self ) );
141 
142 		if ( profile_name )
143 		{
144 			cx->profile = mlt_profile_init( profile_name );
145 			cx->profile->is_explicit = 1;
146 		}
147 		else
148 		{
149 			cx->profile = mlt_profile_clone( profile );
150 			cx->profile->is_explicit = 0;
151 		}
152 
153 		// Encapsulate a real producer for the resource
154 		cx->producer = mlt_factory_producer( cx->profile, NULL,
155 			mlt_properties_get( properties, "resource" ) );
156 		if ( ( profile_name && !strcmp( profile_name, "auto" ) ) ||
157 			mlt_properties_get_int( properties, "autoprofile" ) )
158 		{
159 			mlt_profile_from_producer( cx->profile, cx->producer );
160 			mlt_producer_close( cx->producer );
161 			cx->producer = mlt_factory_producer( cx->profile, NULL,	mlt_properties_get( properties, "resource" ) );
162 		}
163 
164 		// Since we control the seeking, prevent it from seeking on its own
165 		mlt_producer_set_speed( cx->producer, 0 );
166 		cx->audio_position = -1;
167 
168 		// We will encapsulate a consumer
169 		cx->consumer = mlt_consumer_new( cx->profile );
170 		// Do not use _pass_list on real_time so that it defaults to 0 in the absence of
171 		// an explicit real_time property.
172 		mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( cx->consumer ), "real_time",
173 			mlt_properties_get_int( properties, "real_time" ) );
174 		mlt_properties_pass_list( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties,
175 			"buffer, prefill, deinterlace_method, rescale" );
176 
177 		mlt_properties_pass( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties, CONSUMER_PROPERTIES_PREFIX );
178 		mlt_properties_pass( MLT_PRODUCER_PROPERTIES( cx->producer ), properties, PRODUCER_PROPERTIES_PREFIX );
179 		mlt_events_listen( properties, self, "property-changed", ( mlt_listener )property_changed );
180 
181 		// Connect it all together
182 		mlt_consumer_connect( cx->consumer, MLT_PRODUCER_SERVICE( cx->producer ) );
183 		mlt_consumer_start( cx->consumer );
184 	}
185 
186 	// Generate a frame
187 	*frame = mlt_frame_init( MLT_PRODUCER_SERVICE( self ) );
188 	if ( *frame )
189 	{
190 		// Seek the producer to the correct place
191 		// Calculate our positions
192 		double actual_position = (double) mlt_producer_frame( self );
193 		if ( mlt_producer_get_speed( self ) != 0 )
194 			actual_position *= mlt_producer_get_speed( self );
195 		mlt_position need_first = floor( actual_position );
196 		mlt_producer_seek( cx->producer,
197 			lrint( need_first * mlt_profile_fps( cx->profile ) / mlt_producer_get_fps( self ) ) );
198 
199 		// Get the nested frame
200 		mlt_frame nested_frame = mlt_consumer_rt_frame( cx->consumer );
201 
202 		// Stack the producer and our methods on the nested frame
203 		mlt_frame_push_service( *frame, nested_frame );
204 		mlt_frame_push_service( *frame, cx );
205 		mlt_frame_push_get_image( *frame, get_image );
206 		mlt_frame_push_audio( *frame, nested_frame );
207 		mlt_frame_push_audio( *frame, cx );
208 		mlt_frame_push_audio( *frame, get_audio );
209 
210 		// Give the returned frame temporal identity
211 		mlt_frame_set_position( *frame, mlt_producer_position( self ) );
212 
213 		// Store the nested frame on the produced frame for destruction
214 		mlt_properties frame_props = MLT_FRAME_PROPERTIES( *frame );
215 		mlt_properties_set_data( frame_props, "_producer_consumer.frame", nested_frame, 0, (mlt_destructor) mlt_frame_close, NULL );
216 
217 		// Inform the normalizers about our video properties
218 		mlt_properties_set_double( frame_props, "aspect_ratio", mlt_profile_sar( cx->profile ) );
219 		mlt_properties_set_int( frame_props, "width", cx->profile->width );
220 		mlt_properties_set_int( frame_props, "height", cx->profile->height );
221 		mlt_properties_set_int( frame_props, "meta.media.width", cx->profile->width );
222 		mlt_properties_set_int( frame_props, "meta.media.height", cx->profile->height );
223 		mlt_properties_set_int( frame_props, "progressive", cx->profile->progressive );
224 	}
225 
226 	// Calculate the next timecode
227 	mlt_producer_prepare_next( self );
228 
229 	return 0;
230 }
231 
producer_close(mlt_producer self)232 static void producer_close( mlt_producer self )
233 {
234 	context cx = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( self ), "context", NULL );
235 
236 	// Shut down all the encapsulated services
237 	if ( cx )
238 	{
239 		mlt_consumer_stop( cx->consumer );
240 		mlt_consumer_close( cx->consumer );
241 		mlt_producer_close( cx->producer );
242 		mlt_profile_close( cx->profile );
243 	}
244 
245 	self->close = NULL;
246 	mlt_producer_close( self );
247 	free( self );
248 }
249 
producer_consumer_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)250 mlt_producer producer_consumer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
251 {
252 	mlt_producer self = mlt_producer_new( profile );
253 
254 	// Encapsulate the real producer
255 	mlt_profile temp_profile = mlt_profile_clone( profile );
256 	temp_profile->is_explicit = 0;
257 	mlt_producer real_producer = mlt_factory_producer( temp_profile, NULL, arg );
258 
259 	if ( self && real_producer )
260 	{
261 		// Override some producer methods
262 		self->close = ( mlt_destructor )producer_close;
263 		self->get_frame = get_frame;
264 
265 		// Get the properties of this producer
266 		mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
267 		mlt_properties_set( properties, "resource", arg );
268 		mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "out, length" );
269 
270 		// Done with the producer - will re-open later when we have the profile property
271 		mlt_producer_close( real_producer );
272 	}
273 	else
274 	{
275 		if ( self )
276 			mlt_producer_close( self );
277 		if ( real_producer )
278 			mlt_producer_close( real_producer );
279 
280 		self = NULL;
281 	}
282 	mlt_profile_close( temp_profile );
283 	return self;
284 }
285