]> git.sesse.net Git - mlt/blob - src/modules/core/producer_consumer.c
ba61f33e6723db0c28d21adea0ca8261e9f84c3a
[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         int is_close_profile;
34 };
35 typedef struct context_s *context; 
36
37
38 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
39 {
40         context cx = mlt_frame_pop_service( frame );
41         mlt_frame nested_frame = mlt_frame_pop_service( frame );
42
43         *width = cx->profile->width;
44         *height = cx->profile->height;
45
46         int result = mlt_frame_get_image( nested_frame, image, format, width, height, writable );
47
48         // Allocate the image
49         int size = *width * *height * ( *format == mlt_image_yuv422 ? 2 : *format == mlt_image_rgb24 ? 3 :
50                 ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) ? 4 : ( 3 / 2 ) );
51         uint8_t *new_image = mlt_pool_alloc( size );
52
53         // Update the frame
54         mlt_properties properties = mlt_frame_properties( frame );
55         mlt_properties_set_data( properties, "image", new_image, size, mlt_pool_release, NULL );
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_properties_set_data( properties, "alpha", new_image, size, mlt_pool_release, NULL );
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         mlt_frame nested_frame = mlt_frame_pop_audio( frame );
75         int result = mlt_frame_get_audio( nested_frame, buffer, format, frequency, channels, samples );
76         int size = *channels * *samples;
77
78         switch ( *format )
79         {
80                 case mlt_audio_s16:
81                         size *= sizeof( int16_t );
82                         break;
83                 case mlt_audio_s32:
84                         size *= sizeof( int32_t );
85                 case mlt_audio_float:
86                         size *= sizeof( float );
87                 default:
88                         mlt_log_error( NULL, "[producer consumer] Invalid audio format\n" );
89         }
90         int16_t *new_buffer = mlt_pool_alloc( size );
91         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "audio", new_buffer, size, mlt_pool_release, NULL );
92         memcpy( new_buffer, *buffer, size );
93         *buffer = new_buffer;
94
95         return result;
96 }
97
98 static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
99 {
100         mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
101         context cx = mlt_properties_get_data( properties, "context", NULL );
102
103         if ( !cx )
104         {
105                 // Allocate and initialize our context
106                 cx = mlt_pool_alloc( sizeof( struct context_s ) );
107                 mlt_properties_set_data( properties, "context", cx, 0, mlt_pool_release, NULL );
108                 cx->this = this;
109                 char *profile_name = mlt_properties_get( properties, "profile" );
110                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
111
112                 if ( profile_name )
113                 {
114                         cx->profile = mlt_profile_init( profile_name );
115                         cx->is_close_profile = 1;
116                         cx->profile->is_explicit = 1;
117                 }
118                 else
119                 {
120                         cx->profile = profile;
121                         cx->is_close_profile = 0;
122                         cx->profile->is_explicit = 0;
123                 }
124
125                 // For now, we must conform the nested network's frame rate to the parent network's
126                 // framerate.
127                 cx->profile->frame_rate_num = profile->frame_rate_num;
128                 cx->profile->frame_rate_den = profile->frame_rate_den;
129
130                 // Encapsulate a real producer for the resource
131                 cx->producer = mlt_factory_producer( cx->profile, NULL,
132                         mlt_properties_get( properties, "resource" ) );
133                 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( cx->producer ),
134                         "out, length" );
135
136                 // Since we control the seeking, prevent it from seeking on its own
137                 mlt_producer_set_speed( cx->producer, 0 );
138
139                 // We will encapsulate a consumer
140                 cx->consumer = mlt_consumer_new( cx->profile );
141                 // Do not use _pass_list on real_time so that it defaults to 0 in the absence of
142                 // an explicit real_time property.
143                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( cx->consumer ), "real_time",
144                         mlt_properties_get_int( properties, "real_time" ) );
145                 mlt_properties_pass_list( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties,
146                         "buffer, prefill, deinterlace_method, rescale" );
147         
148                 // Connect it all together
149                 mlt_consumer_connect( cx->consumer, MLT_PRODUCER_SERVICE( cx->producer ) );
150                 mlt_consumer_start( cx->consumer );
151         }
152
153         // Generate a frame
154         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
155         if ( frame )
156         {
157                 // Our "in" needs to be the same, keep it so
158                 mlt_properties_pass_list( MLT_PRODUCER_PROPERTIES( cx->producer ), properties, "in, out" );
159
160                 // Seek the producer to the correct place
161                 // Calculate our positions
162                 double actual_position = (double)mlt_producer_position( this );
163                 if ( mlt_producer_get_speed( this ) != 0 )
164                         actual_position *= mlt_producer_get_speed( this );
165                 mlt_position need_first = floor( actual_position );
166                 mlt_producer_seek( cx->producer, need_first );
167                 
168                 // Get the nested frame
169                 mlt_frame nested_frame = mlt_consumer_rt_frame( cx->consumer );
170
171                 // Stack the producer and our methods on the nested frame
172                 mlt_frame_push_service( *frame, nested_frame );
173                 mlt_frame_push_service( *frame, cx );
174                 mlt_frame_push_get_image( *frame, get_image );
175                 mlt_frame_push_audio( *frame, nested_frame );
176                 mlt_frame_push_audio( *frame, get_audio );
177                 
178                 // Give the returned frame temporal identity
179                 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
180                 
181                 // Store the nested frame on the produced frame for destruction
182                 mlt_properties frame_props = MLT_FRAME_PROPERTIES( *frame );
183                 mlt_properties_set_data( frame_props, "_producer_consumer.frame", nested_frame, 0, (mlt_destructor) mlt_frame_close, NULL );
184
185                 // Inform the normalizers about our video properties
186                 mlt_properties_set_double( frame_props, "aspect_ratio", mlt_profile_sar( cx->profile ) );
187                 mlt_properties_set_int( frame_props, "width", cx->profile->width );
188                 mlt_properties_set_int( frame_props, "height", cx->profile->height );
189                 mlt_properties_set_int( frame_props, "real_width", cx->profile->width );
190                 mlt_properties_set_int( frame_props, "real_height", cx->profile->height );
191                 mlt_properties_set_int( frame_props, "progressive", cx->profile->progressive );
192         }
193
194         // Calculate the next timecode
195         mlt_producer_prepare_next( this );
196
197         return 0;
198 }
199
200 static void producer_close( mlt_producer this )
201 {
202         context cx = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( this ), "context", NULL );
203         
204         // Shut down all the encapsulated services
205         if ( cx )
206         {
207                 mlt_consumer_stop( cx->consumer );
208                 mlt_consumer_close( cx->consumer );
209                 mlt_producer_close( cx->producer );
210                 if ( cx->is_close_profile )
211                         mlt_profile_close( cx->profile );
212         }
213         
214         this->close = NULL;
215         mlt_producer_close( this );
216         free( this );
217 }
218
219 mlt_producer producer_consumer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
220 {
221         mlt_producer this = mlt_producer_new( profile );
222
223         // Encapsulate the real producer
224         mlt_profile temp_profile = mlt_profile_init( NULL );
225         mlt_producer real_producer = mlt_factory_producer( temp_profile, NULL, arg );
226
227         if ( this && real_producer )
228         {
229                 // Override some producer methods
230                 this->close = ( mlt_destructor )producer_close;
231                 this->get_frame = get_frame;
232                 
233                 // Get the properties of this producer
234                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
235                 mlt_properties_set( properties, "resource", arg );
236                 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "out, length" );
237
238                 // Done with the producer - will re-open later when we have the profile property
239                 mlt_producer_close( real_producer );
240         }
241         else
242         {
243                 if ( this )
244                         mlt_producer_close( this );
245                 if ( real_producer )
246                         mlt_producer_close( real_producer );
247
248                 this = NULL;
249         }
250         mlt_profile_close( temp_profile );
251         return this;
252 }