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