]> git.sesse.net Git - mlt/blob - src/modules/core/producer_consumer.c
Fix reading binary files on Windows.
[mlt] / src / modules / core / producer_consumer.c
1 /*
2  * producer_consumer.c -- produce as a consumer of an encapsulated producer
3  * Copyright (C) 2008 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.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27
28 struct context_s {
29         mlt_producer this;
30         mlt_producer producer;
31         mlt_consumer consumer;
32         mlt_profile profile;
33         int64_t audio_counter;
34         mlt_position audio_position;
35 };
36 typedef struct context_s *context; 
37
38
39 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
40 {
41         context cx = mlt_frame_pop_service( frame );
42         mlt_frame nested_frame = mlt_frame_pop_service( frame );
43
44         *width = cx->profile->width;
45         *height = cx->profile->height;
46
47         int result = mlt_frame_get_image( nested_frame, image, format, width, height, writable );
48
49         // Allocate the image
50         int size = mlt_image_format_size( *format, *width, *height, NULL );
51         uint8_t *new_image = mlt_pool_alloc( size );
52
53         // Update the frame
54         mlt_properties properties = mlt_frame_properties( frame );
55         mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
56         memcpy( new_image, *image, size );
57         mlt_properties_set( properties, "progressive", mlt_properties_get( MLT_FRAME_PROPERTIES(nested_frame), "progressive" ) );
58         *image = new_image;
59         
60         // Copy the alpha channel
61         uint8_t *alpha = mlt_properties_get_data( MLT_FRAME_PROPERTIES( nested_frame ), "alpha", &size );
62         if ( alpha && size > 0 )
63         {
64                 new_image = mlt_pool_alloc( size );
65                 memcpy( new_image, alpha, size );
66                 mlt_frame_set_alpha( frame, new_image, size, mlt_pool_release );
67         }
68
69         return result;
70 }
71
72 static int get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
73 {
74         context cx = mlt_frame_pop_audio( frame );
75         mlt_frame nested_frame = mlt_frame_pop_audio( frame );
76         int result = 0;
77
78         // if not repeating last frame
79         if ( mlt_frame_get_position( nested_frame ) != cx->audio_position )
80         {
81                 double fps = mlt_profile_fps( cx->profile );
82                 if ( mlt_producer_get_fps( cx->this ) < fps )
83                         fps = mlt_producer_get_fps( cx->this );
84                 *samples = mlt_sample_calculator( fps, *frequency, cx->audio_counter++ );
85                 result = mlt_frame_get_audio( nested_frame, buffer, format, frequency, channels, samples );
86                 int size = mlt_audio_format_size( *format, *samples, *channels );
87                 int16_t *new_buffer = mlt_pool_alloc( size );
88
89                 mlt_frame_set_audio( frame, new_buffer, *format, size, mlt_pool_release );
90                 memcpy( new_buffer, *buffer, size );
91                 *buffer = new_buffer;
92                 cx->audio_position = mlt_frame_get_position( nested_frame );
93         }
94         else
95         {
96                 // otherwise return no samples
97                 *samples = 0;
98                 *buffer = NULL;
99         }
100
101         return result;
102 }
103
104 static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
105 {
106         mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
107         context cx = mlt_properties_get_data( properties, "context", NULL );
108
109         if ( !cx )
110         {
111                 // Allocate and initialize our context
112                 cx = mlt_pool_alloc( sizeof( struct context_s ) );
113                 memset( cx, 0, sizeof( *cx ) );
114                 mlt_properties_set_data( properties, "context", cx, 0, mlt_pool_release, NULL );
115                 cx->this = this;
116                 char *profile_name = mlt_properties_get( properties, "profile" );
117                 if ( !profile_name )
118                         profile_name = mlt_properties_get( properties, "mlt_profile" );
119                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
120
121                 if ( profile_name )
122                 {
123                         cx->profile = mlt_profile_init( profile_name );
124                         cx->profile->is_explicit = 1;
125                 }
126                 else
127                 {
128                         cx->profile = mlt_profile_clone( profile );
129                         cx->profile->is_explicit = 0;
130                 }
131
132                 // Encapsulate a real producer for the resource
133                 cx->producer = mlt_factory_producer( cx->profile, NULL,
134                         mlt_properties_get( properties, "resource" ) );
135                 if ( ( profile_name && !strcmp( profile_name, "auto" ) ) ||
136                         mlt_properties_get_int( properties, "autoprofile" ) )
137                 {
138                         mlt_profile_from_producer( cx->profile, cx->producer );
139                         mlt_producer_close( cx->producer );
140                         cx->producer = mlt_factory_producer( cx->profile, NULL, mlt_properties_get( properties, "resource" ) );
141                 }
142
143                 // Since we control the seeking, prevent it from seeking on its own
144                 mlt_producer_set_speed( cx->producer, 0 );
145                 cx->audio_position = -1;
146
147                 // We will encapsulate a consumer
148                 cx->consumer = mlt_consumer_new( cx->profile );
149                 // Do not use _pass_list on real_time so that it defaults to 0 in the absence of
150                 // an explicit real_time property.
151                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( cx->consumer ), "real_time",
152                         mlt_properties_get_int( properties, "real_time" ) );
153                 mlt_properties_pass_list( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties,
154                         "buffer, prefill, deinterlace_method, rescale" );
155         
156                 // Connect it all together
157                 mlt_consumer_connect( cx->consumer, MLT_PRODUCER_SERVICE( cx->producer ) );
158                 mlt_consumer_start( cx->consumer );
159         }
160
161         // Generate a frame
162         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
163         if ( *frame )
164         {
165                 // Seek the producer to the correct place
166                 // Calculate our positions
167                 double actual_position = (double) mlt_producer_frame( this );
168                 if ( mlt_producer_get_speed( this ) != 0 )
169                         actual_position *= mlt_producer_get_speed( this );
170                 mlt_position need_first = floor( actual_position );
171                 mlt_producer_seek( cx->producer,
172                         lrint( need_first * mlt_profile_fps( cx->profile ) / mlt_producer_get_fps( this ) ) );
173
174                 // Get the nested frame
175                 mlt_frame nested_frame = mlt_consumer_rt_frame( cx->consumer );
176
177                 // Stack the producer and our methods on the nested frame
178                 mlt_frame_push_service( *frame, nested_frame );
179                 mlt_frame_push_service( *frame, cx );
180                 mlt_frame_push_get_image( *frame, get_image );
181                 mlt_frame_push_audio( *frame, nested_frame );
182                 mlt_frame_push_audio( *frame, cx );
183                 mlt_frame_push_audio( *frame, get_audio );
184                 
185                 // Give the returned frame temporal identity
186                 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
187                 
188                 // Store the nested frame on the produced frame for destruction
189                 mlt_properties frame_props = MLT_FRAME_PROPERTIES( *frame );
190                 mlt_properties_set_data( frame_props, "_producer_consumer.frame", nested_frame, 0, (mlt_destructor) mlt_frame_close, NULL );
191
192                 // Inform the normalizers about our video properties
193                 mlt_properties_set_double( frame_props, "aspect_ratio", mlt_profile_sar( cx->profile ) );
194                 mlt_properties_set_int( frame_props, "width", cx->profile->width );
195                 mlt_properties_set_int( frame_props, "height", cx->profile->height );
196                 mlt_properties_set_int( frame_props, "meta.media.width", cx->profile->width );
197                 mlt_properties_set_int( frame_props, "meta.media.height", cx->profile->height );
198                 mlt_properties_set_int( frame_props, "progressive", cx->profile->progressive );
199         }
200
201         // Calculate the next timecode
202         mlt_producer_prepare_next( this );
203
204         return 0;
205 }
206
207 static void producer_close( mlt_producer this )
208 {
209         context cx = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( this ), "context", NULL );
210         
211         // Shut down all the encapsulated services
212         if ( cx )
213         {
214                 mlt_consumer_stop( cx->consumer );
215                 mlt_consumer_close( cx->consumer );
216                 mlt_producer_close( cx->producer );
217                 mlt_profile_close( cx->profile );
218         }
219         
220         this->close = NULL;
221         mlt_producer_close( this );
222         free( this );
223 }
224
225 mlt_producer producer_consumer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
226 {
227         mlt_producer this = mlt_producer_new( profile );
228
229         // Encapsulate the real producer
230         mlt_profile temp_profile = mlt_profile_clone( profile );
231         temp_profile->is_explicit = 0;
232         mlt_producer real_producer = mlt_factory_producer( temp_profile, NULL, arg );
233
234         if ( this && real_producer )
235         {
236                 // Override some producer methods
237                 this->close = ( mlt_destructor )producer_close;
238                 this->get_frame = get_frame;
239                 
240                 // Get the properties of this producer
241                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
242                 mlt_properties_set( properties, "resource", arg );
243                 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "out, length" );
244
245                 // Done with the producer - will re-open later when we have the profile property
246                 mlt_producer_close( real_producer );
247         }
248         else
249         {
250                 if ( this )
251                         mlt_producer_close( this );
252                 if ( real_producer )
253                         mlt_producer_close( real_producer );
254
255                 this = NULL;
256         }
257         mlt_profile_close( temp_profile );
258         return this;
259 }